Example #1
0
        public void CalculatePriceInTheMainCurrency_WithMainCurrency()
        {
            //Arrange
            ResetDataBase();
            ProductsBL productsBL = DI.Resolve <ProductsBL>();

            //Act
            decimal actual = productsBL.CalculatePriceInTheMainCurrency(1, 100);

            //Assert
            Assert.That(actual, Is.EqualTo(100));
        }
Example #2
0
        public void CalculatePriceInTheMainCurrency()
        {
            //Arrange
            ResetDataBase();
            ProductsBL productsBL = DI.Resolve <ProductsBL>();

            //Act
            decimal actual = productsBL.CalculatePriceInTheMainCurrency(3, 100, new DateTime(2016, 5, 5));

            //Assert
            Assert.That(actual, Is.EqualTo(2564));
        }
Example #3
0
        public ActionResult Save(ProductCreateOrEditViewModel model)
        {
            if (!model.CategorySelectorViewModel.Id.HasValue)
            {
                ModelState.AddModelError("CategorySelectorViewModel.Name", "Вказана неіснуюча категорія");
            }

            Product alreadyExistedProduct = _productsBL.GetByName(model.Name);

            if (alreadyExistedProduct != null && model.Id.HasValue && model.Id.Value != alreadyExistedProduct.Id)
            {
                if (!alreadyExistedProduct.IsDeleted)
                {
                    ModelState.AddModelError("Name", "Продукт з таким іменем вже існує");
                }
            }

            if (ModelState.IsValid)
            {
                if (alreadyExistedProduct != null && model.Id.HasValue && model.Id.Value != alreadyExistedProduct.Id)
                {
                    if (alreadyExistedProduct.IsDeleted)
                    {
                        _productsBL.DeletePermanently(alreadyExistedProduct.Id);
                    }
                }

                Product target = GetProductToBeSaved(model);

                Product byCode = _productsBL.GetByCode(model.Code);

                if (byCode != null && byCode.Id != target.Id)
                {
                    ModelState.AddModelError("Code", "Введений код належить іншому продукту");
                    return(View("Edit", model));
                }

                target.Categories.Clear();
                target.Categories.Add(_productCategoriesBL.GetById(model.CategorySelectorViewModel.Id.Value));

                target.Name             = model.Name;
                target.Description      = model.Description;
                target.ShortDescription = model.ShortDescription;
                decimal price = decimal.Parse(model.Price);
                target.Price   = price;
                target.Publish = model.Publish;
                target.Code    = model.Code;
                target.PriceInTheMainCurrency = _productsBL.CalculatePriceInTheMainCurrency(model.CurrencyIdOfThePrice.Value, price);
                target.CurrencyIdOfThePrice   = model.CurrencyIdOfThePrice.Value;

                ProcessBrand(model, target);
                ProcessCountry(model, target);
                ProcessPhotos(model, target);
                ProcessMetadata(model.MetaDataViewModel, target);

                if (target.Id == 0)
                {
                    _productsBL.Create(target);
                }
                else
                {
                    _productsBL.Update(target);
                }

                if (ModelState.IsValid)
                {
                    TempData[Constants.TempDataKeys.PRODUCTS_FILTER_VIEW_MODEL] = new ProductsFilterViewModel(model.CategorySelectorViewModel.Id.Value);
                    return(RedirectToAction("Index"));
                }
            }
            if (model.CategorySelectorViewModel.Id.HasValue)
            {
                model.CategorySelectorViewModel.ParentCategories =
                    _productCategoriesBL.GetParentCategories(model.CategorySelectorViewModel.Id.Value).Select(parentCategory => parentCategory.Name).ToArray();
            }

            return(View("Edit", model));
        }