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

@@ -7,7 +7,7 @@ ENV_NAME=DEV
# Database secrets # Database secrets
DB_HOST=31.7.78.133 DB_HOST=31.7.78.133
DB_PORT=14352 DB_PORT=14352
DB_NAME=Development DB_NAME=Production
DB_USERNAME=postgres DB_USERNAME=postgres
DB_PASSWORD=pfLIVXupbDetvFMt2gUvxLXUL9b4HIOHaPcKXsBEZ1i8zl0iLUjmhUfXlGfJKcTV DB_PASSWORD=pfLIVXupbDetvFMt2gUvxLXUL9b4HIOHaPcKXsBEZ1i8zl0iLUjmhUfXlGfJKcTV

View File

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

View File

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