fix structure of base_prices in rancher pos distributions data
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user