From 7a2ef4758d5cbc6b6ce33df30c182022325ad658 Mon Sep 17 00:00:00 2001 From: Nilesh Pant <58652823+NileshPant1999@users.noreply.github.com> Date: Wed, 27 Sep 2023 16:24:47 +0530 Subject: [PATCH] bug fix: check for error key in exception response (#359) --- apps/sage_intacct/utils.py | 108 ++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 48 deletions(-) diff --git a/apps/sage_intacct/utils.py b/apps/sage_intacct/utils.py index b90d1dc3..627526b8 100644 --- a/apps/sage_intacct/utils.py +++ b/apps/sage_intacct/utils.py @@ -1131,19 +1131,22 @@ def post_expense_report(self, expense_report: ExpenseReport, expense_report_line return created_expense_report except WrongParamsError as exception: logger.info(exception.response) - sage_intacct_errors = exception.response['error'] - error_words_list = ['period', 'closed', 'Date must be on or after'] - if any(word in sage_intacct_errors[0]['description2'] for word in error_words_list): - if configuration.change_accounting_period: - first_day_of_month = datetime.today().date().replace(day=1) - expense_report_payload = self.__construct_expense_report(expense_report, expense_report_lineitems) - expense_report_payload['datecreated'] = { - 'year': first_day_of_month.year, - 'month': first_day_of_month.month, - 'day': first_day_of_month.day - }, - created_expense_report = self.connection.expense_reports.post(expense_report_payload) - return created_expense_report + if 'error' in exception.response: + sage_intacct_errors = exception.response['error'] + error_words_list = ['period', 'closed', 'Date must be on or after'] + if any(word in sage_intacct_errors[0]['description2'] for word in error_words_list): + if configuration.change_accounting_period: + first_day_of_month = datetime.today().date().replace(day=1) + expense_report_payload = self.__construct_expense_report(expense_report, expense_report_lineitems) + expense_report_payload['datecreated'] = { + 'year': first_day_of_month.year, + 'month': first_day_of_month.month, + 'day': first_day_of_month.day + }, + created_expense_report = self.connection.expense_reports.post(expense_report_payload) + return created_expense_report + else: + raise else: raise else: @@ -1161,15 +1164,18 @@ def post_bill(self, bill: Bill, bill_lineitems: List[BillLineitem]): except WrongParamsError as exception: logger.info(exception.response) - sage_intacct_errors = exception.response['error'] - error_words_list = ['period', 'closed', 'Date must be on or after'] - if any(word in sage_intacct_errors[0]['description2'] for word in error_words_list): - if configuration.change_accounting_period: - first_day_of_month = datetime.today().date().replace(day=1) - bill_payload = self.__construct_bill(bill, bill_lineitems) - bill_payload['WHENCREATED'] = first_day_of_month - created_bill = self.connection.bills.post(bill_payload) - return created_bill + if 'error' in exception.response: + sage_intacct_errors = exception.response['error'] + error_words_list = ['period', 'closed', 'Date must be on or after'] + if any(word in sage_intacct_errors[0]['description2'] for word in error_words_list): + if configuration.change_accounting_period: + first_day_of_month = datetime.today().date().replace(day=1) + bill_payload = self.__construct_bill(bill, bill_lineitems) + bill_payload['WHENCREATED'] = first_day_of_month + created_bill = self.connection.bills.post(bill_payload) + return created_bill + else: + raise else: raise else: @@ -1187,15 +1193,18 @@ def post_journal_entry(self, journal_entry: JournalEntry, journal_entry_lineitem except WrongParamsError as exception: logger.info(exception.response) - sage_intacct_errors = exception.response['error'] - error_words_list = ['period', 'closed', 'Date must be on or after'] - if any(word in sage_intacct_errors[0]['description2'] for word in error_words_list): - if configuration.change_accounting_period: - first_day_of_month = datetime.today().date().replace(day=1) - journal_entry_payload = self.__construct_journal_entry(journal_entry, journal_entry_lineitems) - journal_entry_payload['batch_date'] = first_day_of_month - created_journal_entry = self.connection.journal_entries.post(journal_entry_payload) - return created_journal_entry + if 'error' in exception.response: + sage_intacct_errors = exception.response['error'] + error_words_list = ['period', 'closed', 'Date must be on or after'] + if any(word in sage_intacct_errors[0]['description2'] for word in error_words_list): + if configuration.change_accounting_period: + first_day_of_month = datetime.today().date().replace(day=1) + journal_entry_payload = self.__construct_journal_entry(journal_entry, journal_entry_lineitems) + journal_entry_payload['batch_date'] = first_day_of_month + created_journal_entry = self.connection.journal_entries.post(journal_entry_payload) + return created_journal_entry + else: + raise else: raise else: @@ -1237,23 +1246,26 @@ def post_charge_card_transaction(self, charge_card_transaction: ChargeCardTransa except WrongParamsError as exception: logger.info(exception.response) - sage_intacct_errors = exception.response['error'] - error_words_list = ['period', 'closed', 'Date must be on or after'] - if any(word in sage_intacct_errors[0]['description2'] for word in error_words_list): - if configuration.change_accounting_period: - first_day_of_month = datetime.today().date().replace(day=1) - charge_card_transaction_payload = self.__construct_charge_card_transaction( - charge_card_transaction, charge_card_transaction_lineitems - ) - charge_card_transaction_payload['paymentdate'] = { - 'year': first_day_of_month.year, - 'month': first_day_of_month.month, - 'day': first_day_of_month.day - }, - created_charge_card_transaction = self.connection.charge_card_transactions.post( - charge_card_transaction_payload - ) - return created_charge_card_transaction + if 'error' in exception.response: + sage_intacct_errors = exception.response['error'] + error_words_list = ['period', 'closed', 'Date must be on or after'] + if any(word in sage_intacct_errors[0]['description2'] for word in error_words_list): + if configuration.change_accounting_period: + first_day_of_month = datetime.today().date().replace(day=1) + charge_card_transaction_payload = self.__construct_charge_card_transaction( + charge_card_transaction, charge_card_transaction_lineitems + ) + charge_card_transaction_payload['paymentdate'] = { + 'year': first_day_of_month.year, + 'month': first_day_of_month.month, + 'day': first_day_of_month.day + }, + created_charge_card_transaction = self.connection.charge_card_transactions.post( + charge_card_transaction_payload + ) + return created_charge_card_transaction + else: + raise else: raise else: