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