Skip to content

Commit b904627

Browse files
committed
[FIX] account_payment_pro: include to_pay_move_line_ids in _compute_to_pay_amount dependency
1 parent a5d3aa1 commit b904627

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

account_payment_pro/models/account_payment.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _compute_write_off_available(self):
150150
rec.env["account.write_off.type"].search([("company_ids", "=", rec.company_id.id)], limit=1)
151151
)
152152

153-
@api.constrains("to_pay_move_line_ids", "state")
153+
@api.constrains("to_pay_move_line_ids")
154154
def _check_to_pay_lines_account(self):
155155
"""TODO ver si esto tmb lo llevamos a la UI y lo mostramos como un warning.
156156
tmb podemos dar mas info al usuario en el error"""
@@ -549,7 +549,7 @@ def _compute_selected_debt(self):
549549
# factor = -1
550550
# rec.selected_debt = sum(rec.to_pay_move_line_ids._origin.mapped('amount_residual')) * factor
551551

552-
@api.depends("selected_debt", "unreconciled_amount")
552+
@api.depends("selected_debt", "unreconciled_amount", "to_pay_move_line_ids")
553553
def _compute_to_pay_amount(self):
554554
for rec in self:
555555
rec.to_pay_amount = rec.selected_debt + rec.unreconciled_amount
@@ -565,6 +565,13 @@ def _inverse_to_pay_amount(self):
565565
):
566566
rec.unreconciled_amount = rec.to_pay_amount - rec.selected_debt
567567

568+
@api.onchange("to_pay_move_line_ids")
569+
def _onchange_to_pay_move_line_ids(self):
570+
"""Evitar que se recompute automáticamente cuando el usuario edita manualmente
571+
el campo many2many desde la UI"""
572+
# Establecemos un flag para que _compute_to_pay_move_lines no se ejecute
573+
self = self.with_context(skip_auto_recompute_to_pay_lines=True)
574+
568575
# We dont set 'is_internal_transfer' as a dependencies as it could leed to recompute to_pay_move_line_ids
569576
@api.depends("partner_id", "partner_type", "company_id")
570577
def _compute_to_pay_move_lines(self):
@@ -573,6 +580,10 @@ def _compute_to_pay_move_lines(self):
573580
# if self._context.get('created_automatically'):
574581
# return
575582

583+
# No recomputar si el usuario está editando manualmente el many2many desde la UI
584+
if self._context.get("skip_auto_recompute_to_pay_lines"):
585+
return
586+
576587
# Se recomputan las lienas solo si la deuda que esta seleccionada solo si
577588
# cambio el partner, compania o partner_type
578589
records = self.filtered(lambda x: x.state == "draft")
@@ -598,6 +609,10 @@ def _get_filter_payments(self, records, extra_fields):
598609

599610
def _get_to_pay_move_lines_domain(self):
600611
self.ensure_one()
612+
# Proteger contra búsquedas si no hay partner definido
613+
if not self.partner_id:
614+
return [("id", "=", False)]
615+
601616
domain = [
602617
("partner_id", "=", self.partner_id.commercial_partner_id.id),
603618
("company_id", "=", self.company_id.id),
@@ -611,7 +626,7 @@ def _get_to_pay_move_lines_domain(self):
611626
"asset_receivable" if self.partner_type == "customer" else "liability_payable",
612627
),
613628
]
614-
# TODO revisar bien estos, no debería ser necesario, ver el blame porque se agrego lo del active_ids
629+
# Solo agregar active_ids si estamos en contexto de seleccionar líneas específicas
615630
if self.env.context.get("active_ids") and self.env.context.get("active_model") == "account.move.line":
616631
domain.append(("move_id.line_ids", "in", self.env.context.get("active_ids")))
617632
return domain
@@ -628,6 +643,7 @@ def action_add_all(self):
628643

629644
def remove_all(self):
630645
self.to_pay_move_line_ids = False
646+
self.to_pay_amount = 0.0
631647

632648
@api.constrains("partner_id", "to_pay_move_line_ids")
633649
def check_to_pay_lines(self):
@@ -662,6 +678,7 @@ def _reconcile_after_post(self):
662678

663679
def action_post(self):
664680
res = super().action_post()
681+
self._check_to_pay_lines_account()
665682
self._reconcile_after_post()
666683
return res
667684

0 commit comments

Comments
 (0)