예제 #1
0
    def load_transactions_from_workbook(self):
        accounts_table = AccountsTable(self.this_app.db_conn)
        trans_tab = TransactionsTable(self.this_app.db_conn)
        sheet = self.workbook.active

        #  The spreadsheet we are loading from does not repeat all transactions for split transactions.
        #  Theirfore it is necessary to retain the previous transactions.
        previous_transaction_date_string = "1960-01-12 00:00:00"
        previous_account_id = 0
        previous_account_name = ""
        previous_description = ""

        self.this_app.output(
            "\n\n    The following transactions have been added:\n")

        rc = 0

        for transaction in sheet.iter_rows(min_row=9,
                                           max_row=9999,
                                           min_col=1,
                                           max_col=11,
                                           values_only=True):
            if transaction[1] == self.end_of_transactions_label:
                break

            try:
                self.validate_transaction(transaction)
                account_name = transaction[account_col]
                if account_name:
                    account_id = accounts_table.get_id(account_name)
                    previous_account_id = account_id
                    previous_account_name = account_name
                else:
                    account_id = previous_account_id
                    account_name = previous_account_name

                transaction_date_string = str(transaction[trans_date_col])
                if transaction_date_string == "None":
                    transaction_date_string = previous_transaction_date_string
                else:
                    previous_transaction_date_string = transaction_date_string
                iso_date_string = str(transaction_date_string.split()[0])
                transaction_date = datetime.date.fromisoformat(iso_date_string)

                category_name = transaction[category_col]
                category_id = self.get_category_id(transaction)

                amount = transaction[amount_col]

                this_trans = Transaction(account_id, transaction_date,
                                         category_id, amount)

                this_trans.cleared = transaction[cleared_col]
                this_trans.number = transaction[number_col]
                this_trans.tag = transaction[tag_col]

                this_trans.description = transaction[description_col]
                if this_trans.description:
                    previous_description = this_trans.description
                else:
                    this_trans.description = previous_description

                this_trans.memo = transaction[memo_col]
                this_trans.tax_item = transaction[tax_col]

                trans_tab.insert_transaction(this_trans)
                self.this_app.output(
                    f"      {account_name}, {transaction_date}, {category_name}, {amount}\n"
                )

            except InvalidTransactionException as e:
                self.this_app.error(
                    "An unhandable exception has occured. Please review the log file."
                )
                self.this_app.output(f"\nError! {e.message}\n")
                self.this_app.output(f"{transaction}\n")
                self.this_app.output(
                    "\nProcessing is terminating on this file.")
                rc = 16
                break

        return rc