GenerrateInvoice.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 TaxInvoice;
  10. use think\facade\Cache;
  11. use think\facade\Config;use think\facade\Db;
  12. class GenerrateInvoice extends Command
  13. {
  14. protected function configure()
  15. {
  16. // 指令配置
  17. $this->setName('generrateinvoice')
  18. ->setDescription('the generrateinvoice command');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. { $generrateinvoice= Cache::store("redis")->get("generrateinvoice");
  22. if($generrateinvoice==0) Cache::store("redis")->set("generrateinvoiceo",1,180);
  23. Db::startTrans();
  24. // 指令输出
  25. try {
  26. $data =$this->GetAll();
  27. if(empty($data)){
  28. Db::rollback();
  29. return '';
  30. }
  31. $date=date("Y-m-d H:i:s");
  32. $seller_code =$this->isTicket();
  33. $fail=[];
  34. $interface =[];
  35. foreach ($data as $value){
  36. $invNo=$value['invNo'];
  37. if(!in_array($value['XHFSBH'],$seller_code)){
  38. Db::name("invoice_interface")->where(['invNo'=>$value['invNo']])->update(["status"=>3,"remark"=>"不支持金税","updatetime"=>date("Y-m-d H:i:s")]);
  39. $fail[]=$value['invNo'];
  40. }
  41. $fplxdm= $value['FPLXDM'];
  42. if (!key_exists($value['XHFSBH']."-".$value['FPLXDM'],$interface)){
  43. $pch =randomkeys(26).date("YmdHis");
  44. $interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDZXX']=[];
  45. $interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDPCXX']=['DDQQPCH'=>$pch,'KPFS'=>"0",'NSRSBH'=>$value['XHFSBH'],"FPLXDM"=>$value['FPLXDM']];
  46. }else{
  47. $pch =$interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDPCXX']['DDQQPCH'];
  48. }
  49. Db::name("invoice_interface")->where(['invNo'=>$value['invNo']])->update(['DDQQPCH'=>$pch,'DDQQLSH'=>$value['DDQQLSH'],"updatetime"=>date("Y-m-d H:i:s")]);
  50. $temp=[];
  51. unset($value['invNo']);
  52. unset($value['KPFS']);
  53. unset($value['FPLXDM']);
  54. unset($value['DDQQPCH']);
  55. $temp["DDMXXX"]=$value['DDMXXX'];
  56. unset($value['DDMXXX']);
  57. $temp["DDTXX"]=$value;
  58. $interface[$value['XHFSBH']."-".$fplxdm]['DDZXX'][]=$temp;
  59. $output->writeln( "【 $date 】开票单号".$invNo);
  60. }
  61. $this->CheckInvoice($interface);
  62. $output->writeln( "【 $date 】开票申请成功");
  63. Db::commit();
  64. Cache::store("redis")->set("generrateinvoice",0);
  65. }catch (\Exception $e){
  66. Cache::store("redis")->set("generrateinvoice",0);
  67. Db::rollback();
  68. $output->writeln( "【 $date 】开票申请失败".$e->getMessage());
  69. }
  70. Cache::store("redis")->set("generrateinvoice",0);
  71. }
  72. protected function GetAll(){
  73. $list =Db::name("invoice_interface")->json(["DDMXXX"])->lock(true)->withoutField("id,status,is_check,result,remark,addtime,updatetime")->limit(1)->where(["status"=>0])->select()->toArray();
  74. return $list;
  75. }
  76. protected function isTicket(){
  77. $company =Db::name("company_info")->where(["out_ticket"=>1,'is_del'=>0])->column("company_license");
  78. return $company;
  79. }
  80. protected function CheckInvoice($data){
  81. if(empty($data)){
  82. throw new \Exception("开票数据不能为空");
  83. }
  84. $invoice=Config::get("invoice");
  85. $tax =new TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  86. foreach ($data AS $value){
  87. $result = $tax->GenerateInvoice($value['DDPCXX'],$value['DDZXX']);
  88. if(isset($result['ZTDM'])&&$result['ZTDM']=='010000'){
  89. Db::name("invoice_interface")->where(['DDQQPCH'=>$result['DDQQPCH']])->update(['status'=>1,
  90. "remark"=>$result['ZTXX'],"updatetime"=>date("Y-m-d H:i:s")]);
  91. }else{
  92. Db::name("invoice_interface")->where(['DDQQPCH'=>$value['DDPCXX']['DDQQPCH']])->update(['status'=>4,"remark"=>$result['ZTXX']??$result['message'],"updatetime"=>date("Y-m-d H:i:s")]);
  93. }
  94. }
  95. }
  96. }