/// <summary> /// Update certain receipt's fields by a given receipt id. /// </summary> /// <param name="receiptId">Give the id of the receipt to update.</param> public void Update(int receiptId) { // Find the needed receipt. var receiptToUpdate = this.eazyCartContext.Receipts.FirstOrDefault(x => x.Id == receiptId); var allProductReceipts = this.eazyCartContext .ProductsReceipts .Where(x => x.ReceiptId == receiptId) .ToList(); if (allProductReceipts.Count() == 0) { throw new ArgumentException("No products in this receipt"); } //Update Product Quantities decimal grandTotal = 0; foreach (var productReceipt in allProductReceipts) { var product = this.eazyCartContext .Products.FirstOrDefault(x => x.Code == productReceipt.ProductCode); product.Quantity -= productReceipt.Quantity; grandTotal += (product.SellingPrice * productReceipt.Quantity) * (1 - 0.01M * (decimal)productReceipt.DiscountPercentage); } receiptToUpdate.TimeOfPurchase = DateTime.Now; receiptToUpdate.GrandTotal = grandTotal; eazyCartContext.SaveChanges(); }
/// <summary> /// Update certain supplier's fields. /// </summary> /// <param name="supplierName">The new name of the supplier.</param> /// <param name="supplierId">Id of supplier to update.</param> /// <param name="countryName">The new country name of the supplier.</param> /// <param name="cityName">The new city name of the supplier.</param> public void Update(string supplierName, int supplierId, string countryName, string cityName) { List <City> allCitiesWithGivenName = this.eazyCartContext .Cities .Where(x => x.Name == cityName) .ToList(); // Find the city and the country of the selected supplier. Country country; City city; try { country = this.eazyCartContext.Countries.First(x => x.Name == countryName); city = allCitiesWithGivenName.First(x => x.CountryId == country.Id); } catch { throw new ArgumentException("No such country/city exists."); } var supplierToUpdate = this.eazyCartContext .Suppliers.FirstOrDefault(x => x.Id == supplierId); supplierToUpdate.Name = supplierName; supplierToUpdate.CityId = city.Id; eazyCartContext.SaveChanges(); }
/// <summary> /// Update a certain product by providing information about /// each of product's fields. /// </summary> /// <param name="productCode">Give the product's code to update.</param> /// <param name="productBarcode">Give the product's barcode to update.</param> /// <param name="categoryName">Give the product's category to update.</param> /// <param name="productName">Give the product's name to update.</param> /// <param name="quantity">Give the product's quantity to update.</param> /// <param name="supplierName">Give the product's supplier name to update.</param> /// <param name="deliveryPrice">Give the product's delivery price to update.</param> /// <param name="sellingPrice">Give the product's selling price to update.</param> /// <param name="unitName">Give the product's unit name to update.</param> public void Update(string productCode, string productBarcode, string categoryName, string productName, decimal quantity, string supplierName, decimal deliveryPrice, decimal sellingPrice, string unitName) { var unit = this.eazyCartContext.Units.FirstOrDefault(x => x.Name == unitName); // Validation for quanity. if (quantity != Math.Floor(quantity) && unit.Id == 1) { throw new ArgumentException("Quantity must be a whole number."); } if (quantity <= 0) { throw new ArgumentException("Quantity must be positive."); } Category category; Supplier supplier; try { category = this.eazyCartContext.Categories.First(x => x.Name == categoryName); supplier = this.eazyCartContext.Suppliers.First(x => x.Name == supplierName); } catch { throw new ArgumentException("Invalid information about category/supplier."); } var productToUpdate = this.eazyCartContext.Products.FirstOrDefault(x => x.Code == productCode); productToUpdate.Barcode = productBarcode; productToUpdate.CategoryId = category.Id; productToUpdate.Name = productName; productToUpdate.Quantity = quantity; productToUpdate.SupplierId = supplier.Id; productToUpdate.DeliveryPrice = deliveryPrice; productToUpdate.SellingPrice = sellingPrice; productToUpdate.UnitId = unit.Id; eazyCartContext.SaveChanges(); }