checkJob($data); if(!$check){ $job->delete(); return; } echo "执行识别数据"; $do=$this->doJob($data,$job); if($do){ $job->delete(); }else{ if ($job->attempts() > 3) { //通过这个方法可以检查这个任务已经重试了几次了 $job->delete(); $this->failed($data); } } Log::write(var_export($job->attempts()),true); } public function failed($data){ Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save(['status'=>2]); $num =Db::name("invoice_img")->where([['payid',"=", $data['payid']],["is_del","=",0],['status',"=",2]]) ->count(); if($num>0){ Db::name("pay_stages")->where("id","=",$data['payid'])->save(["status"=>9]); } } private function doJob($data,$job){ $result = OcrByInvoice(["url"=>$data['image']]); $result = json_decode($result,true); if(isset($result['code'])&&$result['code']==0 ){ if(isset($object['error_code'])){ if ($job->attempts() > 3) { //通过这个方法可以检查这个任务已经重试了几次了 $job->delete(); $this->failed($data); }else{ Db::name("pay_stages")->where("id","=",$data['payid'])->save(["remark"=>json_encode($result,JSON_UNESCAPED_UNICODE)]); return false; } } $object = $result['data']["result"]; try{ $ist = Db::name("invoice")->where([['code',"=",$object['code']],["number","=",$object['number']]]) ->find(); if($ist){ if($ist['id']!=$data['id']){ Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save(["remark"=>"已存在发票{$object['code']}"]); $sql ="update cfp_pay_stages set remark=CONCAT(remark,'已存在发票{$object['code']};') where id={$data['payid']}"; Db::query($sql); $job->delete(); $this->failed($data); return false; } } $obje=json_encode($object,JSON_UNESCAPED_UNICODE); Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save(["status"=>0,'invoice_info'=>$obje,"remark"=>""]); $info = Db::name("invoice_info")->where("invid","=",$data['id'])->find(); if(empty($info)){ $info=[]; } Log::write(Db::name("invoice_img")->getLastSql()); $info['payid']= $data['payid']; $info['invid']= $data['id']; $info['type']= $object['type']; $info['code']= $object['code']; $info['number']= $object['number']; $info['check_code']= $object['check_code']; $info['issue_date']= $object['issue_date']; $info['buyer_name']= $object['buyer_name']; $info['buyer_id']= $object['buyer_id']; $info['buyer_address']= $object['buyer_address']; $info['buyer_bank']= $object['buyer_bank']; $info['seller_name']= $object['seller_name']; $info['seller_id']= $object['seller_id']; $info['seller_address']= $object['seller_address']; $info['seller_bank']= $object['seller_bank']; // $info['subtotal_amount']= str_replace("¥",'',$object['subtotal_amount']); // $info['subtotal_tax']=str_replace("¥",'',$object['subtotal_tax']); // $info['total']= str_replace("¥",'',$object['total']); $info['subtotal_amount']= $object['subtotal_amount']; $info['subtotal_tax']=$object['subtotal_tax']; $info['total']= $object['total']; $info['item_list']= json_encode($object['item_list'],JSON_UNESCAPED_UNICODE); $info['status']= 0; $info['change_field']= ""; Db::name("invoice_info")->save($info); $num =Db::name("invoice_img")->where([['payid',"=", $data['payid']],["is_del","=",0],['status',"<>",0]]) ->count(); if($num<=0){ // $this->lastCheck($data); Db::name("pay_stages")->where("id","=",$data['payid'])->save(["status"=>5,"remark"=>""]); } return true; }catch (\Exception $e){ echo $e->getMessage(); return false; } } } private function checkJob($data){ $list= Db::name("invoice_img")->where("id","=",$data['id'])->find(); if($list['status']==0){ return false; } return true; } private function CheckInvoice($invocie,$data){ $paystage = Db::name("pay_stages")->where("id","=",$data['payid'])->find(); $remak=""; if($paystage['companyNo']!=""){ $company = Db::name("company_info")->where("companyNo","=",$paystage['companyNo'])->find(); if(!empty($company)){ if($company['company_name']!=$invocie['buyer_name']){ $remak.="\r\n 发票购买方企业名称不匹配"; } if($company['company_license']!=$invocie['buyer_id']){ $remak.="\r\n 发票购买方企业纳税识别号不匹配"; } if(stripos($company['company_address'].$company['mobile'],trim($invocie['buyer_address']))===false){ $remak.="\r\n 发票购买方企业地址不匹配"; } if(stripos($company['bank_name'].$company['bankNo'],trim($invocie['buyer_bank']))===false){ $remak.="\r\n 发票购买方企业银行账户不匹配"; } } } // var_dump($company['company_address'].$company['mobile'],trim($invocie['buyer_address'])); $invoiceInfo = Db::name("invoice")->where([["payid","=",$data['payid']],['status',"=",0]])->find(); if(!empty($invoiceInfo)){ if($invoiceInfo['seller_name']!=$invocie['seller_name']){ $remak.="\r\n 发票销售方企业名称不匹配"; } if($invoiceInfo['seller_id']!=$invocie['seller_id']){ $remak.="\r\n 发票销售方企业纳税识别号不匹配"; } if($invoiceInfo['seller_address']!=$invocie['seller_address']){ $remak.="\r\n 发票销售方企业地址不匹配"; } if($invoiceInfo['seller_bank']!=$invocie['seller_bank']){ $remak.="\r\n 发票销售方企业银行账户不匹配"; } } //var_dump($remak); if($remak!=""){ $paystage['remark'].=$remak; $paystage['status']=9; $paystage['updatetime']=date("Y-m-d H:i:s"); Db::name("pay_stages")->save($paystage); Log::write(Db::name("pay_stages")->getLastSql(),"pay"); return false; }else{ return true; } } private function lastCheck($data){ $list = Db::name("invoice")->where("payid","=",$data["payid"])->select(); $isbool = True; $remark=""; $rate=0; $total = 0;//发票总额; $invid=[]; $paystage = Db::name("pay_stages")->where("id","=",$data["payid"])->find(); if(!empty($list)){ foreach ($list as $key=>$value){ $item= json_decode($value['item_list'],true); $total +=$value['total']; $temp_total = number_format($value['subtotal_amount']+$value['subtotal_tax'],2); if($temp_total!=$value["total"]){ $isbool=false; $remark.="{$value['id']}发票价税总计不匹配;"; $invid[]=$value['id']; } if(!empty($item)){ $totla_amount =0; $totla_tax =0; $totla_fee =0; foreach ($item as $k=>$val){ $rate_tax=substr($val['tax_rate'],0,-1); $temp_tax = number_format($val['amount']*$val['tax_rate']/100,2); if($temp_tax!=$val['tax']){ $isbool=false; $remark.="{$value['id']}发票明细税额不匹配;"; $invid[]=$value['id']; } $totla_amount+=$val['amount']; $totla_tax+=$val['tax']; if($rate==0){ $rate=$rate_tax; }else{ if($rate_tax!=$rate){ $isbool=false; $remark.="{$value['id']}发票税率不匹配;"; $invid[]=$value['id']; } } } $totla_amount = number_format($totla_amount,2); $totla_tax = number_format($totla_tax,2); if($totla_amount!==$value['subtotal_amount']){ $isbool=false; $remark.="{$value['id']}发票明细总计金额不匹配;"; $invid[]=$value['id']; } if($totla_tax!==$value['subtotal_tax']){ $isbool=false; $remark.="{$value['id']}发票总计税额不匹配;"; $invid[]=$value['id']; } } } $paytotal = number_format($paystage['pay_fee'],2); $total = number_format($total,2); if($paytotal!=$total){ $isbool=false; $remark.="{$value['id']}发票总计金额不匹配;"; $invid[]=$value['id']; } var_dump($remark,$invid); if($isbool){ Db::name("pay_stages")->where("id","=",$data['payid'])->save(["status"=>5,"updatetime"=>date("Y-m-d H:i:s")]); }else{ $paystage['remark'].=$remark; $paystage['status']=9; $paystage['updatetime']=date("Y-m-d H:i:s"); Db::name("pay_stages")->where("id","=",$data['payid'])->save($paystage); Db::name("invoice_img")->where("id","in",$invid)->save(['status'=>2,"updatetime"=>date("Y-m-d H:i:s")]); } } } }