123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\command;
- use think\console\Command;
- use think\console\Input;
- use think\facade\Config;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;
- use think\facade\Cache;
- use think\facade\Db;
- class downredinvres extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('downredinvres')
- ->setDescription('the downredinvres command');
- }
- protected function execute(Input $input, Output $output)
- {
- $GetOrderInfoAndInvoiceInfo= Cache::store("redis")->get("downredinvres");
- if($GetOrderInfoAndInvoiceInfo==1) return '';
- Cache::store("redis")->set("downredinvres",1,160);
- Db::startTrans();
- $date= date("Y-m-d H:i:s");
- $row =Db::name("invoice_red")->where(["status"=>1,"is_del"=>0])->where("updatetime","<=",date("Y-m-d H:i:s"))
- ->lock(true)->findOrEmpty();
- if(empty($row)) {
- Db::rollback();
- return '';
- }
- try{
- $data=["updatetime"=>date("Y-m-d H:i:s",time()+600)];
- $opendatetime =date("Ymd",strtotime($row['addtime']));
- $ebddatetime =date("Ymd",strtotime("+2 days",strtotime($row['addtime'])));
- $invoiceConf=Config::get('invoice');
- if(!isset($invoiceConf[$row['XHFSBH']])){
- $data['remark']="金税配置不存在";
- Db::name('invoice_red')->where(['id'=>$row['id']])->update($data);
- Db::commit();
- return '';
- }
- $invoice = $invoiceConf[$row['XHFSBH']];
- $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
- if($row['addtime']==$row['updatetime']){
- $pch=randomkeys(8).md5(date("YmdHis").$row['id']);
- $data['SQBSCQQPCH']=$pch;
- $row['SQBSCQQPCH'] =$pch;
- }
- $result=$Tax->DownloadRedInvoiceApplicationResult($row['SQBSCQQPCH'],$row['XHFSBH'],$row['FPLXDM'],$opendatetime,$opendatetime);
- if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
- if(isset($result['HZSQDXZZXX']) && !empty($result['HZSQDXZZXX'])){
- $limimt=[];
- foreach ($result['HZSQDXZZXX'] as $item){
- $resultChild=$item['HZSQDXZTXX'];
- $temp=[];
- if(isset($limimt[$resultChild['YFPHM']]) &&$limimt[$resultChild['YFPHM']]['status']==2 ) continue;
- $temp['YFPDM']=$resultChild['YFPDM'];
- $temp['YFPHM']=$resultChild['YFPHM'];
- $temp['XXBBH']=$resultChild['XXBBH'];
- $temp['remark']=$resultChild['ZTXX'];
- $temp['updatetime']=date("Y-m-d H:i:s");
- if($resultChild['ZTDM']=='060000'){
- $temp['status']=2;
- }elseif($resultChild['ZTDM']=='069999'){
- $temp['status']=5;
- }else{
- $temp['status']=1;
- }
- $limimt[$resultChild['YFPHM']] = $temp;
- }
- if(empty($limimt)){
- $data['remark']=$result['ZTXX'];
- Db::name('invoice_red')->where($row)->update($data);
- }else{
- $this->checkData($limimt);
- }
- }
- }else{
- $data['remark']=$result['ZTXX'];
- if(!isset($result['ZTDM']) || $result['ZTDM']!='060111'){
- $data['status']=5;
- $this->updateReturn($row['returnCode'],$result['ZTXX']);
- }
- Db::name('invoice_red')->where(['id'=>$row['id']])->update($data);
- }
- Db::commit();
- $output->writeln("[$date] ".$row['invRed'].'发票申请表获取成功');
- }catch (\Exception $e){
- Db::rollback();
- $output->writeln("[$date] ".$row['invRed'].$e->getMessage().$e->getLine());
- }
- }
- //立即提交红冲开票
- public function AddRed($fpdm,$fphm){
- $cod=["YFPDM"=>$fpdm,"YFPHM"=>$fphm,"status"=>2,"is_del"=>0];
- $red=Db::name("invoice_red")->where($cod)->findOrEmpty();
- if(empty($red))throw new \Exception("未找到发票红冲申请信息");
- $where=["invNo"=>$red["invNo"],"type"=>0,"status"=>1];
- $ticket =Db::name("invoice_ticket")->where($where)->findOrEmpty();
- $invinfo = Db::name("invoice_pool")->where(["invNo"=>$red['invNo'],"is_del"=>0,"status"=>4])->findOrEmpty();
- if(empty($ticket) || empty($invinfo)){
- Db::name("invoice_red")->where($red)->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>"申请成功,原票信息本地未找到"]);
- Db::name("invoice_return")->where(["returnCode"=>$red['returnCode']])->update(["status"=>4,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>"申请成功,原票信息本地未找到"]);
- return ;
- }
- $data=[ "invNo"=>$red['invRed'],
- "KPLX"=>"1",
- "XHFSBH"=>$ticket['seller_id'],
- "XHFMC"=>$ticket['seller_title'],
- "XHFDZ"=>$ticket['seller_addr'],
- "XHFDH"=>$ticket['seller_mobile'],
- "XHFYH"=>$ticket['seller_bank'],
- "XHFZH"=>$ticket['seller_bankNo'],
- "GMFBM"=>"",
- "GMFSBH"=>$ticket['buyer_id'],
- "GMFMC"=>$ticket['buyer_title'],
- "GMFDZ"=>$ticket['buyer_addr'],
- "GMFDH"=>$ticket['buyer_mobile'],
- "GMFYH"=>$ticket['buyer_bank'],
- "GMFZH"=>$ticket['buyer_bankNo'],
- "GMFLX"=>$invinfo['company_type']??'01',
- "GMFSJH"=>"",
- "GMFDZYX"=>'',
- "KPR"=>$ticket['issuer'],
- "SKR"=>$ticket['reciver'],
- "FHR"=>$ticket['reviewer'],
- "QDBZ"=>"0",
- "YFPDM"=>$red['YFPDM'],
- "YFPHM"=>$red['YFPHM'],
- "YFPLX"=>$red['FPLXDM'] == '004' ||$red['FPLXDM'] =='028'||$red['red_reason']=='' ?'':$red['FPLXDM'],
- "YFPKPRQ"=>$red['FPLXDM'] == '004' ||$red['FPLXDM'] =='028'||$red['red_reason']=='' ? '':date("Y-m-d",strtotime($red['YFPKPRQ'])),
- "CHYY"=>$red['FPLXDM'] == '004' ||$red['FPLXDM'] =='028' ? '':$red['red_reason'],
- "TSCHBZ"=>"0",
- "JSHJ"=>"-".$ticket['inv_total'],
- "HJJE"=>$red['HJJE'],
- "HJSE"=>$red['HJSE'],
- "BZ"=> $red['FPLXDM'] == '004' || $red['FPLXDM'] =='028'?"开具红字增值税专用发票信息表编号{$red['XXBBH']}":"对应正数发票代码:{$red['YFPDM']} 号码:{$red['YFPHM']}",
- "DDMXXX"=>$red['DDMXXX'],
- "FPLXDM"=>$red['FPLXDM'],
- "DDQQPCH"=>"",
- "DDQQLSH"=>randomkeys(8).md5(date('YmdHis').$red['id']),
- "status"=>"0",
- "result"=>"",
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $invoice=Db::name("invoice_interface")->insert($data);
- if($invoice==false){
- throw new \Exception("红冲提交失败");
- }
- Db::name("invoice_red")->where($red)->update(["status"=>3,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>"红冲开票申请已提交待开票"]);
- }
- /**红冲申请失败退票信息失败
- * @param $fpdm
- * @param $fphm
- * @param string $remark
- * @throws \think\db\exception\DbException
- */
- public function updateReturn($returnCode,$remark=''){
- Db::name("invoice_return")->where(["returnCode"=>$returnCode,"status"=>1])->update(["status"=>4,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark]);
- }
-
-
- public function checkData($resultChildArr){
- foreach ($resultChildArr as $resultChild){
- $where=['YFPDM'=>$resultChild['YFPDM'] , 'YFPHM'=>$resultChild['YFPHM'] , 'status'=>1];
- $row=Db::name('invoice_red')->where($where)->findOrEmpty();
- if (!empty($row)) {
- Db::name('invoice_red')->where($where)->update($resultChild);
- if ($resultChild['status'] == 2) $this->AddRed($resultChild['YFPDM'] , $resultChild['YFPHM']);
- if ($resultChild['status'] == 5) $this->updateReturn($row['returnCode'] ,$resultChild['remark']);
- }
- }
-
- }
- }
|