ReorderChild.php 33 KB

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