Ejemplo n.º 1
0
        public ActionResult Return(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            SaleOrder order = _context.SaleOrders.Find(id);

            order.Items = _context.Items.Where(i => i.SaleOrderId == order.Id).Include(i => i.Product).ToList();

            foreach (var item in order.Items)
            {
                item.Product.Stocks = _context.Stocks.Where(s => s.ProductId == item.ProductId).ToList();
            }

            if (order == null)
            {
                return(HttpNotFound());
            }

            if (order.UserId != Global.User.Id)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }

            order.ReturnOrder();

            foreach (var item in order.Items)
            {
                foreach (var stock in item.Product.Stocks)
                {
                    if (stock.Id == 0)
                    {
                        _context.Entry(stock).State = EntityState.Added;
                    }
                }
            }

            _context.Entry(order).Property(o => o.OrderStatus).IsModified = true;
            _context.Entry(order).State = EntityState.Detached;
            _context.SaveChanges();

            return(RedirectToAction("Index"));
        }