Purch.php 52 KB

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