Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions l10n_latam_check_ux/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
29 changes: 19 additions & 10 deletions l10n_latam_check_ux/tests/test_check_transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Loading