CheckInvoice.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\command;
  4. use app\admin\model\CompanyInfo;use app\admin\model\InvoiceInfo;use app\admin\model\Pay;use app\admin\model\PayInvoice;use app\admin\model\SupplierInfo;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;use think\facade\Config;use think\facade\Db;
  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. if(Cache::get('checkInvoiceP')==1)return;
  27. $list = $this->getInvoice();
  28. $invoice =Config::get('invoiceType');
  29. $this->InvoiceNum = $invoice['KingInvoice'];
  30. $this->output=$output;
  31. while ($list->valid()){
  32. Cache::set('checkInvoiceP',1,1800);
  33. $invoice = $list->current();
  34. $this->checkTicket($invoice);
  35. $this->output->writeln("[".date(DATE_W3C)."]".$invoice['hpNo']."验票");
  36. $list->next();
  37. }
  38. Cache::set('checkInvoiceP',0);
  39. }
  40. protected function getInvoice(){
  41. $list = PayInvoice::where([["status","in",[1,9]],["open_time","<",$this->datetime],["updatetime","<=",date("Y-m-d H:i:s")],['is_del',"=",0]])->cursor();
  42. foreach ($list as $item){
  43. yield $item;
  44. }
  45. }
  46. protected function checkTicket($invoice){
  47. $pay = Pay::where(["payNo"=>$invoice['payNo'],"is_del"=>0,"status"=>2])->findOrEmpty();
  48. if(!$pay->isEmpty()){
  49. if($pay->winv_fee<=0) return PayInvoice::where(["id"=>$invoice['id']])->update(["status"=>8,"check_remark"=>"待开票金额不足","updatetime"=>date("Y-m-d H:i:s")]);
  50. $this->taxCheck($invoice);
  51. }
  52. }
  53. protected function taxCheck($invoice){
  54. $invoiceConf=Config::get('invoice');
  55. $iinvoicenfo = $invoiceConf['91110113MA004JNJ28'];
  56. $Tax =new \TaxInvoice($iinvoicenfo['appKey'],$iinvoicenfo['appSecret'],$iinvoicenfo['entCode']);
  57. $opentime = date("Ymd",strtotime($invoice['open_time']));
  58. $checkNum= $invoice['checkNumber'];
  59. $invNum= $this->InvoiceNum[$invoice['invoiceType']];
  60. $subtotal= $invoice['inv_subtotal_amount'];
  61. if(!in_array($invNum,['012','022',"028"]))$checkNum= substr($invoice['checkNumber'],-6);
  62. if(in_array($invNum,['021','022'])) $subtotal= $invoice['inv_amount'];
  63. $result=$Tax->CheckInvoiceSingle( $checkNum,$subtotal,$invoice['invoiceCode'],$invoice['invoiceNumber'],$opentime,$invNum);;
  64. $this->output->writeln('[' . date(DATE_W3C) . ']' . json_encode($result,JSON_UNESCAPED_UNICODE));
  65. Db::startTrans();
  66. try{
  67. if(isset($result['code']) && $result['code']=='0000' and count($result['data'])>0){
  68. if($result['data']['cyjg']=='0001'){
  69. $data= $result['data'];
  70. $this->changeFiled($data,$invoice);
  71. }elseif (in_array($result['data']['cyjg'],['0002','1014'])){
  72. $this->lastUpdate($invoice['id']);
  73. }else $this->faild($invoice['id'],$result['data']['cyjgxx']);
  74. }else $this->faild($invoice['id']);
  75. }catch (\Exception $e){
  76. Db::rollback();
  77. echo $e->getMessage();
  78. }
  79. Db::commit();
  80. }
  81. protected function changeFiled($data,$invoice){
  82. $save = [
  83. "payNo"=>$invoice['payNo'],
  84. "hpNo"=>$invoice['hpNo'],
  85. "type"=>$invoice['invoiceType'],
  86. "title"=>"",
  87. "code"=>$data['fpdm'],
  88. "check_code"=>$data['jym'],
  89. "number"=>$data['fphm'],
  90. "issue_date"=>$data['kprq'],
  91. "encryption_block"=>"",
  92. "buyer_name"=>$data['gmfmc'],
  93. "buyer_id"=>$data['gmfsbh'],
  94. "buyer_address"=>$data['gmfdzdh'],
  95. "buyer_bank"=>$data['gmfyhzh'],
  96. "seller_name"=>$data['xhfmc'],
  97. "seller_id"=>$data['xhfsbh'],
  98. "seller_address"=>$data['xhfdzdh'],
  99. "seller_bank"=>$data['xhfyhzh'],
  100. "subtotal_amount"=>$data['fpje'],
  101. "subtotal_tax"=>$data['fpse'],
  102. "machine_number"=>$data['jqbh'],
  103. "status"=>$this->taxStatus[$data['zfbz']]??0,
  104. "total"=>$data['jshj'],
  105. "total_in_words"=>"",
  106. "receiver"=>$data['skr'],
  107. "issuer"=>$data['kpr'],
  108. "reviewer"=>$data['fhr'],
  109. "remarks"=>$data['bz'],
  110. "change_field"=>"",
  111. "item_list"=>$this->checkItem($data['detailList'])
  112. ];
  113. $pay = Pay::where(["payNo"=>$invoice['payNo'],"is_del"=>0,"status"=>2])->findOrEmpty();
  114. $inv_fee=($pay->winv_fee <= $save['total'])?$pay->winv_fee:$save['total'];
  115. $pay->winv_fee = $pay->winv_fee - $inv_fee;
  116. $pay->inv_fee = $pay->inv_fee+$inv_fee;
  117. $pay->save();
  118. $buyerCode=CompanyInfo::getRegisterCode($pay->companyNo);
  119. $sellerCode=SupplierInfo::getRegisterCode($pay->supplierNo);
  120. PayInvoice::where(["hpNo"=>$invoice['hpNo'],"status"=>1])->update([
  121. "status"=>11,
  122. "invStatus"=>$this->taxStatus[$data['zfbz']]??0,
  123. "check_remark"=>'',
  124. "inv_fee"=>$inv_fee,
  125. "inv_amount"=>$save['total'],
  126. "buyer_check"=>$buyerCode==$save['buyer_id']?1:2,
  127. "seller_check"=>$sellerCode==$save['seller_id']?1:2,
  128. "inv_subtotal_amount"=>$save['subtotal_amount'],
  129. "updatetime"=>date("Y-m-d H:i:s")
  130. ]);
  131. InvoiceInfo::create($save);
  132. }
  133. protected function checkItem($detail){
  134. if(!empty($detail)) return array_map(function ($item) {
  135. return [
  136. 'name'=>$item['hwmc'],
  137. 'specification'=>$item['ggxh'],
  138. 'unit'=>$item['jldw'],
  139. 'quantity'=>$item['spsl'],
  140. 'unit_price'=>$item['bhsdj'],
  141. 'amount'=>$item['je'],
  142. 'tax'=>$item['se'],
  143. 'tax_rate'=>$item['sl'],
  144. 'license_plate_number'=>$item['ssflbm'],
  145. ];
  146. }, $detail);
  147. return [];
  148. }
  149. protected function faild($id,$msg=''){
  150. PayInvoice::where(["id"=>$id])->update(["status"=>5,"check_remark"=>$msg,"updatetime"=>date("Y-m-d H:i:s")]);
  151. }
  152. protected function lastUpdate($id){
  153. PayInvoice::where(["id"=>$id])->update(['status'=>9,'check_remark'=>'验证失败,等待第二天验证', "updatetime"=> date("Y-m-d H:i:s",strtotime("+1 day"))]);
  154. }
  155. }