249 lines
11 KiB
PHP
249 lines
11 KiB
PHP
<?php
|
|
require_once __DIR__ . "/../includes/functions.php";
|
|
require_permission("expense_categories_view");
|
|
require_once __DIR__ . '/../db/config.php';
|
|
$pdo = db();
|
|
|
|
$message = '';
|
|
|
|
// Handle Add/Edit Expense Category
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
|
$action = $_POST['action'];
|
|
$name = trim($_POST['name']);
|
|
$name_ar = trim($_POST['name_ar'] ?? '');
|
|
$description = trim($_POST['description']);
|
|
$id = isset($_POST['id']) ? (int)$_POST['id'] : null;
|
|
|
|
if (empty($name)) {
|
|
$message = '<div class="alert alert-danger">Category name is required.</div>';
|
|
} else {
|
|
try {
|
|
if ($action === 'edit_expense_category' && $id) {
|
|
if (!has_permission('expense_categories_add')) {
|
|
$message = '<div class="alert alert-danger">Access Denied.</div>';
|
|
} else {
|
|
$stmt = $pdo->prepare("UPDATE expense_categories SET name = ?, name_ar = ?, description = ? WHERE id = ?");
|
|
$stmt->execute([$name, $name_ar, $description, $id]);
|
|
$message = '<div class="alert alert-success">Expense category updated successfully!</div>';
|
|
}
|
|
} elseif ($action === 'add_expense_category') {
|
|
if (!has_permission('expense_categories_add')) {
|
|
$message = '<div class="alert alert-danger">Access Denied.</div>';
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO expense_categories (name, name_ar, description) VALUES (?, ?, ?)");
|
|
$stmt->execute([$name, $name_ar, $description]);
|
|
$message = '<div class="alert alert-success">Expense category created successfully!</div>';
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Database error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle Delete (Soft Delete)
|
|
if (isset($_GET['delete'])) {
|
|
if (!has_permission('expense_categories_del')) {
|
|
$message = '<div class="alert alert-danger">Access Denied: You do not have permission to delete expense categories.</div>';
|
|
} else {
|
|
try {
|
|
$id = (int)$_GET['delete'];
|
|
// Soft delete to preserve relations with expenses
|
|
$pdo->prepare("UPDATE expense_categories SET is_deleted = 1 WHERE id = ?")->execute([$id]);
|
|
header("Location: expense_categories.php?deleted=1");
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Error removing category: ' . $e->getMessage() . '</div>';
|
|
}
|
|
}
|
|
}
|
|
|
|
if (isset($_GET['deleted'])) {
|
|
$message = '<div class="alert alert-success">Expense category removed successfully!</div>';
|
|
}
|
|
|
|
$query = "SELECT * FROM expense_categories WHERE is_deleted = 0 ORDER BY name ASC";
|
|
$expense_categories_pagination = paginate_query($pdo, $query);
|
|
$expense_categories = $expense_categories_pagination['data'];
|
|
|
|
include 'includes/header.php';
|
|
?>
|
|
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h2 class="fw-bold mb-0">Expense Categories</h2>
|
|
<?php if (has_permission('expense_categories_add')): ?>
|
|
<button class="btn btn-primary" onclick="openAddModal()">
|
|
<i class="bi bi-plus-lg"></i> Add Category
|
|
</button>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?= $message ?>
|
|
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-body p-0">
|
|
<!-- Pagination Controls -->
|
|
<div class="p-3 border-bottom bg-light">
|
|
<?php render_pagination_controls($expense_categories_pagination); ?>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead class="bg-light">
|
|
<tr>
|
|
<th class="ps-4">ID</th>
|
|
<th>Name</th>
|
|
<th>Arabic Name</th>
|
|
<th>Description</th>
|
|
<th class="text-end pe-4">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($expense_categories as $cat): ?>
|
|
<tr>
|
|
<td class="ps-4 fw-medium">#<?= $cat['id'] ?></td>
|
|
<td class="fw-bold"><?= htmlspecialchars($cat['name']) ?></td>
|
|
<td><?= htmlspecialchars($cat['name_ar'] ?: '-') ?></td>
|
|
<td><small class="text-muted"><?= htmlspecialchars($cat['description'] ?: '-') ?></small></td>
|
|
<td class="text-end pe-4">
|
|
<?php if (has_permission('expense_categories_add')): ?>
|
|
<button type="button" class="btn btn-sm btn-outline-primary me-1"
|
|
onclick='openEditModal(<?= htmlspecialchars(json_encode($cat), ENT_QUOTES, "UTF-8") ?>)' title="Edit"><i class="bi bi-pencil"></i></button>
|
|
<?php endif; ?>
|
|
|
|
<?php if (has_permission('expense_categories_del')): ?>
|
|
<a href="?delete=<?= $cat['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('<?= t('are_you_sure') ?>')"><i class="bi bi-trash"></i></a>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php if (empty($expense_categories)): ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center py-4 text-muted">No expense categories found.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- Bottom Pagination -->
|
|
<div class="p-3 border-top bg-light">
|
|
<?php render_pagination_controls($expense_categories_pagination); ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Expense Category Modal -->
|
|
<?php if (has_permission('expense_categories_add')): ?>
|
|
<div class="modal fade" id="expenseCategoryModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header bg-primary text-white">
|
|
<h5 class="modal-title" id="expenseCategoryModalTitle">Add New Category</h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<form method="POST" id="expenseCategoryForm">
|
|
<div class="modal-body">
|
|
<input type="hidden" name="action" id="expenseCategoryAction" value="add_expense_category">
|
|
<input type="hidden" name="id" id="expenseCategoryId">
|
|
<div class="mb-3">
|
|
<label class="form-label">Category Name <span class="text-danger">*</span></label>
|
|
<div class="input-group">
|
|
<input type="text" name="name" id="expenseCategoryName" class="form-control" required>
|
|
<button class="btn btn-outline-secondary" type="button" id="btnTranslate">
|
|
<i class="bi bi-translate text-primary"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Arabic Name</label>
|
|
<input type="text" name="name_ar" id="expenseCategoryNameAr" class="form-control" dir="rtl">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Description</label>
|
|
<textarea name="description" id="expenseCategoryDescription" class="form-control" rows="3"></textarea>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
<button type="submit" class="btn btn-primary">Save Category</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function getExpenseCategoryModal() {
|
|
if (typeof bootstrap === 'undefined') return null;
|
|
const el = document.getElementById('expenseCategoryModal');
|
|
return el ? bootstrap.Modal.getOrCreateInstance(el) : null;
|
|
}
|
|
|
|
function openAddModal() {
|
|
const modal = getExpenseCategoryModal();
|
|
if (!modal) return;
|
|
|
|
document.getElementById('expenseCategoryModalTitle').innerText = 'Add New Category';
|
|
document.getElementById('expenseCategoryAction').value = 'add_expense_category';
|
|
document.getElementById('expenseCategoryForm').reset();
|
|
document.getElementById('expenseCategoryId').value = '';
|
|
modal.show();
|
|
}
|
|
|
|
function openEditModal(cat) {
|
|
if (!cat) return;
|
|
const modal = getExpenseCategoryModal();
|
|
if (!modal) return;
|
|
|
|
document.getElementById('expenseCategoryModalTitle').innerText = 'Edit Category';
|
|
document.getElementById('expenseCategoryAction').value = 'edit_expense_category';
|
|
document.getElementById('expenseCategoryId').value = cat.id;
|
|
document.getElementById('expenseCategoryName').value = cat.name || '';
|
|
document.getElementById('expenseCategoryNameAr').value = cat.name_ar || '';
|
|
document.getElementById('expenseCategoryDescription').value = cat.description || '';
|
|
|
|
modal.show();
|
|
}
|
|
|
|
document.getElementById('btnTranslate').addEventListener('click', function() {
|
|
const text = document.getElementById('expenseCategoryName').value;
|
|
if (!text) {
|
|
alert('Please enter a category name first.');
|
|
return;
|
|
}
|
|
|
|
const btn = this;
|
|
const originalHtml = btn.innerHTML;
|
|
btn.disabled = true;
|
|
btn.innerHTML = '<span class="spinner-border spinner-border-sm text-primary" role="status" aria-hidden="true"></span>';
|
|
|
|
fetch('../api/translate.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
text: text,
|
|
target_lang: 'Arabic'
|
|
}),
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
document.getElementById('expenseCategoryNameAr').value = data.translated_text;
|
|
} else {
|
|
alert('Translation failed: ' + (data.error || 'Unknown error'));
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
alert('An error occurred during translation.');
|
|
})
|
|
.finally(() => {
|
|
btn.disabled = false;
|
|
btn.innerHTML = originalHtml;
|
|
});
|
|
});
|
|
</script>
|
|
<?php endif; ?>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|