Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Netsuite Tax Override #662

Merged
merged 8 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.14 on 2024-11-04 10:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mappings', '0014_auto_20240417_0807'),
]

operations = [
migrations.AddField(
model_name='generalmapping',
name='is_tax_balancing_enabled',
field=models.BooleanField(default=False, help_text='Is tax balancing enabled'),
),
]
1 change: 1 addition & 0 deletions apps/mappings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class GeneralMapping(models.Model):
)

override_tax_details = models.BooleanField(default=False, help_text='Override tax details')
is_tax_balancing_enabled = models.BooleanField(default=False, help_text='Is tax balancing enabled')

workspace = models.OneToOneField(Workspace, on_delete=models.PROTECT, help_text='Reference to Workspace model', related_name='general_mappings')
created_at = models.DateTimeField(auto_now_add=True, help_text='Created at datetime')
Expand Down
458 changes: 225 additions & 233 deletions apps/netsuite/connector.py

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions apps/netsuite/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,18 @@ def get_tax_group_mapping(lineitem: Expense = None, workspace_id: int = None):

def get_tax_item_id_or_none(expense_group: ExpenseGroup, general_mapping: GeneralMapping, lineitem: Expense = None):
tax_code = None
mapping = get_tax_group_mapping(lineitem, expense_group.workspace_id)
tax_setting: MappingSetting = MappingSetting.objects.filter(
workspace_id=expense_group.workspace_id,
destination_field='TAX_ITEM'
).first()

if tax_setting:
mapping = get_tax_group_mapping(lineitem, expense_group.workspace_id)

if mapping:
tax_code = mapping.destination.destination_id
else:
tax_code = general_mapping.default_tax_code_id
if mapping:
tax_code = mapping.destination.destination_id
else:
tax_code = general_mapping.default_tax_code_id

return tax_code

Expand Down
18 changes: 10 additions & 8 deletions apps/netsuite/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ def construct_payload_and_update_export(expense_id_receipt_url_map: dict, task_l
construct_lines = getattr(netsuite_connection, func)

# calling the target construct payload function with credit and debit
credit_line = construct_lines(export_line_items, credit='Credit', org_id=workspace.fyle_org_id)
credit_line = construct_lines(export_line_items, general_mappings, credit='Credit', org_id=workspace.fyle_org_id)
debit_line = construct_lines(
export_line_items, debit='Debit', attachment_links=expense_id_receipt_url_map,
export_line_items, general_mappings, debit='Debit', attachment_links=expense_id_receipt_url_map,
cluster_domain=cluster_domain, org_id=workspace.fyle_org_id
)
lines.extend(credit_line)
Expand All @@ -337,11 +337,11 @@ def construct_payload_and_update_export(expense_id_receipt_url_map: dict, task_l
elif task_log.type == 'CREATING_BILL':
construct_lines = getattr(netsuite_connection, func)
# calling the target construct payload function
expense_list, item_list = construct_lines(export_line_items, expense_id_receipt_url_map, cluster_domain, workspace.fyle_org_id, general_mappings.override_tax_details)
expense_list, item_list = construct_lines(export_line_items, expense_id_receipt_url_map, cluster_domain, workspace.fyle_org_id, general_mappings.override_tax_details, general_mappings)
else:
construct_lines = getattr(netsuite_connection, func)
# calling the target construct payload function
lines = construct_lines(export_line_items, expense_id_receipt_url_map, cluster_domain, workspace.fyle_org_id)
lines = construct_lines(export_line_items, general_mappings, expense_id_receipt_url_map, cluster_domain, workspace.fyle_org_id)

# final payload to be sent to netsuite, since this is an update operation, we need to pass the external id
if task_log.type == 'CREATING_BILL':
Expand Down Expand Up @@ -487,7 +487,7 @@ def create_bill(expense_group: ExpenseGroup, task_log_id, last_export):

bill_lineitems_objects = BillLineitem.create_bill_lineitems(expense_group, configuration)

created_bill = netsuite_connection.post_bill(bill_object, bill_lineitems_objects)
created_bill = netsuite_connection.post_bill(bill_object, bill_lineitems_objects, general_mappings)
logger.info('Created Bill with Expense Group %s successfully', expense_group.id)

task_log.detail = created_bill
Expand Down Expand Up @@ -567,7 +567,7 @@ def create_credit_card_charge(expense_group, task_log_id, last_export):
attachment_links[expense.expense_id] = attachment_link

created_credit_card_charge = netsuite_connection.post_credit_card_charge(
credit_card_charge_object, credit_card_charge_lineitems_object, attachment_links, refund
credit_card_charge_object, credit_card_charge_lineitems_object, general_mappings, attachment_links, refund
)
worker_logger.info('Created Credit Card Charge with Expense Group %s successfully', expense_group.id)

Expand Down Expand Up @@ -612,6 +612,7 @@ def create_expense_report(expense_group, task_log_id, last_export):
return

configuration = Configuration.objects.get(workspace_id=expense_group.workspace_id)
general_mapping = GeneralMapping.objects.get(workspace_id=expense_group.workspace_id)

fyle_credentials = FyleCredential.objects.get(workspace_id=expense_group.workspace_id)
netsuite_credentials = NetSuiteCredentials.objects.get(workspace_id=expense_group.workspace_id)
Expand All @@ -633,7 +634,7 @@ def create_expense_report(expense_group, task_log_id, last_export):
)

created_expense_report = netsuite_connection.post_expense_report(
expense_report_object, expense_report_lineitems_objects
expense_report_object, expense_report_lineitems_objects, general_mapping
)
worker_logger.info('Created Expense Report with Expense Group %s successfully', expense_group.id)

Expand Down Expand Up @@ -676,6 +677,7 @@ def create_journal_entry(expense_group, task_log_id, last_export):
return

configuration = Configuration.objects.get(workspace_id=expense_group.workspace_id)
general_mapping = GeneralMapping.objects.get(workspace_id=expense_group.workspace_id)


fyle_credentials = FyleCredential.objects.get(workspace_id=expense_group.workspace_id)
Expand All @@ -698,7 +700,7 @@ def create_journal_entry(expense_group, task_log_id, last_export):
)

created_journal_entry = netsuite_connection.post_journal_entry(
journal_entry_object, journal_entry_lineitems_objects, configuration
journal_entry_object, journal_entry_lineitems_objects, configuration, general_mapping
)
worker_logger.info('Created Journal Entry with Expense Group %s successfully', expense_group.id)

Expand Down
19 changes: 11 additions & 8 deletions tests/sql_fixtures/reset_db_fixtures/reset_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,8 @@ CREATE TABLE public.general_mappings (
class_level character varying(255),
class_name character varying(255),
default_tax_code_id character varying(255),
default_tax_code_name character varying(255)
default_tax_code_name character varying(255),
is_tax_balancing_enabled boolean NOT NULL
);


Expand Down Expand Up @@ -7987,9 +7988,10 @@ COPY public.django_migrations (id, app, name, applied) FROM stdin;
198 netsuite 0027_auto_20240924_0820 2024-09-24 08:24:35.223017+00
199 fyle_accounting_mappings 0026_destinationattribute_code 2024-10-01 08:54:06.770864+00
200 workspaces 0039_configuration_je_single_credit_line 2024-10-11 13:43:49.169823+00
201 fyle 0034_expense_is_posted_at_null 2024-11-17 20:37:53.17847+00
202 tasks 0012_alter_tasklog_expense_group 2024-11-17 20:37:53.213044+00
203 workspaces 0040_alter_configuration_change_accounting_period 2024-11-18 04:28:36.094429+00
201 mappings 0015_generalmapping_is_tax_balancing_enabled 2024-11-11 18:30:21.068097+00
202 fyle 0034_expense_is_posted_at_null 2024-11-17 20:37:53.17847+00
203 tasks 0012_alter_tasklog_expense_group 2024-11-17 20:37:53.213044+00
204 workspaces 0040_alter_configuration_change_accounting_period 2024-11-18 04:28:36.094429+00
\.


Expand Down Expand Up @@ -11655,10 +11657,10 @@ COPY public.fyle_credentials (id, refresh_token, created_at, updated_at, workspa
-- Data for Name: general_mappings; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.general_mappings (id, location_name, location_id, accounts_payable_name, accounts_payable_id, created_at, updated_at, workspace_id, default_ccc_account_id, default_ccc_account_name, reimbursable_account_id, reimbursable_account_name, default_ccc_vendor_id, default_ccc_vendor_name, vendor_payment_account_id, vendor_payment_account_name, location_level, department_level, use_employee_department, use_employee_class, use_employee_location, department_id, department_name, override_tax_details, class_id, class_level, class_name, default_tax_code_id, default_tax_code_name) FROM stdin;
1 hubajuba 8 Accounts Payable 25 2021-11-15 08:56:31.432106+00 2021-11-15 13:21:26.113427+00 1 \N \N 118 Unapproved Expense Reports 1674 Ashwin Vendor \N \N TRANSACTION_BODY \N f f f \N \N f \N \N \N \N \N
2 \N \N Accounts Payable 25 2021-11-16 04:18:39.195287+00 2021-11-16 04:18:39.195312+00 2 228 Aus Account 118 Unapproved Expense Reports 12104 Nilesh Aus Vendor \N \N \N \N f f f \N \N f \N \N \N \N \N
3 hukiju 10 \N \N 2021-12-03 11:24:17.962764+00 2021-12-03 11:24:17.962809+00 49 228 Aus Account 228 Aus Account 12104 Nilesh Aus Vendor \N \N TRANSACTION_BODY \N f f f \N \N f \N \N \N \N \N
COPY public.general_mappings (id, location_name, location_id, accounts_payable_name, accounts_payable_id, created_at, updated_at, workspace_id, default_ccc_account_id, default_ccc_account_name, reimbursable_account_id, reimbursable_account_name, default_ccc_vendor_id, default_ccc_vendor_name, vendor_payment_account_id, vendor_payment_account_name, location_level, department_level, use_employee_department, use_employee_class, use_employee_location, department_id, department_name, override_tax_details, class_id, class_level, class_name, default_tax_code_id, default_tax_code_name, is_tax_balancing_enabled) FROM stdin;
1 hubajuba 8 Accounts Payable 25 2021-11-15 08:56:31.432106+00 2021-11-15 13:21:26.113427+00 1 \N \N 118 Unapproved Expense Reports 1674 Ashwin Vendor \N \N TRANSACTION_BODY \N f f f \N \N f \N \N \N \N \N f
2 \N \N Accounts Payable 25 2021-11-16 04:18:39.195287+00 2021-11-16 04:18:39.195312+00 2 228 Aus Account 118 Unapproved Expense Reports 12104 Nilesh Aus Vendor \N \N \N \N f f f \N \N f \N \N \N \N \N f
3 hukiju 10 \N \N 2021-12-03 11:24:17.962764+00 2021-12-03 11:24:17.962809+00 49 228 Aus Account 228 Aus Account 12104 Nilesh Aus Vendor \N \N TRANSACTION_BODY \N f f f \N \N f \N \N \N \N \N f
\.


Expand Down Expand Up @@ -11901,6 +11903,7 @@ SELECT pg_catalog.setval('public.django_content_type_id_seq', 47, true);
-- Name: django_migrations_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.django_migrations_id_seq', 201, true);
SELECT pg_catalog.setval('public.django_migrations_id_seq', 203, true);


Expand Down
30 changes: 30 additions & 0 deletions tests/test_netsuite/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def create_expense_report(db, add_netsuite_credentials, add_fyle_credentials):
)

expense_group = ExpenseGroup.objects.filter(workspace_id=1).first()
for expense in expense_group.expenses.all():
expense.workspace_id = 1
expense.save()
configuration = Configuration.objects.get(workspace_id=1)
expense_report = ExpenseReport.create_expense_report(expense_group)
expense_report_lineitems = ExpenseReportLineItem.create_expense_report_lineitems(expense_group, configuration)
Expand All @@ -89,6 +92,9 @@ def create_expense_report(db, add_netsuite_credentials, add_fyle_credentials):
def create_bill_account_based(db, add_netsuite_credentials, add_fyle_credentials):

expense_group = ExpenseGroup.objects.get(id=2)
for expense in expense_group.expenses.all():
expense.workspace_id = 1
expense.save()
configuration = Configuration.objects.get(workspace_id=1)
bill = Bill.create_bill(expense_group)
bill_lineitem = BillLineitem.create_bill_lineitems(expense_group, configuration)
Expand Down Expand Up @@ -243,6 +249,9 @@ def create_bill_task(db, add_netsuite_credentials, add_fyle_credentials):
def create_journal_entry(db, add_netsuite_credentials, add_fyle_credentials):

expense_group = ExpenseGroup.objects.filter(workspace_id=49).first()
for expense in expense_group.expenses.all():
expense.workspace_id = 1
expense.save()
configuration = Configuration.objects.get(workspace_id=1)
journal_entry = JournalEntry.create_journal_entry(expense_group)
journal_entry_lineitem = JournalEntryLineItem.create_journal_entry_lineitems(expense_group, configuration)
Expand All @@ -254,6 +263,9 @@ def create_journal_entry(db, add_netsuite_credentials, add_fyle_credentials):
def create_credit_card_charge(db, add_netsuite_credentials, add_fyle_credentials):

expense_group = ExpenseGroup.objects.filter(workspace_id=49).last()
for expense in expense_group.expenses.all():
expense.workspace_id = 49
expense.save()
configuration = Configuration.objects.get(workspace_id=1)
credit_card_charge_object = CreditCardCharge.create_credit_card_charge(expense_group)

Expand Down Expand Up @@ -292,3 +304,21 @@ def add_custom_segment(db):
custom_segments[i] = CustomSegment(**custom_segments[i])

CustomSegment.objects.bulk_create(custom_segments)

@pytest.fixture
def add_tax_destination_attributes(db):

for i in(1, 2, 49):
DestinationAttribute.objects.create(
id = 98765+i,
attribute_type='TAX_ITEM',
value='Rushikesh',
destination_id = '103578',
active = True,
detail = {
'tax_rate': 5.0
},
workspace_id = i,
created_at = datetime.now(),
updated_at = datetime.now(),
)
42 changes: 38 additions & 4 deletions tests/test_netsuite/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@
from datetime import datetime, timezone

data = {
'tax_list_detail' : {
'taxDetails': [
{
'taxType': {
'internalId': 'tax_type_1'
},
'taxCode': {
'internalId': 'tax_code_1'
},
'taxRate': 'tax_type_1',
'taxBasis': 90.0,
'taxAmount': 10.0,
'taxDetailsReference': 'EXP001'
},
{
'taxType': {
'internalId': 'tax_type_1'
},
'taxCode': {
'internalId': 'tax_code_1'
},
'taxRate': 'tax_type_1', 'taxBasis': 180.0,
'taxAmount': 20.0,
'taxDetailsReference': 'EXP002'
}
]
},
'expense':[{
"id": 1,
"employee_email": "[email protected]",
Expand Down Expand Up @@ -964,7 +991,12 @@
'taxAccount': None,
'taxBasis': None,
'tax1Amt': None,
'taxCode': None,
'taxCode': {
'externalId': None,
'internalId': None,
'name': None,
'type': 'taxGroup'
},
'taxRate1': None,
'totalAmount': None,
}, {
Expand Down Expand Up @@ -1170,17 +1202,19 @@
'location': {'internalId': None},
'customer': {'internalId': None},
'customFieldList': [{'scriptId': 'custcolfyle_expense_url',
'type': 'String',
'value': 'None/app/admin/#/enterprise/view_expense/txcKVVELn1Vl?org_id=orHe8CpW2hyN'
},{'scriptId': 'custcolfyle_expense_url_2',
'type': 'String',
'value': 'None/app/admin/#/enterprise/view_expense/txcKVVELn1Vl?org_id=orHe8CpW2hyN'
}],
'isBillable': False,
'taxAmount': None,
'taxCode': {
'name': None,
'internalId': None,
'externalId': None,
'type': 'taxGroup',
'internalId': None,
'name': None,
'type': 'taxGroup'
},
}],
'externalId': 'cc-charge 48 - [email protected]',
Expand Down
Loading
Loading