From 66f881867926f609e501104b13863df95646e955 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Mon, 10 Nov 2025 10:43:39 +0330 Subject: [PATCH] add - IR to all sheba in devices --- apps/pos_device/services/services.py | 6 +-- .../api/v1/serializers/product_serializers.py | 4 +- .../quota_distribution_serializers.py | 2 +- .../services/quota_distribution_service.py | 51 +++++++++++++++++-- apps/warehouse/pos/api/v1/api.py | 4 +- 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/apps/pos_device/services/services.py b/apps/pos_device/services/services.py index 6ddffe4..6667167 100644 --- a/apps/pos_device/services/services.py +++ b/apps/pos_device/services/services.py @@ -27,7 +27,7 @@ def pos_organizations_sharing_information( "organization_name": item.organization.name, "bank_account": { "credit_card": item.organization.bank_information.first().card, - "sheba": item.organization.bank_information.first().sheba, + "sheba": "IR" + item.organization.bank_information.first().sheba, "account": item.organization.bank_information.first().account, } if item.organization.bank_information.exists() else {}, "broker": item.broker.name if item.broker else None, @@ -57,7 +57,7 @@ def pos_organizations_sharing_information( "organization_name": item.organization.name, "bank_account": { "credit_card": item.organization.bank_information.first().card, - "sheba": item.organization.bank_information.first().sheba, + "sheba": "IR" + item.organization.bank_information.first().sheba, "account": item.organization.bank_information.first().account, } if item.organization.bank_information.exists() else {}, "broker": item.broker.name if item.broker else None, @@ -118,7 +118,7 @@ def agency_organization_pos_info( "organization_name": agency.name, "bank_account": { "credit_card": agency.bank_information.first().card, - "sheba": agency.bank_information.first().sheba, + "sheba": "IR" + agency.bank_information.first().sheba, "account": agency.bank_information.first().account, } if agency.bank_information.exists() else {}, "amount": agc_share_amount, diff --git a/apps/product/pos/api/v1/serializers/product_serializers.py b/apps/product/pos/api/v1/serializers/product_serializers.py index 50735b7..0c44f89 100644 --- a/apps/product/pos/api/v1/serializers/product_serializers.py +++ b/apps/product/pos/api/v1/serializers/product_serializers.py @@ -40,13 +40,13 @@ class POSFreeProductSerializer(serializers.ModelSerializer): } representation['total_price'] = instance.price + instance.company_fee - representation['main_shaba'] = instance.organization.bank_information.all().first().sheba # noqa + representation['main_shaba'] = "IR" + instance.organization.bank_information.all().first().sheba # noqa main_company = product_models.Organization.objects.get( national_unique_id='1111111111' ) main_company_bank_account = main_company.bank_information.first() - representation['company_sheba'] = main_company_bank_account.sheba + representation['company_sheba'] = "IR" + main_company_bank_account.sheba return representation diff --git a/apps/product/pos/api/v1/serializers/quota_distribution_serializers.py b/apps/product/pos/api/v1/serializers/quota_distribution_serializers.py index 9f116a3..445fb30 100644 --- a/apps/product/pos/api/v1/serializers/quota_distribution_serializers.py +++ b/apps/product/pos/api/v1/serializers/quota_distribution_serializers.py @@ -119,7 +119,7 @@ class QuotaDistributionSerializer(serializers.ModelSerializer): ) representation['pricing'] = { # noqa - 'main_account_sheba': organization.bank_information.first().sheba, + 'main_account_sheba': "IR" + organization.bank_information.first().sheba, 'pricing_attributes': quota_attribute_value(instance.quota), 'sharing': sharing_list, 'base_prices': quota_pricing_items_by_type(instance.quota, sharing=sharing_list) diff --git a/apps/product/services/quota_distribution_service.py b/apps/product/services/quota_distribution_service.py index 079bea9..5253212 100644 --- a/apps/product/services/quota_distribution_service.py +++ b/apps/product/services/quota_distribution_service.py @@ -1,4 +1,6 @@ +from django.db import transaction from django.db.models import Sum +from rest_framework.exceptions import APIException from apps.product import models as product_models @@ -15,14 +17,57 @@ class QuotaDistributionService: self.distribution = product_models.QuotaDistribution.objects.filter( quota=quota, assigned_organization=organization, - quota__is_closed=False, warehouse_entry__gt=0 ).first() @property - def total_weight(self): + def total_weight(): return self.distribution.aggregate( total=Sum('weight') )['total'] or 0 - pass + @property + def remaining_weight(): + return self.distribution.aggregate( + total=Sum('remaining_weight') + )['total'] or 0 + + def to_representation(self): # noqa + return { + 'quota_id': self.quota.id, + 'quota_identity': self.quota.quota_id, + 'product': self.quota.product.name, + 'total_weight': self.total_weight, + 'remaining_weight': self.remaining_weight, + 'distribution_ids': self.distribution_ids, + } + + @transaction.atomic + def consume(self, weight): # noqa + """ + Consume 'amount' of weight across all distributions in order. + Automatically splits usage if needed. + """ + + if weight > self.remaining_weight: + raise APIException('Not enough weight to consume') + + remaining_to_consume = weight + + for dist in self.distribution.select_for_update(): + if remaining_to_consume <= 0: + break + + available = dist.remaining_weight + if available <= 0: + continue + + consume_amount = min(available, remaining_to_consume) + dist.remaining_weight -= consume_amount + dist.save(update_fields=["remaining_weight"]) + remaining_to_consume -= consume_amount + + if remaining_to_consume > 0: + raise APIException('Not enough weight to consume') + + return True diff --git a/apps/warehouse/pos/api/v1/api.py b/apps/warehouse/pos/api/v1/api.py index 7136f3f..f351457 100644 --- a/apps/warehouse/pos/api/v1/api.py +++ b/apps/warehouse/pos/api/v1/api.py @@ -74,7 +74,7 @@ class InventoryEntryViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDevice # paginate & response page = self.paginate_queryset(available_entries) # noqa - if page is not None: + if page is not None: # noqa serializer = self.get_serializer(page, many=True, context={'rancher': rancher.first(), 'device': device}) # set custom message for paginator if not rancher: @@ -117,7 +117,7 @@ class InventoryQuotaSaleTransactionViewSet(viewsets.ModelViewSet, DynamicSearchM # paginate & response page = self.paginate_queryset(queryset) - if page is not None: + if page is not None: # noqa serializer = self.get_serializer(page, many=True) return self.get_paginated_response(serializer.data)