public ActionResult Create(CustomersCreditCardShipping customerscreditcardshipping)
        {
            string szError = "";
            string szEncriptedData = "";

            if (ModelState.IsValid)
            {
                //Encode the credit card info
                if (!string.IsNullOrEmpty(customerscreditcardshipping.CreditNumber))
                {
                    szEncriptedData = TimelyDepotMVC.Controllers.PaymentController.EncriptInfo02(customerscreditcardshipping.CreditNumber, ref szError);
                    customerscreditcardshipping.CreditNumber = szEncriptedData;
                }

                //Encode the secure code
                if (!string.IsNullOrEmpty(customerscreditcardshipping.SecureCode))
                {
                    szEncriptedData = TimelyDepotMVC.Controllers.PaymentController.EncriptInfo02(customerscreditcardshipping.SecureCode, ref szError);
                    customerscreditcardshipping.SecureCode = szEncriptedData;
                }

                db.CustomersCreditCardShippings.Add(customerscreditcardshipping);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(customerscreditcardshipping);
        }
        //
        // GET: /Customers/Edit
        public ActionResult Edit(string modomultiple, int? page, int id = 0)
        {
            int nHas = 0;
            int nPos = -1;
            string szError = string.Empty;
            string szDecriptedData = string.Empty;
            string szDecriptedCode = string.Empty;

            //int? page = null;
            int pageNote = 0;
            int pageSubsidiary = 0;
            int pageSales = 0;
            int pageShipp = 0;
            int pageIndex = 0;
            int pageSize = 10;
            string szCustomerInfo = string.Empty;
            string szCustomerId = string.Empty;
            string szMsg = string.Empty;

            List<KeyValuePair<string, string>> listSelector = new List<KeyValuePair<string, string>>();

            IQueryable<CustomersContactAddress> qryCusAdd = null;
            IQueryable<CustomersSubsidiaryAddress> qryCusSub = null;
            IQueryable<CustomersBillingDept> qryCusBil = null;
            IQueryable<CustomersSalesContact> qryCusSal = null;
            IQueryable<CustomersHistory> qryCusHty = null;
            IQueryable<CustomersCreditCardShipping> qryCusCre = null;
            IQueryable<CustomersSpecialNotes> qryCusNote = null;
            IQueryable<CustomersShipAddress> qryCusShip = null;
            IQueryable<Deptos> qryDepto = null;
            IQueryable<Bussines> qryBussines = null;
            IQueryable<Origin> qryOrigin = null;
            IQueryable<Terms> qryTerms = null;
            IQueryable<CustomerDefaults> qryCusDefault = null;

            CustomersContactAddress custMainContact = null;
            CustomersBillingDept custBilling = null;
            CustomersSalesContact custSalesContact = null;
            CustomersHistory custHistory = null;
            CustomersCreditCardShipping CustCredit = null;
            CustomersSubsidiaryAddress custSubsidiary = null;
            CustomersShipAddress CusShip = null;
            CustomersSpecialNotes CustNotes = null;
            CustomerDefaults customerdefaults = null;

            List<CustomersSpecialNotes> notesList = new List<CustomersSpecialNotes>();
            List<CustomersSubsidiaryAddress> subsidiaryList = new List<CustomersSubsidiaryAddress>();
            List<CustomersSalesContact> salesList = new List<CustomersSalesContact>();
            List<CustomersShipAddress> shipList = new List<CustomersShipAddress>();

            //Get the customer's data
            Customers customers = db.Customers.Find(id);
            if (customers != null)
            {
                //Use the customer defaults
                customerdefaults = db.CustomerDefaults.Where(cudf => cudf.CustomerId == id).FirstOrDefault<CustomerDefaults>();
                if (customerdefaults == null)
                {
                    customerdefaults = new CustomerDefaults();
                    customerdefaults.CustomerId = id;
                    db.CustomerDefaults.Add(customerdefaults);
                    db.SaveChanges();
                }

                szCustomerInfo = customers.CustomerNo;

                qryCusAdd = db.CustomersContactAddresses.Where(cutadd => cutadd.CustomerId == customers.Id);
                if (qryCusAdd.Count() > 0)
                {
                    custMainContact = qryCusAdd.FirstOrDefault<CustomersContactAddress>();
                    szCustomerInfo = string.Format("{0} {1}", szCustomerInfo, custMainContact.CompanyName);
                    szCustomerId = customers.Id.ToString();
                }
                else
                {
                    custMainContact = new CustomersContactAddress();
                    custMainContact.CustomerId = customers.Id;
                    db.CustomersContactAddresses.Add(custMainContact);
                    szCustomerId = customers.Id.ToString();
                }

                qryCusBil = db.CustomersBillingDepts.Where(cutadd => cutadd.CustomerId == customers.Id);
                if (qryCusBil.Count() > 0)
                {
                    custBilling = qryCusBil.FirstOrDefault<CustomersBillingDept>();
                }
                else
                {
                    custBilling = new CustomersBillingDept();
                    custBilling.CustomerId = customers.Id;
                    db.CustomersBillingDepts.Add(custBilling);
                }
                qryCusHty = db.CustomersHistories.Where(cutadd => cutadd.CustomerId == customers.Id);
                if (qryCusHty.Count() > 0)
                {
                    custHistory = qryCusHty.FirstOrDefault<CustomersHistory>();
                }
                else
                {
                    custHistory = new CustomersHistory();
                    custHistory.CustomerId = customers.Id;
                    db.CustomersHistories.Add(custHistory);
                }

                qryCusCre = db.CustomersCreditCardShippings.Where(cutadd => cutadd.CustomerId == customers.Id);
                if (qryCusCre.Count() > 0)
                {
                    CustCredit = qryCusCre.FirstOrDefault<CustomersCreditCardShipping>();
                    if (customerdefaults.NoteId == null)
                    {
                        customerdefaults.NoteId = CustCredit.Id;
                        customerdefaults.NoteName = string.Format("{0}", CustCredit.CreditNumber);
                    }
                }
                else
                {
                    CustCredit = new CustomersCreditCardShipping();
                    CustCredit.CustomerId = customers.Id;
                    db.CustomersCreditCardShippings.Add(CustCredit);
                    if (customerdefaults.NoteId == null)
                    {
                        customerdefaults.NoteId = CustCredit.Id;
                        customerdefaults.NoteName = string.Format("{0}", CustCredit.Name);
                    }
                }

                qryCusShip = db.CustomersShipAddresses.Where(cutadd => cutadd.CustomerId == customers.Id).OrderBy(cutadd => cutadd.FirstName).ThenBy(cutadd => cutadd.LastName);
                if (qryCusShip.Count() > 0)
                {
                    //CusShip = qryCusShip.FirstOrDefault<CustomersShipAddress>();
                    foreach (var item in qryCusShip)
                    {
                        shipList.Add(item);
                        if (customerdefaults.ShiptoAddressId == null)
                        {
                            customerdefaults.ShiptoAddressId = item.Id;
                            customerdefaults.ShiptoName = item.Address1;
                        }
                    }
                }
                else
                {
                    CusShip = new CustomersShipAddress();
                    CusShip.CustomerId = customers.Id;
                    db.CustomersShipAddresses.Add(CusShip);
                    db.SaveChanges();
                    shipList.Add(CusShip);
                    if (customerdefaults.ShiptoAddressId == null)
                    {
                        customerdefaults.ShiptoAddressId = CusShip.Id;
                        customerdefaults.ShiptoName = CusShip.Address1;
                    }
                }

                qryCusSal = db.CustomersSalesContacts.Where(cutadd => cutadd.CustomerId == customers.Id).OrderBy(cutadd => cutadd.FirstName).ThenBy(cutadd => cutadd.LastName);
                if (qryCusSal.Count() > 0)
                {
                    //custSalesContact = qryCusSal.FirstOrDefault<CustomersSalesContact>();
                    foreach (var item in qryCusSal)
                    {
                        salesList.Add(item);
                        if (customerdefaults.SalesContactId == null)
                        {
                            customerdefaults.SalesContactId = item.Id;
                            customerdefaults.SalesName = string.Format("{0} {1}", item.FirstName, item.LastName);
                            custSalesContact = db.CustomersSalesContacts.Find(item.Id);
                        }
                    }
                }
                else
                {
                    custSalesContact = new CustomersSalesContact();
                    custSalesContact.CustomerId = customers.Id;
                    db.CustomersSalesContacts.Add(custSalesContact);
                    db.SaveChanges();
                    salesList.Add(custSalesContact);
                    if (customerdefaults.SalesContactId == null)
                    {
                        customerdefaults.SalesContactId = custSalesContact.Id;
                        customerdefaults.SalesName = string.Format("{0} {1}", custSalesContact.FirstName, custSalesContact.LastName);
                    }
                }

                qryCusSub = db.CustomersSubsidiaryAddresses.Where(cutadd => cutadd.CustomerId == customers.Id).OrderBy(cutadd => cutadd.CompanyName);
                if (qryCusSub.Count() > 0)
                {
                    //custSubsidiary = qryCusSub.FirstOrDefault<CustomersSubsidiaryAddress>();
                    foreach (var item in qryCusSub)
                    {
                        subsidiaryList.Add(item);
                        if (customerdefaults.SubsidiaryId == null)
                        {
                            customerdefaults.SubsidiaryId = item.Id;
                            customerdefaults.SubsidiaryName = string.Format("{0}", item.CompanyName);
                        }
                        if (custSubsidiary == null)
                        {
                            custSubsidiary = item;
                            customerdefaults.SubsidiaryId = item.Id;
                            customerdefaults.SubsidiaryName = string.Format("{0}", item.CompanyName);
                        }
                    }
                }
                else
                {
                    custSubsidiary = new CustomersSubsidiaryAddress();
                    custSubsidiary.CustomerId = customers.Id;
                    db.CustomersSubsidiaryAddresses.Add(custSubsidiary);
                    db.SaveChanges();
                    subsidiaryList.Add(custSubsidiary);
                    if (customerdefaults.SubsidiaryId == null)
                    {
                        customerdefaults.SubsidiaryId = custSubsidiary.Id;
                        customerdefaults.SubsidiaryName = string.Format("{0}", custSubsidiary.CompanyName);
                    }
                }

                qryCusNote = db.CustomersSpecialNotes.Where(cutadd => cutadd.CustomerId == customers.Id).OrderByDescending(cutadd => cutadd.Id);
                if (qryCusNote.Count() > 0)
                {
                    //CustNotes = qryCusNote.FirstOrDefault<CustomersSpecialNotes>();
                    foreach (var item in qryCusNote)
                    {
                        notesList.Add(item);
                        //if (customerdefaults.NoteId == null)
                        //{
                        //    customerdefaults.NoteId = item.Id;
                        //    customerdefaults.NoteName = string.Format("{0}", item.SpecialNote);
                        //}
                        if (CustNotes == null)
                        {
                            CustNotes = item;
                        }
                    }
                }
                else
                {
                    CustNotes = new CustomersSpecialNotes();
                    CustNotes.CustomerId = customers.Id;
                    db.CustomersSpecialNotes.Add(CustNotes);
                    notesList.Add(CustNotes);
                    if (customerdefaults.NoteId == null)
                    {
                        customerdefaults.NoteId = CustNotes.Id;
                        customerdefaults.NoteName = string.Format("{0}", CustNotes.SpecialNote);
                    }
                }

            }

            db.SaveChanges();

            ViewBag.CustomerInfo = szCustomerInfo;
            ViewBag.CustomerId = szCustomerId;

            //Get the dropdown data
            qryDepto = db.Deptos.OrderBy(dpt => dpt.Name);
            if (qryDepto.Count() > 0)
            {
                foreach (var item in qryDepto)
                {
                    listSelector.Add(new KeyValuePair<string, string>(item.DeptoNo, item.Name));
                }
            }
            SelectList deptoslist = new SelectList(listSelector, "Key", "Value");
            ViewBag.DeptosList = deptoslist;

            listSelector = new List<KeyValuePair<string, string>>();
            qryBussines = db.Bussines.OrderBy(bss => bss.BussinesType);
            if (qryBussines.Count() > 0)
            {
                foreach (var item in qryBussines)
                {
                    listSelector.Add(new KeyValuePair<string, string>(item.BussinesType, item.BussinesType));
                }
            }
            SelectList bussineslist = new SelectList(listSelector, "Key", "Value");
            ViewBag.BussinesList = bussineslist;

            listSelector = new List<KeyValuePair<string, string>>();
            qryOrigin = db.Origins.OrderBy(org => org.Name);
            if (qryOrigin.Count() > 0)
            {
                foreach (var item in qryOrigin)
                {
                    listSelector.Add(new KeyValuePair<string, string>(item.Name, item.Name));
                }
            }
            SelectList Originlist = new SelectList(listSelector, "Key", "Value");
            ViewBag.OriginList = Originlist;

            listSelector = new List<KeyValuePair<string, string>>();
            qryTerms = db.Terms.OrderBy(trm => trm.Term);
            if (qryTerms.Count() > 0)
            {
                foreach (var item in qryTerms)
                {
                    listSelector.Add(new KeyValuePair<string, string>(item.Term, item.Term));
                }
            }
            SelectList Termslist = new SelectList(listSelector, "Key", "Value");
            ViewBag.TermsList = Termslist;

            listSelector = new List<KeyValuePair<string, string>>();
            qryCusSal = db.CustomersSalesContacts.Where(csp => csp.CustomerId == customers.Id).OrderBy(csp => csp.FirstName).ThenBy(csp => csp.LastName);
            if (qryCusSal.Count() > 0)
            {
                foreach (var item in qryCusSal)
                {
                    szMsg = string.Format("{0} {1}", item.FirstName, item.LastName);
                    listSelector.Add(new KeyValuePair<string, string>(szMsg, szMsg));
                }
            }
            SelectList SalesPersonslist = new SelectList(listSelector, "Key", "Value");
            ViewBag.SalesPersonsList = SalesPersonslist;

            listSelector = new List<KeyValuePair<string, string>>();
            qryCusSal = db.CustomersSalesContacts.Where(csp => csp.CustomerId == customers.Id).OrderBy(csp => csp.FirstName).ThenBy(csp => csp.LastName);
            if (qryCusSal.Count() > 0)
            {
                foreach (var item in qryCusSal)
                {
                    szMsg = string.Format("{0} {1}", item.FirstName, item.LastName);
                    listSelector.Add(new KeyValuePair<string, string>(item.Id.ToString(), szMsg));
                }
            }
            SelectList cusdefaultlist = new SelectList(listSelector, "Key", "Value");
            ViewBag.SalesContactList = cusdefaultlist;

            listSelector = new List<KeyValuePair<string, string>>();
            qryCusSub = db.CustomersSubsidiaryAddresses.Where(cusb => cusb.CustomerId == customers.Id).OrderBy(cusb => cusb.CompanyName);
            if (qryCusSub.Count() > 0)
            {
                foreach (var item in qryCusSub)
                {
                    listSelector.Add(new KeyValuePair<string, string>(item.Id.ToString(), item.CompanyName));
                }
            }
            SelectList cussublist = new SelectList(listSelector, "Key", "Value");
            ViewBag.SubsidiaryList = cussublist;

            listSelector = new List<KeyValuePair<string, string>>();
            qryCusShip = db.CustomersShipAddresses.Where(cusb => cusb.CustomerId == customers.Id).OrderBy(cusb => cusb.FirstName).ThenBy(cusb => cusb.LastName);
            if (qryCusShip.Count() > 0)
            {
                foreach (var item in qryCusShip)
                {
                    szMsg = string.Format("{0} {1}", item.FirstName, item.LastName);
                    listSelector.Add(new KeyValuePair<string, string>(item.Id.ToString(), szMsg));
                }
            }
            SelectList cusshiplist = new SelectList(listSelector, "Key", "Value");
            ViewBag.ShipList = cusshiplist;

            listSelector = new List<KeyValuePair<string, string>>();
            qryCusCre = db.CustomersCreditCardShippings.Where(cucr => cucr.CustomerId == customers.Id).OrderBy(cucr => cucr.CreditNumber);
            if (qryCusCre.Count() > 0)
            {
                foreach (var item in qryCusCre)
                {
                    szError = string.Empty;
                    szDecriptedData = TimelyDepotMVC.Controllers.PaymentController.DecodeInfo02(item.CreditNumber, ref szError);
                    if (!string.IsNullOrEmpty(szError))
                    {
                        nPos = szError.IndexOf("data to decode");
                        if (nPos != -1)
                        {
                            szDecriptedData = string.Empty;
                        }
                        else
                        {
                            szDecriptedData = string.Format("******");
                        }
                    }
                    else
                    {
                        //Mask the card number
                        nHas = szDecriptedData.Length;
                        if (nHas > 4)
                        {
                            szMsg = szDecriptedData.Substring(nHas - 4, 4);
                            szDecriptedData = string.Format("******{0}", szMsg);
                        }
                        else
                        {
                            szDecriptedData = string.Format("******");
                        }
                    }

                    //listSelector.Add(new KeyValuePair<string, string>(item.Id.ToString(), item.CreditNumber));
                    listSelector.Add(new KeyValuePair<string, string>(item.Id.ToString(), szDecriptedData));
                }
            }
            SelectList cuscreditlist = new SelectList(listSelector, "Key", "Value");
            ViewBag.CreditCardList = cuscreditlist;

            //Get the missing (null) objects
            if (custSalesContact == null)
            {
                custSalesContact = db.CustomersSalesContacts.Find(customerdefaults.SalesContactId);
                if (custSalesContact == null)
                {
                    custSalesContact = new CustomersSalesContact();
                }
            }
            if (custSubsidiary == null)
            {
                custSubsidiary = db.CustomersSubsidiaryAddresses.Find(customerdefaults.SubsidiaryId);
                if (custSubsidiary == null)
                {
                    custSubsidiary = new CustomersSubsidiaryAddress();
                }
            }
            if (CusShip == null)
            {
                CusShip = db.CustomersShipAddresses.Find(customerdefaults.ShiptoAddressId);
                if (CusShip == null)
                {
                    CusShip = new CustomersShipAddress();
                }
            }

            //Get theActive/Inactive list
            listSelector = new List<KeyValuePair<string, string>>();
            listSelector.Add(new KeyValuePair<string, string>("Y", "Active"));
            listSelector.Add(new KeyValuePair<string, string>("N", "Inactive"));
            SelectList activeinactivelist = new SelectList(listSelector, "Key", "Value");
            ViewBag.ActiveInactivelist = activeinactivelist;

            CustomerView cusview = new CustomerView();
            cusview.customer = customers;
            cusview.customeraddress = custMainContact;
            cusview.customerbilling = custBilling;
            cusview.customerhistory = custHistory;
            cusview.customercredit = CustCredit;
            cusview.customerdefaults = customerdefaults;
            cusview.customersalescontact = custSalesContact;
            cusview.custmersubsidiary = custSubsidiary;
            cusview.customershipaddress = CusShip;
            cusview.customernote = CustNotes;

            if (TempData["AddCustomer"] != null)
            {
                ViewBag.AddCustomerHlp = TempData["AddCustomer"].ToString();
            }

            //Set the page
            if (page == null)
            {
                pageIndex = 1;
            }
            else
            {
                pageIndex = Convert.ToInt32(page);
            }

            if (string.IsNullOrEmpty(modomultiple))
            {
                pageNote = pageIndex;
                pageSubsidiary = pageIndex;
                pageSales = pageIndex;
                pageShipp = pageIndex;
            }
            else
            {
                switch (modomultiple)
                {
                    case "SpecialNote":
                        pageNote = pageIndex;
                        break;
                    case "Subsidiary":
                        pageSubsidiary = pageIndex;
                        break;
                    case "Sales":
                        pageSales = pageIndex;
                        PageSize = 1;
                        break;
                    case "Shipp":
                        pageShipp = pageIndex;
                        break;
                    default:
                        break;
                }
            }

            //Verify page numbre
            if (pageNote == 0)
            {
                pageNote = 1;
            }

            var noteListHlp = notesList.ToPagedList(1, 1);
            ViewBag.OnePageOfNotesData = noteListHlp;
            cusview.customernotesList = noteListHlp;

            var subsidiaryListHlp = subsidiaryList.ToPagedList(1, 1);
            ViewBag.OnePageOfsubsidiarysData = subsidiaryListHlp;
            cusview.customersubsidiaryList = subsidiaryListHlp;

            //var salesListHlp = salesList.ToPagedList(pageSales, pageSize);
            var salesListHlp = salesList.ToPagedList(1, 1);
            ViewBag.OnePageOfsalesData = salesListHlp;
            cusview.customersalesList = salesListHlp;

            var shippListHlp = shipList.ToPagedList(1, 1);
            ViewBag.OnePageOfshipsData = shippListHlp;
            cusview.customershipList = shippListHlp;

            //Set the error, if any
            if (TempData["Error"] != null)
            {
                ViewBag.Error = TempData["Error"].ToString();
            }

            //Set the active tab
            if (TempData["ActiveTab"] != null)
            {
                ViewBag.ActiveTab = TempData["ActiveTab"].ToString();
            }

            return View(cusview);
        }
        public PartialViewResult CreateEditCreditCardFromPayment(string customerid, string salesOrderNo,int id = 0)
        {
            int nCustomerId = Convert.ToInt32(customerid);
            CustomerDefaults customerDefault = null;
            CustomersCreditCardShipping customercredit = null;
            IQueryable<CustomersCardType> qryCardType = null;

            List<KeyValuePair<string, string>> listSelector = new List<KeyValuePair<string, string>>();

            if (id == 0)
            {
                customercredit = new CustomersCreditCardShipping();
                customercredit.CustomerId = nCustomerId;
                customercredit.ExpirationDate = DateTime.Now;
            }
            else
            {
                customercredit = db.CustomersCreditCardShippings.Find(id);
            }

            //Get the customer default id
            ViewBag.CustomerDefaultId = 0;
            customerDefault = db.CustomerDefaults.Where(ctdf => ctdf.CustomerId == nCustomerId).FirstOrDefault<CustomerDefaults>();
            if (customerDefault != null)
            {
                ViewBag.CustomerDefaultId = customerDefault.Id;
            }

            listSelector = new List<KeyValuePair<string, string>>();
            qryCardType = db.CustomersCardTypes.OrderBy(cucr => cucr.CardType);
            if (qryCardType.Count() > 0)
            {
                foreach (var item in qryCardType)
                {
                    listSelector.Add(new KeyValuePair<string, string>(item.CardType, item.CardType));
                }
            }
            var cardtypelist = new SelectList(listSelector, "Key", "Value");
            ViewBag.CardTypeList = cardtypelist;
            ViewBag.SalesOrderNo = salesOrderNo;

            return PartialView(customercredit);
        }
        public ActionResult CreateEditCreditCardFromPayment(CustomersCreditCardShipping customenote, string customerdefault, string salesOrderNo,  string ExpirationDateHlp, string CreditNumber01, string SecureCode01, string PaymentUrl = "")
        {
            int nCustomerDefault = Convert.ToInt32(customerdefault);
            int nPos = -1;
            string szError = string.Empty;
            string szEncriptedData = string.Empty;
            DateTime dDate = DateTime.Now;
            CustomerDefaults custdefault = null;

            if (customenote != null)
            {
                if (ModelState.IsValid)
                {
                    if (customenote.Id == 0)
                    {
                        if (string.IsNullOrEmpty(customenote.Tel))
                        {
                            customenote.Tel = "0";
                        }
                        customenote.Tel = customenote.Tel.Replace("-", string.Empty);
                        db.CustomersCreditCardShippings.Add(customenote);

                        //Set Card number
                        //Encript the credit card info
                        //use the user supplied data
                        if (!string.IsNullOrEmpty(CreditNumber01))
                        {
                            nPos = CreditNumber01.IndexOf("*");
                            if (nPos == -1)
                            {
                                customenote.CreditNumber = CreditNumber01;

                                //Encode the credit card info
                                if (!string.IsNullOrEmpty(customenote.CreditNumber))
                                {
                                    szEncriptedData = PaymentController.EncriptInfo02(customenote.CreditNumber, ref szError);
                                    customenote.CreditNumber = szEncriptedData;
                                }
                            }
                            else
                            {
                                //Do not replace the credit card number
                            }
                        }

                        //Set Secure Code
                        if (!string.IsNullOrEmpty(customenote.SecureCode))
                        {
                            //Encode the credit card info
                            if (!string.IsNullOrEmpty(customenote.SecureCode))
                            {
                                szEncriptedData = PaymentController.EncriptInfo02(customenote.SecureCode, ref szError);
                                customenote.SecureCode = szEncriptedData;
                            }

                        }
                        db.SaveChanges();

                        //Set the default customer value
                        custdefault = db.CustomerDefaults.Find(nCustomerDefault);
                        if (custdefault != null)
                        {
                            custdefault.NoteId = customenote.Id;
                            custdefault.NoteName = string.Format("{0}", customenote.CreditNumber);
                            db.Entry(custdefault).State = EntityState.Modified;
                        }
                        db.SaveChanges();

                    }
                    else
                    {
                        //Set the default customer value
                        custdefault = db.CustomerDefaults.Find(nCustomerDefault);
                        if (custdefault != null)
                        {
                            custdefault.NoteId = customenote.Id;
                            custdefault.NoteName = string.Format("{0}", customenote.CreditNumber);
                            db.Entry(custdefault).State = EntityState.Modified;
                        }

                        if (!string.IsNullOrEmpty(ExpirationDateHlp))
                        {
                            dDate = Convert.ToDateTime(ExpirationDateHlp);
                            customenote.ExpirationDate = dDate;
                        }

                        if (string.IsNullOrEmpty(customenote.Tel))
                        {
                            customenote.Tel = "0";
                        }
                        customenote.Tel = customenote.Tel.Replace("-", string.Empty);

                        //Encript the credit card info
                        //use the user supplied data
                        if (!string.IsNullOrEmpty(CreditNumber01))
                        {
                            nPos = CreditNumber01.IndexOf("*");
                            if (nPos == -1)
                            {
                                customenote.CreditNumber = CreditNumber01;

                                //Encode the credit card info
                                if (!string.IsNullOrEmpty(customenote.CreditNumber))
                                {
                                    szEncriptedData = TimelyDepotMVC.Controllers.PaymentController.EncriptInfo02(customenote.CreditNumber, ref szError);
                                    customenote.CreditNumber = szEncriptedData;
                                }
                            }

                        }

                        //Encode the credit card info
                        if (!string.IsNullOrEmpty(customenote.SecureCode))
                        {
                            szEncriptedData = PaymentController.EncriptInfo02(customenote.SecureCode, ref szError);
                            customenote.SecureCode = szEncriptedData;
                        }

                        db.Entry(customenote).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                }
            }

            return !string.IsNullOrEmpty(salesOrderNo) ?
                this.RedirectToAction("AddCreditCardPayment", "Payment", new { salesOrderNumber = salesOrderNo }) :
                this.RedirectToAction("Edit", new { id = customenote.CustomerId});
        }
        public void UpdateCustomerCreditCard(CustomerView cusview)
        {
            CustomersCreditCardShipping customeraddress = null;

            if (cusview.customercredit != null)
            {
                if (cusview.customercredit.Id == 0)
                {
                    customeraddress = new CustomersCreditCardShipping();
                }
                else
                {
                    customeraddress = db.CustomersCreditCardShippings.Find(cusview.customercredit.Id);
                }

                if (customeraddress != null)
                {
                    customeraddress.Name = cusview.customercredit.Name;
                    customeraddress.CreditNumber = cusview.customercredit.CreditNumber;
                    customeraddress.ExpirationDate = cusview.customercredit.ExpirationDate;
                    customeraddress.SecureCode = cusview.customercredit.SecureCode;
                    customeraddress.Address1 = cusview.customercredit.Address1;
                    customeraddress.Address2 = cusview.customercredit.Address2;
                    customeraddress.Country = cusview.customercredit.Country;
                    customeraddress.State = cusview.customercredit.State;
                    customeraddress.Tel = cusview.customercredit.Tel.Replace("-", string.Empty);
                    customeraddress.Zip = cusview.customercredit.Zip;
                    customeraddress.CustomerId = cusview.customercredit.CustomerId;

                    db.Entry(customeraddress).State = EntityState.Modified;
                    db.SaveChanges();

                }
            }
        }
        public ActionResult Edit(CustomersCreditCardShipping customerscreditcardshipping, string CreditNumber01, string SecureCode01)
        {
            int nPos = -1;
            string szError = "";
            string szEncriptedData = "";

            if (ModelState.IsValid)
            {
                //use the user supplied data
                if (!string.IsNullOrEmpty(CreditNumber01))
                {
                    nPos = CreditNumber01.IndexOf("*");
                    if (nPos == -1)
                    {
                        customerscreditcardshipping.CreditNumber = CreditNumber01;

                        //Encode the credit card info
                        if (!string.IsNullOrEmpty(customerscreditcardshipping.CreditNumber))
                        {
                            szEncriptedData = TimelyDepotMVC.Controllers.PaymentController.EncriptInfo02(customerscreditcardshipping.CreditNumber, ref szError);
                            customerscreditcardshipping.CreditNumber = szEncriptedData;
                        }
                    }
                    else
                    {
                        //Do not replace the credit card number
                    }
                }

                if (!string.IsNullOrEmpty(SecureCode01))
                {
                    nPos = -1;
                    nPos = SecureCode01.IndexOf("*");
                    if (nPos == -1)
                    {
                        customerscreditcardshipping.SecureCode = SecureCode01;

                        //Encode the credit card info
                        if (!string.IsNullOrEmpty(customerscreditcardshipping.SecureCode))
                        {
                            szEncriptedData = TimelyDepotMVC.Controllers.PaymentController.EncriptInfo02(customerscreditcardshipping.SecureCode, ref szError);
                            customerscreditcardshipping.SecureCode = szEncriptedData;
                        }
                    }
                    else
                    {
                        //Do not replace the credit card number
                    }
                }

                db.Entry(customerscreditcardshipping).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(customerscreditcardshipping);
        }