ReorderChild.php 19 KB

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