public BOCAAssessmentViewModel AddBOCAAssessment(ServiceRequestModel <BOCAAssessmentViewModel> serviceRequestModel) { DateTime assessmentDate; DateTime.TryParse(serviceRequestModel.Model.Date, out assessmentDate); var monthStartDate = assessmentDate.FirstDayOfMonth(); var monthEndDate = assessmentDate.LastDayOfMonth(); if (assessmentDate.Date > GeneralService.CurrentDate.Date) { throw new ArgumentNullException($"The assessment date cannot be in future.", innerException: null); } var assessmentExists = AssessmentRepository.GetAll().Any(c => c.BranchId == serviceRequestModel.Model.BranchId && c.Date >= monthStartDate && c.Date <= monthEndDate); if (assessmentExists) { throw new ArgumentNullException($"BOCA Assessment for the month {assessmentDate.ToString(@"MMM\'yy")} already exists in the system", innerException: null); } var branchOicUser = _sharedService.GetBranchOICUser(serviceRequestModel.Model.BranchId); if (branchOicUser == null) { throw new ArgumentNullException($"There is no user assigned with role Branch OIC for the branch.", innerException: null); } var mapped = _mapper.Map <BOCAAssessmentViewModel, BOCAAssessment>(serviceRequestModel.Model); //mapped.Id = Guid.NewGuid(); //it is not required as Id is already populated var branch = _setupService.GetBranch(mapped.BranchId); var branchAbbr = branch != null ? branch.ShortName : string.Empty; if (string.IsNullOrWhiteSpace(branchAbbr)) { throw new ArgumentNullException($"Assessment number could not be generated as Branch Code is not valid", innerException: null); } var nextBocaRunningNo = _sharedService.GetNextRunningNumber(RunningNumberType.BOCA, mapped.Date.Year, branchAbbr); if (nextBocaRunningNo == 0) { throw new ArgumentNullException($"Error while generating the Assessment Number", innerException: null); } mapped.AssessmentNo = $"{"BOCA"}/{mapped.Date.Year}-{branchAbbr}-{nextBocaRunningNo.ToString().PadLeft(3, '0')}"; mapped.AssessorId = serviceRequestModel.LoginInfo.UserId; mapped.BranchOICUserId = branchOicUser.Id; mapped.Status = AssessmentStatus.Open; mapped.CreatedDate = GeneralService.CurrentDate; if (!serviceRequestModel.Model.IsPartialSave) { mapped.Status = serviceRequestModel.Model.ChecklistCriteria.SelectMany(x => x.BOCAChecklist).Where(x => x.CARRRequest != null).Select(x => x.CARRRequest).Any() ? AssessmentStatus.Open : AssessmentStatus.Closed; mapped.IsCompleted = true; } AssessmentRepository.Add(mapped); var carrCount = 0; foreach (var bocaViewModel in serviceRequestModel.Model.ChecklistCriteria.SelectMany(x => x.BOCAChecklist).ToList()) { //bocaViewModel.Id = Guid.NewGuid(); bocaViewModel.BOCAAssessmentId = mapped.Id; var boca = _mapper.Map <BOCAViewModel, BOCA>(bocaViewModel); boca.Attachments = null; BocaRepository.Add(boca); if (bocaViewModel.CARRRequest != null) { var nextCarrRunningNo = 0; if (!serviceRequestModel.Model.IsPartialSave) { nextCarrRunningNo = _sharedService.GetNextRunningNumber(RunningNumberType.CARR, mapped.Date.Year, branchAbbr); } var carrReq = _mapper.Map <CARRRequestViewModel, CARRRequest>(bocaViewModel.CARRRequest); var carrId = Guid.NewGuid(); // response for partial save bocaViewModel.CARRRequest.Id = carrId; carrReq.Id = carrId; carrReq.BOCAId = boca.Id; if (!serviceRequestModel.Model.IsPartialSave) { carrReq.ReferenceNo = $"{"BOCA/CARR"}/{mapped.Date.Year}-{branchAbbr}-{nextCarrRunningNo.ToString().PadLeft(3, '0')}"; } //carrReq.RecommendedReplyDate = mapped.Date.AddDays(14); carrReq.CARRStatus = CARRStatus.Pending; CarrRequestRepository.Add(carrReq); carrCount++; } if (bocaViewModel.Attachments.Any()) { bocaViewModel.Attachments.ForEach(y => { y.Id = Guid.NewGuid(); y.BOCAId = boca.Id; var p = _mapper.Map <BocaAttachmentViewModel, BOCAAttachment>(y); var attachFullName = p.AttachmentFullName.MoveToDestination(serviceRequestModel.Model.HostingEnviromentPath, "BOCA/" + mapped.Id); y.AttachmentFullName = attachFullName; p.AttachmentFullName = attachFullName; AttachmentRepository.Add(p); }); } } _genericUnitOfWork.SaveChanges(); if (serviceRequestModel.Model.IsPartialSave) { serviceRequestModel.Model.AssessmentNo = mapped.AssessmentNo; return(serviceRequestModel.Model); } if (carrCount == 0) { return(new BOCAAssessmentViewModel()); } var bocaNotificationViewModel = new BOCANotificationViewModel { AssessmentNo = mapped.AssessmentNo, CARRRequestCount = carrCount, ToEmail = branchOicUser.Email }; SendCarrRequestedEmailsToBranchOicUser(bocaNotificationViewModel); return(new BOCAAssessmentViewModel()); }
public AocaAssessmentViewModel AddAocaAssessment(ServiceRequestModel <AocaAssessmentViewModel> serviceRequestModel) { DateTime assessmentDate; DateTime.TryParse(serviceRequestModel.Model.Date, out assessmentDate); var monthStartDate = assessmentDate.FirstDayOfMonth(); var monthEndDate = assessmentDate.LastDayOfMonth(); if (assessmentDate.Date > GeneralService.CurrentDate.Date) { throw new ArgumentNullException($"The assessment date cannot be in future.", innerException: null); } var assessmentExists = AssessmentRepository.GetAll().Any(c => c.AgentId == serviceRequestModel.Model.AgentId && c.Date >= monthStartDate && c.Date <= monthEndDate); if (assessmentExists) { throw new ArgumentNullException($"AOCA Assessment for the month {assessmentDate.ToString(@"MMM\'yy")} already exists in the system", innerException: null); } var agentCode = _genericUnitOfWork.GetRepository <Agent, Guid>().GetAll().FirstOrDefault(c => c.Id == serviceRequestModel.Model.AgentId)?.Code; var mapped = _mapper.Map <AocaAssessmentViewModel, AOCAAssessment>(serviceRequestModel.Model); mapped.AssessmentNo = _sharedService.GenerateNumber(RunningNumberType.AOCA, mapped.Date.Year, agentCode ?? "", "AOCA/", "yy", "MM", 3); mapped.CreatedById = serviceRequestModel.LoginInfo.UserId; mapped.CreatedDate = GeneralService.CurrentDate; mapped.Status = AssessmentStatus.Open; if (!serviceRequestModel.Model.IsPartialSave) { mapped.Status = serviceRequestModel.Model.ChecklistCriteria.SelectMany(x => x.AOCAChecklist).Where(x => x.CARRRequest != null).Select(x => x.CARRRequest).Any() ? AssessmentStatus.Open : AssessmentStatus.Closed; mapped.IsCompleted = true; } AssessmentRepository.Add(mapped); var carrCount = 0; foreach (var aocaViewModel in serviceRequestModel.Model.ChecklistCriteria.SelectMany(x => x.AOCAChecklist).ToList()) { //aocaViewModel.Id = Guid.NewGuid(); aocaViewModel.AOCAAssessmentId = mapped.Id; var aoca = _mapper.Map <AOCAViewModel, AOCA>(aocaViewModel); aoca.Attachments = null; AocaRepository.Add(aoca); if (aocaViewModel.CARRRequest != null) { var carrReq = _mapper.Map <AOCACARRRequestViewModel, AOCACARRRequest>(aocaViewModel.CARRRequest); var carrId = Guid.NewGuid(); // response for partial save aocaViewModel.CARRRequest.Id = carrId; carrReq.Id = carrId; carrReq.AOCAId = aoca.Id; if (!serviceRequestModel.Model.IsPartialSave) { var nextCarrRunningNo = _sharedService.GetNextRunningNumber(RunningNumberType.AOCACARR, mapped.Date.Year, agentCode); carrReq.ReferenceNo = $"{"AOCA/CARR"}/{mapped.Date.Year}-{agentCode}-{nextCarrRunningNo.ToString().PadLeft(3, '0')}"; } // carrReq.RecommendedReplyDate = mapped.Date.AddDays(14); carrReq.CARRStatus = CARRStatus.Pending; CarrRequestRepository.Add(carrReq); carrCount++; } if (aocaViewModel.Attachments.Any()) { aocaViewModel.Attachments.ForEach(y => { y.Id = Guid.NewGuid(); y.AOCAId = aoca.Id; var attachment = _mapper.Map <AocaAttachmentViewModel, AOCAAttachment>(y); var attachmentFullName = attachment.AttachmentFullName.MoveToDestination( serviceRequestModel.Model.HostingEnviromentPath, "AOCA/" + mapped.Id); attachment.AttachmentFullName = attachmentFullName; y.AttachmentFullName = attachmentFullName; AttachmentRepository.Add(attachment); }); } } _genericUnitOfWork.SaveChanges(); if (serviceRequestModel.Model.IsPartialSave) { serviceRequestModel.Model.AssessmentNo = mapped.AssessmentNo; return(serviceRequestModel.Model); } if (carrCount == 0) { return(new AocaAssessmentViewModel()); } var agentDetail = AgentRepository.GetById(mapped.AgentId); var aocaNotificationViewModel = new AOCANotificationViewModel { AssessmentNo = mapped.AssessmentNo, CARRRequestCount = carrCount, ToEmail = agentDetail.User.Email, CcEmail = agentDetail.User.Email }; SendCarrRequestedEmailsToAllAgentManagementUsers(aocaNotificationViewModel); return(new AocaAssessmentViewModel()); }