Sale.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 app\BaseController;
  7. use Exception;
  8. use think\facade\Cache;
  9. use think\facade\Db;
  10. class Sale extends BaseController
  11. {
  12. //发货单列表
  13. public function saleout()
  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 = [["a.is_del", "=", 0], ['wpo.supplierNo', '=', $this->request->user['supplierNo']]];
  19. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  20. if ($orderCode !== "") {
  21. $where[] = ['a.orderCode', "like", "%$orderCode%"];
  22. }
  23. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? trim($this->post['order_type']) : "";
  24. if ($order_type !== "") {
  25. $where[] = ['a.order_type', "=", $order_type];
  26. }
  27. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  28. if ($outCode !== "") {
  29. $where[] = ['a.outCode', "like", "%$outCode%"];
  30. }
  31. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : "";
  32. if ($apply_name !== "") {
  33. $where[] = ['a.apply_name', "like", "%$apply_name%"];
  34. }
  35. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  36. if ($good_code !== "") {
  37. $where[] = ['b.good_code', "like", "%$good_code%"];
  38. }
  39. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  40. if ($good_name !== "") {
  41. $where[] = ['b.good_name', "like", "%$good_name%"];
  42. }
  43. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  44. if ($status !== "") {
  45. $where[] = ['a.status', "=", $status];
  46. }
  47. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  48. if ($start != "") {
  49. $where[] = ["a.addtime", '>=', $start];
  50. }
  51. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  52. if ($end != "") {
  53. $where[] = ["a.addtime", '<=', $end];
  54. }
  55. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  56. if ($companyNo != "") {
  57. $where[] = ['b.customer_code', "like", "%$companyNo%"];
  58. }
  59. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] !== "" ? $this->post['cgdNo'] : "";
  60. if ($cgdNo != "") {
  61. $where[] = ["os.cgdNo", 'like', '%' . $cgdNo . '%'];
  62. }
  63. // $role = $this->checkRole();
  64. // $condition = '';
  65. // if (!empty($role['write']) && $this->uid != "") {
  66. // // $where[]=["a.apply_id","in",$role['write']];
  67. // $condition .= " (b.is_stock=1 and n.contactor = {$this->uid}) or (b.is_stock=0 and wpo.cgder_id = {$this->uid}) or a.apply_id in (" . implode(',',
  68. // $role['write']) . ")";
  69. // }
  70. // if(!empty($role['platform']) ){
  71. // $where[]=["b.platform_id","in",$role['platform']];
  72. // }
  73. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  74. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  75. //发货时间筛选
  76. $start_sendtime = isset($this->post['start_sendtime']) && $this->post['start_sendtime'] !== "" ? $this->post['start_sendtime'] : "";
  77. $end_sendtime = isset($this->post['end_sendtime']) && $this->post['end_sendtime'] !== "" ? $this->post['end_sendtime'] : "";
  78. if ($start_sendtime != "" && $end_sendtime != "") {
  79. $where[] = ["a.sendtime", 'between', [$start_sendtime . " 00:00:00", $end_sendtime . " 223:59:59"]];
  80. $where[] = ["a.status", '>=', 2];//搜索发货时间时,要指定状态为已发货及之后的状态值(0待发货,1待库管发货,2已发货待收货,3已收货,4已全部退货',)
  81. }
  82. $count = Db::name('order_out')
  83. ->alias('a')
  84. ->join("sale b", "b.orderCode=a.orderCode", "left")
  85. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  86. ->join("warehouse_info n", "n.wsm_code=a.wsm_code", "left")
  87. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  88. ->leftJoin("order_send os", "os.outCode=a.outCode")
  89. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  90. ->where($where)
  91. // ->where($condition)
  92. ->count();
  93. $total = ceil($count / $size);
  94. $page = $page >= $total ? $total : $page;
  95. $list = Db::name('order_out')
  96. ->alias('a')
  97. ->join("sale b", "b.orderCode=a.orderCode", "left")
  98. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  99. ->join("warehouse_info n", "n.wsm_code=a.wsm_code AND n.is_del=0", "left")
  100. ->leftJoin("order_send os", "os.outCode=a.outCode")
  101. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  102. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  103. ->field("a.*,b.order_type,b.order_source,b.good_name,b.good_code,b.skuCode,b.supplierNo,b.customer_code,v.companyName,b.origin_price,b.sale_price,b.total_price,os.cgdNo,u.itemid")
  104. ->where($where)
  105. // ->where($condition)
  106. ->order("addtime desc")
  107. ->page($page, $size)
  108. ->cursor();
  109. $data = [];
  110. foreach ($list as $value) {
  111. $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code")
  112. ->where(["k.wsm_code" => $value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  113. $addr = Db::name("order_addr")->where(["id" => $value['addrid']])->find();
  114. if ($addr) {
  115. $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : '';
  116. if (is_string($addinfo) && $addinfo != '') {
  117. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  118. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']);
  119. }
  120. $addr['addr_cn'] = GetAddr(json_encode($addinfo));
  121. }
  122. if ($value['order_type'] == 3) {
  123. $goon = Db::name("good_zixun")->field('id,cat_id')->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  124. } else {
  125. $goon = Db::name('good_platform')->field('a.id,b.cat_id')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')->where(['a.skuCode' => $value['skuCode']])->find();
  126. }
  127. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  128. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  129. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  130. $value['addr'] = isset($addr['addr']) ? $addr['addr_cn'] . $addr['addr'] : "";
  131. $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  132. $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  133. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  134. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  135. $value['sendtime'] = $value['status'] < 2 ? '' : $value['sendtime'];
  136. $data[] = $value;
  137. }
  138. return json_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  139. }
  140. //发货单详情
  141. public function saleOutInfo()
  142. {
  143. $this->post = $this->request->filter('trim')->post();
  144. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  145. if ($outCode == "") {
  146. return json_show(1002, "参数outcode不能为空");
  147. }
  148. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find();
  149. if (empty($codeinfo)) {
  150. return json_show(1002, "未找到出库数据");
  151. }
  152. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  153. if ($item['order_type'] == 3) {
  154. $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find();
  155. if ($goodinfo == false) {
  156. return json_show(1004, "未找到商品数据");
  157. }
  158. } else {
  159. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  160. ->where(['a.skuCode' => $item['skuCode']])->find();
  161. if ($goodinfo == false) {
  162. return json_show(1002, "未找到商品数据");
  163. }
  164. }
  165. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  166. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  167. if ($addr) {
  168. $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : '';
  169. if (is_string($addinfo) && $addinfo != '') {
  170. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  171. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']);
  172. }
  173. $addr['addr_cn'] = GetAddr(json_encode($addinfo));
  174. }
  175. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  176. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  177. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  178. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  179. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  180. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  181. $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : "";
  182. $codeinfo['addr'] = $addr['addr_cn'] ?? '';
  183. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  184. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  185. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  186. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  187. $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime'];
  188. $codeinfo['can'] = $int;
  189. $codeinfo['order_source'] = $item['order_source'];
  190. $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find();
  191. $codeinfo['order_return'] = $orderReturn;
  192. return json_show(0, "获取成功", $codeinfo);
  193. }
  194. //库管发货
  195. public function outSend()
  196. {
  197. $this->post = $this->request->filter('trim')->post();
  198. $outCode = isset($this->post['outCode']) && $this->post['outCode'] != "" ? trim($this->post['outCode']) : "";
  199. if ($outCode == "") {
  200. return json_show(1004, "参数outCode不能为空");
  201. }
  202. $outinfo = Db::name("order_out")->where(["outCode" => $outCode, "is_del" => 0])->find();
  203. if ($outinfo == false) {
  204. return json_show(1004, "发货数据未找到");
  205. }
  206. $einfo = Db::name('sale')->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->find();
  207. if (empty($einfo)) {
  208. return json_show(1002, "未找到销售订单数据");
  209. }
  210. $is_reurn = Db::name("sale_return")->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->where("status", "<", 4)->find();
  211. if ($is_reurn != false) {
  212. return json_show(1002, "销售单存在退货未处理完成");
  213. }
  214. $post_name = isset($this->post['post_name']) && $this->post['post_name'] != "" ? trim($this->post['post_name']) : "";
  215. if ($post_name == "") {
  216. return json_show(1004, "参数post_name不能为空");
  217. }
  218. $post_code = isset($this->post['post_code']) && $this->post['post_code'] != "" ? trim($this->post['post_code']) : "";
  219. if ($post_code == "") {
  220. return json_show(1004, "参数post_code不能为空");
  221. }
  222. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  223. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] != "" ? floatval($this->post['post_fee']) : "";
  224. $outinfo['post_name'] = $post_name;
  225. $outinfo['post_code'] = $post_code;
  226. $outinfo['post_fee'] = $post_fee;
  227. $outinfo['sendtime'] = date("Y-m-d H:i:s");
  228. $old_outinfo_status = $outinfo['status'];
  229. $outinfo['status'] = 2;
  230. $outinfo['updatetime'] = date("Y-m-d H:i:s");
  231. $outinfo['remark'] = $remark;
  232. if ($einfo['wsend_num'] < $outinfo['send_num']) {
  233. return json_show(1004, "订单未发货数量不足");
  234. }
  235. Db::startTrans();
  236. try {
  237. $up = Db::name("order_out")->save($outinfo);
  238. // $user_info = GetUserInfo($this->post['token']);
  239. $uid = $this->request->user['uid'];
  240. $uname = $this->request->user['nickname'];
  241. if ($up) {
  242. //修改状态,添加待办
  243. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  244. "order_code" => $outinfo['outCode'],//出库单号
  245. "status" => $old_outinfo_status,//这里的status是之前的值
  246. "action_remark" => '',//备注
  247. "action_type" => "edit"//新建create,编辑edit,更改状态status
  248. ], "CKD", $outinfo['status'], $outinfo);
  249. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  250. "order_type" => 'CKD',
  251. "order_code" => $outinfo['outCode'],//出库单号
  252. "order_id" => $outinfo['id'],
  253. "order_status" => $outinfo['status'],
  254. "before_status" => $old_outinfo_status,
  255. 'wait_id' => $outinfo['apply_id'],
  256. 'wait_name' => $outinfo['apply_name'],
  257. ]);
  258. $orderstatus = $einfo['status'];
  259. $einfo['send_num'] += $outinfo['send_num'];
  260. $einfo['wsend_num'] -= $outinfo['send_num'];
  261. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  262. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  263. $einfo['updatetime'] = date("Y-m-d H:i:s");
  264. $saleup = Db::name("sale")->save($einfo);
  265. if ($saleup == false) {
  266. Db::rollback();
  267. return json_show(1002, "销售单状态更新失败");
  268. }
  269. // }
  270. //修改状态,添加待办
  271. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  272. "order_code" => $einfo['orderCode'],//出库单号
  273. "status" => $orderstatus,//这里的status是之前的值
  274. "action_remark" => '',//备注
  275. "action_type" => "edit"//新建create,编辑edit,更改状态status
  276. ], "XSQRD", $einfo['status'], $einfo);
  277. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  278. "order_type" => 'XSQRD',
  279. "order_code" => $einfo['orderCode'],//出库单号
  280. "order_id" => $einfo['id'],
  281. "order_status" => $einfo['status'], "before_status" => $orderstatus
  282. ]);
  283. $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find();
  284. if ($stokc == false) {
  285. Db::rollback();
  286. return json_show(1002, "未找到库存数据");
  287. } else {
  288. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  289. Db::rollback();
  290. return json_show(1002, "超出库存数量");
  291. }
  292. $stokc['wait_out_stock'] -= $outinfo['send_num'];
  293. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  294. // $stokc['intra_stock']+=$outinfo['send_num'];
  295. $stokc['updatetime'] = date("Y-m-d H:i:s");
  296. }
  297. $stoc = Db::name("good_stock")->save($stokc);
  298. if ($stoc == false) {
  299. Db::rollback();
  300. return json_show(1002, "库存更新失败");
  301. }
  302. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stokc['id'], "type" => 2, 'stock' => $outinfo['send_num'], "stock_name" => "wait_out_stock"];
  303. GoodLog::LogAdd(['id' => $uid, 'nickname' => $uname], $good_data, "CKD");
  304. Cache::store("redis")->handler()->lPush("SENDOUT", $outCode);
  305. Db::commit();
  306. } else {
  307. Db::rollback();
  308. return json_show(1004, "发货失败");
  309. }
  310. //如果是有赞订单的话,将发货信息推到有赞
  311. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  312. if ($einfo['order_source'] == 5) {
  313. $res = curl_request(config('app.yz_domain') . 'api/yz_out_send', ['orderCode' => $einfo['orderCode'], 'out_stype' => $post_name, 'post_code' => $post_code, 'uid' => $uid, 'uname' => $uname, 'order_out' => $outCode]);
  314. $res = json_decode($res, true);
  315. if ($res['code'] != 0) return json_show(0, '发货成功,' . $res['message']);
  316. }
  317. return json_show(0, "发货成功");
  318. } catch (\Exception $e) {
  319. Db::rollback();
  320. return json_show(1004, $e->getMessage());
  321. }
  322. }
  323. //库管批量发货
  324. public function outSendBatchByImport()
  325. {
  326. $param = $this->request->only(['list', 'token'], 'post', 'trim');
  327. if (empty($param['list'])) return json_show(1005, 'list参数不能为空');
  328. // $user = GetUserInfo($param['token']);
  329. $createrid = $this->request->user['uid'];
  330. $creater = $this->request->user['nickname'];
  331. //所有发货单信息
  332. $order_out_infos = Db::name("order_out")
  333. ->where('status', 1)
  334. ->where('is_del', 0)
  335. ->whereIn('outCode', array_column($param['list'], 'outCode'))
  336. ->column('id,status,send_num,orderCode,wsm_code,apply_id,apply_name', 'outCode');
  337. //处理数据
  338. Db::startTrans();
  339. try {
  340. $good_data = [];
  341. $temp_out_codes = [];
  342. foreach ($param['list'] as $value) {
  343. if (!isset($order_out_infos[$value['outCode']])) throw new Exception($value['outCode'] . '该发货单未找到或状态不允许发货');
  344. //每次循环都需要重新查询对应数据,为了数据及时更新
  345. $sale_infos = Db::name("sale")
  346. ->where('orderCode', $order_out_infos[$value['outCode']]['orderCode'])
  347. ->where('is_del', 0)
  348. ->column('id,send_num,wsend_num,status,good_code,order_source', 'orderCode');
  349. if (!isset($sale_infos[$order_out_infos[$value['outCode']]['orderCode']])) throw new Exception($value['outCode'] . '该发货单对应的销售单未找到');
  350. //判断发货单是否重复
  351. if (isset($temp_out_codes[$value['outCode']])) {
  352. Db::rollback();
  353. return json_show(1005, '下列发货申请单号重复', [$value]);
  354. } else $temp_out_codes[$value['outCode']] = $value['outCode'];
  355. //【发货单】
  356. //查询已有发货单信息及状态
  357. //更新发货单
  358. $up = Db::name("order_out")
  359. ->where(['id' => $order_out_infos[$value['outCode']]['id'], 'outCode' => $value['outCode'], 'status' => 1, "is_del" => 0])
  360. ->update([
  361. 'post_name' => isset($value['post_name']) ? $value['post_name'] : '',
  362. 'post_code' => $value['post_code'],
  363. 'post_fee' => $value['post_fee'],
  364. 'sendtime' => date('Y-m-d H:i:s'),
  365. 'status' => 2,
  366. 'updatetime' => date('Y-m-d H:i:s'),
  367. 'remark' => isset($value['remark']) ? $value['remark'] : '',
  368. ]);
  369. if ($up) {
  370. //修改状态,添加待办
  371. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  372. "order_code" => $value['outCode'],//出库单号
  373. "status" => $order_out_infos[$value['outCode']]['status'],//这里的status是之前的值
  374. "action_remark" => '',//备注
  375. "action_type" => "edit"//新建create,编辑edit,更改状态status
  376. ], "CKD", 2, $order_out_infos[$value['outCode']]);
  377. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  378. "order_type" => 'CKD',
  379. "order_code" => $value['outCode'],//出库单号
  380. "order_id" => $order_out_infos[$value['outCode']]['id'],
  381. "order_status" => 2,
  382. "before_status" => $order_out_infos[$value['outCode']]['status'],
  383. 'wait_id' => $order_out_infos[$value['outCode']]['apply_id'],
  384. 'wait_name' => $order_out_infos[$value['outCode']]['apply_name'],
  385. ]);
  386. //【销售单】
  387. $einfo = [];
  388. $einfo['send_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['send_num'] + $order_out_infos[$value['outCode']]['send_num'];
  389. $einfo['wsend_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['wsend_num'] - $order_out_infos[$value['outCode']]['send_num'];
  390. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  391. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  392. $einfo['updatetime'] = date("Y-m-d H:i:s");
  393. $saleup = Db::name("sale")
  394. ->where('id', $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'])
  395. ->update($einfo);
  396. if ($saleup == false) throw new Exception($value['outCode'] . '销售单状态更新失败');
  397. //修改状态,添加待办
  398. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  399. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//销售单号
  400. "status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],//这里的status是之前的值
  401. "action_remark" => '',//备注
  402. "action_type" => "edit"//新建create,编辑edit,更改状态status
  403. ], "XSQRD", $einfo['status'], $einfo);
  404. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  405. "order_type" => 'XSQRD',
  406. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//出库单号
  407. "order_id" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'],
  408. "order_status" => $einfo['status'], "before_status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status']
  409. ]);
  410. //【库存】
  411. $stock = Db::name("good_stock")
  412. ->field('id,wait_out_stock,usable_stock,total_stock,updatetime')
  413. ->where([
  414. "is_del" => 0,
  415. 'spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'],
  416. "wsm_code" => $order_out_infos[$value['outCode']]['wsm_code'],
  417. ])->find();
  418. if ($stock == false) throw new Exception($value['outCode'] . '未找到库存数据');
  419. else {
  420. if ($order_out_infos[$value['outCode']]['send_num'] > $stock['wait_out_stock']) throw new Exception($value['outCode'] . '超出库存数量');
  421. $stock['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num'];
  422. $stock['total_stock'] = $stock['wait_out_stock'] + $stock['usable_stock'];
  423. $stock['updatetime'] = date("Y-m-d H:i:s");
  424. $stock_rs = Db::name("good_stock")
  425. ->where('id', $stock['id'])
  426. ->update($stock);
  427. if ($stock_rs == false) throw new Exception($value['outCode'] . '库存更新失败');
  428. $good_data[] = [
  429. 'good_log_code' => $value['outCode'],
  430. 'stock_id' => $stock['id'],
  431. 'type' => 2,
  432. 'stock' => $order_out_infos[$value['outCode']]['send_num'],
  433. 'stock_name' => 'wait_out_stock'
  434. ];
  435. }
  436. } else throw new Exception($value['outCode'] . '发货失败');
  437. Cache::store("redis")->handler()->lPush("SENDOUT", $value['outCode']);
  438. }
  439. if (!empty($good_data)) GoodLog::LogAdd(['id' => $createrid, 'nickname' => $creater], $good_data, "CKD");
  440. Db::commit();
  441. return json_show(0, '发货成功');
  442. } catch (Exception $exception) {
  443. Db::rollback();
  444. return json_show(1004, $exception->getMessage());
  445. }
  446. }
  447. }