updating promotion
This commit is contained in:
parent
01855f99eb
commit
41c061c036
53
index.php
53
index.php
@ -21,6 +21,24 @@ function numberToWordsOMR($number) {
|
||||
return $result . " Only";
|
||||
}
|
||||
|
||||
function getPromotionalPrice($item) {
|
||||
$price = (float)$item['sale_price'];
|
||||
if (isset($item['is_promotion']) && $item['is_promotion']) {
|
||||
$today = date('Y-m-d');
|
||||
$start = !empty($item['promotion_start']) ? $item['promotion_start'] : null;
|
||||
$end = !empty($item['promotion_end']) ? $item['promotion_end'] : null;
|
||||
|
||||
$active = true;
|
||||
if ($start && $today < $start) $active = false;
|
||||
if ($end && $today > $end) $active = false;
|
||||
|
||||
if ($active) {
|
||||
$price = $price * (1 - (float)$item['promotion_percent'] / 100);
|
||||
}
|
||||
}
|
||||
return $price;
|
||||
}
|
||||
|
||||
function numberToWords($num) {
|
||||
$num = (int)$num;
|
||||
if ($num === 0) return "Zero";
|
||||
@ -37,9 +55,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['action'])) {
|
||||
if ($_GET['action'] === 'search_items') {
|
||||
header('Content-Type: application/json');
|
||||
$q = $_GET['q'] ?? '';
|
||||
$stmt = db()->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 = db()->prepare("SELECT id, name_en, name_ar, sku, sale_price, purchase_price, stock_quantity, vat_rate, is_promotion, promotion_start, promotion_end, promotion_percent 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());
|
||||
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
foreach ($results as &$item) {
|
||||
$item['sale_price'] = getPromotionalPrice($item);
|
||||
}
|
||||
echo json_encode($results);
|
||||
exit;
|
||||
}
|
||||
if ($_GET['action'] === 'get_payments') {
|
||||
@ -1780,7 +1802,11 @@ switch ($page) {
|
||||
ORDER BY q.id DESC");
|
||||
$stmt->execute($params);
|
||||
$data['quotations'] = $stmt->fetchAll();
|
||||
$data['items_list'] = db()->query("SELECT id, name_en, name_ar, sale_price, purchase_price, stock_quantity, vat_rate, is_promotion, promotion_start, promotion_end, promotion_percent FROM stock_items ORDER BY name_en ASC")->fetchAll();
|
||||
$items_list_raw = db()->query("SELECT id, name_en, name_ar, sale_price, purchase_price, stock_quantity, vat_rate, is_promotion, promotion_start, promotion_end, promotion_percent FROM stock_items ORDER BY name_en ASC")->fetchAll(PDO::FETCH_ASSOC);
|
||||
foreach ($items_list_raw as &$item) {
|
||||
$item['sale_price'] = getPromotionalPrice($item);
|
||||
}
|
||||
$data['items_list'] = $items_list_raw;
|
||||
$data['customers_list'] = db()->query("SELECT id, name FROM customers WHERE type = 'customer' ORDER BY name ASC")->fetchAll();
|
||||
break;
|
||||
case 'payment_methods':
|
||||
@ -1830,7 +1856,11 @@ switch ($page) {
|
||||
}
|
||||
unset($inv);
|
||||
|
||||
$data['items_list'] = db()->query("SELECT id, name_en, name_ar, sale_price, purchase_price, stock_quantity, vat_rate, is_promotion, promotion_start, promotion_end, promotion_percent FROM stock_items ORDER BY name_en ASC")->fetchAll();
|
||||
$items_list_raw = db()->query("SELECT id, name_en, name_ar, sale_price, purchase_price, stock_quantity, vat_rate, is_promotion, promotion_start, promotion_end, promotion_percent FROM stock_items ORDER BY name_en ASC")->fetchAll(PDO::FETCH_ASSOC);
|
||||
foreach ($items_list_raw as &$item) {
|
||||
$item['sale_price'] = getPromotionalPrice($item);
|
||||
}
|
||||
$data['items_list'] = $items_list_raw;
|
||||
$data['customers_list'] = db()->query("SELECT id, name FROM customers WHERE type = '" . ($type === 'sale' ? 'customer' : 'supplier') . "' ORDER BY name ASC")->fetchAll();
|
||||
break;
|
||||
|
||||
@ -3172,7 +3202,13 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
|
||||
<?php elseif ($page === 'pos'): ?>
|
||||
<?php
|
||||
$products = db()->query("SELECT * FROM stock_items WHERE stock_quantity > 0 ORDER BY name_en ASC")->fetchAll(PDO::FETCH_ASSOC);
|
||||
$products_raw = db()->query("SELECT * FROM stock_items WHERE stock_quantity > 0 ORDER BY name_en ASC")->fetchAll(PDO::FETCH_ASSOC);
|
||||
$products = [];
|
||||
foreach ($products_raw as $p) {
|
||||
$p['original_price'] = (float)$p['sale_price'];
|
||||
$p['sale_price'] = getPromotionalPrice($p);
|
||||
$products[] = $p;
|
||||
}
|
||||
$customers = db()->query("SELECT * FROM customers WHERE type = 'customer' ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC);
|
||||
?>
|
||||
<div class="pos-container">
|
||||
@ -3204,7 +3240,12 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Accounting System';
|
||||
<div class="fw-bold mb-1 product-name" data-en="<?= htmlspecialchars($p['name_en']) ?>" data-ar="<?= htmlspecialchars($p['name_ar']) ?>"><?= htmlspecialchars($p['name_en']) ?></div>
|
||||
<div class="small text-muted mb-2"><?= htmlspecialchars($p['sku']) ?></div>
|
||||
<div class="d-flex justify-content-between align-items-center mt-auto">
|
||||
<span class="price text-primary">OMR <?= number_format((float)$p['sale_price'], 3) ?></span>
|
||||
<div class="d-flex flex-column">
|
||||
<?php if ($p['sale_price'] < $p['original_price']): ?>
|
||||
<span class="text-muted smaller text-decoration-line-through">OMR <?= number_format($p['original_price'], 3) ?></span>
|
||||
<?php endif; ?>
|
||||
<span class="price text-primary fw-bold">OMR <?= number_format((float)$p['sale_price'], 3) ?></span>
|
||||
</div>
|
||||
<span class="badge bg-light text-dark small"><?= (float)$p['stock_quantity'] ?> left</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user