feat: general ledger, automatic posting, VAT settlement, withholdings and F24 (#62 #63 #64 #65 #66 #67 #68)
M7 adds the double-entry ledger so that the documents of M5 and M6 reach the accounts without retyping, and the Italian VAT obligations can be prepared from the same data. - Chart of accounts on three levels with the Italian "PMI standard" model in the fiscal pack, journal templates with proposed lines, VAT registers; the chart import now writes the accounts and exports them (#62, ADR 0036). - Journal entries: draft, posted, final with the libro giornale number, reversal; recurring entries with the nightly ledger.recurring job (#63). - Documents post their entry inside the document transaction (PostingEngine, LedgerPoster) with VAT lines, open items and withholdings; a change rewrites the same entry, a cancellation removes or reverses it (#64, ADR 0037). - VAT registers with final print, monthly or quarterly settlement with pro rata, interest, minimum and advance, confirmation with the LIQ entry, period lock and F24 line, LIPE file and F24 data (#65). - Operated and suffered withholdings, CU data, stamp duty by quarter with the 5,000 euro threshold, F24 summary with its payment entry (#66). - Party ledgers, account ledgers, trial balance and libro giornale (#67). - Demo accounting with the settlements of the past months confirmed and checked by hand in a test; wiki Contabilita, IVA, Fiscalita italiana completed with screenshots; CHANGELOG 0.7.0, STATUS, ADR 0036-0037 (#68). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
838362bfe1
commit
9e3ba92993
83 files changed
+13838
-497
No files matched your search
@@ -0,0 +1,123 @@
|
||||
using Khaemhat.Application.Instance;
|
||||
using Khaemhat.Application.Ledger;
|
||||
using Khaemhat.Application.Security;
|
||||
using Khaemhat.Core.Accounting;
|
||||
using Khaemhat.Core.Tables;
|
||||
using Khaemhat.Core.Tax;
|
||||
using Khaemhat.Data;
|
||||
using Khaemhat.Data.Querying;
|
||||
|
||||
namespace Khaemhat.Application.Seeding;
|
||||
|
||||
/// <summary>
|
||||
/// The demo accounting (SPEC §14, issue #68): the demo documents posted, the professional's invoice paid so that its
|
||||
/// withholding is due, the monthly rental of the office printer as a recurring entry, the VAT settlements of the periods already
|
||||
/// ended confirmed, and the F24 already due paid. It runs after the demo sales and purchases and only once: when
|
||||
/// the company has typed or settled anything in the ledger it adds nothing.
|
||||
/// </summary>
|
||||
/// <param name="ledger">Posts the documents.</param>
|
||||
/// <param name="journal">Writes the model entry of the rental.</param>
|
||||
/// <param name="vat">Computes and confirms the settlements.</param>
|
||||
/// <param name="taxes">Withholdings, stamp duty and F24.</param>
|
||||
/// <param name="recurring">The recurring rental.</param>
|
||||
/// <param name="factory">Reads the ledger.</param>
|
||||
/// <param name="clock">Today.</param>
|
||||
public sealed class DemoLedgerSeeder(
|
||||
LedgerService ledger, JournalEntryService journal, VatService vat, TaxService taxes, RecurringEntryService recurring, IUnitOfWorkFactory factory, TimeProvider clock)
|
||||
: ICompanySeeder
|
||||
{
|
||||
/// <summary>The account of the rental, from the Italian chart of accounts (Noleggi).</summary>
|
||||
public const string RentAccount = "22.01.003";
|
||||
|
||||
/// <summary>The bank account that pays it.</summary>
|
||||
public const string BankAccount = "07.01.001";
|
||||
|
||||
/// <summary>The monthly rental of the demo office printer.</summary>
|
||||
public const decimal Rent = 45m;
|
||||
|
||||
private static readonly Actor Seed = Actor.System;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Order => 40;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task SeedAsync(string companyCode, CompanySeedContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
if (!context.Demo)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var today = DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(clock.GetUtcNow(), TimeZoneInfo.Local).DateTime);
|
||||
var start = new DateOnly(today.Year, 1, 1);
|
||||
long? rentId;
|
||||
long? bankId;
|
||||
VatPeriodicity periodicity;
|
||||
await using (var uow = await factory.CreateAsync(DatabaseScope.Company(companyCode), cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
var accounts = uow.Repository<GlAccount, long>();
|
||||
if (await accounts.CountAsync(Query.For<GlAccount>(), cancellationToken).ConfigureAwait(false) == 0)
|
||||
{
|
||||
return; // No chart of accounts: the company has no accounting module.
|
||||
}
|
||||
|
||||
if (await uow.Repository<GlEntry, long>().CountAsync(
|
||||
Query.For<GlEntry>().Where(e => e.Source == GlEntrySource.Manual || e.Source == GlEntrySource.VatSettlement), cancellationToken).ConfigureAwait(false) > 0
|
||||
|| await uow.Repository<VatSettlement, long>().CountAsync(Query.For<VatSettlement>(), cancellationToken).ConfigureAwait(false) > 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
rentId = (await accounts.FindAsync(Query.For<GlAccount>().Where(a => a.Code == RentAccount), cancellationToken).ConfigureAwait(false))?.Id;
|
||||
bankId = (await accounts.FindAsync(Query.For<GlAccount>().Where(a => a.Code == BankAccount), cancellationToken).ConfigureAwait(false))?.Id;
|
||||
periodicity = (await uow.Repository<CompanyProfile, long>().FindAsync(Query.For<CompanyProfile>(), cancellationToken).ConfigureAwait(false))?.VatPeriodicity
|
||||
?? VatPeriodicity.Monthly;
|
||||
}
|
||||
|
||||
// The documents confirmed before the ledger existed.
|
||||
await ledger.PostPendingAsync(Seed, companyCode, start, today, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// The professionals' invoices paid ten days after their date: their withholdings become due.
|
||||
foreach (var row in await taxes.WithholdingsAsync(Seed, companyCode, today.Year, WithholdingDirection.Operated, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
if (row.Withholding.Status == WithholdingStatus.Waiting)
|
||||
{
|
||||
var paid = row.Withholding.DocumentDate.AddDays(10);
|
||||
await taxes.InvoicePaidAsync(Seed, companyCode, row.Withholding.Id, paid > today ? today : paid, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
// The printer rental of January, and the same entry every month after as a recurring entry.
|
||||
if (rentId is { } rent && bankId is { } bank)
|
||||
{
|
||||
var first = new DateOnly(today.Year, 1, 5);
|
||||
var model = await journal.SaveAsync(Seed, companyCode, new EntryInput(null, first, "Noleggio stampante multifunzione",
|
||||
[new EntryLineDraft(rent, null, null, Rent, 0m), new EntryLineDraft(bank, null, null, 0m, Rent)]) { Post = true }, cancellationToken).ConfigureAwait(false);
|
||||
await recurring.SaveAsync(Seed, companyCode, new RecurringEntryInput(null, "Noleggio mensile stampante", model.Entry.Id, RecurrenceFrequency.Monthly,
|
||||
RecurringEntryService.Next(first, RecurrenceFrequency.Monthly), null, false), cancellationToken).ConfigureAwait(false);
|
||||
await recurring.GenerateAsync(Seed, companyCode, today, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// The settlements of the periods already ended, in order, and the stamp duty of the quarters ended.
|
||||
var periods = periodicity == VatPeriodicity.Quarterly ? (today.Month - 1) / 3 : today.Month - 1;
|
||||
for (var number = 1; number <= periods; number++)
|
||||
{
|
||||
var computed = await vat.ComputeAsync(Seed, companyCode, today.Year, number, cancellationToken).ConfigureAwait(false);
|
||||
await vat.ConfirmAsync(Seed, companyCode, computed.Settlement.Id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
for (var quarter = 1; quarter <= (today.Month - 1) / 3; quarter++)
|
||||
{
|
||||
await taxes.StampDutyPaymentAsync(Seed, companyCode, today.Year, quarter, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// The F24 already due are paid on their day; those still to come stay to pay.
|
||||
var dues = (await taxes.PaymentsAsync(Seed, companyCode, today.Year, cancellationToken).ConfigureAwait(false))
|
||||
.Where(p => p.PaidOn is null && p.DueDate < today).Select(p => p.DueDate).Distinct().Order().ToList();
|
||||
foreach (var due in dues)
|
||||
{
|
||||
await taxes.PayAsync(Seed, companyCode, due, due, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
using Khaemhat.Application.Modules;
|
||||
using Khaemhat.Core.Accounting;
|
||||
using Khaemhat.Core.Tables;
|
||||
using Khaemhat.Data.Querying;
|
||||
using Khaemhat.Fiscal.Abstractions;
|
||||
|
||||
namespace Khaemhat.Application.Seeding;
|
||||
|
||||
/// <summary>
|
||||
/// The ledger of the base data (SPEC §14, issue #62): the model chart of accounts of the country pack in three levels,
|
||||
/// the VAT registers, the journal templates with their lines and, on the document types that have none, the template
|
||||
/// and the register they post with. Like every base seeder it adds only what is missing.
|
||||
/// </summary>
|
||||
public sealed class LedgerSeeder : ISeeder
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string ModuleCode => ModuleCodes.Accounting;
|
||||
|
||||
/// <inheritdoc />
|
||||
public SeedKind Kind => SeedKind.Base;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Order => 50;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task RunAsync(SeedContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
if (context.Pack.GetFeature<ILedgerTemplate>() is not { } ledger)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var accounts = await SeedAccountsAsync(context, ledger, cancellationToken).ConfigureAwait(false);
|
||||
await context.EnsureAsync(
|
||||
"vat_registers",
|
||||
ledger.VatRegisters.Select(r => new VatRegister { Code = r.Code, Description = r.Description, Kind = r.Kind, ReverseCharge = r.ReverseCharge }),
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
await SeedTemplatesAsync(context, ledger, accounts, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var types = context.UnitOfWork.Repository<DocumentType, long>();
|
||||
foreach (var posting in ledger.DocumentPostings)
|
||||
{
|
||||
var type = await types.FindAsync(Query.For<DocumentType>().Where(t => t.Code == posting.DocumentTypeCode), cancellationToken).ConfigureAwait(false);
|
||||
if (type is not null && type.JournalTemplateCode is null && type.VatRegisterCode is null)
|
||||
{
|
||||
type.JournalTemplateCode = posting.JournalTemplateCode;
|
||||
type.VatRegisterCode = posting.VatRegisterCode;
|
||||
await types.UpdateAsync(type, cancellationToken).ConfigureAwait(false);
|
||||
context.Count("document_types");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<Dictionary<string, GlAccount>> SeedAccountsAsync(SeedContext context, ILedgerTemplate ledger, CancellationToken cancellationToken)
|
||||
{
|
||||
var repository = context.UnitOfWork.Repository<GlAccount, long>();
|
||||
var existing = (await repository.ListAsync(Query.For<GlAccount>(), cancellationToken).ConfigureAwait(false)).Items
|
||||
.ToDictionary(a => a.Code, StringComparer.Ordinal);
|
||||
var parents = ledger.Accounts.Where(a => a.ParentCode is not null).Select(a => a.ParentCode!).ToHashSet(StringComparer.Ordinal);
|
||||
foreach (var template in ledger.Accounts.OrderBy(a => a.Level).ThenBy(a => a.Code, StringComparer.Ordinal))
|
||||
{
|
||||
if (existing.ContainsKey(template.Code))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var account = new GlAccount
|
||||
{
|
||||
Code = template.Code,
|
||||
Description = template.Description,
|
||||
Level = template.Level,
|
||||
ParentId = template.ParentCode is { } parent && existing.TryGetValue(parent, out var found) ? found.Id : null,
|
||||
Nature = template.Nature,
|
||||
Type = template.Type,
|
||||
StatementLine = template.StatementLine,
|
||||
SubLedger = template.SubLedger,
|
||||
VatRelevant = template.VatRelevant,
|
||||
ClosesAutomatically = template.Nature != AccountNature.Memorandum,
|
||||
Postable = !parents.Contains(template.Code),
|
||||
};
|
||||
account.Id = await repository.InsertAsync(account, cancellationToken).ConfigureAwait(false);
|
||||
existing[account.Code] = account;
|
||||
context.Count("gl_accounts");
|
||||
}
|
||||
|
||||
return existing;
|
||||
}
|
||||
|
||||
private static async Task SeedTemplatesAsync(
|
||||
SeedContext context, ILedgerTemplate ledger, Dictionary<string, GlAccount> accounts, CancellationToken cancellationToken)
|
||||
{
|
||||
var repository = context.UnitOfWork.Repository<JournalTemplate, long>();
|
||||
var lines = context.UnitOfWork.Repository<JournalTemplateLine, long>();
|
||||
var existing = (await repository.ListAsync(Query.For<JournalTemplate>(), cancellationToken).ConfigureAwait(false)).Items
|
||||
.Select(t => t.Code).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var template in ledger.JournalTemplates.Where(t => !existing.Contains(t.Code)))
|
||||
{
|
||||
var id = await repository.InsertAsync(new JournalTemplate
|
||||
{
|
||||
Code = template.Code,
|
||||
Description = template.Description,
|
||||
Area = template.Area,
|
||||
VatRegisterCode = template.VatRegisterCode,
|
||||
OpensItems = template.OpensItems,
|
||||
Reverses = template.Reverses,
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
context.Count("gl_journal_templates");
|
||||
for (var i = 0; i < template.Lines.Count; i++)
|
||||
{
|
||||
var line = template.Lines[i];
|
||||
await lines.InsertAsync(new JournalTemplateLine
|
||||
{
|
||||
JournalTemplateId = id,
|
||||
SortOrder = (i + 1) * 10,
|
||||
Role = line.Role,
|
||||
Side = line.Side,
|
||||
AccountId = line.AccountCode is { } code && accounts.TryGetValue(code, out var account) ? account.Id : null,
|
||||
Description = line.Description,
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user