From 8a6aa50f797f31705979944e110b1b39984fdc73 Mon Sep 17 00:00:00 2001 From: Herculino Trotta Date: Sun, 17 Nov 2024 01:23:55 -0300 Subject: [PATCH] feat: add function for calculating daily_spending_allowance --- app/apps/monthly_overview/utils/__init__.py | 0 .../utils/daily_spending_allowance.py | 65 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 app/apps/monthly_overview/utils/__init__.py create mode 100644 app/apps/monthly_overview/utils/daily_spending_allowance.py diff --git a/app/apps/monthly_overview/utils/__init__.py b/app/apps/monthly_overview/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/apps/monthly_overview/utils/daily_spending_allowance.py b/app/apps/monthly_overview/utils/daily_spending_allowance.py new file mode 100644 index 0000000..1f02bfd --- /dev/null +++ b/app/apps/monthly_overview/utils/daily_spending_allowance.py @@ -0,0 +1,65 @@ +from decimal import Decimal +from django.utils import timezone +from calendar import monthrange + +from apps.common.functions.dates import remaining_days_in_month + + +def calculate_daily_allowance_currency(currency_totals, month=None, year=None): + """ + Calculate daily spending allowance based on currency totals. + + Args: + currency_totals (dict): Result from calculate_currency_totals function + month (int, optional): Month to calculate for. Defaults to current month. + year (int, optional): Year to calculate for. Defaults to current year. + + Returns: + dict: Daily allowance per currency, or empty dict if not current month + """ + print(currency_totals) + # If month/year not provided, use current date + current_date = timezone.localdate(timezone.now()) + if month is None: + month = current_date.month + if year is None: + year = current_date.year + + # Only calculate if we're looking at the current month + if current_date.month != month or current_date.year != year: + return {} + + # Calculate remaining days in month + _, days_in_month = monthrange(year, month) + remaining_days = remaining_days_in_month( + current_date=current_date, month=month, year=year + ) + + # Calculate daily allowance for each currency + result = {} + + for currency_id, data in currency_totals.items(): + # Get the total_final value, skip if not present + total_final = data.get("total_final") + if total_final is None: + continue + + # Calculate daily allowance + daily_amount = ( + total_final / remaining_days if remaining_days > 0 else Decimal("0") + ) + + # Only include positive amounts + if daily_amount > 0: + result[currency_id] = {"currency": data["currency"], "amount": daily_amount} + + # Include exchanged amount if available + if "exchanged" in data and "total_final" in data["exchanged"]: + exchanged_daily = data["exchanged"]["total_final"] / remaining_days + if exchanged_daily > 0: + result[currency_id]["exchanged"] = { + "currency": data["exchanged"]["currency"], + "amount": exchanged_daily, + } + + return result