CheckTicket.php 5.9 KB

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