ReorderChild.php 35 KB

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