// GET: OrderHeaders/Create
        public ActionResult Create(int?id)
        {
            var view = new NewOrderView
            {
                Date       = DateTime.Now,
                NeedByDate = DateTime.Now
            };

            ViewBag.UserId = new SelectList(db.Users, "UserId", "FirstName");
            var v = db.NewOrderView.Where(o => o.UserId == 1).FirstOrDefault();

            if (v != null)
            {
                ViewBag.CustomerId  = new SelectList(db.Customers.Where(c => c.VendorId == 74 && c.CustomerId == v.CustomerId), "CustomerId", "Names");
                ViewBag.ShipToId    = new SelectList(db.ShipToes.Where(c => c.VendorId == 74), "ShipToId", "ShipToName", v.ShipToId);
                ViewBag.ContactId   = new SelectList(db.Contacts.Where(c => c.VendorId == 74 && c.ShipToId == v.ShipToId), "ContactId", "Name", v.ContactId);
                ViewBag.PriceListId = new SelectList(CombosHelper.GetPriceList(v.CustomerId).OrderBy(P => P.PriceListId), "PriceListId", "ListDescription", v.PriceListId);
                view = v;
            }
            else
            {
                ViewBag.CustomerId  = new SelectList(CombosHelper.GetCustomer(74), "CustomerId", "Names");
                ViewBag.ShipToId    = new SelectList(CombosHelper.GetShipto(74), "ShipToId", "ShipToName");
                ViewBag.ContactId   = new SelectList(CombosHelper.GetContact(74), "ContactId", "Name");
                ViewBag.PriceListId = new SelectList(db.PriceLists.OrderBy(P => P.PriceListId), "PriceListId", "ListDescription");
            }

            view.OrderDetails = db.OrderDetailTmp.Where(o => o.UserId == 1).ToList();
            //ViewBag.ShipToId = new SelectList(db.ShipToes.Where(c => c.VendorId == 74 && c.CustomerId == db.Customers.FirstOrDefault().CustomerId).OrderBy(c => c.ShipToName), "ShipToId", "ShipToName");
            //ViewBag.ContactId = new SelectList(db.Contacts.Where(c => c.VendorId == 74 && c.ShipToId == db.ShipToes.FirstOrDefault().ShipToId).OrderBy(c => c.Name), "ContactId", "Name");
            //ViewBag.PriceListId = new SelectList(db.PriceLists.Where(p => p.PriceListId == 0).OrderBy(P => P.PriceListId), "PriceListId", "ListDescription");
            return(View(view));
        }
        public ActionResult Edit(OrderHeader orderHeader)
        {
            string control = string.Empty;
            int    sw      = 1;

            if (orderHeader.ContactId == 0)
            {
                control = "Contacto";
                sw      = 0;
            }
            else
            {
                var contact = db.Contacts.Where(c => c.ContactId == orderHeader.ContactId).FirstOrDefault();
                orderHeader.ConNum = contact.ConNum;
            }
            if (orderHeader.ShipToId == 0)
            {
                control = "ShipTo";
                sw      = 0;
            }
            else
            {
                var shipto = db.ShipToes.Where(s => s.ShipToId == orderHeader.ShipToId).FirstOrDefault();
                orderHeader.ShipToNum = shipto.ShipToNum;
            }
            if (orderHeader.CustomerId == 0)
            {
                control = "Cliente";
                sw      = 0;
            }
            else
            {
                var customer = db.Customers.Where(c => c.CustomerId == orderHeader.CustomerId).FirstOrDefault();
                orderHeader.CustId = customer.CustId;
            }

            orderHeader.Platform        = "WEB";
            orderHeader.RowMod          = "U";
            orderHeader.SalesCategory   = "";
            orderHeader.OrderDetailList = db.OrderDetails.Where(o => o.SalesOrderHeaderId == orderHeader.SalesOrderHeaderId).ToList();
            if (ModelState.IsValid && sw == 1)
            {
                db.Entry(orderHeader).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CustomerId  = new SelectList(db.Customers.Where(c => c.VendorId == 74 && c.CustomerId == orderHeader.CustomerId), "CustomerId", "Names", orderHeader.CustomerId);
            ViewBag.ShipToId    = new SelectList(db.ShipToes.Where(s => s.VendorId == 74 && s.CustomerId == orderHeader.CustomerId), "ShipToId", "ShipToName", orderHeader.ShipToId);
            ViewBag.ContactId   = new SelectList(db.Contacts.Where(c => c.VendorId == 74 && c.ShipToId == orderHeader.ShipToId), "ContactId", "Name", orderHeader.ContactId);
            ViewBag.PriceListId = new SelectList(CombosHelper.GetPriceList(orderHeader.CustomerId), "PriceListId", "ListDescription", orderHeader.CustomerId);
            ViewBag.UserId      = new SelectList(db.Users, "UserId", "FirstName", orderHeader.UserId);
            ModelState.AddModelError(String.Empty, control + " Invalido");
            return(View(orderHeader));
        }
        // GET: OrderHeaders/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            OrderHeader orderHeader = db.OrderHeaders.Find(id);

            if (orderHeader == null)
            {
                return(HttpNotFound());
            }
            orderHeader.OrderDetailList = db.OrderDetails.Where(o => o.SalesOrderHeaderId == orderHeader.SalesOrderHeaderId).ToList();
            ViewBag.CustomerId          = new SelectList(db.Customers.Where(c => c.VendorId == 74 && c.CustomerId == orderHeader.CustomerId), "CustomerId", "Names", orderHeader.CustomerId);
            ViewBag.ShipToId            = new SelectList(db.ShipToes.Where(s => s.VendorId == 74 && s.CustomerId == orderHeader.CustomerId), "ShipToId", "ShipToName", orderHeader.ShipToId);
            ViewBag.ContactId           = new SelectList(db.Contacts.Where(c => c.VendorId == 74 && c.ShipToId == orderHeader.ShipToId), "ContactId", "Name", orderHeader.ContactId);
            ViewBag.PriceListId         = new SelectList(CombosHelper.GetPriceList(orderHeader.CustomerId).OrderBy(P => P.PriceListId), "PriceListId", "ListDescription");

            ViewBag.UserId = new SelectList(db.Users, "UserId", "FirstName", orderHeader.UserId);
            return(View(orderHeader));
        }