ReorderChild.php 36 KB

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