diff --git a/pms/models/pms_property.py b/pms/models/pms_property.py index cebc2e1e8..a97bfdf47 100644 --- a/pms/models/pms_property.py +++ b/pms/models/pms_property.py @@ -671,6 +671,9 @@ class PmsProperty(models.Model): and validate the draft invoices created by the folios """ date_reference = fields.Date.today() - relativedelta(days=offset) + # REVIEW: We clean the autoinvoice_date of the past draft invoices + # to avoid blocking the autoinvoicing + self.clean_date_on_past_draft_invoices(date_reference) folios = self.env["pms.folio"].search( [ ("sale_line_ids.autoinvoice_date", "=", date_reference), @@ -712,6 +715,32 @@ class PmsProperty(models.Model): self.autovalidate_folio_invoice(invoice) return True + @api.model + def clean_date_on_past_draft_invoices(self, date_reference): + """ + This method is used to clean the date on past draft invoices + """ + journal_ids = ( + self.env["account.journal"] + .search( + [ + ("type", "=", "sale"), + ("pms_property_ids", "!=", False), + ] + ) + .ids + ) + draft_invoices = self.env["account.move"].search( + [ + ("state", "=", "draft"), + ("invoice_date", "<", date_reference), + ("journal_id", "in", journal_ids), + ] + ) + if draft_invoices: + draft_invoices.write({"invoice_date": date_reference}) + return True + def autovalidate_folio_invoice(self, invoice): try: with self.env.cr.savepoint():