Skip to content

Commit 166b634

Browse files
committed
[MIG] account_reconcile_bg: Migration to 19.0
1 parent 3fb8d89 commit 166b634

8 files changed

Lines changed: 96 additions & 242 deletions

File tree

account_reconcile_bg/__manifest__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
##############################################################################
2020
{
2121
"name": "Account Reconcile Background",
22-
"version": "18.0.1.0.0",
22+
"version": "19.0.1.0.0",
2323
"category": "Accounting",
2424
"author": "ADHOC SA",
2525
"website": "https://www.adhoc.com.ar",
2626
"license": "AGPL-3",
2727
"summary": "Process bank reconciliation in background for large payment batches",
2828
"depends": [
29-
"account_accountant",
29+
"account_accountant_batch_payment",
3030
"base_bg",
3131
],
3232
"data": [

account_reconcile_bg/data/ir_config_parameter_data.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<odoo>
33

4-
<!-- Parámetro de configuración para el umbral de líneas -->
54
<record id="config_lines_threshold" model="ir.config_parameter">
65
<field name="key">account_reconcile_bg.lines_threshold</field>
76
<field name="value">100</field>
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
2-
from . import bank_rec_widget
32
from . import account_bank_statement_line
43
from . import account_move_line
54
from . import bg_job

account_reconcile_bg/models/account_bank_statement_line.py

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# For copyright and license notices, see __manifest__.py file in module root
33
# directory
44
##############################################################################
5-
import logging
6-
75
from markupsafe import Markup
86
from odoo import _, api, fields, models
97
from odoo.exceptions import UserError
@@ -14,42 +12,68 @@ class AccountBankStatementLine(models.Model):
1412

1513
reconciliation_in_background = fields.Boolean(
1614
string="Reconciliation in Background",
17-
default=False,
1815
readonly=True,
1916
help="Indicates that this line is being reconciled in background",
2017
)
2118

22-
def _bg_validate_reconciliation(self, selected_aml_ids=None):
23-
"""
24-
Método ejecutado en background para validar la conciliación.
25-
Se llama desde el job de base_bg.
26-
27-
:param selected_aml_ids: IDs de las líneas seleccionadas por el usuario
28-
"""
19+
def set_batch_payment_bank_statement_line(self, batch_payment_id):
2920
self.ensure_one()
30-
_logger = logging.getLogger(__name__)
3121

32-
# Preparar datos para mensaje
33-
base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url")
34-
st_line_url = f"{base_url}/odoo/account.bank.statement.line/{self.id}"
35-
st_line_name = self.name or f"Line {self.id}"
22+
if self.env.context.get("account_reconcile_bg_skip"):
23+
return super().set_batch_payment_bank_statement_line(batch_payment_id)
3624

37-
try:
38-
# Crear el widget de conciliación
39-
wizard = self.env["bank.rec.widget"].with_context(default_st_line_id=self.id).new({})
25+
if self.reconciliation_in_background:
26+
raise UserError(
27+
_("This reconciliation is already being processed in background. Please wait until it finishes.")
28+
)
29+
30+
batch_payment = self.env["account.batch.payment"].browse(batch_payment_id)
31+
threshold = int(
32+
self.env["ir.config_parameter"].sudo().get_param("account_reconcile_bg.lines_threshold", "100")
33+
)
34+
if len(batch_payment.payment_ids) < threshold:
35+
return super().set_batch_payment_bank_statement_line(batch_payment_id)
36+
37+
self._enqueue_batch_reconciliation(batch_payment_id)
38+
39+
def _enqueue_batch_reconciliation(self, batch_payment_id):
40+
self.write({"reconciliation_in_background": True})
41+
self.env.flush_all()
4042

41-
_logger.info(f"[BG] Wizard created for st_line {self.id}")
43+
try:
44+
self.env["base.bg"].bg_enqueue_records(
45+
self,
46+
"_bg_set_batch_payment_bank_statement_line",
47+
threshold=1,
48+
name=_("Bank Reconciliation: %s") % self.name,
49+
priority=5,
50+
batch_payment_id=batch_payment_id,
51+
)
52+
except Exception:
53+
self.write({"reconciliation_in_background": False})
54+
raise
4255

43-
# Agregar las líneas al widget correctamente usando el método interno
44-
if selected_aml_ids:
45-
amls = self.env["account.move.line"].browse(selected_aml_ids)
46-
wizard._action_add_new_amls(amls, allow_partial=False)
56+
self.env["bus.bus"]._sendone(
57+
self.env.user.partner_id,
58+
"simple_notification",
59+
{
60+
"type": "success",
61+
"message": _(
62+
"This reconciliation is being processed in background. You will be notified when it's done."
63+
),
64+
},
65+
)
4766

48-
# Ejecutar la validación con el context manager
49-
with wizard._action_validate_method():
50-
wizard._action_validate()
67+
def _bg_set_batch_payment_bank_statement_line(self, batch_payment_id=None):
68+
self.ensure_one()
69+
base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url")
70+
st_line_url = f"{base_url}/odoo/account.bank.statement.line/{self.id}"
71+
st_line_name = self.name or f"Line {self.id}"
5172

52-
# Retornar mensaje de éxito
73+
try:
74+
self.with_context(account_reconcile_bg_skip=True).set_batch_payment_bank_statement_line(
75+
batch_payment_id
76+
)
5377
return Markup(
5478
_("Bank reconciliation completed successfully:<br><a href='%s' target='_blank'>%s</a>")
5579
% (st_line_url, st_line_name)
@@ -62,13 +86,8 @@ def _bg_validate_reconciliation(self, selected_aml_ids=None):
6286
finally:
6387
self.write({"reconciliation_in_background": False})
6488

65-
@api.constrains(
66-
"amount",
67-
"amount_currency",
68-
"currency_id",
69-
)
89+
@api.constrains("amount", "amount_currency", "currency_id")
7090
def _check_reconciliation_in_background(self):
71-
"""Valida que no se modifiquen líneas en proceso de conciliación background."""
7291
if self.env.context.get("bg_job"):
7392
return
7493
for line in self:

account_reconcile_bg/models/account_move_line.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class AccountMoveLine(models.Model):
1010

1111
reconciliation_in_background = fields.Boolean(
1212
string="Reconciliation in Background",
13-
default=False,
1413
readonly=True,
1514
help="Indicates that this line is being reconciled in background",
1615
)

account_reconcile_bg/models/bank_rec_widget.py

Lines changed: 0 additions & 120 deletions
This file was deleted.

account_reconcile_bg/models/bg_job.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,20 @@ class BgJob(models.Model):
99
_inherit = "bg.job"
1010

1111
def cancel(self, message: str | None = None):
12-
"""Override para limpiar el flag cuando se cancela el job."""
1312
res = super().cancel(message=message)
14-
self.filtered(
15-
lambda j: j.model == "account.bank.statement.line" and j.method == "_bg_validate_reconciliation"
16-
)._clean_reconciliation_flag()
13+
self._clear_reconciliation_bg_flag()
1714
return res
1815

1916
def fail(self, error_message: str):
20-
"""Override para limpiar el flag cuando falla el job."""
2117
res = super().fail(error_message)
22-
self.filtered(
23-
lambda j: j.model == "account.bank.statement.line" and j.method == "_bg_validate_reconciliation"
24-
)._clean_reconciliation_flag()
18+
self._clear_reconciliation_bg_flag()
2519
return res
2620

27-
def _clean_reconciliation_flag(self):
28-
"""Limpia el flag reconciliation_in_background para jobs de conciliación."""
29-
for job in self:
30-
kwargs = job.kwargs_json or {}
31-
# Limpiar flag de la línea de extracto
32-
record_ids = kwargs.get("_record_ids", [])
33-
if record_ids:
34-
lines = self.env["account.bank.statement.line"].browse(record_ids).exists()
35-
if lines:
36-
lines.write({"reconciliation_in_background": False})
37-
# Limpiar flag de las líneas de pago (solo las que existen)
38-
selected_aml_ids = kwargs.get("selected_aml_ids", [])
39-
if selected_aml_ids:
40-
amls = self.env["account.move.line"].browse(selected_aml_ids).exists()
41-
if amls:
42-
amls.write({"reconciliation_in_background": False})
21+
def _clear_reconciliation_bg_flag(self):
22+
for job in self.filtered(
23+
lambda j: j.model == "account.bank.statement.line"
24+
and j.method == "_bg_set_batch_payment_bank_statement_line"
25+
):
26+
self.env["account.bank.statement.line"].browse(
27+
(job.kwargs_json or {}).get("_record_ids", [])
28+
).exists().write({"reconciliation_in_background": False})

0 commit comments

Comments
 (0)