Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,Position,ProductID,Quantity,Unit,ShoppingCartID")] ShoppingCartLine shoppingCartLine)
        {
            if (id != shoppingCartLine.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(shoppingCartLine);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ShoppingCartLineExists(shoppingCartLine.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            ViewData["ShoppingCartID"] = new SelectList(_context.ShoppingCarts, "ID", "ID", shoppingCartLine.ShoppingCartID);
            return(View(shoppingCartLine));
        }
Пример #2
0
        /// <summary>
        /// Adds to shopping cart.
        /// </summary>
        /// <param name="productCode">The product code.</param>
        /// <param name="quantity">The quantity.</param>
        public static void AddToShoppingCart(string productCode, string quantity)
        {
            Assert.ArgumentNotNullOrEmpty(productCode, "productCode");
            Assert.ArgumentNotNullOrEmpty(quantity, "quantity");

            IShoppingCartManager shoppingCartManager = Context.Entity.Resolve <IShoppingCartManager>();

            uint q;

            if (string.IsNullOrEmpty(quantity) || !uint.TryParse(quantity, out q))
            {
                shoppingCartManager.AddProduct(productCode, 1);
            }
            else
            {
                shoppingCartManager.AddProduct(productCode, q);
            }

            ShoppingCart     shoppingCart             = Context.Entity.GetInstance <ShoppingCart>();
            ShoppingCartLine existingShoppingCartLine = shoppingCart.ShoppingCartLines.FirstOrDefault(p => p.Product.Code.Equals(productCode));

            try
            {
                Tracker.StartTracking();
                AnalyticsUtil.AddToShoppingCart(existingShoppingCartLine.Product.Code, existingShoppingCartLine.Product.Title, 1, existingShoppingCartLine.Totals.PriceExVat);
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
        }
        public void UpdateQuantityByItemId(string itemId, int quantity)
        {
            ShoppingCartLine cartLine = this.NState.CurrentShoppingCart.FindLine(_catalogService.GetItem(itemId));

            cartLine.Quantity = quantity;
            this.NState.Save();
        }
Пример #4
0
        /// <summary>
        /// Maps the specified source.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="destination">The destination.</param>
        protected virtual void Map([NotNull] ShoppingCartLine source, [NotNull] DomainModel.Orders.OrderLine destination)
        {
            Assert.ArgumentNotNull(source, "source");
            Assert.ArgumentNotNull(destination, "destination");

            destination.Product     = source.Product;
            destination.Totals      = source.Totals;
            destination.Quantity    = source.Quantity;
            destination.FriendlyUrl = source.FriendlyUrl;
            destination.ImageUrl    = source.ImageUrl;
        }
Пример #5
0
        public async Task <IActionResult> Create([Bind("ID,Position,ProductID,Quantity,Unit,ShoppingCartID")] ShoppingCartLine shoppingCartLine)
        {
            if (ModelState.IsValid)
            {
                _context.Add(shoppingCartLine);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewData["ShoppingCartID"] = new SelectList(_context.ShoppingCarts, "ID", "ID", shoppingCartLine.ShoppingCartID);
            return(View(shoppingCartLine));
        }
Пример #6
0
        /// <summary>
        /// Gets the Friendly Url
        /// </summary>
        /// <param name="dataItem">The data item.</param>
        /// <returns>Shopping cart item friendly url.</returns>
        protected string ShoppingCartLineFriendlyUrl(object dataItem)
        {
            ShoppingCartLine shoppingCartLine = dataItem as ShoppingCartLine;

            if (shoppingCartLine == null)
            {
                Log.Warn("Product line is null.", this);
                return("-");
            }

            return(AnalyticsUtil.AddFollowListToQueryString(shoppingCartLine.FriendlyUrl, "ShoppingCartSpot"));
        }
Пример #7
0
        /// <summary>
        /// Gets the Friendly Url
        /// </summary>
        /// <param name="dataItem">
        /// The data item.
        /// </param>
        /// <returns>
        /// Shopping cart item friendly url.
        /// </returns>
        protected string ShoppingCartLineFriendlyUrl(object dataItem)
        {
            string friendlyUrl;

            if (this.DisplayMode == OrderDisplayMode.ShoppingCart)
            {
                ShoppingCartLine shoppingCartItem = (ShoppingCartLine)dataItem;
                friendlyUrl = shoppingCartItem.FriendlyUrl;
            }
            else
            {
                OrderLine orderLine = (OrderLine)dataItem;
                friendlyUrl = orderLine.FriendlyUrl;
            }

            return(AnalyticsUtil.AddFollowListToQueryString(friendlyUrl, this.DisplayMode.ToString()));
        }
Пример #8
0
        public void InitOrder(Account account, ShoppingCart cart, Address address)
        {
            _account   = account;
            _orderDate = DateTime.Now;

            _shippingAddress = address;
            _billingAddress  = address;

            _totalPrice = cart.Total;

            IEnumerator enumerator = cart.GetAllLines();

            while (enumerator.MoveNext())
            {
                ShoppingCartLine cartLine = (ShoppingCartLine)enumerator.Current;
                AddLineItem(cartLine);
            }
        }
Пример #9
0
        /// <summary>
        /// Deletes from shopping cart.
        /// </summary>
        /// <param name="productCode">The product code.</param>
        public static void DeleteFromShoppingCart(string productCode)
        {
            Assert.ArgumentNotNullOrEmpty(productCode, "productCode");

            ShoppingCart     shoppingCart             = Context.Entity.GetInstance <ShoppingCart>();
            ShoppingCartLine existingShoppingCartLine = shoppingCart.ShoppingCartLines.FirstOrDefault(p => p.Product.Code.Equals(productCode));

            try
            {
                if (existingShoppingCartLine != null)
                {
                    Tracker.StartTracking();
                    AnalyticsUtil.ShoppingCartProductRemoved(existingShoppingCartLine.Product.Code, existingShoppingCartLine.Product.Title, existingShoppingCartLine.Quantity);
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
            }

            IShoppingCartManager shoppingCartManager = Context.Entity.Resolve <IShoppingCartManager>();

            shoppingCartManager.RemoveProduct(productCode);
        }
Пример #10
0
        public void UpdateQuantityByItemId(string itemId, int quantity)
        {
            ShoppingCartLine cartLine = _cart.FindLine(_catalogService.GetItem(itemId));

            cartLine.Quantity = quantity;
        }
Пример #11
0
        public void AddLineItem(ShoppingCartLine cartItem)
        {
            LineItem lineItem = new LineItem(_lineItems.Count + 1, cartItem);

            AddLineItem(lineItem);
        }
Пример #12
0
 public ShoppingCartManager(IProductRepository productRepository, IProductStockManager stockManager, ShoppingCartLine cartLinePrototype) : base(productRepository, stockManager, cartLinePrototype)
 {
 }
        public async Task <IActionResult> PutShoppingCartLine([FromRoute] int id, [FromBody] ShoppingCartLine shoppingCartLine)
        {
            _logger.LogDebug($"api/ShoppingCartLines/PutShoppingCartLine id:{id}");
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = ModelState.IsValid }));
            }

            if (id != shoppingCartLine.ID)
            {
                return(BadRequest(new { message = "Die Id ist nicht bekannt" }));
            }
            var cart = await _context.ShoppingCarts.SingleAsync(c => c.ID.Equals(shoppingCartLine.ShoppingCartID));

            var line = await _context.ShoppingCartLines.Where(s => s.ID.Equals(id)).SingleAsync();

            var product = await _context.Products.Where(p => p.ProductID.Equals(shoppingCartLine.ProductID)).SingleAsync();

            decimal lineQuantity = Math.Round(line.Quantity);

            _logger.LogDebug($"api/ShoppingCartLines/PutShoppingCartLine lineQuantity = {lineQuantity}, shoppingCartLine.Quantity = {shoppingCartLine.Quantity}");

            if (lineQuantity < shoppingCartLine.Quantity)
            {
                _logger.LogDebug($"api/ShoppingCartLines/PutShoppingCartLine lineQuantity < shoppingCartLine.Quantity");
                decimal diff = shoppingCartLine.Quantity - line.Quantity;
                _logger.LogDebug($"api/ShoppingCartLines/PutShoppingCartLine lineQuantity < shoppingCartLine.Quantity diff = {diff}, AvailableQuantity = {product.AvailableQuantity}");
                if (product.AvailableQuantity >= diff)
                {
                    product.AvailableQuantity -= diff;
                }
                else
                {
                    return(BadRequest("Nicht genung Ware verfügbar!"));
                }
                _logger.LogDebug($"api/ShoppingCartLines/PutShoppingCartLine lineQuantity < shoppingCartLine.Quantity AvailableQuantity - diff = AvailableQuantity = {product.AvailableQuantity}");
            }
            if (lineQuantity > shoppingCartLine.Quantity)
            {
                _logger.LogDebug($"api/ShoppingCartLines/PutShoppingCartLine lineQuantity > shoppingCartLine.Quantity");
                decimal diff = line.Quantity - shoppingCartLine.Quantity;
                _logger.LogDebug($"api/ShoppingCartLines/PutShoppingCartLine lineQuantity > shoppingCartLine.Quantity diff = {diff}, AvailableQuantity = {product.AvailableQuantity}");
                product.AvailableQuantity += diff;
                _logger.LogDebug($"api/ShoppingCartLines/PutShoppingCartLine lineQuantity > shoppingCartLine.Quantity AvailableQuantity + diff = AvailableQuantity = {product.AvailableQuantity}");
            }


            try
            {
                line.Quantity      = shoppingCartLine.Quantity;
                line.SellBasePrice = shoppingCartLine.SellBasePrice;

                cart.LastChange = DateTime.Now;
                _context.Update(product);
                _context.Update(line);
                _context.Update(cart);
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Api Error PutShoppingCartLine{id}", null);

                if (!ShoppingCartLineExists(id))
                {
                    return(NotFound(new { message = "Zeile konnte nicht geändert werden!" }));
                }
                else
                {
                    return(BadRequest(e));
                }
            }

            return(Ok(new { id = shoppingCartLine.ID, productid = shoppingCartLine.ProductID }));
        }
        public async Task <ActionResult <ShoppingCartLine> > PostShoppingCartLine([FromBody] ShoppingCartLine shoppingCartLine)
        {
            int position = 1000;

            try
            {
                var lines = await _context.ShoppingCartLines.Where(l => l.ShoppingCartID == shoppingCartLine.ShoppingCartID).ToListAsync();

                var product = await _context.Products.Where(p => p.ProductID.Equals(shoppingCartLine.ProductID)).SingleAsync();

                var cart = await _context.ShoppingCarts.SingleAsync(c => c.ID.Equals(shoppingCartLine.ShoppingCartID));

                cart.LastChange = DateTime.Now;
                _context.ShoppingCarts.Update(cart);

                if (product.AvailableQuantity >= shoppingCartLine.Quantity)
                {
                    product.AvailableQuantity -= shoppingCartLine.Quantity;
                    _context.Products.Update(product);
                }
                else
                {
                    return(BadRequest(new { message = "Nicht genung Ware verfügbar!" }));
                }



                if (lines != null && lines.Count() > 0)
                {
                    position += lines.Count() + 1;
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(new { message = ModelState }));
                }

                bool isUpdate = false;

                foreach (ShoppingCartLine line in lines)
                {
                    if (line.ProductID == shoppingCartLine.ProductID)
                    {
                        isUpdate       = true;
                        line.Quantity += shoppingCartLine.Quantity;
                        _context.ShoppingCartLines.Update(line);
                        break;
                    }
                }

                if (!isUpdate)
                {
                    if (shoppingCartLine.Quantity >= product.MinimumPurchaseQuantity)
                    {
                        shoppingCartLine.Position = position;
                        _context.ShoppingCartLines.Add(shoppingCartLine);
                    }
                    else
                    {
                        return(BadRequest(new { message = "Die Menge liegt unter Mindestabnahme!" }));
                    }
                }
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Api Error Post ShoppingCartLine", null);
                return(BadRequest(e));
            }

            //return CreatedAtAction(nameof(GetShoppingCartLine), new { id = shoppingCartLine.ID }, shoppingCartLine);
            //return CreatedAtAction("GetShoppingCartLine", shoppingCartLine);
            return(Ok(new { id = shoppingCartLine.ID, productid = shoppingCartLine.ProductID }));
        }
Пример #15
0
 public LineItem(int lineNumber, ShoppingCartLine cartItem)
 {
     _lineNumber   = lineNumber;
     this.Quantity = cartItem.Quantity;
     this.Item     = cartItem.Item;
 }