ReorderChild.php 36 KB

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