// GET: Customers/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            ProjectController  projectController  = new ProjectController(_context, _config);
            EstimateController estimateController = new EstimateController(_context, _config);

            if (id == null)
            {
                return(NotFound());
            }

            DateTime Today = DateTime.UtcNow;

            CustomerDetailViewModel viewModel = new CustomerDetailViewModel {
                Customer = await _context.Customer.FirstOrDefaultAsync(c => c.Id == id),
                CustomerCurrentProjects   = await _context.Project.Where(p => p.CustomerId == id && p.IsComplete == false).ToListAsync(),
                CustomerCompletedProjects = await _context.Project.Where(p => p.CustomerId == id && p.IsComplete == true).ToListAsync(),
                CustomerCurrentEstimates  = await _context.Estimate.Where(e => e.CustomerId == id && e.ExpirationDate <= Today).ToListAsync(),
                CustomerPastEstimates     = await _context.Estimate.Where(e => e.CustomerId == id && Today > e.ExpirationDate).ToListAsync()
            };


            if (viewModel.Customer == null)
            {
                return(NotFound());
            }

            return(View(viewModel));
        }
        // GET: EstimateCosts/Create
        public async Task <IActionResult> Create(int id)
        {
            DateTime                    Today      = DateTime.UtcNow;
            EstimateController          controller = new EstimateController(_context, _config);
            EstimateCostCreateViewModel model      = new EstimateCostCreateViewModel {
                EstimateId = id,
                Estimate   = controller.GetEstimateById(id),
                Costs      = new List <EstimateCost>(),
                CostItems  = new List <CostItem>()
            };

            model.CostItems = await _context.CostItem
                              .Include(ci => ci.CostCategory)
                              .Include(ci => ci.UnitOfMeasure)
                              .OrderBy(ci => ci.ItemName)
                              .ToListAsync();

            EstimateCost Cost = new EstimateCost {
                EstimateId = id
            };

            model.Costs.Add(Cost);

            return(View(model));
        }
        public async Task <IActionResult> Edit(int id,
                                               [Bind("Id,ItemName,EstimateId,Category,UnitOfMeasure,CostPerUnit,MarkupPercent,Quantity")]
                                               CustomEstimateCost customEstimateCost)
        {
            if (id != customEstimateCost.Id)
            {
                return(NotFound());
            }

            ModelState.Remove("Estimate");

            if (ModelState.IsValid)
            {
                _context.Update(customEstimateCost);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", "Estimate", new { id = customEstimateCost.EstimateId }));
            }

            EstimateController controller = new EstimateController(_context, _config);

            customEstimateCost.Estimate = controller.GetEstimateById(customEstimateCost.EstimateId);

            ViewData["ProjectId"] = customEstimateCost.EstimateId;
            return(View(customEstimateCost));
        }
        // GET: CustomEstimateCost/Create
        public IActionResult Create(int id)
        {
            DateTime today = DateTime.UtcNow;

            EstimateController controller = new EstimateController(_context, _config);

            CustomEstimateCostCreateViewModel viewModel = new CustomEstimateCostCreateViewModel {
                EstimateId  = id,
                Estimate    = controller.GetEstimateById(id),
                CustomCosts = new List <CustomEstimateCost>(),
            };

            CustomEstimateCost customCost = new CustomEstimateCost {
                EstimateId    = id,
                ItemName      = "",
                UnitOfMeasure = "",
                Category      = "Custom",
                Quantity      = 0
            };

            viewModel.CustomCosts.Add(customCost);

            ViewData["EstimateId"] = id;
            return(View(viewModel));
        }
        // GET: CustomEstimateCost/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            EstimateController controller = new EstimateController(_context, _config);

            var customEstimateCost = await _context.CustomEstimateCost.FindAsync(id);

            customEstimateCost.Category = "Custom";
            customEstimateCost.Estimate = controller.GetEstimateById(customEstimateCost.EstimateId);

            if (customEstimateCost == null)
            {
                return(NotFound());
            }

            ViewData["ProjectId"] = customEstimateCost.EstimateId;
            return(View(customEstimateCost));
        }