CheckInvoice.php 6.0 KB

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