From 2d4e6a890cb9acc9b51f13e27d1aa78c2189cd57 Mon Sep 17 00:00:00 2001 From: Camila Vives Date: Tue, 11 Aug 2026 15:16:57 +0000 Subject: [PATCH] [FIX] l10n_latam_check_ux: partner check credit over the company hierarchy _credit_debit_get of core aggregates the journal items with child_of over the root company and bypass_access, but the checks were added up for the active company only: standing on the parent company the partner credit included the invoices of the branch and not its checks, so add_check_credit stopped making sense on any installation with branches. Checks of a company of the tree that the user has not enabled were silently dropped as well. --- l10n_latam_check_ux/models/res_partner.py | 36 +++++++++++-------- .../tests/test_check_transfers.py | 29 +++++++++------ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/l10n_latam_check_ux/models/res_partner.py b/l10n_latam_check_ux/models/res_partner.py index 16632582b..78b802589 100644 --- a/l10n_latam_check_ux/models/res_partner.py +++ b/l10n_latam_check_ux/models/res_partner.py @@ -11,21 +11,27 @@ class ResPartner(models.Model): def _credit_debit_get(self): super()._credit_debit_get() for partner in self.filtered("add_check_credit"): - partner_checks = self.env["l10n_latam.check"].search( - [ - # Partner actual - ("partner_id", "=", partner.id), - # Filtro por empresa actual - ("company_id", "=", self.env.company.id), - # Filtro On-Hand - ( - "current_journal_id.inbound_payment_method_line_ids.payment_method_id.code", - "=", - "in_third_party_checks", - ), - # Cuya fecha de pago sea mayor a la de hoy - ("payment_date", ">", fields.Date.context_today(self)), - ] + # sudo + child_of para alinearnos con el _credit_debit_get de core, que agrega los + # apuntes de toda la jerarquía de compañías (branches) con bypass_access. + partner_checks = ( + self.env["l10n_latam.check"] + .sudo() + .search( + [ + # Partner actual + ("partner_id", "=", partner.id), + # Filtro por la compañía activa y sus sucursales + ("company_id", "child_of", self.env.company.root_id.id), + # Filtro On-Hand + ( + "current_journal_id.inbound_payment_method_line_ids.payment_method_id.code", + "=", + "in_third_party_checks", + ), + # Cuya fecha de pago sea mayor a la de hoy + ("payment_date", ">", fields.Date.context_today(self)), + ] + ) ) partner.credit += sum(partner_checks.mapped("amount")) diff --git a/l10n_latam_check_ux/tests/test_check_transfers.py b/l10n_latam_check_ux/tests/test_check_transfers.py index 2d2ad11e5..20c9dd65b 100644 --- a/l10n_latam_check_ux/tests/test_check_transfers.py +++ b/l10n_latam_check_ux/tests/test_check_transfers.py @@ -190,16 +190,7 @@ def test_check_operations_keep_branch_company_operations(self): Las operaciones de la sucursal de origen tienen que seguir visibles en el historial del cheque después de transferirlo a un diario de la compañía padre. """ - branch = self.env["res.company"].create({"name": "Branch", "parent_id": self.company.id}) - # the branch shares the parent chart of accounts, but the company dependent properties of - # the partner have to be set for it so the receipt can be posted - self.partner_a.with_company(branch).write( - { - "property_account_receivable_id": self.company_data["default_account_receivable"].id, - "property_account_payable_id": self.company_data["default_account_payable"].id, - } - ) - branch.transfer_account_id = self.company.transfer_account_id + branch = self._create_branch() self.assertTrue(branch.transfer_account_id, "An internal transfer account is required to run this test") branch_journal = self._create_check_journal("Branch Third Party Checks", "BTPC", company=branch) check = self._create_third_party_check(branch_journal, "UX-BRANCH-0001") @@ -345,3 +336,21 @@ def test_reject_wizard_requires_a_journal_of_the_check_company(self): self.assertEqual(wizard.company_id, self.company) with self.assertRaisesRegex(UserError, "belongs to"): wizard.action_confirm() + + def test_partner_check_credit_includes_branch_checks(self): + """El crédito por cheques tiene que abarcar el árbol de compañías, como el de core. + + `_credit_debit_get` de core agrega los apuntes con `child_of` sobre la raíz, así que + sumar los cheques de una sola compañía deja el total inconsistente: la casa central ve + las facturas de la sucursal pero no sus cheques. + """ + branch = self._create_branch() + branch_journal = self._create_check_journal("Branch Third Party Checks", "BTPC1", company=branch) + self._create_third_party_check(branch_journal, "UX-CREDIT-0001") + + partner_from_parent = self.partner_a.with_company(self.company) + partner_from_parent.add_check_credit = False + credit_without_checks = partner_from_parent.credit + partner_from_parent.add_check_credit = True + + self.assertEqual(partner_from_parent.credit - credit_without_checks, 100.0)