using Khaemhat.Application.Security;
using Khaemhat.Core.Documents;
using Khaemhat.Core.Tables;
using Khaemhat.Data;
using Khaemhat.Data.Querying;
namespace Khaemhat.Application.Documents;
///
/// A document type of the company with the behaviour of its kind (SPEC ยง11.3, ADR 0032): code, kind, direction,
/// numerator, warehouse reason, effects, sources for fulfilment, SDI type, journal template, VAT register, print
/// template.
///
/// The row of document_types.
/// The behaviour of its kind.
public sealed record DocumentDefinition(DocumentType Type, DocumentBehavior Behavior)
{
/// The code of the type.
public string Code => Type.Code;
/// The kind.
public DocumentKind Kind => Behavior.Kind;
/// The family (tables).
public DocumentFamily Family => Behavior.Family;
/// The life cycle.
public DocumentLifecycle Lifecycle => Behavior.Lifecycle;
/// The action that confirms the documents: issue for invoices, confirm for the others.
public DocumentAction ConfirmAction => Lifecycle == DocumentLifecycle.Invoice ? DocumentAction.Issue : DocumentAction.Confirm;
}
/// Reads the definitions of the company's document types.
public static class DocumentDefinitionService
{
/// Error: the document type does not exist.
public const string TypeMissing = "document.type.missing";
/// Error: the document type has no kind.
public const string TypeWithoutKind = "document.type.no-kind";
/// Error: the document type is disabled.
public const string TypeDisabled = "document.type.disabled";
/// The definition of a document type.
/// A company unit of work.
/// The type.
/// Cancellation.
/// The definition.
/// The type is missing or has no kind.
public static async Task GetAsync(IUnitOfWork uow, long typeId, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(uow);
var type = await uow.Repository().GetAsync(typeId, cancellationToken).ConfigureAwait(false)
?? throw new ValidationFailedException([nameof(DocumentHeader.DocumentTypeId) + ":" + TypeMissing]);
return Of(type);
}
/// The definition of a document type by code.
/// A company unit of work.
/// The code, such as FI.
/// Cancellation.
/// The definition.
/// The type is missing or has no kind.
public static async Task GetAsync(IUnitOfWork uow, string code, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(uow);
var normalized = (code ?? string.Empty).Trim().ToUpperInvariant();
var type = await uow.Repository().FindAsync(Query.For().Where(t => t.Code == normalized), cancellationToken).ConfigureAwait(false)
?? throw new ValidationFailedException([nameof(DocumentHeader.DocumentTypeId) + ":" + TypeMissing]);
return Of(type);
}
/// The definitions of every usable type (enabled, with a kind), by code.
/// A company unit of work.
/// Cancellation.
/// The definitions.
public static async Task> ListAsync(IUnitOfWork uow, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(uow);
var types = await uow.Repository().ListAsync(Query.For().OrderBy(t => t.Code), cancellationToken).ConfigureAwait(false);
return [.. types.Items.Where(t => !t.Disabled && t.DocumentKind is not null).Select(Of)];
}
/// The definition of a type row.
/// The row.
/// The definition.
/// The type has no kind.
public static DocumentDefinition Of(DocumentType type)
{
ArgumentNullException.ThrowIfNull(type);
return type.DocumentKind is { } kind
? new DocumentDefinition(type, DocumentBehaviors.Of(kind))
: throw new ValidationFailedException([nameof(DocumentHeader.DocumentTypeId) + ":" + TypeWithoutKind]);
}
}