fix structure of base_prices in rancher pos distributions data
This commit is contained in:
@@ -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