Purch.php 64 KB

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