void SaveLedger(hccLedger ledger, hccCart CurrentCart, hccUserProfile profile)
        {
            ledger = new hccLedger
            {
                PaymentDue        = CurrentCart.PaymentDue,
                TotalAmount       = CurrentCart.TotalAmount,
                AspNetUserID      = CurrentCart.AspNetUserID.Value,
                AsscCartID        = CurrentCart.CartID,
                CreatedBy         = (Guid)Helpers.LoggedUser.ProviderUserKey,
                CreatedDate       = DateTime.Now,
                Description       = "Cart Order Payment - Purchase Number: " + CurrentCart.PurchaseNumber.ToString() + " - from re-snapshot of order.",
                TransactionTypeID = (int)Enums.LedgerTransactionType.Purchase
            };

            if (CurrentCart.IsTestOrder)
            {
                ledger.Description += " - Test Mode";
            }

            if (CurrentCart.CreditAppliedToBalance > 0)
            {
                profile.AccountBalance   = profile.AccountBalance - CurrentCart.CreditAppliedToBalance;
                ledger.CreditFromBalance = CurrentCart.CreditAppliedToBalance;
            }

            hccLedger lastLedger        = hccLedger.GetByMembershipID(profile.MembershipID, null).OrderByDescending(a => a.CreatedDate).FirstOrDefault();
            bool      isDuplicateLedger = false;

            if (lastLedger != null)
            {
                if (ledger.CreatedBy == lastLedger.CreatedBy &&
                    ledger.CreditFromBalance == lastLedger.CreditFromBalance &&
                    ledger.Description == lastLedger.Description &&
                    ledger.PaymentDue == lastLedger.PaymentDue &&
                    ledger.TransactionTypeID == lastLedger.TransactionTypeID &&
                    ledger.TotalAmount == lastLedger.TotalAmount)
                {
                    isDuplicateLedger = true;
                }
            }

            if (!isDuplicateLedger)
            {
                ledger.PostBalance = profile.AccountBalance;
                ledger.Save();
                profile.Save();
            }
        }
        private void ProcessNewOrder(int cartId)
        {
            //bool dupTransaction = false;
            hccCart CurrentCart = null;

            try
            {
                // TODO: Check the cart for more then one recurring item

                CurrentCart = hccCart.GetById(cartId);

                hccUserProfile profile  = hccUserProfile.GetParentProfileBy(CurrentCart.AspNetUserID.Value);
                hccAddress     billAddr = null;

                var ppName =
                    hccUserProfile.GetParentProfileBy((Guid)hccCart.GetById(cartId).AspNetUserID).ParentProfileName;
                var pName = hccUserProfile.GetParentProfileBy((Guid)hccCart.GetById(cartId).AspNetUserID).ASPUser.Email;

                //if (CurrentCart.StatusID == (int)Enums.CartStatus.Unfinalized)
                if (CurrentCart.StatusID == (int)Enums.CartStatus.Unfinalized)
                {
                    if (profile != null)
                    {
                        AuthNetConfig ANConfig = new AuthNetConfig();
                        hccUserProfilePaymentProfile activePaymentProfile = profile.ActivePaymentProfile;
                        bool isDuplicateTransaction = false;
                        bool isAuthNet = false;

                        if (ANConfig.Settings.TestMode)
                        {
                            CurrentCart.IsTestOrder = true;
                        }

                        // Check for existing account balance, calculate total balance
                        if (CurrentCart.PaymentDue > 0.00m)
                        {
                            try
                            {
                                // if total balance remains
                                CustomerInformationManager cim = new CustomerInformationManager();

                                if (activePaymentProfile != null)
                                {
                                    // do not validate, per Duncan, YouTrack HC1-339
                                    //string valProfile = cim.ValidateProfile(profile.AuthNetProfileID,
                                    //    activePaymentProfile.AuthNetPaymentProfileID, AuthorizeNet.ValidationMode.TestMode);

                                    AuthorizeNet.Order order = new AuthorizeNet.Order(profile.AuthNetProfileID,
                                                                                      activePaymentProfile.AuthNetPaymentProfileID, null);

                                    // charge CIM account with PaymentDue balance
                                    order.Amount        = CurrentCart.PaymentDue;
                                    order.InvoiceNumber = CurrentCart.PurchaseNumber.ToString();
                                    order.Description   = "Healthy Chef Creations Purchase #" +
                                                          CurrentCart.PurchaseNumber.ToString();
                                    // Add a PO number to make purchases unique as subsequent transactions with the same amount are rejected by Auth.net as duplicate
                                    // order.PONumber = "PO" + CurrentCart.PurchaseNumber.ToString();

                                    AuthorizeNet.IGatewayResponse rsp = cim.AuthorizeAndCapture(order);

                                    try
                                    {
                                        CurrentCart.AuthNetResponse = rsp.ResponseCode + "|" + rsp.Approved.ToString()
                                                                      + "|" + rsp.AuthorizationCode + "|" +
                                                                      rsp.InvoiceNumber + "|" + rsp.Message
                                                                      + "|" + rsp.TransactionID + "|" +
                                                                      rsp.Amount.ToString() + "|" + rsp.CardNumber;
                                    }
                                    catch (Exception)
                                    {
                                    }

                                    if (rsp.ResponseCode.StartsWith("1"))
                                    {
                                        CurrentCart.ModifiedBy       = (Guid)Helpers.LoggedUser.ProviderUserKey;
                                        CurrentCart.ModifiedDate     = DateTime.Now;
                                        CurrentCart.PurchaseBy       = (Guid)Helpers.LoggedUser.ProviderUserKey;
                                        CurrentCart.PurchaseDate     = DateTime.Now;
                                        CurrentCart.PaymentProfileID = activePaymentProfile.PaymentProfileID;
                                        CurrentCart.StatusID         = (int)Enums.CartStatus.Paid;

                                        isAuthNet = true;
                                    }
                                    else if (rsp.Message.Contains("E00027")) // Duplicate transaction
                                    {
                                        order = new AuthorizeNet.Order(profile.AuthNetProfileID,
                                                                       activePaymentProfile.AuthNetPaymentProfileID, null)
                                        {
                                            Amount = CurrentCart.PaymentDue - .01m,
                                            // Subtract a penny from payment to make the value distinct
                                            InvoiceNumber = CurrentCart.PurchaseNumber.ToString(),
                                            Description   =
                                                "Healthy Chef Creations Purchase #" +
                                                CurrentCart.PurchaseNumber.ToString()
                                        };

                                        // charge CIM account with PaymentDue balance
                                        rsp = cim.AuthorizeAndCapture(order);

                                        try
                                        {
                                            CurrentCart.AuthNetResponse = rsp.ResponseCode + "|" +
                                                                          rsp.Approved.ToString()
                                                                          + "|" + rsp.AuthorizationCode + "|" +
                                                                          rsp.InvoiceNumber + "|" + rsp.Message
                                                                          + "|" + rsp.TransactionID + "|" +
                                                                          rsp.Amount.ToString() + "|" + rsp.CardNumber;

                                            if (rsp.ResponseCode.StartsWith("1"))
                                            {
                                                //CurrentCart.PaymentDue = CurrentCart.PaymentDue - .01m;
                                                CurrentCart.ModifiedBy       = (Guid)Helpers.LoggedUser.ProviderUserKey;
                                                CurrentCart.ModifiedDate     = DateTime.Now;
                                                CurrentCart.PurchaseBy       = (Guid)Helpers.LoggedUser.ProviderUserKey;
                                                CurrentCart.PurchaseDate     = DateTime.Now;
                                                CurrentCart.PaymentProfileID = activePaymentProfile.PaymentProfileID;
                                                CurrentCart.StatusID         = (int)Enums.CartStatus.Paid;

                                                isAuthNet = true;
                                            }
                                            else
                                            {
                                                lblConfirmFeedback.Text += "Authorize.Net " + rsp.Message + @" (" +
                                                                           ppName + @", " + pName + @")" + @"<br />";
                                                // CurrentCart.AuthNetResponse;
                                                lblConfirmFeedback.ForeColor = System.Drawing.Color.Red;
                                            }
                                        }
                                        catch (Exception)
                                        {
                                        }
                                    }
                                    else
                                    {
                                        lblConfirmFeedback.Text += "Authorize.Net " + rsp.Message + @" (" + ppName +
                                                                   @", " + pName + @")" + @"<br />";
                                        // CurrentCart.AuthNetResponse;
                                        lblConfirmFeedback.ForeColor = System.Drawing.Color.Red;
                                    }
                                    CurrentCart.Save();
                                }
                                else
                                {
                                    lblConfirmFeedback.Text += "No payment profile found." + @" (" + ppName + @", " +
                                                               pName + @")" + @"<br />";
                                }
                            }
                            catch (Exception ex)
                            {
                                lblConfirmFeedback.Text += "Authorize.Net " + ex.Message + @" (" + ppName + @", " +
                                                           pName + @")" + @"<br />";
                                lblConfirmFeedback.ForeColor = System.Drawing.Color.Red;
                                if (ex is InvalidOperationException)
                                {
                                    if (CurrentCart.IsTestOrder)
                                    {
                                        CurrentCart.ModifiedBy       = (Guid)Helpers.LoggedUser.ProviderUserKey;
                                        CurrentCart.ModifiedDate     = DateTime.Now;
                                        CurrentCart.PaymentProfileID = activePaymentProfile.PaymentProfileID;
                                        CurrentCart.AuthNetResponse  = ex.Message;
                                        CurrentCart.StatusID         = (int)Enums.CartStatus.Unfinalized;
                                        CurrentCart.Save();
                                    }
                                    else
                                    {
                                        BayshoreSolutions.WebModules.WebModulesAuditEvent.Raise(ex.Message, this, ex);
                                        lblConfirmFeedback.Visible = true;
                                        lblConfirmFeedback.Text   += "Authorize.Net " + ex.Message + @" (" + ppName +
                                                                     @", " + pName + @")" + @"<br />";
                                        lblConfirmFeedback.ForeColor = System.Drawing.Color.Red;
                                    }
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            // no balance left to pay on order, set as paid
                            CurrentCart.AuthNetResponse = "Paid with account balance.";
                            CurrentCart.ModifiedBy      = (Guid)Helpers.LoggedUser.ProviderUserKey;
                            CurrentCart.ModifiedDate    = DateTime.Now;
                            CurrentCart.PurchaseBy      = (Guid)Helpers.LoggedUser.ProviderUserKey;
                            CurrentCart.PurchaseDate    = DateTime.Now;
                            CurrentCart.StatusID        = (int)Enums.CartStatus.Paid;
                            CurrentCart.Save();
                        }

                        if ((Enums.CartStatus)CurrentCart.StatusID == Enums.CartStatus.Paid)
                        //&& !isDuplicateTransaction
                        {
                            hccLedger ledger = new hccLedger
                            {
                                //PaymentDue = dupTransaction ? CurrentCart.PaymentDue : CurrentCart.PaymentDue - .01m,
                                //TotalAmount = dupTransaction ? CurrentCart.TotalAmount : CurrentCart.TotalAmount - .01m,
                                PaymentDue   = CurrentCart.PaymentDue,
                                TotalAmount  = CurrentCart.TotalAmount,
                                AspNetUserID = CurrentCart.AspNetUserID.Value,
                                AsscCartID   = CurrentCart.CartID,
                                CreatedBy    = (Guid)Helpers.LoggedUser.ProviderUserKey,
                                CreatedDate  = DateTime.Now,
                                Description  =
                                    "Cart Order Payment - Purchase Number: " + CurrentCart.PurchaseNumber.ToString(),
                                TransactionTypeID = (int)Enums.LedgerTransactionType.Purchase
                            };

                            if (CurrentCart.IsTestOrder)
                            {
                                ledger.Description += " - Test Mode";
                            }

                            if (CurrentCart.CreditAppliedToBalance > 0)
                            {
                                profile.AccountBalance   = profile.AccountBalance - CurrentCart.CreditAppliedToBalance;
                                ledger.CreditFromBalance = CurrentCart.CreditAppliedToBalance;
                            }

                            hccLedger lastLedger =
                                hccLedger.GetByMembershipID(profile.MembershipID, null)
                                .OrderByDescending(a => a.CreatedDate)
                                .FirstOrDefault();
                            bool isDuplicateLedger = false;

                            if (lastLedger != null)
                            {
                                if (ledger.CreatedBy == lastLedger.CreatedBy &&
                                    ledger.CreditFromBalance == lastLedger.CreditFromBalance &&
                                    ledger.Description == lastLedger.Description &&
                                    ledger.PaymentDue == lastLedger.PaymentDue &&
                                    ledger.TransactionTypeID == lastLedger.TransactionTypeID &&
                                    ledger.TotalAmount == lastLedger.TotalAmount)
                                {
                                    isDuplicateLedger = true;
                                }
                            }

                            if (!isDuplicateLedger)
                            {
                                ledger.PostBalance = profile.AccountBalance;
                                ledger.Save();
                                profile.Save();

                                // create snapshot here
                                hccCartSnapshot snap = new hccCartSnapshot
                                {
                                    CartId                  = cartId,
                                    MembershipId            = profile.MembershipID,
                                    LedgerId                = ledger.LedgerID,
                                    AccountBalance          = profile.AccountBalance,
                                    AuthNetProfileId        = profile.AuthNetProfileID,
                                    CreatedBy               = (Guid)Helpers.LoggedUser.ProviderUserKey,
                                    CreatedDate             = DateTime.Now,
                                    DefaultCouponId         = profile.DefaultCouponId,
                                    Email                   = profile.ASPUser.Email,
                                    FirstName               = profile.FirstName,
                                    LastName                = profile.LastName,
                                    ProfileName             = profile.ProfileName,
                                    AuthNetPaymentProfileId =
                                        (isAuthNet == true ? activePaymentProfile.AuthNetPaymentProfileID : string.Empty),
                                    CardTypeId = (isAuthNet == true ? activePaymentProfile.CardTypeID : 0),
                                    CCLast4    = (isAuthNet == true ? activePaymentProfile.CCLast4 : string.Empty),
                                    ExpMon     = (isAuthNet == true ? activePaymentProfile.ExpMon : 0),
                                    ExpYear    = (isAuthNet == true ? activePaymentProfile.ExpYear : 0),
                                    NameOnCard = (isAuthNet == true ? activePaymentProfile.NameOnCard : string.Empty)
                                };
                                snap.Save();

                                hccUserProfile parentProfile =
                                    hccUserProfile.GetParentProfileBy(CurrentCart.AspNetUserID.Value);
                                if (parentProfile.BillingAddressID.HasValue)
                                {
                                    billAddr = hccAddress.GetById(parentProfile.BillingAddressID.Value);
                                }

                                hccAddress snapBillAddr = new hccAddress
                                {
                                    Address1              = billAddr.Address1,
                                    Address2              = billAddr.Address2,
                                    AddressTypeID         = (int)Enums.AddressType.BillingSnap,
                                    City                  = billAddr.City,
                                    Country               = billAddr.Country,
                                    DefaultShippingTypeID = billAddr.DefaultShippingTypeID,
                                    FirstName             = billAddr.FirstName,
                                    IsBusiness            = billAddr.IsBusiness,
                                    LastName              = billAddr.LastName,
                                    Phone                 = billAddr.Phone,
                                    PostalCode            = billAddr.PostalCode,
                                    State                 = billAddr.State,
                                    ProfileName           = parentProfile.ProfileName
                                };
                                snapBillAddr.Save();

                                // copy and replace of all addresses for snapshot
                                List <hccCartItem> cartItems = hccCartItem.GetBy(CurrentCart.CartID);

                                cartItems.ToList().ForEach(delegate(hccCartItem ci)
                                {
                                    hccAddress shipAddr = null;
                                    if (ci.UserProfile.ShippingAddressID.HasValue)
                                    {
                                        shipAddr = hccAddress.GetById(ci.UserProfile.ShippingAddressID.Value);
                                    }

                                    if (shipAddr != null)
                                    {
                                        hccAddress snapShipAddr = new hccAddress
                                        {
                                            Address1              = shipAddr.Address1,
                                            Address2              = shipAddr.Address2,
                                            AddressTypeID         = (int)Enums.AddressType.ShippingSnap,
                                            City                  = shipAddr.City,
                                            Country               = shipAddr.Country,
                                            DefaultShippingTypeID = shipAddr.DefaultShippingTypeID,
                                            FirstName             = shipAddr.FirstName,
                                            IsBusiness            = shipAddr.IsBusiness,
                                            LastName              = shipAddr.LastName,
                                            Phone                 = shipAddr.Phone,
                                            PostalCode            = shipAddr.PostalCode,
                                            State                 = shipAddr.State,
                                            ProfileName           = ci.UserProfile.ProfileName
                                        };
                                        snapShipAddr.Save();
                                        ci.SnapShipAddrId = snapShipAddr.AddressID;
                                    }

                                    ci.SnapBillAddrId = snapBillAddr.AddressID;
                                    ci.Save();
                                });


                                try
                                {
                                    Email.EmailController ec = new Email.EmailController();
                                    ec.SendMail_OrderConfirmationMerchant(profile.FirstName + " " + profile.LastName,
                                                                          CurrentCart.ToHtml(), cartId);
                                    ec.SendMail_OrderConfirmationCustomer(profile.ASPUser.Email,
                                                                          profile.FirstName + " " + profile.LastName, CurrentCart.ToHtml());
                                }
                                catch (Exception ex)
                                {
                                    BayshoreSolutions.WebModules.WebModulesAuditEvent.Raise("Send Mail Failed", this, ex);
                                } //throw; }

                                //if (IsForPublic)
                                //{
                                //    Response.Redirect(string.Format("~/cart/order-confirmation.aspx?pn={0}&tl={1}&tx={2}&ts={3}&ct={4}&st={5}&cy={6}",
                                //        CurrentCart.PurchaseNumber, CurrentCart.TotalAmount, CurrentCart.TaxableAmount, CurrentCart.ShippingAmount,
                                //        billAddr.City, billAddr.State, billAddr.Country), false);
                                //}
                                //else
                                //{
                                //    CurrentCart = hccCart.GetCurrentCart(profile.ASPUser);
                                //    CurrentCartId = CurrentCart.CartID;

                                //    pnlCartDisplay.Visible = true;
                                //    pnlConfirm.Visible = false;

                                //    Clear();
                                //    Bind();
                                //}
                                //OnCartSaved(new CartEventArgs(CurrentCartId));
                            }
                        }
                        //else
                        //{
                        //    BayshoreSolutions.WebModules.WebModulesAuditEvent.Raise("Duplicate transaction attempted: " + CurrentCart.PurchaseNumber.ToString(), this, new Exception("Duplicate transaction attempted by:" + Helpers.LoggedUser.UserName));
                        //}
                    }
                    else
                    {
                        Response.Redirect("~/login.aspx", true);
                    }
                }
                //else
                //{
                //if (IsForPublic)
                //{
                //    //Response.Redirect("~/cart/order-confirmation.aspx?cid=" + CurrentCartId.ToString(), false);
                //    Response.Redirect(string.Format("~/cart/order-confirmation.aspx?pn={0}&tl={1}&tx={2}&ts={3}&ct={4}&st={5}&cy={6}",
                //                    CurrentCart.PurchaseNumber, CurrentCart.TotalAmount, CurrentCart.TaxableAmount, CurrentCart.ShippingAmount,
                //                    billAddr.City, billAddr.State, billAddr.Country), false);
                //}
                //else
                //{
                //    CurrentCart = hccCart.GetCurrentCart(profile.ASPUser);
                //    CurrentCartId = CurrentCart.CartID;

                //    pnlCartDisplay.Visible = true;
                //    pnlConfirm.Visible = false;

                //    Clear();
                //    Bind();

                //    OnCartSaved(new CartEventArgs(CurrentCartId));
                //}
                //}
            }
            catch (Exception ex)
            {
                BayshoreSolutions.WebModules.WebModulesAuditEvent.Raise(ex.Data + " " + ex.InnerException, this,
                                                                        new Exception("Recurring order error in method ProcessNewOrder: " + Helpers.LoggedUser.UserName));
            }
        }
        protected override void SaveForm()
        {
            try
            {
                hccLedger ledger = new hccLedger
                {
                    AspNetUserID      = CurrentAspNetUserId,
                    CreatedBy         = (Guid)Helpers.LoggedUser.ProviderUserKey,
                    CreatedDate       = DateTime.Now,
                    Description       = txtXactDesc.Text.Trim(),
                    TransactionTypeID = int.Parse(ddlXactTypes.SelectedValue)
                };

                if (ledger.TransactionType == Enums.LedgerTransactionType.RedeemGiftCertificate)
                {
                    hccCartItem gcCartItem   = hccCartItem.GetGiftBy(txtXactGCRedeem.Text);
                    bool        updateLedger = false;

                    if (gcCartItem != null)
                    {
                        gcCartItem.Gift_RedeemedBy   = CurrentAspNetUserId;
                        gcCartItem.Gift_RedeemedDate = DateTime.Now;
                        gcCartItem.Save();
                        updateLedger = true;
                    }
                    else
                    {
                        ImportedGiftCert cert = ImportedGiftCert.GetBy(txtXactGCRedeem.Text);
                        if (cert != null)
                        {
                            cert.used_by   = gcCartItem.UserProfileID;
                            cert.date_used = DateTime.Now.ToString();
                            cert.is_used   = "Y";
                            cert.Save();
                            updateLedger = true;
                        }
                    }

                    if (updateLedger)
                    {
                        ledger.TotalAmount    = gcCartItem.ItemPrice;
                        ledger.GiftRedeemCode = gcCartItem.Gift_RedeemCode;
                        ledger.Description    = "Gift Certificate Redemption: " + ledger.GiftRedeemCode;
                    }
                    else
                    {
                        ledger = null;
                    }
                }
                else
                {
                    ledger.TotalAmount = decimal.Parse(txtXactAmount.Text.Trim());
                }

                hccUserProfile profile = hccUserProfile.GetParentProfileBy(CurrentAspNetUserId);

                if (profile != null)
                {
                    // check against last entry for duplicate
                    hccLedger lastEntry        = hccLedger.GetByMembershipID(profile.MembershipID, null).OrderByDescending(a => a.CreatedDate).FirstOrDefault();
                    bool      isDuplicateEntry = false;

                    if (ledger != null)
                    {
                        if (lastEntry != null &&
                            ledger.CreatedBy == lastEntry.CreatedBy &&
                            ledger.CreditFromBalance == lastEntry.CreditFromBalance &&
                            ledger.Description == lastEntry.Description &&
                            ledger.PaymentDue == lastEntry.PaymentDue &&
                            ledger.TransactionTypeID == lastEntry.TransactionTypeID &&
                            ledger.TotalAmount == lastEntry.TotalAmount)
                        {
                            isDuplicateEntry = true;
                        }

                        if (!isDuplicateEntry)
                        {
                            switch (ledger.TransactionType)
                            {
                            case Enums.LedgerTransactionType.HCCAccountCredit:
                            case Enums.LedgerTransactionType.RedeemGiftCertificate:
                                profile.AccountBalance = profile.AccountBalance + ledger.TotalAmount;
                                break;

                            case Enums.LedgerTransactionType.HCCAccountDebit:
                                profile.AccountBalance = profile.AccountBalance - ledger.TotalAmount;
                                break;

                            case Enums.LedgerTransactionType.Purchase:
                            case Enums.LedgerTransactionType.Return:
                                ledger.PaymentDue = ledger.TotalAmount;
                                break;

                            default: break;
                            }

                            ledger.PostBalance = profile.AccountBalance;
                            ledger.Save();
                            profile.Save();

                            this.Bind();
                            OnSaved(new ControlSavedEventArgs(CurrentAspNetUserId));

                            txtXactAmount.Text   = string.Empty;
                            txtXactDesc.Text     = string.Empty;
                            txtXactGCRedeem.Text = string.Empty;
                            ddlXactTypes.ClearSelection();
                        }
                        else
                        {
                            lblXactFeedback.Text = "This appears to be a duplicate transaction. If it is not a duplicate, please provide a comment to differentiate this transaction from the last.";
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }