ReorderChild.php 38 KB

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