add: add filter for income-expence list api

This commit is contained in:
behruz-dev
2025-09-25 15:44:45 +05:00
parent f8a423f65a
commit e854e5a7fe
2 changed files with 70 additions and 4 deletions

View File

@@ -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'
]
'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

View File

@@ -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'
]
'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