'2','N'=>'1','H'=>'3' ]; protected $output; protected function configure() { // 指令配置 $this->setName('checkinvoice') ->setDescription('the checkinvoice command'); } protected function execute(Input $input, Output $output) { $this->datetime = date("Y-m-d"); if(Cache::get('checkInvoiceP')==1)return; $list = $this->getInvoice(); $invoice =Config::get('invoiceType'); $this->InvoiceNum = $invoice['KingInvoice']; $this->output=$output; while ($list->valid()){ Cache::set('checkInvoiceP',1,1800); $invoice = $list->current(); $this->checkTicket($invoice); $this->output->writeln("[".date(DATE_W3C)."]".$invoice['hpNo']."验票"); $list->next(); } Cache::set('checkInvoiceP',0); } protected function getInvoice(){ $list = PayInvoice::where([["status","in",[1,9]],["open_time","<",$this->datetime],["updatetime","<=",date("Y-m-d H:i:s")],['is_del',"=",0]])->cursor(); foreach ($list as $item){ yield $item; } } protected function checkTicket($invoice){ $pay = Pay::where(["payNo"=>$invoice['payNo'],"is_del"=>0,"status"=>2])->findOrEmpty(); if(!$pay->isEmpty()){ if($pay->winv_fee<=0) return PayInvoice::where(["id"=>$invoice['id']])->update(["status"=>8,"check_remark"=>"待开票金额不足","updatetime"=>date("Y-m-d H:i:s")]); $this->taxCheck($invoice); } } protected function taxCheck($invoice){ $invoiceConf=Config::get('invoice'); $iinvoicenfo = $invoiceConf['91110113MA004JNJ28']; $Tax =new \TaxInvoice($iinvoicenfo['appKey'],$iinvoicenfo['appSecret'],$iinvoicenfo['entCode']); $opentime = date("Ymd",strtotime($invoice['open_time'])); $checkNum= $invoice['checkNumber']; $invNum= $this->InvoiceNum[$invoice['invoiceType']]; $subtotal= $invoice['inv_subtotal_amount']; if(!in_array($invNum,['012','022',"028"]))$checkNum= substr($invoice['checkNumber'],-6); if(in_array($invNum,['021','022'])) $subtotal= $invoice['inv_amount']; $result=$Tax->CheckInvoiceSingle( $checkNum,$subtotal,$invoice['invoiceCode'],$invoice['invoiceNumber'],$opentime,$invNum);; $this->output->writeln('[' . date(DATE_W3C) . ']' . json_encode($result,JSON_UNESCAPED_UNICODE)); Db::startTrans(); try{ if(isset($result['code']) && $result['code']=='0000' and count($result['data'])>0){ if($result['data']['cyjg']=='0001'){ $data= $result['data']; $this->changeFiled($data,$invoice); }elseif (in_array($result['data']['cyjg'],['0002','1014'])){ $this->lastUpdate($invoice['id']); }else $this->faild($invoice['id'],$result['data']['cyjgxx']); }else $this->faild($invoice['id']); }catch (\Exception $e){ Db::rollback(); echo $e->getMessage(); } Db::commit(); } protected function changeFiled($data,$invoice){ $save = [ "payNo"=>$invoice['payNo'], "hpNo"=>$invoice['hpNo'], "type"=>$invoice['invoiceType'], "title"=>"", "code"=>$data['fpdm'], "check_code"=>$data['jym'], "number"=>$data['fphm'], "issue_date"=>$data['kprq'], "encryption_block"=>"", "buyer_name"=>$data['gmfmc'], "buyer_id"=>$data['gmfsbh'], "buyer_address"=>$data['gmfdzdh'], "buyer_bank"=>$data['gmfyhzh'], "seller_name"=>$data['xhfmc'], "seller_id"=>$data['xhfsbh'], "seller_address"=>$data['xhfdzdh'], "seller_bank"=>$data['xhfyhzh'], "subtotal_amount"=>$data['fpje'], "subtotal_tax"=>$data['fpse'], "machine_number"=>$data['jqbh'], "status"=>$this->taxStatus[$data['zfbz']]??0, "total"=>$data['jshj'], "total_in_words"=>"", "receiver"=>$data['skr'], "issuer"=>$data['kpr'], "reviewer"=>$data['fhr'], "remarks"=>$data['bz'], "change_field"=>"", "item_list"=>$this->checkItem($data['detailList']) ]; $pay = Pay::where(["payNo"=>$invoice['payNo'],"is_del"=>0,"status"=>2])->findOrEmpty(); $inv_fee=($pay->winv_fee <= $save['total'])?$pay->winv_fee:$save['total']; $pay->winv_fee = $pay->winv_fee - $inv_fee; $pay->inv_fee = $pay->inv_fee+$inv_fee; $pay->save(); $buyerCode=CompanyInfo::getRegisterCode($pay->companyNo); $sellerCode=SupplierInfo::getRegisterCode($pay->supplierNo); PayInvoice::where(["hpNo"=>$invoice['hpNo'],"status"=>1])->update([ "status"=>11, "invStatus"=>$this->taxStatus[$data['zfbz']]??0, "check_remark"=>'', "inv_fee"=>$inv_fee, "inv_amount"=>$save['total'], "buyer_check"=>$buyerCode==$save['buyer_id']?1:2, "seller_check"=>$sellerCode==$save['seller_id']?1:2, "inv_subtotal_amount"=>$save['subtotal_amount'], "updatetime"=>date("Y-m-d H:i:s") ]); InvoiceInfo::create($save); } protected function checkItem($detail){ if(!empty($detail)) return array_map(function ($item) { return [ 'name'=>$item['hwmc'], 'specification'=>$item['ggxh'], 'unit'=>$item['jldw'], 'quantity'=>$item['spsl'], 'unit_price'=>$item['bhsdj'], 'amount'=>$item['je'], 'tax'=>$item['se'], 'tax_rate'=>$item['sl'], 'license_plate_number'=>$item['ssflbm'], ]; }, $detail); return []; } protected function faild($id,$msg=''){ PayInvoice::where(["id"=>$id])->update(["status"=>5,"check_remark"=>$msg,"updatetime"=>date("Y-m-d H:i:s")]); } protected function lastUpdate($id){ PayInvoice::where(["id"=>$id])->update(['status'=>9,'check_remark'=>'验证失败,等待第二天验证', "updatetime"=> date("Y-m-d H:i:s",strtotime("+1 day"))]); } }