22# For copyright and license notices, see __manifest__.py file in module root
33# directory
44##############################################################################
5- import logging
6-
75from markupsafe import Markup
86from odoo import _ , api , fields , models
97from 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 :
0 commit comments