AccountingNext/Schaad.Accounting.Services/SettingsService.cs
Claudio Schaad 7d7bd6073c Add "Strom" page for the yearly NeoVac electricity bill
A new page (/electricity) to record one electricity bill per billing
period (1 July-30 June) with a grid, add/edit dialog, and two Chart.js
charts (kWh split grid/PV per period, CHF/kWh as a line).

- ElectricityBill (Common): Grid/PV kWh and CHF, side costs, advance
  payment; computed TotalKwh, PvShare, TotalChf, Balance, ChfPerKwh.
- Stored once per mandator at {DataPath}/{mandator}/electricity.json
  instead of per year, since the billing period doesn't align with
  calendar years (new ISettingsService.GetMandatorPath(),
  BaseRepository gets a directory-aware SaveAsync overload).
- electricity.json is also editable through the JSON editor
  (RawDataRepository).
- charts.js: stackedBar/line charts can now show a unit other than
  CHF (defaults to CHF, so the Analyse page is unaffected).
- The add/edit dialog links to an annotated example (SVG, placeholder
  data) showing which value on the real NeoVac bill goes into which
  field; opens in its own dialog since the main dialog has a fixed
  height.
- Tests for the computed properties and the new repositories.

Also bumps the TargetFramework to net10.0 and updates the local
DataPath in appsettings.Development.json.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-25 20:30:40 +01:00

116 lines
No EOL
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Schaad.Accounting.Datasets;
using Schaad.Accounting.Interfaces;
namespace Schaad.Accounting.Services
{
public class SettingsService : ISettingsService
{
private readonly SettingsDataset settingsDataset;
private string mandator;
private int year = DateTime.Now.Year;
public SettingsService(SettingsDataset settingsDataset)
{
this.settingsDataset = settingsDataset;
this.mandator = settingsDataset.DefaultMandator;
}
public int GetYear()
{
return year;
}
public void SetYear(int year)
{
this.year = year;
}
public bool TrySetYear(int year)
{
var oldYear = this.year;
SetYear(year);
if (Directory.GetFiles(GetDbPath()).Any() == false)
{
SetYear(oldYear);
return false;
}
return true;
}
public string GetMandator()
{
return mandator;
}
/// <summary>
/// Mandators that have a folder for the given year.
/// </summary>
public IReadOnlyList<string> GetMandators(int year)
{
var dataPath = GetSettings().DataPath;
if (Directory.Exists(dataPath) == false)
{
return [];
}
return Directory.GetDirectories(dataPath)
.Where(d => Directory.Exists(Path.Combine(d, year.ToString())))
.Select(Path.GetFileName)
.Where(name => !DataFolders.IsReserved(name))
.ToList();
}
public void SetMandator(string mandator)
{
this.mandator = mandator;
}
public string GetBackupFilePath()
{
var backupPath = EnsureDirectory(Path.Combine(GetSettings().DataPath, DataFolders.Backup, mandator, year.ToString()));
return Path.Combine(backupPath, DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip");
}
public string GetUploadPath()
{
return EnsureDirectory(Path.Combine(GetSettings().DataPath, DataFolders.Uploads, mandator, year.ToString()));
}
public string GetCreditCardStatementPath()
{
return EnsureDirectory(Path.Combine(GetUploadPath(), DataFolders.CreditCard));
}
public string GetDbPath()
{
return EnsureDirectory(Path.Combine(GetSettings().DataPath, mandator, year.ToString()));
}
public string GetLastYearDbPath()
{
return EnsureDirectory(Path.Combine(GetSettings().DataPath, mandator, (year - 1).ToString()));
}
public string GetMandatorPath()
{
return EnsureDirectory(Path.Combine(GetSettings().DataPath, mandator));
}
public SettingsDataset GetSettings()
{
return settingsDataset;
}
private static string EnsureDirectory(string path)
{
if (Directory.Exists(path) == false)
{
Directory.CreateDirectory(path);
}
return path;
}
}
}