public ResultOperation Remove(int id) { return(SurroundWithTryCatch(() => { string error = string.Empty; ProductEntry entry = DB.ProductEntries.SingleOrDefault(r => r.Id == id); if (entry == null) { error = "Item was not found."; } else { if (entry.SaleItems.Any()) { error = "You cannot remove this product! There are sales related to this item."; } else { DB.ProductEntries.Remove(entry); DB.SaveChanges(); } } return error; } )); }
private void VerifyOtherEntries(ProductEntry entry, PurchaseItem item) { List <ProductEntry> entries = GetProductEntriesWithSamePrice(entry.IdProduct, entry.UnitPrice); int qttAvail = entries.Sum(r => r.QuantityAvailable); if (qttAvail >= item.Qtd) { int qttAux = item.Qtd; foreach (var ent in entries) { if (qttAux == 0) { break; } if (ent.QuantityAvailable <= qttAux) { qttAux -= ent.QuantityAvailable; ent.QuantityAvailable = 0; } else { ent.QuantityAvailable -= qttAux; qttAux = 0; } } } else { errorMessage = $"Product {item.productName} has only {qttAvail} available unities.<br/>"; } }
public ResultOperation Update(InventoryProduct inventoryProduct) { return(SurroundWithTryCatch(() => { string error = string.Empty; ProductEntry entry = DB.ProductEntries.Include("SaleItems") .FirstOrDefault(r => r.Id == inventoryProduct.Id); if (entry == null) { error = "Inventory Item was not found."; } else if (entry.QuantityAvailable == 0 && entry.QuantityBought > inventoryProduct.Qtd) { error = $"This product has sold {entry.QuantityBought } unities. You must specify in quantity bought a number higher than it"; } else { entry.PurchaseDate = inventoryProduct.DatePurchase; entry.UnitPrice = inventoryProduct.Price; entry.QuantityAvailable += (inventoryProduct.Qtd - entry.QuantityBought); entry.QuantityBought = inventoryProduct.Qtd; entry.UnitPriceAcquisition = inventoryProduct.PriceAcquisition; DB.SaveChanges(); } return error; })); }
public ResultOperation SavePurchase(DateTime date, PurchaseItem[] items) { if (items.Any(r => r.Qtd <= 0)) { return(new ResultOperation() { Success = false, Message = "There are invalid quantities in this request. Please review before saving purchase!" }); } using (var transaction = DB.Database.BeginTransaction(System.Data.IsolationLevel.RepeatableRead)) { try { // Creating the header of the sale Sale sale = new Sale() { Date = date }; // Items that will be worked through loop SaleItem saleItem = null; ProductEntry entry = null; // For each item that came from the client foreach (var item in items) { //Looking for the specific entry and not the product itself that is in a different table entry = DB.ProductEntries.FirstOrDefault(r => r.Id == item.IdProduct); //Something must be wrong, the entry was not found if (entry == null) { errorMessage += $"Product {item.productName} was not found!<br/>"; break; } else { // Something happened and the quantity requested is not available if (entry.QuantityAvailable < item.Qtd) { VerifyOtherEntries(entry, item); } else { // Here is where the transaction is useful // if someone else is changing the QuantityAvailable, // we need to avoid that this transaction override the update in a concurrent environment entry.QuantityAvailable -= item.Qtd; } if (string.IsNullOrEmpty(errorMessage)) { // Creating one item for the sale saleItem = new SaleItem() { ProductEntry = entry, UnitPrice = entry.UnitPrice, Quantity = item.Qtd }; // You can do this, or just set the property Sale from saleItem with 'sale' sale.SaleItems.Add(saleItem); } } } DB.Sales.Add(sale); } catch (Exception ex) { errorMessage = ex.Message; } if (string.IsNullOrEmpty(errorMessage)) { try { // You must call save changes first DB.SaveChanges(); transaction.Commit(); } catch (Exception) { errorMessage = "The server is busy! Try again, if the problem persists, get in touch with the System Admin"; } } else { transaction.Rollback(); } } return(new ResultOperation() { Success = errorMessage == string.Empty, Message = errorMessage }); }