change list of rancher inventory entries to rancher distributions for one item be as sale in pos device

This commit is contained in:
2025-09-06 12:04:41 +03:30
parent 272e9ed2c8
commit 18a7955e3a
5 changed files with 151 additions and 50 deletions

View File

@@ -1,21 +1,31 @@
from apps.warehouse.models import InventoryEntry, InventoryQuotaSaleTransaction
from apps.herd.services.services import rancher_quota_weight, get_rancher_statistics
from apps.warehouse.models import InventoryEntry, InventoryQuotaSaleTransaction
from apps.product.models import QuotaDistribution
from apps.core.models import SystemConfig
from django.db.models import Sum
def get_total_sold(inventory_entry, rancher):
def get_total_sold(rancher, inventory_entry: InventoryEntry = None, distribution: QuotaDistribution = None):
"""
"""
return (
InventoryQuotaSaleTransaction.objects.filter(
inventory_entry=inventory_entry,
rancher=rancher
).aggregate(total=Sum('weight'))['total'] or 0
)
if inventory_entry:
return (
InventoryQuotaSaleTransaction.objects.filter(
inventory_entry=inventory_entry,
rancher=rancher
).aggregate(total=Sum('weight'))['total'] or 0
)
elif distribution:
return (
InventoryQuotaSaleTransaction.objects.filter(
inventory_entry=inventory_entry,
rancher=rancher
).aggregate(total=Sum('weight'))['total'] or 0
)
def can_buy_from_inventory(rancher, inventory_entry: InventoryEntry):
def can_buy_from_inventory(rancher, inventory_entry: InventoryEntry = None, distribution: QuotaDistribution = None):
"""
"""
if SystemConfig.get("IGNORE_ALL_RANCHER_PURCHASE_LIMITS") == "true":
@@ -24,8 +34,21 @@ def can_buy_from_inventory(rancher, inventory_entry: InventoryEntry):
if rancher.ignore_purchase_limit:
return True
quota_weight = rancher_quota_weight(rancher, inventory_entry) # {total, by_type}
total_allowed = quota_weight['total']
if inventory_entry:
# check if quota is open and acceptable to sale
if inventory_entry.distribution.quota.is_in_valid_time():
quota_weight = rancher_quota_weight(rancher, inventory_entry=inventory_entry) # {total, by_type}
else:
return False
elif distribution:
# check if quota is open and acceptable to sale
if distribution.quota.is_in_valid_time():
quota_weight = rancher_quota_weight(rancher, distribution=distribution) # {total, by_type}
else:
return False
total_allowed = quota_weight['total'] # noqa
total_sold = get_total_sold(inventory_entry, rancher)