fix agencie share on pos device - fix validation bug of free product pos
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
from apps.product.models import Quota, QuotaLivestockAllocation
|
||||
from collections import defaultdict
|
||||
|
||||
from apps.product.models import (
|
||||
Quota,
|
||||
QuotaLivestockAllocation,
|
||||
QuotaPriceCalculationItems,
|
||||
QuotaFinalPriceTypes
|
||||
)
|
||||
from apps.warehouse.models import InventoryEntry
|
||||
from apps.herd.models import Rancher
|
||||
from django.db.models import Sum
|
||||
import typing
|
||||
|
||||
|
||||
@@ -86,3 +94,42 @@ def quota_attribute_value(quota: Quota) -> typing.Any:
|
||||
} for attr in attributes]
|
||||
|
||||
return attribute_values_list
|
||||
|
||||
|
||||
def quota_pricing_items_by_type(quota: Quota) -> typing.Any:
|
||||
"""
|
||||
information of quota pricing items by final price type
|
||||
Optimized: fetch all pricing items once, group by pricing_type
|
||||
"""
|
||||
# مرحله ۱: همهی آیتمهای مربوط به این quota رو یکجا بگیر
|
||||
items = (
|
||||
QuotaPriceCalculationItems.objects
|
||||
.filter(quota=quota)
|
||||
.select_related("pricing_type")
|
||||
.values("pricing_type_id", "pricing_type__en_name", "pricing_type__name", "name", "value")
|
||||
)
|
||||
|
||||
# مرحله ۲: گروهبندی آیتمها بر اساس pricing_type
|
||||
grouped = defaultdict(list)
|
||||
for item in items:
|
||||
key = item["pricing_type__en_name"]
|
||||
grouped[key].append({
|
||||
"name": item["name"],
|
||||
"value": item["value"]
|
||||
})
|
||||
|
||||
# مرحله ۳: جمع کل هر گروه
|
||||
result = []
|
||||
for en_name, group_items in grouped.items():
|
||||
total_price = sum(i["value"] for i in group_items if i["value"])
|
||||
fa_name = next(
|
||||
(i["pricing_type__name"] for i in items if i["pricing_type__en_name"] == en_name),
|
||||
en_name
|
||||
)
|
||||
result.append({
|
||||
en_name: group_items,
|
||||
f"{en_name}_fa": fa_name,
|
||||
f"{en_name}_total_price": total_price,
|
||||
})
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user