rancher quota statistics including livestock allocations & incentive plan assignments

This commit is contained in:
2025-08-30 16:09:37 +03:30
parent 8015d9a954
commit 9d9d4d3b80
4 changed files with 75 additions and 17 deletions

View File

@@ -51,23 +51,60 @@ def rancher_quota_weight(rancher, inventory_entry: InventoryEntry):
}
quota: Quota = inventory_entry.distribution.quota
allocations = quota.livestock_allocations.all()
allocations = quota.livestock_allocations.all() # list of quota live stock allocations
livestock_counts = get_rancher_statistics(rancher)
total_weight = 0
details = {}
alloc_details = {}
plan_details = {}
result_list = []
for alloc in allocations:
animal_type = alloc.livestock_type.name
per_head = alloc.quantity_kg
count = livestock_counts.get(live_stock_meta.get(animal_type), 0)
# list of quota allocations, get allocations weight on any animal type
for alloc in allocations: # noqa
if alloc.livestock_type:
animal_type = alloc.livestock_type.name
per_head = alloc.quantity_kg
count = livestock_counts.get(live_stock_meta.get(animal_type), 0)
weight = per_head * count
details[animal_type] = {"weight": weight, "type": alloc.livestock_type.weight_type}
total_weight += weight
weight = per_head * count
alloc_details[animal_type] = {"weight": weight, "type": alloc.livestock_type.weight_type}
total_weight += weight
return {
"total": total_weight,
"by_type": details
}
# list of quota incentive plans, get plans weight on any animal type
incentive_plans = quota.incentive_assignments.all()
for plan in incentive_plans: # noqa
if plan.livestock_type:
animal_type = plan.livestock_type.name
per_head = plan.quantity_kg
count = livestock_counts.get(live_stock_meta.get(animal_type), 0)
weight = per_head * count
plan_details[animal_type] = {"weight": weight, "type": plan.livestock_type.weight_type}
total_weight += weight
# summation of incentive plans & livestock allocations animal types weight
result_details = {"total": total_weight, 'by_type': {}}
all_keys = set(alloc_details.keys()) | set(plan_details.keys()) # get all keys from plan & allocations data
for key in all_keys:
alloc_weight = alloc_details.get(key, {}).get("weight", 0) # total weight of quota livestock allocations data
plan_weight = plan_details.get(key, {}).get("weight", 0) # total weight of quota incentive plan data
# get animal type (Heavy, Light)
animal_type = alloc_details.get(
key, {}
).get("type") or plan_details.get(
key, {}
).get("type")
# final result, total weights
result_list.append({
"name": key,
"weight": alloc_weight + plan_weight,
"type": animal_type
})
result_details['by_type'] = result_list
return result_details