OrderOutChild.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. {
  13. $outCode = $this->request->post('outCode', '', 'trim');
  14. if ($outCode == '') return json_show(1004, '发货单号不能为空');
  15. $out = Db::name('order_out')
  16. ->alias('a')
  17. ->field('a.id,a.outCode,a.send_status,b.good_code')
  18. ->leftJoin('sale b', 'b.orderCode=a.orderCode')
  19. ->where(['a.is_del' => 0, 'a.outCode' => $outCode])
  20. ->findOrEmpty();
  21. if (empty($out)) return json_show(1004, '该发货单不存在');
  22. if ($out['send_status'] != 0) return json_show(1004, '该发货单状态错误');
  23. $list = Db::name("good_stock")
  24. ->alias("a")
  25. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  26. ->field("a.id,a.wsm_code,b.name wsm_name,b.companyNo,b.companyName,b.supplierNo,b.supplierName,b.contactor_name,a.usable_stock")
  27. ->where(["spuCode" => $out['good_code'], "a.is_del" => 0])
  28. ->order(['id' => 'asc'])
  29. ->select()
  30. ->toArray();
  31. return json_show(0, '获取列表成功', $list);
  32. }
  33. //发货工单列表
  34. public function list()
  35. {
  36. $param = $this->request->only([
  37. 'page' => 1,
  38. 'size' => 10,
  39. 'start' => '',
  40. 'end' => '',
  41. 'orderCode' => '',
  42. 'customer_code' => '',
  43. 'status' => '',
  44. 'outChildCode' => '',
  45. 'companyNo' => '',
  46. 'spuCode' => '',
  47. 'outCode' => '',
  48. 'order_source' => '',
  49. 'wsm_code' => '',
  50. 'supplierNo' => '',
  51. 'skuCode' => '',
  52. 'apply_id' => '',
  53. 'apply_name' => '',
  54. 'order_type' => '',
  55. ], 'post', 'trim');
  56. $where = [['a.is_del', '=', 0]];
  57. if ($param['start'] != '') $where[] = ['a.addtime', '>=', $param['start']];
  58. if ($param['end'] != '') $where[] = ['a.addtime', '<', $param['end'] . ' 23:59:59'];
  59. if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
  60. if ($param['customer_code'] != '') $where[] = ['a.customer_code', 'like', '%' . $param['customer_code'] . '%'];
  61. if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
  62. if ($param['outChildCode'] != '') $where[] = ['a.outChildCode', 'like', '%' . $param['outChildCode'] . '%'];
  63. if ($param['companyNo'] != '') $where[] = ['a.companyNo', 'like', '%' . $param['companyNo'] . '%'];
  64. if ($param['spuCode'] != '') $where[] = ['a.spuCode', 'like', '%' . $param['spuCode'] . '%'];
  65. if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
  66. if ($param['order_source'] != '') $where[] = ['a.order_source', '=', $param['order_source']];
  67. if ($param['wsm_code'] != '') $where[] = ['a.wsm_code', 'like', '%' . $param['wsm_code'] . '%'];
  68. if ($param['supplierNo'] != '') $where[] = ['a.supplierNo', 'like', '%' . $param['supplierNo'] . '%'];
  69. if ($param['skuCode'] != '') $where[] = ['a.skuCode', 'like', '%' . $param['skuCode'] . '%'];
  70. if ($param['apply_id'] !== '') $where[] = ['a.apply_id', '=', $param['apply_id']];
  71. if ($param['apply_name'] != '') $where[] = ['a.apply_name', 'like', '%' . $param['apply_name'] . '%'];
  72. if ($param['order_type'] != '') $where[] = ['a.order_type', '=', $param['order_type']];
  73. $count = Db::name('order_out_child')
  74. ->alias('a')
  75. ->where($where)
  76. ->count('a.id');
  77. $list = Db::name('order_out_child')
  78. ->alias('a')
  79. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  80. ->page($param['page'], $param['size'])
  81. ->where($where)
  82. ->select()
  83. ->toArray();
  84. $all_apply_id = array_column($list, 'apply_id');
  85. $company_name = get_company_name_by_uid($all_apply_id);
  86. $data = [];
  87. foreach ($list as $value) {
  88. $value['company_name'] = $company_name[$value['apply_id']] ?? '';
  89. $data[] = $value;
  90. }
  91. return json_show(0, '获取成功', ['count' => $count, 'list' => $data]);
  92. }
  93. //导出
  94. public function export()
  95. {
  96. $param = $this->request->only([
  97. 'start' => '',
  98. 'end' => '',
  99. 'orderCode' => '',
  100. 'customer_code' => '',
  101. 'status' => '',
  102. 'outChildCode' => '',
  103. 'companyNo' => '',
  104. 'spuCode' => '',
  105. 'outCode' => '',
  106. 'order_source' => '',
  107. 'wsm_code' => '',
  108. 'supplierNo' => '',
  109. 'skuCode' => '',
  110. 'apply_id' => '',
  111. 'apply_name' => '',
  112. 'order_type' => '',
  113. ], 'post', 'trim');
  114. $where = [['a.id', '=', 0]];
  115. if ($param['start'] != '') $where[] = ['a.addtime', '>=', $param['start']];
  116. if ($param['end'] != '') $where[] = ['a.addtime', '<', $param['end'] . ' 23:59:59'];
  117. if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
  118. if ($param['customer_code'] != '') $where[] = ['a.customer_code', 'like', '%' . $param['customer_code'] . '%'];
  119. if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
  120. if ($param['outChildCode'] != '') $where[] = ['a.outChildCode', 'like', '%' . $param['outChildCode'] . '%'];
  121. if ($param['companyNo'] != '') $where[] = ['a.companyNo', 'like', '%' . $param['companyNo'] . '%'];
  122. if ($param['spuCode'] != '') $where[] = ['a.spuCode', 'like', '%' . $param['spuCode'] . '%'];
  123. if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
  124. if ($param['order_source'] != '') $where[] = ['a.order_source', '=', $param['order_source']];
  125. if ($param['wsm_code'] != '') $where[] = ['a.wsm_code', 'like', '%' . $param['wsm_code'] . '%'];
  126. if ($param['supplierNo'] != '') $where[] = ['a.supplierNo', 'like', '%' . $param['supplierNo'] . '%'];
  127. if ($param['skuCode'] != '') $where[] = ['a.skuCode', 'like', '%' . $param['skuCode'] . '%'];
  128. if ($param['apply_id'] !== '') $where[] = ['a.apply_id', '=', $param['apply_id']];
  129. if ($param['apply_name'] != '') $where[] = ['a.apply_name', 'like', '%' . $param['apply_name'] . '%'];
  130. if ($param['order_type'] != '') $where[] = ['a.order_type', '=', $param['order_type']];
  131. $list = Db::name('order_out_child')
  132. ->alias('a')
  133. ->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 申请人名称')
  134. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  135. ->select()
  136. ->toArray();
  137. // $all_apply_id = array_column($list, 'apply_id');
  138. // $company_name = get_company_name_by_uid($all_apply_id);
  139. // $data = [];
  140. // foreach ($list as &$value) {
  141. //
  142. // $value['company_name'] = $company_name[$value['apply_id']] ?? '';
  143. // unset($value['apply_id']);
  144. //
  145. // }
  146. if (empty($list)) $list[] = ['没有相关可导出的数据'];
  147. excelSave('发货工单' . date('YmdHis'), array_keys($list[0]), $list);
  148. // return json_show(0, '获取成功', $data);
  149. }
  150. //拆单
  151. public function add()
  152. {
  153. $param = $this->request->only(['outCode', 'list'], 'post', 'trim');
  154. $val = Validate::rule([
  155. 'outCode|发货单号' => 'require',
  156. 'list' => 'require|array|max:100',
  157. ]);
  158. // $list=[
  159. // ['wsm_code'=>'','num'=>100],
  160. // ['wsm_code'=>'','num'=>100],
  161. // ];
  162. if ($val->check($param) == false) return json_show(1004, $val->getError());
  163. $val_child = Validate::rule([
  164. 'wsm_code|仓库编码' => 'require',
  165. 'num|发货数量' => 'require|number|gt:0|elt:999999999999',
  166. ]);
  167. $info = Db::name('order_out')
  168. ->alias('a')
  169. ->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')
  170. ->leftJoin('sale b', 'b.orderCode=a.orderCode AND b.is_del=0')
  171. ->where(['a.is_del' => 0, 'a.outCode' => $param['outCode'], 'a.send_status' => 0])
  172. ->findOrEmpty();
  173. if (empty($info)) return json_show(1004, '该发货单不存在或状态有误');
  174. if ($info['wsend_num'] < $info['send_num']) return json_show(1004, "订单待发货数量不足");
  175. if ($info['wsend_num'] < array_sum(array_column($param['list'], 'num'))) return json_show(1004, "仓库总发货数与发货单待发货数不同");
  176. //所有仓库信息
  177. $wsm = Db::name('good_stock')
  178. ->where(['is_del' => 0, 'spuCode' => $info['good_code'], 'wsm_code' => array_column($param['list'], 'wsm_code')])
  179. ->column('id', 'wsm_code');
  180. Db::startTrans();
  181. try {
  182. $date = date('Y-m-d H:i:s');
  183. $insert = [];
  184. $i = 0;
  185. foreach ($param['list'] as $value) {
  186. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  187. //改变编码规则,将原编码后两位换成序列号
  188. //str_pad字符串填充
  189. $outChildCode = substr(makeNo('TCD'), 0, -2) . str_pad($i++, 2, '0', STR_PAD_LEFT);
  190. $insert[] = [
  191. 'outChildCode' => $outChildCode,
  192. 'orderCode' => $info['orderCode'],
  193. 'outCode' => $param['outCode'],
  194. 'companyNo' => $info['companyNo'],
  195. 'companyName' => $info['companyName'],
  196. 'customer_code' => $info['customer_code'],
  197. 'customer_name' => $info['customerName'],
  198. 'supplierNo' => $info['supplierNo'],
  199. 'supplierName' => $info['supplierName'],
  200. 'spuCode' => $info['good_code'],
  201. 'skuCode' => $info['skuCode'],
  202. 'good_name' => $info['good_name'],
  203. 'order_type' => $info['order_type'],
  204. 'order_source' => $info['order_source'],
  205. 'num' => $value['num'],
  206. 'wsm_code' => $value['wsm_code'],
  207. 'apply_id' => $info['apply_id'],
  208. 'apply_name' => $info['apply_name'],
  209. 'addrid' => $info['addrid'],
  210. 'status' => 1,
  211. 'is_del' => 0,
  212. 'addtime' => $date,
  213. 'updatetime' => $date,
  214. ];
  215. if (!isset($wsm[$value['wsm_code']])) throw new Exception($value['wsm_code'] . '该仓库不存在');
  216. }
  217. Db::name('order_out_child')->insertAll($insert);
  218. foreach ($insert as $item) {
  219. GoodStockInfo::ChildAddBn($item['outChildCode'], $wsm[$value['wsm_code']]);//维护bn号
  220. }
  221. Db::name('order_out')
  222. ->where(['id' => $info['id'], 'is_del' => 0, 'outCode' => $param['outCode'], 'send_status' => 0])
  223. ->update(['send_status' => 1, 'status' => 1]);
  224. Db::commit();
  225. return json_show(0, '分单完成');
  226. } catch (Exception $exception) {
  227. Db::rollback();
  228. return json_show(1004, '分单失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  229. }
  230. }
  231. //详情
  232. public function info()
  233. {
  234. $outChildCode = $this->request->post('outChildCode', '', 'trim');
  235. if ($outChildCode == '') return json_show(1004, '发货工单号不能为空');
  236. $info = Db::name('order_out_child')
  237. ->where(['is_del' => 0, 'outChildCode' => $outChildCode])
  238. ->findOrEmpty();
  239. return json_show(0, '获取详情成功', $info);
  240. }
  241. //发货
  242. public function send()
  243. {
  244. if ($this->level != 1) return json_show(0, '管理员不能操作');
  245. $param = $this->request->post('list/a', '', 'trim');
  246. $temp = Db::name('order_out_child')
  247. ->field('id,outChildCode')
  248. ->where(['is_del' => 0, 'outChildCode' => array_column($param, 'outChildCode')])
  249. ->where('status', '<>', 1)
  250. ->findOrEmpty();
  251. if (!empty($temp)) return json_show(1004, $temp['outChildCode'] . '状态错误,不能发货');
  252. $child = Db::name('order_out_child')
  253. ->where(['is_del' => 0, 'status' => 1, 'outChildCode' => array_column($param, 'outChildCode')])
  254. ->column('id,orderCode,outCode,num,wsm_code,supplierNo', 'outChildCode');
  255. //检查所有的发货单是否有正在进行的售后单
  256. $temp = Db::name('order_return')
  257. ->where(['is_del' => 0, 'outCode' => array_unique(array_column($child, 'outCode'))])
  258. ->whereNotIn('status', [5, 6, 8])
  259. ->field('id,outCode')
  260. ->findOrEmpty();
  261. if (!empty($temp)) return json_show(1004, $temp['outCode'] . '有正在进行中的售后单,无法发货');
  262. $order_out = Db::name('order_out')
  263. ->where(['is_del' => 0, 'outCode' => array_unique(array_column($child, 'outCode'))])
  264. ->column('id,send_num,send_status,0 already_send_num', 'outCode');
  265. $sale = Db::name('sale')
  266. ->where(['is_del' => 0, 'orderCode' => array_unique(array_column($child, 'orderCode'))])
  267. ->column('id,good_num,send_num,wsend_num,good_code,supNo,is_stock,order_source', 'orderCode');
  268. $is_reurn = Db::name("sale_return")
  269. ->field('id,orderCode')
  270. ->where(['orderCode' => array_column($child, 'orderCode'), 'is_del' => 0])
  271. ->where("status", "in", [1, 2, 3, 7, 9, 10])
  272. ->findOrEmpty();
  273. if (!empty($is_reurn)) return json_show(1004, $is_reurn['orderCode'] . "存在退货未处理完成");
  274. //判断供应商是否开通账号,若开通的话,则只能由供应商账号操作
  275. if ($this->level != 3) {
  276. $temp = checkHasAccountBySupplierNos(array_column($sale, 'supNo'));
  277. if (!empty($temp)) return json_show(1004, implode(',', array_keys($temp)) . '这些供应商已经开通账号,请使用供应商账号操作');
  278. }
  279. $val = Validate::rule([
  280. 'outChildCode|发货工单号' => 'require|max:255',
  281. 'post_name|物流公司' => 'require|max:255',
  282. 'post_code|物流单号' => 'require|alphaDash|max:255',
  283. 'post_fee|物流费用' => 'require|egt:0|max:99999999.99'
  284. ]);
  285. Db::startTrans();
  286. try {
  287. $date = date('Y-m-d H:i:s');
  288. //所有供应商负责人
  289. $userCommon = \app\admin\common\User::getIns();
  290. $person_temp = $userCommon->handle('sGetList', ['more_code' => array_unique(array_column($child, 'supplierNo'))]);
  291. $person = array_column($person_temp['data']['list'], 'personid', 'code');
  292. unset($userCommon);
  293. unset($person_temp);
  294. //所有仓库管理员
  295. $wsm_contactor = Db::name('warehouse_info')
  296. ->where(['is_del' => 0, 'wsm_code' => array_unique(array_column($child, 'wsm_code'))])
  297. ->column('contactor', 'wsm_code');
  298. $yz_tmp = [];
  299. foreach ($param as $value) {
  300. if ($val->check($value) == false) throw new Exception($val->getError());
  301. //如果是库存品,只能由库管操作
  302. //非库存品和采返商品只能由供应商负责人操作
  303. if ($sale[$child[$value['outChildCode']]['orderCode']]['is_stock'] == 1) {
  304. if ($this->uid != $wsm_contactor[$child[$value['outChildCode']]['wsm_code']]) throw new Exception('库存品只能由仓库管理员操作');
  305. } else {
  306. if ($this->uid != $person[$child[$value['outChildCode']]['supplierNo']]) throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  307. }
  308. //工单
  309. if (!isset($child[$value['outChildCode']])) throw new Exception($value['outChildCode'] . '工单不存在或状态不允许发货');
  310. Db::name('order_out_child')
  311. ->where(['id' => $child[$value['outChildCode']]['id'], 'is_del' => 0, 'status' => 1])
  312. ->update([
  313. 'post_name' => $value['post_name'],
  314. 'post_code' => $value['post_code'],
  315. 'post_fee' => $value['post_fee'],
  316. 'status' => 2,
  317. 'sendtime' => $date,
  318. 'updatetime' => $date,
  319. ]);
  320. $order_out[$child[$value['outChildCode']]['outCode']]['already_send_num'] += $child[$value['outChildCode']]['num'];
  321. //发货单
  322. //send_status 2部分发货,3全部发货
  323. Db::name('order_out')
  324. ->where(['id' => $order_out[$child[$value['outChildCode']]['outCode']]['id']])
  325. ->update(['send_status' => $order_out[$child[$value['outChildCode']]['outCode']]['already_send_num'] >= $order_out[$child[$value['outChildCode']]['outCode']]['send_num'] ? 3 : 2, 'updatetime' => $date]);
  326. $sale[$child[$value['outChildCode']]['orderCode']]['send_num'] += $child[$value['outChildCode']]['num'];
  327. $sale[$child[$value['outChildCode']]['orderCode']]['wsend_num'] -= $child[$value['outChildCode']]['num'];
  328. //销售单
  329. $send_status = $sale[$child[$value['outChildCode']]['orderCode']]['send_num'] >= $sale[$child[$value['outChildCode']]['orderCode']]['good_num'] ? 3 : 2;
  330. Db::name('sale')
  331. ->where(['is_del' => 0, 'id' => $sale[$child[$value['outChildCode']]['orderCode']]['id']])
  332. ->update([
  333. 'send_num' => $sale[$child[$value['outChildCode']]['orderCode']]['send_num'],
  334. 'wsend_num' => $sale[$child[$value['outChildCode']]['orderCode']]['wsend_num'],
  335. 'send_status' => $send_status,
  336. ]);
  337. if ($sale[$child[$value['outChildCode']]['orderCode']]['order_source'] == 5 && $send_status == 3) {
  338. $yz_tmp[] = [
  339. 'post_name' => $value['post_name'],
  340. 'post_code' => $value['post_code'],
  341. 'outCode' => $child[$value['outChildCode']]['outCode'],
  342. 'orderCode' => $child[$value['outChildCode']]['orderCode'],
  343. ];
  344. }
  345. //库存
  346. Db::name('good_stock')
  347. ->where(['is_del' => 0, 'spuCode' => $sale[$child[$value['outChildCode']]['orderCode']]['good_code'], 'wsm_code' => $child[$value['outChildCode']]['wsm_code']])
  348. ->dec('wait_out_stock', $child[$value['outChildCode']]['num'])
  349. ->update(['updatetime' => $date]);
  350. }
  351. //日志、待办已办……
  352. Db::commit();
  353. //如果是有赞订单 且 所属发货单全部发货了,将发货信息推到有赞
  354. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  355. foreach ($yz_tmp as $temp) {
  356. curl_request(config('app.yz_domain') . 'api/yz_out_send', ['orderCode' => $temp['orderCode'], 'out_stype' => $temp['post_name'], 'post_code' => $temp['post_code'], 'uid' => $this->uid, 'uname' => $this->uname, 'order_out' => $temp['outCode']]);
  357. }
  358. // if ($einfo['order_source'] == 5) {
  359. // $res = curl_request(config('app.yz_domain') . 'api/yz_out_send', ['orderCode' => $einfo['orderCode'], 'out_stype' => $post_name, 'post_code' => $post_code, 'uid' => $uid, 'uname' => $uname, 'order_out' => $outCode]);
  360. // $res = json_decode($res, true);
  361. // if ($res['code'] != 0) return app_show(0, '发货成功,' . $res['message']);
  362. // }
  363. return json_show(0, '操作完成');
  364. } catch (Exception $exception) {
  365. Db::rollback();
  366. return json_show(1004, $exception->getMessage());
  367. }
  368. }
  369. }