/* Bonyan ERP — Pricing manager (إدخال يدوي لسعر كل مادة تحت كل نوع تسعير) لا نسب مئوية ولا قيم تُطبّق على الجميع — كل خانة سعر مستقل قابل للتحرير. التغييرات تنعكس على القائمة المنسدلة وأسعار الفاتورة. */ function PricingScreen() { const I = window.BnyIcon; const D = window.BnyData; const { Card, Button, Badge, Input } = window.BonyanDesignSystem_ad3ca0; const isMobile = window.useBnyMobile(); const [types, setTypes] = React.useState(() => D.priceTypes.map((t) => ({ ...t }))); const [cur, setCur] = React.useState("iqd"); const [prices, setPrices] = React.useState(() => { const o = {}; D.materials.forEach((m) => { o[m.sku] = { iqd: { ...(m.prices || {}) }, usd: { ...(m.pricesUsd || {}) } }; }); return o; }); // إبقاء البيانات متاحة لباقي البرنامج (الفاتورة) React.useEffect(() => { D.materials.forEach((m) => { const p = prices[m.sku]; if (p) { m.prices = { ...p.iqd }; m.pricesUsd = { ...p.usd }; } }); D.priceTypes = types.map((t) => ({ ...t })); }, [prices, types]); // الوضع المباشر: احفظ التسعير في Supabase بعد توقف التعديل (debounce) const priceSaved = React.useRef(false); React.useEffect(() => { if (!window.BnyConfigured || !window.BnyDB || !window.BnyDB.savePricing) return; if (!priceSaved.current) { priceSaved.current = true; return; } const t = setTimeout(() => { window.BnyDB.savePricing(D.materials, types).catch((e) => console.warn("بُنيان: فشل حفظ التسعير", e)); }, 800); return () => clearTimeout(t); }, [prices, types]); const sym = window.BnyCur(cur); const fmt = (n) => (n === "" || n == null) ? "" : Number(n).toLocaleString("en-US"); const cell = (sku, tid) => { const p = prices[sku]; return p && p[cur] ? p[cur][tid] : undefined; }; const setCell = (sku, tid, raw) => { const v = raw === "" ? 0 : Number(raw); setPrices((p) => ({ ...p, [sku]: { ...p[sku], [cur]: { ...p[sku][cur], [tid]: Number.isNaN(v) ? 0 : v } } })); }; const addType = () => { const id = "t" + Date.now(); setTypes((ts) => [...ts, { id, name: "نوع جديد" }]); setPrices((p) => { const np = { ...p }; D.materials.forEach((m) => { const c = np[m.sku]; np[m.sku] = { iqd: { ...c.iqd, [id]: c.iqd.main || 0 }, usd: { ...c.usd, [id]: c.usd.main || 0 } }; }); return np; }); }; const renameType = (id, name) => setTypes((ts) => ts.map((t) => (t.id === id ? { ...t, name } : t))); const removeType = (id) => setTypes((ts) => ts.filter((t) => t.id !== id)); const priceInput = (sku, tid, w) => (cur === "usd" ? setCell(sku, tid, e.target.value)} style={{ height: 36, width: w, fontSize: 13, padding: "0 8px" }} /> : setCell(sku, tid, e.target.value.replace(/[^\d]/g, ""))} style={{ height: 36, width: w, fontSize: 13, padding: "0 8px" }} /> ); return (
سعر كل مادة يُدخَل يدويًا تحت كل نوع — قيم مستقلة تمامًا (ليست نسبًا). بدّل العملة لإدخال أسعار الدولار. اسم النوع لا يظهر في الفاتورة المطبوعة.
| المادة | {types.map((t) =>{t.name} | )}
|---|---|
| {m.name} / {m.unit} | {types.map((t) => ({priceInput(m.sku, t.id, 120)} | ))}