From 386008b3095aa2a2d0beca151137f74aa7fae2f8 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Wed, 19 Nov 2025 16:49:00 +0330 Subject: [PATCH] add - org quota stat to inventory_entry / inventory_balance to org_quota_stat --- ...ationquotastats_inventory_entry_balance.py | 18 +++++++++++++++++ apps/product/models.py | 2 ++ .../api/v1/serializers/quota_serializers.py | 3 +++ .../0044_inventoryentry_org_quota_stat.py | 20 +++++++++++++++++++ apps/warehouse/models.py | 6 ++++++ apps/warehouse/web/api/v1/serializers.py | 1 + 6 files changed, 50 insertions(+) create mode 100644 apps/product/migrations/0097_organizationquotastats_inventory_entry_balance.py create mode 100644 apps/warehouse/migrations/0044_inventoryentry_org_quota_stat.py diff --git a/apps/product/migrations/0097_organizationquotastats_inventory_entry_balance.py b/apps/product/migrations/0097_organizationquotastats_inventory_entry_balance.py new file mode 100644 index 0000000..e9bcc35 --- /dev/null +++ b/apps/product/migrations/0097_organizationquotastats_inventory_entry_balance.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0 on 2025-11-19 13:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('product', '0096_organizationquotastats_inventory_received'), + ] + + operations = [ + migrations.AddField( + model_name='organizationquotastats', + name='inventory_entry_balance', + field=models.PositiveBigIntegerField(default=0), + ), + ] diff --git a/apps/product/models.py b/apps/product/models.py index 756045c..3a2629b 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -470,6 +470,7 @@ class Quota(BaseModel): organization=org ) return { + "id": stat.first().id if stat.exists() else 0, "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, @@ -817,6 +818,7 @@ class OrganizationQuotaStats(BaseModel): total_amount = models.PositiveBigIntegerField(default=0) total_distributed = models.PositiveBigIntegerField(default=0) inventory_received = models.PositiveBigIntegerField(default=0) + inventory_entry_balance = models.PositiveBigIntegerField(default=0) sold_amount = models.PositiveBigIntegerField(default=0) remaining_amount = models.PositiveBigIntegerField(default=0) # total - sold stat_type = models.CharField(max_length=150, choices=( diff --git a/apps/product/web/api/v1/serializers/quota_serializers.py b/apps/product/web/api/v1/serializers/quota_serializers.py index c11a03b..fd4b821 100644 --- a/apps/product/web/api/v1/serializers/quota_serializers.py +++ b/apps/product/web/api/v1/serializers/quota_serializers.py @@ -29,6 +29,9 @@ class QuotaSerializer(serializers.ModelSerializer): org = self.context['org'] quota_weight_by_org = instance.quota_amount_by_org(org) if quota_weight_by_org: + # organization quota stat record + representation['org_quota_stat'] = quota_weight_by_org['id'] + representation['quota_weight'] = quota_weight_by_org['quota_weight'] representation['quota_distributed'] = quota_weight_by_org['quota_distributed'] representation['remaining_weight'] = quota_weight_by_org['remaining_weight'] diff --git a/apps/warehouse/migrations/0044_inventoryentry_org_quota_stat.py b/apps/warehouse/migrations/0044_inventoryentry_org_quota_stat.py new file mode 100644 index 0000000..e2ea8cf --- /dev/null +++ b/apps/warehouse/migrations/0044_inventoryentry_org_quota_stat.py @@ -0,0 +1,20 @@ +# Generated by Django 5.0 on 2025-11-19 13:16 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('product', '0097_organizationquotastats_inventory_entry_balance'), + ('warehouse', '0043_inventoryentry_quota'), + ] + + operations = [ + migrations.AddField( + model_name='inventoryentry', + name='org_quota_stat', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='inventory_entry', to='product.organizationquotastats'), + ), + ] diff --git a/apps/warehouse/models.py b/apps/warehouse/models.py index c18cd50..45d2570 100644 --- a/apps/warehouse/models.py +++ b/apps/warehouse/models.py @@ -31,6 +31,12 @@ class InventoryEntry(BaseModel): related_name="inventory", null=True ) + org_quota_stat = models.ForeignKey( + product_models.OrganizationQuotaStats, + on_delete=models.CASCADE, + related_name='inventory_entry', + null=True + ) weight = models.PositiveBigIntegerField(default=0) balance = models.PositiveBigIntegerField(default=0) lading_number = models.CharField(max_length=50, null=True) diff --git a/apps/warehouse/web/api/v1/serializers.py b/apps/warehouse/web/api/v1/serializers.py index fbc52cc..863f0b6 100644 --- a/apps/warehouse/web/api/v1/serializers.py +++ b/apps/warehouse/web/api/v1/serializers.py @@ -15,6 +15,7 @@ class InventoryEntrySerializer(serializers.ModelSerializer): "modify_date", "organization", "quota", + 'org_quota_stat', "weight", "balance", "lading_number",