Order.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. ->field('po.*,on.orderCode')
  129. ->where($where)
  130. ->where(function ($query) use ($where) {
  131. $query->whereOr([['po.order_type', '<>', 1], ['po.order_source', '=', 0]]);
  132. })
  133. ->page($page, $size)
  134. ->order("po.addtime desc")
  135. ->select()
  136. ->toArray();
  137. $all_createrid = array_column($list,'cgder_id');
  138. $item = get_company_name_by_uid($all_createrid);
  139. $data = [];
  140. foreach ($list as $value) {
  141. $value['wsm_name'] = "";
  142. if ($value['wsm_code'] != "") {
  143. $wsmcode = Db::name("warehouse_info")
  144. ->alias("a")
  145. ->leftJoin("supplier b", "a.supplierNo=b.code")
  146. ->where(["a.wsm_code" => $value['wsm_code']])
  147. ->field("a.name as wsm_name,b.name,b.code")
  148. ->find();
  149. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  150. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  151. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  152. }
  153. if ($value['order_type'] == 3) {
  154. $goon = Db::name("good_zixun")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
  155. $value["speclist"] = isset($goon['specinfo']) && $goon['specinfo'] != "" ? json_decode($goon['specinfo'], true) : "";
  156. } else {
  157. $goon = Db::name('good_basic')->where(['spuCode' => $value['spuCode']])->find();
  158. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  159. $speclist = [];
  160. if (!empty($spec)) {
  161. foreach ($spec as $val) {
  162. $temp = [];
  163. $temp['id'] = $val['id'];
  164. $temp['spuCode'] = $val['spuCode'];
  165. $temp['spec_id'] = $val['spec_id'];
  166. $temp['spec_value_id'] = $val['spec_value_id'];
  167. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  168. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  169. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  170. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  171. $speclist[] = $temp;
  172. }
  173. }
  174. $value["speclist"] = empty($speclist) ? [] : $speclist;
  175. }
  176. //采购单详情字段order_type 为1或者2时,取线上商品库 商品创建人 order_type 为3或者4时 取采购反馈的商品库 商品反馈人
  177. // $value['cgder_id'] = $goon['createrid'];
  178. // $value['cgder'] = $goon['creater'];
  179. $inorder = Db::name("purchease_in")->where(['cgdNo' => $value['cgdNo'], "is_del" => 0])->select();
  180. $value['child'] = empty($inorder) ? [] : $inorder;
  181. $value['send_num'] -= $value['th_num'];
  182. $value['total_fee'] = round($value['total_fee'] - $value['th_fee'], 2);
  183. //补充orderCode
  184. // $value['orderCode']=isset($all_orderCodes[$value['cgdNo']])?$all_orderCodes[$value['cgdNo']]:'';
  185. $value['company_name'] = $item[$value['cgder_id']]??'';
  186. $data[] = $value;
  187. }
  188. return json_show(0, "获取成功", ["list" => $data, "count" => $count]);
  189. }
  190. //详情
  191. public function info()
  192. {
  193. $this->post = $this->request->filter('trim')->post();
  194. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
  195. if ($cgdNo == "") {
  196. return json_show(1004, "参数cgdNo 不能为空");
  197. }
  198. //采购单
  199. $data = Db::name("purchease_order")->where(["cgdNo" => $cgdNo, "is_del" => 0, 'supplierNo' => $this->request->user['supplierNo']])->find();
  200. if (empty($data)) {
  201. return json_show(1004, "未找到数据");
  202. }
  203. //采购退货单
  204. $im = Db::name('purchease_back')->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->select();
  205. $var = [];
  206. foreach ($im as $value) {
  207. if ($data['order_type'] == 3) {
  208. $goo = Db::name("good_zixun")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
  209. } else {
  210. $goo = Db::name('good_basic')->where(['spuCode' => $value['spuCode']])->find();
  211. }
  212. if ($goo == false) {
  213. return json_show(1005, "未找到商品数据");
  214. }
  215. $cat = isset($goo['cat_id']) && $goo['cat_id'] != 0 ? made($goo['cat_id']) : [];
  216. $value['cant'] = $cat;
  217. $var[] = $value;
  218. }
  219. //采购工差单
  220. $dom = Db::name("purchease_diff")->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->find();
  221. if ($data['order_type'] == 3) {
  222. $goon = Db::name("good_zixun")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->find();
  223. } else {
  224. $goon = Db::name('good_basic')->where(['spuCode' => $data['spuCode']])->find();
  225. }
  226. if (empty($goon)) {
  227. return json_show(1005, "未找到商品数据");
  228. } else {
  229. $goon['exclusive'] = isset($goon['is_exclusive']) ? makeExcluse($goon['is_exclusive']) : "";
  230. $unit = Db::name("unit")->where(["id" => $goon['good_unit']])->find();
  231. $goon['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  232. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  233. $speclist = [];
  234. if (!empty($spec)) {
  235. foreach ($spec as $value) {
  236. $temp = [];
  237. $temp['id'] = $value['id'];
  238. $temp['spuCode'] = $value['spuCode'];
  239. $temp['spec_id'] = $value['spec_id'];
  240. $temp['spec_value_id'] = $value['spec_value_id'];
  241. $temp['is_del'] = $value['is_del'];
  242. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  243. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  244. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  245. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  246. $speclist[] = $temp;
  247. }
  248. }
  249. $goon["speclist"] = empty($speclist) ? [] : $speclist;
  250. $proof = Db::name("good_proof")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  251. $goon['proof'] = isset($proof) && $proof != false ? $proof : [];
  252. $goon['origin_place_cn'] = "";
  253. $goon['delivery_place_cn'] = "";
  254. if (isset($goon['delivery_place']) && $goon['delivery_place'] !== "") {
  255. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  256. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['delivery_place']);
  257. $goon['delivery_place_cn'] = GetAddr(json_encode($place));
  258. }
  259. if (isset($goon['delivery_place']) && $goon['origin_place'] !== "") {
  260. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  261. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['origin_place']);
  262. $goon['origin_place_cn'] = GetAddr(json_encode($place));
  263. }
  264. if ($goon['brand_id'] != 0) {
  265. $brand = Db::name("brand")->where(["id" => $goon['brand_id']])->find();
  266. $goon["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  267. } else {
  268. $goon["brand_name"] = "";
  269. $goon["brand_id"] = "";
  270. }
  271. $supplier = Db::name("supplier")->where(["code" => $goon['supplierNo']])->find();
  272. $goon['supplierName'] = isset($supplier['name']) ? $supplier['name'] : "";
  273. $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
  274. if (isset($goon['companyNo']) && $goon['companyNo'] != "") {
  275. $company = Db::name("business")->where(["companyNo" => $goon['companyNo']])->find();
  276. }
  277. $goon['company'] = isset($company['company']) ? $company['company'] : "";
  278. }
  279. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  280. $data['wsm_name'] = "";
  281. if ($data['wsm_code'] != "") {
  282. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")
  283. ->leftJoin("warehouse_addr c", "a.wsm_code=c.wsm_code and c.is_del=0")
  284. ->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();
  285. $data['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  286. $data['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  287. $data['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  288. $data['wsm_contactor'] = isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor'] : "";
  289. $data['wsm_mobile'] = isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile'] : "";
  290. // $data['wsm_addr'] =isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  291. $data['addr_code'] = isset($wsmcode['addr_code']) ? $wsmcode['addr_code'] : "";
  292. $data['wsm_addr'] = isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']) : "";
  293. $data['wsm_addr'] .= isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr'] : "";
  294. $inorder = Db::name("purchease_in")->where(['cgdNo' => $data['cgdNo'], "is_del" => 0])->select();
  295. $data['child'] = empty($inorder) ? [] : $inorder;
  296. }
  297. $data['can'] = $int;
  298. $data['goodinfo'] = $goon;
  299. $data['purcheasediff'] = $dom;
  300. $data['send_num'] -= $data['th_num'];
  301. $data['total_fee'] = round($data['total_fee'] - $data['th_fee'], 2);
  302. if (isset($data['companyNo']) && $data['companyNo'] != "") {
  303. $company = Db::name("business")->where(["companyNo" => $data['companyNo']])->find();
  304. }
  305. $data['company'] = isset($company['company']) ? $company['company'] : "";
  306. //$data['info'] = $var;
  307. $data['purcheaseback'] = $var;
  308. //补充orderCode
  309. $data['orderCode'] = Db::name('order_num')
  310. ->where('cgdNo', $data['cgdNo'])
  311. ->value('orderCode', '');
  312. //补充收货地址
  313. $addr = Db::name('order_addr')
  314. ->field('id,addr,addr_code,contactor,mobile,receipt_quantity')
  315. ->where(['orderCode' => $data['orderCode'], 'is_del' => 0])
  316. ->select()
  317. ->toArray();
  318. if (!empty($addr)) {
  319. foreach ($addr as &$vv) {
  320. $temp = explode(',', $vv['addr_code']);
  321. $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] : '']));
  322. $vv['addr_code'] = $temp_ . $vv['addr'];
  323. }
  324. } else $addr = [];
  325. $data['addr_info'] = $addr;
  326. //补充客户名称
  327. if (empty($data['orderCode'])) $data['customer'] = [];
  328. else {
  329. $data['customer'] = Db::name('sale')
  330. ->alias('s')
  331. ->field('c.companyNo,c.companyName')
  332. ->leftJoin('customer_info c', 'c.companyNo=s.customer_code AND c.is_del=0')
  333. ->where(['s.orderCode' => $data['orderCode']])
  334. ->find();
  335. }
  336. return json_show(0, "获取成功", $data);
  337. }
  338. //订单确认
  339. public function status()
  340. {
  341. $this->post = $this->request->filter('trim')->post();
  342. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? $this->post['cgdNo'] : [];
  343. if (empty($cgdNo)) return json_show(1004, "参数cgdNo 不能为空");
  344. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  345. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  346. if ($status === "") return json_show(1004, "参数status 不能为空");
  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)) return json_show(1004, $data['cgdNo'] . "采购单发货中无法取消");
  355. }
  356. //原始数据
  357. $info = Db::name("purchease_order")
  358. ->whereIn('cgdNo', $cgdNo)
  359. ->where('status', 0)
  360. ->column('id,cgdNo,status,supplierNo,spuCode,order_type', 'cgdNo');
  361. if (empty($info)) return json_show(1005, '没有可供确认的订单');
  362. Db::startTrans();
  363. try {
  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. $isreturn = cgd_sale_return($vlue);
  382. if($isreturn){
  383. throw new Exception("{$vlue}关联销售单存在退货单正在进行");
  384. }
  385. // if($status==1 || $status==2){
  386. // $orderinfo = Db::name('sale')
  387. // ->alias('a')
  388. // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid')
  389. // ->leftJoin('order_num b','b.orderCode=a.orderCode')
  390. // ->where([
  391. // 'a.is_del'=>0,
  392. // 'b.cgdNo'=>$vlue,
  393. // ])->findOrEmpty();
  394. // $is_stock = Db::name('good_basic')
  395. // ->where(['is_del' => 0, 'spuCode' => $info[$vlue]['spuCode']])
  396. // ->value('is_stock');
  397. // if ($is_stock == 1) {
  398. //库存品,推给31库管人员、41库管-张凯旋
  399. // $uids = Db::name('user_role')
  400. // ->where([
  401. // ['is_del', '=', 0],
  402. // ['roleid', 'in', [31, 41]],
  403. // ['status', '=', 1]
  404. // ])->column('uid');
  405. // if (!in_array($uid, $uids)) throw new Exception('库存品订单只能由库管人员操作');
  406. // $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)];
  407. // } else {
  408. //非库存品和采返商品,推给供应商负责人(还得是没开通账号的供应商)
  409. // $res = SupplierRelationUserModel::field('id')
  410. // ->where(['is_del' => SupplierUserModel::$is_del_normal, 'supplierNo' => $info[$vlue]['supplierNo'], 'status' => SupplierUserModel::$status_normal])
  411. // ->findOrEmpty()
  412. // ->isEmpty();
  413. // if (!$res) throw new Exception('供应商已开通账号,只能由供应商端操作');
  414. // if ($uid != $supplier_cgderid[$info[$vlue]['supplierNo']]) throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  415. // $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];
  416. // }
  417. // }//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];
  418. $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];
  419. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], $process);
  420. $order = ["order_code" => $vlue, "status" => '', "action_remark" => $remark, "action_type" => "status"];
  421. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], $order, 'CGD', $status, $this->post);
  422. }
  423. Db::commit();
  424. return json_show(0, "更新成功");
  425. } else throw new Exception('更新失败');
  426. } catch (Exception $exception) {
  427. Db::rollback();
  428. return json_show(1004, $exception->getMessage());
  429. }
  430. }
  431. //批量入库
  432. public function add()
  433. {
  434. $param = $this->request->filter('trim')->only(['cgdNo'], 'post');
  435. $val = Validate::rule(['cgdNo|订单编号' => 'require|array|max:100']);
  436. //入库方式 供应商包邮,
  437. //运费 0
  438. if (!$val->check($param)) return json_show(1004, $val->getError());
  439. $cgdinfo = Db::name("purchease_order")
  440. ->whereIn('cgdNo', $param['cgdNo'])
  441. ->where(['is_del' => 0, 'supplierNo' => $this->request->user['supplierNo']])
  442. ->where('status', '=', 1)
  443. ->column('id,wsm_code,good_num,good_price,spuCode,order_type,send_status,status,supplierNo', 'cgdNo');
  444. if (empty($cgdinfo)) return json_show(1005, "未找到采购单数据或采购单未发货");
  445. $all_wsm_code = array_column($cgdinfo, 'wsm_code');
  446. $recep = Db::name('warehouse_addr')
  447. ->where('is_del', 0)
  448. ->whereIn('wsm_code', $all_wsm_code)
  449. ->column('wsm_name', 'wsm_code');
  450. $uid = $this->request->user['uid'];
  451. $nickname = $this->request->user['nickname'];
  452. Db::startTrans();
  453. try {
  454. $date = date('Y-m-d H:i:s');
  455. $supplier_cgderid = Db::name('supplier')
  456. ->where('is_del',0)
  457. ->whereIn('code',array_column($cgdinfo,'supplierNo'))
  458. ->column('personid,person','code');
  459. $i = 0;
  460. $status = 4;//4入库完成
  461. foreach ($param['cgdNo'] as $cgdNo) {
  462. $isreturn = cgd_sale_return($cgdNo);
  463. if($isreturn){
  464. throw new Exception("{$cgdNo}关联销售单存在退货单正在进行");
  465. }
  466. if (empty($cgdinfo[$cgdNo])) throw new Exception('未找到采购单数据或采购单未发货');
  467. $wsm_in_code = makeNo("CF");
  468. $bn_code = makeNo("BN");
  469. $send_num = $cgdinfo[$cgdNo]['good_num'];
  470. //改变编码规则,将原编码后两位换成序列号
  471. //str_pad字符串填充
  472. $wsm_in_code = substr($wsm_in_code, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  473. $i++;
  474. //入库单
  475. $win = Db::name("purchease_in")->insertGetId([
  476. "wsm_in_code" => $wsm_in_code,
  477. "cgdNo" => $cgdNo,
  478. "bnCode"=>$bn_code,
  479. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  480. "wsm_reaper" => isset($recep[$cgdinfo[$cgdNo]['wsm_code']]) ? $recep[$cgdinfo[$cgdNo]['wsm_code']] : '',
  481. "send_num" => $cgdinfo[$cgdNo]['good_num'],
  482. "post_company" => '',
  483. "post_code" => '',
  484. "post_fee" => 0,//运费固定值:0
  485. "sendtime" => $date,
  486. "apply_id" => $this->request->user['uid'],
  487. "apply_name" => $this->request->user['nickname'],
  488. "wait_num" => 0,
  489. "wsm_num" => 0,
  490. "sendtype" => 2,//发货方式,固定值,供应商包邮2
  491. "status" => $status,
  492. "addtime" => $date,
  493. "updatetime" => $date
  494. ]);
  495. if ($win) {
  496. $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
  497. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $sto, "RKD", $status, $this->request->filter('trim')->post());
  498. $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status,'holder_id' => $this->request->user['uid']];
  499. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  500. $order = ["order_code" => $cgdNo, "status" => $cgdinfo[$cgdNo]['status'], "action_remark" => '', "action_type" => "edit"];
  501. //维护台账信息
  502. Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . $date . "' WHERE `cgdNo`='{$cgdNo}'");
  503. //////////////////
  504. $good = Db::name("good_stock")
  505. ->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])
  506. ->lock(true)
  507. ->find();
  508. if (empty($good)) {
  509. $good = [
  510. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  511. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  512. "usable_stock" => 0,
  513. "wait_out_stock" => 0,
  514. "wait_in_stock" => $send_num,
  515. "total_stock" => 0,
  516. "addtime" => date("Y-m-d H:i:s"),
  517. "updatetime" => date("Y-m-d H:i:s"),
  518. ];
  519. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "create"];
  520. } else {
  521. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "edit"];
  522. }
  523. $good['wait_in_stock'] -= $send_num;
  524. // if ($good['presale_stock'] > 0) {
  525. // if ($good['presale_stock'] >= $send_num) {
  526. // $good['presale_stock'] -= $send_num;
  527. // $good['wait_out_stock'] += $send_num;
  528. // } else {
  529. // $good['presale_stock'] = 0;
  530. // $good['usable_stock'] += $send_num - $good['presale_stock'];
  531. // $good['wait_out_stock'] += $good['presale_stock'];
  532. // }
  533. // } else {
  534. $good['usable_stock'] += $send_num;
  535. // }
  536. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  537. $good['updatetime'] = date("Y-m-d H:i:s");
  538. $upd = Db::name("good_stock")->save($good);
  539. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  540. //商品变动日志表,good_log_code字段存储采购单单号
  541. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" =>$stockid, "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  542. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" =>$stockid, "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
  543. if ($upd) {
  544. GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
  545. if ($cgdinfo[$cgdNo]['order_type'] == 1) {
  546. $bnin=GoodStockInfo::AddBn($stockid,$bn_code,$send_num,$cgdinfo[$cgdNo]['good_price']);
  547. if($bnin==false){
  548. Db::rollback();
  549. return error_show(1004,"库存bn数据新增失败");
  550. }
  551. $bk = Db::name("order_bk")->where(['cgdNo' => $cgdNo])->find();
  552. if ($bk == false) {
  553. $bk = [
  554. "cgdNo" => $cgdNo,
  555. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  556. "companyNo" => $cgdinfo[$cgdNo]['companyNo'],
  557. "total_num" => $send_num,
  558. "merge_num" => 0,
  559. "balance_num" => $send_num,
  560. "status" => 1,
  561. "is_del" => 0,
  562. 'addtime' => date("Y-m-d H:i:s"),
  563. "updatetime" => date("Y-m-d H:i:s")
  564. ];
  565. $upcgd = Db::name("order_bk")->save($bk);
  566. } else {
  567. $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
  568. $bk['balance_num'] += $send_num;
  569. $bk['total_num'] += $send_num;
  570. $bk['updatetime'] = date("Y-m-d H:i:s");
  571. $upcgd = Db::name("order_bk")->where($where)->update($bk);
  572. }
  573. if ($upcgd == false) throw new Exception('采购单数量更新失败');
  574. }
  575. if ($cgdinfo[$cgdNo]['send_status'] == 1 && $cgdinfo[$cgdNo]['order_type'] != 1) {
  576. $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
  577. if (!empty($send)) {
  578. $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()->toArray();
  579. if (!empty($out)) {
  580. $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')]);
  581. if ($up == false) throw new Exception('出库单出库失败');
  582. else {
  583. //查询是否有非库存品订单
  584. $is_stock = Db::name('sale')
  585. ->where([
  586. ['is_del','=',0],
  587. ['orderCode','in',array_column($out,'orderCode')]
  588. ])->column('is_stock','orderCode');
  589. foreach ($out as $k => $v_outCode) {
  590. //修改状态,添加待办
  591. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], [
  592. "order_code" => $v_outCode['outCode'],//出库单号
  593. "status" => 0,//这里的status是之前的值
  594. "action_remark" => '',//备注
  595. "action_type" => "status"//新建create,编辑edit,更改状态status
  596. ], "CKD", 1, $param);
  597. //库存品,推给库管和库管-张凯旋
  598. if($is_stock[$v_outCode['orderCode']] == 1){
  599. $roleid = config('app.wsm_cgder_role');
  600. $uids = Db::name('user_role')
  601. ->where('is_del', 0)
  602. ->whereIn('roleid', $roleid)
  603. ->column('uid');
  604. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
  605. "order_type" => 'CKD',
  606. "order_code" => $v_outCode['outCode'],//出库单号
  607. "order_id" => $v_outCode['id'],
  608. "order_status" => 1,
  609. "before_status" => 0,
  610. // 'wait_id' => $wait['personid'],
  611. // 'wait_name' => $wait['person'],
  612. // 'holder_id' => $wait['personid'],
  613. 'handle_user_list' => implode(',', $uids)
  614. ]);
  615. }else{
  616. //推给供应商负责人
  617. $wait = $supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']];
  618. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
  619. "order_type" => 'CKD',
  620. "order_code" => $v_outCode['outCode'],//出库单号
  621. "order_id" => $v_outCode['id'],
  622. "order_status" => 1,
  623. "before_status" => 0,
  624. 'wait_id' => $wait['personid'],
  625. 'wait_name' => $wait['person'],
  626. 'holder_id' => $wait['personid'],
  627. ]);
  628. }
  629. }
  630. }
  631. $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])->find();
  632. if ($good1 == false) throw new Exception('未找到对应商品库存');
  633. $out_num = intval(array_sum(array_column($out, "send_num")));
  634. if ($out_num > $good1['usable_stock']) throw new Exception('库存数量不足发货');
  635. $good1['usable_stock'] -= $out_num;
  636. $good1['wait_out_stock'] += $out_num;
  637. $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
  638. $good1['updatetime'] = date("Y-m-d H:i:s");
  639. $upd = Db::name("good_stock")->save($good1);
  640. if ($upd == false) throw new Exception('库存数量更新失败');
  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) {
  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. foreach ($specinfo as $val) {
  883. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  884. }
  885. $value['税率'] = $temp['tax'] . '%';
  886. } else {
  887. $good_unit = Db::name('good_basic')
  888. ->field('id,good_unit,tax')
  889. ->where(['spuCode' => $value['产品编号']])
  890. ->find();
  891. $spec = Db::name("good_spec")
  892. ->field('id,spec_id,spec_value_id')
  893. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  894. ->select()
  895. ->toArray();
  896. $speclist = [];
  897. if (!empty($spec)) {
  898. foreach ($spec as $val) {
  899. $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', '');
  900. }
  901. }
  902. $value['税率'] = isset($good_unit['tax']) ? $good_unit['tax'] . '%' : '';
  903. }
  904. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  905. $value['单位'] = isset($good_unit['good_unit']) ? Db::name('unit')->where(['id' => $good_unit['good_unit'], 'is_del' => 0])->value('unit', '') : '';
  906. if (!empty($value['addr_code'])) {
  907. $temp = explode(',', $value['addr_code']);
  908. $temp_ = GetAddr(json_encode(['provice_code' => $temp[0], 'city_code' => $temp[1], 'area_code' => $temp[2]]));
  909. $value['收货地址'] = $temp_ . $value['收货地址'];
  910. }
  911. unset($value['addr_code']);
  912. unset($value['order_type']);
  913. }
  914. $headerArr = array_keys($list[0]);
  915. excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
  916. }
  917. }