Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d69ec7f7a9 | ||
|
|
34290b9371 | ||
|
|
cff35e3660 | ||
|
|
b469238153 | ||
|
|
2fd72fecf7 | ||
|
|
ae62a0391b |
+16
-19
@@ -18,28 +18,9 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
_DASHBOARD_DDL = [
|
_DASHBOARD_DDL = [
|
||||||
# One reference unit per category (units.html «واحد مرجع هر دسته»)
|
# One reference unit per category (units.html «واحد مرجع هر دسته»)
|
||||||
"""
|
|
||||||
CREATE TABLE IF NOT EXISTS category_reference_units (
|
|
||||||
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
|
|
||||||
category_id UUID NOT NULL UNIQUE REFERENCES material_categories(id) ON DELETE CASCADE,
|
|
||||||
ref_unit TEXT NOT NULL,
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
||||||
)
|
|
||||||
""",
|
|
||||||
# Brand-scoped / category-scoped conversions: 1 from_unit = factor × to_unit.
|
# Brand-scoped / category-scoped conversions: 1 from_unit = factor × to_unit.
|
||||||
# UNIQUE must treat brand NULL as a concrete value (PG treats NULLs as distinct).
|
# UNIQUE must treat brand NULL as a concrete value (PG treats NULLs as distinct).
|
||||||
"""
|
"""
|
||||||
CREATE TABLE IF NOT EXISTS unit_conversions (
|
|
||||||
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
|
|
||||||
category_id UUID NOT NULL REFERENCES material_categories(id) ON DELETE CASCADE,
|
|
||||||
brand TEXT,
|
|
||||||
from_unit TEXT NOT NULL,
|
|
||||||
to_unit TEXT NOT NULL,
|
|
||||||
factor NUMERIC(18,6) NOT NULL,
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
||||||
)
|
|
||||||
""",
|
|
||||||
"""
|
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_unit_conversion
|
CREATE UNIQUE INDEX IF NOT EXISTS uq_unit_conversion
|
||||||
ON unit_conversions (category_id, COALESCE(brand, ''), from_unit, to_unit)
|
ON unit_conversions (category_id, COALESCE(brand, ''), from_unit, to_unit)
|
||||||
""",
|
""",
|
||||||
@@ -413,6 +394,22 @@ async def ensure_v26(engine: AsyncEngine) -> None:
|
|||||||
""")
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
async def ensure_drop_category_tables(engine: AsyncEngine) -> None:
|
||||||
|
"""v2.7 follow-up: the category taxonomy is reduced to a supplier label.
|
||||||
|
|
||||||
|
These three tables backed features that were never populated (0 rows on every
|
||||||
|
volume): per-category reference units and conversion factors (the
|
||||||
|
per-reference-unit price line) and the category -> attribute mapping. Dropping
|
||||||
|
them is idempotent and loses no data. material_categories itself stays — every
|
||||||
|
supplier carries one as its label.
|
||||||
|
"""
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.execute(text(
|
||||||
|
"DROP TABLE IF EXISTS category_attributes, category_reference_units, unit_conversions CASCADE"
|
||||||
|
))
|
||||||
|
logger.info("category taxonomy tables dropped (attributes / ref units / conversions)")
|
||||||
|
|
||||||
|
|
||||||
async def ensure_audit_table(engine: AsyncEngine) -> None:
|
async def ensure_audit_table(engine: AsyncEngine) -> None:
|
||||||
"""v2.7: general activity trail (audit_log). Idempotent; safe on any volume."""
|
"""v2.7: general activity trail (audit_log). Idempotent; safe on any volume."""
|
||||||
ddl = """
|
ddl = """
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ from app.db.engine import AsyncSessionLocal, engine
|
|||||||
from app.db.init_db import (
|
from app.db.init_db import (
|
||||||
ensure_attachment_table,
|
ensure_attachment_table,
|
||||||
ensure_audit_table,
|
ensure_audit_table,
|
||||||
|
ensure_drop_category_tables,
|
||||||
ensure_dashboard_tables,
|
ensure_dashboard_tables,
|
||||||
ensure_material_topic_column,
|
ensure_material_topic_column,
|
||||||
ensure_quote_projects,
|
ensure_quote_projects,
|
||||||
@@ -105,6 +106,7 @@ async def lifespan(_app: FastAPI):
|
|||||||
await ensure_quote_projects(engine)
|
await ensure_quote_projects(engine)
|
||||||
await ensure_v26(engine)
|
await ensure_v26(engine)
|
||||||
await ensure_audit_table(engine)
|
await ensure_audit_table(engine)
|
||||||
|
await ensure_drop_category_tables(engine)
|
||||||
task = asyncio.create_task(_auto_expire_loop()) if settings.AUTO_EXPIRE_ENABLED else None
|
task = asyncio.create_task(_auto_expire_loop()) if settings.AUTO_EXPIRE_ENABLED else None
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ from app.routers import (
|
|||||||
auth,
|
auth,
|
||||||
brand_materials,
|
brand_materials,
|
||||||
brands,
|
brands,
|
||||||
category_attributes,
|
|
||||||
category_ref_units,
|
|
||||||
files,
|
files,
|
||||||
inventory,
|
inventory,
|
||||||
item_decisions,
|
item_decisions,
|
||||||
@@ -24,7 +22,6 @@ from app.routers import (
|
|||||||
supplier_materials,
|
supplier_materials,
|
||||||
suppliers,
|
suppliers,
|
||||||
termsheets,
|
termsheets,
|
||||||
unit_conversions,
|
|
||||||
units,
|
units,
|
||||||
users,
|
users,
|
||||||
)
|
)
|
||||||
@@ -35,7 +32,6 @@ ROUTERS = [
|
|||||||
brands.router,
|
brands.router,
|
||||||
material_categories.router,
|
material_categories.router,
|
||||||
attribute_definitions.router,
|
attribute_definitions.router,
|
||||||
category_attributes.router,
|
|
||||||
materials.router,
|
materials.router,
|
||||||
brand_materials.router,
|
brand_materials.router,
|
||||||
suppliers.router,
|
suppliers.router,
|
||||||
@@ -51,8 +47,6 @@ ROUTERS = [
|
|||||||
item_receipts.router,
|
item_receipts.router,
|
||||||
quote_events.router,
|
quote_events.router,
|
||||||
files.router,
|
files.router,
|
||||||
category_ref_units.router,
|
|
||||||
unit_conversions.router,
|
|
||||||
standard_conversions.router,
|
standard_conversions.router,
|
||||||
inventory.router,
|
inventory.router,
|
||||||
pm.router,
|
pm.router,
|
||||||
|
|||||||
@@ -1,136 +0,0 @@
|
|||||||
"""Category attributes — scoped attribute definitions per category (P2 set).
|
|
||||||
|
|
||||||
Identified by composite key {category_id}/{attribute_definition_id}.
|
|
||||||
The nested `attribute` object is ALWAYS included (not expand-based).
|
|
||||||
"""
|
|
||||||
from fastapi import APIRouter, Depends, Query
|
|
||||||
from sqlalchemy import select
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
|
||||||
|
|
||||||
from app.db.engine import get_db
|
|
||||||
from app.models.catalog import AttributeDefinition, CategoryAttribute, MaterialCategory
|
|
||||||
from app.schemas.catalog import CategoryAttributeCreate, CategoryAttributeUpdate
|
|
||||||
from app.utils.errors import ConflictError, NotFoundError
|
|
||||||
from app.utils.pagination import paginate
|
|
||||||
|
|
||||||
router = APIRouter(
|
|
||||||
prefix="/material-categories/{category_id}/attributes",
|
|
||||||
tags=["category-attributes"],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def ca_to_dict(ca: CategoryAttribute, attribute: AttributeDefinition) -> dict:
|
|
||||||
return {
|
|
||||||
"id": str(ca.id),
|
|
||||||
"category_id": str(ca.category_id),
|
|
||||||
"attribute_definition_id": str(ca.attribute_definition_id),
|
|
||||||
"is_required": ca.is_required,
|
|
||||||
"is_filterable": ca.is_filterable,
|
|
||||||
"conditional_rules": ca.conditional_rules,
|
|
||||||
"sort_order": ca.sort_order,
|
|
||||||
"attribute": {
|
|
||||||
"attribute_key": attribute.attribute_key,
|
|
||||||
"label": attribute.label,
|
|
||||||
"data_type": attribute.data_type,
|
|
||||||
"unit_id": attribute.unit_id,
|
|
||||||
"enum_values": attribute.enum_values,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async def _category_or_404(db: AsyncSession, category_id) -> MaterialCategory:
|
|
||||||
category = await db.get(MaterialCategory, category_id)
|
|
||||||
if category is None:
|
|
||||||
raise NotFoundError("دستهبندی یافت نشد")
|
|
||||||
return category
|
|
||||||
|
|
||||||
|
|
||||||
async def _with_attribute(db: AsyncSession, ca: CategoryAttribute) -> dict:
|
|
||||||
attribute = await db.get(AttributeDefinition, ca.attribute_definition_id)
|
|
||||||
return ca_to_dict(ca, attribute)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
|
||||||
async def list_category_attributes(
|
|
||||||
category_id,
|
|
||||||
page: int = Query(1, ge=1),
|
|
||||||
page_size: int = Query(20, ge=1, le=100),
|
|
||||||
db: AsyncSession = Depends(get_db),
|
|
||||||
):
|
|
||||||
await _category_or_404(db, category_id)
|
|
||||||
stmt = (
|
|
||||||
select(CategoryAttribute)
|
|
||||||
.where(CategoryAttribute.category_id == category_id)
|
|
||||||
.order_by(CategoryAttribute.sort_order.asc())
|
|
||||||
)
|
|
||||||
items, meta = await paginate(stmt, page, page_size, db)
|
|
||||||
return {"data": [await _with_attribute(db, ca) for ca in items], "meta": meta}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("", status_code=201)
|
|
||||||
async def create_category_attribute(
|
|
||||||
category_id, payload: CategoryAttributeCreate, db: AsyncSession = Depends(get_db)
|
|
||||||
):
|
|
||||||
await _category_or_404(db, category_id)
|
|
||||||
if await db.get(AttributeDefinition, payload.attribute_definition_id) is None:
|
|
||||||
raise NotFoundError("تعریف ویژگی یافت نشد", field="attribute_definition_id")
|
|
||||||
duplicate = await db.scalar(
|
|
||||||
select(CategoryAttribute.id).where(
|
|
||||||
CategoryAttribute.category_id == category_id,
|
|
||||||
CategoryAttribute.attribute_definition_id == payload.attribute_definition_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if duplicate is not None:
|
|
||||||
raise ConflictError("این ویژگی قبلاً به این دستهبندی اضافه شده است", field="attribute_definition_id")
|
|
||||||
ca = CategoryAttribute(
|
|
||||||
category_id=category_id,
|
|
||||||
attribute_definition_id=payload.attribute_definition_id,
|
|
||||||
is_required=payload.is_required,
|
|
||||||
is_filterable=payload.is_filterable,
|
|
||||||
conditional_rules=payload.conditional_rules,
|
|
||||||
sort_order=payload.sort_order,
|
|
||||||
)
|
|
||||||
db.add(ca)
|
|
||||||
await db.flush()
|
|
||||||
return {"data": await _with_attribute(db, ca)}
|
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{attribute_definition_id}")
|
|
||||||
async def update_category_attribute(
|
|
||||||
category_id, attribute_definition_id, payload: CategoryAttributeUpdate, db: AsyncSession = Depends(get_db)
|
|
||||||
):
|
|
||||||
ca = await db.scalar(
|
|
||||||
select(CategoryAttribute).where(
|
|
||||||
CategoryAttribute.category_id == category_id,
|
|
||||||
CategoryAttribute.attribute_definition_id == attribute_definition_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if ca is None:
|
|
||||||
raise NotFoundError("ویژگی این دستهبندی یافت نشد")
|
|
||||||
if payload.is_required is not None:
|
|
||||||
ca.is_required = payload.is_required
|
|
||||||
if payload.is_filterable is not None:
|
|
||||||
ca.is_filterable = payload.is_filterable
|
|
||||||
if payload.conditional_rules is not None:
|
|
||||||
ca.conditional_rules = payload.conditional_rules
|
|
||||||
if payload.sort_order is not None:
|
|
||||||
ca.sort_order = payload.sort_order
|
|
||||||
await db.flush()
|
|
||||||
return {"data": await _with_attribute(db, ca)}
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{attribute_definition_id}")
|
|
||||||
async def delete_category_attribute(
|
|
||||||
category_id, attribute_definition_id, db: AsyncSession = Depends(get_db)
|
|
||||||
):
|
|
||||||
ca = await db.scalar(
|
|
||||||
select(CategoryAttribute).where(
|
|
||||||
CategoryAttribute.category_id == category_id,
|
|
||||||
CategoryAttribute.attribute_definition_id == attribute_definition_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if ca is None:
|
|
||||||
raise NotFoundError("ویژگی این دستهبندی یافت نشد")
|
|
||||||
await db.delete(ca)
|
|
||||||
await db.flush()
|
|
||||||
return {"data": {"deleted": True}}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
"""Category reference units — units.html dashboard (P2-26/27).
|
|
||||||
|
|
||||||
One reference unit per category (UNIQUE category_id); PUT is an upsert.
|
|
||||||
"""
|
|
||||||
from fastapi import APIRouter, Depends, Query
|
|
||||||
from sqlalchemy import select
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
|
||||||
|
|
||||||
from app.db.engine import get_db
|
|
||||||
from app.models.catalog import CategoryReferenceUnit, MaterialCategory
|
|
||||||
from app.schemas.catalog import CategoryReferenceUnitPut
|
|
||||||
from app.services.expand_service import category_brief
|
|
||||||
from app.utils.errors import NotFoundError
|
|
||||||
from app.utils.pagination import paginate
|
|
||||||
from app.utils.serialization import to_iso
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/category-reference-units", tags=["category-reference-units"])
|
|
||||||
|
|
||||||
|
|
||||||
def cru_to_dict(cru: CategoryReferenceUnit, expand: set[str] = frozenset()) -> dict:
|
|
||||||
d = {
|
|
||||||
"id": str(cru.id),
|
|
||||||
"category_id": str(cru.category_id),
|
|
||||||
"ref_unit": cru.ref_unit,
|
|
||||||
"updated_at": to_iso(cru.updated_at),
|
|
||||||
}
|
|
||||||
if "category" in expand and hasattr(cru, "_category"):
|
|
||||||
d["category"] = category_brief(cru._category)
|
|
||||||
return d
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
|
||||||
async def list_category_reference_units(
|
|
||||||
page: int = Query(1, ge=1),
|
|
||||||
page_size: int = Query(20, ge=1, le=100),
|
|
||||||
category_id=None,
|
|
||||||
expand: str | None = None,
|
|
||||||
db: AsyncSession = Depends(get_db),
|
|
||||||
):
|
|
||||||
stmt = select(CategoryReferenceUnit)
|
|
||||||
if category_id:
|
|
||||||
stmt = stmt.where(CategoryReferenceUnit.category_id == category_id)
|
|
||||||
items, meta = await paginate(stmt, page, page_size, db)
|
|
||||||
tokens = {t.strip() for t in expand.split(",")} if expand else set()
|
|
||||||
if "category" in tokens:
|
|
||||||
cat_ids = {cru.category_id for cru in items}
|
|
||||||
cats = {}
|
|
||||||
if cat_ids:
|
|
||||||
cats = {c.id: c for c in (await db.execute(select(MaterialCategory).where(MaterialCategory.id.in_(cat_ids)))).scalars()}
|
|
||||||
for cru in items:
|
|
||||||
cru._category = cats.get(cru.category_id)
|
|
||||||
return {"data": [cru_to_dict(cru, tokens) for cru in items], "meta": meta}
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{category_id}")
|
|
||||||
async def upsert_category_reference_unit(
|
|
||||||
category_id, payload: CategoryReferenceUnitPut, db: AsyncSession = Depends(get_db)
|
|
||||||
):
|
|
||||||
category = await db.get(MaterialCategory, category_id)
|
|
||||||
if category is None:
|
|
||||||
raise NotFoundError("دستهبندی یافت نشد")
|
|
||||||
cru = await db.scalar(select(CategoryReferenceUnit).where(CategoryReferenceUnit.category_id == category_id))
|
|
||||||
if cru is None:
|
|
||||||
cru = CategoryReferenceUnit(category_id=category_id, ref_unit=payload.ref_unit)
|
|
||||||
db.add(cru)
|
|
||||||
else:
|
|
||||||
cru.ref_unit = payload.ref_unit
|
|
||||||
await db.flush()
|
|
||||||
return {"data": {"category_id": str(category_id), "ref_unit": cru.ref_unit}}
|
|
||||||
@@ -314,7 +314,6 @@ async def delete_warehouse(
|
|||||||
@router.get("")
|
@router.get("")
|
||||||
async def list_inventory(
|
async def list_inventory(
|
||||||
project_id: uuid.UUID | None = None,
|
project_id: uuid.UUID | None = None,
|
||||||
category_id=None,
|
|
||||||
warehouse_id=None,
|
warehouse_id=None,
|
||||||
material_id=None,
|
material_id=None,
|
||||||
sort: str | None = None,
|
sort: str | None = None,
|
||||||
@@ -332,10 +331,6 @@ async def list_inventory(
|
|||||||
stmt = stmt.join(Warehouse, Warehouse.id == InventoryItem.warehouse_id).where(
|
stmt = stmt.join(Warehouse, Warehouse.id == InventoryItem.warehouse_id).where(
|
||||||
Warehouse.project_id == project_id
|
Warehouse.project_id == project_id
|
||||||
)
|
)
|
||||||
if category_id is not None:
|
|
||||||
stmt = stmt.join(Material, Material.id == InventoryItem.material_id).where(
|
|
||||||
Material.category_id == category_id
|
|
||||||
)
|
|
||||||
if warehouse_id is not None:
|
if warehouse_id is not None:
|
||||||
stmt = stmt.where(InventoryItem.warehouse_id == warehouse_id)
|
stmt = stmt.where(InventoryItem.warehouse_id == warehouse_id)
|
||||||
if material_id is not None:
|
if material_id is not None:
|
||||||
@@ -357,10 +352,6 @@ async def _serialize_inventory_rows(db: AsyncSession, items: list[InventoryItem]
|
|||||||
unit_ids = {i.unit_id for i in items}
|
unit_ids = {i.unit_id for i in items}
|
||||||
warehouses = {w.id: w for w in (await db.execute(select(Warehouse).where(Warehouse.id.in_(wh_ids)))).scalars()}
|
warehouses = {w.id: w for w in (await db.execute(select(Warehouse).where(Warehouse.id.in_(wh_ids)))).scalars()}
|
||||||
materials = {m.id: m for m in (await db.execute(select(Material).where(Material.id.in_(mat_ids)))).scalars()}
|
materials = {m.id: m for m in (await db.execute(select(Material).where(Material.id.in_(mat_ids)))).scalars()}
|
||||||
categories = {}
|
|
||||||
if materials:
|
|
||||||
cat_ids = {m.category_id for m in materials.values()}
|
|
||||||
categories = {c.id: c for c in (await db.execute(select(MaterialCategory).where(MaterialCategory.id.in_(cat_ids)))).scalars()}
|
|
||||||
brands = {}
|
brands = {}
|
||||||
if brand_ids:
|
if brand_ids:
|
||||||
brands = {b.id: b for b in (await db.execute(select(Brand).where(Brand.id.in_(brand_ids)))).scalars()}
|
brands = {b.id: b for b in (await db.execute(select(Brand).where(Brand.id.in_(brand_ids)))).scalars()}
|
||||||
@@ -370,7 +361,7 @@ async def _serialize_inventory_rows(db: AsyncSession, items: list[InventoryItem]
|
|||||||
return [
|
return [
|
||||||
inventory_to_dict(
|
inventory_to_dict(
|
||||||
i, warehouses[i.warehouse_id], materials[i.material_id],
|
i, warehouses[i.warehouse_id], materials[i.material_id],
|
||||||
categories.get(materials[i.material_id].category_id), brands.get(i.brand_id), units.get(i.unit_id),
|
None, brands.get(i.brand_id), units.get(i.unit_id),
|
||||||
)
|
)
|
||||||
for i in items
|
for i in items
|
||||||
]
|
]
|
||||||
@@ -532,58 +523,3 @@ async def transaction_types():
|
|||||||
"PURCHASE", "WITHDRAWAL", "RETURN", "TRANSFER_IN", "TRANSFER_OUT",
|
"PURCHASE", "WITHDRAWAL", "RETURN", "TRANSFER_IN", "TRANSFER_OUT",
|
||||||
"DAMAGE", "WASTE", "LOSS", "FOUND", "STOCK_COUNT",
|
"DAMAGE", "WASTE", "LOSS", "FOUND", "STOCK_COUNT",
|
||||||
)]}
|
)]}
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Material selector — filtered by Category + Warehouse (transaction forms)
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
@router.get("/materials/options")
|
|
||||||
async def material_options(
|
|
||||||
category_id: uuid.UUID,
|
|
||||||
warehouse_id: uuid.UUID | None = None,
|
|
||||||
scope: Scope = Depends(inventory_scope),
|
|
||||||
db: AsyncSession = Depends(get_db),
|
|
||||||
):
|
|
||||||
"""Materials for the transaction form.
|
|
||||||
|
|
||||||
Always filtered by category. When `warehouse_id` is given, only materials
|
|
||||||
that already exist in that warehouse's inventory are returned (so the PM
|
|
||||||
works with the actual stock of that warehouse); otherwise the full
|
|
||||||
category list is returned (e.g. first purchase of a new material).
|
|
||||||
"""
|
|
||||||
category = await db.get(MaterialCategory, category_id)
|
|
||||||
if category is None:
|
|
||||||
raise NotFoundError("دستهبندی یافت نشد", field="category_id")
|
|
||||||
|
|
||||||
stmt = select(Material).where(Material.category_id == category_id, Material.status == "active")
|
|
||||||
if warehouse_id is not None:
|
|
||||||
wh = await get_warehouse_or_404(db, warehouse_id)
|
|
||||||
if scope.role == ROLE_PM and wh.project_id != scope.project_id:
|
|
||||||
raise ForbiddenError("این انبار متعلق به پروژه شما نیست")
|
|
||||||
stmt = stmt.where(
|
|
||||||
Material.id.in_(
|
|
||||||
select(InventoryItem.material_id).where(InventoryItem.warehouse_id == warehouse_id)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
stmt = stmt.order_by(Material.name)
|
|
||||||
materials = (await db.execute(stmt)).scalars().all()
|
|
||||||
|
|
||||||
# Brands per material (from brand_materials catalog + brands actually used in stock)
|
|
||||||
brand_rows = (await db.execute(
|
|
||||||
select(BrandMaterial.material_id, Brand.id, Brand.name)
|
|
||||||
.join(Brand, Brand.id == BrandMaterial.brand_id)
|
|
||||||
.where(BrandMaterial.material_id.in_([m.id for m in materials]), BrandMaterial.is_active.is_(True))
|
|
||||||
)).all()
|
|
||||||
brands_by_material: dict[str, list[dict]] = {}
|
|
||||||
for mat_id, b_id, b_name in brand_rows:
|
|
||||||
brands_by_material.setdefault(str(mat_id), []).append({"id": str(b_id), "name": b_name})
|
|
||||||
|
|
||||||
return {"data": [
|
|
||||||
{
|
|
||||||
"id": str(m.id),
|
|
||||||
"name": m.name,
|
|
||||||
"brands": brands_by_material.get(str(m.id), []),
|
|
||||||
}
|
|
||||||
for m in materials
|
|
||||||
]}
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ from sqlalchemy import func, or_, select
|
|||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from app.db.engine import get_db
|
from app.db.engine import get_db
|
||||||
from app.models.catalog import Material, MaterialCategory
|
from app.models.catalog import MaterialCategory
|
||||||
|
from app.models.procurement import Supplier
|
||||||
from app.schemas.catalog import MaterialCategoryCreate, MaterialCategoryUpdate
|
from app.schemas.catalog import MaterialCategoryCreate, MaterialCategoryUpdate
|
||||||
from app.utils.errors import ConflictError, NotFoundError
|
from app.utils.errors import ConflictError, NotFoundError
|
||||||
from app.utils.pagination import paginate, sort_query
|
from app.utils.pagination import paginate, sort_query
|
||||||
@@ -95,10 +96,14 @@ async def delete_category(category_id, db: AsyncSession = Depends(get_db)):
|
|||||||
category = await db.get(MaterialCategory, category_id)
|
category = await db.get(MaterialCategory, category_id)
|
||||||
if category is None:
|
if category is None:
|
||||||
raise NotFoundError("دستهبندی یافت نشد")
|
raise NotFoundError("دستهبندی یافت نشد")
|
||||||
# materials.category_id is ON DELETE RESTRICT — block if any material exists.
|
# v2.7: materials no longer carry a category, so what blocks deletion is the
|
||||||
linked = await db.scalar(select(func.count()).select_from(Material).where(Material.category_id == category_id))
|
# supplier side — suppliers.category_id is ON DELETE RESTRICT. Every
|
||||||
|
# material of that category is reached through those suppliers.
|
||||||
|
linked = await db.scalar(
|
||||||
|
select(func.count()).select_from(Supplier).where(Supplier.category_id == category_id)
|
||||||
|
)
|
||||||
if linked:
|
if linked:
|
||||||
raise ConflictError(f"این دستهبندی شامل {linked} متریال است و قابل حذف نیست")
|
raise ConflictError(f"این دستهبندی برای {linked} تأمینکننده ثبت شده و قابل حذف نیست")
|
||||||
await db.delete(category) # category_attributes cascade via DDL
|
await db.delete(category) # category_attributes cascade via DDL
|
||||||
await db.flush()
|
await db.flush()
|
||||||
return {"data": {"id": str(category_id), "deleted": True}}
|
return {"data": {"id": str(category_id), "deleted": True}}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ from app.models.catalog import (
|
|||||||
MaterialUnit,
|
MaterialUnit,
|
||||||
Unit,
|
Unit,
|
||||||
)
|
)
|
||||||
from app.models.procurement import LineItem, Supplier, SupplierMaterial
|
from app.models.procurement import LineItem
|
||||||
from app.schemas.catalog import (
|
from app.schemas.catalog import (
|
||||||
MaterialAttributeValuesPut,
|
MaterialAttributeValuesPut,
|
||||||
MaterialCreate,
|
MaterialCreate,
|
||||||
@@ -97,7 +97,6 @@ async def _apply_attr_filters(db: AsyncSession, stmt, request: Request):
|
|||||||
async def list_materials(
|
async def list_materials(
|
||||||
page: int = Query(1, ge=1),
|
page: int = Query(1, ge=1),
|
||||||
page_size: int = Query(20, ge=1, le=100),
|
page_size: int = Query(20, ge=1, le=100),
|
||||||
category_id=None,
|
|
||||||
brand_id=None,
|
brand_id=None,
|
||||||
status: str | None = None,
|
status: str | None = None,
|
||||||
q: str | None = None,
|
q: str | None = None,
|
||||||
@@ -107,17 +106,6 @@ async def list_materials(
|
|||||||
db: AsyncSession = Depends(get_db),
|
db: AsyncSession = Depends(get_db),
|
||||||
):
|
):
|
||||||
stmt = select(Material)
|
stmt = select(Material)
|
||||||
if category_id:
|
|
||||||
sub = (
|
|
||||||
select(1)
|
|
||||||
.select_from(SupplierMaterial)
|
|
||||||
.join(Supplier, Supplier.id == SupplierMaterial.supplier_id)
|
|
||||||
.where(
|
|
||||||
SupplierMaterial.material_id == Material.id,
|
|
||||||
Supplier.category_id == category_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
stmt = stmt.where(exists(sub))
|
|
||||||
if brand_id:
|
if brand_id:
|
||||||
# WHERE EXISTS brand_materials with that brand_id
|
# WHERE EXISTS brand_materials with that brand_id
|
||||||
sub = select(1).where(
|
sub = select(1).where(
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||||||
from app.auth import UserContext, get_optional_user
|
from app.auth import UserContext, get_optional_user
|
||||||
from app.config import settings
|
from app.config import settings
|
||||||
from app.db.engine import get_db
|
from app.db.engine import get_db
|
||||||
from app.models.catalog import BrandMaterial, Material, MaterialCategory, Unit
|
from app.models.catalog import BrandMaterial, Material, Unit
|
||||||
from app.models.inventory import InventoryItem, InventoryTransaction, Warehouse
|
from app.models.inventory import InventoryItem, InventoryTransaction, Warehouse
|
||||||
from app.models.procurement import (
|
from app.models.procurement import (
|
||||||
ItemDecision,
|
ItemDecision,
|
||||||
@@ -201,12 +201,6 @@ async def pm_on_the_way(
|
|||||||
# unit / category lookups for display
|
# unit / category lookups for display
|
||||||
unit_ids = {d.LineItem.unit_id for d in decisions}
|
unit_ids = {d.LineItem.unit_id for d in decisions}
|
||||||
units = {u.id: u for u in (await db.execute(select(Unit).where(Unit.id.in_(unit_ids)))).scalars()}
|
units = {u.id: u for u in (await db.execute(select(Unit).where(Unit.id.in_(unit_ids)))).scalars()}
|
||||||
sup_cat_ids = {d.Supplier.category_id for d in decisions if d.Supplier.category_id}
|
|
||||||
cats = {}
|
|
||||||
if sup_cat_ids:
|
|
||||||
rows = (await db.execute(select(MaterialCategory).where(MaterialCategory.id.in_(sup_cat_ids)))).scalars().all()
|
|
||||||
cats = {c.id: c for c in rows}
|
|
||||||
|
|
||||||
out = []
|
out = []
|
||||||
for d, li, q, m, sup, proj in decisions:
|
for d, li, q, m, sup, proj in decisions:
|
||||||
received = received_map.get(d.id)
|
received = received_map.get(d.id)
|
||||||
@@ -219,8 +213,7 @@ async def pm_on_the_way(
|
|||||||
"supplier": {"id": str(sup.id), "name": sup.name},
|
"supplier": {"id": str(sup.id), "name": sup.name},
|
||||||
"project": {"id": str(proj.id), "name": proj.name} if proj else None,
|
"project": {"id": str(proj.id), "name": proj.name} if proj else None,
|
||||||
"decided_at": to_iso(d.decided_at),
|
"decided_at": to_iso(d.decided_at),
|
||||||
"material": {"id": str(m.id), "name": m.name,
|
"material": {"id": str(m.id), "name": m.name},
|
||||||
"category": {"id": str(c.id), "name": c.name} if (c := cats.get(sup.category_id)) else None},
|
|
||||||
"unit": {"id": units[li.unit_id].id, "code": units[li.unit_id].code,
|
"unit": {"id": units[li.unit_id].id, "code": units[li.unit_id].code,
|
||||||
"name_fa": units[li.unit_id].name_fa} if li.unit_id in units else None,
|
"name_fa": units[li.unit_id].name_fa} if li.unit_id in units else None,
|
||||||
"ordered_qty": str(li.quantity),
|
"ordered_qty": str(li.quantity),
|
||||||
|
|||||||
@@ -45,14 +45,6 @@ router = APIRouter(prefix="/quotes", tags=["quotes"])
|
|||||||
|
|
||||||
# --- list filter helpers ---------------------------------------------------
|
# --- list filter helpers ---------------------------------------------------
|
||||||
|
|
||||||
def _quote_category_exists(category_id) -> exists:
|
|
||||||
return exists(
|
|
||||||
select(1).where(
|
|
||||||
Supplier.id == Quote.supplier_id,
|
|
||||||
Supplier.category_id == category_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _quote_search(q: str) -> exists:
|
def _quote_search(q: str) -> exists:
|
||||||
like = f"%{q}%"
|
like = f"%{q}%"
|
||||||
@@ -80,7 +72,6 @@ async def get_quote_stats(
|
|||||||
project_id=None,
|
project_id=None,
|
||||||
quote_date_from: datetime | None = None,
|
quote_date_from: datetime | None = None,
|
||||||
quote_date_to: datetime | None = None,
|
quote_date_to: datetime | None = None,
|
||||||
category_id=None,
|
|
||||||
db: AsyncSession = Depends(get_db),
|
db: AsyncSession = Depends(get_db),
|
||||||
):
|
):
|
||||||
return {
|
return {
|
||||||
@@ -90,7 +81,6 @@ async def get_quote_stats(
|
|||||||
project_id=project_id,
|
project_id=project_id,
|
||||||
quote_date_from=quote_date_from,
|
quote_date_from=quote_date_from,
|
||||||
quote_date_to=quote_date_to,
|
quote_date_to=quote_date_to,
|
||||||
category_id=category_id,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +96,6 @@ async def list_quotes(
|
|||||||
status: str | None = None,
|
status: str | None = None,
|
||||||
supplier_id=None,
|
supplier_id=None,
|
||||||
project_id=None,
|
project_id=None,
|
||||||
category_id=None,
|
|
||||||
quote_date_from: datetime | None = None,
|
quote_date_from: datetime | None = None,
|
||||||
quote_date_to: datetime | None = None,
|
quote_date_to: datetime | None = None,
|
||||||
q: str | None = None,
|
q: str | None = None,
|
||||||
@@ -137,8 +126,6 @@ async def list_quotes(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if category_id:
|
|
||||||
stmt = stmt.where(_quote_category_exists(category_id))
|
|
||||||
if quote_date_from is not None:
|
if quote_date_from is not None:
|
||||||
stmt = stmt.where(Quote.quote_date >= quote_date_from)
|
stmt = stmt.where(Quote.quote_date >= quote_date_from)
|
||||||
if quote_date_to is not None:
|
if quote_date_to is not None:
|
||||||
|
|||||||
@@ -1,146 +0,0 @@
|
|||||||
"""Unit conversions — category/brand-scoped (units.html dashboard, P2-28..31).
|
|
||||||
|
|
||||||
Model: {category_id, brand?, from_unit, to_unit, factor}
|
|
||||||
1 from_unit = factor × to_unit
|
|
||||||
brand empty = all brands of the category; brand set = that brand only.
|
|
||||||
"""
|
|
||||||
from fastapi import APIRouter, Depends, Query
|
|
||||||
from sqlalchemy import select
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
|
||||||
|
|
||||||
from app.db.engine import get_db
|
|
||||||
from app.models.catalog import MaterialCategory, UnitConversion
|
|
||||||
from app.schemas.catalog import UnitConversionCreate, UnitConversionUpdate
|
|
||||||
from app.services.expand_service import category_brief
|
|
||||||
from app.utils.errors import ConflictError, NotFoundError
|
|
||||||
from app.utils.pagination import paginate
|
|
||||||
from app.utils.serialization import to_iso
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/unit-conversions", tags=["unit-conversions"])
|
|
||||||
|
|
||||||
|
|
||||||
def uc_to_dict(uc: UnitConversion, expand: set[str] = frozenset()) -> dict:
|
|
||||||
d = {
|
|
||||||
"id": str(uc.id),
|
|
||||||
"category_id": str(uc.category_id),
|
|
||||||
"brand": uc.brand,
|
|
||||||
"from_unit": uc.from_unit,
|
|
||||||
"to_unit": uc.to_unit,
|
|
||||||
"factor": float(uc.factor),
|
|
||||||
"updated_at": to_iso(uc.updated_at),
|
|
||||||
}
|
|
||||||
if "category" in expand and hasattr(uc, "_category"):
|
|
||||||
d["category"] = category_brief(uc._category)
|
|
||||||
return d
|
|
||||||
|
|
||||||
|
|
||||||
def _duplicate_condition(uc_or_none, category_id, brand, from_unit, to_unit):
|
|
||||||
"""Uniqueness treats NULL brand as a concrete value (matches the partial unique index)."""
|
|
||||||
where = (
|
|
||||||
UnitConversion.category_id == category_id,
|
|
||||||
UnitConversion.from_unit == from_unit,
|
|
||||||
UnitConversion.to_unit == to_unit,
|
|
||||||
)
|
|
||||||
# PG «IS» فقط با NULL معتبر است؛ برای برند مقداردار از «==» استفاده میشود
|
|
||||||
where = where + (UnitConversion.brand == brand if brand is not None else UnitConversion.brand.is_(None),)
|
|
||||||
if uc_or_none is not None:
|
|
||||||
where = where + (UnitConversion.id != uc_or_none.id,)
|
|
||||||
return where
|
|
||||||
|
|
||||||
|
|
||||||
async def _check_duplicate(db: AsyncSession, exclude_id, category_id, brand, from_unit, to_unit) -> None:
|
|
||||||
duplicate = await db.scalar(
|
|
||||||
select(UnitConversion.id).where(*_duplicate_condition(exclude_id, category_id, brand, from_unit, to_unit))
|
|
||||||
)
|
|
||||||
if duplicate is not None:
|
|
||||||
raise ConflictError("این تبدیل قبلاً ثبت شده است")
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
|
||||||
async def list_unit_conversions(
|
|
||||||
page: int = Query(1, ge=1),
|
|
||||||
page_size: int = Query(20, ge=1, le=100),
|
|
||||||
category_id=None,
|
|
||||||
brand: str | None = None,
|
|
||||||
from_unit: str | None = None,
|
|
||||||
to_unit: str | None = None,
|
|
||||||
expand: str | None = None,
|
|
||||||
db: AsyncSession = Depends(get_db),
|
|
||||||
):
|
|
||||||
stmt = select(UnitConversion)
|
|
||||||
if category_id:
|
|
||||||
stmt = stmt.where(UnitConversion.category_id == category_id)
|
|
||||||
if brand:
|
|
||||||
stmt = stmt.where(UnitConversion.brand == brand)
|
|
||||||
if from_unit:
|
|
||||||
stmt = stmt.where(UnitConversion.from_unit == from_unit)
|
|
||||||
if to_unit:
|
|
||||||
stmt = stmt.where(UnitConversion.to_unit == to_unit)
|
|
||||||
items, meta = await paginate(stmt, page, page_size, db)
|
|
||||||
tokens = {t.strip() for t in expand.split(",")} if expand else set()
|
|
||||||
if "category" in tokens:
|
|
||||||
cat_ids = {uc.category_id for uc in items}
|
|
||||||
cats = {}
|
|
||||||
if cat_ids:
|
|
||||||
cats = {c.id: c for c in (await db.execute(select(MaterialCategory).where(MaterialCategory.id.in_(cat_ids)))).scalars()}
|
|
||||||
for uc in items:
|
|
||||||
uc._category = cats.get(uc.category_id)
|
|
||||||
return {"data": [uc_to_dict(uc, tokens) for uc in items], "meta": meta}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("", status_code=201)
|
|
||||||
async def create_unit_conversion(payload: UnitConversionCreate, db: AsyncSession = Depends(get_db)):
|
|
||||||
category = await db.get(MaterialCategory, payload.category_id)
|
|
||||||
if category is None:
|
|
||||||
raise NotFoundError("دستهبندی یافت نشد", field="category_id")
|
|
||||||
await _check_duplicate(db, None, payload.category_id, payload.brand, payload.from_unit, payload.to_unit)
|
|
||||||
uc = UnitConversion(
|
|
||||||
category_id=payload.category_id,
|
|
||||||
brand=payload.brand,
|
|
||||||
from_unit=payload.from_unit,
|
|
||||||
to_unit=payload.to_unit,
|
|
||||||
factor=payload.factor,
|
|
||||||
)
|
|
||||||
db.add(uc)
|
|
||||||
await db.flush()
|
|
||||||
return {"data": uc_to_dict(uc)}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{uc_id}")
|
|
||||||
async def get_unit_conversion(uc_id, db: AsyncSession = Depends(get_db)):
|
|
||||||
uc = await db.get(UnitConversion, uc_id)
|
|
||||||
if uc is None:
|
|
||||||
raise NotFoundError("تبدیل واحد یافت نشد")
|
|
||||||
return {"data": uc_to_dict(uc)}
|
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{uc_id}")
|
|
||||||
async def update_unit_conversion(uc_id, payload: UnitConversionUpdate, db: AsyncSession = Depends(get_db)):
|
|
||||||
uc = await db.get(UnitConversion, uc_id)
|
|
||||||
if uc is None:
|
|
||||||
raise NotFoundError("تبدیل واحد یافت نشد")
|
|
||||||
new_brand = payload.brand if payload.brand is not None else uc.brand
|
|
||||||
new_from = payload.from_unit if payload.from_unit is not None else uc.from_unit
|
|
||||||
new_to = payload.to_unit if payload.to_unit is not None else uc.to_unit
|
|
||||||
if payload.brand is not None or payload.from_unit is not None or payload.to_unit is not None:
|
|
||||||
await _check_duplicate(db, uc.id, uc.category_id, new_brand, new_from, new_to)
|
|
||||||
if payload.brand is not None:
|
|
||||||
uc.brand = payload.brand
|
|
||||||
if payload.from_unit is not None:
|
|
||||||
uc.from_unit = payload.from_unit
|
|
||||||
if payload.to_unit is not None:
|
|
||||||
uc.to_unit = payload.to_unit
|
|
||||||
if payload.factor is not None:
|
|
||||||
uc.factor = payload.factor
|
|
||||||
await db.flush()
|
|
||||||
return {"data": uc_to_dict(uc)}
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{uc_id}")
|
|
||||||
async def delete_unit_conversion(uc_id, db: AsyncSession = Depends(get_db)):
|
|
||||||
uc = await db.get(UnitConversion, uc_id)
|
|
||||||
if uc is None:
|
|
||||||
raise NotFoundError("تبدیل واحد یافت نشد")
|
|
||||||
await db.delete(uc)
|
|
||||||
await db.flush()
|
|
||||||
return {"data": {"deleted": True}}
|
|
||||||
@@ -37,13 +37,14 @@ async def track(event: str, properties: dict[str, Any] | None = None, profile_id
|
|||||||
"""Record a named event; swallows every error."""
|
"""Record a named event; swallows every error."""
|
||||||
if not _configured():
|
if not _configured():
|
||||||
return
|
return
|
||||||
body = {
|
# OpenPanel's Track API takes {type, payload:{…}} (same envelope the browser SDK
|
||||||
"type": "track",
|
# sends) — a flat body is rejected with 400, which this module swallows by design.
|
||||||
"event": event,
|
# The payload field is "name" (ITrackPayload in packages/validation) — "event" is
|
||||||
"properties": properties or {},
|
# rejected with a 400 that this module swallows.
|
||||||
}
|
inner: dict[str, Any] = {"name": event, "properties": properties or {}}
|
||||||
if profile_id:
|
if profile_id:
|
||||||
body["profileId"] = profile_id
|
inner["profileId"] = profile_id
|
||||||
|
body = {"type": "track", "payload": inner}
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
|
async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
|
||||||
await client.post(
|
await client.post(
|
||||||
@@ -66,7 +67,7 @@ async def identify(profile_id: str, properties: dict[str, Any] | None = None) ->
|
|||||||
async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
|
async with httpx.AsyncClient(timeout=_TIMEOUT) as client:
|
||||||
await client.post(
|
await client.post(
|
||||||
f"{settings.OPENPANEL_API_URL.rstrip('/')}/track",
|
f"{settings.OPENPANEL_API_URL.rstrip('/')}/track",
|
||||||
json={"type": "identify", "profileId": profile_id, "properties": properties or {}},
|
json={"type": "identify", "payload": {"profileId": profile_id, "properties": properties or {}}},
|
||||||
headers={
|
headers={
|
||||||
"openpanel-client-id": settings.OPENPANEL_CLIENT_ID,
|
"openpanel-client-id": settings.OPENPANEL_CLIENT_ID,
|
||||||
"openpanel-client-secret": settings.OPENPANEL_CLIENT_SECRET,
|
"openpanel-client-secret": settings.OPENPANEL_CLIENT_SECRET,
|
||||||
|
|||||||
@@ -252,6 +252,8 @@ def _material_to_dict(m: Material, expand: set[str], maps: dict) -> dict:
|
|||||||
"created_at": to_iso(m.created_at),
|
"created_at": to_iso(m.created_at),
|
||||||
"updated_at": to_iso(m.updated_at),
|
"updated_at": to_iso(m.updated_at),
|
||||||
}
|
}
|
||||||
|
# Category is a supplier attribute now, not a material one — the key stays
|
||||||
|
# in the payload (null) so the contract shape is unchanged.
|
||||||
if "category" in expand:
|
if "category" in expand:
|
||||||
d["category"] = None
|
d["category"] = None
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -873,7 +873,6 @@ async def quote_stats(
|
|||||||
project_id: UUID | None = None,
|
project_id: UUID | None = None,
|
||||||
quote_date_from=None,
|
quote_date_from=None,
|
||||||
quote_date_to=None,
|
quote_date_to=None,
|
||||||
category_id: UUID | None = None,
|
|
||||||
) -> dict:
|
) -> dict:
|
||||||
conditions = []
|
conditions = []
|
||||||
if supplier_id is not None:
|
if supplier_id is not None:
|
||||||
@@ -884,8 +883,6 @@ async def quote_stats(
|
|||||||
conditions.append(Quote.quote_date >= quote_date_from)
|
conditions.append(Quote.quote_date >= quote_date_from)
|
||||||
if quote_date_to is not None:
|
if quote_date_to is not None:
|
||||||
conditions.append(Quote.quote_date <= quote_date_to)
|
conditions.append(Quote.quote_date <= quote_date_to)
|
||||||
if category_id is not None:
|
|
||||||
conditions.append(_quote_has_category(category_id))
|
|
||||||
|
|
||||||
rows = (
|
rows = (
|
||||||
await db.execute(
|
await db.execute(
|
||||||
@@ -917,18 +914,6 @@ async def quote_stats(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _quote_has_category(category_id: UUID):
|
|
||||||
"""EXISTS: quote's supplier belongs to category_id."""
|
|
||||||
sub = (
|
|
||||||
select(1)
|
|
||||||
.where(
|
|
||||||
Supplier.id == Quote.supplier_id,
|
|
||||||
Supplier.category_id == category_id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return func.exists(sub)
|
|
||||||
|
|
||||||
|
|
||||||
async def project_stats(db: AsyncSession, project_id: UUID) -> dict:
|
async def project_stats(db: AsyncSession, project_id: UUID) -> dict:
|
||||||
"""Counts + total value per quote status for ONE project, isolated per project.
|
"""Counts + total value per quote status for ONE project, isolated per project.
|
||||||
|
|
||||||
|
|||||||
@@ -209,7 +209,7 @@ async def run() -> None:
|
|||||||
"item_decisions, item_termsheets, termsheet_schedule_steps, termsheets, line_item_specs, "
|
"item_decisions, item_termsheets, termsheet_schedule_steps, termsheets, line_item_specs, "
|
||||||
"line_items, quotes, supplier_materials, suppliers, brand_materials, "
|
"line_items, quotes, supplier_materials, suppliers, brand_materials, "
|
||||||
"material_attribute_values, materials, brands, warehouses, projects, users, "
|
"material_attribute_values, materials, brands, warehouses, projects, users, "
|
||||||
"category_reference_units, unit_conversions, standard_conversions "
|
"standard_conversions "
|
||||||
"RESTART IDENTITY CASCADE"
|
"RESTART IDENTITY CASCADE"
|
||||||
))
|
))
|
||||||
# Only the 8 canonical topic categories may exist — any leftover slug
|
# Only the 8 canonical topic categories may exist — any leftover slug
|
||||||
|
|||||||
@@ -23,9 +23,13 @@ ENV VITE_ENABLE_DEV_LOGIN=${DEV_LOGIN_ENABLED}
|
|||||||
# Empty values disable the feature (analytics.ts is a no-op then).
|
# Empty values disable the feature (analytics.ts is a no-op then).
|
||||||
ARG VITE_OPENPANEL_URL=""
|
ARG VITE_OPENPANEL_URL=""
|
||||||
ARG VITE_OPENPANEL_CLIENT_ID=""
|
ARG VITE_OPENPANEL_CLIENT_ID=""
|
||||||
|
# The browser script (op1.js) lives on the dashboard origin, not under the API path,
|
||||||
|
# so it needs its own arg — defined here or the value never reaches the bundle.
|
||||||
|
ARG VITE_OPENPANEL_SCRIPT_URL=""
|
||||||
ARG VITE_SENTRY_DSN=""
|
ARG VITE_SENTRY_DSN=""
|
||||||
ENV VITE_OPENPANEL_URL=${VITE_OPENPANEL_URL} \
|
ENV VITE_OPENPANEL_URL=${VITE_OPENPANEL_URL} \
|
||||||
VITE_OPENPANEL_CLIENT_ID=${VITE_OPENPANEL_CLIENT_ID} \
|
VITE_OPENPANEL_CLIENT_ID=${VITE_OPENPANEL_CLIENT_ID} \
|
||||||
|
VITE_OPENPANEL_SCRIPT_URL=${VITE_OPENPANEL_SCRIPT_URL} \
|
||||||
VITE_SENTRY_DSN=${VITE_SENTRY_DSN}
|
VITE_SENTRY_DSN=${VITE_SENTRY_DSN}
|
||||||
|
|
||||||
# Source code + build
|
# Source code + build
|
||||||
|
|||||||
@@ -3,11 +3,4 @@ export { useQuotes, useCreateQuote, useUpdateQuote, useDeleteQuote } from "./use
|
|||||||
export { useSuppliers, useAddSupplier, useUpdateSupplier, useRemoveSupplier } from "./useSuppliers";
|
export { useSuppliers, useAddSupplier, useUpdateSupplier, useRemoveSupplier } from "./useSuppliers";
|
||||||
export { useMaterials, useAddMaterial } from "./useMaterials";
|
export { useMaterials, useAddMaterial } from "./useMaterials";
|
||||||
export { useProjects, useAddProject } from "./useProjects";
|
export { useProjects, useAddProject } from "./useProjects";
|
||||||
export {
|
export { useUnits, useSaveStandardConversion, useDeleteStandardConversion } from "./useUnits";
|
||||||
useUnits,
|
|
||||||
useSaveRefUnit,
|
|
||||||
useSaveConversion,
|
|
||||||
useDeleteConversion,
|
|
||||||
useSaveStandardConversion,
|
|
||||||
useDeleteStandardConversion,
|
|
||||||
} from "./useUnits";
|
|
||||||
|
|||||||
@@ -1,52 +1,16 @@
|
|||||||
// ============================================================
|
// ============================================================
|
||||||
// hooks واحدها — واحد مرجع، ضرایب دستهای و ثابت
|
// hooks واحدها — تبدیلهای ثابت جهانی (دستهایها حذف شدند)
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
import { useMutation } from "@tanstack/react-query";
|
import { useMutation } from "@tanstack/react-query";
|
||||||
import type { CategoryUnit, StandardConversion, UnitConversion } from "../lib/types";
|
import type { StandardConversion } from "../lib/types";
|
||||||
import { api } from "../lib/api";
|
import { api } from "../lib/api";
|
||||||
import { useAppStore } from "../store";
|
import { useAppStore } from "../store";
|
||||||
import { useInvalidateStore } from "./useStoreData";
|
import { useInvalidateStore } from "./useStoreData";
|
||||||
|
|
||||||
export function useUnits(): {
|
export function useUnits(): { standardConversions: StandardConversion[] } {
|
||||||
categoryUnits: CategoryUnit[];
|
|
||||||
unitConversions: UnitConversion[];
|
|
||||||
standardConversions: StandardConversion[];
|
|
||||||
} {
|
|
||||||
const categoryUnits = useAppStore((s) => s.categoryUnits);
|
|
||||||
const unitConversions = useAppStore((s) => s.unitConversions);
|
|
||||||
const standardConversions = useAppStore((s) => s.standardConversions);
|
const standardConversions = useAppStore((s) => s.standardConversions);
|
||||||
return { categoryUnits, unitConversions, standardConversions };
|
return { standardConversions };
|
||||||
}
|
|
||||||
|
|
||||||
export function useSaveRefUnit() {
|
|
||||||
const setCategoryUnit = useAppStore((s) => s.setCategoryUnit);
|
|
||||||
const invalidate = useInvalidateStore();
|
|
||||||
return useMutation({
|
|
||||||
mutationFn: (cu: CategoryUnit) => api.units.saveCategoryUnit(cu),
|
|
||||||
onMutate: (cu) => setCategoryUnit(cu),
|
|
||||||
onSettled: () => invalidate(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useSaveConversion() {
|
|
||||||
const setConversion = useAppStore((s) => s.setConversion);
|
|
||||||
const invalidate = useInvalidateStore();
|
|
||||||
return useMutation({
|
|
||||||
mutationFn: (c: UnitConversion) => api.units.saveConversion(c),
|
|
||||||
onMutate: (c) => setConversion(c),
|
|
||||||
onSettled: () => invalidate(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useDeleteConversion() {
|
|
||||||
const deleteConversion = useAppStore((s) => s.deleteConversion);
|
|
||||||
const invalidate = useInvalidateStore();
|
|
||||||
return useMutation({
|
|
||||||
mutationFn: (id: string) => api.units.deleteConversion(id),
|
|
||||||
onMutate: (id) => deleteConversion(id),
|
|
||||||
onSettled: () => invalidate(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useSaveStandardConversion() {
|
export function useSaveStandardConversion() {
|
||||||
|
|||||||
@@ -22,8 +22,31 @@ declare global {
|
|||||||
|
|
||||||
const OPENPANEL_URL = (import.meta.env?.VITE_OPENPANEL_URL as string | undefined)?.trim();
|
const OPENPANEL_URL = (import.meta.env?.VITE_OPENPANEL_URL as string | undefined)?.trim();
|
||||||
const OPENPANEL_CLIENT_ID = (import.meta.env?.VITE_OPENPANEL_CLIENT_ID as string | undefined)?.trim();
|
const OPENPANEL_CLIENT_ID = (import.meta.env?.VITE_OPENPANEL_CLIENT_ID as string | undefined)?.trim();
|
||||||
|
const OPENPANEL_SCRIPT_URL = (import.meta.env?.VITE_OPENPANEL_SCRIPT_URL as string | undefined)?.trim();
|
||||||
const SENTRY_DSN = (import.meta.env?.VITE_SENTRY_DSN as string | undefined)?.trim();
|
const SENTRY_DSN = (import.meta.env?.VITE_SENTRY_DSN as string | undefined)?.trim();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the OpenPanel browser script (op1.js) and its queueing shim.
|
||||||
|
*
|
||||||
|
* The shim is OpenPanel's documented snippet: `window.op` becomes a queue while the
|
||||||
|
* script is in flight, so `op("init", …)` can be called immediately and the real
|
||||||
|
* script drains the queue on load. Without it, every `window.op?.()` call here is a
|
||||||
|
* silent no-op — which is what happened before this loader existed.
|
||||||
|
*/
|
||||||
|
function loadOpenPanelScript(src: string): void {
|
||||||
|
if (document.querySelector("script[data-openpanel]")) return;
|
||||||
|
const shim = document.createElement("script");
|
||||||
|
shim.textContent =
|
||||||
|
'window.op=window.op||function(){var n=[];return new Proxy(function(){arguments.length&&n.push([].slice.call(arguments))},{get:function(t,r){return"q"===r?n:function(){n.push([r].concat([].slice.call(arguments)))}},has:function(t,r){return"q"===r}}) }();';
|
||||||
|
document.head.appendChild(shim);
|
||||||
|
const script = document.createElement("script");
|
||||||
|
script.src = src;
|
||||||
|
script.async = true;
|
||||||
|
script.defer = true;
|
||||||
|
script.dataset.openpanel = "1";
|
||||||
|
document.head.appendChild(script);
|
||||||
|
}
|
||||||
|
|
||||||
export function initAnalytics(): void {
|
export function initAnalytics(): void {
|
||||||
if (SENTRY_DSN) {
|
if (SENTRY_DSN) {
|
||||||
Sentry.init({
|
Sentry.init({
|
||||||
@@ -35,6 +58,7 @@ export function initAnalytics(): void {
|
|||||||
}
|
}
|
||||||
if (OPENPANEL_URL && OPENPANEL_CLIENT_ID) {
|
if (OPENPANEL_URL && OPENPANEL_CLIENT_ID) {
|
||||||
try {
|
try {
|
||||||
|
if (OPENPANEL_SCRIPT_URL) loadOpenPanelScript(OPENPANEL_SCRIPT_URL);
|
||||||
window.op?.("init", {
|
window.op?.("init", {
|
||||||
apiUrl: OPENPANEL_URL,
|
apiUrl: OPENPANEL_URL,
|
||||||
clientId: OPENPANEL_CLIENT_ID,
|
clientId: OPENPANEL_CLIENT_ID,
|
||||||
|
|||||||
+13
-112
@@ -18,7 +18,6 @@ import { jalaliToGregorian, toAsciiDigits } from "./jalali";
|
|||||||
import { useAuthStore } from "../store/auth";
|
import { useAuthStore } from "../store/auth";
|
||||||
import type {
|
import type {
|
||||||
Attachment,
|
Attachment,
|
||||||
CategoryUnit,
|
|
||||||
ConfirmedReceipt,
|
ConfirmedReceipt,
|
||||||
InventoryRow,
|
InventoryRow,
|
||||||
InventoryTransaction,
|
InventoryTransaction,
|
||||||
@@ -30,7 +29,6 @@ import type {
|
|||||||
Store,
|
Store,
|
||||||
Supplier,
|
Supplier,
|
||||||
TransactionType,
|
TransactionType,
|
||||||
UnitConversion,
|
|
||||||
Warehouse,
|
Warehouse,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
@@ -252,10 +250,6 @@ async function patchData<T>(path: string, body: unknown): Promise<T> {
|
|||||||
const res = await request<{ data: T }>(path, { method: "PATCH", body: JSON.stringify(body) });
|
const res = await request<{ data: T }>(path, { method: "PATCH", body: JSON.stringify(body) });
|
||||||
return res.data;
|
return res.data;
|
||||||
}
|
}
|
||||||
async function putData<T>(path: string, body: unknown): Promise<T> {
|
|
||||||
const res = await request<{ data: T }>(path, { method: "PUT", body: JSON.stringify(body) });
|
|
||||||
return res.data;
|
|
||||||
}
|
|
||||||
async function deleteData<T>(path: string): Promise<T> {
|
async function deleteData<T>(path: string): Promise<T> {
|
||||||
const res = await request<{ data: T }>(path, { method: "DELETE" });
|
const res = await request<{ data: T }>(path, { method: "DELETE" });
|
||||||
return res.data;
|
return res.data;
|
||||||
@@ -322,7 +316,6 @@ interface RawSupplier {
|
|||||||
phone?: string | null;
|
phone?: string | null;
|
||||||
contact_person?: string | null;
|
contact_person?: string | null;
|
||||||
tax_id?: string | null;
|
tax_id?: string | null;
|
||||||
categories?: string[] | null; // متریالهای تأمینشده
|
|
||||||
// v2.6: هر تأمینکننده دقیقاً یک دسته دارد (FK) — بکاند این را میفرستد
|
// v2.6: هر تأمینکننده دقیقاً یک دسته دارد (FK) — بکاند این را میفرستد
|
||||||
category_id?: string | null;
|
category_id?: string | null;
|
||||||
category?: { id: string; slug: string; name: string } | null;
|
category?: { id: string; slug: string; name: string } | null;
|
||||||
@@ -402,21 +395,6 @@ interface RawQuote {
|
|||||||
line_items?: RawLineItem[] | null;
|
line_items?: RawLineItem[] | null;
|
||||||
termsheets?: RawTermsheet[] | null;
|
termsheets?: RawTermsheet[] | null;
|
||||||
}
|
}
|
||||||
interface RawCategoryUnit {
|
|
||||||
id: string;
|
|
||||||
category_id: string;
|
|
||||||
ref_unit: string;
|
|
||||||
category?: RawCategory | null;
|
|
||||||
}
|
|
||||||
interface RawUnitConversion {
|
|
||||||
id: string;
|
|
||||||
category_id: string;
|
|
||||||
brand?: string | null;
|
|
||||||
from_unit: string;
|
|
||||||
to_unit: string;
|
|
||||||
factor: number;
|
|
||||||
category?: RawCategory | null;
|
|
||||||
}
|
|
||||||
interface RawStandardConversion {
|
interface RawStandardConversion {
|
||||||
id: string;
|
id: string;
|
||||||
from_unit: string;
|
from_unit: string;
|
||||||
@@ -562,7 +540,6 @@ function adaptMaterial(raw: RawMaterial, lookup: Lookup): Material {
|
|||||||
topic:
|
topic:
|
||||||
raw.topic ||
|
raw.topic ||
|
||||||
(raw.category?.slug ? lookup.categorySlugToName.get(raw.category.slug) || "" : ""),
|
(raw.category?.slug ? lookup.categorySlugToName.get(raw.category.slug) || "" : ""),
|
||||||
category: raw.category?.name || "",
|
|
||||||
brand: raw.brands?.[0]?.name || "",
|
brand: raw.brands?.[0]?.name || "",
|
||||||
// مشخصهها از attribute های متریال خوانده میشوند (EAV)
|
// مشخصهها از attribute های متریال خوانده میشوند (EAV)
|
||||||
specs: (raw.attributes || [])
|
specs: (raw.attributes || [])
|
||||||
@@ -643,11 +620,13 @@ function adaptQuote(raw: RawQuote): Quote {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function adaptSupplier(raw: RawSupplier, topic: string, categories: string[]): Supplier {
|
function adaptSupplier(raw: RawSupplier, topic: string, categories: string[]): Supplier {
|
||||||
|
// هر تأمینکننده دقیقاً یک دسته دارد (FK) و بکاند آن را به شکل category
|
||||||
|
// برمیگرداند — منبع اصلی همین است؛ پارامتر categories فقط پشتیبان است.
|
||||||
|
const own = raw.category?.name ? [raw.category.name] : [];
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
topic,
|
topic,
|
||||||
// دستههای ذخیرهشده روی تأمینکننده (بکاند) اولویت دارند؛ وگرنه از quotes استنتاج میشود
|
categories: own.length ? own : categories,
|
||||||
categories: Array.isArray(raw.categories) && raw.categories.length ? raw.categories : categories,
|
|
||||||
name: raw.name,
|
name: raw.name,
|
||||||
location: raw.location || "",
|
location: raw.location || "",
|
||||||
phone: raw.phone || "",
|
phone: raw.phone || "",
|
||||||
@@ -665,54 +644,29 @@ function deriveSupplierTopics(
|
|||||||
materialsById: Map<string, Material>,
|
materialsById: Map<string, Material>,
|
||||||
): Supplier[] {
|
): Supplier[] {
|
||||||
const topics = new Map<string, Set<string>>();
|
const topics = new Map<string, Set<string>>();
|
||||||
const cats = new Map<string, Set<string>>();
|
|
||||||
for (const q of quotes) {
|
for (const q of quotes) {
|
||||||
for (const li of q.line_items) {
|
for (const li of q.line_items) {
|
||||||
const m = materialsById.get(li.material_id);
|
const m = materialsById.get(li.material_id);
|
||||||
if (!m) continue;
|
if (!m) continue;
|
||||||
if (!topics.has(q.supplier_id)) topics.set(q.supplier_id, new Set());
|
if (!topics.has(q.supplier_id)) topics.set(q.supplier_id, new Set());
|
||||||
if (!cats.has(q.supplier_id)) cats.set(q.supplier_id, new Set());
|
|
||||||
if (m.topic) topics.get(q.supplier_id)!.add(m.topic);
|
if (m.topic) topics.get(q.supplier_id)!.add(m.topic);
|
||||||
if (m.category) cats.get(q.supplier_id)!.add(m.category);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rawSuppliers.map((s) => {
|
return rawSuppliers.map((s) => {
|
||||||
const t = topics.get(s.id);
|
const t = topics.get(s.id);
|
||||||
const c = cats.get(s.id);
|
|
||||||
const derivedTopic = t && t.size ? [...t][0] : "";
|
const derivedTopic = t && t.size ? [...t][0] : "";
|
||||||
const derivedCats = c ? [...c].sort((a, b) => a.localeCompare(b, "fa")) : [];
|
|
||||||
// اگر از quotes چیزی پیدا نشد، registry را چک کن
|
// اگر از quotes چیزی پیدا نشد، registry را چک کن
|
||||||
if (!derivedTopic && !derivedCats.length) {
|
if (!derivedTopic) {
|
||||||
const reg = supplierTopicRegistry.get(s.id);
|
const reg = supplierTopicRegistry.get(s.id);
|
||||||
if (reg) {
|
if (reg) return adaptSupplier(s, reg.topic, reg.categories);
|
||||||
return adaptSupplier(s, reg.topic, reg.categories);
|
|
||||||
}
|
}
|
||||||
}
|
// دستهٔ ثبتشده روی خودِ تأمینکننده (FK بکاند) منبع اصلی است و نامش با نام
|
||||||
// v2.7: متریالها از دسته جدا شدند، پس استنتاج از اقلام پیشنهاد معمولاً
|
// تاپیکها یکی است؛ تاپیک از اقلام پیشنهاد استنتاج میشود.
|
||||||
// دستهای نمیدهد و صفحهی تأمینکنندهها بینام میشد. دستهٔ ثبتشده روی
|
|
||||||
// خودِ تأمینکننده (FK بکاند) منبع اصلی است؛ نامش با نام تاپیکها یکی است.
|
|
||||||
const backendCat = s.category?.name || "";
|
const backendCat = s.category?.name || "";
|
||||||
return adaptSupplier(
|
return adaptSupplier(s, derivedTopic || backendCat, backendCat ? [backendCat] : []);
|
||||||
s,
|
|
||||||
derivedTopic || backendCat,
|
|
||||||
derivedCats.length ? derivedCats : backendCat ? [backendCat] : [],
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function adaptCategoryUnit(raw: RawCategoryUnit): CategoryUnit {
|
|
||||||
return { id: raw.id, category: raw.category?.name || "", ref_unit: raw.ref_unit };
|
|
||||||
}
|
|
||||||
function adaptUnitConversion(raw: RawUnitConversion, lookup: Lookup): UnitConversion {
|
|
||||||
return {
|
|
||||||
id: raw.id,
|
|
||||||
category: raw.category?.name || lookup.categoryIdToName.get(raw.category_id) || "",
|
|
||||||
brand: raw.brand || "",
|
|
||||||
from_unit: raw.from_unit,
|
|
||||||
to_unit: raw.to_unit,
|
|
||||||
factor: raw.factor,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
function adaptStandardConversion(raw: RawStandardConversion): StandardConversion {
|
function adaptStandardConversion(raw: RawStandardConversion): StandardConversion {
|
||||||
return { id: raw.id, from_unit: raw.from_unit, to_unit: raw.to_unit, factor: raw.factor };
|
return { id: raw.id, from_unit: raw.from_unit, to_unit: raw.to_unit, factor: raw.factor };
|
||||||
}
|
}
|
||||||
@@ -830,16 +784,14 @@ function registerSupplierTopic(id: string, topic: string, categories: string[]):
|
|||||||
export const api = {
|
export const api = {
|
||||||
// ---------- کل store (یک بار در ریشه) ----------
|
// ---------- کل store (یک بار در ریشه) ----------
|
||||||
async listAll(): Promise<Store> {
|
async listAll(): Promise<Store> {
|
||||||
const [quotesRes, suppliersRes, materialsRes, projectsRes, unitsRes, categoriesRes, cruRes, ucRes, scuRes] =
|
const [quotesRes, suppliersRes, materialsRes, projectsRes, unitsRes, categoriesRes, scuRes] =
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
getData<RawQuote[]>(`/quotes?expand=supplier,project,events,line_items,termsheets,decision,receipt&page_size=${PAGE_SIZE}`),
|
getData<RawQuote[]>(`/quotes?expand=supplier,project,events,line_items,termsheets,decision,receipt&page_size=${PAGE_SIZE}`),
|
||||||
getData<RawSupplier[]>(`/suppliers?page_size=${PAGE_SIZE}`),
|
getData<RawSupplier[]>(`/suppliers?page_size=${PAGE_SIZE}`),
|
||||||
getData<RawMaterial[]>(`/materials?expand=category,brands,canonical_unit,attributes&page_size=${PAGE_SIZE}`),
|
getData<RawMaterial[]>(`/materials?expand=brands,canonical_unit,attributes&page_size=${PAGE_SIZE}`),
|
||||||
getData<RawProject[]>(`/projects?page_size=${PAGE_SIZE}`),
|
getData<RawProject[]>(`/projects?page_size=${PAGE_SIZE}`),
|
||||||
getData<RawUnit[]>(`/units?page_size=${PAGE_SIZE}`),
|
getData<RawUnit[]>(`/units?page_size=${PAGE_SIZE}`),
|
||||||
getData<RawMaterialCategory[]>(`/material-categories?page_size=${PAGE_SIZE}`),
|
getData<RawMaterialCategory[]>(`/material-categories?page_size=${PAGE_SIZE}`),
|
||||||
getData<RawCategoryUnit[]>(`/category-reference-units?expand=category&page_size=${PAGE_SIZE}`),
|
|
||||||
getData<RawUnitConversion[]>(`/unit-conversions?expand=category&page_size=${PAGE_SIZE}`),
|
|
||||||
getData<RawStandardConversion[]>(`/standard-conversions?page_size=${PAGE_SIZE}`),
|
getData<RawStandardConversion[]>(`/standard-conversions?page_size=${PAGE_SIZE}`),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -854,8 +806,6 @@ export const api = {
|
|||||||
materials,
|
materials,
|
||||||
projects: projectsRes.map((p) => ({ id: p.id, name: p.name })),
|
projects: projectsRes.map((p) => ({ id: p.id, name: p.name })),
|
||||||
quotes,
|
quotes,
|
||||||
categoryUnits: cruRes.map(adaptCategoryUnit),
|
|
||||||
unitConversions: ucRes.map((uc) => adaptUnitConversion(uc, lookup)),
|
|
||||||
standardConversions: scuRes.map(adaptStandardConversion),
|
standardConversions: scuRes.map(adaptStandardConversion),
|
||||||
topics: buildTopics(categoriesRes),
|
topics: buildTopics(categoriesRes),
|
||||||
};
|
};
|
||||||
@@ -1130,7 +1080,7 @@ export const api = {
|
|||||||
|
|
||||||
materials: {
|
materials: {
|
||||||
list: async (): Promise<Material[]> => {
|
list: async (): Promise<Material[]> => {
|
||||||
const raw = await getData<RawMaterial[]>(`/materials?expand=category,brands,canonical_unit,attributes&page_size=${PAGE_SIZE}`);
|
const raw = await getData<RawMaterial[]>(`/materials?expand=brands,canonical_unit,attributes&page_size=${PAGE_SIZE}`);
|
||||||
const units = await getData<RawUnit[]>(`/units?page_size=${PAGE_SIZE}`);
|
const units = await getData<RawUnit[]>(`/units?page_size=${PAGE_SIZE}`);
|
||||||
const categories = await getData<RawMaterialCategory[]>(`/material-categories?page_size=${PAGE_SIZE}`);
|
const categories = await getData<RawMaterialCategory[]>(`/material-categories?page_size=${PAGE_SIZE}`);
|
||||||
return raw.map((m) => adaptMaterial(m, buildLookup(units, categories)));
|
return raw.map((m) => adaptMaterial(m, buildLookup(units, categories)));
|
||||||
@@ -1149,7 +1099,7 @@ export const api = {
|
|||||||
// name فقط نامِ خودِ متریال است؛ برند در brand_ids و مشخصهها در
|
// name فقط نامِ خودِ متریال است؛ برند در brand_ids و مشخصهها در
|
||||||
// attributes (EAV) ذخیره میشوند. اگر برند/مشخصه داخل name ریخته شود،
|
// attributes (EAV) ذخیره میشوند. اگر برند/مشخصه داخل name ریخته شود،
|
||||||
// در ویزارد بهعنوان «نامِ متریال» کش میشود و مشخصهها در نام ظاهر میشوند.
|
// در ویزارد بهعنوان «نامِ متریال» کش میشود و مشخصهها در نام ظاهر میشوند.
|
||||||
const name = (m.category || m.brand || (m.specs || []).join(" ") || "").trim() || "کالای جدید";
|
const name = (m.name || m.brand || (m.specs || []).join(" ") || "").trim() || "کالای جدید";
|
||||||
// مشخصهها بهصورت attribute (EAV) ذخیره میشوند تا بعد از رفرش دیده شوند
|
// مشخصهها بهصورت attribute (EAV) ذخیره میشوند تا بعد از رفرش دیده شوند
|
||||||
const attrs: { attribute_definition_id: string; value: string }[] = [];
|
const attrs: { attribute_definition_id: string; value: string }[] = [];
|
||||||
const seen = new Set<string>();
|
const seen = new Set<string>();
|
||||||
@@ -1190,46 +1140,6 @@ export const api = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
units: {
|
units: {
|
||||||
getCategoryUnits: async (): Promise<CategoryUnit[]> => {
|
|
||||||
const raw = await getData<RawCategoryUnit[]>(`/category-reference-units?expand=category&page_size=${PAGE_SIZE}`);
|
|
||||||
return raw.map(adaptCategoryUnit);
|
|
||||||
},
|
|
||||||
saveCategoryUnit: async (cu: CategoryUnit): Promise<void> => {
|
|
||||||
const lookup = await fetchLookup();
|
|
||||||
const categoryId = lookup.categoryNameToId.get(norm(cu.category));
|
|
||||||
if (!categoryId) throw new Error(`دستهبندی «${cu.category}» یافت نشد`);
|
|
||||||
await putData(`/category-reference-units/${categoryId}`, { ref_unit: cu.ref_unit });
|
|
||||||
},
|
|
||||||
getConversions: async (): Promise<UnitConversion[]> => {
|
|
||||||
const lookup = await fetchLookup();
|
|
||||||
const raw = await getData<RawUnitConversion[]>(`/unit-conversions?expand=category&page_size=${PAGE_SIZE}`);
|
|
||||||
return raw.map((uc) => adaptUnitConversion(uc, lookup));
|
|
||||||
},
|
|
||||||
saveConversion: async (c: UnitConversion): Promise<void> => {
|
|
||||||
const lookup = await fetchLookup();
|
|
||||||
const categoryId = lookup.categoryNameToId.get(norm(c.category));
|
|
||||||
if (!categoryId) throw new Error(`دستهبندی «${c.category}» یافت نشد`);
|
|
||||||
const body = {
|
|
||||||
category_id: categoryId,
|
|
||||||
brand: c.brand || null,
|
|
||||||
from_unit: c.from_unit,
|
|
||||||
to_unit: c.to_unit,
|
|
||||||
factor: c.factor,
|
|
||||||
};
|
|
||||||
if (looksLikeUuid(c.id)) {
|
|
||||||
await patchData(`/unit-conversions/${c.id}`, {
|
|
||||||
brand: c.brand || null,
|
|
||||||
from_unit: c.from_unit,
|
|
||||||
to_unit: c.to_unit,
|
|
||||||
factor: c.factor,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
await postData("/unit-conversions", body);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
deleteConversion: async (id: string): Promise<void> => {
|
|
||||||
await deleteData(`/unit-conversions/${id}`);
|
|
||||||
},
|
|
||||||
getStandardConversions: async (): Promise<StandardConversion[]> => {
|
getStandardConversions: async (): Promise<StandardConversion[]> => {
|
||||||
const raw = await getData<RawStandardConversion[]>(`/standard-conversions?page_size=${PAGE_SIZE}`);
|
const raw = await getData<RawStandardConversion[]>(`/standard-conversions?page_size=${PAGE_SIZE}`);
|
||||||
return raw.map(adaptStandardConversion);
|
return raw.map(adaptStandardConversion);
|
||||||
@@ -1775,15 +1685,6 @@ export const apiV26 = {
|
|||||||
types: async (): Promise<{ type: TransactionType; label: string }[]> => {
|
types: async (): Promise<{ type: TransactionType; label: string }[]> => {
|
||||||
return getData<{ type: TransactionType; label: string }[]>("/inventory/meta/types");
|
return getData<{ type: TransactionType; label: string }[]>("/inventory/meta/types");
|
||||||
},
|
},
|
||||||
materialOptions: async (category_id: string, warehouse_id?: string): Promise<{ id: string; name: string; brands: { id: string; name: string }[] }[]> => {
|
|
||||||
const params: Record<string, string> = { category_id };
|
|
||||||
if (warehouse_id) params.warehouse_id = warehouse_id;
|
|
||||||
const pid = activeProjectId();
|
|
||||||
if (pid) params.project_id = pid;
|
|
||||||
return getData<{ id: string; name: string; brands: { id: string; name: string }[] }[]>(
|
|
||||||
`/inventory/materials/options?${new URLSearchParams(params).toString()}`,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
pm: {
|
pm: {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// منطق بیزنس مشترک پیشنهادها — پورت از app.js
|
// منطق بیزنس مشترک پیشنهادها — پورت از app.js
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
import type { LineItem, Material, Quote, StandardConversion, Store, Supplier, UnitConversion } from "./types";
|
import type { LineItem, Material, Quote, Store, Supplier } from "./types";
|
||||||
import { normalize } from "./normalize";
|
import { normalize } from "./normalize";
|
||||||
import { groupCommas } from "./numutil";
|
import { groupCommas } from "./numutil";
|
||||||
import { fmtDate } from "./formatters";
|
import { fmtDate } from "./formatters";
|
||||||
@@ -17,7 +17,6 @@ export function materialById(materials: Material[], id: string): Material {
|
|||||||
id: "",
|
id: "",
|
||||||
name: "",
|
name: "",
|
||||||
topic: "",
|
topic: "",
|
||||||
category: "؟",
|
|
||||||
brand: "",
|
brand: "",
|
||||||
specs: [],
|
specs: [],
|
||||||
base_unit: "",
|
base_unit: "",
|
||||||
@@ -46,7 +45,7 @@ export function specsLabel(specs: string[]): string {
|
|||||||
* [نام برند] . [نام متریال] . [مشخصه ۱] . [مشخصه ۲]
|
* [نام برند] . [نام متریال] . [مشخصه ۱] . [مشخصه ۲]
|
||||||
*/
|
*/
|
||||||
export function formatMaterialTitle(
|
export function formatMaterialTitle(
|
||||||
m: Pick<Material, "name" | "brand" | "specs" | "category"> | null | undefined,
|
m: Pick<Material, "name" | "brand" | "specs"> | null | undefined,
|
||||||
extraSpecs?: string[],
|
extraSpecs?: string[],
|
||||||
): string {
|
): string {
|
||||||
if (!m) return "کالا";
|
if (!m) return "کالا";
|
||||||
@@ -75,7 +74,7 @@ export function formatMaterialTitle(
|
|||||||
if (rawName) {
|
if (rawName) {
|
||||||
parts.push(rawName);
|
parts.push(rawName);
|
||||||
} else if (!rawBrand) {
|
} else if (!rawBrand) {
|
||||||
parts.push(m.category && m.category !== "؟" ? m.category : "کالا");
|
parts.push("کالا");
|
||||||
}
|
}
|
||||||
|
|
||||||
// اضافه کردن مشخصهها بدون تکرار
|
// اضافه کردن مشخصهها بدون تکرار
|
||||||
@@ -95,10 +94,67 @@ export function formatMaterialTitle(
|
|||||||
return parts.join(" . ");
|
return parts.join(" . ");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function materialLabel(m: Pick<Material, "name" | "brand" | "specs" | "category"> | null | undefined): string {
|
export function materialLabel(m: Pick<Material, "name" | "brand" | "specs"> | null | undefined): string {
|
||||||
return formatMaterialTitle(m);
|
return formatMaterialTitle(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// نام پایهی متریال از نامِ ذخیرهشده.
|
||||||
|
// متریالهای جدید فقط نامِ خودِ متریال را در name دارند (برند در brand و
|
||||||
|
// مشخصهها در attributes ذخیره میشوند) و دستنخورده برمیگردند. رکوردهای قدیمی
|
||||||
|
// name را به شکل «برند + مشخصه» ذخیره کردهاند؛ آن قطعات جدا میشوند تا نامِ
|
||||||
|
// پایه باقی بماند.
|
||||||
|
export function materialBaseName(name: string, brand: string, specs: string[]): string {
|
||||||
|
const fallback = (name || "").trim();
|
||||||
|
if (!fallback) return "";
|
||||||
|
let base = fallback;
|
||||||
|
// بلندترین قطعه اول حذف میشود تا حذفهای جزئی، رشته را ناقص نکنند
|
||||||
|
const parts = [brand, ...specs]
|
||||||
|
.map((p) => (p || "").trim())
|
||||||
|
.filter(Boolean)
|
||||||
|
.sort((a, b) => b.length - a.length);
|
||||||
|
for (const p of parts) {
|
||||||
|
if (!base.includes(p)) continue;
|
||||||
|
const next = base.split(p).join(" ").replace(/\s+/g, " ").trim();
|
||||||
|
// اگر حذفِ این قطعه کلِ نام را خالی کند، یعنی همان نامِ متریال بوده — نگهش دار
|
||||||
|
if (!next) continue;
|
||||||
|
base = next;
|
||||||
|
}
|
||||||
|
// شکلِ قدیمیِ خراب میتواند فقط یک مشخصه باقی بگذارد؛ آن دیگر نامِ متریال نیست
|
||||||
|
if (parts.some((p) => p === base)) return fallback;
|
||||||
|
return base || fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* یافتن متریالِ متناظر با یک ردیفِ اقلام.
|
||||||
|
*
|
||||||
|
* هویت متریال = نام + برند + مشخصه. پیش از v2.7 دسته هم در این تطبیق بود، ولی
|
||||||
|
* دسته دیگر روی متریال نیست؛ تکیه بر نام جلوی اشتباهگرفتن دو متریال با
|
||||||
|
* برند و مشخصهی یکسان (مثل دو چمن مصنوعیِ همبرند) را میگیرد. نامِ خالی یعنی
|
||||||
|
* هویت نامعلوم — در آن حالت تطبیق نمیدهیم و متریالِ تازه ساخته میشود.
|
||||||
|
*/
|
||||||
|
export function matchMaterial(
|
||||||
|
materials: Material[],
|
||||||
|
name: string,
|
||||||
|
brand: string,
|
||||||
|
specs: string[],
|
||||||
|
): Material | undefined {
|
||||||
|
const wantName = normalize(name);
|
||||||
|
if (!wantName) return undefined;
|
||||||
|
const wantBrand = normalize(brand);
|
||||||
|
const wantSpecs = specsKey(specs);
|
||||||
|
const sameIdentity = (m: Material) => normalize(m.brand) === wantBrand && specsKey(m.specs) === wantSpecs;
|
||||||
|
const base = normalize(materialBaseName(name, brand, specs));
|
||||||
|
return (
|
||||||
|
materials.find((m) => normalize(m.name) === wantName && sameIdentity(m)) ||
|
||||||
|
materials.find(
|
||||||
|
(m) =>
|
||||||
|
base &&
|
||||||
|
normalize(materialBaseName(m.name, m.brand, m.specs)) === base &&
|
||||||
|
sameIdentity(m),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// آرایهی termsheet_ids یک قلم (هر دو فرمت قدیم/جدید)
|
// آرایهی termsheet_ids یک قلم (هر دو فرمت قدیم/جدید)
|
||||||
export function itemTermsheetIds(li: LineItem): string[] {
|
export function itemTermsheetIds(li: LineItem): string[] {
|
||||||
if (li.termsheet_ids) return li.termsheet_ids;
|
if (li.termsheet_ids) return li.termsheet_ids;
|
||||||
@@ -158,54 +214,6 @@ export function itemProjects(q: Quote, li: LineItem): string[] {
|
|||||||
return (li.projects || []).map((p) => (typeof p === "string" ? p : p.name)).filter(Boolean);
|
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 → لینک به داشبورد واحدها
|
// واحدهای ناشناختهی استفادهشده در یک quote → لینک به داشبورد واحدها
|
||||||
// واحدها دیگر hardcode نیستند — همه از بکاند میآیند؛ هشداری نمایش داده نمیشود
|
// واحدها دیگر hardcode نیستند — همه از بکاند میآیند؛ هشداری نمایش داده نمیشود
|
||||||
export function unitWarning(_store: Store, _q: Quote): { unit: string; cat: string }[] {
|
export function unitWarning(_store: Store, _q: Quote): { unit: string; cat: string }[] {
|
||||||
@@ -275,9 +283,8 @@ export function termsheetSummary(store: Store, q: Quote): { title: string; sub:
|
|||||||
// فیلترهای دفتر
|
// فیلترهای دفتر
|
||||||
export function filterQuotes(
|
export function filterQuotes(
|
||||||
store: Store,
|
store: Store,
|
||||||
filters: { category: string; supplier: string; status: string; search: string },
|
filters: { supplier: string; status: string; search: string },
|
||||||
): Quote[] {
|
): Quote[] {
|
||||||
const catVal = filters.category.trim();
|
|
||||||
const supVal = filters.supplier.trim();
|
const supVal = filters.supplier.trim();
|
||||||
const stVal = filters.status.trim();
|
const stVal = filters.status.trim();
|
||||||
const q = filters.search.trim().toLowerCase();
|
const q = filters.search.trim().toLowerCase();
|
||||||
@@ -300,12 +307,11 @@ export function filterQuotes(
|
|||||||
if (stKey && quote.status !== stKey) return false;
|
if (stKey && quote.status !== stKey) return false;
|
||||||
|
|
||||||
const matInfo = activeItems(quote).map((li) => materialById(store.materials, li.material_id));
|
const matInfo = activeItems(quote).map((li) => materialById(store.materials, li.material_id));
|
||||||
if (catVal && !matInfo.some((m) => m.category === catVal)) return false;
|
|
||||||
|
|
||||||
if (q) {
|
if (q) {
|
||||||
const hay =
|
const hay =
|
||||||
`${supObj.name} ${quote.project || ""} ` +
|
`${supObj.name} ${quote.project || ""} ` +
|
||||||
matInfo.map((m) => `${m.category} ${m.brand} ${materialSpecs(m).join(" ")}`).join(" ");
|
matInfo.map((m) => `${m.brand} ${materialSpecs(m).join(" ")}`).join(" ");
|
||||||
if (!hay.toLowerCase().includes(q)) return false;
|
if (!hay.toLowerCase().includes(q)) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -20,9 +20,8 @@ export interface Supplier {
|
|||||||
// ماده/کالا
|
// ماده/کالا
|
||||||
export interface Material {
|
export interface Material {
|
||||||
id: string; // "m1", "m2", ...
|
id: string; // "m1", "m2", ...
|
||||||
name: string; // نام کامل ذخیرهشده در بکاند (برند + مشخصه)
|
name: string; // نامِ خودِ متریال — برند در brand و مشخصهها در specs
|
||||||
topic: string;
|
topic: string;
|
||||||
category: string; // اختیاری در بکاند؛ همیشه string در فرانت (default: "")
|
|
||||||
brand: string; // مثلاً "ذوب آهن"
|
brand: string; // مثلاً "ذوب آهن"
|
||||||
specs: string[]; // آرایهای از مشخصهها (مثلاً ["A2"])
|
specs: string[]; // آرایهای از مشخصهها (مثلاً ["A2"])
|
||||||
base_unit: string; // واحد پایه
|
base_unit: string; // واحد پایه
|
||||||
@@ -97,23 +96,6 @@ export interface Quote {
|
|||||||
termsheets: Termsheet[];
|
termsheets: Termsheet[];
|
||||||
}
|
}
|
||||||
|
|
||||||
// واحد مرجع هر دسته
|
|
||||||
export interface CategoryUnit {
|
|
||||||
id: string;
|
|
||||||
category: string;
|
|
||||||
ref_unit: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ضریب تبدیل (دستهای)
|
|
||||||
export interface UnitConversion {
|
|
||||||
id: string;
|
|
||||||
category: string;
|
|
||||||
brand: string; // خالی = همه برندها
|
|
||||||
from_unit: string;
|
|
||||||
to_unit: string;
|
|
||||||
factor: number; // 1 from_unit = factor * to_unit
|
|
||||||
}
|
|
||||||
|
|
||||||
// ضریب تبدیل استاندارد (جهانی)
|
// ضریب تبدیل استاندارد (جهانی)
|
||||||
export interface StandardConversion {
|
export interface StandardConversion {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -128,8 +110,6 @@ export interface Store {
|
|||||||
materials: Material[];
|
materials: Material[];
|
||||||
projects: Project[];
|
projects: Project[];
|
||||||
quotes: Quote[];
|
quotes: Quote[];
|
||||||
categoryUnits: CategoryUnit[];
|
|
||||||
unitConversions: UnitConversion[];
|
|
||||||
standardConversions: StandardConversion[];
|
standardConversions: StandardConversion[];
|
||||||
topics: Topic[]; // دستهبندیهای ۸گانه از /material-categories
|
topics: Topic[]; // دستهبندیهای ۸گانه از /material-categories
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import { AppSwitcherMenu } from "../../components/AppSwitcherMenu";
|
|||||||
import { AcceptModal } from "./AcceptModal";
|
import { AcceptModal } from "./AcceptModal";
|
||||||
import { EditModal } from "./EditModal";
|
import { EditModal } from "./EditModal";
|
||||||
|
|
||||||
const EMPTY_FILTERS: Filters = { category: "", supplier: "", status: "", search: "" };
|
const EMPTY_FILTERS: Filters = { supplier: "", status: "", search: "" };
|
||||||
|
|
||||||
export function DashboardPage() {
|
export function DashboardPage() {
|
||||||
useStoreData();
|
useStoreData();
|
||||||
@@ -28,7 +28,7 @@ export function DashboardPage() {
|
|||||||
const suppliers = useSuppliers();
|
const suppliers = useSuppliers();
|
||||||
const materials = useMaterials();
|
const materials = useMaterials();
|
||||||
const projects = useProjects();
|
const projects = useProjects();
|
||||||
const { categoryUnits, unitConversions, standardConversions } = useUnits();
|
const { standardConversions } = useUnits();
|
||||||
|
|
||||||
const expandedQuoteIds = useAppStore((s) => s.expandedQuoteIds);
|
const expandedQuoteIds = useAppStore((s) => s.expandedQuoteIds);
|
||||||
const toggleExpand = useAppStore((s) => s.toggleExpand);
|
const toggleExpand = useAppStore((s) => s.toggleExpand);
|
||||||
@@ -40,8 +40,8 @@ export function DashboardPage() {
|
|||||||
const [editId, setEditId] = useState<string | null>(null);
|
const [editId, setEditId] = useState<string | null>(null);
|
||||||
|
|
||||||
const store = useMemo(
|
const store = useMemo(
|
||||||
() => ({ quotes, suppliers, materials, projects, categoryUnits, unitConversions, standardConversions, topics }),
|
() => ({ quotes, suppliers, materials, projects, standardConversions, topics }),
|
||||||
[quotes, suppliers, materials, projects, categoryUnits, unitConversions, standardConversions, topics],
|
[quotes, suppliers, materials, projects, standardConversions, topics],
|
||||||
);
|
);
|
||||||
|
|
||||||
// فیلتر + مرتبسازی بر اساس زمان ساخت (created_at)
|
// فیلتر + مرتبسازی بر اساس زمان ساخت (created_at)
|
||||||
@@ -80,7 +80,6 @@ export function DashboardPage() {
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const categoryOptions = useMemo(() => [...new Set(materials.map((m) => m.category))], [materials]);
|
|
||||||
const supplierOptions = useMemo(() => suppliers.map((s) => ({ id: s.id, name: s.name })), [suppliers]);
|
const supplierOptions = useMemo(() => suppliers.map((s) => ({ id: s.id, name: s.name })), [suppliers]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -138,7 +137,6 @@ export function DashboardPage() {
|
|||||||
<FilterToolbar
|
<FilterToolbar
|
||||||
filters={filters}
|
filters={filters}
|
||||||
onChange={setFilters}
|
onChange={setFilters}
|
||||||
categories={categoryOptions}
|
|
||||||
suppliers={supplierOptions}
|
suppliers={supplierOptions}
|
||||||
sortDir={sortDir}
|
sortDir={sortDir}
|
||||||
onToggleSort={() => setSortDir((d) => (d === "desc" ? "asc" : "desc"))}
|
onToggleSort={() => setSortDir((d) => (d === "desc" ? "asc" : "desc"))}
|
||||||
|
|||||||
@@ -14,11 +14,10 @@ import {
|
|||||||
useAddSupplier,
|
useAddSupplier,
|
||||||
useAddMaterial,
|
useAddMaterial,
|
||||||
} from "../../hooks";
|
} from "../../hooks";
|
||||||
import { itemTermsheetIds, materialById, materialSpecs, supplierById } from "../../lib/quoteUtils";
|
import { itemTermsheetIds, matchMaterial, materialById, materialSpecs, supplierById } from "../../lib/quoteUtils";
|
||||||
import { STATUS_LABEL, statusOptionsFor } from "../../lib/constants";
|
import { STATUS_LABEL, statusOptionsFor } from "../../lib/constants";
|
||||||
import { faNum } from "../../lib/formatters";
|
import { faNum } from "../../lib/formatters";
|
||||||
import { normalize } from "../../lib/normalize";
|
import { normalize } from "../../lib/normalize";
|
||||||
import { specsKey } from "../../lib/quoteUtils";
|
|
||||||
import { Combobox } from "../../components/Combobox";
|
import { Combobox } from "../../components/Combobox";
|
||||||
import { JalaliDatePicker } from "../../components/JalaliDatePicker";
|
import { JalaliDatePicker } from "../../components/JalaliDatePicker";
|
||||||
import { NumberInput } from "../../components/NumberInput";
|
import { NumberInput } from "../../components/NumberInput";
|
||||||
@@ -154,7 +153,9 @@ export function EditModal({ quoteId, onClose }: EditModalProps) {
|
|||||||
return {
|
return {
|
||||||
key: li.id,
|
key: li.id,
|
||||||
origLi: li,
|
origLi: li,
|
||||||
cat: m.category || "",
|
// «متریال» این ردیف = نامِ خودِ متریال (v2.7: متریال دسته ندارد؛
|
||||||
|
// دسته روی تأمینکننده است و دیگر برچسبِ این فیلد نیست)
|
||||||
|
cat: m.name || "",
|
||||||
brand: m.brand || "",
|
brand: m.brand || "",
|
||||||
specs: materialSpecs(m),
|
specs: materialSpecs(m),
|
||||||
qty: li.quantity,
|
qty: li.quantity,
|
||||||
@@ -294,12 +295,8 @@ export function EditModal({ quoteId, onClose }: EditModalProps) {
|
|||||||
const total = row.total;
|
const total = row.total;
|
||||||
if (!cat && !brand && qty == null && price == null) continue;
|
if (!cat && !brand && qty == null && price == null) continue;
|
||||||
|
|
||||||
let material = materialsCopy.find(
|
// هویت متریال = نام + برند + مشخصه (دیگر دستهای برای تطبیق وجود ندارد)
|
||||||
(m) =>
|
let material = matchMaterial(materialsCopy, cat, brand, specs);
|
||||||
normalize(m.category) === normalize(cat) &&
|
|
||||||
normalize(m.brand) === normalize(brand) &&
|
|
||||||
specsKey(m.specs) === specsKey(specs),
|
|
||||||
);
|
|
||||||
let materialId: string;
|
let materialId: string;
|
||||||
if (material) {
|
if (material) {
|
||||||
materialId = material.id;
|
materialId = material.id;
|
||||||
@@ -308,7 +305,6 @@ export function EditModal({ quoteId, onClose }: EditModalProps) {
|
|||||||
id: nextId("m", materialsCopy),
|
id: nextId("m", materialsCopy),
|
||||||
name: (cat || brand || specs.join(" ") || "").trim() || "کالای جدید",
|
name: (cat || brand || specs.join(" ") || "").trim() || "کالای جدید",
|
||||||
topic: materialById(materials, row.origLi?.material_id || "").topic || "",
|
topic: materialById(materials, row.origLi?.material_id || "").topic || "",
|
||||||
category: cat,
|
|
||||||
brand,
|
brand,
|
||||||
specs,
|
specs,
|
||||||
base_unit: unit,
|
base_unit: unit,
|
||||||
@@ -579,7 +575,7 @@ export function EditModal({ quoteId, onClose }: EditModalProps) {
|
|||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-2.5">
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-2.5">
|
||||||
<div className="flex flex-col gap-1">
|
<div className="flex flex-col gap-1">
|
||||||
<label className="text-[11px] font-semibold text-gray-600">متریال / دسته</label>
|
<label className="text-[11px] font-semibold text-gray-600">متریال</label>
|
||||||
<input
|
<input
|
||||||
disabled={hasReceipts}
|
disabled={hasReceipts}
|
||||||
className="h-9 w-full rounded-md border border-gray-200 bg-white px-2.5 text-xs text-gray-800 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500/30 disabled:cursor-not-allowed disabled:bg-gray-50 disabled:opacity-60"
|
className="h-9 w-full rounded-md border border-gray-200 bg-white px-2.5 text-xs text-gray-800 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500/30 disabled:cursor-not-allowed disabled:bg-gray-50 disabled:opacity-60"
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { SlidersHorizontal, X, ArrowUpDown, RotateCcw } from "lucide-react";
|
|||||||
import { Combobox } from "../../components/Combobox";
|
import { Combobox } from "../../components/Combobox";
|
||||||
|
|
||||||
export interface Filters {
|
export interface Filters {
|
||||||
category: string;
|
|
||||||
supplier: string;
|
supplier: string;
|
||||||
status: string;
|
status: string;
|
||||||
search: string;
|
search: string;
|
||||||
@@ -40,7 +39,6 @@ const STATUS_TO_KEY: Record<string, string> = {
|
|||||||
export interface FilterToolbarProps {
|
export interface FilterToolbarProps {
|
||||||
filters: Filters;
|
filters: Filters;
|
||||||
onChange: (f: Filters) => void;
|
onChange: (f: Filters) => void;
|
||||||
categories: string[];
|
|
||||||
suppliers: { id: string; name: string }[];
|
suppliers: { id: string; name: string }[];
|
||||||
sortDir: "desc" | "asc";
|
sortDir: "desc" | "asc";
|
||||||
onToggleSort: () => void;
|
onToggleSort: () => void;
|
||||||
@@ -49,7 +47,6 @@ export interface FilterToolbarProps {
|
|||||||
export function FilterToolbar({
|
export function FilterToolbar({
|
||||||
filters,
|
filters,
|
||||||
onChange,
|
onChange,
|
||||||
categories,
|
|
||||||
suppliers,
|
suppliers,
|
||||||
sortDir,
|
sortDir,
|
||||||
onToggleSort,
|
onToggleSort,
|
||||||
@@ -58,10 +55,10 @@ export function FilterToolbar({
|
|||||||
const set = (patch: Partial<Filters>) => onChange({ ...filters, ...patch });
|
const set = (patch: Partial<Filters>) => onChange({ ...filters, ...patch });
|
||||||
|
|
||||||
// شمارش تعداد فیلترهای فعال به جز سرچ
|
// شمارش تعداد فیلترهای فعال به جز سرچ
|
||||||
const activeFilterCount = [filters.category, filters.supplier, filters.status].filter(Boolean).length;
|
const activeFilterCount = [filters.supplier, filters.status].filter(Boolean).length;
|
||||||
|
|
||||||
const clearFilters = () => {
|
const clearFilters = () => {
|
||||||
onChange({ ...filters, category: "", supplier: "", status: "" });
|
onChange({ ...filters, supplier: "", status: "" });
|
||||||
};
|
};
|
||||||
|
|
||||||
// تبدیل کلید انگلیسی به لیبل فارسی برای نمایش داخل کامبوباکس
|
// تبدیل کلید انگلیسی به لیبل فارسی برای نمایش داخل کامبوباکس
|
||||||
@@ -153,18 +150,6 @@ export function FilterToolbar({
|
|||||||
|
|
||||||
{/* دراپداونها داخل کشو */}
|
{/* دراپداونها داخل کشو */}
|
||||||
<div className="flex flex-col gap-3.5">
|
<div className="flex flex-col gap-3.5">
|
||||||
<div className="field">
|
|
||||||
<label className="mb-1 block text-xs font-semibold text-muted">دسته</label>
|
|
||||||
<Combobox
|
|
||||||
value={filters.category}
|
|
||||||
onChange={(it: { id: string; label: string }) => set({ category: it.label })}
|
|
||||||
items={categories}
|
|
||||||
allowAdd={false}
|
|
||||||
placeholder="همه دستهها"
|
|
||||||
onInputChange={(v) => set({ category: v })}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="field">
|
<div className="field">
|
||||||
<label className="mb-1 block text-xs font-semibold text-muted">تأمینکننده</label>
|
<label className="mb-1 block text-xs font-semibold text-muted">تأمینکننده</label>
|
||||||
<Combobox
|
<Combobox
|
||||||
@@ -204,17 +189,6 @@ export function FilterToolbar({
|
|||||||
|
|
||||||
{/* ===== نمای دسکتاپ (md به بالا) ===== */}
|
{/* ===== نمای دسکتاپ (md به بالا) ===== */}
|
||||||
<div className="hidden md:flex flex-wrap items-end gap-3">
|
<div className="hidden md:flex flex-wrap items-end gap-3">
|
||||||
<div className="field min-w-[150px] flex-1">
|
|
||||||
<label>دسته</label>
|
|
||||||
<Combobox
|
|
||||||
value={filters.category}
|
|
||||||
onChange={(it: { id: string; label: string }) => set({ category: it.label })}
|
|
||||||
items={categories}
|
|
||||||
allowAdd={false}
|
|
||||||
placeholder="همه"
|
|
||||||
onInputChange={(v) => set({ category: v })}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div className="field min-w-[170px] flex-1">
|
<div className="field min-w-[170px] flex-1">
|
||||||
<label>تأمینکننده</label>
|
<label>تأمینکننده</label>
|
||||||
<Combobox
|
<Combobox
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Package, Scale, CreditCard, CalendarDays, Paperclip, Building2, Ban } from "lucide-react";
|
import { Package, CreditCard, CalendarDays, Paperclip, Building2, Ban } from "lucide-react";
|
||||||
import type { Attachment, Quote, Store } from "../../lib/types";
|
import type { Attachment, Quote, Store } from "../../lib/types";
|
||||||
import { api } from "../../lib/api";
|
import { api } from "../../lib/api";
|
||||||
import {
|
import {
|
||||||
@@ -7,7 +7,6 @@ import {
|
|||||||
itemTermsheetIds,
|
itemTermsheetIds,
|
||||||
lineItemTotal,
|
lineItemTotal,
|
||||||
materialById,
|
materialById,
|
||||||
refConverted,
|
|
||||||
} from "../../lib/quoteUtils";
|
} from "../../lib/quoteUtils";
|
||||||
import { fmtDate, fmtDateTime, fmtNum } from "../../lib/formatters";
|
import { fmtDate, fmtDateTime, fmtNum } from "../../lib/formatters";
|
||||||
import { groupCommas } from "../../lib/numutil";
|
import { groupCommas } from "../../lib/numutil";
|
||||||
@@ -76,7 +75,6 @@ export function QuoteDetail({ store, q }: QuoteDetailProps) {
|
|||||||
const tsIds = itemTermsheetIds(li).filter((tid) => tsList.some((t) => t.id === tid));
|
const tsIds = itemTermsheetIds(li).filter((tid) => tsList.some((t) => t.id === tid));
|
||||||
const total = lineItemTotal(li);
|
const total = lineItemTotal(li);
|
||||||
const projs = itemProjects(q, li);
|
const projs = itemProjects(q, li);
|
||||||
const conv = isRejected ? null : refConverted(store, li);
|
|
||||||
|
|
||||||
// رسید جزیی → قلم به دو بخش «رسید شده» و «در راه» شکسته میشود (نمایشی):
|
// رسید جزیی → قلم به دو بخش «رسید شده» و «در راه» شکسته میشود (نمایشی):
|
||||||
// مجموع دو بخش همیشه با تعداد/مبلغ اصلی برابر است (عدم تغییر جمع کل پیشنهاد).
|
// مجموع دو بخش همیشه با تعداد/مبلغ اصلی برابر است (عدم تغییر جمع کل پیشنهاد).
|
||||||
@@ -140,12 +138,6 @@ export function QuoteDetail({ store, q }: QuoteDetailProps) {
|
|||||||
{whNames.length > 0 && <span>به انبار: {whNames.join("، ")}</span>}
|
{whNames.length > 0 && <span>به انبار: {whNames.join("، ")}</span>}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{conv && (
|
|
||||||
<div className="fb-conv">
|
|
||||||
<Scale className="me-1 inline h-3 w-3" /> ≈ {groupCommas(conv.qty, true)} {conv.unit}
|
|
||||||
{conv.perRef != null ? ` · ${groupCommas(conv.perRef, true)} تومان/${conv.unit}` : ""}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -220,7 +220,7 @@ export function ChangeInventoryPage() {
|
|||||||
let m = matByName(matName);
|
let m = matByName(matName);
|
||||||
if (!m) {
|
if (!m) {
|
||||||
const created = await apiV26.catalog.createMaterial({ name: matName, canonical_unit_id: uid, brand_ids: bid ? [bid] : [] });
|
const created = await apiV26.catalog.createMaterial({ name: matName, canonical_unit_id: uid, brand_ids: bid ? [bid] : [] });
|
||||||
m = { id: created.id, name: created.name, category: null, canonical_unit: null };
|
m = { id: created.id, name: created.name, canonical_unit: null };
|
||||||
}
|
}
|
||||||
const op: Record<string, unknown> = {
|
const op: Record<string, unknown> = {
|
||||||
transaction_type: "PURCHASE", warehouse_id: whId, material_id: m.id, brand_id: bid,
|
transaction_type: "PURCHASE", warehouse_id: whId, material_id: m.id, brand_id: bid,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { useAuthStore } from "../../store/auth";
|
|||||||
import { AppSwitcherMenu } from "../../components/AppSwitcherMenu";
|
import { AppSwitcherMenu } from "../../components/AppSwitcherMenu";
|
||||||
import { inventorySubheader } from "./panelMenu";
|
import { inventorySubheader } from "./panelMenu";
|
||||||
import { InventoryTopNav } from "./InventoryTopNav";
|
import { InventoryTopNav } from "./InventoryTopNav";
|
||||||
import { useInventoryData, brandName, catName, materialName, unitName, whName } from "./useInventoryData";
|
import { useInventoryData, brandName, materialName, unitName, whName } from "./useInventoryData";
|
||||||
import type { InventoryRow, InventoryTransaction, TransactionType } from "../../lib/types";
|
import type { InventoryRow, InventoryTransaction, TransactionType } from "../../lib/types";
|
||||||
import { faNum } from "../../lib/formatters";
|
import { faNum } from "../../lib/formatters";
|
||||||
import { isoToJalaliStr } from "../../lib/jalali";
|
import { isoToJalaliStr } from "../../lib/jalali";
|
||||||
@@ -55,8 +55,7 @@ export function InventoryPage() {
|
|||||||
const isPM = user?.role === "pm";
|
const isPM = user?.role === "pm";
|
||||||
const [params] = useSearchParams();
|
const [params] = useSearchParams();
|
||||||
|
|
||||||
const [filters, setFilters] = useState<{ category: string; warehouse: string; material: string; project: string }>({
|
const [filters, setFilters] = useState<{ warehouse: string; material: string; project: string }>({
|
||||||
category: "",
|
|
||||||
warehouse: params.get("warehouse") || "",
|
warehouse: params.get("warehouse") || "",
|
||||||
material: "",
|
material: "",
|
||||||
project: "",
|
project: "",
|
||||||
@@ -81,7 +80,6 @@ export function InventoryPage() {
|
|||||||
inventory.forEach((r) => {
|
inventory.forEach((r) => {
|
||||||
const whId = r.warehouse?.id;
|
const whId = r.warehouse?.id;
|
||||||
if (!whId || !visibleIds.has(whId)) return;
|
if (!whId || !visibleIds.has(whId)) return;
|
||||||
if (filters.category && r.material.category?.id !== filters.category) return;
|
|
||||||
if (filters.warehouse && whId !== filters.warehouse) return;
|
if (filters.warehouse && whId !== filters.warehouse) return;
|
||||||
if (filters.material && !r.material.name.includes(filters.material.trim())) return;
|
if (filters.material && !r.material.name.includes(filters.material.trim())) return;
|
||||||
const gk = `${whId}|${r.material.id}|${specsHash(r.specs)}`;
|
const gk = `${whId}|${r.material.id}|${specsHash(r.specs)}`;
|
||||||
@@ -132,7 +130,7 @@ export function InventoryPage() {
|
|||||||
.slice()
|
.slice()
|
||||||
.sort((a, b) => new Date(a.transaction_date).getTime() - new Date(b.transaction_date).getTime());
|
.sort((a, b) => new Date(a.transaction_date).getTime() - new Date(b.transaction_date).getTime());
|
||||||
|
|
||||||
const hasFilter = !!(filters.category || filters.warehouse || (isAdmin && filters.project));
|
const hasFilter = !!(filters.warehouse || (isAdmin && filters.project));
|
||||||
|
|
||||||
if (loading) return <div className="min-h-screen" />;
|
if (loading) return <div className="min-h-screen" />;
|
||||||
if (!user || (!isAdmin && !isPM)) {
|
if (!user || (!isAdmin && !isPM)) {
|
||||||
@@ -248,40 +246,6 @@ export function InventoryPage() {
|
|||||||
{/* فیلترهای پیشرفته — برچسبهای لمسی بدون سلکت و بدون بیرونزدگی */}
|
{/* فیلترهای پیشرفته — برچسبهای لمسی بدون سلکت و بدون بیرونزدگی */}
|
||||||
{showMobileFilters && (
|
{showMobileFilters && (
|
||||||
<div className="mt-2.5 border-t border-line/60 pt-2.5 flex flex-col gap-2.5">
|
<div className="mt-2.5 border-t border-line/60 pt-2.5 flex flex-col gap-2.5">
|
||||||
{/* دستهبندیها */}
|
|
||||||
<div>
|
|
||||||
<div className="mb-1.5 text-[11px] font-bold text-muted">دستهبندی متریال:</div>
|
|
||||||
<div className="flex flex-wrap gap-1.5">
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={() => setFilters((f) => ({ ...f, category: "" }))}
|
|
||||||
className={cn(
|
|
||||||
"rounded-lg px-2.5 py-1 text-[11.5px] transition-colors",
|
|
||||||
!filters.category
|
|
||||||
? "bg-accent text-white font-bold"
|
|
||||||
: "border border-line bg-bg/40 text-ink hover:bg-panel"
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
همه دستهبندیها
|
|
||||||
</button>
|
|
||||||
{lookup.categories.map((c) => (
|
|
||||||
<button
|
|
||||||
key={c.id}
|
|
||||||
type="button"
|
|
||||||
onClick={() => setFilters((f) => ({ ...f, category: f.category === c.id ? "" : c.id }))}
|
|
||||||
className={cn(
|
|
||||||
"rounded-lg px-2.5 py-1 text-[11.5px] transition-colors",
|
|
||||||
filters.category === c.id
|
|
||||||
? "bg-accent text-white font-bold"
|
|
||||||
: "border border-line bg-bg/40 text-ink hover:bg-panel"
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{c.name}
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* پروژهها (مخصوص ادمین) */}
|
{/* پروژهها (مخصوص ادمین) */}
|
||||||
{isAdmin && lookup.projects.length > 0 && (
|
{isAdmin && lookup.projects.length > 0 && (
|
||||||
<div>
|
<div>
|
||||||
@@ -324,7 +288,7 @@ export function InventoryPage() {
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="text-xs text-red font-medium hover:underline inline-flex items-center gap-1"
|
className="text-xs text-red font-medium hover:underline inline-flex items-center gap-1"
|
||||||
onClick={() => setFilters({ category: "", warehouse: "", material: "", project: "" })}
|
onClick={() => setFilters({ warehouse: "", material: "", project: "" })}
|
||||||
>
|
>
|
||||||
<X className="h-3 w-3" />
|
<X className="h-3 w-3" />
|
||||||
پاک کردن فیلترها
|
پاک کردن فیلترها
|
||||||
@@ -347,7 +311,6 @@ export function InventoryPage() {
|
|||||||
) : (
|
) : (
|
||||||
groups.map((g) => {
|
groups.map((g) => {
|
||||||
const isExp = expandedKey === g.key;
|
const isExp = expandedKey === g.key;
|
||||||
const matCat = lookup.materials.find((m) => m.id === g.matId)?.category;
|
|
||||||
return (
|
return (
|
||||||
<InventoryMobileCard
|
<InventoryMobileCard
|
||||||
key={g.key}
|
key={g.key}
|
||||||
@@ -355,7 +318,6 @@ export function InventoryPage() {
|
|||||||
isExp={isExp}
|
isExp={isExp}
|
||||||
isAdmin={isAdmin}
|
isAdmin={isAdmin}
|
||||||
matName={materialName(lookup.materials, g.matId)}
|
matName={materialName(lookup.materials, g.matId)}
|
||||||
matCatName={matCat ? catName(lookup.categories, matCat.id) : "—"}
|
|
||||||
wh={whName(warehouses, g.whId)}
|
wh={whName(warehouses, g.whId)}
|
||||||
projName={lookup.projects.find((p) => p.id === warehouses.find((w) => w.id === g.whId)?.project_id)?.name || ""}
|
projName={lookup.projects.find((p) => p.id === warehouses.find((w) => w.id === g.whId)?.project_id)?.name || ""}
|
||||||
unit={unitName(lookup.units, g.unitId)}
|
unit={unitName(lookup.units, g.unitId)}
|
||||||
@@ -410,7 +372,6 @@ export function InventoryPage() {
|
|||||||
<tbody>
|
<tbody>
|
||||||
{groups.map((g) => {
|
{groups.map((g) => {
|
||||||
const isExp = expandedKey === g.key;
|
const isExp = expandedKey === g.key;
|
||||||
const matCat = lookup.materials.find((m) => m.id === g.matId)?.category;
|
|
||||||
return (
|
return (
|
||||||
<GroupRow
|
<GroupRow
|
||||||
key={g.key}
|
key={g.key}
|
||||||
@@ -418,7 +379,6 @@ export function InventoryPage() {
|
|||||||
isExp={isExp}
|
isExp={isExp}
|
||||||
isAdmin={isAdmin}
|
isAdmin={isAdmin}
|
||||||
matName={materialName(lookup.materials, g.matId)}
|
matName={materialName(lookup.materials, g.matId)}
|
||||||
matCatName={matCat ? catName(lookup.categories, matCat.id) : "—"}
|
|
||||||
wh={whName(warehouses, g.whId)}
|
wh={whName(warehouses, g.whId)}
|
||||||
projName={lookup.projects.find((p) => p.id === warehouses.find((w) => w.id === g.whId)?.project_id)?.name || ""}
|
projName={lookup.projects.find((p) => p.id === warehouses.find((w) => w.id === g.whId)?.project_id)?.name || ""}
|
||||||
unit={unitName(lookup.units, g.unitId)}
|
unit={unitName(lookup.units, g.unitId)}
|
||||||
@@ -475,7 +435,6 @@ function InventoryMobileCard({
|
|||||||
isExp,
|
isExp,
|
||||||
isAdmin,
|
isAdmin,
|
||||||
matName,
|
matName,
|
||||||
matCatName,
|
|
||||||
wh,
|
wh,
|
||||||
projName,
|
projName,
|
||||||
unit,
|
unit,
|
||||||
@@ -486,7 +445,6 @@ function InventoryMobileCard({
|
|||||||
isExp: boolean;
|
isExp: boolean;
|
||||||
isAdmin: boolean;
|
isAdmin: boolean;
|
||||||
matName: string;
|
matName: string;
|
||||||
matCatName: string;
|
|
||||||
wh: string;
|
wh: string;
|
||||||
projName: string;
|
projName: string;
|
||||||
unit: string;
|
unit: string;
|
||||||
@@ -508,11 +466,6 @@ function InventoryMobileCard({
|
|||||||
<h3 className="text-[13.5px] font-bold leading-snug text-ink break-words">
|
<h3 className="text-[13.5px] font-bold leading-snug text-ink break-words">
|
||||||
{matName}
|
{matName}
|
||||||
</h3>
|
</h3>
|
||||||
{matCatName !== "—" && (
|
|
||||||
<span className="mt-0.5 inline-block text-[11px] text-muted">
|
|
||||||
{matCatName}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
@@ -574,12 +527,11 @@ function InventoryMobileCard({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function GroupRow({ g, isExp, isAdmin, matName, matCatName, wh, projName, unit, onToggle, timeline }: {
|
function GroupRow({ g, isExp, isAdmin, matName, wh, projName, unit, onToggle, timeline }: {
|
||||||
g: Group;
|
g: Group;
|
||||||
isExp: boolean;
|
isExp: boolean;
|
||||||
isAdmin: boolean;
|
isAdmin: boolean;
|
||||||
matName: string;
|
matName: string;
|
||||||
matCatName: string;
|
|
||||||
wh: string;
|
wh: string;
|
||||||
projName: string;
|
projName: string;
|
||||||
unit: string;
|
unit: string;
|
||||||
@@ -591,7 +543,6 @@ function GroupRow({ g, isExp, isAdmin, matName, matCatName, wh, projName, unit,
|
|||||||
<tr className={`border-b border-line/60 cursor-pointer transition-colors ${isExp ? "bg-accent/5" : "hover:bg-accent/5"}`} onClick={onToggle}>
|
<tr className={`border-b border-line/60 cursor-pointer transition-colors ${isExp ? "bg-accent/5" : "hover:bg-accent/5"}`} onClick={onToggle}>
|
||||||
<td className="py-2.5 text-center text-muted">{isExp ? "▾" : "▸"}</td>
|
<td className="py-2.5 text-center text-muted">{isExp ? "▾" : "▸"}</td>
|
||||||
<td className="py-2.5 pe-3 font-bold text-ink">{matName}</td>
|
<td className="py-2.5 pe-3 font-bold text-ink">{matName}</td>
|
||||||
<td className="py-2.5 pe-3 text-muted">{matCatName}</td>
|
|
||||||
<td className="py-2.5 pe-3">
|
<td className="py-2.5 pe-3">
|
||||||
<div>{wh}</div>
|
<div>{wh}</div>
|
||||||
{isAdmin && <span className="block text-[10.5px] text-muted">{projName}</span>}
|
{isAdmin && <span className="block text-[10.5px] text-muted">{projName}</span>}
|
||||||
|
|||||||
@@ -9,10 +9,9 @@ import { useDataChanged } from "../../lib/crossTab";
|
|||||||
import type { InventoryRow, InventoryTransaction, Warehouse } from "../../lib/types";
|
import type { InventoryRow, InventoryTransaction, Warehouse } from "../../lib/types";
|
||||||
|
|
||||||
export interface LookupData {
|
export interface LookupData {
|
||||||
categories: { id: string; slug: string; name: string }[];
|
|
||||||
units: { id: number; code: string; name_fa: string | null }[];
|
units: { id: number; code: string; name_fa: string | null }[];
|
||||||
brands: { id: string; name: string }[];
|
brands: { id: string; name: string }[];
|
||||||
materials: { id: string; name: string; category: { id: string; name: string } | null; canonical_unit: { id: number; code: string; name_fa: string | null } | null }[];
|
materials: { id: string; name: string; canonical_unit: { id: number; code: string; name_fa: string | null } | null }[];
|
||||||
projects: { id: string; name: string; pm_user_id: string | null; pm_name?: string | null }[];
|
projects: { id: string; name: string; pm_user_id: string | null; pm_name?: string | null }[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,14 +33,13 @@ export function useInventoryData(): InventoryData {
|
|||||||
const [inventory, setInventory] = useState<InventoryRow[]>([]);
|
const [inventory, setInventory] = useState<InventoryRow[]>([]);
|
||||||
const [transactions, setTransactions] = useState<InventoryTransaction[]>([]);
|
const [transactions, setTransactions] = useState<InventoryTransaction[]>([]);
|
||||||
const [lookup, setLookup] = useState<LookupData>({
|
const [lookup, setLookup] = useState<LookupData>({
|
||||||
categories: [], units: [], brands: [], materials: [], projects: [],
|
units: [], brands: [], materials: [], projects: [],
|
||||||
});
|
});
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
const load = useCallback(async () => {
|
const load = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const [cats, units, brands, mats, projs, whs, inv, txs] = await Promise.all([
|
const [units, brands, mats, projs, whs, inv, txs] = await Promise.all([
|
||||||
apiV26.lookup.categories(),
|
|
||||||
apiV26.lookup.units(),
|
apiV26.lookup.units(),
|
||||||
apiV26.lookup.brands(),
|
apiV26.lookup.brands(),
|
||||||
apiV26.lookup.materials(),
|
apiV26.lookup.materials(),
|
||||||
@@ -50,7 +48,7 @@ export function useInventoryData(): InventoryData {
|
|||||||
apiV26.inventory.list({ project_id: projectId }),
|
apiV26.inventory.list({ project_id: projectId }),
|
||||||
apiV26.inventory.transactions({ project_id: projectId }),
|
apiV26.inventory.transactions({ project_id: projectId }),
|
||||||
]);
|
]);
|
||||||
setLookup({ categories: cats, units, brands, materials: mats, projects: projs });
|
setLookup({ units, brands, materials: mats, projects: projs });
|
||||||
setWarehouses(whs);
|
setWarehouses(whs);
|
||||||
setInventory(inv);
|
setInventory(inv);
|
||||||
setTransactions(txs);
|
setTransactions(txs);
|
||||||
@@ -108,11 +106,6 @@ export function materialName(materials: LookupData["materials"], id: string): st
|
|||||||
return m ? m.name : "—";
|
return m ? m.name : "—";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function catName(categories: LookupData["categories"], id: string | null | undefined): string {
|
|
||||||
const c = categories.find((x) => x.id === id);
|
|
||||||
return c ? c.name : "—";
|
|
||||||
}
|
|
||||||
|
|
||||||
export function projName(projects: LookupData["projects"], id: string | null | undefined): string {
|
export function projName(projects: LookupData["projects"], id: string | null | undefined): string {
|
||||||
const p = projects.find((x) => x.id === id);
|
const p = projects.find((x) => x.id === id);
|
||||||
return p ? p.name : "—";
|
return p ? p.name : "—";
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import {
|
|||||||
lineItemTotal,
|
lineItemTotal,
|
||||||
materialById,
|
materialById,
|
||||||
materialLabel,
|
materialLabel,
|
||||||
refConverted,
|
|
||||||
supplierById,
|
supplierById,
|
||||||
} from "../../lib/quoteUtils";
|
} from "../../lib/quoteUtils";
|
||||||
import { faNum, fmtDate } from "../../lib/formatters";
|
import { faNum, fmtDate } from "../../lib/formatters";
|
||||||
@@ -81,11 +80,11 @@ export function ProjectsPage() {
|
|||||||
const quotes = useQuotes();
|
const quotes = useQuotes();
|
||||||
const suppliers = useSuppliers();
|
const suppliers = useSuppliers();
|
||||||
const materials = useMaterials();
|
const materials = useMaterials();
|
||||||
const { categoryUnits, unitConversions, standardConversions } = useUnits();
|
const { standardConversions } = useUnits();
|
||||||
const addProject = useAddProject();
|
const addProject = useAddProject();
|
||||||
const topics = useAppStore((s) => s.topics);
|
const topics = useAppStore((s) => s.topics);
|
||||||
|
|
||||||
const store: Store = { quotes, suppliers, materials, projects, categoryUnits, unitConversions, standardConversions, topics };
|
const store: Store = { quotes, suppliers, materials, projects, standardConversions, topics };
|
||||||
|
|
||||||
const [openProject, setOpenProject] = useState<string | null>(null);
|
const [openProject, setOpenProject] = useState<string | null>(null);
|
||||||
const [newName, setNewName] = useState("");
|
const [newName, setNewName] = useState("");
|
||||||
@@ -189,17 +188,10 @@ function renderProjectDetail(store: Store, name: string): React.ReactElement {
|
|||||||
|
|
||||||
const itemLine = (li: Quote["line_items"][number]) => {
|
const itemLine = (li: Quote["line_items"][number]) => {
|
||||||
const total = lineItemTotal(li);
|
const total = lineItemTotal(li);
|
||||||
const conv = refConverted(store, li);
|
|
||||||
return (
|
return (
|
||||||
<div key={li.id}>
|
<div key={li.id}>
|
||||||
{matName(li.material_id)} — {faNum(li.quantity)} {li.unit} × {faNum(li.unit_price || 0)} ={" "}
|
{matName(li.material_id)} — {faNum(li.quantity)} {li.unit} × {faNum(li.unit_price || 0)} ={" "}
|
||||||
<b>{faNum(total || 0)} تومان</b>
|
<b>{faNum(total || 0)} تومان</b>
|
||||||
{conv && (
|
|
||||||
<span className="pj-conv">
|
|
||||||
{" "}
|
|
||||||
— <span className="pj-conv">≈ {faNum(conv.qty)} {conv.unit}({faNum(conv.perRef ?? 0)} تومان/{conv.unit})</span>
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -48,20 +48,8 @@ export function SupplierDetailPage() {
|
|||||||
return [...set].sort((a, b) => a.localeCompare(b, "fa"));
|
return [...set].sort((a, b) => a.localeCompare(b, "fa"));
|
||||||
}, [materials]);
|
}, [materials]);
|
||||||
|
|
||||||
// متریالهای خریداریشده از این تأمینکننده (از پیشنهادها)
|
// دستهی این تأمینکننده — دسته فقط برچسبِ خودِ تأمینکننده است (v2.7)
|
||||||
const purchased = useMemo(() => {
|
const purchased = useMemo(() => [...(s?.categories || [])].sort((a, b) => a.localeCompare(b, "fa")), [s]);
|
||||||
const out = new Set<string>();
|
|
||||||
quotes.forEach((q) => {
|
|
||||||
if (q.supplier_id !== id) return;
|
|
||||||
(q.line_items || []).forEach((li) => {
|
|
||||||
if (li.rejected) return;
|
|
||||||
const m = materials.find((x) => x.id === li.material_id);
|
|
||||||
if (!m || !m.category) return;
|
|
||||||
out.add(m.category);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return [...out].sort((a, b) => a.localeCompare(b, "fa"));
|
|
||||||
}, [quotes, materials, id]);
|
|
||||||
|
|
||||||
if (!s) {
|
if (!s) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,20 +1,9 @@
|
|||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { MapPin, Phone, User, Plus, Timer } from "lucide-react";
|
import { MapPin, Phone, User, Plus, Timer } from "lucide-react";
|
||||||
import type { Material, Supplier } from "../../lib/types";
|
import type { Supplier } from "../../lib/types";
|
||||||
import { normalize } from "../../lib/normalize";
|
import { normalize } from "../../lib/normalize";
|
||||||
import { fmtNum } from "../../lib/formatters";
|
import { fmtNum } from "../../lib/formatters";
|
||||||
|
|
||||||
export function topicCategories(topic: string, materials: Material[], suppliers: Supplier[]): string[] {
|
|
||||||
const out = new Set<string>();
|
|
||||||
materials
|
|
||||||
.filter((m) => topic && normalize(m.topic) === normalize(topic) && m.category)
|
|
||||||
.forEach((m) => out.add(m.category));
|
|
||||||
suppliers
|
|
||||||
.filter((s) => topic && normalize(s.topic) === normalize(topic))
|
|
||||||
.forEach((s) => (s.categories || []).forEach((c) => c && out.add(c)));
|
|
||||||
return [...out].sort((a, b) => a.localeCompare(b, "fa"));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function suppliersOfCategory(topic: string, cat: string, suppliers: Supplier[]): Supplier[] {
|
export function suppliersOfCategory(topic: string, cat: string, suppliers: Supplier[]): Supplier[] {
|
||||||
return suppliers.filter(
|
return suppliers.filter(
|
||||||
(s) => normalize(s.topic) === normalize(topic) && (s.categories || []).some((c) => normalize(c) === normalize(cat)),
|
(s) => normalize(s.topic) === normalize(topic) && (s.categories || []).some((c) => normalize(c) === normalize(cat)),
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useMemo, useState } from "react";
|
import { useMemo, useState } from "react";
|
||||||
import type { Material, Supplier } from "../../lib/types";
|
import type { Supplier } from "../../lib/types";
|
||||||
import { normalize } from "../../lib/normalize";
|
import { normalize } from "../../lib/normalize";
|
||||||
import { useSuppliers } from "../../hooks";
|
import { useSuppliers } from "../../hooks";
|
||||||
import { useAppStore } from "../../store";
|
import { useAppStore } from "../../store";
|
||||||
@@ -179,14 +179,3 @@ export function Step1TopicSupplier({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// دستههای موجود داخل یک تاپیک (از materials + تأمینکنندهها)
|
|
||||||
export function topicCategories(topic: string, suppliers: Supplier[], materials: Material[]): string[] {
|
|
||||||
const out = new Set<string>();
|
|
||||||
materials
|
|
||||||
.filter((m) => topic && normalize(m.topic) === normalize(topic) && m.category)
|
|
||||||
.forEach((m) => out.add(m.category));
|
|
||||||
suppliers
|
|
||||||
.filter((s) => topic && normalize(s.topic) === normalize(topic))
|
|
||||||
.forEach((s) => (s.categories || []).forEach((c) => c && out.add(c)));
|
|
||||||
return [...out].sort((a, b) => a.localeCompare(b, "fa"));
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { useEffect, useMemo, useState } from "react";
|
|||||||
import type { Material } from "../../lib/types";
|
import type { Material } from "../../lib/types";
|
||||||
import { normalize } from "../../lib/normalize";
|
import { normalize } from "../../lib/normalize";
|
||||||
import { useMaterials } from "../../hooks";
|
import { useMaterials } from "../../hooks";
|
||||||
import { specsKey } from "../../lib/quoteUtils";
|
import { matchMaterial, materialBaseName } from "../../lib/quoteUtils";
|
||||||
import { Combobox } from "../../components/Combobox";
|
import { Combobox } from "../../components/Combobox";
|
||||||
import { NumberInput } from "../../components/NumberInput";
|
import { NumberInput } from "../../components/NumberInput";
|
||||||
import { MultiSelectCombobox } from "../../components/MultiSelectCombobox";
|
import { MultiSelectCombobox } from "../../components/MultiSelectCombobox";
|
||||||
@@ -41,38 +41,12 @@ export function itemRowLabel(it: ItemDraft): string {
|
|||||||
return qtyPart ? `${name} -- ${qtyPart}` : name;
|
return qtyPart ? `${name} -- ${qtyPart}` : name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// نام پایهی متریال از نامِ ذخیرهشده.
|
// مقادیر مجزای name/brand/spec در کاتالوگ سراسری
|
||||||
// متریالهای جدید فقط نامِ خودِ متریال را در name دارند (برند در brand_ids و
|
|
||||||
// مشخصهها در attributes ذخیره میشوند) و دستنخورده برمیگردند. رکوردهای قدیمی
|
|
||||||
// name را به شکل «برند + دسته + مشخصه» ذخیره کردهاند؛ آن قطعات جدا میشوند تا
|
|
||||||
// نامِ پایه باقی بماند.
|
|
||||||
function materialBaseName(name: string, brand: string, specs: string[], category = ""): string {
|
|
||||||
const fallback = (name || "").trim();
|
|
||||||
if (!fallback) return "";
|
|
||||||
let base = fallback;
|
|
||||||
// بلندترین قطعه اول حذف میشود تا حذفهای جزئی، رشته را ناقص نکنند
|
|
||||||
const parts = [brand, category, ...specs]
|
|
||||||
.map((p) => (p || "").trim())
|
|
||||||
.filter(Boolean)
|
|
||||||
.sort((a, b) => b.length - a.length);
|
|
||||||
for (const p of parts) {
|
|
||||||
if (!base.includes(p)) continue;
|
|
||||||
const next = base.split(p).join(" ").replace(/\s+/g, " ").trim();
|
|
||||||
// اگر حذفِ این قطعه کلِ نام را خالی کند، یعنی همان نامِ متریال بوده — نگهش دار
|
|
||||||
if (!next) continue;
|
|
||||||
base = next;
|
|
||||||
}
|
|
||||||
// شکلِ قدیمیِ خراب میتواند فقط یک مشخصه باقی بگذارد؛ آن دیگر نامِ متریال نیست
|
|
||||||
if (parts.some((p) => p === base)) return fallback;
|
|
||||||
return base || fallback;
|
|
||||||
}
|
|
||||||
|
|
||||||
// مقادیر مجزای category/brand/spec در کاتالوگ سراسری
|
|
||||||
export function materialOptions(
|
export function materialOptions(
|
||||||
materials: Material[],
|
materials: Material[],
|
||||||
_topic: string,
|
_topic: string,
|
||||||
item: ItemDraft,
|
item: ItemDraft,
|
||||||
field: "cat" | "brand" | "spec",
|
field: "brand" | "spec",
|
||||||
allItems: ItemDraft[] = [],
|
allItems: ItemDraft[] = [],
|
||||||
apiBrands: string[] = [],
|
apiBrands: string[] = [],
|
||||||
apiSpecs: string[] = [],
|
apiSpecs: string[] = [],
|
||||||
@@ -92,19 +66,19 @@ export function materialOptions(
|
|||||||
return [...out];
|
return [...out];
|
||||||
};
|
};
|
||||||
|
|
||||||
if (field === "cat") {
|
|
||||||
const base = new Set<string>(materials.map((m) => m.category).filter(Boolean));
|
|
||||||
fromRows((it) => it.cat).forEach((c) => base.add(c));
|
|
||||||
return [...base].sort((a, b) => a.localeCompare(b, "fa"));
|
|
||||||
}
|
|
||||||
if (field === "brand") {
|
if (field === "brand") {
|
||||||
// برند سراسری است — مستقل از تاپیک و دسته
|
// برند سراسری است — مستقل از تاپیک و متریال
|
||||||
const allBrands = new Set(materials.map((m) => m.brand).filter(Boolean));
|
const allBrands = new Set(materials.map((m) => m.brand).filter(Boolean));
|
||||||
apiBrands.forEach((b) => b && allBrands.add(b));
|
apiBrands.forEach((b) => b && allBrands.add(b));
|
||||||
fromRows((it) => it.brand).forEach((b) => allBrands.add(b));
|
fromRows((it) => it.brand).forEach((b) => allBrands.add(b));
|
||||||
return [...allBrands].sort((a, b) => a.localeCompare(b, "fa"));
|
return [...allBrands].sort((a, b) => a.localeCompare(b, "fa"));
|
||||||
}
|
}
|
||||||
if (cat) list = list.filter((m) => normalize(m.category) === normalize(cat));
|
// فیلدِ اولِ هر ردیف «نام متریال» است (نه دسته) — مشخصههای متریالِ
|
||||||
|
// همنام پیشنهاد میشوند.
|
||||||
|
if (cat)
|
||||||
|
list = list.filter(
|
||||||
|
(m) => normalize(materialBaseName(m.name, m.brand, m.specs)) === normalize(cat),
|
||||||
|
);
|
||||||
// مشخصهها سراسری هستند: رجیستری attribute-definitions + مشخصههای همهی
|
// مشخصهها سراسری هستند: رجیستری attribute-definitions + مشخصههای همهی
|
||||||
// متریالها + مشخصههای تایپشده در سایر ردیفها. به تاپیک/دسته/برندِ قلمِ
|
// متریالها + مشخصههای تایپشده در سایر ردیفها. به تاپیک/دسته/برندِ قلمِ
|
||||||
// فعلی گره نمیخورند تا هنگام افزودن پیشنهاد جدید همیشه در دسترس باشند.
|
// فعلی گره نمیخورند تا هنگام افزودن پیشنهاد جدید همیشه در دسترس باشند.
|
||||||
@@ -135,7 +109,7 @@ export function Step2LineItems({ items, onItemsChange, selectedTopic, onRemember
|
|||||||
const allMaterialNames = useMemo(() => {
|
const allMaterialNames = useMemo(() => {
|
||||||
const set = new Set<string>();
|
const set = new Set<string>();
|
||||||
materials.forEach((m) => {
|
materials.forEach((m) => {
|
||||||
const base = materialBaseName(m.name, m.brand, m.specs, m.category);
|
const base = materialBaseName(m.name, m.brand, m.specs);
|
||||||
if (base) set.add(base);
|
if (base) set.add(base);
|
||||||
});
|
});
|
||||||
return [...set].sort((a, b) => a.localeCompare(b, "fa"));
|
return [...set].sort((a, b) => a.localeCompare(b, "fa"));
|
||||||
@@ -193,13 +167,9 @@ export function Step2LineItems({ items, onItemsChange, selectedTopic, onRemember
|
|||||||
if (!it.specs.length) return;
|
if (!it.specs.length) return;
|
||||||
rec = { topic: selectedTopic, category: it.cat.trim(), brand: it.brand.trim(), specs: it.specs };
|
rec = { topic: selectedTopic, category: it.cat.trim(), brand: it.brand.trim(), specs: it.specs };
|
||||||
}
|
}
|
||||||
const dup = materials.some(
|
// rec.category در واقع نامِ متریال است (فیلدِ اولِ ردیف) — تطبیق بر اساس
|
||||||
(m) =>
|
// همان هویتِ متریال انجام میشود، نه دسته.
|
||||||
normalize(m.topic) === normalize(rec.topic) &&
|
const dup = !!matchMaterial(materials, rec.category, rec.brand, rec.specs);
|
||||||
normalize(m.category) === normalize(rec.category) &&
|
|
||||||
normalize(m.brand) === normalize(rec.brand) &&
|
|
||||||
specsKey(m.specs) === specsKey(rec.specs),
|
|
||||||
);
|
|
||||||
if (dup) return;
|
if (dup) return;
|
||||||
onRemember(rec);
|
onRemember(rec);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import { topicByName } from "../../lib/topics";
|
|||||||
import { useAppStore } from "../../store";
|
import { useAppStore } from "../../store";
|
||||||
import { normalize } from "../../lib/normalize";
|
import { normalize } from "../../lib/normalize";
|
||||||
import { faNum } from "../../lib/formatters";
|
import { faNum } from "../../lib/formatters";
|
||||||
import { specsKey } from "../../lib/quoteUtils";
|
import { matchMaterial } from "../../lib/quoteUtils";
|
||||||
import { Header, PageLayout } from "../../components/layout/Header";
|
import { Header, PageLayout } from "../../components/layout/Header";
|
||||||
import { Stepper } from "./Stepper";
|
import { Stepper } from "./Stepper";
|
||||||
import { Step1TopicSupplier } from "./Step1TopicSupplier";
|
import { Step1TopicSupplier } from "./Step1TopicSupplier";
|
||||||
@@ -291,22 +291,19 @@ export function WizardPage() {
|
|||||||
if (!cat && !brand && qty == null && price == null) continue;
|
if (!cat && !brand && qty == null && price == null) continue;
|
||||||
|
|
||||||
const wantName = (cat || brand || specs.join(" ") || "").trim() || "کالای جدید";
|
const wantName = (cat || brand || specs.join(" ") || "").trim() || "کالای جدید";
|
||||||
|
// هویت متریال = نام + برند + مشخصه. پیش از v2.7 دسته هم در تطبیق بود؛
|
||||||
|
// دسته دیگر روی متریال نیست و تکیه بر نام جلوی اشتباهگرفتن دو متریال با
|
||||||
|
// برند و مشخصهی یکسان را میگیرد (matchMaterial نامِ پایه را هم میبیند
|
||||||
|
// تا رکوردهای قدیمی با نامِ «برند + دسته + مشخصه» پیدا شوند).
|
||||||
let material = materials.find((m) => looksLikeUuid(m.id) && normalize(m.name) === normalize(wantName));
|
let material = materials.find((m) => looksLikeUuid(m.id) && normalize(m.name) === normalize(wantName));
|
||||||
if (!material) {
|
if (!material) {
|
||||||
material = materials.find(
|
const matched = matchMaterial(
|
||||||
(m) =>
|
materials.filter((m) => looksLikeUuid(m.id)),
|
||||||
looksLikeUuid(m.id) &&
|
cat,
|
||||||
normalize(m.category) === normalize(cat) &&
|
brand,
|
||||||
normalize(m.brand) === normalize(brand) &&
|
specs,
|
||||||
specsKey(m.specs) === specsKey(specs),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// رکوردهای قدیمی name را به شکل «برند + دسته + مشخصه» ذخیره کردهاند؛
|
|
||||||
// تطبیق دوم بر اساس category/brand/specs انجام میشود تا آنها هم پیدا شوند.
|
|
||||||
if (!material && cat) {
|
|
||||||
material = materials.find(
|
|
||||||
(m) => looksLikeUuid(m.id) && normalize(m.category) === normalize(cat) && normalize(m.brand) === normalize(brand),
|
|
||||||
);
|
);
|
||||||
|
if (matched) material = matched;
|
||||||
}
|
}
|
||||||
let materialId: string;
|
let materialId: string;
|
||||||
if (material && looksLikeUuid(material.id)) {
|
if (material && looksLikeUuid(material.id)) {
|
||||||
@@ -317,7 +314,6 @@ export function WizardPage() {
|
|||||||
id: nextId("m", materials),
|
id: nextId("m", materials),
|
||||||
name: wantName,
|
name: wantName,
|
||||||
topic: selectedTopic,
|
topic: selectedTopic,
|
||||||
category: cat,
|
|
||||||
brand,
|
brand,
|
||||||
specs,
|
specs,
|
||||||
base_unit: unit || "عدد",
|
base_unit: unit || "عدد",
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import type {
|
import type {
|
||||||
CategoryUnit,
|
|
||||||
Material,
|
Material,
|
||||||
Project,
|
Project,
|
||||||
Quote,
|
Quote,
|
||||||
@@ -15,7 +14,6 @@ import type {
|
|||||||
Store,
|
Store,
|
||||||
Supplier,
|
Supplier,
|
||||||
Topic,
|
Topic,
|
||||||
UnitConversion,
|
|
||||||
} from "../lib/types";
|
} from "../lib/types";
|
||||||
|
|
||||||
interface AppStore {
|
interface AppStore {
|
||||||
@@ -24,8 +22,6 @@ interface AppStore {
|
|||||||
materials: Material[];
|
materials: Material[];
|
||||||
projects: Project[];
|
projects: Project[];
|
||||||
quotes: Quote[];
|
quotes: Quote[];
|
||||||
categoryUnits: CategoryUnit[];
|
|
||||||
unitConversions: UnitConversion[];
|
|
||||||
standardConversions: StandardConversion[];
|
standardConversions: StandardConversion[];
|
||||||
topics: Topic[]; // دستهبندیهای ۸گانه — از /material-categories
|
topics: Topic[]; // دستهبندیهای ۸گانه — از /material-categories
|
||||||
|
|
||||||
@@ -45,9 +41,6 @@ interface AppStore {
|
|||||||
deleteSupplier: (id: string) => void;
|
deleteSupplier: (id: string) => void;
|
||||||
addMaterial: (m: Material) => void;
|
addMaterial: (m: Material) => void;
|
||||||
addProject: (p: Project) => void;
|
addProject: (p: Project) => void;
|
||||||
setCategoryUnit: (cu: CategoryUnit) => void;
|
|
||||||
setConversion: (c: UnitConversion) => void;
|
|
||||||
deleteConversion: (id: string) => void;
|
|
||||||
setStandardConversion: (sc: StandardConversion) => void;
|
setStandardConversion: (sc: StandardConversion) => void;
|
||||||
deleteStandardConversion: (id: string) => void;
|
deleteStandardConversion: (id: string) => void;
|
||||||
|
|
||||||
@@ -62,8 +55,6 @@ export const useAppStore = create<AppStore>()((set) => ({
|
|||||||
materials: [],
|
materials: [],
|
||||||
projects: [],
|
projects: [],
|
||||||
quotes: [],
|
quotes: [],
|
||||||
categoryUnits: [],
|
|
||||||
unitConversions: [],
|
|
||||||
standardConversions: [],
|
standardConversions: [],
|
||||||
topics: [],
|
topics: [],
|
||||||
|
|
||||||
@@ -77,8 +68,6 @@ export const useAppStore = create<AppStore>()((set) => ({
|
|||||||
materials: store.materials,
|
materials: store.materials,
|
||||||
projects: store.projects,
|
projects: store.projects,
|
||||||
quotes: store.quotes,
|
quotes: store.quotes,
|
||||||
categoryUnits: store.categoryUnits,
|
|
||||||
unitConversions: store.unitConversions,
|
|
||||||
standardConversions: store.standardConversions,
|
standardConversions: store.standardConversions,
|
||||||
topics: store.topics,
|
topics: store.topics,
|
||||||
}),
|
}),
|
||||||
@@ -96,24 +85,6 @@ export const useAppStore = create<AppStore>()((set) => ({
|
|||||||
deleteSupplier: (id) => set((s) => ({ suppliers: s.suppliers.filter((x) => x.id !== id) })),
|
deleteSupplier: (id) => set((s) => ({ suppliers: s.suppliers.filter((x) => x.id !== id) })),
|
||||||
addMaterial: (m) => set((s) => ({ materials: [...s.materials, m] })),
|
addMaterial: (m) => set((s) => ({ materials: [...s.materials, m] })),
|
||||||
addProject: (p) => set((s) => ({ projects: [...s.projects, p] })),
|
addProject: (p) => set((s) => ({ projects: [...s.projects, p] })),
|
||||||
setCategoryUnit: (cu) =>
|
|
||||||
set((s) => {
|
|
||||||
const idx = s.categoryUnits.findIndex((x) => x.id === cu.id);
|
|
||||||
const categoryUnits = [...s.categoryUnits];
|
|
||||||
if (idx >= 0) categoryUnits[idx] = cu;
|
|
||||||
else categoryUnits.push(cu);
|
|
||||||
return { categoryUnits };
|
|
||||||
}),
|
|
||||||
setConversion: (c) =>
|
|
||||||
set((s) => {
|
|
||||||
const idx = s.unitConversions.findIndex((x) => x.id === c.id);
|
|
||||||
const unitConversions = [...s.unitConversions];
|
|
||||||
if (idx >= 0) unitConversions[idx] = c;
|
|
||||||
else unitConversions.push(c);
|
|
||||||
return { unitConversions };
|
|
||||||
}),
|
|
||||||
deleteConversion: (id) =>
|
|
||||||
set((s) => ({ unitConversions: s.unitConversions.filter((x) => x.id !== id) })),
|
|
||||||
setStandardConversion: (sc) =>
|
setStandardConversion: (sc) =>
|
||||||
set((s) => {
|
set((s) => {
|
||||||
const idx = s.standardConversions.findIndex((x) => x.id === sc.id);
|
const idx = s.standardConversions.findIndex((x) => x.id === sc.id);
|
||||||
|
|||||||
Reference in New Issue
Block a user