ReorderChild.php 16 KB

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