QueryInvalidInvoice.php 5.7 KB

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