// GET: CustomProjectCosts/Create public IActionResult Create(int id) { DateTime today = DateTime.UtcNow; ProjectController controller = new ProjectController(_context, _config); CustomCostCreateViewModel viewModel = new CustomCostCreateViewModel { ProjectId = id, Project = controller.GetProjectById(id), CustomCosts = new List <CustomProjectCost>(), }; CustomProjectCost customCost = new CustomProjectCost { ProjectId = id, ItemName = "", DateUsed = today, UnitOfMeasure = "", Category = "Custom", Quantity = 0 }; viewModel.CustomCosts.Add(customCost); ViewData["ProjectId"] = id; return(View(viewModel)); }
public async Task <IActionResult> Create(CustomCostCreateViewModel customProjectCosts) { List <CustomProjectCost> CustomProjectCostsInContext = await _context.CustomProjectCost .Where(cpc => cpc.ProjectId == customProjectCosts.ProjectId).ToListAsync(); List <CustomProjectCost> CostsEntered = (customProjectCosts.CustomCosts?.Count > 0) ? customProjectCosts.CustomCosts : customProjectCosts.RejectedEntries; List <CustomProjectCost> RejectecdEntries = new List <CustomProjectCost>(); List <CustomProjectCost> UpdatedRecords = new List <CustomProjectCost>(); Project Project = await _context.Project.FirstOrDefaultAsync(p => p.Id == customProjectCosts.ProjectId); DateTime CheckStartDate = Project.StartDate; foreach (var cost in CostsEntered.ToList()) { CustomProjectCost ExistingCost = new CustomProjectCost(); if (cost.ItemName != null) { ExistingCost = CustomProjectCostsInContext .FirstOrDefault(cpc => cpc.ProjectId == cost.ProjectId && cpc.ItemName.ToUpper() == cost.ItemName.ToUpper() && cpc.DateUsed == cost?.DateUsed); } else { ExistingCost = null; } if (cost.ItemName == null || cost.UnitOfMeasure == null || cost.CostPerUnit <= 0 || cost.Quantity == 0 || cost.DateUsed < CheckStartDate) { 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 projectCost in CostsEntered) { _context.Add(projectCost); await _context.SaveChangesAsync(); } if (RejectecdEntries.Count > 0 || UpdatedRecords.Count > 0) { CustomCostCreateViewModel viewModel = new CustomCostCreateViewModel { ProjectId = customProjectCosts.ProjectId, RejectedEntries = RejectecdEntries, UpdatedRecords = UpdatedRecords }; ViewData["ProjectId"] = customProjectCosts.ProjectId; return(View("CreateFinish", viewModel)); } else { return(RedirectToAction("Details", "Project", new { id = customProjectCosts.ProjectId })); } }