CheckInvoice.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\cxinv\command;
  4. use app\cxinv\model\Invoice;use app\cxinv\model\InvoiceInfo;use app\user\model\Business;use app\user\model\Supplier;use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;use think\facade\Cache;
  9. class CheckInvoice extends Command
  10. {
  11. protected $datetime;
  12. protected $InvoiceNum;
  13. protected $taxStatus = [
  14. 'Y'=>'2','N'=>'1','H'=>'3'
  15. ];
  16. protected $output;
  17. protected function configure()
  18. {
  19. // 指令配置
  20. $this->setName('checkinvoice')
  21. ->setDescription('the checkinvoice command');
  22. }
  23. protected function execute(Input $input, Output $output)
  24. {
  25. $this->datetime = date('Y-m-d');
  26. $list = $this->getInvoice();
  27. $this->InvoiceNum =[
  28. 'special'=>'004',
  29. 'normal'=>'007',
  30. 'special_electronic'=>'028',
  31. 'blockchain_electronic'=>'016',
  32. 'electronic'=>'026',
  33. 'fully_digitalized_special_electronic'=>'021',
  34. 'fully_digitalized_normal_electronic'=>'022',
  35. ];
  36. $this->output=$output;
  37. // if(Cache::get('checkInvoiceP')==1)return;
  38. while ($list->valid()){
  39. Cache::set('checkInvoiceP',1,1800);
  40. $invoice = $list->current();
  41. $this->taxCheck($invoice);
  42. $this->output->writeln('['.date(DATE_W3C).']'.$invoice['hpNo'].'验票');
  43. $list->next();
  44. }
  45. Cache::set('checkInvoiceP',0);
  46. }
  47. protected function getInvoice(){
  48. $list = Invoice::where([['status','=',0],['updatetime','<=',date('Y-m-d H:i:s')]])->cursor();
  49. foreach ($list as $item){
  50. yield $item;
  51. }
  52. }
  53. protected function taxCheck($invoice){
  54. $Tax =new \TaxInvoice(env('tax.appkey'),env('tax.appsecret'),env('tax.entcode'));
  55. $invNum= $this->InvoiceNum[$invoice['invoice_type']];
  56. $subtotal= $invoice['invoice_subtotal'];
  57. if(!in_array($invNum,['012','022','028']))$checkNum= substr($invoice['check_code'],-6);
  58. else $checkNum= $invoice['check_code'];
  59. if(in_array($invNum,['021','022'])) $subtotal= $invoice['invoice_total'];
  60. $result=$Tax->CheckInvoiceSingle($checkNum,$subtotal,$invoice['invoice_code'],$invoice['invoice_number'],$invoice['open_date'],$invNum);;
  61. $this->output->writeln('[' . date(DATE_W3C) . ']' . json_encode($result,JSON_UNESCAPED_UNICODE));
  62. Invoice::startTrans();
  63. try{
  64. if(isset($result['code']) && $result['code']=='0000' and count($result['data'])>0){
  65. if($result['data']['cyjg']=='0001'){
  66. $data= $result['data'];
  67. $this->changeFiled($data,$invoice);
  68. }elseif (in_array($result['data']['cyjg'],['0002','1014'])){
  69. $this->lastUpdate($invoice['id']);
  70. }else $this->faild($invoice['id'],$result['data']['cyjgxx']);
  71. }else $this->faild($invoice['id']);
  72. }catch (\Exception $e){
  73. Invoice::rollback();
  74. echo $e->getMessage();
  75. }
  76. Invoice::commit();
  77. }
  78. protected function changeFiled($data,$invoice){
  79. $save = [
  80. 'payNo'=>$invoice['InvCode'],
  81. 'hpNo'=>'',
  82. 'type'=>$invoice['invoice_type'],
  83. 'title'=>'',
  84. 'code'=>$data['fpdm'],
  85. 'check_code'=>$data['jym'],
  86. 'number'=>$data['fphm'],
  87. 'issue_date'=>$data['kprq'],
  88. 'encryption_block'=>'',
  89. 'buyer_name'=>$data['gmfmc'],
  90. 'buyer_id'=>$data['gmfsbh'],
  91. 'buyer_address'=>$data['gmfdzdh'],
  92. 'buyer_bank'=>$data['gmfyhzh'],
  93. 'seller_name'=>$data['xhfmc'],
  94. 'seller_id'=>$data['xhfsbh'],
  95. 'seller_address'=>$data['xhfdzdh'],
  96. 'seller_bank'=>$data['xhfyhzh'],
  97. 'subtotal_amount'=>$data['fpje'],
  98. 'subtotal_tax'=>$data['fpse'],
  99. 'machine_number'=>$data['jqbh'],
  100. 'status'=>$this->taxStatus[$data['zfbz']]??0,
  101. 'total'=>$data['jshj'],
  102. 'total_in_words'=>'',
  103. 'receiver'=>$data['skr'],
  104. 'issuer'=>$data['kpr'],
  105. 'reviewer'=>$data['fhr'],
  106. 'remarks'=>$data['bz'],
  107. 'change_field'=>'',
  108. 'item_list'=>$this->checkItem($data['detailList'])
  109. ];
  110. InvoiceInfo::create($save);
  111. $buyerinfo = Business::where(['inv_code'=>$invoice['buyer_id']])->findOrEmpty();
  112. $update=[];
  113. if(!$buyerinfo->isEmpty()){
  114. $update=["companyNo"=>$buyerinfo->companyNo,"companyName"=>$buyerinfo->company];
  115. }else $update=['companyNo'=>'','companyName'=>$save['buyer_name']];
  116. $saller= Supplier::where(['registercode'=>$invoice['seller_id']])->findOrEmpty();
  117. if (!$saller->isEmpty()){
  118. $update['supplierNo'] = $saller->code;
  119. $update['supplierName'] = $saller->name;
  120. }else $update['supplierName'] = $save['seller_name'];
  121. Invoice::where(['id'=>$invoice['id']])->update(array_merge($update,[
  122. 'status'=>1,
  123. 'inv_status'=>$this->taxStatus[$data['zfbz']]??0,
  124. 'updatetime'=>date('Y-m-d H:i:s'),
  125. 'remark'=>'验票成功'
  126. ]));
  127. }
  128. protected function checkItem($detail){
  129. if(!empty($detail)) return array_map(function ($item) {
  130. return [
  131. 'name'=>$item['hwmc'],
  132. 'specification'=>$item['jldw'],
  133. 'unit'=>$item['ggxh'],
  134. 'quantity'=>$item['spsl'],
  135. 'unit_price'=>$item['bhsdj'],
  136. 'amount'=>$item['je'],
  137. 'tax'=>$item['se'],
  138. 'tax_rate'=>$item['sl'],
  139. 'license_plate_number'=>$item['ssflbm'],
  140. ];
  141. }, $detail);
  142. return [];
  143. }
  144. protected function faild($id,$msg=''){
  145. Invoice::where(['id'=>$id])->update(['status'=>5,'remark'=>$msg,'updatetime'=>date('Y-m-d H:i:s')]);
  146. }
  147. protected function lastUpdate($id){
  148. Invoice::where(['id'=>$id])->update(['status'=>0,'remark'=>'验证失败,等待第二天验证', 'updatetime'=> date('Y-m-d H:i:s',strtotime('+1 day'))]);
  149. }
  150. }