12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\ActionLog;
- use app\admin\model\CgdPrice;use app\admin\model\DataGroup as DataGroupModel;
- use app\admin\model\GoodTax;use app\admin\model\OrderTax;use app\admin\model\ProcessOrder;
- use think\App;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- //采购单相关功能
- class Purch extends Base
- {
- public $noble=[];
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->noble=\think\facade\Config::get("noble");
- }
- //采购单列表
- public function list()
- {
- $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]];
- $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 error_show(1004, "未找到有关入库单信息");
- }
- $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[] = ['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[] = ['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', "=", $supplierNo];
- }
- $wsm_supplierNo = isset($this->post['wsm_supplierNo']) && $this->post['wsm_supplierNo'] != "" ? trim($this->post['wsm_supplierNo']) : "";
- if ($wsm_supplierNo !== "") {
- $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 . " 00:00:00"];
- }
- $end = isset($this->post['end']) && $this->post['end'] != "" ? $this->post['end'] : "";
- if ($end !== "") {
- $where[] = ['po.addtime', "<=", $end . " 23:59:59"];
- }
- $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 . " 00:00:00"];
- }
- $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 . " 23:59:59"];
- }
- $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? $this->post['orderCode'] : "";
- if ($orderCode !== "") {
- //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
- $where[] = ['on.orderCode', "like", '%' . $orderCode . '%'];
- }
- //筛选供应商名称
- $supplier_name = isset($this->post['supplier_name']) && $this->post['supplier_name'] != "" ? $this->post['supplier_name'] : "";
- if ($supplier_name != '') $where[] = ['po.supplier_name', 'like', '%' . $supplier_name . '%'];
- $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] != "" ? trim($this->post['relaComNo']) : "";
- if ($relaComNo !== "") $where[] = ['po.companyNo', '=', $relaComNo];
- $userCommon = \app\admin\common\User::getIns();
- $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)];
- $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type']) : "";
- if ($order_type !== "") $where[] = ["po.order_type", '=', $order_type];
- $order_source = isset($this->post['order_source']) && $this->post['order_source'] !== "" ? intval($this->post['order_source']) : "";
- if ($order_source !== "") $where[] = ["po.order_source", '=', $order_source];
- $count = Db::name("purchease_order")
- ->alias('po')
- ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
- // ->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')
- ->field('po.*,on.orderCode,"" supplier_cgderid,"" supplier_cgder')
- ->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();
- $account = checkHasAccountBySupplierNos(array_column($list, 'supplierNo'));
- $all_createrid = array_column($list, 'cgder_id');
- $item = get_company_name_by_uid($all_createrid);
- $data = [];
- $wsmCodes = array_column($list, 'wsm_code');
- $wsmcode = Db::name("warehouse_info")
- ->alias("a")
- ->where(["a.wsm_code" => $wsmCodes])
- ->column("a.name as wsm_name,a.supplierNo as code", "wsm_code");
- // $wsmsupplier=[];
- // if(!empty($wsmcode)){
- //
- //
- // $wsmsupplier = $names['data']??[];
- // }
- $names = $userCommon->handle('getCodeAndName', ['code' => array_unique(array_column($wsmcode, "code"))]);
- //补充供应商负责人(sip.personid as supplier_cgderid,sip.person as supplier_cgder)
- $person_list = $userCommon->handle('sGetList', ['more_code' => array_unique(array_column($list, 'supplierNo')), 'size' => $size]);
- $person_list = array_column($person_list['data']['list'], null, 'code');
- $goon_3_4 = Db::name('good_zixun')
- ->where(['spuCode' => array_column($list, 'spuCode'), 'is_del' => 0])
- ->column('specinfo,cat_id,createrid,creater', 'spuCode');
- $goon_1_2 = Db::name('good_basic')
- ->where(['is_del' => 0, 'spuCode' => array_column($list, 'spuCode')])
- ->column('cat_id,createrid,creater', 'spuCode');
- $cat_temp = [];
- foreach ($list as $value) {
- // $value['wsm_name'] = isset($wsmcode[$value['wsm_code']]['wsm_name']) ? $wsmcode[$value['wsm_code']]['wsm_name'] : "";
- // $value['wsm_supplier'] =isset($wsmcode[$value['wsm_code']]['code'])? $person_list[$wsmcode[$value['wsm_code']]['code']]['name'] ?? "":"";
- // $value['wsm_supplierNo'] = $wsmcode[$value['wsm_code']]['code'] ?? "";
- $value['wsm_name'] = $value['wsm_code'] != '' ? $wsmcode[$value['wsm_code']]['wsm_name'] ?? '' : "";
- $value['wsm_supplier'] = $value['wsm_code'] != '' ? $names[$wsmcode[$value['wsm_code']]['code']] ?? '' : '';
- $value['wsm_supplierNo'] = $value['wsm_code'] != '' ? $wsmcode[$value['wsm_code']]['code'] ?? "" : '';
- $value['supplier_cgderid'] = $person_list[$value['supplierNo']]['personid'] ?? '';
- $value['supplier_cgder'] = $person_list[$value['supplierNo']]['person'] ?? '';
- if ($value['order_type'] == 3 || $value['order_type'] == 4) {
- $value["speclist"] =isset($goon_3_4[$value['spuCode']])? json_decode
- ($goon_3_4[$value['spuCode']]['specinfo'],true):'';
- //为了格式统一,缺了一个spec_value
- if ($value['speclist'] != '') {
- foreach ($value['speclist'] as &$v) {
- $v['spec_value'] = $v['spec_value_name'];
- }
- }
- $cat_id =isset($goon_3_4[$value['spuCode']])? $goon_3_4[$value['spuCode']]['cat_id']:0;
- } else {
- $spec = Db::name("good_spec")->where(["spuCode" => $value['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;
- $cat_id = $goon_1_2[$value['spuCode']]['cat_id'];
- }
- if (!isset($cat_temp[$cat_id])) $cat_temp[$cat_id] = made($cat_id);
- $value['can'] = $cat_temp[$cat_id];
- $inorder = Db::name("purchease_in")
- ->where(['cgdNo' => $value['cgdNo'], "is_del" => 0])
- ->select()
- ->toArray();
- $value['child'] = empty($inorder) ? [] : $inorder;
- $value['send_num'] -= $value['th_num'];
- $value['total_fee'] = round($value['total_fee'] - $value['th_fee'], 2);
- $value['company_name'] = $item[$value['cgder_id']] ?? '';
- $value['has_account'] = (int)isset($account[$value['supplierNo']]);
- $data[] = $value;
- }
- return app_show(0, "获取成功", ["list" => $data, "count" => $count]);
- }
- public function info()
- {
- $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
- if ($cgdNo == "") {
- return error_show(1004, "参数cgdNo 不能为空");
- }
- //采购单
- $data = Db::name("purchease_order")
- ->alias('a')
- ->field('a.*,b.orderCode')
- ->leftJoin('order_num b', 'b.cgdNo=a.cgdNo')
- ->where(["a.cgdNo" => $cgdNo, "a.is_del" => 0])
- ->findOrEmpty();
- if (empty($data)) {
- return error_show(1004, "未找到数据");
- }
- //采购退货单
- $im = Db::name('purchease_back')->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->select();
- $var = [];
- foreach ($im as $value) {
- if (in_array($data['order_type'], [3, 4])) {
- $goo = Db::name("good_zixun")->field('cat_id')->where(["spuCode" => $value['spuCode'], "is_del" => 0])->findOrEmpty();
- } else {
- $goo = Db::name('good_basic')->field('cat_id')->where(['spuCode' => $value['spuCode']])->findOrEmpty();
- }
- if (empty($goo)) {
- return error_show(1002, "未找到商品数据");
- }
- $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])->order("id desc")->find();
- if (in_array($data['order_type'], [3, 4])) {
- $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 error_show(1002, "未找到商品数据");
- } else {
- $taxInfo=OrderTax::where(['code'=>$data['cgdNo']])->findOrEmpty();
- $data['tax_name'] = $taxInfo['tax_name']??'';
- $data['tax_code'] =$taxInfo['tax_code']??'';
- $data['tax_short_name'] =$taxInfo['tax_short_name']??'';
- $data['tax']=$taxInfo['inv_tax']??'';
- $data['inv_good_name']=$taxInfo['inv_good_name']??'';
- $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"] = "";
- }
- $userCommon = \app\admin\common\User::getIns();
- $supplier = $userCommon->handle('sInfo', ['code' => $goon['supplierNo']]);
- // $supplier = Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
- $goon['supplier_cgderid'] = $supplier['data']['personid'] ?? '';
- $goon['supplier_cgder'] = $supplier['data']['person'] ?? '';
- $data['pay_type'] = $supplier['data']['pay_type'] ?? '';
- $goon['supplierName'] = isset($supplier['data']['name']) ? $supplier['data']['name'] : "";
- $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
- //补充客户名称
- if (empty($data['orderCode'])) $data['customer'] = ['companyNo' => '', 'companyName' => ''];
- 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', '');
- }
- $tmp = $userCommon->handle('getCodeAndName', ['code' => [
- $goon['companyNo'],
- $data['wsm_code'],
- $data['companyNo'],
- $customer_code ?? ''
- ]]);
- if (!empty($data['orderCode'])) {
- $data['customer']['companyNo'] = $customer_code;
- $data['customer']['companyName'] = $tmp['data'][$customer_code] ?? '';
- }
- if (isset($goon['companyNo']) && $goon['companyNo'] != "") {
- // $company = Db::name("business")->where(["companyNo"=>$goon['companyNo']])->find();
- }
- $goon['company'] = $tmp['data'][$goon['companyNo']] ?? '';
- }
- $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
- $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")
- ->find();
- $data['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
- $data['wsm_supplier'] = isset($wsmcode['data'][$data['wsm_code']]) ? $wsmcode['data'][$data['wsm_code']] : "";
- $data['wsm_supplierNo'] = isset($wsmcode['data'][$data['wsm_code']]) ? $wsmcode['data'][$data['wsm_code']] : "";
- $data['wsm_contactor'] = isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor'] : "";
- $data['wsm_mobile'] = isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile'] : "";
- $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);
- $data['company'] = '';
- if (isset($data['companyNo']) && $data['companyNo'] != "") {
- $data['company'] = $tmp['data'][$data['companyNo']] ?? '';
- }
- $data['purcheaseback'] = $var;
- $addr = [];
- $data['addr_info'] = $addr;
- $data['has_account'] = (int)isset(checkHasAccountBySupplierNos([$data['supplierNo']])[$data['supplierNo']]);
- //补充商品创建人字段
- $data['good_createrid'] = $goon['createrid'] ?? '';
- $data['good_creater'] = $goon['creater'] ?? '';
- return app_show(0, "获取成功", $data);
- }
- public function edit(){
- $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
- if($cgdNo==""){
- return error_show(1004,"参数cgdNo 不能为空");
- }
- $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
- if(empty($data)){
- return error_show(1004,"未找到数据");
- }
- $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$this->post['remark'],"action_type"=>"edit"];
- $wsm_code = isset($this->post['wsm_code'])&&$this->post['wsm_code']!=""? trim($this->post['wsm_code']):"";
- if($wsm_code!=""){
- $ware = Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code,"is_del"=>0])->find();
- if(empty($ware)){
- return error_show(1004,"未找到仓库信息");
- }
- // $good = Db::name("good_stock")->where(["wsm_code"=>$wsm_code,"good_type_code"=>$data['good_type_code'],"is_del"=>0])->find();
- // if(empty($good)){
- // return error_show(1004,"未找到仓库下商品信息");
- // }
- $data['wsm_code'] = $wsm_code;
- }
- $cgdid = isset($this->post['cgder_id'])&&$this->post['cgder_id']!=""?intval($this->post['cgder_id']):"";
- if($cgdid!=''){
- $cgduser = GetInfoById($this->post['token'],["id"=>$cgdid]);
- if((!empty($cgduser) && $cgduser['code']!=0) ||empty($cgduser) ){
- return error_show($cgduser['code'],$cgduser['message']);
- }
- $userinfo = $cgduser['data'];
- $data['cgder_id'] = $cgdid;
- $data['cgder'] = $userinfo['nickname'];
- }
- $good_num = isset($this->post['good_num'])&&$this->post['good_num']!=""?intval($this->post['good_num']):"";
- if($good_num!=""){
- $data['good_num'] = $good_num;
- }
- $good_price = isset($this->post['good_price'])&&$this->post['good_price']!=""?$this->post['good_price']:"";
- if($good_price!=""){
- $data['good_price'] = $good_price;
- }
- // $total_fee = isset($this->post['total_fee'])&&$this->post['total_fee']!=""?$this->post['total_fee']:"";
- // if($total_fee!=""){
- // $data['total_fee'] = $total_fee;
- // }
- $pakge_fee = isset($this->post['pakge_fee'])&&$this->post['pakge_fee']!=""?$this->post['pakge_fee']:"";
- if($pakge_fee!=""){
- $data['pakge_fee'] = $pakge_fee;
- }
- $cert_fee = isset($this->post['cert_fee'])&&$this->post['cert_fee']!=""?$this->post['cert_fee']:"";
- if($cert_fee!=""){
- $data['cert_fee'] = $cert_fee;
- }
- $open_fee = isset($this->post['open_fee'])&&$this->post['open_fee']!=""?$this->post['open_fee']:"";
- if($open_fee!=""){
- $data['open_fee'] = $open_fee;
- }
- $delivery_fee = isset($this->post['delivery_fee'])&&$this->post['delivery_fee']!=""?$this->post['delivery_fee']:"";
- if($delivery_fee!=""){
- $data['delivery_fee'] = $delivery_fee;
- }
- $mark_fee = isset($this->post['mark_fee'])&&$this->post['mark_fee']!=""?$this->post['mark_fee']:"";
- if($mark_fee!=""){
- $data['mark_fee'] = $mark_fee;
- }
- $teach_fee = isset($this->post['teach_fee'])&&$this->post['teach_fee']!=""?$this->post['teach_fee']:"";
- if($teach_fee!=""){
- $data['teach_fee'] = $teach_fee;
- }
- $nake_fee = isset($this->post['nake_fee'])&&$this->post['nake_fee']!=""?$this->post['nake_fee']:"";
- if($nake_fee!=""){
- $data['nake_fee'] = $nake_fee;
- }
- $weight = isset($this->post['weight'])&&$this->post['weight']!=""?$this->post['weight']:"";
- if($weight!=""){
- $data['weight'] = $weight;
- }
- $diff_weight = isset($this->post['diff_weight'])&&$this->post['diff_weight']!=""?$this->post['diff_weight']:"";
- if($diff_weight!=""){
- $data['diff_weight'] = $diff_weight;
- }
- $diff_fee = isset($this->post['diff_fee'])&&$this->post['diff_fee']!=""?$this->post['diff_fee']:"";
- if($diff_fee!=""){
- $data['diff_fee'] = $diff_fee;
- }
- $supplierNo = isset($this->post['supplierNo'])&&$this->post['supplierNo']!=""?trim($this->post['supplierNo']) :"";
- if($supplierNo!=""){
- $userCommon = \app\admin\common\User::getIns();
- $supplier_temp = $userCommon->handle('getCodeAndName', ['code' => $supplierNo]);
- // $supplier =Db::name("supplier")->where(['code'=>$supplierNo])->find();
- if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1004,"未找到供应商信息");
- // if(empty($supplier)){
- // return error_show(1004,"未找到供应商信息");
- // }
- $data['supplierNo'] = $supplierNo;
- $data['supplier_name'] = $supplier_temp['data'][$supplierNo]??"";
- }
- $remark = isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
- if($remark!=""){
- $data['remark'] = $remark;
- }
- $data['good_price'] = round(($data['pakge_fee']+$data['weight']*$data['gold_price']+$data['nake_fee']+$data['mark_fee']+$data['cert_fee']+$data['open_fee']/$data['good_num']+$data['teach_fee']*$data['weight']+$data['delivery_fee']),2);
- $data['total_fee'] = round($data['good_price']*$data['good_num'],2);
- $data['origin_total'] = round($data['good_price']*$data['good_num'],2);
- $data['updatetime'] =date("Y-m-d H:i:s");
- $upd=Db::name("purchease_order")->save($data);
- if($upd){
- //修改状态,添加待办
- ActionLog::logAdd($this->post['token'], $order, "CGD", $data['status'], $this->post);
- return app_show(0,"更新成功");
- }else{
- return error_show(1004,'更新失败');
- }
- }
- //修改采购单
- public function editNew()
- {
- $param = $this->request->only(['cgdNo', 'supplierNo', 'token', 'wsm_code' => ''], 'post', 'trim');
- $val = Validate::rule([
- 'cgdNo|采购单号' => 'require',
- 'supplierNo|供应商编号' => 'require',
- 'token' => 'require',
- ]);
- if (!$val->check($param)) return error_show(1004, $val->getError());
- Db::startTrans();
- try {
- $rs = Db::name('purchease_order')
- ->alias('po')
- ->field('po.id,po.order_type,po.order_source,po.status,po.cgder_id,po.cgder,po.supplierNo,po.supplier_name,po.wsm_code,wi.name wsm_name')
- ->leftJoin('warehouse_info wi', 'wi.wsm_code=po.wsm_code AND wi.is_del=0')
- ->where([
- 'po.cgdNo' => $param['cgdNo'],
- 'po.is_del' => 0,
- 'po.status' => 0
- ])->find();
- if (empty($rs)) throw new Exception('采购单不存在或不允许修改');
- $update = ['updatetime' => date('Y-m-d H:i:s')];
- //order_type==1备库
- if ($rs['order_type'] == 1 && $rs['order_source']==0) {
- if (empty($param['wsm_code'])) throw new Exception('备库单请传入仓库编码');
- else {
- $wsm_rs = Db::name('warehouse_info')
- ->field('id,wsm_code,name wsm_name')
- ->where([
- 'wsm_code' => $param['wsm_code'],
- 'is_del' => 0,
- ])->find();
- if (empty($wsm_rs)) throw new Exception('查不到该仓库的记录');
- else $update['wsm_code'] = $wsm_rs['wsm_code'];
- }
- }
- //供应商信息
- $userCommon= \app\admin\common\User::getIns();
- $supplier_temp =$userCommon->handle("sInfo",["code"=>$param['supplierNo']]);
- if ($supplier_temp['code']!=0 || empty($supplier_temp['data'])) throw new Exception('查不到该供应商的记录');
- else {
- $supplier_rs=$supplier_temp['data'];
- $update['cgder_id'] = $supplier_rs['personid'];
- $update['cgder'] = $supplier_rs['person'];
- $update['supplierNo'] = $supplier_rs['code'];
- $update['supplier_name'] = $supplier_rs['name'];
- }
- //是否修改内容确定
- if (isset($update['wsm_code'])) {
- if ($update['wsm_code'] == $rs['wsm_code'] && $update['supplierNo'] == $rs['supplierNo']) throw new Exception('没有修改的内容,不能提交');
- } elseif ($update['supplierNo'] == $rs['supplierNo']) throw new Exception('没有修改的内容,不能提交');
- if ($update['supplierNo'] == $rs['supplierNo'] && $param['wsm_code'] == $rs['wsm_code']) throw new Exception('没有修改的内容');
- //修改
- Db::name('purchease_order')
- ->where([
- 'id' => $rs['id'],
- 'is_del' => 0,
- 'status' => 0
- ])->update($update);
- $user = GetUserInfo($param['token']);
- $creater_id = isset($user['data']['id']) ? $user['data']['id'] : 0;
- $creater = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
- //添加修改记录
- Db::name('purchease_change_supplier_log')
- ->insert([
- 'cgdNo' => $param['cgdNo'],
- 'cgder_id_old' => $rs['cgder_id'],
- 'cgder_id' => $update['cgder_id'],
- 'cgder_old' => $rs['cgder'],
- 'cgder' => $update['cgder'],
- 'supplierNo_old' => $rs['supplierNo'],
- 'supplierNo' => $update['supplierNo'],
- 'supplier_name_old' => $rs['supplier_name'],
- 'supplier_name' => $update['supplier_name'],
- 'wsm_code_old' => $rs['wsm_code'],
- 'wsm_code' => isset($update['wsm_code']) ? $update['wsm_code'] : $rs['wsm_code'],
- 'wsm_name_old' => $rs['wsm_name'],
- 'wsm_name' => isset($wsm_rs['wsm_name']) ? $wsm_rs['wsm_name'] : $rs['wsm_name'],
- 'addtime' => date('Y-m-d H:i:s'),
- 'updatetime' => date('Y-m-d H:i:s'),
- 'creater_id' => $creater_id,
- 'creater' => $creater,
- ]);
- //记录日志
- ActionLog::logAdd(['id' => $creater_id, 'nickname' => $creater], $rs, "CGD", $rs['status'], $param);
- //维护台账
- Db::name('standing_book')
- ->where('cgdNo', $param['cgdNo'])
- ->update(['supplierNo' => $param['supplierNo'], 'updatetime' => date('Y-m-d H:i:s')]);
- Db::commit();
- return app_show(0, '修改成功');
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1005, $exception->getMessage());
- }
- }
- public function status()
- {
- $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? $this->post['cgdNo'] : [];
- if (empty($cgdNo)) {
- return error_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 error_show(1004, "参数status 不能为空");
- }
- Db::startTrans();
- try {
- if ($status == 0) {
- $data = Db::name("purchease_order")
- ->field('id,cgdNo,spuCode')
- ->whereIn('cgdNo', $cgdNo)
- ->where(["is_del" => 0])
- ->where('send_status', '>', 1)
- ->find();
- if (!empty($data)) throw new Exception($data['cgdNo'] . "采购单发货中无法取消");
- }
- //原始数据
- $info = Db::name("purchease_order")
- ->whereIn('cgdNo', $cgdNo)
- ->column('id,cgdNo,status,supplierNo,spuCode,order_type,good_price,good_num', 'cgdNo');
- $upd = Db::name("purchease_order")
- ->whereIn('cgdNo', $cgdNo)
- ->save([
- 'status' => $status,
- 'remark' => $remark,
- 'updatetime' => date("Y-m-d H:i:s"),
- ]);
- if ($upd) {
- //获取供应商的负责人
- $userCommon = \app\admin\common\User::getIns();
- $supplier = $userCommon->handle('sGetList',['more_code'=>array_column($info, 'supplierNo')]);
- $supplier_cgderid = array_column($supplier['data']['list'],'personid','code');
- $uid = $this->uid;
- $uname = $this->uname;
- $all_has_supplier_account = checkHasAccountBySupplierNos(array_column($info, 'supplierNo'));
- foreach ($cgdNo as $vlue) {
- $isreturn = cgd_sale_return($vlue);
- if($isreturn){
- throw new Exception("{$vlue}关联销售单存在退货单正在进行");
- }
- if($status == 1){
- //如果开通了供应商账号,则只允许level3账号操作,level2账号不允许操作
- if (isset($all_has_supplier_account[$info[$vlue]['supplierNo']]) && $this->level == 2) throw new Exception('已开通供应商账号,当前账号不能操作');
- if($info[$vlue]['order_type']==1){
- $createInfo=[
- "cgdNo"=>$info[$vlue]['cgdNo'],
- "goodNo"=>$info[$vlue]['spuCode'],
- "goodNum"=>$info[$vlue]['good_num'],
- "goodPrice"=>$info[$vlue]['good_price']
- ];
- CgdPrice::create($createInfo);
- }
-
- }
- if ($status == 1 || $status == 2) {
- $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];
- 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);
- }
- } else throw new Exception('更新失败');
- Db::commit();
- return app_show(0, "更新成功");
- } catch (Exception $exception) {
- Db::rollback();
- return error_show(1004, '更新失败,' . $exception->getMessage());
- }
- }
- //创建采购工差单
- public function diffcreat(){
- $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
- $cgdNo = $this->post['cgdNo'] && $this->post['cgdNo'] !=="" ? trim($this->post['cgdNo']) :"";
- if($cgdNo==""){
- return error_show(1002,"参数good_code不能为空");
- }
- $cg = Db::name('purchease_order')->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find();
- if($cg==""){
- return error_show(1002,"未找到采购单数据");
- }
- if (in_array($cg['order_type'], [3, 4])) {
- $gd = Db::name("good_zixun")->where(["spuCode"=>$cg['spuCode']])->find();
- }else {
- $gd =Db::name('good_basic')->where(['spuCode'=>$cg['spuCode']])->find();
- }
- if($gd==""){
- return error_show(1002,"未找到商品数据");
- }
- $diff_weight = isset($this->post['diff_weight']) && $this->post['diff_weight'] !=="" ? floatval($this->post['diff_weight']) :"";
- if($diff_weight===""){
- return error_show(1002,"参数diff_weight不能为空");
- }
- $orderCode = Db::name("order_num")->where([["cgdNo","=",$cg['cgdNo']],["status","=",1]])->findOrEmpty();
- $order=[];
- if(!empty($orderCode)){
- $order = Db::name("sale")->where("orderCode", '=', $orderCode['orderCode'])->findOrEmpty();
- if(empty($order)){
- return error_show(1002,"未找到确认单信息");
- }
- }
- $userCommon = \app\admin\common\User::getIns();
- $supp =$userCommon->handle("getCodeAndName",["code"=>[$cg['companyNo'],$order['customer_code']??"",$order['supplierNo']??""]]);
- $SuppArr= $supp['data']??[];
- $rid= $this->uid;
- $rname= $this->uname;
- $diff_price = round($diff_weight*$cg['gold_price']+$diff_weight*$cg['teach_fee'],2);
- //如果有未处理的工差单,不允许创建新的
- $wait_check = Db::name('purchease_diff')
- ->field('id')
- ->where(['apply_id'=>$rid,'status'=>1,'is_del'=>0,"cgdNo"=>$cgdNo])
- ->find();
- if(!empty($wait_check)) return error_show('1005','您还有未处理的工差单,请先处理');
- $data=[
- "cgdNo"=>$cgdNo,
- "good_code"=>$cg['spuCode'],
- "good_name"=>$cg['good_name'],
- "sale_price"=>$cg['good_price'],
- "good_weight"=>$cg['weight']*$cg['good_num'],
- "good_num"=>$cg['good_num'],
- "apply_id"=>$rid,
- "apply_name"=>$rname,
- "diff_weight"=>$diff_weight,
- "diff_price"=>$diff_price,
- "gold_price"=>$cg['good_price'],
- "supplierName"=>$cg['supplier_name'],
- "supplierNo"=>$cg['supplierNo'],
- "companyNo"=>$cg['companyNo'],
- "companyName"=>$SuppArr[$cg['companyNo']]??'',
- "status"=>1,
- "is_del"=>0,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- Db::startTrans();
- try{
- $item = Db::name("purchease_diff")->insert($data,true);
- if($item>0){
- $sto = ["order_code"=>$cgdNo,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
- ActionLog::logAdd(['id'=>$rid,'nickname'=>$rname],$sto,"CGGCD",1,$data);
- $process=["order_code"=>$cgdNo,"order_id"=>$item,"order_status"=>1,"order_type"=>'CGGCD',"before_status"=>1,'holder_id'=>$rid];
- ProcessOrder::AddProcess(['id'=>$rid,'nickname'=>$rname],$process);
- $update_standing_book_data = [
- 'cgd_diff_id' => $item,//采购工差单id
- 'updatetime' => date("Y-m-d H:i:s")
- ];
- if(!empty($order)){
- $data=[
- "cgd_diffid"=>$item,
- "orderCode"=>$orderCode['orderCode'],
- "good_code"=>$cg['spuCode'],
- "good_name"=>$cg['good_name'],
- "diff_weight"=>$diff_weight,
- "diff_price"=>round($diff_weight*$order['gold_price']+$diff_weight*$order['cost_price'],2),
- "gold_price"=>$order['sale_price'],
- "customerName"=>$SuppArr[$order['customer_code']]??"",
- "customerNo"=>$order['customer_code'],
- "companyNo"=>$order['supplierNo'],
- "companyName"=>$SuppArr[$order['supplierNo']]??'',
- "supplierName"=>$cg['supplier_name'],
- "supplierNo"=>$cg['supplierNo'],
- "status"=>1,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $cgd = Db::name('sale_diff')->insert($data,true);
- if($cgd==0){
- Db::rollback();
- return error_show(1002,"新建失败");
- }
- $sto = ["order_code"=>$orderCode['orderCode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
- ActionLog::logAdd($this->post['token'],$sto,"XSGCD",1,$data);
- $process=["order_code"=>$orderCode['orderCode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'XSGCD',"before_status"=>1,'holder_id'=>$order['apply_id']];
- ProcessOrder::AddProcess($this->post['token'],$process);
- $update_standing_book_data['sale_diff_id']=$cgd;//工差单id
- }
- //维护台账记录
- Db::name('standing_book')
- ->where('cgdNo', $cgdNo)
- ->update($update_standing_book_data);
- Db::commit();
- return error_show(0,"新建成功");
- }else{
- Db::rollback();
- return error_show(1002,"新建失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1002,$e->getMessage());
- }
- }
- public function difflist(){
- $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 = [['a.is_del',"=",0]];
- $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):"";
- if($cgdNo!=""){
- $where[]=['a.cgdNo',"like", "%$cgdNo%"];
- }
- $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
- if($status!==""){
- $where[]=['a.status',"=",$status];
- }
- $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
- if ($start !="") {
- $where[]= ["a.addtime",'>=',$start];
- }
- $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
- if($end !=""){
- $where[]= ["a.addtime",'<=',$end];
- }
- $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
- if($apply_name!==""){
- $where[]=['a.apply_name',"like","%$apply_name%"];
- }
- $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
- if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
- $order_source = $order_source = isset($this->post['order_source']) && $this->post['order_source'] != "" ? intval($this->post['order_source']) : "";
- if ($order_source !== "") $where[] = ['c.order_source', "=", $order_source];
- $order_type = $order_source = isset($this->post['order_type']) && $this->post['order_type'] != "" ? intval($this->post['order_type']) : "";
- if ($order_type !== "") $where[] = ['c.order_type', "=", $order_type];
- // $role = $this->checkDataShare();
- // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ['a.apply_id', 'in', $role[DataGroupModel::$type_全部]];
- $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] != "" ? trim($this->post['relaComNo']) : "";
- if ($relaComNo != '') $where[] = ['a.companyNo', '=', $relaComNo];
- $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
- if ($supplierNo != '') $where[] = ['b.supplierNo', '=', $supplierNo];
- if ($this->level == 2) {
- $role = $this->checkDataShare();
- $hand = resign_hand_user($this->uid, 0);
- if (!empty($role[DataGroupModel::$type_全部])) {
- $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
- $where[] = ['a.apply_id', 'in', $arr];
- }
- // if($relaComNo!='') $where[]=['b.companyNo','=', $relaComNo];
- }
- // if($this->level==3){
- // if($relaComNo!='') $where[]=['b.supplierNo','=', $relaComNo];
- // }
- $count = Db::name('purchease_diff')
- ->alias('a')
- ->leftJoin("good b","b.spuCode=a.good_code")
- ->leftJoin("purchease_order c","c.cgdNo=a.cgdNo")
- ->where($where)
- ->count();
- $total = ceil($count/$size);
- $page = $page >= $total ? $total : $page;
- $list = Db::name('purchease_diff')
- ->alias('a')
- ->leftJoin("good b","b.spuCode=a.good_code")
- ->leftJoin("purchease_order c","c.cgdNo=a.cgdNo")
- ->where($where)
- ->page($page,$size)
- ->order("a.addtime desc")
- ->field("a.*,b.cat_id,c.order_source,c.order_type")
- ->select()
- ->toArray();
- $all_createrid = array_column($list,'apply_id');
- $item = get_company_name_by_uid($all_createrid);
- $data=[];
- foreach ($list as $value){
- $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
- $value['company_name'] = $item[$value['apply_id']]??'';
- //是否具有编辑权限
- $value['is_allow_update'] = 0;
- if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
- $data[]=$value;
- }
- return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
- }
- public function diffinfo(){
- $id = isset($this->post['id']) && $this->post['id'] !=="" ? trim($this->post['id']) :"";
- if($id==""){
- return error_show(1002,"参数id 不能为空");
- }
- $idinf = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
- if($idinf==false){
- return error_show(1002,"未找到采购工差单数据");
- }
- $dn = Db::name('purchease_order')->where(['cgdNo'=>$idinf['cgdNo'],'is_del'=>0])->find();
- if (in_array($dn['order_type'], [3, 4])) {
- $goon = Db::name("good_zixun")->where(["spuCode"=>$dn['spuCode'],"is_del"=>0])->find();
- }else {
- $goon =Db::name('good_basic')->where(['spuCode'=>$dn['spuCode']])->find();
- }
- // $goon = Db::name('good')->where(['spuCode'=>$idinf['good_code']])->find();
- if(empty($goon)){
- return error_show(1002,"未找到商品数据");
- }
- $idinf['wsm_name'] ="";
- $idinf['wsm_supplier'] ="";
- $idinf['wsm_supplierNo'] ="";
- $idinf['wsm_contactor'] ="";
- $idinf['wsm_mobile'] ="";
- $idinf['addr_code'] ="";
- $idinf['wsm_addr'] ="";
- if($dn['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"=>$dn['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();
- $idinf['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
- $idinf['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
- $idinf['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
- $idinf['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
- $idinf['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
- $idinf['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
- $idinf['addr_cn'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
- $idinf['wsm_addr']=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
- }
- $idinf['info'] =$dn;
- $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
- $idinf['can']=$int;
- $idinf['order_type']=$dn['order_type'];
- $idinf['order_source']=$dn['order_source'];
- //取出销售的工差单信息
- $diff = Db::name('sale_diff')->field('id,customer_remark,is_act,remark')->where('cgd_diffid',$id)->find();
- $idinf['diff_customer_remark']=$diff['customer_remark'];
- $idinf['diff_is_act']=$diff['is_act'];
- $idinf['diff_remark']=$diff['remark'];
- return app_show(0,"获取成功",$idinf);
- }
- public function diffstatu(){
- $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
- if($id==""){
- return error_show(1002,"参数id不能为空");
- }
- $dio = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
- if(empty($dio)){
- return error_show(1002,"工差订单未找到");
- }
- $cgd = Db::name("purchease_order")->where(['cgdNo'=>$dio['cgdNo']])->find();
- if(empty($cgd)){
- return error_show(1002,"采购单未找到");
- }
- $salediff=Db::name("sale_diff")->where(["cgd_diffid"=>$dio['id']])->find();
- if($salediff==false){
- return error_show(1002,"销售工差单未找到");
- }
- $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
- if($status===""){
- return error_show(1002,"参数status不能为空");
- }
- Db::startTrans();
- try{
- $temp = $dio['status'];
- $dio['status']=$status;
- $dio['updatetime']=date("Y-m-d H:i:s");
- $item = Db::name("purchease_diff")->save($dio);
- if ($item){
- if($status==2){
- $cgd['diff_fee'] =$dio['diff_price'];
- $cgd['diff_weight'] =$dio['diff_weight'];
- $cgd['updatetime'] =date("Y-m-d H:i:s");
- $cgp=Db::name("purchease_order")->save($cgd);
- if($cgp==false){
- Db::rollback();
- return error_show(1002,"采购单更新失败");
- }
- if($salediff['is_act']==1){
- $saled=[
- "diff_fee"=>$salediff['diff_price'],
- "diff_weight"=>$salediff['diff_weight'],
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $up =Db::name("sale")->where(["orderCode"=>$salediff['orderCode']])->save($saled);
- if($up==false){
- Db::rollback();
- return error_show(1002,"销售单更新失败");
- }
- }
- }
- $sto = ["order_code" => $dio['cgdNo'], "status" => 1, "action_remark" => '', "action_type" => "create"];
- ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $sto, "CGGCD", $status, $this->post);
- $process = ["order_code" => $dio['cgdNo'], "order_id" => $dio['id'], "order_status" => $status, "order_type" => 'CGGCD', "before_status" => 1, 'holder_id' => $dio['apply_id']];
- ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
- Db::commit();
- return error_show(0,"更新成功");
- }else{
- Db::rollback();
- return error_show(1002,"更新失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1002,$e->getMessage()."|".$e->getLine());
- }
- }
- //修改采购单的实时金价
- public function editGoldPrice()
- {
- $param = $this->request->only(['cgdNo', 'gold_price'], 'post', 'trim');
- $val = Validate::rule(['cgdNo|采购单号' => 'require', 'gold_price|实时金价' => 'require|float']);
- if (!$val->check($param)) return error_show(1004, $val->getError());
- //采购单
- $data = Db::name("purchease_order")
- ->field('id,demo_fee,good_num,weight,open_fee,teach_fee,pakge_fee,mark_fee,cert_fee,nake_fee,delivery_fee,status')
- ->where(["cgdNo" => $param['cgdNo'], "is_del" => 0])
- ->find();
- if (empty($data)) return error_show(1004, "未找到该采购单数据");
- if ($data['status'] != 0) return error_show(1005, '该状态下不允许修改实时金价');
- $sale_price = round($data['demo_fee'] / $data['good_num'] + $data['open_fee'] / $data['good_num'] +$data['weight'] * $param["gold_price"] + $data['teach_fee'] * $data['weight'] +
- $data['pakge_fee'] + $data['mark_fee'] + $data['cert_fee'] +$data['nake_fee'] + $data['delivery_fee'],2);
- $total_price = round($data['good_num']*$sale_price,2);
- $rs = Db::name("purchease_order")
- ->where('id', $data['id'])
- ->update([
- 'updatetime' => date('Y-m-d H:i:s'),
- 'gold_price' => $param['gold_price'],
- "good_price"=>$sale_price,
- "total_fee"=>$total_price,
- "origin_total"=>$total_price
- ]);
- return $rs ? app_show(0, '修改采购单实时金价成功') : error_show(1005, '修改采购单实时金价失败');
- }
- //采购单导出
- public function exportCgdList()
- {
- $cgdNos = $this->request->post('cgdNos', [], 'trim');
- if (empty($cgdNos)) return error_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 采购货款,"" as 购买方公司,s.arrive_time as 到货时间,"" 税率,oa.addr 收货地址,oa.mobile 电话,oa.contactor 联系人,s.platform_order 平台订单编号,oa.receipt_quantity 地址发货数量, oa.addr_code,po.order_type,s.supplierNo')
- ->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();
- $all_supplierNo = array_column($list, 'supplierNo');
- $userCommon = \app\admin\common\User::getIns();
- $tmp = $userCommon->handle('getCodeAndName', ['code' => $all_supplierNo]);
- foreach ($list as &$value) {
- if (in_array($value['order_type'], [3, 4])) {
- $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['收货地址'];
- }
- $value['购买方公司'] = $tmp['data'][$value['supplierNo']] ?? '';
- unset($value['addr_code']);
- unset($value['order_type']);
- unset($value['supplierNo']);
- }
- $headerArr = array_keys($list[0]);
- excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
- }
- //获取采购单修改供应商的记录列表
- public function getChangeSupplierList()
- {
- $param = $this->request->only(['cgdNo' => '', 'start' => '', 'end' => '', 'creater' => '', 'page' => 1, 'size' => 15, 'company_name' => ''], 'post', 'trim');
- $where = [['a.is_del', '=', 0]];
- if (!empty($param['cgdNo'])) $where[] = ['a.cgdNo', 'like', '%' . $param['cgdNo'] . '%'];
- if (!empty($param['start'])) $where[] = ['a.addtime', '>=', $param['start']];
- if (!empty($param['end'])) $where[] = ['a.addtime', '<=', $param['end'] . ' 23:59:59'];
- if (!empty($param['creater'])) $where[] = ['a.creater', 'like', '%' . $param['creater'] . '%'];
- if (!empty($param['company_name'])) $where[] = ["a.creater_id", 'in', get_company_item_user_by_name($param['company_name'])];
- $count = Db::name('purchease_change_supplier_log')
- ->alias('a')
- ->where($where)
- ->count('a.id');
- $list = Db::name('purchease_change_supplier_log')
- ->alias('a')
- ->where($where)
- ->order('a.id', 'desc')
- ->page($param['page'], $param['size'])
- ->select()
- ->toArray();
- $all_createrid =empty($list)?[]: array_column($list,'creater_id');
- $item = get_company_name_by_uid($all_createrid);
- foreach ($list as &$value){
- $val['company_name']=$item[$value['creater_id']]??'';
- }
- return app_show(0, '获取成功', ['list' => $list, 'count' => $count]);
- }
- // public function create(){
- // $customer = isset($this->post['customer_code'])&&$this->post['customer_code']!="" ?trim($this->post['customer_code']):"";
- // if($customer==""){
- // return error_show(1004,"参数customer_code不能为空");
- // }
- // $supplier = isset($this->post['supplier_code'])&&$this->post['supplier_code']!="" ?trim($this->post['supplier_code']):"";
- // if($supplier==""){
- // return error_show(1004,"参数supplier_code不能为空");
- // }
- // $good_code = isset($this->post['good_code']) && $this->post['good_code']!=""?trim($this->post['good_code']):"";
- // if($good_code==""){
- // return error_show(1004,"参数good_code不能为空");
- // }
- // $good_num = isset($this->post['good_num'])&& $this->post['good_num']!==""? intval($this->post['good_num']):"";
- // if($good_num===""){
- // return error_show(1004,"参数good_code不能为空");
- // }
- // $file_url = isset($this->post['file_url'])&& $this->post['file_url']!==""? trim($this->post['file_url']):"";
- // if($file_url===""){
- // return error_show(1004,"参数file_url不能为空");
- // }
- // $mark =isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
- // if($mark==""){
- // return error_show(1004,"参数remark不能为空");
- // }
- //
- // $data=[
- // ""
- // ];
- // }
- }
|