[IMP] account_statement_import_txt_xlsx: wizard: 2 amount columns, for IN and OUT

[IMP] account_statement_import_txt_xlsx: wizard: amount2_reverse boolean field, with tests
This commit is contained in:
Henrik Norlin
2022-04-03 15:06:07 +02:00
committed by Miquel Raïch
parent 60b4066b8a
commit cdb5dcfb7a
7 changed files with 95 additions and 2 deletions

View File

@@ -66,6 +66,14 @@ class AccountStatementImportSheetMapping(models.Model):
required=True,
help="Amount of transaction in journal's currency",
)
amount2_column = fields.Char(
string="Amount2 column",
help="Some statements have two amount columns, for IN and OUT",
)
amount2_reverse = fields.Boolean(
string="Amount2 reverse +/-",
help="If there are positive numbers for money going OUT, reverse +/-",
)
balance_column = fields.Char(
help="Balance after transaction in journal's currency",
)

View File

@@ -113,6 +113,9 @@ class AccountStatementImportSheetParser(models.TransientModel):
header.index(mapping.currency_column) if mapping.currency_column else None
)
columns["amount_column"] = header.index(mapping.amount_column)
columns["amount2_column"] = (
header.index(mapping.amount2_column) if mapping.amount2_column else None
)
columns["balance_column"] = (
header.index(mapping.balance_column) if mapping.balance_column else None
)
@@ -189,7 +192,13 @@ class AccountStatementImportSheetParser(models.TransientModel):
if columns["currency_column"] is not None
else currency_code
)
amount = values[columns["amount_column"]]
factor = -1 if mapping["amount2_reverse"] else 1
if values[columns["amount_column"]] in (False, "", "0.00"):
amount = values[columns["amount2_column"]]
amount = factor * self._parse_decimal(amount, mapping)
else:
amount = values[columns.get("amount_column")]
amount = self._parse_decimal(amount, mapping)
balance = (
values[columns["balance_column"]]
if columns["balance_column"] is not None
@@ -252,7 +261,6 @@ class AccountStatementImportSheetParser(models.TransientModel):
if isinstance(timestamp, str):
timestamp = datetime.strptime(timestamp, mapping.timestamp_format)
amount = self._parse_decimal(amount, mapping)
if balance:
balance = self._parse_decimal(balance, mapping)
else:

View File

@@ -0,0 +1,3 @@
"Date","Label","Amount","Amount2","Balance","Partner Name","Bank Account"
"12/15/2018","Your best supplier","0.00","33.50","-23.50","John Doe","123456789"
"12/15/2018","Your payment","1,533.50","0.00","1,510.00","Azure Interior",""
1 Date Label Amount Amount2 Balance Partner Name Bank Account
2 12/15/2018 Your best supplier 0.00 33.50 -23.50 John Doe 123456789
3 12/15/2018 Your payment 1,533.50 0.00 1,510.00 Azure Interior

View File

@@ -31,6 +31,15 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
self.AccountStatementImportSheetMappingWizard = self.env[
"account.statement.import.sheet.mapping.wizard"
]
self.suspense_account = self.env["account.account"].create(
{
"code": "987654",
"name": "Suspense Account",
"user_type_id": self.env.ref(
"account.data_account_type_current_assets"
).id,
}
)
def _data_file(self, filename, encoding=None):
mode = "rt" if encoding else "rb"
@@ -47,6 +56,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
data = self._data_file("fixtures/sample_statement_en.csv", "utf-8")
@@ -71,6 +81,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
data = self._data_file("fixtures/empty_statement_en.csv", "utf-8")
@@ -95,6 +106,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
data = self._data_file("fixtures/sample_statement_en.xlsx")
@@ -119,6 +131,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
data = self._data_file("fixtures/empty_statement_en.xlsx")
@@ -189,6 +202,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
data = self._data_file("fixtures/original_currency.csv", "utf-8")
@@ -219,6 +233,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
data = self._data_file("fixtures/original_currency_empty.csv", "utf-8")
@@ -247,6 +262,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
statement_map = self.sample_statement_map.copy(
@@ -282,6 +298,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
statement_map = self.sample_statement_map.copy(
@@ -316,6 +333,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
statement_map = self.sample_statement_map.copy(
@@ -345,3 +363,40 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
self.assertEqual(statement.balance_start, 10.0)
self.assertEqual(statement.balance_end_real, 1510.0)
self.assertEqual(statement.balance_end, 1510.0)
def test_amount2(self):
journal = self.AccountJournal.create(
{
"name": "Bank",
"type": "bank",
"code": "BANK",
"currency_id": self.currency_usd.id,
"suspense_account_id": self.suspense_account.id,
}
)
statement_map = self.sample_statement_map.copy(
{
"amount2_column": "Amount2",
"amount2_reverse": True,
"balance_column": "Balance",
"original_currency_column": None,
"original_amount_column": None,
}
)
data = self._data_file("fixtures/amount2.csv", "utf-8")
wizard = self.AccountStatementImport.with_context(journal_id=journal.id).create(
{
"statement_filename": "fixtures/amount2.csv",
"statement_file": data,
"sheet_mapping_id": statement_map.id,
}
)
wizard.with_context(
account_statement_import_txt_xlsx_test=True
).import_file_button()
statement = self.AccountBankStatement.search([("journal_id", "=", journal.id)])
self.assertEqual(len(statement), 1)
self.assertEqual(len(statement.line_ids), 2)
self.assertEqual(statement.balance_start, 10.0)
self.assertEqual(statement.balance_end_real, 1510.0)
self.assertEqual(statement.balance_end, 1510.0)

View File

@@ -38,6 +38,10 @@
</group>
<group>
<field name="timestamp_format" />
<field
name="amount2_reverse"
attrs="{'invisible': [('amount2_column', '=', False)]}"
/>
</group>
<group
attrs="{'invisible': [('debit_credit_column', '=', False)]}"
@@ -56,6 +60,7 @@
<field name="timestamp_column" />
<field name="currency_column" />
<field name="amount_column" />
<field name="amount2_column" />
<field name="balance_column" />
<field name="original_currency_column" />
<field name="original_amount_column" />

View File

@@ -42,6 +42,14 @@ class AccountStatementImportSheetMappingWizard(models.TransientModel):
amount_column = fields.Char(
help="Amount of transaction in journal's currency",
)
amount2_column = fields.Char(
string="Amount2 column",
help="Some statements have two amount columns, for IN and OUT",
)
amount2_reverse = fields.Boolean(
string="Amount2 reverse +/-",
help="If there are positive numbers for money going OUT, reverse +/-",
)
balance_column = fields.Char(
help="Balance after transaction in journal's currency",
)

View File

@@ -52,6 +52,12 @@
context="{'header': header}"
attrs="{'required': [('state', '=', 'final')]}"
/>
<field
name="amount2_column"
widget="dynamic_dropdown"
values="statement_columns"
context="{'header': header}"
/>
<field
name="balance_column"
widget="dynamic_dropdown"