Adds the company tables of SPEC 11.1 behind one generic service so that every table gets the same rules, history, cache and CSV import/export, plus the company profile, fiscal years with monthly lock and the base-data seeder. - 26 coded tables, exchange rates, company profile, fiscal years and periods: entities, maps and migrations 010-014 - TableService + TableCatalog: unique codes, per-table rules (VAT codes are checked by the fiscal pack), delete or disable when in use, single default, CSV export/import all-or-nothing, cache with cross-instance invalidation - CompanyProfileService (Italian identifiers through the pack), FiscalYearService with the date check used by every dated record - EcbRatesClient and the ECB rates system task; CsvReader - ISeeder/SeedRunner with the data version in company settings; base tables from the pack (IBaseTablesTemplate) and an embedded ISO country list Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
3.2 KiB
C#
73 lines
3.2 KiB
C#
using Khaemhat.Application.Configuration;
|
|
using Khaemhat.Core.Caching;
|
|
using Khaemhat.Core.Tables;
|
|
using Khaemhat.Data;
|
|
using Khaemhat.Data.Querying;
|
|
using Khaemhat.Fiscal.Abstractions;
|
|
|
|
namespace Khaemhat.Application.Tables;
|
|
|
|
/// <summary>
|
|
/// The fiscal pack of each company: the one of <c>company_profile.country</c>, or of <c>KHAEMHAT_COUNTRY</c> while the
|
|
/// profile is missing (ADR 0025). The country is cached per company.
|
|
/// </summary>
|
|
public sealed class CompanyFiscalContext
|
|
{
|
|
private static readonly TimeSpan CacheTime = TimeSpan.FromHours(1);
|
|
|
|
private readonly IUnitOfWorkFactory _factory;
|
|
private readonly FiscalPackCatalog _packs;
|
|
private readonly ICacheProvider _cache;
|
|
private readonly string _defaultCountry;
|
|
|
|
/// <summary>Creates the context.</summary>
|
|
/// <param name="factory">Opens the company databases.</param>
|
|
/// <param name="packs">The fiscal packs.</param>
|
|
/// <param name="cache">The cache.</param>
|
|
/// <param name="options">The instance options (<c>KHAEMHAT_COUNTRY</c>).</param>
|
|
public CompanyFiscalContext(IUnitOfWorkFactory factory, FiscalPackCatalog packs, ICacheProvider cache, KhaemhatOptions options)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(factory);
|
|
ArgumentNullException.ThrowIfNull(packs);
|
|
ArgumentNullException.ThrowIfNull(cache);
|
|
ArgumentNullException.ThrowIfNull(options);
|
|
_factory = factory;
|
|
_packs = packs;
|
|
_cache = cache;
|
|
_defaultCountry = options.Country;
|
|
}
|
|
|
|
/// <summary>The country given to new companies.</summary>
|
|
public string DefaultCountry => _defaultCountry;
|
|
|
|
/// <summary>The pack of the instance's default country.</summary>
|
|
public IFiscalPack DefaultPack => _packs.ForCountry(_defaultCountry);
|
|
|
|
/// <summary>The fiscal pack of a company.</summary>
|
|
/// <param name="companyCode">The company.</param>
|
|
/// <param name="cancellationToken">Cancellation.</param>
|
|
/// <returns>The pack.</returns>
|
|
public async Task<IFiscalPack> PackOfAsync(string companyCode, CancellationToken cancellationToken = default)
|
|
{
|
|
var company = CompanyCodes.Normalize(companyCode);
|
|
var country = await _cache.GetOrCreateAsync(
|
|
CacheKeys.For(company, "fiscal", "country"),
|
|
async ct =>
|
|
{
|
|
await using var uow = await _factory.CreateAsync(DatabaseScope.Company(company), ct).ConfigureAwait(false);
|
|
var profile = await uow.Repository<CompanyProfile, long>().FindAsync(Query.For<CompanyProfile>(), ct).ConfigureAwait(false);
|
|
return profile?.Country ?? _defaultCountry;
|
|
},
|
|
CacheTime,
|
|
cancellationToken).ConfigureAwait(false);
|
|
return _packs.TryForCountry(country, out var pack) ? pack : DefaultPack;
|
|
}
|
|
|
|
/// <summary>Forgets the cached country of a company (after the profile changes).</summary>
|
|
/// <param name="companyCode">The company.</param>
|
|
/// <param name="cancellationToken">Cancellation.</param>
|
|
/// <returns>A task.</returns>
|
|
public ValueTask InvalidateAsync(string companyCode, CancellationToken cancellationToken = default) =>
|
|
_cache.PublishInvalidationAsync(CacheKeys.Prefix(CompanyCodes.Normalize(companyCode), "fiscal"), cancellationToken);
|
|
}
|