feat: guided import and export for parties, items, prices, opening stock and chart of accounts (#44)

Companies starting on Khaemhat need to bring their master data from another
system or a spreadsheet, and the chart of accounts has no Italian exchange
standard, so SPEC 12.12 asks for a guided wizard and an own layout.

- Administration > Import / export: CSV (separator and encoding guessed),
  XLSX (in-house reader, first sheet) and JSON, automatic column mapping with
  aliases, row-by-row check, error report, import confirmation and log entry.
- Rows go through PartyService, ItemService and PriceListService, so the
  import has exactly the rules, error codes and history of the forms; options
  cover existing and missing codes, VIES and city from postal code.
- Export writes the same columns as the import, plus empty templates.
- Opening stock and the chart of accounts (khaemhat-coa layout, natura, tipo,
  flag) are check-only until M6 and M7 add the ledgers to write into.
- PartyService.SaveAsync takes an explicit VIES switch for the import.
- ADR 0031, wiki Import-Export, CHANGELOG and STATUS updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-12 11:52:59 +01:00
1 parent 3dbbaba86e
commit caa33a2cee
29 files changed
+4783 -90

No files matched your search

@@ -0,0 +1,197 @@
using Khaemhat.Application.Security;
namespace Khaemhat.Application.ImportExport;
/// <summary>The formats of the guided import and export (SPEC §12.12).</summary>
public enum TransferFormat
{
/// <summary>Text with <c>;</c>, UTF-8 (other separators and Windows-1252 are recognized when reading).</summary>
Csv = 0,
/// <summary>Excel workbook, first sheet, headers in the first row.</summary>
Xlsx = 1,
/// <summary>JSON: an array of objects keyed by column, or the object of <c>khaemhat-coa.json</c>.</summary>
Json = 2,
}
/// <summary>The kind of value of a column, for reading and writing it.</summary>
public enum ImportFieldKind
{
/// <summary>Free text.</summary>
Text = 0,
/// <summary>A code of another archive or table.</summary>
Code = 1,
/// <summary>A decimal number (Italian or invariant notation).</summary>
Number = 2,
/// <summary>A date (<c>dd/MM/yyyy</c>, ISO or a spreadsheet serial).</summary>
Date = 3,
/// <summary>Yes or no.</summary>
Boolean = 4,
/// <summary>One of a list of words.</summary>
Choice = 5,
}
/// <summary>A column an entity accepts.</summary>
/// <param name="Key">The column name used in files and templates (<c>partita_iva</c>).</param>
/// <param name="Kind">The kind of value.</param>
/// <param name="Required">Whether a new record needs it.</param>
/// <param name="Aliases">Other headers recognized by the automatic mapping, already normalized.</param>
/// <param name="Choices">The words accepted for <see cref="ImportFieldKind.Choice"/>.</param>
public sealed record ImportField(string Key, ImportFieldKind Kind = ImportFieldKind.Text, bool Required = false, IReadOnlyList<string>? Aliases = null,
IReadOnlyList<string>? Choices = null);
/// <summary>A file read for the import.</summary>
/// <param name="Name">The file name.</param>
/// <param name="Format">The format.</param>
/// <param name="Encoding">The text encoding (CSV), such as <c>UTF-8</c>.</param>
/// <param name="Separator">The separator (CSV).</param>
/// <param name="Headers">The headers of the columns.</param>
/// <param name="Rows">The rows under the headers, each as long as the headers.</param>
/// <param name="Sections">For <c>khaemhat-coa.json</c>: the other sections with their number of entries.</param>
public sealed record ImportFile(string Name, TransferFormat Format, string? Encoding, char? Separator, IReadOnlyList<string> Headers,
IReadOnlyList<IReadOnlyList<string>> Rows, IReadOnlyDictionary<string, int>? Sections = null)
{
/// <summary>The number of data rows.</summary>
public int Count => Rows.Count;
}
/// <summary>What happens to a row whose code is already in the archive.</summary>
public enum ExistingCodeAction
{
/// <summary>The record is updated with the non empty cells of the row.</summary>
Update = 0,
/// <summary>The row is skipped.</summary>
Skip = 1,
}
/// <summary>What happens to a row without a code.</summary>
public enum MissingCodeAction
{
/// <summary>The code comes from the automatic code rules.</summary>
Generate = 0,
/// <summary>The row is an error.</summary>
Refuse = 1,
}
/// <summary>The options of an import (mockup 077, step 1).</summary>
public sealed record ImportOptions
{
/// <summary>Rows whose code exists.</summary>
public ExistingCodeAction Existing { get; init; } = ExistingCodeAction.Update;
/// <summary>Rows without a code.</summary>
public MissingCodeAction Missing { get; init; } = MissingCodeAction.Generate;
/// <summary>Check the VAT numbers of EU parties on VIES while importing.</summary>
public bool VerifyVat { get; init; }
/// <summary>Fill the city from the postal code (and the postal code from the city) when one is missing.</summary>
public bool FillFromPostalCode { get; init; } = true;
}
/// <summary>Who imports, where and how: what a target needs besides the rows.</summary>
/// <param name="Actor">Who imports.</param>
/// <param name="CompanyCode">The company.</param>
/// <param name="Options">The options.</param>
public sealed record ImportContext(Actor Actor, string CompanyCode, ImportOptions Options);
/// <summary>A row of the file with the cells of the mapped columns by field key.</summary>
/// <param name="Line">The line in the file, counting the header as line 1.</param>
/// <param name="Values">The trimmed cells by field key; empty cells are left out.</param>
public sealed record ImportRow(int Line, IReadOnlyDictionary<string, string> Values)
{
/// <summary>The cell of a field, or <see langword="null"/> when empty or not mapped.</summary>
/// <param name="key">The field.</param>
/// <returns>The text.</returns>
public string? this[string key] => Values.TryGetValue(key, out var value) ? value : null;
}
/// <summary>The outcome of a row in the preview (mockup 077: OK, avviso, errore).</summary>
public enum ImportOutcome
{
/// <summary>Importable.</summary>
Ok = 0,
/// <summary>Importable, with a correction proposed or a remark.</summary>
Warning = 1,
/// <summary>Not importable.</summary>
Error = 2,
}
/// <summary>What the import does with a row.</summary>
public enum ImportAction
{
/// <summary>A new record.</summary>
Create = 0,
/// <summary>An existing record changes.</summary>
Update = 1,
/// <summary>Nothing (existing code to skip, or an error).</summary>
Skip = 2,
}
/// <summary>A remark on a row: a text code with the field it concerns.</summary>
/// <param name="Field">The field key, or empty for the whole row.</param>
/// <param name="Code">The text code (an error code of the services or <c>import.*</c>).</param>
/// <param name="IsWarning">A warning instead of an error.</param>
/// <param name="Value">A value to show in the text (<c>{0}</c>), such as the code not found or the city proposed.</param>
public sealed record ImportMessage(string Field, string Code, bool IsWarning = false, string? Value = null);
/// <summary>The check of a row.</summary>
/// <param name="Line">The line in the file.</param>
/// <param name="Key">The code or description that identifies the row in the preview.</param>
/// <param name="Action">What the import will do.</param>
/// <param name="Messages">The remarks.</param>
public sealed record ImportRowResult(int Line, string Key, ImportAction Action, IReadOnlyList<ImportMessage> Messages)
{
/// <summary>The outcome from the remarks.</summary>
public ImportOutcome Outcome =>
Messages.Any(m => !m.IsWarning) ? ImportOutcome.Error : Messages.Count > 0 ? ImportOutcome.Warning : ImportOutcome.Ok;
}
/// <summary>The preview of an import (mockup 077, steps 3 and 4).</summary>
/// <param name="Target">The entity.</param>
/// <param name="Rows">The checked rows, in file order.</param>
/// <param name="CanApply">Whether the entity can be written now (opening stock and chart of accounts only check, until M6 and M7).</param>
public sealed record ImportPreview(string Target, IReadOnlyList<ImportRowResult> Rows, bool CanApply)
{
/// <summary>Rows that will create a record.</summary>
public int Creates => Rows.Count(r => r.Outcome != ImportOutcome.Error && r.Action == ImportAction.Create);
/// <summary>Rows that will update a record.</summary>
public int Updates => Rows.Count(r => r.Outcome != ImportOutcome.Error && r.Action == ImportAction.Update);
/// <summary>Rows skipped on purpose.</summary>
public int Skips => Rows.Count(r => r.Outcome != ImportOutcome.Error && r.Action == ImportAction.Skip);
/// <summary>Rows with errors.</summary>
public int Errors => Rows.Count(r => r.Outcome == ImportOutcome.Error);
/// <summary>Rows with warnings only.</summary>
public int Warnings => Rows.Count(r => r.Outcome == ImportOutcome.Warning);
/// <summary>Rows the import writes.</summary>
public int Importable => Creates + Updates;
}
/// <summary>The result of an import.</summary>
/// <param name="Target">The entity.</param>
/// <param name="Created">Records created.</param>
/// <param name="Updated">Records updated.</param>
/// <param name="Skipped">Rows skipped on purpose.</param>
/// <param name="Rows">Every row with its final outcome; rows refused while writing carry their errors.</param>
public sealed record ImportReport(string Target, int Created, int Updated, int Skipped, IReadOnlyList<ImportRowResult> Rows)
{
/// <summary>Rows not imported because of errors.</summary>
public int Failed => Rows.Count(r => r.Outcome == ImportOutcome.Error);
}