import - rancher dashboard by quota

This commit is contained in:
2025-12-14 16:51:42 +03:30
parent a607bd8949
commit ca61ce4d84
3 changed files with 45 additions and 21 deletions

View File

@@ -5,7 +5,7 @@ from rest_framework.exceptions import APIException
from apps.core.models import SystemConfig
from apps.herd.services.services import rancher_quota_weight
from apps.product.models import QuotaDistribution
from apps.product.models import QuotaDistribution, OrganizationQuotaStats
from apps.warehouse.models import (
InventoryEntry,
InventoryQuotaSaleTransaction,
@@ -15,7 +15,12 @@ from apps.warehouse.models import (
)
def get_total_sold(rancher, inventory_entry: InventoryEntry = None, distribution: QuotaDistribution = None):
def get_total_sold(
rancher,
inventory_entry: InventoryEntry = None,
distribution: QuotaDistribution = None,
quota_stat: OrganizationQuotaStats = None
):
"""
"""
if inventory_entry:
@@ -34,8 +39,21 @@ def get_total_sold(rancher, inventory_entry: InventoryEntry = None, distribution
).aggregate(total=Sum('weight'))['total'] or 0
)
elif quota_stat:
return (
InventoryQuotaSaleItem.objects.filter(
quota_stat=quota_stat,
transaction__rancher=rancher
).aggregate(total=Sum('weight'))['total'] or 0
)
def can_buy_from_inventory(rancher, inventory_entry: InventoryEntry = None, distribution: QuotaDistribution = None):
def can_buy_from_inventory(
rancher,
inventory_entry: InventoryEntry = None,
distribution: QuotaDistribution = None,
quota_stat: OrganizationQuotaStats = None
):
"""
"""
if SystemConfig.get("IGNORE_ALL_RANCHER_PURCHASE_LIMITS") == "true":
@@ -58,6 +76,14 @@ def can_buy_from_inventory(rancher, inventory_entry: InventoryEntry = None, dist
else:
return False
elif quota_stat:
if quota_stat.quota.is_in_valid_time():
quota_weight = rancher_quota_weight(
rancher, quota=quota_stat.quota, quota_stat=quota_stat
) # {total, by_type}
else:
return False
total_allowed = quota_weight['total'] # noqa
total_sold = get_total_sold(inventory_entry, rancher)