38808-vm/accounting.php
2026-03-11 17:54:07 +00:00

146 lines
6.1 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 = "حدث خطأ أثناء إضافة القيد.";
}
}
$ledger = get_full_ledger();
$trial_balance = get_trial_balance();
$balance_sheet = get_balance_sheet();
?>
<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['Assets'], 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['Liabilities'], 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['Equity'], 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['Revenue'] - $balance_sheet['Expenses'], 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" class="form-control" required>
<?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>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>