Preparando proyecto para migración a repositorio MVE
This commit is contained in:
@@ -359,44 +359,16 @@ function ajax_lista()
|
||||
$id_usuario = $_SESSION['usuario_id'];
|
||||
$conn = getConnection();
|
||||
|
||||
// Obtener RFC del importador para filtrar solo sus pedimentos
|
||||
$sqlImportador = "SELECT rfc FROM informacion_general WHERE id_usuario = ?";
|
||||
$stmtImportador = sqlsrv_query($conn, $sqlImportador, [$id_usuario]);
|
||||
|
||||
if ($stmtImportador === false) {
|
||||
echo json_encode([
|
||||
"draw" => intval($_GET['draw'] ?? 0),
|
||||
"recordsTotal" => 0,
|
||||
"recordsFiltered" => 0,
|
||||
"data" => [],
|
||||
"error" => "Error al consultar información del importador"
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$importador = sqlsrv_fetch_array($stmtImportador, SQLSRV_FETCH_ASSOC);
|
||||
|
||||
if (!$importador) {
|
||||
echo json_encode([
|
||||
"draw" => intval($_GET['draw'] ?? 0),
|
||||
"recordsTotal" => 0,
|
||||
"recordsFiltered" => 0,
|
||||
"data" => []
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Parámetros de DataTables
|
||||
$draw = intval($_GET['draw'] ?? 0);
|
||||
$start = intval($_GET['start'] ?? 0);
|
||||
$length = intval($_GET['length'] ?? 10);
|
||||
$search = $_GET['search']['value'] ?? '';
|
||||
|
||||
// Total registros sin filtro
|
||||
$sqlTotal = "SELECT COUNT(*) AS total FROM PREVIOS_COMPARTIDOS_WS WHERE ClienteRFC = ?";
|
||||
$stmt = sqlsrv_query($conn, $sqlTotal, [$importador['rfc']]);
|
||||
|
||||
if ($stmt === false) {
|
||||
// Total registros sin filtro en nuevas tablas
|
||||
$sqlTotal = "SELECT COUNT(*) AS total FROM pedimentos WHERE usuario_id = ?";
|
||||
$stmtTotal = sqlsrv_query($conn, $sqlTotal, [$id_usuario]);
|
||||
if ($stmtTotal === false) {
|
||||
echo json_encode([
|
||||
"draw" => $draw,
|
||||
"recordsTotal" => 0,
|
||||
@@ -406,24 +378,21 @@ function ajax_lista()
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
|
||||
$recordsTotal = (int)($row['total'] ?? 0);
|
||||
|
||||
// Construir condiciones de filtro
|
||||
$where = "ClienteRFC = ?";
|
||||
$params = [$importador['rfc']];
|
||||
$rowT = sqlsrv_fetch_array($stmtTotal, SQLSRV_FETCH_ASSOC);
|
||||
$recordsTotal = (int)($rowT['total'] ?? 0);
|
||||
|
||||
// Filtro y búsqueda
|
||||
$where = "p.usuario_id = ?";
|
||||
$params = [$id_usuario];
|
||||
if ($search !== '') {
|
||||
$where .= " AND (Pedimento LIKE ? OR ClienteNombre LIKE ? OR ClavePed LIKE ?)";
|
||||
$where .= " AND (p.numero_pedimento LIKE ? OR p.rfc_importador LIKE ? OR p.clave_documento LIKE ? OR p.patente LIKE ? OR p.aduana LIKE ?)";
|
||||
$like = "%{$search}%";
|
||||
$params = array_merge($params, [$like, $like, $like]);
|
||||
$params = array_merge($params, [$like, $like, $like, $like, $like]);
|
||||
}
|
||||
|
||||
// Total registros filtrados
|
||||
$sqlFiltered = "SELECT COUNT(*) AS total FROM PREVIOS_COMPARTIDOS_WS WHERE $where";
|
||||
// Total filtrado
|
||||
$sqlFiltered = "SELECT COUNT(*) AS total FROM pedimentos p WHERE $where";
|
||||
$stmtF = sqlsrv_query($conn, $sqlFiltered, $params);
|
||||
|
||||
if ($stmtF === false) {
|
||||
echo json_encode([
|
||||
"draw" => $draw,
|
||||
@@ -434,34 +403,41 @@ function ajax_lista()
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$rowF = sqlsrv_fetch_array($stmtF, SQLSRV_FETCH_ASSOC);
|
||||
$recordsFiltered = (int)($rowF['total'] ?? 0);
|
||||
|
||||
// Datos de la página
|
||||
$sqlData = "SELECT IdPrevio, Pedimento, ClienteRFC, ClienteNombre, Timestamp, Status
|
||||
FROM PREVIOS_COMPARTIDOS_WS
|
||||
WHERE $where
|
||||
ORDER BY Timestamp DESC
|
||||
// Datos paginados
|
||||
$sqlData = "SELECT p.id, p.numero_pedimento, p.rfc_importador, p.fecha_creacion, p.estado,
|
||||
ig.nombre AS nombre_importador
|
||||
FROM pedimentos p
|
||||
LEFT JOIN informacion_general ig ON ig.id_usuario = p.usuario_id
|
||||
WHERE $where
|
||||
ORDER BY p.fecha_creacion DESC
|
||||
OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
|
||||
$params[] = $start;
|
||||
$params[] = $length;
|
||||
|
||||
$stmtD = sqlsrv_query($conn, $sqlData, $params);
|
||||
$paramsData = array_merge($params, [$start, $length]);
|
||||
$stmtD = sqlsrv_query($conn, $sqlData, $paramsData);
|
||||
|
||||
$data = [];
|
||||
if ($stmtD !== false) {
|
||||
while ($r = sqlsrv_fetch_array($stmtD, SQLSRV_FETCH_ASSOC)) {
|
||||
$timestamp = $r['Timestamp'] instanceof DateTime ? $r['Timestamp']->format('Y-m-d H:i:s') : '';
|
||||
$status_text = $r['Status'] == 1 ? 'Activo' : 'Inactivo';
|
||||
|
||||
$fecha = '';
|
||||
if (isset($r['fecha_creacion'])) {
|
||||
if ($r['fecha_creacion'] instanceof DateTime) {
|
||||
$fecha = $r['fecha_creacion']->format('Y-m-d H:i:s');
|
||||
} elseif (is_array($r['fecha_creacion']) && isset($r['fecha_creacion']['date'])) {
|
||||
// Por si viene como array (SQLSRV con print_r)
|
||||
$fecha = substr($r['fecha_creacion']['date'], 0, 19);
|
||||
}
|
||||
}
|
||||
$estado = strtolower((string)$r['estado']) === 'activo' || $r['estado'] === 1 ? 'Activo' : 'Inactivo';
|
||||
|
||||
$data[] = [
|
||||
$r['IdPrevio'],
|
||||
$r['Pedimento'],
|
||||
$r['ClienteRFC'],
|
||||
$r['ClienteNombre'],
|
||||
$timestamp,
|
||||
$status_text
|
||||
$r['id'],
|
||||
$r['numero_pedimento'],
|
||||
$r['rfc_importador'],
|
||||
$r['nombre_importador'] ?? '',
|
||||
$fecha,
|
||||
$estado
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -555,4 +531,157 @@ function buscar_pedimentos()
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
echo json_encode($pedimentos, JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Devuelve información general del pedimento (cabecera)
|
||||
* GET /IMPORTADORES/catalogo_pedimentos/ajax_pedimento?id=123
|
||||
*/
|
||||
function ajax_pedimento()
|
||||
{
|
||||
if (!($_SESSION['usuario_id'] ?? false)) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['success' => false, 'message' => 'No autorizado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$id_usuario = $_SESSION['usuario_id'];
|
||||
$pedimento_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||||
if ($pedimento_id <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'ID inválido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
$sql = "SELECT p.id, p.numero_pedimento, p.patente, p.aduana, p.anio, p.clave_documento,
|
||||
p.rfc_importador, p.fecha_creacion, p.estado
|
||||
FROM pedimentos p
|
||||
WHERE p.id = ? AND p.usuario_id = ?";
|
||||
$stmt = sqlsrv_query($conn, $sql, [$pedimento_id, $id_usuario]);
|
||||
if ($stmt === false) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Error de base de datos']);
|
||||
exit;
|
||||
}
|
||||
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
if (!$row) {
|
||||
echo json_encode(['success' => false, 'message' => 'No encontrado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Formatear fecha si es DateTime
|
||||
if (isset($row['fecha_creacion']) && $row['fecha_creacion'] instanceof DateTime) {
|
||||
$row['fecha_creacion'] = $row['fecha_creacion']->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'data' => $row]);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Devuelve las facturas del pedimento
|
||||
* GET /IMPORTADORES/catalogo_pedimentos/ajax_facturas?pedimento_id=123
|
||||
*/
|
||||
function ajax_facturas()
|
||||
{
|
||||
if (!($_SESSION['usuario_id'] ?? false)) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['success' => false, 'message' => 'No autorizado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$id_usuario = $_SESSION['usuario_id'];
|
||||
$pedimento_id = isset($_GET['pedimento_id']) ? (int)$_GET['pedimento_id'] : 0;
|
||||
if ($pedimento_id <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'ID inválido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
// Verificar propiedad
|
||||
$chk = sqlsrv_query($conn, "SELECT 1 FROM pedimentos WHERE id = ? AND usuario_id = ?", [$pedimento_id, $id_usuario]);
|
||||
$own = $chk && sqlsrv_fetch_array($chk) ? true : false;
|
||||
if ($chk) sqlsrv_free_stmt($chk);
|
||||
if (!$own) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['success' => false, 'message' => 'Pedimento no encontrado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = "SELECT id, numero_factura, fecha_factura, valor_dolares, valor_factura, cove, moneda, proveedor
|
||||
FROM pedimento_facturas
|
||||
WHERE pedimento_id = ?
|
||||
ORDER BY fecha_factura ASC, id ASC";
|
||||
$stmt = sqlsrv_query($conn, $sql, [$pedimento_id]);
|
||||
if ($stmt === false) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Error de base de datos']);
|
||||
exit;
|
||||
}
|
||||
$items = [];
|
||||
while ($r = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
||||
if ($r['fecha_factura'] instanceof DateTime) {
|
||||
$r['fecha_factura'] = $r['fecha_factura']->format('Y-m-d');
|
||||
}
|
||||
$items[] = $r;
|
||||
}
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
echo json_encode(['success' => true, 'data' => $items]);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Devuelve las partidas del pedimento
|
||||
* GET /IMPORTADORES/catalogo_pedimentos/ajax_partidas?pedimento_id=123
|
||||
*/
|
||||
function ajax_partidas()
|
||||
{
|
||||
if (!($_SESSION['usuario_id'] ?? false)) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['success' => false, 'message' => 'No autorizado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$id_usuario = $_SESSION['usuario_id'];
|
||||
$pedimento_id = isset($_GET['pedimento_id']) ? (int)$_GET['pedimento_id'] : 0;
|
||||
if ($pedimento_id <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'ID inválido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
// Verificar propiedad
|
||||
$chk = sqlsrv_query($conn, "SELECT 1 FROM pedimentos WHERE id = ? AND usuario_id = ?", [$pedimento_id, $id_usuario]);
|
||||
$own = $chk && sqlsrv_fetch_array($chk) ? true : false;
|
||||
if ($chk) sqlsrv_free_stmt($chk);
|
||||
if (!$own) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['success' => false, 'message' => 'Pedimento no encontrado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = "SELECT id, secuencia, fraccion_arancelaria, descripcion, cantidad, unidad, valor_unitario, peso_neto, peso_bruto
|
||||
FROM pedimento_partidas
|
||||
WHERE pedimento_id = ?
|
||||
ORDER BY secuencia ASC, id ASC";
|
||||
$stmt = sqlsrv_query($conn, $sql, [$pedimento_id]);
|
||||
if ($stmt === false) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Error de base de datos']);
|
||||
exit;
|
||||
}
|
||||
$items = [];
|
||||
while ($r = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
||||
$items[] = $r;
|
||||
}
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
echo json_encode(['success' => true, 'data' => $items]);
|
||||
exit;
|
||||
}
|
||||
Reference in New Issue
Block a user