from datetime import date from sqlalchemy import select from app.db.session import Base, SessionLocal, engine from app.models.assumption import FreightCostRule, PackagingCostRule, ProcessCostRule from app.models.mix import Mix, MixIngredient from app.models.product import Product from app.models.raw_material import RawMaterial, RawMaterialPriceVersion def seed_if_empty(): Base.metadata.create_all(bind=engine) with SessionLocal() as db: existing = db.scalar(select(RawMaterial.id)) if existing is not None: return maize = RawMaterial(name="Maize", supplier="Example Supplier", unit_of_measure="tonne", kg_per_unit=1000, status="active") barley = RawMaterial(name="Barley", supplier="Example Supplier", unit_of_measure="tonne", kg_per_unit=1000, status="active") acid_buf = RawMaterial(name="Acid Buf", supplier="Example Supplier", unit_of_measure="bag", kg_per_unit=25, status="active") maize.price_versions.append(RawMaterialPriceVersion(market_value=520, waste_percentage=0.02, effective_date=date(2026, 4, 1))) barley.price_versions.append(RawMaterialPriceVersion(market_value=470, waste_percentage=0.015, effective_date=date(2026, 4, 1))) acid_buf.price_versions.append(RawMaterialPriceVersion(market_value=39, waste_percentage=0.0, effective_date=date(2026, 4, 1))) db.add_all([maize, barley, acid_buf]) db.flush() db.add_all( [ ProcessCostRule(process_name="standard_bagging", grading_cost=0.055, bagging_cost=0.04, cracking_cost=0.0), ProcessCostRule(process_name="bulk_loadout", grading_cost=0.03, bagging_cost=0.0, cracking_cost=0.0), PackagingCostRule(sale_type="standard", unit_of_measure="20kg bag", own_bag=False, bag_cost=0.63), PackagingCostRule(sale_type="bulka", unit_of_measure="550kg bulka", own_bag=False, bag_cost=7.5), FreightCostRule(sale_type="standard", unit_of_measure="20kg bag", cost_per_unit=1.45), FreightCostRule(sale_type="bulka", unit_of_measure="550kg bulka", cost_per_unit=18.0), ] ) db.flush() mix = Mix(client_name="Specialty Feeds", name="Pigeon Mix", status="active", version=1, notes="Seed recipe for MVP") db.add(mix) db.flush() db.add_all( [ MixIngredient(mix_id=mix.id, raw_material_id=maize.id, quantity_kg=180), MixIngredient(mix_id=mix.id, raw_material_id=barley.id, quantity_kg=95), MixIngredient(mix_id=mix.id, raw_material_id=acid_buf.id, quantity_kg=5), ] ) db.flush() db.add( Product( client_name="Specialty Feeds", item_id="SKU-001", name="Specialty Pigeon Breeder 20kg", mix_id=mix.id, sale_type="standard", own_bag=False, unit_of_measure="20kg bag", items_per_pallet=50, bagging_process="standard_bagging", distributor_margin=0.225, wholesale_margin=0.18, notes="Reference product for formula parity work", ) ) db.commit() if __name__ == "__main__": seed_if_empty()