OrderOutChild.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace app\admin\controller;
  3. //发货工单(从属于发货单)
  4. use app\admin\model\GoodStockInfo;
  5. use think\Exception;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. class OrderOutChild extends Base
  9. {
  10. //分单时可用仓库列表
  11. public function getWsmList(){
  12. $outCode = $this->request->post('outCode','','trim');
  13. if($outCode=='') return json_show(1004,'发货单号不能为空');
  14. $out=Db::name('order_out')
  15. ->alias('a')
  16. ->field('a.id,a.outCode,a.send_status,b.good_code')
  17. ->leftJoin('sale b','b.orderCode=a.orderCode')
  18. ->where(['a.is_del'=>0,'a.outCode'=>$outCode])
  19. ->findOrEmpty();
  20. if(empty($out)) return json_show(1004,'该发货单不存在');
  21. if($out['send_status'] != 0) return json_show(1004,'该发货单状态错误');
  22. $list = Db::name("good_stock")
  23. ->alias("a")
  24. ->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code")
  25. ->field("a.id,a.wsm_code,b.name wsm_name,b.companyNo,b.companyName,b.supplierNo,b.supplierName,b.contactor_name,a.usable_stock")
  26. ->where(["spuCode"=>$out['good_code'],"a.is_del"=>0])
  27. ->order(['id'=>'asc'])
  28. ->select()
  29. ->toArray();
  30. return json_show(0,'获取列表成功',$list);
  31. }
  32. //发货工单列表
  33. public function list()
  34. {
  35. $param = $this->request->only([
  36. 'page' => 1,
  37. 'size' => 10,
  38. 'start' => '',
  39. 'end' => '',
  40. 'orderCode' => '',
  41. 'customer_code' => '',
  42. 'status' => '',
  43. 'outChildCode' => '',
  44. 'companyNo' => '',
  45. 'spuCode' => '',
  46. 'outCode' => '',
  47. 'order_source' => '',
  48. 'wsm_code' => '',
  49. 'supplierNo' => '',
  50. 'skuCode' => '',
  51. 'apply_id' => '',
  52. 'apply_name' => '',
  53. 'order_type' => '',
  54. ], 'post', 'trim');
  55. $where = [['a.is_del', '=', 0]];
  56. if ($param['start'] != '') $where[] = ['a.addtime', '>=', $param['start']];
  57. if ($param['end'] != '') $where[] = ['a.addtime', '<', $param['end'] . ' 23:59:59'];
  58. if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
  59. if ($param['customer_code'] != '') $where[] = ['a.customer_code', 'like', '%' . $param['customer_code'] . '%'];
  60. if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
  61. if ($param['outChildCode'] != '') $where[] = ['a.outChildCode', 'like', '%' . $param['outChildCode'] . '%'];
  62. if ($param['companyNo'] != '') $where[] = ['a.companyNo', 'like', '%' . $param['companyNo'] . '%'];
  63. if ($param['spuCode'] != '') $where[] = ['a.spuCode', 'like', '%' . $param['spuCode'] . '%'];
  64. if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
  65. if ($param['order_source'] != '') $where[] = ['a.order_source', '=', $param['order_source']];
  66. if ($param['wsm_code'] != '') $where[] = ['a.wsm_code', 'like', '%' . $param['wsm_code'] . '%'];
  67. if ($param['supplierNo'] != '') $where[] = ['a.supplierNo', 'like', '%' . $param['supplierNo'] . '%'];
  68. if ($param['skuCode'] != '') $where[] = ['a.skuCode', 'like', '%' . $param['skuCode'] . '%'];
  69. if ($param['apply_id'] !== '') $where[] = ['a.apply_id', '=', $param['apply_id']];
  70. if ($param['apply_name'] != '') $where[] = ['a.apply_name', 'like', '%' . $param['apply_name'] . '%'];
  71. if ($param['order_type'] != '') $where[] = ['a.order_type', '=', $param['order_type']];
  72. $count = Db::name('order_out_child')
  73. ->alias('a')
  74. ->where($where)
  75. ->count('a.id');
  76. $list = Db::name('order_out_child')
  77. ->alias('a')
  78. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  79. ->page($param['page'], $param['size'])
  80. ->where($where)
  81. ->select()
  82. ->toArray();
  83. $all_apply_id = array_column($list, 'apply_id');
  84. $company_name = get_company_name_by_uid($all_apply_id);
  85. $data = [];
  86. foreach ($list as $value) {
  87. $value['company_name'] = $company_name[$value['apply_id']] ?? '';
  88. $data[] = $value;
  89. }
  90. return json_show(0, '获取成功', ['count' => $count, 'list' => $data]);
  91. }
  92. //导出
  93. public function export()
  94. {
  95. $param = $this->request->only([
  96. 'start' => '',
  97. 'end' => '',
  98. 'orderCode' => '',
  99. 'customer_code' => '',
  100. 'status' => '',
  101. 'outChildCode' => '',
  102. 'companyNo' => '',
  103. 'spuCode' => '',
  104. 'outCode' => '',
  105. 'order_source' => '',
  106. 'wsm_code' => '',
  107. 'supplierNo' => '',
  108. 'skuCode' => '',
  109. 'apply_id' => '',
  110. 'apply_name' => '',
  111. 'order_type' => '',
  112. ], 'post', 'trim');
  113. $where = [['a.id', '=', 0]];
  114. if ($param['start'] != '') $where[] = ['a.addtime', '>=', $param['start']];
  115. if ($param['end'] != '') $where[] = ['a.addtime', '<', $param['end'] . ' 23:59:59'];
  116. if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
  117. if ($param['customer_code'] != '') $where[] = ['a.customer_code', 'like', '%' . $param['customer_code'] . '%'];
  118. if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
  119. if ($param['outChildCode'] != '') $where[] = ['a.outChildCode', 'like', '%' . $param['outChildCode'] . '%'];
  120. if ($param['companyNo'] != '') $where[] = ['a.companyNo', 'like', '%' . $param['companyNo'] . '%'];
  121. if ($param['spuCode'] != '') $where[] = ['a.spuCode', 'like', '%' . $param['spuCode'] . '%'];
  122. if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
  123. if ($param['order_source'] != '') $where[] = ['a.order_source', '=', $param['order_source']];
  124. if ($param['wsm_code'] != '') $where[] = ['a.wsm_code', 'like', '%' . $param['wsm_code'] . '%'];
  125. if ($param['supplierNo'] != '') $where[] = ['a.supplierNo', 'like', '%' . $param['supplierNo'] . '%'];
  126. if ($param['skuCode'] != '') $where[] = ['a.skuCode', 'like', '%' . $param['skuCode'] . '%'];
  127. if ($param['apply_id'] !== '') $where[] = ['a.apply_id', '=', $param['apply_id']];
  128. if ($param['apply_name'] != '') $where[] = ['a.apply_name', 'like', '%' . $param['apply_name'] . '%'];
  129. if ($param['order_type'] != '') $where[] = ['a.order_type', '=', $param['order_type']];
  130. $list = Db::name('order_out_child')
  131. ->alias('a')
  132. ->field('a.outChildCode 发货工单号,a.orderCode 确认单编号,a.outCode 发货申请单号,a.companyNo 业务公司编号,a.companyName 业务公司名称,a.customer_code 客户编号,a.customer_name 客户名称,a.supplierNo 供应商编号,a.supplierName 供应商名称,a.spuCode 商品成本编号,a.skuCode 商品上线编号,a.good_name 商品名称,case a.order_source when 1 then "直接下单" when 2 then "咨询" when 3 then "项目" when 4 then "平台" when 5 then "有赞" when 6 then "售后补换货" when 7 then "报备转单" when 8 then "支付渠道" end "订单来源",case order_type when 1 then "备库" when 2 then "非库存" when 3 then "咨询商品" when 4 then "报备商品" end "商品类型",a.num 数量,case a.status when 1 then "待发货" when 2 then "发货完成" when 3 then "已收货" when 4 then "已全部退货" end "分单状态",a.addtime 下单时间,a.apply_id,a.apply_name 申请人名称')
  133. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  134. ->select()
  135. ->toArray();
  136. // $all_apply_id = array_column($list, 'apply_id');
  137. // $company_name = get_company_name_by_uid($all_apply_id);
  138. // $data = [];
  139. // foreach ($list as &$value) {
  140. //
  141. // $value['company_name'] = $company_name[$value['apply_id']] ?? '';
  142. // unset($value['apply_id']);
  143. //
  144. // }
  145. if (empty($list)) $list[] = ['没有相关可导出的数据'];
  146. excelSave('发货工单' . date('YmdHis'), array_keys($list[0]), $list);
  147. // return json_show(0, '获取成功', $data);
  148. }
  149. //分单
  150. public function add()
  151. {
  152. $param = $this->request->only(['outCode', 'list'], 'post', 'trim');
  153. $val = Validate::rule([
  154. 'outCode|发货单号' => 'require',
  155. 'list' => 'require|array|max:100',
  156. ]);
  157. // $list=[
  158. // ['wsm_code'=>'','num'=>100],
  159. // ['wsm_code'=>'','num'=>100],
  160. // ];
  161. if ($val->check($param) == false) return json_show(1004, $val->getError());
  162. $val_child = Validate::rule([
  163. 'wsm_code|仓库编码' => 'require',
  164. 'num|发货数量' => 'require|number|gt:0|elt:999999999999',
  165. ]);
  166. $info = Db::name('order_out')
  167. ->alias('a')
  168. ->field('a.*,b.supplierNo companyNo,b.supplierName companyName,b.customer_code,b.customerName,b.supNo supplierNo,b.supName supplierName,b.good_code,b.skuCode,b.good_name,b.order_source,b.good_num,b.wsend_num')
  169. ->leftJoin('sale b', 'b.orderCode=a.orderCode AND b.is_del=0')
  170. ->where(['a.is_del' => 0, 'a.outCode' => $param['outCode'], 'a.send_status' => 0])
  171. ->findOrEmpty();
  172. if (empty($info)) return json_show(1004, '该发货单不存在或状态有误');
  173. if ($info['wsend_num'] < $info['send_num']) return json_show(1004,"订单待发货数量不足");
  174. if ($info['wsend_num'] < array_sum($param['list'],'num')) return json_show(1004,"仓库总发货数与发货单待发货数不同");
  175. //所有仓库信息
  176. $wsm = Db::name('warehouse_info')
  177. ->where(['is_del' => 0, 'wsm_code' => array_column($param['list'], 'wsm_code')])
  178. ->column('id', 'wsm_code');
  179. Db::startTrans();
  180. try {
  181. $date = date('Y-m-d H:i:s');
  182. $insert = [];
  183. $i = 0;
  184. foreach ($param['list'] as $value) {
  185. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  186. //改变编码规则,将原编码后两位换成序列号
  187. //str_pad字符串填充
  188. $outChildCode = substr(makeNo('TCD'), 0, -2) . str_pad($i++, 2, '0', STR_PAD_LEFT);
  189. $insert[] = [
  190. 'outChildCode' => $outChildCode,
  191. 'orderCode' => $info['orderCode'],
  192. 'outCode' => $param['outCode'],
  193. 'companyNo' => $info['companyNo'],
  194. 'companyName' => $info['companyName'],
  195. 'customer_code' => $info['customer_code'],
  196. 'customer_name' => $info['customerName'],
  197. 'supplierNo' => $info['supplierNo'],
  198. 'supplierName' => $info['supplierName'],
  199. 'spuCode' => $info['good_code'],
  200. 'skuCode' => $info['skuCode'],
  201. 'good_name' => $info['good_name'],
  202. 'order_type' => $info['order_type'],
  203. 'order_source' => $info['order_source'],
  204. 'num' => $value['num'],
  205. 'wsm_code' => $value['wsm_code'],
  206. 'apply_id' => $info['apply_id'],
  207. 'apply_name' => $info['apply_name'],
  208. 'addrid' => $info['addrid'],
  209. 'status' => 1,
  210. 'is_del' => 0,
  211. 'addtime' => $date,
  212. 'updatetime' => $date,
  213. ];
  214. if (!isset($wsm[$value['wsm_code']])) throw new Exception($value['wsm_code'] . '该仓库不存在');
  215. //维护bn号
  216. GoodStockInfo::ChildAddBn($outChildCode, $wsm[$value['wsm_code']]);
  217. }
  218. Db::name('order_out_child')->insertAll($insert);
  219. Db::name('order_out')
  220. ->where(['id' => $info['id'], 'is_del' => 0, 'outCode' => $param['outCode'], 'send_status' => 0])
  221. ->update(['send_status' => 1, 'status' => 1]);
  222. Db::commit();
  223. return json_show(0, '分单完成');
  224. } catch (Exception $exception) {
  225. Db::rollback();
  226. return json_show(1004, '分单失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  227. }
  228. }
  229. //详情
  230. public function info()
  231. {
  232. $outChildCode = $this->request->post('outChildCode','','trim');
  233. if($outChildCode=='') return json_show(1004,'发货工单号不能为空');
  234. $info = Db::name('order_out_child')
  235. ->where(['is_del'=>0,'outChildCode'=>$outChildCode])
  236. ->findOrEmpty();
  237. return json_show(0,'获取详情成功',$info);
  238. }
  239. //发货
  240. public function send()
  241. {
  242. $param = $this->request->post('list/a','','trim');
  243. $temp=Db::name('order_out_child')
  244. ->field('id,outChildCode')
  245. ->where(['is_del'=>0,'outChildCode'=>array_column($param,'outChildCode')])
  246. ->where('status','<>',1)
  247. ->findOrEmpty();
  248. if(!empty($temp)) return json_show(1004,$temp['outChildCode'].'状态错误,不能发货');
  249. $child = Db::name('order_out_child')
  250. ->where(['is_del' => 0, 'status' => 1, 'outChildCode' => array_column($param, 'outChildCode')])
  251. ->column('id,orderCode,outCode,total_num,num', 'outChildCode');
  252. $order_out = Db::name('order_out')
  253. ->where(['is_del'=>0,'outCode'=>array_unique(array_column($child,'outCode'))])
  254. ->column('id,send_num,send_status,0 already_send_num','outCode');
  255. $sale = Db::name('sale')
  256. ->where(['is_del'=>0,'orderCode'=>array_unique(array_column($child,'orderCode'))])
  257. ->column('id,good_num,send_num,wsend_num','orderCode');
  258. $val=Validate::rule([
  259. 'outChildCode|发货工单号'=>'require|max:255',
  260. 'post_name|物流公司'=>'require|max:255',
  261. 'post_code|物流单号'=>'require|alphaDash|max:255',
  262. 'post_fee|物流费用'=>'require|egt:0|max:99999999.99'
  263. ]);
  264. Db::startTrans();
  265. try {
  266. $date = date('Y-m-d H:i:s');
  267. foreach ($param as $value){
  268. if($val->check($value)==false) throw new Exception($val->getError());
  269. //工单
  270. if(!isset($child[$value['outChildCode']])) throw new Exception($value['outChildCode'].'工单不存在或状态不允许发货');
  271. Db::name('order_out_child')
  272. ->where(['id' => $child[$value['outChildCode']]['id'], 'is_del' => 0, 'status' => 1])
  273. ->update([
  274. 'post_name' => $value['post_name'],
  275. 'post_code' => $value['post_code'],
  276. 'post_fee' => $value['post_fee'],
  277. 'status' => 2,
  278. 'sendtime' => $date,
  279. 'updatetime' => $date,
  280. ]);
  281. $order_out[$child[$value['outChildCode']]['outCode']]['already_send_num'] += $child[$value['outChildCode']]['num'];
  282. //发货单
  283. //send_status 2部分发货,3全部发货
  284. Db::name('order_out')
  285. ->where(['id'=>$order_out[$child[$value['outChildCode']]['outCode']]['id']])
  286. ->update(['send_status'=>$order_out[$child[$value['outChildCode']]['outCode']]['already_send_num']>=$order_out[$child[$value['outChildCode']]['outCode']]['send_num']?3:2,'updatetime'=>$date]);
  287. //销售单
  288. Db::name('sale')
  289. ->where(['is_del'=>0,'id'=>$sale[$child[$value['outChildCode']]['orderCode']]['id']])
  290. ->update([
  291. 'send_num'=>++$sale[$child[$value['outChildCode']]['orderCode']]['send_num'],
  292. 'wsend_num'=>--$sale[$child[$value['outChildCode']]['orderCode']]['wsend_num'],
  293. 'send_status'=>$sale[$child[$value['outChildCode']]['orderCode']]['send_num']>=$sale[$child[$value['outChildCode']]['orderCode']]['good_num']?3:2,
  294. ]);
  295. //库存
  296. Db::name('good_stock')
  297. ->where(['is_del'=>0,'spuCode'=>'','wsm_code'=>''])
  298. ->dec('wait_out_stock',$child[$value['outChildCode']]['num'])
  299. ->update(['updatetime'=>$date]);
  300. }
  301. Db::commit();
  302. return json_show(0,'操作完成');
  303. }catch (Exception $exception){
  304. Db::rollback();
  305. return json_show(1004, $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  306. }
  307. }
  308. }