device login
This commit is contained in:
157
apps/product/pos/api/v1/serializers/product_serializers.py
Normal file
157
apps/product/pos/api/v1/serializers/product_serializers.py
Normal file
@@ -0,0 +1,157 @@
|
||||
from rest_framework import serializers
|
||||
from apps.product import models as product_models
|
||||
from apps.authorization.api.v1 import serializers as authorize_serializers
|
||||
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer, OrganizationTypeSerializer
|
||||
|
||||
|
||||
class ProductCategorySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.ProductCategory
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class ProductSerializer(serializers.ModelSerializer):
|
||||
""" Serializer of product """
|
||||
|
||||
class Meta:
|
||||
model = product_models.Product
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom output of product serializer """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if instance.category:
|
||||
representation['category'] = {
|
||||
'id': instance.category.id,
|
||||
'name': instance.category.name
|
||||
}
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class ProductStatsSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.ProductStats
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" custom output of product stat """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
representation['product'] = {
|
||||
'id': instance.product.id,
|
||||
'name': instance.product.name
|
||||
}
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class AttributeSerializer(serializers.ModelSerializer):
|
||||
""" serialize attributes of reference product """
|
||||
|
||||
class Meta:
|
||||
model = product_models.Attribute
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.product:
|
||||
representation['product'] = {
|
||||
'id': instance.product.id,
|
||||
'name': instance.product.name
|
||||
}
|
||||
|
||||
if instance.type:
|
||||
representation['type'] = {
|
||||
'id': instance.type.id,
|
||||
'unit': instance.type.unit
|
||||
}
|
||||
return representation
|
||||
|
||||
|
||||
class AttributeValueSerializer(serializers.ModelSerializer):
|
||||
""" serialize attribute values for child products """
|
||||
|
||||
class Meta:
|
||||
model = product_models.AttributeValue
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"attribute",
|
||||
"value",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom Output of attribute values """
|
||||
representation = super().to_representation(instance)
|
||||
if instance.attribute:
|
||||
representation['attribute_name'] = instance.attribute.name
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.attribute = validated_data.get('attribute', instance.attribute)
|
||||
instance.value = validated_data.get('value', instance.value)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class BrokerSerializer(serializers.ModelSerializer):
|
||||
""" serialize product broker """
|
||||
|
||||
class Meta:
|
||||
model = product_models.Broker
|
||||
fields = '__all__'
|
||||
depth = 0
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.organization_type:
|
||||
representation['organization_type'] = OrganizationTypeSerializer(
|
||||
instance.organization_type
|
||||
).data
|
||||
|
||||
if instance.product:
|
||||
representation['product'] = ProductSerializer(
|
||||
instance.product
|
||||
).data
|
||||
|
||||
if instance.calculation_strategy:
|
||||
representation['calculation_strategy'] = {
|
||||
'id': instance.calculation_strategy.id,
|
||||
'unit': instance.calculation_strategy.unit
|
||||
}
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class SaleUnitSerializer(serializers.ModelSerializer):
|
||||
""" serialize unit of products for sale """
|
||||
|
||||
class Meta:
|
||||
model = product_models.SaleUnit
|
||||
fields = [
|
||||
"id",
|
||||
"product",
|
||||
"unit",
|
||||
"required",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if instance.product:
|
||||
representation['product'] = {
|
||||
'id': instance.product.id,
|
||||
'name': instance.product.name
|
||||
}
|
||||
return representation
|
||||
|
||||
|
||||
class IncentivePlanSerializer(serializers.ModelSerializer): # noqa
|
||||
class Meta:
|
||||
model = product_models.IncentivePlan
|
||||
fields = '__all__'
|
||||
@@ -0,0 +1,94 @@
|
||||
from rest_framework import serializers
|
||||
from apps.product import models as product_models
|
||||
from rest_framework.exceptions import APIException
|
||||
from apps.product.web.api.v1.serializers.quota_serializers import QuotaSerializer
|
||||
from django.db import models
|
||||
from apps.product.exceptions import (
|
||||
QuotaWeightException,
|
||||
QuotaClosedException,
|
||||
QuotaExpiredTimeException,
|
||||
QuotaLimitByOrganizationException
|
||||
)
|
||||
|
||||
|
||||
class QuotaDistributionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaDistribution
|
||||
fields = '__all__'
|
||||
extra_kwargs = {
|
||||
'assigner_organization': {
|
||||
'required': False
|
||||
}
|
||||
}
|
||||
|
||||
def validate(self, data):
|
||||
"""
|
||||
@ to validate if distribution weight
|
||||
more than quota weight raise exception
|
||||
@ if quota is closed raise exception
|
||||
@ if quota has organization limit, before distribution
|
||||
check assigned organization
|
||||
"""
|
||||
|
||||
quota = data['quota']
|
||||
assigned_organization = data['assigned_organization']
|
||||
amount = data['weight']
|
||||
instance_id = self.instance.id if self.instance else None
|
||||
|
||||
# check quota expired time
|
||||
if not quota.is_in_valid_time():
|
||||
raise QuotaExpiredTimeException()
|
||||
|
||||
# check if quota is closed
|
||||
if quota.is_closed:
|
||||
raise QuotaClosedException()
|
||||
|
||||
# check if assigned organization is in quota limitation
|
||||
if quota.has_organization_limit is True and assigned_organization not in quota.limit_by_organizations.all():
|
||||
raise QuotaLimitByOrganizationException()
|
||||
|
||||
# total quota distributions weight
|
||||
total = product_models.QuotaDistribution.objects.filter(
|
||||
quota=quota
|
||||
).exclude(id=instance_id).aggregate(
|
||||
total=models.Sum('weight')
|
||||
)['total'] or 0
|
||||
if total + amount > quota.quota_weight:
|
||||
raise QuotaWeightException()
|
||||
|
||||
if self.instance:
|
||||
# total warehouse inventory entry
|
||||
total_entry = self.instance.inventory_entry.aggregate(
|
||||
total=models.Sum('weight')
|
||||
)['total'] or 0
|
||||
|
||||
# if inventory entry weight is bigger than distribute weight in edit distribution
|
||||
if total_entry > self.instance.weight:
|
||||
raise APIException("وزن وارد شده کمتر از وزن ورودی به انبار است", code=403) # noqa
|
||||
|
||||
# if weight is more than distribution remaining weight
|
||||
if self.instance.weight > self.instance.remaining_weight:
|
||||
raise APIException("وزن وارد شده بیشتر از وزن باقیمانده است", code=403) # noqa
|
||||
|
||||
return data
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom output of serializer """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if instance.quota:
|
||||
representation['quota'] = QuotaSerializer(instance.quota).data
|
||||
|
||||
if instance.assigned_organization:
|
||||
representation['assigned_organization'] = {
|
||||
'organization': instance.assigned_organization.name,
|
||||
'id': instance.assigned_organization.id
|
||||
}
|
||||
|
||||
if instance.assigner_organization:
|
||||
representation['assigner_organization'] = {
|
||||
'organization': instance.assigner_organization.name,
|
||||
'id': instance.assigner_organization.id
|
||||
}
|
||||
|
||||
return representation
|
||||
237
apps/product/pos/api/v1/serializers/quota_serializers.py
Normal file
237
apps/product/pos/api/v1/serializers/quota_serializers.py
Normal file
@@ -0,0 +1,237 @@
|
||||
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer
|
||||
from apps.authorization.api.v1 import serializers as authorize_serializers
|
||||
from apps.product.web.api.v1.serializers import product_serializers
|
||||
from apps.livestock.web.api.v1.serializers import LiveStockTypeSerializer
|
||||
from apps.product import models as product_models
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class QuotaSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.Quota
|
||||
fields = '__all__'
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, product_models.Quota):
|
||||
if instance.sale_unit:
|
||||
representation['sale_unit'] = product_serializers.SaleUnitSerializer(
|
||||
instance.sale_unit
|
||||
).data
|
||||
representation['product'] = {"product": instance.product.name, "product_id": instance.product.id}
|
||||
representation['incentive_plan'] = QuotaIncentiveAssignmentSerializer(
|
||||
instance.incentive_assignments.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['attribute_values'] = product_serializers.AttributeValueSerializer(
|
||||
instance.attribute_values.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['brokers'] = QuotaBrokerValueSerializer(
|
||||
instance.broker_values.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_allocations'] = QuotaLiveStockAllocationSerializer(
|
||||
instance.livestock_allocations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['livestock_limitations'] = QuotaLiveStockAgeLimitationSerializer(
|
||||
instance.livestock_age_limitations.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
representation['limit_by_organizations'] = [
|
||||
{"name": limit.name, "id": limit.id} for limit in instance.limit_by_organizations.all()
|
||||
]
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota_id = validated_data.get('quota_id', instance.quota_id)
|
||||
instance.quota_code = validated_data.get('quota_code', instance.quota_code)
|
||||
instance.quota_weight = validated_data.get('quota_weight', instance.quota_weight)
|
||||
instance.remaining_weight = validated_data.get('remaining_weight', instance.remaining_weight)
|
||||
instance.quota_distributed = validated_data.get('quota_distributed', instance.quota_distributed)
|
||||
instance.product = validated_data.get('product', instance.product)
|
||||
instance.sale_type = validated_data.get('sale_type', instance.sale_type)
|
||||
instance.sale_unit = validated_data.get('sale_unit', instance.sale_type)
|
||||
instance.month_choices = validated_data.get('month_choices', instance.month_choices)
|
||||
instance.sale_license = validated_data.get('sale_license', instance.sale_license)
|
||||
instance.group = validated_data.get('group', instance.group)
|
||||
instance.has_distribution_limit = validated_data.get('has_distribution_limit', instance.has_distribution_limit)
|
||||
instance.distribution_mode = validated_data.get('distribution_mode', instance.distribution_mode)
|
||||
instance.has_organization_limit = validated_data.get('has_organization_limit', instance.has_organization_limit)
|
||||
instance.base_price_factory = validated_data.get('base_price_factory', instance.base_price_factory)
|
||||
instance.base_price_cooperative = validated_data.get('base_price_cooperative', instance.base_price_cooperative)
|
||||
instance.final_price = validated_data.get('final_price', instance.final_price)
|
||||
instance.is_closed = validated_data.get('is_closed', instance.is_closed)
|
||||
instance.closed_at = validated_data.get('closed_at', instance.closed_at)
|
||||
instance.save()
|
||||
|
||||
# update assigned organization many to many
|
||||
if 'assigned_organizations' in validated_data.keys():
|
||||
instance.assigned_organizations.clear()
|
||||
instance.assigned_organizations.add(
|
||||
*(validated_data.get('assigned_organizations', instance.assigned_organizations))
|
||||
)
|
||||
|
||||
# update organization limit many to many
|
||||
if 'limit_by_organizations' in validated_data.keys():
|
||||
instance.limit_by_organizations.clear()
|
||||
instance.limit_by_organizations.add(
|
||||
*(validated_data.get('limit_by_organizations', instance.limit_by_organizations))
|
||||
)
|
||||
else:
|
||||
instance.limit_by_organizations.clear()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaStatsSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaStats
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaIncentiveAssignment
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"incentive_plan",
|
||||
"heavy_value",
|
||||
"light_value",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom Output for incentive plans """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
representation['incentive_plan_name'] = instance.incentive_plan.name
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.incentive_plan = validated_data.get('incentive_plan', instance.incentive_plan)
|
||||
instance.heavy_value = validated_data.get('heavy_value', instance.heavy_value)
|
||||
instance.light_value = validated_data.get('light_value', instance.light_value)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaBrokerValueSerializer(serializers.ModelSerializer): # noqa
|
||||
class Meta:
|
||||
model = product_models.QuotaBrokerValue
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"broker",
|
||||
"value",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" Custom Output of broker values """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
representation['broker_name'] = instance.broker.name
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.broker = validated_data.get('broker', instance.broker)
|
||||
instance.value = validated_data.get('value', instance.value)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaLiveStockAllocationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaLivestockAllocation
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"livestock_group",
|
||||
"livestock_type",
|
||||
"livestock_subtype",
|
||||
"quantity_kg",
|
||||
]
|
||||
extra_kwargs = {
|
||||
'livestock_group': {
|
||||
'required': False
|
||||
},
|
||||
'livestock_type': {
|
||||
'required': False
|
||||
},
|
||||
'livestock_subtype': {
|
||||
'required': False
|
||||
}
|
||||
}
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" custom output for live stock type """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, product_models.QuotaLivestockAllocation):
|
||||
representation['livestock_type'] = LiveStockTypeSerializer(instance.livestock_type).data
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.livestock_group = validated_data.get('livestock_group', instance.livestock_group)
|
||||
instance.livestock_type = validated_data.get('livestock_type', instance.livestock_type)
|
||||
instance.livestock_subtype = validated_data.get('livestock_subtype', instance.livestock_subtype)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
class QuotaLiveStockAgeLimitationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = product_models.QuotaLiveStockAgeLimitation
|
||||
fields = [
|
||||
"id",
|
||||
"quota",
|
||||
"livestock_type",
|
||||
"livestock_subtype",
|
||||
"age_month",
|
||||
]
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" custom output for livestock type """
|
||||
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, product_models.QuotaLiveStockAgeLimitation):
|
||||
representation['livestock_type'] = LiveStockTypeSerializer(
|
||||
instance.livestock_type
|
||||
).data
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" Custom Update """
|
||||
|
||||
instance.quota = validated_data.get('quota', instance.quota)
|
||||
instance.livestock_type = validated_data.get('livestock_type', instance.livestock_type)
|
||||
instance.livestock_subtype = validated_data.get('livestock_subtype', instance.livestock_subtype)
|
||||
instance.age_month = validated_data.get('age_month', instance.age_month)
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
Reference in New Issue
Block a user