154 lines
6.0 KiB
C#
154 lines
6.0 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_Region : SaxModel_Admin
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public Model_Region(SaxLocaliser localiser) :
|
|
base(localiser)
|
|
{
|
|
AdditionalFiles_JS.Add("js/sax-region.js");
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// List properties.
|
|
public class FilterData
|
|
{
|
|
public String Region { get; set; }
|
|
public String RegionTypeCode { get; set; }
|
|
}
|
|
public FilterData Filter { get; } = new FilterData();
|
|
public Sax.RegionType FilterRegionType
|
|
{
|
|
get { return (Sax.RegionType)RegionTypeCodes.GetTypeForCode(Filter.RegionTypeCode); }
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Region properties.
|
|
public String RegionId { get; set; }
|
|
public String RegionTypeCode { get; set; }
|
|
public Sax.RegionType RegionType
|
|
{
|
|
get { return (Sax.RegionType)RegionTypeCodes.GetTypeForCode(RegionTypeCode); }
|
|
set { RegionTypeCode = RegionTypeCodes.GetCodeForType(value); }
|
|
}
|
|
public String RegionCountryIso2Code { get; set; }
|
|
public String RegionName { get; set; }
|
|
public String RegionIdParent { get; set; }
|
|
public String RegionStatusCode { get; set; }
|
|
public SaStatus RegionStatus { get { return SacsSystem.GetStatusForCode(RegionStatusCode); } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Validation.
|
|
//-------------------------------------------------------------------
|
|
// Region.
|
|
public bool ValidateRegionData(SaxController_Base.AjaxResponse_Data responseData)
|
|
{
|
|
// Region Type.
|
|
if (RegionTypeCode != null)
|
|
{
|
|
responseData.SetControlValue(nameof(RegionTypeCode), RegionTypeCode);
|
|
if (StringUtil.IsEmpty(RegionTypeCode) || (int)RegionType < 1)
|
|
responseData.SetControlError(nameof(RegionTypeCode), Localiser["msg.validation.mandatory.value"]);
|
|
}
|
|
|
|
// Region ISO2 code.
|
|
if (RegionCountryIso2Code != null)
|
|
{
|
|
RegionCountryIso2Code = RegionCountryIso2Code.ToUpperInvariant();
|
|
responseData.SetControlValue(nameof(RegionCountryIso2Code), RegionCountryIso2Code);
|
|
if (RegionType == Sax.RegionType.Country)
|
|
{
|
|
if (StringUtil.IsEmpty(RegionCountryIso2Code))
|
|
responseData.SetControlError(nameof(RegionCountryIso2Code), Localiser["msg.validation.mandatory.value"]);
|
|
else if (StringUtil.GetLength(RegionCountryIso2Code) != GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Region_CountryIso2Code))
|
|
responseData.SetControlError(nameof(RegionCountryIso2Code), Localiser["msg.validation.invalid.length", GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Region_CountryIso2Code)]);
|
|
else
|
|
{
|
|
// Check for duplicate code.
|
|
SaxRegion.Params prmRegion = new SaxRegion.Params();
|
|
SysId regionId = RegionId.ToSysId();
|
|
if (SysId.IsValid(regionId))
|
|
prmRegion.IdsExcluded.Add(regionId);
|
|
prmRegion.CountryIso2CodesIncluded.Add(RegionCountryIso2Code);
|
|
prmRegion.StatusCat = SaxRegion.StatusCat.Any;
|
|
if (ReferenceMgr.GetRegionCount(prmRegion) > 0)
|
|
responseData.SetControlError(nameof(RegionCountryIso2Code), Localiser["msg.validation.duplicate.value"]);
|
|
}
|
|
}
|
|
else if ((int)RegionType > 0 && RegionType != Sax.RegionType.Country)
|
|
{
|
|
if (!StringUtil.IsEmpty(RegionCountryIso2Code))
|
|
responseData.SetControlError(nameof(RegionCountryIso2Code), Localiser["msg.validation.invalid.value"]);
|
|
}
|
|
}
|
|
|
|
// Region name.
|
|
if (RegionName != null)
|
|
{
|
|
responseData.SetControlValue(nameof(RegionName), RegionName);
|
|
if (StringUtil.IsEmpty(RegionName))
|
|
responseData.SetControlError(nameof(RegionName), Localiser["msg.validation.mandatory.value"]);
|
|
else if (StringUtil.GetLength(RegionName) > GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Region_Name))
|
|
responseData.SetControlError(nameof(RegionName), Localiser["msg.validation.maximum.length", GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Region_Name)]);
|
|
else
|
|
{
|
|
// Check for duplicate name.
|
|
SaxRegion.Params prmRegion = new SaxRegion.Params();
|
|
SysId regionId = RegionId.ToSysId();
|
|
if (SysId.IsValid(regionId))
|
|
prmRegion.IdsExcluded.Add(regionId);
|
|
prmRegion.NameFull = RegionName;
|
|
prmRegion.StatusCat = SaxRegion.StatusCat.Any;
|
|
if (ReferenceMgr.GetRegionCount(prmRegion) > 0)
|
|
responseData.SetControlError(nameof(RegionName), Localiser["msg.validation.duplicate.value"]);
|
|
}
|
|
}
|
|
|
|
// Parent region.
|
|
if (!StringUtil.IsEmpty(RegionIdParent))
|
|
{
|
|
SaxRegion parentRegion = ReferenceMgr.GetRegionById(RegionIdParent.ToSysId());
|
|
if (parentRegion != null)
|
|
{
|
|
if (RegionType<= 0 || SaxRegion.IsValidParentRegionType(RegionType, parentRegion.RegionType))
|
|
{
|
|
}
|
|
else responseData.SetControlError(nameof(RegionIdParent), Localiser["region.msg.parent.type.invalid"]);
|
|
}
|
|
else responseData.SetControlError(nameof(RegionIdParent), Localiser["msg.validation.invalid.value"]);
|
|
}
|
|
|
|
// Region status.
|
|
if (RegionStatusCode != null)
|
|
{
|
|
if (StringUtil.IsEmpty(RegionStatusCode))
|
|
responseData.SetControlError(nameof(RegionStatus), Localiser["msg.validation.mandatory.value"]);
|
|
else if (!SaxRegion.ValidStatuses.Contains(RegionStatus))
|
|
responseData.SetControlError(nameof(RegionStatus), Localiser["msg.validation.invalid.value"]);
|
|
}
|
|
|
|
return !responseData.HasControlErrors();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|