// 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));
        }
        public async Task <IActionResult> Create(CustomEstimateCostCreateViewModel customEstimateCosts)
        {
            List <CustomEstimateCost> CustomEstimateCostsInContext = await _context.CustomEstimateCost
                                                                     .Where(cec => cec.EstimateId == customEstimateCosts.EstimateId).ToListAsync();

            List <CustomEstimateCost> CostsEntered = (customEstimateCosts.CustomCosts?.Count > 0) ?
                                                     customEstimateCosts.CustomCosts : customEstimateCosts.RejectedEntries;
            List <CustomEstimateCost> RejectecdEntries = new List <CustomEstimateCost>();
            List <CustomEstimateCost> UpdatedRecords   = new List <CustomEstimateCost>();

            Estimate Estimate = await _context.Estimate.FirstOrDefaultAsync(e => e.Id == customEstimateCosts.EstimateId);

            foreach (var cost in CostsEntered.ToList())
            {
                CustomEstimateCost ExistingCost = new CustomEstimateCost();

                if (cost.ItemName != null)
                {
                    ExistingCost = CustomEstimateCostsInContext
                                   .FirstOrDefault(cec => cec.EstimateId == cost.EstimateId && cec.ItemName.ToUpper() == cost.ItemName.ToUpper());
                }
                else
                {
                    ExistingCost = null;
                }

                if (cost.ItemName == null || cost.UnitOfMeasure == null || cost.CostPerUnit <= 0 ||
                    cost.Quantity == 0 || cost.MarkupPercent <= 0)
                {
                    CostsEntered.Remove(cost);
                    RejectecdEntries.Add(cost);
                }

                if (ExistingCost != null)
                {
                    ExistingCost.Quantity += cost.Quantity;
                    CostsEntered.Remove(cost);
                    UpdatedRecords.Add(ExistingCost);

                    _context.Update(ExistingCost);
                    await _context.SaveChangesAsync();
                }
            }

            foreach (var estimateCost in CostsEntered)
            {
                _context.Add(estimateCost);
                await _context.SaveChangesAsync();
            }

            if (RejectecdEntries.Count > 0 || UpdatedRecords.Count > 0)
            {
                CustomEstimateCostCreateViewModel viewModel = new CustomEstimateCostCreateViewModel {
                    EstimateId      = customEstimateCosts.EstimateId,
                    RejectedEntries = RejectecdEntries,
                    UpdatedRecords  = UpdatedRecords
                };


                ViewData["EstimateId"] = customEstimateCosts.EstimateId;
                return(View("CreateFinish", viewModel));
            }
            return(RedirectToAction("Details", "Estimate", new { id = customEstimateCosts.EstimateId }));
        }