From b34c7c410e32a525281e84301201463d72f0dbb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Sat, 19 Mar 2022 09:04:20 +0100 Subject: [PATCH] [RFC]pms: simplified invoice by journal type and out_invoice move type --- pms/__manifest__.py | 1 + pms/models/account_journal.py | 50 ++++++++++++++++++++++++++++++++++- pms/models/account_move.py | 15 +++++++---- pms/models/pms_folio.py | 17 +----------- pms/models/pms_property.py | 14 +++++++++- pms/report/invoice.xml | 31 ++++++++++++++++++++++ 6 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 pms/report/invoice.xml diff --git a/pms/__manifest__.py b/pms/__manifest__.py index e28274f0f..a14528989 100644 --- a/pms/__manifest__.py +++ b/pms/__manifest__.py @@ -42,6 +42,7 @@ "report/pms_folio.xml", "report/pms_folio_templates.xml", "report/traveller_report_action.xml", + "report/invoice.xml", # "templates/pms_email_template.xml", "data/menus.xml", "wizards/wizard_payment_folio.xml", diff --git a/pms/models/account_journal.py b/pms/models/account_journal.py index 9edb82d3c..deea22ab4 100644 --- a/pms/models/account_journal.py +++ b/pms/models/account_journal.py @@ -1,4 +1,4 @@ -from odoo import fields, models +from odoo import _, api, fields, models class AccountJournal(models.Model): @@ -20,3 +20,51 @@ class AccountJournal(models.Model): string="For manual payments", help="Use to pay for reservations", ) + is_simplified_invoice = fields.Boolean( + string="Simplified invoice", + help="Use to simplified invoice", + compute="_compute_is_simplified_invoice", + readonly=False, + store=True, + ) + + @api.depends("pms_property_ids", "pms_property_ids.journal_simplified_invoice_id") + def _compute_is_simplified_invoice(self): + self.is_simplified_invoice = False + for journal in self: + if journal.id in journal.pms_property_ids.mapped( + "journal_simplified_invoice_id.id" + ): + journal.is_simplified_invoice = True + + @api.constrains("is_simplified_invoice") + def _check_pms_properties_simplified_invoice(self): + for journal in self: + if ( + journal.is_simplified_invoice + and journal.id + in journal.pms_property_ids.mapped("journal_normal_invoice_id.id") + ): + raise models.ValidationError( + _( + "The journal %s is used for normal invoices in the properties: %s" + % ( + journal.name, + ", ".join(journal.pms_property_ids.mapped("name")), + ) + ) + ) + if ( + not journal.is_simplified_invoice + and journal.id + in journal.pms_property_ids.mapped("journal_simplified_invoice_id.id") + ): + raise models.ValidationError( + _( + "The journal %s is used for simplified invoices in the properties: %s" + % ( + journal.name, + ", ".join(journal.pms_property_ids.mapped("name")), + ) + ) + ) diff --git a/pms/models/account_move.py b/pms/models/account_move.py index 1ea62a7eb..16c51fb65 100644 --- a/pms/models/account_move.py +++ b/pms/models/account_move.py @@ -32,6 +32,11 @@ class AccountMove(models.Model): # check_pms_properties=True, ) # journal_id = fields.Many2one(check_pms_properties=True) + is_simplified_invoice = fields.Boolean( + help="Technical field to know if the invoice is simplified", + related="journal_id.is_simplified_invoice", + store=True, + ) @api.onchange("pms_property_id") def _onchange_pms_property_id(self): @@ -292,7 +297,7 @@ class AccountMove(models.Model): Check invoice and receipts legal status """ self.ensure_one() - if self.move_type == "out_invoice" and ( + if not self.journal_id.is_simplified_invoice and ( not self.partner_id or not self.partner_id._check_enought_invoice_data() ): raise UserError( @@ -301,18 +306,18 @@ class AccountMove(models.Model): " partner has the complete information required." ) ) - if self.move_type == "out_receipt": - self._check_receipt_restrictions() + if self.journal_id.is_simplified_invoice: + self._check_simplified_restrictions() return True - def _check_receipt_restrictions(self): + def _check_simplified_restrictions(self): self.ensure_one() if ( self.pms_property_id and self.amount_total > self.pms_property_id.max_amount_simplified_invoice ): mens = _( - "The total amount of the receipt is higher than the " + "The total amount of the simplified invoice is higher than the " "maximum amount allowed for simplified invoices." ) self.folio_ids.message_post(body=mens) diff --git a/pms/models/pms_folio.py b/pms/models/pms_folio.py index 31498b17a..239567cb0 100644 --- a/pms/models/pms_folio.py +++ b/pms/models/pms_folio.py @@ -1703,12 +1703,6 @@ class PmsFolio(models.Model): .with_context(default_move_type="out_invoice", auto_name=True) .create(invoice_vals) ) - else: - move = ( - self.env["account.move"] - .with_context(default_move_type="out_receipt", auto_name=True) - .create(invoice_vals) - ) moves += move return moves @@ -1773,7 +1767,7 @@ class PmsFolio(models.Model): invoice_vals = { "ref": self.client_order_ref or "", - "move_type": self._get_default_move_type(partner_invoice_id), + "move_type": "out_invoice", "narration": self.note, "currency_id": self.pricelist_id.currency_id.id, # 'campaign_id': self.campaign_id.id, @@ -1803,15 +1797,6 @@ class PmsFolio(models.Model): return pms_property.journal_simplified_invoice_id return pms_property.journal_normal_invoice_id - def _get_default_move_type(self, partner_invoice_id): - self.ensure_one() - partner = self.env["res.partner"].browse(partner_invoice_id) - if not partner._check_enought_invoice_data() and self._context.get( - "autoinvoice" - ): - return "out_receipt" - return "out_invoice" - def do_payment( self, journal, diff --git a/pms/models/pms_property.py b/pms/models/pms_property.py index 75cca3cf0..7ce8710ee 100644 --- a/pms/models/pms_property.py +++ b/pms/models/pms_property.py @@ -176,7 +176,10 @@ class PmsProperty(models.Model): journal_normal_invoice_id = fields.Many2one( string="Normal Invoice Journal", comodel_name="account.journal", - domain=[("type", "=", "sale")], + domain=[ + ("type", "=", "sale"), + ("is_simplified_invoice", "=", False), + ], help="Journal used to create the normal invoice", check_company=True, check_pms_properties=True, @@ -616,3 +619,12 @@ class PmsProperty(models.Model): if invoices: invoices.action_post() return True + + @api.constrains("journal_normal_invoice_id") + def _check_journal_normal_invoice(self): + for pms_property in self.filtered("journal_normal_invoice_id"): + if pms_property.journal_normal_invoice_id.is_simplified_invoice: + raise ValidationError( + _("Journal %s is not allowed to be used for normal invoices") + % pms_property.journal_normal_invoice_id.name + ) diff --git a/pms/report/invoice.xml b/pms/report/invoice.xml new file mode 100644 index 000000000..a7e97acc4 --- /dev/null +++ b/pms/report/invoice.xml @@ -0,0 +1,31 @@ + + + +