Order.php 45 KB

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