Materials are now global and category-free. The category lives only on the supplier (suppliers.category_id); every category-facing query goes through the supplier edge instead of the material row.
Schema (migration.sql + init_db DDL):
- drop the materials.category_id column, its FK, and the v2.5 first-link-wins automation (fn_derive_material_category + trg_supplier_materials_derive_category) — the DDL runs idempotently at API startup
Backend:
- GET /materials?category_id=… now filters via EXISTS(supplier_materials ⋈ suppliers.category_id)
- quote category filters (quotes list) use the quote's supplier category
- /pm/on-the-way derives the display category from the supplier of the quote
- supplier_materials links no longer derive/return a material category
- expand_service: material expand=category resolves to null (kept for contract compatibility)
- material create/update no longer validates or stores category_id; seed.py aligned
Frontend:
- material creation no longer sends category_id
- formatMaterialTitle reworked to a strict order with « . » separator: [Brand] . [Material] . [Spec 1] . [Spec 2], brand deduped when the stored name already starts with it; specs separator switched from « · » to « . »
Validated: python compile, api + frontend image builds (tsc + vite) clean; branch cut from the tip of prod_v4 (= origin/production, 5a9ef40 — the team's merge made them identical).
349 lines
13 KiB
TypeScript
349 lines
13 KiB
TypeScript
// ============================================================
|
|
// منطق بیزنس مشترک پیشنهادها — پورت از app.js
|
|
// ============================================================
|
|
|
|
import type { LineItem, Material, Quote, StandardConversion, Store, Supplier, UnitConversion } from "./types";
|
|
import { normalize } from "./normalize";
|
|
import { groupCommas } from "./numutil";
|
|
import { fmtDate } from "./formatters";
|
|
|
|
export function supplierById(suppliers: Supplier[], id: string): Supplier {
|
|
return suppliers.find((x) => x.id === id) || { id: "", name: "؟", location: "", topic: "", categories: [], phone: "", contact: "" };
|
|
}
|
|
|
|
export function materialById(materials: Material[], id: string): Material {
|
|
return (
|
|
materials.find((x) => x.id === id) || {
|
|
id: "",
|
|
name: "",
|
|
topic: "",
|
|
category: "؟",
|
|
brand: "",
|
|
specs: [],
|
|
base_unit: "",
|
|
}
|
|
);
|
|
}
|
|
|
|
// spec chips یک material (دادههای قدیمی ممکن است spec رشتهای داشته باشند)
|
|
export function materialSpecs(m: Material | null | undefined): string[] {
|
|
return (m && m.specs) || [];
|
|
}
|
|
|
|
// کلید متعارف یک مجموعه spec — ترتیب مهم نیست، متن نرمالشده
|
|
export function specsKey(specs: string[]): string {
|
|
return (specs || []).map((s) => normalize(s)).filter(Boolean).sort().join("|");
|
|
}
|
|
|
|
// specها با جداکنندهی « . »
|
|
export function specsLabel(specs: string[]): string {
|
|
return specs.join(" . ");
|
|
}
|
|
|
|
/**
|
|
* عنوان تمیز و بدون تکرار کالا:
|
|
* ترتیب اکید با جداکنندهی نقطه:
|
|
* [نام برند] . [نام متریال] . [مشخصه ۱] . [مشخصه ۲]
|
|
*/
|
|
export function formatMaterialTitle(
|
|
m: Pick<Material, "name" | "brand" | "specs" | "category"> | null | undefined,
|
|
extraSpecs?: string[],
|
|
): string {
|
|
if (!m) return "کالا";
|
|
let rawName = (m.name || "").trim();
|
|
const rawBrand = (m.brand || "").trim();
|
|
const rawSpecs = [...(m.specs || []), ...(extraSpecs || [])]
|
|
.map((s) => (s || "").trim())
|
|
.filter(Boolean);
|
|
|
|
const normBrand = normalize(rawBrand);
|
|
|
|
// اگر نام کالا با نام برند شروع شده باشد، برند را از ابتدای نام برمیداریم تا تکرار نشود
|
|
if (normBrand && rawName) {
|
|
const normName = normalize(rawName);
|
|
if (normName.startsWith(normBrand)) {
|
|
const rest = rawName.slice(rawBrand.length).trim();
|
|
rawName = rest.replace(/^[.\s·\-]+/, "").trim();
|
|
}
|
|
}
|
|
|
|
// قطعات نهایی با ترتیب مشخص: [Brand Name] . [Material Name] . [Specification 1] . [Specification 2]
|
|
const parts: string[] = [];
|
|
if (rawBrand) {
|
|
parts.push(rawBrand);
|
|
}
|
|
if (rawName) {
|
|
parts.push(rawName);
|
|
} else if (!rawBrand) {
|
|
parts.push(m.category && m.category !== "؟" ? m.category : "کالا");
|
|
}
|
|
|
|
// اضافه کردن مشخصهها بدون تکرار
|
|
const normCurrent = normalize(parts.join(" "));
|
|
const seenNormSpecs = new Set<string>();
|
|
|
|
for (const s of rawSpecs) {
|
|
const ns = normalize(s);
|
|
if (!ns || seenNormSpecs.has(ns)) continue;
|
|
seenNormSpecs.add(ns);
|
|
|
|
// اگر مشخصه قبلاً داخل نام یا برند آمده باشد، تکرار نمیکنیم
|
|
if (normCurrent.includes(ns)) continue;
|
|
parts.push(s);
|
|
}
|
|
|
|
return parts.join(" . ");
|
|
}
|
|
|
|
export function materialLabel(m: Pick<Material, "name" | "brand" | "specs" | "category"> | null | undefined): string {
|
|
return formatMaterialTitle(m);
|
|
}
|
|
|
|
// آرایهی termsheet_ids یک قلم (هر دو فرمت قدیم/جدید)
|
|
export function itemTermsheetIds(li: LineItem): string[] {
|
|
if (li.termsheet_ids) return li.termsheet_ids;
|
|
return [];
|
|
}
|
|
|
|
// اقلام داخل معامله (ردشدهها نگه داشته میشوند ولی هرگز شمرده نمیشوند)
|
|
export function activeItems(q: Quote): LineItem[] {
|
|
return (q.line_items || []).filter((li) => !li.rejected);
|
|
}
|
|
|
|
// جمع کل اقلام فعال (total_price اگر هست، وگرنه unit*qty)
|
|
export function quoteTotal(q: Quote): number {
|
|
return activeItems(q).reduce((sum, li) => {
|
|
const t =
|
|
li.total_price != null
|
|
? li.total_price
|
|
: li.unit_price != null && li.quantity != null
|
|
? li.unit_price * li.quantity
|
|
: 0;
|
|
return sum + (t || 0);
|
|
}, 0);
|
|
}
|
|
|
|
// ترمشیت مؤثر: پذیرفتهشده، وگرنه اولین offered
|
|
export function effectiveTermsheet(q: Quote): Quote["termsheets"][number] | null {
|
|
if (!q.termsheets || !q.termsheets.length) return null;
|
|
return q.termsheets.find((t) => t.status === "accepted") || q.termsheets[0];
|
|
}
|
|
|
|
// id ترمشیتهایی که واقعاً توسط اقلام فعال ارجاع شدهاند
|
|
export function usedTermsheetIds(q: Quote): string[] {
|
|
const ids = new Set<string>();
|
|
activeItems(q).forEach((li) => {
|
|
itemTermsheetIds(li).forEach((tid) => {
|
|
if (tid) ids.add(tid);
|
|
});
|
|
});
|
|
return [...ids];
|
|
}
|
|
|
|
// مجموع هر قلم
|
|
export function lineItemTotal(li: LineItem): number | null {
|
|
if (li.total_price != null) return li.total_price;
|
|
if (li.unit_price != null && li.quantity != null) return li.unit_price * li.quantity;
|
|
return null;
|
|
}
|
|
|
|
// پروژههای یک قلم:
|
|
// - در وضعیت offered: تمام پروژههای کاندیدا (line_item_projects)
|
|
// - در وضعیت accepted یا arrived: فقط تک پروژهی انتخابشده در خرید (item_decisions.project_id)
|
|
export function itemProjects(q: Quote, li: LineItem): string[] {
|
|
if (q.status === "accepted" || q.status === "arrived") {
|
|
return li.target_project?.name ? [li.target_project.name] : [];
|
|
}
|
|
// For 'offered', 'rejected', 'expired': return all candidate projects
|
|
return (li.projects || []).map((p) => (typeof p === "string" ? p : p.name)).filter(Boolean);
|
|
}
|
|
|
|
// ---------- واحد مرجع و ضریب تبدیل ----------
|
|
|
|
// واحد مرجع یک دسته
|
|
export function refUnitForCategory(store: Pick<Store, "categoryUnits">, category: string): string {
|
|
const cu = (store.categoryUnits || []).find((c) => normalize(c.category) === normalize(category));
|
|
return cu ? cu.ref_unit : "";
|
|
}
|
|
|
|
// ضریب تبدیل با اولویت: دستهای (برند مشخص > همه برندها) > تبدیل ثابت جهانی
|
|
export function findConversion(
|
|
store: Pick<Store, "unitConversions" | "standardConversions">,
|
|
category: string,
|
|
brand: string,
|
|
fromUnit: string,
|
|
toUnit: string,
|
|
): UnitConversion | StandardConversion | null {
|
|
const list = (store.unitConversions || []).filter(
|
|
(c) =>
|
|
normalize(c.category) === normalize(category) &&
|
|
normalize(c.from_unit) === normalize(fromUnit) &&
|
|
normalize(c.to_unit) === normalize(toUnit) &&
|
|
(!c.brand || normalize(c.brand) === normalize(brand)),
|
|
);
|
|
list.sort((a, b) => (b.brand ? 1 : 0) - (a.brand ? 1 : 0));
|
|
if (list[0]) return list[0];
|
|
return (
|
|
(store.standardConversions || []).find(
|
|
(c) => normalize(c.from_unit) === normalize(fromUnit) && normalize(c.to_unit) === normalize(toUnit),
|
|
) || null
|
|
);
|
|
}
|
|
|
|
// تبدیل یک قلم به واحد مرجع دسته؛ null وقتی ضریبی نیست یا واحد از قبل مرجع است
|
|
export function refConverted(
|
|
store: Pick<Store, "categoryUnits" | "unitConversions" | "standardConversions" | "materials">,
|
|
li: LineItem,
|
|
): { qty: number; unit: string; perRef: number | null } | null {
|
|
const m = materialById(store.materials, li.material_id);
|
|
const ref = refUnitForCategory(store, m.category);
|
|
if (!ref || !li.unit || normalize(ref) === normalize(li.unit)) return null;
|
|
const conv = findConversion(store, m.category, m.brand, li.unit, ref);
|
|
if (!conv) return null;
|
|
const qty = (li.quantity || 0) * conv.factor;
|
|
const total = li.total_price != null ? li.total_price : (li.unit_price || 0) * (li.quantity || 0);
|
|
const perRef = qty > 0 ? Math.round(total / qty) : null;
|
|
return { qty, unit: ref, perRef };
|
|
}
|
|
|
|
// واحدهای ناشناختهی استفادهشده در یک quote → لینک به داشبورد واحدها
|
|
// واحدها دیگر hardcode نیستند — همه از بکاند میآیند؛ هشداری نمایش داده نمیشود
|
|
export function unitWarning(_store: Store, _q: Quote): { unit: string; cat: string }[] {
|
|
return [];
|
|
}
|
|
|
|
// ستون پروژه:
|
|
// در وضعیتهای پذیرفتهشده یا رسیده، فقط پروژههای هدف خرید اقلام نشان داده میشوند
|
|
export function projectCell(q: Quote): string {
|
|
if (q.status === "accepted" || q.status === "arrived") {
|
|
const targets = (q.line_items || [])
|
|
.filter((li) => !li.rejected)
|
|
.map((li) =>
|
|
li.target_project?.name ||
|
|
(li.projects && li.projects.length === 1
|
|
? typeof li.projects[0] === "string"
|
|
? li.projects[0]
|
|
: li.projects[0].name
|
|
: null),
|
|
)
|
|
.filter(Boolean) as string[];
|
|
const unique = [...new Set(targets)];
|
|
if (unique.length) return unique.join("، ");
|
|
}
|
|
if (q.projects && q.projects.length) return q.projects.join("، ");
|
|
return q.project || "—";
|
|
}
|
|
|
|
// خلاصهی اقلام (ردیف جمعشده)
|
|
export function itemsSummary(store: Store, q: Quote): { text: string; count: number } {
|
|
const items = activeItems(q).map((li) => {
|
|
const m = materialById(store.materials, li.material_id);
|
|
return materialLabel(m);
|
|
});
|
|
return { text: items.join("، "), count: items.length };
|
|
}
|
|
|
|
// ستون معامله — نمایش فشرده برای ردیف جمعشده
|
|
export function termsheetSummary(store: Store, q: Quote): { title: string; sub: string; color: string } {
|
|
if (!q.termsheets || !q.termsheets.length) return { title: "—", sub: "", color: "" };
|
|
const used = usedTermsheetIds(q);
|
|
const accepted = q.termsheets.find((t) => t.status === "accepted");
|
|
if (accepted) {
|
|
const items = activeItems(q).filter((li) => itemTermsheetIds(li).includes(accepted.id));
|
|
const itemsDesc =
|
|
items.length === q.line_items.length
|
|
? "همه اقلام"
|
|
: items
|
|
.map((li) => {
|
|
const m = materialById(store.materials, li.material_id);
|
|
const specText = specsLabel(materialSpecs(m));
|
|
return specText || m.brand;
|
|
})
|
|
.join("، ");
|
|
return {
|
|
title: accepted.label,
|
|
sub: `${itemsDesc} · ${accepted.milestones.length} مرحله`,
|
|
color: "var(--green)",
|
|
};
|
|
}
|
|
if (used.length === 1) {
|
|
return { title: q.termsheets[0].label, sub: `${q.termsheets.length} گزینه`, color: "" };
|
|
}
|
|
return { title: `${used.length} termsheet جدا · ${q.termsheets.length} گزینه`, sub: "", color: "" };
|
|
}
|
|
|
|
// فیلترهای دفتر
|
|
export function filterQuotes(
|
|
store: Store,
|
|
filters: { category: string; supplier: string; status: string; search: string },
|
|
): Quote[] {
|
|
const catVal = filters.category.trim();
|
|
const supVal = filters.supplier.trim();
|
|
const stVal = filters.status.trim();
|
|
const q = filters.search.trim().toLowerCase();
|
|
|
|
const STATUS_BY_LABEL: Record<string, string> = {
|
|
"پیشنهاد شده": "offered",
|
|
"پذیرفته شده": "accepted",
|
|
"رسیده": "arrived",
|
|
};
|
|
const stKey = STATUS_BY_LABEL[stVal] || "";
|
|
|
|
return store.quotes.filter((quote) => {
|
|
const supObj = supplierById(store.suppliers, quote.supplier_id);
|
|
if (supVal && supObj.name !== supVal) return false;
|
|
if (stKey && quote.status !== stKey) return false;
|
|
|
|
const matInfo = activeItems(quote).map((li) => materialById(store.materials, li.material_id));
|
|
if (catVal && !matInfo.some((m) => m.category === catVal)) return false;
|
|
|
|
if (q) {
|
|
const hay =
|
|
`${supObj.name} ${quote.project || ""} ` +
|
|
matInfo.map((m) => `${m.category} ${m.brand} ${materialSpecs(m).join(" ")}`).join(" ");
|
|
if (!hay.toLowerCase().includes(q)) return false;
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
// وضعیت منقضی
|
|
export function isExpired(q: Quote): boolean {
|
|
return (
|
|
q.status === "offered" &&
|
|
!!q.expiry_date &&
|
|
new Date(q.expiry_date) < new Date(new Date().toDateString())
|
|
);
|
|
}
|
|
|
|
// آمادهسازی نمایش ترمشیت در حالت expanded
|
|
export function termSheetBlocks(
|
|
store: Store,
|
|
q: Quote,
|
|
): { ts: Quote["termsheets"][number]; isAccepted: boolean; isUsed: boolean; itemsDesc: string; milestones: string[] }[] {
|
|
const used = usedTermsheetIds(q);
|
|
const active = activeItems(q);
|
|
return q.termsheets.map((ts) => {
|
|
const isAccepted = ts.status === "accepted";
|
|
const isUsed = used.includes(ts.id);
|
|
const items = active.filter((li) => itemTermsheetIds(li).includes(ts.id));
|
|
const itemsDesc =
|
|
items.length === active.length
|
|
? "همه اقلام"
|
|
: items.length
|
|
? items
|
|
.map((li) => {
|
|
const m = materialById(store.materials, li.material_id);
|
|
return specsLabel(materialSpecs(m)) || m.brand;
|
|
})
|
|
.join("، ")
|
|
: "بدون قلم";
|
|
const milestones = ts.milestones.map((m) => {
|
|
const dateLabel = m.when_iso ? fmtDate(m.when_iso) : "?";
|
|
const amt = m.amount != null ? groupCommas(m.amount, true) : "?";
|
|
return `${dateLabel} · ${amt}`;
|
|
});
|
|
return { ts, isAccepted, isUsed, itemsDesc, milestones };
|
|
});
|
|
}
|