public FreeMarketObject UpdateQuantities(List <OrderDetail> changedItems) { FreeMarketObject res = new FreeMarketObject(); OrderDetail temp = new OrderDetail(); if (changedItems != null && changedItems.Count > 0) { SetQuantityOnHand(changedItems); foreach (OrderDetail detail in changedItems) { temp = Body.OrderDetails .Where(c => c.ProductNumber == detail.ProductNumber && c.SupplierNumber == detail.SupplierNumber && c.SizeType == detail.SizeType) .FirstOrDefault(); if (temp != null) { int oldQuantity = temp.Quantity; if (oldQuantity > detail.Quantity) { int difference = oldQuantity - detail.Quantity; FreeStock(detail.ProductNumber, detail.SupplierNumber, (int)detail.CustodianNumber, difference, detail.SizeType); } else { int difference = detail.Quantity - oldQuantity; if (detail.QuantityOnHand > difference) { ReserveStock(detail.ProductNumber, detail.SupplierNumber, (int)detail.CustodianNumber, difference, detail.SizeType); } else { res.Result = FreeMarketResult.Failure; res.Message += string.Format("\n {0} is out of stock. Please try a smaller quantity.", detail.ProductDescription); continue; } } Body.OrderDetails .Where(c => c.ProductNumber == detail.ProductNumber && c.SupplierNumber == detail.SupplierNumber && c.SizeType == detail.SizeType) .FirstOrDefault() .Quantity = detail.Quantity; } } } // Keep the OrderTotal in sync UpdateTotal(); return(res); }
public FreeMarketObject AddItemFromProduct(int productNumber, int supplierNumber, int quantity, int custodianNumber, int sizeType) { // Validate if (productNumber == 0 || supplierNumber == 0) { return new FreeMarketObject { Result = FreeMarketResult.Failure, Argument = null, Message = "No products could be found." } } ; FreeMarketObject res = new FreeMarketObject(); using (FreeMarketEntities db = new FreeMarketEntities()) { // Check whether the item already exists OrderDetail existingItem = Body.OrderDetails .Where(c => c.ProductNumber == productNumber && c.SupplierNumber == supplierNumber && c.SizeType == sizeType) .FirstOrDefault(); if (existingItem != null) { // Update the existing item existingItem.Update(quantity); // Setup return object var productInfo = db.GetProduct(productNumber, supplierNumber, sizeType).FirstOrDefault(); if (productInfo == null) { return new FreeMarketObject { Result = FreeMarketResult.Failure, Argument = null, Message = "Product, Supplier or Price does not exist." } } ; res.Result = FreeMarketResult.Success; res.Message = string.Format("{0} ({1}) has been added to your cart.", productInfo.Description, quantity); } else { // A new OrderDetail must be created var productInfo = db.GetProduct(productNumber, supplierNumber, sizeType) .FirstOrDefault(); if (productInfo == null) { return new FreeMarketObject { Result = FreeMarketResult.Failure, Argument = null, Message = "No products could be found." } } ; string status = "Unconfirmed"; // Add a small image for the CartBody int imageNumber = db.ProductPictures .Where(c => c.ProductNumber == productInfo.ProductNumber && c.Dimensions == PictureSize.Small.ToString()) .Select(c => c.PictureNumber) .FirstOrDefault(); // Add the new item to the Session variable Body.OrderDetails.Add( new OrderDetail() { CourierName = null, CustodianNumber = custodianNumber, OrderItemStatus = status, OrderItemValue = productInfo.PricePerUnit * quantity, OrderNumber = Order.OrderNumber, PaidCourier = null, PaidSupplier = null, PayCourier = null, PaySupplier = null, Price = productInfo.PricePerUnit, ProductNumber = productInfo.ProductNumber, ProductDescription = productInfo.Description, ProductDepartment = productInfo.DepartmentName, Quantity = quantity, Settled = null, SupplierNumber = productInfo.SupplierNumberID, SupplierName = productInfo.SupplierName, SizeType = sizeType, MainImageNumber = imageNumber }); res.Result = FreeMarketResult.Success; res.Message = string.Format("{0} ({1}) has been added to your cart.", productInfo.Description, quantity); } } // Keep the OrderTotal in sync UpdateTotal(); return(res); }
public static FreeMarketObject CreateNewCashOrder(CashOrderViewModel model) { FreeMarketObject result = new FreeMarketObject { Result = FreeMarketResult.NoResult, Argument = null, Message = null }; using (FreeMarketEntities db = new FreeMarketEntities()) { CashCustomer customer = db.CashCustomers.Find(model.Order.CashCustomerId); if (customer == null) { customer = new CashCustomer { DeliveryAddress = model.Order.CustomerDeliveryAddress, Email = model.Order.CustomerEmail, Name = model.Order.CustomerName, PhoneNumber = model.Order.CustomerPhone }; db.CashCustomers.Add(customer); db.SaveChanges(); } else { customer.DeliveryAddress = model.Order.CustomerDeliveryAddress; customer.Email = model.Order.CustomerEmail; customer.Name = model.Order.CustomerName; customer.PhoneNumber = model.Order.CustomerPhone; db.Entry(customer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } CashOrder order = new CashOrder { CashCustomerId = customer.Id, DatePlaced = DateTime.Now, Status = "Completed", Total = 0 }; db.CashOrders.Add(order); db.SaveChanges(); foreach (Product p in model.Products.Products) { if (p.CashQuantity > 0) { decimal price = decimal.Parse(p.SelectedPrice); CashOrderDetail detail = new CashOrderDetail { CashOrderId = order.OrderId, ProductNumber = p.ProductNumber, SupplierNumber = p.SupplierNumber, Quantity = p.CashQuantity, Price = price, OrderItemTotal = price * p.CashQuantity, CustodianNumber = model.SelectedCustodian }; db.CashOrderDetails.Add(detail); db.SaveChanges(); order.Total += detail.OrderItemTotal; db.Entry(order).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); ProductCustodian custodian = db.ProductCustodians .Where(c => c.CustodianNumber == model.SelectedCustodian && c.ProductNumber == p.ProductNumber && c.SupplierNumber == p.SupplierNumber) .FirstOrDefault(); custodian.QuantityOnHand -= p.CashQuantity; db.Entry(custodian).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } db.SaveChanges(); if (customer != null && order != null && db.CashOrderDetails.Any(c => c.CashOrderId == order.OrderId)) { result.Result = FreeMarketResult.Success; } else { result.Result = FreeMarketResult.Failure; } } return(result); }
public static FreeMarketObject ModifyOrder(CashOrderViewModel model) { FreeMarketObject result = new FreeMarketObject { Result = FreeMarketResult.NoResult, Argument = null, Message = null }; using (FreeMarketEntities db = new FreeMarketEntities()) { CashCustomer customer = db.CashCustomers.Find(model.Order.CashCustomerId); customer.DeliveryAddress = model.Order.CustomerDeliveryAddress; customer.Email = model.Order.CustomerEmail; customer.Name = model.Order.CustomerName; customer.PhoneNumber = model.Order.CustomerPhone; db.Entry(customer).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); CashOrder order = db.CashOrders.Find(model.Order.OrderId); List <GetCashOrderDetails_Result> orderDetails = db.GetCashOrderDetails(order.OrderId).ToList(); foreach (Product p in model.Products.Products) { if (p.CashQuantity > 0) { decimal price = decimal.Parse(p.SelectedPrice); if (orderDetails.Any(c => c.ProductNumber == p.ProductNumber && c.SupplierNumber == p.SupplierNumber)) { CashOrderDetail existingDetail = db.CashOrderDetails .Where(c => c.CashOrderId == order.OrderId && c.ProductNumber == p.ProductNumber && c.SupplierNumber == p.SupplierNumber) .FirstOrDefault(); existingDetail.Price = price; existingDetail.OrderItemTotal = price * p.CashQuantity; if (existingDetail.Quantity > p.CashQuantity) { int stock = existingDetail.Quantity - p.CashQuantity; AddStockToCustodian(order.OrderId, p.ProductNumber, p.SupplierNumber, model.SelectedCustodian, stock); } else { int stock = p.CashQuantity - existingDetail.Quantity; RemoveStockFromCustodian(order.OrderId, p.ProductNumber, p.SupplierNumber, model.SelectedCustodian, stock); } existingDetail.Quantity = p.CashQuantity; db.Entry(existingDetail).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } else { CashOrderDetail detail = new CashOrderDetail { CashOrderId = order.OrderId, ProductNumber = p.ProductNumber, SupplierNumber = p.SupplierNumber, Quantity = p.CashQuantity, Price = price, OrderItemTotal = price * p.CashQuantity, CustodianNumber = model.SelectedCustodian }; db.CashOrderDetails.Add(detail); db.SaveChanges(); RemoveStockFromCustodian(order.OrderId, p.ProductNumber, p.SupplierNumber, model.SelectedCustodian, p.CashQuantity); } } else { CashOrderDetail toRemove = db.CashOrderDetails .Where(c => c.CashOrderId == order.OrderId && c.ProductNumber == p.ProductNumber && c.SupplierNumber == p.SupplierNumber) .FirstOrDefault(); if (toRemove != null) { db.CashOrderDetails.Remove(toRemove); AddStockToCustodian(order.OrderId, p.ProductNumber, p.SupplierNumber, model.SelectedCustodian, toRemove.Quantity); } } } db.SaveChanges(); List <GetCashOrderDetails_Result> details = db.GetCashOrderDetails(order.OrderId).ToList(); order.Total = details.Sum(c => c.OrderItemTotal); order.DatePlaced = DateTime.Now; db.Entry(order).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); if (customer != null && order != null && db.CashOrderDetails.Any(c => c.CashOrderId == order.OrderId)) { result.Result = FreeMarketResult.Success; } else { result.Result = FreeMarketResult.Failure; } } return(result); }