AfterChild.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Exception;
  4. use think\facade\Db;
  5. use think\facade\Validate;
  6. //退货工单
  7. class AfterChild extends Base
  8. {
  9. //退货工单列表
  10. public function getList()
  11. {
  12. $param = $this->request->only(['page' => 1, 'size' => 10, 'returnCode' => '', 'status' => '', 'orderCode' => '', 'outCode' => ''], 'post', 'trim');
  13. $where = [['a.is_del', '=', 0]];
  14. if ($param['returnCode'] != '') $where[] = ['a.returnCode', 'like', '%' . $param['returnCode'] . '%'];
  15. if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
  16. if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
  17. if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
  18. $count = Db::name('order_return_child')
  19. ->alias('a')
  20. ->leftJoin('warehouse_info b', 'b.wsm_code=a.send_wsm_code')
  21. ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code')
  22. ->where($where)
  23. ->count('a.id');
  24. $list = Db::name('order_return_child')
  25. ->alias('a')
  26. ->field('a.id,a.returnCode,a.orderCode,a.outCode,a.order_out_child_status,a.outChildCode,b.supplierName send_supplierName,b.supplierNo send_supplierNo,b.name send_wsm_name,a.send_wsm_code,b.contactor_name send_contactor_name,a.send_num,a.return_num,c.supplierName return_supplierName,c.supplierNo return_supplierNo,c.name return_wsm_name,a.return_wsm_code,c.contactor_name return_contactor_name,a.status')
  27. ->leftJoin('warehouse_info b', 'b.wsm_code=a.send_wsm_code')
  28. ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code')
  29. ->where($where)
  30. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  31. ->page($param['page'], $param['size'])
  32. ->select()
  33. ->toArray();
  34. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  35. }
  36. //设置退货工单
  37. public function setOrderReturnChild()
  38. {
  39. $param = $this->request->only(['returnCode', 'list'], 'post', 'trim');
  40. $val = Validate::rule([
  41. 'returnCode|售后申请单编码' => 'require',
  42. 'list|工单集合' => 'require|array|max:100',
  43. ]);
  44. if ($val->check($param) == false) return json_show(1004, $val->getError());
  45. $info = Db::name('order_return')
  46. ->where(['is_del' => 0, 'returnCode' => $param['returnCode']])
  47. ->findOrEmpty();
  48. if (empty($info)) return json_show(1004, '售后申请单不存在');
  49. if ($info['status'] != 11) return json_show(1004, '售后申请单状态错误');
  50. if ($info['is_receive'] != 1) return json_show(1004, '未收到货,无需设置退货工单');
  51. $order_out = Db::name('order_out')
  52. ->field('id,status order_out_status,orderCode')
  53. ->where(['is_del' => 0, 'outCode' => $info['outCode']])
  54. ->findOrEmpty();
  55. if (empty($order_out)) return json_show(1004, '未找到对应的发货单');
  56. $val_child = Validate::rule([
  57. 'outChildCode|发货工单号' => 'require',
  58. 'return_num|退货数量' => 'require|number|gt:0|max:999999999999',
  59. 'return_wsm_code|退货仓库编码' => 'require'
  60. ]);
  61. //发货工单详情
  62. $order_out_child = Db::name('order_out_child')
  63. ->where(['is_del' => 0, 'outCode' => $info['outCode'], 'outChildCode' => array_column($param['list'], 'outChildCode')])
  64. ->column('num,wsm_code,status', 'outChildCode');
  65. //所有仓库详情
  66. $wsm_info = Db::name('warehouse_info')
  67. ->where(['is_del' => 0, 'wsm_code' => array_unique(array_merge(array_column($order_out_child, 'wsm_code'), array_column($param['list'], 'return_wsm_code')))])
  68. ->column('id', 'wsm_code');
  69. Db::startTrans();
  70. try {
  71. $insert = [];
  72. $date = date('Y-m-d H:i:s');
  73. foreach ($param['list'] as $value) {
  74. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  75. if (!isset($order_out_child[$value['outChildCode']])) throw new Exception($value['outChildCode'] . '发货工单不存在');
  76. if (!isset($wsm_info[$value['return_wsm_code']])) throw new Exception($value['return_wsm_code'] . '退货仓库不存在');
  77. $insert[] = [
  78. 'returnCode' => $param['returnCode'],
  79. 'orderCode' => $order_out['orderCode'],
  80. 'outCode' => $info['outCode'],
  81. 'order_out_child_status' => $order_out_child[$value['outChildCode']]['status'],
  82. 'outChildCode' => $value['outChildCode'],
  83. 'send_wsm_code' => $order_out_child[$value['outChildCode']]['wsm_code'],
  84. 'send_num' => $order_out_child[$value['outChildCode']]['num'],
  85. 'return_num' => $value['return_num'],
  86. 'return_wsm_code' => $value['return_wsm_code'],
  87. 'can_sell_num' => 0,
  88. 'defective_num' => 0,
  89. 'loss_num' => 0,
  90. 'apply_id' => $this->uid,
  91. 'apply_name' => $this->uname,
  92. 'status' => 1,
  93. 'is_del' => 0,
  94. 'remark' => '',
  95. 'addtime' => $date,
  96. 'updatetime' => $date,
  97. ];
  98. }
  99. if ($insert) Db::name('order_return_child')->insertAll($insert);
  100. Db::name('order_return')
  101. ->where(['is_del' => 0, 'id' => $info['id'], 'status' => 11])
  102. ->update(['status' => 4, 'updatetime' => $date]);
  103. Db::commit();
  104. return json_show(0, '设置退货工单成功');
  105. } catch (Exception $exception) {
  106. Db::rollback();
  107. return json_show(1004, $exception->getMessage());
  108. }
  109. }
  110. //库管收货
  111. public function deliveryOrderReturnChild()
  112. {
  113. $param = $this->request->only(['returnCode', 'list'], 'post', 'trim');
  114. $val = Validate::rule([
  115. 'returnCode|退货申请单号' => 'require',
  116. 'list|退货工单集合' => 'require|array|max:100',
  117. ]);
  118. if ($val->check($param) == false) return json_show(1004, $val->getError());
  119. $order_return = Db::name('order_return')
  120. ->alias('a')
  121. ->field('a.id,a.good_code,a.outCode,b.orderCode,c.is_stock,c.order_type,a.status')
  122. ->leftJoin('order_out b', 'b.outCode=a.outCode AND b.is_del=0')
  123. ->leftJoin('sale c', 'c.orderCode=b.orderCode AND c.is_del=0')
  124. ->where(['a.is_del' => 0, 'a.returnCode' => $param['returnCode']])
  125. ->findOrEmpty();
  126. if (empty($order_return)) return json_show(1004, '售后申请单不存在');
  127. if ($order_return['status'] != 12) return json_show(1004, '售后申请单状态有误');
  128. $info = Db::name('order_return_child')
  129. ->alias('a')
  130. ->leftJoin('order_out_child b', 'b.outChildCode=a.outChildCode')
  131. ->where(['a.is_del' => 0, 'a.returnCode' => $param['returnCode'], 'a.id' => array_column($param['list'], 'id')])
  132. ->column('a.id,a.return_wsm_code,a.outChildCode,b.wsm_code,a.status', 'a.id');
  133. if (empty($info)) return json_show(1004, '退货工单不存在');
  134. $val_child = Validate::rule([
  135. 'id|退货工单id' => 'require|gt:0|max:999999999',
  136. 'can_sell_num|销售仓入库数量' => 'require|number|egt:0|max:999999999999',
  137. 'defective_num|次品仓入库数量' => 'require|number|egt:0|max:999999999999',
  138. 'loss_num|丢失数量' => 'require|number|egt:0|max:999999999999',
  139. 'remark|备注' => 'max:255'
  140. ]);
  141. Db::startTrans();
  142. try {
  143. $date = date('Y-m-d H:i:s');
  144. foreach ($param['list'] as $value) {
  145. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  146. if (!isset($info[$value['id']])) throw new Exception($value['id'] . '退货工单不存在');
  147. if ($info[$value['id']]['status'] != 1) throw new Exception($value['id'] . '退货工单状态有误');
  148. //退货工单
  149. Db::name('order_return_child')
  150. ->where(['is_del' => 0, 'id' => $value['id']])
  151. ->update([
  152. 'can_sell_num' => $value['can_sell_num'],
  153. 'defective_num' => $value['defective_num'],
  154. 'loss_num' => $value['loss_num'],
  155. 'remark' => $value['remark'] ?? '',
  156. 'updatetime' => $date,
  157. 'status' => 2
  158. ]);
  159. }
  160. $total = array_sum(array_column($param['list'], 'can_sell_num'));
  161. if ($total > 0) {
  162. if ($order_return['is_stock'] != 1) {
  163. //根据转化表判断该非库存品是否转化为库存品
  164. $spuCode = Db::name('good_change_stock')
  165. ->where(['old_spuCode' => $order_return['good_code'], 'old_order_type' => $order_return['order_type']])
  166. ->value('spuCode', '');
  167. if ($spuCode == '') {
  168. //将非库存品转化为库存品
  169. $spuCode = makeNo('SKU');
  170. if ($order_return['order_type'] == 2) {
  171. $good = Db::name('good_basic')
  172. ->where('spuCode', $order_return['good_code'])
  173. ->findOrEmpty();
  174. if (!empty($good)) {
  175. Db::name('good_basic')->insert(array_merge($good, [
  176. 'id' => null,
  177. 'spuCode' => $spuCode,
  178. 'addtime' => $date,
  179. 'updatetime' => $date,
  180. ]));
  181. }
  182. } else {
  183. $good = Db::name('good_zixun')
  184. ->where([])
  185. ->findOrEmpty();
  186. if (!empty($good)) {
  187. Db::name('good_basic')->insert([
  188. 'spuCode' => $spuCode,
  189. 'good_code' => $good['good_code'],
  190. 'good_name' => $good['good_name'],
  191. 'cat_id' => $good['cat_id'],
  192. 'brand_id' => $good['brand_id'],
  193. 'companyNo' => $good['companyNo'],
  194. 'companyName' => $good['companyName'],
  195. 'good_unit' => $good['good_unit'],
  196. 'good_type' => $good['good_type'],
  197. 'moq' => $good['moq'],
  198. 'is_exclusive' => 0,
  199. 'customized' => $good['customized'],
  200. 'tax' => $good['tax'],
  201. 'supplierNo' => $good['supplierNo'],
  202. 'supplierName' => $good['supplierName'],
  203. 'is_auth' => $good['is_auth'],
  204. 'auth_img' => $good['auth_img'],
  205. 'is_stock' => 1,
  206. 'after_sales' => '',
  207. 'craft_desc' => $good['craft_desc'],
  208. 'good_remark' => $good['good_remark'],
  209. 'good_size' => '',
  210. 'weight' => $good['weight'],
  211. 'packing_way' => '',
  212. 'packing_size' => '',
  213. 'packing_spec' => '',
  214. 'packing_list' => '',
  215. 'packing_weight' => 0,
  216. 'good_bar' => '',
  217. 'supply_area' => $good['supply_area'],
  218. 'delivery_place' => '',
  219. 'origin_place' => '',
  220. 'delivery_day' => '',
  221. 'lead_time' => '',
  222. 'sample_day' => '',
  223. 'sample_fee' => '',
  224. 'good_img' => $good['good_img'],
  225. 'good_thumb_img' => $good['good_thumb_img'],
  226. 'good_info_img' => $good['good_info_img'],
  227. 'cert_fee' => 0,
  228. 'packing_fee' => 0,
  229. 'cost_fee' => 0,
  230. 'mark_fee' => 0,
  231. 'demo_fee' => 0,
  232. 'open_fee' => 0,
  233. 'noble_metal' => $good['noble_metal'],
  234. 'noble_weight' => 0,
  235. 'is_gold_price' => $good['is_gold_price'],
  236. 'cgd_gold_price' => 0,
  237. 'market_price' => 0,
  238. 'nake_price' => 0,
  239. 'is_step' => 0,
  240. 'is_online' => 0,
  241. 'status' => 0,
  242. 'createrid' => $good['createrid'],
  243. 'creater' => $good['creater'],
  244. 'field_change' => '',
  245. 'is_del' => 0,
  246. 'addtime' => $date,
  247. 'updatetime' => $date,
  248. 'is_diff' => 0,
  249. 'config' => '',
  250. 'other_config' => '',
  251. 'stock_moq' => 0,
  252. 'is_support_barter' => 1,
  253. 'chargerid' => $good['createrid'],
  254. 'charger' => $good['creater'],
  255. 'is_support_stock' => 1
  256. ]);
  257. }
  258. }
  259. if (!empty($good)) {
  260. //关联表增数据
  261. Db::name('good_change_stock')
  262. ->insert([
  263. 'old_spuCode' => $order_return['good_code'],
  264. 'old_order_type' => $order_return['order_type'],
  265. 'spuCode' => $spuCode,
  266. 'addtime' => $date,
  267. 'updatetime' => $date,
  268. ]);
  269. } else throw new Exception('未找到对应的商品数据');
  270. }
  271. //新增bn记录,维护旧有bn记录
  272. $child_bns = Db::name('child_bn')
  273. ->field('id,num,origin_price')
  274. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode']])
  275. ->order(['num' => 'desc', 'id' => 'asc'])
  276. ->cursor();
  277. $origin_price = $j = 0;
  278. foreach ($child_bns as $child_bn) {
  279. if ($origin_price === 0) $origin_price = $child_bn['origin_price'];
  280. Db::name('child_bn')
  281. ->where(['id' => $child_bn['id']])
  282. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  283. ->update(['updatetime' => $date]);
  284. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  285. if ($total == 0) break;
  286. }
  287. foreach ($param['list'] as $value) {
  288. if ($value['can_sell_num'] > 0) {
  289. $stockid = Db::name('good_stock')
  290. ->insertGetId([
  291. 'project_code' => '',
  292. 'spuCode' => $spuCode,
  293. 'wsm_code' => $info[$value['id']]['return_wsm_code'],
  294. 'wait_in_stock' => 0,
  295. 'wait_out_stock' => 0,
  296. 'usable_stock' => $value['can_sell_num'],
  297. 'intra_stock' => 0,
  298. 'total_stock' => $value['can_sell_num'],
  299. 'status' => 1,
  300. 'is_del' => 0,
  301. 'warn_stock' => 0,
  302. 'addtime' => $date,
  303. 'updatetime' => $date,
  304. ]);
  305. Db::name('good_stock_info')
  306. ->insert([
  307. 'stockid' => $stockid,
  308. 'bnCode' => substr(makeNo("BN"), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
  309. 'total_num' => $value['can_sell_num'],
  310. 'used_num' => 0,
  311. 'balance_num' => $value['can_sell_num'],
  312. 'origin_price' => $origin_price,
  313. 'addtime' => $date,
  314. 'updatetime' => $date,
  315. ]);
  316. }
  317. }
  318. } else {
  319. //库存品,直接维护stock和info
  320. $stockids = Db::name('good_stock')
  321. ->where(['is_del' => 0, 'spuCode' => $order_return['good_code'], 'wsm_code' => array_column($info, 'return_wsm_code')])
  322. ->column('id', 'wsm_code');
  323. foreach ($param['list'] as $value) {
  324. //定义了stockid,维护bn
  325. if (isset($stockids[$info[$value['id']]['return_wsm_code']])) {
  326. $good_stock_info = Db::name('good_stock_info')
  327. ->where(['stockid' => $stockids[$info[$value['id']]['return_wsm_code']]])
  328. ->findOrEmpty();
  329. if (empty($good_stock_info)) {
  330. //从child_bn查询bn号并维护,新建到good_stock_info中
  331. $child_bns = Db::name('child_bn')
  332. ->field('id,num,bnCode,origin_price')
  333. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  334. ->order(['num' => 'desc', 'id' => 'asc'])
  335. ->cursor();
  336. $total = $value['can_sell_num'];
  337. foreach ($child_bns as $child_bn) {
  338. Db::name('child_bn')
  339. ->where(['id' => $child_bn['id']])
  340. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  341. ->update(['updatetime' => $date]);
  342. Db::name('good_stock_info')
  343. ->insert([
  344. 'stockid' => $stockids[$info[$value['id']]['return_wsm_code']],
  345. 'bnCode' => $child_bn['bnCode'],
  346. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  347. 'used_num' => 0,
  348. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  349. 'origin_price' => $child_bn['origin_price'],
  350. 'addtime' => $date,
  351. 'updatetime' => $date,
  352. ]);
  353. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  354. if ($total == 0) break;
  355. }
  356. } else {
  357. //维护记录
  358. Db::name('good_stock_info')
  359. ->where(['id' => $good_stock_info['id']])
  360. ->inc('balance_num', $value['can_sell_num'])//可用数量增加
  361. ->dec('used_num', $value['can_sell_num'])//已用数量减少
  362. ->update(['updatetime' => $date]);
  363. }
  364. } else {
  365. //新建good_stock和good_stock_info
  366. $stockid = Db::name('good_stock')
  367. ->insertGetId([
  368. 'project_code' => '',
  369. 'spuCode' => $order_return['good_code'],
  370. 'wsm_code' => $value['return_wsm_code'],
  371. 'wait_in_stock' => 0,
  372. 'wait_out_stock' => 0,
  373. 'usable_stock' => $value['can_sell_num'],
  374. 'intra_stock' => 0,
  375. 'total_stock' => $value['can_sell_num'],
  376. 'status' => 1,
  377. 'is_del' => 0,
  378. 'warn_stock' => 0,
  379. 'addtime' => $date,
  380. 'updatetime' => $date,
  381. ]);
  382. //从child_bn查询bn号并维护,新建到good_stock_info中
  383. $child_bns = Db::name('child_bn')
  384. ->field('id,num,bnCode,origin_price')
  385. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  386. ->order(['num' => 'desc', 'id' => 'asc'])
  387. ->cursor();
  388. $total = $value['can_sell_num'];
  389. foreach ($child_bns as $child_bn) {
  390. Db::name('child_bn')
  391. ->where(['id' => $child_bn['id']])
  392. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  393. ->update(['updatetime' => $date]);
  394. Db::name('good_stock_info')
  395. ->insert([
  396. 'stockid' => $stockid,
  397. 'bnCode' => $child_bn['bnCode'],
  398. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  399. 'used_num' => 0,
  400. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  401. 'origin_price' => $child_bn['origin_price'],
  402. 'addtime' => $date,
  403. 'updatetime' => $date,
  404. ]);
  405. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  406. if ($total == 0) break;
  407. }
  408. }
  409. }
  410. }
  411. }
  412. //售后申请单状态维护
  413. Db::name('order_return')
  414. ->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'status' => 12])
  415. ->update(['status' => 5, 'updatetime' => $date]);
  416. Db::commit();
  417. return json_show(0, '库管收货成功');
  418. } catch (Exception $exception) {
  419. Db::rollback();
  420. return json_show(1004, $exception->getMessage());
  421. }
  422. }
  423. //更新退货工单标记
  424. public function setRecord()
  425. {
  426. $param = $this->request->only(['id', 'record'], 'post', 'trim');
  427. $val = Validate::rule([
  428. 'id|退货工单id' => 'require|number|gt:0',
  429. 'record|标记内容' => 'require',
  430. ]);
  431. if ($val->check($param) == false) return json_show(1004, $val->getError());
  432. $temp = Db::name('order_return_child')
  433. ->field('id')
  434. ->where(['is_del' => 0, 'id' => $param['id']])
  435. ->findOrEmpty();
  436. if (empty($temp)) return json_show(1004, '该退货工单不存在');
  437. $rs = Db::name('order_return_child')
  438. ->where(['is_del' => 0, 'id' => $param['id']])
  439. ->update(['record' => $param['record'], 'updatetime' => date('Y-m-d H:i:s')]);
  440. return $rs ? json_show(0, '更新退货工单标记内容成功') : json_show(1004, '更新退货工单标记内容失败');
  441. }
  442. }