OrderOutChild.php 43 KB

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