public ViewProductViewModel(int productNumber, int supplierNumber, int quantityRequested, int orderNumber = 0) { // Validate if (productNumber == 0 || supplierNumber == 0 || quantityRequested < 1) { InitializeDefault(); } using (FreeMarketEntities db = new FreeMarketEntities()) { decimal minValue = db.ProductSuppliers .Where(c => c.ProductNumber == productNumber && c.SupplierNumber == supplierNumber) .Min(c => c.PricePerUnit); // Validate ProductSupplier productSupplierTemp = db.ProductSuppliers .Where(c => c.ProductNumber == productNumber && c.SupplierNumber == supplierNumber && c.PricePerUnit == minValue) .FirstOrDefault(); if (productSupplierTemp == null) { InitializeDefault(); return; } Quantity = quantityRequested; ProductNumber = productNumber; SupplierNumber = supplierNumber; ProductCustodian custodian = ShoppingCart.GetStockAvailable(productNumber, supplierNumber, quantityRequested, productSupplierTemp.SizeType); if (custodian != null) { CannotDeliver = false; CustodianQuantityOnHand = custodian.QuantityOnHand; CustodianNumber = custodian.CustodianNumber; } else { CannotDeliver = true; CustodianQuantityOnHand = 0; CustodianNumber = 0; } SetInstances(productNumber, supplierNumber); ReviewPageSize = 4; ProductSizes = db.GetProductSizes(productNumber, supplierNumber) .Select(c => new SelectListItem { Text = c.Description, Value = c.SizeId.ToString(), Selected = productSupplierTemp.SizeType == c.SizeId ? true : false }) .ToList(); } }
public static ProductCollection GetOneProductFromEachCategory() { ProductCollection products = new ProductCollection(); List <Department> departments = new List <Department>(); using (FreeMarketEntities db = new FreeMarketEntities()) { departments = db.Departments.ToList(); foreach (Department dep in departments) { Product prod = db.Products .Where(c => c.DepartmentNumber == dep.DepartmentNumber) .FirstOrDefault(); if (prod != null) { ProductSupplier supplier = db.ProductSuppliers .Where(c => c.ProductNumber == prod.ProductNumber) .FirstOrDefault(); if (supplier != null) { Product prodImage = Product.GetProduct(supplier.ProductNumber, supplier.SupplierNumber, supplier.SizeType); if (prodImage != null) { products.Products.Add(prodImage); } } } } return(products); } }
public static void SaveProduct(Product product) { Product productDb = new Product(); List <ProductSupplier> productSupplierDb = new List <ProductSupplier>(); using (FreeMarketEntities db = new FreeMarketEntities()) { productDb = db.Products.Find(product.ProductNumber); if (productDb != null) { productDb.Activated = product.Activated; productDb.DateModified = DateTime.Now; productDb.DepartmentNumber = int.Parse(product.SelectedDepartment); productDb.Description = product.Description; productDb.LongDescription = product.LongDescription; productDb.Size = product.Size; productDb.Weight = product.Weight; productDb.IsVirtual = product.IsVirtual; db.Entry(productDb).State = EntityState.Modified; db.SaveChanges(); } // For each productsize entry on the form foreach (ProductSize x in product.SizeVariations) { // Query the productsupplier table to get a record ProductSupplier temp = db.ProductSuppliers .Where(c => c.ProductNumber == product.ProductNumber && c.SupplierNumber == product.SupplierNumber && c.SizeType == x.SizeId) .FirstOrDefault(); // If a record does not exist if (temp == null) { // Check the activated field of the form and add a new record if it is true if (x.Activated == true) { ProductSupplier prodSup = new ProductSupplier { PricePerUnit = x.PricePerUnit, ProductNumber = product.ProductNumber, SupplierNumber = product.SupplierNumber, SizeType = x.SizeId, }; db.ProductSuppliers.Add(prodSup); db.SaveChanges(); } } // If a record does exist else { // Check the activated field and remove it if it is false if (x.Activated == false) { db.ProductSuppliers.Remove(temp); db.SaveChanges(); } else { // If the prices differ, record it in the pricehistory table if (x.PricePerUnit != temp.PricePerUnit) { PriceHistory history = new PriceHistory() { OldPrice = temp.PricePerUnit, NewPrice = x.PricePerUnit, ProductNumber = product.ProductNumber, SupplierNumber = product.SupplierNumber, Date = DateTime.Now, Type = x.SizeId.ToString() }; db.PriceHistories.Add(history); // Update the record temp.PricePerUnit = x.PricePerUnit; db.Entry(temp).State = EntityState.Modified; db.SaveChanges(); } } } } } }
public static void CreateNewProduct(Product product) { try { product.DateAdded = DateTime.Now; product.DateModified = DateTime.Now; product.DepartmentNumber = int.Parse(product.SelectedDepartment); } catch { return; } using (FreeMarketEntities db = new FreeMarketEntities()) { db.Products.Add(product); db.SaveChanges(); foreach (ProductSize item in product.SizeVariations) { if (item.PricePerUnit > 0 && item.Activated == true) { ProductSupplier productSupplierDb = new ProductSupplier() { ProductNumber = product.ProductNumber, SupplierNumber = int.Parse(product.SelectedSupplier), PricePerUnit = item.PricePerUnit, SpecialPricePerUnit = 0, RetailPricePerUnit = 0, SizeType = item.SizeId }; db.ProductSuppliers.Add(productSupplierDb); db.SaveChanges(); } } Custodian custodian = db.Custodians.Find(product.SelectedCustodianNumber); if (custodian == null) { return; } try { foreach (ProductSize item in product.SizeVariations) { if (item.PricePerUnit > 0 && item.Activated == true) { ProductCustodian productCustodianDb = new ProductCustodian() { AmountLastIncreasedBySupplier = null, CustodianNumber = product.SelectedCustodianNumber, DateLastIncreasedBySupplier = null, ProductNumber = product.ProductNumber, SupplierNumber = int.Parse(product.SelectedSupplier), QuantityOnHand = 0, StockReservedForOrders = 0, SizeType = item.SizeId }; db.ProductCustodians.Add(productCustodianDb); db.SaveChanges(); } } } catch (Exception e) { ExceptionLogging.LogException(e); } } }