From 0ffdfad07f425e48d7333593fe98ca5a3b409398 Mon Sep 17 00:00:00 2001 From: James Kachel Date: Mon, 2 Oct 2023 01:31:18 -0500 Subject: [PATCH] Fixing formatting errors on receipt page, should not charge tax if TaxRate (#2775) * Fixing formatting errors on receipt page, should not charge tax if TaxRate is inactive * missed one raw int(0) * Updating serializer for basket to respect taxrate active flag --- ecommerce/api.py | 4 +++- ecommerce/mail_api_test.py | 2 +- ecommerce/serializers.py | 15 +++++++++++---- ecommerce/serializers_test.py | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/ecommerce/api.py b/ecommerce/api.py index 7f9f9add3..f730055d6 100644 --- a/ecommerce/api.py +++ b/ecommerce/api.py @@ -859,7 +859,9 @@ def create_unfulfilled_order(validated_basket, affiliate_id=None, **kwargs): ) try: - tax_rate_info = TaxRate.objects.get(country_code=country_code) + tax_rate_info = TaxRate.objects.filter(active=True).get( + country_code=country_code + ) except (TaxRate.DoesNotExist, TaxRate.MultipleObjectsReturned): # not using get_or_create here because we don't want the rate to stick around tax_rate_info = TaxRate() diff --git a/ecommerce/mail_api_test.py b/ecommerce/mail_api_test.py index 41e800d15..cecfac92e 100644 --- a/ecommerce/mail_api_test.py +++ b/ecommerce/mail_api_test.py @@ -262,7 +262,7 @@ def test_send_ecommerce_order_receipt(mocker, receipt_data): { "quantity": 1, "total_paid": "100.00", - "tax_paid": 0, + "tax_paid": "0.00", "discount": "0.00", "price": "100.00", "readable_id": get_readable_id( diff --git a/ecommerce/serializers.py b/ecommerce/serializers.py index 0603950c8..f54d5a905 100644 --- a/ecommerce/serializers.py +++ b/ecommerce/serializers.py @@ -1,6 +1,7 @@ """ ecommerce serializers """ # pylint: disable=too-many-lines import logging +from decimal import Decimal from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.core.validators import MaxValueValidator, MinValueValidator @@ -371,7 +372,11 @@ def get_tax_info(self, _): if request and hasattr(request, "user"): country_code = determine_visitor_country(request) if country_code is not None: - return TaxRate.objects.get(country_code=country_code).to_dict() + return ( + TaxRate.objects.filter(active=True) + .get(country_code=country_code) + .to_dict() + ) else: log.error("No request object in get_tax_info") except TaxRate.DoesNotExist: @@ -975,9 +980,11 @@ def get_lines(self, instance): product_version=line.product_version, tax_rate=instance.tax_rate, ) - tax_paid = product_price_and_tax["tax_assessed"] * line.quantity + tax_paid = Decimal( + product_price_and_tax["tax_assessed"] * line.quantity + ).quantize(Decimal(".01")) total_price = product_price_and_tax["price"] * line.quantity - total_paid = total_price + tax_paid + total_paid = Decimal(total_price + tax_paid).quantize(Decimal(".01")) discount = (line.product_version.price * line.quantity) - total_price dates = CourseRunEnrollment.objects.filter( @@ -1016,7 +1023,7 @@ def get_lines(self, instance): dict( quantity=line.quantity, total_paid=str(total_paid), - tax_paid=tax_paid, + tax_paid=str(tax_paid), discount=str(discount), CEUs=str(CEUs) if CEUs else None, **BaseProductVersionSerializer(line.product_version).data, diff --git a/ecommerce/serializers_test.py b/ecommerce/serializers_test.py index df0ca2eff..47a56be24 100644 --- a/ecommerce/serializers_test.py +++ b/ecommerce/serializers_test.py @@ -459,7 +459,7 @@ def test_serialize_order_receipt(receipt_data): "end_date": None, "price": str(product_version.price), "total_paid": str(line.quantity * product_version.price), - "tax_paid": 0, + "tax_paid": "0.00", "quantity": line.quantity, "CEUs": product_version.product.content_object.course.page.certificate_page.CEUs, }