OrderOutChild.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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\GoodStockInfo;
  7. use app\admin\model\ProcessOrder;
  8. use think\Exception;
  9. use think\facade\Db;
  10. use think\facade\Validate;
  11. class OrderOutChild extends Base
  12. {
  13. //分单时可用仓库列表
  14. public function getWsmList()
  15. {
  16. $outCode = $this->request->post('outCode', '', 'trim');
  17. if ($outCode == '') return json_show(1004, '发货单号不能为空');
  18. $out = Db::name('order_out')
  19. ->alias('a')
  20. ->field('a.id,a.outCode,a.send_status,b.good_code')
  21. ->leftJoin('sale b', 'b.orderCode=a.orderCode')
  22. ->where(['a.is_del' => 0, 'a.outCode' => $outCode])
  23. ->findOrEmpty();
  24. if (empty($out)) return json_show(1004, '该发货单不存在');
  25. if ($out['send_status'] != 0) return json_show(1004, '该发货单状态错误');
  26. $list = Db::name("good_stock")
  27. ->alias("a")
  28. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  29. ->field("a.id,a.wsm_code,b.name wsm_name,b.companyNo,b.companyName,b.supplierNo,b.supplierName,b.contactor_name,a.usable_stock")
  30. ->where(["spuCode" => $out['good_code'], "a.is_del" => 0])
  31. ->order(['id' => 'asc'])
  32. ->select()
  33. ->toArray();
  34. return json_show(0, '获取列表成功', $list);
  35. }
  36. //发货工单列表
  37. public function list()
  38. {
  39. $param = $this->request->only([
  40. 'page' => 1,
  41. 'size' => 10,
  42. 'start' => '',
  43. 'end' => '',
  44. 'orderCode' => '',
  45. 'customer_code' => '',
  46. 'status' => '',
  47. 'outChildCode' => '',
  48. 'companyNo' => '',
  49. 'spuCode' => '',
  50. 'outCode' => '',
  51. 'order_source' => '',
  52. 'wsm_code' => '',
  53. 'supplierNo' => '',
  54. 'skuCode' => '',
  55. 'apply_id' => '',
  56. 'apply_name' => '',
  57. 'order_type' => '',
  58. 'relaComNo' => '',
  59. 'use_type' => '',
  60. 'IsByMe' => '0',
  61. ], 'post', 'trim');
  62. $where = [['a.is_del', '=', 0]];
  63. if ($param['start'] != '') $where[] = ['a.addtime', '>=', $param['start']];
  64. if ($param['end'] != '') $where[] = ['a.addtime', '<', $param['end'] . ' 23:59:59'];
  65. if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
  66. if ($param['customer_code'] != '') $where[] = ['a.customer_code', 'like', '%' . $param['customer_code'] . '%'];
  67. if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
  68. if ($param['outChildCode'] != '') $where[] = ['a.outChildCode', 'like', '%' . $param['outChildCode'] . '%'];
  69. if ($param['companyNo'] != '') $where[] = ['a.companyNo', 'like', '%' . $param['companyNo'] . '%'];
  70. if ($param['spuCode'] != '') $where[] = ['a.spuCode', 'like', '%' . $param['spuCode'] . '%'];
  71. if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
  72. if ($param['order_source'] != '') $where[] = ['a.order_source', '=', $param['order_source']];
  73. if ($param['wsm_code'] != '') $where[] = ['a.wsm_code', 'like', '%' . $param['wsm_code'] . '%'];
  74. if ($param['supplierNo'] != '') $where[] = ['a.supplierNo', 'like', '%' . $param['supplierNo'] . '%'];
  75. if ($param['skuCode'] != '') $where[] = ['a.skuCode', 'like', '%' . $param['skuCode'] . '%'];
  76. if ($param['apply_id'] !== '') $where[] = ['a.apply_id', '=', $param['apply_id']];
  77. if ($param['apply_name'] != '') $where[] = ['a.apply_name', 'like', '%' . $param['apply_name'] . '%'];
  78. if ($param['order_type'] != '') $where[] = ['a.order_type', '=', $param['order_type']];
  79. if ($param['relaComNo'] != '') $where[] = ['a.companyNo', '=', $param['relaComNo']];
  80. if ($param['use_type'] !== '') $where[] = ['p.use_type', '=', $param['use_type']];
  81. $condition = '1=1';
  82. if($param['IsByMe']==1)$condition = '1=0';
  83. //只有level2的账号过滤数据权限
  84. if ($this->level == 2) {
  85. //库管只能看到库存品订单,供应商负责人只能看到非库存品订单
  86. //是否是仓库管理员
  87. $tmp = Db::name('warehouse_info')->field('id')->where(['is_del' => 0, 'contactor' => $this->uid])->findOrEmpty();
  88. //库管看到所有的库存品发货申请单
  89. if (!empty($tmp)) $condition .= " or (a.order_type=1 AND c.contactor=" . $this->uid.")";
  90. else {
  91. if($param['IsByMe']==0){
  92. $role = $this->checkDataShare();
  93. if (!empty($role[DataGroupModel::$type_全部])) {
  94. // if ($condition != '') $condition .= " or ";
  95. $condition .= " or (a.apply_id in (" . implode(',', $role[DataGroupModel::$type_全部]) . "))";
  96. }
  97. }
  98. }
  99. $person_supplier = Db::connect('mysql_sys')
  100. ->name('supplier')
  101. ->where(['is_del' => 0, 'personid' => $this->uid])
  102. ->column('code');
  103. if ($person_supplier) {
  104. if($param['IsByMe']==1){
  105. $suppe = checkHasAccountBySupplierNos($person_supplier);
  106. $suppAcco =array_diff($person_supplier,array_keys($suppe));
  107. $person_supplier= $suppAcco;
  108. }
  109. $condition .= " or (a.order_type<>1 and a.supplierNo in ('" . implode('\',\'', $person_supplier) . "'))";
  110. }
  111. }
  112. if ($this->level == 3 && $param['IsByMe']==1) {
  113. //是否是仓库管理员
  114. $tmp = Db::name('warehouse_info')->field('id')->where(['is_del' => 0, 'contactor' => $this->uid])->findOrEmpty();
  115. //库管看到所有的库存品发货申请单
  116. if (!empty($tmp)) $condition .= " or (a.order_type=1 AND c.contactor=" . $this->uid.")";
  117. $condition .= " or (a.order_type <>1 )";
  118. }
  119. $count = Db::name('order_out_child')
  120. ->alias('a')
  121. ->leftJoin('warehouse_info c', 'c.wsm_code=a.wsm_code AND c.is_del=0')
  122. ->leftJoin('sale d', 'd.orderCode=a.orderCode AND d.is_del=0')
  123. ->leftJoin('platform p', 'p.id=d.platform_id AND p.is_del=0')
  124. ->where($where)
  125. ->where($condition)
  126. ->count('a.id');
  127. $list = Db::name('order_out_child')
  128. ->alias('a')
  129. ->field('a.*,c.supplierNo wsm_supplierNo,c.supplierName wsm_supplierName,c.name wsm_name,c.contactor_name,p.use_type')
  130. ->leftJoin('warehouse_info c', 'c.wsm_code=a.wsm_code AND c.is_del=0')
  131. ->leftJoin('sale d', 'd.orderCode=a.orderCode AND d.is_del=0')
  132. ->leftJoin('platform p', 'p.id=d.platform_id AND p.is_del=0')
  133. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  134. ->page($param['page'], $param['size'])
  135. ->where($where)
  136. ->where($condition)
  137. ->select()
  138. ->toArray();
  139. // $all_apply_id = array_column($list, 'apply_id');
  140. // $company_name = get_company_name_by_uid($all_apply_id);
  141. //校验是否开通了供应商账号
  142. $supp_account = checkHasAccountBySupplierNos(array_unique(array_column($list, 'supplierNo')));
  143. $data = [];
  144. foreach ($list as $value) {
  145. // $value['company_name'] = $company_name[$value['apply_id']] ?? '';
  146. $value['has_account'] = (int)isset($supp_account[$value['supplierNo']]);
  147. $data[] = $value;
  148. }
  149. return json_show(0, '获取成功', ['count' => $count, 'list' => $data]);
  150. }
  151. //导出
  152. public function export()
  153. {
  154. $param = $this->request->only([
  155. 'start' => '',
  156. 'end' => '',
  157. 'orderCode' => '',
  158. 'customer_code' => '',
  159. 'status' => '',
  160. 'outChildCode' => '',
  161. 'companyNo' => '',
  162. 'spuCode' => '',
  163. 'outCode' => '',
  164. 'order_source' => '',
  165. 'wsm_code' => '',
  166. 'supplierNo' => '',
  167. 'skuCode' => '',
  168. 'apply_id' => '',
  169. 'apply_name' => '',
  170. 'order_type' => '',
  171. 'relaComNo' => '',
  172. 'use_type' => '',
  173. 'IsByMe' => '0',
  174. ], 'post', 'trim');
  175. $where = [['a.is_del', '=', 0]];
  176. if ($param['start'] != '') $where[] = ['a.addtime', '>=', $param['start']];
  177. if ($param['end'] != '') $where[] = ['a.addtime', '<', $param['end'] . ' 23:59:59'];
  178. if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
  179. if ($param['customer_code'] != '') $where[] = ['a.customer_code', 'like', '%' . $param['customer_code'] . '%'];
  180. if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
  181. if ($param['outChildCode'] != '') $where[] = ['a.outChildCode', 'like', '%' . $param['outChildCode'] . '%'];
  182. if ($param['companyNo'] != '') $where[] = ['a.companyNo', 'like', '%' . $param['companyNo'] . '%'];
  183. if ($param['spuCode'] != '') $where[] = ['a.spuCode', 'like', '%' . $param['spuCode'] . '%'];
  184. if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
  185. if ($param['order_source'] != '') $where[] = ['a.order_source', '=', $param['order_source']];
  186. if ($param['wsm_code'] != '') $where[] = ['a.wsm_code', 'like', '%' . $param['wsm_code'] . '%'];
  187. if ($param['supplierNo'] != '') $where[] = ['a.supplierNo', 'like', '%' . $param['supplierNo'] . '%'];
  188. if ($param['skuCode'] != '') $where[] = ['a.skuCode', 'like', '%' . $param['skuCode'] . '%'];
  189. if ($param['apply_id'] !== '') $where[] = ['a.apply_id', '=', $param['apply_id']];
  190. if ($param['apply_name'] != '') $where[] = ['a.apply_name', 'like', '%' . $param['apply_name'] . '%'];
  191. if ($param['order_type'] != '') $where[] = ['a.order_type', '=', $param['order_type']];
  192. if ($param['relaComNo'] != '') $where[] = ['a.companyNo', '=', $param['relaComNo']];
  193. if ($param['use_type'] !== '') $where[] = ['p.use_type', '=', $param['use_type']];
  194. $condition = '1=1';
  195. if($param['IsByMe']==1)$condition = '1=0';
  196. //只有level2的账号过滤数据权限
  197. if ($this->level == 2) {
  198. //库管只能看到库存品订单,供应商负责人只能看到非库存品订单
  199. //是否是仓库管理员
  200. $tmp = Db::name('warehouse_info')->field('id')->where(['is_del' => 0, 'contactor' => $this->uid])->findOrEmpty();
  201. //库管看到所有的库存品发货申请单
  202. if (!empty($tmp)) $condition .= " or (a.order_type=1 AND c.contactor=" . $this->uid.")";
  203. else {
  204. if($param['IsByMe']==0){
  205. $role = $this->checkDataShare();
  206. if (!empty($role[DataGroupModel::$type_全部])) {
  207. // if ($condition != '') $condition .= " or ";
  208. $condition .= " or (a.apply_id in (" . implode(',', $role[DataGroupModel::$type_全部]) . "))";
  209. }
  210. }
  211. }
  212. $person_supplier = Db::connect('mysql_sys')
  213. ->name('supplier')
  214. ->where(['is_del' => 0, 'personid' => $this->uid])
  215. ->column('code');
  216. if ($person_supplier) {
  217. if($param['IsByMe']==1){
  218. $suppe = checkHasAccountBySupplierNos($person_supplier);
  219. $suppAcco =array_diff($person_supplier,array_keys($suppe));
  220. $person_supplier= $suppAcco;
  221. }
  222. $condition .= " or (a.order_type<>1 and a.supplierNo in ('" . implode('\',\'', $person_supplier) . "'))";
  223. }
  224. }
  225. if ($this->level == 3 && $param['IsByMe']==1) {
  226. //是否是仓库管理员
  227. $tmp = Db::name('warehouse_info')->field('id')->where(['is_del' => 0, 'contactor' => $this->uid])->findOrEmpty();
  228. //库管看到所有的库存品发货申请单
  229. if (!empty($tmp)) $condition .= " or (a.order_type=1 AND c.contactor=" . $this->uid.")";
  230. }
  231. $list = Db::name('order_out_child')
  232. ->alias('a')
  233. ->field('a.outChildCode 发货工单号,a.outCode 发货单编号,a.orderCode 销售订单编号,"" 供应商端,case a.status when 1 then "待发货" when 2 then "发货完成" when 3 then "已收货" when 4 then "已全部退货" end "状态" ,case a.order_type when 1 then "备库" when 2 then "非库存" when 3 then "咨询商品" when 4 then "报备商品" end "商品类型",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 "订单来源",a.num 总数量,a.wsm_code 仓库编号,c.name 仓库名称,a.spuCode 商品成本编码,a.skuCode 商品上线编码,a.companyNo 业务公司编号,a.companyName 业务公司名称,a.customer_code 客户编号,a.customer_name 客户名称,a.supplierNo 供应商编号,a.supplierName 供应商名称,a.addtime 下单时间,a.apply_name 申请人,case p.use_type when 0 then "无" when 1 then "to B" when 2 then "to C" end 对接类型,a.post_name 物流公司,a.post_code 物流单号,a.post_fee 物流费用')
  234. ->leftJoin('warehouse_info c', 'c.wsm_code=a.wsm_code AND c.is_del=0')
  235. ->leftJoin('sale d', 'd.orderCode=a.orderCode AND d.is_del=0')
  236. ->leftJoin('platform p', 'p.id=d.platform_id AND p.is_del=0')
  237. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  238. ->where($where)
  239. ->where($condition)
  240. ->select()
  241. ->toArray();
  242. //校验是否开通了供应商账号
  243. $supp_account = checkHasAccountBySupplierNos(array_unique(array_column($list, '供应商编号')));
  244. // $all_apply_id = array_column($list, 'apply_id');
  245. // $company_name = get_company_name_by_uid($all_apply_id);
  246. // $data = [];
  247. foreach ($list as &$value) {
  248. $value['供应商端'] = isset($supp_account[$value['供应商编号']]) ? '已开通' : '未开通';
  249. // $value['company_name'] = $company_name[$value['apply_id']] ?? '';
  250. // unset($value['apply_id']);
  251. }
  252. if (empty($list)) $list[] = ['没有相关可导出的数据'];
  253. excelSave('发货工单' . date('YmdHis'), array_keys($list[0]), $list);
  254. // return json_show(0, '获取成功', $data);
  255. }
  256. //拆单
  257. public function add()
  258. {
  259. $param = $this->request->only(['outCode', 'list'], 'post', 'trim');
  260. $val = Validate::rule([
  261. 'outCode|发货单号' => 'require',
  262. 'list' => 'require|array|max:100',
  263. ]);
  264. // $list=[
  265. // ['wsm_code'=>'','num'=>100],
  266. // ['wsm_code'=>'','num'=>100],
  267. // ];
  268. if ($val->check($param) == false) return json_show(1004, $val->getError());
  269. $val_child = Validate::rule([
  270. 'wsm_code|仓库编码' => 'require',
  271. 'num|发货数量' => 'require|number|gt:0|elt:999999999999',
  272. ]);
  273. $info = Db::name('order_out')
  274. ->alias('a')
  275. ->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')
  276. ->leftJoin('sale b', 'b.orderCode=a.orderCode AND b.is_del=0')
  277. ->where(['a.is_del' => 0, 'a.outCode' => $param['outCode'], 'a.send_status' => 1])
  278. ->findOrEmpty();
  279. if (empty($info)) return json_show(1004, '该发货单不存在或状态有误');
  280. if ($info['wsend_num'] < $info['send_num']) return json_show(1004, "订单待发货数量不足");
  281. if ($info['wsend_num'] < array_sum(array_column($param['list'], 'num'))) return json_show(1004, "仓库总发货数与发货单待发货数不同");
  282. //所有仓库信息
  283. $wsm = Db::name('good_stock')
  284. ->where(['is_del' => 0, 'spuCode' => $info['good_code'], 'wsm_code' => array_column($param['list'], 'wsm_code')])
  285. ->column('id', 'wsm_code');
  286. Db::startTrans();
  287. try {
  288. $date = date('Y-m-d H:i:s');
  289. $insert = [];
  290. $i = 0;
  291. foreach ($param['list'] as $value) {
  292. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  293. //改变编码规则,将原编码后两位换成序列号
  294. //str_pad字符串填充
  295. $outChildCode = substr(makeNo('TCD'), 0, -2) . str_pad($i++, 2, '0', STR_PAD_LEFT);
  296. $insert[] = [
  297. 'outChildCode' => $outChildCode,
  298. 'orderCode' => $info['orderCode'],
  299. 'outCode' => $param['outCode'],
  300. 'companyNo' => $info['companyNo'],
  301. 'companyName' => $info['companyName'],
  302. 'customer_code' => $info['customer_code'],
  303. 'customer_name' => $info['customerName'],
  304. 'supplierNo' => $info['supplierNo'],
  305. 'supplierName' => $info['supplierName'],
  306. 'spuCode' => $info['good_code'],
  307. 'skuCode' => $info['skuCode'],
  308. 'good_name' => $info['good_name'],
  309. 'order_type' => $info['order_type'],
  310. 'order_source' => $info['order_source'],
  311. 'num' => $value['num'],
  312. 'wsm_code' => $value['wsm_code'],
  313. 'apply_id' => $info['apply_id'],
  314. 'apply_name' => $info['apply_name'],
  315. 'addrid' => $info['addrid'],
  316. 'status' => 1,
  317. 'is_del' => 0,
  318. 'addtime' => $date,
  319. 'updatetime' => $date,
  320. ];
  321. if (!isset($wsm[$value['wsm_code']])) throw new Exception($value['wsm_code'] . '该仓库不存在');
  322. }
  323. Db::name('order_out_child')->insertAll($insert);
  324. foreach ($insert as $item) {
  325. GoodStockInfo::ChildAddBn($item['outChildCode'], $wsm[$value['wsm_code']]);//维护bn号
  326. }
  327. Db::name('order_out')
  328. ->where(['id' => $info['id'], 'is_del' => 0, 'outCode' => $param['outCode'], 'send_status' => 1])
  329. ->update(['send_status' => 2, 'status' => 1]);
  330. Db::commit();
  331. return json_show(0, '分单完成');
  332. } catch (Exception $exception) {
  333. Db::rollback();
  334. return json_show(1004, '分单失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  335. }
  336. }
  337. //详情
  338. public function info()
  339. {
  340. $outChildCode = $this->request->post('outChildCode', '', 'trim');
  341. if ($outChildCode == '') return json_show(1004, '发货工单号不能为空');
  342. $info = Db::name('order_out_child')
  343. ->alias('a')
  344. ->field('a.*,b.name wsm_name,b.contactor,b.contactor_name')
  345. ->leftJoin('warehouse_info b', 'b.wsm_code=a.wsm_code')
  346. ->where(['a.is_del' => 0, 'a.outChildCode' => $outChildCode])
  347. ->findOrEmpty();
  348. //校验是否开通了供应商账号
  349. $supp_account = checkHasAccountBySupplierNos([$info['supplierNo']]);
  350. $info['has_account'] = (int)isset($supp_account[$info['supplierNo']]);
  351. return json_show(0, '获取详情成功', $info);
  352. }
  353. //发货
  354. public function send()
  355. {
  356. if ($this->level == 1) return json_show(0, '管理员不能操作');
  357. $param = $this->request->post('list/a', '', 'trim');
  358. $temp = Db::name('order_out_child')
  359. ->field('id,outChildCode')
  360. ->where(['is_del' => 0, 'outChildCode' => array_column($param, 'outChildCode')])
  361. ->where('status', '<>', 1)
  362. ->findOrEmpty();
  363. if (!empty($temp)) return json_show(1004, $temp['outChildCode'] . '状态错误,不能发货');
  364. $child = Db::name('order_out_child')
  365. ->where(['is_del' => 0, 'status' => 1, 'outChildCode' => array_column($param, 'outChildCode')])
  366. ->column('id,orderCode,outCode,num,wsm_code,supplierNo,apply_id', 'outChildCode');
  367. //检查所有的发货单是否有正在进行的售后单
  368. $temp = Db::name('order_return')
  369. ->where(['is_del' => 0, 'outCode' => array_unique(array_column($child, 'outCode'))])
  370. ->whereNotIn('status', [5, 6, 8])
  371. ->field('id,outCode')
  372. ->findOrEmpty();
  373. if (!empty($temp)) return json_show(1004, $temp['outCode'] . '有正在进行中的售后单,无法发货');
  374. $order_out = Db::name('order_out')
  375. ->where(['is_del' => 0, 'outCode' => array_unique(array_column($child, 'outCode'))])
  376. ->column('id,send_num,send_status,0 already_send_num,post_name,post_code,post_fee,status,apply_id', 'outCode');
  377. $sale = Db::name('sale')
  378. ->where(['is_del' => 0, 'orderCode' => array_unique(array_column($child, 'orderCode'))])
  379. ->column('id,good_num,send_num,wsend_num,good_code,supNo,is_stock,order_source,cgderid,status', 'orderCode');
  380. $is_reurn = Db::name("sale_return")
  381. ->field('id,orderCode')
  382. ->where(['orderCode' => array_column($child, 'orderCode'), 'is_del' => 0])
  383. ->where("status", "in", [1, 2, 3, 7, 8, 9, 10, 11, 12])
  384. ->findOrEmpty();
  385. if (!empty($is_reurn)) return json_show(1004, $is_reurn['orderCode'] . "存在退货未处理完成");
  386. //判断供应商是否开通账号,若开通的话,则只能由供应商账号操作
  387. // if ($this->level != 3) {
  388. // $temp = checkHasAccountBySupplierNos(array_column($sale, 'supNo'));
  389. // if (!empty($temp)) return json_show(1004, implode(',', array_keys($temp)) . '这些供应商已经开通账号,请使用供应商账号操作');
  390. // }
  391. $val = Validate::rule([
  392. 'outChildCode|发货工单号' => 'require|max:255',
  393. 'post_name|物流公司' => 'require|max:255',
  394. 'post_code|物流单号' => 'require|alphaDash|max:255',
  395. 'post_fee|物流费用' => 'require|egt:0|max:99999999.99'
  396. ]);
  397. Db::startTrans();
  398. try {
  399. $date = date('Y-m-d H:i:s');
  400. //所有供应商负责人
  401. $person = get_personid_by_supplierNo(array_unique(array_column($child, 'supplierNo')));
  402. // $userCommon = \app\admin\common\User::getIns();
  403. // $person_temp = $userCommon->handle('sGetList', ['more_code' => array_unique(array_column($child, 'supplierNo')), 'size' => count(array_column($child, 'supplierNo'))]);
  404. // $person = array_column($person_temp['data']['list'], 'personid', 'code');
  405. // unset($userCommon);
  406. // unset($person_temp);
  407. //所有仓库管理员
  408. $wsm_contactor = Db::name('warehouse_info')
  409. ->where(['is_del' => 0, 'wsm_code' => array_unique(array_column($child, 'wsm_code'))])
  410. ->column('contactor', 'wsm_code');
  411. $yz_tmp = [];
  412. foreach ($param as $value) {
  413. if ($val->check($value) == false) throw new Exception($val->getError());
  414. //如果是库存品,只能由库管操作
  415. //其他商品,若level2只能由供应商负责人操作,level3不限制
  416. if ($sale[$child[$value['outChildCode']]['orderCode']]['is_stock'] == 1) {
  417. if ($this->uid != $wsm_contactor[$child[$value['outChildCode']]['wsm_code']]) throw new Exception('库存品只能由仓库管理员操作');
  418. } else {
  419. if (($this->level == 2) && ($this->uid != $person[$child[$value['outChildCode']]['supplierNo']])) throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  420. }
  421. // if ($this->level == 2) {
  422. // if ($sale[$child[$value['outChildCode']]['orderCode']]['is_stock'] == 1) {
  423. // if ($this->uid != $wsm_contactor[$child[$value['outChildCode']]['wsm_code']]) throw new Exception('库存品只能由仓库管理员操作');
  424. // } else {
  425. // if ($this->uid != $person[$child[$value['outChildCode']]['supplierNo']]) throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  426. // }
  427. // }
  428. // if (($this->level) == 3 && ($sale[$child[$value['outChildCode']]['orderCode']]['is_stock'] == 1) && ($this->uid != $wsm_contactor[$child[$value['outChildCode']]['wsm_code']])) throw new Exception('库存品只能由仓库管理员操作');
  429. //工单
  430. if (!isset($child[$value['outChildCode']])) throw new Exception($value['outChildCode'] . '工单不存在或状态不允许发货');
  431. Db::name('order_out_child')
  432. ->where(['id' => $child[$value['outChildCode']]['id'], 'is_del' => 0, 'status' => 1])
  433. ->update([
  434. 'post_name' => $value['post_name'],
  435. 'post_code' => $value['post_code'],
  436. 'post_fee' => $value['post_fee'],
  437. 'status' => 2,
  438. 'sendtime' => $date,
  439. 'updatetime' => $date,
  440. ]);
  441. //修改状态,添加待办
  442. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  443. "order_code" => $value['outChildCode'],//单号
  444. "status" => 1,//这里的status是之前的值
  445. "action_remark" => '',//备注
  446. "action_type" => "status"//新建create,编辑edit,更改状态status
  447. ], "FHGD", 2, ['post_name' => $value['post_name'],
  448. 'post_code' => $value['post_code'],
  449. 'post_fee' => $value['post_fee'],
  450. 'status' => 2,
  451. 'sendtime' => $date,
  452. 'updatetime' => $date]);
  453. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  454. "order_type" => 'FHGD',
  455. "order_code" => $value['outChildCode'],//单号
  456. "order_id" => $child[$value['outChildCode']]['id'],
  457. "order_status" => 2,
  458. "before_status" => 1,
  459. 'holder_id' => $child[$value['outChildCode']]['apply_id']
  460. ]);
  461. $order_out[$child[$value['outChildCode']]['outCode']]['already_send_num'] += $child[$value['outChildCode']]['num'];
  462. $order_out[$child[$value['outChildCode']]['outCode']]['already_send_num'] += $child[$value['outChildCode']]['num'];
  463. $order_out[$child[$value['outChildCode']]['outCode']]['already_send_num'] += $child[$value['outChildCode']]['num'];
  464. if($order_out[$child[$value['outChildCode']]['outCode']]['post_name']==''||$order_out[$child[$value['outChildCode']]['outCode']]['post_code']=='' ){
  465. $order_out[$child[$value['outChildCode']]['outCode']]['post_name']= $value['post_name'];
  466. $order_out[$child[$value['outChildCode']]['outCode']]['post_code']= $value['post_code'];
  467. $order_out[$child[$value['outChildCode']]['outCode']]['post_fee']= $value['post_fee'];
  468. }
  469. //发货单
  470. //send_status 3部分发货,4全部发货
  471. Db::name('order_out')
  472. ->where(['id' => $order_out[$child[$value['outChildCode']]['outCode']]['id']])
  473. ->update([
  474. 'send_status' => $order_out[$child[$value['outChildCode']]['outCode']]['already_send_num'] >= $order_out[$child[$value['outChildCode']]['outCode']]['send_num'] ? 4 : 3,
  475. 'status' => 2,
  476. 'post_name' => $order_out[$child[$value['outChildCode']]['outCode']]['post_name'],
  477. 'post_code' => $order_out[$child[$value['outChildCode']]['outCode']]['post_code'],
  478. 'post_fee' => $order_out[$child[$value['outChildCode']]['outCode']]['post_fee'],
  479. 'updatetime' => $date
  480. ]);
  481. $sale[$child[$value['outChildCode']]['orderCode']]['send_num'] += $child[$value['outChildCode']]['num'];
  482. $sale[$child[$value['outChildCode']]['orderCode']]['wsend_num'] -= $child[$value['outChildCode']]['num'];
  483. //修改状态,添加待办
  484. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  485. "order_code" => $child[$value['outChildCode']]['outCode'],//单号
  486. "status" => $order_out[$child[$value['outChildCode']]['outCode']]['status'],//这里的status是之前的值
  487. "action_remark" => '',//备注
  488. "action_type" => "status"//新建create,编辑edit,更改状态status
  489. ], "CKD", 2, ['send_status' => $order_out[$child[$value['outChildCode']]['outCode']]['already_send_num'] >= $order_out[$child[$value['outChildCode']]['outCode']]['send_num'] ? 4 : 3, 'status' => 2, 'updatetime' => $date]);
  490. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  491. "order_type" => 'CKD',
  492. "order_code" => $child[$value['outChildCode']]['outCode'],//单号
  493. "order_id" => $order_out[$child[$value['outChildCode']]['outCode']]['id'],
  494. "order_status" => 2,
  495. "before_status" => $order_out[$child[$value['outChildCode']]['outCode']]['status'],
  496. 'holder_id' => $order_out[$child[$value['outChildCode']]['outCode']]['apply_id']
  497. ]);
  498. //销售单
  499. $send_status = $sale[$child[$value['outChildCode']]['orderCode']]['send_num'] >= $sale[$child[$value['outChildCode']]['orderCode']]['good_num'] ? 4 : 3;
  500. $sale_update_data = [
  501. 'send_num' => $sale[$child[$value['outChildCode']]['orderCode']]['send_num'],
  502. 'wsend_num' => $sale[$child[$value['outChildCode']]['orderCode']]['wsend_num'],
  503. 'send_status' => $send_status,
  504. 'updatetime' => $date
  505. ];
  506. if ($sale_update_data['wsend_num'] == 0) $sale_update_data['status'] = 2;
  507. elseif ($sale_update_data['send_num'] == 0) $sale_update_data['status'] = 0;
  508. else $sale_update_data['status'] = 1;
  509. Db::name('sale')
  510. ->where(['is_del' => 0, 'id' => $sale[$child[$value['outChildCode']]['orderCode']]['id']])
  511. ->update($sale_update_data);
  512. if ($sale[$child[$value['outChildCode']]['orderCode']]['order_source'] == 5 && $send_status == 4) {
  513. $yz_tmp[] = [
  514. 'post_name' => $value['post_name'],
  515. 'post_code' => $value['post_code'],
  516. 'outCode' => $child[$value['outChildCode']]['outCode'],
  517. 'orderCode' => $child[$value['outChildCode']]['orderCode'],
  518. ];
  519. }
  520. //修改状态,添加待办
  521. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  522. "order_code" => $child[$value['outChildCode']]['orderCode'],//单号
  523. "status" => $sale[$child[$value['outChildCode']]['orderCode']]['status'],//这里的status是之前的值
  524. "action_remark" => '',//备注
  525. "action_type" => "status"//新建create,编辑edit,更改状态status
  526. ], "XSQRD", $sale_update_data['status'], $sale_update_data);
  527. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  528. "order_type" => 'XSQRD',
  529. "order_code" => $child[$value['outChildCode']]['orderCode'],//单号
  530. "order_id" => $sale[$child[$value['outChildCode']]['orderCode']]['id'],
  531. "order_status" => $sale_update_data['status'],
  532. "before_status" => $sale[$child[$value['outChildCode']]['orderCode']]['status'],
  533. 'holder_id' => $sale[$child[$value['outChildCode']]['orderCode']]['cgderid']
  534. ]);
  535. //库存
  536. Db::name('good_stock')
  537. ->data(['updatetime' => $date])
  538. ->where(['is_del' => 0, 'spuCode' => $sale[$child[$value['outChildCode']]['orderCode']]['good_code'], 'wsm_code' => $child[$value['outChildCode']]['wsm_code']])
  539. ->dec('wait_out_stock', $child[$value['outChildCode']]['num'])
  540. ->update();
  541. }
  542. //日志、待办已办……
  543. Db::commit();
  544. //如果是有赞订单 且 所属发货单全部发货了,将发货信息推到有赞
  545. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  546. foreach ($yz_tmp as $temp) {
  547. 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']]);
  548. }
  549. // if ($einfo['order_source'] == 5) {
  550. // $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]);
  551. // $res = json_decode($res, true);
  552. // if ($res['code'] != 0) return app_show(0, '发货成功,' . $res['message']);
  553. // }
  554. return json_show(0, '操作完成');
  555. } catch (Exception $exception) {
  556. Db::rollback();
  557. return json_show(1004, $exception->getMessage());
  558. }
  559. }
  560. //根据发货单号查询发货工单(不控制权限,不分页,无token,该方法加入白名单)
  561. public function getListByOutCode()
  562. {
  563. $param = $this->request->only(['outCode' => '', 'orderCode' => ''], 'post', 'trim');
  564. $val = Validate::rule([
  565. 'outCode' => 'requireWithout:orderCode',
  566. 'orderCode' => 'requireWithout:outCode',
  567. ]);
  568. if ($val->check($param) == false) return json_show(1004, $val->getError());
  569. $where = [['a.is_del', '=', 0]];
  570. if ($param['outCode'] !== '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
  571. if ($param['orderCode'] !== '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
  572. $data = Db::name('order_out_child')
  573. ->alias('a')
  574. ->field('a.*,c.contactor_name,c.name wsm_name')
  575. ->leftJoin('warehouse_info c', 'c.wsm_code=a.wsm_code AND c.is_del=0')
  576. ->where($where)
  577. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  578. ->select()
  579. ->toArray();
  580. return json_show(0, '获取列表成功', $data);
  581. }
  582. //根据发货单号获取发货工单列表
  583. public function getList()
  584. {
  585. $param = $this->request->only(['outCode'], 'post', 'trim');
  586. $val = Validate::rule(['outCode' => 'require']);
  587. if ($val->check($param) == false) return json_show(1004, $val->getError());
  588. $where = ['a.is_del' => 0, 'outCode' => $param['outCode']];
  589. $list = Db::name('order_out_child')
  590. ->alias('a')
  591. ->field('a.*,c.supplierNo wsm_supplierNo,c.supplierName wsm_supplierName,c.name wsm_name,c.contactor_name')
  592. ->leftJoin('warehouse_info c', 'c.wsm_code=a.wsm_code AND c.is_del=0')
  593. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  594. ->where($where)
  595. ->select()
  596. ->toArray();
  597. // $all_apply_id = array_column($list, 'apply_id');
  598. // $company_name = get_company_name_by_uid($all_apply_id);
  599. // //校验是否开通了供应商账号
  600. // $supp_account = checkHasAccountBySupplierNos(array_unique(array_column($list, 'supplierNo')));
  601. //
  602. // $data = [];
  603. // foreach ($list as $value) {
  604. // $value['company_name'] = $company_name[$value['apply_id']] ?? '';
  605. // $value['has_account'] = (int)isset($supp_account[$value['supplierNo']]);
  606. // $data[] = $value;
  607. // }
  608. return json_show(0, '获取成功', $list);
  609. }
  610. }