OrderOutChild.php 43 KB

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