2026-04-09 10:12:19 +00:00

117 lines
4.8 KiB
JavaScript

document.addEventListener('DOMContentLoaded', () => {
if (window.bootstrap) {
document.querySelectorAll('.toast').forEach((element) => {
const toast = new bootstrap.Toast(element);
toast.show();
});
}
const catalogNode = document.getElementById('catalog-data');
const customerSelect = document.getElementById('customer_id');
const productSelect = document.getElementById('product_id');
const qtyInput = document.getElementById('qty');
const submitButton = document.getElementById('submit-order-btn');
const hintBox = document.getElementById('sales-order-hint');
if (!catalogNode || !customerSelect || !productSelect || !qtyInput) {
return;
}
let catalog = {};
try {
catalog = JSON.parse(catalogNode.textContent || '{}');
} catch (error) {
console.error('Failed to parse catalog JSON', error);
}
const summaryCustomer = document.getElementById('summary-customer');
const summaryBranch = document.getElementById('summary-branch');
const summaryPrice = document.getElementById('summary-price');
const summaryStock = document.getElementById('summary-stock');
const summaryMargin = document.getElementById('summary-margin');
const summaryTotal = document.getElementById('summary-total');
const formatMoney = (value) => `${Number(value).toFixed(2)} ر.س`;
const getSelectedCustomer = () => catalog[customerSelect.value] || null;
const getSelectedProduct = () => {
const current = getSelectedCustomer();
if (!current) return null;
return current.items.find((item) => String(item.product_id) === productSelect.value) || null;
};
const resetSummary = () => {
summaryCustomer.textContent = '—';
summaryBranch.textContent = '—';
summaryPrice.textContent = '—';
summaryStock.textContent = '—';
summaryMargin.textContent = '—';
summaryTotal.textContent = '—';
};
const updateProducts = () => {
const current = getSelectedCustomer();
productSelect.innerHTML = '';
if (!current) {
productSelect.disabled = true;
productSelect.innerHTML = '<option value="">اختر العميل أولاً</option>';
resetSummary();
hintBox.textContent = 'اختر عميلاً لعرض الأصناف المسموح بها له. إذا كانت الكمية المطلوبة أكبر من المتاح سيظهر تنبيه ويتم تعطيل الإرسال.';
submitButton.disabled = false;
return;
}
summaryCustomer.textContent = current.customer_name;
summaryBranch.textContent = current.branch_name;
productSelect.disabled = false;
productSelect.innerHTML = '<option value="">اختر الصنف</option>';
current.items.forEach((item) => {
const option = document.createElement('option');
option.value = item.product_id;
option.textContent = `${item.product_name}${item.sku}`;
productSelect.appendChild(option);
});
resetCalculatedFields();
};
const resetCalculatedFields = () => {
summaryPrice.textContent = '—';
summaryStock.textContent = '—';
summaryMargin.textContent = '—';
summaryTotal.textContent = '—';
submitButton.disabled = false;
};
const updateSummary = () => {
const product = getSelectedProduct();
const qty = Number(qtyInput.value || 0);
if (!product) {
resetCalculatedFields();
return;
}
const price = Number(product.special_price || 0);
const margin = price - Number(product.cost_price || 0);
const total = qty > 0 ? qty * price : 0;
summaryPrice.textContent = formatMoney(price);
summaryStock.textContent = `${Number(product.stock_qty).toFixed(0)} ${product.unit}`;
summaryMargin.textContent = formatMoney(margin);
summaryTotal.textContent = formatMoney(total);
if (qty > Number(product.stock_qty)) {
hintBox.textContent = 'الكمية المطلوبة تتجاوز المخزون الحالي. خفّض الكمية أو حدّث المخزون أولاً.';
hintBox.className = 'alert alert-warning border mt-4 mb-0 small';
submitButton.disabled = true;
} else {
hintBox.textContent = 'السعر الخاص تم تطبيقه تلقائياً بناءً على العميل والصنف المحدد.';
hintBox.className = 'alert alert-light border mt-4 mb-0 small';
submitButton.disabled = false;
}
};
customerSelect.addEventListener('change', updateProducts);
productSelect.addEventListener('change', updateSummary);
qtyInput.addEventListener('input', updateSummary);
updateProducts();
});