add filter to party
This commit is contained in:
@@ -1,32 +1,31 @@
|
||||
from calendar import monthrange
|
||||
|
||||
from django.utils.timezone import timedelta, now
|
||||
|
||||
import django_filters
|
||||
|
||||
from core.apps.orders.models import Party
|
||||
|
||||
|
||||
class PartyFilter(django_filters.FilterSet):
|
||||
DATE_CHOICES = (
|
||||
('today', 'bugun'),
|
||||
('last_week', 'oxirgi hafta'),
|
||||
('last_month', 'oxirgi oy'),
|
||||
('last_year', 'oirgi yil'),
|
||||
|
||||
('today', 'Bugun'),
|
||||
('last_week', 'Oxirgi hafta'),
|
||||
('last_month', 'Oxirgi oy'),
|
||||
('last_year', 'Oxirgi yil'),
|
||||
)
|
||||
|
||||
# delivery date filters
|
||||
delivery_date = django_filters.ChoiceFilter(
|
||||
choices=DATE_CHOICES, method='filter_by_deliveyer_date'
|
||||
choices=DATE_CHOICES, method='filter_by_delivery_date'
|
||||
)
|
||||
delivery_start_date = django_filters.DateFilter(field_name="delivery_date", lookup_expr="gte")
|
||||
delivery_end_date = django_filters.DateFilter(field_name="delivery_date", lookup_expr="lte")
|
||||
|
||||
# payment date filters
|
||||
payment_date = django_filters.ChoiceFilter(
|
||||
choices=DATE_CHOICES, method='filter_by_payment_date'
|
||||
)
|
||||
payment_start_date = django_filters.DateFilter(field_name='payment_date', lookup_expr='gte')
|
||||
payment_end_date = django_filters.DateFilter(field_name='payment_date', lookup_expr='lte')
|
||||
|
||||
# order date filters
|
||||
order_date = django_filters.ChoiceFilter(
|
||||
choices=DATE_CHOICES, method='filter_by_order_date'
|
||||
@@ -37,38 +36,40 @@ class PartyFilter(django_filters.FilterSet):
|
||||
class Meta:
|
||||
model = Party
|
||||
fields = [
|
||||
'status', 'payment_status'
|
||||
'status', 'payment_status', 'confirmation',
|
||||
'orders__wherehouse', 'orders__project', 'orders__project_folder',
|
||||
'mediator', 'orders__counterparty',
|
||||
]
|
||||
|
||||
def filter_by_delivery_date(self, queryset, name, value):
|
||||
today = now().date()
|
||||
|
||||
|
||||
if value == 'today':
|
||||
return queryset.filter(delivery_date=today)
|
||||
|
||||
|
||||
elif value == 'last_week':
|
||||
start_date = today - timedelta(days=today.weekday() + 7)
|
||||
end_date = start_date + timedelta(days=6)
|
||||
return queryset.filter(delivery_date__range=(start_date, end_date))
|
||||
|
||||
elif value == 'last_year':
|
||||
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.moth - 1
|
||||
|
||||
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(delivery_date__range=(start_last_month, end_last_month))
|
||||
|
||||
elif value == 'last_year':
|
||||
start_year = today.replace(year=today.year - 1, month=1, day=1)
|
||||
end_year = today.replace(year=today.year - 1, month=12, day=31)
|
||||
return queryset.filter(delivery_date__range=(start_year, end_year))
|
||||
|
||||
return queryset
|
||||
|
||||
def filter_by_payment_date(self, queryset, name, value):
|
||||
@@ -76,53 +77,59 @@ class PartyFilter(django_filters.FilterSet):
|
||||
|
||||
if value == 'today':
|
||||
return queryset.filter(payment_date=today)
|
||||
|
||||
elif value == 'last_week':
|
||||
start_date = today - timedelta(days=today.weekday() + 7)
|
||||
end_date = start_date + timedelta(days=6)
|
||||
return queryset.filter(payment_date__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.moth - 1
|
||||
|
||||
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(payment_date__range=(start_last_month, end_last_month))
|
||||
|
||||
elif value == 'last_year':
|
||||
start_year = today.replace(year=today.year - 1, month=1, day=1)
|
||||
end_year = today.replace(year=today.year - 1, month=12, day=31)
|
||||
return queryset.filter(payment_date__range=(start_year, end_year))
|
||||
|
||||
return queryset
|
||||
|
||||
|
||||
def filter_by_order_date(self, queryset, name, value):
|
||||
today = now().date()
|
||||
|
||||
if value == 'today':
|
||||
return queryset.filter(order_date=today)
|
||||
|
||||
elif value == 'last_week':
|
||||
start_date = today - timedelta(days=today.weekday() + 7)
|
||||
end_date = start_date + timedelta(days=6)
|
||||
return queryset.filter(order_date__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.moth - 1
|
||||
|
||||
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(order_date__range=(start_last_month, end_last_month))
|
||||
|
||||
elif value == 'last_year':
|
||||
start_year = today.replace(year=today.year - 1, month=1, day=1)
|
||||
end_year = today.replace(year=today.year - 1, month=12, day=31)
|
||||
return queryset.filter(order_date__range=(start_year, end_year))
|
||||
|
||||
return queryset
|
||||
|
||||
18
core/apps/orders/migrations/0017_alter_party_confirmation.py
Normal file
18
core/apps/orders/migrations/0017_alter_party_confirmation.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.2.4 on 2025-08-22 11:19
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('orders', '0016_remove_party_qqs_order_qqs'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='party',
|
||||
name='confirmation',
|
||||
field=models.CharField(choices=[('EXPECTED', 'kutilmoqda'), ('APPROVER', 'tasdiqlangan'), ('REJECTED', 'rad etilgan')], default='EXPECTED', max_length=20),
|
||||
),
|
||||
]
|
||||
@@ -21,6 +21,11 @@ class Party(BaseModel):
|
||||
('NOT_PAID', "to'lanmagan"),
|
||||
('OVERPAID', "ortiqcha to'langan"),
|
||||
)
|
||||
CONFIRMATION = (
|
||||
('EXPECTED', 'kutilmoqda'),
|
||||
('APPROVER', 'tasdiqlangan'),
|
||||
('REJECTED', 'rad etilgan'),
|
||||
)
|
||||
|
||||
number = models.PositiveIntegerField(default=1)
|
||||
orders = models.ManyToManyField(Order, related_name='parties', null=True, blank=True)
|
||||
@@ -33,7 +38,7 @@ class Party(BaseModel):
|
||||
# choices
|
||||
status = models.CharField(max_length=20, choices=STATUS, default='NEW')
|
||||
payment_status = models.CharField(max_length=20, choices=PAYMENT_STATUS, default='NOT_PAID')
|
||||
confirmation = models.BooleanField(default=False)
|
||||
confirmation = models.CharField(max_length=20, choices=CONFIRMATION, default='EXPECTED')
|
||||
# percentages
|
||||
payment_percentage = models.FloatField(null=True, blank=True)
|
||||
process = models.FloatField(null=True, blank=True)
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
from rest_framework import generics, views
|
||||
from rest_framework.response import Response
|
||||
|
||||
from django_filters.rest_framework.backends import DjangoFilterBackend
|
||||
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
from core.apps.orders.serializers import party as serializers
|
||||
from core.apps.orders.models import Order, Party, PartyAmount
|
||||
from core.apps.orders.filters.party import PartyFilter
|
||||
|
||||
|
||||
class PartyCreateApiView(generics.GenericAPIView):
|
||||
@@ -31,8 +34,14 @@ class PartyListApiView(generics.GenericAPIView):
|
||||
queryset = Party.objects.select_related('party_amount').prefetch_related('orders')
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
filter_backends = [DjangoFilterBackend]
|
||||
filterset_class = PartyFilter
|
||||
|
||||
def get(self, request):
|
||||
parties = self.get_queryset()
|
||||
parties = self.filter_queryset(self.get_queryset())
|
||||
page = self.paginate_queryset(parties)
|
||||
if page is not None:
|
||||
serializer = self.serializer_class(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
serializer = self.serializer_class(parties, many=True)
|
||||
return Response(serializer.data, status=200)
|
||||
Reference in New Issue
Block a user