TransWar File Browser

Path: C:\

<?php
  ini_set('max_execution_time',259200); 
  ini_set ( 'memory_limit' , '-1' ) ;
  date_default_timezone_set('America/Sao_Paulo');
  $dateadded = date("Y-m-d"); 
	define('API_KEY', '75594207-b5d7-43bf-a7c2-571b3030308d');  
	function Dia105($dia120){
		$d1=substr($dia120, 8,2);
		$m1=substr($dia120, 5,2);
		$a1=substr($dia120, 0,4);
		return $d1."-".$m1."-".$a1;
	}
	function Dia105H($dia120){
		$d1=substr($dia120, 8,2);
		$m1=substr($dia120, 5,2);
		$a1=substr($dia120, 0,4);
		$h1=substr($dia120, 11,8);
		return $d1."-".$m1."-".$a1." ".$h1;
	}
	function Dia120($dia105){
		$d1=substr($dia105, 0,2);
		$m1=substr($dia105, 3,2);
		$a1=substr($dia105, 6,4);
		return $a1."-".$m1."-".$d1;
	}
  function Limpa($str){
    $str=trim($str);
    $str=str_replace('\\', '-', $str);
    $str=str_replace(' - ', '-', $str);
    $str=str_replace(' ', '-', $str);
    $str = iconv( "UTF-8" , "ASCII//TRANSLIT//IGNORE" , $str );
    $str = preg_replace( array( '/[ ]/' , '/[^A-Za-z0-9\-]/' ) , array( '' , '' ) , $str );
    return $str;
  }
	require_once("includes/RequestCTe.php");
	require_once("includes/RequestCTeId.php");
	require_once("includes/RequestMinuta.php");
	require_once("includes/RequestMinutaId.php");
	require_once("includes/RequestVeic.php");
	require_once("includes/RequestOcorr.php");
	if (isset($_POST['di']))  { $de=$_POST['di'];} else { $de=$dateadded;}
	if (isset($_POST['df']))  { $ate=$_POST['df'];} else { $ate=$dateadded;}
	$de=$de2=Dia120($de);
	$ate=$ate2=Dia120($ate);
	//$de2=Dia120('01-08-2023');
	//$ate2=$dateadded;
	$sts='';
	if (isset($_POST['op0'])) { $sts=$_POST['op0'];}
	if (isset($_POST['op1'])) { if($sts==''){$sts=$_POST['op1'];}else{$sts.=','.$_POST['op1'];}}
	if (isset($_POST['op2'])) { if($sts==''){$sts=$_POST['op2'];}else{$sts.=','.$_POST['op2'];}}
	if (isset($_POST['op3'])) { if($sts==''){$sts=$_POST['op3'];}else{$sts.=','.$_POST['op3'];}}
	if (isset($_POST['op4'])) { if($sts==''){$sts=$_POST['op4'];}else{$sts.=','.$_POST['op4'];}}
	if (isset($_POST['op5'])) { if($sts==''){$sts=$_POST['op5'];}else{$sts.=','.$_POST['op5'];}}
	if (isset($_POST['op6'])) { if($sts==''){$sts=$_POST['op6'];}else{$sts.=','.$_POST['op6'];}}
	if($sts==''){$sts='2,3,5';}
	$sts2='';
	if (isset($_POST['op7'])) { $sts2=$_POST['op7'];}
	if (isset($_POST['op8'])) { if($sts2==''){$sts2=$_POST['op8'];}else{$sts2.=','.$_POST['op8'];}}
	if (isset($_POST['op9'])) { if($sts2==''){$sts2=$_POST['op9'];}else{$sts2.=','.$_POST['op9'];}}
	if (isset($_POST['op10'])) { if($sts2==''){$sts2=$_POST['op10'];}else{$sts2.=','.$_POST['op10'];}}
	if (isset($_POST['op11'])) { if($sts2==''){$sts2=$_POST['op11'];}else{$sts2.=','.$_POST['op11'];}}
	if (isset($_POST['op12'])) { if($sts2==''){$sts2=$_POST['op12'];}else{$sts2.=','.$_POST['op12'];}}
	if (isset($_POST['op13'])) { if($sts2==''){$sts2=$_POST['op13'];}else{$sts2.=','.$_POST['op13'];}}
	if (isset($_POST['op14'])) { if($sts2==''){$sts2=$_POST['op14'];}else{$sts2.=','.$_POST['op14'];}}
	if (isset($_POST['op15'])) { if($sts2==''){$sts2=$_POST['op15'];}else{$sts2.=','.$_POST['op15'];}}
	if (isset($_POST['op16'])) { if($sts2==''){$sts2=$_POST['op16'];}else{$sts2.=','.$_POST['op16'];}}
	if (isset($_POST['op17'])) { if($sts2==''){$sts2=$_POST['op17'];}else{$sts2.=','.$_POST['op17'];}}
	if($sts2==''){$sts2='3,4,8,9,10';}
	$stts[0]	=	"Cadastrado";
	$stts[1]	=	"Emitido";
	$stts[2]	=	"Em Trânsito";
	$stts[3]	=	"Entregue";
	$stts[4]	=	"Cancelado";
	$stts[5]	=	"Não Entregue";
	$stts[6]	=	"Inutilizado";
	$stts2[0]	=	"Cadastrada";
	$stts2[1]	=	"Agendada";
	$stts2[2]	=	"Tentativa";
	$stts2[3]	=	"Coleta Parcial";
	$stts2[4]	=	"Coletada";
	$stts2[5]	=	"Problema";
	$stts2[6]	=	"Cancelada";
	$stts2[7]	=	"Inutilizada";
	$stts2[8]	=	"Em Trânsito";
	$stts2[9]	=	"Em Execução";
	$stts2[10]	=	"Finalizada";
	$rodd['00']	=	"Não Aplicável";
	$rodd['01']	=	"Truck";
	$rodd['02']	=	"Toco";
	$rodd['03']	=	"Cavalo Mecânico";
	$rodd['04']	=	"VAN";
	$rodd['05']	=	"Utilitário";
	$rodd['06']	=	"Outros";
	$url = "https://transwar.lannister.emitir.me/operations/search?page=1";
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	$headers = array(
	   "accept: application/json",
	   "x-api-key: ".API_KEY,
	);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_HEADER, false);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
	$resp = curl_exec($curl);
	curl_close($curl);
	$dat=json_decode($resp, true);
	foreach($dat['data'] as $h => $w) {
    $oper[$w['id']] = $w['name'];
	}
	echo '<pre>';
	//print_r($oper);
	$url = "https://transwar.lannister.emitir.me/cost-centers/search?page=1";
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	$headers = array(
	   "accept: application/json",
	   "x-api-key: ".API_KEY,
	);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_HEADER, false);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
	$resp = curl_exec($curl);
	curl_close($curl);
	$dat=json_decode($resp, true);
	foreach($dat['data'] as $h => $w) {
    $ccust[$w['id']] = $w['name']; 
	}
	//print_r($ccust);
	//echo 'de: '.$de.'<br>até: '.$ate.'<br>status CTe: '.$sts.'<br>status Minuta: '.$sts2.'<br>';
	//exit();
	$dataResponse = new RequestCTe();
  $resposta = $dataResponse->getResponse('1',$de,$ate,$sts);
  $tudo=json_decode($resposta[1], true);
	//print_r($tudo);
	//exit();
  $resta=$resposta[0];if($resta<=5){sleep(40);}
	//echo '<br><strong>Restam <font size="5" color="red">'.$resta.'</font> consultas dentro do limite de tempo!</strong><br>';
	if($tudo!==''){
	  if ($tudo['success']==1){
	  	$total=$tudo['total'];
			$pagina=array();
	    $npage=ceil($total/40);
			//echo '<strong>Para listar <font size="5" color="red">'.$npage.'</font> páginas com <font size="5" color="red">'.$total.'</font> registros.</strong><br>';
	    if($npage>=1){
	    	//for($i=$npage-3;$i<=$npage;$i++){	// tres 3 últimas páginas
	    	//for($i=1;$i<=2;$i++){						// teste 3 primeiras páginas
	    	for($i=1;$i<=$npage;$i++){   		// em produção totas as páginas
					$dataResponse = new RequestCTe();
					$resposta[$i] = $dataResponse->getResponse($i,$de,$ate,$sts);
				  $tudo[$i]=json_decode($resposta[$i][1], true);
				  $resta=$resposta[$i][0];if($resta<=5){sleep(10);}
	  			if ($tudo[$i]['success']==1){
	  				$data=$tudo[$i]['data'];
	  				$nreg=count($data);
	  				//for($y=0;$y<$nreg;$y++){
	  				for($y=0;$y<$nreg;$y++){
							$pagina[$i][$y]['numero'] = $data[$y]['number'];
							if(isset($data[$y]['driver']['name'])){$pagina[$i][$y]['nome']=$data[$y]['driver']['name'];}else{$pagina[$i][$y]['nome']='';}
							if(isset($data[$y]['vehicles']['traction']['license_plate'])){
								$pagina[$i][$y]['placa']=$data[$y]['vehicles']['traction']['license_plate'];
								$dataResponseVeic = new RequestVeic();
								$respVeic = $dataResponseVeic->getVeic($data[$y]['vehicles']['traction']['license_plate']);
								$pagina[$i][$y]['rodado'] = $rodd[$respVeic];
							}else{$pagina[$i][$y]['placa']='';$pagina[$i][$y]['rodado'] = '';}
							if(isset($data[$y]['classifications']['operation'])){$pagina[$i][$y]['descricao']=$oper[$data[$y]['classifications']['operation']];}else{$pagina[$i][$y]['descricao']='';}
							$dataResponseID = new RequestCTeId();
							$respID = $dataResponseID->getNFe($data[$y]['id']);
							$pagina[$i][$y]['NFeNumeros']	= $respID;	
							$pagina[$i][$y]['NFeTotal'] 	= $data[$y]['cargo']['value'];
							$name[0]			= $data[$y]['sender']['name'];				// remetente
							if(isset($data[$y]['dispatcher']['name'])){$name[1]=$data[$y]['dispatcher']['name'];}else{$name[1]='';} 	// expedidor
							if(isset($data[$y]['receiver']['name'])){$name[2]=$data[$y]['receiver']['name'];}else{$name[2]='';} 	// expedidor
							$name[3]			= $data[$y]['addressee']['name'];		// destinatario
							if(isset($data[$y]['customer']['name'])){$name[4]=$data[$y]['customer']['name'];}else{$name[4]='';} 	// expedidor
							$pagina[$i][$y]['tomador']			=	$name[$data[$y]['taker']];
							if(isset($data[$y]['classifications']['costcenter'])){$pagina[$i][$y]['centroCusto']=$ccust[$data[$y]['classifications']['costcenter']];}else{$pagina[$i][$y]['centroCusto']='';}
							$pagina[$i][$y]['remetente']		=	$name[0];
							$pagina[$i][$y]['destinatario']	=	$name[3];
							$pagina[$i][$y]['status']				=	$stts[$data[$y]['status']];
							$dataResponseOcorr = new RequestOcorr();
							$respOcorr = $dataResponseOcorr->getOcorr($data[$y]['key']);
							$pagina[$i][$y]['ocorrencia']		=	$respOcorr;	
							$pagina[$i][$y]['emissao']			=	Dia105($data[$y]['issued_at']);
							$pagina[$i][$y]['atualizacao']	=	Dia105H($data[$y]['updated_at']);
	  				}

	  			}
				}
			}
		}
	}
	$i2=$i-1;
	$dataResponse2 = new RequestMinuta();
  $resposta2 = $dataResponse2->getResponse2('1',$de2,$ate2,$sts2);
  $tudo=json_decode($resposta2[1], true);
	//print_r($tudo);
	//exit();
  $resta=$resposta2[0];if($resta<=5){sleep(40);}
	//echo '<br><strong>Restam <font size="5" color="red">'.$resta.'</font> consultas dentro do limite de tempo!</strong><br>';
	if($tudo!==''){
	  if ($tudo['success']==1){
	  	$total=$tudo['total'];
	    $npage=ceil($total/40);
			//echo '<strong>Para listar <font size="5" color="red">'.$npage.'</font> páginas com <font size="5" color="red">'.$total.'</font> registros.</strong><br>';
	    if($npage>=1){
	    	//for($i=$npage-3;$i<=$npage;$i++){	// tres 3 últimas páginas
	    	//for($i=1;$i<=2;$i++){						// teste 3 primeiras páginas
	    	for($i3=1;$i3<=$npage;$i3++){   		// em produção totas as páginas
					$dataResponse2 = new RequestMinuta();
					$resposta2[$i3] = $dataResponse2->getResponse2($i3,$de2,$ate2,$sts2);
				  $tudo[$i3]=json_decode($resposta2[$i3][1], true);
				  $resta=$resposta2[$i3][0];if($resta<=5){sleep(10);}
	  			if ($tudo[$i3]['success']==1){
	  				$data=$tudo[$i3]['data'];
	  				$nreg=count($data);
	  				for($y=0;$y<$nreg;$y++){
	  					$i=$i3+$i2;
							$pagina[$i][$y]['numero'] = $data[$y]['number'];
							if(isset($data[$y]['driver']['name'])){$pagina[$i][$y]['nome']=$data[$y]['driver']['name'];}else{$pagina[$i][$y]['nome']='';}
							$dataResponseID = new RequestMinutaId();
							$dataMin = $dataResponseID->getMinuta($data[$y]['id']);
				  		$nf='';
				  		foreach($dataMin['nfs'] as $w => $q){
				  			if($nf==''){$nf=$q['number'];}else{$nf.=','.$q['number'];}
				  		}
							$pagina[$i][$y]['NFeNumeros']	= $nf;	
							if(isset($dataMin['vehicles']['traction'])){
								$pagina[$i][$y]['placa']=$dataMin['vehicles']['traction'];
								$dataResponseVeic = new RequestVeic();
								$respVeic = $dataResponseVeic->getVeic($dataMin['vehicles']['traction']);
								$pagina[$i][$y]['rodado'] = $rodd[$respVeic];
							}else{$pagina[$i][$y]['placa']='';$pagina[$i][$y]['rodado'] = '';}
							if(isset($dataMin['classifications']['operation'])){$pagina[$i][$y]['descricao']=$oper[$dataMin['classifications']['operation']];}else{$pagina[$i][$y]['descricao']='';}
							$pagina[$i][$y]['NFeTotal'] 	= $dataMin['cargo']['value'];
							$name[0]	= $data[$y]['withdrawal']['name'];// remetente
							$name[1] 	= $data[$y]['receiver']['name']; 	// destinatário
							$name[3]	= $data[$y]['requestor']['name'];	// solicitante
							$pagina[$i][$y]['tomador']			=	$name[$data[$y]['taker']];
							if(isset($dataMin['classifications']['costcenter'])){$pagina[$i][$y]['centroCusto']=$ccust[$dataMin['classifications']['costcenter']];}else{$pagina[$i][$y]['centroCusto']='';}
							$pagina[$i][$y]['remetente']		=	$name[0];
							$pagina[$i][$y]['destinatario']	=	$name[1];
							$pagina[$i][$y]['status']				=	$stts2[$dataMin['status']];
							if(isset($dataMin['occurrences'][0]['description'])){$pagina[$i][$y]['ocorrencia']=$dataMin['occurrences'][0]['description'];}else{$pagina[$i][$y]['ocorrencia']='';}
							$pagina[$i][$y]['emissao']			=	Dia105($dataMin['created_at']);
							$pagina[$i][$y]['atualizacao']	=	Dia105H($dataMin['updated_at']);
	  				}

	  			}
				}
			}
		}
	}
	$json=json_encode($pagina);
	$fopen=fopen('JSON/CTes.json','w');
	$fwrite=fwrite($fopen,$json);
	fclose($fopen);
	include('includes/geraexcel.php');
	echo "<script language='JavaScript'>location.href='index.php?arquivo=".$arqxlsx."';</script>";
?>