Beispiel #1
0
def schedule_expense_group_creation(workspace_id: int, user: str):
    """
    Schedule Expense group creation
    :param workspace_id: Workspace id
    :param user: User email
    :return: None
    """
    fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)
    fyle_connector = FyleConnector(fyle_credentials.refresh_token,
                                   workspace_id)
    fyle_sdk_connection = fyle_connector.connection

    jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection)

    task_log = TaskLog.objects.create(workspace_id=workspace_id,
                                      type='FETCHING_EXPENSES',
                                      status='IN_PROGRESS')

    created_job = jobs.trigger_now(
        callback_url='{0}{1}'.format(
            settings.API_URL,
            '/workspaces/{0}/fyle/expense_groups/'.format(workspace_id)),
        callback_method='POST',
        object_id=task_log.id,
        payload={'task_log_id': task_log.id},
        job_description='Fetch expenses: Workspace id - {0}, user - {1}'.
        format(workspace_id, user))
    task_log.task_id = created_job['id']
    task_log.save()
Beispiel #2
0
def schedule_invoice_creation(workspace_id, expense_group_ids, user):
    """
    Schedule Invoice creation
    :param workspace_id:
    :param expense_group_ids:
    :param user:
    :return:
    """
    expense_groups = ExpenseGroup.objects.filter(
        workspace_id=workspace_id, id__in=expense_group_ids).all()
    fyle_sdk_connection = connect_to_fyle(workspace_id)
    jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection)

    for expense_group in expense_groups:
        task_log = TaskLog.objects.create(
            workspace_id=expense_group.workspace.id,
            expense_group=expense_group,
            type='CREATING INVOICE',
            status='IN_PROGRESS')

        created_job = jobs.trigger_now(
            callback_url='{0}{1}'.format(
                settings.API_BASE_URL,
                '/workspace_jobs/{0}/expense_group/{1}/invoice/trigger/'.
                format(workspace_id, expense_group.id)),
            callback_method='POST',
            object_id=task_log.id,
            payload={'task_log_id': task_log.id},
            job_description=f'Create invoice: Workspace id - {workspace_id}, \
            user - {user}, expense group id - {expense_group.id}')
        task_log.task_id = created_job['id']
        task_log.save()
Beispiel #3
0
def schedule_expense_group_creation(workspace_id, user):
    """
    Schedule Expense Group creation
    :param workspace_id:
    :param user:
    :return:
    """

    task_log = TaskLog.objects.create(workspace_id=workspace_id,
                                      type="FETCHING EXPENSES",
                                      status="IN_PROGRESS")

    try:
        fyle_sdk_connection = connect_to_fyle(workspace_id)
        jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection)
        created_job = jobs.trigger_now(
            callback_url='{0}{1}'.format(
                settings.API_BASE_URL,
                '/workspace_jobs/{0}/expense_group/trigger/'.format(
                    workspace_id)),
            callback_method='POST',
            object_id=task_log.id,
            payload={'task_log_id': task_log.id},
            job_description=
            f'Fetch expenses: Workspace id - {workspace_id}, user - {user}')
        task_log.task_id = created_job['id']
        task_log.save()
    except FyleCredential.DoesNotExist:
        LOGGER.error('Error: Fyle Credentials not found for this workspace.')
        task_log.detail = {
            'error': 'Please connect your Source (Fyle) Account'
        }
        task_log.status = 'FYLE CONNECTION ERROR'
        task_log.save()
Beispiel #4
0
def schedule_bills_creation(workspace_id: int, expense_group_ids: List[str],
                            user):
    """
    Schedule bills creation
    :param expense_group_ids: List of expense group ids
    :param workspace_id: workspace id
    :param user: user email
    :return: None
    """
    if expense_group_ids:
        expense_groups = ExpenseGroup.objects.filter(
            workspace_id=workspace_id,
            id__in=expense_group_ids,
            bill__id__isnull=True).all()
    else:
        expense_groups = ExpenseGroup.objects.filter(
            workspace_id=workspace_id, bill__id__isnull=True).all()

    fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)
    fyle_connector = FyleConnector(fyle_credentials.refresh_token,
                                   workspace_id)
    fyle_sdk_connection = fyle_connector.connection
    jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection)

    for expense_group in expense_groups:
        task_log, _ = TaskLog.objects.update_or_create(
            workspace_id=expense_group.workspace_id,
            expense_group=expense_group,
            defaults={
                'status': 'IN_PROGRESS',
                'type': 'CREATING_BILL'
            })
        created_job = jobs.trigger_now(
            callback_url='{0}{1}'.format(
                settings.API_URL,
                '/workspaces/{0}/qbo/bills/'.format(workspace_id)),
            callback_method='POST',
            object_id=task_log.id,
            payload={
                'expense_group_id': expense_group.id,
                'task_log_id': task_log.id
            },
            job_description=
            'Create Bill: Workspace id - {0}, user - {1}, expense group id - {2}'
            .format(workspace_id, user, expense_group.id))
        task_log.task_id = created_job['id']
        task_log.save()