public void addBill(int _houseID, string _billType, double _amount, DateTime dueDate, string[] _tenantID, string[] _tenantAmounts)
        {
            using (var context = new houseMateEntities01())
            {
                // create a new house bill
                house_bill newHouseBill = new house_bill
                {
                    FK_houseID = _houseID,
                    billType = _billType,
                    amount = _amount,
                    dueDate = dueDate,
                    paid_ = 0
                };
                context.house_bill.Add(newHouseBill);
                context.SaveChanges();

                // create individual bills for each tenant in the house
                for (int i = 0; i < _tenantID.Length; i++)
                {
                    if(_tenantAmounts[i] != "0")
                    {
                        individual_bills newIndividualBill = new individual_bills
                        {
                            FK_houseBillID = newHouseBill.PK_houseBillID,
                            FK_tenantID = Convert.ToInt32(_tenantID[i]),
                            splitAmount = Convert.ToDouble(_tenantAmounts[i])
                        };
                        context.individual_bills.Add(newIndividualBill);
                        context.SaveChanges();
                    }

                }
            }
        }
        public List<Bill> getBills(int houseID)
        {
            // create the list to be returned
            List<Bill> billList = new List<Bill>();
            using(var context = new houseMateEntities01())
            {
                // select house bills for a house that have not been paid
                List<house_bill> billL = new List<house_bill>();
                billL.AddRange(from h in context.house_bill
                               where h.FK_houseID == houseID && h.paid_ == 0
                               select h);

                int tNum = getNumTenants(houseID);

                foreach (house_bill h in billL)
                {
                    // get individual amouts for each bill
                    List<tenantBill> tList = getInividuals(h.PK_houseBillID);
                    tenantBill[] tenList = new tenantBill[tNum];
                    int incr = 0;
                    foreach (tenantBill t in tList)
                    {
                        tenList[incr] = t;
                        incr++;
                    }

                    string[] names = new string[tNum];
                    string[] amounts = new string[tNum];
                    string[] paid = new string[tNum];
                    int[] tIDs = new int[tNum];
                    for (int i = 0; i < tNum; i++ )
                    {
                        if (tenList[i] != null)
                        {
                            names[i] = tenList[i].tenantName;
                            amounts[i] = tenList[i].amount.ToString();
                            paid[i] = tenList[i].paid;
                            tIDs[i] = tenList[i].tenID;
                        }
                        else
                        {
                            names[i] = "";
                            amounts[i] = "";
                            paid[i] = "";
                            tIDs[i] = -1;
                        }
                    }

                    // add bills to a list to be returned
                    billList.Add(new Bill(h.PK_houseBillID, h.amount, h.billType, Convert.ToDateTime(h.dueDate), names, amounts, paid, tIDs, tNum));

                }
            }
            List<Bill> orderedBillList = billList.OrderBy(o => o.dueDate).ToList();
            return orderedBillList;
        }
 public string getTenName(int tid)
 {
     using (var context = new houseMateEntities01())
     {
         tenant myTenant = (from t in context.tenants
                           where t.FK_aspMemberID == tid && t.isCurrent == 0
                           select t).FirstOrDefault();
         string name = myTenant.my_aspnet_membership.Email;
         return name;
     }
 }
        public string[] getNames(int houseID, string nameFragment)
        {
            using (var context = new houseMateEntities01())
            {
                int listID = getListID(houseID);

                string[] names = (from item in context.items
                                    where item.FK_listID == listID && item.name.ToLower().Contains(nameFragment.ToLower())
                                    select item.name).ToArray();
                return names;
            }
        }
        public string[] getDescs(int houseID, string nameFragment)
        {
            using (var context = new houseMateEntities01())
            {
                int listID = getListID(houseID);

                string[] descs = (from item in context.items
                                    where item.FK_listID == listID && item.name.ToLower().Equals(nameFragment.ToLower())
                                    select item.description).ToArray();
                return descs;
            }
        }
 public void addChore(int houseID, string choreName_)
 {
     using (var context = new houseMateEntities01())
     {
         chore newChore = new chore
         {
             FK_houseID = houseID,
             choreName = choreName_
         };
         context.chores.Add(newChore);
         context.SaveChanges();
     }
 }
        public void buyItem(int itemID)
        {
            using (var context = new houseMateEntities01())
            {
                var boughtItem = (from item in context.items
                                    where item.PK_itemID == itemID
                                    select item).FirstOrDefault();
                boughtItem.bought_ = 1;
                context.SaveChanges();

                //Record purchase here

            }
        }
        public House createHouse(string housename, string password, int userID, string _addr, string _city, string _state)
        {
            using (var context = new houseMateEntities01())
            {
                if (!houseExists(housename))
                {
                    if (getTID(userID) <= 0)
                    {
                        // create the new house
                        house newHouse = new house
                        {
                            houseName = housename,
                            password = password,
                            address = _addr,
                            city = _city,
                            state = _state
                        };
                        context.houses.Add(newHouse);
                        context.SaveChanges();

                        // create the list for that house
                        list newList = new list
                        {
                            FK_houseID = newHouse.PK_houseID
                        };
                        context.lists.Add(newList);
                        context.SaveChanges();

                        // create the notice board for that house
                        notice_board newNBoard = new notice_board
                        {
                            FK_houseID = newHouse.PK_houseID
                        };
                        context.notice_board.Add(newNBoard);
                        context.SaveChanges();

                        return joinHouse(housename, password, userID);
                    }
                    else
                    {
                        return new House(-1, "already in a house");
                    }
                }
                else
                {
                    return new House(-1, "house doesn't exist");
                }
            }
        }
        public List<Chore> getChores(int houseID)
        {
            List<Chore> choreList = new List<Chore>();
            using (var context = new houseMateEntities01())
            {
                List<chore_allocation> cList = new List<chore_allocation>();
                cList.AddRange(from c in context.chore_allocation
                               where(c.chore.FK_houseID == houseID)
                               select c);

                List<int> tids_ = new List<int>();
                tids_.AddRange(from t in context.tenants
                               where t.FK_houseID == houseID
                               select t.PK_tenantID);
                int[] tids = tids_.ToArray();

                List<string> tNameLi = new List<string>();
                tNameLi.AddRange(from t in context.tenants
                                 where t.FK_houseID == houseID && t.isCurrent == 0
                                 select t.my_aspnet_membership.Email);

                string[] tNames = tNameLi.ToArray();

                List<string> nameLi = new List<string>();
                nameLi.AddRange(from c in context.chores
                                where c.FK_houseID == houseID
                                select c.choreName);
                string[] names = nameLi.ToArray();

                int numChores = names.Length;

                chore_allocation[] cArr = cList.ToArray();
                if (cArr.Length < 1)
                {
                    choreList.Add(new Chore("", "", "", numChores, names, tNames, tids));
                }
                else
                {
                    foreach (chore_allocation c in cArr)
                    {
                        choreList.Add(new Chore(c.chore.choreName, c.tenant.my_aspnet_membership.Email, c.dayOfWeek, numChores, names, tNames, tids));
                    }
                }

            }
            return choreList;
        }
        public void allocateChore(int houseID, string cName, int tenantID, string dayOfWeek_, int cycle_)
        {
            using (var context = new houseMateEntities01())
            {
                chore chore_ = context.chores.First(c=>c.FK_houseID == houseID && c.choreName.Equals(cName));

                chore_allocation newAllocation = new chore_allocation
                {
                    FK_choreID = chore_.PK_choreID,
                    FK_tenantID = tenantID,
                    dayOfWeek = dayOfWeek_,
                    cycle = (sbyte)cycle_
                };
                context.chore_allocation.Add(newAllocation);
                context.SaveChanges();
            }
        }
        public void addItem(int houseID, string name_, string desc_, string category_)
        {
            using (var context = new houseMateEntities01())
            {
                int listID = getListID(houseID);
                bool added = false;

                item newItem = new item
                {
                    FK_listID = listID,
                    name = name_,
                    category = category_,
                    bought_ = 0,
                    description = desc_
                };
                var itemArr = from item in context.items
                                 where item.FK_listID == listID
                                 select item;
                List<item> itemList = new List<item>();
                itemList.AddRange(itemArr);

                foreach (item i in itemList)
                {
                    if (!added)
                    {
                        if (i.name.ToLower().Equals(newItem.name.ToLower()) && i.description.ToLower().Equals(newItem.description.ToLower()))
                        {
                            i.bought_ = 0;
                            context.SaveChanges();
                            added = true;
                        }
                    }
                }

                if (!added)
                {
                    context.items.Add(newItem);
                    context.SaveChanges();
                }
            }
        }
        // if a user is associated with a house, return that house
        public House getHouse(int userID)
        {
            using (var context = new houseMateEntities01())
            {
                try
                {
                    var hn = (from t in context.tenants
                             where t.FK_aspMemberID == userID && t.isCurrent == 0
                             select t.house.houseName).Single();
                    string hName = hn;

                    return new House(Convert.ToInt32((from t in context.tenants
                                                      where t.FK_aspMemberID == userID && t.isCurrent == 0
                                                     select t.house.PK_houseID).FirstOrDefault()),  hName);

                }
                catch
                {
                    return new House(-1, "no house");
                }
            }
        }
 //public string getHouseName(int houseID)
 //{
 //    using (var context = new houseMateEntities01())
 //    {
 //        return context.houses
 //            .Where(h=> h.PK_houseID == houseID)
 //            .Select(h=> h.houseName).Single();
 //    }
 //}
 //public string getHousePass(int houseID)
 //{
 //    using (var context = new houseMateEntities01())
 //    {
 //        return context.houses
 //            .Where(h => h.PK_houseID == houseID)
 //            .Select(h => h.password).Single();
 //    }
 //}
 //public void setHouseName(int houseID, string newHouseName)
 //{
 //    using (var context = new houseMateEntities01())
 //    {
 //        house current = context.houses
 //            .First(h=> h.PK_houseID == houseID);
 //        current.houseName = newHouseName;
 //        context.SaveChanges();
 //    }
 //}
 //public void setHousePass(int houseID, string newHousePass)
 //{
 //    using (var context = new houseMateEntities01())
 //    {
 //        house current = context.houses
 //            .First(h => h.PK_houseID == houseID);
 //        current.password = newHousePass;
 //        context.SaveChanges();
 //    }
 //}
 public int leaveHouse(int uid)
 {
     using (var context = new houseMateEntities01())
     {
         try
         {
             tenant current = context.tenants
                         .First(t => t.my_aspnet_membership.userId == uid);
             current.isCurrent = 1;
             context.SaveChanges();
             return 1;
         }
         catch
         {
             return -1;
         }
     }
 }
        private bool CheckFullyPaid(int billID)
        {
            using (var context = new houseMateEntities01())
            {
                // select the individual bills for a given bill
                List<individual_bills> bills = new List<individual_bills>();
                bills.AddRange(from b in context.individual_bills
                               where b.FK_houseBillID == billID
                               select b);

                // if any one individual bill has not been paid, return false. else the bill has been paid
                foreach (individual_bills b in bills)
                {
                    if (b.datePaid == null)
                    {
                        return false;
                    }
                }
                return true;
            }
        }
 private int getListID(int houseID)
 {
     using (var context = new houseMateEntities01())
     {
         return Convert.ToInt32((from list in context.lists
                                 where list.FK_houseID == houseID
                                 select list.PK_listID).Single());
     }
 }
 public List<myTenant> getTenants(int houseID)
 {
     List<myTenant> tList = new List<myTenant>();
     using (var context = new houseMateEntities01())
     {
         List<tenant> tenants = new List<tenant>();
         tenants.AddRange(from t in context.tenants
                          where(t.FK_houseID == houseID && t.isCurrent == 0)
                          select t);
         foreach (tenant t in tenants)
         {
             tList.Add(new myTenant(t.PK_tenantID, t.my_aspnet_membership.Email));
         }
     }
     return tList;
 }
        public HouseInfo getInfo(int houseID)
        {
            using (var context = new houseMateEntities01())
            {
                house currH = context.houses
                                .Where(h => h.PK_houseID == houseID)
                                .Select(h => h).Single();

                return new HouseInfo(currH.houseName, currH.password, currH.wifiPass, currH.binNight, Convert.ToInt32(currH.recycOrGreen));
            }
        }
        private bool houseExists(string housename)
        {
            using (var context = new houseMateEntities01())
            {

                bool existing = context.houses.Any(h => h.houseName.Equals(housename));

                if (existing)
                {
                    return true;
                }

                return false;
            }
        }
        public void updateRegID(int tenID, string regID)
        {
            using (var context = new houseMateEntities01())
            {
                try
                {
                    var ten1 = (from t in context.tenants
                                where t.PK_tenantID == tenID
                                select t).FirstOrDefault();
                    ten1.registrationID = regID;
                    context.SaveChanges();
                }
                catch
                {

                }
            }
        }
 public void setInfo(int houseID, string houseName, string housePwd, string wifi, string binNight, string recOrGre)
 {
     using (var context = new houseMateEntities01())
     {
         house currH = context.houses
                         .First(h => h.PK_houseID == houseID);
         if (houseName != "") currH.houseName = houseName;
         if (housePwd != "") currH.password = housePwd;
         if(wifi != "") currH.wifiPass = wifi;
         if (binNight != "") currH.binNight = binNight;
         if (recOrGre != "")
         {
             if (recOrGre == "recycling") currH.recycOrGreen = 0;
             else currH.recycOrGreen = 1;
         }
         context.SaveChanges();
     }
 }
        public void payBill(int billID, int tenantID)
        {
            using (var context = new houseMateEntities01())
            {
                // select the individual bill for the tenant and mark it as paid
                individual_bills bill = context.individual_bills
                                        .First(i => i.FK_houseBillID == billID && i.FK_tenantID == tenantID);
                bill.datePaid = DateTime.Now;
                context.SaveChanges();

                // if all tenants have paid this bill then mark the whole bill as paid
                if(CheckFullyPaid(billID))
                {
                    house_bill mainBill = context.house_bill
                                          .First(b=>b.PK_houseBillID == billID);
                    mainBill.paid_ = 1;
                    context.SaveChanges();
                }

                // Record payment here

            }
        }
 private int getNumTenants(int houseID)
 {
     int numTenants = 0;
     using(var context = new houseMateEntities01())
     {
         // select all current tenants in a house
         List<tenant> tenantList = new List<tenant>();
         tenantList.AddRange(from t in context.tenants
                             where t.FK_houseID == houseID && t.isCurrent == 0
                             select t);
         foreach(tenant t in tenantList)
         {
             // count the tenants
             numTenants++;
         }
     }
     return numTenants;
 }
        public House joinHouse(string houseName, string password, int userID)
        {
            using (var context = new houseMateEntities01())
            {
                if (getTID(userID) <= 0)
                {
                    int hID = Convert.ToInt32(context.houses
                        .Where(h => h.houseName.Equals(houseName) && h.password.Equals(password))
                        .Select(h => h.PK_houseID).FirstOrDefault());

                    tenant newTennant = new tenant
                    {
                        FK_houseID = hID,
                        FK_aspMemberID = userID
                    };
                    context.tenants.Add(newTennant);
                    context.SaveChanges();

                    return getHouse(userID);
                }
                else
                {
                    return new House(-1, "already in a house");
                }
            }
        }
 public int getTID(int uid)
 {
     using (var context = new houseMateEntities01())
     {
         return Convert.ToInt32(context.tenants
             .Where(t=> t.FK_aspMemberID == uid && t.isCurrent == 0)
             .Select(t=> t.PK_tenantID).FirstOrDefault());
     }
 }
        public List<Item> getShoppingList(int houseID)
        {
            List<Item> shoppingList = new List<Item>();

            using(var context = new houseMateEntities01())
            {
                int listID = getListID(houseID);

                if (listID != 0)
                {
                    List<item> itemList = new List<item>();

                    var itemVar = from i in context.items
                                  where i.FK_listID == listID && i.bought_ == 0
                                  select i;

                    itemList.AddRange(itemVar);

                    foreach (item i in itemList)
                    {
                        shoppingList.Add(new Item(i.PK_itemID,i.name.ToString(), i.description.ToString(), i.category.ToString()));
                    }
                }
            }
            return shoppingList;
        }
        public List<tenantBill> getInividuals(int billID)
        {
            // create a list to be returned
            List<tenantBill> tenList = new List<tenantBill>();
            using(var context = new houseMateEntities01())
            {
                // select individual bills for a billID for all current tenants
                List<individual_bills> tenL = new List<individual_bills>();
                tenL.AddRange(from i in context.individual_bills
                              where i.FK_houseBillID == billID && i.tenant.isCurrent == 0
                              select i);

                // fill return list with selected values
                foreach (individual_bills i in tenL)
                {
                    string name = i.tenant.my_aspnet_membership.Email;
                    DateTime? date;
                    if (i.datePaid != null) date = (DateTime)i.datePaid;
                    else date = null;

                    tenList.Add(new tenantBill(i.FK_houseBillID, name, Convert.ToDouble(i.splitAmount), date, i.FK_tenantID));
                }
            }
            return tenList;
        }