QueryInvalidInvoice.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\command;
  4. 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;
  9. use think\facade\Cache;use think\facade\Config;
  10. use TaxInvoice;
  11. use think\facade\Db;
  12. class QueryInvalidInvoice extends Command
  13. {
  14. protected function configure()
  15. {
  16. // 指令配置
  17. $this->setName('queryinvalidinvoice')
  18. ->setDescription('the queryinvalidinvoice command');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. $generrateinvoice= Cache::store("redis")->get("queryinvalidinvoice");
  23. if($generrateinvoice==1) return '';
  24. Cache::store("redis")->set("queryinvalidinvoice",1,180);
  25. $date=date("Y-m-d H:i:s");
  26. Db::startTrans();
  27. try{
  28. $data =Db::name("invoice_return")->where(["status"=>1,"discard"=>2])->where("updatetime","<=",date("Y-m-d H:i:s"))->lock(true)->findOrEmpty();
  29. if(empty($data)){
  30. Db::rollback();
  31. return ;
  32. };
  33. $invoiceInfo = Db::name("invoice_ticket")->where(["invNo"=>$data['invNo'],"is_del"=>0,"status"=>1])->findOrEmpty();
  34. if(empty($invoiceInfo))throw new \Exception($data['invNo']."未找到开票数据");
  35. $check=$this->ChickInvoice($invoiceInfo['seller_id'],$invoiceInfo['inv_code'],$invoiceInfo['inv_number']);
  36. if(isset($check['ZTDM']) && $check['ZTDM']=='001000') {
  37. $reups=Db::name("invoice_return")->where($data)->update(["discard"=>2,"remark"=>$data['remark']."({$check['ZTXX']})","updatetime"=>date("Y-m-d H:i:s",time()+120)]);
  38. if($reups==false)throw new \Exception("退票数据更新失败");
  39. Db::commit();
  40. $output->writeln("[$date] {$data['invNo']} {$check['ZTXX']}");
  41. return ;
  42. }
  43. if(isset($check['ZTDM']) && $check['ZTDM']=='000000'){
  44. $reup=Db::name("invoice_return")->where($data)->update(["status"=>2,"discard"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  45. if($reup==false)throw new \Exception($data['invNo']."退票数据更新失败");
  46. $poolup=Db::name("invoice_pool")->where(["invNo"=>$data['invNo'],"is_del"=>0,"status"=>4])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  47. if($poolup==false)throw new \Exception($data['invNo']."开票票数据更新失败");
  48. $ticket = Db::name("invoice_ticket")->where(["invNo"=>$data['invNo'],"is_del"=>0,"status"=>1])->update(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  49. if($ticket==false)throw new \Exception($data['invNo']."发票详情数据更新失败");
  50. $infoup =Db::name("invoice_good")->where(["invNo"=>$data['invNo'],"is_del"=>0])->update(["goodNum"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  51. if($infoup==false)throw new \Exception($data['invNo']."发票商品信息修改失败");
  52. $qrdArr=Db::name("assoc")->where(["viceCode"=>$data['invNo'],"is_del"=>0,"status"=>2])->column("id,orderCode,cancel_fee");
  53. if(!empty($qrdArr)){
  54. foreach ($qrdArr as $value){
  55. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  56. if($qrdinfo==false){
  57. throw new \Exception($value['orderCode']."确认单信息未找到");
  58. }
  59. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  60. throw new \Exception($value['orderCode']."确认单信息开票金额不足");
  61. }
  62. $update =[
  63. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  64. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  65. "inv_status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 ? 1 : 2,
  66. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']-$value['cancel_fee']==0&&$qrdinfo['pay_status']==1 ?0 : 1,
  67. "updatetime"=>date("Y-m-d H:i:s"),
  68. ];
  69. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  70. if($qrdup==false){
  71. throw new \Exception($value['orderCode']."确认单信息更新失败");
  72. }
  73. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  74. $assocup =Db::name("assoc")->where($value)->update($assoc);
  75. if($assocup==false){
  76. throw new \Exception($value['orderCode']."确认单关联信息更新失败");
  77. }
  78. }
  79. }else{
  80. throw new \Exception($data['invNo']."未找到关联订单数据信息");
  81. }
  82. }else{
  83. $reup=Db::name("invoice_return")->where($data)->update(["status"=>4,"remark"=>$data['remark']."({$check['ZTXX']})","updatetime"=>date("Y-m-d H:i:s")]);
  84. if($reup==false)throw new \Exception($data['invNo']."退票数据更新失败");
  85. }
  86. Cache::store("redis")->set("queryinvalidinvoice",0);
  87. Db::commit();
  88. $output->writeln("[$date] {$data['invNo']}数据处理完成");
  89. }catch (\Exception $e){
  90. Db::rollback();
  91. Cache::store("redis")->set("queryinvalidinvoice",0);
  92. $output->writeln("[$date] ".$e->getMessage());
  93. }
  94. }
  95. private function ChickInvoice($sellid,$invoice_code,$invoice_num){
  96. $invoiceConf=Config::get("invoice");
  97. if(!isset($invoiceConf[$sellid]))throw new \Exception("金税配置不存在");
  98. $invoice = $invoiceConf[$sellid];
  99. $tax =new TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  100. $reuslt = $tax->QueryInvalidInvoice($sellid,$invoice_num,$invoice_code);
  101. return $reuslt;
  102. }
  103. }