Exemplo n.º 1
0
        public async Task <IActionResult> UpdateTaxAndShippingPrices([FromBody] TaxAndShippingPriceRequestVm model)
        {
            var currentUser = await _workContext.GetCurrentUser();

            Address address;

            if (model.ExistingShippingAddressId != 0)
            {
                address = await _userAddressRepository.Query().Where(x => x.Id == model.ExistingShippingAddressId).Select(x => x.Address).FirstOrDefaultAsync();

                if (address == null)
                {
                    return(NotFound());
                }
            }
            else
            {
                address = new Address
                {
                    CountryId         = model.NewShippingAddress.CountryId,
                    StateOrProvinceId = model.NewShippingAddress.StateOrProvinceId,
                    DistrictId        = model.NewShippingAddress.DistrictId,
                    ZipCode           = model.NewShippingAddress.ZipCode,
                    AddressLine1      = model.NewShippingAddress.AddressLine1,
                };
            }

            var orderTaxAndShippingPrice = new OrderTaxAndShippingPriceVm
            {
                Cart = await _cartService.GetCart(currentUser.Id)
            };

            var cart = await _cartRepository.Query().Where(x => x.Id == orderTaxAndShippingPrice.Cart.Id).FirstOrDefaultAsync();

            cart.TaxAmount = orderTaxAndShippingPrice.Cart.TaxAmount = await _orderService.GetTax(currentUser.Id, address.CountryId, address.StateOrProvinceId, address.ZipCode);

            var request = new GetShippingPriceRequest
            {
                OrderAmount     = orderTaxAndShippingPrice.Cart.OrderTotal,
                ShippingAddress = address
            };

            orderTaxAndShippingPrice.ShippingPrices = await _shippingPriceService.GetApplicableShippingPrices(request);

            var selectedShippingMethod = string.IsNullOrWhiteSpace(model.SelectedShippingMethodName)
                ? orderTaxAndShippingPrice.ShippingPrices.FirstOrDefault()
                : orderTaxAndShippingPrice.ShippingPrices.FirstOrDefault(x => x.Name == model.SelectedShippingMethodName);

            if (selectedShippingMethod != null)
            {
                cart.ShippingAmount = orderTaxAndShippingPrice.Cart.ShippingAmount = selectedShippingMethod.Price;
                cart.ShippingMethod = orderTaxAndShippingPrice.SelectedShippingMethodName = selectedShippingMethod.Name;
            }

            await _cartRepository.SaveChangesAsync();

            return(Ok(orderTaxAndShippingPrice));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateTaxAndShippingPrices([FromBody] TaxAndShippingPriceRequestVm model)
        {
            var currentUser = await _workContext.GetCurrentUser();

            var cart = await _cartService.GetActiveCart(currentUser.Id);

            var orderTaxAndShippingPrice = await _orderService.UpdateTaxAndShippingPrices(cart.Id, model);

            return(Ok(orderTaxAndShippingPrice));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> UpdateTaxAndShippingPrices(long cartId, [FromBody] TaxAndShippingPriceRequestVm model)
        {
            var currentUser = await _workContext.GetCurrentUser();

            var cart = await _cartRepository.Query().FirstOrDefaultAsync(x => x.Id == cartId);

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

            if (cart.CreatedById != currentUser.Id)
            {
                return(Forbid());
            }

            var orderTaxAndShippingPrice = await _orderService.UpdateTaxAndShippingPrices(cart.Id, model);

            return(Ok(orderTaxAndShippingPrice));
        }
Exemplo n.º 4
0
        public async Task <OrderTaxAndShippingPriceVm> UpdateTaxAndShippingPrices(long cartId, TaxAndShippingPriceRequestVm model)
        {
            var cart = await _cartRepository.Query().FirstOrDefaultAsync(x => x.Id == cartId);

            if (cart == null)
            {
                throw new ApplicationException($"Cart id {cartId} not found");
            }

            Address address;

            if (model.ExistingShippingAddressId != 0)
            {
                address = await _userAddressRepository.Query().Where(x => x.Id == model.ExistingShippingAddressId).Select(x => x.Address).FirstOrDefaultAsync();

                if (address == null)
                {
                    throw new ApplicationException($"Address id {model.ExistingShippingAddressId} not found");
                }
            }
            else
            {
                address = new Address
                {
                    CountryId         = model.NewShippingAddress.CountryId,
                    StateOrProvinceId = model.NewShippingAddress.StateOrProvinceId,
                    DistrictId        = model.NewShippingAddress.DistrictId,
                    ZipCode           = model.NewShippingAddress.ZipCode,
                    AddressLine1      = model.NewShippingAddress.AddressLine1,
                };
            }

            var orderTaxAndShippingPrice = new OrderTaxAndShippingPriceVm
            {
                Cart = await _cartService.GetActiveCartDetails(cart.CustomerId, cart.CreatedById)
            };

            cart.TaxAmount = orderTaxAndShippingPrice.Cart.TaxAmount = await GetTax(cartId, address.CountryId, address.StateOrProvinceId, address.ZipCode);

            var request = new GetShippingPriceRequest
            {
                OrderAmount     = orderTaxAndShippingPrice.Cart.OrderTotal,
                ShippingAddress = address
            };

            orderTaxAndShippingPrice.ShippingPrices = await _shippingPriceService.GetApplicableShippingPrices(request);

            var selectedShippingMethod = string.IsNullOrWhiteSpace(model.SelectedShippingMethodName)
                ? orderTaxAndShippingPrice.ShippingPrices.FirstOrDefault()
                : orderTaxAndShippingPrice.ShippingPrices.FirstOrDefault(x => x.Name == model.SelectedShippingMethodName);

            if (selectedShippingMethod != null)
            {
                cart.ShippingAmount = orderTaxAndShippingPrice.Cart.ShippingAmount = selectedShippingMethod.Price;
                cart.ShippingMethod = orderTaxAndShippingPrice.SelectedShippingMethodName = selectedShippingMethod.Name;
            }

            await _cartRepository.SaveChangesAsync();

            return(orderTaxAndShippingPrice);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> UpdateTaxAndShippingPrices([FromBody] TaxAndShippingPriceRequestVm model)
        {
            //codigo origen
            //  var currentUser = await _workContext.GetCurrentUser();
            //var cart = await _cartService.GetActiveCart(currentUser.Id);
            //var orderTaxAndShippingPrice = await _orderService.UpdateTaxAndShippingPrices(cart.Id, model);

            //obtenemos usuario
            var currentUser = await _workContext.GetCurrentUser();

            //comprobamos si tiene carritos activos, si los tiene, los desactivamos para grabar uno nuevo.
            bool obtenerCarrito = false;
            var  cart           = await _cartService.GetActiveCart(currentUser.Id);

            var carrito = DataCart();

            if (cart == null && carrito != null)
            {
                foreach (CartItem ci in carrito.Items)
                {
                    await _cartService.AddToCart(currentUser.Id, ci.ProductId, ci.Quantity);
                }
            }
            if (cart != null && carrito != null)
            {
                if (cart.Items.Count == carrito.Items.Count)
                {
                    decimal totalcarrito = decimal.Zero;
                    decimal totalcart    = decimal.Zero;
                    foreach (CartItem ci in carrito.Items)
                    {
                        totalcarrito += (ci.Product.Price * ci.Quantity);
                    }
                    foreach (CartItem ci in cart.Items)
                    {
                        var p = RecuperaArtículo(GetIP(), GetSession(), ci.ProductId);
                        totalcart += (decimal.Parse(p.result.pricewithtax.Replace(".", ",")) * ci.Quantity);
                    }
                    if (totalcart != totalcarrito)
                    {
                        cart.IsActive = false;
                        _cartRepository.SaveChanges();
                        //añadimos lineas a un nuevo carrito
                        foreach (CartItem ci in carrito.Items)
                        {
                            await _cartService.AddToCart(currentUser.Id, ci.ProductId, ci.Quantity);
                        }
                        obtenerCarrito = true;
                        //volvemos a obtener el carrito
                    }
                }
                else
                {
                    cart.IsActive = false;
                    _cartRepository.SaveChanges();
                    //añadimos lineas a un nuevo carrito
                    foreach (CartItem ci in carrito.Items)
                    {
                        await _cartService.AddToCart(currentUser.Id, ci.ProductId, ci.Quantity);
                    }
                    obtenerCarrito = true;
                    //volvemos a obtener el carrito
                }
            }

            //obtenemos carrito para confirmar
            if (obtenerCarrito)
            {
                var cart2 = await _cartService.GetActiveCart(currentUser.Id);

                //devuelve precios de envios
                var orderTaxAndShippingPrice = await _orderService.UpdateTaxAndShippingPrices(cart2.Id, model);

                //despues de esto, tenemos que revisar el precio del order, esto nos devuelve los gastos de envio, pero no el total del pedido.
                foreach (CartItem ci in carrito.Items)
                {
                    orderTaxAndShippingPrice.Cart.SubTotal += (ci.Product.Price * ci.Quantity);
                }
                return(Ok(orderTaxAndShippingPrice));
            }
            else
            {
                //devuelve precios de envios
                var orderTaxAndShippingPrice = await _orderService.UpdateTaxAndShippingPrices(cart.Id, model);

                //despues de esto, tenemos que revisar el precio del order, esto nos devuelve los gastos de envio, pero no el total del pedido.
                foreach (CartItem ci in carrito.Items)
                {
                    orderTaxAndShippingPrice.Cart.SubTotal += (ci.Product.Price * ci.Quantity);
                }
                return(Ok(orderTaxAndShippingPrice));
            }
            //cart2.ShippingAmount = orderTaxAndShippingPrice.ShippingPrices[0].Price;
            //await _cartRepository.SaveChangesAsync();
        }
Exemplo n.º 6
0
        public async Task <OrderTaxAndShippingPriceVm> UpdateTaxAndShippingPrices(long cartId, TaxAndShippingPriceRequestVm model)
        {
            var cart = await _cartRepository.Query().FirstOrDefaultAsync(x => x.Id == cartId);

            if (cart == null)
            {
                throw new ApplicationException($"Cart id {cartId} not found");
            }

            Address address;

            if (model.ExistingShippingAddressId != 0)
            {
                address = await _userAddressRepository.Query().Where(x => x.Id == model.ExistingShippingAddressId).Select(x => x.Address).FirstOrDefaultAsync();

                if (address == null)
                {
                    throw new ApplicationException($"Address id {model.ExistingShippingAddressId} not found");
                }
            }
            else
            {
                address = new Address
                {
                    CountryId         = model.NewShippingAddress.CountryId,
                    StateOrProvinceId = model.NewShippingAddress.StateOrProvinceId,
                    DistrictId        = model.NewShippingAddress.DistrictId,
                    ZipCode           = model.NewShippingAddress.ZipCode,
                    AddressLine1      = model.NewShippingAddress.AddressLine1,
                };
            }

            var orderTaxAndShippingPrice = new OrderTaxAndShippingPriceVm
            {
                Cart = await _cartService.GetActiveCartDetails(cart.CustomerId, cart.CreatedById)
            };

            await _cartRepository.SaveChangesAsync();

            return(orderTaxAndShippingPrice);
        }