[IMP] account_statement_import_txt_xlsx: amount_debit_column, amount_credit_column

This commit is contained in:
Henrik Norlin
2022-12-20 11:06:19 +01:00
parent eecfd33ead
commit 93e7f23549
9 changed files with 122 additions and 39 deletions

View File

@@ -0,0 +1,53 @@
# Copyright 2022 AppsToGROW - Henrik Norlin
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from openupgradelib import openupgrade
_fields_to_add = [
(
"amount_debit_column",
"account.statement.import.sheet.mapping",
"account_statement_import_sheet_mapping",
"char",
"varchar",
"account_statement_import_txt_xlsx",
),
(
"amount_credit_column",
"account.statement.import.sheet.mapping",
"account_statement_import_sheet_mapping",
"char",
"varchar",
"account_statement_import_txt_xlsx",
),
]
def amount_to_debit_amount_and_amount2_to_credit_amount(env):
cr = env.cr
sql_amount2_exists = """SELECT count(id) FROM ir_model_fields
WHERE name = 'amount2_column'
AND model = 'account.statement.import.sheet.mapping';"""
cr.execute(sql_amount2_exists)
if cr.fetchone()[0] > 0:
openupgrade.add_fields(env, _fields_to_add)
cr.execute(
"""ALTER TABLE account_statement_import_sheet_mapping
ALTER COLUMN amount_column DROP NOT NULL;"""
)
openupgrade.logged_query(
cr,
"""
UPDATE account_statement_import_sheet_mapping
SET
amount_credit_column = amount2_column,
amount_debit_column = amount_column,
amount_column = NULL
WHERE amount2_column IS NOT NULL;
""",
)
@openupgrade.migrate()
def migrate(env, version):
amount_to_debit_amount_and_amount2_to_credit_amount(env)

View File

@@ -66,16 +66,15 @@ class AccountStatementImportSheetMapping(models.Model):
)
amount_column = fields.Char(
string="Amount column",
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",
amount_debit_column = fields.Char(
string="Debit amount column",
help="Debit amount of transaction in journal's currency",
)
amount2_reverse = fields.Boolean(
string="Amount2 reverse +/-",
help="If there are positive numbers for money going OUT, reverse +/-",
amount_credit_column = fields.Char(
string="Credit amount column",
help="Credit amount of transaction in journal's currency",
)
balance_column = fields.Char(
string="Balance column",
@@ -138,6 +137,19 @@ class AccountStatementImportSheetMapping(models.Model):
help="Partner's bank account",
)
_sql_constraints = [
(
"check_amount_columns",
(
"CHECK("
"amount_column IS NULL "
"OR (amount_debit_column IS NULL AND amount_credit_column IS NULL)"
")"
),
"Use amount_column OR (amount_debit_column AND amount_credit_column).",
),
]
@api.onchange("float_thousands_sep")
def onchange_thousands_separator(self):
if "dot" == self.float_thousands_sep == self.float_decimal_sep:

View File

@@ -112,9 +112,18 @@ class AccountStatementImportSheetParser(models.TransientModel):
columns["currency_column"] = (
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["amount_column"] = (
header.index(mapping.amount_column) if mapping.amount_column else None
)
columns["amount_debit_column"] = (
header.index(mapping.amount_debit_column)
if mapping.amount_debit_column
else None
)
columns["amount_credit_column"] = (
header.index(mapping.amount_credit_column)
if mapping.amount_credit_column
else None
)
columns["balance_column"] = (
header.index(mapping.balance_column) if mapping.balance_column else None
@@ -192,13 +201,17 @@ class AccountStatementImportSheetParser(models.TransientModel):
if columns["currency_column"] is not None
else currency_code
)
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)
def _decimal(column_name):
if columns[column_name]:
return self._parse_decimal(values[columns[column_name]], mapping)
amount = _decimal("amount_column")
if not amount:
amount = abs(_decimal("amount_debit_column") or 0)
if not amount:
amount = -abs(_decimal("amount_credit_column") or 0)
balance = (
values[columns["balance_column"]]
if columns["balance_column"] is not None

View File

@@ -1,3 +0,0 @@
"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

@@ -0,0 +1,5 @@
"Date","Label","Debit","Credit","Balance","Partner Name","Bank Account"
"12/15/2018","Credit 20.00","0.00","20.00","-10.00","John Doe","123456789"
"12/15/2018","Credit 13.50","0.00","-13.50","-23.50","John Doe","123456789"
"12/15/2018","Debit 33.50","-33.50","0.00","10.00","Azure Interior",""
"12/15/2018","Debit 1500","1,500.00","0.00","1,510.00","Azure Interior",""
1 Date Label Debit Credit Balance Partner Name Bank Account
2 12/15/2018 Credit 20.00 0.00 20.00 -10.00 John Doe 123456789
3 12/15/2018 Credit 13.50 0.00 -13.50 -23.50 John Doe 123456789
4 12/15/2018 Debit 33.50 -33.50 0.00 10.00 Azure Interior
5 12/15/2018 Debit 1500 1,500.00 0.00 1,510.00 Azure Interior

View File

@@ -362,7 +362,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
self.assertEqual(statement.balance_end_real, 1510.0)
self.assertEqual(statement.balance_end, 1510.0)
def test_amount2(self):
def test_debit_credit_amount(self):
journal = self.AccountJournal.create(
{
"name": "Bank",
@@ -374,17 +374,18 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
)
statement_map = self.sample_statement_map.copy(
{
"amount2_column": "Amount2",
"amount2_reverse": True,
"amount_debit_column": "Debit",
"amount_credit_column": "Credit",
"balance_column": "Balance",
"amount_column": None,
"original_currency_column": None,
"original_amount_column": None,
}
)
data = self._data_file("fixtures/amount2.csv", "utf-8")
data = self._data_file("fixtures/debit_credit_amount.csv", "utf-8")
wizard = self.AccountStatementImport.with_context(journal_id=journal.id).create(
{
"statement_filename": "fixtures/amount2.csv",
"statement_filename": "fixtures/debit_credit_amount.csv",
"statement_file": data,
"sheet_mapping_id": statement_map.id,
}
@@ -394,7 +395,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
).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(len(statement.line_ids), 4)
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,10 +38,6 @@
</group>
<group>
<field name="timestamp_format" />
<field
name="amount2_reverse"
attrs="{'invisible': [('amount2_column', '=', False)]}"
/>
</group>
<group
attrs="{'invisible': [('debit_credit_column', '=', False)]}"
@@ -60,7 +56,8 @@
<field name="timestamp_column" />
<field name="currency_column" />
<field name="amount_column" />
<field name="amount2_column" />
<field name="amount_debit_column" />
<field name="amount_credit_column" />
<field name="balance_column" />
<field name="original_currency_column" />
<field name="original_amount_column" />

View File

@@ -47,13 +47,13 @@ class AccountStatementImportSheetMappingWizard(models.TransientModel):
string="Amount column",
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",
amount_debit_column = fields.Char(
string="Debit amount column",
help="Debit amount of transaction in journal's currency",
)
amount2_reverse = fields.Boolean(
string="Amount2 reverse +/-",
help="If there are positive numbers for money going OUT, reverse +/-",
amount_credit_column = fields.Boolean(
string="Credit amount column",
help="Credit amount of transaction in journal's currency",
)
balance_column = fields.Char(
string="Balance column",

View File

@@ -50,10 +50,15 @@
widget="dynamic_dropdown"
values="statement_columns"
context="{'header': header}"
attrs="{'required': [('state', '=', 'final')]}"
/>
<field
name="amount2_column"
name="amount_debit_column"
widget="dynamic_dropdown"
values="statement_columns"
context="{'header': header}"
/>
<field
name="amount_credit_column"
widget="dynamic_dropdown"
values="statement_columns"
context="{'header': header}"