public void SplitBill(int billId, List<OrderItem> updatesToOriginalBillItems, List<OrderItem> newBillItems)
        {
            // TODO: Actually go through and split that bill into two
            using (var context = new RestaurantContext())
            {
                // TODO: 0) Validation :)
                // 1) Get the bill
                var bill = context.Bills.Find(billId);
                if (bill == null) throw new ArgumentException("Invalid Bill ID - does not exist");

                // 2) Loop through bill items, if item not in original, remove
                List<BillItem> toMove = new List<BillItem>();
                foreach(var item in bill.Items) // the items already in the DB
                {
                    bool inOriginal = updatesToOriginalBillItems.Any(x => x.ItemName == item.Item.Description);
                    bool inNewItems = newBillItems.Any(x => x.ItemName == item.Item.Description);
                    if(!inOriginal)
                    {
                        // TODO: clean
                        if (!inNewItems)
                            throw new Exception("Hey - someone's got to pay for that!");
                        toMove.Add(item);
                    }
                }
                foreach (var item in toMove)
                    context.BillItems.Remove(item);

                // 3) Make a new bill
                var newBill = new Bill()
                {
                    BillDate = bill.BillDate,
                    Comment = "Split from bill# " + bill.BillID,
                    NumberInParty = bill.NumberInParty, // meh
                    OrderPlaced = bill.OrderPlaced,
                    OrderReady = bill.OrderReady,
                    OrderServed = bill.OrderServed,
                    WaiterID = bill.WaiterID
                    // TODO: thorny question about rules around splitting bill for a single table vs. reservation
                };

                // 4) Add the new missing items to the new bill
                foreach(var item in toMove)
                {
                    newBill.Items.Add(new BillItem()
                        {
                            ItemID = item.ItemID,
                            Notes = item.Notes,
                            Quantity = item.Quantity,
                            SalePrice = item.SalePrice,
                            UnitCost = item.UnitCost
                        });
                }

                // 5) Add the new bill to the context
                context.Bills.Add(newBill);

                // 6) hope for the best.
                context.SaveChanges();
            }
        }
 public List<SpecialEvent> ListSpecialEvents()
 {
     using (var context = new RestaurantContext())
     {
         return context.SpecialEvents.ToList();
     }
 }
 public List<Item> ListMenuItems()
 {
     using (var context = new RestaurantContext())
     {
         return context.Items.Include(it => it.MenuCategory).ToList();
     }
 }
 /// <summary>
 /// Seats a customer that is a walk-in
 /// </summary>
 /// <param name="when">A mock value of the date/time (Temporary - see remarks)</param>
 /// <param name="tableNumber">Table number to be seated</param>
 /// <param name="customerCount">Number of customers being seated</param>
 /// <param name="waiterId">Id of waiter that is serving</param>
 public void SeatCustomer(DateTime when, byte tableNumber, int customerCount, int waiterId)
 {
     var availableSeats = AvailableSeatingByDateTime(when.Date, when.TimeOfDay);
     using (var context = new RestaurantContext())
     {
         List<string> errors = new List<string>();
         // Rule checking:
         // - Table must be available - typically a direct check on the table, but proxied based on the mocked time here
         // - Table must be big enough for the # of customers
         if (!availableSeats.Exists(x => x.Table == tableNumber))
             errors.Add("Table is currently not available");
         else if (!availableSeats.Exists(x => x.Table == tableNumber && x.Seating >= customerCount))
             errors.Add("Insufficient seating capacity for number of customers.");
         if (errors.Count > 0)
             throw new BusinessRuleException("Unable to seat customer", errors);
         Bill seatedCustomer = new Bill()
         {
             BillDate = when,
             NumberInParty = customerCount,
             WaiterID = waiterId,
             TableID = context.Tables.Single(x => x.TableNumber == tableNumber).TableID
         };
         context.Bills.Add(seatedCustomer);
         context.SaveChanges();
     }
 }
        public List<Category> ListCategorizedMenuItems()
        {
            using (var context = new RestaurantContext())
            {
                var data = from cat in context.MenuCategories
                           orderby cat.Description
                           select new Category()
                           {
                               Description = cat.Description,
                               MenuItems = from item in cat.Items
                                           where item.Active
                                           orderby item.Description
                                           select new MenuItem()
                                           {
                                               Description = item.Description,
                                               Price = item.CurrentPrice,
                                               Calories = item.Calories,
                                               Comment = item.Comment
                                           }

                           };
                return data.ToList();
            }

        }
 public List<Waiter> ListAllWaiters()
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         return context.Waiters.ToList();
     }
 }
 public List<ReservationCollection> ReservationsByTime(DateTime date)
 {
     using (var context = new RestaurantContext())
     {
         var result = from data in context.Reservations
                      where data.ReservationDate.Year == date.Year
                         && data.ReservationDate.Month == date.Month
                         && data.ReservationDate.Day == date.Day
                         && data.ReservationStatus == Reservation.Booked
                      select new ReservationSummary()
                      {
                          ID = data.ReservationID, // needed for when we actually seat the reservation
                          Name = data.CustomerName,
                          Date = data.ReservationDate,
                          NumberInParty = data.NumberInParty,
                          Status = data.ReservationStatus,
                          Event = data.SpecialEvent.Description,
                          Contact = data.ContactPhone
                          //,
                          //Tables = from seat in data.ReservationTables
                          //         select seat.Table.TableNumber
                      };
         var finalResult = from item in result
                           group item by item.Date.Hour into itemGroup
                           select new ReservationCollection()
                           {
                               Hour = itemGroup.Key,
                               Reservations = itemGroup.ToList()
                           };
         return finalResult.ToList();
     }
 }
 public Waiter GetWaiter(int waiterId)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         return context.Waiters.Find(waiterId);
     }
 }
 public Waiters GetWaiter(int WaiterID)
 {
     using(RestaurantContext context = new RestaurantContext())
     {
         return context.Waiters.Find(WaiterID);
     }
 }
 public DateTime GetLastBillDateTime()
 {
     using (var context = new RestaurantContext())
     {
         var result = context.Bills.Max(x => x.BillDate);
         return result;
     }
 }
 public List<Item> ListMenuItems()
 {
     using(var context = new RestaurantContext())
     {   //Note: To use the lambda or Method style of Include, you need to use System.Data.Entity
         //get item data and include category data for each item
         //the .Include() method on the DbSet(T) class performs "eager loading" of data.
         return context.Items.Include(it => it.MenuCategory).ToList();
     }
 }
 public void DeleteSpecialEvent(SpecialEvent item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         var existing = context.SpecialEvents.Find(item.EventCode);
         context.SpecialEvents.Remove(existing);
         context.SaveChanges();
     }
 }
 public void DeleteItem(Item item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         var existing = context.Items.Find(item.ItemID);
         context.Items.Remove(existing);
         context.SaveChanges();
     }
 }
 public void AddSpecialEvent(SpecialEvent item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         // TODO: Validation rules...
         var added = context.SpecialEvents.Add(item);
         context.SaveChanges();
     }
 }
 public void DeleteWaiter(Waiter item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         var existing = context.Waiters.Find(item.WaiterID);
         context.Waiters.Remove(existing);
         context.SaveChanges();
     }
 }
 public int AddTable(Table item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         var added = context.Tables.Add(item);
         context.SaveChanges();
         return added.TableID;
     }
 }
 public List<Item> ListMenuItems()
 {
     using (var context = new RestaurantContext())
     {
         // Get the Item data and include the Category data for each item
         return context.Items.Include(x => x.Category).ToList();
         // The .Include() method on the DbSet<T> class performs "eager loading" of data.
     }
 }
 public int AddTable(Table item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         //TODO: Validation of waiter data
         var added = context.Tables.Add(item);
         context.SaveChanges();
         return added.TableID;
     }
 }
 public int AddItem(Item item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         // TODO: Validation rules...
         var added = context.Items.Add(item);
         context.SaveChanges();
         return added.ItemID;
     }
 }
 public void DeleteWaiter(Waiter item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         //TODO: Validation of waiter data...
         var existing = context.Waiters.Find(item.WaiterID);
         context.Waiters.Remove(existing);
         context.SaveChanges();
     }
 }
 public void UpdateWaiter(Waiters item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         var attached = context.Waiters.Attach(item);
         var existing = context.Entry<Waiters>(attached);
         existing.State = System.Data.Entity.EntityState.Modified;
         context.SaveChanges();
     }
 }
 public List<Reservation> GetReservationBySpecialEvent(string eventcode)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         var result = from res in context.Reservations
                      where res.EventCode == eventcode
                      select res;
         return result.ToList();
     }
 }
 public int AddWaiter(Waiter item)
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         // TODO: Validation rules...
         var added = context.Waiters.Add(item);
         context.SaveChanges();
         return added.WaiterID;
     }
 }
 public void UpdateWaiter(Waiter item)
 {        
     using (RestaurantContext context = new RestaurantContext())
     {
         //TODO: Validation
         var attached = context.Waiters.Attach(item);
         var matchingWithExistingValues = context.Entry<Waiter>(attached);
         matchingWithExistingValues.State = System.Data.Entity.EntityState.Modified;
         context.SaveChanges();
     }
 }
        public List<Reservation> getReservation(string EventCode)
        {
            using (RestaurantContext context = new RestaurantContext())
            {
                var data = from dog in context.Reservations
                           where dog.EventCode == EventCode
                           select dog;


                return data.ToList();
                
            }
        
        }
 public List<ListDataItem> ListActiveBills()
 {
     using (var context = new RestaurantContext())
     {
         var result = from data in context.Bills
                      where !data.PaidStatus
                         && data.Items.Count() > 0
                      select new ListDataItem()
                      {
                          DisplayText = data.BillID.ToString() + " (Table " + data.Table.TableNumber.ToString() + ")",
                          KeyValue = data.BillID
                      };
         return result.ToList();
     }
 }
 public List<CategorizedItemSale> TotalCategorizedItemSales()
 {
     using (var context = new RestaurantContext())
     {
         var results = from info in context.BillItems
                       orderby info.Item.Category.Description, info.Item.Description
                       select new CategorizedItemSale()
                       {
                           CategoryDescription = info.Item.Category.Description,
                           ItemDescription = info.Item.Description,
                           Quantity = info.Quantity,
                           Price = info.SalePrice * info.Quantity,
                           Cost = info.UnitCost * info.Quantity
                       };
         return results.ToList();
     }
 }
 public List<CategoryMenuItem> GetReportCategoryMenuItems()
 {
     using (RestaurantContext context = new RestaurantContext())
     {
         var results = from cat in context.Items
                       orderby cat.Category.Description, cat.Description
                       select new CategoryMenuItem()
                       {
                           CategoryDescription = cat.Category.Description,
                           ItemDescription = cat.Description,
                           Price = cat.CurrentPrice,
                           Calories = cat.Calories,
                           Comment = cat.Comment
                       };
         return results.ToList();
     }
 }
 public List<CategorizedItemSale> TotalCategorizedItemSales()
 {
     using (var context = new RestaurantContext())
     {
         var result = from info in context.BillItems
                      orderby info.Items.Category.Description, info.Items.Description
                      select new CategorizedItemSale
                      {
                          CategoryDescription = info.Items.Category.Description,
                          ItemDescription = info.Items.Description,
                          Quantity = info.Quantity,
                          // Do teh calculations inside the LINQ query 
                          Price = info.SalePrice * info.Quantity,
                          Cost = info.UnitCost * info.Quantity
                      };
        return result.ToList();
     }
 }
 public Order GetBill(int billId)
 {
     using (var context = new RestaurantContext())
     {
         var result = from data in context.Bills
                      where data.BillID == billId
                      select new Order()
                      {
                          BillID = data.BillID,
                          Items = (from info in data.Items
                                  select new OrderItem()
                                  {
                                      ItemName = info.Item.Description,
                                      Price = info.SalePrice,
                                      Quantity = info.Quantity
                                  }).ToList()
                      };
         return result.FirstOrDefault();
     }
 }