Sale.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. 'holder_id' => $outinfo['apply_id'],
  258. ]);
  259. $orderstatus = $einfo['status'];
  260. $einfo['send_num'] += $outinfo['send_num'];
  261. $einfo['wsend_num'] -= $outinfo['send_num'];
  262. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  263. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  264. $einfo['updatetime'] = date("Y-m-d H:i:s");
  265. $saleup = Db::name("sale")->save($einfo);
  266. if ($saleup == false) {
  267. Db::rollback();
  268. return json_show(1002, "销售单状态更新失败");
  269. }
  270. // }
  271. //修改状态,添加待办
  272. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  273. "order_code" => $einfo['orderCode'],//出库单号
  274. "status" => $orderstatus,//这里的status是之前的值
  275. "action_remark" => '',//备注
  276. "action_type" => "edit"//新建create,编辑edit,更改状态status
  277. ], "XSQRD", $einfo['status'], $einfo);
  278. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  279. "order_type" => 'XSQRD',
  280. "order_code" => $einfo['orderCode'],//出库单号
  281. "order_id" => $einfo['id'],
  282. "order_status" => $einfo['status'],
  283. "before_status" => $orderstatus,
  284. 'holder_id' => $einfo['apply_id']
  285. ]);
  286. $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find();
  287. if ($stokc == false) {
  288. Db::rollback();
  289. return json_show(1002, "未找到库存数据");
  290. } else {
  291. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  292. Db::rollback();
  293. return json_show(1002, "超出库存数量");
  294. }
  295. $stokc['wait_out_stock'] -= $outinfo['send_num'];
  296. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  297. // $stokc['intra_stock']+=$outinfo['send_num'];
  298. $stokc['updatetime'] = date("Y-m-d H:i:s");
  299. }
  300. $stoc = Db::name("good_stock")->save($stokc);
  301. if ($stoc == false) {
  302. Db::rollback();
  303. return json_show(1002, "库存更新失败");
  304. }
  305. if($einfo["order_type"]!=1){
  306. $outsend =Db::name("order_send")->where(["outCode"=> $outCode])->findOrEmpty();
  307. if(empty($outsend)){
  308. Db::rollback();
  309. return error_show(1002, "未找到关联采购单");
  310. }
  311. $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  312. ->findOrEmpty();
  313. $bn_code =makeNo("BN");
  314. $bnin=GoodStockInfo::AddBn($stokc["id"],$bn_code,$outinfo['send_num'],$cgdinfo['good_price']??0);
  315. if($bnin==false){
  316. Db::rollback();
  317. return error_show(1004,"库存bn数据新增失败");
  318. }
  319. $bnup =GoodStockInfo::OrderBn($outinfo['orderCode'],$stokc["id"],$outinfo['send_num']);
  320. if($bnup==false){
  321. Db::rollback();
  322. return error_show(1004,"库存bn库存数更新失败");
  323. }
  324. }
  325. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stokc['id'], "type" => 2, 'stock' => $outinfo['send_num'], "stock_name" => "wait_out_stock"];
  326. GoodLog::LogAdd(['id' => $uid, 'nickname' => $uname], $good_data, "CKD");
  327. Cache::store("redis")->handler()->lPush("SENDOUT", $outCode);
  328. Db::commit();
  329. } else {
  330. Db::rollback();
  331. return json_show(1004, "发货失败");
  332. }
  333. //如果是有赞订单的话,将发货信息推到有赞
  334. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  335. if ($einfo['order_source'] == 5) {
  336. $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]);
  337. $res = json_decode($res, true);
  338. if ($res['code'] != 0) return json_show(0, '发货成功,' . $res['message']);
  339. }
  340. return json_show(0, "发货成功");
  341. } catch (\Exception $e) {
  342. Db::rollback();
  343. return json_show(1004, $e->getMessage());
  344. }
  345. }
  346. //库管批量发货
  347. public function outSendBatchByImport()
  348. {
  349. $param = $this->request->only(['list'], 'post', 'trim');
  350. if (empty($param['list'])) return json_show(1005, 'list参数不能为空');
  351. //只允许处理所属供应商下的采购单关联的发货单
  352. $temp = Db::name('order_out')
  353. ->alias('a')
  354. ->leftJoin('order_num b', 'b.orderCode=a.orderCode')
  355. ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo')
  356. ->where([
  357. ['a.is_del', '=', 0],
  358. ['a.outCode', 'in', array_column($param['list'], 'outCode')],
  359. ['c.supplierNo', '<>', $this->request->user['supplierNo']]
  360. ])->column('a.outCode');
  361. if (!empty($temp)) return json_show(1003, '不允许处理其他供应商的单子', $temp);
  362. // $user = GetUserInfo($param['token']);
  363. $createrid = $this->request->user['uid'];
  364. $creater = $this->request->user['nickname'];
  365. //所有发货单信息
  366. $order_out_infos = Db::name("order_out")
  367. ->where('is_del', 0)
  368. ->whereIn('outCode', array_column($param['list'], 'outCode'))
  369. ->whereIn('status', [0, 1])
  370. ->column('id,status,send_num,orderCode,wsm_code,apply_id,apply_name', 'outCode');
  371. //处理数据
  372. Db::startTrans();
  373. try {
  374. $good_data = [];
  375. $temp_out_codes = [];
  376. foreach ($param['list'] as $value) {
  377. if (!isset($order_out_infos[$value['outCode']])) throw new Exception($value['outCode'] . '该发货单未找到或状态不允许发货');
  378. //每次循环都需要重新查询对应数据,为了数据及时更新
  379. $sale_infos = Db::name("sale")
  380. ->where('orderCode', $order_out_infos[$value['outCode']]['orderCode'])
  381. ->where('is_del', 0)
  382. ->column('id,send_num,wsend_num,status,good_code,order_source,apply_id', 'orderCode');
  383. if (!isset($sale_infos[$order_out_infos[$value['outCode']]['orderCode']])) throw new Exception($value['outCode'] . '该发货单对应的销售单未找到');
  384. //判断发货单是否重复
  385. if (isset($temp_out_codes[$value['outCode']])) {
  386. Db::rollback();
  387. return json_show(1005, '下列发货申请单号重复', [$value]);
  388. } else $temp_out_codes[$value['outCode']] = $value['outCode'];
  389. //【发货单】
  390. //查询已有发货单信息及状态
  391. //更新发货单
  392. $up = Db::name("order_out")
  393. ->where(['id' => $order_out_infos[$value['outCode']]['id'], 'outCode' => $value['outCode'], 'status' => 1, "is_del" => 0])
  394. ->update([
  395. 'post_name' => isset($value['post_name']) ? $value['post_name'] : '',
  396. 'post_code' => $value['post_code'],
  397. 'post_fee' => $value['post_fee'],
  398. 'sendtime' => date('Y-m-d H:i:s'),
  399. 'status' => 2,
  400. 'updatetime' => date('Y-m-d H:i:s'),
  401. 'remark' => isset($value['remark']) ? $value['remark'] : '',
  402. ]);
  403. if ($up) {
  404. //修改状态,添加待办
  405. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  406. "order_code" => $value['outCode'],//出库单号
  407. "status" => $order_out_infos[$value['outCode']]['status'],//这里的status是之前的值
  408. "action_remark" => '',//备注
  409. "action_type" => "edit"//新建create,编辑edit,更改状态status
  410. ], "CKD", 2, $order_out_infos[$value['outCode']]);
  411. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  412. "order_type" => 'CKD',
  413. "order_code" => $value['outCode'],//出库单号
  414. "order_id" => $order_out_infos[$value['outCode']]['id'],
  415. "order_status" => 2,
  416. "before_status" => $order_out_infos[$value['outCode']]['status'],
  417. 'wait_id' => $order_out_infos[$value['outCode']]['apply_id'],
  418. 'wait_name' => $order_out_infos[$value['outCode']]['apply_name'],
  419. 'holder_id' => $order_out_infos[$value['outCode']]['apply_id'],
  420. ]);
  421. //【销售单】
  422. $einfo = [];
  423. $einfo['send_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['send_num'] + $order_out_infos[$value['outCode']]['send_num'];
  424. $einfo['wsend_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['wsend_num'] - $order_out_infos[$value['outCode']]['send_num'];
  425. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  426. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  427. $einfo['updatetime'] = date("Y-m-d H:i:s");
  428. $saleup = Db::name("sale")
  429. ->where('id', $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'])
  430. ->update($einfo);
  431. if ($saleup == false) throw new Exception($value['outCode'] . '销售单状态更新失败');
  432. //修改状态,添加待办
  433. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  434. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//销售单号
  435. "status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],//这里的status是之前的值
  436. "action_remark" => '',//备注
  437. "action_type" => "edit"//新建create,编辑edit,更改状态status
  438. ], "XSQRD", $einfo['status'], $einfo);
  439. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  440. "order_type" => 'XSQRD',
  441. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//出库单号
  442. "order_id" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'],
  443. "order_status" => $einfo['status'],
  444. "before_status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],
  445. 'holder_id' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['apply_id']
  446. ]);
  447. //【库存】
  448. $stock = Db::name("good_stock")
  449. ->field('id,wait_out_stock,usable_stock,total_stock,updatetime')
  450. ->where([
  451. "is_del" => 0,
  452. 'spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'],
  453. "wsm_code" => $order_out_infos[$value['outCode']]['wsm_code'],
  454. ])->find();
  455. if ($stock == false) throw new Exception($value['outCode'] . '未找到库存数据');
  456. else {
  457. if ($order_out_infos[$value['outCode']]['send_num'] > $stock['wait_out_stock']) throw new Exception($value['outCode'] . '超出库存数量');
  458. $stock['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num'];
  459. $stock['total_stock'] = $stock['wait_out_stock'] + $stock['usable_stock'];
  460. $stock['updatetime'] = date("Y-m-d H:i:s");
  461. $stock_rs = Db::name("good_stock")
  462. ->where('id', $stock['id'])
  463. ->update($stock);
  464. if ($stock_rs == false) throw new Exception($value['outCode'] . '库存更新失败');
  465. if($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]["order_type"]!=1){
  466. $outsend =Db::name("order_send")->where(["outCode"=> $value['outCode']])->findOrEmpty();
  467. if(empty($outsend)){
  468. Db::rollback();
  469. return error_show(1002, "未找到关联采购单");
  470. }
  471. $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  472. ->findOrEmpty();
  473. $bn_code =makeNo("BN");
  474. $bnin=GoodStockInfo::AddBn($stock["id"],$bn_code,$order_out_infos[$value['outCode']]['send_num'],$cgdinfo['good_price']??0);
  475. if($bnin==false){
  476. Db::rollback();
  477. return error_show(1004,"库存bn数据新增失败");
  478. }
  479. $bnup =GoodStockInfo::OrderBn($order_out_infos[$value['outCode']]['orderCode'],$stock["id"],$order_out_infos[$value['outCode']]['send_num']);
  480. if($bnup==false){
  481. Db::rollback();
  482. return error_show(1004,"库存bn库存数更新失败");
  483. }
  484. }
  485. $good_data[] = [
  486. 'good_log_code' => $value['outCode'],
  487. 'stock_id' => $stock['id'],
  488. 'type' => 2,
  489. 'stock' => $order_out_infos[$value['outCode']]['send_num'],
  490. 'stock_name' => 'wait_out_stock'
  491. ];
  492. }
  493. } else throw new Exception($value['outCode'] . '发货失败');
  494. Cache::store("redis")->handler()->lPush("SENDOUT", $value['outCode']);
  495. }
  496. if (!empty($good_data)) GoodLog::LogAdd(['id' => $createrid, 'nickname' => $creater], $good_data, "CKD");
  497. Db::commit();
  498. return json_show(0, '发货成功');
  499. } catch (Exception $exception) {
  500. Db::rollback();
  501. return json_show(1004, $exception->getMessage());
  502. }
  503. }
  504. //咨询商品详情
  505. public function goodzxinfo()
  506. {
  507. $this->post = $this->request->filter('trim')->post();
  508. $orderCode = isset($this->post['spuCode']) && $this->post['spuCode'] != '' ? trim($this->post['spuCode']) : "";
  509. if ($orderCode == "") {
  510. return json_show(1003, "参数spuCode不能为空");
  511. }
  512. $order = Db::name("good_zixun")->where(["spuCode" => $orderCode, "is_del" => 0])->find();
  513. if (empty($order)) {
  514. return json_show(1003, "订单数据未找到");
  515. }
  516. $unit = Db::name("unit")->where(["id" => $order['good_unit']])->find();
  517. if ($order['brand_id'] != 0) {
  518. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  519. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  520. } else {
  521. $order["brand_name"] = "";
  522. $order["brand_id"] = "";
  523. }
  524. $order['specinfo'] = json_decode($order['specinfo'], true);
  525. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  526. $order['cat_info'] = made($order['cat_id'], []);
  527. $order['noble_name'] = isset($order['noble_metal']) && $order['noble_metal'] != 0 ? $this->noble[$order['noble_metal']] : "";
  528. if ($order['is_gold_price'] == 1) {
  529. $price = Db::name("gold_price1")->where(["type" => $order['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  530. }
  531. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  532. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  533. $palt = Db::name("platform")->where(["id" => $order['platform_id']])->find();
  534. $order['platform_name'] = isset($palt['platform_name']) ? $palt['platform_name'] : "";
  535. $company = Db::name("business")->where(["companyNo" => $order['companyNo']])->find();
  536. $order['company'] = isset($company['company']) ? $company['company'] : "";
  537. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  538. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  539. return json_show(0, "获取成功", $order);
  540. }
  541. //发货申请单导出
  542. public function exportSaleOut()
  543. {
  544. $outCodes = $this->request->filter('trim')->post('outCodes');
  545. if (empty($outCodes)) return json_show(1004, '要导出的发货申请单编号不能为空');
  546. $i = 1;
  547. $send_type = [1 => '直接发货', 2 => '延时发货'];
  548. $status = [0 => '待发货', 1 => '待库管发货', 2 => '已发货待收货', 3 => '已收货', 4 => '已全部退货'];
  549. $list = Db::name("order_out")
  550. ->alias('a')
  551. ->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 商品分类')
  552. ->whereIn('a.outCode', $outCodes)
  553. ->order("a.addtime desc")
  554. ->withAttr('序号', function () use (&$i) {
  555. return $i++;
  556. })->withAttr('采购单状态', function ($val) use ($status) {
  557. return isset($status[$val]) ? $status[$val] : '';
  558. })->withAttr('发货方式', function ($val) use ($send_type) {
  559. return isset($send_type[$val]) ? $send_type[$val] : '';
  560. })->where("a.is_del", "=", 0)
  561. ->leftJoin("sale b", "b.orderCode=a.orderCode")
  562. ->leftJoin("order_addr oa", "oa.id=a.addrid AND oa.is_del=0")
  563. ->leftJoin("warehouse_info wi", "wi.wsm_code=a.wsm_code AND wi.is_del=0")
  564. ->leftJoin("supplier s", "s.code=wi.supplierNo AND s.is_del=0")
  565. ->leftJoin('order_send os', 'os.outCode=a.outCode ')
  566. ->leftJoin("purchease_order po", "po.cgdNo=os.cgdNo AND po.is_del=0")
  567. ->select()
  568. ->toArray();
  569. foreach ($list as &$value) {
  570. if ($value['order_type'] == 3) {
  571. $temp = Db::name("good_zixun")
  572. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  573. ->field('id,specinfo,good_unit')
  574. ->find();
  575. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  576. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  577. $speclist = [];
  578. foreach ($specinfo as $val) {
  579. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  580. }
  581. } else {
  582. $good_unit = Db::name('good_basic')
  583. ->where(['spuCode' => $value['产品编号']])
  584. ->value('good_unit', 0);
  585. $spec = Db::name("good_spec")
  586. ->field('id,spec_id,spec_value_id')
  587. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  588. ->select()
  589. ->toArray();
  590. $speclist = [];
  591. if (!empty($spec)) {
  592. foreach ($spec as $val) {
  593. $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', '');
  594. }
  595. }
  596. }
  597. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  598. $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : '';
  599. $value['商品分类'] = implode('/', array_column(made($value['商品分类']), 'name'));
  600. $value['发货申请单物流单号'] = ' ' . $value['发货申请单物流单号'];//添加空格防止将数字转为科学计数法(比如,韵达的快递单号就是纯数字)
  601. $addinfo = $value['addr_code'] != '' ? json_decode($value['addr_code'], true) ?? $value['addr_code'] : '';
  602. if (is_string($addinfo) && $addinfo != '') {
  603. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  604. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $value['addr_code']);
  605. }
  606. $addr_cn = GetAddr(json_encode($addinfo));
  607. $value['收货地址'] = $addr_cn . $value['收货地址'];
  608. unset($value['order_type']);
  609. unset($value['addr_code']);
  610. }
  611. if (empty($list)) $list[] = '没有可供导出的数据';
  612. $headerArr = array_keys($list[0]);
  613. excelSave('发货申请单导出' . date('YmdHis'), $headerArr, $list);
  614. }
  615. //发货单详情
  616. public function outInfo()
  617. {
  618. $outCode = $this->request->filter('trim')->post('outCode');
  619. //isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  620. if ($outCode == "") {
  621. return json_show(1002, "参数outcode不能为空");
  622. }
  623. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find();
  624. if (empty($codeinfo)) {
  625. return json_show(1002, "未找到出库数据");
  626. }
  627. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  628. if ($item['order_type'] == 3) {
  629. $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find();
  630. if ($goodinfo == false) {
  631. return json_show(1004, "未找到商品数据");
  632. }
  633. } else {
  634. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  635. ->where(['a.skuCode' => $item['skuCode']])->find();
  636. if ($goodinfo == false) {
  637. return json_show(1002, "未找到商品数据");
  638. }
  639. }
  640. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  641. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  642. if ($addr) {
  643. $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : '';
  644. if (is_string($addinfo) && $addinfo != '') {
  645. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  646. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']);
  647. }
  648. $addr['addr_cn'] = GetAddr(json_encode($addinfo));
  649. }
  650. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  651. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  652. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  653. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  654. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  655. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  656. $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : "";
  657. $codeinfo['addr'] = $addr['addr_cn'] ?? '';
  658. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  659. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  660. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  661. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  662. $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime'];
  663. $codeinfo['can'] = $int;
  664. $codeinfo['order_source'] = $item['order_source'];
  665. $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find();
  666. $codeinfo['order_return'] = $orderReturn;
  667. return json_show(0, "获取成功", $codeinfo);
  668. }
  669. //获取采反商品详情,当数据不在good_zixun中,模仿goodzxinfo
  670. public function getGoodZxInfoByNotZixun()
  671. {
  672. $param = $this->request->only(['spuCode'], 'post', 'trim');
  673. $val = Validate::rule(['spuCode' => 'require']);
  674. if (!$val->check($param)) return json_show(1004, $val->getError());
  675. $order = Db::name('consult_bids')
  676. ->field(true)
  677. ->where(['spuCode' => $param['spuCode'], 'is_del' => 0])
  678. ->find();
  679. if (empty($order)) return json_show(1003, "数据未找到");
  680. // $orderCode= isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  681. // if($orderCode==""){
  682. // return json_show(1003,"参数spuCode不能为空");
  683. // }
  684. // $order= Db::name("good_zixun")->where(["spuCode"=>$orderCode,"is_del"=>0])->find();
  685. // if(empty($order)){
  686. // return json_show(1003,"订单数据未找到");
  687. // }
  688. $unit = Db::name("unit")->where(["id" => $order['unit_id']])->find();
  689. if ($order['brand_id'] != 0) {
  690. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  691. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  692. } else {
  693. $order["brand_name"] = "";
  694. $order["brand_id"] = "";
  695. }
  696. $order['specinfo'] = json_decode($order['specinfo'], true);
  697. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  698. $order['cat_info'] = made($order['cat_id'], []);
  699. $order['noble_name'] = isset($order['metal_id']) && $order['metal_id'] != 0 ? $this->noble[$order['metal_id']] : "";
  700. if ($order['is_gold_price'] == 1) {
  701. $price = Db::name("gold_price1")->where(["type" => $order['metal_id'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  702. }
  703. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  704. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  705. // $palt =Db::name("platform")->where(["id"=>$order['platform_id']])->find();
  706. // $order['platform_name']=isset($palt['platform_name']) ? $palt['platform_name']:"";
  707. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  708. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  709. return json_show(0, "获取成功", $order);
  710. }
  711. }