Files
Saxonite/SaxoniteCoreWebAdmin/Admin/Models/Model_Organisation.cs
T
2021-07-02 09:28:37 +01:00

154 lines
6.2 KiB
C#

using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
using Saxonite.Base.App;
using Saxonite.Net.Core.Web.Base.App;
using Saxonite.Net.Core.Web.Base.Controllers;
using Saxonite.Net.Core.Web.Base.Models;
using Saxonite.Net.Standard.Base.App;
using Saxonite.Net.Standard.Base.Bus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Saxonite.Net.Core.Web.Admin.Models
{
//=======================================================================
public class Model_Organisation : SaxModel_Admin
{
//-------------------------------------------------------------------
// Construction.
public Model_Organisation(SaxLocaliser localiser) :
base(localiser)
{
AdditionalFiles_JS.Add("js/sax-organisation.js");
}
//-------------------------------------------------------------------
// List configuration.
public class FilterData
{
public String Organisation { get; set; }
public String OrganisationTypeCode { get; set; }
public String OrganisationCategoryCode { get; set; }
}
public FilterData Filter { get; } = new FilterData();
public Sax.OrganisationType FilterOrganisationType
{
get { return (Sax.OrganisationType)OrganisationTypeCodes.GetTypeForCode(Filter.OrganisationTypeCode); }
}
public Sax.OrganisationCategory FilterOrganisationCategory
{
get { return (Sax.OrganisationCategory)OrganisationCategoryCodes.GetTypeForCode(Filter.OrganisationCategoryCode); }
}
//-------------------------------------------------------------------
// Organisation properties.
public String OrganisationId { get; set; }
public String OrganisationName { get; set; }
public String OrganisationTypeValue { get; set; }
public Sax.OrganisationType OrganisationType { get { return (Sax.OrganisationType)OrganisationTypeCodes.GetTypeForCode(OrganisationTypeValue); } }
public String OrganisationCategoryValue { get; set; }
public Sax.OrganisationCategory OrganisationCategory { get { return (Sax.OrganisationCategory)OrganisationCategoryCodes.GetTypeForCode(OrganisationCategoryValue); } }
public String OrganisationSportId { get; set; }
public String OrganisationRegionIdCountry { get; set; }
// Common properties.
public String OrganisationNotes { get; set; }
public String OrganisationStatusCode { get; set; }
public SaStatus OrganisationStatus { get { return SacsSystem.GetStatusForCode(OrganisationStatusCode); } }
// Utilities.
public bool IsSportMandatory()
{
if (OrganisationCategory == Sax.OrganisationCategory.Club) return true;
if (OrganisationCategory == Sax.OrganisationCategory.Association) return true;
return false;
}
//-------------------------------------------------------------------
// Validation.
//-------------------------------------------------------------------
// Organisation validation.
public bool ValidateOrganisationData(SaxController_Base.AjaxResponse_Data responseData)
{
// Organisation name.
if (OrganisationName != null)
{
responseData.SetControlValue(nameof(OrganisationName), OrganisationName);
if (StringUtil.IsEmpty(OrganisationName))
responseData.SetControlError(nameof(OrganisationName), Localiser["msg.validation.mandatory.value"]);
else if (StringUtil.GetLength(OrganisationName) > GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Name))
responseData.SetControlError(nameof(OrganisationName), Localiser["msg.validation.maximum.length", GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Name)]);
}
// Organisation type.
if (OrganisationTypeValue != null)
{
if (StringUtil.IsEmpty(OrganisationTypeValue))
responseData.SetControlError(nameof(OrganisationTypeValue), Localiser["msg.validation.mandatory.value"]);
}
// Organisation category.
if (OrganisationCategoryValue != null)
{
if (StringUtil.IsEmpty(OrganisationCategoryValue))
responseData.SetControlError(nameof(OrganisationCategoryValue), Localiser["msg.validation.mandatory.value"]);
}
// Organisation sport.
if (OrganisationSportId != null)
{
if (SysId.IsValid(OrganisationSportId.ToSysId()) || IsSportMandatory())
{
using (SaxSport sport = ReferenceMgr.GetSportById(OrganisationSportId.ToSysId()))
{
if (sport == null)
responseData.SetControlError(nameof(OrganisationSportId), Localiser["msg.validation.invalid.value"]);
}
}
}
// Organisation country.
if (OrganisationRegionIdCountry != null)
{
using (SaxRegion region = ReferenceMgr.GetRegionById(OrganisationRegionIdCountry.ToSysId()))
{
if (region == null)
responseData.SetControlError(nameof(OrganisationRegionIdCountry), Localiser["msg.validation.invalid.value"]);
else if (region.RegionType != Sax.RegionType.Country)
responseData.SetControlError(nameof(OrganisationRegionIdCountry), "Organisation region must be a country");
}
if (!StringUtil.IsEmpty(OrganisationName))
{
// Name must be unqiue within region.
SaxOrganisation.Params prmOrganisation = new SaxOrganisation.Params();
if (SysId.IsValid(OrganisationId.ToSysId()))
prmOrganisation.IdsExcluded.Add(OrganisationId.ToSysId());
prmOrganisation.NameFull = OrganisationName;
prmOrganisation.RegionIdsIncluded.Add(OrganisationRegionIdCountry.ToSysId());
prmOrganisation.StatusCat = SaxOrganisation.StatusCat.Any;
if (AccountMgr.GetOrganisationCount(prmOrganisation) > 0)
responseData.SetControlError(nameof(OrganisationName), Localiser["msg.validation.duplicate.value"]);
}
}
// Organisation status.
if (OrganisationStatusCode != null)
{
if (StringUtil.IsEmpty(OrganisationStatusCode))
responseData.SetControlError(nameof(OrganisationStatus), Localiser["msg.validation.mandatory.value"]);
else if (!SaxOrganisation.ValidStatuses.Contains(OrganisationStatus))
responseData.SetControlError(nameof(OrganisationStatus), Localiser["msg.validation.invalid.value"]);
}
return !responseData.HasControlErrors();
}
//-------------------------------------------------------------------
}
//=======================================================================
}