add - whole system of inventory entry in organization quotas stat

This commit is contained in:
2025-11-19 15:56:15 +03:30
parent 42c01f3eb5
commit 1bf6950ccb
9 changed files with 152 additions and 34 deletions

View File

@@ -1,5 +1,6 @@
from apps.product.models import QuotaDistribution, OrganizationQuotaStats
from apps.product.validators.quota_stats_validator import QuotaStatsValidator
from apps.warehouse.models import InventoryEntry
class QuotaStatsService:
@@ -102,3 +103,53 @@ class QuotaStatsService:
assigned_stat.remaining_amount -= distribution.weight
assigned_stat.distributions.remove(distribution)
assigned_stat.save()
@staticmethod
def apply_inventory_entry(entry: InventoryEntry, created):
quota = entry.quota
weight = entry.weight
if not created:
old_weight = entry._old_weight # noqa
diff = weight - old_weight
else:
diff = weight
print("0000", diff)
QuotaStatsService._propagate_inventory(
organization=entry.organization,
quota=quota,
diff=diff
)
@staticmethod
def remove_inventory_entry(entry: InventoryEntry):
quota = entry.quota
weight = entry.weight
QuotaStatsService._propagate_inventory(
organization=entry.organization,
quota=quota,
diff=-weight
)
@staticmethod
def _propagate_inventory(organization, quota, diff):
org = organization
while org:
stat = OrganizationQuotaStats.objects.filter(
organization=org,
quota=quota
).first()
if stat:
print(stat.id)
print(org.id, quota.id)
stat.inventory_received = (stat.inventory_received or 0) + diff
print(stat.remaining_amount)
stat.remaining_amount = stat.remaining_amount - diff
if stat.inventory_received < 0:
stat.inventory_received = 0
stat.save(update_fields=['inventory_received', 'remaining_amount'])
org = org.parent_organization