ReorderChild.php 35 KB

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