diff --git a/apps/qbd/models.py b/apps/qbd/models.py index 167645b..4b09c8c 100644 --- a/apps/qbd/models.py +++ b/apps/qbd/models.py @@ -9,6 +9,7 @@ AdvancedSetting, ExportSettings, FieldMapping, FyleCredential, Workspace ) +from apps.mappings.models import QBDMapping def get_class_and_project_name(field_mappings: FieldMapping, expense: Expense): @@ -125,6 +126,18 @@ def get_top_purpose(workspace_id: str, expense: Expense, default: str) -> str: return memo +def get_corporate_card_name(corporate_card_id: str, workspace_id: int, export_settings: ExportSettings): + corporate_card_name = export_settings.credit_card_account_name + + qbd_mapping = QBDMapping.objects.filter(workspace_id=workspace_id, source_id=corporate_card_id) + + if len(qbd_mapping) > 0 and qbd_mapping[0].destination_value: + corporate_card_name = qbd_mapping[0].destination_value + + return corporate_card_name + + + class Bill(models.Model): """ Bills Table Model Class @@ -386,13 +399,15 @@ def create_credit_card_purchase( date_preference = export_settings.credit_card_expense_date + corporate_card_name = get_corporate_card_name(expenses[0].corporate_card_id, workspace_id, export_settings) + if export_settings.credit_card_entity_name_preference == 'EMPLOYEE': name = expenses[0].employee_name credit_card_purchase = CreditCardPurchase.objects.create( transaction_type='CREDIT CARD', date=get_transaction_date(expenses, date_preference), - account=export_settings.credit_card_account_name, + account=corporate_card_name, name=name, class_name='', amount=sum([expense.amount for expense in expenses]) * -1, diff --git a/tests/conftest.py b/tests/conftest.py index 566af84..f54f4c9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,9 +18,11 @@ AdvancedSetting ) from apps.tasks.models import AccountingExport +from apps.mappings.models import QBDMapping from quickbooks_desktop_api.tests import settings from .test_fyle.fixtures import fixtures as fyle_fixtures +from .test_mapping.fixture import fixture as mapping_fixtures @pytest.fixture @@ -268,3 +270,16 @@ def add_expenses(): expense['id'] = expense['id'] + str(workspace_id) Expense.create_expense_objects(expenses, workspace_id) + + +@pytest.fixture() +@pytest.mark.django_db(databases=['default']) +def add_ccc_mapping(): + """ + Add Expense to a workspace + """ + mappings = mapping_fixtures['create_qbd_mapping'] + + for workspace_id in [1, 2, 3]: + QBDMapping.update_or_create_mapping_objects(mappings, workspace_id) + diff --git a/tests/test_qbd/test_tasks.py b/tests/test_qbd/test_tasks.py index 6a54dff..86342ab 100644 --- a/tests/test_qbd/test_tasks.py +++ b/tests/test_qbd/test_tasks.py @@ -260,7 +260,7 @@ def test_create_credit_card_purchases_iif_file_expense_vendor( create_temp_workspace, add_accounting_export_bills, add_accounting_export_expenses, add_fyle_credentials, add_export_settings, add_field_mappings, add_advanced_settings, - mocker + add_ccc_mapping, mocker ): """ Test create credit card purchases iif file @@ -321,7 +321,7 @@ def test_create_credit_card_purchases_iif_file_expense_employee( create_temp_workspace, add_accounting_export_bills, add_accounting_export_expenses, add_fyle_credentials, add_export_settings, add_field_mappings, add_advanced_settings, - mocker + add_ccc_mapping, mocker ): """ Test create credit card purchases iif file @@ -380,9 +380,10 @@ def test_create_credit_card_purchases_iif_file_expense_employee( @pytest.mark.django_db(databases=['default']) def test_create_credit_card_purchases_iif_file_expense_fail( - create_temp_workspace, add_accounting_export_bills, - add_accounting_export_expenses, add_fyle_credentials, - add_export_settings, add_advanced_settings, mocker + create_temp_workspace, add_accounting_export_bills, + add_accounting_export_expenses, add_fyle_credentials, + add_export_settings, add_advanced_settings, + add_ccc_mapping, mocker ): """ Test create credit card purchases iif file @@ -435,7 +436,7 @@ def test_create_credit_card_purchases_iif_file_expense_fatal( create_temp_workspace, add_accounting_export_bills, add_accounting_export_expenses, add_fyle_credentials, add_export_settings, add_field_mappings, add_advanced_settings, - mocker + add_ccc_mapping, mocker ): """ Test create credit card purchases iif file @@ -785,7 +786,7 @@ def test_email_failure( create_temp_workspace, add_accounting_export_bills, add_accounting_export_expenses, add_fyle_credentials, add_export_settings, add_field_mappings, add_advanced_settings, - mocker + add_ccc_mapping, mocker ): """ Test create journals iif file Failed for email failure