diff --git a/apps/product/services/__init__.py b/apps/product/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/product/services/search/distribution_search.py b/apps/product/services/search/distribution_search.py new file mode 100644 index 0000000..33df23b --- /dev/null +++ b/apps/product/services/search/distribution_search.py @@ -0,0 +1,40 @@ +from django.db.models import Q +from apps.product.models import QuotaDistribution +from django.db.models.functions import TruncDate + + +class QuotaDistributionSearch: + def __init__(self, query: str = None, start_date: str = None, end_date: str = None): + self.query = (query or '').strip() + self.start_date = start_date + self.end_date = end_date + + def search(self): + """ multi term search & filter by date range """ + + queryset = QuotaDistribution.objects.all() # noqa + + if self.start_date or self.end_date: + queryset = queryset.annotate(date_only=TruncDate('create_date')) + if self.start_date: + queryset = queryset.filter(date_only__gte=self.start_date) + if self.end_date: + queryset = queryset.filter(date_only__lte=self.end_date) + + # convert string to list of words + if self.query: + keywords = [word.strip() for word in self.query.split(',') if word.strip()] + if keywords: + combined_q = Q() + for keyword in keywords: + combined_q |= Q(assigner_organization__name__icontains=keyword) + combined_q |= Q(assigned_organization__name__icontains=keyword) + combined_q |= Q(distribution_id__icontains=keyword) + combined_q |= Q(quota__quota_id__icontains=keyword) + combined_q |= Q(quota__product__name__icontains=keyword) + combined_q |= Q(quota__sale_type__icontains=keyword) + combined_q |= Q(quota__group__icontains=keyword) + + queryset = queryset.filter(combined_q) + + return queryset.distinct() diff --git a/apps/product/services.py b/apps/product/services/services.py similarity index 100% rename from apps/product/services.py rename to apps/product/services/services.py diff --git a/apps/product/web/api/v1/viewsets/quota_api.py b/apps/product/web/api/v1/viewsets/quota_api.py index 4f5cc46..20a77a4 100644 --- a/apps/product/web/api/v1/viewsets/quota_api.py +++ b/apps/product/web/api/v1/viewsets/quota_api.py @@ -1,9 +1,8 @@ -from apps.product.web.api.v1.serializers import product_serializers as product_serializers from apps.product.web.api.v1.serializers import quota_distribution_serializers from apps.product.web.api.v1.serializers import quota_serializers from apps.product.exceptions import QuotaExpiredTimeException from apps.core.pagination import CustomPageNumberPagination -from apps.product.services import get_products_in_warehouse +from apps.product.services.services import get_products_in_warehouse from apps.product.web.api.v1.viewsets import product_api from common.helpers import get_organization_by_user from rest_framework.exceptions import APIException diff --git a/apps/product/web/api/v1/viewsets/quota_distribution_api.py b/apps/product/web/api/v1/viewsets/quota_distribution_api.py index 3a13441..771fe5e 100644 --- a/apps/product/web/api/v1/viewsets/quota_distribution_api.py +++ b/apps/product/web/api/v1/viewsets/quota_distribution_api.py @@ -91,6 +91,7 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet): Q(assigned_organization=organization) ).order_by('-modify_date') ) + elif query.get('param') == 'assigner': # paginate queryset page = self.paginate_queryset( diff --git a/apps/warehouse/services/__init__.py b/apps/warehouse/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/warehouse/services/search/__init__.py b/apps/warehouse/services/search/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/warehouse/services/search.py b/apps/warehouse/services/search/inventory_search.py similarity index 100% rename from apps/warehouse/services/search.py rename to apps/warehouse/services/search/inventory_search.py diff --git a/apps/warehouse/web/api/v1/api.py b/apps/warehouse/web/api/v1/api.py index 685dc5a..34f0ce9 100644 --- a/apps/warehouse/web/api/v1/api.py +++ b/apps/warehouse/web/api/v1/api.py @@ -1,8 +1,7 @@ from apps.warehouse.web.api.v1 import serializers as warehouse_serializers -from apps.warehouse.services.search import InventoryEntrySearch +from apps.warehouse.services.search.inventory_search import InventoryEntrySearch from apps.warehouse import models as warehouse_models from common.helpers import get_organization_by_user -from rest_framework.exceptions import APIException from common.generics import base64_to_image_file from common.liara_tools import upload_to_liara from rest_framework.decorators import action diff --git a/apps/warehouse/web/api/v1/serializers.py b/apps/warehouse/web/api/v1/serializers.py index d8a0dde..df800be 100644 --- a/apps/warehouse/web/api/v1/serializers.py +++ b/apps/warehouse/web/api/v1/serializers.py @@ -16,6 +16,7 @@ class InventoryEntrySerializer(serializers.ModelSerializer): "id", "create_date", "modify_date", + "organization", "distribution", "weight", "lading_number",