ReorderChild.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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')
  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. $all_loss_num = Db::name('sale_return_child')
  287. ->where(['is_del' => 0, 'returnCode' => $info['returnCode']])
  288. ->sum('loss_num');
  289. $thnum = $info['thnum'] - $all_loss_num;//退货总数量-丢失总数量
  290. // if ($order['wsend_num'] < $thnum) throw new Exception("销售单未发货数量不足退货");
  291. //如果 发货 维护销售单的
  292. //else 未发货数量减,
  293. if ($info['order_out_status'] == 1) {
  294. $order['wsend_num'] -= $thnum;
  295. $order['send_num'] += $thnum;
  296. }
  297. // $lor = $order['status'];
  298. $order['status'] = $order['wsend_num'] == 0 ? 2 : ($order['send_num'] == 0 ? 0 : 1);
  299. $order['send_status'] = $order['wsend_num'] == 0 ? 3 : ($order['send_num'] == 0 ? 1 : 2);
  300. $order['th_num'] += $thnum;
  301. if ($order['th_num'] == $order['send_num'] && $order['wsend_num'] == 0) {
  302. $order['status'] = 3;
  303. }
  304. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  305. $order['updatetime'] = $date;
  306. $uap = Db::name("sale")->save($order);
  307. if ($uap == false) throw new Exception('销售单订单更新失败');
  308. if ($old_status != $order['status']) {
  309. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  310. "order_code" => $order["orderCode"],//出库单号
  311. "status" => $old_status,//这里的status是之前的值
  312. "action_remark" => '',//备注
  313. "action_type" => "status"//新建create,编辑edit,更改状态status
  314. ], "XSQRD", $order['status'], $order);
  315. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  316. "order_type" => 'XSQRD',
  317. "order_code" => $order["orderCode"],//出库单号
  318. "order_id" => $order["id"],
  319. "order_status" => $order['status'],
  320. "before_status" => $old_status
  321. ]);
  322. }
  323. //非库存品转存为库存品
  324. if ($info['is_stock'] != 1) {
  325. //根据转化表判断该非库存品是否转化为库存品
  326. $spuCode = Db::name('good_change_stock')
  327. ->where(['old_spuCode' => $info['spuCode'], 'old_order_type' => $info['order_type']])
  328. ->value('spuCode', '');
  329. if ($spuCode == '') {
  330. //将非库存品转化为库存品
  331. $spuCode = makeNo('SKU');
  332. if ($info['order_type'] == 2) {
  333. $good = Db::name('good_basic')
  334. ->where('spuCode', $info['spuCode'])
  335. ->findOrEmpty();
  336. if (!empty($good)) {
  337. Db::name('good_basic')->insert(array_merge($good, [
  338. 'id' => null,
  339. 'spuCode' => $spuCode,
  340. 'addtime' => $date,
  341. 'updatetime' => $date,
  342. 'status' => 0,
  343. 'is_stock' => 1,
  344. ]));
  345. }
  346. } else {
  347. $good = Db::name('good_zixun')
  348. ->where(['spuCode' => $info['spuCode']])
  349. ->findOrEmpty();
  350. if (!empty($good)) {
  351. Db::name('good_basic')->insert([
  352. 'spuCode' => $spuCode,
  353. 'good_code' => $good['good_code'],
  354. 'good_name' => $good['good_name'],
  355. 'cat_id' => $good['cat_id'],
  356. 'brand_id' => $good['brand_id'],
  357. 'companyNo' => $good['companyNo'],
  358. 'companyName' => $good['companyName'],
  359. 'good_unit' => $good['good_unit'],
  360. 'good_type' => $good['good_type'],
  361. 'moq' => $good['moq'],
  362. 'is_exclusive' => 0,
  363. 'customized' => $good['customized'],
  364. 'tax' => $good['tax'],
  365. 'supplierNo' => $good['supplierNo'],
  366. 'supplierName' => $good['supplierName'],
  367. 'is_auth' => $good['is_auth'],
  368. 'auth_img' => $good['auth_img'],
  369. 'is_stock' => 1,
  370. 'after_sales' => '',
  371. 'craft_desc' => $good['craft_desc'],
  372. 'good_remark' => $good['good_remark'],
  373. 'good_size' => '',
  374. 'weight' => $good['weight'],
  375. 'packing_way' => '',
  376. 'packing_size' => '',
  377. 'packing_spec' => '',
  378. 'packing_list' => '',
  379. 'packing_weight' => 0,
  380. 'good_bar' => '',
  381. 'supply_area' => $good['supply_area'],
  382. 'delivery_place' => '',
  383. 'origin_place' => '',
  384. 'delivery_day' => '',
  385. 'lead_time' => '',
  386. 'sample_day' => '',
  387. 'sample_fee' => '',
  388. 'good_img' => $good['good_img'],
  389. 'good_thumb_img' => $good['good_thumb_img'],
  390. 'good_info_img' => $good['good_info_img'],
  391. 'cert_fee' => 0,
  392. 'packing_fee' => 0,
  393. 'cost_fee' => 0,
  394. 'mark_fee' => 0,
  395. 'demo_fee' => 0,
  396. 'open_fee' => 0,
  397. 'noble_metal' => $good['noble_metal'],
  398. 'noble_weight' => 0,
  399. 'is_gold_price' => $good['is_gold_price'],
  400. 'cgd_gold_price' => 0,
  401. 'market_price' => 0,
  402. 'nake_price' => 0,
  403. 'is_step' => 0,
  404. 'is_online' => 0,
  405. 'status' => 0,
  406. 'createrid' => $good['createrid'],
  407. 'creater' => $good['creater'],
  408. 'field_change' => '',
  409. 'is_del' => 0,
  410. 'addtime' => $date,
  411. 'updatetime' => $date,
  412. 'is_diff' => 0,
  413. 'config' => '',
  414. 'other_config' => '',
  415. 'stock_moq' => 0,
  416. 'is_support_barter' => 1,
  417. 'chargerid' => $good['createrid'],
  418. 'charger' => $good['creater'],
  419. 'is_support_stock' => 1
  420. ]);
  421. }
  422. }
  423. if (!empty($good)) {
  424. //关联表增数据
  425. Db::name('good_change_stock')
  426. ->insert([
  427. 'old_spuCode' => $info['spuCode'],
  428. 'old_order_type' => $info['order_type'],
  429. 'spuCode' => $spuCode,
  430. 'addtime' => $date,
  431. 'updatetime' => $date,
  432. ]);
  433. } else throw new Exception('未找到对应的商品数据');
  434. }
  435. //新增bn记录,维护旧有bn记录
  436. $child_bns = Db::name('child_bn')
  437. ->field('id,num,origin_price')
  438. ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode']])
  439. ->order(['num' => 'desc', 'id' => 'asc'])
  440. ->cursor();
  441. $total = $info['return_num'];
  442. $origin_price = $j = 0;
  443. foreach ($child_bns as $child_bn) {
  444. if ($origin_price === 0) $origin_price = $child_bn['origin_price'];
  445. Db::name('child_bn')
  446. ->where(['id' => $child_bn['id']])
  447. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  448. ->update(['updatetime' => $date]);
  449. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  450. if ($total == 0) break;
  451. }
  452. if (($info['return_num'] - $info['loss_num']) > 0) {
  453. $stockid = Db::name('good_stock')
  454. ->insertGetId([
  455. 'project_code' => '',
  456. 'spuCode' => $spuCode,
  457. 'wsm_code' => $info['return_wsm_code'],
  458. 'wait_in_stock' => 0,
  459. 'wait_out_stock' => 0,
  460. 'usable_stock' => $info['return_num'] - $info['loss_num'],
  461. 'intra_stock' => 0,
  462. 'total_stock' => $info['return_num'] - $info['loss_num'],
  463. 'status' => 1,
  464. 'is_del' => 0,
  465. 'warn_stock' => 0,
  466. 'addtime' => $date,
  467. 'updatetime' => $date,
  468. ]);
  469. Db::name('good_stock_info')
  470. ->insert([
  471. 'stockid' => $stockid,
  472. 'bnCode' => substr(makeNo("BN"), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
  473. 'total_num' => $info['return_num'] - $info['loss_num'],
  474. 'used_num' => 0,
  475. 'balance_num' => $info['return_num'] - $info['loss_num'],
  476. 'origin_price' => $origin_price,
  477. 'addtime' => $date,
  478. 'updatetime' => $date,
  479. ]);
  480. }
  481. } else {
  482. //维护商品层面的库存数
  483. $has_good = Db::name('good')
  484. ->field('id')
  485. ->where(['is_del' => 0, 'spuCode' => $info['spuCode']])
  486. ->findOrEmpty();
  487. if (empty($has_good)) throw new Exception('未找到商品上线记录');
  488. else {
  489. Db::name('good')
  490. ->where('id', $has_good['id'])
  491. ->inc('usable_stock', $thnum)
  492. ->update(['updatetime' => $date]);
  493. }
  494. }
  495. }
  496. //发货工单数量减少
  497. $tmp = Db::name('order_out_child')
  498. ->where(['is_del' => 0, 'outChildCode' => $info['outChildCode']])
  499. ->field('id,num')
  500. ->findOrEmpty();
  501. $tmp['num'] -= $info['return_num'];
  502. $tmp['updatetime'] = $date;
  503. if ($tmp['num'] <= 0) $tmp['is_del'] = 1;
  504. Db::name('order_out_child')
  505. ->where(['is_del' => 0, 'id' => $tmp['id']])
  506. ->update($tmp);
  507. //维护发货单
  508. $temp = Db::name('order_out')
  509. ->field('id,send_num')
  510. ->where(['is_del' => 0, 'outCode' => $info['outCode']])
  511. ->findOrEmpty();
  512. $temp['send_num'] -= $info['return_num'];
  513. $temp['updatetime'] = $date;
  514. if ($temp['send_num'] <= 0) $temp['is_del'] = 1;
  515. Db::name('order_out')
  516. ->where('id', $temp['id'])
  517. ->update($temp);
  518. //维护bn
  519. if ($info['order_out_status'] == 1) {
  520. //待发货,说明此时有库存,有bn号
  521. Db::name('child_bn')
  522. ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode'], 'childCode' => $info['outChildCode']])
  523. ->inc('num', $info['return_num'])
  524. ->update(['updatetime' => $date]);
  525. Db::name('good_stock')
  526. ->where(['is_del' => 0, 'spuCode' => $info['spuCode'], 'wsm_code' => $info['return_wsm_code']])
  527. ->inc('usable_stock', $info['return_num'])
  528. ->dec('wait_out_stock', $info['return_num'])
  529. ->update(['updatetime' => $date]);
  530. }
  531. Db::commit();
  532. return json_show(0, '设置退货工单成功');
  533. } catch (Exception $exception) {
  534. Db::rollback();
  535. return json_show(1004, $exception->getMessage());
  536. }
  537. }
  538. //详情
  539. public function info()
  540. {
  541. $param = $this->request->only(['id'], 'post', 'trim');
  542. $val = Validate::rule(['id' => 'require|number|gt:0']);
  543. if ($val->check($param) == false) return json_show(1004, $val->getError());
  544. $info = Db::name('sale_return_child')
  545. ->alias('a')
  546. ->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')
  547. ->leftJoin('sale_return b', 'b.returnCode=a.returnCode and b.is_del=0')
  548. ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code and c.is_del=0')
  549. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  550. ->findOrEmpty();
  551. return empty($info) ? json_show(1004, '该退货工单不存在') : json_show(0, '获取退货工单详情成功', $info);
  552. }
  553. //更新退货工单标记
  554. public function setRecord()
  555. {
  556. $param = $this->request->only(['id', 'record'], 'post', 'trim');
  557. $val = Validate::rule([
  558. 'id|退货工单id' => 'require|number|gt:0',
  559. 'record|标记内容' => 'require',
  560. ]);
  561. if ($val->check($param) == false) return json_show(1004, $val->getError());
  562. $temp = Db::name('sale_return_child')
  563. ->field('id')
  564. ->where(['is_del' => 0, 'id' => $param['id']])
  565. ->findOrEmpty();
  566. if (empty($temp)) return json_show(1004, '该退货工单不存在');
  567. $rs = Db::name('sale_return_child')
  568. ->where(['is_del' => 0, 'id' => $param['id']])
  569. ->update(['record' => $param['record'], 'updatetime' => date('Y-m-d H:i:s')]);
  570. return $rs ? json_show(0, '更新退货工单标记内容成功') : json_show(1004, '更新退货工单标记内容失败');
  571. }
  572. }