diff --git a/db/migrations/066_create_inversion_general_table.sql b/db/migrations/066_create_inversion_general_table.sql new file mode 100644 index 0000000..8a6862f --- /dev/null +++ b/db/migrations/066_create_inversion_general_table.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS inversion_general ( + id INT AUTO_INCREMENT PRIMARY KEY, + fecha DATE NOT NULL, + tipo_gasto VARCHAR(100) NOT NULL, + monto DECIMAL(10, 2) NOT NULL, + descripcion VARCHAR(255) DEFAULT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); diff --git a/edit_gasto.php b/edit_gasto.php new file mode 100644 index 0000000..fe4841f --- /dev/null +++ b/edit_gasto.php @@ -0,0 +1,112 @@ +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'; +?> + +
+

Editar Gasto

+ +
+
+ Modificar Información del Gasto +
+
+
+ + + +
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+ + Cancelar +
+
+
+
+ + \ No newline at end of file diff --git a/inversion_general.php b/inversion_general.php index 31b0293..df0fef5 100644 --- a/inversion_general.php +++ b/inversion_general.php @@ -3,10 +3,6 @@ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); -// Log errors to a file -ini_set('log_errors', 1); -ini_set('error_log', 'php-error.log'); - session_start(); require_once 'db/config.php'; @@ -17,18 +13,203 @@ if (!isset($_SESSION['user_id']) || !isset($_SESSION['user_role']) || !in_array( exit; } +$pdo = db(); + +// Handle form submission to add a new expense +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_expense'])) { + $fecha = $_POST['fecha']; + $tipo_gasto = $_POST['tipo_gasto']; + $monto = $_POST['monto']; + $descripcion = $_POST['descripcion']; + + if (!empty($fecha) && !empty($tipo_gasto) && !empty($monto)) { + $stmt = $pdo->prepare("INSERT INTO inversion_general (fecha, tipo_gasto, monto, descripcion) VALUES (?, ?, ?, ?)"); + $stmt->execute([$fecha, $tipo_gasto, $monto, $descripcion]); + // Redirect to the same month view after adding an expense + $month = date('m', strtotime($fecha)); + $year = date('Y', strtotime($fecha)); + header("Location: inversion_general.php?month=$month&year=$year&status=success"); + exit; + } else { + header("Location: inversion_general.php?status=error"); + exit; + } +} + +// Handle deletion of an expense +if (isset($_GET['delete_id'])) { + $delete_id = $_GET['delete_id']; + // Get the month and year from the query string for redirection + $month = $_GET['month'] ?? date('m'); + $year = $_GET['year'] ?? date('Y'); + $stmt = $pdo->prepare("DELETE FROM inversion_general WHERE id = ?"); + $stmt->execute([$delete_id]); + header("Location: inversion_general.php?month=$month&year=$year&status=deleted"); + exit; +} + +// --- Monthly Navigation Logic --- +$month = isset($_GET['month']) ? (int)$_GET['month'] : date('m'); +$year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y'); + +$dateObj = DateTime::createFromFormat('!m', $month); +$monthName = $dateObj->format('F'); // Full month name + +// Create a mapping for Spanish month names +$meses_espanol = [ + 'January' => 'Enero', 'February' => 'Febrero', 'March' => 'Marzo', 'April' => 'Abril', + 'May' => 'Mayo', 'June' => 'Junio', 'July' => 'Julio', 'August' => 'Agosto', + 'September' => 'Septiembre', 'October' => 'Octubre', 'November' => 'Noviembre', 'December' => 'Diciembre' +]; +$monthNameSpanish = $meses_espanol[$monthName]; + +$prev_month = $month - 1; +$prev_year = $year; +if ($prev_month == 0) { + $prev_month = 12; + $prev_year--; +} + +$next_month = $month + 1; +$next_year = $year; +if ($next_month == 13) { + $next_month = 1; + $next_year++; +} +// --- End of Navigation Logic --- + + +// Fetch expenses for the selected month and year +$stmt = $pdo->prepare("SELECT * FROM inversion_general WHERE YEAR(fecha) = ? AND MONTH(fecha) = ? ORDER BY fecha DESC"); +$stmt->execute([$year, $month]); +$gastos = $stmt->fetchAll(PDO::FETCH_ASSOC); + +// Group expenses by type +$gastos_por_tipo = [ + 'Publicidad' => [], + 'Inversion de Mercaderia' => [], + 'Gastos Personales' => [] +]; +$totales_por_tipo = [ + 'Publicidad' => 0, + 'Inversion de Mercaderia' => 0, + 'Gastos Personales' => 0 +]; + +foreach ($gastos as $gasto) { + $tipo = $gasto['tipo_gasto']; + if (array_key_exists($tipo, $gastos_por_tipo)) { + $gastos_por_tipo[$tipo][] = $gasto; + $totales_por_tipo[$tipo] += $gasto['monto']; + } +} + $pageTitle = 'Inversión General'; include 'layout_header.php'; ?>
- - -
+

Registro de Inversión General

+ + + + + +
+
+ Agregar Nuevo Gasto +
-

Esta sección está en construcción.

+
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+ + 'Gastos de Publicidad', + 'Inversion de Mercaderia' => 'Inversión de Mercadería', + 'Gastos Personales' => 'Gastos Personales' + ]; + ?> + + $titulo): ?> +
+
+ + Total: S/ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FechaMontoDescripciónAcciones
No hay gastos registrados en esta categoría para .
S/ + Editar + Eliminar +
+
+
+
+
- + \ No newline at end of file