fix - tarnsaction validation of pre sale & free sale / calculate weight in rancher statistic by rancher activity

This commit is contained in:
2025-11-11 09:50:19 +03:30
parent 3edcd9aa1c
commit f8bbda203f
3 changed files with 40 additions and 31 deletions

View File

@@ -1,16 +1,23 @@
from apps.livestock.models import LiveStock, TemporaryLiveStock
from decimal import Decimal
import typing
from django.db.models import Count, Q, Value
from django.db.models import Sum
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.warehouse.models import (
InventoryEntry,
InventoryQuotaSaleItem
)
from django.db.models import Sum
from apps.product.models import Quota, QuotaDistribution
from apps.herd.services.rancher_service import RancherService
from django.db.models import Count, Q, Value
from django.db.models.functions import Coalesce
import typing
LIVESTOCK_GROPES = {
'I': 'industrial', # صنعتی
'V': 'rural', # روستایی
'N': 'nomadic' # عشایری
}
def get_rancher_statistics(rancher: Rancher = None) -> typing.Any:
@@ -87,22 +94,23 @@ def rancher_quota_weight(
# calculate quota base weight by livestock type & base total weight
for item in allocations: # noqa
if item.livestock_type:
animal_type_fa = item.livestock_type.name
animal_type_en = item.livestock_type.en_name
per_head = item.quantity_kg
count = livestock_counts_dict.get(live_stock_meta.get(animal_type_fa), 0)
if rancher.activity and LIVESTOCK_GROPES[rancher.activity] == item.livestock_group:
animal_type_fa = item.livestock_type.name
animal_type_en = item.livestock_type.en_name
per_head = item.quantity_kg
count = livestock_counts_dict.get(live_stock_meta.get(animal_type_fa), 0)
weight = per_head * count
total_weight += weight
weight = per_head * count
total_weight += weight
if animal_type_en not in merged:
merged[animal_type_en] = {
"name_fa": animal_type_fa,
"weight": weight,
"type": item.livestock_type.weight_type
}
else:
merged[animal_type_en]['weight'] += weight
if animal_type_en not in merged:
merged[animal_type_en] = {
"name_fa": animal_type_fa,
"weight": weight,
"type": item.livestock_type.weight_type
}
else:
merged[animal_type_en]['weight'] += weight
# calculate rancher incentive plans weight by livestock type & add it to total_weight
for item in incentive_plans:

View File

@@ -252,15 +252,16 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer):
if 'quota_distribution' in item.keys():
distribution = QuotaDistribution.objects.get(id=item.get('quota_distribution'))
# if quota has not been in sale time
if not distribution.quota.is_in_sale_licence_time():
raise QuotaSaleTimeException()
total_sale_weight = distribution.sale_items.aggregate(
total=models.Sum('weight')
)['total'] or 0
if not distribution.pre_sale and not distribution.free_sale:
# if quota has not been in sale time
if not distribution.quota.is_in_sale_licence_time():
raise QuotaSaleTimeException()
total_sale_weight = distribution.sale_items.aggregate(
total=models.Sum('weight')
)['total'] or 0
if total_sale_weight + item.get('weight') > distribution.weight:
raise DistributionWeightException()
if total_sale_weight + item.get('weight') > distribution.weight:
raise DistributionWeightException()
return attrs