CheckInvoice.php 7.2 KB

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