Skip to content

Commit

Permalink
[IMP] account_reconcile_oca: Allow to add taxes for manual operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteMeAsap committed Sep 30, 2024
1 parent e11c081 commit 68195c9
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 2 deletions.
116 changes: 115 additions & 1 deletion account_reconcile_oca/models/account_bank_statement_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ class AccountBankStatementLine(models.Model):
manual_move_id = fields.Many2one(
"account.move", default=False, store=False, prefetch=False, readonly=True
)
manual_tax_ids = fields.Many2many(
comodel_name="account.tax",
string="Taxes",
context={"active_test": False},
check_company=True,
store=False,
default=False,
prefetch=False,
help="Taxes that apply on the base amount",
)
can_reconcile = fields.Boolean(sparse="reconcile_data_info")
statement_complete = fields.Boolean(
related="statement_id.is_complete",
Expand Down Expand Up @@ -289,6 +299,7 @@ def _check_line_changed(self, line):
precision_digits=self.company_id.currency_id.decimal_places,
)
or self.manual_account_id.id != line["account_id"][0]
or self.manual_tax_ids.ids != line.get("tax_ids", [])
or self.manual_name != line["name"]
or (
self.manual_partner_id and self.manual_partner_id.name_get()[0] or False
Expand All @@ -302,6 +313,7 @@ def _onchange_manual_reconcile_reference(self):
data = self.reconcile_data_info.get("data", [])
new_data = []
related_move_line_id = False
deleted_line = {}
for line in data:
if line.get("reference") == self.manual_reference:
related_move_line_id = line.get("id")
Expand All @@ -326,6 +338,7 @@ def _onchange_manual_reconcile_reference(self):
"manual_in_currency": False,
"manual_name": False,
"manual_partner_id": False,
"manual_tax_ids": False,
"manual_line_id": False,
"manual_move_id": False,
"manual_move_type": False,
Expand All @@ -336,6 +349,7 @@ def _onchange_manual_reconcile_reference(self):
"manual_amount_in_currency": False,
}
)
deleted_line = line
continue
else:
self.manual_account_id = line["account_id"][0]
Expand Down Expand Up @@ -363,7 +377,14 @@ def _onchange_manual_reconcile_reference(self):
self.manual_move_type = self.manual_line_id.move_id.move_type
self.manual_kind = line["kind"]
self.manual_original_amount = line.get("original_amount", 0.0)
self.manual_tax_ids = (
[Command.set(line.get("tax_ids"))]
if line.get("tax_ids")
else False
)
new_data.append(line)
if deleted_line:
new_data = self._remove_tax_lines(deleted_line, new_data, [])
self.update({"manual_delete": False})
self.reconcile_data_info = self._recompute_suspense_line(
new_data,
Expand All @@ -389,14 +410,19 @@ def _onchange_manual_amount_in_currency(self):
"manual_name",
"manual_amount",
"analytic_distribution",
"manual_tax_ids",
)
def _onchange_manual_reconcile_vals(self):
self.ensure_one()
data = self.reconcile_data_info.get("data", [])
new_data = []
reconcile_auxiliary_id = self.reconcile_data_info["reconcile_auxiliary_id"]
changed_line = {}
tax_lines = []
for line in data:
if line["reference"] == self.manual_reference:
if self._check_line_changed(line):
tax_updated = self.manual_tax_ids.ids != line.get("tax_ids", [])
line.update(
{
"name": self.manual_name,
Expand All @@ -417,10 +443,20 @@ def _onchange_manual_reconcile_vals(self):
"kind": line["kind"]
if line["kind"] != "suspense"
else "other",
"tax_ids": self.manual_tax_ids.ids
if self.manual_tax_ids
else [],
}
)
if line["kind"] == "liquidity":
self._update_move_partner()
if tax_updated and line["kind"] not in ("liquidity", "tax"):
(
line,
tax_lines,
reconcile_auxiliary_id,
) = self._recompute_tax_lines(data, line)
changed_line = line
if self.manual_line_id and self.manual_line_id.id == line.get(
"original_exchange_line_id"
):
Expand All @@ -439,9 +475,13 @@ def _onchange_manual_reconcile_vals(self):
}
)
new_data.append(line)
if changed_line == line:
new_data.extend(tax_lines)
if changed_line:
new_data = self._remove_tax_lines(changed_line, new_data, tax_lines)
self.reconcile_data_info = self._recompute_suspense_line(
new_data,
self.reconcile_data_info["reconcile_auxiliary_id"],
reconcile_auxiliary_id,
self.manual_reference,
)
self.can_reconcile = self.reconcile_data_info.get("can_reconcile", False)
Expand Down Expand Up @@ -977,3 +1017,77 @@ def add_statement(self):
"split_line_id": self.id,
}
return action

def _recompute_tax_lines(self, data, line):
reconcile_auxiliary_id = self.reconcile_data_info["reconcile_auxiliary_id"]
tax_lines = []
account_account_obj = self.env["account.account"]
account_tax_obj = self.env["account.tax"]
line_amount = line.get("amount")
base_tag_ids = []
for existing_line in data:
if (
existing_line["kind"] == "tax"
and existing_line["parent_reference"] == line["reference"]
):
line_amount += existing_line["amount"]

Check warning on line 1033 in account_reconcile_oca/models/account_bank_statement_line.py

View check run for this annotation

Codecov / codecov/patch

account_reconcile_oca/models/account_bank_statement_line.py#L1033

Added line #L1033 was not covered by tests
for manual_tax_id in line.get("tax_ids", []):
manual_tax = account_tax_obj.browse(manual_tax_id)
computed_taxes_dict = manual_tax.with_context(
force_price_include=True
).json_friendly_compute_all(line_amount)
base_tag_ids.extend(computed_taxes_dict.get("base_tags", []))
taxes = computed_taxes_dict.get("taxes")
for tax in taxes:
total_amount = -tax["amount"]
account = (
account_account_obj.browse(tax.get("account_id")).name_get()[0]
if tax.get("account_id")
else self.journal_id.suspense_account_id.name_get()[0]
)
tax_line = {
"reference": "reconcile_auxiliary;%s" % reconcile_auxiliary_id,
"id": False,
"parent_reference": line["reference"],
"account_id": account,
"partner_id": self.partner_id
and self.partner_id.name_get()[0]
or (False, ""),
"date": fields.Date.to_string(self.date),
"name": tax["name"],
"amount": -total_amount,
"credit": total_amount if total_amount > 0 else 0.0,
"debit": -total_amount if total_amount < 0 else 0.0,
"kind": "tax",
"currency_id": self.company_id.currency_id.id,
"line_currency_id": self.company_id.currency_id.id,
"currency_amount": -total_amount,
"tax_tag_ids": tax["tag_ids"],
"tax_repartition_line_id": tax["tax_repartition_line_id"],
}
reconcile_auxiliary_id += 1
line_amount += total_amount
tax_lines.append(tax_line)
base_tag_ids = list(set(base_tag_ids))
line.update(
{
"amount": line_amount,
"currency_amount": line_amount,
"credit": -line_amount if line_amount < 0 else 0.0,
"debit": line_amount if line_amount > 0 else 0.0,
"tax_tag_ids": base_tag_ids,
}
)
self.manual_amount = line_amount
return line, tax_lines, reconcile_auxiliary_id

def _remove_tax_lines(self, line, data, tax_lines):
new_data = data.copy()
for existing_line in data:
if (
existing_line["kind"] == "tax"
and existing_line["parent_reference"] == line["reference"]
and existing_line not in tax_lines
):
new_data.remove(existing_line)

Check warning on line 1092 in account_reconcile_oca/models/account_bank_statement_line.py

View check run for this annotation

Codecov / codecov/patch

account_reconcile_oca/models/account_bank_statement_line.py#L1092

Added line #L1092 was not covered by tests
return new_data
39 changes: 39 additions & 0 deletions account_reconcile_oca/tests/test_bank_account_reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,3 +1155,42 @@ def test_invoice_foreign_currency_change(self):
self.assertFalse(f.add_account_move_line_id)
self.assertTrue(f.can_reconcile)
self.assertEqual(3, len(f.reconcile_data_info["data"]))

def test_reconcile_manual_tax(self):
bank_stmt = self.acc_bank_stmt_model.create(
{
"company_id": self.env.ref("base.main_company").id,
"journal_id": self.bank_journal_euro.id,
"date": time.strftime("%Y-07-15"),
"name": "test",
}
)
bank_stmt_line = self.acc_bank_stmt_line_model.create(
{
"name": "Demo tax",
"journal_id": self.bank_journal_euro.id,
"statement_id": bank_stmt.id,
"amount": 100,
"date": time.strftime("%Y-07-15"),
}
)
with Form(
bank_stmt_line,
view="account_reconcile_oca.bank_statement_line_form_reconcile_view",
) as f:
f.manual_reference = "reconcile_auxiliary;1"
f.manual_account_id = self.company.account_journal_payment_debit_account_id
f.manual_tax_ids.add(self.tax_10)
bank_stmt_line.reconcile_bank_line()
self.assertTrue(
bank_stmt_line.move_id.line_ids.filtered(
lambda r: r.account_id
== self.company.account_journal_payment_debit_account_id
and r.tax_ids == self.tax_10
)
)
self.assertTrue(
bank_stmt_line.move_id.line_ids.filtered(
lambda r: r.tax_line_id == self.tax_10
)
)
8 changes: 7 additions & 1 deletion account_reconcile_oca/views/account_bank_statement_line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,16 @@
string="Account"
attrs="{'readonly': ['|', '|', '|', ('manual_exchange_counterpart', '=', True),('manual_reference', '=', False), ('is_reconciled', '=', True), ('manual_line_id', '!=', False)]}"
/>
<field
name="manual_tax_ids"
string="Taxes"
attrs="{'readonly': ['|','|',('manual_reference', '=', False), ('is_reconciled', '=', True),('manual_kind', '=', 'liquidity')],'invisible':['|',('manual_kind', '=', 'tax'), ('manual_line_id', '!=', False)]}"
widget="many2many_tags"
/>
<field
name="manual_partner_id"
string="Partner"
attrs="{'readonly': ['|', '|', '|', ('manual_exchange_counterpart', '=', True),('manual_reference', '=', False), ('is_reconciled', '=', True), '&amp;', ('manual_line_id', '!=', False), ('manual_kind', '!=', 'liquidity')]}"
attrs="{'readonly': ['|', '|', '|', ('manual_exchange_counterpart', '=', True),('manual_reference', '=', False), ('is_reconciled', '=', True), '&amp;', ('manual_line_id', '!=', False), ('manual_kind', 'not in', ('liquidity','other'))]}"
/>
<field
name="analytic_distribution"
Expand Down

0 comments on commit 68195c9

Please sign in to comment.