def schedule_sync(workspace_id, schedule, user): """ Schedule sync :param workspace_id: workspace_id :param schedule: schedule object :param user: user email """ fyle_sdk_connection = connect_to_fyle(workspace_id) jobs = FyleJobsSDK(settings.FYLE_JOBS_URL, fyle_sdk_connection) created_job = jobs.trigger_interval( callback_url='{0}{1}'.format( settings.API_BASE_URL, '/workspace_jobs/{0}/settings/schedule/trigger/'.format( workspace_id ) ), callback_method='POST', object_id=schedule.id, job_description='Fetch expenses: Workspace id - {0}, user - {1}'.format( workspace_id, user ), start_datetime=schedule.start_datetime.strftime('%Y-%m-%d %H:%M:00.00'), hours=int(schedule.interval_hours) ) schedule.fyle_job_id = created_job['id'] schedule.save()
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()
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()
def fetch_paid_expenses(workspace_id, updated_at): """ Fetch expenses from Fyle API filtered by state and updated_at """ connection = connect_to_fyle(workspace_id) if updated_at is None: expenses = connection.Expenses.get(state='PAID') else: expenses = connection.Expenses.get( state='PAID', updated_at= f'gte:{updated_at.strftime("%Y-%m-%dT%H:%M:%S.%-SZ")}') return expenses
def fetch_expenses_and_create_groups(workspace_id, task_log, user): """ Fetch expenses and create expense groups :param workspace_id :param task_log :param user """ expense_group_ids = [] try: updated_at = None task_logs = TaskLog.objects.filter(workspace__id=workspace_id, type='FETCHING EXPENSES', status='COMPLETE') if task_logs: updated_at = task_logs.latest().created_at expenses = Expense.fetch_paid_expenses(workspace_id, updated_at) expense_objects = Expense.create_expense_objects(expenses) connection = connect_to_fyle(workspace_id) expense_groups = ExpenseGroup.group_expense_by_report_id( expense_objects, workspace_id, connection) expense_group_objects = ExpenseGroup.create_expense_groups( expense_groups) for expense_group in expense_group_objects: expense_group_ids.append(expense_group.id) task_log.status = 'COMPLETE' task_log.detail = 'Expense groups created successfully!' task_log.save() schedule_invoice_creation(workspace_id, expense_group_ids, user) 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() except Exception: error = traceback.format_exc() LOGGER.exception(f'Error: Workspace id - {workspace_id}\n{error}') task_log.detail = {'error': 'Please contact system administrator.'} task_log.status = 'FATAL' task_log.save() return expense_group_ids