public void Save()
 {
     try
     {
         using (AHT_MainDataContext context = new AHT_MainDataContext(ConfigurationManager.ConnectionStrings["AHT_MainConnectionString"].ConnectionString))
         {
             if (this.id > 0)
             {
                 Referral referral = (from a in context.Referrals
                                      where a.id == this.id
                                      select a).SingleOrDefault();
                 GetData(referral);
             }
             else
             {
                 context.Referrals.InsertOnSubmit(this);
             }
             context.SubmitChanges();
         }
     }
     catch (Exception ex)
     {
         throw new ObjectSaveException(ex.Message);
     }
 }
 private void GetData(Referral referral)
 {
     referral.id                 = this.id;
     referral.datecreated        = this.datecreated;
     referral.memberid           = this.memberid;
     referral.ccmember           = this.ccmember;
     referral.messagefrommember  = this.messagefrommember;
     referral.friendname         = this.friendname;
     referral.friendemail        = this.friendemail;
     referral.keycode            = this.keycode;
     referral.pubcode            = this.pubcode;
     referral.dateredeemed       = this.dateredeemed;
     referral.friendid           = this.friendid;
     referral.dateexpires        = this.dateexpires;
     referral.subscriptionlength = this.subscriptionlength;
     referral.testrecord         = this.testrecord;
     referral.reminderemailid    = this.reminderemailid;
 }
        public void PASS_ReferralRedeem()
        {
            try
            {
                HarperLINQ.Referral refer = new HarperLINQ.Referral("*****@*****.**");
                //BaseResponse baseResponse = new SubscriptionLogic().RedeemReferralSubscription(refer.id, "TESTACCOUNT", "TESTACCOUNT",
                //    "*****@*****.**", "US", "12 add121", "", "San Antonio", "TX", "78247", false, "45michlle3262", "testing");

                //string dsd = Cryptography.EncryptData(refer.id.ToString());
                //string sdd = Cryptography.EncryptData("testing");
                MembershipService.MembershipService client = new Tests.MembershipService.MembershipService();
                MembershipService.BaseResponse      actual = client.RedeemReferral(Cryptography.EncryptData(refer.id.ToString()), "TESTACCOUNT", "TESTACCOUNT",
                                                                                   "*****@*****.**", "US", "12 add121", "", "San Antonio", "TX", "78247", false, "45michlle3262", Cryptography.EncryptData("testing"));
                int i = actual.Messages.Count();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                Assert.AreEqual(true, true);//test passed
            }
        }
 public Referral(string email_in)
 {
     try
     {
         using (AHT_MainDataContext context = new AHT_MainDataContext(ConfigurationManager.ConnectionStrings["AHT_MainConnectionString"].ConnectionString))
         {
             Referral referral = (from a in context.Referrals
                                  where a.friendemail == email_in
                                  select a).SingleOrDefault();
             if (referral != null)
             {
                 SetData(referral);
             }
         }
     }
     catch (Exception ex)
     {
         LogEntry = new tbl_AppEventLog(string.Empty, "BusinessLogic", "DataLoadException",
                                        "Error loading tbl_Referral object", string.Format("email_in is '{0}'", email_in),
                                        ex.Message, string.Empty, "Constructor: tbl_Referral(string email_in) ");
         LogEntry.Save();
         throw new DataLoadException(ex.Message);
     }
 }
 private void GetData(Referral referral)
 {
     referral.id = this.id;
     referral.datecreated = this.datecreated;
     referral.memberid = this.memberid;
     referral.ccmember = this.ccmember;
     referral.messagefrommember = this.messagefrommember;
     referral.friendname = this.friendname;
     referral.friendemail = this.friendemail;
     referral.keycode = this.keycode;
     referral.pubcode = this.pubcode;
     referral.dateredeemed = this.dateredeemed;
     referral.friendid = this.friendid;
     referral.dateexpires = this.dateexpires;
     referral.subscriptionlength = this.subscriptionlength;
     referral.testrecord = this.testrecord;
     referral.reminderemailid = this.reminderemailid;
 }
        public void PASS_ReferralRedeem()
        {
            try
            {
                HarperLINQ.Referral refer = new HarperLINQ.Referral("*****@*****.**");
                //BaseResponse baseResponse = new SubscriptionLogic().RedeemReferralSubscription(refer.id, "TESTACCOUNT", "TESTACCOUNT",
                //    "*****@*****.**", "US", "12 add121", "", "San Antonio", "TX", "78247", false, "45michlle3262", "testing");
                
                //string dsd = Cryptography.EncryptData(refer.id.ToString());
                //string sdd = Cryptography.EncryptData("testing");
                MembershipService.MembershipService client = new Tests.MembershipService.MembershipService();
                MembershipService.BaseResponse actual = client.RedeemReferral(Cryptography.EncryptData(refer.id.ToString()), "TESTACCOUNT", "TESTACCOUNT", 
                    "*****@*****.**", "US", "12 add121", "", "San Antonio", "TX", "78247", false, "45michlle3262", Cryptography.EncryptData("testing"));
                int i = actual.Messages.Count();

            }
            catch (Exception ex)
            {
                string s = ex.Message;
                Assert.AreEqual(true, true);//test passed 
            }
        }
 partial void DeleteReferral(Referral instance);
 partial void UpdateReferral(Referral instance);
 partial void InsertReferral(Referral instance);
    protected void Page_Load(object sender, EventArgs e)
    {
        phContent.Controls.Clear();
        LiteralControl ctl = new LiteralControl();
        List<string> referralIds = new List<string>();
        List<HarperLINQ.Referral> referralObjects = new List<HarperLINQ.Referral>();
        if (string.IsNullOrEmpty(Request["Referrals"]))
        {
            ctl.Text = "<p class=\"error-message\">An error has occurred.  Please contact the membership department at <a href=\"mailto:[email protected]\">[email protected]</a></p>";            
        }
        else
        {
            #region get referral objects
            try
            {
                referralIds = Request["Referrals"].Split('|').ToList();
                foreach (string goodReferral in referralIds)
                {
                    int referralid = int.Parse(HarperCRYPTO.Cryptography.DecryptData(goodReferral));
                    HarperLINQ.Referral referral = new HarperLINQ.Referral(referralid);
                    if (referral.id > 0
                        && !string.IsNullOrEmpty(referral.friendemail)
                        && referral.dateexpires >= DateTime.Now
                        && referral.dateredeemed == null)
                    {
                        referralObjects.Add(referral);
                    }
                }
            }
            catch { }
            #endregion

            #region no valid referrals(all are invalid, redeemed, or expired) - show error
            if(referralObjects.Count() <= 0)
            {
                ctl.Text = "<p class=\"error-message\">An error has occurred.  Please contact the membership department at <a href=\"mailto:[email protected]\">[email protected]</a></p>";
            }
            #endregion

            #region no errors - show pdf download
            else if (string.IsNullOrEmpty(Request["Errors"]))
            {
                ctl.Text = "<a href=\"http://www.andrewharper.com\"><img alt=\"header image\" src=\"images/Referral_ThankYouButton.jpg\" border=\"0\" /></a>";
                //ctl.Text = "<a href=\"http://viewer.zmags.com/publication/c0a78eee#/c0a78eee/1\" target=\"_blank\"><img alt=\"header image\" src=\"images/Referral_ThankYouButton.jpg\" border=\"0\" /></a>";
                if (referralObjects.Count() > 1)
                {
                    ctl.Text += "<h1>Your invitations have been sent.</h1>";
                }
                else
                {
                    ctl.Text += "<h1>Your invitation has been sent.</h1>";
                }
            }
            #endregion

            #region at least one good referral and some errors show pdf download and any errors
            else
            {
                #region show errors
                string content = "<a href=\"http://www.andrewharper.com\"><img alt=\"header image\" src=\"images/Referral_ThankYouButton.jpg\" border=\"0\" /></a><p class=\"error-message\">We were unable to refer all of your friends. Please review the following errors.</p><p>";
                //string content = "<a href=\"http://viewer.zmags.com/publication/c0a78eee#/c0a78eee/1\" target=\"_blank\"><img alt=\"header image\" src=\"images/Referral_ThankYouButton.jpg\" border=\"0\" /></a><p class=\"error-message\">We were unable to refer all of your friends. Please review the following errors.</p><p>";
                foreach (string err in Request["Errors"].Split('|'))
                {
                    content += string.Format("{0}<br />", Server.UrlDecode(err));
                }
                content = content.Substring(0, content.LastIndexOf("<br />"));
                content += "</p>";
                #endregion

                #region show successful referrals
                string friendList = string.Empty;
                int i = 0;
                foreach (HarperLINQ.Referral referral in referralObjects)
                {
                    i++;                    
                    if (i == referralObjects.Count())
                    {
                        friendList += referral.friendname;
                    }
                    else if (i == referralObjects.Count()-1)
                    {
                        friendList += string.Format("{0} and ", referral.friendname);
                    }
                    else
                    {
                        friendList += string.Format("{0}, ", referral.friendname);
                    }
                }
                if (referralObjects.Count > 1)
                {
                    content += string.Format("<h1>Your invitations to {0} have been sent.</h1>", friendList);
                }
                else
                {
                    content += string.Format("<h1>Your invitation to {0} has been sent.</h1>", friendList);
                }
                #endregion

                ctl.Text = content;
            }
            #endregion
        }
        phContent.Controls.Add(ctl);
    }
        public BaseResponse CreateReferral(string cusid, string membername, string memberemail, string keycode,
             string pubcode, string friendname, string friendemailaddress, bool ccmember)
        {
            methodName = "CreateReferral";
            
            List<Message> errors = new List<Message>();
            Referral referral = new Referral();
            try
            {
                tbl_Customer member = new tbl_Customer(int.Parse(cusid), false);

                #region validate input
                if (member == null)
                {
                    errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralInputValidationException", BusinessLogicStrings.memberDoesNotExistError, cusid, "", null));
                }
                if (member.SfgId == null)
                {
                    errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralInputValidationException", BusinessLogicStrings.invalidMemberIdError, "", "", null));
                }
                if (string.IsNullOrEmpty(membername))
                {
                    errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralInputValidationException", BusinessLogicStrings.missingMemberNameError, "", "", null));
                }
                if (ccmember && string.IsNullOrEmpty(memberemail))
                {
                    errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralInputValidationException", BusinessLogicStrings.missingMemberEmailError, "", "", null));
                }
                if (string.IsNullOrEmpty(keycode))
                {
                    errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralInputValidationException", BusinessLogicStrings.missingKeycodeError, "", "", null));
                }
                if (string.IsNullOrEmpty(pubcode))
                {
                    errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralInputValidationException", BusinessLogicStrings.missingPubcodeError, "", "", null));
                }
                if (string.IsNullOrEmpty(friendname))
                {
                    errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralInputValidationException", BusinessLogicStrings.missingFriendNameError, "", "", null));
                }
                if (string.IsNullOrEmpty(friendemailaddress))
                {
                    errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralInputValidationException", BusinessLogicStrings.missingFriendEmailError, "", "", null));
                }
                #endregion

                #region enforce business rules
                tbl_Customer friend = new tbl_Customer(friendemailaddress, false);
                try
                {
                    Referral existing_referral = new Referral(friendemailaddress);

                    if (memberemail == friendemailaddress)
                    {
                        errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralBusinessRuleException", BusinessLogicStrings.cannotReferSelfError, "", "", null));
                    }
                    else if (friend.cusID > 0)
                    {
                        errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralBusinessRuleException", BusinessLogicStrings.existingMemberError, "", "", null));
                    }
                    else if (existing_referral.dateredeemed == null)
                    {
                        if (existing_referral.id > 0 && existing_referral.dateexpires.CompareTo(DateTime.Now) >= 0)
                        {
                            errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralBusinessRuleException", BusinessLogicStrings.existingReferralError, "", "", null));
                        }
                    }
                    if (errors.Count <= 0)
                    {
                        GetMemberResponse checkFriend = (GetMemberByUserName(friendemailaddress).TypedResponse as GetMemberResponse);
                        if (checkFriend != null && (checkFriend.MemberFound || checkFriend.WebAccountFound))
                        {
                            errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralBusinessRuleException", BusinessLogicStrings.freindEmailInUseSFGError, "", "", null));
                        }
                    }
                }
                catch (HarperLINQ.DataLoadException dle)
                {
                    errors.Add(new Message(MessageSources.AndrewHarper, 0, "CreateReferralBusinessRuleException", BusinessLogicStrings.freindEmailInUseAHError, "", "", null));
                }
                if (errors.Count() > 0)
                {
                    string errstring = string.Empty;
                    foreach (Message msg in errors)
                    {
                        string sfgmessages = string.Empty;
                        if (msg.SfgMessages != null)
                        {
                            foreach (string sfgmsg in msg.SfgMessages)
                            {
                                sfgmessages += string.Format("SFGMessage: {0}", sfgmsg);
                            }
                        }
                        errstring += string.Format("AhMessage: {0}|| {1}", new object[] { msg.AhMessage, sfgmessages });
                    }
                    throw new Exception(string.Format("Error creating referral: [{0}]", errstring));
                }
                #endregion

                ReferralOffer offer = new ReferralOffer(keycode, pubcode);
                

                #region save referral
                referral = new Referral(int.Parse(cusid), membername, memberemail, keycode, pubcode, friendname, friendemailaddress, ccmember, offer.triallengthinmonths, offer.offerexpiresmonths);
                referral.Save();
                #endregion

                #region send email
                //create mailer and sent mail
                Mailer mailer = new Mailer();

                string ccEmail = memberemail; 

                if (!ccmember)
                {
                    ccEmail = string.Empty;
                }
                
                mailer.SendEmail(ConfigurationManager.AppSettings["mailserviceuser"], 
                    ConfigurationManager.AppSettings["mailservicepwd"],
                    string.Format("Membership Invitation from {0}", membername), 
                    ConfigurationManager.AppSettings["referemailfrom"],
                    friendemailaddress,
                    ccEmail, 
                    string.Empty, 
                    referral.GetReferralEmailBody(), 
                    true, 
                    ConfigurationManager.AppSettings["smtpserver"]);
                #endregion

            }
            catch (Exception ex)
            {
                LogMethodError(methodName, ex);
            }
            if (baseResponse != null && baseResponse.Messages != null)
            {
                foreach (Message error in errors)
                {
                    baseResponse.Messages.Add(error);
                }
            }
            if (baseResponse.Messages.Count() <= 0 && referral != null && referral.id >= 0)
            {
                #region create typed response
                baseResponse.TypedResponse = new ReferralResponse();
                baseResponse.TypedResponse.Success = true;
                (baseResponse.TypedResponse as ReferralResponse).referralid = referral.id;
                #endregion
            }
            else
            {
                baseResponse.TypedResponse = new ReferralResponse();
                baseResponse.TypedResponse.Success = false;
            }
            
            return baseResponse;
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        phContent.Controls.Clear();
        LiteralControl             ctl             = new LiteralControl();
        List <string>              referralIds     = new List <string>();
        List <HarperLINQ.Referral> referralObjects = new List <HarperLINQ.Referral>();

        if (string.IsNullOrEmpty(Request["Referrals"]))
        {
            ctl.Text = "<p class=\"error-message\">An error has occurred.  Please contact the membership department at <a href=\"mailto:[email protected]\">[email protected]</a></p>";
        }
        else
        {
            #region get referral objects
            try
            {
                referralIds = Request["Referrals"].Split('|').ToList();
                foreach (string goodReferral in referralIds)
                {
                    int referralid = int.Parse(HarperCRYPTO.Cryptography.DecryptData(goodReferral));
                    HarperLINQ.Referral referral = new HarperLINQ.Referral(referralid);
                    if (referral.id > 0 &&
                        !string.IsNullOrEmpty(referral.friendemail) &&
                        referral.dateexpires >= DateTime.Now &&
                        referral.dateredeemed == null)
                    {
                        referralObjects.Add(referral);
                    }
                }
            }
            catch { }
            #endregion

            #region no valid referrals(all are invalid, redeemed, or expired) - show error
            if (referralObjects.Count() <= 0)
            {
                ctl.Text = "<p class=\"error-message\">An error has occurred.  Please contact the membership department at <a href=\"mailto:[email protected]\">[email protected]</a></p>";
            }
            #endregion

            #region no errors - show pdf download
            else if (string.IsNullOrEmpty(Request["Errors"]))
            {
                ctl.Text = "<a href=\"http://www.andrewharper.com\"><img alt=\"header image\" src=\"images/Referral_ThankYouButton.jpg\" border=\"0\" /></a>";
                //ctl.Text = "<a href=\"http://viewer.zmags.com/publication/c0a78eee#/c0a78eee/1\" target=\"_blank\"><img alt=\"header image\" src=\"images/Referral_ThankYouButton.jpg\" border=\"0\" /></a>";
                if (referralObjects.Count() > 1)
                {
                    ctl.Text += "<h1>Your invitations have been sent.</h1>";
                }
                else
                {
                    ctl.Text += "<h1>Your invitation has been sent.</h1>";
                }
            }
            #endregion

            #region at least one good referral and some errors show pdf download and any errors
            else
            {
                #region show errors
                string content = "<a href=\"http://www.andrewharper.com\"><img alt=\"header image\" src=\"images/Referral_ThankYouButton.jpg\" border=\"0\" /></a><p class=\"error-message\">We were unable to refer all of your friends. Please review the following errors.</p><p>";
                //string content = "<a href=\"http://viewer.zmags.com/publication/c0a78eee#/c0a78eee/1\" target=\"_blank\"><img alt=\"header image\" src=\"images/Referral_ThankYouButton.jpg\" border=\"0\" /></a><p class=\"error-message\">We were unable to refer all of your friends. Please review the following errors.</p><p>";
                foreach (string err in Request["Errors"].Split('|'))
                {
                    content += string.Format("{0}<br />", Server.UrlDecode(err));
                }
                content  = content.Substring(0, content.LastIndexOf("<br />"));
                content += "</p>";
                #endregion

                #region show successful referrals
                string friendList = string.Empty;
                int    i          = 0;
                foreach (HarperLINQ.Referral referral in referralObjects)
                {
                    i++;
                    if (i == referralObjects.Count())
                    {
                        friendList += referral.friendname;
                    }
                    else if (i == referralObjects.Count() - 1)
                    {
                        friendList += string.Format("{0} and ", referral.friendname);
                    }
                    else
                    {
                        friendList += string.Format("{0}, ", referral.friendname);
                    }
                }
                if (referralObjects.Count > 1)
                {
                    content += string.Format("<h1>Your invitations to {0} have been sent.</h1>", friendList);
                }
                else
                {
                    content += string.Format("<h1>Your invitation to {0} has been sent.</h1>", friendList);
                }
                #endregion

                ctl.Text = content;
            }
            #endregion
        }
        phContent.Controls.Add(ctl);
    }