AccountingNext/Schaad.Accounting.Services/FileService.cs
Claudio Schaad 1a3d93e0e3 Remove dead and unused code
- CSS: drop the Bootstrap template leftovers (NavMenu.razor.css, most of
  MainLayout.razor.css, button/form rules in app.css) and the copied
  Fluent demo rules in BankTransactions.razor.css.
- Bootstrap: keep only bootstrap.min.css (+ map); 42 unused files removed.
- Credit-card statement import, never wired into the Blazor UI: service
  methods, GetCreditCardStatementPath, DI registrations for
  ICreditCardStatementService/IPdfParsingService, FreeSpire.PDF.
- CSV export (GetTransactionListCsvAsync), only called from tests.
- Split predefinitions: model, repository and the JSON editor entry.
- Unused members: TrySetYear, GetUploadPath (+ DataFolders.Uploads),
  GetClasses; unused usings, fields and parameters.
- Home.razor.css -> PendingBookings.razor.css: the pending-bookings table
  moved there in "PR U-10" and had lost its styling since.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 22:46:08 +01:00

114 lines
5.4 KiB
C#

using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Schaad.Accounting.Datasets;
using Schaad.Accounting.Interfaces;
using Schaad.Finance.Api;
namespace Schaad.Accounting.Services
{
public class FileService : IFileService
{
private readonly IAccountRepository accountRepository;
private readonly IBankTransactionRepository bankTransactionRepository;
private readonly ISettingsService settingsService;
private readonly IAccountStatementService accountStatementService;
private readonly ILogger<FileService> logger;
public FileService(
ISettingsService settingsService,
IAccountRepository accountRepository,
IBankTransactionRepository bankTransactionRepository,
IAccountStatementService accountStatementService,
ILogger<FileService> logger)
{
this.settingsService = settingsService;
this.accountRepository = accountRepository;
this.bankTransactionRepository = bankTransactionRepository;
this.accountStatementService = accountStatementService;
this.logger = logger;
}
public string Backup()
{
var outpath = settingsService.GetBackupFilePath();
var sourcepath = settingsService.GetDbPath();
ZipFile.CreateFromDirectory(sourcepath, outpath);
return $"Backup '{outpath}' erfolgreich erstellt";
}
// Upload account statement file (mt940, camt053)
public async Task<IReadOnlyList<MessageDataset>> ImportAccountStatementFileAsync(string filePath)
{
logger.LogInformation("Importing account statement file {FilePath}", filePath);
var messages = new List<MessageDataset>();
var accountStatementResults = accountStatementService.ReadFile(filePath, Encoding.UTF8);
var accountList = (await accountRepository.GetAccountListAsync()).Select(a => a.BankAccountNumber).ToList();
foreach (var accountStatementResult in accountStatementResults)
{
var account = accountStatementResult.AccountStatement;
if (accountStatementResult.IsSuccess == false)
{
logger.LogError("Statement parse failed for account {AccountNumber}: {Error}", account.AccountNumber, accountStatementResult.Error);
messages.Add(new MessageDataset($"Account {account.AccountNumber} NICHT importiert: {accountStatementResult.Error}", MessageStatus.Error));
}
if (accountStatementResult.AccountStatement.Transactions.Any() == false)
{
continue;
}
var message = new MessageDataset($"Konto {account.AccountNumber}");
messages.Add(message);
// only accounts that are in select mandator
if (accountList.Contains(account.AccountNumber))
{
var transactionsThisYear = account.Transactions
.Where(t => t.BookingDate.Year == settingsService.GetYear())
.Select(
t => new BankTransactionDataset
{
Id = t.Id,
Value = (decimal)t.Value,
ValueDate = t.ValueDate,
BookingDate = t.BookingDate,
Text = t.Text,
Debtor = t.Debtor,
Creditor = t.Creditor
})
.ToList();
var count = await bankTransactionRepository.SaveBankTransactionListAsync(account.AccountNumber, transactionsThisYear);
await accountRepository.SaveBankAccountBalanceAsync(account.AccountNumber, (decimal)account.EndBalance.Value);
logger.LogInformation("Imported {Imported} of {Total} transactions for account {AccountNumber}",
count, account.Transactions.Count, account.AccountNumber);
var status = count == account.Transactions.Count ? MessageStatus.Success : MessageStatus.Info;
message.Add($"{count} von {account.Transactions.Count} Transaktion(en) importiert.", status);
// not imported transactions
var notImportedTransactions = account.Transactions.Where(t => transactionsThisYear.Select(tt => tt.Id).Contains(t.Id) == false);
foreach (var trx in notImportedTransactions)
{
message.Add($"Transaktion wegen BookingDatum {trx.BookingDate} nicht importiert: {trx.Text}", MessageStatus.Warning);
}
}
else
{
logger.LogWarning("Skipping account {AccountNumber}: not part of the currently selected mandator ({Skipped} transactions ignored)",
account.AccountNumber, account.Transactions.Count);
message.Add($"Falscher Mandant: {account.Transactions.Count} Transaktion(en) nicht importiert.", MessageStatus.Warning);
}
}
return messages;
}
}
}