From 34427f4de800a073b4e05987fd8d124fc7bf1186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Mon, 10 Jan 2022 23:37:25 +0100 Subject: [PATCH] [ADD]pms: get_default_journal with pms_property context --- pms/models/account_move.py | 34 ++++++++++++++++++++++++++++++++++ pms/models/pms_folio.py | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pms/models/account_move.py b/pms/models/account_move.py index d9ac60d18..90cc65b2b 100644 --- a/pms/models/account_move.py +++ b/pms/models/account_move.py @@ -3,6 +3,7 @@ import json from odoo import _, fields, models +from odoo.exceptions import UserError from odoo.tools import float_is_zero @@ -124,3 +125,36 @@ class AccountMove(models.Model): "type": "ir.actions.act_window", "domain": [("id", "in", payment_ids)], } + + def _search_default_journal(self, journal_types): + """ + Search for the default journal based on the journal type and property, + the parent method is overwritten to add the property filter if + default_pms_property_id is set in context + """ + journal = super(AccountMove, self)._search_default_journal(journal_types) + if self._context.get("pms_property_id"): + property_id = self._context.get("default_pms_property_id") + domain = [ + ("company_id", "=", property_id.company_id), + ("type", "in", journal_types), + ("pms_property_ids", "in", property_id), + ] + journal = self.env["account.journal"].search(domain, limit=1) + if not journal: + domain = [ + ("company_id", "=", property_id.company_id), + ("type", "in", journal_types), + ("pms_property_ids", "=", False), + ] + journal = self.env["account.journal"].search(domain, limit=1) + if not journal: + pms_property = self.env["res.company"].browse(property_id) + error_msg = _( + """No journal could be found in property %(property_name)s + for any of those types: %(journal_types)s""", + property_name=pms_property.display_name, + journal_types=", ".join(journal_types), + ) + raise UserError(error_msg) + return journal diff --git a/pms/models/pms_folio.py b/pms/models/pms_folio.py index 1658f7180..6c6908a01 100644 --- a/pms/models/pms_folio.py +++ b/pms/models/pms_folio.py @@ -1640,12 +1640,12 @@ class PmsFolio(models.Model): (making sure to call super() to establish a clean extension chain). """ self.ensure_one() - journal = ( self.env["account.move"] .with_context( default_move_type="out_invoice", default_company_id=self.company_id.id, + default_pms_property_id=self.pms_property_id.id, ) ._get_default_journal() )