feat: automatic codes and automatic filling (#43)
Parties, items and the CRM archives need codes given by the system without
duplicates, and the address and bank fields should fill themselves; this adds
both before the party and item screens that use them.
- CodeMask ({seq:0000}, {anno}, {categoria}) and code_sequences (migration
015): one sequence per rule, year and category, taken under a lock in the
transaction that saves the record, skipping codes written by hand
- CodeRuleService: rules in company settings with the masks of SPEC 11.12,
validation, preview of the next code, manual code allowed or not
- AutoFillService: municipality from postal code and back, personal tax code,
VIES, bank and BIC from the IBAN, delivery date on working days, options
that can be switched off (the ECB job honours its option)
- IBankDirectory.ReadIban gives ABI and CAB even without the ABI list; the
Italian pack now registers the directory
- Company preferences page (mockup 074) at /tabelle/preferenze, in the menu
- aria-checked now renders true/false on switch rows and profile radios
- ADR 0027, wiki, CHANGELOG, STATUS, DATA-MODEL
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9bcf8c1082
commit
07555de700
27 files changed
+1627
-40
No files matched your search
@@ -99,7 +99,7 @@
|
||||
<div class="kh-seg" role="radiogroup" aria-label="@T["Pages.Profile.Density"]">
|
||||
@foreach (var density in Enum.GetValues<Density>())
|
||||
{
|
||||
<button type="button" role="radio" class="@(Draft.Density == density ? "on" : null)" aria-checked="@(Draft.Density == density)"
|
||||
<button type="button" role="radio" class="@(Draft.Density == density ? "on" : null)" aria-checked="@(Draft.Density == density ? "true" : "false")"
|
||||
@onclick="() => ChangeAsync(Draft with { Density = density })">@T["Pages.Profile.Density." + density]</button>
|
||||
}
|
||||
</div>
|
||||
@@ -110,7 +110,7 @@
|
||||
<div class="kh-seg" role="radiogroup" aria-label="@T["Pages.Profile.TextSize"]">
|
||||
@foreach (var size in UserPreferences.TextSizes)
|
||||
{
|
||||
<button type="button" role="radio" class="@(Draft.TextSize == size ? "on" : null)" aria-checked="@(Draft.TextSize == size)"
|
||||
<button type="button" role="radio" class="@(Draft.TextSize == size ? "on" : null)" aria-checked="@(Draft.TextSize == size ? "true" : "false")"
|
||||
@onclick="() => ChangeAsync(Draft with { TextSize = size })">@T.Format("Pages.Profile.TextSize.Value", size)</button>
|
||||
}
|
||||
</div>
|
||||
@@ -121,7 +121,7 @@
|
||||
<div class="kh-seg" role="radiogroup" aria-label="@T["Pages.Profile.Menu"]">
|
||||
@foreach (var (value, key) in MenuChoices)
|
||||
{
|
||||
<button type="button" role="radio" class="@(Draft.MenuCollapsed == value ? "on" : null)" aria-checked="@(Draft.MenuCollapsed == value)"
|
||||
<button type="button" role="radio" class="@(Draft.MenuCollapsed == value ? "on" : null)" aria-checked="@(Draft.MenuCollapsed == value ? "true" : "false")"
|
||||
@onclick="() => ChangeAsync(Draft with { MenuCollapsed = value })">@T[key]</button>
|
||||
}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
@page "/tabelle/preferenze"
|
||||
@attribute [Authorize]
|
||||
@using Khaemhat.Application.Licensing
|
||||
@using Khaemhat.Application.Security
|
||||
@using Khaemhat.Application.Tables
|
||||
@using Khaemhat.Core.Tables
|
||||
@using Khaemhat.Web.Tables
|
||||
@inject ShellSession Session
|
||||
@inject CodeRuleService Codes
|
||||
@inject AutoFillService AutoFill
|
||||
@inject TableService Tables
|
||||
@inject ILicenseGate License
|
||||
@inject ToastService Toasts
|
||||
@inject TimeProvider Clock
|
||||
|
||||
@* Company preferences (SPEC §11.12, mockup 074): the masks of the automatic codes with the preview of the next code,
|
||||
and the automatic filling options. The other tabs of the mockup arrive with the documents and the e-mails. *@
|
||||
<KhPageTitle Text="@T["CompanyPreferences.Title"]" Icon="settings" />
|
||||
|
||||
<TablesGuard>
|
||||
<div class="phead">
|
||||
<div>
|
||||
<div class="crumbs"><a href="@TableScreens.BasePath">@T["Shell.Menu.CompanyTables"]</a> / @T["CompanyPreferences.Title"]</div>
|
||||
<h1>@T["CompanyPreferences.Heading"]</h1>
|
||||
<div class="sub">@T["CompanyPreferences.Hint"]</div>
|
||||
</div>
|
||||
<div class="grow"></div>
|
||||
@if (_modified)
|
||||
{
|
||||
<KhBadge Tone="KhTone.Warning">@T["CompanyTables.Modified"]</KhBadge>
|
||||
}
|
||||
<div class="toolbar">
|
||||
<button type="button" class="btn" disabled="@(!_modified)" @onclick="UndoAsync"><KhIcon Name="undo" /> @T["CompanyTables.Undo"]</button>
|
||||
<button type="button" class="btn primary" disabled="@(!_modified || !CanWrite || _busy)" @onclick="SaveAsync"><KhIcon Name="save" /> @T["Common.Save"]</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (_loaded)
|
||||
{
|
||||
<section class="panel kh-tables-profile">
|
||||
<KhTabs Items="TabItems" Active="@_tab" ActiveChanged="t => _tab = t" Label="@T["CompanyPreferences.Title"]" />
|
||||
@if (_tab == "codes")
|
||||
{
|
||||
<KhForm Validation="_validation" ReadOnly="!CanWrite" Layout="KhFormLayout.Groups">
|
||||
@foreach (var group in CodeRuleService.Definitions.GroupBy(d => d.Group))
|
||||
{
|
||||
<KhFieldGroup Title="@T["CompanyPreferences.Groups." + group.Key]" Icon="@GroupIcon(group.Key)">
|
||||
@foreach (var definition in group)
|
||||
{
|
||||
var rule = _rules[definition.Key];
|
||||
<KhTextBox Label="@T["CompanyPreferences.Rules." + definition.Key]" Name="@(definition.Key + "." + CodeRuleService.MaskField)" Mono="true"
|
||||
Value="@rule.Mask" MaxLength="40" ReadOnly="!CanWrite" Hint="@HintOf(definition)"
|
||||
ValueChanged="v => ChangeMaskAsync(definition.Key, v)" Span="6" />
|
||||
}
|
||||
<KhCheck Label="@T["CompanyPreferences.AllowManual"]" Name="@(group.Key + ".manual")" Value="group.All(d => _rules[d.Key].AllowManual)"
|
||||
ValueChanged="v => SetManual(group.Key, v)" ReadOnly="!CanWrite" Span="12" />
|
||||
@if (group.Key == "items")
|
||||
{
|
||||
<KhCheck Label="@T["CompanyPreferences.UniqueBarcode"]" Name="items.unique-barcode" Value="UniqueBarcode"
|
||||
ValueChanged="SetUniqueBarcode" ReadOnly="!CanWrite" Span="12" />
|
||||
}
|
||||
</KhFieldGroup>
|
||||
}
|
||||
<KhFieldGroup Title="@T["CompanyPreferences.Groups.documents"]" Icon="file">
|
||||
<p class="muted c12">@T["CompanyPreferences.Numerators"] <a href="/tabelle/numeratori">@T["CompanyPreferences.NumeratorsLink"]</a></p>
|
||||
</KhFieldGroup>
|
||||
</KhForm>
|
||||
<p class="muted kh-pad"><KhIcon Name="info" /> @T["CompanyPreferences.Placeholders"]</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="kh-pad kh-autofill">
|
||||
@foreach (var option in AutoFillService.Options)
|
||||
{
|
||||
<KhSwitchRow Label="@T["CompanyPreferences.Options." + option.Key]" Hint="@T["CompanyPreferences.Options." + option.Key + ".Hint"]"
|
||||
Value="_options.GetValueOrDefault(option.Key)" ValueChanged="v => SetOption(option.Key, v)" />
|
||||
}
|
||||
<p class="muted"><KhIcon Name="info" /> @T["CompanyPreferences.AutoFillHint"]</p>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
}
|
||||
</TablesGuard>
|
||||
|
||||
@code {
|
||||
private readonly KhValidation _validation = new();
|
||||
private Dictionary<string, CodeRule> _rules = new(StringComparer.Ordinal);
|
||||
private Dictionary<string, CodeRule> _savedRules = new(StringComparer.Ordinal);
|
||||
private Dictionary<string, bool> _options = new(StringComparer.Ordinal);
|
||||
private Dictionary<string, bool> _savedOptions = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<string, string?> _previews = new(StringComparer.Ordinal);
|
||||
private string? _category;
|
||||
private string _tab = "codes";
|
||||
private bool _loaded;
|
||||
private bool _modified;
|
||||
private bool _busy;
|
||||
|
||||
private bool CanWrite => Session.Permissions.Allows(TableService.WritePermission);
|
||||
|
||||
private bool UniqueBarcode => _rules.TryGetValue("items", out var items) && items.UniqueBarcode;
|
||||
|
||||
private IReadOnlyList<KhTabItem> TabItems =>
|
||||
[
|
||||
new("codes", "key", T["CompanyPreferences.Tabs.codes"], _validation.Messages.Count is > 0 and var n ? n : null),
|
||||
new("autofill", "zap", T["CompanyPreferences.Tabs.autofill"], null),
|
||||
];
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await Session.EnsureLoadedAsync();
|
||||
if (Session.Actor is { } actor && Session.Company?.Code is { } company && Session.Permissions.Allows(TableService.ReadPermission))
|
||||
{
|
||||
_category = (await Tables.ActiveAsync<ItemCategory>(actor, company)).OrderBy(c => c.Code, StringComparer.Ordinal).FirstOrDefault()?.Code ?? "GEN";
|
||||
Show(await Codes.RulesAsync(actor, company), await AutoFill.OptionsAsync(actor, company));
|
||||
await PreviewAllAsync();
|
||||
_loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void Show(IReadOnlyList<CodeRule> rules, IReadOnlyDictionary<string, bool> options)
|
||||
{
|
||||
_rules = rules.ToDictionary(r => r.Key, StringComparer.Ordinal);
|
||||
_savedRules = new(_rules, StringComparer.Ordinal);
|
||||
_options = options.ToDictionary(o => o.Key, o => o.Value, StringComparer.Ordinal);
|
||||
_savedOptions = new(_options, StringComparer.Ordinal);
|
||||
_modified = false;
|
||||
_validation.Clear();
|
||||
}
|
||||
|
||||
private string HintOf(CodeRuleDefinition definition)
|
||||
{
|
||||
if (!License.IsModuleActive(definition.Module))
|
||||
{
|
||||
return T.Format("CompanyPreferences.ModuleOff", definition.Module);
|
||||
}
|
||||
|
||||
return _previews.GetValueOrDefault(definition.Key) is { } next ? T.Format("CompanyPreferences.Next", next) : T["CompanyPreferences.NextInvalid"];
|
||||
}
|
||||
|
||||
private static string GroupIcon(string group) => group switch
|
||||
{
|
||||
"parties" => "users",
|
||||
"items" => "box",
|
||||
_ => "target",
|
||||
};
|
||||
|
||||
private async Task ChangeMaskAsync(string key, string? mask)
|
||||
{
|
||||
_rules[key] = _rules[key] with { Mask = (mask ?? string.Empty).Trim() };
|
||||
_modified = true;
|
||||
await PreviewAsync(key);
|
||||
}
|
||||
|
||||
private void SetManual(string group, bool allow)
|
||||
{
|
||||
foreach (var definition in CodeRuleService.Definitions.Where(d => d.Group == group))
|
||||
{
|
||||
_rules[definition.Key] = _rules[definition.Key] with { AllowManual = allow };
|
||||
}
|
||||
|
||||
_modified = true;
|
||||
}
|
||||
|
||||
private void SetUniqueBarcode(bool unique)
|
||||
{
|
||||
_rules["items"] = _rules["items"] with { UniqueBarcode = unique };
|
||||
_modified = true;
|
||||
}
|
||||
|
||||
private void SetOption(string key, bool on)
|
||||
{
|
||||
if (!CanWrite)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_options[key] = on;
|
||||
_modified = true;
|
||||
}
|
||||
|
||||
private async Task PreviewAllAsync()
|
||||
{
|
||||
foreach (var key in _rules.Keys.ToList())
|
||||
{
|
||||
await PreviewAsync(key);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PreviewAsync(string key)
|
||||
{
|
||||
if (Session.Actor is { } actor && Session.Company?.Code is { } company)
|
||||
{
|
||||
var today = DateOnly.FromDateTime(Clock.GetLocalNow().DateTime);
|
||||
_previews[key] = await Codes.PreviewAsync(actor, company, _rules[key], today, _category);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UndoAsync()
|
||||
{
|
||||
Show([.. _savedRules.Values], _savedOptions);
|
||||
await PreviewAllAsync();
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
if (Session.Actor is not { } actor || Session.Company?.Code is not { } company)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_busy = true;
|
||||
_validation.Clear();
|
||||
try
|
||||
{
|
||||
var problems = CodeRuleService.Validate(_rules.Values);
|
||||
if (problems.Count > 0)
|
||||
{
|
||||
Refuse(problems.Select(p => ((string?)p.Field, p.Code)));
|
||||
return;
|
||||
}
|
||||
|
||||
var rules = await Codes.SaveAsync(actor, company, [.. _rules.Values]);
|
||||
var options = await AutoFill.SaveOptionsAsync(actor, company, _options);
|
||||
Show(rules, options);
|
||||
await PreviewAllAsync();
|
||||
Toasts.Show(ToastKind.Success, T["CompanyPreferences.Saved"], null);
|
||||
}
|
||||
catch (ValidationFailedException ex)
|
||||
{
|
||||
Refuse(ex.Errors.Select(TableValues.SplitError));
|
||||
}
|
||||
finally
|
||||
{
|
||||
_busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void Refuse(IEnumerable<(string? Field, string Code)> errors)
|
||||
{
|
||||
foreach (var (field, code) in errors)
|
||||
{
|
||||
_validation.Add(field, T.Contains(code) ? T[code] : T["CompanyTables.UnknownError"]);
|
||||
}
|
||||
|
||||
_tab = "codes";
|
||||
Toasts.Show(ToastKind.Error, T["CompanyTables.NotSaved"], T["CompanyTables.FixFields"]);
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,7 @@
|
||||
[
|
||||
("/tabelle/profilo-azienda", "building", "CompanyTables.Profile.Title", "CompanyTables.Profile.Hint"),
|
||||
("/tabelle/esercizi", "calendar", "CompanyTables.FiscalYears.Title", "CompanyTables.FiscalYears.Hint"),
|
||||
("/tabelle/preferenze", "settings", "CompanyPreferences.Title", "CompanyPreferences.Hint"),
|
||||
("/tabelle/cambi", "euro", "CompanyTables.Rates.Title", "CompanyTables.Rates.Hint"),
|
||||
];
|
||||
}
|
||||
Reference in new issue
Block a user