update normal sale
This commit is contained in:
parent
52c9aed760
commit
b6bf7b7b5e
@ -44,7 +44,23 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
continue;
|
||||
}
|
||||
$product = $catalog[$sku];
|
||||
$price = (float) $product['price'];
|
||||
$editedName = trim((string) ($item['name'] ?? ''));
|
||||
$finalName = $editedName !== ''
|
||||
? $editedName
|
||||
: trim((string) ($product['name_' . current_lang()] ?? ''));
|
||||
if ($finalName === '') {
|
||||
$finalName = trim((string) ($product['name_ar'] ?? ''));
|
||||
}
|
||||
if ($finalName === '') {
|
||||
$finalName = trim((string) ($product['name_en'] ?? ''));
|
||||
}
|
||||
if ($finalName === '') {
|
||||
$finalName = $sku;
|
||||
}
|
||||
|
||||
$price = isset($item['price']) && is_numeric($item['price'])
|
||||
? max(0, (float) $item['price'])
|
||||
: (float) $product['price'];
|
||||
$lineTotal = $price * $qty;
|
||||
|
||||
$vatPercent = (float) ($product['vat'] ?? 0);
|
||||
@ -54,8 +70,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
$normalized[] = [
|
||||
'sku' => $sku,
|
||||
'name_ar' => $product['name_ar'],
|
||||
'name_en' => $product['name_en'],
|
||||
'name_ar' => $finalName,
|
||||
'name_en' => $finalName,
|
||||
'qty' => $qty,
|
||||
'price' => $price,
|
||||
'line_total' => $lineTotal,
|
||||
@ -260,6 +276,12 @@ require __DIR__ . '/header.php';
|
||||
color: #212529;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
.line-input {
|
||||
min-width: 0;
|
||||
}
|
||||
.line-price-input {
|
||||
min-width: 96px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="container-fluid py-4">
|
||||
@ -600,6 +622,29 @@ function addItemToInvoice(sku) {
|
||||
renderInvoice();
|
||||
}
|
||||
|
||||
function changeItemName(sku, newName) {
|
||||
if (!invoiceItems[sku]) {
|
||||
return;
|
||||
}
|
||||
const fallbackName = '<?= current_lang() ?>' === 'ar'
|
||||
? (catalogData[sku].name_ar || catalogData[sku].name_en || sku)
|
||||
: (catalogData[sku].name_en || catalogData[sku].name_ar || sku);
|
||||
const nextName = String(newName || '').trim();
|
||||
invoiceItems[sku].name = nextName !== '' ? nextName : fallbackName;
|
||||
renderInvoice();
|
||||
}
|
||||
|
||||
function changeItemPrice(sku, newPrice) {
|
||||
if (!invoiceItems[sku]) {
|
||||
return;
|
||||
}
|
||||
const parsed = parseFloat(newPrice);
|
||||
invoiceItems[sku].price = Number.isFinite(parsed) && parsed >= 0
|
||||
? Math.round(parsed * 1000) / 1000
|
||||
: (parseFloat(catalogData[sku].price) || 0);
|
||||
renderInvoice();
|
||||
}
|
||||
|
||||
function changeQty(sku, newQty) {
|
||||
const qty = parseInt(newQty);
|
||||
if (isNaN(qty) || qty < 1) {
|
||||
@ -615,6 +660,12 @@ function removeItem(sku) {
|
||||
renderInvoice();
|
||||
}
|
||||
|
||||
function escapeHtml(value) {
|
||||
return String(value ?? '').replace(/[&<>"']/g, function(char) {
|
||||
return ({'&': '&', '<': '<', '>': '>', '"': '"', "'": '''})[char] || char;
|
||||
});
|
||||
}
|
||||
|
||||
function renderInvoice() {
|
||||
const skus = Object.keys(invoiceItems);
|
||||
if (skus.length === 0) {
|
||||
@ -639,15 +690,20 @@ function renderInvoice() {
|
||||
totalVat += itemVat;
|
||||
|
||||
totalAmount += lineTotal;
|
||||
cartData.push({ sku: item.sku, qty: item.qty });
|
||||
cartData.push({ sku: item.sku, name: item.name, price: item.price, qty: item.qty });
|
||||
|
||||
const safeSku = escapeHtml(item.sku);
|
||||
const safeName = escapeHtml(item.name);
|
||||
|
||||
const tr = document.createElement('tr');
|
||||
tr.innerHTML = `
|
||||
<td>
|
||||
<div class="fw-medium text-dark">${item.name}</div>
|
||||
<div class="text-muted small">SKU: ${item.sku}</div>
|
||||
<input type="text" class="form-control form-control-sm line-input fw-medium mb-2" value="${safeName}" onchange="changeItemName('${sku}', this.value)" onkeyup="if(event.key==='Enter') changeItemName('${sku}', this.value)">
|
||||
<div class="text-muted small">SKU: ${safeSku}</div>
|
||||
</td>
|
||||
<td class="text-center align-middle">
|
||||
<input type="number" class="form-control form-control-sm text-center line-input line-price-input mx-auto" min="0" step="0.001" value="${item.price.toFixed(3)}" onchange="changeItemPrice('${sku}', this.value)" onkeyup="if(event.key==='Enter') changeItemPrice('${sku}', this.value)">
|
||||
</td>
|
||||
<td class="text-center text-muted align-middle">${item.price.toFixed(3)}</td>
|
||||
<td class="text-center align-middle">
|
||||
<input type="number" class="qty-control mx-auto fw-medium" min="1" value="${item.qty}" onchange="changeQty('${sku}', this.value)" onkeyup="if(event.key==='Enter') changeQty('${sku}', this.value)">
|
||||
</td>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user