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

@@ -474,6 +474,7 @@ class Quota(BaseModel):
"remaining_weight": stat.first().remaining_amount if stat.exists() else 0,
"quota_distributed": stat.first().total_distributed if stat.exists() else 0,
"been_sold": stat.first().sold_amount if stat.exists() else 0,
"inventory_received": stat.first().inventory_received if stat.exists() else 0,
}
def soft_delete(self):

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

View File

@@ -33,6 +33,7 @@ class QuotaSerializer(serializers.ModelSerializer):
representation['quota_distributed'] = quota_weight_by_org['quota_distributed']
representation['remaining_weight'] = quota_weight_by_org['remaining_weight']
representation['been_sold'] = quota_weight_by_org['been_sold']
representation['inventory_received'] = quota_weight_by_org['inventory_received']
representation['distributions_number_by_me'] = instance.distributions_assigned.filter(
assigner_organization=org
).count()