public void AddNewSDPart(POCOSDPart newSDpart) { using (var context = new eBikesContext()) { // checking part invetory on hand if (newSDpart.ServiceDetailID == 0) { throw new Exception("Please select a service detail to add part"); } var existingSD = context.ServiceDetails.Find(newSDpart.ServiceDetailID); if (newSDpart.Quantity == 0) { throw new Exception("The quantity of part must be a positive whole number"); } if (existingSD.Status == "C") { throw new Exception("Selected service detail is closed, can not be edit."); } var part = context.Parts.Find(newSDpart.PartID); if (part == null) { throw new Exception("The partID is not valid."); } if (newSDpart.Quantity > part.QuantityOnHand) { throw new Exception($"Failed PartID: {newSDpart.PartID} only has {part.QuantityOnHand} in inventory."); } if (existingSD.Status == "S") { part.QuantityOnHand -= newSDpart.Quantity; } ServiceDetailPart SDpart = new ServiceDetailPart(); SDpart.PartID = newSDpart.PartID; SDpart.Quantity = (short)newSDpart.Quantity; SDpart.ServiceDetailID = newSDpart.ServiceDetailID; SDpart.SellingPrice = context.Parts.Find(newSDpart.PartID).SellingPrice; context.ServiceDetailParts.Add(SDpart); context.SaveChanges(); } }
public void UpdateSDPart(POCOSDPart newSDpart) { using (var context = new eBikesContext()) { // checking part invetory on hand var existingSDpart = context.ServiceDetailParts.Find(newSDpart.ServiceDetailPartID); var existingSD = context.ServiceDetails.Find(existingSDpart.ServiceDetailID); if (existingSD.Status == "C") { throw new Exception("Service detail is closed, part can not be updated"); } if (newSDpart.Quantity == 0) { throw new Exception("The quantity of part must be a positive whole number"); } var part = context.Parts.Find(newSDpart.PartID); if (newSDpart.Quantity > part.QuantityOnHand) { throw new Exception($"PartID: {newSDpart.PartID} only has Quantity: {part.QuantityOnHand} in inventory."); } if (existingSD.Status == "S") { var existingPart = context.Parts.Find(newSDpart.PartID); var oldSDpart = context.ServiceDetailParts.Find(newSDpart.ServiceDetailPartID); if (newSDpart.Quantity > oldSDpart.Quantity) { existingPart.QuantityOnHand -= newSDpart.Quantity - oldSDpart.Quantity; } if (newSDpart.Quantity < oldSDpart.Quantity) { existingPart.QuantityOnHand += oldSDpart.Quantity - newSDpart.Quantity; } } var NewSDpart = context.ServiceDetailParts.Find(newSDpart.ServiceDetailPartID); NewSDpart.Quantity = (short)newSDpart.Quantity; context.SaveChanges(); } }
public void RemoveSDPart(POCOSDPart info) { using (var context = new eBikesContext()) { var existingSDpart = context.ServiceDetailParts.Find(info.ServiceDetailPartID); var existingSD = context.ServiceDetails.Find(existingSDpart.ServiceDetailID); if (existingSD.Status == "C") { throw new Exception("Service detail is closed, part can not be removed"); } // if the serviceDetail already started then return part back to inventory if (existingSD.Status == "S") { var existingPart = context.Parts.Find(existingSDpart.PartID); existingPart.QuantityOnHand += existingSDpart.Quantity; } context.ServiceDetailParts.Remove(context.ServiceDetailParts.Find(info.ServiceDetailPartID)); context.SaveChanges(); } }