fix organization pagination - quota distribution

This commit is contained in:
2025-06-15 09:08:19 +03:30
parent b52baa5d3b
commit 92382d0ad1
17 changed files with 647 additions and 24 deletions

View File

@@ -5,9 +5,9 @@ from django.contrib.postgres.fields import ArrayField
class LivestockGroup(models.TextChoices):
ROOSTAEI = "roostaei", "روستایی" # noqa
SANATI = "sanati", "صنعتی" # noqa
ASHAYERI = "ashayeri", "عشایری" # noqa
ROOSTAEI = "rural", "روستایی" # noqa
SANATI = "industrial", "صنعتی" # noqa
ASHAYERI = "nomadic", "عشایری" # noqa
class LivestockType(models.TextChoices):
@@ -48,7 +48,9 @@ class ProductCategory(BaseModel):
class Product(BaseModel):
""" Child of reference product - like: brown rice """
name = models.CharField(max_length=250, default='empty') # noqa
product_id = models.BigIntegerField(default=0)
type_choices = (
('free', 'FREE'), # free product
('gov', 'GOVERNMENTAL') # government product
@@ -138,6 +140,7 @@ class Broker(BaseModel):
('public', 'PUBLIC'),
('exclusive', 'EXCLUSIVE')
)
name = models.CharField(max_length=255, null=True)
product = models.ForeignKey(
Product,
on_delete=models.CASCADE,
@@ -150,7 +153,6 @@ class Broker(BaseModel):
related_name='product_organization',
null=True
)
calculation_strategy = models.CharField(
max_length=3,
choices=CALCULATION_CHOICES,
@@ -230,12 +232,27 @@ class IncentivePlan(BaseModel):
class Quota(BaseModel):
""" quota for product with some conditions """
quota_id = models.CharField(max_length=15, null=True)
registerer_organization = models.ForeignKey(
UserRelations,
on_delete=models.CASCADE,
related_name='quotas',
null=True
)
assigned_organizations = models.ManyToManyField(
UserRelations,
related_name='assigned_quotas',
blank=True
)
quota_id = models.PositiveBigIntegerField(null=True, blank=True)
quota_code = models.CharField(max_length=15, null=True)
quota_weight = models.PositiveIntegerField(default=0)
quota_distributed = models.PositiveIntegerField(default=0)
quota_balance = models.PositiveIntegerField(default=0)
product = models.ForeignKey(
Product,
on_delete=models.CASCADE,
related_name='quota'
related_name='quotas',
null=True
)
sale_type = models.CharField(max_length=50, choices=[("free", "آزاد"), ("gov", "دولتی")]) # noqa
month_choices = ArrayField(base_field=models.IntegerField(), null=True)
@@ -253,7 +270,36 @@ class Quota(BaseModel):
def __str__(self):
return f"Quota ({self.id}) for {self.product.name}"
def save(self, *args, **kwargs):
def generate_quota_id(self): # noqa
""" generate id for quota from 1001 """
last = Quota.objects.filter(quota_id__gte=1001, quota_id__lte=1999).order_by('-quota_id').first()
if last:
next_code = last.quota_id + 1
else:
next_code = 1001
return next_code
def calculate_final_price(self):
""" calculate final price of quota """
factor_total = sum([
f.value for f in self.attribute_values.all()
])
broker_total = sum([
b.value for b in self.broker_values.all()
])
coop = self.base_price_cooperative or 0
factory = self.base_price_factory or 0
return factor_total + broker_total + coop + factory
def save(self, calculate_final_price=None, *args, **kwargs):
if not self.quota_id:
self.quota_id = self.generate_quota_id()
if calculate_final_price:
if not self.final_price:
self.final_price = self.calculate_final_price()
return super(Quota, self).save(*args, **kwargs)
@@ -327,3 +373,55 @@ class QuotaLivestockAllocation(BaseModel):
def save(self, *args, **kwargs):
return super(QuotaLivestockAllocation, self).save(*args, **kwargs)
class QuotaLiveStockAgeLimitation(BaseModel):
quota = models.ForeignKey(
Quota,
on_delete=models.CASCADE,
related_name='livestock_age_limitations',
null=True
)
livestock_type = models.CharField(max_length=20, choices=LivestockType.choices)
livestock_subtype = models.CharField(max_length=20, choices=LivestockSubtype.choices)
age_month = models.PositiveIntegerField(default=0)
def __str__(self):
return f"{self.livestock_type}/{self.livestock_subtype}: {self.age_month} month"
def save(self, *args, **kwargs):
return super(QuotaLiveStockAgeLimitation, self).save(*args, **kwargs)
class QuotaDistribution(BaseModel):
assigner_organization = models.ForeignKey(
UserRelations,
on_delete=models.CASCADE,
related_name='distributions_assigner',
null=True
)
description = models.TextField(max_length=1000, null=True)
distribution_id = models.CharField(max_length=20, null=True)
quota = models.ForeignKey(
Quota,
on_delete=models.CASCADE,
related_name='distributions_assigned',
null=True
)
assigned_organization = models.ForeignKey(
UserRelations,
on_delete=models.CASCADE,
related_name='distributions',
null=True
)
weight = models.PositiveBigIntegerField(default=0)
warehouse_entry = models.PositiveBigIntegerField(default=0)
warehouse_balance = models.PositiveBigIntegerField(default=0)
been_sold = models.PositiveBigIntegerField(default=0)
def __str__(self):
return f"{self.distribution_id}-{self.assigned_organization.organization.name}"
def save(self, *args, **kwargs):
return super(QuotaDistribution, self).save(*args, **kwargs)