AfterChild.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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.return_num', '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. Db::name('order_out_child')
  161. ->where(['is_del' => 0, 'outChildCode' => $info[$value['id']]['outChildCode']])
  162. ->dec('num', $info[$value['id']]['return_num'])
  163. ->update(['updatetime' => $date]);
  164. }
  165. $total = array_sum(array_column($param['list'], 'can_sell_num'));
  166. if ($total > 0) {
  167. if ($order_return['is_stock'] != 1) {
  168. //根据转化表判断该非库存品是否转化为库存品
  169. $spuCode = Db::name('good_change_stock')
  170. ->where(['old_spuCode' => $order_return['good_code'], 'old_order_type' => $order_return['order_type']])
  171. ->value('spuCode', '');
  172. if ($spuCode == '') {
  173. //将非库存品转化为库存品
  174. $spuCode = makeNo('SKU');
  175. if ($order_return['order_type'] == 2) {
  176. $good = Db::name('good_basic')
  177. ->where('spuCode', $order_return['good_code'])
  178. ->findOrEmpty();
  179. if (!empty($good)) {
  180. Db::name('good_basic')->insert(array_merge($good, [
  181. 'id' => null,
  182. 'spuCode' => $spuCode,
  183. 'addtime' => $date,
  184. 'updatetime' => $date,
  185. ]));
  186. }
  187. } else {
  188. $good = Db::name('good_zixun')
  189. ->where([])
  190. ->findOrEmpty();
  191. if (!empty($good)) {
  192. Db::name('good_basic')->insert([
  193. 'spuCode' => $spuCode,
  194. 'good_code' => $good['good_code'],
  195. 'good_name' => $good['good_name'],
  196. 'cat_id' => $good['cat_id'],
  197. 'brand_id' => $good['brand_id'],
  198. 'companyNo' => $good['companyNo'],
  199. 'companyName' => $good['companyName'],
  200. 'good_unit' => $good['good_unit'],
  201. 'good_type' => $good['good_type'],
  202. 'moq' => $good['moq'],
  203. 'is_exclusive' => 0,
  204. 'customized' => $good['customized'],
  205. 'tax' => $good['tax'],
  206. 'supplierNo' => $good['supplierNo'],
  207. 'supplierName' => $good['supplierName'],
  208. 'is_auth' => $good['is_auth'],
  209. 'auth_img' => $good['auth_img'],
  210. 'is_stock' => 1,
  211. 'after_sales' => '',
  212. 'craft_desc' => $good['craft_desc'],
  213. 'good_remark' => $good['good_remark'],
  214. 'good_size' => '',
  215. 'weight' => $good['weight'],
  216. 'packing_way' => '',
  217. 'packing_size' => '',
  218. 'packing_spec' => '',
  219. 'packing_list' => '',
  220. 'packing_weight' => 0,
  221. 'good_bar' => '',
  222. 'supply_area' => $good['supply_area'],
  223. 'delivery_place' => '',
  224. 'origin_place' => '',
  225. 'delivery_day' => '',
  226. 'lead_time' => '',
  227. 'sample_day' => '',
  228. 'sample_fee' => '',
  229. 'good_img' => $good['good_img'],
  230. 'good_thumb_img' => $good['good_thumb_img'],
  231. 'good_info_img' => $good['good_info_img'],
  232. 'cert_fee' => 0,
  233. 'packing_fee' => 0,
  234. 'cost_fee' => 0,
  235. 'mark_fee' => 0,
  236. 'demo_fee' => 0,
  237. 'open_fee' => 0,
  238. 'noble_metal' => $good['noble_metal'],
  239. 'noble_weight' => 0,
  240. 'is_gold_price' => $good['is_gold_price'],
  241. 'cgd_gold_price' => 0,
  242. 'market_price' => 0,
  243. 'nake_price' => 0,
  244. 'is_step' => 0,
  245. 'is_online' => 0,
  246. 'status' => 0,
  247. 'createrid' => $good['createrid'],
  248. 'creater' => $good['creater'],
  249. 'field_change' => '',
  250. 'is_del' => 0,
  251. 'addtime' => $date,
  252. 'updatetime' => $date,
  253. 'is_diff' => 0,
  254. 'config' => '',
  255. 'other_config' => '',
  256. 'stock_moq' => 0,
  257. 'is_support_barter' => 1,
  258. 'chargerid' => $good['createrid'],
  259. 'charger' => $good['creater'],
  260. 'is_support_stock' => 1
  261. ]);
  262. }
  263. }
  264. if (!empty($good)) {
  265. //关联表增数据
  266. Db::name('good_change_stock')
  267. ->insert([
  268. 'old_spuCode' => $order_return['good_code'],
  269. 'old_order_type' => $order_return['order_type'],
  270. 'spuCode' => $spuCode,
  271. 'addtime' => $date,
  272. 'updatetime' => $date,
  273. ]);
  274. } else throw new Exception('未找到对应的商品数据');
  275. }
  276. //新增bn记录,维护旧有bn记录
  277. $child_bns = Db::name('child_bn')
  278. ->field('id,num,origin_price')
  279. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode']])
  280. ->order(['num' => 'desc', 'id' => 'asc'])
  281. ->cursor();
  282. $origin_price = $j = 0;
  283. foreach ($child_bns as $child_bn) {
  284. if ($origin_price === 0) $origin_price = $child_bn['origin_price'];
  285. Db::name('child_bn')
  286. ->where(['id' => $child_bn['id']])
  287. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  288. ->update(['updatetime' => $date]);
  289. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  290. if ($total == 0) break;
  291. }
  292. foreach ($param['list'] as $value) {
  293. if ($value['can_sell_num'] > 0) {
  294. $stockid = Db::name('good_stock')
  295. ->insertGetId([
  296. 'project_code' => '',
  297. 'spuCode' => $spuCode,
  298. 'wsm_code' => $info[$value['id']]['return_wsm_code'],
  299. 'wait_in_stock' => 0,
  300. 'wait_out_stock' => 0,
  301. 'usable_stock' => $value['can_sell_num'],
  302. 'intra_stock' => 0,
  303. 'total_stock' => $value['can_sell_num'],
  304. 'status' => 1,
  305. 'is_del' => 0,
  306. 'warn_stock' => 0,
  307. 'addtime' => $date,
  308. 'updatetime' => $date,
  309. ]);
  310. Db::name('good_stock_info')
  311. ->insert([
  312. 'stockid' => $stockid,
  313. 'bnCode' => substr(makeNo("BN"), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
  314. 'total_num' => $value['can_sell_num'],
  315. 'used_num' => 0,
  316. 'balance_num' => $value['can_sell_num'],
  317. 'origin_price' => $origin_price,
  318. 'addtime' => $date,
  319. 'updatetime' => $date,
  320. ]);
  321. }
  322. }
  323. } else {
  324. //库存品,直接维护stock和info
  325. $stockids = Db::name('good_stock')
  326. ->where(['is_del' => 0, 'spuCode' => $order_return['good_code'], 'wsm_code' => array_column($info, 'return_wsm_code')])
  327. ->column('id', 'wsm_code');
  328. foreach ($param['list'] as $value) {
  329. //定义了stockid,维护bn
  330. if (isset($stockids[$info[$value['id']]['return_wsm_code']])) {
  331. $good_stock_info = Db::name('good_stock_info')
  332. ->where(['stockid' => $stockids[$info[$value['id']]['return_wsm_code']]])
  333. ->findOrEmpty();
  334. if (empty($good_stock_info)) {
  335. //从child_bn查询bn号并维护,新建到good_stock_info中
  336. $child_bns = Db::name('child_bn')
  337. ->field('id,num,bnCode,origin_price')
  338. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  339. ->order(['num' => 'desc', 'id' => 'asc'])
  340. ->cursor();
  341. $total = $value['can_sell_num'];
  342. foreach ($child_bns as $child_bn) {
  343. Db::name('child_bn')
  344. ->where(['id' => $child_bn['id']])
  345. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  346. ->update(['updatetime' => $date]);
  347. Db::name('good_stock_info')
  348. ->insert([
  349. 'stockid' => $stockids[$info[$value['id']]['return_wsm_code']],
  350. 'bnCode' => $child_bn['bnCode'],
  351. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  352. 'used_num' => 0,
  353. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  354. 'origin_price' => $child_bn['origin_price'],
  355. 'addtime' => $date,
  356. 'updatetime' => $date,
  357. ]);
  358. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  359. if ($total == 0) break;
  360. }
  361. } else {
  362. //维护记录
  363. Db::name('good_stock_info')
  364. ->where(['id' => $good_stock_info['id']])
  365. ->inc('balance_num', $value['can_sell_num'])//可用数量增加
  366. ->dec('used_num', $value['can_sell_num'])//已用数量减少
  367. ->update(['updatetime' => $date]);
  368. }
  369. } else {
  370. //新建good_stock和good_stock_info
  371. $stockid = Db::name('good_stock')
  372. ->insertGetId([
  373. 'project_code' => '',
  374. 'spuCode' => $order_return['good_code'],
  375. 'wsm_code' => $value['return_wsm_code'],
  376. 'wait_in_stock' => 0,
  377. 'wait_out_stock' => 0,
  378. 'usable_stock' => $value['can_sell_num'],
  379. 'intra_stock' => 0,
  380. 'total_stock' => $value['can_sell_num'],
  381. 'status' => 1,
  382. 'is_del' => 0,
  383. 'warn_stock' => 0,
  384. 'addtime' => $date,
  385. 'updatetime' => $date,
  386. ]);
  387. //从child_bn查询bn号并维护,新建到good_stock_info中
  388. $child_bns = Db::name('child_bn')
  389. ->field('id,num,bnCode,origin_price')
  390. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  391. ->order(['num' => 'desc', 'id' => 'asc'])
  392. ->cursor();
  393. $total = $value['can_sell_num'];
  394. foreach ($child_bns as $child_bn) {
  395. Db::name('child_bn')
  396. ->where(['id' => $child_bn['id']])
  397. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  398. ->update(['updatetime' => $date]);
  399. Db::name('good_stock_info')
  400. ->insert([
  401. 'stockid' => $stockid,
  402. 'bnCode' => $child_bn['bnCode'],
  403. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  404. 'used_num' => 0,
  405. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  406. 'origin_price' => $child_bn['origin_price'],
  407. 'addtime' => $date,
  408. 'updatetime' => $date,
  409. ]);
  410. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  411. if ($total == 0) break;
  412. }
  413. }
  414. }
  415. //维护商品层面的库存数
  416. Db::name('good')
  417. ->where(['is_del' => 0, 'spuCode' => $order_return['good_code']])
  418. ->inc('usable_stock', $info['return_num'] - $param['loss_num'])
  419. ->update(['updatetime' => $date]);
  420. }
  421. }
  422. //售后申请单状态维护
  423. Db::name('order_return')
  424. ->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'status' => 12])
  425. ->update(['status' => 5, 'updatetime' => $date]);
  426. Db::commit();
  427. return json_show(0, '库管收货成功');
  428. } catch (Exception $exception) {
  429. Db::rollback();
  430. return json_show(1004, $exception->getMessage());
  431. }
  432. }
  433. //更新退货工单标记
  434. public function setRecord()
  435. {
  436. $param = $this->request->only(['id', 'record'], 'post', 'trim');
  437. $val = Validate::rule([
  438. 'id|退货工单id' => 'require|number|gt:0',
  439. 'record|标记内容' => 'require',
  440. ]);
  441. if ($val->check($param) == false) return json_show(1004, $val->getError());
  442. $temp = Db::name('order_return_child')
  443. ->field('id')
  444. ->where(['is_del' => 0, 'id' => $param['id']])
  445. ->findOrEmpty();
  446. if (empty($temp)) return json_show(1004, '该退货工单不存在');
  447. $rs = Db::name('order_return_child')
  448. ->where(['is_del' => 0, 'id' => $param['id']])
  449. ->update(['record' => $param['record'], 'updatetime' => date('Y-m-d H:i:s')]);
  450. return $rs ? json_show(0, '更新退货工单标记内容成功') : json_show(1004, '更新退货工单标记内容失败');
  451. }
  452. //详情
  453. public function info()
  454. {
  455. $param = $this->request->only(['id'], 'post', 'trim');
  456. $val = Validate::rule([
  457. 'id|退货工单id' => 'require|number|gt:0',
  458. ]);
  459. if ($val->check($param) == false) return json_show(1004, $val->getError());
  460. $info = Db::name('order_return_child')
  461. ->alias('a')
  462. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  463. ->findOrEmpty();
  464. if (empty($info)) return json_show(1004, '该退货工单不存在');
  465. return empty($info) ? json_show(1004, '该退货工单不存在') : json_show(0, '获取退货工单详情成功', $info);
  466. }
  467. }