add - new changes on OrganizationQuotaStat & calculative signals on quota/distribution

This commit is contained in:
2025-11-16 10:54:56 +03:30
parent 3169298f91
commit 2ba59174d3
5 changed files with 141 additions and 38 deletions

View File

@@ -4,6 +4,7 @@ import jdatetime
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.db import transaction
from django.db.models import Sum
from simple_history.models import HistoricalRecords
from apps.authentication.models import OrganizationType, Organization
@@ -468,7 +469,12 @@ class Quota(BaseModel):
quota=self,
organization=org
)
return stat.first().total_amount if stat.exists() else None
return {
"quota_weight": stat.first().total_amount if stat.exists() else 0,
"remaining_weight": stat.first().remaining_amount if stat.exists() else 0,
"quota_distributed": stat.first().total_distributed if stat.exists() else 0,
"been_sold": stat.first().sold_amount if stat.exists() else 0,
}
def soft_delete(self):
self.trash = True
@@ -808,6 +814,29 @@ class OrganizationQuotaStats(BaseModel):
distributions = models.ManyToManyField(QuotaDistribution)
total_amount = models.PositiveBigIntegerField(default=0)
total_distributed = models.PositiveBigIntegerField(default=0)
sold_amount = models.PositiveBigIntegerField(default=0)
remaining_amount = models.PositiveBigIntegerField(default=0) # total - sold
def update_amount(self):
""" calculate total/sold/remaining """
from apps.warehouse.models import InventoryQuotaSaleItem
# calculate total amount of distribution
self.total_distributed = self.distributions.filter().aggregate(
total=Sum('weight')
)['total'] or 0
self.sold_amount = InventoryQuotaSaleItem.objects.filter(
quota_distribution__in=self.distributions.all(),
transaction__transaction_status='success'
).aggregate(
total=Sum('weight')
)['total'] or 0
self.remaining_amount = self.total_amount - self.sold_amount
self.save()
def __str__(self):
return f"organization: {self.organization} - quota: {self.quota}"