From ed57517af74880fb934c231148dc6549a963cbcb Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Mon, 17 Nov 2025 11:09:25 +0330 Subject: [PATCH] fix - import stat type in orgquotastat --- ...020_alter_rancher_ignore_purchase_limit.py | 18 ++++++++++++++++++ .../0095_organizationquotastats_stat_type.py | 18 ++++++++++++++++++ apps/product/models.py | 4 ++++ apps/product/services/quota_stat_service.py | 19 +++++++++++-------- apps/product/signals.py | 1 + 5 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 apps/herd/migrations/0020_alter_rancher_ignore_purchase_limit.py create mode 100644 apps/product/migrations/0095_organizationquotastats_stat_type.py diff --git a/apps/herd/migrations/0020_alter_rancher_ignore_purchase_limit.py b/apps/herd/migrations/0020_alter_rancher_ignore_purchase_limit.py new file mode 100644 index 0000000..b80f50e --- /dev/null +++ b/apps/herd/migrations/0020_alter_rancher_ignore_purchase_limit.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0 on 2025-11-17 07:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('herd', '0019_alter_rancher_union_code_alter_rancher_union_name'), + ] + + operations = [ + migrations.AlterField( + model_name='rancher', + name='ignore_purchase_limit', + field=models.BooleanField(default=True, help_text='if its true rancher has not buy limitations'), + ), + ] diff --git a/apps/product/migrations/0095_organizationquotastats_stat_type.py b/apps/product/migrations/0095_organizationquotastats_stat_type.py new file mode 100644 index 0000000..155eeff --- /dev/null +++ b/apps/product/migrations/0095_organizationquotastats_stat_type.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0 on 2025-11-17 07:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('product', '0094_organizationquotastats_remaining_amount_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='organizationquotastats', + name='stat_type', + field=models.CharField(choices=[('distribution', 'DISTRIBUTION'), ('quota', 'QUOTA')], default='distribution', max_length=150), + ), + ] diff --git a/apps/product/models.py b/apps/product/models.py index 2c68ea0..f168e2c 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -817,6 +817,10 @@ class OrganizationQuotaStats(BaseModel): total_distributed = 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=( + ('distribution', 'DISTRIBUTION'), + ('quota', 'QUOTA'), + ), default='distribution') def update_amount(self, main_quota=None): """ calculate total/sold/remaining """ diff --git a/apps/product/services/quota_stat_service.py b/apps/product/services/quota_stat_service.py index a394eed..3dbabd3 100644 --- a/apps/product/services/quota_stat_service.py +++ b/apps/product/services/quota_stat_service.py @@ -14,7 +14,8 @@ class QuotaStatsService: # ================ origin ================ assigner_stat, _ = OrganizationQuotaStats.objects.get_or_create( organization=assigner, - quota=quota + quota=quota, + stat_type='distribution' ) assigner_stat.remaining_amount -= distribution.weight @@ -24,7 +25,8 @@ class QuotaStatsService: # ============== destination ================ assigned_stat, _ = OrganizationQuotaStats.objects.get_or_create( organization=assigned, - quota=quota + quota=quota, + stat_type='distribution' ) assigned_stat.total_amount += distribution.weight @@ -51,15 +53,16 @@ class QuotaStatsService: organization=assigned, quota=quota ) - # if diff > 0 it is added to destination - assigner_stat.remaining_amount -= diff - assigner_stat.total_distributed += diff - assigner_stat.save() + if assigner_stat.stat_type == 'distribution': + # if diff > 0 it is added to destination + assigner_stat.remaining_amount -= diff + print("distributed : ", assigner_stat.total_distributed) + assigner_stat.total_distributed += diff + print(assigner_stat.total_distributed, diff) + assigner_stat.save() assigned_stat.total_amount += diff assigned_stat.remaining_amount += diff - print(assigned_stat.id) - assigned_stat.save() @staticmethod diff --git a/apps/product/signals.py b/apps/product/signals.py index 5b8872d..8ec74f9 100644 --- a/apps/product/signals.py +++ b/apps/product/signals.py @@ -309,6 +309,7 @@ def organization_quota_stats(sender, instance: Quota, created: bool, **kwargs): org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create( quota=instance, organization=instance.registerer_organization, + stat_type='quota' ) org_quota_stat.total_amount = instance.quota_weight org_quota_stat.total_distributed = instance.quota_distributed