public ActionResult Create(CustomersSalesContact customerssalescontact)
        {
            if (ModelState.IsValid)
            {
                db.CustomersSalesContacts.Add(customerssalescontact);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(customerssalescontact);
        }
        //
        // 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 CreateEditSales(string customerid, int id = 0)
        {
            int nCustomerId = Convert.ToInt32(customerid);
            string szCustomerInfo = string.Empty;
            //IQueryable<CustomersSalesContact> qryAddress = null;

            CustomersSalesContact customersalescontact = null;
            Customers customer = null;
            CustomerDefaults customerDefault = null;
            //CustomersSubsidiaryAddress customeraddress = null;

            if (id == 0)
            {
                customersalescontact = new CustomersSalesContact();
                customersalescontact.CustomerId = nCustomerId;
            }
            else
            {
                customersalescontact = db.CustomersSalesContacts.Find(id);
            }

            //Get Customer info
            if (customersalescontact != null)
            {
                customer = db.Customers.Find(customersalescontact.CustomerId);
                if (customer != null)
                {
                    szCustomerInfo = string.Format("{0}", customer.CustomerNo);

                    //qryAddress = db.CustomersSubsidiaryAddresses.Where(cust => cust.CustomerId == customer.Id);
                    //if (qryAddress.Count() > 0)
                    //{
                    //    customeraddress = qryAddress.FirstOrDefault<CustomersSubsidiaryAddress>();
                    //    if (customeraddress != null)
                    //    {
                    //        szCustomerInfo = string.Format("{0} {1}", szCustomerInfo, customeraddress.CompanyName);
                    //    }
                    //}
                }
            }
            ViewBag.CustomerInfo = szCustomerInfo;

            //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;
            }

            return PartialView(customersalescontact);
        }
        public ActionResult UpdateCustomerSales(CustomersSalesContact customenote, string customerdefault)
        {
            int nCustomerDefault = Convert.ToInt32(customerdefault);
            CustomerDefaults custdefault = null;

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

                        db.CustomersSalesContacts.Add(customenote);
                        db.SaveChanges();

                        //Set the default customer value
                        custdefault = db.CustomerDefaults.Find(nCustomerDefault);
                        if (custdefault != null)
                        {
                            custdefault.SalesContactId = customenote.Id;
                            custdefault.SalesName = string.Format("{0} {1}", customenote.FirstName, customenote.LastName);
                            db.Entry(custdefault).State = EntityState.Modified;
                        }
                        db.SaveChanges();
                    }
                    else
                    {
                        //Set the default customer value
                        custdefault = db.CustomerDefaults.Find(nCustomerDefault);
                        if (custdefault != null)
                        {
                            custdefault.SalesContactId = customenote.Id;
                            custdefault.SalesName = string.Format("{0} {1}", customenote.FirstName, customenote.LastName);
                            db.Entry(custdefault).State = EntityState.Modified;
                        }
                        if (string.IsNullOrEmpty(customenote.Tel))
                        {
                            customenote.Tel = "0";
                        }
                        if (string.IsNullOrEmpty(customenote.Fax))
                        {
                            customenote.Fax = "0";
                        }
                        customenote.Tel = customenote.Tel.Replace("-", string.Empty);
                        customenote.Fax = customenote.Fax.Replace("-", string.Empty);

                        db.Entry(customenote).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    //return RedirectToAction("Index");
                    return RedirectToAction("Edit", new { id = Convert.ToInt32(customenote.CustomerId) });
                }

            }

            return RedirectToAction("Index");
        }
 public ActionResult Edit(CustomersSalesContact customerssalescontact)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customerssalescontact).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(customerssalescontact);
 }