37392-vm/add_expense.php
Flatlogic Bot b400910e2f gastos02
2026-01-13 02:34:53 +00:00

181 lines
10 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$message = '';
$error = '';
$pdo = db();
// Fetch data for dropdowns
$categories = $pdo->query('SELECT * FROM categories ORDER BY name')->fetchAll();
$users = $pdo->query('SELECT * FROM users ORDER BY name')->fetchAll();
$accounts = $pdo->query('SELECT * FROM accounts ORDER BY name')->fetchAll();
$expense_types = ['expense', 'income', 'transfer'];
$split_types = ['none', 'equally', 'parts', 'amounts'];
$currencies = ['USD', 'EUR', 'COP'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
try {
// Sanitize and validate input
$expense_date = filter_input(INPUT_POST, 'expense_date', FILTER_SANITIZE_STRING);
$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING);
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_FLOAT);
$currency = filter_input(INPUT_POST, 'currency', FILTER_SANITIZE_STRING);
$category_id = filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
$user_id = filter_input(INPUT_POST, 'user_id', FILTER_VALIDATE_INT);
$account_id = filter_input(INPUT_POST, 'account_id', FILTER_VALIDATE_INT);
$expense_type = filter_input(INPUT_POST, 'expense_type', FILTER_SANITIZE_STRING);
$split_type = filter_input(INPUT_POST, 'split_type', FILTER_SANITIZE_STRING);
// Handle file upload
$receipt_path = null;
if (isset($_FILES['receipt']) && $_FILES['receipt']['error'] == UPLOAD_ERR_OK) {
$upload_dir = __DIR__ . '/assets/uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0775, true);
}
$filename = uniqid() . '-' . basename($_FILES['receipt']['name']);
$receipt_path = '/assets/uploads/' . $filename;
move_uploaded_file($_FILES['receipt']['tmp_name'], $upload_dir . $filename);
}
$sql = "INSERT INTO expenses (expense_date, title, amount, currency, expense_type, split_type, category_id, user_id, account_id, receipt_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
if ($stmt->execute([$expense_date, $title, $amount, $currency, $expense_type, $split_type, $category_id, $user_id, $account_id, $receipt_path])) {
header("Location: index.php?message=success");
exit;
} else {
$error = "Error saving the expense.";
}
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
$error = "Database error. Please try again later.";
} catch (Exception $e) {
error_log("File Upload Error: " . $e->getMessage());
$error = "Error uploading the file.";
}
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Añadir Gasto</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body style="background-color: #F3F4F6;">
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-lg-8">
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<div class="card shadow-sm" style="border-radius: 1rem;">
<div class="card-body p-4 p-md-5">
<div class="d-flex align-items-center mb-4">
<a href="index.php" class="btn btn-light me-3"><i class="bi bi-arrow-left"></i></a>
<h2 class="mb-0">Añadir Nuevo Gasto</h2>
</div>
<form id="add-expense-form" method="POST" action="add_expense.php" enctype="multipart/form-data">
<div class="row g-3">
<div class="col-md-6">
<label for="expense-date" class="form-label">Fecha</label>
<input type="date" class="form-control" name="expense_date" required>
</div>
<div class="col-md-6">
<label for="title" class="form-label">Título</label>
<input type="text" class="form-control" name="title" placeholder="Ej: Cena con amigos" required>
</div>
<div class="col-md-6">
<label for="amount" class="form-label">Monto</label>
<div class="input-group">
<span class="input-group-text">$</span>
<input type="number" class="form-control" name="amount" placeholder="100.00" step="0.01" required>
</div>
</div>
<div class="col-md-6">
<label for="currency" class="form-label">Moneda</label>
<select name="currency" class="form-select" required>
<?php foreach ($currencies as $currency): ?>
<option value="<?php echo $currency; ?>"><?php echo $currency; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label for="category_id" class="form-label">Categoría</label>
<select name="category_id" class="form-select">
<option value="">Elegir...</option>
<?php foreach ($categories as $category): ?>
<option value="<?php echo $category['id']; ?>"><?php echo htmlspecialchars($category['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label for="user_id" class="form-label">Usuario</label>
<select name="user_id" class="form-select">
<option value="">Elegir...</option>
<?php foreach ($users as $user): ?>
<option value="<?php echo $user['id']; ?>"><?php echo htmlspecialchars($user['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-12">
<label for="account_id" class="form-label">Cuenta</label>
<select name="account_id" class="form-select">
<option value="">Elegir...</option>
<?php foreach ($accounts as $account): ?>
<option value="<?php echo $account['id']; ?>"><?php echo htmlspecialchars($account['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label for="expense_type" class="form-label">Tipo de Gasto</label>
<select name="expense_type" class="form-select" required>
<?php foreach ($expense_types as $type): ?>
<option value="<?php echo $type; ?>"><?php echo ucfirst($type); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label for="split_type" class="form-label">Tipo de División</label>
<select name="split_type" class="form-select" required>
<?php foreach ($split_types as $type): ?>
<option value="<?php echo $type; ?>"><?php echo ucfirst($type); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-12">
<label for="receipt" class="form-label">Adjuntar Recibo</label>
<input class="form-control" type="file" name="receipt">
</div>
</div>
<hr class="my-4">
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
<button type="button" class="btn btn-light me-md-2" onclick="window.location.href='index.php'">Cancelar</button>
<button type="submit" class="btn btn-primary">Guardar Gasto</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>