123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?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 think\facade\Cache;use think\facade\Config;
- use TaxInvoice;
- use think\facade\Db;
- class QueryInvalidInvoice extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('queryinvalidinvoice')
- ->setDescription('the queryinvalidinvoice command');
- }
- protected function execute(Input $input, Output $output)
- {
- $generrateinvoice= Cache::store("redis")->get("queryinvalidinvoice");
- if($generrateinvoice==0) Cache::store("redis")->set("queryinvalidinvoice",1,180);
- $date=date("Y-m-d H:i:s");
- Db::startTrans();
- try{
- $data =Db::name("invoice_return")->where(["status"=>1,"discard"=>2])->where("updatetime","<=",date("Y-m-d H:i:s"))->lock(true)->findOrEmpty();
- if(empty($data)){
- Db::rollback();
- return ;
- };
- $invoiceInfo = Db::name("invoice_ticket")->where(["invNo"=>$data['invNo'],"is_del"=>0,"status"=>1])->findOrEmpty();
- if(empty($invoiceInfo))throw new \Exception($data['invNo']."未找到开票数据");
- $check=$this->ChickInvoice($invoiceInfo['seller_id'],$invoiceInfo['inv_code'],$invoiceInfo['inv_number']);
- if(isset($check['ZTDM']) && $check['ZTDM']=='001000') {
- $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)]);
- if($reups==false)throw new \Exception("退票数据更新失败");
- Db::commit();
- $output->writeln("[$date] {$data['invNo']} {$check['ZTXX']}");
- return ;
- }
- if(isset($check['ZTDM']) && $check['ZTDM']=='000000'){
- $reup=Db::name("invoice_return")->where($data)->update(["status"=>2,"discard"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
- if($reup==false)throw new \Exception($data['invNo']."退票数据更新失败");
- $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")]);
- if($poolup==false)throw new \Exception($data['invNo']."开票票数据更新失败");
- $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")]);
- if($ticket==false)throw new \Exception($data['invNo']."发票详情数据更新失败");
- $infoup =Db::name("invoice_good")->where(["invNo"=>$data['invNo'],"is_del"=>0])->update(["goodNum"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
- if($infoup==false)throw new \Exception($data['invNo']."发票商品信息修改失败");
- $qrdArr=Db::name("assoc")->where(["viceCode"=>$data['invNo'],"is_del"=>0,"status"=>2])->column("id,orderCode,cancel_fee");
- if(!empty($qrdArr)){
- foreach ($qrdArr as $value){
- $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
- if($qrdinfo==false){
- throw new \Exception($value['orderCode']."确认单信息未找到");
- }
- if($qrdinfo['ainv_fee']<$value['cancel_fee']){
- throw new \Exception($value['orderCode']."确认单信息开票金额不足");
- }
- $update =[
- "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
- "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
- "inv_status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 ? 1 : 2,
- "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']-$value['cancel_fee']==0&&$qrdinfo['pay_status']==1 ?0 : 1,
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
- if($qrdup==false){
- throw new \Exception($value['orderCode']."确认单信息更新失败");
- }
- $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
- $assocup =Db::name("assoc")->where($value)->update($assoc);
- if($assocup==false){
- throw new \Exception($value['orderCode']."确认单关联信息更新失败");
- }
- }
- }else{
- throw new \Exception($data['invNo']."未找到关联订单数据信息");
- }
- }else{
- $reup=Db::name("invoice_return")->where($data)->update(["status"=>4,"remark"=>$data['remark']."({$check['ZTXX']})","updatetime"=>date("Y-m-d H:i:s")]);
- if($reup==false)throw new \Exception($data['invNo']."退票数据更新失败");
- }
- Cache::store("redis")->set("queryinvalidinvoice",0);
- Db::commit();
- $output->writeln("[$date] {$data['invNo']}数据处理完成");
- }catch (\Exception $e){
- Db::rollback();
- Cache::store("redis")->set("queryinvalidinvoice",0);
- $output->writeln("[$date] ".$e->getMessage());
- }
- }
- private function ChickInvoice($sellid,$invoice_code,$invoice_num){
- $invoice=Config::get("invoice");
- $tax =new TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
- $reuslt = $tax->QueryInvalidInvoice($sellid,$invoice_num,$invoice_code);
- return $reuslt;
- }
- }
|