Como ya me funciona bien lo de los envíos a batuz os dejo el código
Lo primero que hago es separar por ejercicios el envío, no sea que en enero quira enviar facturas de diciembre y de enero
Código PHP:
if (file_exists($guardado_enviar_tbai)) {
$fra_enviada = file_get_contents($guardado_enviar_tbai);
$fichero = base64_encode($fra_enviada);
$xml_facturas[] = "<TicketBai>".$fichero."</TicketBai>"; // array de facturas
}
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .= "<lrpjfecsgap:LROEPJ240FacturasEmitidasConSGAltaPeticion
xmlns:lrpjfecsgap='https://www.batuz.eus/fitxategiak/batuz/LROE/esquemas/LROE_PJ_240_1_1_FacturasEmitidas_ConSG_AltaPeticion_V1_0_1.xsd'>";
$xml .= "<Cabecera>
<Modelo>240</Modelo>
<Capitulo>1</Capitulo>
<Subcapitulo>1.1</Subcapitulo>
<Operacion>A00</Operacion>
<Version>1.0</Version>
<Ejercicio>".$xml_ejercicio."</Ejercicio> // ejercicio 2020 o 2021 o 2022 ...
<ObligadoTributario>
<NIF>".$cif_340."</NIF>
<ApellidosNombreRazonSocial>".$mombre_340."</ApellidosNombreRazonSocial>
</ObligadoTributario>
</Cabecera>";
$xml .= "<FacturasEmitidas>";
foreach($xml_facturas as $value) // array con los tbai en base64
{
$xml .= "<FacturaEmitida>";
$xml .= $value;
$xml .= "</FacturaEmitida>";
}
$xml .= "</FacturasEmitidas>";
$xml .= "</lrpjfecsgap:LROEPJ240FacturasEmitidasConSGAltaPeticion>";
$ahora = round(microtime(true) * 10000);
$ahora = sprintf ( "%.0f", $ahora ); // nombre del fichero xml de envío
$guardar_como_xml = 'LROE_240_'.$ahora.'.xml';
file_put_contents($directorio_TBAI_envios.$guardar_como_xml, $xml);
$guardar_como_gzip = 'LROE_240_'.$ahora.'.xml.gz';
$xml_data = file_get_contents($directorio_TBAI_envios.$guardar_como_xml);
$gzip_data = gzencode($xml_data); // nombre del fichero gz de envío
$eusbizkaian3data = '{"con":"LROE","apa":"1.1","inte":';
$eusbizkaian3data .= '{"nif":"'.$cif_340.'","nrs":"'.$mombre_340.'"},';
$eusbizkaian3data .= '"drs":{"mode":"240","ejer":"'.$xml_ejercicio.'"}}';
$header_soap = array(
"Accept-Encoding: gzip",
"Content-Encoding: gzip",
"Content-Type: application/octet-stream",
"eus-bizkaia-n3-version: 1.0",
"eus-bizkaia-n3-content-type: application/xml",
"eus-bizkaia-n3-data: ".$eusbizkaian3data,
"Content-length: ".strlen($gzip_data)
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url_TBAI); // url de envío pruebas
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 500);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 500);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_HEADER , true);
curl_setopt($soap_do, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($soap_do, CURLOPT_POST, true);
curl_setopt($soap_do, CURLOPT_SSLCERT, getcwd().'/_certificandos'.$alias_pdf.'/'.$cif_340.'.pem');
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $gzip_data);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header_soap);
chdir('ticketBAI');
$response = curl_exec($soap_do);
$size_response = curl_getinfo($soap_do, CURLINFO_HEADER_SIZE);
$header_response = substr($response, 0, $size_response);
$body_response = substr($response, $size_response);
function get_headers_from_curl_response($header_response) // para la cabecera respuesta
{
$headers_r = array();
foreach (explode("\r\n", $header_response) as $i => $line)
if ($i !== 0)
{
list ($key, $value) = explode(': ', $line);
$headers_r[$key] = utf8_encode($value);
}
return $headers_r;
}
Continúo para ver las cabeceras de respuesta
Código PHP:
$headers_r = get_headers_from_curl_response($header_response);
//var_dump($headers_r);
$respuesta_tipo = $headers_r['eus-bizkaia-n3-tipo-respuesta'];
if($respuesta_tipo == 'Incorrecto')
{
include_once($_SERVER['DOCUMENT_ROOT'].'/_configs/_header_ifx.php' );
$respuesta_codigo = $headers_r['eus-bizkaia-n3-codigo-respuesta'];
$respuesta_mensaje = $headers_r['eus-bizkaia-n3-mensaje-respuesta'];
echo "<br><TABLE align=center width=97% class=gris>";
echo "<tr class=amarillo>";
echo "<td align=left><div id=tit1>
Respuesta del envío de hoy a TBAI Bizkaia de facturas emitidas firmadas";
echo " <b>$respuesta_tipo</b></td></tr>";
echo "<tr class=amarillo>";
echo "<td align=left><div id=tit1>
Error $respuesta_codigo";
echo " <b>$respuesta_mensaje</b></td>";
echo"</tr></table><br>";
// exit();
}
Si va todo bien descomprimo, descomprimo y guardo
Código PHP:
if(!curl_exec($soap_do)) {
$err = 'Error: ' . curl_error($soap_do);
curl_close($soap_do);
echo "<br>No se puede conectar con la TBAI Bizkaia.<br><br>";
print_r($err);
exit();
echo "<br>";
echo "<br><b>Respuesta de la TBAI: Bizkaia</b><br><br>";
print_r($response);
exit();
} else {
curl_close($soap_do);
$xmlString = gzdecode(gzdecode($body_response));
//$xmlString = gzdecode($xmlString);
$xmlString = preg_replace('/(<\s*)\w+:/','$1',$xmlString); // removes <xxx:
$xmlString = preg_replace('/(<\/\s*)\w+:/','$1',$xmlString); // removes </xxx:
$xmlString = preg_replace('/\s+xmlns:[^>]+/','',$xmlString); // removes xmlns:...
$use_errors = libxml_use_internal_errors(true);
$parser = simplexml_load_string($xmlString);
if ($parser === false) {
echo "<br>No se puede cargar el fichero comprimido devuelto por la TBAI.<br><br>";
echo "<br>";
echo "<br><b>Respuesta de la TBAI 0:</b><br><br>";
print_r($headers_r);
exit();
}
libxml_clear_errors();
libxml_use_internal_errors($use_errors);
if ($parser === false) exit();
$parser->asXML($directorio_respuestas.'LROE_240_'.$ahora.'.xml');
}
Para mostrar los registros, pendiente de afinar
Código PHP:
$facturas_en_TBAI = $parser;
if ($facturas_en_TBAI === false)
{
echo "<br><b>Respuesta de TBAI parser:</b><br><br>";
print_r ($facturas_en_TBAI);
}
echo "<br><TABLE align=center width=97% class=gris>";
echo "<tr class=amarillo>";
echo "<td align=left><div id=tit1>
Respuesta del envío de hoy a TBAI Bizkaia de facturas emitidas firmadas";
echo " <b>$respuesta_tipo</b></td>";
echo"</tr></table><br>";
if($respuesta_tipo == 'Incorrecto' || $respuesta_tipo == 'Parcialmente correcto')
{
include_once($_SERVER['DOCUMENT_ROOT'].'/_configs/_header_ifx.php' );
$respuesta_codigo = $headers_r['eus-bizkaia-n3-codigo-respuesta'];
$respuesta_mensaje = $headers_r['eus-bizkaia-n3-mensaje-respuesta'];
echo "<TABLE align=center width=97% class=gris id='sortedtable'><thead>";
echo "<tr class=odd>";
echo "<th align=left><div id=tit2>Factura</div></th>";
echo "<th align=left><div id=tit2>Fecha</div></th>";
echo "<th align=left><div id=tit2>Error</div></th>";
echo "<th align=left><div id=tit2>Descripción</div></th>";
echo "</tr></thead>";
$a = 0;
foreach ($facturas_en_TBAI->Registros->Registro as $nodo)
{
$SerieFactura = $nodo->Identificador->IDFactura->SerieFactura;
$NumFactura = $nodo->Identificador->IDFactura->NumFactura;
$FechaExpedicionFactura = $nodo->Identificador->IDFactura->FechaExpedicionFactura;
$EstadoRegistro = $nodo->SituacionRegistro->EstadoRegistro;
$CodigoErrorRegistro = $nodo->SituacionRegistro->CodigoErrorRegistro;
$DescripcionErrorRegistro = $nodo->SituacionRegistro->DescripcionErrorRegistro;
$DescripcionErrorRegistro = utf8_decode($DescripcionErrorRegistro);
if($EstadoRegistro == 'Correcto' || $CodigoErrorRegistro == 'B4_2000003')
{
$xml_enviado_tbai = $guardado_enviar[$SerieFactura.'-'.$NumFactura];
$guardado_enviar_tbai = $directorio_TBAI.$xml_enviado_tbai;
$guardado_enviado_tbai = $directorio_TBAI_envios.$xml_enviado_tbai;
rename($guardado_enviar_tbai,$guardado_enviado_tbai); /// atencion a esto
}
if($EstadoRegistro != 'Correcto')
{
echo "<tr "; if( $a%2 == 1 ) echo " class=odd";
echo "><td class=firstcol align=left><div id=tit3> ".$SerieFactura.'-'.$NumFactura."</div></td>";
echo "<td align=left><div id=tit3> ".$FechaExpedicionFactura."</div></td>";
echo "<td align=left><div id=tit3> ".$CodigoErrorRegistro."</div></td>";
echo "<td align=left><div id=tit3> ".$DescripcionErrorRegistro."</div></td>";
$a++;
echo "</tr>";
}
} // fin foreach ($facturas_en_TBAI->Registros->Registro as $nodo)
echo '</table><br>';
}
// fin if($respuesta_tipo == 'Incorrecto' || $respuesta_tipo == 'Parcialmente correcto')
if($respuesta_tipo == 'Correcto')
{
include_once($_SERVER['DOCUMENT_ROOT'].'/_configs/_header_ifx.php' );
foreach ($facturas_en_TBAI->Registros->Registro as $nodo)
{
$SerieFactura = $nodo->Identificador->IDFactura->SerieFactura;
$NumFactura = $nodo->Identificador->IDFactura->NumFactura;
$xml_enviado_tbai = $guardado_enviar[$SerieFactura.'-'.$NumFactura];
$guardado_enviar_tbai = $directorio_TBAI.$xml_enviado_tbai;
$guardado_enviado_tbai = $directorio_TBAI_envios.$xml_enviado_tbai;
rename($guardado_enviar_tbai,$guardado_enviado_tbai); /// atencion a esto
}
}
// fin if($respuesta_tipo == 'Correcto')
unset($facturas_en_TBAI);
Comentamos