Sale.php 44 KB

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