using CRM.Library; using CRM.Models; using CRM.Models.Enums; using CRM.Models.ParticipantAddress; using CRM.Services; using CRM.Services.Participant; using CRM.Web.ClassLibrary; using Kendo.Mvc.Extensions; using Kendo.Mvc.UI; using System; using System.Collections.Generic; using System.Web.Mvc; using System.Web.Routing; namespace CRM.Web.Controllers { [CustomAuthAttribute(Roles = "Site Administrator, Case Manager")] [CustomHandleException] public class ParticipantAddressController : Controller { private readonly ParticipantAddressService _participantAddressService; public ParticipantAddressController() { _participantAddressService = new ParticipantAddressService(new ModelStateWrapper(ModelState)); } public ActionResult ParticipantAddress(string participantMemberCode) { return PartialView("_ParticipantAddress", participantMemberCode); } public ActionResult ParticipantAddresses_Datasource([DataSourceRequest] DataSourceRequest request, string participantMemberCode) { IEnumerable addresses = _participantAddressService.GetParticipantAddressModels(participantMemberCode); DataSourceResult result = addresses.ToDataSourceResult(request); return Json(result); } public ActionResult Edit(int participantAddressID) { ParticipantAddressViewModel address = _participantAddressService.GetParticipantAddressModel(participantAddressID); bool isSiteAdministrator = User.IsSiteAdmin(); bool isAgencyAdministrator = User.IsAgencyAdmin(); int employeeID = User.GetEmployeeID(); int employeesAgencyID = User.GetEmployeeAgencyID(); address.CountyComboList = ComboExtensions.GetPostalCountyCombo(address.Zip); address.EmployeeComboList = ComboExtensions.GetCounselorFilterList(false, false, false, false, true, isSiteAdministrator, isAgencyAdministrator, employeeID, employeesAgencyID, null); address.StateComboList = ComboExtensions.GetStates(address.Zip); address.AddressTypeComboList = ComboExtensions.GetReferenceValuesForSelectList((int)(SharedEnums.ReferenceTypes.AddressType)); return View(address); } public ActionResult AddressType_Datasource(string participantMemberCode) { var income = ComboExtensions.GetReferenceValuesForSelectList((int)(SharedEnums.ReferenceTypes.AddressType)); return Json(income, JsonRequestBehavior.AllowGet); } public JsonResult StateCode_DataSource() { return Json(ComboExtensions.GetStates(null), JsonRequestBehavior.AllowGet); } public JsonResult County_DataSource(string text, string zipCode) { return Json(ComboExtensions.GetPostalCountyCombo(text), JsonRequestBehavior.AllowGet); } [HttpPost, ExportModelStateToTempData] public ActionResult Update([DataSourceRequest] DataSourceRequest request, ParticipantAddressViewModel address) { if (ModelState.IsValid) { _participantAddressService.Update(address, User.GetEmployeeID()); GlobalNotificationsLibrary.AddMessage(User.GetEmployeeID(), address.ParticipantMemberCode, "Address Updated", SharedEnums.NotificationTypes.success, SharedEnums.NotificationCategories.toast); } else { GlobalNotificationsLibrary.AddMessage(User.GetEmployeeID(), address.ParticipantMemberCode, FormExtentionHelpers.GetErrorListFromModelState(ModelState), SharedEnums.NotificationTypes.error, SharedEnums.NotificationCategories.notification); GlobalNotificationsLibrary.AddMessage(User.GetEmployeeID(), address.ParticipantMemberCode, "Address Update Failed", SharedEnums.NotificationTypes.error, SharedEnums.NotificationCategories.toast); } KendoDataSourceResult kendoDataSourceResult = new KendoDataSourceResult(request, address, GlobalNotificationsLibrary.GetMessages()); return Json(kendoDataSourceResult); } public ActionResult GetPostalByZip(string zip) { var postCode = _participantAddressService.GetPostalByZip(zip); return Json(new { data = postCode }, JsonRequestBehavior.AllowGet); } [HttpPost] public object Delete([DataSourceRequest] DataSourceRequest request, int participantAddressID, string participantMemberCode, int? participantProgramID) { KendoDataSourceResult kendoDataSourceResult = null; try { _participantAddressService.DeleteAddress(participantAddressID, User.GetEmployeeID()); GlobalNotificationsLibrary.AddMessage(User.GetEmployeeID(), participantMemberCode, "Address Deleted", SharedEnums.NotificationTypes.success, SharedEnums.NotificationCategories.toast); } catch (Exception e) { ModelState.AddModelError("participantAddressID", "An error occured while deleting Address."); GlobalNotificationsLibrary.AddMessage(User.GetEmployeeID(), participantMemberCode, "An error occured while attempting to delete Address. " + e.Message, SharedEnums.NotificationTypes.error, SharedEnums.NotificationCategories.notification); } kendoDataSourceResult = new KendoDataSourceResult(request, participantAddressID, GlobalNotificationsLibrary.GetMessages()); return Json(kendoDataSourceResult); } public ActionResult Create(string participantMemberCode) { ParticipantAddressViewModel address = new ParticipantAddressViewModel(); bool isSiteAdministrator = User.IsSiteAdmin(); bool isAgencyAdministrator = User.IsAgencyAdmin(); int employeeID = User.GetEmployeeID(); int employeesAgencyID = User.GetEmployeeAgencyID(); address.CountyComboList = ComboExtensions.GetPostalCountyCombo(address.Zip); address.EmployeeComboList = ComboExtensions.GetCounselorFilterList(false, false, false, false, true, isSiteAdministrator, isAgencyAdministrator, employeeID, employeesAgencyID, null); address.StateComboList = ComboExtensions.GetStates(address.Zip); address.AddressTypeComboList = ComboExtensions.GetReferenceValuesForSelectList((int)(SharedEnums.ReferenceTypes.AddressType)); address.ParticipantMemberCode = participantMemberCode; return View(address); } [HttpPost] public ActionResult Create([DataSourceRequest] DataSourceRequest request, ParticipantAddressViewModel address) { if (ModelState.IsValid) { address.CreateDate = ConfigurationManagerExtensions.GetESTDate(); address.CreateEmployeeID = User.GetEmployeeID(); _participantAddressService.Create(address, User.GetEmployeeID()); GlobalNotificationsLibrary.AddMessage(User.GetEmployeeID(), address.ParticipantMemberCode, "Address Created", SharedEnums.NotificationTypes.success, SharedEnums.NotificationCategories.toast); } else { GlobalNotificationsLibrary.AddMessage(User.GetEmployeeID(), address.ParticipantMemberCode, FormExtentionHelpers.GetErrorListFromModelState(ModelState), SharedEnums.NotificationTypes.error, SharedEnums.NotificationCategories.notification); GlobalNotificationsLibrary.AddMessage(User.GetEmployeeID(), address.ParticipantMemberCode, "Address Creation Failed", SharedEnums.NotificationTypes.error, SharedEnums.NotificationCategories.toast); } KendoDataSourceResult kendoDataSourceResult = new KendoDataSourceResult(request, address, ModelState, GlobalNotificationsLibrary.GetMessages()); return Json(kendoDataSourceResult); } } }