From 7fad8b58b910554dc463e788c823a8c045143147 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Mon, 6 Oct 2025 10:32:29 +0330 Subject: [PATCH] fix structure of base_prices in rancher pos distributions data --- apps/pos_device/services/services.py | 10 +----- .../quota_distribution_serializers.py | 14 ++++---- apps/product/services/services.py | 33 ++++++++++++------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/apps/pos_device/services/services.py b/apps/pos_device/services/services.py index 26237ac..e39cdba 100644 --- a/apps/pos_device/services/services.py +++ b/apps/pos_device/services/services.py @@ -60,15 +60,7 @@ def pos_organizations_sharing_information( ) if quota.pricing_items.filter( name='base_price' ) else None, - - # """ - # if we will need to get agencies share amount, we can use this bellow code - # - # # item.holders_share_amount.filter(quota_distribution=distribution).first().share_amount - # # if item.holders_share_amount.filter(quota_distribution=distribution).exists() else None - # """ - - "default_account": item.default + "default_account": True }) return sharing_information_list 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 0d70b5a..f03ac79 100644 --- a/apps/product/pos/api/v1/serializers/quota_distribution_serializers.py +++ b/apps/product/pos/api/v1/serializers/quota_distribution_serializers.py @@ -110,16 +110,18 @@ class QuotaDistributionSerializer(serializers.ModelSerializer): 'free_sale_for_this_rancher': rancher.ignore_purchase_limit } - representation['pricing'] = { # noqa - 'main_account_sheba': organization.bank_information.first().sheba, - 'pricing_attributes': quota_attribute_value(instance.quota), - 'sharing': pos_organizations_sharing_information( + sharing_list = pos_organizations_sharing_information( device, instance.quota, distribution=instance, owner_org=organization - ), - 'base_prices': quota_pricing_items_by_type(instance.quota) + ) + + representation['pricing'] = { # noqa + 'main_account_sheba': 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) } if 'rancher' in self.context.keys(): diff --git a/apps/product/services/services.py b/apps/product/services/services.py index d965e9a..e79963b 100644 --- a/apps/product/services/services.py +++ b/apps/product/services/services.py @@ -96,20 +96,29 @@ def quota_attribute_value(quota: Quota) -> typing.Any: return attribute_values_list -def quota_pricing_items_by_type(quota: Quota) -> typing.Any: +def quota_pricing_items_by_type(quota: Quota, sharing: list) -> typing.Any: """ information of quota pricing items by final price type Optimized: fetch all pricing items once, group by pricing_type """ - # مرحله ۱: همه‌ی آیتم‌های مربوط به این quota رو یکجا بگیر + + # calculate pos sharing accounts total price + # summation with bellow price items and set final total price in output + calculate_sharing_total_price = sum(item['amount'] for item in sharing) + items = ( - QuotaPriceCalculationItems.objects - .filter(quota=quota) - .select_related("pricing_type") - .values("pricing_type_id", "pricing_type__en_name", "pricing_type__name", "name", "value") + QuotaPriceCalculationItems.objects.filter(quota=quota).select_related( + "pricing_type" + ).values( + "pricing_type_id", + "pricing_type__en_name", + "pricing_type__name", + "name", + "value" + ) ) - # مرحله ۲: گروه‌بندی آیتم‌ها بر اساس pricing_type + # split price items grouped = defaultdict(list) for item in items: key = item["pricing_type__en_name"] @@ -118,7 +127,7 @@ def quota_pricing_items_by_type(quota: Quota) -> typing.Any: "value": item["value"] }) - # مرحله ۳: جمع کل هر گروه + # get total items price & final result result = [] for en_name, group_items in grouped.items(): total_price = sum(i["value"] for i in group_items if i["value"]) @@ -127,9 +136,11 @@ def quota_pricing_items_by_type(quota: Quota) -> typing.Any: en_name ) result.append({ - en_name: group_items, - f"{en_name}_fa": fa_name, - f"{en_name}_total_price": total_price, + 'id': en_name, + 'items': group_items, + "name": fa_name, + "price": total_price, + "total_price": total_price + calculate_sharing_total_price }) return result