Purch.php 55 KB

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