예제 #1
0
        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());
        }