fix - caculate my childs quota stat / add assigned orgs & assigned_to_me status
This commit is contained in:
@@ -24,6 +24,8 @@ class QuotaSerializer(serializers.ModelSerializer):
|
||||
def to_representation(self, instance: product_models.Quota):
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
assigned_orgs = instance.assigned_organizations.all()
|
||||
|
||||
# change quota weight by organization received weight
|
||||
if 'org' in self.context.keys():
|
||||
org = self.context['org']
|
||||
@@ -51,71 +53,77 @@ class QuotaSerializer(serializers.ModelSerializer):
|
||||
"weight": dist.weight,
|
||||
} for dist in instance.distributions_assigned.filter(assigned_organization=org)]
|
||||
|
||||
if isinstance(instance, product_models.Quota):
|
||||
if instance.sale_unit:
|
||||
representation['sale_unit'] = product_serializers.SaleUnitSerializer(
|
||||
instance.sale_unit
|
||||
).data
|
||||
representation['product'] = {"product": instance.product.name, "product_id": instance.product.id}
|
||||
representation['assigned_to_me'] = True if org in assigned_orgs else False
|
||||
|
||||
# quota incentive plan data
|
||||
incentive_plan_map = {}
|
||||
for assign in instance.incentive_assignments.all():
|
||||
plan_id = assign.incentive_plan.id
|
||||
if plan_id not in incentive_plan_map:
|
||||
incentive_plan_map[plan_id] = {
|
||||
"id": assign.id,
|
||||
"name": assign.incentive_plan.name,
|
||||
"incentive_plan": plan_id,
|
||||
"live_stocks": []
|
||||
}
|
||||
# list of assigned organizations that received this quota
|
||||
representation['assigned_organizations'] = [{
|
||||
"name": org.name, "id": org.id
|
||||
} for org in assigned_orgs]
|
||||
|
||||
if assign.livestock_type:
|
||||
incentive_plan_map[plan_id]['live_stocks'].append({
|
||||
"id": assign.livestock_type.id,
|
||||
"name": assign.livestock_type.name,
|
||||
"quantity": assign.quantity_kg
|
||||
})
|
||||
|
||||
representation['incentive_plan'] = list(incentive_plan_map.values())
|
||||
|
||||
representation['attribute_values'] = product_serializers.AttributeValueSerializer(
|
||||
instance.attribute_values.all(),
|
||||
many=True
|
||||
if instance.sale_unit:
|
||||
representation['sale_unit'] = product_serializers.SaleUnitSerializer(
|
||||
instance.sale_unit
|
||||
).data
|
||||
representation['product'] = {"product": instance.product.name, "product_id": instance.product.id}
|
||||
|
||||
representation['brokers'] = QuotaBrokerValueSerializer(
|
||||
instance.broker_values.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_allocations'] = QuotaLiveStockAllocationSerializer(
|
||||
instance.livestock_allocations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_limitations'] = QuotaLiveStockAgeLimitationSerializer(
|
||||
instance.livestock_age_limitations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['limit_by_organizations'] = [
|
||||
{"name": limit.name, "id": limit.id} for limit in instance.limit_by_organizations.all()
|
||||
]
|
||||
|
||||
# Build a simplified list of pricing items for API output:
|
||||
# map `pricing_type_id` to `pricing_type` and keep `name` and `value`
|
||||
items = [
|
||||
{
|
||||
"pricing_type": it["pricing_type_id"],
|
||||
"pricing_type_name": it["pricing_type__name"],
|
||||
"name": it["name"],
|
||||
"value": it["value"],
|
||||
# quota incentive plan data
|
||||
incentive_plan_map = {}
|
||||
for assign in instance.incentive_assignments.all():
|
||||
plan_id = assign.incentive_plan.id
|
||||
if plan_id not in incentive_plan_map:
|
||||
incentive_plan_map[plan_id] = {
|
||||
"id": assign.id,
|
||||
"name": assign.incentive_plan.name,
|
||||
"incentive_plan": plan_id,
|
||||
"live_stocks": []
|
||||
}
|
||||
for it in instance.pricing_items.values("pricing_type_id", "pricing_type__name", "name", "value")
|
||||
]
|
||||
|
||||
representation["price_calculation_items"] = items
|
||||
if assign.livestock_type:
|
||||
incentive_plan_map[plan_id]['live_stocks'].append({
|
||||
"id": assign.livestock_type.id,
|
||||
"name": assign.livestock_type.name,
|
||||
"quantity": assign.quantity_kg
|
||||
})
|
||||
|
||||
representation['incentive_plan'] = list(incentive_plan_map.values())
|
||||
|
||||
representation['attribute_values'] = product_serializers.AttributeValueSerializer(
|
||||
instance.attribute_values.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['brokers'] = QuotaBrokerValueSerializer(
|
||||
instance.broker_values.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_allocations'] = QuotaLiveStockAllocationSerializer(
|
||||
instance.livestock_allocations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_limitations'] = QuotaLiveStockAgeLimitationSerializer(
|
||||
instance.livestock_age_limitations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['limit_by_organizations'] = [
|
||||
{"name": limit.name, "id": limit.id} for limit in instance.limit_by_organizations.all()
|
||||
]
|
||||
|
||||
# Build a simplified list of pricing items for API output:
|
||||
# map `pricing_type_id` to `pricing_type` and keep `name` and `value`
|
||||
items = [
|
||||
{
|
||||
"pricing_type": it["pricing_type_id"],
|
||||
"pricing_type_name": it["pricing_type__name"],
|
||||
"name": it["name"],
|
||||
"value": it["value"],
|
||||
}
|
||||
for it in instance.pricing_items.values("pricing_type_id", "pricing_type__name", "name", "value")
|
||||
]
|
||||
|
||||
representation["price_calculation_items"] = items
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
Reference in New Issue
Block a user