CRUD gestión de locaciones

This commit is contained in:
2025-06-09 08:50:23 -06:00
parent 2eb3ec6061
commit 55fb7c0bf4
28 changed files with 1982 additions and 234 deletions

View File

@@ -754,12 +754,27 @@ function ajax_lista()
if ($status === 200 && ($json = json_decode($resp, true)) && is_array($json)) {
foreach ($json as $p) {
$clave = htmlspecialchars($p['Clave'] ?? '', ENT_QUOTES);
// Construir dirección
$direccion = trim(implode(', ', array_filter([
$p['Calles'] ?? '',
'Num. Ext: ' . ($p['NumExt'] ?? ''),
'Num. Int: ' . ($p['NumInt'] ?? ''),
$p['Colonia'] ?? '',
$p['Municipio'] ?? '',
$p['Ciudad'] ?? '',
'C.P. ' . ($p['CodigoPostal'] ?? ''),
$p['EntidadFederativa'] ?? '',
$p['Pais'] ?? ''
])));
$dataList[] = [
$clave,
htmlspecialchars($p['Nombre'] ?? '', ENT_QUOTES),
htmlspecialchars($p['RFC'] ?? '', ENT_QUOTES),
htmlspecialchars($p['Ciudad'] ?? '', ENT_QUOTES),
htmlspecialchars($p['Telefono']?? '', ENT_QUOTES),
htmlspecialchars($direccion ?? '', ENT_QUOTES),
// Acciones
"<a href=\"/IMPORTADORES/proveedores/editar?clave=" . rawurlencode($clave) . "\" class=\"btn btn-sm btn-primary\">✏️</a>
<button class=\"btn btn-sm btn-danger\" onclick=\"confirmDelete('{$clave}')\">🗑️</button>"
@@ -990,7 +1005,6 @@ function update_status() {
exit;
}
// Función para generar el PDF
use Dompdf\Dompdf;
use Dompdf\Options;
@@ -1049,6 +1063,12 @@ function pdf() {
return;
}
// 2.5. Obtener información del proveedor desde API
$proveedor_info = null;
if (!empty($solicitud['proveedor_clave'])) {
$proveedor_info = obtenerProveedorPorClave($solicitud['proveedor_clave']);
}
// 3. Obtener partidas de la solicitud
$sql = "
SELECT
@@ -1074,15 +1094,18 @@ function pdf() {
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_path = realpath(__DIR__ . '/../../public/assets/img/logo_siih.png');
$logo_url = 'file://' . $logo_path;
$config = "SELECT * FROM configuracion_sistema";
$stmt = sqlsrv_query($conn, $config);
if ($stmt === false) {
throw new Exception("Error en la consulta: " . print_r(sqlsrv_errors(), true));
}
$configuracion = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
// 5. Generar HTML del PDF
$html = generarHTMLPDF($solicitud, $partidas, $nombre_sistema, $siglas, $logo_url);
$html = generarHTMLPDF($solicitud, $partidas, $configuracion);
// 6. Generar PDF usando DomPDF con Composer
require_once __DIR__ . '/../../vendor/autoload.php'; // Ajusta ruta si es necesario
@@ -1108,12 +1131,59 @@ function pdf() {
}
}
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');
// NUEVA FUNCIÓN: Obtener información del proveedor por clave
function obtenerProveedorPorClave($clave) {
// Obtener token de la API
$token = getApiToken();
if (!$token) {
error_log('[obtenerProveedorPorClave] Sin token válido');
return null;
}
// Construir URL de la API
$apiBase = rtrim($_ENV['API_URL'] ?? '', '/');
$url = $apiBase . '/proveedores';
// Ejecutar cURL para obtener todos los proveedores
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => ["Authorization: $token", 'Accept: application/json'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
]);
$resp = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status !== 200) {
error_log("[obtenerProveedorPorClave] Error HTTP $status al obtener proveedores");
return null;
}
$proveedores = json_decode($resp, true);
if (!is_array($proveedores)) {
error_log('[obtenerProveedorPorClave] Respuesta de API no es un array válido');
return null;
}
// Buscar el proveedor por clave
foreach ($proveedores as $proveedor) {
// Probar tanto 'Clave' como 'CLAVE' por si acaso
$proveedor_clave = $proveedor['CLAVE'] ?? $proveedor['Clave'] ?? null;
if ($proveedor_clave === $clave) {
return $proveedor;
}
}
error_log("[obtenerProveedorPorClave] Proveedor con clave '$clave' no encontrado");
return null;
}
function generarHTMLPDF($solicitud, $partidas, $configuracion) {
// Formatear fecha
$fecha_expedicion = $solicitud['fecha_factura']->format('d/m/Y');
$fecha_vencimiento = $solicitud['fecha_factura']->modify('+30 days')->format('d/m/Y');
// Construir dirección del importador
$direccion_completa = trim(
@@ -1126,6 +1196,37 @@ function generarHTMLPDF($solicitud, $partidas, $nombre_sistema, $siglas, $logo_u
($solicitud['codigo_postal'] ?? '')
);
// Construir información del proveedor
$proveedor_nombre = 'Proveedor no disponible';
$proveedor_rfc = 'RFC no disponible';
$proveedor_direccion = 'Dirección no disponible';
$proveedor_telefono = 'Teléfono no disponible';
// Construir información del proveedor
if ($proveedor_info) {
// Usar nombres de campos en mayúsculas según la estructura de la tabla
$proveedor_nombre = $proveedor_info['NOMBRE'] ?? $proveedor_info['Nombre'] ?? 'Nombre no disponible';
$proveedor_rfc = $proveedor_info['RFC'] ?? $proveedor_info['IDENTFISCAL'] ?? 'RFC no disponible';
$proveedor_telefono = trim($proveedor_info['TELEFONO'] ?? $proveedor_info['Telefono'] ?? 'Teléfono no disponible');
// Construir dirección del proveedor usando los campos correctos
$direccion_partes = array_filter([
$proveedor_info['CALLES'] ?? $proveedor_info['Calles'] ?? '',
($proveedor_info['NUMEXT'] ?? $proveedor_info['NumExt'] ?? '') ? 'Num. Ext: ' . ($proveedor_info['NUMEXT'] ?? $proveedor_info['NumExt']) : '',
($proveedor_info['NUMINT'] ?? $proveedor_info['NumInt'] ?? '') ? 'Num. Int: ' . ($proveedor_info['NUMINT'] ?? $proveedor_info['NumInt']) : '',
$proveedor_info['COLONIA'] ?? $proveedor_info['Colonia'] ?? '',
$proveedor_info['MUNICIPIO'] ?? $proveedor_info['Municipio'] ?? '',
$proveedor_info['CIUDAD'] ?? $proveedor_info['Ciudad'] ?? '',
($proveedor_info['CODIGOPOSTAL'] ?? $proveedor_info['CodigoPostal'] ?? '') ? 'C.P. ' . ($proveedor_info['CODIGOPOSTAL'] ?? $proveedor_info['CodigoPostal']) : '',
$proveedor_info['ENTIDADFEDERATIVA'] ?? $proveedor_info['EntidadFederativa'] ?? '',
$proveedor_info['PAIS'] ?? $proveedor_info['Pais'] ?? ''
]);
if (!empty($direccion_partes)) {
$proveedor_direccion = implode(', ', $direccion_partes);
}
}
// Calcular el total sumando todas las partidas
$total = 0;
foreach ($partidas as $partida) {
@@ -1189,16 +1290,37 @@ function generarHTMLPDF($solicitud, $partidas, $nombre_sistema, $siglas, $logo_u
return trim($resultado);
}
// Obtener la moneda de la solicitud o usar MXN por defecto
$moneda_codigo = $solicitud['tipo_moneda'] ?? 'MXN';
// Configuración de monedas
$monedas_config = [
'MXN' => ['nombre' => 'PESOS', 'sufijo' => 'M.N.', 'centavos' => 'CENTAVOS'],
'USD' => ['nombre' => 'DÓLARES', 'sufijo' => 'USD', 'centavos' => 'CENTAVOS'],
'EUR' => ['nombre' => 'EUROS', 'sufijo' => 'EUR', 'centavos' => 'CÉNTIMOS'],
'CNY' => ['nombre' => 'YUANES', 'sufijo' => 'CNY', 'centavos' => 'JIAO'],
'GBP' => ['nombre' => 'LIBRAS', 'sufijo' => 'GBP', 'centavos' => 'PENIQUES'],
'JPY' => ['nombre' => 'YENES', 'sufijo' => 'JPY', 'centavos' => 'SEN']
];
$config_moneda = $monedas_config[$moneda_codigo] ?? $monedas_config['MXN'];
// Convertir total a texto
$partes = explode('.', number_format($total, 2, '.', ''));
$pesos = (int)$partes[0];
$centavos = (int)$partes[1];
$enteros = (int)$partes[0];
$decimales = (int)$partes[1];
$total_texto = strtoupper(numeroATexto($pesos)) . ' PESOS';
if ($centavos > 0) {
$total_texto .= ' CON ' . str_pad($centavos, 2, '0', STR_PAD_LEFT) . '/100 M.N.';
$total_texto = strtoupper(numeroATexto($enteros)) . ' ' . $config_moneda['nombre'];
// Para JPY no se usan decimales tradicionalmente
if ($moneda_codigo === 'JPY') {
$total_texto .= ' ' . $config_moneda['sufijo'];
} else {
$total_texto .= ' 00/100 M.N.';
if ($decimales > 0) {
$total_texto .= ' CON ' . str_pad($decimales, 2, '0', STR_PAD_LEFT) . '/100 ' . $config_moneda['sufijo'];
} else {
$total_texto .= ' 00/100 ' . $config_moneda['sufijo'];
}
}
$html = '
@@ -1209,30 +1331,38 @@ function generarHTMLPDF($solicitud, $partidas, $nombre_sistema, $siglas, $logo_u
<title>Solicitud de Importación</title>
<style>
body { font-family: Arial, sans-serif; font-size: 9px; margin: 0; padding: 15px; line-height: 1.2; }
<!-- Encabezado -->
.header { margin-bottom: 10px; border-bottom: 2px solid #000; padding-bottom: 10px; font-size: 10px; }
.logo-section { width: 20%; text-align: left; vertical-align: center; }
.logo { max-width: 100px; height: auto; }
.company-info { width: 60%; text-align: center; vertical-align: top; }
/** Encabezado **/
.header { padding-bottom: 50px; }
.logo-section { width: 20%; text-align: left; }
.logo { max-width: 100px; height: auto; vertical-align: center; }
.siglas { font-size: 15px; }
.company-info { width: 60%; text-align: center; vertical-align: top; font-size: 12px; }
.company-name { font-weight: bold; font-size: 25px; margin-bottom: 3px; }
.invoice-info { width: 20%; text-align: right; vertical-align: top; font-size: 12px; }
<!-- Información del Cliente -->
.info-section { margin-bottom: 10px; border-bottom: 2px solid #000; padding-bottom: 10px; }
.client-info { width: 75%; border: 0.5px solid #000; margin-right: 10px; }
/** Sección de Información **/
.info-section { border: 1px solid black; border-collapse: collapse; width: 100%; table-layout: fixed; }
.clave-section { border: 0.5px solid black; border-collapse: collapse; width: 100%; table-layout: fixed; padding-bottom: 15px; }
/** Información del Proveedor **/
.proveedor-info { width: 100%; }
.provedor-info table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.p-field { width: 100px; background-color: #d0d0d0; font-weight: bold; text-align: center; font-size: 12px; }
.field { border-bottom: 0.5px solid #000; padding: 5px; font-size: 12px; }
.dates-info { width: 25%; border: 0.5px solid #000; }
.section-title { background-color: #d0d0d0; font-weight: bold; padding: 5px; text-align: center; font-size: 12px; border-right: 0.5px solid #000; }
<!-- Partidas -->
.products-table { border-collapse: collapse; margin-bottom: 15px; border: 1px solid #000; }
/** Fechas **/
.dates-info { width: 25%; border: 1px solid #000; }
.dates-info table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.d-field { background-color: #d0d0d0; font-weight: bold; text-align: center; font-size: 12px; }
.date { font-weight: bold; text-align: center; font-size: 12px; padding: 7.5px; }
/** Partidas **/
.products-table { border-collapse: collapse; border: 0.5px solid #000; }
.products-table td { border: 0.5px solid #000; padding: 10px; text-align: center; font-size: 10px; }
.products-table th { border: 0.5px solid #000; padding: 5px; background-color: #d0d0d0; font-weight: bold; text-align: center; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.font-bold { font-weight: bold; }
<!-- Total -->
/** Total **/
.totals-section { float: right; width: 250px; }
.total-row { display: flex; justify-content: space-between; margin-top: 25px; font-size: 12px; }
<!-- Nota inferior -->
/** Nota inferior **/
.footer-info { }
.footer-note { font-size: 10px; background-color: #d0d0d0; padding: 5px; border: 0.5px solid #000; }
</style>
@@ -1242,44 +1372,67 @@ function generarHTMLPDF($solicitud, $partidas, $nombre_sistema, $siglas, $logo_u
<table class="header" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td class="logo-section">
<img src="' . $logo_url . '" alt="Logo" class="logo"><br>
<img src="' . htmlspecialchars($configuracion['logo_url'] ?? 'assets/img/logo_siih.png') . '" alt="Logo" class="logo"><br>
<div class="siglas"><strong>' . htmlspecialchars($configuracion['siglas'] ?? 'SIIH') . '</strong></div>
</td>
<td class="company-info">
<div class="company-name">' . htmlspecialchars($nombre_sistema) . '</div>
<div class="company-name">' . htmlspecialchars($solicitud['importador_nombre']) . '</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>
</td>
<td class="invoice-info">
<div><strong>' . htmlspecialchars($configuracion['nombre_plataforma'] ?? 'Sistema Integral para Importadores de Hidrocarburos') . '</strong></div><br>
<div class="invoice-title">Solicitud de Importación</div>
<div><strong>No. ' . htmlspecialchars($solicitud['id_solicitud']) . '</strong></div>
</td>
</tr>
</table>
<br><br><br><br>
<!-- INFORMACIÓN DEL CLIENTE Y FECHAS -->
<table class="info-section" cellspacing="0" cellpadding="0" width="100%">
<!-- SECCIÓN DE INFORMACIÓN DEL PROVEEDOR Y FECHAS -->
<table class="info-section" cellspacing="0" cellpadding="0">
<tr>
<td class="client-info">
<div class="field"><strong>RAZÓN SOCIAL:</strong> ' . htmlspecialchars($solicitud['importador_nombre'] ?? 'No disponible') . '</div>
<div class="field"><strong>DOMICILIO FISCAL:</strong> ' . htmlspecialchars($direccion_completa) . '</div>
<div class="field"><strong>RFC:</strong> ' . htmlspecialchars($solicitud['importador_rfc'] ?? 'No disponible') . '</div>
<div class="field"><strong>TELÉFONO:</strong> ' . htmlspecialchars($solicitud['telefono'] ?? 'No disponible') . '</div>
<div class="field"><strong>RÉGIMEN FISCAL:</strong> 601-General de Ley Personas Morales</div>
<!-- PROVEEDOR -->
<td>
<table class="proveedor-info" cellspacing="0" cellpadding="0">
<tr>
<td class="p-field">RAZÓN SOCIAL:</td>
<td class="field">' . htmlspecialchars($proveedor_nombre) . '</td>
</tr>
</table>
<table class="proveedor-info" cellspacing="0" cellpadding="0">
<tr>
<td class="p-field" style="height: 45px;">DIRECCIÓN:</td>
<td class="field">' . htmlspecialchars($proveedor_direccion) . '</td>
</tr>
</table>
<table class="proveedor-info" cellspacing="0" cellpadding="0">
<tr>
<td class="p-field">RFC:</td>
<td class="field" style="border-bottom: none;">' . htmlspecialchars($proveedor_rfc) . '</td>
</tr>
</table>
</td>
<!-- FECHAS -->
<td class="dates-info">
<table width="100%" cellspacing="0" cellpadding="2">
<tr><td class="section-title">FECHA DE EXPEDICIÓN</td></tr>
<tr><td class="text-center font-bold" style="font-size: 12px; border-bottom: 0.5px solid #000; padding-bottom: 15px; padding-top: 12.5px;">' . $fecha_expedicion . '</td></tr>
<tr><td class="section-title" style="padding-top: 10px;">FECHA DE VENCIMIENTO</td></tr>
<tr><td class="text-center font-bold" style="font-size: 12px; padding-bottom: 15px; padding-top: 12.5px;">' . $fecha_vencimiento . '</td></tr>
<table cellspacing="0" cellpadding="2">
<tr><td class="d-field" style="border-left: 0.5px solid black;">FECHA DE EXPEDICIÓN</td></tr>
<tr><td class="date" style="border-bottom: 0.5px solid black;">' . $fecha_expedicion . '</td></tr>
<tr><td class="d-field" style="border-left: 0.5px solid black;">FECHA DE VENCIMIENTO</td></tr>
<tr><td class="date">' . $fecha_vencimiento . '</td></tr>
</table>
</td>
</tr>
</table>
<br><br><br>
<table class="clave-section" cellspacing="0" cellpadding="0">
<tr>
<td class="p-field">CLAVE:</td>
<td class="field" style="border-bottom: none;">' . htmlspecialchars($solicitud['proveedor_clave'] ?? 'No disponible') . '</td>
<td class="p-field">TELÉFONO:</td>
<td class="field" style="border-bottom: none;">' . htmlspecialchars($proveedor_telefono) . '</td>
</tr>
</table>
<!-- TABLA DE PRODUCTOS/PARTIDAS -->
<table class="products-table" cellspacing="0" cellpadding="0" width="100%">
@@ -1300,12 +1453,13 @@ function generarHTMLPDF($solicitud, $partidas, $nombre_sistema, $siglas, $logo_u
$cantidad = (float)($partida['cantidad_comercial'] ?? 0);
$valor_partida = (float)($partida['valor_factura'] ?? 0);
$html .= '<tr>
$html .= '
<tr>
<td>' . htmlspecialchars($partida['descripcion']) . '</td>
<td>' . htmlspecialchars($partida['unidad_descripcion'] ?? 'Unidad de servicio (E48)') . '</td>
<td>$' . number_format($precio_unitario > 0 ? $precio_unitario : $valor_partida, 2) . '</td>
<td>' . $moneda_codigo . ' ' . number_format($precio_unitario > 0 ? $precio_unitario : $valor_partida, 2) . '</td>
<td>' . number_format($cantidad > 0 ? $cantidad : 1, 0) . '</td>
<td>$' . number_format($valor_partida, 2) . '</td>
<td>' . $moneda_codigo . ' ' . number_format($valor_partida, 2) . '</td>
</tr>';
}
@@ -1322,7 +1476,7 @@ function generarHTMLPDF($solicitud, $partidas, $nombre_sistema, $siglas, $logo_u
<div class="totals-section">
<div class="total-row text-right">
<span><strong>Total:</strong></span>
<span><strong>$' . number_format($total, 2) . '</strong></span>
<span><strong>' . $moneda_codigo . ' ' . number_format($total, 2) . '</strong></span>
</div>
</div>