AfterChild.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. ->data(['updatetime' => $date])
  251. ->where(['is_del' => 0, 'outChildCode' => $info[$value['id']]['outChildCode']])
  252. ->dec('num', $info[$value['id']]['return_num'])
  253. ->update();
  254. }
  255. $total = array_sum(array_column($param['list'], 'can_sell_num'));
  256. if ($total > 0) {
  257. if ($order_return['is_stock'] != 1) {
  258. //根据转化表判断该非库存品是否转化为库存品
  259. $spuCode = Db::name('good_change_stock')
  260. ->where(['old_spuCode' => $order_return['good_code'], 'old_order_type' => $order_return['order_type']])
  261. ->value('spuCode', '');
  262. if ($spuCode == '') {
  263. //将非库存品转化为库存品
  264. $spuCode = makeNo('SKU');
  265. if ($order_return['order_type'] == 2) {
  266. $good = Db::name('good_basic')
  267. ->where('spuCode', $order_return['good_code'])
  268. ->findOrEmpty();
  269. if (!empty($good)) {
  270. Db::name('good_basic')->insert(array_merge($good, [
  271. 'id' => null,
  272. 'is_stock' => 1,
  273. 'spuCode' => $spuCode,
  274. 'addtime' => $date,
  275. 'updatetime' => $date,
  276. 'status' => 7,
  277. 'createrid'=>0,
  278. 'creater'=>'system'
  279. ]));
  280. }
  281. } else {
  282. $good = Db::name('good_zixun')
  283. ->where(['spuCode' => $order_return['good_code']])
  284. ->findOrEmpty();
  285. if (!empty($good)) {
  286. Db::name('good_basic')->insert([
  287. 'spuCode' => $spuCode,
  288. 'good_code' => $good['good_code'],
  289. 'good_name' => $good['good_name'],
  290. 'cat_id' => $good['cat_id'],
  291. 'brand_id' => $good['brand_id'],
  292. 'companyNo' => $good['companyNo'],
  293. 'companyName' => $good['companyName'],
  294. 'good_unit' => $good['good_unit'],
  295. 'good_type' => $good['good_type'],
  296. 'moq' => $good['moq'],
  297. 'is_exclusive' => 0,
  298. 'customized' => $good['customized'],
  299. 'tax' => $good['tax'],
  300. 'supplierNo' => $good['supplierNo'],
  301. 'supplierName' => $good['supplierName'],
  302. 'is_auth' => $good['is_auth'],
  303. 'auth_img' => $good['auth_img'],
  304. 'is_stock' => 1,
  305. 'after_sales' => '',
  306. 'craft_desc' => $good['craft_desc'],
  307. 'good_remark' => $good['good_remark'],
  308. 'good_size' => '',
  309. 'weight' => $good['weight'],
  310. 'packing_way' => '',
  311. 'packing_size' => '',
  312. 'packing_spec' => '',
  313. 'packing_list' => '',
  314. 'packing_weight' => 0,
  315. 'good_bar' => '',
  316. 'supply_area' => $good['supply_area'],
  317. 'delivery_place' => '',
  318. 'origin_place' => '',
  319. 'delivery_day' => '',
  320. 'lead_time' => '',
  321. 'sample_day' => '',
  322. 'sample_fee' => '',
  323. 'good_img' => $good['good_img'],
  324. 'good_thumb_img' => $good['good_thumb_img'],
  325. 'good_info_img' => $good['good_info_img'],
  326. 'cert_fee' => 0,
  327. 'packing_fee' => 0,
  328. 'cost_fee' => 0,
  329. 'mark_fee' => 0,
  330. 'demo_fee' => 0,
  331. 'open_fee' => 0,
  332. 'noble_metal' => $good['noble_metal'],
  333. 'noble_weight' => 0,
  334. 'is_gold_price' => $good['is_gold_price'],
  335. 'cgd_gold_price' => 0,
  336. 'market_price' => 0,
  337. 'nake_price' => 0,
  338. 'is_step' => 0,
  339. 'is_online' => 0,
  340. 'status' => 7,
  341. 'createrid' => 0,
  342. 'creater' => 'system',
  343. 'field_change' => '',
  344. 'is_del' => 0,
  345. 'addtime' => $date,
  346. 'updatetime' => $date,
  347. 'is_diff' => 0,
  348. 'config' => '',
  349. 'other_config' => '',
  350. 'stock_moq' => 0,
  351. 'is_support_barter' => 1,
  352. 'chargerid' => $good['createrid'],
  353. 'charger' => $good['creater'],
  354. 'is_support_stock' => 1
  355. ]);
  356. }
  357. }
  358. if (!empty($good)) {
  359. //关联表增数据
  360. Db::name('good_change_stock')
  361. ->insert([
  362. 'old_spuCode' => $order_return['good_code'],
  363. 'old_order_type' => $order_return['order_type'],
  364. 'spuCode' => $spuCode,
  365. 'addtime' => $date,
  366. 'updatetime' => $date,
  367. ]);
  368. } else throw new Exception('未找到对应的商品数据');
  369. }
  370. //新增bn记录,维护旧有bn记录
  371. $child_bns = Db::name('child_bn')
  372. ->field('id,num,origin_price')
  373. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode']])
  374. ->order(['num' => 'desc', 'id' => 'asc'])
  375. ->cursor();
  376. $origin_price = $j = 0;
  377. foreach ($child_bns as $child_bn) {
  378. if ($origin_price === 0) $origin_price = $child_bn['origin_price'];
  379. Db::name('child_bn')
  380. ->data(['updatetime' => $date])
  381. ->where(['id' => $child_bn['id']])
  382. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  383. ->update();
  384. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  385. if ($total == 0) break;
  386. }
  387. foreach ($param['list'] as $value) {
  388. if ($value['can_sell_num'] > 0) {
  389. $stockid = Db::name('good_stock')
  390. ->insertGetId([
  391. 'project_code' => '',
  392. 'spuCode' => $spuCode,
  393. 'wsm_code' => $info[$value['id']]['return_wsm_code'],
  394. 'wait_in_stock' => 0,
  395. 'wait_out_stock' => 0,
  396. 'usable_stock' => $value['can_sell_num'],
  397. 'intra_stock' => 0,
  398. 'total_stock' => $value['can_sell_num'],
  399. 'status' => 1,
  400. 'is_del' => 0,
  401. 'warn_stock' => 0,
  402. 'addtime' => $date,
  403. 'updatetime' => $date,
  404. ]);
  405. Db::name('good_stock_info')
  406. ->insert([
  407. 'stockid' => $stockid,
  408. 'bnCode' => substr(makeNo("BN"), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
  409. 'total_num' => $value['can_sell_num'],
  410. 'used_num' => 0,
  411. 'balance_num' => $value['can_sell_num'],
  412. 'origin_price' => $origin_price,
  413. 'addtime' => $date,
  414. 'updatetime' => $date,
  415. ]);
  416. }
  417. }
  418. } else {
  419. //库存品,直接维护stock和info
  420. $stockids = Db::name('good_stock')
  421. ->where(['is_del' => 0, 'spuCode' => $order_return['good_code'], 'wsm_code' => array_column($info, 'return_wsm_code')])
  422. ->column('id', 'wsm_code');
  423. foreach ($param['list'] as $value) {
  424. //定义了stockid,维护bn
  425. if (isset($stockids[$info[$value['id']]['return_wsm_code']])) {
  426. $good_stock_info = Db::name('good_stock_info')
  427. ->where(['stockid' => $stockids[$info[$value['id']]['return_wsm_code']]])
  428. ->findOrEmpty();
  429. if (empty($good_stock_info)) {
  430. //从child_bn查询bn号并维护,新建到good_stock_info中
  431. $child_bns = Db::name('child_bn')
  432. ->field('id,num,bnCode,origin_price')
  433. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  434. ->order(['num' => 'desc', 'id' => 'asc'])
  435. ->cursor();
  436. $total = $value['can_sell_num'];
  437. foreach ($child_bns as $child_bn) {
  438. Db::name('child_bn')
  439. ->data(['updatetime' => $date])
  440. ->where(['id' => $child_bn['id']])
  441. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  442. ->update();
  443. Db::name('good_stock_info')
  444. ->insert([
  445. 'stockid' => $stockids[$info[$value['id']]['return_wsm_code']],
  446. 'bnCode' => $child_bn['bnCode'],
  447. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  448. 'used_num' => 0,
  449. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  450. 'origin_price' => $child_bn['origin_price'],
  451. 'addtime' => $date,
  452. 'updatetime' => $date,
  453. ]);
  454. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  455. if ($total == 0) break;
  456. }
  457. } else {
  458. //维护记录
  459. Db::name('good_stock_info')
  460. ->data(['updatetime' => $date])
  461. ->where(['id' => $good_stock_info['id']])
  462. ->inc('balance_num', $value['can_sell_num'])//可用数量增加
  463. ->dec('used_num', $value['can_sell_num'])//已用数量减少
  464. ->update();
  465. }
  466. //维护good_stock
  467. Db::name('good_stock')
  468. ->data(['updatetime' => $date])
  469. ->where(['is_del' => 0, 'id' => $stockids[$info[$value['id']]['return_wsm_code']]])
  470. ->inc('usable_stock', $value['can_sell_num'])
  471. ->inc('total_stock', $value['can_sell_num'])
  472. ->update();
  473. } else {
  474. //新建good_stock和good_stock_info
  475. $stockid = Db::name('good_stock')
  476. ->insertGetId([
  477. 'project_code' => '',
  478. 'spuCode' => $order_return['good_code'],
  479. 'wsm_code' => $value['return_wsm_code'],
  480. 'wait_in_stock' => 0,
  481. 'wait_out_stock' => 0,
  482. 'usable_stock' => $value['can_sell_num'],
  483. 'intra_stock' => 0,
  484. 'total_stock' => $value['can_sell_num'],
  485. 'status' => 1,
  486. 'is_del' => 0,
  487. 'warn_stock' => 0,
  488. 'addtime' => $date,
  489. 'updatetime' => $date,
  490. ]);
  491. //从child_bn查询bn号并维护,新建到good_stock_info中
  492. $child_bns = Db::name('child_bn')
  493. ->field('id,num,bnCode,origin_price')
  494. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  495. ->order(['num' => 'desc', 'id' => 'asc'])
  496. ->cursor();
  497. $total = $value['can_sell_num'];
  498. foreach ($child_bns as $child_bn) {
  499. Db::name('child_bn')
  500. ->data(['updatetime' => $date])
  501. ->where(['id' => $child_bn['id']])
  502. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  503. ->update();
  504. Db::name('good_stock_info')
  505. ->insert([
  506. 'stockid' => $stockid,
  507. 'bnCode' => $child_bn['bnCode'],
  508. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  509. 'used_num' => 0,
  510. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  511. 'origin_price' => $child_bn['origin_price'],
  512. 'addtime' => $date,
  513. 'updatetime' => $date,
  514. ]);
  515. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  516. if ($total == 0) break;
  517. }
  518. }
  519. }
  520. //维护商品层面的库存数
  521. Db::name('good')
  522. ->data(['updatetime' => $date])
  523. ->where(['is_del' => 0, 'spuCode' => $order_return['good_code']])
  524. ->inc('usable_stock', array_sum(array_column($param['list'], 'can_sell_num')))
  525. ->update();
  526. }
  527. }
  528. //售后申请单状态维护
  529. Db::name('order_return')
  530. ->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'status' => 12])
  531. ->update(['status' => 5, 'updatetime' => $date]);
  532. //售后退货工单设置信息维护
  533. Db::name('order_return_prepare')
  534. ->where(['is_del' => 0, 'status' => 1, 'id' => array_column($info, 'id')])
  535. ->update(['updatetime' => $date, 'status' => 3]);
  536. Db::commit();
  537. return json_show(0, '库管收货成功');
  538. } catch (Exception $exception) {
  539. Db::rollback();
  540. return json_show(1004, $exception->getMessage());
  541. }
  542. }
  543. //更新退货工单标记
  544. public function setRecord()
  545. {
  546. $param = $this->request->only(['id', 'record'], 'post', 'trim');
  547. $val = Validate::rule([
  548. 'id|退货工单id' => 'require|number|gt:0',
  549. 'record|标记内容' => 'require',
  550. ]);
  551. if ($val->check($param) == false) return json_show(1004, $val->getError());
  552. $temp = Db::name('order_return_child')
  553. ->field('id')
  554. ->where(['is_del' => 0, 'id' => $param['id']])
  555. ->findOrEmpty();
  556. if (empty($temp)) return json_show(1004, '该退货工单不存在');
  557. $rs = Db::name('order_return_child')
  558. ->where(['is_del' => 0, 'id' => $param['id']])
  559. ->update(['record' => $param['record'], 'updatetime' => date('Y-m-d H:i:s')]);
  560. return $rs ? json_show(0, '更新退货工单标记内容成功') : json_show(1004, '更新退货工单标记内容失败');
  561. }
  562. //详情
  563. public function info()
  564. {
  565. $param = $this->request->only(['id'], 'post', 'trim');
  566. $val = Validate::rule([
  567. 'id|退货工单id' => 'require|number|gt:0',
  568. ]);
  569. if ($val->check($param) == false) return json_show(1004, $val->getError());
  570. $info = Db::name('order_return_child')
  571. ->alias('a')
  572. ->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')
  573. ->leftJoin('warehouse_info b', 'b.wsm_code=a.send_wsm_code')
  574. ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code')
  575. ->leftJoin('order_return d', 'd.returnCode=a.returnCode and d.is_del=0')
  576. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  577. ->findOrEmpty();
  578. if (empty($info)) return json_show(1004, '该退货工单不存在');
  579. return empty($info) ? json_show(1004, '该退货工单不存在') : json_show(0, '获取退货工单详情成功', $info);
  580. }
  581. //获取 售后退货工单设置信息
  582. public function getPreList()
  583. {
  584. $param = $this->request->only(['page' => 1, 'size' => 10, 'returnCode' => '', 'status' => ''], 'post', 'trim');
  585. $db = Db::name('order_return_prepare')
  586. ->alias('a')
  587. ->leftJoin('order_return b', 'b.returnCode=a.returnCode and b.is_del=0')
  588. ->leftJoin('order_out_child c', 'c.outChildCode=a.outChildCode and c.is_del=0')
  589. ->leftJoin('warehouse_info d', 'd.wsm_code=c.wsm_code')
  590. ->leftJoin('warehouse_info e', 'e.wsm_code=a.return_wsm_code')
  591. ->where('a.is_del', 0);
  592. if ($param['returnCode'] != '') $db->whereLike('a.returnCode', '%' . $param['returnCode'] . '%');
  593. if ($param['status'] !== '') $db->where('a.status', $param['status']);
  594. $count = $db
  595. ->count('a.id');
  596. $list = $db
  597. ->field(['a.id',
  598. 'a.returnCode',
  599. 'b.orderCode',
  600. 'c.outCode',
  601. 'a.order_out_child_status',
  602. 'a.outChildCode',
  603. 'd.supplierName send_supplierName',
  604. 'd.supplierNo send_supplierNo',
  605. 'd.name send_wsm_name',
  606. 'c.wsm_code send_wsm_code',
  607. 'd.contactor_name send_contactor_name',
  608. 'c.num send_num',
  609. 'a.return_num',
  610. 'e.supplierName return_supplierName',
  611. 'e.supplierNo return_supplierNo',
  612. 'e.name return_wsm_name',
  613. 'a.return_wsm_code',
  614. 'e.contactor_name return_contactor_name',
  615. 'a.status'])
  616. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  617. ->page($param['page'], $param['size'])
  618. ->select()
  619. ->toArray();
  620. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  621. }
  622. }