// GET: CostPerUnits/Create public async Task <IActionResult> Create(int id) { MaintainCostPerUnitRecords(_context); var today = DateTime.Now.ToString("yyyy-MM-dd"); CostPerUnit Cost = new CostPerUnit { CostItem = await _context.CostItem.FindAsync(id) }; ViewData["DefaultDate"] = today; ViewData["CostItemId"] = id; return(View(Cost)); }
/// <summary> /// Provides the description of the model settings for summary (GetFullSummary) /// </summary> /// <param name="formatForParentControl">Use full verbose description</param> /// <returns></returns> public override string ModelSummary(bool formatForParentControl) { string html = ""; if (CostPerUnit > 0) { html += "<div class=\"activityentry\">"; html += "<span class=\"setvalue\">" + CostPerUnit.ToString("#,##0.##") + "</span> days from "; html += "</div>"; } else { html += "<div class=\"errorlink\">"; html += "Invalid transmutation cost. No days labour per unit provided."; html += "</div>"; } return html; }
public async Task <IActionResult> Create(int id, CostPerUnit costPerUnit) { costPerUnit.CostItemId = id; costPerUnit.Id = 0; DateTime Today = DateTime.UtcNow; CostPerUnit CuurentCostPerUnit = _context.CostPerUnit.Where(cpu => cpu.CostItemId == costPerUnit.CostItemId) .FirstOrDefault(cpu => cpu.EndDate == null); ModelState.Remove("CostItemId"); if (CuurentCostPerUnit != null && costPerUnit.EndDate == null) { CuurentCostPerUnit.EndDate = costPerUnit.StartDate.AddDays(-1); if (ModelState.IsValid) { _context.Update(CuurentCostPerUnit); await _context.SaveChangesAsync(); _context.Add(costPerUnit); await _context.SaveChangesAsync(); MaintainCostPerUnitRecords(_context); return(RedirectToAction("Details", "CostItem", new { id = costPerUnit.CostItemId })); } ViewData["CostItemId"] = id; return(View(costPerUnit)); } //else if (CuurentCostPerUnit != null && costPerUnit.EndDate != null) { //} if (ModelState.IsValid) { _context.Add(costPerUnit); await _context.SaveChangesAsync(); MaintainCostPerUnitRecords(_context); return(RedirectToAction("Details", "CostItem", new { id = costPerUnit.CostItemId })); } ViewData["CostItemId"] = id; return(View(costPerUnit)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,StartDate,EndDate,Cost,CostItemId")] CostPerUnit costPerUnit) { if (id != costPerUnit.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(costPerUnit); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CostPerUnitExists(costPerUnit.Id)) { return(NotFound()); } throw; } return(RedirectToAction("Details", "CostItem", new { id = costPerUnit.CostItemId })); } ViewData["CostItemId"] = costPerUnit.CostItemId; return(View(costPerUnit)); }
public async Task <IActionResult> Create(ProjectCostCreateViewModel projectCosts) { List <ProjectCost> ProjectCostsInContext = await _context.ProjectCost .Where(pc => pc.ProjectId == projectCosts.ProjectId).ToListAsync(); List <ProjectCost> CostsEntered = (projectCosts.Costs?.Count > 0) ? projectCosts.Costs : projectCosts.RejectedEntries; List <ProjectCost> RejectecdEntries = new List <ProjectCost>(); List <ProjectCost> UpdatedRecords = new List <ProjectCost>(); Project Project = await _context.Project.FirstOrDefaultAsync(p => p.Id == projectCosts.ProjectId); DateTime CheckDate = Project.StartDate; foreach (var cost in CostsEntered.ToList()) { ProjectCost ExistingCost = ProjectCostsInContext .FirstOrDefault(pc => pc.ProjectId == cost.ProjectId && pc.CostItemId == cost.CostItemId && pc.DateUsed == cost.DateUsed); if (cost.DateUsed < CheckDate || cost.ProjectId == 0 || cost.CostItemId == 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 projectCost in CostsEntered) { CostPerUnit CuurentCostPerUnit = _context.CostPerUnit .Where(cpu => cpu.CostItemId == projectCost.CostItemId) .FirstOrDefault(cpu => cpu.EndDate == null); if (projectCost.DateUsed < CuurentCostPerUnit.StartDate) { CostPerUnit CostPerUnitCorrectDate = _context.CostPerUnit .Where(cpu => cpu.CostItemId == projectCost.CostItemId) .FirstOrDefault(cpu => projectCost.DateUsed <= cpu.EndDate && projectCost.DateUsed >= cpu.StartDate); projectCost.CostPerUnitId = CostPerUnitCorrectDate.Id; } else { projectCost.CostPerUnitId = CuurentCostPerUnit.Id; } _context.Add(projectCost); await _context.SaveChangesAsync(); } if (RejectecdEntries.Count > 0 || UpdatedRecords.Count > 0) { if (RejectecdEntries.Count > 0) { foreach (var item in RejectecdEntries) { item.CostItem = _context.CostItem.FirstOrDefault(ci => ci.Id == item.CostItemId); item.CostPerUnit = _context.CostPerUnit.FirstOrDefault(cpu => cpu.Id == item.CostPerUnitId); } } if (UpdatedRecords.Count > 0) { foreach (var item in UpdatedRecords) { item.CostItem = _context.CostItem.FirstOrDefault(ci => ci.Id == item.CostItemId); item.CostPerUnit = _context.CostPerUnit.FirstOrDefault(cpu => cpu.Id == item.CostPerUnitId); } } ProjectCostCreateViewModel viewModel = new ProjectCostCreateViewModel { ProjectId = projectCosts.ProjectId, CostItems = new List <CostItem>(), RejectedEntries = RejectecdEntries, UpdatedRecords = UpdatedRecords }; viewModel.CostItems = _context.CostItem.Include(ci => ci.UnitOfMeasure).OrderBy(ci => ci.ItemName).ToList(); return(View("CreateFinish", viewModel)); } else { return(RedirectToAction("Details", "Project", new { id = projectCosts.ProjectId })); } }