Esempio n. 1
0
        public void AddCITBooking(ServiceRequestModel <CITBookingViewModel> serviceRequestModel)
        {
            var loginBranch = serviceRequestModel.LoginInfo.LoginUserBranch;

            if (loginBranch == null)
            {
                throw new ArgumentNullException($"Only BOIC can book the CIT.", innerException: null);
            }

            var repo      = _genericUnitOfWork.GetRepository <CITBooking, Guid>();
            var todayDate = GeneralService.CurrentDate.Date;

            var today = repo.GetAll().Any(c => TruncateTime(c.CreatedDate) == todayDate &&
                                          c.BranchId == loginBranch.Id && c.CITBookingCancel == null &&
                                          c.CIT == null);

            if (today)
            {
                throw new ArgumentNullException($"Please cancel or post cit to do next booking.", innerException: null);
            }


            var mapped = _mapper.Map <CITBookingViewModel, CITBooking>(serviceRequestModel.Model);

            mapped.Id          = Guid.NewGuid();
            mapped.CreatedById = serviceRequestModel.CurrentUserId.Value;
            mapped.CreatedDate = GeneralService.CurrentDate;
            mapped.BranchId    = loginBranch.Id;
            mapped.BookingCode = _sharedService.GenerateNumber(RunningNumberType.CITBooking, mapped.CreatedDate.Year,
                                                               loginBranch.ShortName, "CIT/Booking/", "yyyy", "MM", 3);
            repo.Add(mapped);



            // Sending mail to boas execuitives

            _genericUnitOfWork.SaveChanges();
        }
        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());
        }