fix - caculate my childs quota stat / add assigned orgs & assigned_to_me status
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,7 +53,13 @@ class QuotaSerializer(serializers.ModelSerializer):
|
||||
"weight": dist.weight,
|
||||
} for dist in instance.distributions_assigned.filter(assigned_organization=org)]
|
||||
|
||||
if isinstance(instance, product_models.Quota):
|
||||
representation['assigned_to_me'] = True if org in assigned_orgs else False
|
||||
|
||||
# list of assigned organizations that received this quota
|
||||
representation['assigned_organizations'] = [{
|
||||
"name": org.name, "id": org.id
|
||||
} for org in assigned_orgs]
|
||||
|
||||
if instance.sale_unit:
|
||||
representation['sale_unit'] = product_serializers.SaleUnitSerializer(
|
||||
instance.sale_unit
|
||||
|
||||
@@ -359,8 +359,6 @@ class QuotaViewSet(BaseViewSet, SoftDeleteMixin, viewsets.ModelViewSet, DynamicS
|
||||
self.get_queryset(visibility_by_org_scope=True).filter(
|
||||
is_closed=False)) # return by search param or all objects
|
||||
|
||||
print(queryset)
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(
|
||||
queryset.filter(
|
||||
|
||||
Reference in New Issue
Block a user