From a133baf76272bbd4b7c65091d1c83292393b8aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Lodeiros?= Date: Tue, 10 Jan 2023 18:01:16 +0100 Subject: [PATCH] [IMP]pms: improvemente auto invoicing flow --- pms/models/folio_sale_line.py | 7 +++---- pms/models/pms_property.py | 31 +++++++++++++++++++++++++++++++ pms_l10n_es/models/res_partner.py | 8 ++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/pms/models/folio_sale_line.py b/pms/models/folio_sale_line.py index 3219bd4ac..ec7c2c5d5 100644 --- a/pms/models/folio_sale_line.py +++ b/pms/models/folio_sale_line.py @@ -425,7 +425,6 @@ class FolioSaleLine(models.Model): "service_id.reservation_id.checkout", ) def _compute_autoinvoice_date(self): - self.autoinvoice_date = False for record in self: record.autoinvoice_date = record._get_to_invoice_date() @@ -441,7 +440,7 @@ class FolioSaleLine(models.Model): if not last_checkout: return False invoicing_policy = ( - self.pms_property_id.default_invoicing_policy + self.folio_id.pms_property_id.default_invoicing_policy if not partner or partner.invoicing_policy == "property" else partner.invoicing_policy ) @@ -449,14 +448,14 @@ class FolioSaleLine(models.Model): return False if invoicing_policy == "checkout": margin_days = ( - self.pms_property_id.margin_days_autoinvoice + self.folio_id.pms_property_id.margin_days_autoinvoice if not partner or partner.invoicing_policy == "property" else partner.margin_days_autoinvoice ) return last_checkout + timedelta(days=margin_days) if invoicing_policy == "month_day": month_day = ( - self.pms_property_id.invoicing_month_day + self.folio_id.pms_property_id.invoicing_month_day if not partner or partner.invoicing_policy == "property" else partner.invoicing_month_day ) diff --git a/pms/models/pms_property.py b/pms/models/pms_property.py index f01611d44..5b0542992 100644 --- a/pms/models/pms_property.py +++ b/pms/models/pms_property.py @@ -689,10 +689,41 @@ class PmsProperty(models.Model): ) for folio in folios_to_invoice: try: + # REVIEW: folio sale line "_compute_auotinvoice_date" sometimes + # dont work in services (probably cache issue¿?), we ensure that the date is + # set or recompute this + for line in folio.sale_line_ids.filtered( + lambda l: not l.autoinvoice_date + ): + line._compute_autoinvoice_date() invoice = folio.with_context(autoinvoice=True)._create_invoices( grouped=True, ) if invoice: + if ( + invoice.amount_total + > invoice.pms_property_id.max_amount_simplified_invoice + and invoice.journal_id.is_simplified_invoice + ): + hosts_to_invoice = ( + invoice.folio_ids.partner_invoice_ids.filtered( + lambda p: p._check_enought_invoice_data() + ).mapped("id") + ) + if hosts_to_invoice: + invoice.partner_id = hosts_to_invoice[0] + invoice.journal_id = ( + invoice.pms_property_id.journal_normal_invoice_id + ) + else: + mens = _( + "The total amount of the simplified invoice is higher than the " + "maximum amount allowed for simplified invoices, and dont have " + "enought data in hosts to create a normal invoice." + ) + if self.folio_ids: + self.folio_ids.message_post(body=mens) + raise ValidationError(mens) invoice.action_post() except Exception as e: folio.message_post(body=_("Error in autoinvoicing folio: " + str(e))) diff --git a/pms_l10n_es/models/res_partner.py b/pms_l10n_es/models/res_partner.py index 2a55a4588..00008c4ce 100644 --- a/pms_l10n_es/models/res_partner.py +++ b/pms_l10n_es/models/res_partner.py @@ -50,7 +50,9 @@ class ResPartner(models.Model): # REVIEW: Force Contrain vat # https://github.com/odoo/odoo/issues/23242 if vals.get("vat") or vals.get("country_id"): - self.check_vat() + country = self.env["res.country"].browse(vals.get("country_id")) + if country.code == "ES": + self.check_vat() self._pms_check_unique_vat() return res @@ -60,7 +62,9 @@ class ResPartner(models.Model): # REVIEW: Force Contrain vat # https://github.com/odoo/odoo/issues/23242 if vals.get("vat") and vals.get("country_id"): - records.check_vat() + country = self.env["res.country"].browse(vals.get("country_id")) + if country.code == "ES": + self.check_vat() records._pms_check_unique_vat() return records