using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.Rendering; using Sarand.BusinessMgr.Net.Core.Web.App; using Sarand.BusinessMgr.Net.Standard.Base.App; using Sarand.BusinessMgr.Net.Standard.Base.Bus; using Sarand.BusinessMgr.Net.Standard.Base.Mgr; 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.Text; namespace Sarand.BusinessMgr.Net.Core.Web.Models { //======================================================================= public class SbmModel_Base { //------------------------------------------------------------------- // Data members. private SbmInternals internals; private readonly List infoMessages = new List(); private readonly List warningMessages = new List(); private readonly List errorMessages = new List(); //------------------------------------------------------------------- // Protected construction. protected SbmModel_Base(SbmLocaliser localiser) { Localiser = localiser; } //------------------------------------------------------------------- // Application properties. private SbmApplication_Web App { get { return SbmGlobals_Web.Application; } } protected 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 DbUpdateInfo DbUpdateInfo { get { return SessionData?.DbUpdateInfo ?? new DbUpdateInfo(null, "", SbmSystem.ComputerName); } } public bool DebugMode { get { return SbmSystem.DebugMode; } } public SbmLocaliser Localiser { get; } //------------------------------------------------------------------- // Page configuration. public bool OK { get { return !HasMessages(); } } public virtual bool ProhibitCache { get; set; } = true; public String PageCaption { get; set; } public String InstanceVersion { get { return !StringUtil.IsEmpty(App.AppConfig.Instance) ? App.AppConfig.Instance + " v" + Sbm.Version.GetFullVersion() : "v" + Sbm.Version.GetAppVersion(); } } public bool IncludeGoogleAnalytics { get; set; } = false; //------------------------------------------------------------------- // Business components. // NB Keep these accessors protected to avoid them being called by teh model binding logic before the session data is ready. protected SbmInternals Internals { get { if (internals == null) internals = new SbmInternals(SessionData_Web.UserContext); return internals; } } // 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; } } // Consoles. protected SbmConfigConsole ConfigConsole { get { return Internals.ConfigConsole; } } 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 GetCodesOfType(Sbm.SystemCode systemCode) { return Internals.GetCodesOfType(systemCode); } //------------------------------------------------------------------- // Security. public bool ValidUserSession { get { return SessionData?.ValidUserSession ?? false; } } public bool AwaitingLogin { get { return !ValidUserSession; } } public bool KeepLoggedIn { get { return !AwaitingLogin && SessionData.KeepLoggedIn; } } /// Maximum length allowed for password entry: sign-up, change password etc. public int PasswordMaxLength { get; } = 32; //------------------------------------------------------------------- /// /// Form control value max length. /// In many cases the database columns allow plenty of extra space for the value, in case they need to be encrypted. /// Hence, several cases where the value entered in the control isn't allowed the full database length. /// public int GetControlMaxLength(SbmDbDfn.ColumnIdx column) { int maxLength = (int)SbmDbDfn.GetColTextLen(column); switch (column) { case SbmDbDfn.ColumnIdx.Affiliate_Email: return Math.Min(maxLength, 60); 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()); } } //------------------------------------------------------------------- // 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. /// List of Address Types for dropdown. public IEnumerable GetAddressTypeCodesForSelect() { return new SelectList(GetCodesOfType(Sbm.SystemCode.AddressType), "Code", "Desc"); } /// List of Affiliate Types for dropdown. public IEnumerable GetAffiliateTypeCodesForSelect() { return new SelectList(GetCodesOfType(Sbm.SystemCode.AffiliateType), "Code", "Desc"); } /// List of Association Types for dropdown. public IEnumerable GetAssociationTypeCodesForSelect() { return new SelectList(GetCodesOfType(Sbm.SystemCode.AssociationType), "Code", "Desc"); } /// List of Commodity Types for dropdown. public IEnumerable GetCommodityTypeCodesForSelect() { return new SelectList(GetCodesOfType(Sbm.SystemCode.CommodityType), "Code", "Desc"); } //------------------------------------------------------------------- // Version parameter for includes. public String HrefVersion { get { DateTime versionDate = SbmSystem.AssemblyBuildAt; if (!DateUtil.IsValid(versionDate) || SbmSystem.DebugMode) versionDate = DateTime.Now; return DateUtil.Format(versionDate, "yyyyMMdd" /*+ (SacsSystem.Debug ? "HHmm" : "")*/); } } // Additional includes. public List AdditionalFiles_CSS { get; set; } = new List(); public List AdditionalFiles_JS { get; set; } = new List(); //------------------------------------------------------------------- // Redirection locations. public class RedirectLocationData { // Properties populated in Controller_Base.OnActionExecuted. public String Base { get; internal set; } public String Home { get; internal set; } public String Home_SessionExpired { get; internal set; } } public RedirectLocationData RedirectLocation { get; } = new RedirectLocationData(); //------------------------------------------------------------------- // Paging properties. public class DataPaging { private DbQuery.Paging paging { get; } = new DbQuery.Paging(); public void PopulatePagingTo(DbQuery.Paging resultsPaging) { resultsPaging.PageLength = paging.PageLength; resultsPaging.PageNo = paging.PageNo; } public bool IsPaged { get { return paging.Paged; } } public uint PageNo { get { return paging.PageNo; } set { paging.PageNo = value; } } public ushort PageLength { get { return paging.PageLength; } set { paging.PageLength = value; } } public uint PageCount { get { return (PageLength > 0 ? (uint)Math.Ceiling((decimal)ItemTotal / PageLength) : 0); } } public ushort ItemCount { get; set; } private ulong itemTotal = 0; public ulong ItemTotal { get { return itemTotal; } set { itemTotal = value; // Check whether the total number of items is less than the current page and reduce page no if needed. if (IsPaged && (PageNo - 1) * PageLength > itemTotal) PageNo = (uint)((itemTotal - 1) / PageLength + 1); } } } public DataPaging Paging { get; } = new DataPaging(); public void PopulatePagingTo(DbQuery.Paging resultsPaging) { Paging.PopulatePagingTo(resultsPaging); } //------------------------------------------------------------------- // Form properties. /// /// User session index to identify the site resources, eg. database. /// Equivalent to the Site index on the login form. /// Populates value on logout form to identify default site for next login. /// public ushort SessionIdx { get { return SessionContext.Index; } } public String FormAction { get; set; } //------------------------------------------------------------------- // Messages. //------------------------------------------------------------------- public bool HasMessages() { return infoMessages.Count > 0 || warningMessages.Count > 0 || errorMessages.Count > 0; } public List InfoMessages { get { return new List(infoMessages); } } public List WarningMessages { get { return new List(warningMessages); } } public List ErrorMessages { get { return new List(errorMessages); } } //------------------------------------------------------------------- // Form messages. public void AddInfoMessage(String message) { if (StringUtil.IsEmpty(StringUtil.TrimAll(message))) return; infoMessages.Add(message); } public void AddWarningMessage(String message) { if (StringUtil.IsEmpty(StringUtil.TrimAll(message))) return; warningMessages.Add(message); } public void AddErrorMessage(String message) { if (StringUtil.IsEmpty(StringUtil.TrimAll(message))) return; errorMessages.Add(message); } //------------------------------------------------------------------- } //======================================================================= }