public List <int> GetDaysWithAllergens(int cartCalendarId)
        {
            try
            {
                List <int>          daysWithAllergens = new List <int>();
                hccProgramPlan      plan    = hccProgramPlan.GetById(this.Plan_PlanID.Value);
                hccUserProfile      profile = hccUserProfile.GetById(this.UserProfileID);
                hccCartItemCalendar cartCal = hccCartItemCalendar.GetById(cartCalendarId);

                if (plan != null && profile != null)
                {
                    // get list of user profile allergens
                    List <hccAllergen> userAllergens = profile.GetAllergens();

                    if (userAllergens.Count > 0)
                    {
                        List <hccProgramDefaultMenu> defaultMenus = hccProgramDefaultMenu.GetBy(cartCal.CalendarID, plan.ProgramID);

                        defaultMenus.ForEach(delegate(hccProgramDefaultMenu defaultMenu)
                        {
                            hccCartDefaultMenuException excMenu = hccCartDefaultMenuException.GetBy(defaultMenu.DefaultMenuID, cartCal.CartCalendarID);
                            List <hccAllergen> menuAllg         = new List <hccAllergen>();

                            if (excMenu == null)
                            {
                                hccMenuItem item = hccMenuItem.GetById(defaultMenu.MenuItemID);
                                if (item != null)
                                {
                                    menuAllg = item.GetAllergens();
                                }
                            }
                            else
                            {
                                menuAllg = excMenu.GetMenuItemAllergens();
                            }

                            if (menuAllg.Count > 0)
                            {
                                if (userAllergens.Intersect(menuAllg).Count() > 0)
                                {
                                    daysWithAllergens.Add(defaultMenu.DayNumber);
                                }
                            }
                        });
                    }
                }
                return(daysWithAllergens);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static List <BalanceReportItem> GetBalanceReportItems(DateTime reportDate)
        {
            Guid x = new Guid();

            try
            {
                List <BalanceReportItem> items   = new List <BalanceReportItem>();
                List <hccLedger>         ledgers = new List <hccLedger>();

                var rt = GetAll().Where(a => a.CreatedDate <= reportDate.AddDays(1))
                         //&& (a.PostBalance > 0.00m || a.PostBalance < 0.00m))
                         .OrderByDescending(a => a.CreatedDate)
                         .GroupBy(a => a.AspNetUserID);

                rt.ToList().ForEach(delegate(IGrouping <Guid, hccLedger> group)
                {
                    BalanceReportItem bri = new BalanceReportItem
                    {
                        MembershipID    = group.Key,
                        BalanceAsOfDate = group.First().PostBalance
                    };

                    x = group.Key;
                    hccUserProfile user = hccUserProfile.GetParentProfileBy(group.Key);
                    if (user != null)
                    {
                        bri.CustomerName  = user.FullName;
                        bri.CustomerEmail = user.ASPUser.Email;
                    }

                    hccLedger last = hccLedger.GetByMembershipID(group.Key, null)
                                     .FirstOrDefault(); //a => a.TransactionType == Enums.LedgerTransactionType.Purchase);

                    if (last != null)
                    {
                        bri.LastPurchaseDate   = last.CreatedDate;
                        bri.LastPurchaseAmount = last.PaymentDue;
                    }

                    items.Add(bri);
                });

                return(items.OrderBy(a => a.CustomerName).ToList());
            }
            catch (Exception ex)
            {
                var y = x;
                throw;
            }
        }
        public static hccCartItem Gift_GenerateNew(int cartId)
        {
            try
            {
                string redeemCode = Guid.NewGuid().ToString().Substring(0, 8).ToUpper();

                while (RedeemCodeExists(redeemCode))
                {
                    redeemCode = Guid.NewGuid().ToString().Substring(0, 8).ToUpper();
                }

                hccCartItem giftCert = new hccCartItem
                {
                    CartID          = cartId,
                    ItemTypeID      = (int)Enums.CartItemType.GiftCard,
                    IsTaxable       = false,
                    Quantity        = 1,
                    CreatedBy       = (Helpers.LoggedUser == null ? Guid.Empty : (Guid)Helpers.LoggedUser.ProviderUserKey),
                    CreatedDate     = DateTime.Now,
                    IsCompleted     = false,
                    Gift_RedeemCode = redeemCode
                };

                hccCart userCart = hccCart.GetById(cartId);
                if (userCart.AspNetUserID.HasValue && userCart.AspNetUserID.Value != Guid.Empty)
                {
                    hccUserProfile prof = hccUserProfile.GetParentProfileBy(userCart.AspNetUserID.Value);

                    if (prof != null)
                    {
                        giftCert.UserProfileID = prof.UserProfileID;
                    }

                    giftCert.Gift_IssuedTo   = userCart.AspNetUserID;
                    giftCert.Gift_IssuedDate = DateTime.Now;
                }

                return(giftCert);
            }
            catch (Exception ex) { throw ex; }
        }
        public static void Import(out int count)
        {
            using (StreamWriter writer = new StreamWriter(@"C:\HCCCustomerOutput.txt"))
            {
                Console.SetOut(writer);

                count = 0;
                List <string> errorReport = new List <string>();

                try
                {
                    List <ImportedCustomer> impCusts = GetAll();

                    foreach (ImportedCustomer impCust in impCusts)
                    {
                        Console.WriteLine(count + " : ");
                        if (!impCust.IsValid)
                        {
                            impCust.Email = "admin" + count.ToString() + "@healthychefcreations.com";
                        }
                        else if (impCust.Email.Contains("info@healthychef") || impCust.Email.Contains("thehealthyassistant@earthlink"))
                        {
                            impCust.Email = "admin" + count.ToString() + "@healthychefcreations.com";
                        }

                        Console.WriteLine(impCust.Email);

                        if (impCust.IsValid)
                        {
                            count++;

                            string         userName    = impCust.Email.Trim().Split('@')[0] + DateTime.Now.ToString("yyyyMMddHHmmtt");
                            string         password    = OrderNumberGenerator.GenerateOrderNumber("?#?#?#?#");
                            string         aspUserName = Membership.GetUserNameByEmail(impCust.Email.Trim());
                            MembershipUser newUser     = null;

                            if (!string.IsNullOrWhiteSpace(aspUserName))
                            {
                                newUser = Membership.GetUser(aspUserName);
                            }

                            MembershipCreateStatus createResult = MembershipCreateStatus.UserRejected;

                            if (newUser == null)
                            {
                                newUser = Membership.CreateUser(userName, password, impCust.Email.Trim(), "import", "import", true, out createResult);

                                if (newUser != null)
                                {
                                    Console.WriteLine(newUser.UserName + "New user.");
                                }
                            }
                            else
                            {
                                Console.WriteLine(newUser.UserName + " Existing user.");
                                createResult = MembershipCreateStatus.Success;
                            }

                            if (newUser != null)
                            {
                                if (createResult == MembershipCreateStatus.Success)
                                {
                                    //Assign Customer role to newUser
                                    try
                                    {
                                        if (!Roles.IsUserInRole(newUser.UserName, "Customer"))
                                        {
                                            Roles.AddUserToRole(newUser.UserName, "Customer");
                                            Console.WriteLine(newUser.UserName + " Role assigned.");
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine(newUser.UserName + " =Assign role failed." + ex.Message + ex.StackTrace);
                                    }
                                    //Send E-mail notification to account user
                                    //HealthyChef.Email.EmailController ec = new HealthyChef.Email.EmailController();
                                    //ec.SendMail_NewUserConfirmation(email, password);

                                    //Create a Healthy Chef profile for this new user
                                    hccUserProfile newProfile = hccUserProfile.GetBy((Guid)newUser.ProviderUserKey).SingleOrDefault(a => !a.ParentProfileID.HasValue);

                                    if (newProfile == null)
                                    {
                                        try
                                        {
                                            newProfile = new hccUserProfile
                                            {
                                                MembershipID   = (Guid)newUser.ProviderUserKey,
                                                CreatedBy      = (Membership.GetUser() == null ? Guid.Empty : (Guid)Membership.GetUser().ProviderUserKey),
                                                CreatedDate    = DateTime.Now,
                                                AccountBalance = 0.00m,
                                                IsActive       = true,
                                                FirstName      = impCust.FirstName.Trim(),
                                                LastName       = impCust.LastName.Trim(),
                                                ProfileName    = impCust.FirstName.Trim()
                                            };

                                            //Save all hccProfile information
                                            using (var cont = new healthychefEntities())
                                            {
                                                System.Data.EntityKey key = cont.CreateEntityKey("hccUserProfiles", newProfile);
                                                object oldObj;

                                                if (cont.TryGetObjectByKey(key, out oldObj))
                                                {
                                                    cont.ApplyCurrentValues("hccUserProfiles", newProfile);
                                                }
                                                else
                                                {
                                                    cont.hccUserProfiles.AddObject(newProfile);
                                                }

                                                cont.SaveChanges();
                                            }
                                            //cont.Refresh(System.Data.Objects.RefreshMode.StoreWins, newProfile);
                                            Console.WriteLine(newUser.UserName + " New profile.");
                                        }
                                        catch (Exception)
                                        {
                                            Console.WriteLine("=" + newUser.UserName + " Save Profile failed.");
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine(newUser.UserName + " Existing profile.");
                                        createResult = MembershipCreateStatus.Success;
                                    }

                                    if (newProfile != null && newProfile.UserProfileID > 0)
                                    {
                                        if (impCust.OtherDeliveryInfo != null && !string.IsNullOrWhiteSpace(impCust.OtherDeliveryInfo))
                                        {
                                            hccUserProfileNote shipNote = new hccUserProfileNote
                                            {
                                                DateCreated   = DateTime.Now,
                                                DisplayToUser = false,
                                                UserProfileID = newProfile.UserProfileID,
                                                IsActive      = true,
                                                Note          = impCust.OtherDeliveryInfo,
                                                NoteTypeID    = (int)Enums.UserProfileNoteTypes.ShippingNote
                                            };

                                            using (var cont = new healthychefEntities())
                                            {
                                                EntityKey key          = cont.CreateEntityKey("hccUserProfileNotes", shipNote);
                                                object    originalItem = null;

                                                if (cont.TryGetObjectByKey(key, out originalItem))
                                                {
                                                    cont.ApplyCurrentValues(key.EntitySetName, shipNote);
                                                }
                                                else
                                                {
                                                    cont.hccUserProfileNotes.AddObject(shipNote);
                                                }

                                                cont.SaveChanges();
                                            }
                                        }

                                        if (impCust.HowDidYouHear != null && !string.IsNullOrWhiteSpace(impCust.HowDidYouHear))
                                        {
                                            hccUserProfileNote hearNote = new hccUserProfileNote
                                            {
                                                DateCreated   = DateTime.Now,
                                                DisplayToUser = false,
                                                UserProfileID = newProfile.UserProfileID,
                                                IsActive      = true,
                                                Note          = impCust.HowDidYouHear,
                                                NoteTypeID    = (int)Enums.UserProfileNoteTypes.GeneralNote
                                            };

                                            using (var cont = new healthychefEntities())
                                            {
                                                EntityKey key          = cont.CreateEntityKey("hccUserProfileNotes", hearNote);
                                                object    originalItem = null;

                                                if (cont.TryGetObjectByKey(key, out originalItem))
                                                {
                                                    cont.ApplyCurrentValues(key.EntitySetName, hearNote);
                                                }
                                                else
                                                {
                                                    cont.hccUserProfileNotes.AddObject(hearNote);
                                                }

                                                cont.SaveChanges();
                                            }
                                        }

                                        try
                                        {
                                            //save Shipping Address
                                            hccAddress shipAddr = null;

                                            if (newProfile.ShippingAddressID.HasValue)
                                            {
                                                shipAddr = hccAddress.GetById(newProfile.ShippingAddressID.Value);
                                            }

                                            if (shipAddr != null)
                                            {
                                                try
                                                {
                                                    newProfile.ShippingAddressID = null;
                                                    //Save all hccProfile information
                                                    using (var cont = new healthychefEntities())
                                                    {
                                                        System.Data.EntityKey key1 = cont.CreateEntityKey("hccUserProfiles", newProfile);
                                                        object oldObj1;

                                                        if (cont.TryGetObjectByKey(key1, out oldObj1))
                                                        {
                                                            cont.ApplyCurrentValues("hccUserProfiles", newProfile);
                                                        }
                                                        else
                                                        {
                                                            cont.hccUserProfiles.AddObject(newProfile);
                                                        }
                                                        cont.SaveChanges();
                                                    }
                                                    //cont.Refresh(System.Data.Objects.RefreshMode.StoreWins, newProfile);
                                                    using (var cont = new healthychefEntities())
                                                    {
                                                        EntityKey key          = cont.CreateEntityKey("hccAddresses", shipAddr);
                                                        object    originalItem = null;

                                                        if (cont.TryGetObjectByKey(key, out originalItem))
                                                        {
                                                            cont.AttachTo(shipAddr.EntityKey.EntitySetName, shipAddr);
                                                            cont.DeleteObject(shipAddr);
                                                        }
                                                        cont.SaveChanges();
                                                    }

                                                    shipAddr = null;
                                                }
                                                catch (Exception ex)
                                                {
                                                    Console.WriteLine(newUser.UserName + " =Delete old shipping address failed." + ex.Message + ex.StackTrace);
                                                }
                                            }

                                            if (shipAddr == null)
                                            {
                                                shipAddr = new hccAddress();
                                            }

                                            if (impCust.ShippingAddress1 != null)
                                            {
                                                shipAddr.Address1 = (string.IsNullOrWhiteSpace(impCust.ShippingAddress1) ? "" : impCust.ShippingAddress1.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.Address1 = "";
                                            }

                                            if (impCust.ShippingAddress2 != null)
                                            {
                                                shipAddr.Address2 = (string.IsNullOrWhiteSpace(impCust.ShippingAddress2) ? "" : impCust.ShippingAddress2.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.Address2 = "";
                                            }

                                            if (impCust.ShippingAddress3 != null)
                                            {
                                                shipAddr.Address2 += " " + (string.IsNullOrWhiteSpace(impCust.ShippingAddress3) ? "" : impCust.ShippingAddress3.Trim());
                                            }

                                            shipAddr.AddressTypeID = (int)Enums.AddressType.Shipping;

                                            if (impCust.ShippingCity != null)
                                            {
                                                shipAddr.City = (string.IsNullOrWhiteSpace(impCust.ShippingCity) ? "" : impCust.ShippingCity.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.City = "";
                                            }

                                            shipAddr.Country = "US";

                                            if (impCust.FirstName != null)
                                            {
                                                shipAddr.FirstName = (string.IsNullOrWhiteSpace(impCust.FirstName) ? "" : impCust.FirstName.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.FirstName = "";
                                            }

                                            shipAddr.IsBusiness = false;

                                            if (impCust.LastName != null)
                                            {
                                                shipAddr.LastName = (string.IsNullOrWhiteSpace(impCust.LastName) ? "" : impCust.LastName.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.LastName = "";
                                            }

                                            if (impCust.Phone1 != null)
                                            {
                                                shipAddr.Phone = (string.IsNullOrWhiteSpace(impCust.Phone1) ? "" : impCust.Phone1.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.Phone = "";
                                            }

                                            if (impCust.Phone1Ext != null)
                                            {
                                                shipAddr.Phone += (string.IsNullOrWhiteSpace(impCust.Phone1Ext.Trim()) ? "" : " x" + impCust.Phone1Ext.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.Phone = "";
                                            }

                                            if (impCust.ShippingZipCode != null)
                                            {
                                                shipAddr.PostalCode = (string.IsNullOrWhiteSpace(impCust.ShippingZipCode) ? "" : impCust.ShippingZipCode.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.PostalCode = "";
                                            }

                                            if (impCust.ShippingState != null)
                                            {
                                                shipAddr.State = (string.IsNullOrWhiteSpace(impCust.ShippingState) ? "" : impCust.ShippingState.Trim());
                                            }
                                            else
                                            {
                                                shipAddr.State = "";
                                            }

                                            if (impCust.ShipMethod == null)
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.Delivery;
                                            }
                                            else if (impCust.ShipMethod.Trim() == "F")
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.Delivery;
                                            }
                                            else if (impCust.ShipMethod.Trim() == "P")
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.LocalPickUp;
                                            }
                                            else if (impCust.ShipMethod.Trim() == "D")
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.LocalDelivery;
                                            }
                                            else
                                            {
                                                shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.Delivery;
                                            }

                                            if (shipAddr != null)
                                            {
                                                try
                                                {
                                                    using (var cont = new healthychefEntities())
                                                    {
                                                        EntityKey key          = cont.CreateEntityKey("hccAddresses", shipAddr);
                                                        object    originalItem = null;

                                                        if (cont.TryGetObjectByKey(key, out originalItem))
                                                        {
                                                            cont.hccAddresses.ApplyCurrentValues((hccAddress)originalItem);
                                                        }
                                                        else
                                                        {
                                                            cont.hccAddresses.AddObject(shipAddr);
                                                        }

                                                        cont.SaveChanges();
                                                    }
                                                    //cont.Refresh(System.Data.Objects.RefreshMode.StoreWins, shipAddr);
                                                }
                                                catch (Exception ex)
                                                {
                                                    Console.WriteLine(newUser.UserName + " =Shipping address save failed." + ex.Message + ex.StackTrace);
                                                }
                                            }

                                            if (shipAddr != null && shipAddr.AddressID > 0)
                                            {
                                                newProfile.ShippingAddressID = shipAddr.AddressID;
                                            }
                                            else
                                            {
                                                newProfile.ShippingAddressID = null;
                                            }

                                            using (var cont = new healthychefEntities())
                                            {
                                                System.Data.EntityKey upkey = cont.CreateEntityKey("hccUserProfiles", newProfile);
                                                object oldObj;

                                                if (cont.TryGetObjectByKey(upkey, out oldObj))
                                                {
                                                    cont.ApplyCurrentValues("hccUserProfiles", newProfile);
                                                }
                                                else
                                                {
                                                    cont.hccUserProfiles.AddObject(newProfile);
                                                }

                                                cont.SaveChanges();
                                            }

                                            Console.WriteLine(newUser.UserName + " Shipping address saved.");
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine(newUser.UserName + " =Shipping address not created." + ex.Message + ex.StackTrace);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("User Profile for user: "******" ID not created.");
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("=New user for user: "******" not created.");
                                }
                            }
                            else
                            {
                                Console.WriteLine(createResult.ToString() + " : " + impCust.Email);
                            }
                        }
                        else
                        {
                            count++; Console.WriteLine("=Customer: " + impCust.FirstName + " " + impCust.LastName + " has no email address.");
                        }
                    }
                }
                catch (Exception ex) { Console.WriteLine("=" + ex.Message + " : " + ex.StackTrace); }
            }
        }
        public static List <CustCalDay> GetCustomerCalendars(DateTime startDate, DateTime endDate)
        {
            try
            {
                List <CustCalDay>         CustCals  = new List <CustCalDay>();
                List <MealItemReportItem> menuItems = ReportSprocs.GetMenuItemsByDateRange(startDate, endDate, false).Where(a => a.ParentTypeId != 1)
                                                      .OrderBy(a => a.DeliveryDate).ThenBy(a => a.CartItemId).ThenBy(a => a.DayNum).ThenBy(a => a.MealTypeId).ToList();

                menuItems.ForEach(delegate(MealItemReportItem result)
                {
                    CustCalDay ccd = CustCals.SingleOrDefault(a => a.UserProfileId == result.UserProfileId && a.DeliveryDate == result.DeliveryDate &&
                                                              a.OrderNumber == result.OrderNumber && a.PlanName == result.PlanName && a.DayNumber == result.DayNum);

                    if (ccd == null)
                    {
                        hccUserProfile prof = hccUserProfile.GetById(result.UserProfileId);

                        if (prof != null)
                        {
                            ccd = new CustCalDay
                            {
                                DeliveryDate  = result.DeliveryDate,
                                CartItemId    = result.CartItemId,
                                OrderNumber   = result.OrderNumber,
                                PlanName      = result.PlanName,
                                DayNumber     = result.DayNum,
                                ProfileName   = prof.ProfileName,
                                UserName      = prof.FullName,
                                LastName      = prof.LastName,
                                FirstName     = prof.FirstName,
                                UserProfileId = result.UserProfileId
                            };

                            ccd = AgregateByDay(ccd, result, true);

                            CustCals.Add(ccd);
                        }
                    }
                    else
                    {
                        ccd = AgregateByDay(ccd, result, false);
                    }
                });

                // trim the crap from the begins
                foreach (CustCalDay day in CustCals)
                {
                    LeftTrim(day);
                }

                // add missed days to show in the report (for example: for 3 day plan add 4-7 days with empty lists)
                var groupsByUser = CustCals.GroupBy(i => new { i.UserName, i.DeliveryDate, i.OrderNumber, i.PlanName }).Select(r => new
                {
                    UserName         = r.Key,
                    DaysPerWeek      = r.GroupBy(g => g.DayNumber).Count(),
                    FirstItemInGroup = r.FirstOrDefault()
                }).ToList();

                foreach (var group in groupsByUser)
                {
                    if (group.DaysPerWeek == 7 || group.FirstItemInGroup == null)
                    {
                        continue;
                    }

                    AddMissedDays(group.DaysPerWeek, CustCals, group.FirstItemInGroup);
                }

                return(CustCals);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static List <ChefProdItem> GetPackSlipItems(string orderNumber, DateTime deliveryDate)
        {
            try
            {
                List <MealItemReportItem> results  = null;
                List <ChefProdItem>       retItems = new List <ChefProdItem>();

                results = ReportSprocs.GetMenuItemsByOrderNumber(orderNumber, deliveryDate).ToList();
                results = results.Where(ri => !hccMenuItem.SideMealTypes.Contains((Enums.MealTypes)ri.MealTypeId)).ToList();

                var cartItems = hccCartItem.GetByIds(results.Select(i => i.CartItemId).ToList());

                if (results != null && results.Count > 0)
                {
                    foreach (MealItemReportItem result in results)
                    {
                        if (result.MealSizeId == 0)
                        {
                            result.MealSizeId = 3; // update noSize to regular size
                        }
                        var cartItem = cartItems.FirstOrDefault(i => i.CartItemID == result.CartItemId);

                        if (cartItem == null)
                        {
                            continue; // should not happen
                        }
                        var newItem = new ChefProdItem
                        {
                            ParentTypeId = result.ParentTypeId,
                            ParentId     = result.ParentId,
                            MenuItemId   = result.MenuItemId,
                            MealTypeId   = result.MealTypeId,
                            MealSizeId   = result.MealSizeId,
                            ItemName     = result.ItemName, // + "-" + result.OrderNumber + " - " + result.ParentTypeId + "-" + result.ParentId,
                            MealType     = Enums.GetEnumDescription(((Enums.MealTypes)result.MealTypeId)),
                            MealSize     = Enums.GetEnumDescription(((Enums.CartItemSize)result.MealSizeId)),
                            Quantity     = result.Quantity,
                            DeliveryDate = result.DeliveryDate,
                            OrderNumber  = result.OrderNumber,
                            DayNumber    = result.DayNum,
                            Prefs        = string.IsNullOrWhiteSpace(result.Prefs) ? "" : result.Prefs,
                            CartItemId   = result.CartItemId,
                            CartItemType = Enums.GetEnumDescription(((Enums.CartItemTypeAbbr)cartItem.ItemTypeID))
                        };

                        if (cartItem.ItemTypeID == Convert.ToInt32((Enums.CartItemType.AlaCarte)) && cartItem.Plan_IsAutoRenew == true)
                        {
                            newItem.IsFamilyStyle = "Yes";
                        }
                        else if (cartItem.ItemTypeID == Convert.ToInt32((Enums.CartItemType.AlaCarte)) && cartItem.Plan_IsAutoRenew == false)
                        {
                            newItem.IsFamilyStyle = "No";
                        }
                        else
                        {
                            newItem.IsFamilyStyle = "N/A";
                        }
                        hccUserProfile prof = hccUserProfile
                                              .GetById(result.UserProfileId);

                        if (prof != null)
                        {
                            newItem.UserName    = prof.ParentProfileName;
                            newItem.ProfileName = prof.ProfileName;
                        }

                        hccCartItemCalendar cartCal = hccCartItemCalendar.GetBy(newItem.CartItemId, newItem.DeliveryDate);

                        if (newItem.ParentTypeId == 1) //a la carte
                        {
                            newItem.Sides = cartItem.GetMealSideMenuItemsAsSectionString(", ");
                            newItem.Side1 = cartItem.GetMealSide1MenuItemName();
                            newItem.Side2 = cartItem.GetMealSide2MenuItemName();
                            retItems.Add(newItem);
                        }
                        else //is program plan
                        {
                            Enums.MealTypes mealType = (Enums.MealTypes)result.MealTypeId;

                            if (mealType == Enums.MealTypes.BreakfastEntree ||
                                mealType == Enums.MealTypes.LunchEntree ||
                                mealType == Enums.MealTypes.DinnerEntree ||
                                mealType == Enums.MealTypes.OtherEntree ||
                                mealType == Enums.MealTypes.ChildEntree ||
                                mealType == Enums.MealTypes.Beverage ||
                                mealType == Enums.MealTypes.Dessert ||
                                mealType == Enums.MealTypes.Goods ||
                                mealType == Enums.MealTypes.Miscellaneous ||
                                mealType == Enums.MealTypes.Salad ||
                                mealType == Enums.MealTypes.Snack ||
                                mealType == Enums.MealTypes.Soup ||
                                mealType == Enums.MealTypes.Supplement)    // Non-Side Types
                            {
                                hccProgramDefaultMenu        defMenu = null;
                                List <hccProgramDefaultMenu> defMenus;

                                if (newItem.ParentTypeId == 2) //plan with exception
                                {
                                    hccCartDefaultMenuException defEx = hccCartDefaultMenuException.GetById(newItem.ParentId);

                                    if (defEx != null)
                                    {
                                        defMenu = hccProgramDefaultMenu.GetById(defEx.DefaultMenuID);

                                        //newItem.Prefs = hccCartDefaultMenuExPref.GetPrefsBy(defEx.DefaultMenuExceptID)
                                        //    .Select(a => a.Name).DefaultIfEmpty("None").Aggregate((c, d) => c + ", " + d);
                                    }
                                }
                                else if (newItem.ParentTypeId == 3) //plan default
                                {
                                    defMenu       = hccProgramDefaultMenu.GetById(newItem.ParentId);
                                    newItem.Prefs = "";
                                }

                                //newItem.DayNumber = defMenu.DayNumber;
                                if (defMenu == null)
                                {
                                    retItems.Add(newItem);
                                    continue;
                                }

                                defMenus = hccProgramDefaultMenu.GetBy(defMenu.CalendarID, defMenu.ProgramID);

                                //cheat to find related sides
                                List <hccProgramDefaultMenu> sides = new List <hccProgramDefaultMenu>();
                                string sideStr = string.Empty;

                                if (newItem.MealTypeId < 100)
                                {
                                    sides = defMenus.Where(a => a.DayNumber == newItem.DayNumber && (a.MealTypeID == (newItem.MealTypeId + 10))).ToList();
                                }

                                sides.ForEach(delegate(hccProgramDefaultMenu sideDefMenu)
                                {
                                    hccCartDefaultMenuException sideEx = hccCartDefaultMenuException.GetBy(sideDefMenu.DefaultMenuID, cartCal.CartCalendarID);
                                    hccMenuItem sideItem;
                                    Enums.CartItemSize sidePortionSize;
                                    string prefsString = string.Empty;

                                    if (sideEx == null)
                                    {
                                        sideItem        = hccMenuItem.GetById(sideDefMenu.MenuItemID);
                                        sidePortionSize = (Enums.CartItemSize)sideDefMenu.MenuItemSizeID;
                                        prefsString     = string.Empty;
                                    }
                                    else
                                    {
                                        sideItem        = hccMenuItem.GetById(sideEx.MenuItemID);
                                        sidePortionSize = (Enums.CartItemSize)defMenu.MenuItemSizeID;
                                        prefsString     = hccCartDefaultMenuExPref.GetPrefsBy(sideEx.DefaultMenuExceptID)
                                                          .Select(a => a.Name).DefaultIfEmpty("None").Aggregate((c, d) => c + ", " + d);
                                    }

                                    if (sideItem != null)
                                    {
                                        switch (sideDefMenu.Ordinal)
                                        {
                                        case 1:
                                            {
                                                newItem.Side1 = sideItem.Name;
                                                if (!string.IsNullOrWhiteSpace(prefsString))
                                                {
                                                    newItem.Side1 += " - " + prefsString;
                                                }
                                                break;
                                            }

                                        case 2:
                                            {
                                                newItem.Side2 = sideItem.Name;
                                                if (!string.IsNullOrWhiteSpace(prefsString))
                                                {
                                                    newItem.Side2 += " - " + prefsString;
                                                }
                                                break;
                                            }

                                        default:
                                            {
                                                // not supported side
                                                break;
                                            }
                                        }
                                    }

                                    if (sidePortionSize == Enums.CartItemSize.NoSize)
                                    {
                                        sidePortionSize = Enums.CartItemSize.RegularSize;
                                    }

                                    if (string.IsNullOrWhiteSpace(sideStr))
                                    {
                                        if (sideItem != null)
                                        {
                                            sideStr = sideItem.Name;
                                        }

                                        //sideStr += " - " + sidePortionSize.ToString();

                                        if (!string.IsNullOrWhiteSpace(prefsString))
                                        {
                                            sideStr += " - " + prefsString;
                                        }
                                    }
                                    else
                                    {
                                        if (sideItem != null)
                                        {
                                            sideStr += ", " + sideItem.Name;
                                        }

                                        //sideStr += " - " + sidePortionSize.ToString();

                                        if (!string.IsNullOrWhiteSpace(prefsString))
                                        {
                                            sideStr += " - " + prefsString;
                                        }
                                    }
                                });

                                if (string.IsNullOrWhiteSpace(sideStr) || string.Equals(sideStr, "None", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    sideStr = string.Empty;
                                }

                                newItem.Sides = sideStr;
                                retItems.Add(newItem);
                            }
                            else // it is a side item
                            { // skip it
                            }
                        }
                    }
                }

                return(retItems);
            }
            catch (Exception)
            {
                throw;
            }
        }