/// <summary> /// if there is sufficient quantity in the store, reduce the store quantity /// </summary> /// <param name="store"></param> /// <param name="ProductID"></param> /// <param name="Quantity"></param> /// <returns></returns> public BusinessObject.Store PurchaseItem(BusinessObject.Store store, int ProductID, int Quantity) { try { // TODO pass this to the BO //get the item from the store var siRepo = new StoreInventoryRepository(); var item = siRepo.GetStoreInventoryByStoreIdAndProductId(StoreID: store._poco.StoreID, ProductID: ProductID); // validate that there is enough stock if (item.StockLevel >= Quantity) { int newStock = item.StockLevel - Quantity; siRepo.UpdateStoreInventory(StoreID: store._poco.StoreID, ProductID: ProductID, Quantity: newStock); // refresh the store's inventory // this is the stores internal private method, so this logic has to move into the store } else { throw new Exception("Insufficient stock to process this request"); } return(store); } catch (Exception) { throw; } }
/// <summary> /// if valid, transfers from the owners stock to the relavant store /// </summary> /// <param name="stockRequestID"></param> public void ProcessStockRequest(int stockRequestID) { try { var StockRequest = new StockRequestRepository().GetStockRequestById(stockRequestID); var product = _ownerInventory.Find(item => item.ProductID == StockRequest.ProductID); if (product != null) { // validate if (product.StockLevel > StockRequest.Quantity && StockRequest.StockAvailability) { product.StockLevel = product.StockLevel - StockRequest.Quantity; product = new OwnerInventoryRepository().UpdateOwnerInventory(product.ProductID, product.StockLevel); //add store stock var siRepo = new StoreInventoryRepository(); var storeInventory = siRepo.GetStoreInventoryByStoreIdAndProductId(StockRequest.StoreID, product.ProductID); storeInventory.StockLevel = storeInventory.StockLevel + StockRequest.Quantity; siRepo.UpdateStoreInventory(product.ProductID, StockRequest.StoreID, storeInventory.StockLevel); //delete stock request new StockRequestRepository().DeleteStockRequest(StockRequest.StockRequestID); } else { WidgetError.DisplayError("Insufficient stock to process this request"); } } else { WidgetError.DisplayError("Invalid product chosen"); } } catch (Exception) { throw; } }