| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- // Función para generar el XML de la factura
- function generarFacturaXML($datos) {
- // Crear documento XML
- $xml = new DOMDocument('1.0', 'UTF-8');
- $xml->formatOutput = true;
-
- // Raíz: factura
- $factura = $xml->createElement('factura');
- $factura->setAttribute('id', 'comprobante');
- $factura->setAttribute('version', '1.1.0');
- $xml->appendChild($factura);
-
- // infoTributaria
- $infoTributaria = $xml->createElement('infoTributaria');
- $factura->appendChild($infoTributaria);
-
- $infoTributaria->appendChild($xml->createElement('ambiente', $datos['ambiente'])); // 1=Pruebas, 2=Producción
- $infoTributaria->appendChild($xml->createElement('tipoEmision', $datos['tipoEmision'])); // 1=Normal
- $infoTributaria->appendChild($xml->createElement('razonSocial', $datos['razonSocial']));
- $infoTributaria->appendChild($xml->createElement('nombreComercial', $datos['nombreComercial']));
- $infoTributaria->appendChild($xml->createElement('ruc', $datos['ruc']));
- $infoTributaria->appendChild($xml->createElement('claveAcceso', $datos['claveAcceso'])); // Genera una clave única (ver algoritmo SRI)
- $infoTributaria->appendChild($xml->createElement('codDoc', '01')); // 01=Factura
- $infoTributaria->appendChild($xml->createElement('estab', $datos['estab']));
- $infoTributaria->appendChild($xml->createElement('ptoEmi', $datos['ptoEmi']));
- $infoTributaria->appendChild($xml->createElement('secuencial', $datos['secuencial']));
- $infoTributaria->appendChild($xml->createElement('dirMatriz', $datos['dirMatriz']));
-
- // infoFactura
- $infoFactura = $xml->createElement('infoFactura');
- $factura->appendChild($infoFactura);
-
- $infoFactura->appendChild($xml->createElement('fechaEmision', $datos['fechaEmision']));
- $infoFactura->appendChild($xml->createElement('dirEstablecimiento', $datos['dirEstablecimiento']));
- $infoFactura->appendChild($xml->createElement('contribuyenteEspecial', $datos['contribuyenteEspecial']));
- $infoFactura->appendChild($xml->createElement('obligadoContabilidad', $datos['obligadoContabilidad']));
-
- // Identificación del comprador
- $tipoIdentificacionComprador = $xml->createElement('tipoIdentificacionComprador', $datos['tipoIdentificacionComprador']);
- $infoFactura->appendChild($tipoIdentificacionComprador);
- $razonSocialComprador = $xml->createElement('razonSocialComprador', $datos['razonSocialComprador']);
- $infoFactura->appendChild($razonSocialComprador);
- $identificacionComprador = $xml->createElement('identificacionComprador', $datos['identificacionComprador']);
- $infoFactura->appendChild($identificacionComprador);
- $direccionComprador = $xml->createElement('direccionComprador', $datos['direccionComprador']);
- $infoFactura->appendChild($direccionComprador);
-
- $infoFactura->appendChild($xml->createElement('totalSinImpuestos', $datos['totalSinImpuestos']));
- $infoFactura->appendChild($xml->createElement('totalDescuento', $datos['totalDescuento']));
-
- // totalConImpuestos (simplificado: solo IVA)
- $totalConImpuestos = $xml->createElement('totalConImpuestos');
- $infoFactura->appendChild($totalConImpuestos);
- $totalImpuesto = $xml->createElement('totalImpuesto');
- $totalConImpuestos->appendChild($totalImpuesto);
- $totalImpuesto->appendChild($xml->createElement('codigo', '2')); // IVA
- $totalImpuesto->appendChild($xml->createElement('codigoPorcentaje', $datos['codigoPorcentaje']));
- $totalImpuesto->appendChild($xml->createElement('baseImponible', $datos['baseImponible']));
- $totalImpuesto->appendChild($xml->createElement('valor', $datos['valorImpuesto']));
-
- $infoFactura->appendChild($xml->createElement('propina', '0.00'));
- $infoFactura->appendChild($xml->createElement('importeTotal', $datos['importeTotal']));
- $infoFactura->appendChild($xml->createElement('moneda', 'DOLAR'));
-
- // pagos (simplificado)
- $pagos = $xml->createElement('pagos');
- $infoFactura->appendChild($pagos);
- $pago = $xml->createElement('pago');
- $pagos->appendChild($pago);
- $pago->appendChild($xml->createElement('formaPago', $datos['formaPago']));
- $pago->appendChild($xml->createElement('total', $datos['importeTotal']));
-
- // detalles
- $detalles = $xml->createElement('detalles');
- $factura->appendChild($detalles);
- foreach ($datos['detalles'] as $item) {
- $detalle = $xml->createElement('detalle');
- $detalles->appendChild($detalle);
- $detalle->appendChild($xml->createElement('codigoPrincipal', $item['codigoPrincipal']));
- $detalle->appendChild($xml->createElement('descripcion', $item['descripcion']));
- $detalle->appendChild($xml->createElement('cantidad', $item['cantidad']));
- $detalle->appendChild($xml->createElement('precioUnitario', $item['precioUnitario']));
- $detalle->appendChild($xml->createElement('descuento', $item['descuento']));
- $detalle->appendChild($xml->createElement('precioTotalSinImpuesto', $item['precioTotalSinImpuesto']));
-
- // impuestos por detalle (IVA)
- $impuestos = $xml->createElement('impuestos');
- $detalle->appendChild($impuestos);
- $impuesto = $xml->createElement('impuesto');
- $impuestos->appendChild($impuesto);
- $impuesto->appendChild($xml->createElement('codigo', '2'));
- $impuesto->appendChild($xml->createElement('codigoPorcentaje', $item['codigoPorcentaje']));
- $impuesto->appendChild($xml->createElement('tarifa', $item['tarifa']));
- $impuesto->appendChild($xml->createElement('baseImponible', $item['baseImponible']));
- $impuesto->appendChild($xml->createElement('valor', $item['valorImpuesto']));
- }
-
- // infoAdicional (opcional)
- $infoAdicional = $xml->createElement('infoAdicional');
- $factura->appendChild($infoAdicional);
- $campoAdicional = $xml->createElement('campoAdicional');
- $campoAdicional->setAttribute('nombre', 'Email');
- $campoAdicional->nodeValue = $datos['emailComprador'];
- $infoAdicional->appendChild($campoAdicional);
-
- return $xml->saveXML();
- }
- // Procesar formulario
- $xmlGenerado = '';
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- // Recopilar datos del formulario
- $datos = [
- 'ambiente' => $_POST['ambiente'],
- 'tipoEmision' => $_POST['tipoEmision'],
- 'razonSocial' => $_POST['razonSocial'],
- 'nombreComercial' => $_POST['nombreComercial'],
- 'ruc' => $_POST['ruc'],
- 'claveAcceso' => $_POST['claveAcceso'], // Debes generar esto dinámicamente
- 'estab' => $_POST['estab'],
- 'ptoEmi' => $_POST['ptoEmi'],
- 'secuencial' => $_POST['secuencial'],
- 'dirMatriz' => $_POST['dirMatriz'],
- 'fechaEmision' => $_POST['fechaEmision'],
- 'dirEstablecimiento' => $_POST['dirEstablecimiento'],
- 'contribuyenteEspecial' => $_POST['contribuyenteEspecial'],
- 'obligadoContabilidad' => $_POST['obligadoContabilidad'],
- 'tipoIdentificacionComprador' => $_POST['tipoIdentificacionComprador'],
- 'razonSocialComprador' => $_POST['razonSocialComprador'],
- 'identificacionComprador' => $_POST['identificacionComprador'],
- 'direccionComprador' => $_POST['direccionComprador'],
- 'totalSinImpuestos' => $_POST['totalSinImpuestos'],
- 'totalDescuento' => $_POST['totalDescuento'],
- 'codigoPorcentaje' => $_POST['codigoPorcentaje'],
- 'baseImponible' => $_POST['baseImponible'],
- 'valorImpuesto' => $_POST['valorImpuesto'],
- 'importeTotal' => $_POST['importeTotal'],
- 'formaPago' => $_POST['formaPago'],
- 'emailComprador' => $_POST['emailComprador'],
- 'detalles' => [
- [
- 'codigoPrincipal' => $_POST['codigoPrincipal1'],
- 'descripcion' => $_POST['descripcion1'],
- 'cantidad' => $_POST['cantidad1'],
- 'precioUnitario' => $_POST['precioUnitario1'],
- 'descuento' => $_POST['descuento1'],
- 'precioTotalSinImpuesto' => $_POST['precioTotalSinImpuesto1'],
- 'codigoPorcentaje' => $_POST['codigoPorcentaje1'],
- 'tarifa' => $_POST['tarifa1'],
- 'baseImponible' => $_POST['baseImponible1'],
- 'valorImpuesto' => $_POST['valorImpuesto1']
- ]
- // Agrega más items si es necesario
- ]
- ];
-
- $xmlGenerado = generarFacturaXML($datos);
-
- // Opcional: Guardar en archivo
- file_put_contents('factura_generada.xml', $xmlGenerado);
- }
- ?>
- <!DOCTYPE html>
- <html lang="es">
- <head>
- <meta charset="UTF-8">
- <title>Generar Factura XML para SRI</title>
- </head>
- <body>
- <h1>Formulario para Generar Factura Electrónica (SRI Ecuador)</h1>
- <form method="post">
- <h2>Info Tributaria</h2>
- Ambiente: <input type="text" name="ambiente" value="1" required><br>
- Tipo Emisión: <input type="text" name="tipoEmision" value="1" required><br>
- Razón Social: <input type="text" name="razonSocial" required><br>
- Nombre Comercial: <input type="text" name="nombreComercial" required><br>
- RUC: <input type="text" name="ruc" required><br>
- Clave de Acceso: <input type="text" name="claveAcceso" required> (Genera una única)<br>
- Establecimiento: <input type="text" name="estab" required><br>
- Punto de Emisión: <input type="text" name="ptoEmi" required><br>
- Secuencial: <input type="text" name="secuencial" required><br>
- Dirección Matriz: <input type="text" name="dirMatriz" required><br>
-
- <h2>Info Factura</h2>
- Fecha Emisión: <input type="date" name="fechaEmision" required><br>
- Dirección Establecimiento: <input type="text" name="dirEstablecimiento" required><br>
- Contribuyente Especial: <input type="text" name="contribuyenteEspecial"><br>
- Obligado Contabilidad: <input type="text" name="obligadoContabilidad" value="SI" required><br>
- Tipo Identificación Comprador: <input type="text" name="tipoIdentificacionComprador" value="04" required> (04=RUC)<br>
- Razón Social Comprador: <input type="text" name="razonSocialComprador" required><br>
- Identificación Comprador: <input type="text" name="identificacionComprador" required><br>
- Dirección Comprador: <input type="text" name="direccionComprador" required><br>
- Total Sin Impuestos: <input type="text" name="totalSinImpuestos" required><br>
- Total Descuento: <input type="text" name="totalDescuento" value="0.00" required><br>
- Código Porcentaje IVA: <input type="text" name="codigoPorcentaje" value="2" required><br>
- Base Imponible IVA: <input type="text" name="baseImponible" required><br>
- Valor Impuesto: <input type="text" name="valorImpuesto" required><br>
- Importe Total: <input type="text" name="importeTotal" required><br>
- Forma de Pago: <input type="text" name="formaPago" value="01" required> (01=Efectivo)<br>
- Email Comprador: <input type="email" name="emailComprador"><br>
-
- <h2>Detalles (Item 1)</h2>
- Código Principal: <input type="text" name="codigoPrincipal1" required><br>
- Descripción: <input type="text" name="descripcion1" required><br>
- Cantidad: <input type="text" name="cantidad1" required><br>
- Precio Unitario: <input type="text" name="precioUnitario1" required><br>
- Descuento: <input type="text" name="descuento1" value="0.00" required><br>
- Precio Total Sin Impuesto: <input type="text" name="precioTotalSinImpuesto1" required><br>
- Código Porcentaje IVA: <input type="text" name="codigoPorcentaje1" value="2" required><br>
- Tarifa IVA: <input type="text" name="tarifa1" value="15.00" required><br>
- Base Imponible IVA: <input type="text" name="baseImponible1" required><br>
- Valor Impuesto: <input type="text" name="valorImpuesto1" required><br>
-
- <button type="submit">Generar XML</button>
- </form>
-
- <?php if ($xmlGenerado): ?>
- <h2>XML Generado:</h2>
- <textarea rows="20" cols="100"><?php echo htmlspecialchars($xmlGenerado); ?></textarea>
- <br><a href="factura_generada.xml" download>Descargar XML</a>
- <?php endif; ?>
- </body>
- </html>
|