ReorderChild.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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', 'type'], 'post', 'trim');
  16. $val = Validate::rule([
  17. 'returnCode|退货单编号' => 'require',
  18. 'outCode|发货单编号' => 'require',
  19. 'list|退货工单集合' => 'require|array|max:100',
  20. 'type|工单退货类型' => 'require|number|in:1,2',
  21. ]);
  22. if ($val->check($param) == false) return json_show(1004, $val->getError());
  23. $saleReturn = Db::name('sale_return')
  24. ->field('id,orderCode,status,companyNo,companyName,customer_code,customer_name,num')
  25. ->where(['is_del' => 0, 'returnCode' => $param['returnCode']])
  26. ->findOrEmpty();
  27. if (empty($saleReturn)) return json_show(1004, '该退货单不存在');
  28. if ($saleReturn['status'] != 11) return json_show(1004, '退货单状态有误');
  29. $sale = Db::name('sale')
  30. ->field('id,sale_price,good_num,total_price')
  31. ->where(['is_del' => 0, 'orderCode' => $saleReturn['orderCode']])
  32. ->findOrEmpty();
  33. if (empty($sale)) return json_show(1004, '销售单不存在');
  34. if ($param['type'] == 2) {
  35. //所有发货工单
  36. $orderOutChild = Db::name('order_out_child')
  37. ->where(['is_del' => 0, 'outChildCode' => array_unique(array_column($param['list'], 'outChildCode'))])
  38. ->column('outCode,wsm_code,num,status', 'outChildCode');
  39. $saleReturnAddr = Db::name('sale_returnaddr')
  40. ->field('id,return_num,is_sale_return_child')
  41. ->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'outCode' => $param['outCode']])
  42. ->findOrEmpty();
  43. if (empty($saleReturnAddr)) return json_show(1004, '该退货地址不存在');
  44. if ($saleReturnAddr['is_sale_return_child'] == 1) return json_show(1004, '该退货地址不能重复设置退货工单');
  45. if ($saleReturnAddr['return_num'] != array_sum(array_column($param['list'], 'return_num'))) return json_show(1004, '数量错误');
  46. }
  47. $val_child = Validate::rule([
  48. 'outChildCode|发货工单号' => 'requireIf:type,2',
  49. 'return_num|退货数量' => 'require|number|gt:0|max:999999999999',
  50. 'return_wsm_code|退货仓库编码' => 'require'
  51. ]);
  52. if ($saleReturn['sale_return'] < array_sum(array_column($param['list'], 'return_num'))) return json_show(1004, '数量错误');
  53. Db::startTrans();
  54. try {
  55. $date = date('Y-m-d H:i:s');
  56. //维护退货地址
  57. if ($param['type'] == 2) {
  58. Db::name('sale_returnaddr')
  59. ->where(['id' => $saleReturnAddr['id'], 'is_del' => 0, 'is_sale_return_child' => 0])
  60. ->update(['is_sale_return_child' => 1, 'updatetime' => $date]);
  61. }
  62. //生成售前退货工单
  63. $insert = [];
  64. $i = 0;
  65. foreach ($param['list'] as $value) {
  66. $value['type'] = $param['type'];
  67. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  68. $insert[] = [
  69. 'orderCode' => $saleReturn['orderCode'],
  70. 'returnCode' => $param['returnCode'],
  71. 'outCode' => $param['type'] == 2 ? $param['outCode'] : '',
  72. 'outChildCode' => $value['outChildCode'] ?? '',
  73. 'order_out_child_status' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['status'] : '',
  74. 'saleReturnChildCode' => substr(makeNo('KCC'), 0, -2) . str_pad($i++, 2, '0', STR_PAD_LEFT),
  75. 'type' => $param['type'],
  76. 'companyNo' => $saleReturn['companyNo'],
  77. 'companyName' => $saleReturn['companyName'],
  78. 'customer_code' => $saleReturn['customer_code'],
  79. 'customerName' => $saleReturn['customer_name'],
  80. 'num' => $sale['good_num'],
  81. 'sale_price' => $sale['sale_price'],
  82. 'total_price' => $sale['total_price'],
  83. 'addtime' => $date,
  84. 'updatetime' => $date,
  85. 'record' => '',
  86. 'send_wsm_code' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['wsm_code'] : '',
  87. 'send_num' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['num'] : '',
  88. 'return_num' => $value['return_num'],
  89. 'return_wsm_code' => $value['return_wsm_code'],
  90. 'good_receive_type' => 0,
  91. 'loss_num' => 0,
  92. 'remark' => '',
  93. 'status' => 1,
  94. 'is_del' => 0,
  95. 'apply_id' => $this->uid,
  96. 'apply_name' => $this->uname,
  97. ];
  98. }
  99. if ($insert) Db::name('sale_return_child')->insertAll($insert);
  100. //维护售前退货单
  101. $total_return_num = Db::name('sale_return_child')->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'status' => 1])->sum('return_num');
  102. if ($total_return_num >= $saleReturn['num']) {
  103. Db::name('sale_return')
  104. ->where(['is_del' => 0, 'id' => $saleReturn['id'], 'status' => 11])
  105. ->update(['status' => 12, 'updatetime' => $date]);
  106. }
  107. Db::commit();
  108. return json_show(0, '设置退货工单成功');
  109. } catch (Exception $exception) {
  110. Db::rollback();
  111. return json_show(1004, $exception->getMessage());
  112. }
  113. }
  114. //退货工单列表
  115. public function getList()
  116. {
  117. $db = Db::name('sale_return_child')
  118. ->alias('a')
  119. ->leftJoin('sale_return b', 'b.returnCode=a.returnCode and b.is_del=0')
  120. ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code and c.is_del=0')
  121. ->where('a.is_del', 0);
  122. $param = $this->request->only(['page' => 1, 'size' => 10, 'returnCode' => '', 'status' => '', 'orderCode' => '', 'outCode' => '', 'outChildCode' => '', 'saleReturnChildCode' => '', 'order_type' => '', 'supplierNo' => ''], 'post', 'trim');
  123. if ($param['returnCode'] != '') $db->whereLike('a.returnCode', '%' . $param['returnCode'] . '%');
  124. if ($param['status'] !== '') $db->where('a.status', $param['status']);
  125. if ($param['orderCode'] != '') $db->whereLike('a.orderCode', '%' . $param['orderCode'] . '%');
  126. if ($param['outCode'] != '') $db->whereLike('a.outCode', '%' . $param['outCode'] . '%');
  127. if ($param['outChildCode'] != '') $db->whereLike('a.outChildCode', '%' . $param['outChildCode'] . '%');
  128. if ($param['saleReturnChildCode'] != '') $db->whereLike('a.saleReturnChildCode', '%' . $param['saleReturnChildCode'] . '%');
  129. if ($param['order_type'] !== '') $db->where('b.order_type', $param['order_type']);
  130. if ($param['supplierNo'] != '') $db->whereLike('b.supplierNo', '%' . $param['supplierNo'] . '%');
  131. //供应商账号,只查询该供应商下所有数据
  132. if ($this->level == 3 && $param['supplierNo'] == '') return json_show(1004, '供应商账号时供应商编码不能为空');
  133. //1.超管,查看全部;
  134. //2.业务公司账号-申请人,初始状态只查看自己创建的;
  135. //3.业务公司账号-退回仓库负责人,只查看自己负责仓库的数据;
  136. //4.业务公司账号-数据共享接受人,共享给自己的数据;
  137. if ($this->level == 2) {
  138. //是否仓库负责人
  139. $is_contactor = Db::name('warehouse_info')
  140. ->field('id')
  141. ->where(['is_del' => 0, 'contactor' => $this->uid])
  142. ->findOrEmpty();
  143. if (empty($is_contactor)) {
  144. $role = $this->checkDataShare();
  145. $hand = resign_hand_user($this->uid, 0);
  146. $db->whereIn('b.apply_id', array_unique(array_merge($role[DataGroupModel::$type_全部], $hand)));
  147. } else {
  148. $db->where('`a`.`return_wsm_code` in ' . Db::name('warehouse_info')->field('wsm_code')->where(['is_del' => 0, 'contactor' => $this->uid])->buildSql());
  149. }
  150. }
  151. $count = $db->count('a.id');
  152. $list = $db
  153. ->field('a.id,a.saleReturnChildCode,a.type,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')
  154. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  155. ->page($param['page'], $param['size'])
  156. ->select()
  157. ->toArray();
  158. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  159. }
  160. //库管收货(退货工单)
  161. public function receive()
  162. {
  163. $param = $this->request->only(['id', 'good_receive_type', 'loss_num', 'remark' => ''], 'post', 'trim');
  164. $val = Validate::rule([
  165. 'id|ID' => 'require|number|gt:0',
  166. 'good_receive_type|货物情况' => 'require|number|between:1,4',
  167. 'loss_num|丢失数量' => 'require|number|gt:0',
  168. 'remark|备注' => 'max:255'
  169. ]);
  170. if ($val->check($param)) return json_show(1004, $val->getError());
  171. $info = Db::name('sale_return_child')
  172. ->alias('a')
  173. ->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')
  174. ->leftJoin('sale b', 'b.orderCode=a.orderCode')
  175. ->leftJoin('order_out_child c', 'c.outChildCode=a.outChildCode AND c.is_del=0')
  176. ->leftJoin('sale_return d', 'd.returnCode=a.returnCode')
  177. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  178. ->findOrEmpty();
  179. if (empty($info)) return json_show(1004, '该退货工单不存在');
  180. if ($info['status'] != 1) return json_show(1004, '该退货工单已收货');
  181. if ($param['loss_num'] > $info['num']) return json_show(1004, '丢失数量大于下单数量');
  182. Db::startTrans();
  183. try {
  184. $date = date('Y-m-d H:i:s');
  185. //维护退货工单
  186. Db::name('sale_return_child')
  187. ->where(['is_del' => 0, 'id' => $param['id'], 'status' => 1])
  188. ->update([
  189. 'status' => 2,
  190. 'updatetime' => $date,
  191. 'good_receive_type' => $param['good_receive_type'],
  192. 'loss_num' => $param['loss_num'],
  193. 'remark' => $param['remark'],
  194. ]);
  195. //修改状态,添加待办
  196. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  197. "order_code" => $info['saleReturnChildCode'],//单号
  198. "status" => $info['status'],//这里的status是之前的值
  199. "action_remark" => '',//备注
  200. "action_type" => "status"//新建create,编辑edit,更改状态status
  201. ], "THGD", 2, [
  202. 'status' => 2,
  203. 'updatetime' => $date,
  204. 'good_receive_type' => $param['good_receive_type'],
  205. 'loss_num' => $param['loss_num'],
  206. 'remark' => $param['remark'],
  207. ]);
  208. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  209. "order_type" => 'THGD',
  210. "order_code" => $info['saleReturnChildCode'],//单号
  211. "order_id" => $info['id'],
  212. "order_status" => 2,
  213. "before_status" => $info['status'],
  214. 'holder_id' => $info['apply_id']
  215. ]);
  216. //检查所属销售单的退货工单是否全部完成退货
  217. $temp = Db::name('sale_return_child')
  218. ->field('id')
  219. ->where(['is_del' => 0, 'returnCode' => $info['returnCode'], 'status' => 1])
  220. ->findOrEmpty();
  221. if (empty($temp)) {
  222. Db::name('sale_return')
  223. ->where(['is_del' => 0, 'returnCode' => $info['returnCode'], 'status' => 12])
  224. ->update(['status' => 4, 'updatetime' => $date]);
  225. //修改状态,添加待办
  226. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  227. "order_code" => $info['returnCode'],//单号
  228. "status" => 12,//这里的status是之前的值
  229. "action_remark" => '',//备注
  230. "action_type" => "status"//新建create,编辑edit,更改状态status
  231. ], "XSTHD", 4, ['status' => 4, 'updatetime' => $date]);
  232. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  233. "order_type" => 'XSTHD',
  234. "order_code" => $info['returnCode'],//单号
  235. "order_id" => $info['sr_id'],
  236. "order_status" => 4,
  237. "before_status" => 12,
  238. 'holder_id' => $info['sr_apply_id']
  239. ]);
  240. //维护销售单
  241. $order = Db::name('sale')->where(['is_del' => 0, 'id' => $info['saleid']])->findOrEmpty();
  242. //未发货数量要减去发货单上的发货数量
  243. // $out_send_num = Db::name('order_out')
  244. // ->where(['is_del' => 0, 'orderCode' => $info['orderCode'], 'status' => [0, 1]])
  245. // ->sum('send_num');
  246. // $order['wsend_num'] -= $out_send_num;
  247. $old_status = $order['status'];
  248. $thnum = $info['thnum'];//退货总数量
  249. // if ($order['wsend_num'] < $thnum) throw new Exception("销售单未发货数量不足退货");
  250. //如果 发货 维护销售单的
  251. //else 未发货数量减,
  252. if ($info['order_out_status'] == 1) {
  253. $order['wsend_num'] -= $thnum;
  254. $order['send_num'] += $thnum;
  255. }
  256. // $lor = $order['status'];
  257. $order['status'] = $order['wsend_num'] == 0 ? 2 : ($order['send_num'] == 0 ? 0 : 1);
  258. $order['send_status'] = $order['wsend_num'] == 0 ? 3 : ($order['send_num'] == 0 ? 1 : 2);
  259. $order['th_num'] += $thnum;
  260. if ($order['th_num'] == $order['send_num'] && $order['wsend_num'] == 0) {
  261. $order['status'] = 3;
  262. }
  263. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  264. $order['updatetime'] = $date;
  265. $uap = Db::name("sale")->save($order);
  266. if ($uap == false) throw new Exception('销售单订单更新失败');
  267. if ($old_status != $order['status']) {
  268. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  269. "order_code" => $order["orderCode"],//出库单号
  270. "status" => $old_status,//这里的status是之前的值
  271. "action_remark" => '',//备注
  272. "action_type" => "status"//新建create,编辑edit,更改状态status
  273. ], "XSQRD", $order['status'], $order);
  274. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  275. "order_type" => 'XSQRD',
  276. "order_code" => $order["orderCode"],//出库单号
  277. "order_id" => $order["id"],
  278. "order_status" => $order['status'],
  279. "before_status" => $old_status
  280. ]);
  281. }
  282. }
  283. //发货工单数量减少
  284. Db::name('order_out_child')
  285. ->where(['is_del' => 0, 'outChildCode' => $info['outChildCode']])
  286. ->dec('num', $info['return_num'])
  287. ->update(['updatetime' => $date]);
  288. //维护bn
  289. if ($info['order_out_status'] == 1) {
  290. //待发货,说明此时有库存,有bn号
  291. Db::name('child_bn')
  292. ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode'], 'childCode' => $info['outChildCode']])
  293. ->inc('num', $info['return_num'])
  294. ->update(['updatetime' => $date]);
  295. Db::name('good_stock')
  296. ->where(['is_del' => 0, 'spuCode' => $info['spuCode'], 'wsm_code' => $info['return_wsm_code']])
  297. ->inc('usable_stock', $info['return_num'])
  298. ->dec('wait_out_stock', $info['return_num'])
  299. ->update(['updatetime' => $date]);
  300. }
  301. Db::commit();
  302. return json_show(0, '设置退货工单成功');
  303. } catch (Exception $exception) {
  304. Db::rollback();
  305. return json_show(1004, $exception->getMessage());
  306. }
  307. }
  308. //详情
  309. public function info()
  310. {
  311. $param = $this->request->only(['id'], 'post', 'trim');
  312. $val = Validate::rule(['id' => 'require|number|gt:0']);
  313. if ($val->check($param) == false) return json_show(1004, $val->getError());
  314. $info = Db::name('sale_return_child')
  315. ->alias('a')
  316. ->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')
  317. ->leftJoin('sale_return b', 'b.returnCode=a.returnCode and b.is_del=0')
  318. ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code and c.is_del=0')
  319. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  320. ->findOrEmpty();
  321. return empty($info) ? json_show(1004, '该退货工单不存在') : json_show(0, '获取退货工单详情成功', $info);
  322. }
  323. //更新退货工单标记
  324. public function setRecord()
  325. {
  326. $param = $this->request->only(['id', 'record'], 'post', 'trim');
  327. $val = Validate::rule([
  328. 'id|退货工单id' => 'require|number|gt:0',
  329. 'record|标记内容' => 'require',
  330. ]);
  331. if ($val->check($param) == false) return json_show(1004, $val->getError());
  332. $temp = Db::name('sale_return_child')
  333. ->field('id')
  334. ->where(['is_del' => 0, 'id' => $param['id']])
  335. ->findOrEmpty();
  336. if (empty($temp)) return json_show(1004, '该退货工单不存在');
  337. $rs = Db::name('sale_return_child')
  338. ->where(['is_del' => 0, 'id' => $param['id']])
  339. ->update(['record' => $param['record'], 'updatetime' => date('Y-m-d H:i:s')]);
  340. return $rs ? json_show(0, '更新退货工单标记内容成功') : json_show(1004, '更新退货工单标记内容失败');
  341. }
  342. }