From 01ab2cbb4555389514b9247323a9704b837e306c Mon Sep 17 00:00:00 2001 From: maziggy Date: Thu, 22 Jan 2026 09:13:48 +0100 Subject: [PATCH] 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. --- CHANGELOG.md | 3 +++ backend/app/api/routes/archives.py | 13 +++++++++++-- backend/app/services/archive.py | 7 +++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43ab117c5..e3ae9ff90 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/backend/app/api/routes/archives.py b/backend/app/api/routes/archives.py index be73f3923..a27f6cbf8 100644 --- a/backend/app/api/routes/archives.py +++ b/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: diff --git a/backend/app/services/archive.py b/backend/app/services/archive.py index 4c7fed8fb..c0a3a27dc 100644 --- a/backend/app/services/archive.py +++ b/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