Skip to content

Commit

Permalink
[IMP] account_operating_unit: allow to pay multiple invoices
Browse files Browse the repository at this point in the history
with different operating units.

That was possible in old versions <v12 but it was removed for unknown
reason.

In general, reconciling a bank journal entry with an invoice
will require Inter OU balancing entries, as long one OU is paying
the balance for the other.
  • Loading branch information
AaronHForgeFlow authored and hbrunn committed Jul 18, 2024
1 parent c4c4c8e commit 3621bb6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 53 deletions.
1 change: 0 additions & 1 deletion account_operating_unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@

from . import models
from . import report
from . import wizards
4 changes: 3 additions & 1 deletion account_operating_unit/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ def _check_ou_balance(self, move):
def _post(self, soft=True):
ml_obj = self.env["account.move.line"]
for move in self:
if not move.company_id.ou_is_self_balanced:
if not move.company_id.ou_is_self_balanced or self.env.context.get(
"inter_ou_balance_entry", False
):
continue

# If all move lines point to the same operating unit, there's no
Expand Down
59 changes: 59 additions & 0 deletions account_operating_unit/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,62 @@ def _check_move_operating_unit(self):
" same."
)
)

def _check_ou_balance(self, lines):
# Look for the balance of each OU
ou_balance = {}
for line in lines:
if line.operating_unit_id.id not in ou_balance:
ou_balance[line.operating_unit_id.id] = 0.0
ou_balance[line.operating_unit_id.id] += line.credit - line.debit
return ou_balance

def reconcile(self):
# if one OU pays the invoices of different OU
# a regularization entry must be created (this
# was a feature in version <= 12)
if self and not self[0].company_id.ou_is_self_balanced:
return super().reconcile()
bank_journal = self.mapped("move_id.journal_id").filtered(
lambda l: l.type in ("bank", "cash")
)
if not bank_journal:
return super().reconcile()
bank_journal = bank_journal[0]
# If all move lines point to the same operating unit, there's no
# need to create a balancing move line
if len(self.mapped("operating_unit_id")) <= 1:
return super().reconcile()
# Create balancing entries for un-balanced OU's.
move_vals = self._prepare_inter_ou_balancing_move(bank_journal)
move = self.env["account.move"].create(move_vals)
ou_balances = self._check_ou_balance(self)
amls = []
for ou_id in list(ou_balances.keys()):
# If the OU is already balanced, then do not continue
if move.company_id.currency_id.is_zero(ou_balances[ou_id]):
continue
# Create a balancing move line in the operating unit
# clearing account
line_data = move._prepare_inter_ou_balancing_move_line(
move, ou_id, ou_balances
)
if line_data:
amls.append(
self.with_context(check_move_validity=False).create(line_data)
)
if amls:
move.with_context(check_move_validity=True).write(
{"line_ids": [(4, aml.id) for aml in amls]}
)
move.with_context(inter_ou_balance_entry=True).action_post()
return super().reconcile()

def _prepare_inter_ou_balancing_move(self, journal):
move_vals = {
"journal_id": journal.id,
"date": max(self.mapped("date")),
"ref": "Inter OU Balancing",
"company_id": journal.company_id.id,
}
return move_vals
13 changes: 11 additions & 2 deletions account_operating_unit/tests/test_payment_operating_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,19 @@ def test_payment_from_two_invoices(self):

register_payments.action_create_payments()
payments = self.payment_model.search([], order="id desc", limit=2)
inter_ou_moves = self.move_model.search(
[("ref", "=", "Inter OU Balancing")], order="id desc", limit=2
)
self.assertEqual(
sum(inter_ou_moves[0].mapped("line_ids.debit")), invoices[0].amount_total
)
self.assertEqual(
sum(inter_ou_moves[1].mapped("line_ids.debit")), invoices[1].amount_total
)
for payment in payments:
# Validate that inter OU balance move lines are created
self.assertEqual(len(payment.move_id.line_ids), 4)
self.assertEqual(payment.amount, invoices[0].amount_total)
self.assertEqual(len(payment.move_id.line_ids), 2)
self.assertAlmostEqual(payment.amount, invoices[0].amount_total)
self.assertEqual(payment.state, "posted")
for invoice in invoices:
self.assertEqual(invoice.payment_state, "paid")
Expand Down
3 changes: 0 additions & 3 deletions account_operating_unit/wizards/__init__.py

This file was deleted.

46 changes: 0 additions & 46 deletions account_operating_unit/wizards/account_payment_register.py

This file was deleted.

0 comments on commit 3621bb6

Please sign in to comment.