CheckTicket.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\command;
  4. use app\admin\model\CompanyInfo;use app\admin\model\InvoicePool;
  5. use app\admin\model\InvoiceTicket;
  6. use think\facade\Cache;use think\facade\Config;
  7. use think\console\Command;
  8. use think\console\Input;
  9. use think\console\input\Argument;
  10. use think\console\input\Option;
  11. use think\console\Output;use think\facade\Db;
  12. class CheckTicket extends Command
  13. {
  14. protected $dateTime;
  15. protected $output;
  16. protected function configure()
  17. {
  18. // 指令配置
  19. $this->setName('checkticket')
  20. ->setDescription('the checkticket command');
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $this->dateTime = date("Y-m-d H:i:s");
  25. if(Cache::get('checkTicketP')==1 || date("H")<12)return;
  26. Cache::set('checkTicketP',1,1800);
  27. $this->output=$output;
  28. $ticket = $this->getTicket();
  29. while ($ticket->valid()) {
  30. Cache::set('checkTicketP',1,1800);
  31. $data = $ticket->current();
  32. $this->output->writeln('[' . date(DATE_W3C) . ']' . $data['invNo'] . '验票');
  33. $this->checkTicket($data);
  34. $ticket->next();
  35. }
  36. Cache::set('checkTicketP',0);
  37. // 指令输出
  38. // $output->writeln('checkticket');
  39. }
  40. protected function getTicket(){
  41. $ticket = InvoiceTicket::where([["is_del","=",0],["type","=",0],["status","=",0],['open_date','<',date("Y-m-d 00:00:00")],['updatetime','<',date('Y-m-d H:i:s')]])
  42. ->field("id,invNo,type,inv_type,inv_code,inv_number,inv_total,inv_subtotal,open_date,check_code")
  43. ->cursor();
  44. foreach ($ticket as $key => $value) {
  45. yield $value;
  46. }
  47. }
  48. protected function checkTicket($ticket){
  49. $info =InvoicePool::where(["invNo"=>$ticket['invNo'],"status"=>3])->findOrEmpty();
  50. if(!$info->isEmpty()){
  51. $companyInfo = CompanyInfo::where(["companyNo"=>$info->inv_out,"is_del"=>0,"status"=>1])->findOrEmpty();
  52. $isTicket = $companyInfo['out_ticket']??0;
  53. $ticket['is_comon'] = $info->is_comon;
  54. $this->taxCheck($ticket);
  55. }
  56. }
  57. protected function taxCheck($info){
  58. $invoiceConf=Config::get('invoice');
  59. $invoice = $invoiceConf['91110113MA004JNJ28'];
  60. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  61. $opentime = date("Ymd",strtotime($info['open_date']));
  62. $checkNum= $info['check_code'];
  63. $subtotal=$info['inv_subtotal'];
  64. if($info['inv_type']!="016")$checkNum= substr($info['check_code'],-6);
  65. if(in_array($info['inv_type'],["022",'021'])) $subtotal= $info['inv_total'];
  66. $result=$Tax->CheckInvoiceSingle( $checkNum,$subtotal,$info['inv_code'],$info['inv_number'],$opentime,$info['inv_type']);
  67. $this->output->writeln('[' . date(DATE_W3C) . ']' . json_encode($result,JSON_UNESCAPED_UNICODE));
  68. Db::startTrans();
  69. try{
  70. if(isset($result['code']) && $result['code']=='0000' && isset($result['data']) && count($result['data'])>0){
  71. if($result['data']['cyjg']=='0001'){
  72. $data= $result['data'];
  73. $this->changeFiled($data,$info);
  74. }elseif (in_array($result['data']['cyjg'],['0002','1014'])){
  75. $this->lastUpdate($info['invNo']);
  76. }else $this->faild($info['invNo'],$result['data']['cyjgxx']);
  77. }else $this->faild($info['invNo'],'验票失败');
  78. Db::commit();
  79. }catch (\Exception $e){
  80. Db::rollback();
  81. echo $e->getMessage()."\r\n";
  82. }
  83. }
  84. protected function changeFiled($data,$info){
  85. $item = $this->itemFiled($data['detailList']);
  86. $tick = (new InvoiceTicket)->where(['invNo'=>$info['invNo'],'type'=>0,'status'=>0,'is_del'=>0])->findOrEmpty();
  87. if(!$tick->isEmpty()){
  88. $tick->save(['inv_total'=>$data['jshj'],
  89. 'inv_subtotal'=>$data['fpje'],
  90. 'buyer_id'=>$data['gmfsbh'],
  91. 'buyer_title'=>$data['gmfmc'],
  92. 'buyer_addr'=>$data['gmfdzdh'],
  93. 'buyer_bank'=>$data['gmfyhzh'],
  94. 'seller_id'=>$data['xhfsbh'],
  95. 'seller_title'=>$data['xhfmc'],
  96. 'seller_addr'=>$data['xhfdzdh'],
  97. 'seller_bank'=>$data['xhfyhzh'],
  98. 'tax_fee'=>$data['fpse'],
  99. 'remark'=>$data['bz'],
  100. 'issuer'=>$data['kpr'],
  101. 'reciver'=>$data['skr'],
  102. 'reviewer'=>$data['fhr'],
  103. 'item'=>$item,
  104. 'status'=>1,
  105. 'updatetime'=>$this->dateTime,
  106. ]);
  107. }
  108. $wq=InvoicePool::where(['invNo'=>$info['invNo']])->save(["status"=>4,"updatetime"=>$this->dateTime]);
  109. if($wq==false) throw new \Exception("更新发票状态失败");
  110. \app\admin\model\Assoc::subOrder($info['invNo']);
  111. if($info['is_comon']==1)event('comonOrder',['invNo'=>$info['invNo'],'type'=>'inv']);
  112. }
  113. protected function itemFiled($detail){
  114. if(!empty($detail)) return array_map(function ($item) {
  115. return [
  116. 'XMMC'=>$item['hwmc'],
  117. 'DW'=>$item['jldw'],
  118. "GGXH"=>$item['ggxh'],
  119. 'SPSL'=>$item['spsl'],
  120. 'DJ'=>$item['bhsdj'],
  121. 'JE'=>$item['je'],
  122. 'SE'=>$item['se'],
  123. 'SL'=>$item['sl'],
  124. 'SPBM'=>$item['ssflbm'],
  125. ];
  126. },$detail);
  127. return [];
  128. }
  129. protected function faild($invNo,$msg=''){
  130. InvoiceTicket::where(['invNo'=>$invNo,"type"=>0,"status"=>0,"is_del"=>0])->save(["status"=>5,"updatetime"=>$this->dateTime]);
  131. InvoicePool::where(['invNo'=>$invNo])->save(["status"=>5,"updatetime"=>$this->dateTime,"check_remark"=>$msg]);
  132. }
  133. protected function lastUpdate($invNo){
  134. InvoiceTicket::where(['invNo'=>$invNo,"type"=>0,"status"=>0,"is_del"=>0])->save(["updatetime"=> date("Y-m-d H:i:s",strtotime("+1 day"))]);
  135. }
  136. protected function getInvoice($invNo){
  137. $info = (new InvoiceTicket)->where(['invNo'=>"INV2407160957046808",'type'=>0,'is_del'=>0])->findOrEmpty();
  138. echo $info->toJson();
  139. if(!$info->isEmpty()){
  140. $info->save(["status"=>rand(1,6)]);
  141. // InvoiceTicket::where(['invNo'=>'INV2407160957046808','type'=>0,'is_del'=>0])->update(['status'=>rand(1,10)]);
  142. }
  143. }
  144. }