示例#1
0
        public async Task <IActionResult> Edit(int id, [Bind("ShippingPeriodID,Decription")] ShippingPeriod shippingPeriod)
        {
            if (id != shippingPeriod.ShippingPeriodID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(shippingPeriod);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ShippingPeriodExists(shippingPeriod.ShippingPeriodID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(shippingPeriod));
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("ShippingPeriodID,Decription")] ShippingPeriod shippingPeriod)
        {
            if (ModelState.IsValid)
            {
                _context.Add(shippingPeriod);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(shippingPeriod));
        }
        // 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));
        }