changes in device assignment - incentive plan structure in quota serializer
This commit is contained in:
@@ -172,45 +172,49 @@ class DeviceAssignmentViewSet(viewsets.ModelViewSet, SoftDeleteMixin):
|
|||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
""" assign pos device to client by company """
|
""" assign pos device to client by company """
|
||||||
|
|
||||||
|
data = request.data.copy()
|
||||||
|
|
||||||
if 'organization' not in request.data.keys():
|
if 'organization' not in request.data.keys():
|
||||||
organization = get_organization_by_user(request.user)
|
organization = get_organization_by_user(request.user)
|
||||||
request.data.update({'organization': organization.id})
|
data['organization'] = organization.id
|
||||||
|
|
||||||
if 'client_data' in request.data.keys():
|
client_id = None
|
||||||
|
client_data = data.get('client_data')
|
||||||
|
|
||||||
# if client will be an organization
|
if client_data and client_data.get('is_organization'):
|
||||||
if request.data['client_data']['is_organization']:
|
org_id = client_data.get('organization')
|
||||||
|
|
||||||
# check if organization have bank account or raise exception
|
# check if organization have bank account or raise exception
|
||||||
if not BankAccountInformation.objects.filter(
|
if not BankAccountInformation.objects.filter(organization_id=org_id).exists():
|
||||||
organization_id=request.data['client_data']['organization']
|
raise OrganizationBankAccountException()
|
||||||
).exists():
|
|
||||||
raise OrganizationBankAccountException()
|
|
||||||
|
|
||||||
# check if organization is a client before
|
# check if organization is a client before
|
||||||
client = pos_models.POSClient.objects.filter(
|
client = pos_models.POSClient.objects.filter(organization_id=org_id)
|
||||||
organization_id=request.data['client_data']['organization']
|
if client.exists():
|
||||||
|
client_id = client.first().id
|
||||||
|
else:
|
||||||
|
# create client
|
||||||
|
client = CustomOperations().custom_create(
|
||||||
|
request=request,
|
||||||
|
view=POSClientViewSet(),
|
||||||
|
data=request.data['client_data']
|
||||||
)
|
)
|
||||||
if client.exists():
|
client_id = client['id']
|
||||||
request.data.update({'client': client.first().id})
|
|
||||||
|
|
||||||
# create client
|
data['client'] = client_id
|
||||||
client = CustomOperations().custom_create(
|
|
||||||
request=request,
|
elif client_data and client_data.get('organization') is False:
|
||||||
view=POSClientViewSet(),
|
pass
|
||||||
data=request.data['client_data']
|
|
||||||
)
|
|
||||||
request.data.update({'client': client['id']})
|
|
||||||
|
|
||||||
# create assignment
|
# create assignment
|
||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
assignment = serializer.save()
|
assignment = serializer.save()
|
||||||
|
|
||||||
# set device status to assigned
|
# set device status to assigned
|
||||||
assignment.device.assigned_state = True
|
assignment.device.assigned_state = True
|
||||||
assignment.device.acceptor = request.data['device_acceptor']
|
assignment.device.acceptor = data['device_acceptor']
|
||||||
assignment.device.terminal = request.data['device_terminal']
|
assignment.device.terminal = data['device_terminal']
|
||||||
if not assignment.device.password:
|
if not assignment.device.password:
|
||||||
assignment.device.password = ''.join(random.choices(string.digits, k=6))
|
assignment.device.password = ''.join(random.choices(string.digits, k=6))
|
||||||
assignment.device.save()
|
assignment.device.save()
|
||||||
|
|||||||
@@ -106,8 +106,8 @@ class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer):
|
|||||||
"id",
|
"id",
|
||||||
"quota",
|
"quota",
|
||||||
"incentive_plan",
|
"incentive_plan",
|
||||||
"heavy_value",
|
"livestock_type",
|
||||||
"light_value",
|
"quantity_kg",
|
||||||
]
|
]
|
||||||
|
|
||||||
def to_representation(self, instance):
|
def to_representation(self, instance):
|
||||||
@@ -115,6 +115,11 @@ class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
representation = super().to_representation(instance)
|
representation = super().to_representation(instance)
|
||||||
representation['incentive_plan_name'] = instance.incentive_plan.name
|
representation['incentive_plan_name'] = instance.incentive_plan.name
|
||||||
|
if instance.livestock_type:
|
||||||
|
representation['livestock_type'] = {
|
||||||
|
'name': instance.livestock_type.name,
|
||||||
|
'id': instance.livestock_type.id
|
||||||
|
}
|
||||||
|
|
||||||
return representation
|
return representation
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user