AfterChild.php 37 KB

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