From b7228e6479a41ebd8cbec31f7428f2000b6b141f Mon Sep 17 00:00:00 2001 From: Nilesh Pant <58652823+NileshPant1999@users.noreply.github.com> Date: Tue, 13 Jun 2023 15:20:33 +0530 Subject: [PATCH] netsuite error parser support for JE and Bills (#118) --- netsuitesdk/api/journal_entries.py | 2 +- netsuitesdk/api/vendor_bills.py | 3 +- netsuitesdk/errors/errors.py | 81 ++++++++++++++++++++++++--- netsuitesdk/errors/parser.py | 4 ++ netsuitesdk/internal/client.py | 2 +- setup.py | 2 +- test/integration/test_vendor_bills.py | 2 +- test/internal/test_upsert.py | 67 +++++++++++----------- 8 files changed, 115 insertions(+), 48 deletions(-) diff --git a/netsuitesdk/api/journal_entries.py b/netsuitesdk/api/journal_entries.py index 18868ba..d1e57da 100644 --- a/netsuitesdk/api/journal_entries.py +++ b/netsuitesdk/api/journal_entries.py @@ -77,5 +77,5 @@ def post(self, data) -> OrderedDict: self.build_record_ref_fields(self.RECORD_REF_FIELDS, data, je) logger.debug('able to create je = %s', je) - res = self.ns_client.upsert(je) + res = self.ns_client.upsert(je, 'journal_entry') return self._serialize(res) diff --git a/netsuitesdk/api/vendor_bills.py b/netsuitesdk/api/vendor_bills.py index e92d182..5e446b6 100644 --- a/netsuitesdk/api/vendor_bills.py +++ b/netsuitesdk/api/vendor_bills.py @@ -67,7 +67,6 @@ def post(self, data) -> OrderedDict: if 'customFieldList' in eod and eod['customFieldList']: custom_fields = [] for field in eod['customFieldList']: - print(field['value']) if field['type'] == 'String': custom_fields.append( self.ns_client.StringCustomFieldRef( @@ -123,5 +122,5 @@ def post(self, data) -> OrderedDict: vb['entity'] = self.ns_client.RecordRef(**(data['entity'])) logger.debug('able to create vb = %s', vb) - res = self.ns_client.upsert(vb) + res = self.ns_client.upsert(vb, 'bills') return self._serialize(res) diff --git a/netsuitesdk/errors/errors.py b/netsuitesdk/errors/errors.py index 648f57a..f27855a 100644 --- a/netsuitesdk/errors/errors.py +++ b/netsuitesdk/errors/errors.py @@ -1,18 +1,81 @@ error_reference = { - "expense_report": - { - 'category_reference_error': {'regex': r"An error occured in a upsert request: Invalid category reference key \d+ for entity \d+", 'keys': ['expense_category', 'employee']}, - 'account_reference_error': {'regex': r"An error occured in a upsert request: Invalid account reference key \d+ for subsidiary \d+", 'keys': ['account', 'subsidiary']}, - 'project_reference_error': {'regex': r"An error occured in a upsert request: Invalid customer reference key \d+ for entity \d+", 'keys': ['customer', 'employee']}, - 'location_reference_error': {'regex': r"An error occured in a upsert request: Invalid location reference key \d+ for subsidiary \d+", 'keys': ['location', 'subsidiary']}, - 'department_reference_error': {'regex':r"An error occured in a upsert request: Invalid department reference key \d+ for subsidiary \d+" , 'keys': ['department', 'subsidiary']}, - 'currency_reference_error': {'regex': r"An error occured in a upsert request: Invalid currency reference key \d+ for subsidiary \d+", 'keys': ['currency', 'subsidiary']} + "expense_report": { + 'category_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid category reference key \d+ for entity \d+", + 'keys': ['expense_category', 'employee'] + }, + 'account_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid account reference key \d+ for subsidiary \d+", + 'keys': ['account', 'subsidiary'] + }, + 'project_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid customer reference key \d+ for entity \d+", + 'keys': ['customer', 'employee'] + }, + 'location_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid location reference key \d+ for subsidiary \d+", + 'keys': ['location', 'subsidiary'] + }, + 'department_reference_error': { + 'regex':r"An error occured in a upsert request: Invalid department reference key \d+ for subsidiary \d+" , + 'keys': ['department', 'subsidiary'] + }, + 'currency_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid currency reference key \d+ for subsidiary \d+", + 'keys': ['currency', 'subsidiary'] + } + }, + "bills": { + 'bill_account_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid account reference key \d+ for subsidiary \d+", + 'keys': ['account', 'subsidiary'] + }, + 'location_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid location reference key \d+ for subsidiary \d+", + 'keys': ['location', 'subsidiary'] + }, + 'department_reference_error': { + 'regex':r"An error occured in a upsert request: Invalid department reference key \d+ for subsidiary \d+" , + 'keys': ['department', 'subsidiary'] + }, + 'currency_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid currency reference key \d+ for subsidiary \d+", + 'keys': ['currency', 'subsidiary'] + }, + 'vendor_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid entity reference key \d+ for subsidiary \d+", + 'keys': ['vendor', 'subsidiary'] + } + }, + "journal_entry": { + 'location_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid location reference key \d+ for subsidiary \d+", + 'keys': ['location', 'subsidiary'] + }, + 'department_reference_error': { + 'regex':r"An error occured in a upsert request: Invalid department reference key \d+ for subsidiary \d+" , + 'keys': ['department', 'subsidiary'] + }, + 'account_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid account reference key \d+ for subsidiary \d+", + 'keys': ['account', 'subsidiary'] + }, + 'currency_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid currency reference key \d+ for subsidiary \d+", + 'keys': ['currency', 'subsidiary'] + }, + 'project_reference_error': { + 'regex': r"An error occured in a upsert request: Invalid customer reference key \d+ for entity \d+", + 'keys': ['customer', 'employee'] + }, } } + list_of_dicts = [ ['expense_category', 'employee'], ['account', 'subsidiary'], ['customer', 'employee'], ['location', 'subsidiary'], - ['department', 'subsidiary'], ['currency', 'subsidiary'] + ['department', 'subsidiary'], ['currency', 'subsidiary'], + ['vendor', 'subsdiary'] ] diff --git a/netsuitesdk/errors/parser.py b/netsuitesdk/errors/parser.py index 3d42ff1..a8b4e07 100644 --- a/netsuitesdk/errors/parser.py +++ b/netsuitesdk/errors/parser.py @@ -21,12 +21,16 @@ def get_entity_values(self, error_dict): object_1 = object_1['acctName'] return object_1, object_2['name'] + if entity_keys[0] == 'vendor': + object_1 = object_1['companyName'] if object_1['companyName'] else object_1['entityId'] + return object_1, object_2['name'] return object_1['name'], object_2['name'] def export_error_parser(self, error_dict, message): parsed_message = message + print('dsdf', list(error_dict)) if list(error_dict) in list_of_dicts: object_1, object_2 = self.get_entity_values(error_dict) entity_keys = list(error_dict) diff --git a/netsuitesdk/internal/client.py b/netsuitesdk/internal/client.py index 0eaad59..72786d2 100644 --- a/netsuitesdk/internal/client.py +++ b/netsuitesdk/internal/client.py @@ -518,7 +518,7 @@ def upsert(self, record, record_type=None): if record_type: error_parser = ErrorParser(self.get) - error_dict = export_error_matcher(exc.message, 'expense_report') + error_dict = export_error_matcher(exc.message, record_type) message = error_parser.export_error_parser(error_dict, exc.message) exc.message = message diff --git a/setup.py b/setup.py index ad274b5..e42eb32 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='netsuitesdk', - version='2.18.1', + version='2.19.0', author='Siva Narayanan', author_email='siva@fyle.in', description='Python SDK for accessing the NetSuite SOAP webservice', diff --git a/test/integration/test_vendor_bills.py b/test/integration/test_vendor_bills.py index 3141638..c460c49 100644 --- a/test/integration/test_vendor_bills.py +++ b/test/integration/test_vendor_bills.py @@ -47,4 +47,4 @@ def test_post(nc): logger.debug('rvb1 = %s', vb4) res = nc.vendor_bills.post(vb4) logger.debug('res = %s', res) - assert res['externalId'] == vb4['externalId'], 'External ID does not match' \ No newline at end of file + assert res['externalId'] == vb4['externalId'], 'External ID does not match' diff --git a/test/internal/test_upsert.py b/test/internal/test_upsert.py index c642ad4..4421dba 100644 --- a/test/internal/test_upsert.py +++ b/test/internal/test_upsert.py @@ -1,8 +1,7 @@ from netsuitesdk.internal.utils import PaginatedSearch from netsuitesdk.internal.exceptions import NetSuiteRequestError import logging -import pytest -import zeep + logger = logging.getLogger(__name__) @@ -74,6 +73,19 @@ def test_upsert_vendor_bill(ns): bill2 = ns.get(recordType='vendorBill', externalId='1234') logger.debug('bill2 = %s', str(bill2)) assert (29.99 < bill2['userTotal']) and (bill2['userTotal'] < 30.01), 'Bill total is not 30.0' + + subs_ref = ns.RecordRef(type='subsdiary', internalId=5) + bill_account_ref = ns.RecordRef(type='account', internalId=237) + vbe1['account'] = bill_account_ref + vbe1['subsdiary'] = subs_ref + + try: + record_ref = ns.upsert(bill, record_type='bills') + logger.debug('record_ref = %s', record_ref) + except NetSuiteRequestError as e: + assert e.message == 'An error occured in a upsert request: Invalid account reference key UK EXP Account for subsidiary Honeycomb Mfg..' + assert e.code == 'INVALID_KEY_OR_REF' + def test_upsert_journal_entry(ns): vendor_ref = ns.RecordRef(type='vendor', internalId=get_vendor(ns).internalId) @@ -117,48 +129,23 @@ def test_upsert_journal_entry(ns): je = ns.get(recordType='journalEntry', externalId='JE_1234') logger.debug('je = %s', str(je)) assert (je['externalId'] == 'JE_1234'), 'Journal Entry External ID does not match' - - -def test_failed_expense_report(ns): - employee_ref = ns.RecordRef(type='employee', internalId=get_employee(ns).internalId) - cat_account_ref = ns.RecordRef(type='account', internalId='3') - loc_ref = ns.RecordRef(type='location', internalId=12) - subs_ref = ns.RecordRef(type='subsdiary', internalId=5) - dep_ref = ns.RecordRef(type='department', internalId=get_department(ns).internalId) - class_ref = ns.RecordRef(type='classification', internalId=get_department(ns).internalId) - currency_ref = ns.RecordRef(type='currency', internalId=get_currency(ns).internalId) - expenses = [] - - er = ns.ExpenseReportExpense() - er['category'] = cat_account_ref - er['amount'] = 10.0 - er['department'] = dep_ref - er['class'] = class_ref - er['location'] = loc_ref - er['currency'] = currency_ref - er['subsdiary'] = subs_ref - expenses.append(er) + cat_account_ref = ns.RecordRef(type='account', internalId=237) + credit_line['account'] = cat_account_ref - expense_report = ns.ExpenseReport(externalId='EXPR_1') - expense_report['expenseReportCurrency'] = currency_ref # US dollar - expense_report['exchangerate'] = 1.0 - expense_report['expenseList'] = ns.ExpenseReportExpenseList(expense=expenses) - expense_report['memo'] = 'test memo' - expense_report['entity'] = employee_ref - logger.debug('upserting expense report %s', expense_report) - try: - record_ref = ns.upsert(expense_report, record_type='expense_report') + record_ref = ns.upsert(journal_entry, 'journal_entry') logger.debug('record_ref = %s', record_ref) except NetSuiteRequestError as e: - assert e.message == 'An error occured in a upsert request: Invalid location reference key UK Location for subsidiary Honeycomb Mfg..' + assert e.message == 'An error occured in a upsert request: Invalid account reference key UK EXP Account for subsidiary Honeycomb Mfg..' assert e.code == 'INVALID_KEY_OR_REF' + def test_upsert_expense_report(ns): employee_ref = ns.RecordRef(type='employee', internalId=get_employee(ns).internalId) bill_account_ref = ns.RecordRef(type='account', internalId=25) cat_account_ref = ns.RecordRef(type='account', internalId='3') + loc_ref = ns.RecordRef(type='location', internalId=get_location(ns).internalId) dep_ref = ns.RecordRef(type='department', internalId=get_department(ns).internalId) class_ref = ns.RecordRef(type='classification', internalId=get_department(ns).internalId) @@ -188,3 +175,17 @@ def test_upsert_expense_report(ns): expr = ns.get(recordType='ExpenseReport', externalId='EXPR_1') logger.debug('expense report = %s', str(expr)) + + + loc_ref = ns.RecordRef(type='location', internalId=12) + subs_ref = ns.RecordRef(type='subsdiary', internalId=5) + + er['location'] = loc_ref + er['subsdiary'] = subs_ref + + try: + record_ref = ns.upsert(expense_report, record_type='expense_report') + logger.debug('record_ref = %s', record_ref) + except NetSuiteRequestError as e: + assert e.message == 'An error occured in a upsert request: Invalid location reference key UK Location for subsidiary Honeycomb Mfg..' + assert e.code == 'INVALID_KEY_OR_REF'