public ActionResult CompleteOrder() { if (Session["customerRecord"] != null && Session["orderRecord"] != null) { WarehouseDBEntities wdb = new WarehouseDBEntities(); Customer customer = (Customer)Session["customerRecord"]; List <Item> items = (List <Item>)Session["orderRecord"]; int customerId; try { customerId = wdb.Customers.Max(i => i.id) + 1; } catch (Exception ex) { customerId = 1; } customer.id = customerId; int warehouseId = Convert.ToInt32(customer.selectedWarehouse); int orderId; try { orderId = wdb.Orders.Max(i => i.orderId) + 1; } catch (Exception ex) { orderId = 1; } wdb.Customers.Add(customer); wdb.SaveChanges(); wdb.Dispose(); wdb = new WarehouseDBEntities(); Order order = new Order(); order.orderId = orderId; order.customerId = customerId; order.orderDate = System.DateTime.Now.Date; order.orderStatus = "Unseen"; order.warehouseId = warehouseId; order.totalOrderQuanitity = 0; foreach (Item item in items) { order.totalOrderQuanitity = order.totalOrderQuanitity + Convert.ToInt32(item.Manufacturer); } wdb.Orders.Add(order); wdb.SaveChanges(); wdb.Dispose(); wdb = new WarehouseDBEntities(); List <item_Order> item_OrderList = new List <item_Order>(); foreach (Item item in items) { item_Order itemOrder = new item_Order(); itemOrder.itemId = item.id; itemOrder.orderId = orderId; itemOrder.quantity = Convert.ToInt32(item.Manufacturer); item_OrderList.Add(itemOrder); Item_Warehouse original = wdb.Item_Warehouse.FirstOrDefault(a => a.itemId == item.id && a.warehouseId == warehouseId); Item_Warehouse copy = original; copy.orders += 1; copy.quantity = copy.quantity - itemOrder.quantity; if (original != null) { wdb.Entry(original).CurrentValues.SetValues(copy); wdb.SaveChanges(); wdb.Dispose(); wdb = new WarehouseDBEntities(); } } wdb.item_Order.AddRange(item_OrderList); wdb.SaveChanges(); wdb.Dispose(); Session.Clear(); Session.Abandon(); } else { RedirectToAction("Register", "Account"); } return(View()); }
public ActionResult Save(List <Item> ware, List <Item_Consignment> itemcon, List <Consignment> con) { int conid = -1; List <ItemsSlotter> isl = new List <ItemsSlotter>(); foreach (Consignment conn in con) { WarehouseBusinessLayer wbl = new WarehouseBusinessLayer(); int warehouseIdd = wbl.getWarehouseId(Convert.ToInt32(Session["UserID"])); conn.warehouseId = warehouseIdd; conn.consignmentStatus = "Added"; try { conid = db.Consignments.Max(u => u.id) + 1; } catch (Exception ex) { conid = 1; } conn.id = conid; cbl.addConsignment(conn); int index = 0; foreach (Item_Consignment ic in itemcon) { Item items = ware.ElementAt(index); ic.itemId = getNewOrOldId(items); ItemsSlotter itemslot = new ItemsSlotter(); itemslot.item_id = ic.itemId; itemslot.quantity = (int)ic.quantity; // itemslot.expiry_date = ic.expiry.Value.ToShortDateString(); Item it = db.Items.FirstOrDefault(a => a.id == itemslot.item_id); itemslot.item_name = it.itemName; bool val = db.Item_Warehouse.Any(o => o.itemId == ic.itemId && o.warehouseId == warehouseIdd); if (val == true) { Item_Warehouse iw = db.Item_Warehouse.FirstOrDefault(a => a.itemId == ic.itemId && a.warehouseId == warehouseIdd); iw.quantity += ic.quantity; cbl.updateItemWarehouse(iw); itemslot.quantity = (int)iw.quantity; // found = true; //TODO: ADO.NET CODE } else { Item_Warehouse iw = new Item_Warehouse(); iw.warehouseId = warehouseIdd; iw.itemId = ic.itemId; iw.quantity = ic.quantity; iw.orders = 0; cbl.addItemWarehouse(iw); itemslot.quantity = (int)iw.quantity; } ic.consignmentId = conid; cbl.addItem_Consignment(ic); isl.Add(itemslot); index++; } //Yaha pe SLotting Horae ha List <Shelf> newShelf = db.Shelves.Where(a => a.warehouse_id == warehouseIdd).ToList(); Slotting slotting = new Slotting(); bool isError = slotting.slotting(newShelf, isl); if (isError) { ViewBag.throwError = "Unfortunately, there is no space in warehouse for the new items"; } List <String> instructionList = slotting.instructionsList; JavaScriptSerializer jss = new JavaScriptSerializer(); string inst = jss.Serialize(instructionList); SlottingBusinessLayer sbl = new SlottingBusinessLayer(); sbl.UpdateConsignmentInstruction(conid, inst); List <String> shelfInserted = slotting.shelfInserted.Distinct().ToList(); string insertedShelfJSON = jss.Serialize(shelfInserted); WarehouseDBEntities wdb = new WarehouseDBEntities(); Consignment consignment = wdb.Consignments.Find(conid); consignment.consignmentStatus = "Added"; consignment.shelfInserted = insertedShelfJSON; wdb.Entry(consignment).State = EntityState.Modified; wdb.SaveChanges(); Session["consignmentID"] = conid; break; } return(RedirectToAction("LoggedIn", "Account")); }