diff --git a/apps/herd/services/rancher_dashboard_service.py b/apps/herd/services/rancher_dashboard_service.py index 6a13745..c9aeb63 100644 --- a/apps/herd/services/rancher_dashboard_service.py +++ b/apps/herd/services/rancher_dashboard_service.py @@ -5,6 +5,7 @@ from apps.herd.models import Rancher, Herd from apps.herd.services.services import rancher_quota_weight from apps.product.models import OrganizationQuotaStats from apps.warehouse.models import InventoryQuotaSaleItem +from apps.warehouse.services.services import can_buy_from_inventory class RancherDashboardService: @@ -78,19 +79,17 @@ class RancherDashboardService: 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 - 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 = [] - 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.update({'product': stat.quota.product.name}) rancher_data_by_quota_usage.append(rancher_quota_data) diff --git a/apps/product/pos/api/v1/viewsets/quota_api.py b/apps/product/pos/api/v1/viewsets/quota_api.py index a03fa2c..eb2c1b0 100644 --- a/apps/product/pos/api/v1/viewsets/quota_api.py +++ b/apps/product/pos/api/v1/viewsets/quota_api.py @@ -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.models import OrganizationQuotaStats 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 @@ -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) ) ).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 page = self.paginate_queryset(available_distributions) # noqa diff --git a/apps/warehouse/services/services.py b/apps/warehouse/services/services.py index b6a3c3f..82db6d5 100644 --- a/apps/warehouse/services/services.py +++ b/apps/warehouse/services/services.py @@ -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)