grales
This commit is contained in:
190
app/controllers/mve.php
Normal file
190
app/controllers/mve.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../config/database.php';
|
||||
require_once __DIR__ . '/../helpers/session.php';
|
||||
|
||||
function index() {
|
||||
include __DIR__ . '/../../views/mve/lista.php';
|
||||
}
|
||||
|
||||
function ajax_guardar_datos_factura() {
|
||||
try {
|
||||
// Validar que el usuario esté autenticado
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
echo json_encode(['success' => false, 'message' => 'Usuario no autenticado']);
|
||||
return;
|
||||
}
|
||||
|
||||
$id_factura = $_POST['id_factura'] ?? null;
|
||||
$id_pedimento = $_POST['id_pedimento'] ?? null;
|
||||
$datos_art65 = json_decode($_POST['datos_art65'] ?? '{}', true);
|
||||
$datos_art66 = json_decode($_POST['datos_art66'] ?? '{}', true);
|
||||
|
||||
if (!$id_factura || !$id_pedimento) {
|
||||
echo json_encode(['success' => false, 'message' => 'Faltan datos requeridos']);
|
||||
return;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
|
||||
// Verificar si ya existen datos para esta factura
|
||||
$stmt = $db->prepare("SELECT id FROM mve_facturas_datos WHERE id_pedimento = ? AND id_factura = ?");
|
||||
$stmt->execute([$id_pedimento, $id_factura]);
|
||||
$existe = $stmt->fetch();
|
||||
|
||||
if ($existe) {
|
||||
// Actualizar registro existente
|
||||
$sql = "UPDATE mve_facturas_datos SET
|
||||
art65_fecha_transporte = ?, art65_importe_transporte = ?,
|
||||
art65_fecha_descuentos = ?, art65_importe_descuentos = ?,
|
||||
art65_fecha_posteriores = ?, art65_importe_posteriores = ?,
|
||||
art65_fecha_contribuciones = ?, art65_importe_contribuciones = ?,
|
||||
art65_fecha_pagos_vendedor = ?, art65_importe_pagos_vendedor = ?,
|
||||
|
||||
art66_fecha_comisiones = ?, art66_importe_comisiones = ?, art66_cargo_comisiones = ?,
|
||||
art66_fecha_envases = ?, art66_importe_envases = ?, art66_cargo_envases = ?,
|
||||
art66_fecha_embalaje = ?, art66_importe_embalaje = ?, art66_cargo_embalaje = ?,
|
||||
art66_fecha_transporte_dec = ?, art66_importe_transporte_dec = ?, art66_cargo_transporte_dec = ?,
|
||||
art66_fecha_ingenieria = ?, art66_importe_ingenieria = ?, art66_cargo_ingenieria = ?,
|
||||
art66_fecha_regalias = ?, art66_importe_regalias = ?, art66_cargo_regalias = ?,
|
||||
art66_fecha_producto = ?, art66_importe_producto = ?, art66_cargo_producto = ?,
|
||||
|
||||
fecha_actualizacion = NOW()
|
||||
WHERE id_pedimento = ? AND id_factura = ?";
|
||||
|
||||
$params = [
|
||||
$datos_art65['fecha_transporte'] ?: null, $datos_art65['importe_transporte'] ?: null,
|
||||
$datos_art65['fecha_descuentos'] ?: null, $datos_art65['importe_descuentos'] ?: null,
|
||||
$datos_art65['fecha_posteriores'] ?: null, $datos_art65['importe_posteriores'] ?: null,
|
||||
$datos_art65['fecha_contribuciones'] ?: null, $datos_art65['importe_contribuciones'] ?: null,
|
||||
$datos_art65['fecha_pagos_vendedor'] ?: null, $datos_art65['importe_pagos_vendedor'] ?: null,
|
||||
|
||||
$datos_art66['fecha_comisiones'] ?: null, $datos_art66['importe_comisiones'] ?: null, $datos_art66['cargo_comisiones'] ?: null,
|
||||
$datos_art66['fecha_envases'] ?: null, $datos_art66['importe_envases'] ?: null, $datos_art66['cargo_envases'] ?: null,
|
||||
$datos_art66['fecha_embalaje'] ?: null, $datos_art66['importe_embalaje'] ?: null, $datos_art66['cargo_embalaje'] ?: null,
|
||||
$datos_art66['fecha_transporte_dec'] ?: null, $datos_art66['importe_transporte_dec'] ?: null, $datos_art66['cargo_transporte_dec'] ?: null,
|
||||
$datos_art66['fecha_ingenieria'] ?: null, $datos_art66['importe_ingenieria'] ?: null, $datos_art66['cargo_ingenieria'] ?: null,
|
||||
$datos_art66['fecha_regalias'] ?: null, $datos_art66['importe_regalias'] ?: null, $datos_art66['cargo_regalias'] ?: null,
|
||||
$datos_art66['fecha_producto'] ?: null, $datos_art66['importe_producto'] ?: null, $datos_art66['cargo_producto'] ?: null,
|
||||
|
||||
$id_pedimento, $id_factura
|
||||
];
|
||||
} else {
|
||||
// Crear nuevo registro
|
||||
$sql = "INSERT INTO mve_facturas_datos (
|
||||
id_pedimento, id_factura, numero_factura,
|
||||
art65_fecha_transporte, art65_importe_transporte,
|
||||
art65_fecha_descuentos, art65_importe_descuentos,
|
||||
art65_fecha_posteriores, art65_importe_posteriores,
|
||||
art65_fecha_contribuciones, art65_importe_contribuciones,
|
||||
art65_fecha_pagos_vendedor, art65_importe_pagos_vendedor,
|
||||
|
||||
art66_fecha_comisiones, art66_importe_comisiones, art66_cargo_comisiones,
|
||||
art66_fecha_envases, art66_importe_envases, art66_cargo_envases,
|
||||
art66_fecha_embalaje, art66_importe_embalaje, art66_cargo_embalaje,
|
||||
art66_fecha_transporte_dec, art66_importe_transporte_dec, art66_cargo_transporte_dec,
|
||||
art66_fecha_ingenieria, art66_importe_ingenieria, art66_cargo_ingenieria,
|
||||
art66_fecha_regalias, art66_importe_regalias, art66_cargo_regalias,
|
||||
art66_fecha_producto, art66_importe_producto, art66_cargo_producto,
|
||||
|
||||
usuario_creacion
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
$params = [
|
||||
$id_pedimento, $id_factura, "FACTURA-$id_factura",
|
||||
$datos_art65['fecha_transporte'] ?: null, $datos_art65['importe_transporte'] ?: null,
|
||||
$datos_art65['fecha_descuentos'] ?: null, $datos_art65['importe_descuentos'] ?: null,
|
||||
$datos_art65['fecha_posteriores'] ?: null, $datos_art65['importe_posteriores'] ?: null,
|
||||
$datos_art65['fecha_contribuciones'] ?: null, $datos_art65['importe_contribuciones'] ?: null,
|
||||
$datos_art65['fecha_pagos_vendedor'] ?: null, $datos_art65['importe_pagos_vendedor'] ?: null,
|
||||
|
||||
$datos_art66['fecha_comisiones'] ?: null, $datos_art66['importe_comisiones'] ?: null, $datos_art66['cargo_comisiones'] ?: null,
|
||||
$datos_art66['fecha_envases'] ?: null, $datos_art66['importe_envases'] ?: null, $datos_art66['cargo_envases'] ?: null,
|
||||
$datos_art66['fecha_embalaje'] ?: null, $datos_art66['importe_embalaje'] ?: null, $datos_art66['cargo_embalaje'] ?: null,
|
||||
$datos_art66['fecha_transporte_dec'] ?: null, $datos_art66['importe_transporte_dec'] ?: null, $datos_art66['cargo_transporte_dec'] ?: null,
|
||||
$datos_art66['fecha_ingenieria'] ?: null, $datos_art66['importe_ingenieria'] ?: null, $datos_art66['cargo_ingenieria'] ?: null,
|
||||
$datos_art66['fecha_regalias'] ?: null, $datos_art66['importe_regalias'] ?: null, $datos_art66['cargo_regalias'] ?: null,
|
||||
$datos_art66['fecha_producto'] ?: null, $datos_art66['importe_producto'] ?: null, $datos_art66['cargo_producto'] ?: null,
|
||||
|
||||
$_SESSION['user_id']
|
||||
];
|
||||
}
|
||||
|
||||
$stmt = $db->prepare($sql);
|
||||
$resultado = $stmt->execute($params);
|
||||
|
||||
if ($resultado) {
|
||||
echo json_encode(['success' => true, 'message' => 'Datos guardados correctamente']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Error al guardar los datos']);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['success' => false, 'message' => 'Error interno: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
function ajax_obtener_datos_factura() {
|
||||
try {
|
||||
$id_factura = $_GET['id_factura'] ?? null;
|
||||
|
||||
if (!$id_factura) {
|
||||
echo json_encode(['success' => false, 'message' => 'ID de factura requerido']);
|
||||
return;
|
||||
}
|
||||
|
||||
$db = getDB();
|
||||
$stmt = $db->prepare("SELECT * FROM mve_facturas_datos WHERE id_factura = ?");
|
||||
$stmt->execute([$id_factura]);
|
||||
$datos = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($datos) {
|
||||
// Estructurar datos para el frontend
|
||||
$datosEstructurados = [
|
||||
'art65' => [
|
||||
'fecha_transporte' => $datos['art65_fecha_transporte'],
|
||||
'importe_transporte' => $datos['art65_importe_transporte'],
|
||||
'fecha_descuentos' => $datos['art65_fecha_descuentos'],
|
||||
'importe_descuentos' => $datos['art65_importe_descuentos'],
|
||||
'fecha_posteriores' => $datos['art65_fecha_posteriores'],
|
||||
'importe_posteriores' => $datos['art65_importe_posteriores'],
|
||||
'fecha_contribuciones' => $datos['art65_fecha_contribuciones'],
|
||||
'importe_contribuciones' => $datos['art65_importe_contribuciones'],
|
||||
'fecha_pagos_vendedor' => $datos['art65_fecha_pagos_vendedor'],
|
||||
'importe_pagos_vendedor' => $datos['art65_importe_pagos_vendedor']
|
||||
],
|
||||
'art66' => [
|
||||
'fecha_comisiones' => $datos['art66_fecha_comisiones'],
|
||||
'importe_comisiones' => $datos['art66_importe_comisiones'],
|
||||
'cargo_comisiones' => $datos['art66_cargo_comisiones'],
|
||||
'fecha_envases' => $datos['art66_fecha_envases'],
|
||||
'importe_envases' => $datos['art66_importe_envases'],
|
||||
'cargo_envases' => $datos['art66_cargo_envases'],
|
||||
'fecha_embalaje' => $datos['art66_fecha_embalaje'],
|
||||
'importe_embalaje' => $datos['art66_importe_embalaje'],
|
||||
'cargo_embalaje' => $datos['art66_cargo_embalaje'],
|
||||
'fecha_transporte_dec' => $datos['art66_fecha_transporte_dec'],
|
||||
'importe_transporte_dec' => $datos['art66_importe_transporte_dec'],
|
||||
'cargo_transporte_dec' => $datos['art66_cargo_transporte_dec'],
|
||||
'fecha_ingenieria' => $datos['art66_fecha_ingenieria'],
|
||||
'importe_ingenieria' => $datos['art66_importe_ingenieria'],
|
||||
'cargo_ingenieria' => $datos['art66_cargo_ingenieria'],
|
||||
'fecha_regalias' => $datos['art66_fecha_regalias'],
|
||||
'importe_regalias' => $datos['art66_importe_regalias'],
|
||||
'cargo_regalias' => $datos['art66_cargo_regalias'],
|
||||
'fecha_producto' => $datos['art66_fecha_producto'],
|
||||
'importe_producto' => $datos['art66_importe_producto'],
|
||||
'cargo_producto' => $datos['art66_cargo_producto']
|
||||
]
|
||||
];
|
||||
|
||||
echo json_encode(['success' => true, 'datos' => $datosEstructurados]);
|
||||
} else {
|
||||
echo json_encode(['success' => true, 'datos' => null]);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['success' => false, 'message' => 'Error interno: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user