Merge PR #457 into 14.0

Signed-off-by pedrobaeza
This commit is contained in:
OCA-git-bot
2022-12-22 12:02:44 +00:00
8 changed files with 181 additions and 5 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,9 +66,16 @@ 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",
) )
amount_debit_column = fields.Char(
string="Debit amount column",
help="Debit amount of transaction in journal's currency",
)
amount_credit_column = fields.Char(
string="Credit amount column",
help="Credit amount of transaction in journal's currency",
)
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",
@@ -130,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:

View File

@@ -112,7 +112,19 @@ 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"] = (
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"] = ( 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 +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
) )
amount = values[columns["amount_column"]]
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 = ( 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 +274,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:

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

@@ -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,41 @@ 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_debit_credit_amount(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(
{
"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/debit_credit_amount.csv", "utf-8")
wizard = self.AccountStatementImport.with_context(journal_id=journal.id).create(
{
"statement_filename": "fixtures/debit_credit_amount.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), 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

@@ -56,6 +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="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" />

View File

@@ -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",
) )
amount_debit_column = fields.Char(
string="Debit amount column",
help="Debit amount of transaction in journal's currency",
)
amount_credit_column = fields.Boolean(
string="Credit amount column",
help="Credit amount of transaction in journal's currency",
)
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",

View File

@@ -50,7 +50,18 @@
widget="dynamic_dropdown" widget="dynamic_dropdown"
values="statement_columns" values="statement_columns"
context="{'header': header}" context="{'header': header}"
attrs="{'required': [('state', '=', 'final')]}" />
<field
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}"
/> />
<field <field
name="balance_column" name="balance_column"