Purch.php 61 KB

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