180 lines
7.4 KiB
PHP
180 lines
7.4 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
require_once 'includes/header.php';
|
|
require_once 'includes/accounting_functions.php';
|
|
|
|
// Check permission
|
|
$user_id = $_SESSION['user_id'];
|
|
$stmt = db()->prepare("SELECT * FROM user_permissions WHERE user_id = ? AND page = 'accounting' AND can_view = 1");
|
|
$stmt->execute([$user_id]);
|
|
if (!$stmt->fetch()) {
|
|
echo "<div class='container mt-4' dir='rtl'>لا تملك صلاحية الوصول لهذه الصفحة.</div>";
|
|
require_once 'includes/footer.php';
|
|
exit;
|
|
}
|
|
|
|
// Handle form submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_entry'])) {
|
|
$date = $_POST['date'];
|
|
$description = $_POST['description'];
|
|
$reference = $_POST['reference'];
|
|
$account = $_POST['account'];
|
|
$debit = (float)$_POST['debit'];
|
|
$credit = (float)$_POST['credit'];
|
|
|
|
$entries = [['account' => $account, 'debit' => $debit, 'credit' => $credit]];
|
|
|
|
if (add_journal_entry($date, $description, $reference, $entries)) {
|
|
$message = "تم إضافة القيد بنجاح.";
|
|
} else {
|
|
$error = "حدث خطأ أثناء إضافة القيد.";
|
|
}
|
|
}
|
|
|
|
// Pagination setup
|
|
$page = isset($_GET['p']) ? (int)$_GET['p'] : 1;
|
|
$limit = 10;
|
|
$offset = ($page - 1) * $limit;
|
|
|
|
// Fetch ledger data with pagination
|
|
$ledger_all = get_full_ledger();
|
|
$total_items = count($ledger_all);
|
|
$total_pages = ceil($total_items / $limit);
|
|
$ledger = array_slice($ledger_all, $offset, $limit);
|
|
|
|
$trial_balance = get_trial_balance();
|
|
$balance_sheet = get_balance_sheet();
|
|
?>
|
|
|
|
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
|
|
<link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
|
|
|
|
<div class="container mt-4" dir="rtl">
|
|
<h2 class="text-right">المحاسبة (Accounting)</h2>
|
|
|
|
<?php if (isset($message)) echo "<div class='alert alert-success'>$message</div>"; ?>
|
|
<?php if (isset($error)) echo "<div class='alert alert-danger'>$error</div>"; ?>
|
|
|
|
<div class="row">
|
|
<div class="col-md-3">
|
|
<div class="card bg-primary text-white text-center mb-3">
|
|
<div class="card-body">
|
|
<h5>الأصول</h5>
|
|
<h3><?= number_format($balance_sheet['أصول'], 2) ?></h3>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<div class="card bg-danger text-white text-center mb-3">
|
|
<div class="card-body">
|
|
<h5>الخصوم</h5>
|
|
<h3><?= number_format($balance_sheet['خصوم'], 2) ?></h3>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<div class="card bg-success text-white text-center mb-3">
|
|
<div class="card-body">
|
|
<h5>حقوق الملكية</h5>
|
|
<h3><?= number_format($balance_sheet['حقوق ملكية'], 2) ?></h3>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<div class="card bg-info text-white text-center mb-3">
|
|
<div class="card-body">
|
|
<h5>صافي الربح/الخسارة</h5>
|
|
<h3><?= number_format($balance_sheet['إيرادات'] - $balance_sheet['مصروفات'], 2) ?></h3>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">إضافة قيد محاسبي جديد</div>
|
|
<div class="card-body">
|
|
<form method="POST">
|
|
<input type="hidden" name="add_entry" value="1">
|
|
<div class="row">
|
|
<div class="col-md-2">
|
|
<label>التاريخ</label>
|
|
<input type="date" name="date" class="form-control" required value="<?= date('Y-m-d') ?>">
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label>الوصف</label>
|
|
<input type="text" name="description" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label>المرجع</label>
|
|
<input type="text" name="reference" class="form-control">
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label>الحساب</label>
|
|
<select name="account" id="accountSelect" class="form-select" required style="width: 100%;">
|
|
<?php foreach (get_all_accounts() as $acc): ?>
|
|
<option value="<?= htmlspecialchars($acc['name']) ?>"><?= htmlspecialchars($acc['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-1">
|
|
<label>مدين</label>
|
|
<input type="number" step="0.01" name="debit" class="form-control" value="0">
|
|
</div>
|
|
<div class="col-md-1">
|
|
<label>دائن</label>
|
|
<input type="number" step="0.01" name="credit" class="form-control" value="0">
|
|
</div>
|
|
<div class="col-md-1 d-flex align-items-end">
|
|
<button type="submit" class="btn btn-primary">حفظ</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-12">
|
|
<h3 class="text-right">دفتر الأستاذ (General Ledger)</h3>
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered text-right">
|
|
<thead><tr><th>التاريخ</th><th>الوصف</th><th>المرجع</th><th>الحساب</th><th>مدين</th><th>دائن</th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($ledger as $row): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($row['date']) ?></td>
|
|
<td><?= htmlspecialchars($row['description']) ?></td>
|
|
<td><?= htmlspecialchars($row['reference']) ?></td>
|
|
<td><?= htmlspecialchars($row['account_name']) ?></td>
|
|
<td><?= number_format($row['debit'], 2) ?></td>
|
|
<td><?= number_format($row['credit'], 2) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<nav>
|
|
<ul class="pagination justify-content-center">
|
|
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
|
|
<li class="page-item <?= $i == $page ? 'active' : '' ?>">
|
|
<a class="page-link" href="?p=<?= $i ?>"><?= $i ?></a>
|
|
</li>
|
|
<?php endfor; ?>
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
|
<script>
|
|
$(document).ready(function() {
|
|
$('#accountSelect').select2({
|
|
theme: 'bootstrap-5',
|
|
dir: 'rtl'
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|