@page "/vendite/{Segment}"
@page "/acquisti/{Segment}"
@page "/magazzino/{Segment}"
@attribute [Authorize]
@using Khaemhat.Application.Documents
@using Khaemhat.Core.Documents
@using Khaemhat.Web.Documents
@inject ShellSession Session
@inject DocumentEngine Engine
@inject DocumentEditor Editor
@inject NavigationManager Navigation
@inject TimeProvider Clock
@* The radar of a kind of documents (SPEC §11.3-§11.6): quotations, orders, delivery notes, invoices (with pro forma
and fees), credit and debit notes; supplier orders, receipts, supplier invoices, self-invoices; warehouse
movements. Search on number, reference and party; filters for state, type and year; totals of the page. Enter or
double click opens the document. *@
@if (!_loaded)
{
@T["Common.Loading"]
}
else if (Session.Company is null)
{
@T["Sales.NoCompany"]
}
else if (_section is null)
{
@T["Pages.NotFound.Title"]
}
else if (!CanRead)
{
@T["Pages.AccessDenied.Title"]
@T["Pages.AccessDenied.Lead"]
}
else
{
@if (_section.Area == SalesScreens.PurchasesPath && _section.Segment == "autofatture")
{
}
@if (NewTypes.Count > 0)
{
@T["Sales.Sections." + _section.Key + ".New"]
}
@if (_section.Area == SalesScreens.BasePath && _section.Segment == "fatture" && Session.Permissions.Allows("sales.invoice.write"))
{
@T["Sales.Deferred.Title"]
}
@if (_section.Area == SalesScreens.PurchasesPath && _section.Segment == "fatture" && Session.Permissions.Allows("purchases.invoice.write"))
{
@T["Shell.Menu.Purchases.Import"]
}
@foreach (var type in NewTypes)
{
Create(type.Code)">
@type.Code
@type.Type.Description
@T["Sales.Hints.Kinds." + type.Kind]
}
}
@code {
private SalesSection? _section;
private IReadOnlyList _definitions = [];
private bool _loaded;
private bool _choosing;
/// The radar segment, such as fatture .
[Parameter]
public string? Segment { get; set; }
private string Title => _section is null ? T["Shell.Menu.Sales"] : T["Sales.Sections." + _section.Key + ".Title"];
private bool Internal => _section?.Direction == DocumentDirection.Internal;
private bool CanRead => _section is not null && Session.Permissions.Allows(DocumentBehaviors.Of(_section.Kinds[0]).ReadPermission);
private DateOnly Today => DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(Clock.GetUtcNow(), TimeZoneInfo.Local).DateTime);
// The types of the radar the user may create, in the order of the section.
private List NewTypes => _section is null ? [] :
[.. _section.NewTypes.Select(code => _definitions.FirstOrDefault(d => d.Code == code)).OfType()
.Where(d => Session.Permissions.Allows(d.Behavior.WritePermission))];
private IReadOnlyList> Columns =>
[
new("number", T[_section?.Kinds.Contains(DocumentKind.PurchaseInvoice) == true ? "Purchases.Fields.Protocol" : "Sales.Fields.DocumentNumber"], r => SalesScreens.Number(T, r.DocumentNumber, r.Status)) { Kind = KhColumnKind.Code, Width = "130px" },
new("date", T["Sales.Fields.DocumentDate"], r => r.Date) { Kind = KhColumnKind.Date, Width = "100px" },
new("type", T["Sales.Radar.Kind"], r => SalesScreens.KindName(T, r.Kind)) { Width = "150px", Sortable = false, Visible = _section?.Kinds.Count > 1 },
new("party", T[SalesScreens.PartyLabel(_section?.Direction ?? DocumentDirection.Sale)], r => r.PartyName) { Sortable = false, Visible = !Internal },
new("reference", T[_section?.Direction == DocumentDirection.Purchase ? "Purchases.Fields.SupplierNumber" : "Sales.Fields.ExternalReference"], r => r.Reference) { Width = "140px" },
new("validUntil", T["Sales.Fields.ValidUntil"], r => r.ValidUntil) { Kind = KhColumnKind.Date, Width = "100px", Visible = _section?.Segment == "preventivi" },
new("delivery", T["Sales.Fields.DeliveryDate"], r => r.DeliveryDate) { Kind = KhColumnKind.Date, Width = "100px", Visible = _section?.Segment is "ordini" or "ddt" or "ricevimenti" },
new("taxable", T["Sales.Totals.Taxable"], r => r.Taxable) { Kind = KhColumnKind.Money, Width = "120px", Visible = !Internal },
new("total", T["Sales.Totals.Total"], r => r.Total) { Kind = KhColumnKind.Money, Width = "120px", Visible = !Internal },
new("status", T["Sales.Radar.State"], r => StateOf(r).Text)
{
Width = "130px",
Template = r => @@StateOf(r).Text ,
},
];
private IReadOnlyList QuickFilters
{
get
{
var filters = new List
{
new("status", T["Sales.Radar.State"], [new(string.Empty, T["Sales.Radar.All"]), .. States.Select(s => new KhOption(s.ToString(), T["Sales.States." + s]))]),
};
if (_section is { Kinds.Count: > 1 })
{
filters.Add(new("kind", T["Sales.Radar.Kind"], [new(string.Empty, T["Sales.Radar.All"]), .. _section.Kinds.Select(k => new KhOption(k.ToString(), SalesScreens.KindName(T, k)))]));
}
var year = Today.Year;
filters.Add(new("year", T["Sales.Radar.Year"],
[new(string.Empty, T["Sales.Radar.All"]), .. Enumerable.Range(0, 3).Select(i => new KhOption((year - i).ToString(T.Culture), (year - i).ToString(T.Culture)))]));
return filters;
}
}
// The states that make sense for the life cycle of the radar.
private IEnumerable States => _section is null ? [] :
DocumentBehaviors.Of(_section.Kinds[0]).Lifecycle == DocumentLifecycle.Invoice && _section.Kinds.All(k => DocumentBehaviors.Of(k).Lifecycle == DocumentLifecycle.Invoice)
? [DocumentStatus.Draft, DocumentStatus.Issued, DocumentStatus.Cancelled]
: [DocumentStatus.Draft, DocumentStatus.Confirmed, DocumentStatus.PartiallyFulfilled, DocumentStatus.Fulfilled, DocumentStatus.Closed, DocumentStatus.Cancelled, DocumentStatus.Issued];
///
protected override async Task OnParametersSetAsync()
{
await Session.EnsureLoadedAsync();
_section = SalesScreens.Find(SalesScreens.AreaOf(Navigation.ToBaseRelativePath(Navigation.Uri)), Segment);
if (!_loaded && Session.Actor is { } actor && Session.Company?.Code is { } company)
{
_definitions = await Engine.DefinitionsAsync(actor, company);
}
_loaded = true;
}
private (string Text, KhTone Tone) StateOf(DocumentRow row) => SalesScreens.State(T, row.Kind, row.Status, row.ValidUntil, Today);
private static string? RowClassOf(DocumentRow row) => row.Status == DocumentStatus.Cancelled ? "muted" : null;
private Task> ListAsync(ListQuery query, CancellationToken cancellationToken) =>
Session.Actor is { } actor && Session.Company?.Code is { } company && _section is not null
? Editor.ListAsync(actor, company, _section.Family, _section.Kinds, query, cancellationToken)
: Task.FromResult(new ListPage([], 0, new Dictionary()));
private void Open(DocumentRow row)
{
if (SalesScreens.RecordUrl(row.Kind, row.Id) is { } url)
{
Navigation.NavigateTo(url);
}
}
private void New()
{
if (NewTypes.Count == 1)
{
Create(NewTypes[0].Code);
}
else
{
_choosing = true;
}
}
private void Create(string code)
{
_choosing = false;
if (_section is not null)
{
Navigation.NavigateTo(SalesScreens.NewUrl(_section, code));
}
}
}