|
|
@@ -856,18 +856,27 @@ async def rescan_archive(
|
|
|
if metadata.get("designer"):
|
|
|
archive.designer = metadata["designer"]
|
|
|
|
|
|
- # Calculate cost based on filament usage and type
|
|
|
+ # Calculate cost: prefer spool-based cost if available, else catalog-based
|
|
|
if archive.filament_used_grams and archive.filament_type:
|
|
|
- primary_type = archive.filament_type.split(",")[0].strip()
|
|
|
- filament_result = await db.execute(select(Filament).where(Filament.type == primary_type).limit(1))
|
|
|
- filament = filament_result.scalar_one_or_none()
|
|
|
- if filament:
|
|
|
- archive.cost = round((archive.filament_used_grams / 1000) * filament.cost_per_kg, 2)
|
|
|
+ from backend.app.models.spool_usage_history import SpoolUsageHistory
|
|
|
+
|
|
|
+ usage_result = await db.execute(
|
|
|
+ select(func.sum(SpoolUsageHistory.cost)).where(SpoolUsageHistory.print_name == archive.print_name)
|
|
|
+ )
|
|
|
+ usage_cost = usage_result.scalar()
|
|
|
+ if usage_cost is not None and usage_cost > 0:
|
|
|
+ archive.cost = round(usage_cost, 2)
|
|
|
else:
|
|
|
- # Use default filament cost from settings
|
|
|
- default_cost_setting = await get_setting(db, "default_filament_cost")
|
|
|
- default_cost_per_kg = float(default_cost_setting) if default_cost_setting else 25.0
|
|
|
- archive.cost = round((archive.filament_used_grams / 1000) * default_cost_per_kg, 2)
|
|
|
+ primary_type = archive.filament_type.split(",")[0].strip()
|
|
|
+ filament_result = await db.execute(select(Filament).where(Filament.type == primary_type).limit(1))
|
|
|
+ filament = filament_result.scalar_one_or_none()
|
|
|
+ if filament:
|
|
|
+ archive.cost = round((archive.filament_used_grams / 1000) * filament.cost_per_kg, 2)
|
|
|
+ else:
|
|
|
+ # Use default filament cost from settings
|
|
|
+ default_cost_setting = await get_setting(db, "default_filament_cost")
|
|
|
+ default_cost_per_kg = float(default_cost_setting) if default_cost_setting else 25.0
|
|
|
+ archive.cost = round((archive.filament_used_grams / 1000) * default_cost_per_kg, 2)
|
|
|
|
|
|
await db.commit()
|
|
|
await db.refresh(archive)
|
|
|
@@ -893,15 +902,27 @@ async def recalculate_all_costs(
|
|
|
default_cost_setting = await get_setting(db, "default_filament_cost")
|
|
|
default_cost_per_kg = float(default_cost_setting) if default_cost_setting else 25.0
|
|
|
|
|
|
+ # Import SpoolUsageHistory for cost lookup
|
|
|
+ from backend.app.models.spool_usage_history import SpoolUsageHistory
|
|
|
+
|
|
|
updated = 0
|
|
|
for archive in archives:
|
|
|
- if archive.filament_used_grams and archive.filament_type:
|
|
|
+ # Prefer sum of spool_usage_history.cost for this archive's print_name
|
|
|
+ usage_result = await db.execute(
|
|
|
+ select(func.sum(SpoolUsageHistory.cost)).where(SpoolUsageHistory.print_name == archive.print_name)
|
|
|
+ )
|
|
|
+ usage_cost = usage_result.scalar()
|
|
|
+ if usage_cost is not None and usage_cost > 0:
|
|
|
+ new_cost = round(usage_cost, 2)
|
|
|
+ elif archive.filament_used_grams and archive.filament_type:
|
|
|
primary_type = archive.filament_type.split(",")[0].strip()
|
|
|
cost_per_kg = filaments.get(primary_type, default_cost_per_kg)
|
|
|
new_cost = round((archive.filament_used_grams / 1000) * cost_per_kg, 2)
|
|
|
- if archive.cost != new_cost:
|
|
|
- archive.cost = new_cost
|
|
|
- updated += 1
|
|
|
+ else:
|
|
|
+ new_cost = None
|
|
|
+ if new_cost is not None and archive.cost != new_cost:
|
|
|
+ archive.cost = new_cost
|
|
|
+ updated += 1
|
|
|
|
|
|
await db.commit()
|
|
|
return {"message": f"Recalculated costs for {updated} archives", "updated": updated}
|