public ActionResult AddProductEdit(int PriceListId, int?SalesOrderHeaderId)
 {
     //var CustomerPriceList = db.CustomerPriceLists.Where(c => c.CustomerId == CustomerID).OrderBy(c => c.CustomerId).ToList();
     ViewBag.PriceListId = new SelectList(db.PriceLists.Where(p => p.PriceListId == PriceListId), "PriceListId", "ListDescription");
     ViewBag.PartId      = new SelectList(CombosHelper.GetPriceListPart(PriceListId), "PartId", "PartDescription");
     //ViewBag.PriceListPartId=
     return(View());
 }
        public ActionResult AddProductEdit(AddproductEditView view)
        {
            int Line;

            if (ModelState.IsValid)
            {
                var orderDetailConsultaLine = db.OrderDetails.Where(o => o.SalesOrderHeaderId == view.SalesOrderHeaderId).OrderByDescending(o => o.OrderLine).FirstOrDefault();
                if (orderDetailConsultaLine != null)
                {
                    Line = orderDetailConsultaLine.OrderLine;
                }
                else
                {
                    Line = 1;
                }
                var orderDetailConsulta = db.OrderDetails.Where(o => o.SalesOrderHeaderId == view.SalesOrderHeaderId && o.PartId == view.PartId).FirstOrDefault();
                if (orderDetailConsulta == null)
                {
                    var priceListParts = db.PriceListParts.Where(p => p.PriceListId == view.PriceListId && p.PartId == view.PartId).FirstOrDefault();
                    var part           = db.Parts.Find(view.PartId);
                    var orderDetail    = new OrderDetail
                    {
                        SalesOrderHeaderId = view.SalesOrderHeaderId,
                        OrderQty           = view.OrderQty,
                        OrderLine          = Line,
                        OrderNum           = 0,
                        PartId             = view.PartId,
                        PartNum            = part.PartNum,
                        PriceListPartId    = priceListParts.PriceListPartId,
                        Reference          = view.Reference,
                        TaxAmt             = 0,
                        Total     = view.OrderQty * view.UnitPrice,
                        UnitPrice = view.UnitPrice,
                    };
                    db.OrderDetails.Add(orderDetail);
                }
                else
                {
                    orderDetailConsulta.OrderQty       += view.OrderQty;
                    db.Entry(orderDetailConsulta).State = EntityState.Modified;
                }

                db.SaveChanges();
                //view.OrderQty = 0;
                //view.UnitPrice = 0;
                //view.Reference = 0;

                // return View(view);
                return(RedirectToAction("Edit/" + view.SalesOrderHeaderId));
            }

            ViewBag.PriceListId = new SelectList(db.PriceLists.Where(p => p.PriceListId == view.PriceListId), "PriceListId", "ListDescription");
            ViewBag.PartId      = new SelectList(CombosHelper.GetPriceListPart(view.PriceListId), "PartId", "PartDescription");
            return(View(view));
        }
        public ActionResult AddProduct(int PriceListId, int?SalesOrderHeaderId, int CustomerId, int ShipToId, int ContactId, DateTime Date, DateTime NeedByDate, string Observations, string Terms)
        {
            if (SalesOrderHeaderId == null || SalesOrderHeaderId == 0)
            {
                var customer = db.Customers.Where(c => c.CustomerId == CustomerId).FirstOrDefault();
                var shipto   = db.ShipToes.Where(s => s.ShipToId == ShipToId).FirstOrDefault();
                var contact  = db.Contacts.Where(c => c.ContactId == ContactId).FirstOrDefault();

                var view = new NewOrderView
                {
                    SalesOrderHeaderId      = 0,
                    ConNum                  = contact.ConNum,
                    ContactId               = contact.ContactId,
                    CreditHold              = customer.CreditHold,
                    CustId                  = customer.CustId,
                    CustomerId              = customer.CustomerId,
                    Observations            = Observations,
                    OrderNum                = 0,
                    Platform                = "WEB",
                    PriceListId             = PriceListId,
                    RowMod                  = "C",
                    SalesOrderHeaderInterId = 0,
                    ShipToId                = shipto.ShipToId,
                    ShipToNum               = shipto.ShipToNum,
                    UserId                  = 1,
                    VendorId                = 74,
                    Date       = Date,
                    NeedByDate = NeedByDate,
                    TermsCode  = Terms
                };
                MovementsHelper.CreateNewOrderNew(view);
            }
            else
            {
                var view = db.NewOrderView.Find(SalesOrderHeaderId);
                view.PriceListId  = PriceListId;
                view.Date         = Date;
                view.Observations = Observations;
                view.NeedByDate   = NeedByDate;
                db.SaveChanges();
            }


            //var CustomerPriceList = db.CustomerPriceLists.Where(c => c.CustomerId == CustomerID).OrderBy(c => c.CustomerId).ToList();
            ViewBag.PriceListId = new SelectList(db.PriceLists.Where(p => p.PriceListId == PriceListId), "PriceListId", "ListDescription", PriceListId);
            ViewBag.PartId      = new SelectList(CombosHelper.GetPriceListPart(PriceListId), "PartId", "PartDescription");
            //ViewBag.PriceListPartId=
            return(View());
        }
        public ActionResult AddProduct(AddproductView view)
        {
            if (ModelState.IsValid)
            {
                var orderDetailTmpConsulta = db.OrderDetailTmp.Where(o => o.UserId == 1 && o.PartId == view.PartId).FirstOrDefault();

                if (orderDetailTmpConsulta == null)
                {
                    var priceListParts = db.PriceListParts.Where(p => p.PriceListId == view.PriceListId && p.PartId == view.PartId).FirstOrDefault();
                    var part           = db.Parts.Find(view.PartId);
                    var orderDetailTmp = new OrderDetailTmp
                    {
                        OrderQty        = view.OrderQty,
                        PartId          = view.PartId,
                        PartNum         = part.PartNum,
                        PartDescription = part.PartDescription,
                        PriceListPartId = priceListParts.PriceListPartId,
                        Reference       = view.Reference,
                        TaxAmt          = 0,
                        Total           = view.OrderQty * view.UnitPrice,
                        UnitPrice       = view.UnitPrice,
                        UserId          = 1
                    };
                    db.OrderDetailTmp.Add(orderDetailTmp);
                }
                else
                {
                    orderDetailTmpConsulta.OrderQty       += view.OrderQty;
                    db.Entry(orderDetailTmpConsulta).State = EntityState.Modified;
                }

                db.SaveChanges();
                //view.OrderQty = 0;
                //view.UnitPrice = 0;
                //view.Reference = 0;

                // return View(view);
                return(RedirectToAction("Create"));
            }

            ViewBag.PriceListId = new SelectList(db.PriceLists.Where(p => p.PriceListId == view.PriceListId), "PriceListId", "ListDescription");
            ViewBag.PartId      = new SelectList(CombosHelper.GetPriceListPart(view.PriceListId), "PartId", "PartDescription");
            return(View(view));
        }