378 lines
15 KiB
C#
378 lines
15 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Sarand.BusinessMgr.Net.Core.Web.App;
|
|
using Sarand.BusinessMgr.Net.Core.Web.Helpers;
|
|
using Sarand.BusinessMgr.Net.Core.Web.Portal.Models;
|
|
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.Helpers;
|
|
using Sarand.BusinessMgr.Net.Standard.Base.Mgr;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Email;
|
|
using Sarand.Net.Standard.Base.Helpers;
|
|
using Sarand.Net.Standard.Base.Security;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace Sarand.BusinessMgr.Net.Core.Web.Portal.Controllers
|
|
{
|
|
//=======================================================================
|
|
// Portal Contact Controller - Ajax actions.
|
|
//=======================================================================
|
|
partial class ContactController
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Affiliate data.
|
|
private class AffiliateData_List : AjaxResponse_Data.ItemData_Base
|
|
{
|
|
public String DisplayName { get; set; }
|
|
public String TypeCode { get; set; }
|
|
public String TypeDesc { get; set; }
|
|
public bool? Individual { get; set; }
|
|
// Address.
|
|
public String AddressSummary { get; set; }
|
|
}
|
|
private class AffiliateData_Full : AffiliateData_List
|
|
{
|
|
// Affiliate properties.
|
|
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();
|
|
}
|
|
private void PopulateAffiliateResponseData(SbmAffiliate affiliate, AffiliateData_List data)
|
|
{
|
|
data.Id = SysId.ToString(affiliate.Id);
|
|
data.DisplayName = affiliate.GetDisplayName();
|
|
data.TypeCode = Internals.AffiliateTypeCodes.GetCodeForType(affiliate.AffiliateType);
|
|
data.TypeDesc = Internals.AffiliateTypeCodes.GetDescForType(affiliate.AffiliateType);
|
|
data.Individual = affiliate.AffiliateType == Sbm.AffiliateType.Individual ? true : (bool?)null;
|
|
// Common properties.
|
|
data.Notes = affiliate.Notes;
|
|
data.StatusCode = affiliate.StatusCode;
|
|
data.StatusDesc = affiliate.StatusDesc;
|
|
}
|
|
private void PopulateAffiliateResponseData(SbmAffiliate affiliate, AffiliateData_Full data)
|
|
{
|
|
PopulateAffiliateResponseData(affiliate, data as AffiliateData_List);
|
|
// Affiliate properties.
|
|
data.PhoneMain = affiliate.PhoneMain;
|
|
data.PhoneAlt = affiliate.PhoneAlt;
|
|
data.PhoneFax = affiliate.PhoneFax;
|
|
data.Email = affiliate.Email;
|
|
data.Website = affiliate.Website;
|
|
// Ogranisation.
|
|
data.AffiliateIdParent = affiliate.AffiliateIdParent;
|
|
data.Name = affiliate.Name;
|
|
data.Abbr = affiliate.Abbr;
|
|
data.Nature = affiliate.Nature;
|
|
data.Contact = affiliate.Contact;
|
|
data.CompanyNo = affiliate.CompanyNo;
|
|
data.VatReg = affiliate.VatReg;
|
|
data.PhoneDirect = affiliate.PhoneDirect;
|
|
// Individual.
|
|
data.Title = affiliate.Title;
|
|
data.Initials = affiliate.Initials;
|
|
data.LastName = affiliate.LastName;
|
|
data.FirstName = affiliate.FirstName;
|
|
data.Salutation = affiliate.Salutation;
|
|
data.PhoneMobile = affiliate.PhoneMobile;
|
|
data.BirthDay = affiliate.BirthDay;
|
|
data.BirthMonth = affiliate.BirthMonth;
|
|
data.BirthYear = affiliate.BirthYear;
|
|
data.MaritalStatus = affiliate.MaritalStatus;
|
|
data.Spouse = affiliate.Spouse;
|
|
data.Children = affiliate.Children;
|
|
data.Hometown = affiliate.Hometown;
|
|
data.UserId = affiliate.UserId;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Contact list.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_ContactList : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_ContactList(SbmModel_Portal model) : base(model) { }
|
|
// Affiliate list.
|
|
public List<AffiliateData_List> Affiliates { get; } = new List<AffiliateData_List>();
|
|
// Reference data.
|
|
public List<CodeDesc> Statuses { get; } = new List<CodeDesc>();
|
|
public String DefaultStatusCode { get; set; }
|
|
}
|
|
// Ajax request.
|
|
[HttpPost]
|
|
[Route("Contact/Ajax/List")]
|
|
[Route("Contact/Ajax/GetList")]
|
|
public IActionResult Ajax_ContactList(Model_Contact model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_ContactList";
|
|
AjaxResponse_ContactList responseData = new AjaxResponse_ContactList(model);
|
|
String messageTitle = Localiser["contact.caption.contacts"];
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
|
|
if (!ValidReferer) return Forbid();
|
|
RefreshModel(model);
|
|
|
|
// Reference data - statuses.
|
|
foreach (SaStatus status in SbmAffiliate.ValidStatuses.FindAll(item => item != SaStatus.Deleted))
|
|
responseData.Statuses.Add(SacsSystem.GetStatusCode(status), Localiser.GetStatusCaption(status));
|
|
responseData.DefaultStatusCode = SacsSystem.GetStatusCode(SbmAffiliate.DefaultStatus);
|
|
|
|
// Item filter.
|
|
SbmAffiliate.Params prmAffiliate = new SbmAffiliate.Params();
|
|
prmAffiliate.Search = model.Filter.ContactSearch;
|
|
prmAffiliate.AffiliateType = model.GetFilterContactType();
|
|
prmAffiliate.StatusCat = SbmAffiliate.StatusCat.NotDeleted;
|
|
prmAffiliate.SortColumns.Add(AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.Name));
|
|
prmAffiliate.SortColumns.Add(AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.LastName));
|
|
model.Paging.ItemTotal = (ulong)AccountMgr.GetAffiliateCount(prmAffiliate);
|
|
model.PopulatePagingTo(prmAffiliate.ResultsPaging);
|
|
|
|
// Item columns.
|
|
ColumnDfn colId = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.Id);
|
|
ColumnDfn colName = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.Name);
|
|
ColumnDfn colAffiliateType = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.Type);
|
|
ColumnDfn colPhoneMain = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.PhoneMain);
|
|
ColumnDfn colPhoneAlt = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.PhoneAlt);
|
|
ColumnDfn colAddress = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.Address_Summary);
|
|
ColumnDfn colNotes = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.Notes);
|
|
ColumnDfn colStatus = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.Status);
|
|
ColumnDfn colCreatedOn = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.CreateOn_Date);
|
|
ColumnDfn colUpdatedOn = AccountMgr.GetAffiliateColumnDfn(SbmAffiliate.Column.UpdateOn_Date);
|
|
List<ColumnDfn> columns = new List<ColumnDfn>()
|
|
{
|
|
colId, colName, colAffiliateType,
|
|
colPhoneMain, colPhoneAlt,
|
|
colAddress,
|
|
colNotes, colStatus, colCreatedOn, colUpdatedOn
|
|
};
|
|
|
|
// Query items.
|
|
using (DbResultSet resultSet = AccountMgr.ExecuteAffiliateQuery(columns, prmAffiliate))
|
|
{
|
|
while (resultSet != null && resultSet.HasRows && resultSet.NextRecord())
|
|
{
|
|
NamedValue.Map values = resultSet.GetRecordValues();
|
|
Sbm.AffiliateType affiliateType = (Sbm.AffiliateType)values.GetLong(colAffiliateType.DataName);
|
|
AffiliateData_List affiliateData = new AffiliateData_List()
|
|
{
|
|
Id = values.GetString(colId.DataName),
|
|
DisplayName = values.GetString(colName.DataName),
|
|
TypeCode = Internals.AffiliateTypeCodes.GetCodeForType(affiliateType),
|
|
TypeDesc = Internals.AffiliateTypeCodes.GetDescForType(affiliateType),
|
|
Individual = affiliateType == Sbm.AffiliateType.Individual ? true : (bool?)null,
|
|
AddressSummary = AccountMgr.GetAffiliateDataValue(colAddress, values).AsString(),
|
|
Notes = values.GetString(colNotes.DataName),
|
|
StatusCode = values.GetString(colStatus.DataName),
|
|
StatusDesc = SacsSystem.GetStatusDescForCode(values.GetString(colStatus.DataName)),
|
|
CreatedOn = DateUtil.Format(values.GetDate(colCreatedOn.DataName), "yyyyMMdd"),
|
|
CreatedOnFmt = Localiser.FormatDate(values.GetDate(colCreatedOn.DataName)),
|
|
UpdatedOn = DateUtil.Format(values.GetDate(colUpdatedOn.DataName), "yyyyMMdd"),
|
|
UpdatedOnFmt = Localiser.FormatDate(values.GetDate(colUpdatedOn.DataName))
|
|
};
|
|
responseData.Affiliates.Add(affiliateData);
|
|
}
|
|
}
|
|
|
|
// Refresh paging info.
|
|
if (model.Paging.IsPaged)
|
|
model.Paging.ItemCount = (ushort)responseData.Affiliates.Count;
|
|
SessionData_Portal.Profile.Contacts.Filter.PageNo = model.Paging.PageNo;
|
|
SessionData_Portal.Profile.Contacts.Filter.PageLength = model.Paging.PageLength;
|
|
responseData.SetPaging(model);
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Affiliate ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_Affiliate : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_Affiliate(SbmModel_Portal model) : base(model) { }
|
|
// Affiliate details.
|
|
public AffiliateData_Full affiliate { get; } = new AffiliateData_Full();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("Contact/Ajax/Item")]
|
|
[Route("Contact/Ajax/GetItem")]
|
|
public IActionResult Ajax_GetItem(Model_Contact model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_GetItem";
|
|
String messageTitle = Localiser["contact.caption.contact"];
|
|
AjaxResponse_Affiliate responseData = new AjaxResponse_Affiliate(model);
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
|
|
if (!ValidReferer) return Forbid();
|
|
RefreshModel(model);
|
|
|
|
// Identify item.
|
|
SysId affiliateId = model.AffiliateId.ToSysId();
|
|
if (!SysId.IsValid(affiliateId))
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.specified"]));
|
|
|
|
// Retrieve item.
|
|
SbmAffiliate affiliate = AccountMgr.GetAffiliateById(affiliateId);
|
|
if (affiliate == null)
|
|
{
|
|
SaLogger.Warn(fn + " Failed to retrieve affiliate for Id: '" + model.AffiliateId + "'");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
|
|
}
|
|
|
|
// Construct response.
|
|
PopulateAffiliateResponseData(affiliate, responseData.affiliate);
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Store Affiliate ajax request.
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("Contact/Ajax/Store")]
|
|
[Route("Contact/Ajax/StoreItem")]
|
|
public IActionResult Ajax_StoreItem(Model_Contact model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_StoreItem";
|
|
String messageTitle = Localiser["contact.caption.contact"];
|
|
AjaxResponse_Affiliate responseData = new AjaxResponse_Affiliate(model);
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
|
|
if (!ValidReferer) return Forbid();
|
|
RefreshModel(model);
|
|
|
|
// Validate item properties.
|
|
model.ValidateAffiliateData(responseData);
|
|
if (responseData.HasControlErrors())
|
|
return Json(responseData);
|
|
|
|
// Validate item Id against 'Exists' flag.
|
|
SysId affiliateId = model.AffiliateId.ToSysId();
|
|
bool contactExists = StringUtil.ToBool(model.ContactExists);
|
|
if (contactExists && !SysId.IsValid(affiliateId))
|
|
{
|
|
SaLogger.Warn(fn + " Id not specified for existing Affiliate.");
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
if (!contactExists && SysId.IsValid(affiliateId))
|
|
{
|
|
SaLogger.Warn(fn + " Id specified for new Affiliate: '" + model.AffiliateId + "'");
|
|
return Json(responseData.SetGoHome(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
|
|
// Retrieve existing item.
|
|
SbmAffiliate affiliate = AccountMgr.GetAffiliateById(affiliateId);
|
|
if (affiliate == null && SysId.IsValid(affiliateId))
|
|
{
|
|
SaLogger.Warn(fn + " Failed to retrieve affiliate for Id: '" + model.AffiliateId + "'");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
|
|
}
|
|
|
|
// Create new item if required.
|
|
if (affiliate == null)
|
|
{
|
|
affiliate = AccountMgr.CreateAffiliate(model.GetContactType());
|
|
}
|
|
else
|
|
{
|
|
if (model.ContactType != null)
|
|
affiliate.AffiliateType = model.GetContactType();
|
|
}
|
|
|
|
// Populate item properties.
|
|
if (affiliate.IsIndividual())
|
|
{
|
|
if (model.ContactFirstName != null)
|
|
affiliate.FirstName = model.ContactFirstName;
|
|
if (model.ContactLastName != null)
|
|
affiliate.LastName = model.ContactLastName;
|
|
}
|
|
else
|
|
{
|
|
if (model.ContactName != null)
|
|
affiliate.Name = model.ContactName;
|
|
}
|
|
// Common properties.
|
|
if (model.ContactNotes != null)
|
|
affiliate.Notes = model.ContactNotes;
|
|
if (model.ContactStatusCode != null)
|
|
affiliate.Status = model.ContactStatus;
|
|
|
|
// Store item.
|
|
if (!SysId.IsValid(affiliate.Id))
|
|
{
|
|
if (!AccountMgr.InsertAffiliate(affiliate, DbUpdateInfo))
|
|
{
|
|
SaLogger.Warn(fn + " Failed to insert affiliate: " + AccountMgr.ErrorStateMessage);
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.insert.failed"] + "\n\n" + AccountMgr.ErrorStateMessage));
|
|
}
|
|
responseData.SetNotification(messageTitle, Localiser["msg.item.created"]);
|
|
}
|
|
else
|
|
{
|
|
if (!AccountMgr.UpdateAffiliate(affiliate, DbUpdateInfo))
|
|
{
|
|
SaLogger.Warn(fn + " Failed to update affiliate [Id:" + SysId.ToString(affiliate.Id) + "]: " + AccountMgr.ErrorStateMessage);
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.update.failed"] + "\n\n" + AccountMgr.ErrorStateMessage));
|
|
}
|
|
responseData.SetNotification(messageTitle, Localiser["msg.item.updated"]);
|
|
}
|
|
|
|
// Construct response.
|
|
PopulateAffiliateResponseData(affiliate, responseData.affiliate);
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|