Exemplo n.º 1
0
        public async Task <RedirectToActionResult> AddItemToCart([FromRoute] int id, [FromRoute] int?number)
        {
            var item = await _itemRepository.GetSingleItem(id);

            if (item == null)
            {
                throw new Exception("Item was not found.");
            }
            var user = await _userManager.FindByNameAsync(GetUserName());

            var cartItemFromDb = await _cartItemRepository.GetCartItem(id, user.Id);

            if (cartItemFromDb != null)
            {
                var newNumber = cartItemFromDb.Number + (number ?? 1);
                await _cartItemRepository.EditCartItem(cartItemFromDb.Id, newNumber);
            }
            else
            {
                var cartItem = new Cart_Item
                {
                    ItemId = id,
                    UserId = user.Id,
                    Number = number ?? 1,
                    Type   = "Tipas"
                };
                await _cartItemRepository.Create(cartItem);
            }

            return(RedirectToAction("ItemDetails", "Item"));
        }
Exemplo n.º 2
0
        public List <CartItem> AddCartItemForUser(User user, long productId)
        {
            if (_productRepository.FindById(productId) == null)
            {
                throw new KeyNotFoundException();
            }

            var cartItem = _cartItemRepository.FindById(user.UserId, productId);

            if (cartItem == null)
            {
                _cartItemRepository.Create(new CartItem
                {
                    UserId    = user.UserId,
                    ProductId = productId,
                    Quantity  = 1
                });
            }
            else // If it already exists, we increment quantity
            {
                cartItem.Quantity++;
                _cartItemRepository.Update(cartItem);
            }

            _cartItemRepository.SaveChanges();
            return(GetAllItemsForUser(user));
        }
Exemplo n.º 3
0
        public void AddItem(string documentId, string cartId, string userName, string manCo, string docType, string subDocType)
        {
            var cartItems = _cartItemRepository.GetCart(cartId);

            var cartitem = cartItems.FirstOrDefault(c => c.Document.ManCo.Code != manCo);

            if (cartitem != null)
            {
                throw new BasketContainsDifferentManCoException("Basket has documents from a different man co");
            }

            bool documentIsCheckedOut = _checkOutEngine.IsDocumentCheckedOut(documentId);

            if (documentIsCheckedOut)
            {
                throw new DocumentCurrentlyCheckedOutException(string.Format("Document {0} is already checked out", cartId));
            }

            var checkOut = _checkOutEngine.CheckOutDocument(userName, documentId, manCo, docType, subDocType);

            if (checkOut == null)
            {
                throw new UnableToCheckOutDocumentException(string.Format("Unable to check out document {0}", documentId));
            }

            var document = _documentRepository.GetDocument(documentId);

            if (document == null)
            {
                throw new UnableToRetrieveDocumentException(string.Format("Unable to retrieve document {0}", documentId));
            }

            var cartItem = new CartItem
            {
                CartId     = cartId,
                DocumentId = document.Id
            };

            _cartItemRepository.Create(cartItem);
        }
Exemplo n.º 4
0
 public void Create(CartItem entity)
 {
     _cartItemRepository.Create(entity);
 }
Exemplo n.º 5
0
        public async Task <ActionResult <CartItem> > Create(CartItem cart)
        {
            await _cartRepository.Create(cart);

            return(CreatedAtAction("GetById", new { id = cart.Id }, cart));
        }
        public async Task <ActionResult <CartTransferObject> > UpdateCart(CartTransferObject cart)
        {
            double gross     = 0.00;
            double tax       = 0.00;
            double net       = 0.00;
            var    itemCount = 0;

            if (string.IsNullOrEmpty(cart.Id))
            {
                cart.Id = Guid.NewGuid().ToString();

                foreach (var item in cart.CartItems)
                {
                    await _inventoryService.ReserveStock(new Gateway.DataTransfer.InventoryService.InventoryTransferObject
                    {
                        Sku              = item.Sku,
                        TransactionType  = "RESERVE",
                        TransactionCount = item.Quantity
                    });

                    item.UnitPrice = await _productClient.GetItemBasePrice(item.ProductId);

                    item.NetPrice   = item.UnitPrice * item.Quantity;
                    itemCount      += item.Quantity;
                    net            += (double)item.NetPrice;
                    item.Tax        = item.NetPrice * (decimal)0.2;
                    tax            += (double)item.Tax;
                    item.GrossPrice = item.NetPrice + item.Tax;
                    gross          += (double)item.GrossPrice;

                    _cartItemRepository.Create(new CartItem
                    {
                        CartId = cart.Id,

                        GrossPrice = item.GrossPrice,
                        Name       = item.Name,
                        NetPrice   = item.NetPrice,
                        Quantity   = item.Quantity,
                        Sku        = item.Sku,
                        Tax        = item.Tax,
                        UnitPrice  = item.UnitPrice,
                    });
                }

                var saveCart = new Cart
                {
                    Id          = cart.Id,
                    CreatedDate = DateTime.Now,
                    Gross       = (decimal)gross,
                    ItemCount   = itemCount,
                    LastUpdated = DateTime.Now,
                    Net         = (decimal)net,
                    Tax         = (decimal)tax
                };

                cart.Gross       = (decimal)gross;
                cart.Net         = (decimal)net;
                cart.Tax         = (decimal)tax;
                cart.TotalItems  = itemCount;
                cart.CreatedDate = DateTime.Now;
                cart.LastUpdated = DateTime.Now;

                _cartRepository.Create(saveCart);
                await _cartRepository.SaveChanges();

                return(Ok(cart));
            }
            else
            {
                var updateCart = await _cartRepository.Get(cart.Id);

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


                foreach (var item in cart.CartItems)
                {
                    item.NetPrice   = item.UnitPrice * item.Quantity;
                    itemCount      += item.Quantity;
                    net            += (double)item.NetPrice;
                    item.Tax        = item.NetPrice * (decimal)0.2;
                    tax            += (double)item.Tax;
                    item.GrossPrice = item.NetPrice + item.Tax;
                    gross          += (double)item.GrossPrice;
                }

                updateCart.LastUpdated = DateTime.Now;
                cart.LastUpdated       = updateCart.LastUpdated;
                cart.TotalItems        = updateCart.ItemCount;
                _cartRepository.Update(updateCart);
                foreach (var item in updateCart.CartItems)
                {
                    _cartItemRepository.Update(item);

                    if (_cartItemRepository.GetEntityState(item) == Microsoft.EntityFrameworkCore.EntityState.Deleted)
                    {
                        await _inventoryService.ReserveStock(new Gateway.DataTransfer.InventoryService.InventoryTransferObject
                        {
                            Sku              = item.Sku,
                            TransactionType  = "FREE",
                            TransactionCount = item.Quantity
                        });
                    }
                    else if (_cartItemRepository.GetEntityState(item) == Microsoft.EntityFrameworkCore.EntityState.Added)
                    {
                        await _inventoryService.ReserveStock(new Gateway.DataTransfer.InventoryService.InventoryTransferObject
                        {
                            Sku              = item.Sku,
                            TransactionType  = "RESERVE",
                            TransactionCount = item.Quantity
                        });
                    }
                }


                await _cartRepository.SaveChanges();

                return(Ok(cart));
            }
        }