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>
139 lines
5.9 KiB
C#
139 lines
5.9 KiB
C#
using System.Globalization;
|
|
using Khaemhat.Infrastructure.Files;
|
|
|
|
namespace Khaemhat.Application.Ledger;
|
|
|
|
/// <summary>A column of a ledger print.</summary>
|
|
/// <param name="Title">The heading.</param>
|
|
/// <param name="Width">The width in points.</param>
|
|
/// <param name="Right">Aligned on the right (amounts).</param>
|
|
public sealed record ReportColumn(string Title, double Width, bool Right = false);
|
|
|
|
/// <summary>
|
|
/// The prints of the ledger (VAT registers, general journal, ledgers, trial balance, issue #65 and #67): A4 landscape,
|
|
/// the company and the title on every page, the headings repeated, the pages numbered from a first page in the form
|
|
/// <c>year/page</c> as the final prints of the books want (art. 2215-bis c.c., numbering by year).
|
|
/// </summary>
|
|
internal static class LedgerReport
|
|
{
|
|
private const double Margin = 28;
|
|
private const double LineHeight = 11;
|
|
private static readonly PdfColor Rule = PdfColor.Gray(180);
|
|
|
|
/// <summary>Writes a print.</summary>
|
|
/// <param name="company">The name, VAT number and address of the company.</param>
|
|
/// <param name="title">The title, such as <c>Registro IVA vendite</c>.</param>
|
|
/// <param name="subtitle">The period and the kind of print.</param>
|
|
/// <param name="columns">The columns.</param>
|
|
/// <param name="rows">The rows; a row whose first cell starts with <c>**</c> is a bold total.</param>
|
|
/// <param name="year">The year of the page numbers.</param>
|
|
/// <param name="firstPage">The number of the first page.</param>
|
|
/// <param name="cancellationToken">Cancellation.</param>
|
|
/// <returns>The file and the number of pages.</returns>
|
|
public static async Task<(byte[] File, int Pages)> WriteAsync(
|
|
string company, string title, string subtitle, IReadOnlyList<ReportColumn> columns, IReadOnlyList<IReadOnlyList<string?>> rows, int year, int firstPage,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
using var buffer = new MemoryStream();
|
|
int pages;
|
|
await using (var pdf = new PdfWriter(buffer, new PdfDocumentInfo { Title = title }, leaveOpen: true))
|
|
{
|
|
var width = PdfWriter.A4Height;
|
|
var height = PdfWriter.A4Width;
|
|
var scale = Math.Min(1d, (width - (2 * Margin)) / Math.Max(1d, columns.Sum(c => c.Width)));
|
|
PdfPage? page = null;
|
|
var y = 0d;
|
|
var number = firstPage;
|
|
async Task NewPageAsync()
|
|
{
|
|
page = await pdf.AddPageAsync(width, height, cancellationToken).ConfigureAwait(false);
|
|
y = height - Margin;
|
|
page.DrawText(company, Margin, y, PdfFont.HelveticaBold, 9);
|
|
page.DrawTextRight(year.ToString(CultureInfo.InvariantCulture) + "/" + number.ToString(CultureInfo.InvariantCulture), width - Margin, y, PdfFont.Helvetica, 9);
|
|
y -= 14;
|
|
page.DrawText(title, Margin, y, PdfFont.HelveticaBold, 12);
|
|
y -= 12;
|
|
page.DrawText(subtitle, Margin, y, PdfFont.Helvetica, 8.5);
|
|
y -= 16;
|
|
var x = Margin;
|
|
foreach (var column in columns)
|
|
{
|
|
var w = column.Width * scale;
|
|
if (column.Right)
|
|
{
|
|
page.DrawTextRight(column.Title, x + w - 2, y, PdfFont.HelveticaBold, 7.5);
|
|
}
|
|
else
|
|
{
|
|
page.DrawText(column.Title, x + 2, y, PdfFont.HelveticaBold, 7.5);
|
|
}
|
|
|
|
x += w;
|
|
}
|
|
|
|
page.DrawLine(Margin, y - 3, width - Margin, y - 3, 0.6, Rule);
|
|
y -= LineHeight + 2;
|
|
number++;
|
|
}
|
|
|
|
await NewPageAsync().ConfigureAwait(false);
|
|
foreach (var row in rows)
|
|
{
|
|
if (y < Margin + LineHeight)
|
|
{
|
|
await NewPageAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
var bold = row.Count > 0 && row[0] is { } first && first.StartsWith("**", StringComparison.Ordinal);
|
|
var font = bold ? PdfFont.HelveticaBold : PdfFont.Helvetica;
|
|
var x = Margin;
|
|
for (var i = 0; i < columns.Count; i++)
|
|
{
|
|
var w = columns[i].Width * scale;
|
|
var text = i < row.Count ? row[i] : null;
|
|
if (i == 0 && bold)
|
|
{
|
|
text = text![2..];
|
|
}
|
|
|
|
text = PdfTextLayout.Fit(text, font, 7.5, w - 4);
|
|
if (columns[i].Right)
|
|
{
|
|
page!.DrawTextRight(text, x + w - 2, y, font, 7.5);
|
|
}
|
|
else
|
|
{
|
|
page!.DrawText(text, x + 2, y, font, 7.5);
|
|
}
|
|
|
|
x += w;
|
|
}
|
|
|
|
if (bold)
|
|
{
|
|
page!.DrawLine(Margin, y + LineHeight - 2, width - Margin, y + LineHeight - 2, 0.4, Rule);
|
|
}
|
|
|
|
y -= LineHeight;
|
|
}
|
|
|
|
pages = pdf.PageCount;
|
|
await pdf.CloseAsync(cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
return (buffer.ToArray(), pages);
|
|
}
|
|
|
|
/// <summary>An amount as the prints show it, with the Italian separators.</summary>
|
|
/// <param name="amount">The amount.</param>
|
|
/// <returns>The text; empty for zero.</returns>
|
|
public static string Money(decimal amount) => amount == 0m ? string.Empty : amount.ToString("#,##0.00", Italian);
|
|
|
|
/// <summary>A date as the prints show it.</summary>
|
|
/// <param name="date">The date.</param>
|
|
/// <returns>The text.</returns>
|
|
public static string Date(DateOnly? date) => date?.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) ?? string.Empty;
|
|
|
|
private static readonly CultureInfo Italian = CultureInfo.GetCultureInfo("it-IT");
|
|
}
|