public ActionResult Create(VendorsHistory vendorshistory)
        {
            if (ModelState.IsValid)
            {
                db.VendorsHistories.Add(vendorshistory);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(vendorshistory);
        }
        public ActionResult Edit(string modomultiple, int? page, int id = 0)
        {
            int pageNote = 0;
            int pageSubsidiary = 0;
            int pageSales = 0;
            int pageShipp = 0;
            int pageIndex = 0;
            int pageSize = 10;
            string szMsg = "";
            string szVendorInfo = "";
            string szVendorId = "";

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

            IQueryable<VendorsContactAddress> qryVenAdd = null;
            IQueryable<VendorsBillingDept> qryVenBill = null;
            IQueryable<VendorsSalesContact> qryVenSale = null;
            IQueryable<VendorsHistory> qryVenHis = null;
            IQueryable<VendorsSpecialNotes> qryCusNote = null;
            IQueryable<VendorTypes> qryBussines = null;
            IQueryable<Origin> qryOrigin = null;
            IQueryable<Terms> qryTerms = null;
            IQueryable<Bussines> qryBussines01 = null;

            VendorsContactAddress venAddress = null;
            VendorsBillingDept venBillin = null;
            VendorsSalesContact venSale = null;
            VendorsHistory venHistory = null;
            VendorsSpecialNotes CustNotes = null;
            VendorDefaults vendordefaults = null;

            List<VendorsSpecialNotes> notesList = new List<VendorsSpecialNotes>();
            List<VendorsSalesContact> salesList = new List<VendorsSalesContact>();

            //Get the vendor's data
            Vendors vendor = db.Vendors.Find(id);
            if (vendor != null)
            {
                //Use the vendors defaults
                vendordefaults = db.VendorDefaults.Where(cudf => cudf.VendorId == id).FirstOrDefault<VendorDefaults>();
                if (vendordefaults == null)
                {
                    vendordefaults = new VendorDefaults();
                    vendordefaults.VendorId = id;
                    db.VendorDefaults.Add(vendordefaults);
                    db.SaveChanges();
                }

                szVendorInfo = vendor.VendorNo;

                qryVenAdd = db.VendorsContactAddresses.Where(cutadd => cutadd.VendorId == vendor.Id);
                if (qryVenAdd.Count() > 0)
                {
                    venAddress = qryVenAdd.FirstOrDefault<VendorsContactAddress>();
                    szVendorInfo = string.Format("{0} {1}", szVendorInfo, venAddress.CompanyName);
                    szVendorId = vendor.Id.ToString();
                }
                else
                {
                    venAddress = new VendorsContactAddress();
                    venAddress.VendorId = vendor.Id;
                    db.VendorsContactAddresses.Add(venAddress);
                    szVendorId = vendor.Id.ToString();
                }

                qryVenBill = db.VendorsBillingDepts.Where(cutadd => cutadd.VendorId == vendor.Id);
                if (qryVenBill.Count() > 0)
                {
                    venBillin = qryVenBill.FirstOrDefault<VendorsBillingDept>();
                }
                else
                {
                    venBillin = new VendorsBillingDept();
                    venBillin.VendorId = vendor.Id;
                    db.VendorsBillingDepts.Add(venBillin);
                }

                qryVenHis = db.VendorsHistories.Where(cutadd => cutadd.VendorId == vendor.Id);
                if (qryVenHis.Count() > 0)
                {
                    venHistory = qryVenHis.FirstOrDefault<VendorsHistory>();
                }
                else
                {
                    venHistory = new VendorsHistory();
                    venHistory.VendorId = vendor.Id;
                    db.VendorsHistories.Add(venHistory);
                }

                qryVenSale = db.VendorsSalesContacts.Where(cutadd => cutadd.VendorId == vendor.Id).OrderBy(cutadd => cutadd.FirstName).ThenBy(cutadd => cutadd.LastName);
                if (qryVenSale.Count() > 0)
                {
                    //custSalesContact = qryCusSal.FirstOrDefault<CustomersSalesContact>();
                    foreach (var item in qryVenSale)
                    {
                        salesList.Add(item);
                        if (venSale == null)
                        {
                            venSale = item;
                            if (vendordefaults.SalesContactId == null)
                            {
                                vendordefaults.SalesContactId = item.Id;
                                vendordefaults.SalesName = string.Format("{0} {1}", item.FirstName, item.LastName);
                                //custSalesContact = db.CustomersSalesContacts.Find(item.Id);
                            }
                        }
                    }
                }
                else
                {
                    venSale = new VendorsSalesContact();
                    venSale.VendorId = vendor.Id;
                    venSale.Address = string.Empty;
                    venSale.City = string.Empty;
                    venSale.Country = string.Empty;
                    venSale.Email = string.Empty;
                    venSale.Fax = "0";
                    venSale.FirstName = string.Empty;
                    venSale.LastName = string.Empty;
                    venSale.Note = string.Empty;
                    venSale.State = string.Empty;
                    venSale.Tel = "0";
                    venSale.Title = string.Empty;
                    venSale.Zip = string.Empty;
                    db.VendorsSalesContacts.Add(venSale);
                    db.SaveChanges();
                    salesList.Add(venSale);
                    if (vendordefaults.SalesContactId == null)
                    {
                        vendordefaults.SalesContactId = venSale.Id;
                        vendordefaults.SalesName = string.Format("{0} {1}", venSale.FirstName, venSale.LastName);
                        //custSalesContact = db.CustomersSalesContacts.Find(item.Id);
                    }
                }

                qryCusNote = db.VendorsSpecialNotes.Where(cutadd => cutadd.VendorId == vendor.Id).OrderByDescending(cutadd => cutadd.Id);
                if (qryCusNote.Count() > 0)
                {
                    //CustNotes = qryCusNote.FirstOrDefault<CustomersSpecialNotes>();
                    foreach (var item in qryCusNote)
                    {
                        notesList.Add(item);
                        if (CustNotes == null)
                        {
                            CustNotes = item;
                        }
                    }
                }
                else
                {
                    CustNotes = new VendorsSpecialNotes();
                    CustNotes.VendorId = vendor.Id;
                    db.VendorsSpecialNotes.Add(CustNotes);
                    notesList.Add(CustNotes);
                }
            }

            db.SaveChanges();

            ViewBag.VendorInfo = szVendorId;
            ViewBag.VendorId = szVendorId;

            //Get the dropdown data
            listSelector = new List<KeyValuePair<string, string>>();
            //qryBussines = db.VendorTypes.OrderBy(bss => bss.VendorType);
            //if (qryBussines.Count() > 0)
            //{
            //    foreach (var item in qryBussines)
            //    {
            //        listSelector.Add(new KeyValuePair<string, string>(item.VendorType, item.VendorType));
            //    }
            //}
            qryBussines01 = db.Bussines.OrderBy(bss => bss.BussinesType);
            if (qryBussines01.Count() > 0)
            {
                foreach (var item in qryBussines01)
                {
                    listSelector.Add(new KeyValuePair<string, string>(item.BussinesType, item.BussinesType));
                }
            }

            SelectList bussineslist = new SelectList(listSelector, "Key", "Value");
            ViewBag.VendorTypeList = 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>>();
            qryVenSale = db.VendorsSalesContacts.Where(vdad => vdad.VendorId == vendor.Id).OrderBy(vdad => vdad.FirstName).ThenBy(vdad => vdad.LastName);
            if (qryVenSale.Count() > 0)
            {
                foreach (var item in qryVenSale)
                {
                    szMsg = string.Format("{0} {1}", item.FirstName, item.LastName);
                    listSelector.Add(new KeyValuePair<string, string>(item.Id.ToString(), szMsg));
                }
            }
            SelectList SalesContactList = new SelectList(listSelector, "Key", "Value");
            ViewBag.SalesContactList = SalesContactList;

            //Fix null values
            if (venSale == null)
            {
                venSale = new VendorsSalesContact();
                venSale.VendorId = vendor.Id;
                venSale.Tel = string.Empty;
                venSale.Fax = string.Empty;
                db.VendorsSalesContacts.Add(venSale);
                db.SaveChanges();

            }
            else
            {
                if (string.IsNullOrEmpty(venSale.Tel))
                {
                    venSale.Tel = string.Empty;
                }
                if (string.IsNullOrEmpty(venSale.Fax))
                {
                    venSale.Fax = string.Empty;
                }
            }

            VendorView vendorview = new VendorView();
            vendorview.vendor = vendor;
            vendorview.vendoraddress = venAddress;
            vendorview.vendorbilling = venBillin;
            vendorview.vendorhistory = venHistory;
            vendorview.vendorsalescontact = venSale;
            vendorview.vendornote = CustNotes;
            vendorview.vendordefaults = vendordefaults;

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

            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 "Sales":
                        pageSales = pageIndex;
                        break;
                    default:
                        break;
                }
            }

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

            if (pageSales == 0)
            {
                pageSales = 1;
            }

            var salesListHlp = salesList.ToPagedList(pageSales, pageSize);
            ViewBag.OnePageOfsalesData = salesListHlp;
            vendorview.vendorsalesList = salesListHlp;

            var notesListHlp = notesList.ToPagedList(pageSales, pageSize);
            ViewBag.OnePageOfnoteData = notesListHlp;
            vendorview.vendornotesList = notesListHlp;

            return View(vendorview);
        }
        public ActionResult UpdateVendorhistory(VendorView vendorview)
        {
            VendorsHistory customeraddress = null;

            if (vendorview.vendorhistory != null)
            {
                if (vendorview.vendorhistory.Id == 0)
                {
                    customeraddress = new VendorsHistory();
                }
                else
                {
                    customeraddress = db.VendorsHistories.Find(vendorview.vendorhistory.Id);
                }

                if (customeraddress != null)
                {
                    customeraddress.OpenPurchaseOrder = vendorview.vendorhistory.OpenPurchaseOrder;
                    customeraddress.OutstandingBalance = vendorview.vendorhistory.OutstandingBalance;
                    customeraddress.VendorId = vendorview.vendorhistory.VendorId;

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

                }
            }

            return RedirectToAction("Edit", new { id = Convert.ToInt32(customeraddress.VendorId) });
            //return RedirectToAction("Index");
        }
 public ActionResult Edit(VendorsHistory vendorshistory)
 {
     if (ModelState.IsValid)
     {
         db.Entry(vendorshistory).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(vendorshistory);
 }