CheckInvoice.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 {
  87. if($result['code']=='0000' && $result['message']=='fail'){
  88. sleep(100);
  89. }else$this->faild($invoice['id'],"验票失败");
  90. }
  91. }catch (\Exception $e){
  92. Db::rollback();
  93. echo $e->getMessage();
  94. }
  95. Db::commit();
  96. }
  97. protected function changeFiled($data,$invoice){
  98. $save = [
  99. "payNo"=>$invoice['payNo'],
  100. "hpNo"=>$invoice['hpNo'],
  101. "type"=>$invoice['invoiceType'],
  102. "title"=>"",
  103. "code"=>$data['fpdm'],
  104. "check_code"=>$data['jym'],
  105. "number"=>$data['fphm'],
  106. "issue_date"=>$data['kprq'],
  107. "encryption_block"=>"",
  108. "buyer_name"=>$data['gmfmc'],
  109. "buyer_id"=>$data['gmfsbh'],
  110. "buyer_address"=>$data['gmfdzdh'],
  111. "buyer_bank"=>$data['gmfyhzh'],
  112. "seller_name"=>$data['xhfmc'],
  113. "seller_id"=>$data['xhfsbh'],
  114. "seller_address"=>$data['xhfdzdh'],
  115. "seller_bank"=>$data['xhfyhzh'],
  116. "subtotal_amount"=>$data['fpje'],
  117. "subtotal_tax"=>$data['fpse'],
  118. "machine_number"=>$data['jqbh'],
  119. "status"=>$this->taxStatus[$data['zfbz']]??0,
  120. "total"=>$data['jshj'],
  121. "total_in_words"=>"",
  122. "receiver"=>$data['skr'],
  123. "issuer"=>$data['kpr'],
  124. "reviewer"=>$data['fhr'],
  125. "remarks"=>$data['bz'],
  126. "change_field"=>"",
  127. "item_list"=>$this->checkItem($data['detailList'])
  128. ];
  129. $pay = Pay::where(["payNo"=>$invoice['payNo'],"is_del"=>0,"status"=>2])->findOrEmpty();
  130. $inv_fee=($pay->winv_fee <= $save['total'])?$pay->winv_fee:$save['total'];
  131. $pay->winv_fee = $pay->winv_fee - $inv_fee;
  132. $pay->inv_fee = $pay->inv_fee+$inv_fee;
  133. $up=$pay->save();
  134. if($up==false) throw new \Exception("对账单更新失败");
  135. $buyerCode=CompanyInfo::getRegisterCode($pay->companyNo);
  136. $sellerCode=SupplierInfo::getRegisterCode($pay->supplierNo);
  137. $sa= PayInvoice::where(["hpNo"=>$invoice['hpNo'],"status"=>[1,9]])->update([
  138. "status"=>$pay->pay_type==1?11:2,
  139. "invStatus"=>$this->taxStatus[$data['zfbz']]??0,
  140. "check_remark"=>'',
  141. "inv_fee"=>$inv_fee,
  142. "inv_amount"=>$save['total'],
  143. "buyer_check"=>$buyerCode==$save['buyer_id']?1:2,
  144. "seller_check"=>$sellerCode==$save['seller_id']?1:2,
  145. "inv_subtotal_amount"=>$save['subtotal_amount'],
  146. "updatetime"=>date("Y-m-d H:i:s")
  147. ]);
  148. if($sa==false) throw new \Exception("{$invoice['hpNo']}验票数据更新失败");
  149. $cres=InvoiceInfo::create($save);
  150. if($cres==false) throw new \Exception("{$invoice['hpNo']}验票数据写入失败");
  151. }
  152. protected function checkItem($detail){
  153. if(!empty($detail)) return array_map(function ($item) {
  154. return [
  155. 'name'=>$item['hwmc'],
  156. 'specification'=>$item['ggxh'],
  157. 'unit'=>$item['jldw'],
  158. 'quantity'=>$item['spsl'],
  159. 'unit_price'=>$item['bhsdj'],
  160. 'amount'=>$item['je'],
  161. 'tax'=>$item['se'],
  162. 'tax_rate'=>$item['sl'],
  163. 'license_plate_number'=>$item['ssflbm'],
  164. ];
  165. }, $detail);
  166. return [];
  167. }
  168. protected function faild($id,$msg=''){
  169. PayInvoice::where(["id"=>$id])->update(["status"=>5,"check_remark"=>$msg,"updatetime"=>date("Y-m-d H:i:s")]);
  170. }
  171. protected function lastUpdate($id){
  172. PayInvoice::where(["id"=>$id])->update(['status'=>9,'check_remark'=>'验证失败,等待第二天验证', "updatetime"=> date("Y-m-d H:i:s",strtotime("+1 day"))]);
  173. }
  174. }