create rancher incentive plan structure - add pos device main sheba
This commit is contained in:
@@ -27,7 +27,8 @@ def pos_organizations_sharing_information(
|
|||||||
).first().value if quota and item.broker else (
|
).first().value if quota and item.broker else (
|
||||||
item.holders_share_amount.filter(quota_distribution=distribution).first().share_amount
|
item.holders_share_amount.filter(quota_distribution=distribution).first().share_amount
|
||||||
if item.holders_share_amount.filter(quota_distribution=distribution).exists() else None
|
if item.holders_share_amount.filter(quota_distribution=distribution).exists() else None
|
||||||
)
|
),
|
||||||
|
"default_account": item.default
|
||||||
} for item in stake_holders]
|
} for item in stake_holders]
|
||||||
|
|
||||||
return sharing_information_list
|
return sharing_information_list
|
||||||
|
|||||||
@@ -53,3 +53,13 @@ class FreePOSProductUniqueCheck(APIException):
|
|||||||
status_code = status.HTTP_403_FORBIDDEN
|
status_code = status.HTTP_403_FORBIDDEN
|
||||||
default_detail = "محصول مورد نظر برای این دستگاه از قبل ثبت شده است" # noqa
|
default_detail = "محصول مورد نظر برای این دستگاه از قبل ثبت شده است" # noqa
|
||||||
default_code = 'error'
|
default_code = 'error'
|
||||||
|
|
||||||
|
|
||||||
|
class RancherIncentivePlanExists(APIException):
|
||||||
|
"""
|
||||||
|
raise exception if rancher with same info
|
||||||
|
"""
|
||||||
|
|
||||||
|
status_code = status.HTTP_403_FORBIDDEN
|
||||||
|
default_detail = "این دامدار برای این طرح با این نوع گونه دام قبلا اضافه شده است" # noqa
|
||||||
|
default_code = 'error'
|
||||||
|
|||||||
@@ -80,6 +80,11 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
|
|||||||
""" Custom output of serializer """
|
""" Custom output of serializer """
|
||||||
|
|
||||||
representation = super().to_representation(instance)
|
representation = super().to_representation(instance)
|
||||||
|
|
||||||
|
organization = self.context['organization']
|
||||||
|
rancher = self.context['rancher']
|
||||||
|
device = self.context['device']
|
||||||
|
|
||||||
if instance.quota:
|
if instance.quota:
|
||||||
representation['quota'] = {
|
representation['quota'] = {
|
||||||
'quota_identity': instance.quota.quota_id,
|
'quota_identity': instance.quota.quota_id,
|
||||||
@@ -87,7 +92,7 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
|
|||||||
'quota_livestock_allocations': quota_live_stock_allocation_info(
|
'quota_livestock_allocations': quota_live_stock_allocation_info(
|
||||||
instance.quota
|
instance.quota
|
||||||
),
|
),
|
||||||
'quota_incentive_plans': quota_incentive_plans_info(instance.quota),
|
'quota_incentive_plans': quota_incentive_plans_info(instance.quota, rancher),
|
||||||
'quota_sale_license': instance.quota.sale_license,
|
'quota_sale_license': instance.quota.sale_license,
|
||||||
'has_sale_license': instance.quota.is_in_valid_time()
|
'has_sale_license': instance.quota.is_in_valid_time()
|
||||||
}
|
}
|
||||||
@@ -101,13 +106,14 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
|
|||||||
'name': instance.quota.product.name,
|
'name': instance.quota.product.name,
|
||||||
'id': instance.quota.product.id,
|
'id': instance.quota.product.id,
|
||||||
'free_sale_for_all': sale_limitation if sale_limitation else False,
|
'free_sale_for_all': sale_limitation if sale_limitation else False,
|
||||||
'free_sale_for_this_rancher': self.context['rancher'].ignore_purchase_limit
|
'free_sale_for_this_rancher': rancher.ignore_purchase_limit
|
||||||
}
|
}
|
||||||
|
|
||||||
representation['pricing'] = { # noqa
|
representation['pricing'] = { # noqa
|
||||||
|
'main_account_sheba': organization.bank_information.first().sheba,
|
||||||
'pricing_attributes': quota_attribute_value(instance.quota),
|
'pricing_attributes': quota_attribute_value(instance.quota),
|
||||||
'sharing': pos_organizations_sharing_information(
|
'sharing': pos_organizations_sharing_information(
|
||||||
self.context['device'],
|
device,
|
||||||
instance.quota,
|
instance.quota,
|
||||||
distribution=instance
|
distribution=instance
|
||||||
),
|
),
|
||||||
@@ -127,12 +133,12 @@ class QuotaDistributionSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
if 'rancher' in self.context.keys():
|
if 'rancher' in self.context.keys():
|
||||||
# rancher herd & live stock statistics
|
# rancher herd & live stock statistics
|
||||||
livestock_counts_list, livestock_counts_dict = get_rancher_statistics(self.context['rancher'])
|
livestock_counts_list, livestock_counts_dict = get_rancher_statistics(rancher)
|
||||||
representation['rancher_statistics'] = livestock_counts_list
|
representation['rancher_statistics'] = livestock_counts_list
|
||||||
|
|
||||||
# rancher live stock statistics by quota distributions
|
# rancher live stock statistics by quota distributions
|
||||||
representation['rancher_quota_weight_statistics'] = rancher_quota_weight(
|
representation['rancher_quota_weight_statistics'] = rancher_quota_weight(
|
||||||
self.context['rancher'], distribution=instance
|
rancher, distribution=instance
|
||||||
)
|
)
|
||||||
|
|
||||||
if instance.assigned_organization:
|
if instance.assigned_organization:
|
||||||
|
|||||||
@@ -106,7 +106,11 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDev
|
|||||||
# paginate & response
|
# paginate & response
|
||||||
page = self.paginate_queryset(available_distributions) # noqa
|
page = self.paginate_queryset(available_distributions) # noqa
|
||||||
if page is not None:
|
if page is not None:
|
||||||
serializer = self.get_serializer(page, many=True, context={'rancher': rancher.first(), 'device': device})
|
serializer = self.get_serializer(page, many=True, context={
|
||||||
|
'rancher': rancher.first(),
|
||||||
|
'device': device,
|
||||||
|
'organization': organization
|
||||||
|
})
|
||||||
# set custom message for paginator
|
# set custom message for paginator
|
||||||
if not rancher:
|
if not rancher:
|
||||||
self.paginator.set_message("دامدار با کد ملی مد نظر یافت نشد") # noqa
|
self.paginator.set_message("دامدار با کد ملی مد نظر یافت نشد") # noqa
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from apps.product.models import Quota, QuotaLivestockAllocation
|
from apps.product.models import Quota, QuotaLivestockAllocation
|
||||||
from apps.warehouse.models import InventoryEntry
|
from apps.warehouse.models import InventoryEntry
|
||||||
|
from apps.herd.models import Rancher
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
|
|
||||||
@@ -32,20 +33,31 @@ def quota_live_stock_allocation_info(quota: Quota) -> typing.Any:
|
|||||||
return allocations_list
|
return allocations_list
|
||||||
|
|
||||||
|
|
||||||
def quota_incentive_plans_info(quota: Quota) -> typing.Any:
|
def quota_incentive_plans_info(quota: Quota, rancher: Rancher) -> typing.Any:
|
||||||
""" information of quota incentive plans """
|
""" information of quota incentive plans """
|
||||||
|
|
||||||
incentive_plans = quota.incentive_assignments.select_related("livestock_type", "incentive_plan")
|
incentive_plans = quota.incentive_assignments.select_related("livestock_type", "incentive_plan")
|
||||||
|
|
||||||
if incentive_plans:
|
if incentive_plans:
|
||||||
incentive_plans_list = [{
|
incentive_plans_list = []
|
||||||
'name': plan.incentive_plan.name,
|
for plan in incentive_plans:
|
||||||
'heavy_value': plan.heavy_value,
|
rancher_plan = plan.incentive_plan.rancher_plans.select_related(
|
||||||
'light_value': plan.light_value,
|
'rancher',
|
||||||
'livestock_type': plan.livestock_type.name if plan.livestock_type else "",
|
'livestock_type'
|
||||||
'livestock_weight_type': plan.livestock_type.weight_type if plan.livestock_type else "",
|
).filter(rancher=rancher, livestock_type=plan.livestock_type)
|
||||||
'quantity_kg': plan.quantity_kg
|
|
||||||
} for plan in incentive_plans]
|
incentive_plans_data = {
|
||||||
|
'name': plan.incentive_plan.name,
|
||||||
|
'heavy_value': plan.heavy_value,
|
||||||
|
'light_value': plan.light_value,
|
||||||
|
'livestock_type': plan.livestock_type.name if plan.livestock_type else "",
|
||||||
|
'livestock_weight_type': plan.livestock_type.weight_type if plan.livestock_type else "",
|
||||||
|
'quantity_kg': plan.quantity_kg,
|
||||||
|
'rancher_plan_statistic': {
|
||||||
|
f'{rancher_plan.first().livestock_type.en_name}': rancher_plan.first().allowed_quantity
|
||||||
|
} if rancher_plan else {}
|
||||||
|
}
|
||||||
|
incentive_plans_list.append(incentive_plans_data)
|
||||||
|
|
||||||
return incentive_plans_list
|
return incentive_plans_list
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ from rest_framework import serializers
|
|||||||
from apps.product import models as product_models
|
from apps.product import models as product_models
|
||||||
from apps.authorization.api.v1 import serializers as authorize_serializers
|
from apps.authorization.api.v1 import serializers as authorize_serializers
|
||||||
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer, OrganizationTypeSerializer
|
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer, OrganizationTypeSerializer
|
||||||
|
from apps.product import exceptions
|
||||||
|
|
||||||
|
|
||||||
class ProductCategorySerializer(serializers.ModelSerializer):
|
class ProductCategorySerializer(serializers.ModelSerializer): # noqa
|
||||||
class Meta:
|
class Meta:
|
||||||
model = product_models.ProductCategory
|
model = product_models.ProductCategory
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
@@ -161,3 +162,20 @@ class IncentivePlanRancherSerializer(serializers.ModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = product_models.IncentivePlanRancher
|
model = product_models.IncentivePlanRancher
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
""" validate incentive plans of a rancher """
|
||||||
|
|
||||||
|
rancher_plans_data = self.context['request'].data['data']
|
||||||
|
|
||||||
|
# check if rancher with same plan & livestock exists
|
||||||
|
if not self.instance:
|
||||||
|
for plan in rancher_plans_data:
|
||||||
|
if product_models.IncentivePlanRancher.objects.filter(
|
||||||
|
plan_id=plan['plan'],
|
||||||
|
rancher_id=plan['rancher'],
|
||||||
|
livestock_type_id=plan['livestock_type'],
|
||||||
|
).exists():
|
||||||
|
raise exceptions.RancherIncentivePlanExists()
|
||||||
|
|
||||||
|
return attrs
|
||||||
|
|||||||
@@ -446,3 +446,15 @@ class IncentivePlanViewSet(viewsets.ModelViewSet, SoftDeleteMixin, DynamicSearch
|
|||||||
class IncentivePlanRancherViewSet(viewsets.ModelViewSet, SoftDeleteMixin, DynamicSearchMixin):
|
class IncentivePlanRancherViewSet(viewsets.ModelViewSet, SoftDeleteMixin, DynamicSearchMixin):
|
||||||
queryset = product_models.IncentivePlanRancher.objects.all()
|
queryset = product_models.IncentivePlanRancher.objects.all()
|
||||||
serializer_class = product_serializers.IncentivePlanRancherSerializer
|
serializer_class = product_serializers.IncentivePlanRancherSerializer
|
||||||
|
search_fields = []
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def create(self, request, *args, **kwargs):
|
||||||
|
""" create rancher incentive plans by livestock type count """
|
||||||
|
|
||||||
|
serializer = self.serializer_class(
|
||||||
|
data=request.data['data'], many=True, context={'request': request}
|
||||||
|
)
|
||||||
|
if serializer.is_valid(raise_exception=True):
|
||||||
|
serializer.save()
|
||||||
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
|
|||||||
Reference in New Issue
Block a user