change map structure of rancher quota weight satistics

This commit is contained in:
2025-09-22 15:36:25 +03:30
parent ecb1049681
commit d528f5e9ac
4 changed files with 66 additions and 17 deletions

View File

@@ -16,11 +16,11 @@ def get_rancher_statistics(rancher: Rancher = None) -> typing.Any:
herd_count=Count("herd", distinct=True), herd_count=Count("herd", distinct=True),
light_count=Count('id', filter=Q(weight_type='L')), light_count=Count('id', filter=Q(weight_type='L')),
heavy_count=Count('id', filter=Q(weight_type='H')), heavy_count=Count('id', filter=Q(weight_type='H')),
sheep_count=Count('id', filter=Q(type__name='گوسفند')), # noqa sheep=Count('id', filter=Q(type__name='گوسفند')), # noqa
goat_count=Count('id', filter=Q(type__name='بز')), goat=Count('id', filter=Q(type__name='بز')),
cow_count=Count('id', filter=Q(type__name='گاو')), cow=Count('id', filter=Q(type__name='گاو')),
camel_count=Count('id', filter=Q(type__name='شتر')), camel=Count('id', filter=Q(type__name='شتر')),
horse_count=Count('id', filter=Q(type__name='بز')), horse=Count('id', filter=Q(type__name='بز')),
) )
return [{'name': key, 'value': value} for key, value in stats.items()], stats return [{'name': key, 'value': value} for key, value in stats.items()], stats
@@ -34,11 +34,11 @@ def rancher_quota_weight(rancher, inventory_entry: InventoryEntry = None, distri
""" """
live_stock_meta = { live_stock_meta = {
"گوسفند": "sheep_count", # noqa "گوسفند": "sheep", # noqa
"بز": "goat_count", "بز": "goat",
"گاو": "cow_count", "گاو": "cow",
"شتر": "camel_count", "شتر": "camel",
"اسب": "horse_count" "اسب": "horse"
} }
if inventory_entry: if inventory_entry:
@@ -60,20 +60,21 @@ def rancher_quota_weight(rancher, inventory_entry: InventoryEntry = None, distri
for item in allocations + incentive_plans: # noqa for item in allocations + incentive_plans: # noqa
if item.livestock_type: if item.livestock_type:
animal_type = item.livestock_type.name animal_type_fa = item.livestock_type.name
animal_type_en = item.livestock_type.en_name
per_head = item.quantity_kg per_head = item.quantity_kg
count = livestock_counts_dict.get(live_stock_meta.get(animal_type), 0) count = livestock_counts_dict.get(live_stock_meta.get(animal_type_fa), 0)
weight = per_head * count weight = per_head * count
total_weight += weight total_weight += weight
if animal_type not in merged: if animal_type_en not in merged:
merged[animal_type] = { merged[animal_type_en] = {
"weight": weight, "weight": weight,
"type": item.livestock_type.weight_type "type": item.livestock_type.weight_type
} }
else: else:
merged[animal_type]['weight'] += weight merged[animal_type_en]['weight'] += weight
return { return {
"total_weight": total_weight, "total_weight": total_weight,

View File

@@ -0,0 +1,23 @@
# Generated by Django 5.0 on 2025-09-22 11:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('livestock', '0013_livestock_archive'),
]
operations = [
migrations.AddField(
model_name='livestockusetype',
name='en_name',
field=models.CharField(max_length=50, null=True),
),
migrations.AlterField(
model_name='livestockusetype',
name='name',
field=models.CharField(max_length=50, null=True),
),
]

View File

@@ -0,0 +1,23 @@
# Generated by Django 5.0 on 2025-09-22 12:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('livestock', '0014_livestockusetype_en_name_alter_livestockusetype_name'),
]
operations = [
migrations.AddField(
model_name='livestocktype',
name='en_name',
field=models.CharField(max_length=50, null=True),
),
migrations.AlterField(
model_name='livestocktype',
name='name',
field=models.CharField(max_length=50, null=True),
),
]

View File

@@ -17,7 +17,8 @@ class LiveStockSpecies(BaseModel): # noqa
class LiveStockType(BaseModel): # noqa class LiveStockType(BaseModel): # noqa
""" types like sheep, cow, camel, etc """ """ types like sheep, cow, camel, etc """
name = models.CharField(max_length=50) name = models.CharField(max_length=50, null=True)
en_name = models.CharField(max_length=50, null=True)
weight_types = ( weight_types = (
('L', 'Light'), ('L', 'Light'),
('H', 'Heavy') ('H', 'Heavy')
@@ -37,7 +38,8 @@ class LiveStockType(BaseModel): # noqa
class LiveStockUseType(BaseModel): class LiveStockUseType(BaseModel):
""" use types like Beef, Milky, etc """ """ use types like Beef, Milky, etc """
name = models.CharField(max_length=50) name = models.CharField(max_length=50, null=True)
en_name = models.CharField(max_length=50, null=True)
def __str__(self): def __str__(self):
return f'{self.name}' return f'{self.name}'