Sale.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. namespace app\abutment\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\GoodLog;
  5. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  6. use Exception;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. use think\facade\Validate;
  10. class Sale extends HomeBaseController
  11. {
  12. //发货单列表
  13. public function saleout()
  14. {
  15. $this->post = $this->request->filter('trim')->post();
  16. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  17. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  18. $where = [["a.is_del", "=", 0], ['wpo.supplierNo', '=', $this->request->user['supplierNo']], ['b.is_stock', '=', '0']];
  19. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  20. if ($orderCode !== "") {
  21. $where[] = ['a.orderCode', "like", "%$orderCode%"];
  22. }
  23. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? trim($this->post['order_type']) : "";
  24. if ($order_type !== "") {
  25. $where[] = ['a.order_type', "=", $order_type];
  26. }
  27. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  28. if ($outCode !== "") {
  29. $where[] = ['a.outCode', "like", "%$outCode%"];
  30. }
  31. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : "";
  32. if ($apply_name !== "") {
  33. $where[] = ['a.apply_name', "like", "%$apply_name%"];
  34. }
  35. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  36. if ($good_code !== "") {
  37. $where[] = ['b.good_code', "like", "%$good_code%"];
  38. }
  39. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  40. if ($good_name !== "") {
  41. $where[] = ['b.good_name', "like", "%$good_name%"];
  42. }
  43. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  44. if ($status !== "") {
  45. $where[] = ['a.status', "=", $status];
  46. }
  47. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  48. if ($start != "") {
  49. $where[] = ["a.addtime", '>=', $start];
  50. }
  51. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  52. if ($end != "") {
  53. $where[] = ["a.addtime", '<=', $end];
  54. }
  55. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  56. if ($companyNo != "") {
  57. $where[] = ['b.customer_code', "like", "%$companyNo%"];
  58. }
  59. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] !== "" ? $this->post['cgdNo'] : "";
  60. if ($cgdNo != "") {
  61. $where[] = ["os.cgdNo", 'like', '%' . $cgdNo . '%'];
  62. }
  63. // $role = $this->checkRole();
  64. // $condition = '';
  65. // if (!empty($role['write']) && $this->uid != "") {
  66. // // $where[]=["a.apply_id","in",$role['write']];
  67. // $condition .= " (b.is_stock=1 and n.contactor = {$this->uid}) or (b.is_stock=0 and wpo.cgder_id = {$this->uid}) or a.apply_id in (" . implode(',',
  68. // $role['write']) . ")";
  69. // }
  70. // if(!empty($role['platform']) ){
  71. // $where[]=["b.platform_id","in",$role['platform']];
  72. // }
  73. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  74. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  75. //发货时间筛选
  76. $start_sendtime = isset($this->post['start_sendtime']) && $this->post['start_sendtime'] !== "" ? $this->post['start_sendtime'] : "";
  77. $end_sendtime = isset($this->post['end_sendtime']) && $this->post['end_sendtime'] !== "" ? $this->post['end_sendtime'] : "";
  78. if ($start_sendtime != "" && $end_sendtime != "") {
  79. $where[] = ["a.sendtime", 'between', [$start_sendtime . " 00:00:00", $end_sendtime . " 223:59:59"]];
  80. $where[] = ["a.status", '>=', 2];//搜索发货时间时,要指定状态为已发货及之后的状态值(0待发货,1待库管发货,2已发货待收货,3已收货,4已全部退货',)
  81. }
  82. $count = Db::name('order_out')
  83. ->alias('a')
  84. ->join("sale b", "b.orderCode=a.orderCode", "left")
  85. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  86. ->join("warehouse_info n", "n.wsm_code=a.wsm_code", "left")
  87. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  88. ->leftJoin("order_send os", "os.outCode=a.outCode")
  89. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  90. ->where($where)
  91. // ->where($condition)
  92. ->count();
  93. $total = ceil($count / $size);
  94. $page = $page >= $total ? $total : $page;
  95. $list = Db::name('order_out')
  96. ->alias('a')
  97. ->join("sale b", "b.orderCode=a.orderCode", "left")
  98. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  99. ->join("warehouse_info n", "n.wsm_code=a.wsm_code AND n.is_del=0", "left")
  100. ->leftJoin("order_send os", "os.outCode=a.outCode")
  101. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  102. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  103. ->field("a.*,b.order_type,b.order_source,b.good_name,b.good_code,b.skuCode,b.supplierNo,b.customer_code,v.companyName,b.origin_price,b.sale_price,b.total_price,os.cgdNo,u.itemid")
  104. ->where($where)
  105. // ->where($condition)
  106. ->order("addtime desc")
  107. ->page($page, $size)
  108. ->cursor();
  109. $data = [];
  110. foreach ($list as $value) {
  111. $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code")
  112. ->where(["k.wsm_code" => $value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  113. $addr = Db::name("order_addr")->where(["id" => $value['addrid']])->find();
  114. if ($addr) {
  115. $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : '';
  116. if (is_string($addinfo) && $addinfo != '') {
  117. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  118. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']);
  119. }
  120. $addr['addr_cn'] = GetAddr(json_encode($addinfo));
  121. }
  122. if ($value['order_type'] == 3) {
  123. $goon = Db::name("good_zixun")->field('id,cat_id')->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  124. } else {
  125. $goon = Db::name('good_platform')->field('a.id,b.cat_id')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')->where(['a.skuCode' => $value['skuCode']])->find();
  126. }
  127. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  128. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  129. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  130. $value['addr'] = isset($addr['addr']) ? $addr['addr_cn'] . $addr['addr'] : "";
  131. $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  132. $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  133. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  134. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  135. $value['sendtime'] = $value['status'] < 2 ? '' : $value['sendtime'];
  136. $data[] = $value;
  137. }
  138. return json_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  139. }
  140. //发货单详情
  141. public function saleOutInfo()
  142. {
  143. $this->post = $this->request->filter('trim')->post();
  144. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  145. if ($outCode == "") {
  146. return json_show(1002, "参数outcode不能为空");
  147. }
  148. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find();
  149. if (empty($codeinfo)) {
  150. return json_show(1002, "未找到出库数据");
  151. }
  152. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  153. if ($item['order_type'] == 3) {
  154. $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find();
  155. if ($goodinfo == false) {
  156. return json_show(1004, "未找到商品数据");
  157. }
  158. } else {
  159. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  160. ->where(['a.skuCode' => $item['skuCode']])->find();
  161. if ($goodinfo == false) {
  162. return json_show(1002, "未找到商品数据");
  163. }
  164. }
  165. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  166. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  167. if ($addr) {
  168. $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : '';
  169. if (is_string($addinfo) && $addinfo != '') {
  170. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  171. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']);
  172. }
  173. $addr['addr_cn'] = GetAddr(json_encode($addinfo));
  174. }
  175. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  176. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  177. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  178. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  179. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  180. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  181. $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : "";
  182. $codeinfo['addr'] = $addr['addr_cn'] ?? '';
  183. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  184. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  185. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  186. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  187. $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime'];
  188. $codeinfo['can'] = $int;
  189. $codeinfo['order_source'] = $item['order_source'];
  190. $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find();
  191. $codeinfo['order_return'] = $orderReturn;
  192. return json_show(0, "获取成功", $codeinfo);
  193. }
  194. //库管发货
  195. public function outSend()
  196. {
  197. $this->post = $this->request->filter('trim')->post();
  198. $outCode = isset($this->post['outCode']) && $this->post['outCode'] != "" ? trim($this->post['outCode']) : "";
  199. if ($outCode == "") {
  200. return json_show(1004, "参数outCode不能为空");
  201. }
  202. $outinfo = Db::name("order_out")->where(["outCode" => $outCode, "is_del" => 0])->find();
  203. if ($outinfo == false) {
  204. return json_show(1004, "发货数据未找到");
  205. }
  206. $einfo = Db::name('sale')->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->find();
  207. if (empty($einfo)) {
  208. return json_show(1002, "未找到销售订单数据");
  209. }
  210. $is_reurn = Db::name("sale_return")->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->where("status", "<", 4)->find();
  211. if ($is_reurn != false) {
  212. return json_show(1002, "销售单存在退货未处理完成");
  213. }
  214. $post_name = isset($this->post['post_name']) && $this->post['post_name'] != "" ? trim($this->post['post_name']) : "";
  215. if ($post_name == "") {
  216. return json_show(1004, "参数post_name不能为空");
  217. }
  218. $post_code = isset($this->post['post_code']) && $this->post['post_code'] != "" ? trim($this->post['post_code']) : "";
  219. if ($post_code == "") {
  220. return json_show(1004, "参数post_code不能为空");
  221. }
  222. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  223. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] != "" ? floatval($this->post['post_fee']) : "";
  224. $outinfo['post_name'] = $post_name;
  225. $outinfo['post_code'] = $post_code;
  226. $outinfo['post_fee'] = $post_fee;
  227. $outinfo['sendtime'] = date("Y-m-d H:i:s");
  228. $old_outinfo_status = $outinfo['status'];
  229. $outinfo['status'] = 2;
  230. $outinfo['updatetime'] = date("Y-m-d H:i:s");
  231. $outinfo['remark'] = $remark;
  232. if ($einfo['wsend_num'] < $outinfo['send_num']) {
  233. return json_show(1004, "订单未发货数量不足");
  234. }
  235. Db::startTrans();
  236. try {
  237. $up = Db::name("order_out")->save($outinfo);
  238. // $user_info = GetUserInfo($this->post['token']);
  239. $uid = $this->request->user['uid'];
  240. $uname = $this->request->user['nickname'];
  241. if ($up) {
  242. //修改状态,添加待办
  243. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  244. "order_code" => $outinfo['outCode'],//出库单号
  245. "status" => $old_outinfo_status,//这里的status是之前的值
  246. "action_remark" => '',//备注
  247. "action_type" => "edit"//新建create,编辑edit,更改状态status
  248. ], "CKD", $outinfo['status'], $outinfo);
  249. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  250. "order_type" => 'CKD',
  251. "order_code" => $outinfo['outCode'],//出库单号
  252. "order_id" => $outinfo['id'],
  253. "order_status" => $outinfo['status'],
  254. "before_status" => $old_outinfo_status,
  255. 'wait_id' => $outinfo['apply_id'],
  256. 'wait_name' => $outinfo['apply_name'],
  257. ]);
  258. $orderstatus = $einfo['status'];
  259. $einfo['send_num'] += $outinfo['send_num'];
  260. $einfo['wsend_num'] -= $outinfo['send_num'];
  261. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  262. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  263. $einfo['updatetime'] = date("Y-m-d H:i:s");
  264. $saleup = Db::name("sale")->save($einfo);
  265. if ($saleup == false) {
  266. Db::rollback();
  267. return json_show(1002, "销售单状态更新失败");
  268. }
  269. // }
  270. //修改状态,添加待办
  271. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  272. "order_code" => $einfo['orderCode'],//出库单号
  273. "status" => $orderstatus,//这里的status是之前的值
  274. "action_remark" => '',//备注
  275. "action_type" => "edit"//新建create,编辑edit,更改状态status
  276. ], "XSQRD", $einfo['status'], $einfo);
  277. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  278. "order_type" => 'XSQRD',
  279. "order_code" => $einfo['orderCode'],//出库单号
  280. "order_id" => $einfo['id'],
  281. "order_status" => $einfo['status'], "before_status" => $orderstatus
  282. ]);
  283. $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find();
  284. if ($stokc == false) {
  285. Db::rollback();
  286. return json_show(1002, "未找到库存数据");
  287. } else {
  288. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  289. Db::rollback();
  290. return json_show(1002, "超出库存数量");
  291. }
  292. $stokc['wait_out_stock'] -= $outinfo['send_num'];
  293. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  294. // $stokc['intra_stock']+=$outinfo['send_num'];
  295. $stokc['updatetime'] = date("Y-m-d H:i:s");
  296. }
  297. $stoc = Db::name("good_stock")->save($stokc);
  298. if ($stoc == false) {
  299. Db::rollback();
  300. return json_show(1002, "库存更新失败");
  301. }
  302. if($einfo["order_type"]!=1){
  303. $outsend =Db::name("order_send")->where(["outCode"=> $outCode])->findOrEmpty();
  304. if(empty($outsend)){
  305. Db::rollback();
  306. return error_show(1002, "未找到关联采购单");
  307. }
  308. $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  309. ->findOrEmpty();
  310. $bn_code =makeNo("BN");
  311. $bnin=GoodStockInfo::AddBn($stokc["id"],$bn_code,$outinfo['send_num'],$cgdinfo['good_price']??0);
  312. if($bnin==false){
  313. Db::rollback();
  314. return error_show(1004,"库存bn数据新增失败");
  315. }
  316. $bnup =GoodStockInfo::OrderBn($outinfo['orderCode'],$stokc["id"],$outinfo['send_num']);
  317. if($bnup==false){
  318. Db::rollback();
  319. return error_show(1004,"库存bn库存数更新失败");
  320. }
  321. }
  322. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stokc['id'], "type" => 2, 'stock' => $outinfo['send_num'], "stock_name" => "wait_out_stock"];
  323. GoodLog::LogAdd(['id' => $uid, 'nickname' => $uname], $good_data, "CKD");
  324. Cache::store("redis")->handler()->lPush("SENDOUT", $outCode);
  325. Db::commit();
  326. } else {
  327. Db::rollback();
  328. return json_show(1004, "发货失败");
  329. }
  330. //如果是有赞订单的话,将发货信息推到有赞
  331. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  332. if ($einfo['order_source'] == 5) {
  333. $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]);
  334. $res = json_decode($res, true);
  335. if ($res['code'] != 0) return json_show(0, '发货成功,' . $res['message']);
  336. }
  337. return json_show(0, "发货成功");
  338. } catch (\Exception $e) {
  339. Db::rollback();
  340. return json_show(1004, $e->getMessage());
  341. }
  342. }
  343. //库管批量发货
  344. public function outSendBatchByImport()
  345. {
  346. $param = $this->request->only(['list'], 'post', 'trim');
  347. if (empty($param['list'])) return json_show(1005, 'list参数不能为空');
  348. //只允许处理所属供应商下的采购单关联的发货单
  349. $temp = Db::name('order_out')
  350. ->alias('a')
  351. ->leftJoin('order_num b', 'b.orderCode=a.orderCode')
  352. ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo')
  353. ->where([
  354. ['a.is_del', '=', 0],
  355. ['a.outCode', 'in', array_column($param['list'], 'outCode')],
  356. ['c.supplierNo', '<>', $this->request->user['supplierNo']]
  357. ])->column('a.outCode');
  358. if (!empty($temp)) return json_show(1003, '不允许处理其他供应商的单子', $temp);
  359. // $user = GetUserInfo($param['token']);
  360. $createrid = $this->request->user['uid'];
  361. $creater = $this->request->user['nickname'];
  362. //所有发货单信息
  363. $order_out_infos = Db::name("order_out")
  364. ->where('is_del', 0)
  365. ->whereIn('outCode', array_column($param['list'], 'outCode'))
  366. ->whereIn('status', [0, 1])
  367. ->column('id,status,send_num,orderCode,wsm_code,apply_id,apply_name', 'outCode');
  368. //处理数据
  369. Db::startTrans();
  370. try {
  371. $good_data = [];
  372. $temp_out_codes = [];
  373. foreach ($param['list'] as $value) {
  374. if (!isset($order_out_infos[$value['outCode']])) throw new Exception($value['outCode'] . '该发货单未找到或状态不允许发货');
  375. //每次循环都需要重新查询对应数据,为了数据及时更新
  376. $sale_infos = Db::name("sale")
  377. ->where('orderCode', $order_out_infos[$value['outCode']]['orderCode'])
  378. ->where('is_del', 0)
  379. ->column('id,send_num,wsend_num,status,good_code,order_source', 'orderCode');
  380. if (!isset($sale_infos[$order_out_infos[$value['outCode']]['orderCode']])) throw new Exception($value['outCode'] . '该发货单对应的销售单未找到');
  381. //判断发货单是否重复
  382. if (isset($temp_out_codes[$value['outCode']])) {
  383. Db::rollback();
  384. return json_show(1005, '下列发货申请单号重复', [$value]);
  385. } else $temp_out_codes[$value['outCode']] = $value['outCode'];
  386. //【发货单】
  387. //查询已有发货单信息及状态
  388. //更新发货单
  389. $up = Db::name("order_out")
  390. ->where(['id' => $order_out_infos[$value['outCode']]['id'], 'outCode' => $value['outCode'], 'status' => 1, "is_del" => 0])
  391. ->update([
  392. 'post_name' => isset($value['post_name']) ? $value['post_name'] : '',
  393. 'post_code' => $value['post_code'],
  394. 'post_fee' => $value['post_fee'],
  395. 'sendtime' => date('Y-m-d H:i:s'),
  396. 'status' => 2,
  397. 'updatetime' => date('Y-m-d H:i:s'),
  398. 'remark' => isset($value['remark']) ? $value['remark'] : '',
  399. ]);
  400. if ($up) {
  401. //修改状态,添加待办
  402. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  403. "order_code" => $value['outCode'],//出库单号
  404. "status" => $order_out_infos[$value['outCode']]['status'],//这里的status是之前的值
  405. "action_remark" => '',//备注
  406. "action_type" => "edit"//新建create,编辑edit,更改状态status
  407. ], "CKD", 2, $order_out_infos[$value['outCode']]);
  408. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  409. "order_type" => 'CKD',
  410. "order_code" => $value['outCode'],//出库单号
  411. "order_id" => $order_out_infos[$value['outCode']]['id'],
  412. "order_status" => 2,
  413. "before_status" => $order_out_infos[$value['outCode']]['status'],
  414. 'wait_id' => $order_out_infos[$value['outCode']]['apply_id'],
  415. 'wait_name' => $order_out_infos[$value['outCode']]['apply_name'],
  416. ]);
  417. //【销售单】
  418. $einfo = [];
  419. $einfo['send_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['send_num'] + $order_out_infos[$value['outCode']]['send_num'];
  420. $einfo['wsend_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['wsend_num'] - $order_out_infos[$value['outCode']]['send_num'];
  421. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  422. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  423. $einfo['updatetime'] = date("Y-m-d H:i:s");
  424. $saleup = Db::name("sale")
  425. ->where('id', $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'])
  426. ->update($einfo);
  427. if ($saleup == false) throw new Exception($value['outCode'] . '销售单状态更新失败');
  428. //修改状态,添加待办
  429. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  430. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//销售单号
  431. "status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],//这里的status是之前的值
  432. "action_remark" => '',//备注
  433. "action_type" => "edit"//新建create,编辑edit,更改状态status
  434. ], "XSQRD", $einfo['status'], $einfo);
  435. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  436. "order_type" => 'XSQRD',
  437. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//出库单号
  438. "order_id" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'],
  439. "order_status" => $einfo['status'], "before_status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status']
  440. ]);
  441. //【库存】
  442. $stock = Db::name("good_stock")
  443. ->field('id,wait_out_stock,usable_stock,total_stock,updatetime')
  444. ->where([
  445. "is_del" => 0,
  446. 'spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'],
  447. "wsm_code" => $order_out_infos[$value['outCode']]['wsm_code'],
  448. ])->find();
  449. if ($stock == false) throw new Exception($value['outCode'] . '未找到库存数据');
  450. else {
  451. if ($order_out_infos[$value['outCode']]['send_num'] > $stock['wait_out_stock']) throw new Exception($value['outCode'] . '超出库存数量');
  452. $stock['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num'];
  453. $stock['total_stock'] = $stock['wait_out_stock'] + $stock['usable_stock'];
  454. $stock['updatetime'] = date("Y-m-d H:i:s");
  455. $stock_rs = Db::name("good_stock")
  456. ->where('id', $stock['id'])
  457. ->update($stock);
  458. if ($stock_rs == false) throw new Exception($value['outCode'] . '库存更新失败');
  459. if($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]["order_type"]!=1){
  460. $outsend =Db::name("order_send")->where(["outCode"=> $value['outCode']])->findOrEmpty();
  461. if(empty($outsend)){
  462. Db::rollback();
  463. return error_show(1002, "未找到关联采购单");
  464. }
  465. $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  466. ->findOrEmpty();
  467. $bn_code =makeNo("BN");
  468. $bnin=GoodStockInfo::AddBn($stock["id"],$bn_code,$order_out_infos[$value['outCode']]['send_num'],$cgdinfo['good_price']??0);
  469. if($bnin==false){
  470. Db::rollback();
  471. return error_show(1004,"库存bn数据新增失败");
  472. }
  473. $bnup =GoodStockInfo::OrderBn($order_out_infos[$value['outCode']]['orderCode'],$stock["id"],$order_out_infos[$value['outCode']]['send_num']);
  474. if($bnup==false){
  475. Db::rollback();
  476. return error_show(1004,"库存bn库存数更新失败");
  477. }
  478. }
  479. $good_data[] = [
  480. 'good_log_code' => $value['outCode'],
  481. 'stock_id' => $stock['id'],
  482. 'type' => 2,
  483. 'stock' => $order_out_infos[$value['outCode']]['send_num'],
  484. 'stock_name' => 'wait_out_stock'
  485. ];
  486. }
  487. } else throw new Exception($value['outCode'] . '发货失败');
  488. Cache::store("redis")->handler()->lPush("SENDOUT", $value['outCode']);
  489. }
  490. if (!empty($good_data)) GoodLog::LogAdd(['id' => $createrid, 'nickname' => $creater], $good_data, "CKD");
  491. Db::commit();
  492. return json_show(0, '发货成功');
  493. } catch (Exception $exception) {
  494. Db::rollback();
  495. return json_show(1004, $exception->getMessage());
  496. }
  497. }
  498. //咨询商品详情
  499. public function goodzxinfo()
  500. {
  501. $this->post = $this->request->filter('trim')->post();
  502. $orderCode = isset($this->post['spuCode']) && $this->post['spuCode'] != '' ? trim($this->post['spuCode']) : "";
  503. if ($orderCode == "") {
  504. return json_show(1003, "参数spuCode不能为空");
  505. }
  506. $order = Db::name("good_zixun")->where(["spuCode" => $orderCode, "is_del" => 0])->find();
  507. if (empty($order)) {
  508. return json_show(1003, "订单数据未找到");
  509. }
  510. $unit = Db::name("unit")->where(["id" => $order['good_unit']])->find();
  511. if ($order['brand_id'] != 0) {
  512. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  513. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  514. } else {
  515. $order["brand_name"] = "";
  516. $order["brand_id"] = "";
  517. }
  518. $order['specinfo'] = json_decode($order['specinfo'], true);
  519. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  520. $order['cat_info'] = made($order['cat_id'], []);
  521. $order['noble_name'] = isset($order['noble_metal']) && $order['noble_metal'] != 0 ? $this->noble[$order['noble_metal']] : "";
  522. if ($order['is_gold_price'] == 1) {
  523. $price = Db::name("gold_price1")->where(["type" => $order['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  524. }
  525. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  526. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  527. $palt = Db::name("platform")->where(["id" => $order['platform_id']])->find();
  528. $order['platform_name'] = isset($palt['platform_name']) ? $palt['platform_name'] : "";
  529. $company = Db::name("business")->where(["companyNo" => $order['companyNo']])->find();
  530. $order['company'] = isset($company['company']) ? $company['company'] : "";
  531. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  532. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  533. return json_show(0, "获取成功", $order);
  534. }
  535. //发货申请单导出
  536. public function exportSaleOut()
  537. {
  538. $outCodes = $this->request->filter('trim')->post('outCodes');
  539. if (empty($outCodes)) return json_show(1004, '要导出的发货申请单编号不能为空');
  540. $i = 1;
  541. $send_type = [1 => '直接发货', 2 => '延时发货'];
  542. $status = [0 => '待发货', 1 => '待库管发货', 2 => '已发货待收货', 3 => '已收货', 4 => '已全部退货'];
  543. $list = Db::name("order_out")
  544. ->alias('a')
  545. ->field('"" as 序号,a.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,b.orderCode as 确认单号,b.addtime as 确认单时间,b.good_code as 产品编号,b.good_name as 产品名称,"" as 规格,"" as 单位,wi.name as 供应商名称,s.code as 供应商编号,po.nake_fee as 裸价,a.post_fee as 物流费,b.send_type as 发货方式,po.good_price as 采购单价,b.remark as 确认单备注,po.good_num as 采购数量,po.total_fee as 采购货款,a.send_num 发货数量,oa.contactor as 收货人,oa.mobile as 联系方式,oa.addr as 收货地址,oa.addr_code,oa.arrive_time as 到货时间,a.outCode as 发货申请单号,po.order_type,b.platform_order 平台订单号,a.post_name 发货申请单物流公司,a.post_code 发货申请单物流单号,a.post_fee 发货申请单物流费用,a.remark 发货申请单备注,a.apply_name 申请人,b.cat_id 商品分类')
  546. ->whereIn('a.outCode', $outCodes)
  547. ->order("a.addtime desc")
  548. ->withAttr('序号', function () use (&$i) {
  549. return $i++;
  550. })->withAttr('采购单状态', function ($val) use ($status) {
  551. return isset($status[$val]) ? $status[$val] : '';
  552. })->withAttr('发货方式', function ($val) use ($send_type) {
  553. return isset($send_type[$val]) ? $send_type[$val] : '';
  554. })->where("a.is_del", "=", 0)
  555. ->leftJoin("sale b", "b.orderCode=a.orderCode")
  556. ->leftJoin("order_addr oa", "oa.id=a.addrid AND oa.is_del=0")
  557. ->leftJoin("warehouse_info wi", "wi.wsm_code=a.wsm_code AND wi.is_del=0")
  558. ->leftJoin("supplier s", "s.code=wi.supplierNo AND s.is_del=0")
  559. ->leftJoin('order_send os', 'os.outCode=a.outCode ')
  560. ->leftJoin("purchease_order po", "po.cgdNo=os.cgdNo AND po.is_del=0")
  561. ->select()
  562. ->toArray();
  563. foreach ($list as &$value) {
  564. if ($value['order_type'] == 3) {
  565. $temp = Db::name("good_zixun")
  566. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  567. ->field('id,specinfo,good_unit')
  568. ->find();
  569. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  570. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  571. $speclist = [];
  572. foreach ($specinfo as $val) {
  573. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  574. }
  575. } else {
  576. $good_unit = Db::name('good_basic')
  577. ->where(['spuCode' => $value['产品编号']])
  578. ->value('good_unit', 0);
  579. $spec = Db::name("good_spec")
  580. ->field('id,spec_id,spec_value_id')
  581. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  582. ->select()
  583. ->toArray();
  584. $speclist = [];
  585. if (!empty($spec)) {
  586. foreach ($spec as $val) {
  587. $speclist[] = Db::name("specs")->where(["id" => $val['spec_id']])->value('spec_name', '') . ':' . Db::name("spec_value")->where(["id" => $val['spec_value_id']])->value('spec_value', '');
  588. }
  589. }
  590. }
  591. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  592. $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : '';
  593. $value['商品分类'] = implode('/', array_column(made($value['商品分类']), 'name'));
  594. $value['发货申请单物流单号'] = ' ' . $value['发货申请单物流单号'];//添加空格防止将数字转为科学计数法(比如,韵达的快递单号就是纯数字)
  595. $addinfo = $value['addr_code'] != '' ? json_decode($value['addr_code'], true) ?? $value['addr_code'] : '';
  596. if (is_string($addinfo) && $addinfo != '') {
  597. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  598. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $value['addr_code']);
  599. }
  600. $addr_cn = GetAddr(json_encode($addinfo));
  601. $value['收货地址'] = $addr_cn . $value['收货地址'];
  602. unset($value['order_type']);
  603. unset($value['addr_code']);
  604. }
  605. if (empty($list)) $list[] = '没有可供导出的数据';
  606. $headerArr = array_keys($list[0]);
  607. excelSave('发货申请单导出' . date('YmdHis'), $headerArr, $list);
  608. }
  609. //发货单详情
  610. public function outInfo()
  611. {
  612. $outCode = $this->request->filter('trim')->post('outCode');
  613. //isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  614. if ($outCode == "") {
  615. return json_show(1002, "参数outcode不能为空");
  616. }
  617. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find();
  618. if (empty($codeinfo)) {
  619. return json_show(1002, "未找到出库数据");
  620. }
  621. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  622. if ($item['order_type'] == 3) {
  623. $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find();
  624. if ($goodinfo == false) {
  625. return json_show(1004, "未找到商品数据");
  626. }
  627. } else {
  628. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  629. ->where(['a.skuCode' => $item['skuCode']])->find();
  630. if ($goodinfo == false) {
  631. return json_show(1002, "未找到商品数据");
  632. }
  633. }
  634. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  635. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  636. if ($addr) {
  637. $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : '';
  638. if (is_string($addinfo) && $addinfo != '') {
  639. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  640. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']);
  641. }
  642. $addr['addr_cn'] = GetAddr(json_encode($addinfo));
  643. }
  644. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  645. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  646. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  647. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  648. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  649. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  650. $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : "";
  651. $codeinfo['addr'] = $addr['addr_cn'] ?? '';
  652. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  653. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  654. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  655. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  656. $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime'];
  657. $codeinfo['can'] = $int;
  658. $codeinfo['order_source'] = $item['order_source'];
  659. $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find();
  660. $codeinfo['order_return'] = $orderReturn;
  661. return json_show(0, "获取成功", $codeinfo);
  662. }
  663. //获取采反商品详情,当数据不在good_zixun中,模仿goodzxinfo
  664. public function getGoodZxInfoByNotZixun()
  665. {
  666. $param = $this->request->only(['spuCode'], 'post', 'trim');
  667. $val = Validate::rule(['spuCode' => 'require']);
  668. if (!$val->check($param)) return json_show(1004, $val->getError());
  669. $order = Db::name('consult_bids')
  670. ->field(true)
  671. ->where(['spuCode' => $param['spuCode'], 'is_del' => 0])
  672. ->find();
  673. if (empty($order)) return json_show(1003, "数据未找到");
  674. // $orderCode= isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  675. // if($orderCode==""){
  676. // return json_show(1003,"参数spuCode不能为空");
  677. // }
  678. // $order= Db::name("good_zixun")->where(["spuCode"=>$orderCode,"is_del"=>0])->find();
  679. // if(empty($order)){
  680. // return json_show(1003,"订单数据未找到");
  681. // }
  682. $unit = Db::name("unit")->where(["id" => $order['unit_id']])->find();
  683. if ($order['brand_id'] != 0) {
  684. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  685. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  686. } else {
  687. $order["brand_name"] = "";
  688. $order["brand_id"] = "";
  689. }
  690. $order['specinfo'] = json_decode($order['specinfo'], true);
  691. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  692. $order['cat_info'] = made($order['cat_id'], []);
  693. $order['noble_name'] = isset($order['metal_id']) && $order['metal_id'] != 0 ? $this->noble[$order['metal_id']] : "";
  694. if ($order['is_gold_price'] == 1) {
  695. $price = Db::name("gold_price1")->where(["type" => $order['metal_id'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  696. }
  697. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  698. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  699. // $palt =Db::name("platform")->where(["id"=>$order['platform_id']])->find();
  700. // $order['platform_name']=isset($palt['platform_name']) ? $palt['platform_name']:"";
  701. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  702. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  703. return json_show(0, "获取成功", $order);
  704. }
  705. }