Files
BusinessMgr-2019/SaBusMgrCoreWeb/Web/Models/SbmModel_Base.cs
T
2025-01-22 08:01:51 +00:00

288 lines
14 KiB
C#

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Sacs.Web.Components;
using Sarand.BusinessMgr.Net.Core.Web.App;
using Sarand.BusinessMgr.Net.Core.Web.Controllers;
using Sarand.BusinessMgr.Net.Standard.Base.App;
using Sarand.BusinessMgr.Net.Standard.Base.Bus;
using Sarand.BusinessMgr.Net.Standard.Base.Consoles;
using Sarand.BusinessMgr.Net.Standard.Base.Core;
using Sarand.BusinessMgr.Net.Standard.Base.Mgr;
using Sarand.BusinessMgr.Net.Standard.Base.Mgr.Control;
using Sarand.Net.Standard.Base.CodeTypes;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Dbas;
using Sarand.Net.Standard.Base.Diary;
using Sarand.Net.Standard.Base.Interfaces;
using Sarand.Net.Standard.Base.ItemData;
using Sarand.Net.Standard.Base.ItemDocs;
using Sarand.Net.Standard.Base.ItemOptions;
using Sarand.Net.Standard.Base.ReferenceData;
using Sarand.Net.Standard.Base.Security;
using Sarand.Net.Standard.Base.UpdateLog;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Sarand.BusinessMgr.Net.Core.Web.Models
{
//=======================================================================
public abstract class SbmModel_Base : SaModel
{
//-------------------------------------------------------------------
// Data members.
private SbmInternals internals;
private SbmInternals_Control internals_Control;
//-------------------------------------------------------------------
// Protected construction.
protected SbmModel_Base(SbmLocaliser localiser)
{
Localiser = localiser;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (internals != null) internals.Dispose();
internals = null;
if (internals_Control != null) internals_Control.Dispose();
internals_Control = null;
}
base.Dispose(disposing);
}
//-------------------------------------------------------------------
// Application properties.
private SbmApplication_Web App { get { return SbmGlobals_Web.Application; } }
protected App.SbmAppConfig AppConfig { get { return App.AppConfig; } }
protected SbmSystem_Web System { get { return App.System_Web; } }
public String SessionCookieName { get { return App.SessionCookieName; } }
public SbmSessionData SessionData { get { return SessionData_Web; } }
public SbmSessionData_Web SessionData_Web { get; set; }
public SbmSessionContext SessionContext { get { return SessionData?.SessionContext ?? null; } }
public abstract ISbmUserOrganisationContext UserContext_Core { get; }
public DbUpdateInfo DbUpdateInfo { get { return SessionData?.DbUpdateInfo ?? new DbUpdateInfo(null, "", SbmSystem.ComputerName); } }
public SbmLocaliser Localiser { get; }
// Request properties.
public SbmController_Web Controller { get; set; }
public CultureInfo SessionCulture { get { return Controller?.SessionCulture ?? CultureInfo.InvariantCulture; } }
//-------------------------------------------------------------------
// Page configuration.
public String InstanceVersion { get { return !StringUtil.IsEmpty(App.AppConfig.Instance) ? App.AppConfig.Instance + " v" + Sbm.Version.GetFullVersion() : "v" + Sbm.Version.GetAppVersion(); } }
//-------------------------------------------------------------------
// Business components.
// NB Keep these accessors protected to avoid them being called by the model binding logic before the session data is ready.
protected SbmInternals Internals { get { if (internals == null) internals = new SbmInternals(UserContext_Core); return internals; } }
public SbmInternals_Control Internals_Control { get { if (internals_Control == null) internals_Control = new SbmInternals_Control(); return internals_Control; } }
// Managers.
protected SbmAccountMgr AccountMgr { get { return Internals.AccountMgr; } }
protected SbmAssetMgr AssetMgr { get { return Internals.AssetMgr; } }
protected SbmCommodityMgr CommodityMgr { get { return Internals.CommodityMgr; } }
protected SbmPriceMgr PriceMgr { get { return Internals.PriceMgr; } }
protected SbmTransactionMgr TransactionMgr { get { return Internals.TransactionMgr; } }
protected SbmStockMgr StockMgr { get { return Internals.StockMgr; } }
protected SbmProcessMgr ProcessMgr { get { return Internals.ProcessMgr; } }
protected SbmDataItemMgr DataItemMgr { get { return Internals.DataItemMgr; } }
// Control.
protected SbmcAccountMgr ControlAccountMgr { get { return Internals_Control.ControlAccountMgr; } }
protected SbmcInstanceMgr ControlInstanceMgr { get { return Internals_Control.ControlInstanceMgr; } }
protected SbmcItemMgr ControlItemMgr { get { return Internals_Control.ControlItemMgr; } }
// Consoles.
protected SbmSessionConsole SessionConsole { get { return Internals.SessionConsole; } }
protected SaDiaryConsole DiaryConsole { get { return Internals.DiaryConsole; } }
protected SaSecurityConsole SecurityConsole { get { return Internals.SecurityConsole; } }
protected SaCodeTypeConsole CodeTypeConsole { get { return Internals.CodeTypeConsole; } }
protected SaReferenceDataConsole ReferenceDataConsole { get { return Internals.ReferenceDataConsole; } }
protected SaUpdateLogConsole UpdateLogConsole { get { return Internals.UpdateLogConsole; } }
protected SaInterfaceConsole InterfaceConsole { get { return Internals.InterfaceConsole; } }
protected SaItemDocConsole ItemDocConsole { get { return Internals.ItemDocConsole; } }
protected SaItemOptionConsole ItemOptionConsole { get { return Internals.ItemOptionConsole; } }
protected SaItemDataConsole ItemDataConsole { get { return Internals.ItemDataConsole; } }
// Configurations.
protected SbmStockControlConfig StockControlConfig { get { return Internals.StockControlConfig; } }
protected SbmRequirementCostingConfig RequirementCostingConfig { get { return Internals.RequirementCostingConfig; } }
// System codes.
protected List<TypeCodeDesc> GetCodesOfType(Sbm.SystemCode systemCode) { return Internals.GetSystemCodes(systemCode); }
//-------------------------------------------------------------------
// Security.
public bool ValidUserSession { get { return SessionData?.ValidUser ?? false; } }
public bool AwaitingLogin { get { return !ValidUserSession; } }
public bool KeepLoggedIn { get { return !AwaitingLogin && SessionData_Web.KeepLoggedIn; } }
/// <summary>Maximum length allowed for password entry: sign-up, change password etc.</summary>
public int PasswordMaxLength { get; } = 32;
//-------------------------------------------------------------------
/// <summary>
/// Form control value max length - Security tables.
/// <para>In many cases the database columns allow plenty of extra space for the value, in case they need to be encrypted.</para>
/// <para>Hence, several cases where the value entered in the control isn't allowed the full database length.</para>
/// </summary>
public int GetControlMaxLength(SaSecurityDbDfn.ColumnIdx column)
{
int maxLength = (int)SaSecurityDbDfn.GetColTextLen(column);
switch (column)
{
case SaSecurityDbDfn.ColumnIdx.SaSecItem_EmailAddress:
return Math.Min(maxLength, 100);
}
return maxLength;
}
/// <summary>
/// Form control value max length - code type tables.
/// <para>In many cases the database columns allow plenty of extra space for the value, in case they need to be encrypted.</para>
/// <para>Hence, several cases where the value entered in the control isn't allowed the full database length.</para>
/// </summary>
public int GetControlMaxLength(SaCodeTypeDbDfn.ColumnIdx column)
{
int maxLength = (int)SaCodeTypeDbDfn.GetColTextLen(column);
switch (column)
{
case SaCodeTypeDbDfn.ColumnIdx.SaCodeType_Name:
case SaCodeTypeDbDfn.ColumnIdx.SaCode_Description:
return Math.Min(maxLength, 100);
}
return maxLength;
}
/// <summary>
/// Form control value max length - BusinessMgr tables.
/// <para>In many cases the database columns allow plenty of extra space for the value, in case they need to be encrypted.</para>
/// <para>Hence, several cases where the value entered in the control isn't allowed the full database length.</para>
/// </summary>
public int GetControlMaxLength(SbmDbDfn.ColumnIdx column)
{
int maxLength = (int)SbmDbDfn.GetColTextLen(column);
switch (column)
{
case SbmDbDfn.ColumnIdx.Affiliate_Email:
return Math.Min(maxLength, 100);
case SbmDbDfn.ColumnIdx.Affiliate_FirstName:
case SbmDbDfn.ColumnIdx.Affiliate_LastName:
return Math.Min(maxLength, 40);
case SbmDbDfn.ColumnIdx.Affiliate_Name:
return Math.Min(maxLength, 80);
case SbmDbDfn.ColumnIdx.Affiliate_PhoneMain:
case SbmDbDfn.ColumnIdx.Affiliate_PhoneMobile:
case SbmDbDfn.ColumnIdx.Affiliate_PhoneAlt:
return Math.Min(maxLength, 20);
case SbmDbDfn.ColumnIdx.Address_Postcode:
return Math.Min(maxLength, 16);
}
return maxLength;
}
//-------------------------------------------------------------------
// Copyright notice.
public HtmlString CopyrightNotice
{
get
{
int initialYear = 2020;
int currentYear = DateTime.Today.Year;
return Localiser.GetHtmlText("copyright.symbol.year.html", (initialYear != currentYear ? initialYear + "-" : "") + currentYear.ToString());
}
}
//-------------------------------------------------------------------
// Display data - Dates.
public String FormatDateForDisplay(DateTime? value)
{
return DateUtil.Format(value, "d-MMM-yy", SessionCulture);
}
public String FormatDateTimeForDisplay(DateTime? value)
{
return StringUtil.TrimAll(DateUtil.Format(value, "d-MMM-yy HH:mm", SessionCulture));
}
//-------------------------------------------------------------------
// Form data - Dates.
// Format may depend on locale, but as long as these methods are used consistently,
// there should be consistent formatting, display and parsing of date values.
public String CalendarDateFormat
{
get
{
// JQuery-UI DatePicker date format.
// Format:
// d Single digit day of month
// dd Double digit day of month
// D 3 letter day of week abbreviation.
// DD Day of week.
// m Single digit month.
// mm Double digit month.
// M 3 letter month abbreviation.
// MM Month name.
// yy 4 digit year.
// Examples:
// dd/mm/yy 07/04/2012
// dd-M-yy 07-Apr-2012
// d-M-yy 7-Apr-2012
return "dd-M-yy";
}
}
public String FormatDateForForm(DateTime value)
{
return DateUtil.Format(value, "dd-MMM-yyyy");
}
public DateTime ParseDateFromForm(String value)
{
return DateUtil.ParseExact(value, "dd-MMM-yyyy");
}
//-------------------------------------------------------------------
// Link URLs.
public class LinksData
{
public String Facebook { get; } = "https://www.facebook.com/BusinessMgr/";
public String LinkedIn { get; } = "https://www.linkedin.com/company/BusinessMgr/about/";
}
public LinksData Links { get; } = new LinksData();
//-------------------------------------------------------------------
// Form data.
// NB Use methods rathe rthan accessors for properties that depend on session data
// to avoid the model binding logic invoking a getter before it's ready for use.
/// <summary>List of Code Types for dropdown.</summary>
public IEnumerable<SelectListItem> GetCodeTypesForSelect()
{
List<CodeDesc> codeTypes = new List<CodeDesc>();
SaCodeType.Params prmCodeType = new SaCodeType.Params();
prmCodeType.SortColumns.Add(CodeTypeConsole.GetCodeTypeColumnDfn(SaCodeType.Column.Name));
foreach (SaCodeType codeType in CodeTypeConsole.GetCodeTypeList(prmCodeType))
codeTypes.Add(codeType.Code, codeType.Name);
return new SelectList(codeTypes, "Code", "Desc");
}
/// <summary>List of Code Type Codes for dropdown.</summary>
public IEnumerable<SelectListItem> GetCodeTypeCodesForSelect(Sbm.ApplCodeType codeType, bool activeOnly = true)
{
List<CodeDesc> codes = new List<CodeDesc>();
foreach (SaCode code in CodeTypeConsole.GetCodesForCodeType(SbmSystem.GetCodeTypeCode(codeType), activeOnly))
codes.Add(code.Code, code.Description);
return new SelectList(codes, "Code", "Desc");
}
//-------------------------------------------------------------------
// Form properties.
/// <summary>
/// User session index to identify the site resources, eg. database.
/// <para>Equivalent to the Site index on the login form.</para>
/// <para>Populates value on logout form to identify default site for next login.</para>
/// </summary>
public ushort SessionIdx { get { return SessionContext?.Index ?? 0; } }
public String FormAction { get; set; }
//-------------------------------------------------------------------
}
//=======================================================================
}