Patentes en CRUD - Solicitudes de Importación
This commit is contained in:
@@ -85,7 +85,8 @@ function lista()
|
||||
tr.nombre AS transportista,
|
||||
(c.nombre + ' ' + c.apellido) AS chofer,
|
||||
p.nombre AS nombre_pais_proveedor,
|
||||
f.foto_solicitud_url
|
||||
f.foto_solicitud_url,
|
||||
a.patente AS patente
|
||||
FROM dbo.solicitud_importacion_factura f
|
||||
JOIN dbo.transportistas tr
|
||||
ON f.transportista_id = tr.id_transportista
|
||||
@@ -93,6 +94,8 @@ function lista()
|
||||
ON f.chofer_id = c.id_chofer
|
||||
LEFT JOIN dbo.paises p
|
||||
ON f.pais_proveedor = p.id_pais
|
||||
LEFT JOIN dbo.agentes_aduanales a
|
||||
ON f.patente_id = a.id_agente
|
||||
WHERE f.id_importador = ?
|
||||
AND f.id_agencia = ?
|
||||
AND f.status >= 1
|
||||
@@ -121,19 +124,23 @@ function crear()
|
||||
header('Location: /IMPORTADORES/login');
|
||||
exit;
|
||||
}
|
||||
|
||||
$id_importador = $_SESSION['usuario_id'];
|
||||
$conn = getConnection();
|
||||
|
||||
// ✅ NUEVO: Obtener la agencia actual del usuario
|
||||
$stmt = sqlsrv_query($conn, "SELECT id_agencia_en_uso FROM dbo.usuarios_sistema WHERE id_usuario = ?", [$id_importador]);
|
||||
if ($stmt === false) { die(print_r(sqlsrv_errors(), true)); }
|
||||
$id_agencia = null;
|
||||
if ($stmt && $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
||||
$id_agencia = $row['id_agencia_en_uso'];
|
||||
}
|
||||
if ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { $id_agencia = $row['id_agencia_en_uso']; }
|
||||
|
||||
// Carga de datos para selects
|
||||
$patentes = [];
|
||||
$stmtP = sqlsrv_query($conn, "SELECT id_agente, patente, agente_aduanal FROM dbo.agentes_aduanales WHERE id_agencia = ? AND activo = 1 ORDER BY patente", [$id_agencia]);
|
||||
while ($r = sqlsrv_fetch_array($stmtP, SQLSRV_FETCH_ASSOC)) { $patentes[] = $r; }
|
||||
|
||||
$transportistas = [];
|
||||
$stmtT = sqlsrv_query($conn, "SELECT id_transportista, clave_identificador, nombre FROM dbo.transportistas WHERE id_usuario=? AND activo=1 ORDER BY nombre",[$id_importador]);
|
||||
$stmtT = sqlsrv_query($conn, "SELECT id_transportista, clave_identificador, nombre FROM dbo.transportistas WHERE id_usuario = ? AND activo=1 ORDER BY nombre",[$id_importador]);
|
||||
while ($r = sqlsrv_fetch_array($stmtT, SQLSRV_FETCH_ASSOC)) { $transportistas[] = $r; }
|
||||
|
||||
$choferes = [];
|
||||
@@ -173,7 +180,7 @@ function guardar()
|
||||
|
||||
// ✅ Obtener id_agencia_en_uso del usuario
|
||||
$stmtAgencia = sqlsrv_query($conn, "SELECT id_agencia_en_uso FROM dbo.usuarios_sistema WHERE id_usuario = ?", [$id_importador]);
|
||||
$id_agencia = null;
|
||||
$id_agencia = null;
|
||||
if ($stmtAgencia && $row = sqlsrv_fetch_array($stmtAgencia, SQLSRV_FETCH_ASSOC)) {
|
||||
$id_agencia = $row['id_agencia_en_uso'];
|
||||
}
|
||||
@@ -183,18 +190,30 @@ function guardar()
|
||||
}
|
||||
|
||||
// Obtener y validar campos del formulario
|
||||
$aduana_seccion = $_POST['anexo22_apendice'] ?? null;
|
||||
$aduana_seccion = $_POST['anexo22_apendice'] ?? null;
|
||||
$num_factura = trim($_POST['numero_factura'] ?? '');
|
||||
$fecha = $_POST['fecha_factura'] ?? null;
|
||||
$incoterm = $_POST['incoterm'] ?? null;
|
||||
$pais_proveedor = $_POST['pais_proveedor'] ?? null;
|
||||
$tipo_moneda = $_POST['tipo_moneda'] ?? null;
|
||||
$valor_factura = $_POST['valor_factura'] ?? null;
|
||||
$vinculacion = $_POST['vinculacion'] ?? 0;
|
||||
$transportista_id = $_POST['transportista_id'] ?? null;
|
||||
$chofer_id = $_POST['chofer_id'] ?? null;
|
||||
$status = isset($_POST['status']) ? 1 : 0;
|
||||
$proveedor_clave = trim($_POST['proveedor_id'] ?? '');
|
||||
$fecha = $_POST['fecha_factura'] ?? null;
|
||||
$incoterm = $_POST['incoterm'] ?? null;
|
||||
$pais_proveedor = $_POST['pais_proveedor'] ?? null;
|
||||
$tipo_moneda = $_POST['tipo_moneda'] ?? null;
|
||||
$valor_factura = $_POST['valor_factura'] ?? null;
|
||||
$vinculacion = $_POST['vinculacion'] ?? 0;
|
||||
$transportista_id = $_POST['transportista_id'] ?? null;
|
||||
$chofer_id = $_POST['chofer_id'] ?? null;
|
||||
$status = isset($_POST['status']) ? 1 : 0;
|
||||
$proveedor_clave = trim($_POST['proveedor_id'] ?? '');
|
||||
$patente_id = $_POST['patente'] ?? null;
|
||||
|
||||
if ($patente_id) {
|
||||
$stmtValidatePatente = sqlsrv_query($conn,
|
||||
"SELECT id_agente FROM dbo.agentes_aduanales WHERE id_agente = ? AND id_agencia = ? AND activo = 1",
|
||||
[$patente_id, $id_agencia]);
|
||||
|
||||
if (!$stmtValidatePatente || !sqlsrv_fetch_array($stmtValidatePatente, SQLSRV_FETCH_ASSOC)) {
|
||||
die("❌ La patente seleccionada no es válida para su agencia.");
|
||||
}
|
||||
sqlsrv_free_stmt($stmtValidatePatente);
|
||||
}
|
||||
|
||||
if (empty($num_factura) || empty($fecha) || empty($transportista_id) || empty($chofer_id)) {
|
||||
die("❌ Faltan campos obligatorios.");
|
||||
@@ -227,16 +246,17 @@ function guardar()
|
||||
(int)$chofer_id,
|
||||
$fotoUrl,
|
||||
$status,
|
||||
$proveedor_clave
|
||||
$proveedor_clave,
|
||||
$patente_id ? (int)$patente_id : null
|
||||
];
|
||||
|
||||
$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,
|
||||
valor_factura, vinculacion, transportista_id, chofer_id,
|
||||
foto_solicitud_url, status, proveedor_clave)
|
||||
foto_solicitud_url, status, proveedor_clave, patente_id)
|
||||
OUTPUT INSERTED.id_solicitud
|
||||
VALUES(?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
VALUES(?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
$stmt = sqlsrv_query($conn, $sql, $params, ['Scrollable' => SQLSRV_CURSOR_KEYSET]);
|
||||
if ($stmt === false) {
|
||||
@@ -480,12 +500,11 @@ function editar()
|
||||
$id_importador = $_SESSION['usuario_id'];
|
||||
$conn = getConnection();
|
||||
|
||||
// ✅ Obtener id_agencia_en_uso del usuario
|
||||
$stmtAgencia = sqlsrv_query($conn, "SELECT id_agencia_en_uso FROM dbo.usuarios_sistema WHERE id_usuario = ?", [$id_importador]);
|
||||
// ✅ NUEVO: Obtener la agencia actual del usuario
|
||||
$stmtAg = sqlsrv_query($conn, "SELECT id_agencia_en_uso FROM dbo.usuarios_sistema WHERE id_usuario = ?", [$id_importador]);
|
||||
if ($stmtAg === false) { die(print_r(sqlsrv_errors(), true)); }
|
||||
$id_agencia = null;
|
||||
if ($stmtAgencia && $row = sqlsrv_fetch_array($stmtAgencia, SQLSRV_FETCH_ASSOC)) {
|
||||
$id_agencia = $row['id_agencia_en_uso'];
|
||||
}
|
||||
if ($row = sqlsrv_fetch_array($stmtAg, SQLSRV_FETCH_ASSOC)) { $id_agencia = $row['id_agencia_en_uso']; }
|
||||
|
||||
$stmt = sqlsrv_query($conn,"SELECT * FROM dbo.solicitud_importacion_factura WHERE id_solicitud=? AND id_importador=? AND id_agencia=?", [(int)$id_solicitud, $id_importador, $id_agencia]);
|
||||
if($stmt === false) {
|
||||
@@ -503,6 +522,11 @@ function editar()
|
||||
$proveedor_clave_actual = $factura['proveedor_clave'] ?? '';
|
||||
|
||||
// Carga selects (igual que crear)
|
||||
// Patentes
|
||||
$patentes = [];
|
||||
$stmtP = sqlsrv_query($conn, "SELECT id_agente, patente, agente_aduanal FROM dbo.agentes_aduanales WHERE id_agencia = ? AND activo = 1 ORDER BY patente", [$id_agencia]);
|
||||
while ($r = sqlsrv_fetch_array($stmtP, SQLSRV_FETCH_ASSOC)) { $patentes[] = $r; }
|
||||
|
||||
// Transportistas
|
||||
$transportistas = []; $stmtT = sqlsrv_query($conn,"SELECT id_transportista, nombre FROM dbo.transportistas WHERE id_usuario=? AND activo=1 ORDER BY nombre",[$id_importador]);
|
||||
while($r = sqlsrv_fetch_array($stmtT,SQLSRV_FETCH_ASSOC)) {
|
||||
@@ -554,6 +578,8 @@ function actualizar()
|
||||
die("⚠️ No autorizado.");
|
||||
}
|
||||
|
||||
$id_importador = $_SESSION['usuario_id'];
|
||||
|
||||
$id_solicitud = (int)($_POST['id_solicitud'] ?? 0);
|
||||
if ($id_solicitud <= 0) {
|
||||
die("❌ ID inválido.");
|
||||
@@ -570,7 +596,7 @@ function actualizar()
|
||||
}
|
||||
|
||||
// 2) Obtener URL de foto actual desde BD para conservar si no suben nueva
|
||||
$fotoUrl = null;
|
||||
$fotoUrl = null;
|
||||
$stmtFoto = sqlsrv_query(
|
||||
$conn,
|
||||
"SELECT foto_solicitud_url
|
||||
@@ -607,6 +633,19 @@ function actualizar()
|
||||
$transportista_id = (int)($_POST['transportista_id'] ?? 0);
|
||||
$chofer_id = (int)($_POST['chofer_id'] ?? 0);
|
||||
$status = isset($_POST['status']) ?? 1;
|
||||
$patente_id = $_POST['patente'] ?? null;
|
||||
|
||||
// ✅ NUEVO: Validar que la patente pertenezca a la agencia del usuario (si se seleccionó una)
|
||||
if ($patente_id) {
|
||||
$stmtValidatePatente = sqlsrv_query($conn,
|
||||
"SELECT id_agente FROM dbo.agentes_aduanales WHERE id_agente = ? AND id_agencia = ? AND activo = 1",
|
||||
[$patente_id, $id_agencia]);
|
||||
|
||||
if (!$stmtValidatePatente || !sqlsrv_fetch_array($stmtValidatePatente, SQLSRV_FETCH_ASSOC)) {
|
||||
die("❌ La patente seleccionada no es válida para su agencia.");
|
||||
}
|
||||
sqlsrv_free_stmt($stmtValidatePatente);
|
||||
}
|
||||
|
||||
// 5) Validar obligatorios
|
||||
if (empty($num_factura) || empty($fecha) || $transportista_id <= 0 || $chofer_id <= 0) {
|
||||
@@ -629,9 +668,10 @@ function actualizar()
|
||||
chofer_id = ?,
|
||||
foto_solicitud_url = ?,
|
||||
status = ?,
|
||||
patente_id = ?,
|
||||
updated_at = GETDATE()
|
||||
WHERE id_solicitud = ?
|
||||
AND id_importador = ?
|
||||
AND id_importador = ?
|
||||
AND id_agencia = ?
|
||||
";
|
||||
$paramsU = [
|
||||
@@ -648,6 +688,7 @@ function actualizar()
|
||||
$chofer_id,
|
||||
$fotoUrl,
|
||||
$status,
|
||||
$patente_id ? (int)$patente_id : null,
|
||||
$id_solicitud,
|
||||
$_SESSION['usuario_id'],
|
||||
$id_agencia
|
||||
|
||||
Reference in New Issue
Block a user