Order.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. <?php
  2. namespace app\abutment\controller;
  3. use app\abutment\model\SupplierRelationUser as SupplierRelationUserModel;
  4. use app\abutment\model\SupplierUser as SupplierUserModel;
  5. use app\admin\model\ActionLog;
  6. use app\admin\model\GoodLog;
  7. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  8. use think\Exception;
  9. use think\facade\Db;
  10. use think\facade\Validate;
  11. //销售订单(对应于采销端的采购单)
  12. class Order extends HomeBaseController
  13. {
  14. //列表
  15. public function getList()
  16. {
  17. $this->post = $this->request->filter('trim')->post();
  18. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  19. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  20. $where = [['po.is_del', "=", 0], ['po.supplierNo', '=', $this->request->user['supplierNo']], ['s.is_stock', '=', 0]];
  21. $bkcode = isset($this->post['bk_code']) && $this->post['bk_code'] != "" ? trim($this->post['bk_code']) : "";
  22. if ($bkcode != "") {
  23. $where[] = ['po.bkcode', "like", "%{$bkcode}%"];
  24. }
  25. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  26. if ($status !== "") {
  27. // $where['status'] = $status;
  28. $where[] = ['po.status', "=", $status];
  29. }
  30. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
  31. if ($cgdNo != "") {
  32. //$where['cgdNo'] = Db::Raw("like '%{$cgdNo}%'");
  33. $where[] = ['po.cgdNo', "like", "%{$cgdNo}%"];
  34. }
  35. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] != "" ? trim($this->post['apply_name']) : "";
  36. if ($apply_name != "") {
  37. // $where['cgder'] =Db::Raw("like '%{$apply_name}%'");
  38. $where[] = ['po.cgder', "like", "%{$apply_name}%"];
  39. }
  40. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] != "" ? trim($this->post['wsm_code']) : "";
  41. if ($wsm_code != "") {
  42. // $where['wsm_code'] = $wsm_code;
  43. $where[] = ['po.wsm_code', "=", $wsm_code];
  44. }
  45. $wsm_in_code = isset($this->post['wsm_in_code']) && $this->post['wsm_in_code'] != "" ? trim($this->post['wsm_in_code']) : "";
  46. if ($wsm_in_code != "") {
  47. $incode = Db::name("purchease_in")
  48. ->where(["wsm_in_code" => Db::raw(" like %{$wsm_in_code}%"), "is_del" => 0])
  49. ->column("cgdNo");
  50. if (empty($incode)) {
  51. return json_show(1005, "未找到有关入库单信息");
  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")
  73. ->field('id')
  74. ->where(["code" => $wsm_supplierNo])
  75. ->find();
  76. if (empty($supplier)) {
  77. return json_show(1004, "未找到供应商信息");
  78. }
  79. $wsmcode = Db::name("warehouse_info")
  80. ->where(["is_del" => 0, "supplierNo" => $wsm_supplierNo])
  81. ->column("wsm_code");
  82. $where[] = ['po.wsm_code', "in", $wsmcode];
  83. }
  84. $start = isset($this->post['start']) && $this->post['start'] != "" ? $this->post['start'] : "";
  85. if ($start != "") {
  86. // $where['addtime'] = Db::raw(" >= '{$start}'");
  87. $where[] = ['po.addtime', ">=", $start];
  88. }
  89. $end = isset($this->post['end']) && $this->post['end'] != "" ? $this->post['end'] : "";
  90. if ($end != "") {
  91. $where[] = ['po.addtime', "<=", $end];
  92. }
  93. $last_start = isset($this->post['last_start']) && $this->post['last_start'] != "" ? $this->post['last_start'] : "";
  94. if ($last_start != "") {
  95. //$where['lasttime'] = Db::raw(" >= '{$last_start}'");
  96. $where[] = ['po.lasttime', ">=", $last_start];
  97. }
  98. $last_end = isset($this->post['last_end']) && $this->post['last_end'] != "" ? $this->post['last_end'] : "";
  99. if ($last_end != "") {
  100. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  101. $where[] = ['po.lasttime', "<=", $last_end];
  102. }
  103. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? $this->post['orderCode'] : "";
  104. if ($orderCode != "") {
  105. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  106. $where[] = ['on.orderCode', "like", '%' . $orderCode . '%'];
  107. }
  108. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  109. if ($company_name !== "") $where[] = ["po.cgder_id", 'in', get_company_item_user_by_name($company_name)];
  110. // if(!empty($role['platform']) ){
  111. // $where[]=["po.platform_id","in",$role['platform']];
  112. // }
  113. $count = Db::name("purchease_order")
  114. ->alias('po')
  115. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  116. ->leftJoin('sale s', 's.orderCode=on.orderCode')
  117. ->leftJoin("depart_user u", "u.uid=po.cgder_id AND u.is_del=0")->where($where)
  118. ->where(function ($query) use ($where) {
  119. $query->whereOr([['po.order_type', '<>', 1], ['po.order_source', '=', 0]]);
  120. })
  121. ->count('po.id');
  122. $total = ceil($count / $size);
  123. $page = $page >= $total ? $total : $page;
  124. $list = Db::name("purchease_order")
  125. ->alias('po')
  126. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  127. ->leftJoin('sale s', 's.orderCode=on.orderCode')
  128. ->leftJoin("depart_user u", "u.uid=po.cgder_id AND u.is_del=0")
  129. ->field('po.*,on.orderCode,u.itemid')
  130. ->where($where)
  131. ->where(function ($query) use ($where) {
  132. $query->whereOr([['po.order_type', '<>', 1], ['po.order_source', '=', 0]]);
  133. })
  134. ->page($page, $size)
  135. ->order("po.addtime desc")
  136. ->cursor();
  137. $data = [];
  138. foreach ($list as $value) {
  139. $value['wsm_name'] = "";
  140. if ($value['wsm_code'] != "") {
  141. $wsmcode = Db::name("warehouse_info")
  142. ->alias("a")
  143. ->leftJoin("supplier b", "a.supplierNo=b.code")
  144. ->where(["a.wsm_code" => $value['wsm_code']])
  145. ->field("a.name as wsm_name,b.name,b.code")
  146. ->find();
  147. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  148. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  149. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  150. }
  151. if ($value['order_type'] == 3) {
  152. $goon = Db::name("good_zixun")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
  153. $value["speclist"] = isset($goon['specinfo']) && $goon['specinfo'] != "" ? json_decode($goon['specinfo'], true) : "";
  154. } else {
  155. $goon = Db::name('good_basic')->where(['spuCode' => $value['spuCode']])->find();
  156. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  157. $speclist = [];
  158. if (!empty($spec)) {
  159. foreach ($spec as $val) {
  160. $temp = [];
  161. $temp['id'] = $val['id'];
  162. $temp['spuCode'] = $val['spuCode'];
  163. $temp['spec_id'] = $val['spec_id'];
  164. $temp['spec_value_id'] = $val['spec_value_id'];
  165. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  166. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  167. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  168. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  169. $speclist[] = $temp;
  170. }
  171. }
  172. $value["speclist"] = empty($speclist) ? [] : $speclist;
  173. }
  174. //采购单详情字段order_type 为1或者2时,取线上商品库 商品创建人 order_type 为3或者4时 取采购反馈的商品库 商品反馈人
  175. // $value['cgder_id'] = $goon['createrid'];
  176. // $value['cgder'] = $goon['creater'];
  177. $inorder = Db::name("purchease_in")->where(['cgdNo' => $value['cgdNo'], "is_del" => 0])->select();
  178. $value['child'] = empty($inorder) ? [] : $inorder;
  179. $value['send_num'] -= $value['th_num'];
  180. $value['total_fee'] = round($value['total_fee'] - $value['th_fee'], 2);
  181. //补充orderCode
  182. // $value['orderCode']=isset($all_orderCodes[$value['cgdNo']])?$all_orderCodes[$value['cgdNo']]:'';
  183. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  184. $data[] = $value;
  185. }
  186. return json_show(0, "获取成功", ["list" => $data, "count" => $count]);
  187. }
  188. //详情
  189. public function info()
  190. {
  191. $this->post = $this->request->filter('trim')->post();
  192. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
  193. if ($cgdNo == "") {
  194. return json_show(1004, "参数cgdNo 不能为空");
  195. }
  196. //采购单
  197. $data = Db::name("purchease_order")->where(["cgdNo" => $cgdNo, "is_del" => 0, 'supplierNo' => $this->request->user['supplierNo']])->find();
  198. if (empty($data)) {
  199. return json_show(1004, "未找到数据");
  200. }
  201. //采购退货单
  202. $im = Db::name('purchease_back')->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->select();
  203. $var = [];
  204. foreach ($im as $value) {
  205. if ($data['order_type'] == 3) {
  206. $goo = Db::name("good_zixun")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
  207. } else {
  208. $goo = Db::name('good_basic')->where(['spuCode' => $value['spuCode']])->find();
  209. }
  210. if ($goo == false) {
  211. return json_show(1005, "未找到商品数据");
  212. }
  213. $cat = isset($goo['cat_id']) && $goo['cat_id'] != 0 ? made($goo['cat_id']) : [];
  214. $value['cant'] = $cat;
  215. $var[] = $value;
  216. }
  217. //采购工差单
  218. $dom = Db::name("purchease_diff")->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->find();
  219. if ($data['order_type'] == 3) {
  220. $goon = Db::name("good_zixun")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->find();
  221. } else {
  222. $goon = Db::name('good_basic')->where(['spuCode' => $data['spuCode']])->find();
  223. }
  224. if (empty($goon)) {
  225. return json_show(1005, "未找到商品数据");
  226. } else {
  227. $goon['exclusive'] = isset($goon['is_exclusive']) ? makeExcluse($goon['is_exclusive']) : "";
  228. $unit = Db::name("unit")->where(["id" => $goon['good_unit']])->find();
  229. $goon['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  230. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  231. $speclist = [];
  232. if (!empty($spec)) {
  233. foreach ($spec as $value) {
  234. $temp = [];
  235. $temp['id'] = $value['id'];
  236. $temp['spuCode'] = $value['spuCode'];
  237. $temp['spec_id'] = $value['spec_id'];
  238. $temp['spec_value_id'] = $value['spec_value_id'];
  239. $temp['is_del'] = $value['is_del'];
  240. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  241. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  242. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  243. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  244. $speclist[] = $temp;
  245. }
  246. }
  247. $goon["speclist"] = empty($speclist) ? [] : $speclist;
  248. $proof = Db::name("good_proof")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  249. $goon['proof'] = isset($proof) && $proof != false ? $proof : [];
  250. $goon['origin_place_cn'] = "";
  251. $goon['delivery_place_cn'] = "";
  252. if (isset($goon['delivery_place']) && $goon['delivery_place'] !== "") {
  253. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  254. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['delivery_place']);
  255. $goon['delivery_place_cn'] = GetAddr(json_encode($place));
  256. }
  257. if (isset($goon['delivery_place']) && $goon['origin_place'] !== "") {
  258. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  259. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['origin_place']);
  260. $goon['origin_place_cn'] = GetAddr(json_encode($place));
  261. }
  262. if ($goon['brand_id'] != 0) {
  263. $brand = Db::name("brand")->where(["id" => $goon['brand_id']])->find();
  264. $goon["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  265. } else {
  266. $goon["brand_name"] = "";
  267. $goon["brand_id"] = "";
  268. }
  269. $supplier = Db::name("supplier")->where(["code" => $goon['supplierNo']])->find();
  270. $goon['supplierName'] = isset($supplier['name']) ? $supplier['name'] : "";
  271. $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
  272. if (isset($goon['companyNo']) && $goon['companyNo'] != "") {
  273. $company = Db::name("business")->where(["companyNo" => $goon['companyNo']])->find();
  274. }
  275. $goon['company'] = isset($company['company']) ? $company['company'] : "";
  276. }
  277. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  278. $data['wsm_name'] = "";
  279. if ($data['wsm_code'] != "") {
  280. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")
  281. ->leftJoin("warehouse_addr c", "a.wsm_code=c.wsm_code and c.is_del=0")
  282. ->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();
  283. $data['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  284. $data['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  285. $data['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  286. $data['wsm_contactor'] = isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor'] : "";
  287. $data['wsm_mobile'] = isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile'] : "";
  288. // $data['wsm_addr'] =isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  289. $data['addr_code'] = isset($wsmcode['addr_code']) ? $wsmcode['addr_code'] : "";
  290. $data['wsm_addr'] = isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']) : "";
  291. $data['wsm_addr'] .= isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr'] : "";
  292. $inorder = Db::name("purchease_in")->where(['cgdNo' => $data['cgdNo'], "is_del" => 0])->select();
  293. $data['child'] = empty($inorder) ? [] : $inorder;
  294. }
  295. $data['can'] = $int;
  296. $data['goodinfo'] = $goon;
  297. $data['purcheasediff'] = $dom;
  298. $data['send_num'] -= $data['th_num'];
  299. $data['total_fee'] = round($data['total_fee'] - $data['th_fee'], 2);
  300. if (isset($data['companyNo']) && $data['companyNo'] != "") {
  301. $company = Db::name("business")->where(["companyNo" => $data['companyNo']])->find();
  302. }
  303. $data['company'] = isset($company['company']) ? $company['company'] : "";
  304. //$data['info'] = $var;
  305. $data['purcheaseback'] = $var;
  306. //补充orderCode
  307. $data['orderCode'] = Db::name('order_num')
  308. ->where('cgdNo', $data['cgdNo'])
  309. ->value('orderCode', '');
  310. //补充收货地址
  311. $addr = Db::name('order_addr')
  312. ->field('id,addr,addr_code,contactor,mobile,receipt_quantity')
  313. ->where(['orderCode' => $data['orderCode'], 'is_del' => 0])
  314. ->select()
  315. ->toArray();
  316. if (!empty($addr)) {
  317. foreach ($addr as &$vv) {
  318. $temp = explode(',', $vv['addr_code']);
  319. $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] : '']));
  320. $vv['addr_code'] = $temp_ . $vv['addr'];
  321. }
  322. } else $addr = [];
  323. $data['addr_info'] = $addr;
  324. //补充客户名称
  325. if (empty($data['orderCode'])) $data['customer'] = [];
  326. else {
  327. $data['customer'] = Db::name('sale')
  328. ->alias('s')
  329. ->field('c.companyNo,c.companyName')
  330. ->leftJoin('customer_info c', 'c.companyNo=s.customer_code AND c.is_del=0')
  331. ->where(['s.orderCode' => $data['orderCode']])
  332. ->find();
  333. }
  334. return json_show(0, "获取成功", $data);
  335. }
  336. //订单确认
  337. public function status()
  338. {
  339. $this->post = $this->request->filter('trim')->post();
  340. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? $this->post['cgdNo'] : [];
  341. if (empty($cgdNo)) {
  342. return json_show(1004, "参数cgdNo 不能为空");
  343. }
  344. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  345. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  346. if ($status === "") {
  347. return json_show(1004, "参数status 不能为空");
  348. }
  349. if ($status == 0) {
  350. $data = Db::name("purchease_order")
  351. ->field('id,cgdNo')
  352. ->whereIn('cgdNo', $cgdNo)
  353. ->where(["is_del" => 0])
  354. ->where('send_status', '>', 1)
  355. ->find();
  356. if (!empty($data)) {
  357. return json_show(1004, $data['cgdNo'] . "采购单发货中无法取消");
  358. }
  359. }
  360. //原始数据
  361. $info = Db::name("purchease_order")
  362. ->whereIn('cgdNo', $cgdNo)
  363. ->where('status', 0)
  364. ->column('id,cgdNo,status,supplierNo,spuCode,order_type', 'cgdNo');
  365. if (empty($info)) return json_show(1005, '没有可供确认的订单');
  366. $upd = Db::name("purchease_order")
  367. ->whereIn('cgdNo', $cgdNo)
  368. ->where('status', 0)
  369. ->save([
  370. 'status' => $status,
  371. 'remark' => $remark,
  372. 'updatetime' => date("Y-m-d H:i:s"),
  373. ]);
  374. if ($upd) {
  375. // $user=GetUserInfo($this->post['token']);
  376. $uid = $this->request->user['uid']; //isset($user['data']['id'])?$user['data']['id']:0;
  377. $uname = $this->request->user['nickname'];//isset($user['data']['nickname'])?$user['data']['nickname']:'';
  378. $supplier_cgderid = Db::name('supplier')
  379. ->where('is_del',0)
  380. ->whereIn('code',array_column($info,'supplierNo'))
  381. ->column('personid','code');
  382. foreach ($cgdNo as $vlue) {
  383. if($status==2 || $status==3){
  384. // $orderinfo = Db::name('sale')
  385. // ->alias('a')
  386. // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid')
  387. // ->leftJoin('order_num b','b.orderCode=a.orderCode')
  388. // ->where([
  389. // 'a.is_del'=>0,
  390. // 'b.cgdNo'=>$vlue,
  391. // ])->findOrEmpty();
  392. $is_stock = Db::name('good_basic')
  393. ->where(['is_del'=>0,'spuCode'=>$info[$vlue]['spuCode']])
  394. ->value('is_stock');
  395. if($is_stock==1){
  396. //库存品,推给31库管人员、41库管-张凯旋
  397. $uids = Db::name('user_role')
  398. ->where([
  399. ['is_del', '=', 0],
  400. ['roleid', 'in', [31, 41]],
  401. ['status', '=', 1]
  402. ])->column('uid');
  403. if(!in_array($uid,$uids)) throw new Exception('库存品订单只能由库管人员操作');
  404. $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)];
  405. }elseif ($is_stock==0 || $info[$vlue]['order_type']==3) {
  406. //非库存品和采返商品,推给供应商负责人(还得是没开通账号的供应商)
  407. $res = SupplierRelationUserModel::field('id')
  408. ->where(['is_del' => SupplierUserModel::$is_del_normal, 'supplierNo' => $info[$vlue]['supplierNo'], 'status' => SupplierUserModel::$status_normal])
  409. ->findOrEmpty()
  410. ->isEmpty();
  411. if(!$res) throw new Exception('供应商已开通账号,只能由供应商端操作');
  412. if($uid != $supplier_cgderid[$info[$vlue]['supplierNo']])throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  413. $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];
  414. }
  415. }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];
  416. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], $process);
  417. $order = ["order_code" => $vlue, "status" => '', "action_remark" => $remark, "action_type" => "status"];
  418. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], $order, 'CGD', $status, $this->post);
  419. }
  420. return json_show(0, "更新成功");
  421. } else return json_show(1004, '更新失败');
  422. }
  423. //批量入库
  424. public function add()
  425. {
  426. $param = $this->request->filter('trim')->only(['cgdNo'], 'post');
  427. $val = Validate::rule(['cgdNo|订单编号' => 'require|array|max:100']);
  428. //入库方式 供应商包邮,
  429. //运费 0
  430. if (!$val->check($param)) return json_show(1004, $val->getError());
  431. $cgdinfo = Db::name("purchease_order")
  432. ->whereIn('cgdNo', $param['cgdNo'])
  433. ->where(['is_del' => 0, 'supplierNo' => $this->request->user['supplierNo']])
  434. ->where('status', '=', 1)
  435. ->column('id,wsm_code,good_num,good_price,spuCode,order_type,send_status,status,supplierNo', 'cgdNo');
  436. if (empty($cgdinfo)) return json_show(1005, "未找到采购单数据或采购单未发货");
  437. $all_wsm_code = array_column($cgdinfo, 'wsm_code');
  438. $recep = Db::name('warehouse_addr')
  439. ->where('is_del', 0)
  440. ->whereIn('wsm_code', $all_wsm_code)
  441. ->column('wsm_name', 'wsm_code');
  442. $uid = $this->request->user['uid'];
  443. $nickname = $this->request->user['nickname'];
  444. Db::startTrans();
  445. try {
  446. $date = date('Y-m-d H:i:s');
  447. $supplier_cgderid = Db::name('supplier')
  448. ->where('is_del',0)
  449. ->whereIn('code',array_column($cgdinfo,'supplierNo'))
  450. ->column('personid,person','code');
  451. $i = 0;
  452. $status = 4;//4入库完成
  453. foreach ($param['cgdNo'] as $cgdNo) {
  454. if (empty($cgdinfo[$cgdNo])) throw new Exception('未找到采购单数据或采购单未发货');
  455. $wsm_in_code = makeNo("CF");
  456. $bn_code = makeNo("BN");
  457. $send_num = $cgdinfo[$cgdNo]['good_num'];
  458. //改变编码规则,将原编码后两位换成序列号
  459. //str_pad字符串填充
  460. $wsm_in_code = substr($wsm_in_code, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  461. $i++;
  462. //入库单
  463. $win = Db::name("purchease_in")->insertGetId([
  464. "wsm_in_code" => $wsm_in_code,
  465. "cgdNo" => $cgdNo,
  466. "bnCode"=>$bn_code,
  467. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  468. "wsm_reaper" => isset($recep[$cgdinfo[$cgdNo]['wsm_code']]) ? $recep[$cgdinfo[$cgdNo]['wsm_code']] : '',
  469. "send_num" => $cgdinfo[$cgdNo]['good_num'],
  470. "post_company" => '',
  471. "post_code" => '',
  472. "post_fee" => 0,//运费固定值:0
  473. "sendtime" => $date,
  474. "apply_id" => $this->request->user['uid'],
  475. "apply_name" => $this->request->user['nickname'],
  476. "wait_num" => 0,
  477. "wsm_num" => 0,
  478. "sendtype" => 2,//发货方式,固定值,供应商包邮2
  479. "status" => $status,
  480. "addtime" => $date,
  481. "updatetime" => $date
  482. ]);
  483. if ($win) {
  484. $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
  485. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $sto, "RKD", $status, $this->request->filter('trim')->post());
  486. $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status,'holder_id' => $this->request->user['uid']];
  487. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  488. $order = ["order_code" => $cgdNo, "status" => $cgdinfo[$cgdNo]['status'], "action_remark" => '', "action_type" => "edit"];
  489. //维护台账信息
  490. Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . $date . "' WHERE `cgdNo`='{$cgdNo}'");
  491. //////////////////
  492. $good = Db::name("good_stock")
  493. ->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])
  494. ->lock(true)
  495. ->find();
  496. if (empty($good)) {
  497. $good = [
  498. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  499. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  500. "usable_stock" => 0,
  501. "wait_out_stock" => 0,
  502. "wait_in_stock" => $send_num,
  503. "total_stock" => 0,
  504. "addtime" => date("Y-m-d H:i:s"),
  505. "updatetime" => date("Y-m-d H:i:s"),
  506. ];
  507. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "create"];
  508. } else {
  509. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "edit"];
  510. }
  511. $good['wait_in_stock'] -= $send_num;
  512. // if ($good['presale_stock'] > 0) {
  513. // if ($good['presale_stock'] >= $send_num) {
  514. // $good['presale_stock'] -= $send_num;
  515. // $good['wait_out_stock'] += $send_num;
  516. // } else {
  517. // $good['presale_stock'] = 0;
  518. // $good['usable_stock'] += $send_num - $good['presale_stock'];
  519. // $good['wait_out_stock'] += $good['presale_stock'];
  520. // }
  521. // } else {
  522. $good['usable_stock'] += $send_num;
  523. // }
  524. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  525. $good['updatetime'] = date("Y-m-d H:i:s");
  526. $upd = Db::name("good_stock")->save($good);
  527. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  528. //商品变动日志表,good_log_code字段存储采购单单号
  529. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" =>$stockid, "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  530. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" =>$stockid, "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
  531. if ($upd) {
  532. GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
  533. if ($cgdinfo[$cgdNo]['order_type'] == 1) {
  534. $bnin=GoodStockInfo::AddBn($stockid,$bn_code,$send_num,$cgdinfo[$cgdNo]['good_price']);
  535. if($bnin==false){
  536. Db::rollback();
  537. return error_show(1004,"库存bn数据新增失败");
  538. }
  539. $bk = Db::name("order_bk")->where(['cgdNo' => $cgdNo])->find();
  540. if ($bk == false) {
  541. $bk = [
  542. "cgdNo" => $cgdNo,
  543. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  544. "companyNo" => $cgdinfo[$cgdNo]['companyNo'],
  545. "total_num" => $send_num,
  546. "merge_num" => 0,
  547. "balance_num" => $send_num,
  548. "status" => 1,
  549. "is_del" => 0,
  550. 'addtime' => date("Y-m-d H:i:s"),
  551. "updatetime" => date("Y-m-d H:i:s")
  552. ];
  553. $upcgd = Db::name("order_bk")->save($bk);
  554. } else {
  555. $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
  556. $bk['balance_num'] += $send_num;
  557. $bk['total_num'] += $send_num;
  558. $bk['updatetime'] = date("Y-m-d H:i:s");
  559. $upcgd = Db::name("order_bk")->where($where)->update($bk);
  560. }
  561. if ($upcgd == false) throw new Exception('采购单数量更新失败');
  562. }
  563. if ($cgdinfo[$cgdNo]['send_status'] == 1 && $cgdinfo[$cgdNo]['order_type'] != 1) {
  564. $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
  565. if (!empty($send)) {
  566. $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()->toArray();
  567. if (!empty($out)) {
  568. $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->update(["status" => 1, 'wsm_code' => $cgdinfo[$cgdNo]['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
  569. if ($up == false) throw new Exception('出库单出库失败');
  570. else {
  571. //查询是否有非库存品订单
  572. $is_stock = Db::name('sale')
  573. ->where([
  574. ['is_del','=',0],
  575. ['orderCode','in',array_column($out,'orderCode')]
  576. ])->column('is_stock','orderCode');
  577. foreach ($out as $k => $v_outCode) {
  578. //修改状态,添加待办
  579. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], [
  580. "order_code" => $v_outCode['outCode'],//出库单号
  581. "status" => 0,//这里的status是之前的值
  582. "action_remark" => '',//备注
  583. "action_type" => "status"//新建create,编辑edit,更改状态status
  584. ], "CKD", 1, $param);
  585. //库存品,推给库管和库管-张凯旋
  586. if($is_stock[$v_outCode['orderCode']] == 1){
  587. $roleid = config('app.wsm_cgder_role');
  588. $uids = Db::name('user_role')
  589. ->where('is_del', 0)
  590. ->whereIn('roleid', $roleid)
  591. ->column('uid');
  592. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
  593. "order_type" => 'CKD',
  594. "order_code" => $v_outCode['outCode'],//出库单号
  595. "order_id" => $v_outCode['id'],
  596. "order_status" => 1,
  597. "before_status" => 0,
  598. // 'wait_id' => $wait['personid'],
  599. // 'wait_name' => $wait['person'],
  600. // 'holder_id' => $wait['personid'],
  601. 'handle_user_list' => implode(',', $uids)
  602. ]);
  603. }elseif ($is_stock[$v_outCode['orderCode']] == 0 || $cgdinfo[$cgdNo]['order_type']==3){
  604. //非库存品和采返商品,推给供应商负责人
  605. $wait = $supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']];
  606. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
  607. "order_type" => 'CKD',
  608. "order_code" => $v_outCode['outCode'],//出库单号
  609. "order_id" => $v_outCode['id'],
  610. "order_status" => 1,
  611. "before_status" => 0,
  612. 'wait_id' => $wait['personid'],
  613. 'wait_name' => $wait['person'],
  614. 'holder_id' => $wait['personid'],
  615. ]);
  616. }else{
  617. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
  618. "order_type" => 'CKD',
  619. "order_code" => $v_outCode['outCode'],//出库单号
  620. "order_id" => $v_outCode['id'],
  621. "order_status" => 1,
  622. "before_status" => 0,
  623. 'holder_id' => $v_outCode['apply_id']
  624. ]);
  625. }
  626. }
  627. }
  628. $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])->find();
  629. if ($good1 == false) throw new Exception('未找到对应商品库存');
  630. $out_num = intval(array_sum(array_column($out, "send_num")));
  631. if ($out_num > $good1['usable_stock']) throw new Exception('库存数量不足发货');
  632. $good1['usable_stock'] -= $out_num;
  633. $good1['wait_out_stock'] += $out_num;
  634. $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
  635. $good1['updatetime'] = date("Y-m-d H:i:s");
  636. $upd = Db::name("good_stock")->save($good1);
  637. if ($upd == false) throw new Exception('库存数量更新失败');
  638. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  639. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  640. GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
  641. $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
  642. if ($sendp == false) throw new Exception('库存发货失败');
  643. }
  644. }
  645. }
  646. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $order, "CGD", $cgdinfo[$cgdNo]['status'], $cgdinfo[$cgdNo]);
  647. $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo[$cgdNo]['id'], "order_status" => $cgdinfo[$cgdNo]['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => isset($supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']]['personid']) ? $supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']]['personid'] : 0];
  648. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  649. // Db::commit();
  650. // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
  651. } else throw new Exception('商品入库失败');
  652. //////////////////
  653. } else throw new Exception('新建入库单失败');
  654. }
  655. //维护原有采购单信息
  656. Db::name("purchease_order")
  657. ->whereIn('id', array_column($cgdinfo, 'id'))
  658. ->update([
  659. 'wsend_num' => 0,
  660. 'send_num' => Db::raw('good_num'),
  661. 'send_status' => 3,
  662. 'status' => 3,
  663. 'updatetime' => date("Y-m-d H:i:s"),
  664. ]);
  665. Db::commit();
  666. return json_show(0, '新建入库单成功');
  667. // $win = Db::name("purchease_in")->insert($orin, true);
  668. // if ($win) {
  669. // $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
  670. // ActionLog::logAdd($this->post['token'], $sto, "RKD", $status, $this->post);
  671. // $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status];
  672. // ProcessOrder::AddProcess($this->post['token'], $process);
  673. // $order = ["order_code" => $cgdNo, "status" => $cgdinfo['status'], "action_remark" => '', "action_type" => "edit"];
  674. //维护台账信息
  675. // Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `cgdNo`='{$cgdNo}'");
  676. // if ($status == 1) {
  677. // Db::commit();
  678. // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
  679. // }
  680. // if ($status == 4) {
  681. // $cgdinfo['wsend_num'] -= $send_num;
  682. // $cgdinfo['send_num'] += $send_num;
  683. // $cgdinfo['send_status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
  684. // $cgdinfo['status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
  685. // $cgdinfo['updatetime'] = date("Y-m-d H:i:s");
  686. // $up = Db::name("purchease_order")->save($cgdinfo);
  687. // if ($up == false) {
  688. // Db::rollback();
  689. // return json_show(1004, "新建失败");
  690. // }
  691. // $good = Db::name("good_stock")
  692. // ->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])
  693. // ->lock(true)
  694. // ->find();
  695. // if (empty($good)) {
  696. // $good = [
  697. // "spuCode" => $cgdinfo['spuCode'],
  698. // "wsm_code" => $cgdinfo['wsm_code'],
  699. // "usable_stock" => 0,
  700. // "wait_out_stock" => 0,
  701. // "wait_in_stock" => $send_num,
  702. // "total_stock" => 0,
  703. // "addtime" => date("Y-m-d H:i:s"),
  704. // "updatetime" => date("Y-m-d H:i:s"),
  705. // ];
  706. // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  707. // // "action_type"=>"create"];
  708. // } else {
  709. // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  710. // // "action_type"=>"edit"];
  711. // }
  712. // $good['wait_in_stock'] -= $send_num;
  713. // if ($good['presale_stock'] > 0) {
  714. // if ($good['presale_stock'] >= $send_num) {
  715. // $good['presale_stock'] -= $send_num;
  716. // $good['wait_out_stock'] += $send_num;
  717. // } else {
  718. // $good['presale_stock'] = 0;
  719. // $good['usable_stock'] += $send_num - $good['presale_stock'];
  720. // $good['wait_out_stock'] += $good['presale_stock'];
  721. // }
  722. // } else {
  723. // $good['usable_stock'] += $send_num;
  724. // }
  725. //
  726. // $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  727. // $good['updatetime'] = date("Y-m-d H:i:s");
  728. // $upd = Db::name("good_stock")->save($good);
  729. // //商品变动日志表,good_log_code字段存储采购单单号
  730. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  731. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
  732. // if ($upd) {
  733. // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
  734. // if ($cgdinfo['order_type'] == 1) {
  735. // $bk = Db::name("order_bk")->where(['cgdNo' => $cgdinfo['cgdNo']])->find();
  736. // if ($bk == false) {
  737. // $bk = [
  738. // "cgdNo" => $cgdinfo['cgdNo'],
  739. // "spuCode" => $cgdinfo['spuCode'],
  740. // "companyNo" => $cgdinfo['companyNo'],
  741. // "total_num" => $send_num,
  742. // "merge_num" => 0,
  743. // "balance_num" => $send_num,
  744. // "status" => 1,
  745. // "is_del" => 0,
  746. // 'addtime' => date("Y-m-d H:i:s"),
  747. // "updatetime" => date("Y-m-d H:i:s")
  748. // ];
  749. // $upcgd = Db::name("order_bk")->save($bk);
  750. // } else {
  751. // $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
  752. // $bk['balance_num'] += $send_num;
  753. // $bk['total_num'] += $send_num;
  754. // $bk['updatetime'] = date("Y-m-d H:i:s");
  755. // $upcgd = Db::name("order_bk")->where($where)->update($bk);
  756. // }
  757. //
  758. // if ($upcgd == false) {
  759. // Db::rollback();
  760. // return json_show(1004, "采购单数量更新失败");
  761. // }
  762. // }
  763. // if ($cgdinfo['send_status'] == 3 && $cgdinfo['order_type'] != 1) {
  764. // $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
  765. // if (!empty($send)) {
  766. // $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()
  767. // ->toArray();
  768. // if (!empty($out)) {
  769. // $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->update
  770. // (["status" => 1, 'wsm_code' => $cgdinfo['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
  771. // if ($up == false) {
  772. // Db::rollback();
  773. // return json_show(1004, "出库单出库失败");
  774. // } else {
  775. // foreach ($out as $k => $v_outCode) {
  776. // //修改状态,添加待办
  777. // ActionLog::logAdd($this->post['token'], [
  778. // "order_code" => $v_outCode['outCode'],//出库单号
  779. // "status" => 0,//这里的status是之前的值
  780. // "action_remark" => '',//备注
  781. // "action_type" => "status"//新建create,编辑edit,更改状态status
  782. // ], "CKD", 1, $this->post);
  783. //
  784. // ProcessOrder::AddProcess($this->post['token'], [
  785. // "order_type" => 'CKD',
  786. // "order_code" => $v_outCode['outCode'],//出库单号
  787. // "order_id" => $v_outCode['id'],
  788. // "order_status" => 1, "before_status" => 0
  789. // ]);
  790. // }
  791. // }
  792. // $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])->find();
  793. // if ($good1 == false) {
  794. // Db::rollback();
  795. // return json_show(1004, "未找到对应商品库存");
  796. // }
  797. // $out_num = intval(array_sum(array_column($out, "send_num")));
  798. // if ($out_num > $good1['usable_stock']) {
  799. // Db::rollback();
  800. // return json_show(1004, "库存数量不足发货");
  801. // }
  802. // $good1['usable_stock'] -= $out_num;
  803. // $good1['wait_out_stock'] += $out_num;
  804. // $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
  805. // $good1['updatetime'] = date("Y-m-d H:i:s");
  806. // $upd = Db::name("good_stock")->save($good1);
  807. // if ($upd == false) {
  808. // Db::rollback();
  809. // return json_show(1004, "库存数量更新失败");
  810. // }
  811. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  812. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  813. // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
  814. // $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
  815. // if ($sendp == false) {
  816. // Db::rollback();
  817. // return json_show(1004, "库存发货失败");
  818. // }
  819. // }
  820. // }
  821. // }
  822. // ActionLog::logAdd($this->post['token'], $order, "CGD", $cgdinfo['status'], $cgdinfo);
  823. // $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status']];
  824. // ProcessOrder::AddProcess($this->post['token'], $process);
  825. // Db::commit();
  826. // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
  827. // } else {
  828. // Db::rollback();
  829. // return json_show(1002, "商品入库失败");
  830. // }
  831. // }
  832. // Db::commit();
  833. // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
  834. // }
  835. // Db::rollback();
  836. // return json_show(1004, "新建失败");
  837. } catch (Exception $e) {
  838. Db::rollback();
  839. return json_show(1004, $e->getMessage());
  840. }
  841. }
  842. //导出
  843. public function exportCgdList()
  844. {
  845. $cgdNos = $this->request->post('cgdNos', [], 'trim');
  846. if (empty($cgdNos)) return json_show(1004, '要导出的采购单编号不能为空');
  847. $i = 1;
  848. $send_type = [1 => '直接发货', 2 => '延时发货'];
  849. $status = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '已取消订单'];
  850. $list = Db::name("purchease_order")
  851. ->alias('po')
  852. ->field('"" as 序号,po.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,s.orderCode as 确认单号,s.addtime as 确认单时间,po.spuCode as 产品编号,po.good_name as 产品名称,"" as 规格,"" as 单位,po.supplier_name as 供应商名称,po.supplierNo as 供应商编号,po.nake_fee as 裸价,po.delivery_fee as 物流费,s.send_type as 发货方式,po.good_price as 采购单价,s.remark as 确认单备注,po.good_num as 采购数量,po.total_fee as 采购货款,b.company as 购买方公司,s.arrive_time as 到货时间,"" 税率,oa.addr 收货地址,oa.mobile 电话,oa.contactor 联系人,s.platform_order 平台订单编号,oa.receipt_quantity 地址发货数量, oa.addr_code,po.order_type')
  853. ->whereIn('po.cgdNo', $cgdNos)
  854. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  855. ->leftJoin('sale s', 's.orderCode=on.orderCode AND s.is_del=0')
  856. ->leftJoin('order_addr oa', 'oa.orderCode=on.orderCode AND oa.is_del=0')
  857. ->leftJoin('business b', 'b.companyNo=s.supplierNo AND b.is_del=0')
  858. ->order("po.addtime desc")
  859. ->withAttr('序号', function () use (&$i) {
  860. return $i++;
  861. })->withAttr('采购单状态', function ($val) use ($status) {
  862. return isset($status[$val]) ? $status[$val] : '';
  863. })->withAttr('发货方式', function ($val) use ($send_type) {
  864. return isset($send_type[$val]) ? $send_type[$val] : '';
  865. })->withAttr('发货方式', function ($val) use ($send_type) {
  866. return isset($send_type[$val]) ? $send_type[$val] : '';
  867. })
  868. ->select()
  869. ->toArray();
  870. foreach ($list as &$value) {
  871. if ($value['order_type'] == 3) {
  872. $temp = Db::name("good_zixun")
  873. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  874. ->field('id,specinfo,good_unit,tax')
  875. ->find();
  876. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  877. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  878. $speclist = [];
  879. foreach ($specinfo as $val) {
  880. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  881. }
  882. $value['税率'] = $temp['tax'] . '%';
  883. } else {
  884. $good_unit = Db::name('good_basic')
  885. ->field('id,good_unit,tax')
  886. ->where(['spuCode' => $value['产品编号']])
  887. ->find();
  888. $spec = Db::name("good_spec")
  889. ->field('id,spec_id,spec_value_id')
  890. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  891. ->select()
  892. ->toArray();
  893. $speclist = [];
  894. if (!empty($spec)) {
  895. foreach ($spec as $val) {
  896. $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', '');
  897. }
  898. }
  899. $value['税率'] = isset($good_unit['tax']) ? $good_unit['tax'] . '%' : '';
  900. }
  901. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  902. $value['单位'] = isset($good_unit['good_unit']) ? Db::name('unit')->where(['id' => $good_unit['good_unit'], 'is_del' => 0])->value('unit', '') : '';
  903. if (!empty($value['addr_code'])) {
  904. $temp = explode(',', $value['addr_code']);
  905. $temp_ = GetAddr(json_encode(['provice_code' => $temp[0], 'city_code' => $temp[1], 'area_code' => $temp[2]]));
  906. $value['收货地址'] = $temp_ . $value['收货地址'];
  907. }
  908. unset($value['addr_code']);
  909. unset($value['order_type']);
  910. }
  911. $headerArr = array_keys($list[0]);
  912. excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
  913. }
  914. }