This commit is contained in:
2025-10-21 11:41:30 -06:00
parent ed2ef4e01b
commit b1b9e4a77c
49 changed files with 14632 additions and 1736 deletions

View File

@@ -410,6 +410,7 @@ function guardar()
$aduana_seccion = $_POST['anexo22_apendice'] ?? null;
$num_factura = trim($_POST['numero_factura'] ?? '');
$fecha = $_POST['fecha_factura'] ?? null;
$pedimento = trim($_POST['pedimento'] ?? ''); // ✅ NUEVO CAMPO
$incoterm = $_POST['incoterm'] ?? null;
$pais_proveedor = $_POST['pais_proveedor'] ?? null;
$tipo_moneda = $_POST['tipo_moneda'] ?? null;
@@ -462,15 +463,16 @@ function guardar()
$fotoUrl,
$status,
$proveedor_clave,
$patente_id ? (int)$patente_id : null
$patente_id ? (int)$patente_id : null,
$pedimento ?: null // ✅ CORREGIDO: Campo pedimento_vinculado al final
];
$sql = "INSERT INTO dbo.solicitud_importacion_factura
(id_importador, id_agencia, aduana, anexo22_apendice, numero_factura,
fecha_factura, numero_pedimento, incoterm, pais_proveedor, tipo_moneda,
fecha_factura, incoterm, pais_proveedor, tipo_moneda,
valor_factura, vinculacion, transportista_id, chofer_id,
foto_solicitud_url, status, proveedor_clave, patente_id)
foto_solicitud_url, status, proveedor_clave, patente_id, pedimento_vinculado)
OUTPUT INSERTED.id_solicitud
VALUES(?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
";
$stmt = sqlsrv_query($conn, $sql, $params, ['Scrollable' => SQLSRV_CURSOR_KEYSET]);
@@ -947,8 +949,8 @@ function actualizar()
} else {
// INSERTAR nueva partida (asegurarse que no tenga id_partida o sea 0)
$sql = "INSERT INTO dbo.solicitud_importacion_partidas
(id_solicitud, descripcion, cantidad_comercial, cantidad_tarifa,
valor_factura, peso_bruto, unidad_comercial_id, tasa_preferencial)
(id_solicitud, descripcion, cantidad_comercial, cantidad_tarifa, valor_factura,
peso_bruto, unidad_comercial_id, tasa_preferencial)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
";
$params = [ $id_solicitud, $desc, $cantCom, $cantTar, $valPart, $peso, $umId, $tasaPref ];
@@ -1968,7 +1970,7 @@ function generarHTMLPDF($solicitud, $partidas, $configuracion, $proveedor_info =
$unidades = ['', 'uno', 'dos', 'tres', 'cuatro', 'cinco', 'seis', 'siete', 'ocho', 'nueve'];
$decenas = ['', '', 'veinte', 'treinta', 'cuarenta', 'cincuenta', 'sesenta', 'setenta', 'ochenta', 'noventa'];
$especiales = ['diez', 'once', 'doce', 'trece', 'catorce', 'quince', 'dieciséis', 'diecisiete', 'dieciocho', 'diecinueve'];
$centenas = ['', 'ciento', 'doscientos', 'trescientos', 'cuatrocientos', 'quinientos', 'seiscientos', 'setecientos', 'ochocientos', 'novecientos'];
$centenas = ['', 'ciento', 'doscientos', 'trescientos', 'cuatrocientos', 'quinientos', 'seiscientos', 'setecientos', 'ochocientos'];
if ($numero == 0) return 'cero';
if ($numero == 100) return 'cien';
@@ -2212,4 +2214,89 @@ function generarHTMLPDF($solicitud, $partidas, $configuracion, $proveedor_info =
';
return $html;
}
// NUEVO ENDPOINT: Pedimentos de catálogo activos del importador para panel de referencia
function ajax_pedimentos_catalogo()
{
header('Content-Type: application/json; charset=utf-8');
if (!($_SESSION['usuario_id'] ?? false)) {
http_response_code(401);
echo json_encode(['data' => []]);
exit;
}
$id_usuario = $_SESSION['usuario_id'];
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 10;
$conn = getConnection();
// Obtener RFC del importador
$sqlImportador = "SELECT rfc FROM informacion_general WHERE id_usuario = ?";
$stmtImportador = sqlsrv_query($conn, $sqlImportador, [$id_usuario]);
if ($stmtImportador === false) {
echo json_encode(['data' => []]);
exit;
}
$importador = sqlsrv_fetch_array($stmtImportador, SQLSRV_FETCH_ASSOC);
if (!$importador) {
echo json_encode(['data' => []]);
exit;
}
// Obtener pedimentos activos del catálogo
$sql = "SELECT TOP {$limit} IdPrevio, Pedimento, ClienteNombre, ClavePed, Timestamp
FROM PREVIOS_COMPARTIDOS_WS
WHERE ClienteRFC = ? AND Status = 1
ORDER BY Timestamp DESC";
$stmt = sqlsrv_query($conn, $sql, [$importador['rfc']]);
$pedimentos = [];
if ($stmt !== false) {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$fecha = '';
if ($row['Timestamp'] instanceof DateTime) {
$fecha = $row['Timestamp']->format('d/m/Y');
}
$pedimentos[] = [
'numero' => $row['Pedimento'],
'cliente' => $row['ClienteNombre'],
'clave' => $row['ClavePed'],
'fecha' => $fecha
];
}
}
echo json_encode(['data' => $pedimentos]);
exit;
}
function ajax_facturas_por_pedimento() {
header('Content-Type: application/json');
$id_previo = isset($_GET['id_pedimento']) ? trim($_GET['id_pedimento']) : '';
if (!$id_previo) {
echo json_encode(['success' => false, 'error' => 'ID de pedimento no válido']);
return;
}
$conn = getConnection();
// 1. Buscar el número de pedimento real en PREVIOS_COMPARTIDOS_WS
$stmt = sqlsrv_query($conn, "SELECT Pedimento FROM PREVIOS_COMPARTIDOS_WS WHERE IdPrevio = ?", [$id_previo]);
if ($stmt === false || !($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))) {
echo json_encode(['success' => false, 'error' => 'No se encontró el pedimento en PREVIOS_COMPARTIDOS_WS']);
return;
}
$numero_pedimento = $row['Pedimento'];
// 2. Buscar las facturas en solicitud_importacion_factura usando el número de pedimento
// ✅ CORREGIDO: Incluir id_solicitud como id_factura
$stmt2 = sqlsrv_query($conn, "SELECT id_solicitud, numero_factura, fecha_factura, valor_factura FROM solicitud_importacion_factura WHERE numero_pedimento = ?", [$numero_pedimento]);
if ($stmt2 === false) {
echo json_encode(['success' => false, 'error' => 'Error en la consulta de facturas']);
return;
}
$facturas = [];
while ($row2 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {
$facturas[] = [
'id_factura' => $row2['id_solicitud'], // ✅ AGREGADO: Usar id_solicitud como id_factura
'numero_factura' => $row2['numero_factura'],
'fecha' => ($row2['fecha_factura'] instanceof DateTime) ? $row2['fecha_factura']->format('Y-m-d') : $row2['fecha_factura'],
'monto' => $row2['valor_factura']
];
}
echo json_encode(['success' => true, 'facturas' => $facturas]);
}