73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
// Habilitar el reporte de errores para depuración
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$response = ['success' => false, 'message' => 'Error desconocido.'];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$fecha = $_POST['fecha'] ?? null;
|
|
$field = $_POST['field'] ?? null;
|
|
$value = $_POST['value'] ?? null;
|
|
|
|
if (!$fecha || !$field || $value === null) {
|
|
$response['message'] = 'Datos incompletos: fecha, campo o valor no proporcionados.';
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
// Lista blanca de campos editables para seguridad
|
|
$allowed_fields = [
|
|
'bcp_yape', 'banco_nacion', 'interbank', 'bbva', 'otros_ingresos',
|
|
'tu_1', 'tu_2', 'tu_3',
|
|
'fl_1', 'fl_2', 'fl_3'
|
|
];
|
|
|
|
if (!in_array($field, $allowed_fields)) {
|
|
$response['message'] = "El campo '{$field}' no es editable.";
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$db = db();
|
|
|
|
// Verificar si ya existe una fila para esa fecha
|
|
$stmt_check = $db->prepare("SELECT id FROM flujo_de_caja WHERE fecha = ?");
|
|
$stmt_check->execute([$fecha]);
|
|
$existing_id = $stmt_check->fetchColumn();
|
|
|
|
if ($existing_id) {
|
|
// Si existe, actualizar el campo específico
|
|
$sql = "UPDATE flujo_de_caja SET {$field} = ? WHERE id = ?";
|
|
$stmt = $db->prepare($sql);
|
|
$stmt->execute([$value, $existing_id]);
|
|
} else {
|
|
// Si no existe, crear una nueva fila con el valor
|
|
$sql = "INSERT INTO flujo_de_caja (fecha, {$field}) VALUES (?, ?)";
|
|
$stmt = $db->prepare($sql);
|
|
$stmt->execute([$fecha, $value]);
|
|
}
|
|
|
|
if ($stmt->rowCount() > 0 || $existing_id) {
|
|
$response['success'] = true;
|
|
$response['message'] = 'Dato guardado correctamente.';
|
|
} else {
|
|
$response['message'] = 'No se realizaron cambios.';
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
// Capturar errores de la base de datos
|
|
$response['message'] = "Error de base de datos: " . $e->getMessage();
|
|
}
|
|
} else {
|
|
$response['message'] = 'Método de solicitud no válido.';
|
|
}
|
|
|
|
echo json_encode($response);
|
|
?>
|