123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\command;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;
- use TaxInvoice;
- use think\facade\Cache;
- use think\facade\Config;use think\facade\Db;
- class GenerrateInvoice extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('generrateinvoice')
- ->setDescription('the generrateinvoice command');
- }
- protected function execute(Input $input, Output $output)
- {
- $generrateinvoice= Cache::store("redis")->get("generrateinvoice");
- if($generrateinvoice==1) return '';
- Cache::store("redis")->set("generrateinvoiceo",1,180);
- Db::startTrans();
- // 指令输出
- try {
- $data =$this->GetAll();
- if(empty($data)){
- Db::rollback();
- return '';
- }
- $date=date("Y-m-d H:i:s");
- $seller_code =$this->isTicket();
- $fail=[];
- $interface =[];
- foreach ($data as $value){
- $invNo=$value['invNo'];
- if(!in_array($value['XHFSBH'],$seller_code)){
- Db::name("invoice_interface")->where(['invNo'=>$value['invNo']])->update(["status"=>3,"remark"=>"不支持金税","updatetime"=>date("Y-m-d H:i:s")]);
- $fail[]=$value['invNo'];
- }
- $fplxdm= $value['FPLXDM'];
- if (!key_exists($value['XHFSBH']."-".$value['FPLXDM'],$interface)){
- $pch =randomkeys(26).date("YmdHis");
- $interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDZXX']=[];
- $interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDPCXX']=['DDQQPCH'=>$pch,'KPFS'=>"0",'NSRSBH'=>$value['XHFSBH'],"FPLXDM"=>$value['FPLXDM']];
- }else{
- $pch =$interface[$value['XHFSBH']."-".$value['FPLXDM']]['DDPCXX']['DDQQPCH'];
- }
- Db::name("invoice_interface")->where(['invNo'=>$value['invNo']])->update(['DDQQPCH'=>$pch,'DDQQLSH'=>$value['DDQQLSH'],"updatetime"=>date("Y-m-d H:i:s")]);
- $temp=[];
- unset($value['invNo']);
- unset($value['KPFS']);
- unset($value['FPLXDM']);
- unset($value['DDQQPCH']);
- $temp["DDMXXX"]=$value['DDMXXX'];
- unset($value['DDMXXX']);
- $temp["DDTXX"]=$value;
- $interface[$value['XHFSBH']."-".$fplxdm]['DDZXX'][]=$temp;
- $output->writeln( "【 $date 】开票单号".$invNo);
- }
- $this->CheckInvoice($interface);
- $output->writeln( "【 $date 】开票申请成功");
- Db::commit();
- Cache::store("redis")->set("generrateinvoice",0);
- }catch (\Exception $e){
- Cache::store("redis")->set("generrateinvoice",0);
- Db::rollback();
- $output->writeln( "【 $date 】开票申请失败".$e->getMessage());
- }
- Cache::store("redis")->set("generrateinvoice",0);
- }
- protected function GetAll(){
- $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();
- return $list;
- }
- protected function isTicket(){
- $company =Db::name("company_info")->where(["out_ticket"=>1,'is_del'=>0])->column("company_license");
- return $company;
- }
- protected function CheckInvoice($data){
- if(empty($data)){
- throw new \Exception("开票数据不能为空");
- }
- $invoiceConf=Config::get('invoice');
-
- // $tax =new TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
- $tax=[];
- foreach ($data AS $value){
- if(!isset($tax[$value['DDPCXX']['NSRSBH']])){
- if(!isset($invoiceConf[$value['DDPCXX']['NSRSBH']])){
- Db::name('invoice_interface')->where(['DDQQPCH'=>$value['DDPCXX']['DDQQPCH']])->update
- (['status'=>4,'remark'=>"金税配置不存在",'updatetime'=>date('Y-m-d H:i:s')]);
- continue;
- }
- $invoice=$invoiceConf[$value['DDPCXX']['NSRSBH']];
- $tax[$value['DDPCXX']['NSRSBH']] = new TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
- }
- $result = $tax[$value['DDPCXX']['NSRSBH']]->GenerateInvoice($value['DDPCXX'],$value['DDZXX']);
- if(isset($result['ZTDM'])&&$result['ZTDM']=='010000'){
- Db::name("invoice_interface")->where(['DDQQPCH'=>$result['DDQQPCH']])->update(['status'=>1,
- "remark"=>$result['ZTXX'],"updatetime"=>date("Y-m-d H:i:s",time()+60)]);
- }else{
- 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")]);
- }
- }
- }
- }
|