AfterChild.php 27 KB

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