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,6 +5,7 @@ from apps.herd.models import Rancher, Herd
from apps.herd.services.services import rancher_quota_weight from apps.herd.services.services import rancher_quota_weight
from apps.product.models import OrganizationQuotaStats from apps.product.models import OrganizationQuotaStats
from apps.warehouse.models import InventoryQuotaSaleItem from apps.warehouse.models import InventoryQuotaSaleItem
from apps.warehouse.services.services import can_buy_from_inventory
class RancherDashboardService: class RancherDashboardService:
@@ -78,19 +79,17 @@ class RancherDashboardService:
get rancher dashboard by quota usage get rancher dashboard by quota usage
""" """
# get rancher transaction items
transaction_sale_items = InventoryQuotaSaleItem.objects.select_related(
'transaction', 'quota_stat'
).filter(
transaction__rancher=rancher,
# transaction__transaction_status='success',
).distinct('quota_stat').values_list('quota_stat')
# get organization quota stats of transaction item # get organization quota stats of transaction item
quota_stat = OrganizationQuotaStats.objects.filter(id__in=transaction_sale_items) quota_stat = OrganizationQuotaStats.objects.filter(stat_type='quota', quota__is_closed=False)
available_stats = [
stat for stat in quota_stat if (
can_buy_from_inventory(rancher, quota_stat=stat) and rancher is not None
)
]
rancher_data_by_quota_usage = [] rancher_data_by_quota_usage = []
for stat in quota_stat: for stat in available_stats:
rancher_quota_data = rancher_quota_weight(rancher=rancher, quota=stat.quota, quota_stat=stat) rancher_quota_data = rancher_quota_weight(rancher=rancher, quota=stat.quota, quota_stat=stat)
rancher_quota_data.update({'product': stat.quota.product.name}) rancher_quota_data.update({'product': stat.quota.product.name})
rancher_data_by_quota_usage.append(rancher_quota_data) rancher_data_by_quota_usage.append(rancher_quota_data)

View File

@@ -14,6 +14,7 @@ from apps.pos_device.mixins.pos_device_mixin import POSDeviceMixin
from apps.product import models as product_models from apps.product import models as product_models
from apps.product.models import OrganizationQuotaStats from apps.product.models import OrganizationQuotaStats
from apps.product.pos.api.v1.serializers import quota_serializers from apps.product.pos.api.v1.serializers import quota_serializers
from apps.warehouse.services.services import can_buy_from_inventory
def trash(queryset, pk): # noqa def trash(queryset, pk): # noqa
@@ -232,15 +233,13 @@ class OrganizationQuotaStatsViewSet(viewsets.ModelViewSet, DynamicSearchMixin, P
Q(quota__pre_sale=True) | Q(quota__free_sale=True) | Q(inventory_received__gt=0) Q(quota__pre_sale=True) | Q(quota__free_sale=True) | Q(inventory_received__gt=0)
) )
).order_by('-create_date') ).order_by('-create_date')
print(quotas)
# check quota distributions for rancher
# available_distributions = [
# distribution for distribution in distributions if (
# can_buy_from_inventory(rancher.first(), distribution=distribution) & rancher.exists()
# )
# ]
available_distributions = quotas # check quota distributions for rancher
available_distributions = [
stat for stat in quotas if (
can_buy_from_inventory(rancher.first(), quota_stat=stat) & rancher.exists()
)
]
# paginate & response # paginate & response
page = self.paginate_queryset(available_distributions) # noqa page = self.paginate_queryset(available_distributions) # noqa

View File

@@ -5,7 +5,7 @@ from rest_framework.exceptions import APIException
from apps.core.models import SystemConfig from apps.core.models import SystemConfig
from apps.herd.services.services import rancher_quota_weight 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 ( from apps.warehouse.models import (
InventoryEntry, InventoryEntry,
InventoryQuotaSaleTransaction, 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: if inventory_entry:
@@ -34,8 +39,21 @@ def get_total_sold(rancher, inventory_entry: InventoryEntry = None, distribution
).aggregate(total=Sum('weight'))['total'] or 0 ).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": 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: else:
return False 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_allowed = quota_weight['total'] # noqa
total_sold = get_total_sold(inventory_entry, rancher) total_sold = get_total_sold(inventory_entry, rancher)