123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- declare (strict_types = 1);
- namespace app\cxinv\command;
- use app\cxinv\model\Invoice;
- use app\cxinv\model\InvoiceInfo;
- use app\user\model\Business;
- use app\user\model\Supplier;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;use think\facade\Cache;
- class CheckInvoice extends Command
- {
- protected $datetime;
- protected $InvoiceNum;
- protected $taxStatus = [
- 'Y'=>'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('checkInvoicePlus')==1)return;
- $list = $this->getInvoice();
- $this->InvoiceNum =[
- 'special'=>'004',
- 'normal'=>'007',
- 'special_electronic'=>'028',
- 'blockchain_electronic'=>'016',
- 'electronic'=>'026',
- 'fully_digitalized_special_electronic'=>'021',
- 'fully_digitalized_normal_electronic'=>'022',
- ];
- $this->output=$output;
- while ($list->valid()){
- Cache::set('checkInvoicePlus',1,1800);
- $invoice = $list->current();
- $this->taxCheck($invoice);
- $this->output->writeln('['.date(DATE_W3C).']'.$invoice['hpNo'].'验票');
- $list->next();
- }
- Cache::set('checkInvoicePlus',0);
- }
- protected function getInvoice(){
- $list = Invoice::where([['status','=',0],['updatetime','<=',date('Y-m-d H:i:s')]])->cursor();
- foreach ($list as $item){
- yield $item;
- }
- }
- protected function taxCheck($invoice){
- $Tax =new \TaxInvoice(env('tax.appkey'),env('tax.appsecret'),env('tax.entcode'));
- $invNum= $this->InvoiceNum[$invoice['invoice_type']];
- $subtotal= $invoice['invoice_subtotal'];
- $opentime = date('Ymd',strtotime($invoice['open_date']));
- if(!in_array($invNum,['012','022','028']))$checkNum= substr($invoice['check_code'],-6);
- else $checkNum= $invoice['check_code'];
- if(in_array($invNum,['021','022'])) $subtotal= $invoice['invoice_total'];
- $result=$Tax->CheckInvoiceSingle($checkNum,$subtotal,$invoice['invoice_code'],$invoice['invoice_number'],$opentime,$invNum);;
- $this->output->writeln('[' . date(DATE_W3C) . ']' . json_encode($result,JSON_UNESCAPED_UNICODE));
- Invoice::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){
- Invoice::rollback();
- echo $e->getMessage();
- }
- Invoice::commit();
- }
- protected function changeFiled($data,$invoice){
- $save = [
- 'payNo'=>$invoice['InvCode'],
- 'hpNo'=>'',
- 'type'=>$invoice['invoice_type'],
- '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'])
- ];
- InvoiceInfo::create($save);
- $buyerinfo = Business::where(['inv_code'=>$save['buyer_id']])->findOrEmpty();
- $update=[];
- if(!$buyerinfo->isEmpty()){
- $update=["companyNo"=>$buyerinfo->companyNo,"companyName"=>$buyerinfo->company];
- }else $update=['companyNo'=>'','companyName'=>$save['buyer_name']];
- $saller= Supplier::where(['registercode'=>$save['seller_id']])->findOrEmpty();
- if (!$saller->isEmpty()){
- $update['supplierNo'] = $saller->code;
- $update['supplierName'] = $saller->name;
- }else $update['supplierName'] = $save['seller_name'];
- $up= Invoice::where(['id'=>$invoice['id']])->update(array_merge($update,[
- 'status'=>1,
- "invoice_total"=>$data['jshj'],
- 'invoice_subtotal'=>$data['fpje'],
- 'inv_status'=>$this->taxStatus[$data['zfbz']]??0,
- 'remark'=>'验票成功'
- ]));
- if($up==false) throw new \Exception('更新失败');
- }
- protected function checkItem($detail){
- if(!empty($detail)) return array_map(function ($item) {
- return [
- 'name'=>$item['hwmc'],
- 'unit'=>$item['jldw'],
- 'specification'=>$item['ggxh'],
- '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=''){
- Invoice::where(['id'=>$id])->update(['status'=>5,'remark'=>$msg]);
- }
- protected function lastUpdate($id){
- Invoice::where(['id'=>$id])->update(['status'=>0,'remark'=>'验证失败,等待第二天验证', 'updateTime'=> date('Y-m-d H:i:s',strtotime('+1 day'))]);
- }
- }
|