ReorderChild.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace app\admin\controller;
  3. //销售单退货工单
  4. use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. class ReorderChild extends Base
  8. {
  9. //退货工单创建
  10. public function add()
  11. {
  12. $param = $this->request->only(['returnCode', 'list', 'type'], 'post', 'trim');
  13. $val = Validate::rule([
  14. 'returnCode|退货单编号' => 'require',
  15. 'list|退货工单集合' => 'require|array|max:100',
  16. 'type|工单退货类型' => 'require|number|in:1,2',
  17. ]);
  18. if ($val->check($param) == false) return json_show(1004, $val->getError());
  19. $saleReturn = Db::name('sale_return')
  20. ->field('id,orderCode,status,companyNo,companyName,customer_code,customer_name')
  21. ->where(['is_del' => 0, 'returnCode' => $param['returnCode']])
  22. ->findOrEmpty();
  23. if (empty($saleReturn)) return json_show(1004, '该退货单不存在');
  24. if ($saleReturn['status'] != 11) return json_show(1004, '退货单状态有误');
  25. $sale = Db::name('sale')
  26. ->field('id,sale_price,good_num,total_price')
  27. ->where(['is_del' => 0, 'orderCode' => $saleReturn['orderCode']])
  28. ->findOrEmpty();
  29. if (empty($sale)) return json_show(1004, '销售单不存在');
  30. if ($param['type'] == 2) {
  31. //所有发货工单
  32. $orderOutChild = Db::name('order_out_child')
  33. ->where(['is_del' => 0, 'outChildCode' => array_unique(array_column($param['list'], 'outChildCode'))])
  34. ->column('outCode,wsm_code,num,status', 'outChildCode');
  35. }
  36. $val_child = Validate::rule([
  37. 'outChildCode|发货工单号' => 'requireIf:type,2',
  38. 'return_num|退货数量' => 'require|number|gt:0|max:999999999999',
  39. 'return_wsm_code|退货仓库编码' => 'require',
  40. ]);
  41. Db::startTrans();
  42. try {
  43. $date = date('Y-m-d H:i:s');
  44. $insert = [];
  45. $i = 0;
  46. foreach ($param['list'] as $value) {
  47. $value['type'] = $param['type'];
  48. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  49. $insert[] = [
  50. 'orderCode' => $saleReturn['orderCode'],
  51. 'returnCode' => $param['returnCode'],
  52. 'outCode' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['outCode'] : '',
  53. 'outChildCode' => $value['outChildCode'],
  54. 'order_out_child_status' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['status'] : '',
  55. 'saleReturnChildCode' => substr(makeNo('KCC'), 0, -2) . str_pad($i++, 2, '0', STR_PAD_LEFT),
  56. 'type' => $param['type'],
  57. 'companyNo' => $saleReturn['companyNo'],
  58. 'companyName' => $saleReturn['companyName'],
  59. 'customer_code' => $saleReturn['customer_code'],
  60. 'customerName' => $saleReturn['customer_name'],
  61. 'num' => $sale['good_num'],
  62. 'sale_price' => $sale['sale_price'],
  63. 'total_price' => $sale['total_price'],
  64. 'addtime' => $date,
  65. 'updatetime' => $date,
  66. 'record' => '',
  67. 'send_wsm_code' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['wsm_code'] : '',
  68. 'send_num' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['num'] : '',
  69. 'return_num' => $value['return_num'],
  70. 'return_wsm_code' => $value['return_wsm_code'],
  71. 'good_receive_type' => 0,
  72. 'loss_num' => 0,
  73. 'remark' => '',
  74. 'status' => 1,
  75. 'is_del' => 0,
  76. 'apply_id' => $this->uid,
  77. 'apply_name' => $this->uname,
  78. ];
  79. }
  80. if ($insert) Db::name('sale_return_child')->insertAll($insert);
  81. Db::name('sale_return')
  82. ->where(['is_del' => 0, 'id' => $saleReturn['id'], 'status' => 11])
  83. ->update(['status' => 12, 'updatetime' => $date]);
  84. Db::commit();
  85. return json_show(0, '设置退货工单成功');
  86. } catch (Exception $exception) {
  87. Db::rollback();
  88. return json_show(1004, $exception->getMessage());
  89. }
  90. }
  91. //退货工单列表
  92. public function getList()
  93. {
  94. $param = $this->request->only(['page' => 1, 'size' => 10, 'returnCode' => '', 'status' => '', 'orderCode' => '', 'outCode' => '', 'outChildCode' => '', 'saleReturnChildCode' => ''], 'post', 'trim');
  95. $where = [['a.is_del', '=', 0]];
  96. if ($param['returnCode'] != '') $where[] = ['a.returnCode', 'like', '%' . $param['returnCode'] . '%'];
  97. if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
  98. if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
  99. if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
  100. if ($param['outChildCode'] != '') $where[] = ['a.outChildCode', 'like', '%' . $param['outChildCode'] . '%'];
  101. if ($param['saleReturnChildCode'] != '') $where[] = ['a.saleReturnChildCode', 'like', '%' . $param['saleReturnChildCode'] . '%'];
  102. $count = Db::name('sale_return_child')
  103. ->alias('a')
  104. ->where($where)
  105. ->count('a.id');
  106. $list = Db::name('sale_return_child')
  107. ->alias('a')
  108. ->field('a.id,a.saleReturnChildCode,a.type,a.outChildCode,a.outCode,a.companyNo,a.companyName,a.customer_code,a.customer_name,a.num,a.sale_price,a.total_price,a.status,a.addtime')
  109. ->where($where)
  110. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  111. ->page($param['page'], $param['size'])
  112. ->select()
  113. ->toArray();
  114. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  115. }
  116. //库管收货(退货工单)
  117. public function receive()
  118. {
  119. $param = $this->request->only(['id', 'good_receive_type', 'loss_num', 'remark' => '', 'record' => ''], 'post', 'trim');
  120. $val = Validate::rule([
  121. 'id|ID' => 'require|number|gt:0',
  122. 'good_receive_type|货物情况' => 'require|number|between:1,4',
  123. 'loss_num|丢失数量' => 'require|number|gt:0',
  124. 'remark|备注' => 'max:255'
  125. ]);
  126. if ($val->check($param)) return json_show(1004, $val->getError());
  127. $info = Db::name('sale_return_child')
  128. ->alias('a')
  129. ->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')
  130. ->leftJoin('sale b', 'b.orderCode=a.orderCode')
  131. ->leftJoin('order_out_child c', 'c.outChildCode=a.outChildCode AND c.is_del=0')
  132. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  133. ->findOrEmpty();
  134. if (empty($info)) return json_show(1004, '该退货工单不存在');
  135. if ($info['status'] == 1) return json_show(1004, '该退货工单已收货');
  136. if ($param['loss_num'] > $info['num']) return json_show(1004, '丢失数量大于下单数量');
  137. Db::startTrans();
  138. try {
  139. $date = date('Y-m-d H:i:s');
  140. //维护退货工单
  141. Db::name('sale_return_child')
  142. ->where(['is_del' => 0, 'id' => $param['id'], 'status' => 1])
  143. ->update([
  144. 'status' => 2,
  145. 'updatetime' => $date,
  146. 'good_receive_type' => $param['good_receive_type'],
  147. 'loss_num' => $param['loss_num'],
  148. 'remark' => $param['remark'],
  149. 'record' => $param['record']
  150. ]);
  151. //检查所属销售单的退货工单是否全部完成退货
  152. $temp = Db::name('sale_return_child')
  153. ->field('id')
  154. ->where(['is_del' => 0, 'returnCode' => $info['returnCode'], 'status' => 1])
  155. ->findOrEmpty();
  156. if (empty($temp)) {
  157. Db::name('sale_return')
  158. ->where(['is_del' => 0, 'returnCode' => $info['returnCode'], 'status' => 12])
  159. ->update(['status' => 4, 'updatetime' => $date]);
  160. }
  161. //发货工单数量减少
  162. Db::name('order_out_child')
  163. ->where(['is_del' => 0, 'outChildCode' => $info['outChildCode']])
  164. ->dec('num', $info['return_num'])
  165. ->update(['updatetime' => $date]);
  166. //维护bn
  167. if ($info['order_out_status'] == 1) {
  168. //待发货,说明此时有库存,有bn号
  169. Db::name('child_bn')
  170. ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode'], 'childCode' => $info['outChildCode']])
  171. ->inc('num', $info['return_num'])
  172. ->update(['updatetime' => $date]);
  173. Db::name('good_stock')
  174. ->where(['is_del' => 0, 'spuCode' => $info['spuCode'], 'wsm_code' => $info['return_wsm_code']])
  175. ->inc('usable_stock', $info['return_num'])
  176. ->dec('wait_out_stock', $info['return_num'])
  177. ->update(['updatetime' => $date]);
  178. }
  179. Db::commit();
  180. return json_show(0, '设置退货工单成功');
  181. } catch (Exception $exception) {
  182. Db::rollback();
  183. return json_show(1004, $exception->getMessage());
  184. }
  185. }
  186. }