diff --git a/apps/fyle/models.py b/apps/fyle/models.py index 3f5861ca..e8b77690 100644 --- a/apps/fyle/models.py +++ b/apps/fyle/models.py @@ -299,6 +299,62 @@ def _group_expenses(expenses, group_fields, workspace_id): return expense_groups +def filter_expense_groups( + expense_groups, + expenses: Expense, + expense_group_fields, + reimbursable_export_type=None, + ccc_export_type=None, +): + + filtered_expense_groups = [] + + for expense_group in expense_groups: + expense_group_expenses_ids = expense_group['expense_ids'] + filtered_expenses = [ + item for item in expenses if item.id in expense_group_expenses_ids + ] + if 'expense_id' not in expense_group_fields and ( + reimbursable_export_type in ('EXPENSE', 'BILL') or ccc_export_type == 'BILL' + ): + total_amount = 0 + if "spent_at" in expense_group_fields: + grouped_data = defaultdict(list) + for expense in filtered_expenses: + spent_at = expense.spent_at + grouped_data[spent_at].append(expense) + grouped_expenses = list(grouped_data.values()) + expense_groups = [] + for group_expense in grouped_expenses: + total_amount = 0 + for expense in group_expense: + total_amount += expense.amount + if total_amount < 0: + filtered_expenses = list( + filter(lambda expense: expense.amount > 0, group_expense) + ) + else: + for expense in filtered_expenses: + total_amount += expense.amount + + if total_amount < 0: + filtered_expenses = list( + filter(lambda expense: expense.amount > 0, filtered_expenses) + ) + elif reimbursable_export_type != 'JOURNAL ENTRY': + filtered_expenses = list( + filter(lambda expense: expense.amount > 0, filtered_expenses) + ) + + filtered_expense_ids = [item.id for item in filtered_expenses] + + if len(filtered_expense_ids) != 0: + expense_group['expense_ids'] = filtered_expense_ids + filtered_expense_groups.append(expense_group) + + return filtered_expense_groups + + class ExpenseGroup(models.Model): """ Expense Group @@ -319,48 +375,68 @@ class Meta: db_table = 'expense_groups' @staticmethod - def create_expense_groups_by_report_id_fund_source(expense_objects: List[Expense], workspace_id): + def create_expense_groups_by_report_id_fund_source( + expense_objects: List[Expense], workspace_id + ): """ Group expense by report_id and fund_source """ - expense_group_settings = ExpenseGroupSettings.objects.get(workspace_id=workspace_id) - reimbursable_expense_group_fields = expense_group_settings.reimbursable_expense_group_fields - reimbursable_expenses = list(filter(lambda expense: expense.fund_source == 'PERSONAL', expense_objects)) - general_settings = WorkspaceGeneralSettings.objects.get(workspace_id=workspace_id) + expense_groups = [] + expense_group_settings = ExpenseGroupSettings.objects.get( + workspace_id=workspace_id + ) - if general_settings.reimbursable_expenses_object == 'EXPENSE' and 'expense_id' not in reimbursable_expense_group_fields: - total_amount = 0 - if 'spent_at' in reimbursable_expense_group_fields: - grouped_data = defaultdict(list) - for expense in reimbursable_expenses: - spent_at = expense.spent_at - grouped_data[spent_at].append(expense) - grouped_expenses = list(grouped_data.values()) - reimbursable_expenses = [] - for expense_group in grouped_expenses: - total_amount = 0 - for expense in expense_group: - total_amount += expense.amount - if total_amount < 0: - expense_group = list(filter(lambda expense: expense.amount > 0, expense_group)) - reimbursable_expenses.extend(expense_group) - else: - for expense in reimbursable_expenses: - total_amount += expense.amount + reimbursable_expense_group_fields = ( + expense_group_settings.reimbursable_expense_group_fields + ) + reimbursable_expenses = list( + filter(lambda expense: expense.fund_source == "PERSONAL", expense_objects) + ) + general_settings = WorkspaceGeneralSettings.objects.get( + workspace_id=workspace_id + ) - if total_amount < 0: - reimbursable_expenses = list(filter(lambda expense: expense.amount > 0, reimbursable_expenses)) - elif general_settings.reimbursable_expenses_object != 'JOURNAL ENTRY': - reimbursable_expenses = list(filter(lambda expense: expense.amount > 0, reimbursable_expenses)) + reimbursable_expense_groups = _group_expenses( + reimbursable_expenses, reimbursable_expense_group_fields, workspace_id + ) + + filtered_reimbursable_expense_groups = filter_expense_groups( + reimbursable_expense_groups, + reimbursable_expenses, + reimbursable_expense_group_fields, + general_settings.reimbursable_expenses_object, + None + ) + + expense_groups.extend(filtered_reimbursable_expense_groups) - expense_groups = _group_expenses(reimbursable_expenses, reimbursable_expense_group_fields, workspace_id) + corporate_credit_card_expense_group_field = ( + expense_group_settings.corporate_credit_card_expense_group_fields + ) + corporate_credit_card_expenses = list( + filter(lambda expense: expense.fund_source == "CCC", expense_objects) + ) - corporate_credit_card_expense_group_field = expense_group_settings.corporate_credit_card_expense_group_fields - corporate_credit_card_expenses = list(filter(lambda expense: expense.fund_source == 'CCC', expense_objects)) - corporate_credit_card_expense_groups = _group_expenses(corporate_credit_card_expenses, corporate_credit_card_expense_group_field, workspace_id) + filtered_corporate_credit_card_expense_groups = _group_expenses( + corporate_credit_card_expenses, + corporate_credit_card_expense_group_field, + workspace_id, + ) + + if ( + general_settings.corporate_credit_card_expenses_object == "BILL" + and "expense_id" not in corporate_credit_card_expense_group_field + ): + filtered_corporate_credit_card_expense_groups = filter_expense_groups( + filtered_corporate_credit_card_expense_groups, + corporate_credit_card_expenses, + corporate_credit_card_expense_group_field, + None, + general_settings.corporate_credit_card_expenses_object + ) - expense_groups.extend(corporate_credit_card_expense_groups) + expense_groups.extend(filtered_corporate_credit_card_expense_groups) expense_group_objects = [] diff --git a/tests/test_fyle/fixtures.py b/tests/test_fyle/fixtures.py index 337fbf32..e5dbaf14 100644 --- a/tests/test_fyle/fixtures.py +++ b/tests/test_fyle/fixtures.py @@ -124,6 +124,347 @@ }, }, ], + + "expense_refund_spend_at": [ + { + 'id': '1236', + 'employee_email': 'jhonsnow@fyle.in', + 'category': 'Accounts Payable', + 'sub_category': 'Accounts Payable', + 'project': 'Aaron Abbott', + 'project_id': 263589, + 'expense_number': 'E/2021/12/T/4', + 'payment_number': 'P/2023/8/T/1221', + 'org_id': 'orPJvXuoLqvJ', + 'claim_number': 'C/2021/12/R/4', + 'amount': 100, + 'tax_amount': 0, + 'tax_group_id': None, + 'currency': 'USD', + 'foreign_amount': None, + 'foreign_currency': None, + 'report_title': 'report 2', + 'settlement_id': 'set3FdX0zRmdh', + 'employee_name': 'ashwin T', + 'reimbursable': True, + 'billable': False, + 'state': 'APPROVED', + 'vendor': None, + 'cost_center': None, + 'corporate_card_id': None, + 'purpose': None, + 'report_id': 'rpwhYusbrIjd', + 'file_ids': [], + 'spent_at': '2021-12-20T17:00:00', + 'approved_at': '2021-12-22T07:30:26.289842+00:00', + 'posted_at': None, + 'expense_created_at': '2021-12-22T07:30:26.289842+00:00', + 'expense_updated_at': '2021-12-23T07:11:39.770191+00:00', + 'source_account_type': 'PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT', + 'verified_at': None, + 'custom_properties': { + 'Vehicle Type': '', + 'Fyle Categories': '', + }, + }, + { + 'id': '1237', + 'employee_email': 'jhonsnow@fyle.in', + 'category': 'Accounts Payable', + 'sub_category': 'Accounts Payable', + 'project': 'Aaron Abbott', + 'project_id': 263589, + 'expense_number': 'E/2021/12/T/5', + 'payment_number': 'P/2023/8/T/1221', + 'org_id': 'orPJvXuoLqvJ', + 'claim_number': 'C/2021/12/R/4', + 'amount': -105, + 'tax_amount': 0, + 'tax_group_id': None, + 'currency': 'USD', + 'foreign_amount': None, + 'foreign_currency': None, + 'report_title': 'report 2', + 'settlement_id': 'set3FdX0zRmdh', + 'employee_name': 'ashwin T', + 'reimbursable': True, + 'billable': False, + 'state': 'APPROVED', + 'vendor': None, + 'cost_center': None, + 'corporate_card_id': None, + 'purpose': None, + 'report_id': 'rpwhYusbrIj', + 'file_ids': [], + 'spent_at': '2021-12-20T17:00:00', + 'approved_at': '2021-12-22T07:30:26.289842+00:00', + 'posted_at': None, + 'expense_created_at': '2021-12-22T07:30:26.289842+00:00', + 'expense_updated_at': '2021-12-23T07:11:39.770191+00:00', + 'source_account_type': 'PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT', + 'verified_at': None, + 'custom_properties': { + 'Vehicle Type': '', + 'Fyle Categories': '', + }, + }, + ], + "expense_refund_valid": [ + { + "id": "1278", + "employee_email": "jhonsnow@fyle.in", + "category": "Accounts Payable", + "sub_category": "Accounts Payable", + "project": "Aaron Abbott", + "project_id": 263589, + "expense_number": "E/2021/12/T/3", + "payment_number": "P/2023/8/T/1221", + "org_id": "orPJvXuoLqvJ", + "claim_number": "C/2021/12/R/1", + "employee_name": "ashwin T", + "amount": 150, + "tax_amount": 0, + "tax_group_id": None, + "currency": "USD", + "foreign_amount": None, + "foreign_currency": None, + "report_title": "report 2", + "settlement_id": "set3FdX0zRmdh", + "reimbursable": False, + "billable": False, + "state": "APPROVED", + "fund_source": "CCC", + "vendor": None, + "cost_center": None, + "corporate_card_id": None, + "purpose": None, + "report_id": "rpwhYusbrIjd", + "file_ids": [], + "spent_at": "2021-12-22T17:00:00", + "approved_at": "2021-12-22T07:30:26.289842+00:00", + "posted_at": None, + "expense_created_at": "2021-12-23T07:14:13.990650+00:00", + "expense_updated_at": "2021-12-27T05:26:43.954470+00:00", + "source_account_type": "PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT", + "verified_at": None, + "custom_properties": { + "Vehicle Type": "", + "Fyle Categories": "" + } + }, + { + "id": "1265", + "employee_email": "jhonsnow@fyle.in", + "category": "Accounts Payable", + "sub_category": "Accounts Payable", + "project": "Aaron Abbott", + "project_id": 263589, + "expense_number": "E/2021/12/T/3", + "payment_number": "P/2023/8/T/1221", + "org_id": "orPJvXuoLqvJ", + "claim_number": "C/2021/12/R/1", + "employee_name": "ashwin T", + "amount": -145, + "tax_amount": 0, + "tax_group_id": None, + "currency": "USD", + "foreign_amount": None, + "foreign_currency": None, + "report_title": "report 2", + "settlement_id": "set3FdX0zRmdh", + "reimbursable": True, + "billable": False, + "state": "APPROVED", + "fund_source": "CCC", + "vendor": None, + "cost_center": None, + "corporate_card_id": None, + "purpose": None, + "report_id": "rpwhYusbrIjd", + "file_ids": [], + "spent_at": "2021-12-22T17:00:00", + "approved_at": "2021-12-22T07:30:26.289842+00:00", + "posted_at": None, + "expense_created_at": "2021-12-23T07:14:13.990650+00:00", + "expense_updated_at": "2021-12-27T05:26:43.954470+00:00", + "source_account_type": "PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT", + "verified_at": None, + "custom_properties": { + "Vehicle Type": "", + "Fyle Categories": "" + } + } + ], + 'expense_refund_single_ccc':{ + "id": "1265", + "employee_email": "jhonsnow@fyle.in", + "category": "Accounts Payable", + "sub_category": "Accounts Payable", + "project": "Aaron Abbott", + "project_id": 263589, + "expense_number": "E/2021/12/T/3", + "payment_number": "P/2023/8/T/1221", + "org_id": "orPJvXuoLqvJ", + "claim_number": "C/2021/12/R/1", + "employee_name": "ashwin T", + "amount": -145, + "tax_amount": 0, + "tax_group_id": None, + "currency": "USD", + "foreign_amount": None, + "foreign_currency": None, + "report_title": "report 2", + "settlement_id": "set3FdX0zRmdh", + "reimbursable": True, + "billable": False, + "state": "APPROVED", + "fund_source": "CCC", + "vendor": None, + "cost_center": None, + "corporate_card_id": None, + "purpose": None, + "report_id": "rpwhYusbrIjd", + "file_ids": [], + "spent_at": "2021-12-22T17:00:00", + "approved_at": "2021-12-22T07:30:26.289842+00:00", + "posted_at": None, + "expense_created_at": "2021-12-23T07:14:13.990650+00:00", + "expense_updated_at": "2021-12-27T05:26:43.954470+00:00", + "source_account_type": "PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT", + "verified_at": None, + "custom_properties": { + "Vehicle Type": "", + "Fyle Categories": "" + } + }, + 'expense_refund_single':{ + "id": "1265", + "employee_email": "jhonsnow@fyle.in", + "category": "Accounts Payable", + "sub_category": "Accounts Payable", + "project": "Aaron Abbott", + "project_id": 263589, + "expense_number": "E/2021/12/T/3", + "payment_number": "P/2023/8/T/1221", + "org_id": "orPJvXuoLqvJ", + "claim_number": "C/2021/12/R/1", + "employee_name": "ashwin T", + "amount": -145, + "tax_amount": 0, + "tax_group_id": None, + "currency": "USD", + "foreign_amount": None, + "foreign_currency": None, + "report_title": "report 2", + "settlement_id": "set3FdX0zRmdh", + "reimbursable": True, + "billable": False, + "state": "APPROVED", + "fund_source": "PERSONAL", + "vendor": None, + "cost_center": None, + "corporate_card_id": None, + "purpose": None, + "report_id": "rpwhYusbrIjd", + "file_ids": [], + "spent_at": "2021-12-22T17:00:00", + "approved_at": "2021-12-22T07:30:26.289842+00:00", + "posted_at": None, + "expense_created_at": "2021-12-23T07:14:13.990650+00:00", + "expense_updated_at": "2021-12-27T05:26:43.954470+00:00", + "source_account_type": "PERSONAL_CASH_ACCOUNT", + "verified_at": None, + "custom_properties": { + "Vehicle Type": "", + "Fyle Categories": "" + } + }, + "expense_refund_invalid": [ + { + "id": "21321", + "employee_email": "jhonsnow@fyle.in", + "category": "Accounts Payable", + "sub_category": "Accounts Payable", + "project": "Aaron Abbott", + "project_id": 263589, + "expense_number": "E/2021/12/T/8", + "payment_number": "P/2023/8/T/1221", + "org_id": "orPJvXuoLqvJ", + "claim_number": "C/2021/12/R/7", + "employee_name": "ashwin T", + "amount": -150, + "tax_amount": 0, + "tax_group_id": None, + "currency": "USD", + "foreign_amount": None, + "foreign_currency": None, + "report_title": "report 2", + "settlement_id": "set3FdX0zRmdh", + "reimbursable": False, + "billable": False, + "state": "APPROVED", + "fund_source": "CCC", + "vendor": None, + "cost_center": None, + "corporate_card_id": None, + "purpose": None, + "report_id": "rpwhYusbrIje", + "file_ids": [], + "spent_at": "2021-12-22T17:00:00", + "approved_at": "2021-12-22T07:30:26.289842+00:00", + "posted_at": None, + "expense_created_at": "2021-12-23T07:14:13.990650+00:00", + "expense_updated_at": "2021-12-27T05:26:43.954470+00:00", + "source_account_type": "PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT", + "verified_at": None, + "custom_properties": { + "Vehicle Type": "", + "Fyle Categories": "" + } + }, + { + "id": "1111", + "employee_email": "jhonsnow@fyle.in", + "category": "Accounts Payable", + "sub_category": "Accounts Payable", + "project": "Aaron Abbott", + "project_id": 263589, + "expense_number": "E/2021/12/T/7", + "payment_number": "P/2023/8/T/1221", + "org_id": "orPJvXuoLqvJ", + "claim_number": "C/2021/12/R/7", + "employee_name": "ashwin T", + "amount": -160, + "tax_amount": 0, + "tax_group_id": None, + "currency": "USD", + "foreign_amount": None, + "foreign_currency": None, + "report_title": "report 2", + "settlement_id": "set3FdX0zRmdh", + "reimbursable": True, + "billable": False, + "state": "APPROVED", + "fund_source": "CCC", + "vendor": None, + "cost_center": None, + "corporate_card_id": None, + "purpose": None, + "report_id": "rpwhYusbrIje", + "file_ids": [], + "spent_at": "2021-12-22T17:00:00", + "approved_at": "2021-12-22T07:30:26.289842+00:00", + "posted_at": None, + "expense_created_at": "2021-12-23T07:14:13.990650+00:00", + "expense_updated_at": "2021-12-27T05:26:43.954470+00:00", + "source_account_type": "PERSONAL_CORPORATE_CREDIT_CARD_ACCOUNT", + "verified_at": None, + "custom_properties": { + "Vehicle Type": "", + "Fyle Categories": "" + } + } + ], "skipped_expenses": { 'count': 2, 'next': None, diff --git a/tests/test_fyle/test_models.py b/tests/test_fyle/test_models.py index 7aeff38b..8bde7478 100644 --- a/tests/test_fyle/test_models.py +++ b/tests/test_fyle/test_models.py @@ -67,28 +67,219 @@ def test_create_reimbursement(db): def test_create_expense_groups_by_report_id_fund_source_spent_at(db): - expenses = data['expenses_spent_at'] + expenses = data["expenses_spent_at"] expense_objects = Expense.create_expense_objects(expenses, 1) workspace = Workspace.objects.get(id=1) expense_group_setting = ExpenseGroupSettings.objects.get(workspace_id=1) - expense_group_setting.reimbursable_export_date_type = 'spent_at' - reimbursable_expense_group_fields = expense_group_setting.reimbursable_expense_group_fields - reimbursable_expense_group_fields.append('spent_at') - expense_group_setting.reimbursable_expense_group_fields = reimbursable_expense_group_fields + expense_group_setting.reimbursable_export_date_type = "spent_at" + reimbursable_expense_group_fields = ( + expense_group_setting.reimbursable_expense_group_fields + ) + reimbursable_expense_group_fields.append("spent_at") + expense_group_setting.reimbursable_expense_group_fields = ( + reimbursable_expense_group_fields + ) expense_group_setting.save() assert len(expense_objects) == 3 ExpenseGroup.create_expense_groups_by_report_id_fund_source(expense_objects, 1) - expense_group = ExpenseGroup.objects.filter(workspace=workspace).order_by('-created_at').first() + expense_group = ( + ExpenseGroup.objects.filter(workspace=workspace).order_by("-created_at").first() + ) assert expense_group.expenses.count() == 2 +def test_create_expense_groups_refund_invalid(db): + + workspace = Workspace.objects.get(id=1) + configuration = WorkspaceGeneralSettings.objects.get(workspace=workspace) + + configuration.corporate_credit_card_expenses_object = "BILL" + configuration.save() + + expenses = data["expense_refund_invalid"] + expense_objects = Expense.create_expense_objects(expenses, 1) + assert len(expense_objects) == 2 + ExpenseGroup.create_expense_groups_by_report_id_fund_source(expense_objects, 1) + expense_group = ( + ExpenseGroup.objects.filter(workspace=workspace).order_by("-created_at").first() + ) + + assert expense_group is None + + +def test_create_expense_groups_refund(db): + expenses = data["expense_refund_valid"] + expense_objects = Expense.create_expense_objects(expenses, 1) + + assert len(expense_objects) == 2 + workspace = workspace = Workspace.objects.get(id=1) + configuration = WorkspaceGeneralSettings.objects.get(workspace=workspace) + + configuration.corporate_credit_card_expenses_object = "BILL" + configuration.save() + + ExpenseGroup.create_expense_groups_by_report_id_fund_source(expense_objects, 1) + + expense_group = ( + ExpenseGroup.objects.filter(workspace=workspace).order_by("-created_at").first() + ) + assert expense_group.expenses.count() == 2 + + +def creat_expense_groups_by_report_id_refund_spent_at(db): + workspace = workspace = Workspace.objects.get(id=1) + configuration = WorkspaceGeneralSettings.objects.get(workspace=workspace) + + configuration.corporate_credit_card_expenses_object = "BILL" + configuration.save() + + expenses = data["expense_refund_spend_at"] + + expense_objects = Expense.create_expense_objects(expenses, 1) + expense_group_setting = ExpenseGroupSettings.objects.get(workspace_id=1) + expense_group_setting.ccc_export_date_type = "spent_at" + corporate_expense_group_fields = ( + expense_group_setting.corporate_credit_card_expense_group_fields + ) + corporate_expense_group_fields.append("spent_at") + expense_group_setting.corporate_credit_card_expense_group_fields = ( + corporate_expense_group_fields + ) + expense_group_setting.save() + + assert len(expense_objects) == 2 + + ExpenseGroup.create_expense_groups_by_report_id_fund_source(expense_objects, 1) + + expense_group = ( + ExpenseGroup.objects.filter(workspace=workspace).order_by("-created_at").first() + ) + assert expense_group.expenses.count() == 1 + + +def test_create_expense_group_report_id_journal_entry(db): + + workspace = workspace = Workspace.objects.get(id=1) + configuration = WorkspaceGeneralSettings.objects.get(workspace=workspace) + + configuration.corporate_credit_card_expenses_object = "JOURNAL ENTRY" + configuration.save() + + expense_group_setting = ExpenseGroupSettings.objects.get(workspace_id=1) + + corporate_expense_group_fields = ( + expense_group_setting.corporate_credit_card_expense_group_fields + ) + corporate_expense_group_fields.append("expense_id") + expense_group_setting.corporate_credit_card_expense_group_fields = ( + corporate_expense_group_fields + ) + expense_group_setting.save() + workspace = workspace = Workspace.objects.get(id=1) + expenses = data["expense_refund_single_ccc"] + expense_objects = Expense.create_expense_objects([expenses], 1) + assert len(expense_objects) == 1 + ExpenseGroup.create_expense_groups_by_report_id_fund_source(expense_objects, 1) + expense_group = ( + ExpenseGroup.objects.filter(workspace=workspace).order_by("-created_at").first() + ) + assert expense_group.expenses.count() == 1 + + +def test_create_expense_group_report_id_check(db): + + workspace = workspace = Workspace.objects.get(id=1) + configuration = WorkspaceGeneralSettings.objects.get(workspace=workspace) + + configuration.reimbursable_expenses_object = "CHECK" + configuration.save() + + expense_group_setting = ExpenseGroupSettings.objects.get(workspace_id=1) + + reimbursable_expense_group_fields = ( + expense_group_setting.reimbursable_expense_group_fields + ) + reimbursable_expense_group_fields.append("expense_id") + expense_group_setting.reimbursable_expense_group_fields = ( + reimbursable_expense_group_fields + ) + expense_group_setting.save() + workspace = workspace = Workspace.objects.get(id=1) + expenses = data["expense_refund_single"] + expense_objects = Expense.create_expense_objects([expenses], 1) + assert len(expense_objects) == 1 + ExpenseGroup.create_expense_groups_by_report_id_fund_source(expense_objects, 1) + expense_group = ( + ExpenseGroup.objects.filter(workspace=workspace).order_by("-created_at").first() + ) + assert expense_group is None + + +def test_create_expense_group_report_id_expense_report(db): + + workspace = workspace = Workspace.objects.get(id=1) + configuration = WorkspaceGeneralSettings.objects.get(workspace=workspace) + + configuration.reimbursable_expenses_object = "EXPENSE" + configuration.save() + + expense_group_setting = ExpenseGroupSettings.objects.get(workspace_id=1) + + reimbursable_expense_group_fields = ( + expense_group_setting.reimbursable_expense_group_fields + ) + reimbursable_expense_group_fields.append("expense_id") + expense_group_setting.reimbursable_expense_group_fields = ( + reimbursable_expense_group_fields + ) + expense_group_setting.save() + workspace = workspace = Workspace.objects.get(id=1) + expenses = data["expense_refund_single"] + expense_objects = Expense.create_expense_objects([expenses], 1) + assert len(expense_objects) == 1 + ExpenseGroup.create_expense_groups_by_report_id_fund_source(expense_objects, 1) + expense_group = ( + ExpenseGroup.objects.filter(workspace=workspace).order_by("-created_at").first() + ) + assert expense_group is None + + +def test_create_expense_group_report_id_debit_card_expense(db): + + workspace = workspace = Workspace.objects.get(id=1) + configuration = WorkspaceGeneralSettings.objects.get(workspace=workspace) + + configuration.corporate_credit_card_expenses_object = "DEBIT CARD EXPENSE" + configuration.save() + + expense_group_setting = ExpenseGroupSettings.objects.get(workspace_id=1) + + corporate_expense_group_fields = ( + expense_group_setting.corporate_credit_card_expense_group_fields + ) + corporate_expense_group_fields.append("expense_id") + expense_group_setting.corporate_credit_card_expense_group_fields = ( + corporate_expense_group_fields + ) + expense_group_setting.save() + workspace = workspace = Workspace.objects.get(id=1) + expenses = data["expense_refund_single_ccc"] + expense_objects = Expense.create_expense_objects([expenses], 1) + assert len(expense_objects) == 1 + ExpenseGroup.create_expense_groups_by_report_id_fund_source(expense_objects, 1) + expense_group = ( + ExpenseGroup.objects.filter(workspace=workspace).order_by("-created_at").first() + ) + assert expense_group.expenses.count() == 1 + + def test_create_expense_groups_by_report_id_fund_source(db): workspace_id = 4 payload = data['expenses']