Beispiel #1
0
        public ActionResult Checkout(EditAndCheckoutViewModel viewModel, Cart cart)
        {
            // take products from cart and update quantity
            foreach (var item in viewModel.CartLines)
            {
                CartLineViewModel item1 = item;
                cart.Items.Single(x => x.Product.Id == item1.ProductId).Quantity = item.Quantity;
            }

            if (CurrentUser() == null)
            {
                return(View("LoginOrRegister"));
            }

            int i = 0;

            foreach (CartItem cartItem in cart.Items)
            {
                cartItem.Product = daoTemplate.FindByID <Product>(cartItem.Product.Id);
                if (cartItem.Product.Rest() < cartItem.Quantity)
                {
                    ModelState.AddModelError(string.Format("CartLines[{0}].Quantity", i), "Слишком большое количество");
                }
                i++;
            }
            if (!ModelState.IsValid)
            {
                return(View("Review", new ReviewCartViewModel
                {
                    Cart = cart
                }));
            }
            return(RedirectToAction("EnterShippingDetails"));
        }
Beispiel #2
0
        public void TestGetQuantity_ShouldReturnCorrectly(int quantity)
        {
            var productViewModel = new ProductDetailsViewModel();

            var viewModel = new CartLineViewModel(productViewModel, quantity);

            Assert.AreEqual(quantity, viewModel.Quantity);
        }
Beispiel #3
0
        public void TestGetProductId_ShouldReturnCorrectly(int productId)
        {
            var productViewModel = new ProductDetailsViewModel {
                Id = productId
            };

            var viewModel = new CartLineViewModel(productViewModel, 0);

            Assert.AreEqual(productId, viewModel.ProductId);
        }
Beispiel #4
0
        public void TestViewMyInit_ShouldSetViewModelTotalCorrectly(string name,
                                                                    int quantity,
                                                                    decimal price,
                                                                    double volume,
                                                                    string maker,
                                                                    string imageMimeType,
                                                                    string categoryName,
                                                                    int id,
                                                                    decimal total)
        {
            var imgBuffer = new byte[3];
            var category  = new Category(categoryName);
            var product   = new Models.Product(name, category, quantity, price, volume, maker, imageMimeType, imgBuffer);

            var mockedView = new Mock <ICartSummaryView>();

            mockedView.Setup(v => v.Model).Returns(new CartSummaryVIewModel());

            var mockedCartLine = new Mock <ICartLine>();

            mockedCartLine.Setup(c => c.Product).Returns(product);
            mockedCartLine.Setup(c => c.Quantity).Returns(quantity);

            var cartLines = new List <ICartLine> {
                mockedCartLine.Object
            };

            var mockedShoppingCart = new Mock <IShoppingCart>();

            mockedShoppingCart.Setup(c => c.CartLines).Returns(cartLines);
            mockedShoppingCart.Setup(c => c.ComputeTotal()).Returns(total);

            var productDetailsViewModel = new ProductDetailsViewModel {
                Id = id
            };

            var cartLineViewModel = new CartLineViewModel(productDetailsViewModel, quantity);

            var mockedFactory = new Mock <IViewModelFactory>();

            mockedFactory.Setup(f => f.CreateCartLineViewModel(It.IsAny <ProductDetailsViewModel>(), It.IsAny <int>()))
            .Returns(cartLineViewModel);

            var expectedResult = new List <CartLineViewModel> {
                cartLineViewModel
            };

            var presenter = new CartSummaryPresenter(mockedView.Object, mockedShoppingCart.Object, mockedFactory.Object);

            mockedView.Raise(v => v.MyInit += null, EventArgs.Empty);

            Assert.AreEqual(total, mockedView.Object.Model.Total);
        }
        public async Task <RedirectToRouteResult> RemoveFromCart(Cart cart, CartLineViewModel model)
        {
            GoodsModel goods = await this.repository.GetGoodsByIdAsync(model.goodsId);

            SizeModel size = await this.sizeRepository.GetSizeByIdAsync((int)model.sizeId);

            if (goods != null)
            {
                cart.RemoveLine(goods, size);
            }
            return(RedirectToAction("Index", new { model.returnUrl }));
        }
        public async Task <RedirectToRouteResult> AddToCart(Cart cart, CartLineViewModel model)
        {
            if (model.sizeId != null)
            {
                GoodsModel goods = await this.repository.GetGoodsByIdAsync(model.goodsId);

                SizeModel size = await this.sizeRepository.GetSizeByIdAsync((int)model.sizeId);

                if (goods != null)
                {
                    cart.AddItem(goods, size, Convert.ToInt32(model.quantity));
                }
                return(RedirectToAction("Index", new { model.returnUrl }));
            }
            TempData["message"] = "Выберите размер";
            return(RedirectToAction("GetGoodsDescription", "Goods", new { goodsId = model.goodsId }));
        }
Beispiel #7
0
        public ActionResult Recalc(EditAndCheckoutViewModel viewModel, Cart cart)
        {
            // take products from cart and update quantity
            foreach (var item in viewModel.CartLines)
            {
                CartLineViewModel item1 = item;
                cart.Items.Single(x => x.Product.Id == item1.ProductId).Quantity = item.Quantity;
            }
            int i = 0;

            foreach (CartItem cartItem in cart.Items)
            {
                cartItem.Product = daoTemplate.FindByID <Product>(cartItem.Product.Id);
                if (cartItem.Product.Rest() < cartItem.Quantity)
                {
                    ModelState.AddModelError(string.Format("CartLines[{0}].Quantity", i), "Слишком большое количество");
                }
                i++;
            }
            return(View("Review", new ReviewCartViewModel {
                Cart = cart
            }));
        }
        // GET: Orders/Create
        public async Task <IActionResult> Create(Guid?id)
        {
            cartHelper.CheckAndRemove();

            OrderCreateViewModel   vm        = new OrderCreateViewModel();
            List <ShippingAddress> addresses = new List <ShippingAddress>();
            var identity    = User.Identity;
            var currentUser = _userManager.Users.Single(u => u.UserName == identity.Name);
            var userId      = currentUser.Id;

            var customer = await _context.Customers.Where(c => c.UserId == userId).SingleAsync();

            ShippingPrice shippingPriceDefault = null;
            decimal       shipDefaultPrice     = 0.0M;

            int countryDefault          = 1;   //Country 1 Deutschland
            int ShippingPeriodDefaultID = 1;

            if (customer.CustomerID != Guid.Empty)
            {
                var shipping = await _context.ShippingAddresses.SingleOrDefaultAsync(c => c.CustomerID == customer.CustomerID && c.IsMainAddress);

                if (shipping == null)
                {
                    return(RedirectToAction("CustomerIndex", "ShippingAddresses", new { id = customer.CustomerID }));
                }

                countryDefault = shipping.CountryID;
            }

            var countries = await _context.Countries.ToListAsync();

            var country = countries.Single(c => c.ID == countryDefault);

            ViewData["CustomerCountryCode"] = country.Code;

            if (id != null)
            {
                Guid cartId       = (Guid)id;
                var  shoppingCart = await _context.ShoppingCarts.SingleAsync(sc => sc.ID == id);

                decimal total = 0.0M;
                var     lines = _context.ShoppingCartLines.Where(l => l.ShoppingCartID.Equals(shoppingCart.ID));
                List <CartLineViewModel> vmcLines = new List <CartLineViewModel>();

                foreach (var item in lines)
                {
                    string path = string.Empty;
                    try
                    {
                        path = _context.ProductImages.Where(i => i.ProductID.Equals(item.ProductID) && i.IsMainImage).SingleOrDefault().ImageUrl;
                    }
                    catch (Exception)
                    {
                        path = "noImage.svg";
                    }
                    var product = _context.Products.Where(p => p.ProductID.Equals(item.ProductID)).SingleOrDefault();

                    if (ShippingPeriodDefaultID < product.ShippingPeriod)
                    {
                        ShippingPeriodDefaultID = product.ShippingPeriod;
                    }
                    var productShipPrice = await _context.ShippingPrices.SingleAsync(s => s.ShippingPriceTypeId == product.ShippingPriceType && s.CountryId == countryDefault);

                    if (shipDefaultPrice < productShipPrice.Price)
                    {
                        shipDefaultPrice     = productShipPrice.Price;
                        shippingPriceDefault = productShipPrice;
                    }

                    decimal baseprice = _context.Products.Where(p => p.ProductID.Equals(item.ProductID)).SingleOrDefault().Price;

                    decimal pPrice = 0.0M;
                    if (baseprice != 0.0M)
                    {
                        pPrice = baseprice * item.Quantity;
                    }

                    if (string.IsNullOrEmpty(path))
                    {
                        path = "noImage.svg";
                    }
                    var               unitHelper = new UnitHelper(_context, factory);
                    string            unit       = unitHelper.GetUnitName(product.BasesUnitID);
                    string            sekunit    = unitHelper.GetUnitName(product.SecondBaseUnit);
                    CartLineViewModel cvml       = new CartLineViewModel()
                    {
                        ID                      = item.ID,
                        CartID                  = item.ShoppingCartID,
                        ImgPath                 = path,
                        Position                = item.Position,
                        PosPrice                = Math.Round(pPrice, 2),
                        Quantity                = Math.Round(item.Quantity, 2),
                        ProductID               = item.ProductID,
                        Unit                    = unit,
                        ProductName             = product.Name,
                        ProductNo               = product.ProductNumber.ToString(),
                        MinimumPurchaseQuantity = Math.Round(product.MinimumPurchaseQuantity, 2),
                        AvailableQuantity       = Math.Round(product.AvailableQuantity, 2),
                        ShoppingCartID          = shoppingCart.ID,
                        SellBasePrice           = Math.Round(item.SellBasePrice, 2),
                        SellSekPrice            = Math.Round(product.SecondBasePrice, 2),
                        SekUnit                 = sekunit,
                        ShortDescription        = product.ShortDescription,
                        UnitID                  = product.BasesUnitID
                    };
                    vmcLines.Add(cvml);
                    total = total + pPrice;
                }

                CartViewModel cvm = new CartViewModel()
                {
                    ID      = shoppingCart.ID,
                    Number  = shoppingCart.Number,
                    OrderId = shoppingCart.OrderId,
                    Lines   = vmcLines,
                    Total   = total,
                };

                addresses = await _context.ShippingAddresses.Where(sh => sh.CustomerID == customer.CustomerID).ToListAsync();

                vm.ShippingAddresseVMs = new List <ShippingAddressViewModel>();
                int mainShipID = 0;
                foreach (var item in addresses)
                {
                    var shipVm = new ShippingAddressViewModel
                    {
                        ID                = item.ID,
                        FirstName         = item.FirstName,
                        LastName          = item.LastName,
                        Address           = item.Address,
                        AdditionalAddress = item.AdditionalAddress,
                        PostCode          = item.PostCode,
                        City              = item.City,
                        CountryID         = item.CountryID,
                        CustomerID        = item.CustomerID,
                        IsMainAddress     = item.IsMainAddress,
                        CountryName       = countries.Single(c => c.ID == item.CountryID).Name
                    };
                    if (shipVm.IsMainAddress)
                    {
                        mainShipID = shipVm.ID;
                    }
                    vm.ShippingAddresseVMs.Add(shipVm);
                }

                string strOrderNo = await GetActualOrderNo();

                vm.Cart                    = cvm;
                vm.Order                   = new Order();
                vm.Order.Number            = strOrderNo;
                vm.Order.CartID            = cvm.ID;
                vm.Order.ShippingAddressId = mainShipID;
                vm.Order.OrderDate         = DateTime.Now;
            }

            var paymends = await _context.Paymends.ToListAsync();

            ShippingPeriod periodDefault = await _context.ShpippingPeriods.SingleAsync(s => s.ShippingPeriodID == ShippingPeriodDefaultID);

            vm.Cart.Total = vm.Cart.Total + shipDefaultPrice;

            vm.Cart.Total = Math.Round(vm.Cart.Total, 2);

            vm.PayPalTotal = Math.Round(vm.Cart.Total, 2).ToString(CultureInfo.CreateSpecificCulture("en-US"));

            shipDefaultPrice = Math.Round(shipDefaultPrice);

            vm.ShippingPrice = shippingPriceDefault;

            vm.ShippingPeriod = periodDefault;

            vm.CanBuyWithBill = customer.AllowedPayByBill;

            ViewData["Paymends"] = paymends;


            return(View(vm));
        }
        // GET: ShoppingCarts/Details/5
        public async Task <IActionResult> Details(Guid?id)
        {
            var     sessioncart      = HttpContext.Session.GetString("ShoppingCartId");
            int     shipTypeDefault  = 1;      //Type 1 = kleines Paket
            decimal shipDefaultPrice = 0.0M;
            int     countryDefault   = 1;      //Country 1 Deutschland
            int     periodDefault    = 1;



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

            var shoppingCart = await _context.ShoppingCarts.SingleOrDefaultAsync(m => m.ID == id);

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

            if (shoppingCart.CustomerId != Guid.Empty)
            {
                var shipping = await _context.ShippingAddresses.SingleOrDefaultAsync(c => c.CustomerID == shoppingCart.CustomerId && c.IsMainAddress);

                if (shipping == null)
                {
                    return(RedirectToAction("CustomerIndex", "ShippingAddresses", new { id = shoppingCart.CustomerId }));
                }
                countryDefault = shipping.CountryID;
            }

            decimal total = 0.0M;
            var     lines = _context.ShoppingCartLines.Where(l => l.ShoppingCartID.Equals(shoppingCart.ID));
            List <CartLineViewModel> vmcLines = new List <CartLineViewModel>();

            foreach (var item in lines)
            {
                string path = string.Empty;
                try
                {
                    path = _context.ProductImages.Where(i => i.ProductID.Equals(item.ProductID) && i.IsMainImage).SingleOrDefault().ImageUrl;
                }
                catch (Exception)
                {
                    path = "noImage.svg";
                }
                var product = _context.Products.Where(p => p.ProductID.Equals(item.ProductID)).SingleOrDefault();

                if (periodDefault < product.ShippingPeriod)
                {
                    periodDefault = product.ShippingPeriod;
                }

                var productShipPrice = await _context.ShippingPrices.SingleAsync(s => s.ShippingPriceTypeId == product.ShippingPriceType && s.CountryId == countryDefault);

                if (shipDefaultPrice < productShipPrice.Price)
                {
                    shipDefaultPrice = productShipPrice.Price;
                    shipTypeDefault  = productShipPrice.ShippingPriceTypeId;
                }

                decimal baseprice = _context.Products.Where(p => p.ProductID.Equals(item.ProductID)).SingleOrDefault().Price;

                decimal pPrice = 0.0M;
                if (baseprice != 0.0M)
                {
                    pPrice = baseprice * item.Quantity;
                }

                if (string.IsNullOrEmpty(path))
                {
                    path = "noImage.svg";
                }
                string unit    = new UnitHelper(_context, factory).GetUnitName(product.BasesUnitID);
                string sekunit = new UnitHelper(_context, factory).GetUnitName(product.SecondBaseUnit);

                CartLineViewModel cvml = new CartLineViewModel()
                {
                    ID                      = item.ID,
                    CartID                  = item.ShoppingCartID,
                    ImgPath                 = path,
                    Position                = item.Position,
                    PosPrice                = pPrice,
                    Quantity                = item.Quantity,
                    ProductID               = item.ProductID,
                    Unit                    = unit,
                    UnitID                  = item.UnitID,
                    ProductName             = product.Name,
                    ProductNo               = product.ProductNumber.ToString(),
                    ShortDescription        = product.ShortDescription,
                    MinimumPurchaseQuantity = Math.Round(product.MinimumPurchaseQuantity, 2),
                    AvailableQuantity       = Math.Round(product.AvailableQuantity, 2),
                    ShoppingCartID          = shoppingCart.ID,
                    SellBasePrice           = Math.Round(item.SellBasePrice, 2),
                    SellSekPrice            = Math.Round(product.SecondBasePrice, 2),
                    SekUnit                 = sekunit, SlugUrl = $"{item.ProductID}-{product.ProductNumber}-{FriendlyUrlHelper.ReplaceUmlaute(product.Name)}"
                };
                vmcLines.Add(cvml);
                total = total + pPrice;
            }

            ShippingPeriod shippingPeriod = await _context.ShpippingPeriods.SingleAsync(s => s.ShippingPeriodID == periodDefault);

            total            = total + shipDefaultPrice;
            shipDefaultPrice = Math.Round(shipDefaultPrice, 2);
            var shippreise = await new ShippingPricesHelpers(_context).GetShippingPricesViewModels(shipTypeDefault);

            CartViewModel vm = new CartViewModel()
            {
                ID                 = shoppingCart.ID,
                Number             = shoppingCart.Number,
                OrderId            = shoppingCart.OrderId,
                Lines              = vmcLines,
                Total              = total,
                DefaultCountry     = countryDefault,
                ShipPrices         = shippreise,
                ShippingPeriodName = shippingPeriod.Decription
            };

            cartHelper.CheckAndRemove();

            return(View(vm));
        }
        // GET: ShoppingCarts/Edit/5
        public async Task <IActionResult> Edit(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var shoppingCart = await _context.ShoppingCarts.SingleOrDefaultAsync(m => m.ID == id);

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

            var lines = _context.ShoppingCartLines.Where(l => l.ShoppingCartID.Equals(shoppingCart.ID));
            List <CartLineViewModel> vmcLines = new List <CartLineViewModel>();

            foreach (var item in lines)
            {
                string path    = _context.ProductImages.Where(i => i.ProductID.Equals(item.ProductID) && i.IsMainImage).SingleOrDefault().ImageUrl;
                var    product = _context.Products.Where(p => p.ProductID.Equals(item.ProductID)).SingleOrDefault();

                decimal baseprice = _context.Products.Where(p => p.ProductID.Equals(item.ProductID)).SingleOrDefault().Price;

                decimal pPrice = 0.0M;
                if (baseprice != 0.0M)
                {
                    pPrice = baseprice * item.Quantity;
                }

                if (path == null || path.Length == 0)
                {
                    path = "noImage.svg";
                }

                string unit = _context.Units.Where(u => u.UnitID == item.UnitID).Single().Name;

                CartLineViewModel cvml = new CartLineViewModel()
                {
                    ID             = item.ID,
                    CartID         = item.ShoppingCartID,
                    ImgPath        = path,
                    Position       = item.Position,
                    PosPrice       = pPrice,
                    Quantity       = item.Quantity,
                    ProductID      = item.ProductID,
                    Unit           = unit,
                    ProductName    = product.Name,
                    ProductNo      = product.ProductNumber.ToString(),
                    ShoppingCartID = shoppingCart.ID
                };
                vmcLines.Add(cvml);
            }

            CartViewModel vm = new CartViewModel()
            {
                ID      = shoppingCart.ID,
                Number  = shoppingCart.Number,
                OrderId = shoppingCart.OrderId,
                Lines   = vmcLines
            };


            return(View(vm));
        }