Ajustes
This commit is contained in:
@@ -796,4 +796,313 @@ function update_status() {
|
||||
}
|
||||
echo json_encode(['success'=>true]);
|
||||
exit;
|
||||
}
|
||||
|
||||
function pdf() {
|
||||
// Verificar que se recibió el ID
|
||||
if (!isset($_GET['id'])) {
|
||||
http_response_code(400);
|
||||
echo "ID de solicitud requerido";
|
||||
return;
|
||||
}
|
||||
|
||||
$id_solicitud = (int)$_GET['id'];
|
||||
|
||||
try {
|
||||
// 1. Conexión
|
||||
$conn = getConnection();
|
||||
|
||||
// 2. Obtener datos de la solicitud principal
|
||||
$sql = "
|
||||
SELECT
|
||||
s.*,
|
||||
i.nombre as importador_nombre,
|
||||
i.rfc as importador_rfc,
|
||||
i.calle,
|
||||
i.num_exterior,
|
||||
i.num_interior,
|
||||
i.ciudad,
|
||||
i.colonia,
|
||||
i.codigo_postal,
|
||||
i.estado,
|
||||
i.telefono,
|
||||
i.correo,
|
||||
t.nombre as transportista_nombre,
|
||||
c.nombre as chofer_nombre
|
||||
FROM solicitud_importacion_factura s
|
||||
LEFT JOIN informacion_general i ON s.id_importador = i.id_usuario
|
||||
LEFT JOIN transportistas t ON s.transportista_id = t.id_transportista
|
||||
LEFT JOIN choferes c ON s.chofer_id = c.id_chofer
|
||||
WHERE s.id_solicitud = ?
|
||||
";
|
||||
|
||||
$params = array($id_solicitud);
|
||||
$stmt = sqlsrv_query($conn, $sql, $params);
|
||||
|
||||
if ($stmt === false) {
|
||||
throw new Exception("Error en la consulta: " . print_r(sqlsrv_errors(), true));
|
||||
}
|
||||
|
||||
$solicitud = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
if (!$solicitud) {
|
||||
http_response_code(404);
|
||||
echo "Solicitud no encontrada";
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Obtener partidas de la solicitud
|
||||
$sql = "
|
||||
SELECT
|
||||
p.*,
|
||||
u.descripcion as unidad_descripcion
|
||||
FROM solicitud_importacion_partidas p
|
||||
LEFT JOIN unidades_medida_apendice7 u ON p.unidad_comercial_id = u.id
|
||||
WHERE p.id_solicitud = ?
|
||||
ORDER BY p.id_partida
|
||||
";
|
||||
|
||||
$params = array($id_solicitud);
|
||||
$stmt = sqlsrv_query($conn, $sql, $params);
|
||||
|
||||
if ($stmt === false) {
|
||||
throw new Exception("Error en la consulta de partidas: " . print_r(sqlsrv_errors(), true));
|
||||
}
|
||||
|
||||
$partidas = array();
|
||||
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
||||
$partidas[] = $row;
|
||||
}
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
// 4. Configuración del sistema
|
||||
global $config;
|
||||
$nombre_sistema = $config['nombre_plataforma'] ?? 'Sistema Integral para Importadores de Hidrocarburos';
|
||||
$siglas = $config['siglas'] ?? 'SIIH';
|
||||
$logo_url = $config['logo_url'] ?? 'assets/img/logo_siih.png';
|
||||
|
||||
// 5. Generar HTML del PDF
|
||||
$html = generarHTMLPDF($solicitud, $partidas, $nombre_sistema, $siglas, $logo_url);
|
||||
|
||||
// 6. Configurar headers para PDF
|
||||
header('Content-Type: application/pdf');
|
||||
header('Content-Disposition: inline; filename="solicitud_importacion_' . $id_solicitud . '.pdf"');
|
||||
|
||||
// 7. Generar PDF usando DomPDF
|
||||
// Opción 1: DomPDF v0.8.x (compatible con PHP 5.x)
|
||||
if (file_exists('lib/dompdf/dompdf-0.8.6/dompdf_config.inc.php')) {
|
||||
require_once 'lib/dompdf/dompdf-0.8.6/dompdf_config.inc.php';
|
||||
|
||||
$dompdf = new DOMPDF();
|
||||
$dompdf->load_html($html);
|
||||
$dompdf->set_paper('A4', 'portrait');
|
||||
$dompdf->render();
|
||||
|
||||
// Headers
|
||||
header('Content-Type: application/pdf');
|
||||
header('Content-Disposition: inline; filename="solicitud_importacion_' . $id_solicitud . '.pdf"');
|
||||
|
||||
echo $dompdf->output();
|
||||
|
||||
// Opción 2: DomPDF v1.x/2.x (PHP 7+)
|
||||
} elseif (file_exists('vendor/autoload.php') && PHP_VERSION_ID >= 70000) {
|
||||
require_once 'vendor/autoload.php';
|
||||
|
||||
$options = new \Dompdf\Options();
|
||||
$options->set('defaultFont', 'Arial');
|
||||
$options->set('isRemoteEnabled', true);
|
||||
|
||||
$dompdf = new \Dompdf\Dompdf($options);
|
||||
$dompdf->loadHtml($html);
|
||||
$dompdf->setPaper('A4', 'portrait');
|
||||
$dompdf->render();
|
||||
|
||||
header('Content-Type: application/pdf');
|
||||
header('Content-Disposition: inline; filename="solicitud_importacion_' . $id_solicitud . '.pdf"');
|
||||
|
||||
echo $dompdf->output();
|
||||
|
||||
// Opción 3: Alternativa usando mPDF (compatible con PHP 5.6+)
|
||||
} elseif (file_exists('lib/mpdf/mpdf.php')) {
|
||||
require_once 'lib/mpdf/mpdf.php';
|
||||
|
||||
$mpdf = new mPDF();
|
||||
$mpdf->WriteHTML($html);
|
||||
$mpdf->Output('solicitud_importacion_' . $id_solicitud . '.pdf', 'I');
|
||||
|
||||
// Opción 4: TCPDF (muy compatible)
|
||||
} elseif (file_exists('lib/tcpdf/tcpdf.php')) {
|
||||
require_once 'lib/tcpdf/tcpdf.php';
|
||||
|
||||
$pdf = new TCPDF();
|
||||
$pdf->AddPage();
|
||||
$pdf->writeHTML($html, true, false, true, false, '');
|
||||
$pdf->Output('solicitud_importacion_' . $id_solicitud . '.pdf', 'I');
|
||||
|
||||
} else {
|
||||
throw new Exception("No hay librerías PDF disponibles. Instala DomPDF, mPDF o TCPDF.");
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error generando PDF: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo "Error interno del servidor: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
function generarHTMLPDF($solicitud, $partidas, $nombre_sistema, $siglas, $logo_url) {
|
||||
// Formatear fecha
|
||||
$fecha_expedicion = date('d/m/Y');
|
||||
$fecha_factura = $solicitud['fecha_factura']->format('d/m/Y');
|
||||
$fecha_vencimiento = (clone $solicitud['fecha_factura'])->modify('+30 days')->format('d/m/Y');
|
||||
|
||||
|
||||
// Construir dirección del importador
|
||||
$direccion_completa = trim(
|
||||
($solicitud['calle'] ?? '') . ' ' .
|
||||
($solicitud['num_exterior'] ?? '') . ' ' .
|
||||
($solicitud['num_interior'] ? 'Int. ' . $solicitud['num_interior'] : '') . ', ' .
|
||||
($solicitud['colonia'] ?? '') . ', ' .
|
||||
($solicitud['ciudad'] ?? '') . ', ' .
|
||||
($solicitud['estado'] ?? '') . ' ' .
|
||||
($solicitud['codigo_postal'] ?? '')
|
||||
);
|
||||
|
||||
// Calcular totales
|
||||
$subtotal = 0;
|
||||
foreach ($partidas as $partida) {
|
||||
$subtotal += (float)$partida['valor_factura'];
|
||||
}
|
||||
$descuento_pct = 0; // Puedes calcularlo si tienes descuentos
|
||||
$descuento = $subtotal * ($descuento_pct / 100);
|
||||
$total = $subtotal - $descuento;
|
||||
|
||||
$html = '
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Solicitud de Importación</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; font-size: 9px; margin: 0; padding: 15px; line-height: 1.2; }
|
||||
.header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 10px; border-bottom: 2px solid #000; padding-bottom: 10px; }
|
||||
.logo-section { width: 120px; }
|
||||
.logo { max-width: 100px; height: auto; }
|
||||
.company-info { flex: 1; text-align: center; margin: 0 20px; }
|
||||
.company-name { font-weight: bold; font-size: 12px; margin-bottom: 3px; }
|
||||
.invoice-info { width: 180px; border: 1px solid #000; padding: 8px; }
|
||||
.invoice-title { background-color: #f0f0f0; text-align: center; font-weight: bold; margin-bottom: 5px; padding: 3px; }
|
||||
.info-section { display: flex; margin-bottom: 15px; }
|
||||
.client-info, .dates-info { flex: 1; border: 1px solid #000; margin-right: 10px; padding: 8px; }
|
||||
.dates-info { margin-right: 0; width: 200px; }
|
||||
.section-title { background-color: #d0d0d0; font-weight: bold; padding: 3px; margin-bottom: 5px; text-align: center; }
|
||||
.products-table { width: 100%; border-collapse: collapse; margin-bottom: 15px; border: 1px solid #000; }
|
||||
.products-table th,
|
||||
.products-table td { border: 1px solid #000; padding: 4px; text-align: left; font-size: 8px; }
|
||||
.products-table th { background-color: #f0f0f0; font-weight: bold; text-align: center; }
|
||||
.text-center { text-align: center; }
|
||||
.text-right { text-align: right; }
|
||||
.font-bold { font-weight: bold; }
|
||||
.totals-section { float: right; width: 250px; margin-top: 10px; }
|
||||
.total-row { display: flex; justify-content: space-between; margin-bottom: 3px; }
|
||||
.footer-info { clear: both; margin-top: 30px; font-size: 8px; background-color: #e0e0e0; padding: 5px; text-align: center; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<!-- ENCABEZADO -->
|
||||
<div class="header">
|
||||
<div class="logo-section">
|
||||
<img src="' . htmlspecialchars($logo_url) . '" alt="Logo" class="logo">
|
||||
<div style="font-size: 8px; margin-top: 5px;">' . htmlspecialchars($siglas) . '</div>
|
||||
</div>
|
||||
|
||||
<div class="company-info">
|
||||
<div class="company-name">' . htmlspecialchars($nombre_sistema) . '</div>
|
||||
<div>' . htmlspecialchars($direccion_completa ?: 'Dirección no disponible') . '</div>
|
||||
<div>RFC: ' . htmlspecialchars($solicitud['importador_rfc'] ?? 'No disponible') . '</div>
|
||||
<div>Tel: ' . htmlspecialchars($solicitud['telefono'] ?? 'No disponible') . '</div>
|
||||
<div>Email: ' . htmlspecialchars($solicitud['correo'] ?? 'No disponible') . '</div>
|
||||
</div>
|
||||
|
||||
<div class="invoice-info">
|
||||
<div class="invoice-title">Solicitud de Importación</div>
|
||||
<div><strong>No. ' . htmlspecialchars($solicitud['id_solicitud']) . '</strong></div>
|
||||
<div>Régimen simplificado de confianza (RESICO) - 626</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- INFORMACIÓN DEL CLIENTE Y FECHAS -->
|
||||
<div class="info-section">
|
||||
<div class="client-info">
|
||||
<div class="section-title">RAZÓN SOCIAL / ' . htmlspecialchars($solicitud['importador_nombre'] ?? 'No disponible') . '</div>
|
||||
<div><strong>DOMICILIO FISCAL:</strong> ' . htmlspecialchars($direccion_completa) . '</div>
|
||||
<div style="margin-top: 10px;">
|
||||
<div><strong>RFC:</strong> ' . htmlspecialchars($solicitud['importador_rfc'] ?? 'No disponible') . '</div>
|
||||
<div><strong>TELÉFONO:</strong> ' . htmlspecialchars($solicitud['telefono'] ?? 'No disponible') . '</div>
|
||||
</div>
|
||||
<div style="margin-top: 10px;">
|
||||
<div><strong>RÉGIMEN FISCAL:</strong> 601-General de Ley Personas Morales</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="dates-info">
|
||||
<div class="section-title">FECHA DE EXPEDICIÓN</div>
|
||||
<div class="text-center font-bold" style="font-size: 12px;">' . $fecha_expedicion . '</div>
|
||||
<div class="section-title" style="margin-top: 10px;">FECHA DE VENCIMIENTO</div>
|
||||
<div class="text-center">' . $fecha_vencimiento . '</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- TABLA DE PRODUCTOS/PARTIDAS -->
|
||||
<table class="products-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Producto</th>
|
||||
<th>Unidad de Medida</th>
|
||||
<th>Precio Unitario</th>
|
||||
<th>Cantidad</th>
|
||||
<th>Descuento</th>
|
||||
<th>Total</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>';
|
||||
|
||||
// Agregar partidas
|
||||
foreach ($partidas as $partida) {
|
||||
$precio_unitario = (float)($partida['precio_unitario'] ?? 0);
|
||||
$cantidad = (float)($partida['cantidad_comercial'] ?? 0);
|
||||
$valor_partida = (float)($partida['valor_factura'] ?? 0);
|
||||
|
||||
$html .= '<tr>
|
||||
<td>' . htmlspecialchars($partida['descripcion']) . '</td>
|
||||
<td>' . htmlspecialchars($partida['unidad_descripcion'] ?? 'Unidad de servicio (E48)') . '</td>
|
||||
<td class="text-right">$' . number_format($precio_unitario > 0 ? $precio_unitario : $valor_partida, 2) . '</td>
|
||||
<td class="text-center">' . number_format($cantidad > 0 ? $cantidad : 1, 0) . '</td>
|
||||
<td class="text-right">' . number_format($descuento_pct, 1) . '%</td>
|
||||
<td class="text-right">$' . number_format($valor_partida, 2) . '</td>
|
||||
</tr>';
|
||||
}
|
||||
|
||||
$html .= '
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- TOTALES -->
|
||||
<div class="totals-section">
|
||||
<div class="total-row">
|
||||
<span><strong>Total:</strong></span>
|
||||
<span><strong>$' . number_format($total, 2) . '</strong></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- NOTA INFERIOR -->
|
||||
<div class="footer-note">
|
||||
Veinticinco mil seiscientos 50/100 M.N
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
Reference in New Issue
Block a user