using Sarand.BusinessMgr.Net.Standard.Base.App;
using Sarand.BusinessMgr.Net.Standard.Base.Core;
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 System;
using System.Collections.Generic;
namespace Sarand.BusinessMgr.Net.Standard.Base.Bus
{
//=======================================================================
public abstract class SbmAffiliate : SbmHierarchyItem
{
//-------------------------------------------------------------------
// Columns.
public enum Column
{
Hidden,
Pos,
Id,
// Affiliate.
Type,
Type_Code,
Type_Desc,
Name,
Abbrev,
Title,
Initials,
LastName,
FirstName,
Salutation,
Nature,
CompanyNo,
VatReg,
Contact,
PhoneMain,
PhoneDirect,
PhoneAlt,
PhoneMobile,
PhoneFax,
Email,
Website,
// Address.
Address_Summary,
Address_Line1,
Address_Line2,
Address_Line3,
Address_City,
Address_Province,
Address_Postcode,
Address_Country,
ProspectGroup,
// Associated data.
GroupMembers,
AssociationOwner_Count,
AssociationTarget_Count,
// Common properties.
Notes,
Flag,
Status,
Status_Ind,
Status_Desc,
CreateOn_Date,
UpdateOn_Date,
}
//-------------------------------------------------------------------
/// Properties other than common.
public class DataProperties
{
public Sbm.AffiliateType AffiliateType { get; set; } = Sbm.AffiliateType.Unknown;
public String PhoneMain { get; set; } = "";
public String PhoneAlt { get; set; } = "";
public String PhoneFax { get; set; } = "";
public String Email { get; set; } = "";
public String Website { get; set; } = "";
// Ogranisation.
public SysId AffiliateIdParent { get; set; } = new SysId();
public String Name { get; set; } = "";
public String Abbr { get; set; } = "";
public String Nature { get; set; } = "";
public String Contact { get; set; } = "";
public String CompanyNo { get; set; } = "";
public String VatReg { get; set; } = "";
public String PhoneDirect { get; set; } = "";
// Individual.
public String Title { get; set; } = "";
public String Initials { get; set; } = "";
public String LastName { get; set; } = "";
public String FirstName { get; set; } = "";
public String Salutation { get; set; } = "";
public String PhoneMobile { get; set; } = "";
public byte BirthDay { get; set; } = 0;
public byte BirthMonth { get; set; } = 0;
public short BirthYear { get; set; } = 0;
public String MaritalStatus { get; set; } = "";
public String Spouse { get; set; } = "";
public String Children { get; set; } = "";
public String Hometown { get; set; } = "";
public SysId UserId { get; set; } = new SysId();
}
//-------------------------------------------------------------------
// Data.
public new class Data : SacsHierarchyItem.Data
{
// Data members.
private readonly DataProperties data = new DataProperties();
// Construction.
public Data()
{
//Status = SbmAccountMgr.AffiliateStatusDefault;
}
public Sbm.AffiliateType AffiliateType
{
get { return data.AffiliateType; }
set { CheckChanged((int)data.AffiliateType, (int)value); data.AffiliateType = value; }
}
public String PhoneMain
{
get { return data.PhoneMain; }
set { CheckChanged(data.PhoneMain, value); data.PhoneMain = value; }
}
public String PhoneAlt
{
get { return data.PhoneAlt; }
set { CheckChanged(data.PhoneAlt, value); data.PhoneAlt = value; }
}
public String PhoneFax
{
get { return data.PhoneFax; }
set { CheckChanged(data.PhoneFax, value); data.PhoneFax = value; }
}
public String Email
{
get { return data.Email; }
set { CheckChanged(data.Email, value); data.Email = value; }
}
public String Website
{
get { return data.Website; }
set { CheckChanged(data.Website, value); data.Website = value; }
}
// Ogranisation.
public SysId AffiliateIdParent
{
get { return data.AffiliateIdParent; }
set { CheckChanged(data.AffiliateIdParent, value); data.AffiliateIdParent = value; }
}
public String Name
{
get { return data.Name; }
set { CheckChanged(data.Name, value); data.Name = value; }
}
public String Abbr
{
get { return data.Abbr; }
set { CheckChanged(data.Abbr, value); data.Abbr = value; }
}
public String Nature
{
get { return data.Nature; }
set { CheckChanged(data.Nature, value); data.Nature = value; }
}
public String Contact
{
get { return data.Contact; }
set { CheckChanged(data.Contact, value); data.Contact = value; }
}
public String CompanyNo
{
get { return data.CompanyNo; }
set { CheckChanged(data.CompanyNo, value); data.CompanyNo = value; }
}
public String VatReg
{
get { return data.VatReg; }
set { CheckChanged(data.VatReg, value); data.VatReg = value; }
}
public String PhoneDirect
{
get { return data.PhoneDirect; }
set { CheckChanged(data.PhoneDirect, value); data.PhoneDirect = value; }
}
// Individual.
public String Title
{
get { return data.Title; }
set { CheckChanged(data.Title, value); data.Title = value; }
}
public String Initials
{
get { return data.Initials; }
set { CheckChanged(data.Initials, value); data.Initials = value; }
}
public String LastName
{
get { return data.LastName; }
set { CheckChanged(data.LastName, value); data.LastName = value; }
}
public String FirstName
{
get { return data.FirstName; }
set { CheckChanged(data.FirstName, value); data.FirstName = value; }
}
public String Salutation
{
get { return data.Salutation; }
set { CheckChanged(data.Salutation, value); data.Salutation = value; }
}
public String PhoneMobile
{
get { return data.PhoneMobile; }
set { CheckChanged(data.PhoneMobile, value); data.PhoneMobile = value; }
}
public byte BirthDay
{
get { return data.BirthDay; }
set { CheckChanged(data.BirthDay, value); data.BirthDay = value; }
}
public byte BirthMonth
{
get { return data.BirthMonth; }
set { CheckChanged(data.BirthMonth, value); data.BirthMonth = value; }
}
public short BirthYear
{
get { return data.BirthYear; }
set { CheckChanged(data.BirthYear, value); data.BirthYear = value; }
}
public String MaritalStatus
{
get { return data.MaritalStatus; }
set { CheckChanged(data.MaritalStatus, value); data.MaritalStatus = value; }
}
public String Spouse
{
get { return data.Spouse; }
set { CheckChanged(data.Spouse, value); data.Spouse = value; }
}
public String Children
{
get { return data.Children; }
set { CheckChanged(data.Children, value); data.Children = value; }
}
public String Hometown
{
get { return data.Hometown; }
set { CheckChanged(data.Hometown, value); data.Hometown = value; }
}
public SysId UserId
{
get { return data.UserId; }
set { CheckChanged(data.UserId, value); data.UserId = value; }
}
internal void PopulateFrom(Data data)
{
AffiliateType = data.AffiliateType;
PhoneMain = data.PhoneMain;
PhoneAlt = data.PhoneAlt;
PhoneFax = data.PhoneFax;
Email = data.Email;
Website = data.Website;
// Ogranisation
AffiliateIdParent = data.AffiliateIdParent;
Name = data.Name;
Abbr = data.Abbr;
Nature = data.Nature;
Contact = data.Contact;
CompanyNo = data.CompanyNo;
VatReg = data.VatReg;
PhoneDirect = data.PhoneDirect;
// Individual.
Title = data.Title;
Initials = data.Initials;
LastName = data.LastName;
FirstName = data.FirstName;
Salutation = data.Salutation;
PhoneMobile = data.PhoneMobile;
BirthDay = data.BirthDay;
BirthMonth = data.BirthMonth;
BirthYear = data.BirthYear;
MaritalStatus = data.MaritalStatus;
Spouse = data.Spouse;
Children = data.Children;
Hometown = data.Hometown;
UserId = data.UserId;
base.PopulateFrom(data);
}
public Data Clone()
{
Data data = new Data();
data.PopulateFrom(this);
return data;
}
}
public new class ErrField : SacsHierarchyItem.ErrField
{
// Identifies the field that has invalid data.
public ValidValue Type { get; set; } = ValidValue.Valid;
public ValidValue PhoneMain { get; set; } = ValidValue.Valid;
public ValidValue PhoneAlt { get; set; } = ValidValue.Valid;
public ValidValue PhoneFax { get; set; } = ValidValue.Valid;
public ValidValue Email { get; set; } = ValidValue.Valid;
public ValidValue Website { get; set; } = ValidValue.Valid;
public ValidValue AffiliateIdParent { get; set; } = ValidValue.Valid;
public ValidValue Name { get; set; } = ValidValue.Valid;
public ValidValue Abbr { get; set; } = ValidValue.Valid;
public ValidValue Nature { get; set; } = ValidValue.Valid;
public ValidValue Contact { get; set; } = ValidValue.Valid;
public ValidValue CompanyNo { get; set; } = ValidValue.Valid;
public ValidValue VatReg { get; set; } = ValidValue.Valid;
public ValidValue PhoneDirect { get; set; } = ValidValue.Valid;
public ValidValue Title { get; set; } = ValidValue.Valid;
public ValidValue Initials { get; set; } = ValidValue.Valid;
public ValidValue LastName { get; set; } = ValidValue.Valid;
public ValidValue FirstName { get; set; } = ValidValue.Valid;
public ValidValue Salutation { get; set; } = ValidValue.Valid;
public ValidValue PhoneMobile { get; set; } = ValidValue.Valid;
public ValidValue BirthDay { get; set; } = ValidValue.Valid;
public ValidValue BirthMonth { get; set; } = ValidValue.Valid;
public ValidValue BirthYear { get; set; } = ValidValue.Valid;
public ValidValue MaritalStatus { get; set; } = ValidValue.Valid;
public ValidValue Spouse { get; set; } = ValidValue.Valid;
public ValidValue Children { get; set; } = ValidValue.Valid;
public ValidValue Hometown { get; set; } = ValidValue.Valid;
public ValidValue UserId { get; set; } = ValidValue.Valid;
}
//-------------------------------------------------------------------
// Data members.
private readonly Data data = new Data();
public static readonly Byte Flag_Prospect = 0x01;
public static readonly Byte Flag_KeepLoggedIn_Portal = 0x02;
//-------------------------------------------------------------------
// Construction.
protected SbmAffiliate(SysId id, Sbm.AffiliateType affiliateType) : base(id)
{
base.SetData(data);
AffiliateType = affiliateType;
Status = DefaultStatus;
}
//-------------------------------------------------------------------
// Affiliate status.
private static readonly List affiliateStatuses = new List()
{
SaStatus.Current,
SaStatus.OnHold,
//SaStatus.Inactive,
//SaStatus.Cancelled,
SaStatus.Disabled,
};
public static SaStatus DefaultStatus { get { return SaStatus.Current; } }
public static List ValidStatuses { get { return new List(affiliateStatuses); } }
public static DataValue.TriStateType GetStatusTriState(SaStatus status)
{
switch (status)
{
case SaStatus.Active:
/**/
case SaStatus.Current:
return DataValue.TriStateType.Green;
case SaStatus.OnHold:
return DataValue.TriStateType.Amber;
case SaStatus.Inactive:
case SaStatus.Cancelled:
case SaStatus.Disabled:
return DataValue.TriStateType.Red;
case SaStatus.Deleted:
return DataValue.TriStateType.Purple;
}
return DataValue.TriStateType.Unknown;
}
//-------------------------------------------------------------------
// Utilities.
public SbmItemId GetItemId()
{
return new SbmItemId(Sbm.ItemType.Affiliate, Id);
}
// Affiliate category.
public static Sbm.AffiliateCategory GetCategory(Sbm.AffiliateType AffiliateType)
{
if (IsIndividual(AffiliateType))
return Sbm.AffiliateCategory.Individual;
return Sbm.AffiliateCategory.Organisation;
}
public Sbm.AffiliateCategory GetCategory()
{
return GetCategory(AffiliateType);
}
// Affiliate type.
public bool TypeIsValid()
{
if (IsIndividual()) return true;
if (IsCommercial()) return true;
if (IsSociety()) return true;
if (IsAcademic()) return true;
if (IsPublicSector()) return true;
if (IsCharity()) return true;
if (IsContactGroup()) return true;
return false;
}
public static bool IsIndividual(Sbm.AffiliateType affiliateType)
{
return affiliateType == Sbm.AffiliateType.Individual;
}
public bool IsIndividual()
{
return IsIndividual(AffiliateType);
}
public static bool IsOrganisation(Sbm.AffiliateType affiliateType)
{
return !IsIndividual(affiliateType) & !IsContactGroup(affiliateType);
}
public bool IsOrganisation()
{
return IsOrganisation(AffiliateType);
}
// Specific affiliate types.
public bool IsCommercial()
{
return AffiliateType == Sbm.AffiliateType.Commercial;
}
public bool IsSociety()
{
return AffiliateType == Sbm.AffiliateType.Society;
}
public bool IsAcademic()
{
return AffiliateType == Sbm.AffiliateType.Academic;
}
public bool IsPublicSector()
{
return AffiliateType == Sbm.AffiliateType.PublicSector;
}
public bool IsCharity()
{
return AffiliateType == Sbm.AffiliateType.Charity;
}
public static bool IsContactGroup(Sbm.AffiliateType affiliateType)
{
return affiliateType == Sbm.AffiliateType.ContactGroup;
}
public bool IsContactGroup()
{
return IsContactGroup(AffiliateType);
}
/// Most significant phone number.
public String GetPhone()
{
return StringUtil.OnBlank(PhoneMain, PhoneAlt, PhoneMobile, PhoneDirect);
}
// Email validation.
public static bool IsValidEmail(String email)
{
int count = 0;
for (int i = 0; i < email.Length; i++)
{
char c = email[i];
if (c >= 'a' && c <= 'z') continue;
if (c >= 'A' && c <= 'Z') continue;
if (c >= '0' && c <= '9') continue;
if (c == '@')
{
count++;
if (count > 1) return false;
continue;
}
if (c == '.') continue;
if (c == '-') continue;
if (c == '_') continue;
// These 'appear' to be valid characters in the email:
if (c == '+') continue;
if (c == '=') continue;
if (c == '!') continue;
if (c == '#') continue;
return false;
}
return true;
}
//-------------------------------------------------------------------
// Properties.
public Data GetData() { return data.Clone(); }
public void SetData(Data data)
{
// Properties that cannot be changed if previously specified.
if (SysId.IsValid(Id) && SysId.ToInt(Id) != SysId.ToInt(data.Id))
throw new NotSupportedException($"{SysUtil.Fn} Id cannot be changed");
// Populate individual properties to ensure that 'Changed' indicator is kept up to date.
this.data.PopulateFrom(data);
}
//-------------------------------------------------------------------
// Properties.
public Sbm.AffiliateType AffiliateType
{
get { return data.AffiliateType; }
set { data.AffiliateType = value; }
}
public void SetType(Sbm.AffiliateType AffiliateType)
{
if (this.AffiliateType == Sbm.AffiliateType.Individual && this.AffiliateType != AffiliateType)
{
SaLogger.Debug("Not allowed to change type from Individual.");
return;
}
if (this.AffiliateType != Sbm.AffiliateType.Individual && AffiliateType == Sbm.AffiliateType.Individual)
{
SaLogger.Debug("Not allowed to change type to Individual.");
return;
}
if ((int)this.AffiliateType < 1)
{
SaLogger.Debug("Invalid type specified.");
return;
}
this.AffiliateType = AffiliateType;
}
public String GetIndividualFullName(String affTitle, String first, String last)
{
// :
List Names = new List();
Names.Add(affTitle);
Names.Add(first);
Names.Add(last);
return Names.Concat(" ", null, null, true, true);
}
public String GetFullName()
{
if (IsIndividual()) return GetIndividualFullName(Title, FirstName, LastName);
return Name;
}
public static String GetIndividualDisplayName(String firstName, String lastName)
{
// , for individuals.
List Names = new List();
Names.Add(lastName);
Names.Add(firstName);
Names.RemoveBlanks();
return Names.Concat(", ");
}
public static String GetDisplayName(Sbm.AffiliateType affiliateType, String name, String firstName, String lastName)
{
if (IsIndividual(affiliateType)) return GetIndividualDisplayName(firstName, lastName);
return name;
}
public String GetDisplayName()
{
return GetDisplayName(AffiliateType, Name, FirstName, LastName);
}
public String PhoneMain
{
get { return data.PhoneMain; }
set { data.PhoneMain = value; }
}
public String PhoneAlt
{
get { return data.PhoneAlt; }
set { data.PhoneAlt = value; }
}
public String PhoneFax
{
get { return data.PhoneFax; }
set { data.PhoneFax = value; }
}
public String Email
{
get { return data.Email; }
set { data.Email = value; }
}
public String Website
{
get { return data.Website; }
set { data.Website = value; }
}
//-------------------------------------------------------------------
public SysId AffiliateIdParent
{
get { return data.AffiliateIdParent; }
set { data.AffiliateIdParent = value; }
}
public String Name
{
get { return data.Name; }
set { data.Name = value; }
}
public String Abbr
{
get { return data.Abbr; }
set { data.Abbr = value; }
}
public String Nature
{
get { return data.Nature; }
set { data.Nature = value; }
}
public String Contact
{
get { return data.Contact; }
set { data.Contact = value; }
}
public String CompanyNo
{
get { return data.CompanyNo; }
set { data.CompanyNo = value; }
}
public String VatReg
{
get { return data.VatReg; }
set { data.VatReg = value; }
}
public String PhoneDirect
{
get { return data.PhoneDirect; }
set { data.PhoneDirect = value; }
}
//-------------------------------------------------------------------
public String Title
{
get { return data.Title; }
set { data.Title = value; }
}
public String Initials
{
get { return data.Initials; }
set { data.Initials = value; }
}
public String LastName
{
get { return data.LastName; }
set { data.LastName = value; }
}
public String FirstName
{
get { return data.FirstName; }
set { data.FirstName = value; }
}
public String Salutation
{
get { return data.Salutation; }
set { data.Salutation = value; }
}
public String PhoneMobile
{
get { return data.PhoneMobile; }
set { data.PhoneMobile = value; }
}
public byte BirthDay
{
get { return data.BirthDay; }
set { data.BirthDay = value; }
}
public byte BirthMonth
{
get { return data.BirthMonth; }
set { data.BirthMonth = value; }
}
public short BirthYear
{
get { return data.BirthYear; }
set { data.BirthYear = value; }
}
public String MaritalStatus
{
get { return data.MaritalStatus; }
set { data.MaritalStatus = value; }
}
public String Spouse
{
get { return data.Spouse; }
set { data.Spouse = value; }
}
public String Children
{
get { return data.Children; }
set { data.Children = value; }
}
public String Hometown
{
get { return data.Hometown; }
set { data.Hometown = value; }
}
public SysId UserId
{
get { return data.UserId; }
set { data.UserId = value; }
}
//-------------------------------------------------------------------
// Flags.
public bool Prospect
{
get { return GetFlag(Flag_Prospect); }
set { SetFlag(Flag_Prospect, value); }
}
public bool KeepLoggedIn_Portal
{
get { return GetFlag(Flag_KeepLoggedIn_Portal); }
set { SetFlag(Flag_KeepLoggedIn_Portal, value); }
}
//-------------------------------------------------------------------
// P a r a m s
//-------------------------------------------------------------------
/// Parameters - used to define the filters that should be applied to a List of Affiliates
// List parameters.
public enum StatusCat
{
Any,
CurrentOnly,
OnHoldOnly,
NotDeleted,
DeletedOnly,
}
///
/// Item list parameters.
///
public new class Params : SacsHierarchyItem.Params
{
// List filtering.
public HashSet IdsIncluded { get; } = new HashSet();
public HashSet IdsExcluded { get; } = new HashSet();
// Affiliate.
public Sbm.AffiliateType AffiliateType { get; set; } = Sbm.AffiliateType.Any;
public SysId AffiliateIdParent { get; set; }
public SysId UserId { get; set; }
/// Generic search value - name, phone, website, address etc.
public String Search { get; set; }
public String Name { get; set; }
public String NamePartial { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
public String PhonePartial { get; set; }
public String EmailPartial { get; set; }
public String PostcodePartial { get; set; }
public String AddressPartial { get; set; }
public String NotesPartial { get; set; }
// Restrict to affiliates with associations with the following.
public SysId AffiliateIdAssocOwner { get; set; }
public SysId AffiliateIdAssocTarget { get; set; }
public SysId AffiliateIdExcludeTargetsOf { get; set; }
public Sbm.AssociationType AssociationType { get; set; } = Sbm.AssociationType.Any;
public Sbm.AssociationType AssociationTypeExclude { get; set; } = Sbm.AssociationType.Any;
public DateTime AssociationEffectiveOn { get; set; } = DateTime.MinValue;
// Current associations. Default: True [only relevant when association filter specified].
public bool CurrentAssociationsOnly { get; set; } = true;
// Supervised associations - in combination with the owner/target above:
public enum Supervised { Any, Yes, No };
public Supervised OwnerSupervised { get; set; } = Supervised.Any;
public Supervised TargetSupervised { get; set; } = Supervised.Any;
// Interfaces.
public SysId InterfaceIdExcludeSystemUsers { get; set; }
/// Contact status.
public StatusCat StatusCat { get; set; } = StatusCat.NotDeleted;
}
}
//=======================================================================
// A f f i l i a t e D a t a L i s t
//=======================================================================
public static class SbmAffiliateList
{
}
//=======================================================================
}