Autosave: 20260216-112421
This commit is contained in:
parent
ab138d3319
commit
a4b8ae74c2
66
index.php
66
index.php
@ -378,6 +378,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$stmt->execute([$customer_id, $invoice_date, $type, $payment_type, $status, $subtotal, $total_vat, $total_with_vat, $paid_amount]);
|
||||
$invoice_id = $db->lastInsertId();
|
||||
|
||||
if ($paid_amount > 0) {
|
||||
$stmt = $db->prepare("INSERT INTO payments (invoice_id, payment_date, amount, payment_method, notes) VALUES (?, ?, ?, ?, ?)");
|
||||
$stmt->execute([$invoice_id, $invoice_date, $paid_amount, 'Cash', 'Initial payment']);
|
||||
}
|
||||
|
||||
foreach ($items_data as $item) {
|
||||
$stmt = $db->prepare("INSERT INTO invoice_items (invoice_id, item_id, quantity, unit_price, total_price) VALUES (?, ?, ?, ?, ?)");
|
||||
$stmt->execute([$invoice_id, $item['id'], $item['qty'], $item['price'], $item['total']]);
|
||||
@ -454,9 +459,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$db->beginTransaction();
|
||||
try {
|
||||
// Get old invoice type and items to revert stock
|
||||
$stmt = $db->prepare("SELECT type FROM invoices WHERE id = ?");
|
||||
$stmt = $db->prepare("SELECT type, paid_amount FROM invoices WHERE id = ?");
|
||||
$stmt->execute([$invoice_id]);
|
||||
$type = $stmt->fetchColumn();
|
||||
$inv_info = $stmt->fetch();
|
||||
$type = $inv_info['type'];
|
||||
$old_paid_amount = (float)$inv_info['paid_amount'];
|
||||
|
||||
$stmt = $db->prepare("SELECT item_id, quantity FROM invoice_items WHERE invoice_id = ?");
|
||||
$stmt->execute([$invoice_id]);
|
||||
@ -502,7 +509,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
}
|
||||
|
||||
$total_with_vat = $subtotal + $total_vat;
|
||||
$paid_amount = ($status === 'paid') ? $total_with_vat : (($status === 'unpaid') ? 0 : $_POST['paid_amount'] ?? 0);
|
||||
$paid_amount = ($status === 'paid') ? $total_with_vat : (($status === 'unpaid') ? 0 : ($old_paid_amount > 0 ? $old_paid_amount : ($_POST['paid_amount'] ?? 0)));
|
||||
|
||||
if ($paid_amount > $old_paid_amount) {
|
||||
$diff = $paid_amount - $old_paid_amount;
|
||||
$stmt = $db->prepare("INSERT INTO payments (invoice_id, payment_date, amount, payment_method, notes) VALUES (?, ?, ?, ?, ?)");
|
||||
$stmt->execute([$invoice_id, $invoice_date, $diff, 'Cash', 'Payment via edit']);
|
||||
}
|
||||
|
||||
// Update invoice
|
||||
$stmt = $db->prepare("UPDATE invoices SET customer_id = ?, invoice_date = ?, payment_type = ?, status = ?, total_amount = ?, vat_amount = ?, total_with_vat = ?, paid_amount = ? WHERE id = ?");
|
||||
@ -673,11 +686,11 @@ if ($page === 'export') {
|
||||
if (!empty($_GET['start_date'])) { $where[] = "v.invoice_date >= ?"; $params[] = $_GET['start_date']; }
|
||||
if (!empty($_GET['end_date'])) { $where[] = "v.invoice_date <= ?"; $params[] = $_GET['end_date']; }
|
||||
$whereSql = implode(" AND ", $where);
|
||||
$stmt = db()->prepare("SELECT v.id, c.name as customer_name, v.invoice_date, v.payment_type, v.status, v.total_amount, v.vat_amount, v.total_with_vat
|
||||
$stmt = db()->prepare("SELECT v.id, c.name as customer_name, v.invoice_date, v.payment_type, v.status, v.total_with_vat, v.paid_amount, (v.total_with_vat - v.paid_amount) as balance
|
||||
FROM invoices v LEFT JOIN customers c ON v.customer_id = c.id
|
||||
WHERE $whereSql ORDER BY v.id DESC");
|
||||
$stmt->execute($params);
|
||||
fputcsv($output, ['Invoice ID', 'Customer/Supplier', 'Date', 'Payment', 'Status', 'Subtotal', 'VAT', 'Total']);
|
||||
fputcsv($output, ['Invoice ID', 'Customer/Supplier', 'Date', 'Payment', 'Status', 'Total', 'Paid', 'Balance']);
|
||||
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) fputcsv($output, $row);
|
||||
} elseif ($type === 'customers' || $type === 'suppliers') {
|
||||
$custType = ($type === 'suppliers') ? 'supplier' : 'customer';
|
||||
@ -822,7 +835,10 @@ switch ($page) {
|
||||
$data['stats'] = [
|
||||
'total_customers' => db()->query("SELECT COUNT(*) FROM customers WHERE type = 'customer'")->fetchColumn(),
|
||||
'total_items' => db()->query("SELECT COUNT(*) FROM stock_items")->fetchColumn(),
|
||||
'total_sales' => db()->query("SELECT SUM(total_with_vat) FROM invoices WHERE type = 'sale'")->fetchColumn() ?: 0,
|
||||
'total_received' => db()->query("SELECT SUM(amount) FROM payments p JOIN invoices i ON p.invoice_id = i.id WHERE i.type = 'sale'")->fetchColumn() ?: 0,
|
||||
];
|
||||
$data['stats']['total_receivable'] = $data['stats']['total_sales'] - $data['stats']['total_received'];
|
||||
break;
|
||||
}
|
||||
|
||||
@ -982,28 +998,34 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
|
||||
<?php if ($page === 'dashboard'): ?>
|
||||
<div class="row g-4 mb-4">
|
||||
<div class="col-md-3">
|
||||
<div class="card p-3 border-start border-primary border-4">
|
||||
<div class="col">
|
||||
<div class="card p-3 border-start border-primary border-4 h-100">
|
||||
<div class="text-muted small" data-en="Total Sales" data-ar="إجمالي المبيعات">Total Sales</div>
|
||||
<div class="h4 m-0">OMR 24,500.000</div>
|
||||
<div class="h4 m-0">OMR <?= number_format((float)($data['stats']['total_sales'] ?? 0), 3) ?></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<div class="card p-3 border-start border-success border-4">
|
||||
<div class="col">
|
||||
<div class="card p-3 border-start border-success border-4 h-100">
|
||||
<div class="text-muted small" data-en="Total Customers" data-ar="إجمالي العملاء">Total Customers</div>
|
||||
<div class="h4 m-0"><?= $data['stats']['total_customers'] ?></div>
|
||||
<div class="h4 m-0"><?= (int)($data['stats']['total_customers'] ?? 0) ?></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<div class="card p-3 border-start border-info border-4">
|
||||
<div class="col">
|
||||
<div class="card p-3 border-start border-info border-4 h-100">
|
||||
<div class="text-muted small" data-en="Total Items" data-ar="إجمالي الأصناف">Total Items</div>
|
||||
<div class="h4 m-0"><?= $data['stats']['total_items'] ?></div>
|
||||
<div class="h4 m-0"><?= (int)($data['stats']['total_items'] ?? 0) ?></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<div class="card p-3 border-start border-warning border-4">
|
||||
<div class="text-muted small" data-en="Net Profit" data-ar="صافي الربح">Net Profit</div>
|
||||
<div class="h4 m-0">OMR 20,300.000</div>
|
||||
<div class="col">
|
||||
<div class="card p-3 border-start border-warning border-4 h-100">
|
||||
<div class="text-muted small" data-en="Total Received" data-ar="إجمالي المبالغ المحصلة">Total Received</div>
|
||||
<div class="h4 m-0">OMR <?= number_format((float)($data['stats']['total_received'] ?? 0), 3) ?></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="card p-3 border-start border-danger border-4 h-100">
|
||||
<div class="text-muted small" data-en="Total Balance" data-ar="إجمالي الرصيد المتبقي">Total Balance</div>
|
||||
<div class="h4 m-0">OMR <?= number_format((float)($data['stats']['total_receivable'] ?? 0), 3) ?></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -1542,7 +1564,9 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
<th data-en="Date" data-ar="التاريخ">Date</th>
|
||||
<th data-en="<?= $page === 'sales' ? 'Customer' : 'Supplier' ?>" data-ar="<?= $page === 'sales' ? 'العميل' : 'المورد' ?>"><?= $page === 'sales' ? 'Customer' : 'Supplier' ?></th>
|
||||
<th data-en="Status" data-ar="الحالة">Status</th>
|
||||
<th data-en="Total Amount" data-ar="إجمالي المبلغ" class="text-end">Total Amount</th>
|
||||
<th data-en="Total" data-ar="الإجمالي" class="text-end">Total</th>
|
||||
<th data-en="Paid" data-ar="المدفوع" class="text-end">Paid</th>
|
||||
<th data-en="Balance" data-ar="المتبقي" class="text-end">Balance</th>
|
||||
<th data-en="Actions" data-ar="الإجراءات" class="text-end">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@ -1583,7 +1607,9 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
<span data-en="<?= $statusLabelEn ?>" data-ar="<?= $statusLabelAr ?>"><?= $statusLabelEn ?></span>
|
||||
</span>
|
||||
</td>
|
||||
<td class="text-end fw-bold">OMR <?= number_format((float)$inv['total_amount'], 3) ?></td>
|
||||
<td class="text-end fw-bold">OMR <?= number_format((float)$inv['total_with_vat'], 3) ?></td>
|
||||
<td class="text-end text-success">OMR <?= number_format((float)$inv['paid_amount'], 3) ?></td>
|
||||
<td class="text-end text-danger fw-bold">OMR <?= number_format((float)($inv['total_with_vat'] - $inv['paid_amount']), 3) ?></td>
|
||||
<td class="text-end">
|
||||
<div class="btn-group btn-group-sm">
|
||||
<?php if ($inv['status'] !== 'paid'): ?>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user