fix - caculate my childs quota stat / add assigned orgs & assigned_to_me status

This commit is contained in:
2025-11-25 10:20:51 +03:30
parent 303124e1a1
commit 67904fd1a8
3 changed files with 98 additions and 61 deletions

View File

@@ -8,6 +8,7 @@ from django.db.models import Sum
from simple_history.models import HistoricalRecords
from apps.authentication.models import OrganizationType, Organization
from apps.authentication.services.service import get_all_org_child
from apps.authorization.models import UserRelations
from apps.core.models import BaseModel
from apps.herd.models import Rancher
@@ -465,10 +466,40 @@ class Quota(BaseModel):
return persian_date.month in self.sale_license
def quota_amount_by_org(self, org: Organization):
"""
get stats of quota from org quota stat model
"""
stat = OrganizationQuotaStats.objects.filter(
quota=self,
organization=org
)
if not stat.exists():
# get childs of organization
org_childs = get_all_org_child(org) # noqa
# calculate total stats of child orgs
stat = OrganizationQuotaStats.objects.filter(
quota=self,
organization_id__in=[child.id for child in org_childs]
).aggregate(
total_quota_weight=models.Sum('total_amount'),
total_remaining_weight=models.Sum('remaining_amount'),
total_quota_distributed=models.Sum('total_distributed'),
total_been_sold=models.Sum('sold_amount'),
total_inventory_received=models.Sum('inventory_received'),
total_inventory_entry_balance=models.Sum('inventory_entry_balance')
)
return {
"id": 0,
"quota_weight": stat['total_quota_weight'],
"remaining_weight": stat['total_remaining_weight'],
"quota_distributed": stat['total_quota_distributed'],
"been_sold": stat['total_been_sold'],
"inventory_received": stat['total_inventory_received'],
"inventory_entry_balance": stat['total_inventory_entry_balance'],
}
return {
"id": stat.first().id if stat.exists() else 0,
"quota_weight": stat.first().total_amount if stat.exists() else 0,