ReorderChild.php 19 KB

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