Ejemplo n.º 1
0
        public IActionResult EditStock([Required] int stockId, int?currencyId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var stock = _stockRepository.Stocks
                        .Where(s => s.StockId == stockId)
                        .Include(s => s.StockProduct).ThenInclude(sp => sp.Product)
                        .SingleOrDefault();

            if (stock == null)
            {
                return(NotFound());
            }

            var model = _viewModelHelper.GetUpdateStockViewModel();

            model.StockId         = stock.StockId;
            model.StockName       = stock.StockName;
            model.Address         = stock.Address;
            model.ProductsInStock = stock.StockProduct.Select(sp => sp.Product);
            if (currencyId.HasValue)
            {
                model.CurrencyId = currencyId.Value;

                var currency = _currencyRepository.Currencies.Where(c => c.CurrencyId == currencyId).SingleOrDefault();
                if (currency != default)
                {
                    model.CurrentCurrency = currency;
                    model.Sum             = stock.StockProduct.Select(sp => Convert.ToDecimal(sp.Count) * sp.Product.PriceInBaseCurrency / currency.Rate).Sum();
                }
            }
            else
            {
                var currency = _currencyRepository.GetBaseCurrency();
                model.CurrentCurrency = currency;
                model.CurrencyId      = currency.CurrencyId;
                model.Sum             = stock.StockProduct.Select(sp => Convert.ToDecimal(sp.Count) * sp.Product.PriceInBaseCurrency).Sum();
            }

            return(View(model));
        }