fix - change base of pre sale / extra sale on quota stat & total purchase of rancher in Quota usage calculation

This commit is contained in:
2025-11-26 15:50:35 +03:30
parent 817f26519d
commit 65bdb539bc
7 changed files with 51 additions and 26 deletions

View File

@@ -4,11 +4,11 @@ from django.db.models import Sum, functions, Value
class RancherService:
@staticmethod
def get_total_used_weight(rancher, sale_item, distribution):
def get_total_used_weight(rancher, sale_item, quota_stat):
return sale_item.objects.filter(
transaction__rancher=rancher,
transaction__transaction_status='success',
quota_distribution=distribution,
quota_stat=quota_stat,
).aggregate(
total_weight=functions.Coalesce(Sum('weight'), Value(0))
)['total_weight']

View File

@@ -7,7 +7,7 @@ from django.db.models.functions import Coalesce
from apps.herd.models import Rancher
from apps.herd.services.rancher_service import RancherService
from apps.livestock.models import LiveStock, TemporaryLiveStock
from apps.product.models import Quota, QuotaDistribution
from apps.product.models import Quota, QuotaDistribution, OrganizationQuotaStats
from apps.warehouse.models import (
InventoryEntry,
InventoryQuotaSaleItem
@@ -82,7 +82,8 @@ def rancher_quota_weight(
rancher: Rancher,
inventory_entry: InventoryEntry = None,
distribution: QuotaDistribution = None,
quota: Quota = None
quota: Quota = None,
quota_stat: OrganizationQuotaStats = None
):
"""
:param rancher: Rancher instance
@@ -154,20 +155,26 @@ def rancher_quota_weight(
total_weight += rancher_plan_weight
print(total_weight)
# get rancher remaining usage of quota for purchase
rancher_remaining_usage = RancherService.get_total_used_weight(rancher, InventoryQuotaSaleItem, distribution)
rancher_remaining_usage = RancherService.get_total_used_weight(
rancher,
InventoryQuotaSaleItem,
quota_stat=quota_stat
)
if total_weight - rancher_remaining_usage < 0:
remaining_weight = 0
free_sale = rancher_remaining_usage - total_weight
total_purchase = free_sale + total_weight
else:
remaining_weight = total_weight - rancher_remaining_usage
free_sale = 0
total_purchase = total_weight - remaining_weight
return {
"total_weight": total_weight,
"remaining_weight": remaining_weight,
'free_sale': free_sale,
'total_purchase': 0,
'total_purchase': total_purchase,
"rancher_temporary_livestock": rancher.without_herd,
"by_type": [{
"name": key,