Order.php 59 KB

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