some changes in inventory serializer - creted date & distribution data
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
# Generated by Django 5.0 on 2025-07-28 13:45
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authentication', '0027_remove_organizationstats_total_buyers_and_more'),
|
||||
('product', '0055_alter_quota_limit_by_organizations'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='productstats',
|
||||
name='organization',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='product_stats', to='authentication.organization'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='productstats',
|
||||
name='product',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='stats', to='product.product'),
|
||||
),
|
||||
]
|
||||
@@ -135,12 +135,18 @@ class Product(BaseModel):
|
||||
|
||||
|
||||
class ProductStats(BaseModel):
|
||||
product = models.OneToOneField(
|
||||
product = models.ForeignKey(
|
||||
Product,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='stats',
|
||||
null=True
|
||||
)
|
||||
organization = models.ForeignKey(
|
||||
Organization,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='product_stats',
|
||||
null=True
|
||||
)
|
||||
quotas_number = models.PositiveBigIntegerField(default=0)
|
||||
active_quotas_weight = models.PositiveBigIntegerField(default=0)
|
||||
closed_quotas_weight = models.PositiveBigIntegerField(default=0)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from django.db.models import Sum
|
||||
from django.db.models.signals import post_save, post_delete
|
||||
from common.helpers import get_organization_by_user
|
||||
from django.dispatch import receiver
|
||||
from .models import (
|
||||
QuotaDistribution,
|
||||
@@ -13,6 +14,7 @@ from apps.warehouse.models import (
|
||||
InventoryEntry
|
||||
)
|
||||
from django.db import models
|
||||
from crum import get_current_user
|
||||
|
||||
|
||||
def recalculate_remaining_amount(quota):
|
||||
@@ -34,10 +36,12 @@ def update_quota_remaining(sender, instance, **kwargs):
|
||||
def update_product_stats(instance: Product):
|
||||
""" update all stats of product """
|
||||
|
||||
if hasattr(instance, 'stats'):
|
||||
stat = instance.stats
|
||||
organization = get_organization_by_user(get_current_user())
|
||||
|
||||
if ProductStats.objects.filter(organization=organization, product=instance):
|
||||
stat = instance.stats.get(organization=organization, product=instance)
|
||||
else:
|
||||
stat = ProductStats.objects.create(product=instance)
|
||||
stat = ProductStats.objects.create(product=instance, organization=organization)
|
||||
|
||||
# number of quotas
|
||||
quotas_count = instance.quotas.filter(is_closed=False).count() # noqa
|
||||
|
||||
Reference in New Issue
Block a user