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); } ?>