def async_create_expense_groups(workspace_id: int, state: List[str], export_non_reimbursable: bool, task_log: TaskLog): try: with transaction.atomic(): workspace = Workspace.objects.get(pk=workspace_id) last_synced_at = workspace.last_synced_at updated_at = [] if last_synced_at: updated_at.append('gte:{0}'.format( datetime.strftime(last_synced_at, '%Y-%m-%dT%H:%M:%S.000Z'))) workspace.last_synced_at = datetime.now() workspace.save() fyle_credentials = FyleCredential.objects.get( workspace_id=workspace_id) fyle_connector = FyleConnector(fyle_credentials.refresh_token) expenses = fyle_connector.get_expenses( state=state, export_non_reimbursable=export_non_reimbursable, updated_at=updated_at) expense_objects = Expense.create_expense_objects(expenses) expense_group_objects = ExpenseGroup.create_expense_groups_by_report_id( expense_objects, workspace_id) task_log.detail = ExpenseGroupSerializer(expense_group_objects, many=True).data task_log.status = 'COMPLETE' task_log.save(update_fields=['detail', 'status']) except FyleCredential.DoesNotExist: logger.exception('Fyle credentials not found %s', workspace_id) task_log.detail = { 'message': 'Fyle credentials do not exist in workspace' } task_log.status = 'FAILED' task_log.save(update_fields=['detail', 'status']) except Exception: error = traceback.format_exc() task_log.detail = {'error': error} task_log.status = 'FATAL' task_log.save(update_fields=['detail', 'status']) logger.exception('Something unexpected happened workspace_id: %s\n%s', task_log.workspace_id, error)
def handle_sage_intacct_errors(exception, expense_group: ExpenseGroup, task_log: TaskLog, export_type: str): logger.error(exception.response) sage_intacct_errors = literal_eval(exception.response)['error'] error_msg = 'Error while creating' errors = [] if isinstance(sage_intacct_errors, list): for error in sage_intacct_errors: errors.append({ 'expense_group_id': expense_group.id, 'short_description': error['description'] if error['description'] else export_type, 'long_description': error['description2'] if error['description2'] \ else '{0} {1}'.format(error_msg, export_type), 'correction': error['correction'] if error['correction'] else 'Not available' }) elif isinstance(sage_intacct_errors, dict): error = sage_intacct_errors errors.append({ 'expense_group_id': expense_group.id, 'short_description': error['description'] if error['description'] else export_type, 'long_description': error['description2'] if error['description2'] \ else '{0} {1}'.format(error_msg, export_type), 'correction': error['correction'] if error['correction'] else 'Not available' }) task_log.status = 'FAILED' task_log.detail = None task_log.sage_intacct_errors = errors task_log.save(update_fields=['sage_intacct_errors', 'detail', 'status'])
def create_expense_groups(workspace_id: int, fund_source: List[str], task_log: TaskLog): """ Create expense groups :param task_log: Task log object :param workspace_id: workspace id :param state: expense state :param fund_source: expense fund source """ try: with transaction.atomic(): updated_at = [] workspace = Workspace.objects.get(pk=workspace_id) last_synced_at = workspace.last_synced_at if last_synced_at: updated_at.append('gte:{0}'.format( datetime.strftime(last_synced_at, '%Y-%m-%dT%H:%M:%S.000Z'))) fyle_credentials = FyleCredential.objects.get( workspace_id=workspace_id) fyle_connector = FyleConnector(fyle_credentials.refresh_token, workspace_id) expense_group_settings = ExpenseGroupSettings.objects.get( workspace_id=workspace_id) expenses = fyle_connector.get_expenses( state=expense_group_settings.expense_state, updated_at=updated_at, fund_source=fund_source) if expenses: workspace.last_synced_at = datetime.now() workspace.save() expense_objects = Expense.create_expense_objects( expenses, workspace_id) ExpenseGroup.create_expense_groups_by_report_id_fund_source( expense_objects, workspace_id) task_log.status = 'COMPLETE' task_log.save() except FyleCredential.DoesNotExist: logger.exception('Fyle credentials not found %s', workspace_id) task_log.detail = { 'message': 'Fyle credentials do not exist in workspace' } task_log.status = 'FAILED' task_log.save() except Exception: error = traceback.format_exc() task_log.detail = {'error': error} task_log.status = 'FATAL' task_log.save() logger.exception('Something unexpected happened workspace_id: %s %s', task_log.workspace_id, task_log.detail)
def create_expense_groups(workspace_id: int, fund_source: List[str], task_log: TaskLog): """ Create expense groups :param task_log: Task log object :param workspace_id: workspace id :param fund_source: expense fund source """ try: with transaction.atomic(): workspace = Workspace.objects.get(pk=workspace_id) last_synced_at = workspace.last_synced_at fyle_credentials = FyleCredential.objects.get( workspace_id=workspace_id) expense_group_settings = ExpenseGroupSettings.objects.get( workspace_id=workspace_id) platform = PlatformConnector(fyle_credentials) source_account_type = [] for source in fund_source: source_account_type.append(SOURCE_ACCOUNT_MAP[source]) filter_credit_expenses = True if expense_group_settings.import_card_credits: filter_credit_expenses = False expenses = platform.expenses.get( source_account_type=source_account_type, state=expense_group_settings.expense_state, settled_at=last_synced_at if expense_group_settings.expense_state == 'PAYMENT_PROCESSING' else None, filter_credit_expenses=filter_credit_expenses, last_paid_at=last_synced_at if expense_group_settings.expense_state == 'PAID' else None) if expenses: workspace.last_synced_at = datetime.now() workspace.save() expense_objects = Expense.create_expense_objects( expenses, workspace_id) ExpenseGroup.create_expense_groups_by_report_id_fund_source( expense_objects, workspace_id) task_log.status = 'COMPLETE' task_log.save() except FyleCredential.DoesNotExist: logger.info('Fyle credentials not found %s', workspace_id) task_log.detail = { 'message': 'Fyle credentials do not exist in workspace' } task_log.status = 'FAILED' task_log.save() except Exception: error = traceback.format_exc() task_log.detail = {'error': error} task_log.status = 'FATAL' task_log.save() logger.exception('Something unexpected happened workspace_id: %s %s', task_log.workspace_id, task_log.detail)