setName('getorderinfoandinvoiceinfo') ->setDescription('the getorderinfoandinvoiceinfo command'); } protected function execute(Input $input, Output $output) { // 指令输出 $GetOrderInfoAndInvoiceInfo= Cache::store("redis")->get("GetOrderInfoAndInvoiceInfo"); if($GetOrderInfoAndInvoiceInfo==0) Cache::store("redis")->set("GetOrderInfoAndInvoiceInfo",1,180); Db::startTrans(); try{ $data = $this->GetData(); if(empty($data)) { Db::rollback(); return ''; } $date=date("Y-m-d H:i:s"); foreach ($data as $value){ if($value['status']==1){ $this->GetOrderInfo($value); } if($value['status']==2){ if($value['KPLX']==1){ $red=Db::name("invoice_red")->where(["invRed"=>$value['invNo'],"is_del"=>0])->find(); if($red){ $invNo=$red['invNo']; Db::name("invoice_return")->where(["returnCode"=>$red['returnCode'],"status"=>1])->update(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]); Db::name("invoice_red")->where(["invRed"=>$value['invNo'],"is_del"=>0])->update(["status"=>4,"updatetime"=>date("Y-m-d H:i:s")]); Db::name("invoice_pool")->where(["invNo"=>$red['invNo'],"is_del"=>0])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]); } }else{ Db::name("invoice_pool")->where(["invNo"=>$value['invNo'],"is_del"=>0])->update(["status"=>4,"seller_check"=>1,"buyer_check"=>1,"updatetime"=>date("Y-m-d H:i:s")]); } $da=Db::name("invoice_interface")->where($value)->update(["is_check"=>1,"updatetime"=>date("Y-m-d H:i:s")]); if($da) $this->insertTicket($value['result'],$invNo); } if($value['status']==3 || $value['status']==4){ if($value['KPLX']==1){ Db::name("invoice_red")->where(["invRed"=>$value['invNo'],"is_del"=>0])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]); }else{ Db::name("invoice_pool")->where(["invNo"=>$value['invNo'],"is_del"=>0])->update(["status"=>5,"check_remark"=>$value['remark'],"updatetime"=>date("Y-m-d H:i:s")]); } Db::name("invoice_interface")->where($value)->update(["is_check"=>1,"updatetime"=>date("Y-m-d H:i:s")]); } } $output->writeln( "【 $date 】验票完成".$value['invNo']); Db::commit(); Cache::store("redis")->set("GetOrderInfoAndInvoiceInfo",0); }catch (\Exception $exception){ Cache::store("redis")->set("generrateinvoice",0); Db::rollback(); $output->writeln( "【 $date 】开票申请失败".$exception->getMessage()); } Cache::store("redis")->set("GetOrderInfoAndInvoiceInfo",0); } protected function GetData(){ $list=Db::name("invoice_interface")->where([["status",">=",1],["is_check","=",0],["updatetime","<=",date("Y-m-d H:i:s")]])->limit(20)->lock(true)->select()->toArray(); return $list; } protected function GetOrderInfo($order){ $invoice=Config::get("invoice"); $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']); $result=$tax->GetOrderInfoAndInvoiceInfo($order['XHFSBH'],$order['DDQQLSH']); if(isset($result['ZTDM'])&& $result['ZTDM']=='000000'){ foreach ($result['DDFPZXX'] as $value){ $zt=$value['DDFPXX']['DDZT']; $ztxx=$value['DDFPXX']['DDZTXX']; if($zt=='030000'){ $data= $value['DDFPXX']; $data['itemlist'] = $value['DDMXXX']; if($data['KPLX']==0)$this->updateGood($value['DDMXXX'],$order['invNo'],4); else $this->updateGood($value['DDMXXX'],$order['invNo'],6); $exit= json_encode($data,JSON_UNESCAPED_UNICODE); Db::name("invoice_interface")->where(["DDQQLSH"=>$value['DDFPXX']['DDQQLSH']])->update(["status"=>2,"result"=>$exit,"remark"=>$ztxx,"updatetime"=>date("Y-m-d H:i:s")]); } if($zt=='031999' ||$zt=='032997'||$zt=='032999'||$zt=='032998' ){ Db::name("invoice_interface")->where(["DDQQLSH"=>$value['DDFPXX']['DDQQLSH']])->update(["status"=>4,"is_check"=>1,"remark"=>$ztxx,"updatetime"=>date("Y-m-d H:i:s")]); } if($zt=='031000' ||$zt=='032000'){ Db::name("invoice_interface")->where(["DDQQLSH"=>$value['DDFPXX']['DDQQLSH']])->update(["remark"=>$ztxx,"updatetime"=>date("Y-m-d H:i:s")]); } } }else{ throw new Exception($result['message']); } } protected function insertTicket($data,$invNo=''){ $invinfo =Db::name("invoice_ticket")->where(["invNo"=>$invNo,'is_del'=>0])->findOrEmpty(); $json= json_decode($data,true); if(is_array($json)&&!empty($json)){ if(empty($invinfo)){ $invinfo["invNo"]=$invNo; $invinfo["status"]=1; $invinfo["addtime"]=date("Y-m-d H:i:s"); } $invinfo["inv_type"]=$json['FPLXDM']; $invinfo["type"]=$json['KPLX']; $invinfo["inv_code"]=$json['FPDM']; $invinfo["inv_number"]=$json['FPHM']; $invinfo["inv_total"]=$json['JSHJ']; $invinfo["inv_subtotal"]=$json['HJJE']; $invinfo["open_date"]=$json['KPRQ']; $invinfo["seller_id"]=$json['XHFSBH']; $invinfo["seller_title"]=$json['XHFMC']; $invinfo["seller_addr"]=$json['XHFDZ']; $invinfo["seller_mobile"]=$json['XHFDH']; $invinfo["seller_bank"]=$json['XHFYH']; $invinfo["seller_bankNo"]=$json['XHFZH']; $invinfo["buyer_id"]=$json['GMFSBH']; $invinfo["buyer_title"]=$json['GMFMC']; $invinfo["buyer_addr"]=$json['GMFDZ']; $invinfo["buyer_mobile"]=$json['GMFDH']; $invinfo["buyer_bank"]=$json['GMFYH']; $invinfo["buyer_bankNo"]=$json['GMFZH']; $invinfo["tax_fee"]=$json['HJSE']; $invinfo["remark"]=$json['BZ']??$json['CHBZ']; $invinfo["issuer"]=$json['KPR']; $invinfo["reciver"]=$json['SKR']; $invinfo["reviewer"]=$json['FHR']; $invinfo["check_code"]=$json['JYM']; $invinfo["pdfstream"]=$json['PDFZJL']??""; $invinfo["item"]=json_encode($json['itemlist'],JSON_UNESCAPED_UNICODE); Db::name("invoice_ticket")->save($invinfo); } } private function updateGood($data,$invNo,$status=4){ if(empty($data))return; foreach ($data as $value){ if($value['ZXBM']!=''){ $invinfo =Db::name("invoice_good")->where(["invNo"=>$invNo,"id"=>$value['ZXBM'],'is_del'=>0])->findOrEmpty(); if(empty($invinfo)) continue; if($status==4){ $invinfo['goodPrice'] = $value['DJ']; $invinfo['totalPrice'] = $value['JE']; $invinfo['taxPrice'] = $value['SE']; }else{ $invinfo['good_num'] =0; } $invinfo['updatetime'] = date("Y-m-d H:i:s"); Db::name("invoice_good")->save($invinfo); } } $this->updateQRd($invNo,$status); } private function updateQRd($invNo,$status){ $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->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){ echo "$invNo 确认单信息未找到\r\n";break; } if($status==4){ if($qrdinfo['inv_fee']<$value['cancel_fee']){ echo "$invNo 确认单信息开票金额不足 \r\n"; break; } $update =[ "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'], "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'], "invtime"=>date("Y-m-d H:i:s"), "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2, "updatetime"=>date("Y-m-d H:i:s"), ]; } if($status==6){ if($qrdinfo['ainv_fee']<$value['cancel_fee']){ echo "$invNo 确认单信息开票金额不足 \r\n";break; } $update =[ "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'], "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'], "invtime"=>'', "inv_status"=>$qrdinfo['inv_fee']==0 &&($qrdinfo['ainv_fee']-$value['cancel_fee'])==0 ? 1 : 2, "status"=>$qrdinfo['inv_fee']==0 &&($qrdinfo['ainv_fee']-$value['cancel_fee'])==0 && $qrdinfo['pay_status']==0 ? 0 : 1, "updatetime"=>date("Y-m-d H:i:s"), ]; } $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update); if($qrdup==false){ // return "确认单信息更新失败"; echo "$invNo 确认单信息更新失败 \r\n";break; } $assoc=["status"=>$status==4?2:3,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")]; $assocup =Db::name("assoc")->where($value)->update($assoc); if($assocup==false){ // return "确认单关联信息更新失败"; echo "$invNo 确认单关联信息更新失败 \r\n";break; } } } } }