Order.php 55 KB

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