Sale.php 40 KB

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