Order.php 58 KB

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