CheckInvoice.php 6.4 KB

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