Order.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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 think\Exception;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. //销售订单(对应于采销端的采购单)
  10. class Order extends HomeBaseController
  11. {
  12. //列表
  13. public function getList()
  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 = [['po.is_del', "=", 0], ['po.supplierNo', '=', $this->request->user['supplierNo']], ['s.is_stock', '=', 0]];
  19. $bkcode = isset($this->post['bk_code']) && $this->post['bk_code'] != "" ? trim($this->post['bk_code']) : "";
  20. if ($bkcode != "") {
  21. $where[] = ['po.bkcode', "like", "%{$bkcode}%"];
  22. }
  23. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  24. if ($status !== "") {
  25. // $where['status'] = $status;
  26. $where[] = ['po.status', "=", $status];
  27. }
  28. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
  29. if ($cgdNo != "") {
  30. //$where['cgdNo'] = Db::Raw("like '%{$cgdNo}%'");
  31. $where[] = ['po.cgdNo', "like", "%{$cgdNo}%"];
  32. }
  33. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] != "" ? trim($this->post['apply_name']) : "";
  34. if ($apply_name != "") {
  35. // $where['cgder'] =Db::Raw("like '%{$apply_name}%'");
  36. $where[] = ['po.cgder', "like", "%{$apply_name}%"];
  37. }
  38. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] != "" ? trim($this->post['wsm_code']) : "";
  39. if ($wsm_code != "") {
  40. // $where['wsm_code'] = $wsm_code;
  41. $where[] = ['po.wsm_code', "=", $wsm_code];
  42. }
  43. $wsm_in_code = isset($this->post['wsm_in_code']) && $this->post['wsm_in_code'] != "" ? trim($this->post['wsm_in_code']) : "";
  44. if ($wsm_in_code != "") {
  45. $incode = Db::name("purchease_in")
  46. ->where(["wsm_in_code" => Db::raw(" like %{$wsm_in_code}%"), "is_del" => 0])
  47. ->column("cgdNo");
  48. if (empty($incode)) {
  49. return json_show(1005, "未找到有关入库单信息");
  50. }
  51. $where[] = ['po.cgdNo', "in", $incode];
  52. }
  53. $good_name = isset($this->post['good_name']) && $this->post['good_name'] != "" ? trim($this->post['good_name']) : "";
  54. if ($good_name != "") {
  55. //$where['good_name'] = Db::raw(" like %{$good_name}%");
  56. $where[] = ['po.good_name', "like", "%{$good_name}%"];
  57. }
  58. $good_code = isset($this->post['good_code']) && $this->post['good_code'] != "" ? trim($this->post['good_code']) : "";
  59. if ($good_code != "") {
  60. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  61. $where[] = ['po.spuCode', "like", "%{$good_code}%"];
  62. }
  63. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
  64. if ($supplierNo != "") {
  65. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  66. $where[] = ['po.supplierNo', "like", "%{$supplierNo}%"];
  67. }
  68. $wsm_supplierNo = isset($this->post['wsm_supplierNo']) && $this->post['wsm_supplierNo'] != "" ? trim($this->post['wsm_supplierNo']) : "";
  69. if ($wsm_supplierNo != "") {
  70. $supplier = Db::name("supplier")
  71. ->field('id')
  72. ->where(["code" => $wsm_supplierNo])
  73. ->find();
  74. if (empty($supplier)) {
  75. return json_show(1004, "未找到供应商信息");
  76. }
  77. $wsmcode = Db::name("warehouse_info")
  78. ->where(["is_del" => 0, "supplierNo" => $wsm_supplierNo])
  79. ->column("wsm_code");
  80. $where[] = ['po.wsm_code', "in", $wsmcode];
  81. }
  82. $start = isset($this->post['start']) && $this->post['start'] != "" ? $this->post['start'] : "";
  83. if ($start != "") {
  84. // $where['addtime'] = Db::raw(" >= '{$start}'");
  85. $where[] = ['po.addtime', ">=", $start];
  86. }
  87. $end = isset($this->post['end']) && $this->post['end'] != "" ? $this->post['end'] : "";
  88. if ($end != "") {
  89. $where[] = ['po.addtime', "<=", $end];
  90. }
  91. $last_start = isset($this->post['last_start']) && $this->post['last_start'] != "" ? $this->post['last_start'] : "";
  92. if ($last_start != "") {
  93. //$where['lasttime'] = Db::raw(" >= '{$last_start}'");
  94. $where[] = ['po.lasttime', ">=", $last_start];
  95. }
  96. $last_end = isset($this->post['last_end']) && $this->post['last_end'] != "" ? $this->post['last_end'] : "";
  97. if ($last_end != "") {
  98. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  99. $where[] = ['po.lasttime', "<=", $last_end];
  100. }
  101. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? $this->post['orderCode'] : "";
  102. if ($orderCode != "") {
  103. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  104. $where[] = ['on.orderCode', "like", '%' . $orderCode . '%'];
  105. }
  106. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  107. if ($company_name !== "") $where[] = ["po.cgder_id", 'in', get_company_item_user_by_name($company_name)];
  108. // if(!empty($role['platform']) ){
  109. // $where[]=["po.platform_id","in",$role['platform']];
  110. // }
  111. $count = Db::name("purchease_order")
  112. ->alias('po')
  113. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  114. ->leftJoin('sale s', 's.orderCode=on.orderCode')
  115. ->leftJoin("depart_user u", "u.uid=po.cgder_id AND u.is_del=0")->where($where)
  116. ->where(function ($query) use ($where) {
  117. $query->whereOr([['po.order_type', '<>', 1], ['po.order_source', '=', 0]]);
  118. })
  119. ->count('po.id');
  120. $total = ceil($count / $size);
  121. $page = $page >= $total ? $total : $page;
  122. $list = Db::name("purchease_order")
  123. ->alias('po')
  124. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  125. ->leftJoin('sale s', 's.orderCode=on.orderCode')
  126. ->leftJoin("depart_user u", "u.uid=po.cgder_id AND u.is_del=0")
  127. ->field('po.*,on.orderCode,u.itemid')
  128. ->where($where)
  129. ->where(function ($query) use ($where) {
  130. $query->whereOr([['po.order_type', '<>', 1], ['po.order_source', '=', 0]]);
  131. })
  132. ->page($page, $size)
  133. ->order("po.addtime desc")
  134. ->cursor();
  135. $data = [];
  136. foreach ($list as $value) {
  137. $value['wsm_name'] = "";
  138. if ($value['wsm_code'] != "") {
  139. $wsmcode = Db::name("warehouse_info")
  140. ->alias("a")
  141. ->leftJoin("supplier b", "a.supplierNo=b.code")
  142. ->where(["a.wsm_code" => $value['wsm_code']])
  143. ->field("a.name as wsm_name,b.name,b.code")
  144. ->find();
  145. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  146. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  147. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  148. }
  149. if ($value['order_type'] == 3) {
  150. $goon = Db::name("good_zixun")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
  151. $value["speclist"] = isset($goon['specinfo']) && $goon['specinfo'] != "" ? json_decode($goon['specinfo'], true) : "";
  152. } else {
  153. $goon = Db::name('good_basic')->where(['spuCode' => $value['spuCode']])->find();
  154. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  155. $speclist = [];
  156. if (!empty($spec)) {
  157. foreach ($spec as $val) {
  158. $temp = [];
  159. $temp['id'] = $val['id'];
  160. $temp['spuCode'] = $val['spuCode'];
  161. $temp['spec_id'] = $val['spec_id'];
  162. $temp['spec_value_id'] = $val['spec_value_id'];
  163. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  164. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  165. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  166. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  167. $speclist[] = $temp;
  168. }
  169. }
  170. $value["speclist"] = empty($speclist) ? [] : $speclist;
  171. }
  172. //采购单详情字段order_type 为1或者2时,取线上商品库 商品创建人 order_type 为3或者4时 取采购反馈的商品库 商品反馈人
  173. // $value['cgder_id'] = $goon['createrid'];
  174. // $value['cgder'] = $goon['creater'];
  175. $inorder = Db::name("purchease_in")->where(['cgdNo' => $value['cgdNo'], "is_del" => 0])->select();
  176. $value['child'] = empty($inorder) ? [] : $inorder;
  177. $value['send_num'] -= $value['th_num'];
  178. $value['total_fee'] = round($value['total_fee'] - $value['th_fee'], 2);
  179. //补充orderCode
  180. // $value['orderCode']=isset($all_orderCodes[$value['cgdNo']])?$all_orderCodes[$value['cgdNo']]:'';
  181. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  182. $data[] = $value;
  183. }
  184. return json_show(0, "获取成功", ["list" => $data, "count" => $count]);
  185. }
  186. //详情
  187. public function info()
  188. {
  189. $this->post = $this->request->filter('trim')->post();
  190. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
  191. if ($cgdNo == "") {
  192. return json_show(1004, "参数cgdNo 不能为空");
  193. }
  194. //采购单
  195. $data = Db::name("purchease_order")->where(["cgdNo" => $cgdNo, "is_del" => 0, 'supplierNo' => $this->request->user['supplierNo']])->find();
  196. if (empty($data)) {
  197. return json_show(1004, "未找到数据");
  198. }
  199. //采购退货单
  200. $im = Db::name('purchease_back')->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->select();
  201. $var = [];
  202. foreach ($im as $value) {
  203. if ($data['order_type'] == 3) {
  204. $goo = Db::name("good_zixun")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
  205. } else {
  206. $goo = Db::name('good_basic')->where(['spuCode' => $value['spuCode']])->find();
  207. }
  208. if ($goo == false) {
  209. return json_show(1005, "未找到商品数据");
  210. }
  211. $cat = isset($goo['cat_id']) && $goo['cat_id'] != 0 ? made($goo['cat_id']) : [];
  212. $value['cant'] = $cat;
  213. $var[] = $value;
  214. }
  215. //采购工差单
  216. $dom = Db::name("purchease_diff")->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->find();
  217. if ($data['order_type'] == 3) {
  218. $goon = Db::name("good_zixun")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->find();
  219. } else {
  220. $goon = Db::name('good_basic')->where(['spuCode' => $data['spuCode']])->find();
  221. }
  222. if (empty($goon)) {
  223. return json_show(1005, "未找到商品数据");
  224. } else {
  225. $goon['exclusive'] = isset($goon['is_exclusive']) ? makeExcluse($goon['is_exclusive']) : "";
  226. $unit = Db::name("unit")->where(["id" => $goon['good_unit']])->find();
  227. $goon['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  228. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  229. $speclist = [];
  230. if (!empty($spec)) {
  231. foreach ($spec as $value) {
  232. $temp = [];
  233. $temp['id'] = $value['id'];
  234. $temp['spuCode'] = $value['spuCode'];
  235. $temp['spec_id'] = $value['spec_id'];
  236. $temp['spec_value_id'] = $value['spec_value_id'];
  237. $temp['is_del'] = $value['is_del'];
  238. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  239. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  240. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  241. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  242. $speclist[] = $temp;
  243. }
  244. }
  245. $goon["speclist"] = empty($speclist) ? [] : $speclist;
  246. $proof = Db::name("good_proof")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  247. $goon['proof'] = isset($proof) && $proof != false ? $proof : [];
  248. $goon['origin_place_cn'] = "";
  249. $goon['delivery_place_cn'] = "";
  250. if (isset($goon['delivery_place']) && $goon['delivery_place'] !== "") {
  251. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  252. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['delivery_place']);
  253. $goon['delivery_place_cn'] = GetAddr(json_encode($place));
  254. }
  255. if (isset($goon['delivery_place']) && $goon['origin_place'] !== "") {
  256. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  257. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['origin_place']);
  258. $goon['origin_place_cn'] = GetAddr(json_encode($place));
  259. }
  260. if ($goon['brand_id'] != 0) {
  261. $brand = Db::name("brand")->where(["id" => $goon['brand_id']])->find();
  262. $goon["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  263. } else {
  264. $goon["brand_name"] = "";
  265. $goon["brand_id"] = "";
  266. }
  267. $supplier = Db::name("supplier")->where(["code" => $goon['supplierNo']])->find();
  268. $goon['supplierName'] = isset($supplier['name']) ? $supplier['name'] : "";
  269. $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
  270. if (isset($goon['companyNo']) && $goon['companyNo'] != "") {
  271. $company = Db::name("business")->where(["companyNo" => $goon['companyNo']])->find();
  272. }
  273. $goon['company'] = isset($company['company']) ? $company['company'] : "";
  274. }
  275. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  276. $data['wsm_name'] = "";
  277. if ($data['wsm_code'] != "") {
  278. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")
  279. ->leftJoin("warehouse_addr c", "a.wsm_code=c.wsm_code and c.is_del=0")
  280. ->where(["a.wsm_code" => $data['wsm_code']])->field("a.name as wsm_name,b.name,b.code,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_code")->find();
  281. $data['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  282. $data['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  283. $data['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  284. $data['wsm_contactor'] = isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor'] : "";
  285. $data['wsm_mobile'] = isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile'] : "";
  286. // $data['wsm_addr'] =isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  287. $data['addr_code'] = isset($wsmcode['addr_code']) ? $wsmcode['addr_code'] : "";
  288. $data['wsm_addr'] = isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']) : "";
  289. $data['wsm_addr'] .= isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr'] : "";
  290. $inorder = Db::name("purchease_in")->where(['cgdNo' => $data['cgdNo'], "is_del" => 0])->select();
  291. $data['child'] = empty($inorder) ? [] : $inorder;
  292. }
  293. $data['can'] = $int;
  294. $data['goodinfo'] = $goon;
  295. $data['purcheasediff'] = $dom;
  296. $data['send_num'] -= $data['th_num'];
  297. $data['total_fee'] = round($data['total_fee'] - $data['th_fee'], 2);
  298. if (isset($data['companyNo']) && $data['companyNo'] != "") {
  299. $company = Db::name("business")->where(["companyNo" => $data['companyNo']])->find();
  300. }
  301. $data['company'] = isset($company['company']) ? $company['company'] : "";
  302. //$data['info'] = $var;
  303. $data['purcheaseback'] = $var;
  304. //补充orderCode
  305. $data['orderCode'] = Db::name('order_num')
  306. ->where('cgdNo', $data['cgdNo'])
  307. ->value('orderCode', '');
  308. //补充收货地址
  309. $addr = Db::name('order_addr')
  310. ->field('id,addr,addr_code,contactor,mobile,receipt_quantity')
  311. ->where(['orderCode' => $data['orderCode'], 'is_del' => 0])
  312. ->select()
  313. ->toArray();
  314. if (!empty($addr)) {
  315. foreach ($addr as &$vv) {
  316. $temp = explode(',', $vv['addr_code']);
  317. $temp_ = GetAddr(json_encode(['provice_code' => isset($temp[0]) ? $temp[0] : '', 'city_code' => isset($temp[1]) ? $temp[1] : '', 'area_code' => isset($temp[2]) ? $temp[2] : '']));
  318. $vv['addr_code'] = $temp_ . $vv['addr'];
  319. }
  320. } else $addr = [];
  321. $data['addr_info'] = $addr;
  322. //补充客户名称
  323. if (empty($data['orderCode'])) $data['customer'] = [];
  324. else {
  325. $data['customer'] = Db::name('sale')
  326. ->alias('s')
  327. ->field('c.companyNo,c.companyName')
  328. ->leftJoin('customer_info c', 'c.companyNo=s.customer_code AND c.is_del=0')
  329. ->where(['s.orderCode' => $data['orderCode']])
  330. ->find();
  331. }
  332. return json_show(0, "获取成功", $data);
  333. }
  334. //订单确认
  335. public function status()
  336. {
  337. $this->post = $this->request->filter('trim')->post();
  338. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? $this->post['cgdNo'] : [];
  339. if (empty($cgdNo)) {
  340. return json_show(1004, "参数cgdNo 不能为空");
  341. }
  342. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  343. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  344. if ($status === "") {
  345. return json_show(1004, "参数status 不能为空");
  346. }
  347. if ($status == 0) {
  348. $data = Db::name("purchease_order")
  349. ->field('id,cgdNo')
  350. ->whereIn('cgdNo', $cgdNo)
  351. ->where(["is_del" => 0])
  352. ->where('send_status', '>', 1)
  353. ->find();
  354. if (!empty($data)) {
  355. return json_show(1004, $data['cgdNo'] . "采购单发货中无法取消");
  356. }
  357. }
  358. //原始数据
  359. $info = Db::name("purchease_order")
  360. ->whereIn('cgdNo', $cgdNo)
  361. ->where('status', 0)
  362. ->column('id,cgdNo,status', 'cgdNo');
  363. if (empty($info)) return json_show(1005, '没有可供确认的订单');
  364. $upd = Db::name("purchease_order")
  365. ->whereIn('cgdNo', $cgdNo)
  366. ->where('status', 0)
  367. ->save([
  368. 'status' => $status,
  369. 'remark' => $remark,
  370. 'updatetime' => date("Y-m-d H:i:s"),
  371. ]);
  372. if ($upd) {
  373. // $user=GetUserInfo($this->post['token']);
  374. $uid = $this->request->user['uid']; //isset($user['data']['id'])?$user['data']['id']:0;
  375. $uname = $this->request->user['nickname'];//isset($user['data']['nickname'])?$user['data']['nickname']:'';
  376. foreach ($cgdNo as $vlue) {
  377. $process = ["order_code" => $vlue, "order_id" => isset($info[$vlue]['id']) ? $info[$vlue]['id'] : 0, "order_status" => $status, "order_type" => 'CGD', "before_status" => isset($info[$vlue]['status']) ? $info[$vlue]['status'] : 0];
  378. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], $process);
  379. $order = ["order_code" => $vlue, "status" => '', "action_remark" => $remark, "action_type" => "status"];
  380. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], $order, 'CGD', $status, $this->post);
  381. }
  382. return json_show(0, "更新成功");
  383. } else return json_show(1004, '更新失败');
  384. }
  385. //批量入库
  386. public function add()
  387. {
  388. $param = $this->request->filter('trim')->only(['cgdNo'], 'post');
  389. $val = Validate::rule(['cgdNo|订单编号' => 'require|array|max:100']);
  390. //入库方式 供应商包邮,
  391. //运费 0
  392. if (!$val->check($param)) return json_show(1004, $val->getError());
  393. $cgdinfo = Db::name("purchease_order")
  394. ->whereIn('cgdNo', $param['cgdNo'])
  395. ->where(['is_del' => 0, 'supplierNo' => $this->request->user['supplierNo']])
  396. ->where('status', '=', 1)
  397. ->column('id,wsm_code,good_num,good_price,spuCode,order_type,send_status,status', 'cgdNo');
  398. if (empty($cgdinfo)) return json_show(1005, "未找到采购单数据或采购单未发货");
  399. $all_wsm_code = array_column($cgdinfo, 'wsm_code');
  400. $recep = Db::name('warehouse_addr')
  401. ->where('is_del', 0)
  402. ->whereIn('wsm_code', $all_wsm_code)
  403. ->column('wsm_name', 'wsm_code');
  404. $uid = $this->request->user['uid'];
  405. $nickname = $this->request->user['nickname'];
  406. Db::startTrans();
  407. try {
  408. $date = date('Y-m-d H:i:s');
  409. $i = 0;
  410. $status = 4;//4入库完成
  411. foreach ($param['cgdNo'] as $cgdNo) {
  412. if (empty($cgdinfo[$cgdNo])) throw new Exception('未找到采购单数据或采购单未发货');
  413. $wsm_in_code = makeNo("CF");
  414. $bn_code = makeNo("BN");
  415. $send_num = $cgdinfo[$cgdNo]['good_num'];
  416. //改变编码规则,将原编码后两位换成序列号
  417. //str_pad字符串填充
  418. $wsm_in_code = substr($wsm_in_code, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  419. $i++;
  420. //入库单
  421. $win = Db::name("purchease_in")->insertGetId([
  422. "wsm_in_code" => $wsm_in_code,
  423. "cgdNo" => $cgdNo,
  424. "bnCode"=>$bn_code,
  425. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  426. "wsm_reaper" => isset($recep[$cgdinfo[$cgdNo]['wsm_code']]) ? $recep[$cgdinfo[$cgdNo]['wsm_code']] : '',
  427. "send_num" => $cgdinfo[$cgdNo]['good_num'],
  428. "post_company" => '',
  429. "post_code" => '',
  430. "post_fee" => 0,//运费固定值:0
  431. "sendtime" => $date,
  432. "apply_id" => $this->request->user['uid'],
  433. "apply_name" => $this->request->user['nickname'],
  434. "wait_num" => 0,
  435. "wsm_num" => 0,
  436. "sendtype" => 2,//发货方式,固定值,供应商包邮2
  437. "status" => $status,
  438. "addtime" => $date,
  439. "updatetime" => $date
  440. ]);
  441. if ($win) {
  442. $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
  443. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $sto, "RKD", $status, $this->request->filter('trim')->post());
  444. $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status];
  445. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  446. $order = ["order_code" => $cgdNo, "status" => $cgdinfo[$cgdNo]['status'], "action_remark" => '', "action_type" => "edit"];
  447. //维护台账信息
  448. Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . $date . "' WHERE `cgdNo`='{$cgdNo}'");
  449. //////////////////
  450. $good = Db::name("good_stock")
  451. ->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])
  452. ->lock(true)
  453. ->find();
  454. if (empty($good)) {
  455. $good = [
  456. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  457. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  458. "usable_stock" => 0,
  459. "wait_out_stock" => 0,
  460. "wait_in_stock" => $send_num,
  461. "total_stock" => 0,
  462. "addtime" => date("Y-m-d H:i:s"),
  463. "updatetime" => date("Y-m-d H:i:s"),
  464. ];
  465. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "create"];
  466. } else {
  467. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "edit"];
  468. }
  469. $good['wait_in_stock'] -= $send_num;
  470. // if ($good['presale_stock'] > 0) {
  471. // if ($good['presale_stock'] >= $send_num) {
  472. // $good['presale_stock'] -= $send_num;
  473. // $good['wait_out_stock'] += $send_num;
  474. // } else {
  475. // $good['presale_stock'] = 0;
  476. // $good['usable_stock'] += $send_num - $good['presale_stock'];
  477. // $good['wait_out_stock'] += $good['presale_stock'];
  478. // }
  479. // } else {
  480. $good['usable_stock'] += $send_num;
  481. // }
  482. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  483. $good['updatetime'] = date("Y-m-d H:i:s");
  484. $upd = Db::name("good_stock")->save($good);
  485. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  486. //商品变动日志表,good_log_code字段存储采购单单号
  487. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  488. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
  489. if ($upd) {
  490. GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
  491. if ($cgdinfo[$cgdNo]['order_type'] == 1) {
  492. $bnin=GoodStockInfo::AddBn($stockid,$bn_code,$send_num,$cgdinfo[$cgdNo]['good_price']);
  493. if($bnin==false){
  494. Db::rollback();
  495. return error_show(1004,"库存bn数据新增失败");
  496. }
  497. $bk = Db::name("order_bk")->where(['cgdNo' => $cgdNo])->find();
  498. if ($bk == false) {
  499. $bk = [
  500. "cgdNo" => $cgdNo,
  501. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  502. "companyNo" => $cgdinfo[$cgdNo]['companyNo'],
  503. "total_num" => $send_num,
  504. "merge_num" => 0,
  505. "balance_num" => $send_num,
  506. "status" => 1,
  507. "is_del" => 0,
  508. 'addtime' => date("Y-m-d H:i:s"),
  509. "updatetime" => date("Y-m-d H:i:s")
  510. ];
  511. $upcgd = Db::name("order_bk")->save($bk);
  512. } else {
  513. $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
  514. $bk['balance_num'] += $send_num;
  515. $bk['total_num'] += $send_num;
  516. $bk['updatetime'] = date("Y-m-d H:i:s");
  517. $upcgd = Db::name("order_bk")->where($where)->update($bk);
  518. }
  519. if ($upcgd == false) throw new Exception('采购单数量更新失败');
  520. }
  521. if ($cgdinfo[$cgdNo]['send_status'] == 1 && $cgdinfo[$cgdNo]['order_type'] != 1) {
  522. $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
  523. if (!empty($send)) {
  524. $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()->toArray();
  525. if (!empty($out)) {
  526. $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->update(["status" => 1, 'wsm_code' => $cgdinfo[$cgdNo]['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
  527. if ($up == false) throw new Exception('出库单出库失败');
  528. else {
  529. foreach ($out as $k => $v_outCode) {
  530. //修改状态,添加待办
  531. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], [
  532. "order_code" => $v_outCode['outCode'],//出库单号
  533. "status" => 0,//这里的status是之前的值
  534. "action_remark" => '',//备注
  535. "action_type" => "status"//新建create,编辑edit,更改状态status
  536. ], "CKD", 1, $param);
  537. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
  538. "order_type" => 'CKD',
  539. "order_code" => $v_outCode['outCode'],//出库单号
  540. "order_id" => $v_outCode['id'],
  541. "order_status" => 1,
  542. "before_status" => 0
  543. ]);
  544. }
  545. }
  546. $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])->find();
  547. if ($good1 == false) throw new Exception('未找到对应商品库存');
  548. $out_num = intval(array_sum(array_column($out, "send_num")));
  549. if ($out_num > $good1['usable_stock']) throw new Exception('库存数量不足发货');
  550. $good1['usable_stock'] -= $out_num;
  551. $good1['wait_out_stock'] += $out_num;
  552. $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
  553. $good1['updatetime'] = date("Y-m-d H:i:s");
  554. $upd = Db::name("good_stock")->save($good1);
  555. if ($upd == false) throw new Exception('库存数量更新失败');
  556. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  557. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  558. GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
  559. $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
  560. if ($sendp == false) throw new Exception('库存发货失败');
  561. }
  562. }
  563. }
  564. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $order, "CGD", $cgdinfo[$cgdNo]['status'], $cgdinfo[$cgdNo]);
  565. $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo[$cgdNo]['id'], "order_status" => $cgdinfo[$cgdNo]['status'], "order_type" => 'CGD', "before_status" => $order['status']];
  566. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  567. // Db::commit();
  568. // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
  569. } else throw new Exception('商品入库失败');
  570. //////////////////
  571. } else throw new Exception('新建入库单失败');
  572. }
  573. //维护原有采购单信息
  574. Db::name("purchease_order")
  575. ->whereIn('id', array_column($cgdinfo, 'id'))
  576. ->update([
  577. 'wsend_num' => 0,
  578. 'send_num' => Db::raw('good_num'),
  579. 'send_status' => 3,
  580. 'status' => 3,
  581. 'updatetime' => date("Y-m-d H:i:s"),
  582. ]);
  583. Db::commit();
  584. return json_show(0, '新建入库单成功');
  585. // $win = Db::name("purchease_in")->insert($orin, true);
  586. // if ($win) {
  587. // $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
  588. // ActionLog::logAdd($this->post['token'], $sto, "RKD", $status, $this->post);
  589. // $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status];
  590. // ProcessOrder::AddProcess($this->post['token'], $process);
  591. // $order = ["order_code" => $cgdNo, "status" => $cgdinfo['status'], "action_remark" => '', "action_type" => "edit"];
  592. //维护台账信息
  593. // Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `cgdNo`='{$cgdNo}'");
  594. // if ($status == 1) {
  595. // Db::commit();
  596. // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
  597. // }
  598. // if ($status == 4) {
  599. // $cgdinfo['wsend_num'] -= $send_num;
  600. // $cgdinfo['send_num'] += $send_num;
  601. // $cgdinfo['send_status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
  602. // $cgdinfo['status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
  603. // $cgdinfo['updatetime'] = date("Y-m-d H:i:s");
  604. // $up = Db::name("purchease_order")->save($cgdinfo);
  605. // if ($up == false) {
  606. // Db::rollback();
  607. // return json_show(1004, "新建失败");
  608. // }
  609. // $good = Db::name("good_stock")
  610. // ->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])
  611. // ->lock(true)
  612. // ->find();
  613. // if (empty($good)) {
  614. // $good = [
  615. // "spuCode" => $cgdinfo['spuCode'],
  616. // "wsm_code" => $cgdinfo['wsm_code'],
  617. // "usable_stock" => 0,
  618. // "wait_out_stock" => 0,
  619. // "wait_in_stock" => $send_num,
  620. // "total_stock" => 0,
  621. // "addtime" => date("Y-m-d H:i:s"),
  622. // "updatetime" => date("Y-m-d H:i:s"),
  623. // ];
  624. // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  625. // // "action_type"=>"create"];
  626. // } else {
  627. // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  628. // // "action_type"=>"edit"];
  629. // }
  630. // $good['wait_in_stock'] -= $send_num;
  631. // if ($good['presale_stock'] > 0) {
  632. // if ($good['presale_stock'] >= $send_num) {
  633. // $good['presale_stock'] -= $send_num;
  634. // $good['wait_out_stock'] += $send_num;
  635. // } else {
  636. // $good['presale_stock'] = 0;
  637. // $good['usable_stock'] += $send_num - $good['presale_stock'];
  638. // $good['wait_out_stock'] += $good['presale_stock'];
  639. // }
  640. // } else {
  641. // $good['usable_stock'] += $send_num;
  642. // }
  643. //
  644. // $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  645. // $good['updatetime'] = date("Y-m-d H:i:s");
  646. // $upd = Db::name("good_stock")->save($good);
  647. // //商品变动日志表,good_log_code字段存储采购单单号
  648. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  649. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
  650. // if ($upd) {
  651. // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
  652. // if ($cgdinfo['order_type'] == 1) {
  653. // $bk = Db::name("order_bk")->where(['cgdNo' => $cgdinfo['cgdNo']])->find();
  654. // if ($bk == false) {
  655. // $bk = [
  656. // "cgdNo" => $cgdinfo['cgdNo'],
  657. // "spuCode" => $cgdinfo['spuCode'],
  658. // "companyNo" => $cgdinfo['companyNo'],
  659. // "total_num" => $send_num,
  660. // "merge_num" => 0,
  661. // "balance_num" => $send_num,
  662. // "status" => 1,
  663. // "is_del" => 0,
  664. // 'addtime' => date("Y-m-d H:i:s"),
  665. // "updatetime" => date("Y-m-d H:i:s")
  666. // ];
  667. // $upcgd = Db::name("order_bk")->save($bk);
  668. // } else {
  669. // $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
  670. // $bk['balance_num'] += $send_num;
  671. // $bk['total_num'] += $send_num;
  672. // $bk['updatetime'] = date("Y-m-d H:i:s");
  673. // $upcgd = Db::name("order_bk")->where($where)->update($bk);
  674. // }
  675. //
  676. // if ($upcgd == false) {
  677. // Db::rollback();
  678. // return json_show(1004, "采购单数量更新失败");
  679. // }
  680. // }
  681. // if ($cgdinfo['send_status'] == 3 && $cgdinfo['order_type'] != 1) {
  682. // $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
  683. // if (!empty($send)) {
  684. // $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()
  685. // ->toArray();
  686. // if (!empty($out)) {
  687. // $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->update
  688. // (["status" => 1, 'wsm_code' => $cgdinfo['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
  689. // if ($up == false) {
  690. // Db::rollback();
  691. // return json_show(1004, "出库单出库失败");
  692. // } else {
  693. // foreach ($out as $k => $v_outCode) {
  694. // //修改状态,添加待办
  695. // ActionLog::logAdd($this->post['token'], [
  696. // "order_code" => $v_outCode['outCode'],//出库单号
  697. // "status" => 0,//这里的status是之前的值
  698. // "action_remark" => '',//备注
  699. // "action_type" => "status"//新建create,编辑edit,更改状态status
  700. // ], "CKD", 1, $this->post);
  701. //
  702. // ProcessOrder::AddProcess($this->post['token'], [
  703. // "order_type" => 'CKD',
  704. // "order_code" => $v_outCode['outCode'],//出库单号
  705. // "order_id" => $v_outCode['id'],
  706. // "order_status" => 1, "before_status" => 0
  707. // ]);
  708. // }
  709. // }
  710. // $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])->find();
  711. // if ($good1 == false) {
  712. // Db::rollback();
  713. // return json_show(1004, "未找到对应商品库存");
  714. // }
  715. // $out_num = intval(array_sum(array_column($out, "send_num")));
  716. // if ($out_num > $good1['usable_stock']) {
  717. // Db::rollback();
  718. // return json_show(1004, "库存数量不足发货");
  719. // }
  720. // $good1['usable_stock'] -= $out_num;
  721. // $good1['wait_out_stock'] += $out_num;
  722. // $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
  723. // $good1['updatetime'] = date("Y-m-d H:i:s");
  724. // $upd = Db::name("good_stock")->save($good1);
  725. // if ($upd == false) {
  726. // Db::rollback();
  727. // return json_show(1004, "库存数量更新失败");
  728. // }
  729. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  730. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  731. // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
  732. // $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
  733. // if ($sendp == false) {
  734. // Db::rollback();
  735. // return json_show(1004, "库存发货失败");
  736. // }
  737. // }
  738. // }
  739. // }
  740. // ActionLog::logAdd($this->post['token'], $order, "CGD", $cgdinfo['status'], $cgdinfo);
  741. // $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status']];
  742. // ProcessOrder::AddProcess($this->post['token'], $process);
  743. // Db::commit();
  744. // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
  745. // } else {
  746. // Db::rollback();
  747. // return json_show(1002, "商品入库失败");
  748. // }
  749. // }
  750. // Db::commit();
  751. // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
  752. // }
  753. // Db::rollback();
  754. // return json_show(1004, "新建失败");
  755. } catch (Exception $e) {
  756. Db::rollback();
  757. return json_show(1004, $e->getMessage());
  758. }
  759. }
  760. //导出
  761. public function exportCgdList()
  762. {
  763. $cgdNos = $this->request->post('cgdNos', [], 'trim');
  764. if (empty($cgdNos)) return json_show(1004, '要导出的采购单编号不能为空');
  765. $i = 1;
  766. $send_type = [1 => '直接发货', 2 => '延时发货'];
  767. $status = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '已取消订单'];
  768. $list = Db::name("purchease_order")
  769. ->alias('po')
  770. ->field('"" as 序号,po.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,s.orderCode as 确认单号,s.addtime as 确认单时间,po.spuCode as 产品编号,po.good_name as 产品名称,"" as 规格,"" as 单位,po.supplier_name as 供应商名称,po.supplierNo as 供应商编号,po.nake_fee as 裸价,po.delivery_fee as 物流费,s.send_type as 发货方式,po.good_price as 采购单价,s.remark as 确认单备注,po.good_num as 采购数量,po.total_fee as 采购货款,b.company as 购买方公司,s.arrive_time as 到货时间,"" 税率,oa.addr 收货地址,oa.mobile 电话,oa.contactor 联系人,s.platform_order 平台订单编号,oa.receipt_quantity 地址发货数量, oa.addr_code,po.order_type')
  771. ->whereIn('po.cgdNo', $cgdNos)
  772. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  773. ->leftJoin('sale s', 's.orderCode=on.orderCode AND s.is_del=0')
  774. ->leftJoin('order_addr oa', 'oa.orderCode=on.orderCode AND oa.is_del=0')
  775. ->leftJoin('business b', 'b.companyNo=s.supplierNo AND b.is_del=0')
  776. ->order("po.addtime desc")
  777. ->withAttr('序号', function () use (&$i) {
  778. return $i++;
  779. })->withAttr('采购单状态', function ($val) use ($status) {
  780. return isset($status[$val]) ? $status[$val] : '';
  781. })->withAttr('发货方式', function ($val) use ($send_type) {
  782. return isset($send_type[$val]) ? $send_type[$val] : '';
  783. })->withAttr('发货方式', function ($val) use ($send_type) {
  784. return isset($send_type[$val]) ? $send_type[$val] : '';
  785. })
  786. ->select()
  787. ->toArray();
  788. foreach ($list as &$value) {
  789. if ($value['order_type'] == 3) {
  790. $temp = Db::name("good_zixun")
  791. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  792. ->field('id,specinfo,good_unit,tax')
  793. ->find();
  794. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  795. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  796. $speclist = [];
  797. foreach ($specinfo as $val) {
  798. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  799. }
  800. $value['税率'] = $temp['tax'] . '%';
  801. } else {
  802. $good_unit = Db::name('good_basic')
  803. ->field('id,good_unit,tax')
  804. ->where(['spuCode' => $value['产品编号']])
  805. ->find();
  806. $spec = Db::name("good_spec")
  807. ->field('id,spec_id,spec_value_id')
  808. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  809. ->select()
  810. ->toArray();
  811. $speclist = [];
  812. if (!empty($spec)) {
  813. foreach ($spec as $val) {
  814. $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', '');
  815. }
  816. }
  817. $value['税率'] = isset($good_unit['tax']) ? $good_unit['tax'] . '%' : '';
  818. }
  819. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  820. $value['单位'] = isset($good_unit['good_unit']) ? Db::name('unit')->where(['id' => $good_unit['good_unit'], 'is_del' => 0])->value('unit', '') : '';
  821. if (!empty($value['addr_code'])) {
  822. $temp = explode(',', $value['addr_code']);
  823. $temp_ = GetAddr(json_encode(['provice_code' => $temp[0], 'city_code' => $temp[1], 'area_code' => $temp[2]]));
  824. $value['收货地址'] = $temp_ . $value['收货地址'];
  825. }
  826. unset($value['addr_code']);
  827. unset($value['order_type']);
  828. }
  829. $headerArr = array_keys($list[0]);
  830. excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
  831. }
  832. }