ReorderChild.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. ->where($where)
  107. ->count('a.id');
  108. $list = Db::name('sale_return_child')
  109. ->alias('a')
  110. ->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')
  111. ->where($where)
  112. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  113. ->page($param['page'], $param['size'])
  114. ->select()
  115. ->toArray();
  116. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  117. }
  118. //库管收货(退货工单)
  119. public function receive()
  120. {
  121. $param = $this->request->only(['id', 'good_receive_type', 'loss_num', 'remark' => ''], 'post', 'trim');
  122. $val = Validate::rule([
  123. 'id|ID' => 'require|number|gt:0',
  124. 'good_receive_type|货物情况' => 'require|number|between:1,4',
  125. 'loss_num|丢失数量' => 'require|number|gt:0',
  126. 'remark|备注' => 'max:255'
  127. ]);
  128. if ($val->check($param)) return json_show(1004, $val->getError());
  129. $info = Db::name('sale_return_child')
  130. ->alias('a')
  131. ->field('a.id,a.num,a.status,a.returnCode,a.outChildCode,a.return_num,a.return_wsm_code,a.orderCode,a.outCode,b.good_code spuCode,c.status order_out_status,b.id saleid,d.num thnum')
  132. ->leftJoin('sale b', 'b.orderCode=a.orderCode')
  133. ->leftJoin('order_out_child c', 'c.outChildCode=a.outChildCode AND c.is_del=0')
  134. ->leftJoin('sale_return d', 'd.returnCode=a.returnCode')
  135. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  136. ->findOrEmpty();
  137. if (empty($info)) return json_show(1004, '该退货工单不存在');
  138. if ($info['status'] != 1) return json_show(1004, '该退货工单已收货');
  139. if ($param['loss_num'] > $info['num']) return json_show(1004, '丢失数量大于下单数量');
  140. Db::startTrans();
  141. try {
  142. $date = date('Y-m-d H:i:s');
  143. //维护退货工单
  144. Db::name('sale_return_child')
  145. ->where(['is_del' => 0, 'id' => $param['id'], 'status' => 1])
  146. ->update([
  147. 'status' => 2,
  148. 'updatetime' => $date,
  149. 'good_receive_type' => $param['good_receive_type'],
  150. 'loss_num' => $param['loss_num'],
  151. 'remark' => $param['remark'],
  152. ]);
  153. //检查所属销售单的退货工单是否全部完成退货
  154. $temp = Db::name('sale_return_child')
  155. ->field('id')
  156. ->where(['is_del' => 0, 'returnCode' => $info['returnCode'], 'status' => 1])
  157. ->findOrEmpty();
  158. if (empty($temp)) {
  159. Db::name('sale_return')
  160. ->where(['is_del' => 0, 'returnCode' => $info['returnCode'], 'status' => 12])
  161. ->update(['status' => 4, 'updatetime' => $date]);
  162. //维护销售单
  163. $order = Db::name('sale')->where(['is_del' => 0, 'id' => $info['saleid']])->findOrEmpty();
  164. //未发货数量要减去发货单上的发货数量
  165. // $out_send_num = Db::name('order_out')
  166. // ->where(['is_del' => 0, 'orderCode' => $info['orderCode'], 'status' => [0, 1]])
  167. // ->sum('send_num');
  168. // $order['wsend_num'] -= $out_send_num;
  169. $thnum = $info['thnum'];//退货总数量
  170. // if ($order['wsend_num'] < $thnum) throw new Exception("销售单未发货数量不足退货");
  171. //如果 发货 维护销售单的
  172. //else 未发货数量减,
  173. if ($info['order_out_status'] == 1) {
  174. $order['wsend_num'] -= $thnum;
  175. $order['send_num'] += $thnum;
  176. }
  177. // $lor = $order['status'];
  178. $order['status'] = $order['wsend_num'] == 0 ? 2 : ($order['send_num'] == 0 ? 0 : 1);
  179. $order['send_status'] = $order['wsend_num'] == 0 ? 3 : ($order['send_num'] == 0 ? 1 : 2);
  180. $order['th_num'] += $thnum;
  181. if ($order['th_num'] == $order['send_num'] && $order['wsend_num'] == 0) {
  182. $order['status'] = 3;
  183. }
  184. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  185. $order['updatetime'] = $date;
  186. $uap = Db::name("sale")->save($order);
  187. if ($uap == false) throw new Exception('销售单订单更新失败');
  188. // ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  189. // "order_code" => $order["orderCode"],//出库单号
  190. // "status" => $lor,//这里的status是之前的值
  191. // "action_remark" => '',//备注
  192. // "action_type" => "status"//新建create,编辑edit,更改状态status
  193. // ], "XSQRD", $order['status'], $order);
  194. //
  195. // ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  196. // "order_type" => 'XSQRD',
  197. // "order_code" => $order["orderCode"],//出库单号
  198. // "order_id" => $order["id"],
  199. // "order_status" => $order['status'], "before_status" => $lor
  200. // ]);
  201. }
  202. //发货工单数量减少
  203. Db::name('order_out_child')
  204. ->where(['is_del' => 0, 'outChildCode' => $info['outChildCode']])
  205. ->dec('num', $info['return_num'])
  206. ->update(['updatetime' => $date]);
  207. //维护bn
  208. if ($info['order_out_status'] == 1) {
  209. //待发货,说明此时有库存,有bn号
  210. Db::name('child_bn')
  211. ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode'], 'childCode' => $info['outChildCode']])
  212. ->inc('num', $info['return_num'])
  213. ->update(['updatetime' => $date]);
  214. Db::name('good_stock')
  215. ->where(['is_del' => 0, 'spuCode' => $info['spuCode'], 'wsm_code' => $info['return_wsm_code']])
  216. ->inc('usable_stock', $info['return_num'])
  217. ->dec('wait_out_stock', $info['return_num'])
  218. ->update(['updatetime' => $date]);
  219. }
  220. Db::commit();
  221. return json_show(0, '设置退货工单成功');
  222. } catch (Exception $exception) {
  223. Db::rollback();
  224. return json_show(1004, $exception->getMessage());
  225. }
  226. }
  227. //详情
  228. public function info()
  229. {
  230. $param = $this->request->only(['id'], 'post', 'trim');
  231. $val = Validate::rule(['id' => 'require|number|gt:0']);
  232. if ($val->check($param) == false) return json_show(1004, $val->getError());
  233. $info = Db::name('sale_return_child')
  234. ->where(['is_del' => 0, 'id' => $param['id']])
  235. ->findOrEmpty();
  236. return empty($info) ? json_show(1004, '该退货工单不存在') : json_show(0, '获取退货工单详情成功', $info);
  237. }
  238. //更新退货工单标记
  239. public function setRecord()
  240. {
  241. $param = $this->request->only(['id', 'record'], 'post', 'trim');
  242. $val = Validate::rule([
  243. 'id|退货工单id' => 'require|number|gt:0',
  244. 'record|标记内容' => 'require',
  245. ]);
  246. if ($val->check($param) == false) return json_show(1004, $val->getError());
  247. $temp = Db::name('sale_return_child')
  248. ->field('id')
  249. ->where(['is_del' => 0, 'id' => $param['id']])
  250. ->findOrEmpty();
  251. if (empty($temp)) return json_show(1004, '该退货工单不存在');
  252. $rs = Db::name('sale_return_child')
  253. ->where(['is_del' => 0, 'id' => $param['id']])
  254. ->update(['record' => $param['record'], 'updatetime' => date('Y-m-d H:i:s')]);
  255. return $rs ? json_show(0, '更新退货工单标记内容成功') : json_show(1004, '更新退货工单标记内容失败');
  256. }
  257. }