v0.1.11b fixes

This commit is contained in:
2026-06-03 15:09:21 +12:00
parent cf968e802b
commit daa6e60a69
11 changed files with 304 additions and 43 deletions
+43 -1
View File
@@ -8,7 +8,7 @@ from pathlib import Path
import re
from openpyxl import load_workbook
from sqlalchemy import select
from sqlalchemy import func, select
from sqlalchemy.orm import selectinload
from app.db.session import Base, SessionLocal, engine
@@ -726,6 +726,45 @@ def _upsert_product_ingredients(
db.delete(ingredient)
def seed_product_ingredients_from_workbook(db) -> dict[str, int]:
"""Backfill row-specific product formulas for databases seeded before this table existed."""
try:
formula_workbook = _load_workbook("mix_quantites_per_client_per_pr")
except FileNotFoundError:
logger.info("Skipping product ingredient backfill because formula workbook is missing")
return {"formulas": 0, "products_with_formulas": 0, "backfilled": 0}
product_ingredient_rows = _read_product_ingredient_rows(formula_workbook)
if not product_ingredient_rows:
return {"formulas": 0, "products_with_formulas": 0, "backfilled": 0}
raw_material_map = {
material.name: material
for material in db.scalars(select(RawMaterial).where(RawMaterial.tenant_id == TENANT_ID)).all()
}
if not raw_material_map:
return {"formulas": len(product_ingredient_rows), "products_with_formulas": 0, "backfilled": 0}
had_product_ingredients = (
db.scalar(select(ProductIngredient.id).where(ProductIngredient.tenant_id == TENANT_ID).limit(1)) is not None
)
_upsert_product_ingredients(
db,
product_rows=[],
product_ingredient_rows=product_ingredient_rows,
raw_material_map=raw_material_map,
)
db.flush()
products_with_formulas = db.scalar(
select(func.count(func.distinct(ProductIngredient.product_id))).where(ProductIngredient.tenant_id == TENANT_ID)
)
return {
"formulas": len(product_ingredient_rows),
"products_with_formulas": int(products_with_formulas or 0),
"backfilled": 0 if had_product_ingredients else int(products_with_formulas or 0),
}
def _infer_throughput_bag_size(product: Product) -> float | None:
if product.sale_type == "bulka":
return None
@@ -1027,6 +1066,9 @@ def seed_startup_basics():
seed_client_access(db)
seed_access(db)
seed_throughput_workbook(db)
report = seed_product_ingredients_from_workbook(db)
if report["backfilled"]:
logger.info("Product ingredients backfilled from workbook: %s", report)
db.commit()