mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
[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:
@@ -69,6 +69,14 @@ class AccountStatementImportSheetMapping(models.Model):
|
|||||||
required=True,
|
required=True,
|
||||||
help="Amount of transaction in journal's currency",
|
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(
|
balance_column = fields.Char(
|
||||||
string="Balance column",
|
string="Balance column",
|
||||||
help="Balance after transaction in journal's currency",
|
help="Balance after transaction in journal's currency",
|
||||||
|
|||||||
@@ -113,6 +113,9 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
|||||||
header.index(mapping.currency_column) if mapping.currency_column else None
|
header.index(mapping.currency_column) if mapping.currency_column else None
|
||||||
)
|
)
|
||||||
columns["amount_column"] = header.index(mapping.amount_column)
|
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"] = (
|
columns["balance_column"] = (
|
||||||
header.index(mapping.balance_column) if mapping.balance_column else None
|
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
|
if columns["currency_column"] is not None
|
||||||
else currency_code
|
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 = (
|
balance = (
|
||||||
values[columns["balance_column"]]
|
values[columns["balance_column"]]
|
||||||
if columns["balance_column"] is not None
|
if columns["balance_column"] is not None
|
||||||
@@ -252,7 +261,6 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
|||||||
if isinstance(timestamp, str):
|
if isinstance(timestamp, str):
|
||||||
timestamp = datetime.strptime(timestamp, mapping.timestamp_format)
|
timestamp = datetime.strptime(timestamp, mapping.timestamp_format)
|
||||||
|
|
||||||
amount = self._parse_decimal(amount, mapping)
|
|
||||||
if balance:
|
if balance:
|
||||||
balance = self._parse_decimal(balance, mapping)
|
balance = self._parse_decimal(balance, mapping)
|
||||||
else:
|
else:
|
||||||
|
|||||||
3
account_statement_import_txt_xlsx/tests/fixtures/amount2.csv
vendored
Normal file
3
account_statement_import_txt_xlsx/tests/fixtures/amount2.csv
vendored
Normal 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",""
|
||||||
|
@@ -29,6 +29,15 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
self.AccountStatementImportSheetMappingWizard = self.env[
|
self.AccountStatementImportSheetMappingWizard = self.env[
|
||||||
"account.statement.import.sheet.mapping.wizard"
|
"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):
|
def _data_file(self, filename, encoding=None):
|
||||||
mode = "rt" if encoding else "rb"
|
mode = "rt" if encoding else "rb"
|
||||||
@@ -45,6 +54,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
"type": "bank",
|
"type": "bank",
|
||||||
"code": "BANK",
|
"code": "BANK",
|
||||||
"currency_id": self.currency_usd.id,
|
"currency_id": self.currency_usd.id,
|
||||||
|
"suspense_account_id": self.suspense_account.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
data = self._data_file("fixtures/sample_statement_en.csv", "utf-8")
|
data = self._data_file("fixtures/sample_statement_en.csv", "utf-8")
|
||||||
@@ -69,6 +79,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
"type": "bank",
|
"type": "bank",
|
||||||
"code": "BANK",
|
"code": "BANK",
|
||||||
"currency_id": self.currency_usd.id,
|
"currency_id": self.currency_usd.id,
|
||||||
|
"suspense_account_id": self.suspense_account.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
data = self._data_file("fixtures/empty_statement_en.csv", "utf-8")
|
data = self._data_file("fixtures/empty_statement_en.csv", "utf-8")
|
||||||
@@ -93,6 +104,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
"type": "bank",
|
"type": "bank",
|
||||||
"code": "BANK",
|
"code": "BANK",
|
||||||
"currency_id": self.currency_usd.id,
|
"currency_id": self.currency_usd.id,
|
||||||
|
"suspense_account_id": self.suspense_account.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
data = self._data_file("fixtures/sample_statement_en.xlsx")
|
data = self._data_file("fixtures/sample_statement_en.xlsx")
|
||||||
@@ -117,6 +129,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
"type": "bank",
|
"type": "bank",
|
||||||
"code": "BANK",
|
"code": "BANK",
|
||||||
"currency_id": self.currency_usd.id,
|
"currency_id": self.currency_usd.id,
|
||||||
|
"suspense_account_id": self.suspense_account.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
data = self._data_file("fixtures/empty_statement_en.xlsx")
|
data = self._data_file("fixtures/empty_statement_en.xlsx")
|
||||||
@@ -187,6 +200,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
"type": "bank",
|
"type": "bank",
|
||||||
"code": "BANK",
|
"code": "BANK",
|
||||||
"currency_id": self.currency_usd.id,
|
"currency_id": self.currency_usd.id,
|
||||||
|
"suspense_account_id": self.suspense_account.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
data = self._data_file("fixtures/original_currency.csv", "utf-8")
|
data = self._data_file("fixtures/original_currency.csv", "utf-8")
|
||||||
@@ -217,6 +231,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
"type": "bank",
|
"type": "bank",
|
||||||
"code": "BANK",
|
"code": "BANK",
|
||||||
"currency_id": self.currency_usd.id,
|
"currency_id": self.currency_usd.id,
|
||||||
|
"suspense_account_id": self.suspense_account.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
data = self._data_file("fixtures/original_currency_empty.csv", "utf-8")
|
data = self._data_file("fixtures/original_currency_empty.csv", "utf-8")
|
||||||
@@ -245,6 +260,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
"type": "bank",
|
"type": "bank",
|
||||||
"code": "BANK",
|
"code": "BANK",
|
||||||
"currency_id": self.currency_usd.id,
|
"currency_id": self.currency_usd.id,
|
||||||
|
"suspense_account_id": self.suspense_account.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
statement_map = self.sample_statement_map.copy(
|
statement_map = self.sample_statement_map.copy(
|
||||||
@@ -280,6 +296,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
"type": "bank",
|
"type": "bank",
|
||||||
"code": "BANK",
|
"code": "BANK",
|
||||||
"currency_id": self.currency_usd.id,
|
"currency_id": self.currency_usd.id,
|
||||||
|
"suspense_account_id": self.suspense_account.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
statement_map = self.sample_statement_map.copy(
|
statement_map = self.sample_statement_map.copy(
|
||||||
@@ -314,6 +331,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
"type": "bank",
|
"type": "bank",
|
||||||
"code": "BANK",
|
"code": "BANK",
|
||||||
"currency_id": self.currency_usd.id,
|
"currency_id": self.currency_usd.id,
|
||||||
|
"suspense_account_id": self.suspense_account.id,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
statement_map = self.sample_statement_map.copy(
|
statement_map = self.sample_statement_map.copy(
|
||||||
@@ -343,3 +361,40 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
self.assertEqual(statement.balance_start, 10.0)
|
self.assertEqual(statement.balance_start, 10.0)
|
||||||
self.assertEqual(statement.balance_end_real, 1510.0)
|
self.assertEqual(statement.balance_end_real, 1510.0)
|
||||||
self.assertEqual(statement.balance_end, 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)
|
||||||
|
|||||||
@@ -38,6 +38,10 @@
|
|||||||
</group>
|
</group>
|
||||||
<group>
|
<group>
|
||||||
<field name="timestamp_format" />
|
<field name="timestamp_format" />
|
||||||
|
<field
|
||||||
|
name="amount2_reverse"
|
||||||
|
attrs="{'invisible': [('amount2_column', '=', False)]}"
|
||||||
|
/>
|
||||||
</group>
|
</group>
|
||||||
<group
|
<group
|
||||||
attrs="{'invisible': [('debit_credit_column', '=', False)]}"
|
attrs="{'invisible': [('debit_credit_column', '=', False)]}"
|
||||||
@@ -56,6 +60,7 @@
|
|||||||
<field name="timestamp_column" />
|
<field name="timestamp_column" />
|
||||||
<field name="currency_column" />
|
<field name="currency_column" />
|
||||||
<field name="amount_column" />
|
<field name="amount_column" />
|
||||||
|
<field name="amount2_column" />
|
||||||
<field name="balance_column" />
|
<field name="balance_column" />
|
||||||
<field name="original_currency_column" />
|
<field name="original_currency_column" />
|
||||||
<field name="original_amount_column" />
|
<field name="original_amount_column" />
|
||||||
|
|||||||
@@ -47,6 +47,14 @@ class AccountStatementImportSheetMappingWizard(models.TransientModel):
|
|||||||
string="Amount column",
|
string="Amount column",
|
||||||
help="Amount of transaction in journal's currency",
|
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(
|
balance_column = fields.Char(
|
||||||
string="Balance column",
|
string="Balance column",
|
||||||
help="Balance after transaction in journal's currency",
|
help="Balance after transaction in journal's currency",
|
||||||
|
|||||||
@@ -52,6 +52,12 @@
|
|||||||
context="{'header': header}"
|
context="{'header': header}"
|
||||||
attrs="{'required': [('state', '=', 'final')]}"
|
attrs="{'required': [('state', '=', 'final')]}"
|
||||||
/>
|
/>
|
||||||
|
<field
|
||||||
|
name="amount2_column"
|
||||||
|
widget="dynamic_dropdown"
|
||||||
|
values="statement_columns"
|
||||||
|
context="{'header': header}"
|
||||||
|
/>
|
||||||
<field
|
<field
|
||||||
name="balance_column"
|
name="balance_column"
|
||||||
widget="dynamic_dropdown"
|
widget="dynamic_dropdown"
|
||||||
|
|||||||
Reference in New Issue
Block a user