[MIG] account_invoice_default_journal: migrate to 18.0
This commit is contained in:
64
account_invoice_default_journal/models/account_move.py
Normal file
64
account_invoice_default_journal/models/account_move.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# Copyright 2022 Nicolas JEUDY
|
||||
# Copyright 2026 Elabore
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import _, models
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class AccountMove(models.Model):
|
||||
_inherit = "account.move"
|
||||
|
||||
def _get_forced_journal(self):
|
||||
"""
|
||||
Return the journal forced by the products of this move.
|
||||
|
||||
The journal is read from the product of each invoice line:
|
||||
``forced_journal_id`` for customer documents, and
|
||||
``forced_purchase_journal_id`` for vendor documents. Products
|
||||
carrying no forced journal are ignored.
|
||||
|
||||
Only the first record of ``self`` is considered, mirroring the
|
||||
single-record call sites in ``_search_default_journal``.
|
||||
|
||||
:return: the single forced ``account.journal`` record when exactly one
|
||||
product forces a journal. Otherwise the move's current
|
||||
``journal_id``, which is also what non-invoice moves return. An
|
||||
empty ``account.journal`` recordset when ``self`` is empty.
|
||||
:raises UserError: if the lines force more than one distinct journal,
|
||||
since a move belongs to exactly one journal.
|
||||
"""
|
||||
journal_model = self.env["account.journal"]
|
||||
for record in self:
|
||||
if record.move_type in ("out_invoice", "out_refund"):
|
||||
forced_journals = record.invoice_line_ids.product_id.forced_journal_id
|
||||
elif record.move_type in ("in_invoice", "in_refund"):
|
||||
forced_journals = (
|
||||
record.invoice_line_ids.product_id.forced_purchase_journal_id
|
||||
)
|
||||
else:
|
||||
forced_journals = journal_model
|
||||
if len(forced_journals) > 1:
|
||||
raise UserError(
|
||||
_("You can not have 2 forced journals on the same invoice !")
|
||||
)
|
||||
if len(forced_journals) == 1:
|
||||
return forced_journals
|
||||
return record.journal_id
|
||||
return journal_model
|
||||
|
||||
def _search_default_journal(self):
|
||||
"""
|
||||
Prefer the journal forced by the move products over the default one.
|
||||
|
||||
Extends the standard journal lookup so that a journal configured on a
|
||||
product takes precedence. Falls back to the standard behavior when no
|
||||
product forces a journal.
|
||||
|
||||
:return: the ``account.journal`` record to default to.
|
||||
"""
|
||||
res = super()._search_default_journal()
|
||||
forced_journal = self._get_forced_journal()
|
||||
if forced_journal:
|
||||
return forced_journal
|
||||
return res
|
||||
Reference in New Issue
Block a user