Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mhieta committed Jan 9, 2024
2 parents 813c395 + 8ade745 commit d92d12b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 93 deletions.
19 changes: 18 additions & 1 deletion parking_permits/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from parking_permits.exceptions import CreateTalpaProductError, ProductCatalogError

from ..utils import diff_months_ceil, find_next_date
from ..utils import diff_months_ceil, find_next_date, round_up
from .mixins import TimestampedModelMixin, UserStampedModelMixin
from .parking_zone import ParkingZone

Expand Down Expand Up @@ -177,6 +177,23 @@ def get_modified_unit_price(self, is_low_emission, is_secondary):
price += price * self.secondary_vehicle_increase_rate
return price

def get_talpa_pricing(self, is_low_emission, is_secondary):
"""Returns dict of the following price values for Talpa processing e.g.:
{
"price_gross": "20.00",
"price_net": "16.13",
"price_vat": "3.87",
}
"""
price_gross = self.get_modified_unit_price(is_low_emission, is_secondary)
price_net = price_gross / Decimal(1 + (self.vat or 0))

return {
"price_gross": round_up(price_gross),
"price_net": round_up(price_net),
"price_vat": round_up(price_gross - price_net),
}

def get_merchant_id(self):
headers = {
"api-key": settings.TALPA_API_KEY,
Expand Down
42 changes: 21 additions & 21 deletions parking_permits/tests/models/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,30 @@ def test_create_for_customer_should_create_order_with_items(self):
self.assertEqual(order_items[1].unit_price, Decimal(50))
self.assertEqual(order_items[1].quantity, 2)

@freeze_time("2023-03-15")
def test_create_for_permits_should_create_order_items_with_start_end_date_for_open_ended_permits(
self,
):
start_time = timezone.now()
permit = ParkingPermitFactory(
parking_zone=self.zone,
customer=self.customer,
contract_type=ContractType.OPEN_ENDED,
status=ParkingPermitStatus.DRAFT,
start_time=start_time,
month_count=6,
)
order = Order.objects.create_for_permits([permit])
order_items = order.order_items.all().order_by("-quantity")
self.assertEqual(order_items.count(), 1)
order_item = order_items[0]
self.assertEqual(
order_item.start_time.date(), timezone.localtime(start_time).date()
)
self.assertEqual(
order_item.end_time.date(),
get_end_time(start_time, 1).date(),
)
with freeze_time(date(CURRENT_YEAR, 3, 5)):
start_time = timezone.now()
permit = ParkingPermitFactory(
parking_zone=self.zone,
customer=self.customer,
contract_type=ContractType.OPEN_ENDED,
status=ParkingPermitStatus.DRAFT,
start_time=start_time,
month_count=6,
)
order = Order.objects.create_for_permits([permit])
order_items = order.order_items.all().order_by("-quantity")
self.assertEqual(order_items.count(), 1)
order_item = order_items[0]
self.assertEqual(
order_item.start_time.date(), timezone.localtime(start_time).date()
)
self.assertEqual(
order_item.end_time.date(),
get_end_time(start_time, 1).date(),
)

def test_create_renewable_order_should_create_renewal_order(self):
start_time = timezone.make_aware(datetime(CURRENT_YEAR, 3, 15))
Expand Down
25 changes: 25 additions & 0 deletions parking_permits/tests/models/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,28 @@ def test_get_modified_unit_price_return_modified_price(self):
True, True
)
self.assertEqual(secondary_vehicle_low_emission_price, Decimal(7.5))

def test_get_talpa_pricing(self):
product = ProductFactory(
unit_price=Decimal(20),
vat=0.24,
)
pricing = product.get_talpa_pricing(False, False)
assert pricing == {
"price_gross": "20.00",
"price_net": "16.13",
"price_vat": "3.87",
}

def test_get_talpa_pricing_with_discount(self):
product = ProductFactory(
unit_price=Decimal(20),
low_emission_discount=Decimal(0.5),
vat=0.24,
)
pricing = product.get_talpa_pricing(True, False)
assert pricing == {
"price_gross": "10.00",
"price_net": "8.06",
"price_vat": "1.94",
}
85 changes: 23 additions & 62 deletions parking_permits/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

from ..models import Customer
from ..models.common import SourceSystem
from ..utils import round_up
from .keys import rsa_key


Expand Down Expand Up @@ -180,10 +179,16 @@ class BaseResolveEndpointTestCase(APITestCase):
permit_id = "80000001"

def prepare_test_data(
self, permit_id, unit_price, low_emission_discount, primary_permit=True
self,
permit_id,
unit_price,
low_emission_discount,
primary_permit=True,
):
start_date = datetime.date(2023, 1, 1)
end_date = datetime.date(2023, 12, 31)
now = datetime.date.today()
start_date = datetime.date(now.year, 1, 1)
end_date = datetime.date(now.year, 12, 31)

vehicle = VehicleFactory(
power_type=VehiclePowerTypeFactory(identifier="01", name="Bensin"),
emission=45,
Expand All @@ -198,10 +203,10 @@ def prepare_test_data(
euro_min_class_limit=6,
)
permit_start_time = datetime.datetime(
2023, 9, 12, 13, 46, 0, tzinfo=datetime.timezone.utc
now.year, 9, 12, 13, 46, 0, tzinfo=datetime.timezone.utc
)
permit_end_time = datetime.datetime(
2023, 10, 11, 23, 59, 0, tzinfo=datetime.timezone.utc
now.year, 10, 11, 23, 59, 0, tzinfo=datetime.timezone.utc
)
zone_a = ParkingZoneFactory(name="A")
product_detail_list = [[(start_date, end_date), unit_price]]
Expand Down Expand Up @@ -325,17 +330,13 @@ def test_resolve_price_view_for_normal_emission_vehicle(self):
)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 200)
vat = product.vat
price_vat = unit_price * vat
self.assertEqual(
response.data.get("subscriptionId"), self.talpa_subscription_id
)
self.assertEqual(response.data.get("userId"), self.user_id)
self.assertEqual(
response.data.get("priceNet"), round_up(float(unit_price - price_vat))
)
self.assertEqual(response.data.get("priceVat"), round_up(float(price_vat)))
self.assertEqual(response.data.get("priceGross"), round_up(float(unit_price)))
self.assertEqual(response.data.get("priceNet"), "48.39")
self.assertEqual(response.data.get("priceVat"), "11.61")
self.assertEqual(response.data.get("priceGross"), "60.00")

def test_resolve_price_view_for_low_emission_vehicle(self):
unit_price = Decimal(60)
Expand All @@ -353,28 +354,15 @@ def test_resolve_price_view_for_low_emission_vehicle(self):
)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 200)
vat = product.vat
low_emission_price = (
unit_price - unit_price * low_emission_discount
) # discount price
low_emission_price_vat = low_emission_price * vat
self.assertEqual(
response.data.get("subscriptionId"), self.talpa_subscription_id
)
self.assertEqual(response.data.get("userId"), self.user_id)
self.assertEqual(
response.data.get("priceGross"), round_up(float(low_emission_price))
)
self.assertEqual(
response.data.get("priceVat"), round_up(float(low_emission_price_vat))
)
self.assertEqual(
response.data.get("priceNet"),
round_up(float(low_emission_price - low_emission_price_vat)),
)
self.assertEqual(response.data.get("priceGross"), "45.00")
self.assertEqual(response.data.get("priceVat"), "8.71")
self.assertEqual(response.data.get("priceNet"), "36.29")

def test_resolve_price_view_for_secondary_normal_emission_vehicle(self):
secondary_vehicle_increase_rate = Decimal(0.5)
unit_price = Decimal(60)
low_emission_discount = Decimal(0)
permit, product = self.prepare_test_data(
Expand All @@ -390,28 +378,15 @@ def test_resolve_price_view_for_secondary_normal_emission_vehicle(self):
)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 200)
vat = product.vat
secondary_vehicle_price = (
unit_price + unit_price * secondary_vehicle_increase_rate
) # secondary vehicle price
secondary_vehicle_price_vat = secondary_vehicle_price * vat
self.assertEqual(
response.data.get("subscriptionId"), self.talpa_subscription_id
)
self.assertEqual(response.data.get("userId"), self.user_id)
self.assertEqual(
response.data.get("priceGross"), round_up(float(secondary_vehicle_price))
)
self.assertEqual(
response.data.get("priceVat"), round_up(float(secondary_vehicle_price_vat))
)
self.assertEqual(
response.data.get("priceNet"),
round_up(float(secondary_vehicle_price - secondary_vehicle_price_vat)),
)
self.assertEqual(response.data.get("priceGross"), "90.00")
self.assertEqual(response.data.get("priceVat"), "17.42")
self.assertEqual(response.data.get("priceNet"), "72.58")

def test_resolve_price_view_for_secondary_low_emission_vehicle(self):
secondary_vehicle_increase_rate = Decimal(0.5)
unit_price = Decimal(60)
low_emission_discount = Decimal(0.25)
permit, product = self.prepare_test_data(
Expand All @@ -427,27 +402,13 @@ def test_resolve_price_view_for_secondary_low_emission_vehicle(self):
)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 200)
vat = product.vat

modified_price = unit_price
modified_price -= modified_price * low_emission_discount
modified_price += modified_price * secondary_vehicle_increase_rate
modified_price_vat = modified_price * vat

self.assertEqual(
response.data.get("subscriptionId"), self.talpa_subscription_id
)
self.assertEqual(response.data.get("userId"), self.user_id)
self.assertEqual(
response.data.get("priceGross"), round_up(float(modified_price))
)
self.assertEqual(
response.data.get("priceVat"), round_up(float(modified_price_vat))
)
self.assertEqual(
response.data.get("priceNet"),
round_up(float(modified_price - modified_price_vat)),
)
self.assertEqual(response.data.get("priceGross"), "67.50")
self.assertEqual(response.data.get("priceVat"), "13.06")
self.assertEqual(response.data.get("priceNet"), "54.44")

def test_resolve_price_view_should_return_error_if_permit_products_missing(
self,
Expand Down
13 changes: 4 additions & 9 deletions parking_permits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
get_meta_item,
get_meta_value,
get_user_from_api_view_method_args,
round_up,
snake_to_camel_dict,
)

Expand Down Expand Up @@ -297,18 +296,14 @@ def post(self, request, format=None):
product = product_with_quantity[0]
if not product:
return bad_request_response("Product not found")
price = product.get_modified_unit_price(
permit.vehicle.is_low_emission, permit.is_secondary_vehicle
)
vat = product.vat
price_vat = price * vat
response = snake_to_camel_dict(
{
"subscription_id": subscription_id,
"user_id": user_id,
"price_net": round_up(float(price - price_vat)),
"price_vat": round_up(float(price_vat)),
"price_gross": round_up(float(price)),
**product.get_talpa_pricing(
permit.vehicle.is_low_emission,
permit.is_secondary_vehicle,
),
}
)
except Exception as e:
Expand Down

0 comments on commit d92d12b

Please sign in to comment.