From e854e5a7fe94fe4cf1a253f03f19585711ffb2c5 Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Thu, 25 Sep 2025 15:44:45 +0500 Subject: [PATCH] add: add filter for income-expence list api --- core/apps/finance/filters/expence.py | 37 ++++++++++++++++++++++++++-- core/apps/finance/filters/income.py | 37 ++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/core/apps/finance/filters/expence.py b/core/apps/finance/filters/expence.py index 780461b..5c68f99 100644 --- a/core/apps/finance/filters/expence.py +++ b/core/apps/finance/filters/expence.py @@ -1,11 +1,44 @@ +from calendar import monthrange + +from django.utils.timezone import now, timedelta + import django_filters from core.apps.finance.models import Expence class ExpenceFilter(django_filters.FilterSet): + date = django_filters.CharFilter(method='filter_by_created_at') + class Meta: model = Expence fields = [ - 'payment_type', 'project_folder', 'project', 'user', 'expence_type' - ] \ No newline at end of file + 'payment_type', 'project_folder', 'project', 'user', 'expence_type', 'cash_transaction', 'date' + ] + + def filter_by_created_at(self, queryset, name, value): + today = now().date() + + if value == 'today': + return queryset.filter(created_at__date=today) + elif value == 'yesterday': + yesterday = today - timedelta(days=1) + return queryset.filter(created_at__date=yesterday) + elif value == 'last_week': + start_date = today - timedelta(days=today.weekday() + 7) + end_date = start_date + timedelta(days=6) + return queryset.filter(created_at__range=(start_date, end_date)) + elif value == 'last_month': + if today.month == 1: + last_month_year = today.year - 1 + last_month = 12 + else: + last_month_year = today.year + last_month = today.month - 1 + + start_last_month = today.replace(year=last_month_year, month=last_month, day=1) + days_in_last_month = monthrange(last_month_year, last_month)[1] + end_last_month = start_last_month.replace(day=days_in_last_month) + return queryset.filter(created_at__range=(start_last_month, end_last_month)) + else: + return queryset \ No newline at end of file diff --git a/core/apps/finance/filters/income.py b/core/apps/finance/filters/income.py index eee07ac..6adf4ce 100644 --- a/core/apps/finance/filters/income.py +++ b/core/apps/finance/filters/income.py @@ -1,11 +1,44 @@ +from calendar import monthrange + +from django.utils.timezone import timedelta, now + import django_filters from core.apps.finance.models import Income class IncomeFilter(django_filters.FilterSet): + date = django_filters.CharFilter(method='filter_by_created_at') + class Meta: model = Income fields = [ - 'payment_type', 'project_folder', 'project', 'user', 'type_income' - ] \ No newline at end of file + 'payment_type', 'project_folder', 'project', 'user', 'type_income', 'date', 'cash_transaction' + ] + + def filter_by_created_at(self, queryset, name, value): + today = now().date() + + if value == 'today': + return queryset.filter(created_at__date=today) + elif value == 'yesterday': + yesterday = today - timedelta(days=1) + return queryset.filter(created_at__date=yesterday) + elif value == 'last_week': + start_date = today - timedelta(days=today.weekday() + 7) + end_date = start_date + timedelta(days=6) + return queryset.filter(created_at__range=(start_date, end_date)) + elif value == 'last_month': + if today.month == 1: + last_month_year = today.year - 1 + last_month = 12 + else: + last_month_year = today.year + last_month = today.month - 1 + + start_last_month = today.replace(year=last_month_year, month=last_month, day=1) + days_in_last_month = monthrange(last_month_year, last_month)[1] + end_last_month = start_last_month.replace(day=days_in_last_month) + return queryset.filter(created_at__range=(start_last_month, end_last_month)) + else: + return queryset \ No newline at end of file