[IMP]pms: improvemente auto invoicing flow

This commit is contained in:
Darío Lodeiros
2023-01-10 18:01:16 +01:00
parent d9e2a50bfc
commit a133baf762
3 changed files with 40 additions and 6 deletions

View File

@@ -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
)

View File

@@ -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)))

View File

@@ -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