From 9df2cf620065b377d14f5e97aa174bfeadba2c39 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Wed, 3 Sep 2025 14:23:04 +0330 Subject: [PATCH] fix ranvher invetory entry bug for existence of rancher - set pagination message --- apps/core/pagination.py | 14 ++++++++++++++ apps/pos_device/services/services.py | 7 +++++-- apps/pos_device/web/api/v1/viewsets/device.py | 2 +- apps/product/models.py | 4 ++-- apps/warehouse/pos/api/v1/api.py | 15 +++++++++++---- apps/warehouse/pos/api/v1/serializers.py | 5 +++-- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/apps/core/pagination.py b/apps/core/pagination.py index cb301dc..0be8fc7 100644 --- a/apps/core/pagination.py +++ b/apps/core/pagination.py @@ -1,7 +1,21 @@ from rest_framework.pagination import PageNumberPagination +from rest_framework.response import Response class CustomPageNumberPagination(PageNumberPagination): page_size = 20 # default page_size_query_param = 'page_size' # set from client max_page_size = 100 # maximum items to show + message = None + + def get_paginated_response(self, data): + return Response({ + 'message': self.message, # Custom message + 'count': self.page.paginator.count, + 'next': self.get_next_link(), + 'previous': self.get_previous_link(), + 'results': data + }) + + def set_message(self, message): + self.message = message diff --git a/apps/pos_device/services/services.py b/apps/pos_device/services/services.py index af22f50..9bcc559 100644 --- a/apps/pos_device/services/services.py +++ b/apps/pos_device/services/services.py @@ -11,8 +11,11 @@ def pos_organizations_sharing_information(device: Device) -> typing.Any: sharing_information_list = [{ "organization_name": item.organization.name, - "broker": item.broker.name, - "amount": item.broker_amount.value + # "credit_card": item.organization.bank_information.first().card or "", + # "sheba": item.organization.bank_information.first().sheba, + # "account": item.organization.bank_information.first().account, + "broker": item.broker.name if item.broker else None, + "amount": item.broker_amount.value if item.broker else None } for item in stake_holders] return sharing_information_list diff --git a/apps/pos_device/web/api/v1/viewsets/device.py b/apps/pos_device/web/api/v1/viewsets/device.py index bafd51d..06a567e 100644 --- a/apps/pos_device/web/api/v1/viewsets/device.py +++ b/apps/pos_device/web/api/v1/viewsets/device.py @@ -311,7 +311,7 @@ class StakeHoldersViewSet(viewsets.ModelViewSet, DynamicSearchMixin, SoftDeleteM raise OrganizationBankAccountException() serializer = self.serializer_class(data=stakeholder) - if serializer.is_valid(): + if serializer.is_valid(raise_exception=True): serializer.save() stakeholders_data.append(serializer.data) diff --git a/apps/product/models.py b/apps/product/models.py index b571aa3..e8a7518 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -358,8 +358,8 @@ class Quota(BaseModel): related_name='quota_limits', blank=True ) - base_price_factory = models.DecimalField(max_digits=12, decimal_places=2) - base_price_cooperative = models.DecimalField(max_digits=12, decimal_places=2) + base_price_factory = models.PositiveBigIntegerField(default=0) + base_price_cooperative = models.PositiveBigIntegerField(default=0) final_price = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True) is_closed = models.BooleanField(default=False) closed_at = models.DateTimeField(null=True, blank=True) diff --git a/apps/warehouse/pos/api/v1/api.py b/apps/warehouse/pos/api/v1/api.py index 937af88..c8cb061 100644 --- a/apps/warehouse/pos/api/v1/api.py +++ b/apps/warehouse/pos/api/v1/api.py @@ -58,18 +58,25 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDevice ) @transaction.atomic def rancher_inventory_entries(self, request): - """ """ + """ list of inventory entries (quotas) for rancher """ + organization = self.get_device_organization() device = self.get_pos_device() - rancher = Rancher.objects.filter(national_code=request.GET['national_code']).first() + rancher = Rancher.objects.filter(national_code=request.GET['national_code']) entries = self.queryset.filter(organization=organization) - available_entries = [entry for entry in entries if can_buy_from_inventory(rancher, entry)] + # check quota inventory entries for rancher + available_entries = [ + entry for entry in entries if (can_buy_from_inventory(rancher.first(), entry) & rancher.exists()) + ] # paginate & response page = self.paginate_queryset(available_entries) if page is not None: - serializer = self.get_serializer(page, many=True, context={'rancher': rancher, 'device': device}) + serializer = self.get_serializer(page, many=True, context={'rancher': rancher.first(), 'device': device}) + # set custom message for paginator + if not available_entries: + self.paginator.set_message("دامدار با کد ملی مد نظر یافت نشد") # noqa return self.get_paginated_response(serializer.data) diff --git a/apps/warehouse/pos/api/v1/serializers.py b/apps/warehouse/pos/api/v1/serializers.py index 60512fe..098b0b1 100644 --- a/apps/warehouse/pos/api/v1/serializers.py +++ b/apps/warehouse/pos/api/v1/serializers.py @@ -61,9 +61,10 @@ class InventoryEntrySerializer(serializers.ModelSerializer): } representation['pricing'] = { - 'brokers_info': quota_brokers_value(instance.distribution.quota), 'pricing_attributes': quota_attribute_value(instance.distribution.quota), - 'sharing': pos_organizations_sharing_information(self.context['device']) + 'sharing': pos_organizations_sharing_information(self.context['device']), + 'base_price_factory': instance.distribution.quota.base_price_factory, + 'base_price_cooperative': instance.distribution.quota.base_price_cooperative, } if 'rancher' in self.context.keys():