OrderOutChild.php 33 KB

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