some first models of pos & add required to attributes
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
# Generated by Django 5.0 on 2025-07-20 08:59
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('product', '0051_quotabrokervalue_value'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='productstats',
|
||||
name='total_quota',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='productstats',
|
||||
name='active_quotas_weight',
|
||||
field=models.PositiveBigIntegerField(default=0),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='productstats',
|
||||
name='closed_quotas_weight',
|
||||
field=models.PositiveBigIntegerField(default=0),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='productstats',
|
||||
name='quotas_number',
|
||||
field=models.PositiveBigIntegerField(default=0),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='productstats',
|
||||
name='total_distributed_weight',
|
||||
field=models.PositiveBigIntegerField(default=0),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='productstats',
|
||||
name='total_quota_weight',
|
||||
field=models.PositiveBigIntegerField(default=0),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='productstats',
|
||||
name='total_warehouse_entry',
|
||||
field=models.PositiveBigIntegerField(default=0),
|
||||
),
|
||||
]
|
||||
18
apps/product/migrations/0053_attribute_required.py
Normal file
18
apps/product/migrations/0053_attribute_required.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.0 on 2025-07-21 06:06
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('product', '0052_remove_productstats_total_quota_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='attribute',
|
||||
name='required',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
@@ -141,8 +141,13 @@ class ProductStats(BaseModel):
|
||||
related_name='stats',
|
||||
null=True
|
||||
)
|
||||
total_quota = models.PositiveBigIntegerField(default=0)
|
||||
quotas_number = models.PositiveBigIntegerField(default=0)
|
||||
active_quotas_weight = models.PositiveBigIntegerField(default=0)
|
||||
closed_quotas_weight = models.PositiveBigIntegerField(default=0)
|
||||
total_quota_weight = models.PositiveBigIntegerField(default=0)
|
||||
total_remaining = models.PositiveBigIntegerField(default=0)
|
||||
total_distributed_weight = models.PositiveBigIntegerField(default=0)
|
||||
total_warehouse_entry = models.PositiveBigIntegerField(default=0)
|
||||
total_sold = models.PositiveBigIntegerField(default=0)
|
||||
total_transactions = models.PositiveBigIntegerField(default=0)
|
||||
|
||||
@@ -150,7 +155,7 @@ class ProductStats(BaseModel):
|
||||
return f'Product: {self.product.name}-{self.product.id} stats'
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
return super(ProductStats).save(*args, **kwargs)
|
||||
return super(ProductStats, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
class Attribute(BaseModel):
|
||||
@@ -170,7 +175,7 @@ class Attribute(BaseModel):
|
||||
related_name="attributes",
|
||||
null=True
|
||||
)
|
||||
|
||||
required = models.BooleanField(default=False)
|
||||
is_global = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
from django.db.models import Sum
|
||||
from django.db.models.signals import post_save, post_delete
|
||||
from django.dispatch import receiver
|
||||
from .models import QuotaDistribution, Quota, Product
|
||||
from apps.warehouse.models import InventoryQuotaSaleTransaction
|
||||
from .models import (
|
||||
QuotaDistribution,
|
||||
Quota,
|
||||
Product,
|
||||
ProductStats,
|
||||
QuotaStats
|
||||
)
|
||||
from apps.warehouse.models import (
|
||||
InventoryQuotaSaleTransaction,
|
||||
InventoryEntry
|
||||
)
|
||||
from django.db import models
|
||||
|
||||
|
||||
def recalculate_remaining_amount(quota):
|
||||
@@ -22,15 +32,113 @@ def update_quota_remaining(sender, instance, **kwargs):
|
||||
|
||||
|
||||
def update_product_stats(instance: Product):
|
||||
pass
|
||||
""" update all stats of product """
|
||||
|
||||
if hasattr(instance, 'stats'):
|
||||
stat = instance.stats
|
||||
else:
|
||||
stat = ProductStats.objects.create(product=instance)
|
||||
|
||||
# number of quotas
|
||||
quotas_count = instance.quotas.filter(is_closed=False).count() # noqa
|
||||
|
||||
# total weight of product that assigned in quota
|
||||
active_quotas_weight = instance.quotas.filter(is_closed=False).aggregate(
|
||||
total=models.Sum('quota_weight')
|
||||
)['total'] or 0
|
||||
|
||||
closed_quotas_weight = instance.quotas.filter(is_closed=True).aggregate( # noqa
|
||||
total=models.Sum('quota_weight')
|
||||
)['total'] or 0
|
||||
|
||||
total_quotas_weight = instance.quotas.all().aggregate( # noqa
|
||||
total=models.Sum('quota_weight')
|
||||
)['total'] or 0
|
||||
|
||||
# total remaining weight of product quotas
|
||||
total_remaining_quotas_weight = instance.quotas.filter(is_closed=False).aggregate( # noqa
|
||||
total=models.Sum('remaining_weight')
|
||||
)['total'] or 0
|
||||
|
||||
# product total distributed weight from quota
|
||||
total_distributed_weight = QuotaDistribution.objects.filter(
|
||||
quota__product_id=instance.id,
|
||||
quota__is_closed=False
|
||||
).aggregate(total_weight=models.Sum('weight'))['total_weight'] or 0
|
||||
|
||||
# total sold of product from quota
|
||||
total_sold = QuotaDistribution.objects.filter(
|
||||
quota__product_id=instance.id,
|
||||
quota__is_closed=False
|
||||
).aggregate(total_sold=models.Sum('been_sold'))['total_sold'] or 0
|
||||
|
||||
# total entry from product to inventory
|
||||
total_warehouse_entry = QuotaDistribution.objects.filter(
|
||||
quota__product_id=instance.id,
|
||||
quota__is_closed=False
|
||||
).aggregate(total_entry=models.Sum('warehouse_entry'))['total_entry'] or 0
|
||||
|
||||
stat.quotas_number = quotas_count
|
||||
stat.active_quotas_weight = active_quotas_weight
|
||||
stat.closed_quotas_weight = closed_quotas_weight
|
||||
stat.total_quota_weight = total_quotas_weight
|
||||
stat.total_remaining = total_remaining_quotas_weight
|
||||
stat.total_distributed_weight = total_distributed_weight
|
||||
stat.total_warehouse_entry = total_warehouse_entry
|
||||
stat.total_sold = total_sold
|
||||
stat.save(update_fields=[
|
||||
"quotas_number",
|
||||
"active_quotas_weight",
|
||||
"closed_quotas_weight",
|
||||
"total_quota_weight",
|
||||
"total_remaining",
|
||||
"total_distributed_weight",
|
||||
"total_warehouse_entry",
|
||||
"total_sold",
|
||||
])
|
||||
|
||||
|
||||
def update_quota_stats(instance: Quota):
|
||||
pass
|
||||
""" update all stats of quota """
|
||||
|
||||
if hasattr(instance, 'stats'):
|
||||
stat = instance.stats
|
||||
else:
|
||||
stat = QuotaStats.objects.create(quota=instance)
|
||||
|
||||
total_distributed = instance.quota_distributed
|
||||
total_remaining = instance.remaining_weight
|
||||
|
||||
# total entry to inventory from quota
|
||||
total_inventory = InventoryEntry.objects.filter(
|
||||
distribution__quota=instance
|
||||
).aggregate(
|
||||
total_inventory=models.Sum('weight')
|
||||
)['total_inventory'] or 0
|
||||
|
||||
# total sale of distributions from quota
|
||||
total_sale = instance.distributions_assigned.filter(
|
||||
quota__is_closed=False
|
||||
).aggregate(total_sale=models.Sum('been_sold'))['total_sale'] or 0
|
||||
|
||||
stat.total_distributed = total_distributed
|
||||
stat.remaining = total_remaining
|
||||
stat.total_inventory = total_inventory
|
||||
stat.total_sale = total_sale
|
||||
stat.save(update_fields=[
|
||||
"total_distributed",
|
||||
"remaining",
|
||||
"total_inventory",
|
||||
"total_sale",
|
||||
])
|
||||
|
||||
|
||||
@receiver([post_save, post_delete], sender=QuotaDistribution)
|
||||
@receiver([post_save, post_delete], sender=InventoryQuotaSaleTransaction)
|
||||
def update_stats_on_change(sender, instance, **kwargs):
|
||||
update_product_stats(instance)
|
||||
update_quota_stats(instance)
|
||||
if sender == QuotaDistribution:
|
||||
update_product_stats(instance.quota.product)
|
||||
update_quota_stats(instance.quota)
|
||||
elif sender == InventoryQuotaSaleTransaction:
|
||||
update_product_stats(instance.quota_distribution.quota.product)
|
||||
update_quota_stats(instance.quota_distribution.quota)
|
||||
|
||||
Reference in New Issue
Block a user