Order.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. ->column('id,cgdNo,status', 'cgdNo');
  362. $upd = Db::name("purchease_order")
  363. ->whereIn('cgdNo', $cgdNo)
  364. ->save([
  365. 'status' => $status,
  366. 'remark' => $remark,
  367. 'updatetime' => date("Y-m-d H:i:s"),
  368. ]);
  369. if ($upd) {
  370. // $user=GetUserInfo($this->post['token']);
  371. $uid = $this->request->user['uid']; //isset($user['data']['id'])?$user['data']['id']:0;
  372. $uname = $this->request->user['nickname'];//isset($user['data']['nickname'])?$user['data']['nickname']:'';
  373. foreach ($cgdNo as $vlue) {
  374. $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];
  375. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], $process);
  376. $order = ["order_code" => $vlue, "status" => '', "action_remark" => $remark, "action_type" => "status"];
  377. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], $order, 'CGD', $status, $this->post);
  378. }
  379. return json_show(0, "更新成功");
  380. } else return json_show(1004, '更新失败');
  381. }
  382. //批量入库
  383. public function add()
  384. {
  385. $param = $this->request->filter('trim')->only(['cgdNo'], 'post');
  386. $val = Validate::rule(['cgdNo|订单编号' => 'require|array|max:100']);
  387. //入库方式 供应商包邮,
  388. //运费 0
  389. if (!$val->check($param)) return json_show(1004, $val->getError());
  390. $cgdinfo = Db::name("purchease_order")
  391. ->whereIn('cgdNo', $param['cgdNo'])
  392. ->where(['is_del' => 0, 'supplierNo' => $this->request->user['supplierNo']])
  393. ->where('status', '=', 1)
  394. ->column('id,wsm_code,good_num,good_price,spuCode,order_type,send_status,status', 'cgdNo');
  395. if (empty($cgdinfo)) return json_show(1005, "未找到采购单数据或采购单未发货");
  396. $all_wsm_code = array_column($cgdinfo, 'wsm_code');
  397. $recep = Db::name('warehouse_addr')
  398. ->where('is_del', 0)
  399. ->whereIn('wsm_code', $all_wsm_code)
  400. ->column('wsm_name', 'wsm_code');
  401. $uid = $this->request->user['uid'];
  402. $nickname = $this->request->user['nickname'];
  403. Db::startTrans();
  404. try {
  405. $date = date('Y-m-d H:i:s');
  406. $i = 0;
  407. $status = 4;//4入库完成
  408. foreach ($param['cgdNo'] as $cgdNo) {
  409. if (empty($cgdinfo[$cgdNo])) throw new Exception('未找到采购单数据或采购单未发货');
  410. $wsm_in_code = makeNo("CF");
  411. $bn_code = makeNo("BN");
  412. $send_num = $cgdinfo[$cgdNo]['good_num'];
  413. //改变编码规则,将原编码后两位换成序列号
  414. //str_pad字符串填充
  415. $wsm_in_code = substr($wsm_in_code, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  416. $i++;
  417. //入库单
  418. $win = Db::name("purchease_in")->insertGetId([
  419. "wsm_in_code" => $wsm_in_code,
  420. "cgdNo" => $cgdNo,
  421. "bnCode"=>$bn_code,
  422. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  423. "wsm_reaper" => isset($recep[$cgdinfo[$cgdNo]['wsm_code']]) ? $recep[$cgdinfo[$cgdNo]['wsm_code']] : '',
  424. "send_num" => $cgdinfo[$cgdNo]['good_num'],
  425. "post_company" => '',
  426. "post_code" => '',
  427. "post_fee" => 0,//运费固定值:0
  428. "sendtime" => $date,
  429. "apply_id" => $this->request->user['uid'],
  430. "apply_name" => $this->request->user['nickname'],
  431. "wait_num" => 0,
  432. "wsm_num" => 0,
  433. "sendtype" => 2,//发货方式,固定值,供应商包邮2
  434. "status" => $status,
  435. "addtime" => $date,
  436. "updatetime" => $date
  437. ]);
  438. if ($win) {
  439. $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
  440. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $sto, "RKD", $status, $this->request->filter('trim')->post());
  441. $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status];
  442. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  443. $order = ["order_code" => $cgdNo, "status" => $cgdinfo[$cgdNo]['status'], "action_remark" => '', "action_type" => "edit"];
  444. //维护台账信息
  445. Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . $date . "' WHERE `cgdNo`='{$cgdNo}'");
  446. //////////////////
  447. $good = Db::name("good_stock")
  448. ->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])
  449. ->lock(true)
  450. ->find();
  451. if (empty($good)) {
  452. $good = [
  453. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  454. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  455. "usable_stock" => 0,
  456. "wait_out_stock" => 0,
  457. "wait_in_stock" => $send_num,
  458. "total_stock" => 0,
  459. "addtime" => date("Y-m-d H:i:s"),
  460. "updatetime" => date("Y-m-d H:i:s"),
  461. ];
  462. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "create"];
  463. } else {
  464. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "edit"];
  465. }
  466. $good['wait_in_stock'] -= $send_num;
  467. // if ($good['presale_stock'] > 0) {
  468. // if ($good['presale_stock'] >= $send_num) {
  469. // $good['presale_stock'] -= $send_num;
  470. // $good['wait_out_stock'] += $send_num;
  471. // } else {
  472. // $good['presale_stock'] = 0;
  473. // $good['usable_stock'] += $send_num - $good['presale_stock'];
  474. // $good['wait_out_stock'] += $good['presale_stock'];
  475. // }
  476. // } else {
  477. $good['usable_stock'] += $send_num;
  478. // }
  479. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  480. $good['updatetime'] = date("Y-m-d H:i:s");
  481. $upd = Db::name("good_stock")->save($good);
  482. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  483. //商品变动日志表,good_log_code字段存储采购单单号
  484. $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"];
  485. $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"];
  486. if ($upd) {
  487. GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
  488. if ($cgdinfo[$cgdNo]['order_type'] == 1) {
  489. $bnin=GoodStockInfo::AddBn($stockid,$bn_code,$send_num,$cgdinfo[$cgdNo]['good_price']);
  490. if($bnin==false){
  491. Db::rollback();
  492. return error_show(1004,"库存bn数据新增失败");
  493. }
  494. $bk = Db::name("order_bk")->where(['cgdNo' => $cgdNo])->find();
  495. if ($bk == false) {
  496. $bk = [
  497. "cgdNo" => $cgdNo,
  498. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  499. "companyNo" => $cgdinfo[$cgdNo]['companyNo'],
  500. "total_num" => $send_num,
  501. "merge_num" => 0,
  502. "balance_num" => $send_num,
  503. "status" => 1,
  504. "is_del" => 0,
  505. 'addtime' => date("Y-m-d H:i:s"),
  506. "updatetime" => date("Y-m-d H:i:s")
  507. ];
  508. $upcgd = Db::name("order_bk")->save($bk);
  509. } else {
  510. $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
  511. $bk['balance_num'] += $send_num;
  512. $bk['total_num'] += $send_num;
  513. $bk['updatetime'] = date("Y-m-d H:i:s");
  514. $upcgd = Db::name("order_bk")->where($where)->update($bk);
  515. }
  516. if ($upcgd == false) throw new Exception('采购单数量更新失败');
  517. }
  518. if ($cgdinfo[$cgdNo]['send_status'] == 1 && $cgdinfo[$cgdNo]['order_type'] != 1) {
  519. $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
  520. if (!empty($send)) {
  521. $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()->toArray();
  522. if (!empty($out)) {
  523. $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')]);
  524. if ($up == false) throw new Exception('出库单出库失败');
  525. else {
  526. foreach ($out as $k => $v_outCode) {
  527. //修改状态,添加待办
  528. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], [
  529. "order_code" => $v_outCode['outCode'],//出库单号
  530. "status" => 0,//这里的status是之前的值
  531. "action_remark" => '',//备注
  532. "action_type" => "status"//新建create,编辑edit,更改状态status
  533. ], "CKD", 1, $param);
  534. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
  535. "order_type" => 'CKD',
  536. "order_code" => $v_outCode['outCode'],//出库单号
  537. "order_id" => $v_outCode['id'],
  538. "order_status" => 1,
  539. "before_status" => 0
  540. ]);
  541. }
  542. }
  543. $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])->find();
  544. if ($good1 == false) throw new Exception('未找到对应商品库存');
  545. $out_num = intval(array_sum(array_column($out, "send_num")));
  546. if ($out_num > $good1['usable_stock']) throw new Exception('库存数量不足发货');
  547. $good1['usable_stock'] -= $out_num;
  548. $good1['wait_out_stock'] += $out_num;
  549. $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
  550. $good1['updatetime'] = date("Y-m-d H:i:s");
  551. $upd = Db::name("good_stock")->save($good1);
  552. if ($upd == false) throw new Exception('库存数量更新失败');
  553. $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"];
  554. $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"];
  555. GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
  556. $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
  557. if ($sendp == false) throw new Exception('库存发货失败');
  558. }
  559. }
  560. }
  561. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $order, "CGD", $cgdinfo[$cgdNo]['status'], $cgdinfo[$cgdNo]);
  562. $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo[$cgdNo]['id'], "order_status" => $cgdinfo[$cgdNo]['status'], "order_type" => 'CGD', "before_status" => $order['status']];
  563. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  564. // Db::commit();
  565. // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
  566. } else throw new Exception('商品入库失败');
  567. //////////////////
  568. } else throw new Exception('新建入库单失败');
  569. }
  570. //维护原有采购单信息
  571. Db::name("purchease_order")
  572. ->whereIn('id', array_column($cgdinfo, 'id'))
  573. ->update([
  574. 'wsend_num' => 0,
  575. 'send_num' => Db::raw('good_num'),
  576. 'send_status' => 3,
  577. 'status' => 3,
  578. 'updatetime' => date("Y-m-d H:i:s"),
  579. ]);
  580. Db::commit();
  581. return json_show(0, '新建入库单成功');
  582. // $win = Db::name("purchease_in")->insert($orin, true);
  583. // if ($win) {
  584. // $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
  585. // ActionLog::logAdd($this->post['token'], $sto, "RKD", $status, $this->post);
  586. // $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status];
  587. // ProcessOrder::AddProcess($this->post['token'], $process);
  588. // $order = ["order_code" => $cgdNo, "status" => $cgdinfo['status'], "action_remark" => '', "action_type" => "edit"];
  589. //维护台账信息
  590. // 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}'");
  591. // if ($status == 1) {
  592. // Db::commit();
  593. // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
  594. // }
  595. // if ($status == 4) {
  596. // $cgdinfo['wsend_num'] -= $send_num;
  597. // $cgdinfo['send_num'] += $send_num;
  598. // $cgdinfo['send_status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
  599. // $cgdinfo['status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
  600. // $cgdinfo['updatetime'] = date("Y-m-d H:i:s");
  601. // $up = Db::name("purchease_order")->save($cgdinfo);
  602. // if ($up == false) {
  603. // Db::rollback();
  604. // return json_show(1004, "新建失败");
  605. // }
  606. // $good = Db::name("good_stock")
  607. // ->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])
  608. // ->lock(true)
  609. // ->find();
  610. // if (empty($good)) {
  611. // $good = [
  612. // "spuCode" => $cgdinfo['spuCode'],
  613. // "wsm_code" => $cgdinfo['wsm_code'],
  614. // "usable_stock" => 0,
  615. // "wait_out_stock" => 0,
  616. // "wait_in_stock" => $send_num,
  617. // "total_stock" => 0,
  618. // "addtime" => date("Y-m-d H:i:s"),
  619. // "updatetime" => date("Y-m-d H:i:s"),
  620. // ];
  621. // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  622. // // "action_type"=>"create"];
  623. // } else {
  624. // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  625. // // "action_type"=>"edit"];
  626. // }
  627. // $good['wait_in_stock'] -= $send_num;
  628. // if ($good['presale_stock'] > 0) {
  629. // if ($good['presale_stock'] >= $send_num) {
  630. // $good['presale_stock'] -= $send_num;
  631. // $good['wait_out_stock'] += $send_num;
  632. // } else {
  633. // $good['presale_stock'] = 0;
  634. // $good['usable_stock'] += $send_num - $good['presale_stock'];
  635. // $good['wait_out_stock'] += $good['presale_stock'];
  636. // }
  637. // } else {
  638. // $good['usable_stock'] += $send_num;
  639. // }
  640. //
  641. // $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  642. // $good['updatetime'] = date("Y-m-d H:i:s");
  643. // $upd = Db::name("good_stock")->save($good);
  644. // //商品变动日志表,good_log_code字段存储采购单单号
  645. // $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"];
  646. // $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"];
  647. // if ($upd) {
  648. // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
  649. // if ($cgdinfo['order_type'] == 1) {
  650. // $bk = Db::name("order_bk")->where(['cgdNo' => $cgdinfo['cgdNo']])->find();
  651. // if ($bk == false) {
  652. // $bk = [
  653. // "cgdNo" => $cgdinfo['cgdNo'],
  654. // "spuCode" => $cgdinfo['spuCode'],
  655. // "companyNo" => $cgdinfo['companyNo'],
  656. // "total_num" => $send_num,
  657. // "merge_num" => 0,
  658. // "balance_num" => $send_num,
  659. // "status" => 1,
  660. // "is_del" => 0,
  661. // 'addtime' => date("Y-m-d H:i:s"),
  662. // "updatetime" => date("Y-m-d H:i:s")
  663. // ];
  664. // $upcgd = Db::name("order_bk")->save($bk);
  665. // } else {
  666. // $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
  667. // $bk['balance_num'] += $send_num;
  668. // $bk['total_num'] += $send_num;
  669. // $bk['updatetime'] = date("Y-m-d H:i:s");
  670. // $upcgd = Db::name("order_bk")->where($where)->update($bk);
  671. // }
  672. //
  673. // if ($upcgd == false) {
  674. // Db::rollback();
  675. // return json_show(1004, "采购单数量更新失败");
  676. // }
  677. // }
  678. // if ($cgdinfo['send_status'] == 3 && $cgdinfo['order_type'] != 1) {
  679. // $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
  680. // if (!empty($send)) {
  681. // $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()
  682. // ->toArray();
  683. // if (!empty($out)) {
  684. // $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->update
  685. // (["status" => 1, 'wsm_code' => $cgdinfo['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
  686. // if ($up == false) {
  687. // Db::rollback();
  688. // return json_show(1004, "出库单出库失败");
  689. // } else {
  690. // foreach ($out as $k => $v_outCode) {
  691. // //修改状态,添加待办
  692. // ActionLog::logAdd($this->post['token'], [
  693. // "order_code" => $v_outCode['outCode'],//出库单号
  694. // "status" => 0,//这里的status是之前的值
  695. // "action_remark" => '',//备注
  696. // "action_type" => "status"//新建create,编辑edit,更改状态status
  697. // ], "CKD", 1, $this->post);
  698. //
  699. // ProcessOrder::AddProcess($this->post['token'], [
  700. // "order_type" => 'CKD',
  701. // "order_code" => $v_outCode['outCode'],//出库单号
  702. // "order_id" => $v_outCode['id'],
  703. // "order_status" => 1, "before_status" => 0
  704. // ]);
  705. // }
  706. // }
  707. // $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])->find();
  708. // if ($good1 == false) {
  709. // Db::rollback();
  710. // return json_show(1004, "未找到对应商品库存");
  711. // }
  712. // $out_num = intval(array_sum(array_column($out, "send_num")));
  713. // if ($out_num > $good1['usable_stock']) {
  714. // Db::rollback();
  715. // return json_show(1004, "库存数量不足发货");
  716. // }
  717. // $good1['usable_stock'] -= $out_num;
  718. // $good1['wait_out_stock'] += $out_num;
  719. // $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
  720. // $good1['updatetime'] = date("Y-m-d H:i:s");
  721. // $upd = Db::name("good_stock")->save($good1);
  722. // if ($upd == false) {
  723. // Db::rollback();
  724. // return json_show(1004, "库存数量更新失败");
  725. // }
  726. // $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"];
  727. // $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"];
  728. // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
  729. // $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
  730. // if ($sendp == false) {
  731. // Db::rollback();
  732. // return json_show(1004, "库存发货失败");
  733. // }
  734. // }
  735. // }
  736. // }
  737. // ActionLog::logAdd($this->post['token'], $order, "CGD", $cgdinfo['status'], $cgdinfo);
  738. // $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status']];
  739. // ProcessOrder::AddProcess($this->post['token'], $process);
  740. // Db::commit();
  741. // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
  742. // } else {
  743. // Db::rollback();
  744. // return json_show(1002, "商品入库失败");
  745. // }
  746. // }
  747. // Db::commit();
  748. // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
  749. // }
  750. // Db::rollback();
  751. // return json_show(1004, "新建失败");
  752. } catch (Exception $e) {
  753. Db::rollback();
  754. return json_show(1004, $e->getMessage());
  755. }
  756. }
  757. //导出
  758. public function exportCgdList()
  759. {
  760. $cgdNos = $this->request->post('cgdNos', [], 'trim');
  761. if (empty($cgdNos)) return json_show(1004, '要导出的采购单编号不能为空');
  762. $i = 1;
  763. $send_type = [1 => '直接发货', 2 => '延时发货'];
  764. $status = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '已取消订单'];
  765. $list = Db::name("purchease_order")
  766. ->alias('po')
  767. ->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')
  768. ->whereIn('po.cgdNo', $cgdNos)
  769. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  770. ->leftJoin('sale s', 's.orderCode=on.orderCode AND s.is_del=0')
  771. ->leftJoin('order_addr oa', 'oa.orderCode=on.orderCode AND oa.is_del=0')
  772. ->leftJoin('business b', 'b.companyNo=s.supplierNo AND b.is_del=0')
  773. ->order("po.addtime desc")
  774. ->withAttr('序号', function () use (&$i) {
  775. return $i++;
  776. })->withAttr('采购单状态', function ($val) use ($status) {
  777. return isset($status[$val]) ? $status[$val] : '';
  778. })->withAttr('发货方式', function ($val) use ($send_type) {
  779. return isset($send_type[$val]) ? $send_type[$val] : '';
  780. })->withAttr('发货方式', function ($val) use ($send_type) {
  781. return isset($send_type[$val]) ? $send_type[$val] : '';
  782. })
  783. ->select()
  784. ->toArray();
  785. foreach ($list as &$value) {
  786. if ($value['order_type'] == 3) {
  787. $temp = Db::name("good_zixun")
  788. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  789. ->field('id,specinfo,good_unit,tax')
  790. ->find();
  791. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  792. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  793. $speclist = [];
  794. foreach ($specinfo as $val) {
  795. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  796. }
  797. $value['税率'] = $temp['tax'] . '%';
  798. } else {
  799. $good_unit = Db::name('good_basic')
  800. ->field('id,good_unit,tax')
  801. ->where(['spuCode' => $value['产品编号']])
  802. ->find();
  803. $spec = Db::name("good_spec")
  804. ->field('id,spec_id,spec_value_id')
  805. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  806. ->select()
  807. ->toArray();
  808. $speclist = [];
  809. if (!empty($spec)) {
  810. foreach ($spec as $val) {
  811. $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', '');
  812. }
  813. }
  814. $value['税率'] = isset($good_unit['tax']) ? $good_unit['tax'] . '%' : '';
  815. }
  816. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  817. $value['单位'] = isset($good_unit['good_unit']) ? Db::name('unit')->where(['id' => $good_unit['good_unit'], 'is_del' => 0])->value('unit', '') : '';
  818. if (!empty($value['addr_code'])) {
  819. $temp = explode(',', $value['addr_code']);
  820. $temp_ = GetAddr(json_encode(['provice_code' => $temp[0], 'city_code' => $temp[1], 'area_code' => $temp[2]]));
  821. $value['收货地址'] = $temp_ . $value['收货地址'];
  822. }
  823. unset($value['addr_code']);
  824. unset($value['order_type']);
  825. }
  826. $headerArr = array_keys($list[0]);
  827. excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
  828. }
  829. }