factura.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. // Función para generar el XML de la factura
  3. function generarFacturaXML($datos) {
  4. // Crear documento XML
  5. $xml = new DOMDocument('1.0', 'UTF-8');
  6. $xml->formatOutput = true;
  7. // Raíz: factura
  8. $factura = $xml->createElement('factura');
  9. $factura->setAttribute('id', 'comprobante');
  10. $factura->setAttribute('version', '1.1.0');
  11. $xml->appendChild($factura);
  12. // infoTributaria
  13. $infoTributaria = $xml->createElement('infoTributaria');
  14. $factura->appendChild($infoTributaria);
  15. $infoTributaria->appendChild($xml->createElement('ambiente', $datos['ambiente'])); // 1=Pruebas, 2=Producción
  16. $infoTributaria->appendChild($xml->createElement('tipoEmision', $datos['tipoEmision'])); // 1=Normal
  17. $infoTributaria->appendChild($xml->createElement('razonSocial', $datos['razonSocial']));
  18. $infoTributaria->appendChild($xml->createElement('nombreComercial', $datos['nombreComercial']));
  19. $infoTributaria->appendChild($xml->createElement('ruc', $datos['ruc']));
  20. $infoTributaria->appendChild($xml->createElement('claveAcceso', $datos['claveAcceso'])); // Genera una clave única (ver algoritmo SRI)
  21. $infoTributaria->appendChild($xml->createElement('codDoc', '01')); // 01=Factura
  22. $infoTributaria->appendChild($xml->createElement('estab', $datos['estab']));
  23. $infoTributaria->appendChild($xml->createElement('ptoEmi', $datos['ptoEmi']));
  24. $infoTributaria->appendChild($xml->createElement('secuencial', $datos['secuencial']));
  25. $infoTributaria->appendChild($xml->createElement('dirMatriz', $datos['dirMatriz']));
  26. // infoFactura
  27. $infoFactura = $xml->createElement('infoFactura');
  28. $factura->appendChild($infoFactura);
  29. $infoFactura->appendChild($xml->createElement('fechaEmision', $datos['fechaEmision']));
  30. $infoFactura->appendChild($xml->createElement('dirEstablecimiento', $datos['dirEstablecimiento']));
  31. $infoFactura->appendChild($xml->createElement('contribuyenteEspecial', $datos['contribuyenteEspecial']));
  32. $infoFactura->appendChild($xml->createElement('obligadoContabilidad', $datos['obligadoContabilidad']));
  33. // Identificación del comprador
  34. $tipoIdentificacionComprador = $xml->createElement('tipoIdentificacionComprador', $datos['tipoIdentificacionComprador']);
  35. $infoFactura->appendChild($tipoIdentificacionComprador);
  36. $razonSocialComprador = $xml->createElement('razonSocialComprador', $datos['razonSocialComprador']);
  37. $infoFactura->appendChild($razonSocialComprador);
  38. $identificacionComprador = $xml->createElement('identificacionComprador', $datos['identificacionComprador']);
  39. $infoFactura->appendChild($identificacionComprador);
  40. $direccionComprador = $xml->createElement('direccionComprador', $datos['direccionComprador']);
  41. $infoFactura->appendChild($direccionComprador);
  42. $infoFactura->appendChild($xml->createElement('totalSinImpuestos', $datos['totalSinImpuestos']));
  43. $infoFactura->appendChild($xml->createElement('totalDescuento', $datos['totalDescuento']));
  44. // totalConImpuestos (simplificado: solo IVA)
  45. $totalConImpuestos = $xml->createElement('totalConImpuestos');
  46. $infoFactura->appendChild($totalConImpuestos);
  47. $totalImpuesto = $xml->createElement('totalImpuesto');
  48. $totalConImpuestos->appendChild($totalImpuesto);
  49. $totalImpuesto->appendChild($xml->createElement('codigo', '2')); // IVA
  50. $totalImpuesto->appendChild($xml->createElement('codigoPorcentaje', $datos['codigoPorcentaje']));
  51. $totalImpuesto->appendChild($xml->createElement('baseImponible', $datos['baseImponible']));
  52. $totalImpuesto->appendChild($xml->createElement('valor', $datos['valorImpuesto']));
  53. $infoFactura->appendChild($xml->createElement('propina', '0.00'));
  54. $infoFactura->appendChild($xml->createElement('importeTotal', $datos['importeTotal']));
  55. $infoFactura->appendChild($xml->createElement('moneda', 'DOLAR'));
  56. // pagos (simplificado)
  57. $pagos = $xml->createElement('pagos');
  58. $infoFactura->appendChild($pagos);
  59. $pago = $xml->createElement('pago');
  60. $pagos->appendChild($pago);
  61. $pago->appendChild($xml->createElement('formaPago', $datos['formaPago']));
  62. $pago->appendChild($xml->createElement('total', $datos['importeTotal']));
  63. // detalles
  64. $detalles = $xml->createElement('detalles');
  65. $factura->appendChild($detalles);
  66. foreach ($datos['detalles'] as $item) {
  67. $detalle = $xml->createElement('detalle');
  68. $detalles->appendChild($detalle);
  69. $detalle->appendChild($xml->createElement('codigoPrincipal', $item['codigoPrincipal']));
  70. $detalle->appendChild($xml->createElement('descripcion', $item['descripcion']));
  71. $detalle->appendChild($xml->createElement('cantidad', $item['cantidad']));
  72. $detalle->appendChild($xml->createElement('precioUnitario', $item['precioUnitario']));
  73. $detalle->appendChild($xml->createElement('descuento', $item['descuento']));
  74. $detalle->appendChild($xml->createElement('precioTotalSinImpuesto', $item['precioTotalSinImpuesto']));
  75. // impuestos por detalle (IVA)
  76. $impuestos = $xml->createElement('impuestos');
  77. $detalle->appendChild($impuestos);
  78. $impuesto = $xml->createElement('impuesto');
  79. $impuestos->appendChild($impuesto);
  80. $impuesto->appendChild($xml->createElement('codigo', '2'));
  81. $impuesto->appendChild($xml->createElement('codigoPorcentaje', $item['codigoPorcentaje']));
  82. $impuesto->appendChild($xml->createElement('tarifa', $item['tarifa']));
  83. $impuesto->appendChild($xml->createElement('baseImponible', $item['baseImponible']));
  84. $impuesto->appendChild($xml->createElement('valor', $item['valorImpuesto']));
  85. }
  86. // infoAdicional (opcional)
  87. $infoAdicional = $xml->createElement('infoAdicional');
  88. $factura->appendChild($infoAdicional);
  89. $campoAdicional = $xml->createElement('campoAdicional');
  90. $campoAdicional->setAttribute('nombre', 'Email');
  91. $campoAdicional->nodeValue = $datos['emailComprador'];
  92. $infoAdicional->appendChild($campoAdicional);
  93. return $xml->saveXML();
  94. }
  95. // Procesar formulario
  96. $xmlGenerado = '';
  97. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  98. // Recopilar datos del formulario
  99. $datos = [
  100. 'ambiente' => $_POST['ambiente'],
  101. 'tipoEmision' => $_POST['tipoEmision'],
  102. 'razonSocial' => $_POST['razonSocial'],
  103. 'nombreComercial' => $_POST['nombreComercial'],
  104. 'ruc' => $_POST['ruc'],
  105. 'claveAcceso' => $_POST['claveAcceso'], // Debes generar esto dinámicamente
  106. 'estab' => $_POST['estab'],
  107. 'ptoEmi' => $_POST['ptoEmi'],
  108. 'secuencial' => $_POST['secuencial'],
  109. 'dirMatriz' => $_POST['dirMatriz'],
  110. 'fechaEmision' => $_POST['fechaEmision'],
  111. 'dirEstablecimiento' => $_POST['dirEstablecimiento'],
  112. 'contribuyenteEspecial' => $_POST['contribuyenteEspecial'],
  113. 'obligadoContabilidad' => $_POST['obligadoContabilidad'],
  114. 'tipoIdentificacionComprador' => $_POST['tipoIdentificacionComprador'],
  115. 'razonSocialComprador' => $_POST['razonSocialComprador'],
  116. 'identificacionComprador' => $_POST['identificacionComprador'],
  117. 'direccionComprador' => $_POST['direccionComprador'],
  118. 'totalSinImpuestos' => $_POST['totalSinImpuestos'],
  119. 'totalDescuento' => $_POST['totalDescuento'],
  120. 'codigoPorcentaje' => $_POST['codigoPorcentaje'],
  121. 'baseImponible' => $_POST['baseImponible'],
  122. 'valorImpuesto' => $_POST['valorImpuesto'],
  123. 'importeTotal' => $_POST['importeTotal'],
  124. 'formaPago' => $_POST['formaPago'],
  125. 'emailComprador' => $_POST['emailComprador'],
  126. 'detalles' => [
  127. [
  128. 'codigoPrincipal' => $_POST['codigoPrincipal1'],
  129. 'descripcion' => $_POST['descripcion1'],
  130. 'cantidad' => $_POST['cantidad1'],
  131. 'precioUnitario' => $_POST['precioUnitario1'],
  132. 'descuento' => $_POST['descuento1'],
  133. 'precioTotalSinImpuesto' => $_POST['precioTotalSinImpuesto1'],
  134. 'codigoPorcentaje' => $_POST['codigoPorcentaje1'],
  135. 'tarifa' => $_POST['tarifa1'],
  136. 'baseImponible' => $_POST['baseImponible1'],
  137. 'valorImpuesto' => $_POST['valorImpuesto1']
  138. ]
  139. // Agrega más items si es necesario
  140. ]
  141. ];
  142. $xmlGenerado = generarFacturaXML($datos);
  143. // Opcional: Guardar en archivo
  144. file_put_contents('factura_generada.xml', $xmlGenerado);
  145. }
  146. ?>
  147. <!DOCTYPE html>
  148. <html lang="es">
  149. <head>
  150. <meta charset="UTF-8">
  151. <title>Generar Factura XML para SRI</title>
  152. </head>
  153. <body>
  154. <h1>Formulario para Generar Factura Electrónica (SRI Ecuador)</h1>
  155. <form method="post">
  156. <h2>Info Tributaria</h2>
  157. Ambiente: <input type="text" name="ambiente" value="1" required><br>
  158. Tipo Emisión: <input type="text" name="tipoEmision" value="1" required><br>
  159. Razón Social: <input type="text" name="razonSocial" required><br>
  160. Nombre Comercial: <input type="text" name="nombreComercial" required><br>
  161. RUC: <input type="text" name="ruc" required><br>
  162. Clave de Acceso: <input type="text" name="claveAcceso" required> (Genera una única)<br>
  163. Establecimiento: <input type="text" name="estab" required><br>
  164. Punto de Emisión: <input type="text" name="ptoEmi" required><br>
  165. Secuencial: <input type="text" name="secuencial" required><br>
  166. Dirección Matriz: <input type="text" name="dirMatriz" required><br>
  167. <h2>Info Factura</h2>
  168. Fecha Emisión: <input type="date" name="fechaEmision" required><br>
  169. Dirección Establecimiento: <input type="text" name="dirEstablecimiento" required><br>
  170. Contribuyente Especial: <input type="text" name="contribuyenteEspecial"><br>
  171. Obligado Contabilidad: <input type="text" name="obligadoContabilidad" value="SI" required><br>
  172. Tipo Identificación Comprador: <input type="text" name="tipoIdentificacionComprador" value="04" required> (04=RUC)<br>
  173. Razón Social Comprador: <input type="text" name="razonSocialComprador" required><br>
  174. Identificación Comprador: <input type="text" name="identificacionComprador" required><br>
  175. Dirección Comprador: <input type="text" name="direccionComprador" required><br>
  176. Total Sin Impuestos: <input type="text" name="totalSinImpuestos" required><br>
  177. Total Descuento: <input type="text" name="totalDescuento" value="0.00" required><br>
  178. Código Porcentaje IVA: <input type="text" name="codigoPorcentaje" value="2" required><br>
  179. Base Imponible IVA: <input type="text" name="baseImponible" required><br>
  180. Valor Impuesto: <input type="text" name="valorImpuesto" required><br>
  181. Importe Total: <input type="text" name="importeTotal" required><br>
  182. Forma de Pago: <input type="text" name="formaPago" value="01" required> (01=Efectivo)<br>
  183. Email Comprador: <input type="email" name="emailComprador"><br>
  184. <h2>Detalles (Item 1)</h2>
  185. Código Principal: <input type="text" name="codigoPrincipal1" required><br>
  186. Descripción: <input type="text" name="descripcion1" required><br>
  187. Cantidad: <input type="text" name="cantidad1" required><br>
  188. Precio Unitario: <input type="text" name="precioUnitario1" required><br>
  189. Descuento: <input type="text" name="descuento1" value="0.00" required><br>
  190. Precio Total Sin Impuesto: <input type="text" name="precioTotalSinImpuesto1" required><br>
  191. Código Porcentaje IVA: <input type="text" name="codigoPorcentaje1" value="2" required><br>
  192. Tarifa IVA: <input type="text" name="tarifa1" value="15.00" required><br>
  193. Base Imponible IVA: <input type="text" name="baseImponible1" required><br>
  194. Valor Impuesto: <input type="text" name="valorImpuesto1" required><br>
  195. <button type="submit">Generar XML</button>
  196. </form>
  197. <?php if ($xmlGenerado): ?>
  198. <h2>XML Generado:</h2>
  199. <textarea rows="20" cols="100"><?php echo htmlspecialchars($xmlGenerado); ?></textarea>
  200. <br><a href="factura_generada.xml" download>Descargar XML</a>
  201. <?php endif; ?>
  202. </body>
  203. </html>