using Khaemhat.Application.Modules; namespace Khaemhat.Web.Shell; /// Who may see a menu entry besides module and permission. public enum MenuAudience { /// Everybody with the module and the permission. Everyone = 0, /// Only instance accounts (super user and administrators). InstanceAccounts = 1, } /// An entry of the side menu (SPEC §10.2). /// The text key of the label (Shell.Menu.*) and the identity of the entry. /// The icon in icons/sprite.svg. /// The address. /// The licence module that must be active. /// The permission needed, or . /// The milestone that delivers the page, shown while it is not there yet (M4...); once delivered. /// Who may see it. public sealed record MenuItem(string Key, string Icon, string Href, string Module, string? Permission, string? Milestone, MenuAudience Audience = MenuAudience.Everyone) { /// The sub-entries shown under the active module or in the pop-up of the collapsed menu. public IReadOnlyList Children { get; init; } = []; /// Whether an address belongs to this entry or to one of its children. /// The path, without query. /// when it matches. public bool Matches(string path) => Href == "/" ? path == "/" : path.Equals(Href, StringComparison.OrdinalIgnoreCase) || path.StartsWith(Href + "/", StringComparison.OrdinalIgnoreCase); } /// A titled group of the side menu. /// The text key of the title. /// The entries. public sealed record MenuSection(string Key, IReadOnlyList Items); /// /// The side menu of SPEC §10.2, in its order: Home; Anagrafiche, CRM, Vendite, Acquisti, Magazzino, Contabilità, /// Scadenzario, Fatturazione elettronica, Cespiti, Servizi, Canali e-commerce, Report; Tabelle azienda, /// Automazioni, Amministrazione. Entries appear only with the licensed module and the permission. /// public static class MenuCatalog { /// The sections. public static IReadOnlyList Sections { get; } = [ new("Shell.Menu.Section.Main", [ new("Shell.Menu.Home", "home", "/", ModuleCodes.Core, null, null), ]), new("Shell.Menu.Section.Operations", [ Area("Shell.Menu.Parties", "users", "/anagrafiche", ModuleCodes.Core, "parties.party.read", null, ("Shell.Menu.Parties.Customers", "clienti"), ("Shell.Menu.Parties.Suppliers", "fornitori"), ("Shell.Menu.Parties.Contacts", "contatti"), ("Shell.Menu.Parties.Items", "articoli"), ("Shell.Menu.Parties.PriceLists", "listini"), ("Shell.Menu.Parties.Categories", "categorie")), Area("Shell.Menu.Crm", "target", "/crm", ModuleCodes.Crm, "crm.lead.read", "M11", ("Shell.Menu.Crm.Dashboard", "cruscotto"), ("Shell.Menu.Crm.Leads", "lead"), ("Shell.Menu.Crm.Pipeline", "pipeline"), ("Shell.Menu.Crm.Agenda", "agenda"), ("Shell.Menu.Crm.Tickets", "ticket"), ("Shell.Menu.Crm.Campaigns", "campagne")), Area("Shell.Menu.Sales", "cart", "/vendite", ModuleCodes.Core, "sales.invoice.read", null, ("Shell.Menu.Sales.Quotes", "preventivi"), ("Shell.Menu.Sales.Orders", "ordini"), ("Shell.Menu.Sales.DeliveryNotes", "ddt"), ("Shell.Menu.Sales.Invoices", "fatture"), ("Shell.Menu.Sales.CreditNotes", "note-credito"), ("Shell.Menu.Sales.DeferredInvoicing", "fatturazione-differita")), Area("Shell.Menu.Purchases", "truck", "/acquisti", ModuleCodes.Core, "purchases.order.read", null, ("Shell.Menu.Purchases.Orders", "ordini"), ("Shell.Menu.Purchases.Receipts", "ricevimenti"), ("Shell.Menu.Purchases.Invoices", "fatture"), ("Shell.Menu.Purchases.Import", "importa-fatture"), ("Shell.Menu.Purchases.SelfInvoices", "autofatture")), Area("Shell.Menu.Inventory", "box", "/magazzino", ModuleCodes.Core, "inventory.stock.read", null, ("Shell.Menu.Inventory.Stock", "giacenze"), ("Shell.Menu.Inventory.Entries", "movimenti"), ("Shell.Menu.Inventory.StockTake", "inventario"), ("Shell.Menu.Inventory.Reorder", "riordino")), Area("Shell.Menu.Ledger", "book", "/contabilita", ModuleCodes.Accounting, "ledger.entry.read", "M7", ("Shell.Menu.Ledger.Journal", "prima-nota"), ("Shell.Menu.Ledger.Accounts", "piano-dei-conti"), ("Shell.Menu.Ledger.VatRegisters", "registri-iva"), ("Shell.Menu.Ledger.VatSettlement", "liquidazione-iva"), ("Shell.Menu.Ledger.Statements", "bilancio")), Area("Shell.Menu.Finance", "coins", "/scadenzario", ModuleCodes.Finance, "finance.schedule.read", "M9", ("Shell.Menu.Finance.OpenItems", "scadenze"), ("Shell.Menu.Finance.Remittances", "distinte"), ("Shell.Menu.Finance.Dunning", "solleciti"), ("Shell.Menu.Finance.Reconciliation", "riconciliazione")), Area("Shell.Menu.EInvoicing", "file-check", "/fatturazione-elettronica", ModuleCodes.EInvoicing, "einvoice.document.read", "M8", ("Shell.Menu.EInvoicing.Dashboard", "cruscotto"), ("Shell.Menu.EInvoicing.Channels", "canali"), ("Shell.Menu.EInvoicing.StampDuty", "bollo"), ("Shell.Menu.EInvoicing.IntentDeclarations", "dichiarazioni-intento")), Area("Shell.Menu.Assets", "building", "/cespiti", ModuleCodes.Assets, "assets.asset.read", "M10", ("Shell.Menu.Assets.Register", "registro"), ("Shell.Menu.Assets.Depreciation", "ammortamenti")), Area("Shell.Menu.Services", "wrench", "/servizi", ModuleCodes.Services, null, "M12", ("Shell.Menu.Services.Contracts", "contratti"), ("Shell.Menu.Services.Interventions", "interventi")), Area("Shell.Menu.Channels", "globe", "/canali", ModuleCodes.Channels, null, "M12", ("Shell.Menu.Channels.Orders", "ordini")), Area("Shell.Menu.Reports", "chart", "/report", ModuleCodes.Reports, "reports.report.read", "M13"), ]), new("Shell.Menu.Section.System", [ Area("Shell.Menu.CompanyTables", "layers", "/tabelle", ModuleCodes.Core, "company.settings.read", null, ("Shell.Menu.CompanyTables.Profile", "profilo-azienda"), ("Shell.Menu.CompanyTables.FiscalYears", "esercizi"), ("Shell.Menu.CompanyTables.Preferences", "preferenze"), ("Shell.Menu.CompanyTables.TaxCodes", "codici-iva"), ("Shell.Menu.CompanyTables.Payments", "pagamenti"), ("Shell.Menu.CompanyTables.Numerators", "numeratori"), ("Shell.Menu.CompanyTables.Warehouses", "depositi"), ("Shell.Menu.CompanyTables.Currencies", "valute"), ("Shell.Menu.CompanyTables.ExchangeRates", "cambi")), Area("Shell.Menu.Automation", "zap", "/automazioni", ModuleCodes.Automation, null, "M12", ("Shell.Menu.Automation.Rules", "regole"), ("Shell.Menu.Automation.Procedures", "procedure")), WithChild( Area("Shell.Menu.Admin", "shield", "/amministrazione", ModuleCodes.Core, null, "M13", MenuAudience.InstanceAccounts, ("Shell.Menu.Admin.Companies", "aziende"), ("Shell.Menu.Admin.Users", "utenti"), ("Shell.Menu.Admin.Roles", "ruoli"), ("Shell.Menu.Admin.Licenses", "licenze"), ("Shell.Menu.Admin.Jobs", "attivita"), ("Shell.Menu.Admin.Log", "log"), ("Shell.Menu.Admin.Status", "stato")), new MenuItem("Shell.Menu.Admin.ImportExport", "shield", "/amministrazione/import-export", ModuleCodes.Core, null, null, MenuAudience.InstanceAccounts)), ]), ]; /// Every entry with its children, in menu order. public static IEnumerable All => Sections.SelectMany(s => s.Items).SelectMany(i => i.Children.Prepend(i)); /// The entry that best matches an address: the deepest one whose address is a prefix. /// The path, without query. /// The entry, or . public static MenuItem? Find(string path) => All.Where(i => i.Matches(path)).OrderByDescending(i => i.Href.Length).FirstOrDefault(); /// The top-level entry an address belongs to. /// The path. /// The entry, or . public static MenuItem? FindArea(string path) => Sections.SelectMany(s => s.Items).Where(i => i.Matches(path)).OrderByDescending(i => i.Href.Length).FirstOrDefault(); // An entry delivered before the rest of its area (the guided import of M4 under the administration of M13). private static MenuItem WithChild(MenuItem area, MenuItem child) => area with { Children = [.. area.Children, child] }; private static MenuItem Area(string key, string icon, string href, string module, string? permission, string? milestone, params (string Key, string Slug)[] children) => Area(key, icon, href, module, permission, milestone, MenuAudience.Everyone, children); private static MenuItem Area(string key, string icon, string href, string module, string? permission, string? milestone, MenuAudience audience, params (string Key, string Slug)[] children) => new(key, icon, href, module, permission, milestone, audience) { Children = [.. children.Select(c => new MenuItem(c.Key, icon, href + "/" + c.Slug, module, permission, milestone, audience))], }; }