setName('generrateinvoice') ->setDescription('the generrateinvoice command'); } protected function execute(Input $input, Output $output) { $generrateinvoice= Cache::store("redis")->get("generrateinvoice"); if($generrateinvoice==1) return ''; Cache::store("redis")->set("generrateinvoiceo",1,180); Db::startTrans(); // 指令输出 try { $data =$this->GetAll(); if(empty($data)){ Db::rollback(); return ''; } $date=date("Y-m-d H:i:s"); $seller_code =$this->isTicket(); $fail=[]; $interface =[]; foreach ($data as $value){ $invNo=$value['invNo']; if(!in_array($value['XHFSBH'],$seller_code)){ Db::name("invoice_interface")->where(['invNo'=>$value['invNo']])->update(["status"=>3,"remark"=>"不支持金税","updatetime"=>date("Y-m-d H:i:s")]); $fail[]=$value['invNo']; } $fplxdm= $value['FPLXDM']; if (!key_exists($value['XHFSBH']."-".$value['FPLXDM'],$interface)){ $pch =randomkeys(26).date("YmdHis"); $interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDZXX']=[]; $interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDPCXX']=['DDQQPCH'=>$pch,'KPFS'=>"0",'NSRSBH'=>$value['XHFSBH'],"FPLXDM"=>$value['FPLXDM']]; }else{ $pch =$interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDPCXX']['DDQQPCH']; } Db::name("invoice_interface")->where(['invNo'=>$value['invNo']])->update(['DDQQPCH'=>$pch,'DDQQLSH'=>$value['DDQQLSH'],"updatetime"=>date("Y-m-d H:i:s")]); $temp=[]; unset($value['invNo']); unset($value['KPFS']); unset($value['FPLXDM']); unset($value['DDQQPCH']); $temp["DDMXXX"]=$value['DDMXXX']; unset($value['DDMXXX']); $temp["DDTXX"]=$value; $interface[$value['XHFSBH']."-".$fplxdm]['DDZXX'][]=$temp; $output->writeln( "【 $date 】开票单号".$invNo); } $this->CheckInvoice($interface); $output->writeln( "【 $date 】开票申请成功"); Db::commit(); Cache::store("redis")->set("generrateinvoice",0); }catch (\Exception $e){ Cache::store("redis")->set("generrateinvoice",0); Db::rollback(); $output->writeln( "【 $date 】开票申请失败".$e->getMessage()); } Cache::store("redis")->set("generrateinvoice",0); } protected function GetAll(){ $list =Db::name("invoice_interface")->json(["DDMXXX"])->lock(true)->withoutField("id,status,is_check,result,remark,addtime,updatetime")->limit(1)->where(["status"=>0])->select()->toArray(); return $list; } protected function isTicket(){ $company =Db::name("company_info")->where(["out_ticket"=>1,'is_del'=>0])->column("company_license"); return $company; } protected function CheckInvoice($data){ if(empty($data)){ throw new \Exception("开票数据不能为空"); } $invoiceConf=Config::get('invoice'); // $tax =new TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']); $tax=[]; foreach ($data AS $value){ if(!isset($tax[$value['DDPCXX']['NSRSBH']])){ if(!isset($invoiceConf[$value['DDPCXX']['NSRSBH']])){ Db::name('invoice_interface')->where(['DDQQPCH'=>$value['DDPCXX']['DDQQPCH']])->update (['status'=>4,'remark'=>"金税配置不存在",'updatetime'=>date('Y-m-d H:i:s')]); continue; } $invoice=$invoiceConf[$value['DDPCXX']['NSRSBH']]; $tax[$value['DDPCXX']['NSRSBH']] = new TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']); } $result = $tax[$value['DDPCXX']['NSRSBH']]->GenerateInvoice($value['DDPCXX'],$value['DDZXX']); if(isset($result['ZTDM'])&&$result['ZTDM']=='010000'){ Db::name("invoice_interface")->where(['DDQQPCH'=>$result['DDQQPCH']])->update(['status'=>1, "remark"=>$result['ZTXX'],"updatetime"=>date("Y-m-d H:i:s",time()+60)]); }else{ Db::name("invoice_interface")->where(['DDQQPCH'=>$value['DDPCXX']['DDQQPCH']])->update(['status'=>4,"remark"=>$result['ZTXX']??$result['message'],"updatetime"=>date("Y-m-d H:i:s")]); } } } }