QueryInvalidInvoice.php 4.9 KB

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