using System; using System.Text; using System.Data; using System.Linq; using System.Web.Mvc; using System.Data.Entity.Validation; using System.Collections.Generic; using Neo.Afx.Mvc; using Neo.Afx.ViewModels; using Neo.Afx.ComponentModel; using Neo.Afx.Services.Integration; using Neo.Afx.Services.Workflows; using Neo.Afx.Services.Workflows.Entities; using Neo.Afx.Services.Forms.Entities; using Neo.LegitimateLicences.Common; using Neo.LegitimateLicences.Data.Models; using Neo.LegitimateLicences.AccreditationIssueRequest.Models; using Neo.LegitimateLicences.Data; using Neo.Afx.Services.Audits; using Neo.LegitimateLicences.Common.Helpers; using Neo.LegitimateLicences.AccreditationIssueRequest.Models.Database; using AccreditationIssueDetail = Neo.LegitimateLicences.AccreditationIssueRequest.Models.Database.AccreditationIssueDetail; using Neo.Afx.Common; namespace Neo.LegitimateLicences.AccreditationIssueRequest { public partial class AccreditationIssueRequestController { #region Save [HttpPost] public JsonNetResult Save(AccreditationIssueDetail value) { var changes = new List(); var auditDb = new AuditsDatabase(); #region validate originating licence number of provided ////if (value.ExistingLicence != null && !string.IsNullOrEmpty(value.ExistingLicence.OperatingLicenceNumber)) ////{ //// switch (value.SourceApplicationID) //// { //// case (short)Common.SourceApplication.NTLIS: //// if (!value.ExistingLicence.OperatingLicenceNumber.ToUpper().StartsWith("LKNKZN")) //// { //// return Json(new //// { //// Result = "ERROR", //// Message = "The originating licence number is not valid." //// }); //// } //// break; //// case (short)Common.SourceApplication.Legitimate: //// if (!value.ExistingLicence.OperatingLicenceNumber.ToUpper().StartsWith("LGKZN") && //// !value.ExistingLicence.OperatingLicenceNumber.ToUpper().StartsWith("LGTST")) //// { //// return Json(new //// { //// Result = "ERROR", //// Message = "The originating licence number is not valid." //// }); //// } //// break; //// default: //// break; //// } ////} #endregion #region validate event start and end date for special licence ////if (value.AccreditationIssueRequestTypeID == (short)Common.AccreditationIssueRequestType.TemporarySpecialLicence) ////{ //// //this logic must be updated in the LIR save as well if you adjust it //// if (!value.ExistingLicence.SpecialLicenceDateFrom.HasValue || !value.ExistingLicence.SpecialLicenceDateTo.HasValue) //// { //// return Json(new //// { //// Result = "ERROR", //// Message = "You have to specify the date from and date to for the special licence." //// }); //// } //// else { //// if ((value.ExistingLicence.SpecialLicenceDateTo.Value - value.ExistingLicence.SpecialLicenceDateFrom.Value).TotalDays > 14) //// { //// return Json(new //// { //// Result = "ERROR", //// Message = "A special licence duration is not allowed to exceed 14 days." //// }); //// } //// else if (value.ExistingLicence.SpecialLicenceDateTo.Value < value.ExistingLicence.SpecialLicenceDateFrom.Value) //// { //// return Json(new //// { //// Result = "ERROR", //// Message = "The To date must be before or the same as the from date." //// }); //// } //// } ////} #endregion ////try ////{ //// if (value.ApplicationDetailID <= 0) //// { //// changes.Add("New application created"); //// value.CreatedDate = DateTime.Now; //// value.CreatedByID = Profile.UserID; //// value.UpdatedDate = DateTime.Now; //// value.UpdatedByID = Profile.UserID; //// } //// else //// { //// } ////} ////catch (Exception ex) ////{ //// return new JsonNetResult() //// { //// Data = new JsonBaseResult() //// { //// Success = false, //// ErrorCode = "ERROR", //// Error =string.Format("Error: {0}", new LegitimateDatabase().GetInnermostMessage(ex)) //// } //// }; ////} try { //Ensure that we don't have another application, for tyhe same application type, in progress for the ol ////if (value.ApplicationDetailID <= 0) ////{ //// var checkDb = new ApplicationDatabase(); //// var idNumber = value.Applicant.IDNumberOrBusinessRegistrationNo; //// var associationID = value.Applicant.VerifiedAssociationID; //// var registrationNumber = value.Vehicle.VehicleRegistrationNumber; //// if (AccreditationIssueRequestHelper.IsTransferType(value.AccreditationIssueRequestTypeID)) //// { //// idNumber = value.ExistingLicenceHolder.IDNumberOrBusinessRegistrationNo; //// } //// checkDb.PerformPrecheck(value.PrecheckDetailID.Value, value.AccreditationIssueRequestTypeID, value.SourceApplicationID, value.ExistingLicence.OperatingLicenceNumber, idNumber, associationID, registrationNumber, true, Profile.UserID, value.Mode.ModeID, true); ////} Database.Save(); WorkflowsDatabase workflowsDatabase = new WorkflowsDatabase(); workflowsDatabase.ExecutePostFormSave(value.WorkflowInstanceID.Value, Profile.UserID); ////if (changes.Count > 0) ////{ //// auditDb.WriteEntries(2 /*Application*/, 2 /*Updated*/, changes, 1000 /*AccreditationIssueRequestDetails*/, value.ApplicationDetailID, Profile.UserID); ////} } catch (DbEntityValidationException dbEx) { foreach (var validationError in dbEx.EntityValidationErrors.SelectMany(validationErrors => validationErrors.ValidationErrors)) { return new JsonNetResult() { Data = new JsonBaseResult() { Success = false, ErrorCode = "ERROR", Error = string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) } }; } } catch (Exception ex) { #region Error Handling #region Error Additional Data Dictionary additionalData = new Dictionary(); additionalData.Add("value", value); #endregion string error = ErrorHelper.ProcessError(ex, "AccreditationIssueRequest", "Save", additionalData); #endregion if (ex.InnerException != null) { if (ex.InnerException.Message.Contains("IX_PrecheckDetailID")) { return new JsonNetResult() { Data = new JsonBaseResult() { Success = false, ErrorCode = "ERROR", Error = "The application has already been saved against this pre-check. Please submit the original application instead." } }; } else { return new JsonNetResult() { Data = new JsonBaseResult() { Success = false, ErrorCode = "ERROR", Error = string.Format("Error: {0}", new LegitimateDatabase().GetInnermostMessage(ex)) } }; } } else { return new JsonNetResult() { Data = new JsonBaseResult() { Success = false, ErrorCode = "ERROR", Error = string.Format("Error: {0}", new LegitimateDatabase().GetInnermostMessage(ex)) } }; } } var formDb = new AccreditationIssueDatabase(); var formData = formDb.GetAccreditationIssueForm(value.AccreditationIssueDetailID, Profile.UserID); return new JsonNetResult() { Data = new JsonBaseResult() { Result = formData, Success = true } }; } #endregion } }