prepare("SELECT id, name_en, name_ar, sku, sale_price, purchase_price, stock_quantity, vat_rate FROM stock_items WHERE name_en LIKE ? OR name_ar LIKE ? OR sku LIKE ? LIMIT 10");
$stmt->execute(["%$q%", "%$q%", "%$q%"]);
echo json_encode($stmt->fetchAll());
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['add_customer'])) {
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$phone = $_POST['phone'] ?? '';
$balance = (float)($_POST['balance'] ?? 0);
$type = $_POST['type'] ?? 'customer';
if ($name) {
$stmt = db()->prepare("INSERT INTO customers (name, email, phone, balance, type) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$name, $email, $phone, $balance, $type]);
$message = ucfirst($type) . " added successfully!";
}
}
if (isset($_POST['edit_customer'])) {
$id = (int)$_POST['id'];
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$phone = $_POST['phone'] ?? '';
$balance = (float)($_POST['balance'] ?? 0);
if ($id && $name) {
$stmt = db()->prepare("UPDATE customers SET name = ?, email = ?, phone = ?, balance = ? WHERE id = ?");
$stmt->execute([$name, $email, $phone, $balance, $id]);
$message = "Record updated successfully!";
}
}
if (isset($_POST['delete_customer'])) {
$id = (int)$_POST['id'];
if ($id) {
$stmt = db()->prepare("DELETE FROM customers WHERE id = ?");
$stmt->execute([$id]);
$message = "Record deleted successfully!";
}
}
if (isset($_POST['add_category'])) {
$name_en = $_POST['name_en'] ?? '';
$name_ar = $_POST['name_ar'] ?? '';
if ($name_en && $name_ar) {
$stmt = db()->prepare("INSERT INTO stock_categories (name_en, name_ar) VALUES (?, ?)");
$stmt->execute([$name_en, $name_ar]);
$message = "Category added successfully!";
}
}
if (isset($_POST['add_unit'])) {
$name_en = $_POST['name_en'] ?? '';
$name_ar = $_POST['name_ar'] ?? '';
$short_en = $_POST['short_en'] ?? '';
$short_ar = $_POST['short_ar'] ?? '';
if ($name_en && $name_ar) {
$stmt = db()->prepare("INSERT INTO stock_units (name_en, name_ar, short_name_en, short_name_ar) VALUES (?, ?, ?, ?)");
$stmt->execute([$name_en, $name_ar, $short_en, $short_ar]);
$message = "Unit added successfully!";
}
}
if (isset($_POST['add_item'])) {
$cat_id = $_POST['category_id'] ?: null;
$unit_id = $_POST['unit_id'] ?: null;
$supplier_id = $_POST['supplier_id'] ?: null;
$name_en = $_POST['name_en'] ?? '';
$name_ar = $_POST['name_ar'] ?? '';
$sku = $_POST['sku'] ?? '';
$p_price = (float)($_POST['purchase_price'] ?? 0);
$s_price = (float)($_POST['sale_price'] ?? 0);
$qty = (float)($_POST['stock_quantity'] ?? 0);
$min_stock = (float)($_POST['min_stock_level'] ?? 0);
$vat_rate = (float)($_POST['vat_rate'] ?? 0);
$expiry = $_POST['expiry_date'] ?: null;
$image_path = null;
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$filename = uniqid('item_') . '.' . $ext;
$target = 'uploads/items/' . $filename;
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$image_path = $target;
}
}
if ($name_en && $name_ar) {
$stmt = db()->prepare("INSERT INTO stock_items (category_id, unit_id, supplier_id, name_en, name_ar, sku, purchase_price, sale_price, stock_quantity, min_stock_level, expiry_date, image_path, vat_rate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$cat_id, $unit_id, $supplier_id, $name_en, $name_ar, $sku, $p_price, $s_price, $qty, $min_stock, $expiry, $image_path, $vat_rate]);
$message = "Item added successfully!";
}
}
if (isset($_POST['delete_item'])) {
$id = (int)$_POST['id'];
// Optional: delete image file
$item = db()->prepare("SELECT image_path FROM stock_items WHERE id = ?");
$item->execute([$id]);
$path = $item->fetchColumn();
if ($path && file_exists($path)) {
unlink($path);
}
$stmt = db()->prepare("DELETE FROM stock_items WHERE id = ?");
$stmt->execute([$id]);
$message = "Item deleted successfully!";
}
if (isset($_POST['edit_item'])) {
$id = (int)$_POST['id'];
$cat_id = $_POST['category_id'] ?: null;
$unit_id = $_POST['unit_id'] ?: null;
$supplier_id = $_POST['supplier_id'] ?: null;
$name_en = $_POST['name_en'] ?? '';
$name_ar = $_POST['name_ar'] ?? '';
$sku = $_POST['sku'] ?? '';
$p_price = (float)($_POST['purchase_price'] ?? 0);
$s_price = (float)($_POST['sale_price'] ?? 0);
$qty = (float)($_POST['stock_quantity'] ?? 0);
$min_stock = (float)($_POST['min_stock_level'] ?? 0);
$vat_rate = (float)($_POST['vat_rate'] ?? 0);
$expiry = $_POST['expiry_date'] ?: null;
$stmt = db()->prepare("SELECT image_path FROM stock_items WHERE id = ?");
$stmt->execute([$id]);
$image_path = $stmt->fetchColumn();
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
// Delete old image
if ($image_path && file_exists($image_path)) {
unlink($image_path);
}
$ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$filename = uniqid('item_') . '.' . $ext;
$target = 'uploads/items/' . $filename;
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$image_path = $target;
}
}
if ($name_en && $name_ar) {
$stmt = db()->prepare("UPDATE stock_items SET category_id = ?, unit_id = ?, supplier_id = ?, name_en = ?, name_ar = ?, sku = ?, purchase_price = ?, sale_price = ?, stock_quantity = ?, min_stock_level = ?, expiry_date = ?, image_path = ?, vat_rate = ? WHERE id = ?");
$stmt->execute([$cat_id, $unit_id, $supplier_id, $name_en, $name_ar, $sku, $p_price, $s_price, $qty, $min_stock, $expiry, $image_path, $vat_rate, $id]);
$message = "Item updated successfully!";
}
}
if (isset($_POST['import_items'])) {
if (isset($_FILES['excel_file']) && $_FILES['excel_file']['error'] === UPLOAD_ERR_OK) {
$file = $_FILES['excel_file']['tmp_name'];
$handle = fopen($file, 'r');
$header = fgetcsv($handle); // Skip header row
$count = 0;
while (($row = fgetcsv($handle)) !== FALSE) {
// Mapping: sku, eng name, arabic name, sale price, cost price
if (count($row) < 5) continue;
$sku = trim($row[0]);
$name_en = trim($row[1]);
$name_ar = trim($row[2]);
$sale_price = (float)trim($row[3]);
$purchase_price = (float)trim($row[4]);
if ($name_en && $name_ar) {
// Check if SKU exists to update or insert
$existingId = null;
if ($sku !== "") {
$check = db()->prepare("SELECT id FROM stock_items WHERE sku = ?");
$check->execute([$sku]);
$existingId = $check->fetchColumn();
}
if ($existingId) {
$stmt = db()->prepare("UPDATE stock_items SET name_en = ?, name_ar = ?, sale_price = ?, purchase_price = ? WHERE id = ?");
$stmt->execute([$name_en, $name_ar, $sale_price, $purchase_price, $existingId]);
} else {
$stmt = db()->prepare("INSERT INTO stock_items (sku, name_en, name_ar, sale_price, purchase_price) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$sku, $name_en, $name_ar, $sale_price, $purchase_price]);
}
$count++;
}
}
fclose($handle);
$message = "$count items processed successfully!";
}
}
if (isset($_POST['import_customers']) || isset($_POST['import_suppliers'])) {
$type = isset($_POST['import_customers']) ? 'customer' : 'supplier';
if (isset($_FILES['excel_file']) && $_FILES['excel_file']['error'] === UPLOAD_ERR_OK) {
$file = $_FILES['excel_file']['tmp_name'];
$handle = fopen($file, 'r');
$header = fgetcsv($handle);
$count = 0;
while (($row = fgetcsv($handle)) !== FALSE) {
if (count($row) < 4) continue;
$name = trim($row[0]);
$email = trim($row[1]);
$phone = trim($row[2]);
$balance = (float)trim($row[3]);
if ($name) {
$stmt = db()->prepare("INSERT INTO customers (name, email, phone, balance, type) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$name, $email, $phone, $balance, $type]);
$count++;
}
}
fclose($handle);
$message = "$count " . ($type === 'customer' ? 'customers' : 'suppliers') . " imported successfully!";
}
}
if (isset($_POST['import_categories'])) {
if (isset($_FILES['excel_file']) && $_FILES['excel_file']['error'] === UPLOAD_ERR_OK) {
$file = $_FILES['excel_file']['tmp_name'];
$handle = fopen($file, 'r');
$header = fgetcsv($handle);
$count = 0;
while (($row = fgetcsv($handle)) !== FALSE) {
if (count($row) < 2) continue;
$name_en = trim($row[0]);
$name_ar = trim($row[1]);
if ($name_en && $name_ar) {
$stmt = db()->prepare("INSERT INTO stock_categories (name_en, name_ar) VALUES (?, ?)");
$stmt->execute([$name_en, $name_ar]);
$count++;
}
}
fclose($handle);
$message = "$count categories imported successfully!";
}
}
if (isset($_POST['import_units'])) {
if (isset($_FILES['excel_file']) && $_FILES['excel_file']['error'] === UPLOAD_ERR_OK) {
$file = $_FILES['excel_file']['tmp_name'];
$handle = fopen($file, 'r');
$header = fgetcsv($handle);
$count = 0;
while (($row = fgetcsv($handle)) !== FALSE) {
if (count($row) < 4) continue;
$name_en = trim($row[0]);
$name_ar = trim($row[1]);
$short_en = trim($row[2]);
$short_ar = trim($row[3]);
if ($name_en && $name_ar) {
$stmt = db()->prepare("INSERT INTO stock_units (name_en, name_ar, short_name_en, short_name_ar) VALUES (?, ?, ?, ?)");
$stmt->execute([$name_en, $name_ar, $short_en, $short_ar]);
$count++;
}
}
fclose($handle);
$message = "$count units imported successfully!";
}
}
if (isset($_POST['add_invoice'])) {
$customer_id = $_POST['customer_id'] ?: null;
$invoice_date = $_POST['invoice_date'] ?: date('Y-m-d');
$type = $_POST['type'] ?? 'sale'; // 'sale' or 'purchase'
$payment_type = $_POST['payment_type'] ?? 'cash';
$item_ids = $_POST['item_ids'] ?? [];
$quantities = $_POST['quantities'] ?? [];
$prices = $_POST['prices'] ?? [];
if (!empty($item_ids)) {
$db = db();
$db->beginTransaction();
try {
$subtotal = 0;
$total_vat = 0;
$items_data = [];
foreach ($item_ids as $index => $item_id) {
$qty = (float)$quantities[$index];
$price = (float)$prices[$index];
// Fetch vat_rate for this item
$stmtVat = $db->prepare("SELECT vat_rate FROM stock_items WHERE id = ?");
$stmtVat->execute([$item_id]);
$vat_rate = (float)$stmtVat->fetchColumn();
$line_total = $qty * $price;
$line_vat = $line_total * ($vat_rate / 100);
$subtotal += $line_total;
$total_vat += $line_vat;
$items_data[] = [
'id' => $item_id,
'qty' => $qty,
'price' => $price,
'total' => $line_total
];
}
$total_with_vat = $subtotal + $total_vat;
$stmt = $db->prepare("INSERT INTO invoices (customer_id, invoice_date, type, payment_type, total_amount, vat_amount, total_with_vat) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$customer_id, $invoice_date, $type, $payment_type, $subtotal, $total_vat, $total_with_vat]);
$invoice_id = $db->lastInsertId();
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']]);
// Update stock level
if ($type === 'sale') {
$stmt = $db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity - ? WHERE id = ?");
} else {
$stmt = $db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity + ? WHERE id = ?");
}
$stmt->execute([$item['qty'], $item['id']]);
}
$db->commit();
$message = "Invoice #$invoice_id created successfully!";
} catch (Exception $e) {
$db->rollBack();
$message = "Error: " . $e->getMessage();
}
}
}
if (isset($_POST['delete_invoice'])) {
$id = (int)$_POST['id'];
if ($id) {
$db = db();
$db->beginTransaction();
try {
// Get invoice details
$stmt = $db->prepare("SELECT type FROM invoices WHERE id = ?");
$stmt->execute([$id]);
$type = $stmt->fetchColumn();
// Get items to restore stock
$stmt = $db->prepare("SELECT item_id, quantity FROM invoice_items WHERE invoice_id = ?");
$stmt->execute([$id]);
$items = $stmt->fetchAll();
foreach ($items as $item) {
if ($type === 'sale') {
$stmt = $db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity + ? WHERE id = ?");
} else {
$stmt = $db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity - ? WHERE id = ?");
}
$stmt->execute([$item['quantity'], $item['item_id']]);
}
$stmt = $db->prepare("DELETE FROM invoice_items WHERE invoice_id = ?");
$stmt->execute([$id]);
$stmt = $db->prepare("DELETE FROM invoices WHERE id = ?");
$stmt->execute([$id]);
$db->commit();
$message = "Invoice deleted successfully and stock restored!";
} catch (Exception $e) {
$db->rollBack();
$message = "Error: " . $e->getMessage();
}
}
}
if (isset($_POST['edit_invoice'])) {
$invoice_id = (int)$_POST['invoice_id'];
$customer_id = $_POST['customer_id'] ?: null;
$invoice_date = $_POST['invoice_date'] ?: date('Y-m-d');
$payment_type = $_POST['payment_type'] ?? 'cash';
$item_ids = $_POST['item_ids'] ?? [];
$quantities = $_POST['quantities'] ?? [];
$prices = $_POST['prices'] ?? [];
if ($invoice_id && !empty($item_ids)) {
$db = db();
$db->beginTransaction();
try {
// Get old invoice type and items to revert stock
$stmt = $db->prepare("SELECT type FROM invoices WHERE id = ?");
$stmt->execute([$invoice_id]);
$type = $stmt->fetchColumn();
$stmt = $db->prepare("SELECT item_id, quantity FROM invoice_items WHERE invoice_id = ?");
$stmt->execute([$invoice_id]);
$old_items = $stmt->fetchAll();
foreach ($old_items as $item) {
if ($type === 'sale') {
$stmt = $db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity + ? WHERE id = ?");
} else {
$stmt = $db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity - ? WHERE id = ?");
}
$stmt->execute([$item['quantity'], $item['item_id']]);
}
// Delete old items
$stmt = $db->prepare("DELETE FROM invoice_items WHERE invoice_id = ?");
$stmt->execute([$invoice_id]);
// Calculate new totals
$subtotal = 0;
$total_vat = 0;
$items_data = [];
foreach ($item_ids as $index => $item_id) {
$qty = (float)$quantities[$index];
$price = (float)$prices[$index];
$stmtVat = $db->prepare("SELECT vat_rate FROM stock_items WHERE id = ?");
$stmtVat->execute([$item_id]);
$vat_rate = (float)$stmtVat->fetchColumn();
$line_total = $qty * $price;
$line_vat = $line_total * ($vat_rate / 100);
$subtotal += $line_total;
$total_vat += $line_vat;
$items_data[] = [
'id' => $item_id,
'qty' => $qty,
'price' => $price,
'total' => $line_total
];
}
$total_with_vat = $subtotal + $total_vat;
// Update invoice
$stmt = $db->prepare("UPDATE invoices SET customer_id = ?, invoice_date = ?, payment_type = ?, total_amount = ?, vat_amount = ?, total_with_vat = ? WHERE id = ?");
$stmt->execute([$customer_id, $invoice_date, $payment_type, $subtotal, $total_vat, $total_with_vat, $invoice_id]);
// Insert new items and update stock
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']]);
if ($type === 'sale') {
$stmt = $db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity - ? WHERE id = ?");
} else {
$stmt = $db->prepare("UPDATE stock_items SET stock_quantity = stock_quantity + ? WHERE id = ?");
}
$stmt->execute([$item['qty'], $item['id']]);
}
$db->commit();
$message = "Invoice #$invoice_id updated successfully!";
} catch (Exception $e) {
$db->rollBack();
$message = "Error: " . $e->getMessage();
}
}
}
if (isset($_POST['add_payment_method'])) {
$name_en = $_POST['name_en'] ?? '';
$name_ar = $_POST['name_ar'] ?? '';
if ($name_en && $name_ar) {
$stmt = db()->prepare("INSERT INTO payment_methods (name_en, name_ar) VALUES (?, ?)");
$stmt->execute([$name_en, $name_ar]);
$message = "Payment method added successfully!";
}
}
if (isset($_POST['edit_payment_method'])) {
$id = (int)$_POST['id'];
$name_en = $_POST['name_en'] ?? '';
$name_ar = $_POST['name_ar'] ?? '';
if ($id && $name_en && $name_ar) {
$stmt = db()->prepare("UPDATE payment_methods SET name_en = ?, name_ar = ? WHERE id = ?");
$stmt->execute([$name_en, $name_ar, $id]);
$message = "Payment method updated successfully!";
}
}
if (isset($_POST['delete_payment_method'])) {
$id = (int)$_POST['id'];
if ($id) {
$stmt = db()->prepare("DELETE FROM payment_methods WHERE id = ?");
$stmt->execute([$id]);
$message = "Payment method deleted successfully!";
}
}
if (isset($_POST['update_settings'])) {
foreach ($_POST['settings'] as $key => $value) {
$stmt = db()->prepare("INSERT INTO settings (`key`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = ?");
$stmt->execute([$key, $value, $value]);
}
if (isset($_FILES['company_logo']) && $_FILES['company_logo']['error'] === UPLOAD_ERR_OK) {
$ext = pathinfo($_FILES['company_logo']['name'], PATHINFO_EXTENSION);
$filename = 'logo.' . $ext;
$target = 'uploads/' . $filename;
if (!is_dir('uploads')) mkdir('uploads', 0775, true);
if (move_uploaded_file($_FILES['company_logo']['tmp_name'], $target)) {
$stmt = db()->prepare("INSERT INTO settings (`key`, `value`) VALUES ('company_logo', ?) ON DUPLICATE KEY UPDATE `value` = ?");
$stmt->execute([$target, $target]);
}
}
if (isset($_FILES['favicon']) && $_FILES['favicon']['error'] === UPLOAD_ERR_OK) {
$ext = pathinfo($_FILES['favicon']['name'], PATHINFO_EXTENSION);
$filename = 'favicon.' . $ext;
$target = 'uploads/' . $filename;
if (!is_dir('uploads')) mkdir('uploads', 0775, true);
if (move_uploaded_file($_FILES['favicon']['tmp_name'], $target)) {
$stmt = db()->prepare("INSERT INTO settings (`key`, `value`) VALUES ('favicon', ?) ON DUPLICATE KEY UPDATE `value` = ?");
$stmt->execute([$target, $target]);
}
}
if (isset($_FILES['manager_signature']) && $_FILES['manager_signature']['error'] === UPLOAD_ERR_OK) {
$ext = pathinfo($_FILES['manager_signature']['name'], PATHINFO_EXTENSION);
$filename = 'signature.' . $ext;
$target = 'uploads/' . $filename;
if (!is_dir('uploads')) mkdir('uploads', 0775, true);
if (move_uploaded_file($_FILES['manager_signature']['tmp_name'], $target)) {
$stmt = db()->prepare("INSERT INTO settings (`key`, `value`) VALUES ('manager_signature', ?) ON DUPLICATE KEY UPDATE `value` = ?");
$stmt->execute([$target, $target]);
}
}
$message = "Settings updated successfully!";
}
}
// Routing & Data Fetching
$page = $_GET['page'] ?? 'dashboard';
$data = [];
// Global data for modals
$data['categories'] = db()->query("SELECT * FROM stock_categories ORDER BY name_en ASC")->fetchAll();
$data['units'] = db()->query("SELECT * FROM stock_units ORDER BY name_en ASC")->fetchAll();
$data['suppliers'] = db()->query("SELECT * FROM customers WHERE type = 'supplier' ORDER BY name ASC")->fetchAll();
$settings_raw = db()->query("SELECT * FROM settings")->fetchAll();
$data['settings'] = [];
foreach ($settings_raw as $s) {
$data['settings'][$s['key']] = $s['value'];
}
switch ($page) {
case 'suppliers':
$data['customers'] = db()->query("SELECT * FROM customers WHERE type = 'supplier' ORDER BY id DESC")->fetchAll();
break;
case 'customers':
$data['customers'] = db()->query("SELECT * FROM customers WHERE type = 'customer' ORDER BY id DESC")->fetchAll();
break;
case 'categories':
// Already fetched globally
break;
case 'units':
// Already fetched globally
break;
case 'items':
$data['items'] = db()->query("SELECT i.*, c.name_en as cat_en, c.name_ar as cat_ar, u.short_name_en as unit_en, u.short_name_ar as unit_ar, s.name as supplier_name
FROM stock_items i
LEFT JOIN stock_categories c ON i.category_id = c.id
LEFT JOIN stock_units u ON i.unit_id = u.id
LEFT JOIN customers s ON i.supplier_id = s.id
ORDER BY i.id DESC")->fetchAll();
break;
case 'payment_methods':
$data['payment_methods'] = db()->query("SELECT * FROM payment_methods ORDER BY id DESC")->fetchAll();
break;
case 'settings':
// Already fetched globally
break;
case 'sales':
case 'purchases':
$type = ($page === 'sales') ? 'sale' : 'purchase';
$data['invoices'] = db()->query("SELECT v.*, c.name as customer_name
FROM invoices v
LEFT JOIN customers c ON v.customer_id = c.id
WHERE v.type = '$type'
ORDER BY v.id DESC")->fetchAll();
$data['items_list'] = db()->query("SELECT id, name_en, name_ar, sale_price, purchase_price, stock_quantity, vat_rate FROM stock_items ORDER BY name_en ASC")->fetchAll();
$data['customers_list'] = db()->query("SELECT id, name FROM customers WHERE type = '" . ($type === 'sale' ? 'customer' : 'supplier') . "' ORDER BY name ASC")->fetchAll();
break;
default:
$data['customers'] = db()->query("SELECT * FROM customers WHERE type = 'customer' ORDER BY id DESC LIMIT 5")->fetchAll();
// Dashboard stats
$data['stats'] = [
'total_customers' => db()->query("SELECT COUNT(*) FROM customers WHERE type = 'customer'")->fetchColumn(),
'total_items' => db()->query("SELECT COUNT(*) FROM stock_items")->fetchColumn(),
];
break;
}
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
?>
Accounting Admin
['en' => 'Dashboard', 'ar' => 'لوحة القيادة'],
'customers' => ['en' => 'Customers', 'ar' => 'العملاء'],
'suppliers' => ['en' => 'Suppliers', 'ar' => 'الموردون'],
'categories' => ['en' => 'Stock Categories', 'ar' => 'فئات المخزون'],
'units' => ['en' => 'Stock Units', 'ar' => 'وحدات المخزون'],
'items' => ['en' => 'Stock Items', 'ar' => 'أصناف المخزون'],
'payment_methods' => ['en' => 'Payment Methods', 'ar' => 'طرق الدفع'],
'sales' => ['en' => 'Sales Invoices', 'ar' => 'فواتير المبيعات'],
'purchases' => ['en' => 'Purchase Invoices', 'ar' => 'فواتير المشتريات'],
'settings' => ['en' => 'Company Profile', 'ar' => 'ملف الشركة'],
];
$currTitle = $titles[$page] ?? $titles['dashboard'];
?>
= $currTitle['en'] ?>
= $message ?>
Total Customers
= $data['stats']['total_customers'] ?>
Total Items
= $data['stats']['total_items'] ?>
Recent Customers
View All
Name
Phone
Balance
= htmlspecialchars($c['name']) ?>
= htmlspecialchars($c['phone']) ?>
$= number_format((float)$c['balance'], 2) ?>
= $currTitle['en'] ?> Management
Import Excel
Add = $currTitle['en'] ?>
Name
Email
Phone
Balance
Actions
= htmlspecialchars($c['name']) ?>
= htmlspecialchars($c['email']) ?>
= htmlspecialchars($c['phone']) ?>
$= number_format((float)$c['balance'], 2) ?>
Stock Categories
Import Excel
Add Category
ID
Name (EN)
Name (AR)
= $cat['id'] ?>
= htmlspecialchars($cat['name_en']) ?>
= htmlspecialchars($cat['name_ar']) ?>
Stock Units
Import Excel
Add Unit
Name (EN)
Short (EN)
Name (AR)
Short (AR)
= htmlspecialchars($u['name_en']) ?>
= htmlspecialchars($u['short_name_en']) ?>
= htmlspecialchars($u['name_ar']) ?>
= htmlspecialchars($u['short_name_ar']) ?>
Stock Items
Import Excel
Add Item
Image
SKU
Name
Category
Supplier
Stock Level
Expiry
VAT
Actions
= htmlspecialchars($item['sku']) ?>
= htmlspecialchars($item['name_en']) ?>
= htmlspecialchars($item['name_ar']) ?>
= htmlspecialchars($item['cat_en']) ?>
= htmlspecialchars($item['supplier_name'] ?? '---') ?>
= number_format((float)$item['stock_quantity'], 2) ?>
Min: = number_format((float)$item['min_stock_level'], 2) ?>
Low Stock
= $item['expiry_date'] ?: '---' ?>
= number_format((float)$item['vat_rate'], 2) ?>%
SKU = htmlspecialchars($item['sku'] ?: '---') ?>
Category = htmlspecialchars($item['cat_en'] ?: '---') ?>
Supplier = htmlspecialchars($item['supplier_name'] ?? '---') ?>
Unit = htmlspecialchars($item['unit_en'] ?: '---') ?>
Purchase Price $= number_format((float)$item['purchase_price'], 2) ?>
Sale Price $= number_format((float)$item['sale_price'], 2) ?>
Stock = number_format((float)$item['stock_quantity'], 2) ?>
VAT Rate = number_format((float)$item['vat_rate'], 2) ?>%
Min Stock = number_format((float)$item['min_stock_level'], 2) ?>
Expiry Date = $item['expiry_date'] ?: '---' ?>
= $currTitle['en'] ?>
Create New Invoice
Invoice #
Date
= $page === 'sales' ? 'Customer' : 'Supplier' ?>
Total Amount
Actions
prepare("SELECT ii.*, i.name_en, i.name_ar, i.vat_rate
FROM invoice_items ii
JOIN stock_items i ON ii.item_id = i.id
WHERE ii.invoice_id = ?");
$items->execute([$inv['id']]);
$inv['items'] = $items->fetchAll(PDO::FETCH_ASSOC);
?>
INV-= str_pad((string)$inv['id'], 5, '0', STR_PAD_LEFT) ?>
= $inv['invoice_date'] ?>
= htmlspecialchars($inv['customer_name'] ?? '---') ?>
$= number_format((float)$inv['total_amount'], 2) ?>
= htmlspecialchars($data['settings']['company_name'] ?? 'My Company') ?>
= nl2br(htmlspecialchars($data['settings']['company_address'] ?? '')) ?>
Phone : = htmlspecialchars($data['settings']['company_phone']) ?>
Email : = htmlspecialchars($data['settings']['company_email']) ?>
VAT No : = htmlspecialchars($data['settings']['vat_number']) ?>
CR No : = htmlspecialchars($data['settings']['cr_number']) ?>
Reg No : = htmlspecialchars($data['settings']['registration_no']) ?>
Description
Qty
Price
Total
Subtotal
VAT Amount
Total (Inc. VAT)
= htmlspecialchars($data['settings']['manager_name'] ?? '') ?>
Authorized Manager
Bank Details :
= htmlspecialchars($data['settings']['bank_name'] ?? '') ?> -
IBAN : = htmlspecialchars($data['settings']['company_iban'] ?? '') ?>
= nl2br(htmlspecialchars($data['settings']['invoice_footer'] ?? 'Thank you for your business!')) ?>
Payment Methods
Add Payment Method
ID
Name (EN)
Name (AR)
Actions
= $pm['id'] ?>
= htmlspecialchars($pm['name_en'] ?? '') ?>
= htmlspecialchars($pm['name_ar'] ?? '') ?>