123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\ActionLog;
- use app\admin\model\ProcessOrder;
- use think\App;
- 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 =[['is_del',"=",0]];
- $bkcode = isset($this->post['bk_code']) && $this->post['bk_code']!="" ? trim($this->post['bk_code']):"";
- if($bkcode!=""){
- $where[]=['bkcode',"like", "%{$bkcode}%"];
- }
- $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
- if($status!==""){
- // $where['status'] = $status;
- $where[]=['status',"=", $status];
- }
- $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo'])
- :"";
- if($cgdNo!=""){
- //$where['cgdNo'] = Db::Raw("like '%{$cgdNo}%'");
- $where[]=['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[]=['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[]=['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[]=['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[]=['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[]=['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[]=['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")->where(["code"=>$wsm_supplierNo])->find();
- if(empty($supplier)){
- return error_show(1004,"未找到供应商信息");
- }
- $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"supplierNo"=>$wsm_supplierNo])->column("wsm_code");
- $where[]=['wsm_code',"in", $wsmcode];
- }
- $start = isset($this->post['start']) && $this->post['start']!="" ?$this->post['start']:"";
- if($start!=""){
- // $where['addtime'] = Db::raw(" >= '{$start}'");
- $where[]=['addtime',">=", $start];
- }
- $end = isset($this->post['end']) && $this->post['end']!="" ?$this->post['end']:"";
- if($end!=""){
- $where[]=['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[]=['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[]=['lasttime',"<=", $last_end];
- }
- $role=$this->checkRole();
- if(!empty($role['write'])){
- $where[]=["cgder_id","in",$role['write']];
- }
- $count=Db::name("purchease_order")->where($where)->count();
- $total = ceil($count/$size);
- $page = $page >= $total ? $total : $page;
- $list = Db::name("purchease_order")->where($where)->page($page,$size)->order("addtime desc")->select();
- $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|| $value['order_type']==4){
- $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;
- $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")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
- 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($data['order_type']==3|| $data['order_type']==4){
- $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 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])->find();
- if($data['order_type']==3|| $data['order_type']==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{
- $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"]="";
- }
- $supplier = Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
- $goon['supplierName'] = isset($supplier['name'])?$supplier['name']:"";
- $goon['noble_name']=isset($goon['noble_metal'])&&$goon['noble_metal']!=0?$this->noble[$goon['noble_metal']] :"";
- if(isset($goon['companyNo'])&&$goon['companyNo']!=""){
- $company = Db::name("business")->where(["companyNo"=>$goon['companyNo']])->find();
- }
- $goon['company'] = isset($company['company'])?$company['company']:"";
- }
- $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,b.name,b.code,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['name']) ? $wsmcode['name']:"";
- $data['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
- $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;
- 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;
- 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!=""){
- $supplier =Db::name("supplier")->where(['code'=>$supplierNo])->find();
- if(empty($supplier)){
- return error_show(1004,"未找到供应商信息");
- }
- $data['supplierNo'] = $supplierNo;
- $data['supplier_name'] = $supplier['name'];
- }
- $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['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);
- //修改状态,添加待办
- ActionLog::logAdd($this->post['token'], $order, "CGD", $data['status'], $this->post);
- ProcessOrder::AddProcess($this->post['token'], [
- "order_type" => 'CGD',
- "order_code" => $cgdNo,//销售单code
- "order_id" => $data['id'],
- "order_status" => $data['status']
- ]);
- return app_show(0,"更新成功");
- }else{
- return error_show(1004,'更新失败');
- }
- }
- public function status(){
- $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,"未找到数据");
- }
- $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 不能为空");
- }
- if($data['send_status']>1 && $status==0){
- return error_show(1004,"采购单发货中无法取消");
- }
- $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$remark,"action_type"=>"status"];
- $data['status'] = $status;
- $data['remark'] = $remark;
- $data['updatetime'] =date("Y-m-d H:i:s");
- $upd=Db::name("purchease_order")->save($data);
- if($upd){
- $process=["order_code"=>$cgdNo,"order_id"=>$data['id'],"order_status"=>$status,"order_type"=>'CGD'];
- ProcessOrder::AddProcess($this->post['token'],$process);
- ActionLog::logAdd($this->post['token'],$order,'CGD',$status,$this->post);
- return app_show(0,"更新成功");
- }else{
- return error_show(1004,'更新失败');
- }
- }
- public function diffcreat(){
- $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
- //$cgdNo=makeNo("CG");
- $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($cg['order_type']==3|| $cg['order_type']==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不能为空");
- }
- $apply_id =GetUserInfo($token);
- if(empty($apply_id)||$apply_id['code']!=0){
- return error_show(1002,"申请人数据不存在");
- }
- $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
- $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
- $diff_price = round($diff_weight*$cg['gold_price']+$diff_weight*$cg['teach_fee'],2);
- $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'],
- "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){
- $orderCode = Db::name("order_num")->where([["cgdNo","=",$cg['cgdNo']],["status","=",1]])->find();
- if($orderCode!=false){
- $order = Db::name("sale")->where("orderCode", '=', $orderCode['orderCode'])->find();
- if($order==false){
- Db::rollback();
- return error_show(1002,"未找到确认单信息");
- }
- $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'],
- "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){
- // $sto = ["order_code"=>$cg['bkcode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
- // ActionLog::logAdd($this->post['token'],$sto,"ZXGCD",1,$sto);
- // $process=["order_code"=>$cg['bkcode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'ZXGCD'];
- // ProcessOrder::AddProcess($this->post['token'],$process);
- // Db::commit();
- // return error_show(0,"新建成功");
- // }else{
- Db::rollback();
- return error_show(1002,"新建失败");
- }
- }
- 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%"];
- }
- $count = Db::name('purchease_diff')->alias('a')->join("good b","b.spuCode=a.good_code","left")
- ->where($where)->count();
- $total = ceil($count/$size);
- $page = $page >= $total ? $total : $page;
- $list = Db::name('purchease_diff')->alias('a')->join("good b","b.spuCode=a.good_code","left")
- ->where($where)->page($page,$size)->order("a.addtime desc")->field("a.*,b.cat_id")->select();
- $data=[];
- foreach ($list as $value){
- $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
- $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($dn['order_type']==3|| $dn['order_type']==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'];
- //取出销售的工差单信息
- $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,"销售单更新失败");
- }
- }
- }
- 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]);
- 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 采购货款,b.company as 购买方公司,s.arrive_time as 到货时间,po.order_type')
- ->whereIn('po.cgdNo', $cgdNos)
- ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
- ->leftJoin('sale s', 's.orderCode=on.orderCode')
- ->leftJoin('business b', 'b.companyNo=s.supplierNo')
- ->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] : '';
- })
- ->select()
- ->toArray();
- foreach ($list as &$value) {
- if ($value['order_type'] == 3 || $value['order_type'] == 4) {
- $temp = Db::name("good_zixun")
- ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
- ->field('id,specinfo,good_unit')
- ->find();
- $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
- $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
- $speclist = [];
- foreach ($specinfo as $val) {
- $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
- }
- } else {
- $good_unit = Db::name('good_basic')
- ->where(['spuCode' => $value['产品编号']])
- ->value('good_unit', 0);
- $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['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
- $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : '';
- unset($value['order_type']);
- }
- $headerArr = array_keys($list[0]);
- excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
- }
- // 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=[
- // ""
- // ];
- // }
- }
|