/// <summary> /// Add Sale /// </summary> /// <param name="sake">Sale object</param> /// <returns>Result object</returns> public BLResult <Sale> AddSale(Sale sale) { DBResult <Inventory> inventoryResult; DBResult <Sale> saleResult; DBResult <Movie> movieResult; BLResult <Sale> result = new BLResult <Sale>(); // Validating input data if (sale == null) { result.Message = "Invalid data"; return(result); } // Validating inventory inventoryResult = inventoryDataAccess.GetInventoryById(sale.IdInventory); if (!inventoryResult.Success) { result.Message = "Invalid inventory"; return(result); } // Getting current sale price movieResult = movieDataAccess.GetMovieById(inventoryResult.Item.IdMovie); if (!movieResult.Success) { result.Message = $"Error getting price from Movie: {movieResult.Message}"; return(result); } if (inventoryResult.Item.IsNew == false || inventoryResult.Item.IsAvailable == false) { result.Message = "Inventory not found or not available"; return(result); } sale.SalePrice = movieResult.Item.SalePrice; sale.SaleDate = DateTime.Now; // Adding sale saleResult = saleDataAccess.CreateSale(sale); if (saleResult.Success) { result.Success = true; result.Item = sale; // If the operation is successful the item will no longer be available inventoryResult = inventoryDataAccess.SetAvailability(sale.IdInventory, false); if (!inventoryResult.Success) { result.Message = inventoryResult.Message; } result.Success = true; } else { result.Message = saleResult.Message; } return(result); }
public int CreateSale(string customerName, int customerId, string productName, int productId, string salesDate, int quantity, double unitPrice, int availableQuantity) { int newQuantity = 0; double totalPrice = unitPrice * quantity; if (availableQuantity >= quantity) { Sale sale = new Sale() { CustomerName = customerName, CustomerId = customerId, ProductName = productName, ProductId = productId, SalesDate = salesDate, Quantity = quantity, UnitPrice = unitPrice, TotalPrice = totalPrice }; newQuantity = availableQuantity - quantity; ProductDataAccess productDataAccess = new ProductDataAccess(); productDataAccess.UpdateQuantity(newQuantity, productId); return(saleDataAccess.CreateSale(sale)); } else { return(0); } }