public async Task <IActionResult> AddTax(TaxViewModel input)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);

                if (user == null)
                {
                    throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
                }

                await _userTaxRepository.AddUserTax(new UserTax
                {
                    UserId            = user.Id,
                    CharityPaidAmount = input.CharityPaidAmount.Value,
                    TotalIncome       = input.TotalIncome.Value,
                    NumberOfChildren  = input.NumberOfChildren.Value,
                    Year         = input.Year.Value,
                    TaxDueAmount = _taxService.CalculateTax(input.Year.Value, input.NumberOfChildren.Value, input.CharityPaidAmount.Value, input.TotalIncome.Value)
                });

                _logger.LogInformation("New Tax Created");

                return(LocalRedirect($"/Tax/getTax?year={input.Year}"));
            }

            return(View(input));
        }
 public void GetTax_EmptyCart_ShouldReturnZero()
 {
     taxService.CalculateTax(0).Returns(0m);
     sut.GetTotalTax().ShouldBe(0m);
     taxService.Received(1).CalculateTax(0);
 }
Example #3
0
        public ActionResult Create(OrderEditViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(JsonValidationError());
            }

            var user = customerService.FindAll().FirstOrDefault(u => u.Email == model.UserEmail);

            if (user == null)
            {
                var userModel = new CustomerViewModel
                {
                    FirstName   = model.BillingAddress.FirstName,
                    LastName    = model.BillingAddress.LastName,
                    Company     = model.BillingAddress.Company,
                    PhoneNumber = model.BillingAddress.Phone,
                    Email       = model.UserEmail
                };
                try
                {
                    user = customerService.AddOrUpdate(userModel);
                }
                catch (ArgumentException err)
                {
                    return(JsonError(err.Message));
                }
            }

            // Get addresses
            var billingAddress = Mapper.Map <Address>(model.BillingAddress);

            billingAddress.Type = AddressType.Billing;

            var shippingAddress = Mapper.Map <Address>(model.SameShippingAddress
                ? model.BillingAddress
                : model.ShippingAddress);

            shippingAddress.Type = AddressType.Shipping;

            var defaultBillingAddress = customerService.GetAddress(user.Id, AddressType.Billing);

            if (defaultBillingAddress == null || defaultBillingAddress.FirstName == null)
            {
                // Add default billing address
                defaultBillingAddress           = Mapper.Map <Address>(model.BillingAddress);
                defaultBillingAddress.Type      = AddressType.Billing;
                defaultBillingAddress.IsPrimary = true;
                if (defaultBillingAddress.Id == 0)
                {
                    user.Addresses.Add(defaultBillingAddress);
                }
            }

            var defaultShippingAddress = customerService.GetAddress(user.Id, AddressType.Shipping);

            if (defaultShippingAddress == null || defaultBillingAddress.FirstName == null)
            {
                // Add default shipping address
                defaultShippingAddress = Mapper.Map <Address>(model.SameShippingAddress
                    ? model.BillingAddress
                    : model.ShippingAddress);
                defaultShippingAddress.Type      = AddressType.Shipping;
                defaultShippingAddress.IsPrimary = true;
                if (defaultShippingAddress.Id == 0)
                {
                    user.Addresses.Add(defaultShippingAddress);
                }
            }

            db.SaveChanges();

            // Create order
            var order = new Order
            {
                UserId          = user.Id,
                BillingAddress  = billingAddress,
                ShippingAddress = shippingAddress,
                DatePlaced      = DateTime.Now,
                DateUpdated     = DateTime.Now,
                IPAddress       = Request.UserHostAddress,
                UserComments    = model.UserComments,
                Status          = OrderStatus.AwaitingPayment,
                Discount        = model.Discount,
                ShippingAmount  = model.ShippingAmount,
            };

            db.Orders.Add(order);

            var itemDiscount = 0m;

            if (model.Discount < 0)
            {
                itemDiscount = model.Discount / model.Items.Count;
            }

            foreach (var item in model.Items)
            {
                Product product = productFinder.Find(item.ProductId);

                var orderItem = new OrderItem
                {
                    Order        = order,
                    ProductId    = item.ProductId,
                    ProductSkuId = item.ProductSkuId,
                    Quantity     = item.Quantity,
                    Options      = item.Options,
                    ItemPrice    = item.ItemPrice
                };

                db.OrderItems.Add(orderItem);

                order.Subtotal += orderItem.Quantity * orderItem.ItemPrice;

                order.TaxAmount += taxService.CalculateTax(billingAddress.CountryCode, billingAddress.RegionId,
                                                           product.TaxClassId, (orderItem.ItemPrice + itemDiscount) * orderItem.Quantity);
            }

            order.Total = order.Subtotal + order.Discount + order.ShippingAmount;
            if (!settingService.Get <bool>(SettingField.TaxIncludedInPrices))
            {
                order.Total += order.TaxAmount;
            }

            db.SaveChanges();

            return(JsonSuccess(new { orderId = order.Id })
                   .WithSuccess("Order created successfully".TA()));
        }
Example #4
0
 public decimal GetTotalTax()
 {
     return(_taxService.CalculateTax(GetTotal()));
 }