AfterChild.php 32 KB

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