// ini_set('display_errors', 1); // ini_set('display_startup_errors', 1); error_reporting(E_ALL ^ E_NOTICE ^E_WARNING); date_default_timezone_set('America/Sao_Paulo'); //----------------------------------------------------------------------------------------- function strzero( $numero , $tamanho ) { return str_repeat("0", $tamanho - strlen($numero) ) . $numero; } //--------------------------------------------------------------------------------- function print_r_die($_string){ echo "
";
echo print_r($_string);
die("");
}
//---------------------------------------------------------------------------------
$path = "c:/temp/emissor/";
$path = "d:/nfe/";
//$caminho = $path."historico/2025-09";
if ( $_REQUEST["mes"] ) {
$mes = $_REQUEST["mes"];
$ano = $_REQUEST["ano"];
} else {
$mes = date("m");
$ano = date("Y");
}
$caminho = $path."historico/".strzero($ano,2)."-".$mes;
//---------------------------------------------------------------------------------
if ( $_GET["xml"] ) {
$xml = $_GET["xml"];
$arquivo = str_replace("/",'\\',$caminho."/".$xml.($_GET["cancel"]?"_cancel":"").".XML");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/xml");
header("Content-Disposition: attachment; filename=\"".basename($xml.($_GET["cancel"]?"_cancel":"").".XML")."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($arquivo));
readfile($arquivo);
exit();
}
//---------------------------------------------------------------------------------
if ( $_GET["nf"] ) {
$nf = $_GET["nf"];
$arquivo = str_replace("/",'\\',$caminho."/".$nf.".PDF");
header('Content-type: application/pdf');
header('Expires: 0');
header('Pragma: no-cache');
header('Pragma: no-cache');
readfile($arquivo);
exit();
}
//---------------------------------------------------------------------------------
$email = "";
if ( $_POST["email"] ) {
$email = $_POST["email"];
}
$metodo = "h";
if ( $_POST["metodo"] ) {
$metodo = $_POST["metodo"];
}
if ( $_POST["servidor"] ) {
$servidor = $_POST["servidor"];
$porta = $_POST["porta"];
$remetente = $_POST["remetente"];
$senha = $_POST["senha"];
}
if ( $_POST["email"] ) {
$fp = fopen("./email.inf", "w+");
fwrite($fp, $email."|".$metodo."|".$servidor."|".$porta."|".$remetente."|".$senha);
fclose($fp);
}
$email = file("./email.inf");
$email = explode("|",$email[0]);
$metodo = $email[1];
$servidor = $email[2];
$porta = $email[3];
$remetente = $email[4];
$senha = $email[5];
$email = $email[0];
if ( $_POST["email"] ) {
$assenvio = $_POST["assenvio"];
$assmsg = $_POST["msgenvio"];
$asscanc = $_POST["asscanc"] ;
$msgcanc = $_POST["msgcanc"] ;
$asscarta = $_POST["asscarta"];
$msgcarta = $_POST["msgcarta"];
$fp = fopen("./mensagens.inf", "w+");
$bloco = "[ASSENVIO]".$assenvio."\n";
$bloco .= $assmsg."\n";
$bloco .= "[ASSCANC]".$asscanc."\n";
$bloco .= $msgcanc."\n";
$bloco .= "[ASSCARTA]".$asscarta."\n";
$bloco .= $msgcarta."\n";
fwrite($fp, $bloco);
fclose($fp);
$json = json_encode( array ( "servidor" => $servidor, "porta" => $porta,
"remetente" => $remetente , "senha" => $senha , "email" => $email ,
"assenvio" => $assenvio, "msgenvio" => $assmsg, "asscanc" => $asscanc, "msgcanc" => $msgcanc, "asscarta" => $asscarta, "msgcarta" => $msgcarta ) );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.osoliveiras.com.br/sugest/grava_sug_msg.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}
$assmsg = "";
$msgcanc = "";
$msgcarta = "";
$msg = file("./mensagens.inf");
$fenvio = false;
$fcancel= false;
$fcarta = false;
foreach($msg as $x => $linha) {
if ( substr($linha,0,10)=="[ASSENVIO]" ) {
$assenvio = substr($linha,10);
$fenvio = true ;
Continue;
}
if ( substr($linha,0,9)=="[ASSCANC]" ) {
$asscanc = substr($linha,9);
$fenvio = false ;
$fcancel = true;
Continue;
}
if ( substr($linha,0,10)=="[ASSCARTA]" ) {
$asscarta = substr($linha,10);
$fenvio = false ;
$fcancel = false;
$fcarta = true ;
Continue;
}
if ( $fenvio ) $assmsg .= $linha;
if ( $fcancel ) $msgcanc .= $linha;
if ( $fcarta ) $msgcarta .= $linha;
}
//---------------------------------------------------------------------------------
if ( $_GET["a"] ) {
$arquivo = $_GET["a"];
if ( file_exists($arquivo) ) {
$linhas = file($arquivo);
$op = "";
if ( (substr($linhas[0],0,7)=="CANCELA") || (substr($linhas[0],0,5)=="CARTA") ) {
//
// cancela
//
$linhas= explode("|",$linhas[0]);
$numero= $linhas[1];
$op = (substr($linhas[0],0,7)=="CANCELA"?"c":"cc");
$json = json_encode( array ("justificativa" => trim($linhas[2]) ) );
if ( substr($linhas[0],0,5)=="CARTA" ) {
$json = trim($linhas[2]);
}
} else {
//---------------------------------------------------------------
//
// envia
//
$numero = "";
foreach($linhas as $linha) {
$linha = trim($linha);
if ( strpos($linha,"numero") ) {
$numero = substr($linha,9);
$numero = substr($numero,0, strlen($numero)-1 );
break;
}
if ( strpos($linha,"nNF") ) {
$numero = substr($linha,6);
$numero = substr($numero,0, strlen($numero)-1 );
break;
}
}
if ( empty($numero) )
die("Numero nao encontrado : ".$arquivo);
$json = file_get_contents($arquivo);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.osoliveiras.com.br/sugest/teste_curl.php?nota=".$numero."&m=".$metodo."&email=".$email."&op=".$op);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$body = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$retorno = json_decode($body);
curl_close($ch);
//
// move arquivo
//
$caminho = $path."/historico/".date("Y")."-".date("m");
@mkdir($caminho,0755);
if ( $op=="c" ) {
@copy($arquivo,$caminho."/".$numero."_cancel.TXT");
} else {
@copy($arquivo,$caminho."/".$numero.".TXT");
}
unlink($arquivo);
@unlink($caminho."/".$numero."_err.TXT");
header("location:arquivos.php");
exit;
}
}
//---------------------------------------------------------------------------------
$diretorio = dir($path);
$files = scandir($path);
$files2 = scandir($caminho);
$txt = "";
//
// busca os txt's para envio
//
foreach($files as $arquivo) {
$arquivo = pathinfo($path.$arquivo);
if ( strtoupper($arquivo["extension"])=="TXT" ) {
$txt = $arquivo["dirname"]."/".$arquivo["basename"];
$ftxt= filemtime($arquivo["dirname"]."/".$arquivo["basename"]);
$tmp = intval(substr($arquivo["basename"],0,8)).".TXT";
$ctmp= filemtime($caminho."/".intval(substr($arquivo["basename"],0,8)).".TXT");
if ( (in_array( $tmp , $files2 )) && ( $ftxt < filemtime($ctmp) ) ) {
$txt = "";
} else {
break;
}
}
}
//-------------------------------------------------------
//
// busca as danfes
//
$danfes = array();
foreach($files2 as $arquivo) {
$arquivo = pathinfo($path.$arquivo);
if ( strtoupper($arquivo["extension"])=="TXT" ) {
$tmp = intval(substr($arquivo["basename"],0,8));
$fpdf = false ;
$fxml = false ;
$msg = "";
if ( in_array( $tmp.".PDF" , $files2 ) == false ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.osoliveiras.com.br/sugest/nfe_consulta_sug.php?nota=".$tmp."&m=".$metodo);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$body = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$retorno = json_decode($body);
curl_close($ch);
if ( property_exists($retorno,"caminho_danfe") ) {
if ( $retorno->caminho_danfe ) {
if ( !file_exists($caminho."/".$tmp.".PDF") ) {
$fpdf = $caminho."/".$tmp.".PDF";
$xml = $caminho."/".$tmp.".XML";
$cxml = $caminho."/".$tmp."_cancel.XML";
$dorigem = $retorno->caminho_danfe;
$xorigem = $retorno->caminho_xml_nota_fiscal;
$oxml = $retorno->caminho_xml_cancelamento;
@copy( $dorigem, $fpdf );
@copy( $xorigem, $xml );
if ( !empty($oxml) )
@copy( $oxml, $cxml );
$fpdf = true;
}
}
} else {
if ( property_exists($retorno,"mensagem_sefaz") ) {
$msg = "".$retorno->erro_autorizacao." ".$retorno->mensagem_sefaz."";
}
}
} else {
$fpdf = true;
}
$xml = "";
if ( $fpdf ) {
$xml = simplexml_load_file($caminho."/".$tmp.".XML");
$xml = $xml->NFe->infNFe->dest->xNome;
}
if ( file_exists($caminho."/".$tmp."_cancel.XML") ) {
$danfes[] = array( "arquivo" => $tmp , "flag" => $fpdf , "cancel" => $caminho."/".$tmp."_cancel.XML", "nome" => $xml , "msg" => "autorizada" );
} else {
$danfes[] = array( "arquivo" => $tmp , "flag" => $fpdf , "cancel" => "", "nome" => $xml , "msg" => $msg );
}
}
}
//-------------------------------------------------------
?>
|
|
|