Purch.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\ProcessOrder;
  5. use think\App;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. //采购单相关功能
  10. class Purch extends Base
  11. {
  12. public $noble=[];
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $this->noble=\think\facade\Config::get("noble");
  17. }
  18. public function list(){
  19. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  20. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  21. $where = [['po.is_del', "=", 0], ['po.order_type', '<>', 5]];//不显示类型为5即订单导入生成的采购单
  22. $bkcode = isset($this->post['bk_code']) && $this->post['bk_code']!="" ? trim($this->post['bk_code']):"";
  23. if($bkcode!=""){
  24. $where[]=['po.bkcode',"like", "%{$bkcode}%"];
  25. }
  26. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  27. if($status!==""){
  28. // $where['status'] = $status;
  29. $where[]=['po.status',"=", $status];
  30. }
  31. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo'])
  32. :"";
  33. if($cgdNo!=""){
  34. //$where['cgdNo'] = Db::Raw("like '%{$cgdNo}%'");
  35. $where[]=['po.cgdNo',"like", "%{$cgdNo}%"];
  36. }
  37. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name']):"";
  38. if($apply_name!=""){
  39. // $where['cgder'] =Db::Raw("like '%{$apply_name}%'");
  40. $where[]=['po.cgder',"like", "%{$apply_name}%"];
  41. }
  42. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  43. if($wsm_code!=""){
  44. // $where['wsm_code'] = $wsm_code;
  45. $where[]=['po.wsm_code',"=", $wsm_code];
  46. }
  47. $wsm_in_code = isset($this->post['wsm_in_code']) && $this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']):"";
  48. if($wsm_in_code!=""){
  49. $incode = Db::name("purchease_in")->where(["wsm_in_code"=>Db::raw(" like %{$wsm_in_code}%"),"is_del"=>0])->column("cgdNo");
  50. if(empty($incode)){
  51. return error_show(1004,"未找到有关入库单信息");
  52. }
  53. $where[]=['po.cgdNo',"in", $incode];
  54. }
  55. $good_name = isset($this->post['good_name']) && $this->post['good_name']!="" ? trim($this->post['good_name']):"";
  56. if($good_name!=""){
  57. //$where['good_name'] = Db::raw(" like %{$good_name}%");
  58. $where[]=['po.good_name',"like", "%{$good_name}%"];
  59. }
  60. $good_code = isset($this->post['good_code']) && $this->post['good_code']!="" ? trim($this->post['good_code'])
  61. :"";
  62. if($good_code!=""){
  63. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  64. $where[]=['po.spuCode',"like", "%{$good_code}%"];
  65. }
  66. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo']!="" ? trim($this->post['supplierNo'])
  67. :"";
  68. if($supplierNo!=""){
  69. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  70. $where[]=['po.supplierNo',"like", "%{$supplierNo}%"];
  71. }
  72. $wsm_supplierNo = isset($this->post['wsm_supplierNo']) && $this->post['wsm_supplierNo']!="" ? trim($this->post['wsm_supplierNo'])
  73. :"";
  74. if($wsm_supplierNo!=""){
  75. $supplier = Db::name("supplier")->where(["code"=>$wsm_supplierNo])->find();
  76. if(empty($supplier)){
  77. return error_show(1004,"未找到供应商信息");
  78. }
  79. $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"supplierNo"=>$wsm_supplierNo])->column("wsm_code");
  80. $where[]=['po.wsm_code',"in", $wsmcode];
  81. }
  82. $start = isset($this->post['start']) && $this->post['start']!="" ?$this->post['start']:"";
  83. if($start!=""){
  84. // $where['addtime'] = Db::raw(" >= '{$start}'");
  85. $where[]=['po.addtime',">=", $start];
  86. }
  87. $end = isset($this->post['end']) && $this->post['end']!="" ?$this->post['end']:"";
  88. if($end!=""){
  89. $where[]=['po.addtime',"<=", $end];
  90. }
  91. $last_start = isset($this->post['last_start']) && $this->post['last_start']!="" ?$this->post['last_start']:"";
  92. if($last_start!=""){
  93. //$where['lasttime'] = Db::raw(" >= '{$last_start}'");
  94. $where[]=['po.lasttime',">=", $last_start];
  95. }
  96. $last_end = isset($this->post['last_end']) && $this->post['last_end']!="" ?$this->post['last_end']:"";
  97. if($last_end!=""){
  98. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  99. $where[]=['po.lasttime',"<=", $last_end];
  100. }
  101. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ?$this->post['orderCode']:"";
  102. if($orderCode!=""){
  103. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  104. $where[]=['on.orderCode',"like", '%'.$orderCode.'%'];
  105. }
  106. $role=$this->checkRole();
  107. if(!empty($role['write'])){
  108. $where[]=["po.cgder_id","in",$role['write']];
  109. }
  110. // if(!empty($role['platform']) ){
  111. // $where[]=["po.platform_id","in",$role['platform']];
  112. // }
  113. $count=Db::name("purchease_order")->alias('po')->leftJoin('order_num on','on.cgdNo=po.cgdNo')->where($where)->count();
  114. $total = ceil($count/$size);
  115. $page = $page >= $total ? $total : $page;
  116. $list = Db::name("purchease_order")
  117. ->alias('po')
  118. ->leftJoin('order_num on','on.cgdNo=po.cgdNo')
  119. ->field('po.*,on.orderCode')
  120. ->where($where)
  121. ->page($page,$size)
  122. ->order("po.addtime desc")
  123. ->select()
  124. ->toArray();
  125. // $all_orderCodes = Db::name('order_num')
  126. // ->whereIn('cgdNo',array_column($list,'cgdNo'))
  127. // ->column('orderCode','cgdNo');
  128. $data=[];
  129. foreach ($list as $value){
  130. $value['wsm_name']="";
  131. if($value['wsm_code']!=""){
  132. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  133. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  134. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  135. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  136. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  137. }
  138. if($value['order_type']==3|| $value['order_type']==4){
  139. $goon = Db::name("good_zixun")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
  140. $value["speclist"]=isset($goon['specinfo'])&&$goon['specinfo']!=""? json_decode($goon['specinfo'],true):"";
  141. }else {
  142. $goon =Db::name('good_basic')->where(['spuCode'=>$value['spuCode']])->find();
  143. $spec = Db::name("good_spec")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->select()->toArray();
  144. $speclist=[];
  145. if(!empty($spec)){
  146. foreach ($spec as $val){
  147. $temp=[];
  148. $temp['id']=$val['id'];
  149. $temp['spuCode']=$val['spuCode'];
  150. $temp['spec_id']=$val['spec_id'];
  151. $temp['spec_value_id']=$val['spec_value_id'];
  152. $sp = Db::name("specs")->where(["id"=>$val['spec_id']])->find();
  153. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  154. $spv = Db::name("spec_value")->where(["id"=>$val['spec_value_id']])->find();
  155. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  156. $speclist[]=$temp;
  157. }
  158. }
  159. $value["speclist"]=empty($speclist)?[]:$speclist;
  160. }
  161. //采购单详情字段order_type 为1或者2时,取线上商品库 商品创建人 order_type 为3或者4时 取采购反馈的商品库 商品反馈人
  162. // $value['cgder_id'] = $goon['createrid'];
  163. // $value['cgder'] = $goon['creater'];
  164. $inorder= Db::name("purchease_in")->where(['cgdNo'=>$value['cgdNo'],"is_del"=>0])->select();
  165. $value['child']=empty($inorder)? [] : $inorder;
  166. $value['send_num'] -=$value['th_num'];
  167. $value['total_fee'] =round($value['total_fee']-$value['th_fee'],2);
  168. //补充orderCode
  169. // $value['orderCode']=isset($all_orderCodes[$value['cgdNo']])?$all_orderCodes[$value['cgdNo']]:'';
  170. $data[]=$value;
  171. }
  172. return app_show(0,"获取成功",["list"=>$data ,"count"=>$count]);
  173. }
  174. public function info(){
  175. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  176. if($cgdNo==""){
  177. return error_show(1004,"参数cgdNo 不能为空");
  178. }
  179. //采购单
  180. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  181. if(empty($data)){
  182. return error_show(1004,"未找到数据");
  183. }
  184. //采购退货单
  185. $im = Db::name('purchease_back')->where(['cgdNo'=>$data['cgdNo'],'is_del'=>0])->select();
  186. $var=[];
  187. foreach ($im as $value){
  188. if($data['order_type']==3|| $data['order_type']==4){
  189. $goo = Db::name("good_zixun")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
  190. }else {
  191. $goo =Db::name('good_basic')->where(['spuCode'=>$value['spuCode']])->find();
  192. }
  193. if($goo==false) {
  194. return error_show(1002, "未找到商品数据");
  195. }
  196. $cat= isset($goo['cat_id']) && $goo['cat_id'] !=0 ? made($goo['cat_id']):[];
  197. $value['cant']=$cat;
  198. $var[]=$value;
  199. }
  200. //采购工差单
  201. $dom = Db::name("purchease_diff")->where(['cgdNo'=>$data['cgdNo'],'is_del'=>0])->find();
  202. if($data['order_type']==3|| $data['order_type']==4){
  203. $goon = Db::name("good_zixun")->where(["spuCode"=>$data['spuCode'],"is_del"=>0])->find();
  204. }else {
  205. $goon =Db::name('good_basic')->where(['spuCode'=>$data['spuCode']])->find();
  206. }
  207. if(empty($goon)){
  208. return error_show(1002,"未找到商品数据");
  209. }else{
  210. $goon['exclusive']=isset($goon['is_exclusive'])?makeExcluse($goon['is_exclusive']):"";
  211. $unit =Db::name("unit")->where(["id"=>$goon['good_unit']])->find();
  212. $goon['unit'] = isset($unit['unit'])?$unit['unit']:'';
  213. $spec = Db::name("good_spec")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->select()->toArray();
  214. $speclist=[];
  215. if(!empty($spec)){
  216. foreach ($spec as $value){
  217. $temp=[];
  218. $temp['id']=$value['id'];
  219. $temp['spuCode']=$value['spuCode'];
  220. $temp['spec_id']=$value['spec_id'];
  221. $temp['spec_value_id']=$value['spec_value_id'];
  222. $temp['is_del']=$value['is_del'];
  223. $sp = Db::name("specs")->where(["id"=>$value['spec_id']])->find();
  224. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  225. $spv = Db::name("spec_value")->where(["id"=>$value['spec_value_id']])->find();
  226. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  227. $speclist[]=$temp;
  228. }
  229. }
  230. $goon["speclist"]=empty($speclist)?[]:$speclist;
  231. $proof =Db::name("good_proof")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  232. $goon['proof'] = isset($proof)&&$proof!=false? $proof:[];
  233. $goon['origin_place_cn']="";
  234. $goon['delivery_place_cn']="";
  235. if(isset($goon['delivery_place'])&&$goon['delivery_place']!==""){
  236. $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
  237. list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$goon['delivery_place']);
  238. $goon['delivery_place_cn']=GetAddr(json_encode($place));
  239. }
  240. if(isset($goon['delivery_place'])&&$goon['origin_place']!==""){
  241. $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
  242. list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$goon['origin_place']);
  243. $goon['origin_place_cn']=GetAddr(json_encode($place));
  244. }
  245. if($goon['brand_id']!=0){
  246. $brand=Db::name("brand")->where(["id"=>$goon['brand_id']])->find();
  247. $goon["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
  248. }else{
  249. $goon["brand_name"]="";
  250. $goon["brand_id"]="";
  251. }
  252. $supplier = Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  253. $goon['supplierName'] = isset($supplier['name'])?$supplier['name']:"";
  254. $goon['noble_name']=isset($goon['noble_metal'])&&$goon['noble_metal']!=0?$this->noble[$goon['noble_metal']] :"";
  255. if(isset($goon['companyNo'])&&$goon['companyNo']!=""){
  256. $company = Db::name("business")->where(["companyNo"=>$goon['companyNo']])->find();
  257. }
  258. $goon['company'] = isset($company['company'])?$company['company']:"";
  259. }
  260. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  261. $data['wsm_name']="";
  262. if($data['wsm_code']!=""){
  263. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  264. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  265. ->where(["a.wsm_code"=>$data['wsm_code']])->field("a.name as wsm_name,b.name,b.code,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_code")->find();
  266. $data['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  267. $data['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  268. $data['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  269. $data['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  270. $data['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  271. // $data['wsm_addr'] =isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  272. $data['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  273. $data['wsm_addr'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  274. $data['wsm_addr'].=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  275. $inorder= Db::name("purchease_in")->where(['cgdNo'=>$data['cgdNo'],"is_del"=>0])->select();
  276. $data['child']=empty($inorder)? [] : $inorder;
  277. }
  278. $data['can'] = $int;
  279. $data['goodinfo'] = $goon;
  280. $data['purcheasediff'] = $dom;
  281. $data['send_num'] -=$data['th_num'];
  282. $data['total_fee'] =round($data['total_fee']-$data['th_fee'],2);
  283. if(isset($data['companyNo'])&&$data['companyNo']!=""){
  284. $company = Db::name("business")->where(["companyNo"=>$data['companyNo']])->find();
  285. }
  286. $data['company'] = isset($company['company'])?$company['company']:"";
  287. //$data['info'] = $var;
  288. $data['purcheaseback'] = $var;
  289. //补充orderCode
  290. $data['orderCode'] = Db::name('order_num')
  291. ->where('cgdNo', $data['cgdNo'])
  292. ->value('orderCode', '');
  293. //补充收货地址
  294. $addr = Db::name('order_addr')
  295. ->field('id,addr,addr_code,contactor,mobile')
  296. ->where(['orderCode' => $data['orderCode'], 'is_del' => 0])
  297. ->select()
  298. ->toArray();
  299. if (!empty($addr)) {
  300. foreach ($addr as &$vv){
  301. $temp = explode(',', $vv['addr_code']);
  302. $temp_ = GetAddr(json_encode(['provice_code' => isset($temp[0]) ? $temp[0] : '', 'city_code' => isset($temp[1]) ? $temp[1] : '', 'area_code' => isset($temp[2]) ? $temp[2] : '']));
  303. $vv['addr_code'] = $temp_ . $vv['addr'];
  304. }
  305. }else $addr=[];
  306. $data['addr_info'] = $addr;
  307. //补充客户名称
  308. if (empty($data['orderCode'])) $data['customer'] = [];
  309. else {
  310. $data['customer'] = Db::name('sale')
  311. ->alias('s')
  312. ->field('c.companyNo,c.companyName')
  313. ->leftJoin('customer_info c', 'c.companyNo=s.customer_code AND c.is_del=0')
  314. ->where(['s.orderCode' => $data['orderCode']])
  315. ->find();
  316. }
  317. return app_show(0, "获取成功", $data);
  318. }
  319. public function edit(){
  320. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  321. if($cgdNo==""){
  322. return error_show(1004,"参数cgdNo 不能为空");
  323. }
  324. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  325. if(empty($data)){
  326. return error_show(1004,"未找到数据");
  327. }
  328. $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$this->post['remark'],"action_type"=>"edit"];
  329. $wsm_code = isset($this->post['wsm_code'])&&$this->post['wsm_code']!=""? trim($this->post['wsm_code']):"";
  330. if($wsm_code!=""){
  331. $ware = Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code,"is_del"=>0])->find();
  332. if(empty($ware)){
  333. return error_show(1004,"未找到仓库信息");
  334. }
  335. // $good = Db::name("good_stock")->where(["wsm_code"=>$wsm_code,"good_type_code"=>$data['good_type_code'],"is_del"=>0])->find();
  336. // if(empty($good)){
  337. // return error_show(1004,"未找到仓库下商品信息");
  338. // }
  339. $data['wsm_code'] = $wsm_code;
  340. }
  341. $cgdid = isset($this->post['cgder_id'])&&$this->post['cgder_id']!=""?intval($this->post['cgder_id']):"";
  342. if($cgdid!=''){
  343. $cgduser = GetInfoById($this->post['token'],["id"=>$cgdid]);
  344. if((!empty($cgduser) && $cgduser['code']!=0) ||empty($cgduser) ){
  345. return error_show($cgduser['code'],$cgduser['message']);
  346. }
  347. $userinfo = $cgduser['data'];
  348. $data['cgder_id'] = $cgdid;
  349. $data['cgder'] = $userinfo['nickname'];
  350. }
  351. $good_num = isset($this->post['good_num'])&&$this->post['good_num']!=""?intval($this->post['good_num']):"";
  352. if($good_num!=""){
  353. $data['good_num'] = $good_num;
  354. }
  355. $good_price = isset($this->post['good_price'])&&$this->post['good_price']!=""?$this->post['good_price']:"";
  356. if($good_price!=""){
  357. $data['good_price'] = $good_price;
  358. }
  359. // $total_fee = isset($this->post['total_fee'])&&$this->post['total_fee']!=""?$this->post['total_fee']:"";
  360. // if($total_fee!=""){
  361. // $data['total_fee'] = $total_fee;
  362. // }
  363. $pakge_fee = isset($this->post['pakge_fee'])&&$this->post['pakge_fee']!=""?$this->post['pakge_fee']:"";
  364. if($pakge_fee!=""){
  365. $data['pakge_fee'] = $pakge_fee;
  366. }
  367. $cert_fee = isset($this->post['cert_fee'])&&$this->post['cert_fee']!=""?$this->post['cert_fee']:"";
  368. if($cert_fee!=""){
  369. $data['cert_fee'] = $cert_fee;
  370. }
  371. $open_fee = isset($this->post['open_fee'])&&$this->post['open_fee']!=""?$this->post['open_fee']:"";
  372. if($open_fee!=""){
  373. $data['open_fee'] = $open_fee;
  374. }
  375. $delivery_fee = isset($this->post['delivery_fee'])&&$this->post['delivery_fee']!=""?$this->post['delivery_fee']:"";
  376. if($delivery_fee!=""){
  377. $data['delivery_fee'] = $delivery_fee;
  378. }
  379. $mark_fee = isset($this->post['mark_fee'])&&$this->post['mark_fee']!=""?$this->post['mark_fee']:"";
  380. if($mark_fee!=""){
  381. $data['mark_fee'] = $mark_fee;
  382. }
  383. $teach_fee = isset($this->post['teach_fee'])&&$this->post['teach_fee']!=""?$this->post['teach_fee']:"";
  384. if($teach_fee!=""){
  385. $data['teach_fee'] = $teach_fee;
  386. }
  387. $nake_fee = isset($this->post['nake_fee'])&&$this->post['nake_fee']!=""?$this->post['nake_fee']:"";
  388. if($nake_fee!=""){
  389. $data['nake_fee'] = $nake_fee;
  390. }
  391. $weight = isset($this->post['weight'])&&$this->post['weight']!=""?$this->post['weight']:"";
  392. if($weight!=""){
  393. $data['weight'] = $weight;
  394. }
  395. $diff_weight = isset($this->post['diff_weight'])&&$this->post['diff_weight']!=""?$this->post['diff_weight']:"";
  396. if($diff_weight!=""){
  397. $data['diff_weight'] = $diff_weight;
  398. }
  399. $diff_fee = isset($this->post['diff_fee'])&&$this->post['diff_fee']!=""?$this->post['diff_fee']:"";
  400. if($diff_fee!=""){
  401. $data['diff_fee'] = $diff_fee;
  402. }
  403. $supplierNo = isset($this->post['supplierNo'])&&$this->post['supplierNo']!=""?trim($this->post['supplierNo'])
  404. :"";
  405. if($supplierNo!=""){
  406. $supplier =Db::name("supplier")->where(['code'=>$supplierNo])->find();
  407. if(empty($supplier)){
  408. return error_show(1004,"未找到供应商信息");
  409. }
  410. $data['supplierNo'] = $supplierNo;
  411. $data['supplier_name'] = $supplier['name'];
  412. }
  413. $remark = isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  414. if($remark!=""){
  415. $data['remark'] = $remark;
  416. }
  417. $data['good_price'] = round(($data['pakge_fee']+$data['weight']*$data['gold_price']+$data['nake_fee']+$data['mark_fee']+$data['cert_fee']+$data['open_fee']/$data['good_num']+$data['teach_fee']*$data['weight']+$data['delivery_fee']),2);
  418. $data['total_fee'] = round($data['good_price']*$data['good_num'],2);
  419. $data['updatetime'] =date("Y-m-d H:i:s");
  420. $upd=Db::name("purchease_order")->save($data);
  421. if($upd){
  422. // ActionLog::logAdd($this->post['token'],$order,'cgd',$data['status'],$this->post);
  423. //修改状态,添加待办
  424. ActionLog::logAdd($this->post['token'], $order, "CGD", $data['status'], $this->post);
  425. // ProcessOrder::AddProcess($this->post['token'], [
  426. // "order_type" => 'CGD',
  427. // "order_code" => $cgdNo,//销售单code
  428. // "order_id" => $data['id'],
  429. // "order_status" => $data['status'],"before_status"=>1
  430. // ]);
  431. return app_show(0,"更新成功");
  432. }else{
  433. return error_show(1004,'更新失败');
  434. }
  435. }
  436. //修改采购单
  437. public function editNew()
  438. {
  439. $param = $this->request->only(['cgdNo', 'supplierNo', 'token', 'wsm_code' => ''], 'post', 'trim');
  440. $val = Validate::rule([
  441. 'cgdNo|采购单号' => 'require',
  442. 'supplierNo|供应商编号' => 'require',
  443. 'token' => 'require',
  444. ]);
  445. if (!$val->check($param)) return error_show(1004, $val->getError());
  446. Db::startTrans();
  447. try {
  448. $rs = Db::name('purchease_order')
  449. ->alias('po')
  450. ->field('po.id,po.order_type,po.status,po.cgder_id,po.cgder,po.supplierNo,po.supplier_name,po.wsm_code,wi.name wsm_name')
  451. ->leftJoin('warehouse_info wi', 'wi.wsm_code=po.wsm_code AND wi.is_del=0')
  452. ->where([
  453. 'po.cgdNo' => $param['cgdNo'],
  454. 'po.is_del' => 0,
  455. 'po.status' => 0
  456. ])->find();
  457. if (empty($rs)) throw new Exception('采购单不存在或不允许修改');
  458. $update = ['updatetime' => date('Y-m-d H:i:s')];
  459. //order_type==1备库
  460. if ($rs['order_type'] == 1) {
  461. if (empty($param['wsm_code'])) throw new Exception('备库单请传入仓库编码');
  462. else {
  463. $wsm_rs = Db::name('warehouse_info')
  464. ->field('id,wsm_code,name wsm_name')
  465. ->where([
  466. 'wsm_code' => $param['wsm_code'],
  467. 'is_del' => 0,
  468. ])->find();
  469. if (empty($wsm_rs)) throw new Exception('查不到该仓库的记录');
  470. else $update['wsm_code'] = $wsm_rs['wsm_code'];
  471. }
  472. }
  473. //供应商信息
  474. $supplier_rs = Db::name('supplier')
  475. ->field('id,name supplierName,code supplierNo,person,personid')
  476. ->where(['is_del' => 0, 'code' => $param['supplierNo']])
  477. ->find();
  478. if (empty($supplier_rs)) throw new Exception('查不到该供应商的记录');
  479. else {
  480. $update['cgder_id'] = $supplier_rs['personid'];
  481. $update['cgder'] = $supplier_rs['person'];
  482. $update['supplierNo'] = $supplier_rs['supplierNo'];
  483. $update['supplier_name'] = $supplier_rs['supplierName'];
  484. }
  485. //是否修改内容确定
  486. if (isset($update['wsm_code'])) {
  487. if ($update['wsm_code'] == $rs['wsm_code'] && $update['supplierNo'] == $rs['supplierNo']) throw new Exception('没有修改的内容,不能提交');
  488. } elseif ($update['supplierNo'] == $rs['supplierNo']) throw new Exception('没有修改的内容,不能提交');
  489. if ($update['supplierNo'] == $rs['supplierNo'] && $param['wsm_code'] == $rs['wsm_code']) throw new Exception('没有修改的内容');
  490. //修改
  491. Db::name('purchease_order')
  492. ->where([
  493. 'id' => $rs['id'],
  494. 'is_del' => 0,
  495. 'status' => 0
  496. ])->update($update);
  497. $user = GetUserInfo($param['token']);
  498. $creater_id = isset($user['data']['id']) ? $user['data']['id'] : 0;
  499. $creater = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  500. //添加修改记录
  501. Db::name('purchease_change_supplier_log')
  502. ->insert([
  503. 'cgdNo' => $param['cgdNo'],
  504. 'cgder_id_old' => $rs['cgder_id'],
  505. 'cgder_id' => $update['cgder_id'],
  506. 'cgder_old' => $rs['cgder'],
  507. 'cgder' => $update['cgder'],
  508. 'supplierNo_old' => $rs['supplierNo'],
  509. 'supplierNo' => $update['supplierNo'],
  510. 'supplier_name_old' => $rs['supplier_name'],
  511. 'supplier_name' => $update['supplier_name'],
  512. 'wsm_code_old' => $rs['wsm_code'],
  513. 'wsm_code' => isset($update['wsm_code']) ? $update['wsm_code'] : $rs['wsm_code'],
  514. 'wsm_name_old' => $rs['wsm_name'],
  515. 'wsm_name' => isset($wsm_rs['wsm_name']) ? $wsm_rs['wsm_name'] : $rs['wsm_name'],
  516. 'addtime' => date('Y-m-d H:i:s'),
  517. 'updatetime' => date('Y-m-d H:i:s'),
  518. 'creater_id' => $creater_id,
  519. 'creater' => $creater,
  520. ]);
  521. //记录日志
  522. ActionLog::logAdd(['id' => $creater_id, 'nickname' => $creater], $rs, "CGD", $rs['status'], $param);
  523. // ProcessOrder::AddProcess($this->post['token'], [
  524. // "order_type" => 'CGD',
  525. // "order_code" => $param['cgdNo'],//销售单code
  526. // "order_id" => $rs['id'],
  527. // "order_status" => $rs['status']
  528. // ]);
  529. Db::commit();
  530. return app_show(0, '修改成功');
  531. } catch (Exception $exception) {
  532. Db::rollback();
  533. return error_show(1005, $exception->getMessage());
  534. }
  535. }
  536. public function status()
  537. {
  538. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? $this->post['cgdNo'] : [];
  539. if (empty($cgdNo)) {
  540. return error_show(1004, "参数cgdNo 不能为空");
  541. }
  542. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  543. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  544. if ($status === "") {
  545. return error_show(1004, "参数status 不能为空");
  546. }
  547. if ($status == 0) {
  548. $data = Db::name("purchease_order")
  549. ->field('id,cgdNo')
  550. ->whereIn('cgdNo', $cgdNo)
  551. ->where(["is_del" => 0])
  552. ->where('send_status', '>', 1)
  553. ->find();
  554. if (!empty($data)) {
  555. return error_show(1004, $data['cgdNo'] . "采购单发货中无法取消");
  556. }
  557. }
  558. // if($data['send_status']>1 && $status==0){
  559. // return error_show(1004,"采购单发货中无法取消");
  560. // }
  561. // $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$remark,"action_type"=>"status"];
  562. // $data['status'] = $status;
  563. // $data['remark'] = $remark;
  564. // $data['updatetime'] =date("Y-m-d H:i:s");
  565. $upd = Db::name("purchease_order")
  566. ->whereIn('cgdNo', $cgdNo)
  567. ->save([
  568. 'status' => $status,
  569. 'remark' => $remark,
  570. 'updatetime' => date("Y-m-d H:i:s"),
  571. ]);
  572. if ($upd) {
  573. $process = ["order_code" => implode(',', $cgdNo), "order_id" => 0, "order_status" => $status, "order_type" => 'CGD',"before_status"=>0];
  574. ProcessOrder::AddProcess($this->post['token'], $process);
  575. $order = ["order_code" => implode(',', $cgdNo), "status" => '', "action_remark" => $remark, "action_type" => "status"];
  576. ActionLog::logAdd($this->post['token'], $order, 'CGD', $status, $this->post);
  577. return app_show(0, "更新成功");
  578. } else {
  579. return error_show(1004, '更新失败');
  580. }
  581. }
  582. //创建采购工差单
  583. public function diffcreat(){
  584. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  585. //$cgdNo=makeNo("CG");
  586. $cgdNo = $this->post['cgdNo'] && $this->post['cgdNo'] !=="" ? trim($this->post['cgdNo']) :"";
  587. if($cgdNo==""){
  588. return error_show(1002,"参数good_code不能为空");
  589. }
  590. $cg = Db::name('purchease_order')->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find();
  591. if($cg==""){
  592. return error_show(1002,"未找到采购单数据");
  593. }
  594. if($cg['order_type']==3|| $cg['order_type']==4){
  595. $gd = Db::name("good_zixun")->where(["spuCode"=>$cg['spuCode']])->find();
  596. }else {
  597. $gd =Db::name('good_basic')->where(['spuCode'=>$cg['spuCode']])->find();
  598. }
  599. if($gd==""){
  600. return error_show(1002,"未找到商品数据");
  601. }
  602. $diff_weight = isset($this->post['diff_weight']) && $this->post['diff_weight'] !=="" ? floatval($this->post['diff_weight']) :"";
  603. if($diff_weight===""){
  604. return error_show(1002,"参数diff_weight不能为空");
  605. }
  606. $apply_id =GetUserInfo($token);
  607. if(empty($apply_id)||$apply_id['code']!=0){
  608. return error_show(1002,"申请人数据不存在");
  609. }
  610. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  611. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  612. $diff_price = round($diff_weight*$cg['gold_price']+$diff_weight*$cg['teach_fee'],2);
  613. //如果有未处理的工差单,不允许创建新的
  614. $wait_check = Db::name('purchease_diff')
  615. ->field('id')
  616. ->where(['apply_id'=>$rid,'status'=>1,'is_del'=>0])
  617. ->find();
  618. if(!empty($wait_check)) return error_show('1005','您还有未处理的工差单,请先处理');
  619. $data=[
  620. "cgdNo"=>$cgdNo,
  621. "good_code"=>$cg['spuCode'],
  622. "good_name"=>$cg['good_name'],
  623. "sale_price"=>$cg['good_price'],
  624. "good_weight"=>$cg['weight']*$cg['good_num'],
  625. "good_num"=>$cg['good_num'],
  626. "apply_id"=>$rid,
  627. "apply_name"=>$rname,
  628. "diff_weight"=>$diff_weight,
  629. "diff_price"=>$diff_price,
  630. "gold_price"=>$cg['good_price'],
  631. "status"=>1,
  632. "is_del"=>0,
  633. "addtime"=>date("Y-m-d H:i:s"),
  634. "updatetime"=>date("Y-m-d H:i:s")
  635. ];
  636. Db::startTrans();
  637. try{
  638. $item = Db::name("purchease_diff")->insert($data,true);
  639. if($item>0){
  640. $sto = ["order_code"=>$cgdNo,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  641. ActionLog::logAdd($this->post['token'],$sto,"CGGCD",1,$data);
  642. $process=["order_code"=>$cgdNo,"order_id"=>$item,"order_status"=>1,"order_type"=>'CGGCD',"before_status"=>1];
  643. ProcessOrder::AddProcess($this->post['token'],$process);
  644. $orderCode = Db::name("order_num")->where([["cgdNo","=",$cg['cgdNo']],["status","=",1]])->find();
  645. if($orderCode!=false){
  646. $order = Db::name("sale")->where("orderCode", '=', $orderCode['orderCode'])->find();
  647. if($order==false){
  648. Db::rollback();
  649. return error_show(1002,"未找到确认单信息");
  650. }
  651. $data=[
  652. "cgd_diffid"=>$item,
  653. "orderCode"=>$orderCode['orderCode'],
  654. "good_code"=>$cg['spuCode'],
  655. "good_name"=>$cg['good_name'],
  656. "diff_weight"=>$diff_weight,
  657. "diff_price"=>round($diff_weight*$order['gold_price']+$diff_weight*$order['cost_price'],2),
  658. "gold_price"=>$order['sale_price'],
  659. "status"=>1,
  660. "addtime"=>date("Y-m-d H:i:s"),
  661. "updatetime"=>date("Y-m-d H:i:s")
  662. ];
  663. $cgd = Db::name('sale_diff')->insert($data,true);
  664. if($cgd==0){
  665. // $sto = ["order_code"=>$cg['bkcode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  666. // ActionLog::logAdd($this->post['token'],$sto,"ZXGCD",1,$sto);
  667. // $process=["order_code"=>$cg['bkcode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'ZXGCD'];
  668. // ProcessOrder::AddProcess($this->post['token'],$process);
  669. // Db::commit();
  670. // return error_show(0,"新建成功");
  671. // }else{
  672. Db::rollback();
  673. return error_show(1002,"新建失败");
  674. }
  675. $sto = ["order_code"=>$orderCode['orderCode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  676. ActionLog::logAdd($this->post['token'],$sto,"XSGCD",1,$data);
  677. $process=["order_code"=>$orderCode['orderCode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'XSGCD',"before_status"=>1];
  678. ProcessOrder::AddProcess($this->post['token'],$process);
  679. }
  680. Db::commit();
  681. return error_show(0,"新建成功");
  682. }else{
  683. Db::rollback();
  684. return error_show(1002,"新建失败");
  685. }
  686. }catch (\Exception $e){
  687. Db::rollback();
  688. return error_show(1002,$e->getMessage());
  689. }
  690. }
  691. public function difflist(){
  692. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  693. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  694. $where = [['a.is_del',"=",0]];
  695. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):"";
  696. if($cgdNo!=""){
  697. $where[]=['a.cgdNo',"like", "%$cgdNo%"];
  698. }
  699. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
  700. if($status!==""){
  701. $where[]=['a.status',"=",$status];
  702. }
  703. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  704. if ($start !="") {
  705. $where[]= ["a.addtime",'>=',$start];
  706. }
  707. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  708. if($end !=""){
  709. $where[]= ["a.addtime",'<=',$end];
  710. }
  711. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
  712. if($apply_name!==""){
  713. $where[]=['a.apply_name',"like","%$apply_name%"];
  714. }
  715. $count = Db::name('purchease_diff')->alias('a')->join("good b","b.spuCode=a.good_code","left")
  716. ->where($where)->count();
  717. $total = ceil($count/$size);
  718. $page = $page >= $total ? $total : $page;
  719. $list = Db::name('purchease_diff')->alias('a')->join("good b","b.spuCode=a.good_code","left")
  720. ->where($where)->page($page,$size)->order("a.addtime desc")->field("a.*,b.cat_id")->select();
  721. $data=[];
  722. foreach ($list as $value){
  723. $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
  724. $data[]=$value;
  725. }
  726. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  727. }
  728. public function diffinfo(){
  729. $id = isset($this->post['id']) && $this->post['id'] !=="" ? trim($this->post['id']) :"";
  730. if($id==""){
  731. return error_show(1002,"参数id 不能为空");
  732. }
  733. $idinf = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  734. if($idinf==false){
  735. return error_show(1002,"未找到采购工差单数据");
  736. }
  737. $dn = Db::name('purchease_order')->where(['cgdNo'=>$idinf['cgdNo'],'is_del'=>0])->find();
  738. if($dn['order_type']==3|| $dn['order_type']==4){
  739. $goon = Db::name("good_zixun")->where(["spuCode"=>$dn['spuCode'],"is_del"=>0])->find();
  740. }else {
  741. $goon =Db::name('good_basic')->where(['spuCode'=>$dn['spuCode']])->find();
  742. }
  743. // $goon = Db::name('good')->where(['spuCode'=>$idinf['good_code']])->find();
  744. if(empty($goon)){
  745. return error_show(1002,"未找到商品数据");
  746. }
  747. $idinf['wsm_name'] ="";
  748. $idinf['wsm_supplier'] ="";
  749. $idinf['wsm_supplierNo'] ="";
  750. $idinf['wsm_contactor'] ="";
  751. $idinf['wsm_mobile'] ="";
  752. $idinf['addr_code'] ="";
  753. $idinf['wsm_addr'] ="";
  754. if($dn['wsm_code']!=""){
  755. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  756. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  757. ->where(["a.wsm_code"=>$dn['wsm_code']])->field("a.name as wsm_name,b.name,b.code,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_code")->find();
  758. $idinf['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  759. $idinf['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  760. $idinf['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  761. $idinf['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  762. $idinf['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  763. $idinf['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  764. $idinf['addr_cn'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  765. $idinf['wsm_addr']=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  766. }
  767. $idinf['info'] =$dn;
  768. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  769. $idinf['can']=$int;
  770. $idinf['order_type']=$dn['order_type'];
  771. //取出销售的工差单信息
  772. $diff = Db::name('sale_diff')->field('id,customer_remark,is_act,remark')->where('cgd_diffid',$id)->find();
  773. $idinf['diff_customer_remark']=$diff['customer_remark'];
  774. $idinf['diff_is_act']=$diff['is_act'];
  775. $idinf['diff_remark']=$diff['remark'];
  776. return app_show(0,"获取成功",$idinf);
  777. }
  778. public function diffstatu(){
  779. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  780. if($id==""){
  781. return error_show(1002,"参数id不能为空");
  782. }
  783. $dio = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  784. if(empty($dio)){
  785. return error_show(1002,"工差订单未找到");
  786. }
  787. $cgd = Db::name("purchease_order")->where(['cgdNo'=>$dio['cgdNo']])->find();
  788. if(empty($cgd)){
  789. return error_show(1002,"采购单未找到");
  790. }
  791. $salediff=Db::name("sale_diff")->where(["cgd_diffid"=>$dio['id']])->find();
  792. if($salediff==false){
  793. return error_show(1002,"销售工差单未找到");
  794. }
  795. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  796. if($status===""){
  797. return error_show(1002,"参数status不能为空");
  798. }
  799. Db::startTrans();
  800. try{
  801. $temp = $dio['status'];
  802. $dio['status']=$status;
  803. $dio['updatetime']=date("Y-m-d H:i:s");
  804. $item = Db::name("purchease_diff")->save($dio);
  805. if ($item){
  806. if($status==2){
  807. $cgd['diff_fee'] =$dio['diff_price'];
  808. $cgd['diff_weight'] =$dio['diff_weight'];
  809. $cgd['updatetime'] =date("Y-m-d H:i:s");
  810. $cgp=Db::name("purchease_order")->save($cgd);
  811. if($cgp==false){
  812. Db::rollback();
  813. return error_show(1002,"采购单更新失败");
  814. }
  815. if($salediff['is_act']==1){
  816. $saled=[
  817. "diff_fee"=>$salediff['diff_price'],
  818. "diff_weight"=>$salediff['diff_weight'],
  819. "updatetime"=>date("Y-m-d H:i:s")
  820. ];
  821. $up =Db::name("sale")->where(["orderCode"=>$salediff['orderCode']])->save($saled);
  822. if($up==false){
  823. Db::rollback();
  824. return error_show(1002,"销售单更新失败");
  825. }
  826. }
  827. }
  828. $sto = ["order_code"=>$dio['cgdNo'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  829. ActionLog::logAdd($this->post['token'],$sto,"CGGCD",$status,$this->post);
  830. $process=["order_code"=>$dio['cgdNo'],"order_id"=>$dio['id'],"order_status"=>$status,"order_type"=>'CGGCD',"before_status"=>1];
  831. ProcessOrder::AddProcess($this->post['token'],$process);
  832. Db::commit();
  833. return error_show(0,"更新成功");
  834. }else{
  835. Db::rollback();
  836. return error_show(1002,"更新失败");
  837. }
  838. }catch (\Exception $e){
  839. Db::rollback();
  840. return error_show(1002,$e->getMessage()."|".$e->getLine());
  841. }
  842. }
  843. //修改采购单的实时金价
  844. public function editGoldPrice()
  845. {
  846. $param = $this->request->only(['cgdNo', 'gold_price'], 'post', 'trim');
  847. $val = Validate::rule(['cgdNo|采购单号' => 'require', 'gold_price|实时金价' => 'require|float']);
  848. if (!$val->check($param)) return error_show(1004, $val->getError());
  849. //采购单
  850. $data = Db::name("purchease_order")
  851. ->field('id,demo_fee,good_num,weight,open_fee,teach_fee,pakge_fee,mark_fee,cert_fee,nake_fee,delivery_fee,status')
  852. ->where(["cgdNo" => $param['cgdNo'], "is_del" => 0])
  853. ->find();
  854. if (empty($data)) return error_show(1004, "未找到该采购单数据");
  855. if ($data['status'] != 0) return error_show(1005, '该状态下不允许修改实时金价');
  856. $sale_price = round($data['demo_fee'] / $data['good_num'] + $data['open_fee'] / $data['good_num'] +$data['weight'] * $param["gold_price"] + $data['teach_fee'] * $data['weight'] +
  857. $data['pakge_fee'] + $data['mark_fee'] + $data['cert_fee'] +$data['nake_fee'] + $data['delivery_fee'],2);
  858. $total_price = round($data['good_num']*$sale_price,2);
  859. $rs = Db::name("purchease_order")
  860. ->where('id', $data['id'])
  861. ->update(['updatetime' => date('Y-m-d H:i:s'), 'gold_price' => $param['gold_price'],"good_price"=>$sale_price,"total_fee"=>$total_price]);
  862. return $rs ? app_show(0, '修改采购单实时金价成功') : error_show(1005, '修改采购单实时金价失败');
  863. }
  864. //采购单导出
  865. public function exportCgdList()
  866. {
  867. $cgdNos = $this->request->post('cgdNos', [], 'trim');
  868. if (empty($cgdNos)) return error_show(1004, '要导出的采购单编号不能为空');
  869. $i = 1;
  870. $send_type = [1 => '直接发货', 2 => '延时发货'];
  871. $status = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '已取消订单'];
  872. $list = Db::name("purchease_order")
  873. ->alias('po')
  874. ->field('"" as 序号,po.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,s.orderCode as 确认单号,s.addtime as 确认单时间,po.spuCode as 产品编号,po.good_name as 产品名称,"" as 规格,"" as 单位,po.supplier_name as 供应商名称,po.supplierNo as 供应商编号,po.nake_fee as 裸价,po.delivery_fee as 物流费,s.send_type as 发货方式,po.good_price as 采购单价,s.remark as 确认单备注,po.good_num as 采购数量,po.total_fee as 采购货款,b.company as 购买方公司,s.arrive_time as 到货时间,"" 税率,oa.addr 收货地址,oa.mobile 电话,oa.contactor 联系人,s.platform_order 平台订单编号, oa.addr_code,po.order_type')
  875. ->whereIn('po.cgdNo', $cgdNos)
  876. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  877. ->leftJoin('sale s', 's.orderCode=on.orderCode AND s.is_del=0')
  878. ->leftJoin('order_addr oa', 'oa.orderCode=on.orderCode AND oa.is_del=0')
  879. ->leftJoin('business b', 'b.companyNo=s.supplierNo AND b.is_del=0')
  880. ->order("po.addtime desc")
  881. ->withAttr('序号', function () use (&$i) {
  882. return $i++;
  883. })->withAttr('采购单状态', function ($val) use ($status) {
  884. return isset($status[$val]) ? $status[$val] : '';
  885. })->withAttr('发货方式', function ($val) use ($send_type) {
  886. return isset($send_type[$val]) ? $send_type[$val] : '';
  887. })->withAttr('发货方式', function ($val) use ($send_type) {
  888. return isset($send_type[$val]) ? $send_type[$val] : '';
  889. })
  890. ->select()
  891. ->toArray();
  892. foreach ($list as &$value) {
  893. if ($value['order_type'] == 3 || $value['order_type'] == 4) {
  894. $temp = Db::name("good_zixun")
  895. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  896. ->field('id,specinfo,good_unit,tax')
  897. ->find();
  898. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  899. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  900. $speclist = [];
  901. foreach ($specinfo as $val) {
  902. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  903. }
  904. $value['税率'] = $temp['tax'] . '%';
  905. } else {
  906. $good_unit = Db::name('good_basic')
  907. ->field('id,good_unit,tax')
  908. ->where(['spuCode' => $value['产品编号']])
  909. ->find();
  910. $spec = Db::name("good_spec")
  911. ->field('id,spec_id,spec_value_id')
  912. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  913. ->select()
  914. ->toArray();
  915. $speclist = [];
  916. if (!empty($spec)) {
  917. foreach ($spec as $val) {
  918. $speclist[] = Db::name("specs")->where(["id" => $val['spec_id']])->value('spec_name', '') . ':' . Db::name("spec_value")->where(["id" => $val['spec_value_id']])->value('spec_value', '');
  919. }
  920. }
  921. $value['税率'] = isset($good_unit['tax']) ? $good_unit['tax'] . '%' : '';
  922. }
  923. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  924. $value['单位'] = isset($good_unit['good_unit']) ? Db::name('unit')->where(['id' => $good_unit['good_unit'], 'is_del' => 0])->value('unit', '') : '';
  925. if (!empty($value['addr_code'])) {
  926. $temp = explode(',', $value['addr_code']);
  927. $temp_ = GetAddr(json_encode(['provice_code' => $temp[0], 'city_code' => $temp[1], 'area_code' => $temp[2]]));
  928. $value['收货地址'] = $temp_ . $value['收货地址'];
  929. }
  930. unset($value['addr_code']);
  931. unset($value['order_type']);
  932. }
  933. $headerArr = array_keys($list[0]);
  934. excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
  935. }
  936. //获取采购单修改供应商的记录列表
  937. public function getChangeSupplierList(){
  938. $param = $this->request->only(['cgdNo'=>'','start'=>'','end'=>'','creater'=>'','page'=>1,'size'=>15],'post','trim');
  939. $where=[['is_del','=',0]];
  940. if(!empty($param['cgdNo'])) $where[]=['cgdNo','like','%'.$param['cgdNo'].'%'];
  941. if(!empty($param['start'])) $where[]=['addtime','>=',$param['start']];
  942. if(!empty($param['end'])) $where[]=['addtime','<=',$param['end'].' 23:59:59'];
  943. if(!empty($param['creater'])) $where[]=['creater','like','%'.$param['creater'].'%'];
  944. $total = Db::name('purchease_change_supplier_log')
  945. ->where($where)
  946. ->count('id');
  947. $list =Db::name('purchease_change_supplier_log')
  948. ->withoutField('status,remark')
  949. ->where($where)
  950. ->order('id','desc')
  951. ->page($param['page'],$param['size'])
  952. ->select()
  953. ->toArray();
  954. return app_show(0,'获取成功',['list'=>$list,'count'=>$total]);
  955. }
  956. // public function create(){
  957. // $customer = isset($this->post['customer_code'])&&$this->post['customer_code']!="" ?trim($this->post['customer_code']):"";
  958. // if($customer==""){
  959. // return error_show(1004,"参数customer_code不能为空");
  960. // }
  961. // $supplier = isset($this->post['supplier_code'])&&$this->post['supplier_code']!="" ?trim($this->post['supplier_code']):"";
  962. // if($supplier==""){
  963. // return error_show(1004,"参数supplier_code不能为空");
  964. // }
  965. // $good_code = isset($this->post['good_code']) && $this->post['good_code']!=""?trim($this->post['good_code']):"";
  966. // if($good_code==""){
  967. // return error_show(1004,"参数good_code不能为空");
  968. // }
  969. // $good_num = isset($this->post['good_num'])&& $this->post['good_num']!==""? intval($this->post['good_num']):"";
  970. // if($good_num===""){
  971. // return error_show(1004,"参数good_code不能为空");
  972. // }
  973. // $file_url = isset($this->post['file_url'])&& $this->post['file_url']!==""? trim($this->post['file_url']):"";
  974. // if($file_url===""){
  975. // return error_show(1004,"参数file_url不能为空");
  976. // }
  977. // $mark =isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  978. // if($mark==""){
  979. // return error_show(1004,"参数remark不能为空");
  980. // }
  981. //
  982. // $data=[
  983. // ""
  984. // ];
  985. // }
  986. }