public KeyValuePair <int, bool> AddOrder(Dictionary <string, object> items, ApplicationUser user) { int orderId = -1; bool goodsBackordered = false; using (_db) { // we need a transaction as multiple entities involved using (var _trans = _db.Database.BeginTransaction()) { try { Order order = new Order(); order.UserId = user.Id; order.OrderDate = System.DateTime.Now; // calculate the totals and then add the order row to the table foreach (var key in items.Keys) { ProductViewModel item = JsonConvert.DeserializeObject <ProductViewModel>(Convert.ToString(items[key])); if (item.Qty > 0) { order.OrderAmount += item.Qty * item.MSRP; } } order.OrderAmount *= 1.13M; _db.Orders.Add(order); _db.SaveChanges(); //variable for keeping track of real order total - i.e. the goods sold. decimal goodsSoldTotal = 0; // add each product to the orderitems table foreach (var key in items.Keys) { ProductViewModel item = JsonConvert.DeserializeObject <ProductViewModel>(Convert.ToString(items[key])); Product product = _db.Products.Find(item.Id); if (item.Qty > 0) { //Get how many of the item is currently in the database int currentQtyStock = item.QtyOnHand; OrderLineItem oItem = new OrderLineItem(); oItem.ProductId = item.Id; oItem.OrderId = order.Id; oItem.SellingPrice = item.MSRP; oItem.QtyOrdered = item.Qty; //Check if the qty ordered of this product is bigger or smaller than the current qty on hand if (item.Qty < currentQtyStock) { //Decrease the QtyOnHand in the products table by Qty product.QtyOnHand = currentQtyStock - item.Qty; //Order Item Quantity Sold is Qty oItem.QtySold = item.Qty; //Order Item Qty Back Ordered is 0 oItem.QtyBackOrdered = 0; product.QtyOnBackOrder = item.QtyOnBackOrder; } else { goodsBackordered = true; //QtyOnHand in the products table is 0 product.QtyOnHand = 0; //QtyOnBackOrder for the Products table is QtyOnBackOrder + // Qty - currentQtyStock product.QtyOnBackOrder = item.QtyOnBackOrder + (item.Qty - currentQtyStock); //Order item Quantity Sold is currentQtyStock oItem.QtySold = currentQtyStock; //Order item QtyBackOrdered is Qty - currentQtyStock oItem.QtyBackOrdered = item.Qty - currentQtyStock; } _db.OrderLineItems.Add(oItem); //update products table _db.Products.Update(product); _db.SaveChanges(); //increase goods sold amount goodsSoldTotal += oItem.QtySold * oItem.SellingPrice; } } //if the goodsSoldTotal is different than the original order total, update this if (goodsSoldTotal != order.OrderAmount) { order.OrderAmount = goodsSoldTotal * 1.13M; _db.Orders.Update(order); _db.SaveChanges(); } _trans.Commit(); orderId = order.Id; } catch (Exception ex) { orderId = -1; Console.WriteLine(ex.Message); _trans.Rollback(); } } } return(new KeyValuePair <int, bool>(orderId, goodsBackordered)); }
public int AddTray(Dictionary <string, object> items, ApplicationUser user) { int trayId = -1; using (_db) { // we need a transaction as multiple entities involved using (var _trans = _db.Database.BeginTransaction()) { try { Order tray = new Order(); tray.UserId = user.Id; tray.OrderDate = System.DateTime.Now; tray.OrderAmount = 0; // calculate the totals and then add the tray row to the table foreach (var key in items.Keys) { ProductViewModel item = JsonConvert.DeserializeObject <ProductViewModel>(Convert.ToString(items[key])); if (item.Qty > 0) { tray.OrderAmount += item.Qty; tray.OrderTotal += item.Qty * item.CostPrice; } } _db.Orders.Add(tray); _db.SaveChanges(); // then add each item to the trayitems table foreach (var key in items.Keys) { ProductViewModel item = JsonConvert.DeserializeObject <ProductViewModel>(Convert.ToString(items[key])); if (item.Qty > 0) { OrderLineItem order = new OrderLineItem(); order.ProductId = item.Id; order.OrderId = tray.Id; Product product = _db.Products.First(p => p.Id == item.Id); if (product.QtyOnHand >= item.Qty) { order.QtySold = item.Qty; order.QtyOrdered = item.Qty; order.QtyBackOrdered = 0; product.QtyOnHand -= item.Qty; order.SellingPrice += item.CostPrice * order.QtySold; } else { order.QtySold = product.QtyOnHand; order.QtyOrdered = item.Qty; order.QtyBackOrdered = item.Qty - product.QtyOnHand;; product.QtyOnBackOreder += item.Qty - product.QtyOnHand; product.QtyOnHand = 0; order.SellingPrice += item.CostPrice * order.QtySold; } //if (item.Qty > 0) //{ // OrderLineItem tItem = new OrderLineItem(); // tItem.QtyOrdered = item.Qty; // tItem.OrderId = tray.Id; // tItem.ProductId = item.Id; // tItem.QtySold = item.Qty; // tItem.SellingPrice = item.CostPrice; _db.OrderLineItems.Add(order); _db.SaveChanges(); } } // test trans by uncommenting out these 3 lines //int x = 1; //int y = 0; //x = x / y; _trans.Commit(); trayId = tray.Id; } catch (Exception ex) { trayId = -1; Console.WriteLine(ex.Message); _trans.Rollback(); } } } return(trayId); }