public ActionResult Index()
        {
            var department = _uow.GetUserDepartment();

            var balances   = _uow.BalanceDeviceRepository.Get().Where(item => item.Department.Equals(department) && !item.IsArchived);
            var viewModels = new List <BalanceIndexViewModel>();

            foreach (var item in balances)
            {
                CompareDates.SetBalanceToUnverified(item, _uow);
                viewModels.Add(new BalanceIndexViewModel()
                {
                    BalanceId      = item.DeviceId,
                    DeviceCode     = item.DeviceCode,
                    IsVerified     = item.IsVerified,
                    BalanceType    = item.BalanceType,
                    DepartmentName = item.Department.DepartmentName,
                    LastVerifiedBy = item.DeviceVerifications == null ? "N/A" :
                                     item.DeviceVerifications
                                     .Where(x => x.Device.Equals(item))
                                     .Count() == 0 ?
                                     "N/A" :
                                     item.DeviceVerifications
                                     .Where(x => x.Device.Equals(item))
                                     .OrderByDescending(x => x.VerifiedOn)
                                     .First()
                                     .User.UserName
                });
            }
            return(View(viewModels));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(ContentViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //  Check that Start Date and End Date are within Policy Cover Start and Cover End Date
                    Guid policyId = viewModel.PolicyID;
                    viewModel.ErrMsg = string.Empty;

                    var startDate = viewModel.Content.StartDate;
                    var endDate   = viewModel.Content.EndDate;

                    CompareDates compareDates = new CompareDates(_context);

                    if (compareDates.CompareStartDate(policyId, startDate) < 0)
                    {
                        viewModel.ErrMsg = $"Start Date cannot be earlier than Policy Cover Start Date";
                        ModelState.AddModelError(string.Empty, viewModel.ErrMsg);
                    }

                    if (compareDates.CompareEndDate(policyId, endDate) > 0)
                    {
                        viewModel.ErrMsg = $"End Date cannot be later than Policy Cover End Date";
                        ModelState.AddModelError(string.Empty, viewModel.ErrMsg);
                    }

                    if (string.IsNullOrEmpty(viewModel.ErrMsg))
                    {
                        Content content = viewModel.Content;
                        content.ID       = Guid.NewGuid();
                        content.PolicyID = policyId;
                        _context.Add(content);
                        await _context.SaveChangesAsync();

                        return(RedirectToAction("PolicyRisks", "Policies",
                                                new { productId = viewModel.ProductID, clientId = viewModel.ClientID, policyId = viewModel.PolicyID }));
                    }
                }
            }
            catch (DbUpdateException ex)
            {
                var errorMsg = ex.InnerException.Message.ToString();

                viewModel.ErrMsg = errorMsg;
                ModelState.AddModelError(string.Empty, viewModel.ErrMsg);
            }
            viewModel.ResidenceTypeList = new SelectList(await _context.ResidenceTypes
                                                         .AsNoTracking()
                                                         .ToListAsync(), "ID", "Name", viewModel.Content.ResidenceTypeID);
            viewModel.RoofTypeList = new SelectList(await _context.RoofTypes
                                                    .AsNoTracking()
                                                    .ToListAsync(), "ID", "Name", viewModel.Content.RoofTypeID);
            viewModel.WallTypeList = new SelectList(await _context.WallTypes
                                                    .AsNoTracking()
                                                    .ToListAsync(), "ID", "Name", viewModel.Content.WallTypeID);
            return(View(viewModel));
        }