34849-vm/edit_gasto.php
2026-03-09 17:39:45 +00:00

112 lines
5.0 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
require_once 'db/config.php';
// Check if user is logged in and has an admin role
$allowed_roles = ['Administrador', 'admin'];
if (!isset($_SESSION['user_id']) || !isset($_SESSION['user_role']) || !in_array($_SESSION['user_role'], $allowed_roles)) {
header('Location: dashboard.php?error=access_denied');
exit;
}
$pdo = db();
$gasto = null;
$id = $_GET['id'] ?? null;
$month = $_GET['month'] ?? date('m');
$year = $_GET['year'] ?? date('Y');
if (!$id) {
header("Location: inversion_general.php?status=error");
exit;
}
// Handle form submission to update the expense
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_expense'])) {
$fecha = $_POST['fecha'];
$tipo_gasto = $_POST['tipo_gasto'];
$monto = $_POST['monto'];
$descripcion = $_POST['descripcion'];
$update_id = $_POST['id'];
$month_redirect = $_POST['month'];
$year_redirect = $_POST['year'];
if (!empty($fecha) && !empty($tipo_gasto) && !empty($monto) && !empty($update_id)) {
$stmt = $pdo->prepare("UPDATE inversion_general SET fecha = ?, tipo_gasto = ?, monto = ?, descripcion = ? WHERE id = ?");
$stmt->execute([$fecha, $tipo_gasto, $monto, $descripcion, $update_id]);
header("Location: inversion_general.php?month={$month_redirect}&year={$year_redirect}&status=updated");
exit;
} else {
header("Location: edit_gasto.php?id={$update_id}&month={$month_redirect}&year={$year_redirect}&status=error");
exit;
}
}
// Fetch the expense to edit
$stmt = $pdo->prepare("SELECT * FROM inversion_general WHERE id = ?");
$stmt->execute([$id]);
$gasto = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$gasto) {
header("Location: inversion_general.php?status=not_found");
exit;
}
$pageTitle = 'Editar Gasto';
include 'layout_header.php';
?>
<div class="container mt-4">
<h2>Editar Gasto</h2>
<div class="card">
<div class="card-header">
Modificar Información del Gasto
</div>
<div class="card-body">
<form method="POST" action="edit_gasto.php?id=<?php echo htmlspecialchars($id); ?>&month=<?php echo htmlspecialchars($month); ?>&year=<?php echo htmlspecialchars($year); ?>">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($gasto['id']); ?>">
<input type="hidden" name="month" value="<?php echo htmlspecialchars($month); ?>">
<input type="hidden" name="year" value="<?php echo htmlspecialchars($year); ?>">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label for="fecha">Fecha</label>
<input type="date" class="form-control" id="fecha" name="fecha" value="<?php echo htmlspecialchars($gasto['fecha']); ?>" required>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="tipo_gasto">Tipo de Gasto</label>
<select class="form-control" id="tipo_gasto" name="tipo_gasto" required>
<option value="">Seleccione...</option>
<option value="Publicidad" <?php echo ($gasto['tipo_gasto'] == 'Publicidad') ? 'selected' : ''; ?>>Publicidad</option>
<option value="Inversion de Mercaderia" <?php echo ($gasto['tipo_gasto'] == 'Inversion de Mercaderia') ? 'selected' : ''; ?>>Inversión de Mercadería</option>
<option value="Gastos Personales" <?php echo ($gasto['tipo_gasto'] == 'Gastos Personales') ? 'selected' : ''; ?>>Gastos Personales</option>
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="monto">Monto</label>
<input type="number" step="0.01" class="form-control" id="monto" name="monto" value="<?php echo htmlspecialchars($gasto['monto']); ?>" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="descripcion">Descripción</label>
<input type="text" class="form-control" id="descripcion" name="descripcion" value="<?php echo htmlspecialchars($gasto['descripcion']); ?>">
</div>
</div>
</div>
<button type="submit" name="update_expense" class="btn btn-primary mt-3">Actualizar Gasto</button>
<a href="inversion_general.php?month=<?php echo htmlspecialchars($month); ?>&year=<?php echo htmlspecialchars($year); ?>" class="btn btn-secondary mt-3">Cancelar</a>
</form>
</div>
</div>
</div>
<?php include 'layout_footer.php'; ?>