diff --git a/apps/pos_device/web/api/v1/viewsets/device.py b/apps/pos_device/web/api/v1/viewsets/device.py index 74d9abb..e634831 100644 --- a/apps/pos_device/web/api/v1/viewsets/device.py +++ b/apps/pos_device/web/api/v1/viewsets/device.py @@ -172,45 +172,49 @@ class DeviceAssignmentViewSet(viewsets.ModelViewSet, SoftDeleteMixin): def create(self, request, *args, **kwargs): """ assign pos device to client by company """ + data = request.data.copy() + if 'organization' not in request.data.keys(): 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 request.data['client_data']['is_organization']: + if client_data and client_data.get('is_organization'): + org_id = client_data.get('organization') - # check if organization have bank account or raise exception - if not BankAccountInformation.objects.filter( - organization_id=request.data['client_data']['organization'] - ).exists(): - raise OrganizationBankAccountException() + # check if organization have bank account or raise exception + if not BankAccountInformation.objects.filter(organization_id=org_id).exists(): + raise OrganizationBankAccountException() - # check if organization is a client before - client = pos_models.POSClient.objects.filter( - organization_id=request.data['client_data']['organization'] + # check if organization is a client before + client = pos_models.POSClient.objects.filter(organization_id=org_id) + 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(): - request.data.update({'client': client.first().id}) + client_id = client['id'] - # create client - client = CustomOperations().custom_create( - request=request, - view=POSClientViewSet(), - data=request.data['client_data'] - ) - request.data.update({'client': client['id']}) + data['client'] = client_id + + elif client_data and client_data.get('organization') is False: + pass # create assignment - serializer = self.serializer_class(data=request.data) + serializer = self.serializer_class(data=data) if serializer.is_valid(): assignment = serializer.save() # set device status to assigned assignment.device.assigned_state = True - assignment.device.acceptor = request.data['device_acceptor'] - assignment.device.terminal = request.data['device_terminal'] + assignment.device.acceptor = data['device_acceptor'] + assignment.device.terminal = data['device_terminal'] if not assignment.device.password: assignment.device.password = ''.join(random.choices(string.digits, k=6)) assignment.device.save() diff --git a/apps/product/web/api/v1/serializers/quota_serializers.py b/apps/product/web/api/v1/serializers/quota_serializers.py index 92b8922..b16cd25 100644 --- a/apps/product/web/api/v1/serializers/quota_serializers.py +++ b/apps/product/web/api/v1/serializers/quota_serializers.py @@ -106,8 +106,8 @@ class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer): "id", "quota", "incentive_plan", - "heavy_value", - "light_value", + "livestock_type", + "quantity_kg", ] def to_representation(self, instance): @@ -115,6 +115,11 @@ class QuotaIncentiveAssignmentSerializer(serializers.ModelSerializer): representation = super().to_representation(instance) 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