From e3c2d4450f4d96e013757cdd825f9eede02b213a Mon Sep 17 00:00:00 2001 From: i701 Date: Sun, 27 Jul 2025 15:17:47 +0500 Subject: [PATCH] =?UTF-8?q?feat(billing):=20add=20calculate=5Ftotal=5Fnew?= =?UTF-8?q?=5Fprice=20utility=20for=20dynamic=20payment=20calculation=20?= =?UTF-8?q?=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- billing/utils.py | 33 +++++++++++++++++++++++++++++++++ billing/views.py | 6 ++++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 billing/utils.py diff --git a/billing/utils.py b/billing/utils.py new file mode 100644 index 0000000..321667f --- /dev/null +++ b/billing/utils.py @@ -0,0 +1,33 @@ +def calculate_total_new_price(number_of_devices, number_of_months): + monthly_price_map = { + 1: 100, + 2: 175, + 3: 250, + 4: 325, + 5: 400, + 6: 475, + 7: 550, + 8: 625, + 9: 700, + 10: 775, + 11: 850, + 12: 925, + 13: 1000, + 14: 1075, + 15: 1150, + 16: 1225, + 17: 1300, + } + + if number_of_devices < 1 or number_of_devices > 17: + raise ValueError("Number of devices must be between 1 and 17.") + + monthly_price = monthly_price_map[number_of_devices] + total_price = monthly_price * number_of_months + print(f"Monthly price for {number_of_devices} devices: {monthly_price}") + + print(f"Total price for {number_of_months} months: {total_price}") + return total_price + + +calculate_total_new_price(number_of_devices=2, number_of_months=3) diff --git a/billing/views.py b/billing/views.py index 1b129b6..2a75ab7 100644 --- a/billing/views.py +++ b/billing/views.py @@ -15,6 +15,7 @@ from api.tasks import add_new_devices_to_omada from apibase.env import BASE_DIR, env from django.db.models import Prefetch import logging +from .utils import calculate_total_new_price from .models import Device, Payment, Topup, WalletTransaction from .serializers import ( @@ -103,8 +104,9 @@ class ListCreatePaymentView(StaffEditorPermissionMixin, generics.ListCreateAPIVi {"message": "device_ids are required."}, status=status.HTTP_400_BAD_REQUEST, ) - # Create payment - amount = 100 + amount = calculate_total_new_price( + number_of_devices=number_of_devices, number_of_months=number_of_months + ) payment = Payment.objects.create( amount=amount, number_of_months=number_of_months,