GenerrateInvoice.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. {
  22. $generrateinvoice= Cache::store("redis")->get("generrateinvoice");
  23. if($generrateinvoice==1) return '';
  24. Cache::store("redis")->set("generrateinvoiceo",1,180);
  25. Db::startTrans();
  26. // 指令输出
  27. try {
  28. $data =$this->GetAll();
  29. if(empty($data)){
  30. Db::rollback();
  31. return '';
  32. }
  33. $date=date("Y-m-d H:i:s");
  34. $seller_code =$this->isTicket();
  35. $fail=[];
  36. $interface =[];
  37. foreach ($data as $value){
  38. $invNo=$value['invNo'];
  39. if(!in_array($value['XHFSBH'],$seller_code)){
  40. Db::name("invoice_interface")->where(['invNo'=>$value['invNo']])->update(["status"=>3,"remark"=>"不支持金税","updatetime"=>date("Y-m-d H:i:s")]);
  41. $fail[]=$value['invNo'];
  42. }
  43. $fplxdm= $value['FPLXDM'];
  44. if (!key_exists($value['XHFSBH']."-".$value['FPLXDM'],$interface)){
  45. $pch =randomkeys(26).date("YmdHis");
  46. $interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDZXX']=[];
  47. $interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDPCXX']=['DDQQPCH'=>$pch,'KPFS'=>"0",'NSRSBH'=>$value['XHFSBH'],"FPLXDM"=>$value['FPLXDM']];
  48. }else{
  49. $pch =$interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDPCXX']['DDQQPCH'];
  50. }
  51. Db::name("invoice_interface")->where(['invNo'=>$value['invNo']])->update(['DDQQPCH'=>$pch,'DDQQLSH'=>$value['DDQQLSH'],"updatetime"=>date("Y-m-d H:i:s")]);
  52. $temp=[];
  53. unset($value['invNo']);
  54. unset($value['KPFS']);
  55. unset($value['FPLXDM']);
  56. unset($value['DDQQPCH']);
  57. $temp["DDMXXX"]=$value['DDMXXX'];
  58. unset($value['DDMXXX']);
  59. $temp["DDTXX"]=$value;
  60. $interface[$value['XHFSBH']."-".$fplxdm]['DDZXX'][]=$temp;
  61. $output->writeln( "【 $date 】开票单号".$invNo);
  62. }
  63. $this->CheckInvoice($interface);
  64. $output->writeln( "【 $date 】开票申请成功");
  65. Db::commit();
  66. Cache::store("redis")->set("generrateinvoice",0);
  67. }catch (\Exception $e){
  68. Cache::store("redis")->set("generrateinvoice",0);
  69. Db::rollback();
  70. $output->writeln( "【 $date 】开票申请失败".$e->getMessage());
  71. }
  72. Cache::store("redis")->set("generrateinvoice",0);
  73. }
  74. protected function GetAll(){
  75. $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();
  76. return $list;
  77. }
  78. protected function isTicket(){
  79. $company =Db::name("company_info")->where(["out_ticket"=>1,'is_del'=>0])->column("company_license");
  80. return $company;
  81. }
  82. protected function CheckInvoice($data){
  83. if(empty($data)){
  84. throw new \Exception("开票数据不能为空");
  85. }
  86. $invoiceConf=Config::get('invoice');
  87. // $tax =new TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  88. $tax=[];
  89. foreach ($data AS $value){
  90. if(!isset($tax[$value['DDPCXX']['NSRSBH']])){
  91. if(!isset($invoiceConf[$value['DDPCXX']['NSRSBH']])){
  92. Db::name('invoice_interface')->where(['DDQQPCH'=>$value['DDPCXX']['DDQQPCH']])->update
  93. (['status'=>4,'remark'=>"金税配置不存在",'updatetime'=>date('Y-m-d H:i:s')]);
  94. continue;
  95. }
  96. $invoice=$invoiceConf[$value['DDPCXX']['NSRSBH']];
  97. $tax[$value['DDPCXX']['NSRSBH']] = new TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  98. }
  99. $result = $tax[$value['DDPCXX']['NSRSBH']]->GenerateInvoice($value['DDPCXX'],$value['DDZXX']);
  100. if(isset($result['ZTDM'])&&$result['ZTDM']=='010000'){
  101. Db::name("invoice_interface")->where(['DDQQPCH'=>$result['DDQQPCH']])->update(['status'=>1,
  102. "remark"=>$result['ZTXX'],"updatetime"=>date("Y-m-d H:i:s",time()+60)]);
  103. }else{
  104. 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")]);
  105. }
  106. }
  107. }
  108. }