mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[IMP]pms: improvement invoice workflow
This commit is contained in:
@@ -1836,6 +1836,7 @@ class PmsFolio(models.Model):
|
||||
"url": self.get_portal_url(),
|
||||
}
|
||||
|
||||
# flake8:noqa=C901
|
||||
def _create_invoices(
|
||||
self,
|
||||
grouped=False,
|
||||
@@ -1888,6 +1889,46 @@ class PmsFolio(models.Model):
|
||||
if not grouped:
|
||||
invoice_vals_list = self._get_group_vals_list(invoice_vals_list)
|
||||
|
||||
partner_invoice = self.env["res.partner"].browse(partner_invoice_id)
|
||||
partner_invoice_policy = (
|
||||
self.pms_property_id.default_invoicing_policy
|
||||
if partner_invoice.invoicing_policy == "property"
|
||||
else partner_invoice.invoicing_policy
|
||||
)
|
||||
|
||||
if date:
|
||||
invoice_date = date
|
||||
if partner_invoice_policy == "checkout":
|
||||
margin_days_autoinvoice = (
|
||||
self.pms_property_id.margin_days_autoinvoice
|
||||
if partner_invoice.margin_days_autoinvoice == 0
|
||||
else partner_invoice.margin_days_autoinvoice
|
||||
)
|
||||
invoice_date = max(
|
||||
self.env["pms.reservation"]
|
||||
.search([("sale_line_ids", "in", list(lines_to_invoice.keys()))])
|
||||
.mapped("checkout")
|
||||
) + datetime.timedelta(days=margin_days_autoinvoice)
|
||||
if partner_invoice_policy == "month_day":
|
||||
month_day = (
|
||||
self.pms_property_id.invoicing_month_day
|
||||
if partner_invoice.invoicing_month_day == 0
|
||||
else partner_invoice.invoicing_month_day
|
||||
)
|
||||
invoice_date = datetime.date(
|
||||
datetime.date.today().year,
|
||||
datetime.date.today().month,
|
||||
month_day,
|
||||
)
|
||||
if invoice_date < datetime.date.today():
|
||||
invoice_date = datetime.date(
|
||||
datetime.date.today().year,
|
||||
datetime.date.today().month + 1,
|
||||
month_day,
|
||||
)
|
||||
for vals in invoice_vals_list:
|
||||
vals["invoice_date"] = invoice_date
|
||||
|
||||
# 3) Create invoices.
|
||||
|
||||
# As part of the invoice creation, we make sure the
|
||||
|
||||
@@ -643,6 +643,7 @@ class PmsProperty(models.Model):
|
||||
def autoinvoicing(self):
|
||||
"""
|
||||
This method is used to invoicing automatically the folios
|
||||
and validate the draft invoices created by the folios
|
||||
"""
|
||||
folios = self.env["pms.folio"].search(
|
||||
[
|
||||
@@ -653,8 +654,9 @@ class PmsProperty(models.Model):
|
||||
paid_folios = folios.filtered(lambda f: f.pending_amount <= 0)
|
||||
unpaid_folios = folios.filtered(lambda f: f.pending_amount > 0)
|
||||
folios_to_invoice = paid_folios
|
||||
# If the folio is unpaid we will auto invoice only the
|
||||
# not cancelled lines
|
||||
for folio in unpaid_folios:
|
||||
# REVIEW: Change this by state flow folio control
|
||||
if any([res.state != "cancel" for res in folio.reservation_ids]):
|
||||
folios_to_invoice += folio
|
||||
else:
|
||||
@@ -668,6 +670,20 @@ class PmsProperty(models.Model):
|
||||
invoice.action_post()
|
||||
except Exception as e:
|
||||
folio.message_post(body=_("Error in autoinvoicing folio: " + str(e)))
|
||||
draft_invoices_to_post = self.env["account.move"].search(
|
||||
[
|
||||
("state", "=", "draft"),
|
||||
("invoice_date", "=", fields.date.today()),
|
||||
("folio_ids", "!=", False),
|
||||
]
|
||||
)
|
||||
for invoice in draft_invoices_to_post:
|
||||
try:
|
||||
invoice.action_post()
|
||||
except Exception as e:
|
||||
invoice.message_post(
|
||||
body=_("Error in autoinvoicing invoice: " + str(e))
|
||||
)
|
||||
return True
|
||||
|
||||
@api.constrains("journal_normal_invoice_id")
|
||||
|
||||
Reference in New Issue
Block a user