ReorderChild.php 30 KB

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