public ActionResult AddQuoteItem(QuoteItem item) { Material material; List <WasteFactor> wasteFactors = new List <WasteFactor>(); using (var mm = new MaterialsManager()) { material = mm.SingleMaterial(item.MaterialID.GetValueOrDefault()); } using (var qm = new QuoteManager()) { var quote = qm.Single(item.QuoteID); using (var wfm = new WasteFactorManager()) { wasteFactors = wfm.ByCompany(quote.CompanyID.GetValueOrDefault()).ToList(); } decimal wasteFactor = wasteFactors.Any( wf => wf.ProductID == item.ProductID && wf.ProductLineID == item.ProductLineID) ? wasteFactors.First( wf => wf.ProductID == item.ProductID && wf.ProductLineID == item.ProductLineID) .WasteFactor1.GetValueOrDefault() : 0M; // calculate the amount and save switch (item.Measurement) { case (Measurement.SquareFeet): if (item.SquareFt.GetValueOrDefault() == 0) { item.SquareFt = item.Height * item.Width; } var pieceSqFt = (material.Height - material.Overlap.GetValueOrDefault()) * (1M / 12M) * material.Width; var pieces = Math.Ceiling((decimal)(item.SquareFt.GetValueOrDefault() * (1M + wasteFactor) / pieceSqFt)); item.Amount = pieces * material.UnitCost; break; case (Measurement.LinearFeet): item.Amount = item.LinearFt * (1M + wasteFactor) * material.UnitCost / material.Width; break; case (Measurement.Constant): item.Amount = item.Dollars; break; } item = qm.CreateItem(item); } return(RedirectToAction("Edit", new { id = item.QuoteID })); }