// Get next unassigned Loyay card
        public void GetNextCard(Card card)
        {
            using (LoyayContext context = new LoyayContext())
            {
                SqlParameter prog = new SqlParameter("@ProgramID", 200000174)
                {
                    SqlDbType = SqlDbType.Int
                };
                SqlParameter cat = new SqlParameter("@CategoryID", 1823)
                {
                    SqlDbType = SqlDbType.Int
                };
                var cardID = new SqlParameter("@CardID", SqlDbType.Int)
                {
                    Direction = System.Data.ParameterDirection.Output
                };

                context.Database.ExecuteSqlCommand(
                    "GetNextAvailableCardIDByLocationCategory_S_EC @ProgramID, @CategoryID, @CardID out",
                    prog, cat, cardID);

                card.CardID = (int)cardID.Value;
            }

            ActivateCardEnrollment(card);
        }
        // check LoyaltyDetailRewardsOptIn_T_EC for MobileNumber to check for prior optin
        public int CheckForLDROptIn(Card card)
        {
            using (LoyayContext context = new LoyayContext())
            {
                var priorOptIn = new LoyaltyDetailRewardOptIn();
                priorOptIn = context.LoyaltyDetailRewardOptIns
                             .Where(o => o.MobilePhone == card.CH_MPHONE)
                             .Where(o => o.LoyaltyDetailRewardSKUGroupID == 1017372 ||
                                    o.LoyaltyDetailRewardSKUGroupID == 1017370 ||
                                    o.LoyaltyDetailRewardSKUGroupID == 1017371 ||
                                    o.LoyaltyDetailRewardSKUGroupID == 1017555 ||
                                    o.LoyaltyDetailRewardSKUGroupID == 1017556 ||
                                    o.LoyaltyDetailRewardSKUGroupID == 1017539 ||
                                    o.LoyaltyDetailRewardSKUGroupID == 1017538 ||
                                    o.LoyaltyDetailRewardSKUGroupID == 1017537 ||
                                    o.LoyaltyDetailRewardSKUGroupID == 1017540 ||
                                    o.LoyaltyDetailRewardSKUGroupID == 1017541)
                             .FirstOrDefault();

                if (priorOptIn == null)
                {
                    return(0);
                }
                else
                {
                    return(-1);
                }
            }
        }
        public ActionResult HotSpotAccountLookup(Card card)
        {
            string cardNo = card.AccountNumber;

            if (card.AccountNumber != null)
            {
                if (card.AccountNumber.Length == 11)
                {
                    card.AccountNumber = "62714" + cardNo;
                }
                else if (card.AccountNumber.Length == 12)
                {
                    card.AccountNumber = "62714" + cardNo.Substring(0, 11);
                }
            }
            using (LoyayContext context = new LoyayContext())
            {
                Card oldCard = new Card();
                if (card.CH_MPHONE != null)
                {
                    oldCard = context.Cards
                              .Where(c => (c.CH_MPHONE == card.CH_MPHONE || c.CH_HPHONE == card.CH_MPHONE))
                              .Where(c => c.ProgramID == 76)
                              .Where(c => c.CH_STATUS == "P")
                              .OrderByDescending(o => o.AddDate)
                              .FirstOrDefault();
                }
                else if (card.AccountNumber != null)
                {
                    oldCard = context.Cards
                              .Where(c => c.AccountNumber == card.AccountNumber)
                              .Where(c => c.ProgramID == 76)
                              .Where(c => c.CH_STATUS == "P")
                              .OrderByDescending(o => o.AddDate)
                              .FirstOrDefault();
                }

                if (oldCard != null)
                {
                    SaveCardToSession(oldCard);
                    if (CheckForLDROptIn(card) != 0)
                    {
                        TempData["ErrorMessage"] = "You have already enrolled in Fresh Spot rewards. Thanks, you're good to go!";
                        return(RedirectToAction("PriorSignIn", "Home"));
                    }
                    else
                    {
                        return(RedirectToAction("Verify", "Home", oldCard));
                    }
                }
                else
                {
                    TempData["ErrorMessage"] = "No account was found. Try again, or sign up for a new account";
                    string url = string.Format("{0}#lookUpForm", Url.Action("Index", "Home"), 0);
                    return(Redirect(url));
                }
            }
        }
 public Card GetAcctNumberFromCardID(Card card)
 {
     using (LoyayContext context = new LoyayContext())
     {
         var dbCard = context.Cards.Where(x => x.CardID == card.CardID).FirstOrDefault();
         card.AccountNumber = dbCard.AccountNumber;
         return(card);
     }
 }
        public void LogError(Exception ex)
        {
            using (db = new LoyayContext())
            {
                ErrorLog log = new ErrorLog()
                {
                    ErrorTime    = DateTime.Now,
                    ErrorSource  = "Card Holder Website",
                    ErrorMessage = ex.Message,
                    ModuleName   = ex.Source,
                    TargetSite   = ex.TargetSite.ToString(),
                    StackTrace   = ex.StackTrace
                };

                db.ErrorLog.Add(log);
                db.SaveChanges();
            }
        }
        // Adds mobile number to Card record if different than record (at Verification Input for customer to confirm/change mobile number)
        public Card UpdateCardMobileNumber(Card card)
        {
            using (LoyayContext context = new LoyayContext())
            {
                var dbCard = context.Cards.SingleOrDefault(c => c.CardID == card.CardID);
                dbCard.CH_MPHONE = card.CH_MPHONE;
                context.SaveChanges();

                if (CheckForLDROptIn(card) == 0)
                {
                    return(card);
                }
                else
                {
                    RedirectToAction("PriorSignIn", "Home", card);
                    return(card);
                }
            }
        }
 // check Card table for existing Loyay card
 public void CheckForLoyayCard(Card card)
 {
     using (LoyayContext context = new LoyayContext())
     {
         var loyayCard = new Card();
         loyayCard = context.Cards
                     .Where(c => c.CH_STATUS == "P" && c.CH_MPHONE == card.CH_MPHONE && c.ProgramID == 200000174)
                     .OrderByDescending(o => o.AddDate)
                     .FirstOrDefault();
         if (loyayCard != null)
         {
             loyayCard.Email = card.Email;
             SaveCardToSession(loyayCard);
             RedirectToAction("Verify", "Home", loyayCard);
         }
         else
         {
             GetNextCard(card);
         }
     }
 }