quota stat by organization - v1
This commit is contained in:
@@ -12,6 +12,7 @@ from apps.warehouse.models import (
|
||||
from common.helpers import get_organization_by_user
|
||||
from .models import (
|
||||
QuotaDistribution,
|
||||
OrganizationQuotaStats,
|
||||
Quota,
|
||||
Product,
|
||||
ProductStats,
|
||||
@@ -242,15 +243,64 @@ def update_quota_stats(instance: Quota):
|
||||
])
|
||||
|
||||
|
||||
def organization_quota_stats(quota: Quota, distribution: QuotaDistribution = None):
|
||||
"""
|
||||
set total received distributions for every organization
|
||||
"""
|
||||
if distribution:
|
||||
org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create(
|
||||
quota=quota,
|
||||
organization=distribution.assigned_organization,
|
||||
)
|
||||
|
||||
# delete distribution
|
||||
# decrease org stat total amount after remove distribution
|
||||
if distribution.trash:
|
||||
org_quota_stat.total_amount -= distribution.weight
|
||||
org_quota_stat.save(update_fields=["total_amount"])
|
||||
else:
|
||||
# if stat was created before or total amount is 0
|
||||
if not created or org_quota_stat.total_amount == 0:
|
||||
org_quota_stat.total_amount += distribution.weight
|
||||
org_quota_stat.save(update_fields=["total_amount"])
|
||||
org_quota_stat.distributions.add(distribution)
|
||||
|
||||
# prevent from maximum recursion loop
|
||||
distribution.stat_from_signal = True
|
||||
else:
|
||||
org_quota_stat, created = OrganizationQuotaStats.objects.get_or_create(
|
||||
quota=quota,
|
||||
organization=quota.registerer_organization,
|
||||
total_amount=quota.quota_weight
|
||||
)
|
||||
|
||||
# delete quota
|
||||
if quota.trash:
|
||||
org_quota_stat.soft_delete()
|
||||
|
||||
# prevent from maximum recursion loop
|
||||
quota.stat_from_signal = True
|
||||
|
||||
|
||||
@receiver([post_save, post_delete], sender=QuotaDistribution)
|
||||
@receiver([post_save, post_delete], sender=InventoryQuotaSaleTransaction)
|
||||
def update_stats_on_change(sender, instance, **kwargs):
|
||||
if sender == QuotaDistribution:
|
||||
update_product_stats(instance.quota.product, instance)
|
||||
update_quota_stats(instance.quota)
|
||||
# elif sender == InventoryQuotaSaleTransaction:
|
||||
# if instance.quota_distribution:
|
||||
# update_product_stats(instance.quota_distribution.quota.product, instance.quota_distribution)
|
||||
# update_quota_stats(instance.quota_distribution.quota)
|
||||
# else:
|
||||
# print("quota distribution is null - product app signal")
|
||||
|
||||
# if _from_signal=True prevent from maximum recursion loop
|
||||
if getattr(instance, 'stat_from_signal', False):
|
||||
return
|
||||
|
||||
organization_quota_stats(instance.quota, instance)
|
||||
|
||||
|
||||
@receiver([post_save, post_delete], sender=Quota)
|
||||
def update_quota_stats_on_change(sender, instance, **kwargs):
|
||||
if sender == Quota:
|
||||
# if _from_signal=True prevent from maximum recursion loop
|
||||
if getattr(instance, 'stat_from_signal', False):
|
||||
return
|
||||
|
||||
organization_quota_stats(instance)
|
||||
|
||||
Reference in New Issue
Block a user