fix structure of base_prices in rancher pos distributions data

This commit is contained in:
2025-10-06 10:32:29 +03:30
parent c3f745f5d0
commit 7fad8b58b9
3 changed files with 31 additions and 26 deletions

View File

@@ -60,15 +60,7 @@ def pos_organizations_sharing_information(
) if quota.pricing_items.filter( ) if quota.pricing_items.filter(
name='base_price' name='base_price'
) else None, ) else None,
"default_account": True
# """
# 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
}) })
return sharing_information_list return sharing_information_list

View File

@@ -110,16 +110,18 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
'free_sale_for_this_rancher': rancher.ignore_purchase_limit 'free_sale_for_this_rancher': rancher.ignore_purchase_limit
} }
representation['pricing'] = { # noqa sharing_list = pos_organizations_sharing_information(
'main_account_sheba': organization.bank_information.first().sheba,
'pricing_attributes': quota_attribute_value(instance.quota),
'sharing': pos_organizations_sharing_information(
device, device,
instance.quota, instance.quota,
distribution=instance, distribution=instance,
owner_org=organization 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(): if 'rancher' in self.context.keys():

View File

@@ -96,20 +96,29 @@ def quota_attribute_value(quota: Quota) -> typing.Any:
return attribute_values_list 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 information of quota pricing items by final price type
Optimized: fetch all pricing items once, group by pricing_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 = ( items = (
QuotaPriceCalculationItems.objects QuotaPriceCalculationItems.objects.filter(quota=quota).select_related(
.filter(quota=quota) "pricing_type"
.select_related("pricing_type") ).values(
.values("pricing_type_id", "pricing_type__en_name", "pricing_type__name", "name", "value") "pricing_type_id",
"pricing_type__en_name",
"pricing_type__name",
"name",
"value"
)
) )
# مرحله ۲: گروه‌بندی آیتم‌ها بر اساس pricing_type # split price items
grouped = defaultdict(list) grouped = defaultdict(list)
for item in items: for item in items:
key = item["pricing_type__en_name"] key = item["pricing_type__en_name"]
@@ -118,7 +127,7 @@ def quota_pricing_items_by_type(quota: Quota) -> typing.Any:
"value": item["value"] "value": item["value"]
}) })
# مرحله ۳: جمع کل هر گروه # get total items price & final result
result = [] result = []
for en_name, group_items in grouped.items(): for en_name, group_items in grouped.items():
total_price = sum(i["value"] for i in group_items if i["value"]) 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 en_name
) )
result.append({ result.append({
en_name: group_items, 'id': en_name,
f"{en_name}_fa": fa_name, 'items': group_items,
f"{en_name}_total_price": total_price, "name": fa_name,
"price": total_price,
"total_price": total_price + calculate_sharing_total_price
}) })
return result return result