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: amount_debit_column, amount_credit_column
This commit is contained in:
@@ -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)
|
||||||
@@ -66,16 +66,15 @@ class AccountStatementImportSheetMapping(models.Model):
|
|||||||
)
|
)
|
||||||
amount_column = fields.Char(
|
amount_column = fields.Char(
|
||||||
string="Amount column",
|
string="Amount column",
|
||||||
required=True,
|
|
||||||
help="Amount of transaction in journal's currency",
|
help="Amount of transaction in journal's currency",
|
||||||
)
|
)
|
||||||
amount2_column = fields.Char(
|
amount_debit_column = fields.Char(
|
||||||
string="Amount2 column",
|
string="Debit amount column",
|
||||||
help="Some statements have two amount columns, for IN and OUT",
|
help="Debit amount of transaction in journal's currency",
|
||||||
)
|
)
|
||||||
amount2_reverse = fields.Boolean(
|
amount_credit_column = fields.Char(
|
||||||
string="Amount2 reverse +/-",
|
string="Credit amount column",
|
||||||
help="If there are positive numbers for money going OUT, reverse +/-",
|
help="Credit amount of transaction in journal's currency",
|
||||||
)
|
)
|
||||||
balance_column = fields.Char(
|
balance_column = fields.Char(
|
||||||
string="Balance column",
|
string="Balance column",
|
||||||
@@ -138,6 +137,19 @@ class AccountStatementImportSheetMapping(models.Model):
|
|||||||
help="Partner's bank account",
|
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")
|
@api.onchange("float_thousands_sep")
|
||||||
def onchange_thousands_separator(self):
|
def onchange_thousands_separator(self):
|
||||||
if "dot" == self.float_thousands_sep == self.float_decimal_sep:
|
if "dot" == self.float_thousands_sep == self.float_decimal_sep:
|
||||||
|
|||||||
@@ -112,9 +112,18 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
|||||||
columns["currency_column"] = (
|
columns["currency_column"] = (
|
||||||
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"] = (
|
||||||
columns["amount2_column"] = (
|
header.index(mapping.amount_column) if mapping.amount_column else None
|
||||||
header.index(mapping.amount2_column) if mapping.amount2_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"] = (
|
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
|
||||||
@@ -192,13 +201,17 @@ class AccountStatementImportSheetParser(models.TransientModel):
|
|||||||
if columns["currency_column"] is not None
|
if columns["currency_column"] is not None
|
||||||
else currency_code
|
else currency_code
|
||||||
)
|
)
|
||||||
factor = -1 if mapping["amount2_reverse"] else 1
|
|
||||||
if values[columns["amount_column"]] in (False, "", "0.00"):
|
def _decimal(column_name):
|
||||||
amount = values[columns["amount2_column"]]
|
if columns[column_name]:
|
||||||
amount = factor * self._parse_decimal(amount, mapping)
|
return self._parse_decimal(values[columns[column_name]], mapping)
|
||||||
else:
|
|
||||||
amount = values[columns.get("amount_column")]
|
amount = _decimal("amount_column")
|
||||||
amount = self._parse_decimal(amount, mapping)
|
if not amount:
|
||||||
|
amount = abs(_decimal("amount_debit_column") or 0)
|
||||||
|
if not amount:
|
||||||
|
amount = -abs(_decimal("amount_credit_column") or 0)
|
||||||
|
|
||||||
balance = (
|
balance = (
|
||||||
values[columns["balance_column"]]
|
values[columns["balance_column"]]
|
||||||
if columns["balance_column"] is not None
|
if columns["balance_column"] is not None
|
||||||
|
|||||||
@@ -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",""
|
|
||||||
|
5
account_statement_import_txt_xlsx/tests/fixtures/debit_credit_amount.csv
vendored
Normal file
5
account_statement_import_txt_xlsx/tests/fixtures/debit_credit_amount.csv
vendored
Normal 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",""
|
||||||
|
@@ -362,7 +362,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
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):
|
def test_debit_credit_amount(self):
|
||||||
journal = self.AccountJournal.create(
|
journal = self.AccountJournal.create(
|
||||||
{
|
{
|
||||||
"name": "Bank",
|
"name": "Bank",
|
||||||
@@ -374,17 +374,18 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
)
|
)
|
||||||
statement_map = self.sample_statement_map.copy(
|
statement_map = self.sample_statement_map.copy(
|
||||||
{
|
{
|
||||||
"amount2_column": "Amount2",
|
"amount_debit_column": "Debit",
|
||||||
"amount2_reverse": True,
|
"amount_credit_column": "Credit",
|
||||||
"balance_column": "Balance",
|
"balance_column": "Balance",
|
||||||
|
"amount_column": None,
|
||||||
"original_currency_column": None,
|
"original_currency_column": None,
|
||||||
"original_amount_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(
|
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,
|
"statement_file": data,
|
||||||
"sheet_mapping_id": statement_map.id,
|
"sheet_mapping_id": statement_map.id,
|
||||||
}
|
}
|
||||||
@@ -394,7 +395,7 @@ class TestAccountBankStatementImportTxtXlsx(common.TransactionCase):
|
|||||||
).import_file_button()
|
).import_file_button()
|
||||||
statement = self.AccountBankStatement.search([("journal_id", "=", journal.id)])
|
statement = self.AccountBankStatement.search([("journal_id", "=", journal.id)])
|
||||||
self.assertEqual(len(statement), 1)
|
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_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)
|
||||||
|
|||||||
@@ -38,10 +38,6 @@
|
|||||||
</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)]}"
|
||||||
@@ -60,7 +56,8 @@
|
|||||||
<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="amount_debit_column" />
|
||||||
|
<field name="amount_credit_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,13 +47,13 @@ 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(
|
amount_debit_column = fields.Char(
|
||||||
string="Amount2 column",
|
string="Debit amount column",
|
||||||
help="Some statements have two amount columns, for IN and OUT",
|
help="Debit amount of transaction in journal's currency",
|
||||||
)
|
)
|
||||||
amount2_reverse = fields.Boolean(
|
amount_credit_column = fields.Boolean(
|
||||||
string="Amount2 reverse +/-",
|
string="Credit amount column",
|
||||||
help="If there are positive numbers for money going OUT, reverse +/-",
|
help="Credit amount of transaction in journal's currency",
|
||||||
)
|
)
|
||||||
balance_column = fields.Char(
|
balance_column = fields.Char(
|
||||||
string="Balance column",
|
string="Balance column",
|
||||||
|
|||||||
@@ -50,10 +50,15 @@
|
|||||||
widget="dynamic_dropdown"
|
widget="dynamic_dropdown"
|
||||||
values="statement_columns"
|
values="statement_columns"
|
||||||
context="{'header': header}"
|
context="{'header': header}"
|
||||||
attrs="{'required': [('state', '=', 'final')]}"
|
|
||||||
/>
|
/>
|
||||||
<field
|
<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"
|
widget="dynamic_dropdown"
|
||||||
values="statement_columns"
|
values="statement_columns"
|
||||||
context="{'header': header}"
|
context="{'header': header}"
|
||||||
|
|||||||
Reference in New Issue
Block a user