AfterChild.php 37 KB

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