12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037 |
- <?php
- namespace app\abutment\controller;
- use app\abutment\model\SupplierRelationUser as SupplierRelationUserModel;
- use app\abutment\model\SupplierUser as SupplierUserModel;
- use app\admin\common\User;
- use app\admin\model\ActionLog;
- use app\admin\model\GoodLog;
- use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- //销售订单(对应于采销端的采购单)
- class Order extends HomeBaseController
- {
- //列表
- public function getList()
- {
- $this->post = $this->request->filter('trim')->post();
- $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
- $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
- $where = [['po.is_del', "=", 0], ['po.supplierNo', '=', $this->request->user['supplierNo']], ['s.is_stock', '=', 0]];
- $bkcode = isset($this->post['bk_code']) && $this->post['bk_code'] != "" ? trim($this->post['bk_code']) : "";
- if ($bkcode != "") {
- $where[] = ['po.bkcode', "like", "%{$bkcode}%"];
- }
- $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
- if ($status !== "") {
- // $where['status'] = $status;
- $where[] = ['po.status', "=", $status];
- }
- $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
- if ($cgdNo != "") {
- //$where['cgdNo'] = Db::Raw("like '%{$cgdNo}%'");
- $where[] = ['po.cgdNo', "like", "%{$cgdNo}%"];
- }
- $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] != "" ? trim($this->post['apply_name']) : "";
- if ($apply_name != "") {
- // $where['cgder'] =Db::Raw("like '%{$apply_name}%'");
- $where[] = ['po.cgder', "like", "%{$apply_name}%"];
- }
- $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] != "" ? trim($this->post['wsm_code']) : "";
- if ($wsm_code != "") {
- // $where['wsm_code'] = $wsm_code;
- $where[] = ['po.wsm_code', "=", $wsm_code];
- }
- $wsm_in_code = isset($this->post['wsm_in_code']) && $this->post['wsm_in_code'] != "" ? trim($this->post['wsm_in_code']) : "";
- if ($wsm_in_code != "") {
- $incode = Db::name("purchease_in")
- ->where(["wsm_in_code" => Db::raw(" like %{$wsm_in_code}%"), "is_del" => 0])
- ->column("cgdNo");
- if (empty($incode)) {
- return json_show(1005, "未找到有关入库单信息");
- }
- $where[] = ['po.cgdNo', "in", $incode];
- }
- $good_name = isset($this->post['good_name']) && $this->post['good_name'] != "" ? trim($this->post['good_name']) : "";
- if ($good_name != "") {
- //$where['good_name'] = Db::raw(" like %{$good_name}%");
- $where[] = ['po.good_name', "like", "%{$good_name}%"];
- }
- $good_code = isset($this->post['good_code']) && $this->post['good_code'] != "" ? trim($this->post['good_code']) : "";
- if ($good_code != "") {
- // $where['good_code'] = Db::raw(" like %{$good_code}%");
- $where[] = ['po.spuCode', "like", "%{$good_code}%"];
- }
- $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
- if ($supplierNo != "") {
- // $where['good_code'] = Db::raw(" like %{$good_code}%");
- $where[] = ['po.supplierNo', "like", "%{$supplierNo}%"];
- }
- $wsm_supplierNo = isset($this->post['wsm_supplierNo']) && $this->post['wsm_supplierNo'] != "" ? trim($this->post['wsm_supplierNo']) : "";
- if ($wsm_supplierNo != "") {
- $supplier = Db::name("supplier")
- ->field('id')
- ->where(["code" => $wsm_supplierNo])
- ->find();
- if (empty($supplier)) {
- return json_show(1004, "未找到供应商信息");
- }
- $wsmcode = Db::name("warehouse_info")
- ->where(["is_del" => 0, "supplierNo" => $wsm_supplierNo])
- ->column("wsm_code");
- $where[] = ['po.wsm_code', "in", $wsmcode];
- }
- $start = isset($this->post['start']) && $this->post['start'] != "" ? $this->post['start'] : "";
- if ($start != "") {
- // $where['addtime'] = Db::raw(" >= '{$start}'");
- $where[] = ['po.addtime', ">=", $start];
- }
- $end = isset($this->post['end']) && $this->post['end'] != "" ? $this->post['end'] : "";
- if ($end != "") {
- $where[] = ['po.addtime', "<=", $end];
- }
- $last_start = isset($this->post['last_start']) && $this->post['last_start'] != "" ? $this->post['last_start'] : "";
- if ($last_start != "") {
- //$where['lasttime'] = Db::raw(" >= '{$last_start}'");
- $where[] = ['po.lasttime', ">=", $last_start];
- }
- $last_end = isset($this->post['last_end']) && $this->post['last_end'] != "" ? $this->post['last_end'] : "";
- if ($last_end != "") {
- //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
- $where[] = ['po.lasttime', "<=", $last_end];
- }
- $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? $this->post['orderCode'] : "";
- if ($orderCode != "") {
- //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
- $where[] = ['on.orderCode', "like", '%' . $orderCode . '%'];
- }
- $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
- if ($company_name !== "") $where[] = ["po.cgder_id", 'in', get_company_item_user_by_name($company_name)];
- // if(!empty($role['platform']) ){
- // $where[]=["po.platform_id","in",$role['platform']];
- // }
- $count = Db::name("purchease_order")
- ->alias('po')
- ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
- ->leftJoin('sale s', 's.orderCode=on.orderCode')
- ->leftJoin("depart_user u", "u.uid=po.cgder_id AND u.is_del=0")->where($where)
- ->where(function ($query) use ($where) {
- $query->whereOr([['po.order_type', '<>', 1], ['po.order_source', '=', 0]]);
- })
- ->count('po.id');
- $total = ceil($count / $size);
- $page = $page >= $total ? $total : $page;
- $list = Db::name("purchease_order")
- ->alias('po')
- ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
- ->leftJoin('sale s', 's.orderCode=on.orderCode')
- ->field('po.*,on.orderCode')
- ->where($where)
- ->where(function ($query) use ($where) {
- $query->whereOr([['po.order_type', '<>', 1], ['po.order_source', '=', 0]]);
- })
- ->page($page, $size)
- ->order("po.addtime desc")
- ->select()
- ->toArray();
- $all_createrid = array_column($list,'cgder_id');
- $item = get_company_name_by_uid($all_createrid);
- $data = [];
- foreach ($list as $value) {
- $value['wsm_name'] = "";
- if ($value['wsm_code'] != "") {
- $wsmcode = Db::name("warehouse_info")
- ->alias("a")
- ->leftJoin("supplier b", "a.supplierNo=b.code")
- ->where(["a.wsm_code" => $value['wsm_code']])
- ->field("a.name as wsm_name,b.name,b.code")
- ->find();
- $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
- $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
- $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
- }
- if ($value['order_type'] == 3) {
- $goon = Db::name("good_zixun")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
- $value["speclist"] = isset($goon['specinfo']) && $goon['specinfo'] != "" ? json_decode($goon['specinfo'], true) : "";
- } else {
- $goon = Db::name('good_basic')->where(['spuCode' => $value['spuCode']])->find();
- $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
- $speclist = [];
- if (!empty($spec)) {
- foreach ($spec as $val) {
- $temp = [];
- $temp['id'] = $val['id'];
- $temp['spuCode'] = $val['spuCode'];
- $temp['spec_id'] = $val['spec_id'];
- $temp['spec_value_id'] = $val['spec_value_id'];
- $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
- $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
- $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
- $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
- $speclist[] = $temp;
- }
- }
- $value["speclist"] = empty($speclist) ? [] : $speclist;
- }
- //采购单详情字段order_type 为1或者2时,取线上商品库 商品创建人 order_type 为3或者4时 取采购反馈的商品库 商品反馈人
- // $value['cgder_id'] = $goon['createrid'];
- // $value['cgder'] = $goon['creater'];
- $inorder = Db::name("purchease_in")->where(['cgdNo' => $value['cgdNo'], "is_del" => 0])->select();
- $value['child'] = empty($inorder) ? [] : $inorder;
- $value['send_num'] -= $value['th_num'];
- $value['total_fee'] = round($value['total_fee'] - $value['th_fee'], 2);
- //补充orderCode
- // $value['orderCode']=isset($all_orderCodes[$value['cgdNo']])?$all_orderCodes[$value['cgdNo']]:'';
- $value['company_name'] = $item[$value['cgder_id']]??'';
- $data[] = $value;
- }
- return json_show(0, "获取成功", ["list" => $data, "count" => $count]);
- }
- //详情
- public function info()
- {
- $this->post = $this->request->filter('trim')->post();
- $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
- if ($cgdNo == "") {
- return json_show(1004, "参数cgdNo 不能为空");
- }
- //采购单
- $data = Db::name("purchease_order")->where(["cgdNo" => $cgdNo, "is_del" => 0, 'supplierNo' => $this->request->user['supplierNo']])->find();
- if (empty($data)) {
- return json_show(1004, "未找到数据");
- }
- //采购退货单
- $im = Db::name('purchease_back')->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->select();
- $var = [];
- foreach ($im as $value) {
- if ($data['order_type'] == 3) {
- $goo = Db::name("good_zixun")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->find();
- } else {
- $goo = Db::name('good_basic')->where(['spuCode' => $value['spuCode']])->find();
- }
- if ($goo == false) {
- return json_show(1005, "未找到商品数据");
- }
- $cat = isset($goo['cat_id']) && $goo['cat_id'] != 0 ? made($goo['cat_id']) : [];
- $value['cant'] = $cat;
- $var[] = $value;
- }
- //采购工差单
- $dom = Db::name("purchease_diff")->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->find();
- if ($data['order_type'] == 3) {
- $goon = Db::name("good_zixun")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->find();
- } else {
- $goon = Db::name('good_basic')->where(['spuCode' => $data['spuCode']])->find();
- }
- if (empty($goon)) {
- return json_show(1005, "未找到商品数据");
- } else {
- $goon['exclusive'] = isset($goon['is_exclusive']) ? makeExcluse($goon['is_exclusive']) : "";
- $unit = Db::name("unit")->where(["id" => $goon['good_unit']])->find();
- $goon['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
- $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
- $speclist = [];
- if (!empty($spec)) {
- foreach ($spec as $value) {
- $temp = [];
- $temp['id'] = $value['id'];
- $temp['spuCode'] = $value['spuCode'];
- $temp['spec_id'] = $value['spec_id'];
- $temp['spec_value_id'] = $value['spec_value_id'];
- $temp['is_del'] = $value['is_del'];
- $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
- $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
- $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
- $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
- $speclist[] = $temp;
- }
- }
- $goon["speclist"] = empty($speclist) ? [] : $speclist;
- $proof = Db::name("good_proof")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
- $goon['proof'] = isset($proof) && $proof != false ? $proof : [];
- $goon['origin_place_cn'] = "";
- $goon['delivery_place_cn'] = "";
- if (isset($goon['delivery_place']) && $goon['delivery_place'] !== "") {
- $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
- list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['delivery_place']);
- $goon['delivery_place_cn'] = GetAddr(json_encode($place));
- }
- if (isset($goon['delivery_place']) && $goon['origin_place'] !== "") {
- $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
- list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['origin_place']);
- $goon['origin_place_cn'] = GetAddr(json_encode($place));
- }
- if ($goon['brand_id'] != 0) {
- $brand = Db::name("brand")->where(["id" => $goon['brand_id']])->find();
- $goon["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
- } else {
- $goon["brand_name"] = "";
- $goon["brand_id"] = "";
- }
- $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
- }
- $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
- $codes = [$goon['supplierNo'],$goon['companyNo'],$data['companyNo']];
- $data['wsm_name'] = "";
- if ($data['wsm_code'] != "") {
- $wsmcode = Db::name("warehouse_info")
- ->alias("a")
- // ->leftJoin("supplier b", "a.supplierNo=b.code")
- ->leftJoin("warehouse_addr c", "a.wsm_code=c.wsm_code and c.is_del=0")
- ->where(["a.wsm_code" => $data['wsm_code']])
- ->field("a.name as wsm_name,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_code,a.supplierNo")
- ->find();
- $codes[]=$wsmcode['supplierNo'];
- $data['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
- $data['wsm_contactor'] = isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor'] : "";
- $data['wsm_mobile'] = isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile'] : "";
- // $data['wsm_addr'] =isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
- $data['addr_code'] = isset($wsmcode['addr_code']) ? $wsmcode['addr_code'] : "";
- $data['wsm_addr'] = isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']) : "";
- $data['wsm_addr'] .= isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr'] : "";
- $inorder = Db::name("purchease_in")->where(['cgdNo' => $data['cgdNo'], "is_del" => 0])->select();
- $data['child'] = empty($inorder) ? [] : $inorder;
- }
- $data['can'] = $int;
- $data['goodinfo'] = $goon;
- $data['purcheasediff'] = $dom;
- $data['send_num'] -= $data['th_num'];
- $data['total_fee'] = round($data['total_fee'] - $data['th_fee'], 2);
- // if (isset($data['companyNo']) && $data['companyNo'] != "") {
- // $company = Db::name("business")->where(["companyNo" => $data['companyNo']])->find();
- // }
- // $data['company'] = isset($company['company']) ? $company['company'] : "";
- //$data['info'] = $var;
- $data['purcheaseback'] = $var;
- //补充orderCode
- $data['orderCode'] = Db::name('order_num')
- ->where('cgdNo', $data['cgdNo'])
- ->value('orderCode', '');
- //补充收货地址
- $addr = Db::name('order_addr')
- ->field('id,addr,addr_code,contactor,mobile,receipt_quantity')
- ->where(['orderCode' => $data['orderCode'], 'is_del' => 0])
- ->select()
- ->toArray();
- if (!empty($addr)) {
- foreach ($addr as &$vv) {
- $temp = explode(',', $vv['addr_code']);
- $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] : '']));
- $vv['addr_code'] = $temp_ . $vv['addr'];
- }
- } else $addr = [];
- $data['addr_info'] = $addr;
- //补充客户名称
- $customer_code='';
- if (empty($data['orderCode'])) $data['customer'] = [];
- else {
- $customer_code = Db::name('sale')
- ->alias('s')
- // ->field('c.companyNo,c.companyName')
- // ->leftJoin('customer_info c', 'c.companyNo=s.customer_code AND c.is_del=0')
- ->where(['s.orderCode' => $data['orderCode']])
- ->value('s.customer_code');
- $codes[]=$customer_code;
- }
- //补充相关名称
- $userCommon = User::getIns();
- $names = $userCommon->handle('getCodeAndName',['code'=>$codes]);
- if ($data['wsm_code'] != "") {
- $data['wsm_supplier'] = $names['data'][$wsmcode['supplierNo']]??'';//isset($wsmcode['name']) ? $wsmcode['name'] : "";
- $data['wsm_supplierNo'] = $wsmcode['supplierNo'];//isset($wsmcode['code']) ? $wsmcode['code'] : "";
- }
- // $supplier = Db::name("supplier")->where(["code" => $goon['supplierNo']])->find();
- $goon['supplierName'] = $names['data'][$goon['supplierNo']]??'';//isset($supplier['name']) ? $supplier['name'] : "";
- // if (isset($goon['companyNo']) && $goon['companyNo'] != "") {
- // $company = Db::name("business")->where(["companyNo" => $goon['companyNo']])->find();
- // }
- $goon['company'] = $names['data'][$goon['companyNo']]??'';//isset($company['company']) ? $company['company'] : "";
- $data['company'] = $names['data'][$data['companyNo']]??'';//isset($company['company']) ? $company['company'] : "";
- if ($customer_code != '') {
- $data['customer']['companyNo'] = $customer_code;
- $data['customer']['companyName'] = $names['data'][$customer_code] ?? '';
- }
- return json_show(0, "获取成功", $data);
- }
- //订单确认
- public function status()
- {
- $this->post = $this->request->filter('trim')->post();
- $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? $this->post['cgdNo'] : [];
- if (empty($cgdNo)) return json_show(1004, "参数cgdNo 不能为空");
- $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
- $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
- if ($status === "") return json_show(1004, "参数status 不能为空");
- if ($status == 0) {
- $data = Db::name("purchease_order")
- ->field('id,cgdNo')
- ->whereIn('cgdNo', $cgdNo)
- ->where(["is_del" => 0])
- ->where('send_status', '>', 1)
- ->find();
- if (!empty($data)) return json_show(1004, $data['cgdNo'] . "采购单发货中无法取消");
- }
- //原始数据
- $info = Db::name("purchease_order")
- ->whereIn('cgdNo', $cgdNo)
- ->where('status', 0)
- ->column('id,cgdNo,status,supplierNo,spuCode,order_type', 'cgdNo');
- if (empty($info)) return json_show(1005, '没有可供确认的订单');
- Db::startTrans();
- try {
- $upd = Db::name("purchease_order")
- ->whereIn('cgdNo', $cgdNo)
- ->where('status', 0)
- ->save([
- 'status' => $status,
- 'remark' => $remark,
- 'updatetime' => date("Y-m-d H:i:s"),
- ]);
- if ($upd) {
- // $user=GetUserInfo($this->post['token']);
- $uid = $this->request->user['uid']; //isset($user['data']['id'])?$user['data']['id']:0;
- $uname = $this->request->user['nickname'];//isset($user['data']['nickname'])?$user['data']['nickname']:'';
- $supplier_cgderid = Db::name('supplier')
- ->where('is_del', 0)
- ->whereIn('code', array_column($info, 'supplierNo'))
- ->column('personid', 'code');
- foreach ($cgdNo as $vlue) {
- $isreturn = cgd_sale_return($vlue);
- if($isreturn){
- throw new Exception("{$vlue}关联销售单存在退货单正在进行");
- }
- // if($status==1 || $status==2){
- // $orderinfo = Db::name('sale')
- // ->alias('a')
- // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid')
- // ->leftJoin('order_num b','b.orderCode=a.orderCode')
- // ->where([
- // 'a.is_del'=>0,
- // 'b.cgdNo'=>$vlue,
- // ])->findOrEmpty();
- // $is_stock = Db::name('good_basic')
- // ->where(['is_del' => 0, 'spuCode' => $info[$vlue]['spuCode']])
- // ->value('is_stock');
- // if ($is_stock == 1) {
- //库存品,推给31库管人员、41库管-张凯旋
- // $uids = Db::name('user_role')
- // ->where([
- // ['is_del', '=', 0],
- // ['roleid', 'in', [31, 41]],
- // ['status', '=', 1]
- // ])->column('uid');
- // if (!in_array($uid, $uids)) throw new Exception('库存品订单只能由库管人员操作');
- // $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)];
- // } else {
- //非库存品和采返商品,推给供应商负责人(还得是没开通账号的供应商)
- // $res = SupplierRelationUserModel::field('id')
- // ->where(['is_del' => SupplierUserModel::$is_del_normal, 'supplierNo' => $info[$vlue]['supplierNo'], 'status' => SupplierUserModel::$status_normal])
- // ->findOrEmpty()
- // ->isEmpty();
- // if (!$res) throw new Exception('供应商已开通账号,只能由供应商端操作');
- // if ($uid != $supplier_cgderid[$info[$vlue]['supplierNo']]) throw new Exception('非库存品和采返商品只能由供应商负责人操作');
- // $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];
- // }
- // }//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];
- $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];
- ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], $process);
- $order = ["order_code" => $vlue, "status" => '', "action_remark" => $remark, "action_type" => "status"];
- ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], $order, 'CGD', $status, $this->post);
- }
- Db::commit();
- return json_show(0, "更新成功");
- } else throw new Exception('更新失败');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1004, $exception->getMessage());
- }
- }
- //批量入库
- public function add()
- {
- $param = $this->request->filter('trim')->only(['cgdNo'], 'post');
- $val = Validate::rule(['cgdNo|订单编号' => 'require|array|max:100']);
- //入库方式 供应商包邮,
- //运费 0
- if (!$val->check($param)) return json_show(1004, $val->getError());
- $cgdinfo = Db::name("purchease_order")
- ->whereIn('cgdNo', $param['cgdNo'])
- ->where(['is_del' => 0, 'supplierNo' => $this->request->user['supplierNo']])
- ->where('status', 'in', [1, 2])//1待入库,2部分入库
- ->column('id,wsm_code,good_num,good_price,spuCode,order_type,send_status,status,supplierNo', 'cgdNo');
- if (empty($cgdinfo)) return json_show(1005, "未找到采购单数据或采购单未发货");
- $all_wsm_code = array_column($cgdinfo, 'wsm_code');
- $recep = Db::name('warehouse_addr')
- ->where('is_del', 0)
- ->whereIn('wsm_code', $all_wsm_code)
- ->column('wsm_name', 'wsm_code');
- $uid = $this->request->user['uid'];
- $nickname = $this->request->user['nickname'];
- Db::startTrans();
- try {
- $date = date('Y-m-d H:i:s');
- $supplier_cgderid = Db::name('supplier')
- ->where('is_del',0)
- ->whereIn('code',array_column($cgdinfo,'supplierNo'))
- ->column('personid,person','code');
- $i = 0;
- $status = 4;//4入库完成
- foreach ($param['cgdNo'] as $cgdNo) {
- $isreturn = cgd_sale_return($cgdNo);
- if($isreturn){
- throw new Exception("{$cgdNo}关联销售单存在退货单正在进行");
- }
- if (empty($cgdinfo[$cgdNo])) throw new Exception('未找到采购单数据或采购单未发货');
- $wsm_in_code = makeNo("CF");
- $bn_code = makeNo("BN");
- $send_num = $cgdinfo[$cgdNo]['good_num'];
- //改变编码规则,将原编码后两位换成序列号
- //str_pad字符串填充
- $wsm_in_code = substr($wsm_in_code, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
- $i++;
- //入库单
- $win = Db::name("purchease_in")->insertGetId([
- "wsm_in_code" => $wsm_in_code,
- "cgdNo" => $cgdNo,
- "bnCode"=>$bn_code,
- "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
- "wsm_reaper" => isset($recep[$cgdinfo[$cgdNo]['wsm_code']]) ? $recep[$cgdinfo[$cgdNo]['wsm_code']] : '',
- "send_num" => $cgdinfo[$cgdNo]['good_num'],
- "post_company" => '',
- "post_code" => '',
- "post_fee" => 0,//运费固定值:0
- "sendtime" => $date,
- "apply_id" => $this->request->user['uid'],
- "apply_name" => $this->request->user['nickname'],
- "wait_num" => 0,
- "wsm_num" => 0,
- "sendtype" => 2,//发货方式,固定值,供应商包邮2
- "status" => $status,
- "addtime" => $date,
- "updatetime" => $date
- ]);
- if ($win) {
- $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
- ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $sto, "RKD", $status, $this->request->filter('trim')->post());
- $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status,'holder_id' => $this->request->user['uid']];
- ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
- $order = ["order_code" => $cgdNo, "status" => $cgdinfo[$cgdNo]['status'], "action_remark" => '', "action_type" => "edit"];
- //维护台账信息
- Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . $date . "' WHERE `cgdNo`='{$cgdNo}'");
- //////////////////
- $good = Db::name("good_stock")
- ->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])
- ->lock(true)
- ->find();
- if (empty($good)) {
- $good = [
- "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
- "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
- "usable_stock" => 0,
- "wait_out_stock" => 0,
- "wait_in_stock" => $send_num,
- "total_stock" => 0,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s"),
- ];
- // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "create"];
- } else {
- // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "edit"];
- }
- $good['wait_in_stock'] -= $send_num;
- // if ($good['presale_stock'] > 0) {
- // if ($good['presale_stock'] >= $send_num) {
- // $good['presale_stock'] -= $send_num;
- // $good['wait_out_stock'] += $send_num;
- // } else {
- // $good['presale_stock'] = 0;
- // $good['usable_stock'] += $send_num - $good['presale_stock'];
- // $good['wait_out_stock'] += $good['presale_stock'];
- // }
- // } else {
- $good['usable_stock'] += $send_num;
- // }
- $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
- $good['updatetime'] = date("Y-m-d H:i:s");
- $upd = Db::name("good_stock")->save($good);
- $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
- //商品变动日志表,good_log_code字段存储采购单单号
- $good_data=[];
- $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" =>$stockid, "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
- $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" =>$stockid, "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
- if ($upd) {
- GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
- if ($cgdinfo[$cgdNo]['order_type'] == 1) {
- $bnin=GoodStockInfo::AddBn($stockid,$bn_code,$send_num,$cgdinfo[$cgdNo]['good_price']);
- if($bnin==false){
- Db::rollback();
- return error_show(1004,"库存bn数据新增失败");
- }
- $bk = Db::name("order_bk")->where(['cgdNo' => $cgdNo])->find();
- if ($bk == false) {
- $bk = [
- "cgdNo" => $cgdNo,
- "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
- "companyNo" => $cgdinfo[$cgdNo]['companyNo'],
- "total_num" => $send_num,
- "merge_num" => 0,
- "balance_num" => $send_num,
- "status" => 1,
- "is_del" => 0,
- 'addtime' => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- $upcgd = Db::name("order_bk")->save($bk);
- } else {
- $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
- $bk['balance_num'] += $send_num;
- $bk['total_num'] += $send_num;
- $bk['updatetime'] = date("Y-m-d H:i:s");
- $upcgd = Db::name("order_bk")->where($where)->update($bk);
- }
- if ($upcgd == false) throw new Exception('采购单数量更新失败');
- }
- if (in_array($cgdinfo[$cgdNo]['send_status'], [1, 2]) && $cgdinfo[$cgdNo]['order_type'] != 1) {
- $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
- if (!empty($send)) {
- $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0,'is_del'=>0])->select()->toArray();
- if (!empty($out)) {
- $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0,'is_del'=>0])->update(["status" => 1, 'wsm_code' => $cgdinfo[$cgdNo]['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
- if ($up == false) throw new Exception('出库单出库失败');
- else {
- //查询是否有非库存品订单
- $is_stock = Db::name('sale')
- ->where([
- ['is_del','=',0],
- ['orderCode','in',array_column($out,'orderCode')]
- ])->column('is_stock','orderCode');
- foreach ($out as $k => $v_outCode) {
- //修改状态,添加待办
- ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], [
- "order_code" => $v_outCode['outCode'],//出库单号
- "status" => 0,//这里的status是之前的值
- "action_remark" => '',//备注
- "action_type" => "status"//新建create,编辑edit,更改状态status
- ], "CKD", 1, $param);
- //库存品,推给库管和库管-张凯旋
- if($is_stock[$v_outCode['orderCode']] == 1){
- $roleid = config('app.wsm_cgder_role');
- $uids = Db::name('user_role')
- ->where('is_del', 0)
- ->whereIn('roleid', $roleid)
- ->column('uid');
- ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
- "order_type" => 'CKD',
- "order_code" => $v_outCode['outCode'],//出库单号
- "order_id" => $v_outCode['id'],
- "order_status" => 1,
- "before_status" => 0,
- // 'wait_id' => $wait['personid'],
- // 'wait_name' => $wait['person'],
- // 'holder_id' => $wait['personid'],
- 'handle_user_list' => implode(',', $uids)
- ]);
- }else{
- //推给供应商负责人
- $wait = $supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']];
- ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
- "order_type" => 'CKD',
- "order_code" => $v_outCode['outCode'],//出库单号
- "order_id" => $v_outCode['id'],
- "order_status" => 1,
- "before_status" => 0,
- 'wait_id' => $wait['personid'],
- 'wait_name' => $wait['person'],
- 'holder_id' => $wait['personid'],
- ]);
- }
- }
- }
- $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])->find();
- if ($good1 == false) throw new Exception('未找到对应商品库存');
- $out_num = intval(array_sum(array_column($out, "send_num")));
- if ($out_num > $good1['usable_stock']) throw new Exception('库存数量不足发货');
- $good1['usable_stock'] -= $out_num;
- $good1['wait_out_stock'] += $out_num;
- $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
- $good1['updatetime'] = date("Y-m-d H:i:s");
- $upd = Db::name("good_stock")->save($good1);
- if ($upd == false) throw new Exception('库存数量更新失败');
- $good_data=[];
- $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"];
- $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"];
- GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
- $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
- if ($sendp == false) throw new Exception('库存发货失败');
- }
- }
- }
- ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $order, "CGD", $cgdinfo[$cgdNo]['status'], $cgdinfo[$cgdNo]);
- $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];
- ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
- // Db::commit();
- // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
- } else throw new Exception('商品入库失败');
- //////////////////
- } else throw new Exception('新建入库单失败');
- }
- //维护原有采购单信息
- Db::name("purchease_order")
- ->whereIn('id', array_column($cgdinfo, 'id'))
- ->update([
- 'wsend_num' => 0,
- 'send_num' => Db::raw('good_num'),
- 'send_status' => 3,
- 'status' => 3,
- 'updatetime' => date("Y-m-d H:i:s"),
- ]);
- Db::commit();
- return json_show(0, '新建入库单成功');
- // $win = Db::name("purchease_in")->insert($orin, true);
- // if ($win) {
- // $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
- // ActionLog::logAdd($this->post['token'], $sto, "RKD", $status, $this->post);
- // $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status];
- // ProcessOrder::AddProcess($this->post['token'], $process);
- // $order = ["order_code" => $cgdNo, "status" => $cgdinfo['status'], "action_remark" => '', "action_type" => "edit"];
- //维护台账信息
- // 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}'");
- // if ($status == 1) {
- // Db::commit();
- // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
- // }
- // if ($status == 4) {
- // $cgdinfo['wsend_num'] -= $send_num;
- // $cgdinfo['send_num'] += $send_num;
- // $cgdinfo['send_status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
- // $cgdinfo['status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
- // $cgdinfo['updatetime'] = date("Y-m-d H:i:s");
- // $up = Db::name("purchease_order")->save($cgdinfo);
- // if ($up == false) {
- // Db::rollback();
- // return json_show(1004, "新建失败");
- // }
- // $good = Db::name("good_stock")
- // ->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])
- // ->lock(true)
- // ->find();
- // if (empty($good)) {
- // $good = [
- // "spuCode" => $cgdinfo['spuCode'],
- // "wsm_code" => $cgdinfo['wsm_code'],
- // "usable_stock" => 0,
- // "wait_out_stock" => 0,
- // "wait_in_stock" => $send_num,
- // "total_stock" => 0,
- // "addtime" => date("Y-m-d H:i:s"),
- // "updatetime" => date("Y-m-d H:i:s"),
- // ];
- // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
- // // "action_type"=>"create"];
- // } else {
- // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
- // // "action_type"=>"edit"];
- // }
- // $good['wait_in_stock'] -= $send_num;
- // if ($good['presale_stock'] > 0) {
- // if ($good['presale_stock'] >= $send_num) {
- // $good['presale_stock'] -= $send_num;
- // $good['wait_out_stock'] += $send_num;
- // } else {
- // $good['presale_stock'] = 0;
- // $good['usable_stock'] += $send_num - $good['presale_stock'];
- // $good['wait_out_stock'] += $good['presale_stock'];
- // }
- // } else {
- // $good['usable_stock'] += $send_num;
- // }
- //
- // $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
- // $good['updatetime'] = date("Y-m-d H:i:s");
- // $upd = Db::name("good_stock")->save($good);
- // //商品变动日志表,good_log_code字段存储采购单单号
- // $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"];
- // $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"];
- // if ($upd) {
- // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
- // if ($cgdinfo['order_type'] == 1) {
- // $bk = Db::name("order_bk")->where(['cgdNo' => $cgdinfo['cgdNo']])->find();
- // if ($bk == false) {
- // $bk = [
- // "cgdNo" => $cgdinfo['cgdNo'],
- // "spuCode" => $cgdinfo['spuCode'],
- // "companyNo" => $cgdinfo['companyNo'],
- // "total_num" => $send_num,
- // "merge_num" => 0,
- // "balance_num" => $send_num,
- // "status" => 1,
- // "is_del" => 0,
- // 'addtime' => date("Y-m-d H:i:s"),
- // "updatetime" => date("Y-m-d H:i:s")
- // ];
- // $upcgd = Db::name("order_bk")->save($bk);
- // } else {
- // $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
- // $bk['balance_num'] += $send_num;
- // $bk['total_num'] += $send_num;
- // $bk['updatetime'] = date("Y-m-d H:i:s");
- // $upcgd = Db::name("order_bk")->where($where)->update($bk);
- // }
- //
- // if ($upcgd == false) {
- // Db::rollback();
- // return json_show(1004, "采购单数量更新失败");
- // }
- // }
- // if ($cgdinfo['send_status'] == 3 && $cgdinfo['order_type'] != 1) {
- // $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
- // if (!empty($send)) {
- // $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()
- // ->toArray();
- // if (!empty($out)) {
- // $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->update
- // (["status" => 1, 'wsm_code' => $cgdinfo['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
- // if ($up == false) {
- // Db::rollback();
- // return json_show(1004, "出库单出库失败");
- // } else {
- // foreach ($out as $k => $v_outCode) {
- // //修改状态,添加待办
- // ActionLog::logAdd($this->post['token'], [
- // "order_code" => $v_outCode['outCode'],//出库单号
- // "status" => 0,//这里的status是之前的值
- // "action_remark" => '',//备注
- // "action_type" => "status"//新建create,编辑edit,更改状态status
- // ], "CKD", 1, $this->post);
- //
- // ProcessOrder::AddProcess($this->post['token'], [
- // "order_type" => 'CKD',
- // "order_code" => $v_outCode['outCode'],//出库单号
- // "order_id" => $v_outCode['id'],
- // "order_status" => 1, "before_status" => 0
- // ]);
- // }
- // }
- // $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])->find();
- // if ($good1 == false) {
- // Db::rollback();
- // return json_show(1004, "未找到对应商品库存");
- // }
- // $out_num = intval(array_sum(array_column($out, "send_num")));
- // if ($out_num > $good1['usable_stock']) {
- // Db::rollback();
- // return json_show(1004, "库存数量不足发货");
- // }
- // $good1['usable_stock'] -= $out_num;
- // $good1['wait_out_stock'] += $out_num;
- // $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
- // $good1['updatetime'] = date("Y-m-d H:i:s");
- // $upd = Db::name("good_stock")->save($good1);
- // if ($upd == false) {
- // Db::rollback();
- // return json_show(1004, "库存数量更新失败");
- // }
- // $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"];
- // $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"];
- // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
- // $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
- // if ($sendp == false) {
- // Db::rollback();
- // return json_show(1004, "库存发货失败");
- // }
- // }
- // }
- // }
- // ActionLog::logAdd($this->post['token'], $order, "CGD", $cgdinfo['status'], $cgdinfo);
- // $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status']];
- // ProcessOrder::AddProcess($this->post['token'], $process);
- // Db::commit();
- // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
- // } else {
- // Db::rollback();
- // return json_show(1002, "商品入库失败");
- // }
- // }
- // Db::commit();
- // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
- // }
- // Db::rollback();
- // return json_show(1004, "新建失败");
- } catch (Exception $e) {
- Db::rollback();
- return json_show(1004, $e->getMessage());
- }
- }
- //导出
- public function exportCgdList()
- {
- $cgdNos = $this->request->post('cgdNos', [], 'trim');
- if (empty($cgdNos)) return json_show(1004, '要导出的采购单编号不能为空');
- $i = 1;
- $send_type = [1 => '直接发货', 2 => '延时发货'];
- $status = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '已取消订单'];
- $list = Db::name("purchease_order")
- ->alias('po')
- ->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')
- ->whereIn('po.cgdNo', $cgdNos)
- ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
- ->leftJoin('sale s', 's.orderCode=on.orderCode AND s.is_del=0')
- ->leftJoin('order_addr oa', 'oa.orderCode=on.orderCode AND oa.is_del=0')
- ->leftJoin('business b', 'b.companyNo=s.supplierNo AND b.is_del=0')
- ->order("po.addtime desc")
- ->withAttr('序号', function () use (&$i) {
- return $i++;
- })->withAttr('采购单状态', function ($val) use ($status) {
- return isset($status[$val]) ? $status[$val] : '';
- })->withAttr('发货方式', function ($val) use ($send_type) {
- return isset($send_type[$val]) ? $send_type[$val] : '';
- })->withAttr('发货方式', function ($val) use ($send_type) {
- return isset($send_type[$val]) ? $send_type[$val] : '';
- })
- ->select()
- ->toArray();
- foreach ($list as &$value) {
- if ($value['order_type'] == 3) {
- $temp = Db::name("good_zixun")
- ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
- ->field('id,specinfo,good_unit,tax')
- ->find();
- $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
- $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
- $speclist = [];
- if(!empty($specinfo)){
- foreach ($specinfo as $val) {
- $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
- }
- }
- $value['税率'] = $temp['tax'] . '%';
- } else {
- $good_unit = Db::name('good_basic')
- ->field('id,good_unit,tax')
- ->where(['spuCode' => $value['产品编号']])
- ->find();
- $spec = Db::name("good_spec")
- ->field('id,spec_id,spec_value_id')
- ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
- ->select()
- ->toArray();
- $speclist = [];
- if (!empty($spec)) {
- foreach ($spec as $val) {
- $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', '');
- }
- }
- $value['税率'] = isset($good_unit['tax']) ? $good_unit['tax'] . '%' : '';
- }
- $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
- $value['单位'] = isset($good_unit['good_unit']) ? Db::name('unit')->where(['id' => $good_unit['good_unit'], 'is_del' => 0])->value('unit', '') : '';
- if (!empty($value['addr_code'])) {
- $temp = explode(',', $value['addr_code']);
- $temp_ = GetAddr(json_encode(['provice_code' => $temp[0], 'city_code' => $temp[1], 'area_code' => $temp[2]]));
- $value['收货地址'] = $temp_ . $value['收货地址'];
- }
- unset($value['addr_code']);
- unset($value['order_type']);
- }
- $headerArr = array_keys($list[0]);
- excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
- }
- }
|