Ejemplo n.º 1
0
    def post(self, request, **kwargs):
        """
        Post of NetSuite Credentials
        """
        try:
            ns_account_id = request.data.get('ns_account_id')
            ns_consumer_key = settings.NS_CONSUMER_KEY
            ns_consumer_secret = settings.NS_CONSUMER_SECRET
            ns_token_key = request.data.get('ns_token_id')
            ns_token_secret = request.data.get('ns_token_secret')

            workspace = Workspace.objects.get(pk=kwargs['workspace_id'])

            netsuite_credentials = NetSuiteCredentials.objects.filter(
                workspace=workspace).first()

            connection = NetSuiteConnection(ns_account_id, ns_consumer_key,
                                            ns_consumer_secret, ns_token_key,
                                            ns_token_secret)
            accounts = connection.accounts.get_all_generator(1)

            if not netsuite_credentials or not accounts:
                if workspace.ns_account_id:
                    assert_valid(ns_account_id == workspace.ns_account_id,
                                 'Please choose the correct NetSuite account')
                netsuite_credentials = NetSuiteCredentials.objects.create(
                    ns_account_id=ns_account_id,
                    ns_consumer_key=ns_consumer_key,
                    ns_consumer_secret=ns_consumer_secret,
                    ns_token_id=ns_token_key,
                    ns_token_secret=ns_token_secret,
                    workspace=workspace)
                workspace.ns_account_id = ns_account_id
                workspace.save()

            else:
                assert_valid(
                    ns_account_id == netsuite_credentials.ns_account_id,
                    'Please choose the correct NetSuite online account')
                netsuite_credentials.ns_account_id = ns_account_id
                netsuite_credentials.ns_consumer_key = ns_consumer_key
                netsuite_credentials.ns_consumer_secret = ns_consumer_secret
                netsuite_credentials.ns_token_id = ns_token_key
                netsuite_credentials.ns_token_secret = ns_token_secret

                netsuite_credentials.save()

            return Response(
                data=NetSuiteCredentialSerializer(netsuite_credentials).data,
                status=status.HTTP_200_OK)
        except Exception:
            return Response({'message': 'Invalid Login Attempt'},
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        """
        Get task logs by ids
        """
        task_log_ids = self.request.query_params.getlist('id', [])

        assert_valid(task_log_ids != [], 'task log ids not found')

        task_logs = TaskLog.objects.filter(id__in=task_log_ids).all()

        return Response(data=self.serializer_class(task_logs, many=True).data,
                        status=status.HTTP_200_OK)
Ejemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        """
        Post General mapping view
        """
        general_mapping_payload = request.data

        assert_valid(general_mapping_payload is not None,
                     'Request body is empty')

        mapping_utils = MappingUtils(kwargs['workspace_id'])
        general_mapping_object = mapping_utils.create_or_update_general_mapping(
            general_mapping_payload)

        return Response(
            data=self.serializer_class(general_mapping_object).data,
            status=status.HTTP_200_OK)
Ejemplo n.º 4
0
    def post(self, request, **kwargs):
        """
        Post of Fyle Credentials
        """
        try:
            authorization_code = request.data.get('code')

            workspace = Workspace.objects.get(id=kwargs['workspace_id'])

            refresh_token = auth_utils.generate_fyle_refresh_token(
                authorization_code)['refresh_token']
            fyle_user = auth_utils.get_fyle_user(refresh_token)
            org_id = fyle_user['org_id']
            org_name = fyle_user['org_name']

            assert_valid(
                workspace.fyle_org_id and workspace.fyle_org_id == org_id,
                'Please select the correct Fyle account - {0}'.format(
                    workspace.name))

            workspace.name = org_name
            workspace.fyle_org_id = org_id
            workspace.save()

            fyle_credentials, _ = FyleCredential.objects.update_or_create(
                workspace_id=kwargs['workspace_id'],
                defaults={
                    'refresh_token': refresh_token,
                })

            return Response(
                data=FyleCredentialSerializer(fyle_credentials).data,
                status=status.HTTP_200_OK)
        except fyle_exc.UnauthorizedClientError:
            return Response({'message': 'Invalid Authorization Code'},
                            status=status.HTTP_403_FORBIDDEN)
        except fyle_exc.NotFoundClientError:
            return Response({'message': 'Fyle Application not found'},
                            status=status.HTTP_404_NOT_FOUND)
        except fyle_exc.WrongParamsError:
            return Response({'message': 'Some of the parameters are wrong'},
                            status=status.HTTP_400_BAD_REQUEST)
        except fyle_exc.InternalServerError:
            return Response({'message': 'Wrong/Expired Authorization code'},
                            status=status.HTTP_401_UNAUTHORIZED)
Ejemplo n.º 5
0
    def post(self, request, **kwargs):
        """
        Post Settings
        """
        schedule_enabled = request.data.get('schedule_enabled')
        assert_valid(schedule_enabled is not None,
                     'Schedule enabled cannot be null')

        hours = request.data.get('hours')
        assert_valid(hours is not None, 'Hours cannot be left empty')

        workspace_schedule_settings = schedule_sync(
            workspace_id=kwargs['workspace_id'],
            schedule_enabled=schedule_enabled,
            hours=hours)

        return Response(
            data=WorkspaceScheduleSerializer(workspace_schedule_settings).data,
            status=status.HTTP_200_OK)
Ejemplo n.º 6
0
    def create_or_update_subsidiary_mapping(self, subsidiary_mapping: Dict):
        """
        Create or update Subsidiary mappings
        :param subsidiary_mapping: project mapping payload
        :return: subsidiary mappings objects
        """

        assert_valid('subsidiary_name' in subsidiary_mapping and subsidiary_mapping['subsidiary_name'],
                     'subsidiary name field is blank')
        assert_valid('internal_id' in subsidiary_mapping and subsidiary_mapping['internal_id'],
                     'internal id field is blank')

        subsidiary_mapping_object, _ = SubsidiaryMapping.objects.update_or_create(
            workspace_id=self.__workspace_id,
            defaults={
                'subsidiary_name': subsidiary_mapping['subsidiary_name'],
                'internal_id': subsidiary_mapping['internal_id']
            }
        )

        return subsidiary_mapping_object
Ejemplo n.º 7
0
    def post(self, request, *args, **kwargs):
        """
        Validate Custom List from NetSuite
        """
        try:
            segment_type = request.data.get('segment_type')
            script_id = request.data.get('script_id')
            internal_id = request.data.get('internal_id')

            assert_valid(segment_type is not None, 'Segment type not found')
            assert_valid(script_id is not None, 'Script ID not found')
            assert_valid(internal_id is not None, 'Internal ID not found')

            ns_credentials = NetSuiteCredentials.objects.get(
                workspace_id=kwargs['workspace_id'])
            ns_connector = NetSuiteConnector(
                ns_credentials, workspace_id=kwargs['workspace_id'])

            if segment_type == 'CUSTOM_LIST':
                custom_list = ns_connector.connection.custom_lists.get(
                    internal_id)

                CustomSegment.objects.update_or_create(
                    workspace_id=kwargs['workspace_id'],
                    internal_id=internal_id,
                    defaults={
                        'name': custom_list['name'].upper().replace(' ', '_'),
                        'script_id': script_id,
                        'segment_type': segment_type
                    })

            elif segment_type == 'CUSTOM_RECORD':
                custom_record = ns_connector.connection.custom_records.get_all_by_id(
                    internal_id)

                CustomSegment.objects.update_or_create(
                    workspace_id=kwargs['workspace_id'],
                    internal_id=internal_id,
                    defaults={
                        'name':
                        custom_record[0]['recType']['name'].upper().replace(
                            ' ', '_'),
                        'script_id':
                        script_id,
                        'segment_type':
                        segment_type
                    })

            return Response(status=status.HTTP_200_OK)

        except NetSuiteRequestError as exception:
            logger.exception({'error': exception})
            detail = json.dumps(exception.__dict__)
            detail = json.loads(detail)

            return Response(data={
                'message':
                '{0} - {1}'.format(detail['code'], detail['message'])
            },
                            status=status.HTTP_401_UNAUTHORIZED)

        except Exception as e:
            logger.exception(e)
            detail = json.dumps(e.__dict__)
            detail = json.loads(detail)

            return Response(data={
                'message':
                '{0} - {1}'.format(detail['code'], detail['message'])
            },
                            status=status.HTTP_401_UNAUTHORIZED)
Ejemplo n.º 8
0
    def create_or_update_general_mapping(self, general_mapping: Dict):
        """
        Create or update General mappings
        :param general_mapping: project mapping payload
        :return: general mappings objects
        """

        configuration = Configuration.objects.get(workspace_id=self.__workspace_id)

        assert_valid('location_id' in general_mapping, 'location id field is blank')
        assert_valid('location_name' in general_mapping, 'location name field is blank')
        assert_valid('location_level' in general_mapping, 'location level field is blank')

        if general_mapping['location_id'] and general_mapping['location_name']:
            assert_valid(general_mapping['location_level'] is not None, 'location level field is blank')

        params = {
            'location_name': general_mapping['location_name'],
            'location_id': general_mapping['location_id'],
            'location_level': general_mapping['location_level'],
            'accounts_payable_name': None,
            'accounts_payable_id': None,
            'reimbursable_account_name': None,
            'reimbursable_account_id': None,
            'default_ccc_account_name': None,
            'default_ccc_account_id': None,
            'default_ccc_vendor_name': None,
            'default_ccc_vendor_id': None
        }

        mapping_setting = MappingSetting.objects.filter(
            Q(destination_field='VENDOR') | Q(destination_field='EMPLOYEE'),
            source_field='EMPLOYEE', workspace_id=self.__workspace_id
        ).first()

        if mapping_setting.destination_field == 'VENDOR' or\
                configuration.corporate_credit_card_expenses_object == 'BILL':
            assert_valid('accounts_payable_name' in general_mapping and general_mapping['accounts_payable_name'],
                         'account payable account name field is blank')
            assert_valid('accounts_payable_id' in general_mapping and general_mapping['accounts_payable_id'],
                         'account payable account id field is blank')

            params['accounts_payable_name'] = general_mapping.get('accounts_payable_name')
            params['accounts_payable_id'] = general_mapping.get('accounts_payable_id')

        if mapping_setting.destination_field == 'EMPLOYEE':
            assert_valid(
                'reimbursable_account_name' in general_mapping and general_mapping['reimbursable_account_name'],
                'reimbursable account name field is blank'
            )
            assert_valid('reimbursable_account_id' in general_mapping and general_mapping['reimbursable_account_id'],
                         'reimbursable account id field is blank')

            params['reimbursable_account_name'] = general_mapping.get('reimbursable_account_name')
            params['reimbursable_account_id'] = general_mapping.get('reimbursable_account_id')

        if configuration.corporate_credit_card_expenses_object and\
                configuration.corporate_credit_card_expenses_object != 'BILL':
            assert_valid('default_ccc_account_name' in general_mapping and general_mapping['default_ccc_account_name'],
                         'default ccc account name field is blank')
            assert_valid('default_ccc_account_id' in general_mapping and general_mapping['default_ccc_account_id'],
                         'default ccc account id field is blank')

            params['default_ccc_account_name'] = general_mapping.get('default_ccc_account_name')
            params['default_ccc_account_id'] = general_mapping.get('default_ccc_account_id')

        if configuration.corporate_credit_card_expenses_object == 'BILL' or \
                configuration.corporate_credit_card_expenses_object == 'CREDIT CARD CHARGE':
            assert_valid('default_ccc_vendor_name' in general_mapping and general_mapping['default_ccc_vendor_name'],
                         'default ccc vendor name field is blank')
            assert_valid('default_ccc_vendor_id' in general_mapping and general_mapping['default_ccc_vendor_id'],
                         'default ccc vendor id field is blank')

            params['default_ccc_vendor_name'] = general_mapping.get('default_ccc_vendor_name')
            params['default_ccc_vendor_id'] = general_mapping.get('default_ccc_vendor_id')

        if configuration.sync_fyle_to_netsuite_payments:
            assert_valid(
                'vendor_payment_account_name' in general_mapping and general_mapping['vendor_payment_account_name'],
                'vendor payment account name field is blank')
            assert_valid(
                'vendor_payment_account_id' in general_mapping and general_mapping['vendor_payment_account_id'],
                'vendor payment account id field is blank')

            params['vendor_payment_account_name'] = general_mapping.get('vendor_payment_account_name')
            params['vendor_payment_account_id'] = general_mapping.get('vendor_payment_account_id')

        general_mapping_object, _ = GeneralMapping.objects.update_or_create(
            workspace_id=self.__workspace_id,
            defaults=params
        )

        schedule_vendor_payment_creation(
            sync_fyle_to_netsuite_payments=configuration.sync_fyle_to_netsuite_payments,
            workspace_id=self.__workspace_id
        )

        if general_mapping_object.default_ccc_account_name:
            schedule_auto_map_ccc_employees(self.__workspace_id)

        return general_mapping_object