Ejemplo n.º 1
0
 /**
  * Método que genera el XML de EnvioDTE a partir de los datos ya
  * normalizados de un DTE temporal
  * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]delaf.cl)
  * @version 2016-01-02
  */
 public function getEnvioDte($folio = 0, \sasco\LibreDTE\Sii\Folios $Folios = null, \sasco\LibreDTE\FirmaElectronica $Firma = null)
 {
     $dte = json_decode($this->datos, true);
     if (!$dte) {
         return false;
     }
     $dte['Encabezado']['IdDoc']['Folio'] = $folio;
     $Dte = new \sasco\LibreDTE\Sii\Dte($dte, false);
     if ($Folios and !$Dte->timbrar($Folios)) {
         return false;
     }
     if ($Firma and !$Dte->firmar($Firma)) {
         return false;
     }
     $EnvioDte = new \sasco\LibreDTE\Sii\EnvioDte();
     $EnvioDte->agregar($Dte);
     if ($Firma) {
         $EnvioDte->setFirma($Firma);
     }
     $Emisor = $this->getEmisor();
     $EnvioDte->setCaratula(['RutEnvia' => $Firma ? $Firma->getID() : false, 'RutReceptor' => $Emisor->certificacion ? '60803000-K' : $Dte->getReceptor(), 'FchResol' => $Emisor->certificacion ? $Emisor->certificacion_resolucion : $Emisor->resolucion_fecha, 'NroResol' => $Emisor->certificacion ? 0 : $Emisor->resolucion_numero]);
     return $EnvioDte;
 }
// incluir archivos php de la biblioteca y configuraciones
include 'inc.php';
// caratula del libro
$caratula = ['RutEmisorLibro' => '76192083-9', 'RutEnvia' => '11222333-4', 'PeriodoTributario' => '1980-03', 'FchResol' => '2006-01-20', 'NroResol' => 102006, 'TipoOperacion' => 'VENTA', 'TipoLibro' => 'ESPECIAL', 'TipoEnvio' => 'TOTAL', 'FolioNotificacion' => 102006];
// datos del emisor
$Emisor = ['RUTEmisor' => '76192083-9', 'RznSoc' => 'SASCO SpA', 'GiroEmis' => 'Servicios integrales de informática', 'Acteco' => 726000, 'DirOrigen' => 'Santiago', 'CmnaOrigen' => 'Santiago'];
// datos de los DTE (cada elemento del arreglo $set_pruebas es un DTE)
// IMPORTANTE: folios deben coincidir con los de los DTEs que fueron aceptados
// en el proceso de certificación del set de pruebas básico
$set_pruebas = [['Encabezado' => ['IdDoc' => ['TipoDTE' => 33, 'Folio' => 21], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago']], 'Detalle' => [['NmbItem' => 'Cajón AFECTO', 'QtyItem' => 123, 'PrcItem' => 923], ['NmbItem' => 'Relleno AFECTO', 'QtyItem' => 53, 'PrcItem' => 1473]], 'Referencia' => ['TpoDocRef' => 'SET', 'FolioRef' => 21, 'RazonRef' => 'CASO 414175-1']], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 33, 'Folio' => 22], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago']], 'Detalle' => [['NmbItem' => 'Pañuelo AFECTO', 'QtyItem' => 235, 'PrcItem' => 1926, 'DescuentoPct' => 4], ['NmbItem' => 'ITEM 2 AFECTO', 'QtyItem' => 161, 'PrcItem' => 990, 'DescuentoPct' => 5]], 'Referencia' => ['TpoDocRef' => 'SET', 'FolioRef' => 22, 'RazonRef' => 'CASO 414175-2']], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 33, 'Folio' => 23], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago']], 'Detalle' => [['NmbItem' => 'Pintura B&W AFECTO', 'QtyItem' => 24, 'PrcItem' => 1937], ['NmbItem' => 'ITEM 2 AFECTO', 'QtyItem' => 149, 'PrcItem' => 2975], ['IndExe' => 1, 'NmbItem' => 'ITEM 3 SERVICIO EXENTO', 'QtyItem' => 1, 'PrcItem' => 34705]], 'Referencia' => ['TpoDocRef' => 'SET', 'FolioRef' => 23, 'RazonRef' => 'CASO 414175-3']], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 33, 'Folio' => 24], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago']], 'Detalle' => [['NmbItem' => 'ITEM 1 AFECTO', 'QtyItem' => 81, 'PrcItem' => 1672], ['NmbItem' => 'ITEM 2 AFECTO', 'QtyItem' => 35, 'PrcItem' => 1405], ['IndExe' => 1, 'NmbItem' => 'ITEM 3 SERVICIO EXENTO', 'QtyItem' => 2, 'PrcItem' => 6767]], 'DscRcgGlobal' => ['TpoMov' => 'D', 'TpoValor' => '%', 'ValorDR' => 6], 'Referencia' => ['TpoDocRef' => 'SET', 'FolioRef' => 24, 'RazonRef' => 'CASO 414175-4']], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 61, 'Folio' => 13], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago'], 'Totales' => ['MntTotal' => 0]], 'Detalle' => [['NmbItem' => 'Cajón AFECTO']], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => 13, 'RazonRef' => 'CASO 414175-5'], ['TpoDocRef' => 33, 'FolioRef' => 21, 'CodRef' => 2, 'RazonRef' => 'CORRIGE GIRO DEL RECEPTOR']]], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 61, 'Folio' => 14], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago'], 'Totales' => ['MntNeto' => 0, 'TasaIVA' => \sasco\LibreDTE\Sii::getIVA(), 'IVA' => 0, 'MntTotal' => 0]], 'Detalle' => [['NmbItem' => 'Pañuelo AFECTO', 'QtyItem' => 86, 'PrcItem' => 1926, 'DescuentoPct' => 4], ['NmbItem' => 'ITEM 2 AFECTO', 'QtyItem' => 109, 'PrcItem' => 990, 'DescuentoPct' => 5]], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => 14, 'RazonRef' => 'CASO 414175-6'], ['TpoDocRef' => 33, 'FolioRef' => 22, 'CodRef' => 3, 'RazonRef' => 'DEVOLUCION DE MERCADERIAS']]], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 61, 'Folio' => 15], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago'], 'Totales' => ['MntNeto' => 0, 'MntExe' => 0, 'TasaIVA' => \sasco\LibreDTE\Sii::getIVA(), 'IVA' => 0, 'MntTotal' => 0]], 'Detalle' => [['NmbItem' => 'Pintura B&W AFECTO', 'QtyItem' => 24, 'PrcItem' => 1937], ['NmbItem' => 'ITEM 2 AFECTO', 'QtyItem' => 149, 'PrcItem' => 2975], ['IndExe' => 1, 'NmbItem' => 'ITEM 3 SERVICIO EXENTO', 'QtyItem' => 1, 'PrcItem' => 34705]], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => 15, 'RazonRef' => 'CASO 414175-7'], ['TpoDocRef' => 33, 'FolioRef' => 23, 'CodRef' => 1, 'RazonRef' => 'ANULA FACTURA']]], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 56, 'Folio' => 5], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago'], 'Totales' => ['MntTotal' => 0]], 'Detalle' => [['NmbItem' => 'Cajón AFECTO']], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => 5, 'RazonRef' => 'CASO 414175-8'], ['TpoDocRef' => 61, 'FolioRef' => 13, 'CodRef' => 1, 'RazonRef' => 'ANULA NOTA DE CREDITO ELECTRONICA']]]];
// Objetos de Firma y LibroCompraVenta
$Firma = new \sasco\LibreDTE\FirmaElectronica($config['firma']);
$LibroCompraVenta = new \sasco\LibreDTE\Sii\LibroCompraVenta();
// generar cada DTE y agregar su resumen al detalle del libro
foreach ($set_pruebas as $documento) {
    $DTE = new \sasco\LibreDTE\Sii\Dte($documento);
    $LibroCompraVenta->agregar($DTE->getResumen(), false);
    // agregar detalle sin normalizar
}
// enviar libro de ventas y mostrar resultado del envío: track id o bien =false si hubo error
$LibroCompraVenta->setCaratula($caratula);
$LibroCompraVenta->generar(false);
// generar XML sin firma y sin detalle
$LibroCompraVenta->setFirma($Firma);
$track_id = $LibroCompraVenta->enviar();
// enviar XML generado en línea anterior
var_dump($track_id);
// si hubo errores mostrar
foreach (\sasco\LibreDTE\Log::readAll() as $error) {
    echo $error, "\n";
}
// caratula para el envío de los dte
$caratula = ['RutEnvia' => '11222333-4', 'RutReceptor' => '60803000-K', 'FchResol' => '2014-12-05', 'NroResol' => 0];
// datos del emisor
$Emisor = ['RUTEmisor' => '76192083-9', 'RznSoc' => 'SASCO SpA', 'GiroEmis' => 'Servicios integrales de informática', 'Acteco' => 726000, 'DirOrigen' => 'Santiago', 'CmnaOrigen' => 'Santiago'];
// datos de los DTE (cada elemento del arreglo $set_pruebas es un DTE)
$set_pruebas = [['Encabezado' => ['IdDoc' => ['TipoDTE' => 34, 'Folio' => $folios[34]], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago']], 'Detalle' => [['NmbItem' => 'HORAS PROGRAMADOR', 'QtyItem' => 12, 'UnmdItem' => 'Hora', 'PrcItem' => 6991]], 'Referencia' => ['TpoDocRef' => 'SET', 'FolioRef' => $folios[34], 'RazonRef' => 'CASO 414178-1']], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 61, 'Folio' => $folios[61]], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago'], 'Totales' => ['MntExe' => 0, 'MntTotal' => 0]], 'Detalle' => [['NmbItem' => 'HORAS PROGRAMADOR', 'IndExe' => 1, 'QtyItem' => 12, 'UnmdItem' => 'Hora', 'PrcItem' => 874]], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => $folios[61], 'RazonRef' => 'CASO 414178-2'], ['TpoDocRef' => 34, 'FolioRef' => $folios[34], 'CodRef' => 3, 'RazonRef' => 'MODIFICA MONTO']]], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 34, 'Folio' => $folios[34] + 1], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago']], 'Detalle' => [['NmbItem' => 'SERV CONSULTORIA FACT ELECTRONICA', 'QtyItem' => 1, 'PrcItem' => 360368], ['NmbItem' => 'SERV CONSULTORIA GUIA DESPACHO ELECT', 'QtyItem' => 1, 'PrcItem' => 262001]], 'Referencia' => ['TpoDocRef' => 'SET', 'FolioRef' => $folios[34] + 1, 'RazonRef' => 'CASO 414178-3']], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 61, 'Folio' => $folios[61] + 1], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago'], 'Totales' => ['MntExe' => 0, 'MntTotal' => 0]], 'Detalle' => [['NmbItem' => 'SERV CONSULTORIA FACT ELECTRONICA']], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => $folios[61] + 1, 'RazonRef' => 'CASO 414178-4'], ['TpoDocRef' => 34, 'FolioRef' => $folios[34] + 1, 'CodRef' => 2, 'RazonRef' => 'CORRIGE GIRO']]], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 56, 'Folio' => $folios[56]], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago'], 'Totales' => ['MntExe' => 0, 'MntTotal' => 0]], 'Detalle' => [['NmbItem' => 'SERV CONSULTORIA FACT ELECTRONICA']], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => $folios[56], 'RazonRef' => 'CASO 414178-5'], ['TpoDocRef' => 61, 'FolioRef' => $folios[61] + 1, 'CodRef' => 1, 'RazonRef' => 'ANULA NOTA DE CREDITO ELECTRONICA']]], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 34, 'Folio' => $folios[34] + 2], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago']], 'Detalle' => [['NmbItem' => 'CAPACITACION USO CIGUEÑALES', 'QtyItem' => 1, 'PrcItem' => 350414], ['NmbItem' => 'CAPACITACION USO PLC\'s CNC', 'QtyItem' => 1, 'PrcItem' => 239579]], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => $folios[34] + 2, 'RazonRef' => 'CASO 414178-6']]], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 61, 'Folio' => $folios[61] + 2], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago'], 'Totales' => ['MntExe' => 0, 'MntTotal' => 0]], 'Detalle' => [['NmbItem' => 'CAPACITACION USO CIGUEÑALES', 'IndExe' => 1, 'QtyItem' => 1, 'PrcItem' => 175207]], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => $folios[61] + 2, 'RazonRef' => 'CASO 414178-7'], ['TpoDocRef' => 34, 'FolioRef' => $folios[34] + 2, 'CodRef' => 3, 'RazonRef' => 'MODIFICA MONTO']]], ['Encabezado' => ['IdDoc' => ['TipoDTE' => 56, 'Folio' => $folios[56] + 1], 'Emisor' => $Emisor, 'Receptor' => ['RUTRecep' => '55666777-8', 'RznSocRecep' => 'Empresa S.A.', 'GiroRecep' => 'Servicios jurídicos', 'DirRecep' => 'Santiago', 'CmnaRecep' => 'Santiago'], 'Totales' => ['MntExe' => 0, 'MntTotal' => 0]], 'Detalle' => [['NmbItem' => 'CAPACITACION USO PLC\'s CNC', 'IndExe' => 1, 'QtyItem' => 1, 'PrcItem' => 47916]], 'Referencia' => [['TpoDocRef' => 'SET', 'FolioRef' => $folios[56] + 1, 'RazonRef' => 'CASO 414178-8'], ['TpoDocRef' => 34, 'FolioRef' => $folios[34] + 2, 'CodRef' => 3, 'RazonRef' => 'MODIFICA MONTO']]]];
// Objetos de Firma, Folios y EnvioDTE
$Firma = new \sasco\LibreDTE\FirmaElectronica($config['firma']);
$Folios = [];
foreach ($folios as $tipo => $cantidad) {
    $Folios[$tipo] = new \sasco\LibreDTE\Sii\Folios(file_get_contents('xml/folios/' . $tipo . '.xml'));
}
$EnvioDTE = new \sasco\LibreDTE\Sii\EnvioDTE();
// generar cada DTE, timbrar, firmar y agregar al sobre de EnvioDTE
foreach ($set_pruebas as $documento) {
    $DTE = new \sasco\LibreDTE\Sii\Dte($documento);
    if (!$DTE->timbrar($Folios[$DTE->getTipo()])) {
        break;
    }
    if (!$DTE->firmar($Firma)) {
        break;
    }
    $EnvioDTE->agregar($DTE);
}
// enviar dtes y mostrar resultado del envío: track id o bien =false si hubo error
$EnvioDTE->setCaratula($caratula);
$EnvioDTE->setFirma($Firma);
//file_put_contents('xml/EnvioDTE.xml', $EnvioDTE->generar()); // guardar XML en sistema de archivos
$track_id = $EnvioDTE->enviar();
var_dump($track_id);
// si hubo errores mostrar
Ejemplo n.º 4
0
 *
 * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl)
 * @version 2015-09-15
 */
// respuesta en texto plano
header('Content-type: text/plain; charset=ISO-8859-1');
// incluir archivos php de la biblioteca y configuraciones
include 'inc.php';
// datos
$factura = ['Encabezado' => ['IdDoc' => ['TipoDTE' => 33, 'Folio' => 1], 'Emisor' => ['RUTEmisor' => '76192083-9', 'RznSoc' => 'SASCO SpA', 'GiroEmis' => 'Servicios integrales de informática', 'Acteco' => 726000, 'DirOrigen' => 'Santiago', 'CmnaOrigen' => 'Santiago'], 'Receptor' => ['RUTRecep' => '60803000-K', 'RznSocRecep' => 'Servicio de Impuestos Internos', 'GiroRecep' => 'Gobierno', 'DirRecep' => 'Alonso Ovalle 680', 'CmnaRecep' => 'Santiago']], 'Detalle' => [['NmbItem' => 'Cajón AFECTO', 'QtyItem' => 123, 'PrcItem' => 923], ['NmbItem' => 'Relleno AFECTO', 'QtyItem' => 53, 'PrcItem' => 1473]]];
$caratula = ['RutEnvia' => '11222333-4', 'RutReceptor' => '60803000-K', 'FchResol' => '2014-12-05', 'NroResol' => 0];
// Objetos de Firma y Folios
$Firma = new \sasco\LibreDTE\FirmaElectronica($config['firma']);
$Folios = new \sasco\LibreDTE\Sii\Folios(file_get_contents('xml/folios/33.xml'));
// generar XML del DTE timbrado y firmado
$DTE = new \sasco\LibreDTE\Sii\Dte($factura);
$DTE->timbrar($Folios);
$DTE->firmar($Firma);
// generar sobre con el envío del DTE
// en este ejemplo sólo se obtendrá el XML del EnvioDT y se enviará
// posteriormente por el método "paso a paso", existe un método
// EnvioDTE::enviar() que envía el XML que se genera, ver ejemplo 010-set_pruebas.php
$EnvioDTE = new \sasco\LibreDTE\Sii\EnvioDTE();
$EnvioDTE->agregar($DTE);
$EnvioDTE->setCaratula($caratula);
$EnvioDTE->setFirma($Firma);
$xml = $EnvioDTE->generar();
// solicitar token
$token = \sasco\LibreDTE\Sii\Autenticacion::getToken($Firma);
if (!$token) {
    foreach (\sasco\LibreDTE\Log::readAll() as $log) {
Ejemplo n.º 5
0
 /**
  * Recurso de la API que genera el XML de los DTEs solicitados
  * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl)
  * @version 2015-11-21
  */
 public function _api_generar_xml_POST()
 {
     // verificar si se pasaron credenciales de un usuario
     $User = $this->Api->getAuthUser();
     if (is_string($User)) {
         $this->Api->send($User, 401);
     }
     // verificar que se hayan pasado los índices básicos
     foreach (['Emisor', 'Receptor', 'documentos', 'folios', 'firma'] as $key) {
         if (!isset($this->Api->data[$key])) {
             $this->Api->send('Falta índice/variable ' . $key . ' por POST', 500);
         }
     }
     // recuperar folios y definir ambiente
     $folios = [];
     $certificacion = false;
     foreach ($this->Api->data['folios'] as $folio) {
         $Folios = new \sasco\LibreDTE\Sii\Folios(base64_decode($folio));
         $folios[$Folios->getTipo()] = $Folios;
         if ($Folios->getCertificacion()) {
             $certificacion = true;
         }
     }
     // normalizar datos emisor
     $this->Api->data['Emisor']['RUTEmisor'] = str_replace('.', '', $this->Api->data['Emisor']['RUTEmisor']);
     // normalizar datos receptor
     $this->Api->data['Receptor']['RUTRecep'] = str_replace('.', '', $this->Api->data['Receptor']['RUTRecep']);
     // objeto de la firma
     try {
         $Firma = new \sasco\LibreDTE\FirmaElectronica(['data' => base64_decode($this->Api->data['firma']['data']), 'pass' => $this->Api->data['firma']['pass']]);
     } catch (\Exception $e) {
         $this->Api->send('No fue posible abrir la firma digital, quizás contraseña incorrecta', 500);
     }
     // normalizar dte?
     $normalizar_dte = isset($this->Api->data['normalizar_dte']) ? $this->Api->data['normalizar_dte'] : true;
     // armar documentos y guardar en un arreglo
     $Documentos = [];
     foreach ($this->Api->data['documentos'] as $d) {
         // crear documento
         $d['Encabezado']['Emisor'] = $this->Api->data['Emisor'];
         $d['Encabezado']['Receptor'] = $this->Api->data['Receptor'];
         $DTE = new \sasco\LibreDTE\Sii\Dte($d, $normalizar_dte);
         // timbrar, firmar y validar el documento
         if (!isset($folios[$DTE->getTipo()])) {
             return $this->Api->send('Falta el CAF para el tipo de DTE ' . $DTE->getTipo(), 500);
         }
         if (!$DTE->timbrar($folios[$DTE->getTipo()]) or !$DTE->firmar($Firma) or !$DTE->schemaValidate()) {
             return $this->Api->send(implode("\n", \sasco\LibreDTE\Log::readAll()), 500);
         }
         // agregar el DTE al listado
         $Documentos[] = $DTE;
     }
     // armar EnvioDTE si se pasó fecha de resolución y número de resolución
     if (isset($this->Api->data['resolucion']) and !empty($this->Api->data['resolucion']['FchResol']) and isset($this->Api->data['resolucion']['NroResol'])) {
         $EnvioDte = new \sasco\LibreDTE\Sii\EnvioDte();
         foreach ($Documentos as $DTE) {
             $EnvioDte->agregar($DTE);
         }
         $EnvioDte->setCaratula(['RutEnvia' => $Firma->getID(), 'RutReceptor' => $certificacion ? '60803000-K' : $this->Api->data['Receptor']['RUTRecep'], 'FchResol' => $this->Api->data['resolucion']['FchResol'], 'NroResol' => (int) $this->Api->data['resolucion']['NroResol']]);
         $EnvioDte->setFirma($Firma);
         // generar
         $xml = $EnvioDte->generar();
         // validar schema del DTE
         if (!$EnvioDte->schemaValidate()) {
             return $this->Api->send(implode("\n", \sasco\LibreDTE\Log::readAll()), 500);
         }
         $dir = sys_get_temp_dir() . '/EnvioDTE_' . $this->Api->data['Emisor']['RUTEmisor'] . '_' . $this->Api->data['Receptor']['RUTRecep'] . '_' . date('U') . '.xml';
         file_put_contents($dir, $xml);
     } else {
         // directorio temporal para guardar los XML
         $dir = sys_get_temp_dir() . '/DTE_' . $this->Api->data['Emisor']['RUTEmisor'] . '_' . $this->Api->data['Receptor']['RUTRecep'] . '_' . date('U');
         if (is_dir($dir)) {
             \sasco\LibreDTE\File::rmdir($dir);
         }
         if (!mkdir($dir)) {
             $this->Api->send('No fue posible crear directorio temporal para DTEs', 500);
         }
         // procesar cada DTEs e ir agregándolo al directorio que se comprimirá
         foreach ($Documentos as $DTE) {
             // guardar XML
             file_put_contents($dir . '/dte_' . $this->Api->data['Emisor']['RUTEmisor'] . '_' . $DTE->getID() . '.xml', $DTE->saveXML());
         }
     }
     // guardar datos de emisor, receptor y estadísticas
     if (isset($this->Api->data['resolucion'])) {
         $resolucion = [];
         if (!empty($this->Api->data['resolucion']['FchResol'])) {
             $resolucion['FchResol'] = $this->Api->data['resolucion']['FchResol'];
         }
         if (isset($this->Api->data['resolucion']['NroResol'])) {
             $resolucion['NroResol'] = $this->Api->data['resolucion']['NroResol'];
         }
         $this->guardarEmisor($this->Api->data['Emisor'], $resolucion);
     } else {
         $this->guardarEmisor($this->Api->data['Emisor']);
     }
     $this->guardarReceptor($this->Api->data['Receptor']);
     list($emisor, $dv) = explode('-', $this->Api->data['Emisor']['RUTEmisor']);
     list($receptor, $dv) = explode('-', $this->Api->data['Receptor']['RUTRecep']);
     // entregar archivo comprimido que incluirá cada uno de los DTEs
     \sasco\LibreDTE\File::compress($dir, ['format' => 'zip', 'delete' => true]);
 }