Sfoglia il codice sorgente

Fix filament cost not using default setting

The "Default filament cost (per kg)" setting was being ignored when
calculating filament costs. A hardcoded value of 25.0 was used instead.

All three now read the default_filament_cost setting from the database,
falling back to 25.0 only if the setting doesn't exist.

Users can run "Recalculate Costs" from the Archives page to update
existing archives with the correct default cost.
maziggy 4 mesi fa
parent
commit
01ab2cbb45
3 ha cambiato i file con 19 aggiunte e 4 eliminazioni
  1. 3 0
      CHANGELOG.md
  2. 11 2
      backend/app/api/routes/archives.py
  3. 5 2
      backend/app/services/archive.py

+ 3 - 0
CHANGELOG.md

@@ -13,6 +13,9 @@ All notable changes to Bambuddy will be documented in this file.
   - Print energy tracking now works correctly for HA plugs (not just Tasmota)
   - New API endpoint: `GET /api/v1/smart-plugs/ha/sensors` to list available energy sensors
 
+### Fixed
+- **Filament cost using wrong default** - Statistics now correctly uses the "Default filament cost (per kg)" setting instead of hardcoded €25 value (Issue #120)
+
 ## [0.1.6b10] - 2026-01-21
 
 ### New Features

+ 11 - 2
backend/app/api/routes/archives.py

@@ -627,6 +627,7 @@ async def toggle_favorite(
 @router.post("/{archive_id}/rescan", response_model=ArchiveResponse)
 async def rescan_archive(archive_id: int, db: AsyncSession = Depends(get_db)):
     """Rescan the 3MF file and update metadata."""
+    from backend.app.api.routes.settings import get_setting
     from backend.app.services.archive import ThreeMFParser
 
     result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
@@ -672,7 +673,10 @@ async def rescan_archive(archive_id: int, db: AsyncSession = Depends(get_db)):
         if filament:
             archive.cost = round((archive.filament_used_grams / 1000) * filament.cost_per_kg, 2)
         else:
-            archive.cost = round((archive.filament_used_grams / 1000) * 25.0, 2)
+            # 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)
@@ -682,13 +686,18 @@ async def rescan_archive(archive_id: int, db: AsyncSession = Depends(get_db)):
 @router.post("/recalculate-costs")
 async def recalculate_all_costs(db: AsyncSession = Depends(get_db)):
     """Recalculate costs for all archives based on filament usage and prices."""
+    from backend.app.api.routes.settings import get_setting
+
     result = await db.execute(select(PrintArchive))
     archives = list(result.scalars().all())
 
     # Load all filaments for lookup
     filament_result = await db.execute(select(Filament))
     filaments = {f.type: f.cost_per_kg for f in filament_result.scalars().all()}
-    default_cost_per_kg = 25.0
+
+    # Get 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
 
     updated = 0
     for archive in archives:

+ 5 - 2
backend/app/services/archive.py

@@ -844,8 +844,11 @@ class ArchiveService:
             if filament:
                 cost = round((filament_grams / 1000) * filament.cost_per_kg, 2)
             else:
-                # Default cost_per_kg if filament type not found
-                default_cost_per_kg = 25.0
+                # Use default filament cost from settings
+                from backend.app.api.routes.settings import get_setting
+
+                default_cost_setting = await get_setting(self.db, "default_filament_cost")
+                default_cost_per_kg = float(default_cost_setting) if default_cost_setting else 25.0
                 cost = round((filament_grams / 1000) * default_cost_per_kg, 2)
 
         # Calculate quantity from printable objects count