From 73be63947539d9ac6d4c4926a2d8142b9cf1715d Mon Sep 17 00:00:00 2001 From: ruuushhh <66899387+ruuushhh@users.noreply.github.com> Date: Mon, 28 Aug 2023 12:24:45 +0530 Subject: [PATCH] Add Mileage account support (#57) * Add Mileage account * Add Mileage account * Add Mock to the test * Add Mock to the test * Add Mock to the test * Add Mock to the test * Add Mock to the test * Add Mock to the test * Code factor resolved * Comments resolved --- apps/qbd/models.py | 17 +++++++------ .../migrations/0025_auto_20230823_1118.py | 25 +++++++++++++++++++ apps/workspaces/models.py | 4 +++ tests/test_fyle/test_helpers.py | 8 +++++- 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 apps/workspaces/migrations/0025_auto_20230823_1118.py diff --git a/apps/qbd/models.py b/apps/qbd/models.py index 44e5798..167645b 100644 --- a/apps/qbd/models.py +++ b/apps/qbd/models.py @@ -204,7 +204,7 @@ def create_bill( workspace_id=workspace_id ) - line_items = BillLineitem.create_bill_lineitems(expenses, bill, workspace_id) + line_items = BillLineitem.create_bill_lineitems(expenses, bill, workspace_id, export_settings) return bill, line_items @@ -260,7 +260,7 @@ class Meta: db_table = 'bill_lineitems' @staticmethod - def create_bill_lineitems(expenses: List[Expense], bill: Bill, workspace_id: int): + def create_bill_lineitems(expenses: List[Expense], bill: Bill, workspace_id: int, export_settings: ExportSettings): """ Create bill lineitem object :param bill_lineitem: bill lineitem data @@ -279,7 +279,8 @@ def create_bill_lineitems(expenses: List[Expense], bill: Bill, workspace_id: int lineitem = BillLineitem.objects.create( transaction_type='BILL', date=expense.spent_at, - account=expense.category, + account=export_settings.mileage_account_name if expense.category == 'Mileage' and \ + export_settings.mileage_account_name else expense.category, name=project_name, class_name=class_name, amount=expense.amount, @@ -371,6 +372,7 @@ def create_credit_card_purchase( export_settings: ExportSettings, accounting_export: AccountingExport, workspace_id: int + ): """ Create credit card purchase object @@ -600,11 +602,11 @@ def create_journal( ) lineitems = JournalLineitem.create_journal_lineitems( - expenses, journal, workspace_id + expenses, journal, workspace_id, export_settings, fund_source ) return journal, lineitems - + class JournalLineitem(models.Model): """ @@ -651,7 +653,7 @@ class Meta: @staticmethod def create_journal_lineitems( - expenses: List[Expense], journal: Journal, workspace_id: int + expenses: List[Expense], journal: Journal, workspace_id: int, export_settings: ExportSettings, fund_source: str ): """ Create Journal Lineitems @@ -671,7 +673,8 @@ def create_journal_lineitems( lineitem = JournalLineitem.objects.create( transaction_type='GENERAL JOURNAL', date=expense.spent_at, - account=expense.category, + account=export_settings.mileage_account_name if fund_source != 'CCC' and expense.category == 'Mileage' \ + and export_settings.mileage_account_name else expense.category, name=journal.name, class_name=class_name, amount=expense.amount * -1, diff --git a/apps/workspaces/migrations/0025_auto_20230823_1118.py b/apps/workspaces/migrations/0025_auto_20230823_1118.py new file mode 100644 index 0000000..e1314c8 --- /dev/null +++ b/apps/workspaces/migrations/0025_auto_20230823_1118.py @@ -0,0 +1,25 @@ +# Generated by Django 3.1.14 on 2023-08-23 11:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('workspaces', '0024_exportsettings_is_simplify_report_closure_enabled'), + ] + + operations = [ + migrations.AddField( + model_name='exportsettings', + name='mileage_account_name', + field=models.CharField(help_text='Mileage account name', max_length=255, null=True), + ), + migrations.AlterField( + model_name='exportsettings', + name='credit_card_expense_date', + field=models.CharField(choices=[('last_spent_at', 'last_spent_at'), ('spent_at', 'spent_at'), \ + ('posted_at', 'posted_at'), ('created_at', 'created_at')], max_length=255, \ + null=True), + ), + ] diff --git a/apps/workspaces/models.py b/apps/workspaces/models.py index 3ae0fa3..aa26228 100644 --- a/apps/workspaces/models.py +++ b/apps/workspaces/models.py @@ -152,6 +152,10 @@ class ExportSettings(models.Model): max_length=255, help_text='Bank account name', null=True ) + mileage_account_name = models.CharField( + max_length=255, help_text='Mileage account name', + null=True + ) reimbursable_expense_state = models.CharField( max_length=255, choices=REIMBURSABLE_EXPENSE_STATE_CHOICES, diff --git a/tests/test_fyle/test_helpers.py b/tests/test_fyle/test_helpers.py index c8a40bd..66b89b4 100644 --- a/tests/test_fyle/test_helpers.py +++ b/tests/test_fyle/test_helpers.py @@ -2,7 +2,12 @@ from apps.fyle.helpers import post_request -def test_post_request(): +def test_post_request(mocker): + class MockResponse: + def __init__(self, text, status_code): + self.text = text + self.status_code = status_code + url = 'https://api.instantwebtools.net/v1/airlines' body = { @@ -14,4 +19,5 @@ def test_post_request(): 'website': 'www.srilankaairways.com', 'established': '1990' } + mocker.patch('requests.post', return_value=(MockResponse("""{"data": "Airline Posted"}""", 200))) post_request(url, body=json.dumps(body))