From f6f8f4c66937947b46284bcefe29c74abe716071 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Sat, 27 Sep 2025 15:49:51 +0330 Subject: [PATCH] rancher quota usage in transaction - complete --- apps/warehouse/pos/api/v1/serializers.py | 98 +++++++++++++----------- 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/apps/warehouse/pos/api/v1/serializers.py b/apps/warehouse/pos/api/v1/serializers.py index 1967205..8b553ee 100644 --- a/apps/warehouse/pos/api/v1/serializers.py +++ b/apps/warehouse/pos/api/v1/serializers.py @@ -14,7 +14,12 @@ from apps.warehouse.services.services import ( create_pre_sale ) from apps.herd.pos.api.v1.serializers import RancherSerializer -from apps.product.models import QuotaDistribution, Product, QuotaUsage +from apps.product.models import ( + QuotaDistribution, + Product, + QuotaUsage, + IncentivePlan +) from apps.warehouse import models as warehouse_models from apps.livestock.models import LiveStockType from apps.core.models import SystemConfig @@ -166,51 +171,52 @@ class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer): # create pre sale for distribution create_pre_sale(transaction=transaction, sale_item=item) - # # purchase quota usage of rancher - # if 'livestock_statistic' in item_data.keys(): - # - # # get list of livestock types object for transaction item - # livestock_types = { - # lt.en_name: lt - # for lt in LiveStockType.objects.filter( - # en_name__in=[i['name'] for i in item_data.get('livestock_statistic', [])] - # ) - # } - # - # # get list of incentive plans object for transaction item - # incentive_plans = { - # iplan.id: iplan - # for iplan in [] - # } - # - # for item in item_data['livestock_statistic']: - # # get livestock by en name - # livestock_type = livestock_types.get(item['name']) - # if not livestock_types: - # continue - # - # is_incentive = item['id'] != 0 - # # get usage & calculate - # - # usage, created = QuotaUsage.objects.get_or_create( - # rancher=rancher, - # livestock_type=livestock_type, - # distribution=distribution, - # incentive_plan=item['id'] if is_incentive else None, - # defaults={ - # "count": item['count'], - # 'usage_type': 'incentive' if is_incentive else 'base', - # 'incentive_quota_used': item['total_weight'] if is_incentive else 0, - # 'base_quota_used': item['total_weight'] if is_incentive else 0 - # } - # ) - # - # if not created: - # if usage.usage_type == 'incentive': - # usage.incentive_quota_used += item['total_weight'] - # else: - # usage.base_quota_used += item['total_weight'] - # usage.save() + # purchase quota usage of rancher + if 'livestock_statistic' in item_data.keys(): + + # get list of livestock types object for transaction item + livestock_types = { + lt.en_name: lt + for lt in LiveStockType.objects.filter( + en_name__in=[i['name'] for i in item_data.get('livestock_statistic', [])] + ) + } + + # get list of incentive plans object for transaction item + incentive_plans = { + iplan.id: iplan + for iplan in IncentivePlan.objects.all() + } + + for item in item_data['livestock_statistic']: + # get livestock by en name + livestock_type = livestock_types.get(item['name']) + if not livestock_types: + continue + + # true or false + is_incentive = item['id'] != 0 + + # get usage & calculate + usage, created = QuotaUsage.objects.get_or_create( + rancher=rancher, + livestock_type=livestock_type, + distribution=distribution, + incentive_plan=incentive_plans.get(item['id']) if is_incentive else None, + defaults={ + "count": item['count'], + 'usage_type': 'incentive' if is_incentive else 'base', + 'incentive_quota_used': item['total_weight'] if is_incentive else 0, + 'base_quota_used': item['total_weight'] if is_incentive else 0 + } + ) + + if not created: + if usage.usage_type == 'incentive': + usage.incentive_quota_used += item['total_weight'] + else: + usage.base_quota_used += item['total_weight'] + usage.save() transaction.transaction_price = total_price transaction.save()