add: add filter for income-expence list api
This commit is contained in:
@@ -1,11 +1,44 @@
|
|||||||
|
from calendar import monthrange
|
||||||
|
|
||||||
|
from django.utils.timezone import now, timedelta
|
||||||
|
|
||||||
import django_filters
|
import django_filters
|
||||||
|
|
||||||
from core.apps.finance.models import Expence
|
from core.apps.finance.models import Expence
|
||||||
|
|
||||||
|
|
||||||
class ExpenceFilter(django_filters.FilterSet):
|
class ExpenceFilter(django_filters.FilterSet):
|
||||||
|
date = django_filters.CharFilter(method='filter_by_created_at')
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Expence
|
model = Expence
|
||||||
fields = [
|
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
|
||||||
@@ -1,11 +1,44 @@
|
|||||||
|
from calendar import monthrange
|
||||||
|
|
||||||
|
from django.utils.timezone import timedelta, now
|
||||||
|
|
||||||
import django_filters
|
import django_filters
|
||||||
|
|
||||||
from core.apps.finance.models import Income
|
from core.apps.finance.models import Income
|
||||||
|
|
||||||
|
|
||||||
class IncomeFilter(django_filters.FilterSet):
|
class IncomeFilter(django_filters.FilterSet):
|
||||||
|
date = django_filters.CharFilter(method='filter_by_created_at')
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Income
|
model = Income
|
||||||
fields = [
|
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
|
||||||
Reference in New Issue
Block a user