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