pms: autoinvoicing cron with autocommit = False

This commit is contained in:
Darío Lodeiros
2023-03-21 17:11:22 +01:00
parent 49f3263a91
commit 4e2cd75156

View File

@@ -309,7 +309,11 @@ class PmsProperty(models.Model):
def get_real_free_rooms(self, checkin, checkout, current_lines=False):
self.ensure_one()
Avail = self.env["pms.availability"]
target_rooms = self.env["pms.room"].search([("pms_property_id", "=", self.id)])
target_rooms = (
self.env["pms.room"]
.with_context(active_test=True)
.search([("pms_property_id", "=", self.id)])
)
room_type_id = self.env.context.get("room_type_id", False)
if room_type_id:
@@ -349,7 +353,7 @@ class PmsProperty(models.Model):
domain_rooms.append(
("id", "not in", rooms_not_avail_ids),
)
return self.env["pms.room"].search(domain_rooms)
return self.env["pms.room"].with_context(active_test=True).search(domain_rooms)
@api.depends_context(
"checkin",
@@ -661,7 +665,7 @@ class PmsProperty(models.Model):
return True
@api.model
def autoinvoicing(self, offset=0, with_delay=False):
def autoinvoicing(self, offset=0, with_delay=False, autocommit=False):
"""
This method is used to invoicing automatically the folios
and validate the draft invoices created by the folios
@@ -710,56 +714,62 @@ class PmsProperty(models.Model):
def autovalidate_folio_invoice(self, invoice):
try:
invoice.action_post()
with self.env.cr.savepoint():
invoice.action_post()
except Exception as e:
invoice.message_post(body=_("Error in autovalidate invoice: " + str(e)))
def autoinvoice_folio(self, folio):
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()
# REVIEW: Reverse downpayment invoices if the downpayment is not included
# in the service invoice (qty_to_invoice < 0)
downpayment_invoices = (
folio.sale_line_ids.filtered(
lambda l: l.is_downpayment and l.qty_to_invoice < 0
)
.mapped("invoice_lines")
.mapped("move_id")
.filtered(lambda i: i.is_simplified_invoice)
)
if downpayment_invoices:
downpayment_invoices._reverse_moves(cancel=True)
invoices = folio.with_context(autoinvoice=True)._create_invoices(
grouped=True,
final=True,
)
for invoice in invoices:
if (
invoice.amount_total
> invoice.pms_property_id.max_amount_simplified_invoice
and invoice.journal_id.is_simplified_invoice
with self.env.cr.savepoint():
# 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
):
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
line._compute_autoinvoice_date()
# REVIEW: Reverse downpayment invoices if the downpayment is not included
# in the service invoice (qty_to_invoice < 0)
downpayment_invoices = (
folio.sale_line_ids.filtered(
lambda l: l.is_downpayment and l.qty_to_invoice < 0
)
.mapped("invoice_lines")
.mapped("move_id")
.filtered(lambda i: i.is_simplified_invoice)
)
if downpayment_invoices:
downpayment_invoices._reverse_moves(cancel=True)
invoices = folio.with_context(autoinvoice=True)._create_invoices(
grouped=True,
final=True,
)
for invoice in invoices:
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")
)
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."
)
folio.message_post(body=mens)
raise ValidationError(mens)
invoice.action_post()
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."
)
folio.message_post(body=mens)
raise ValidationError(mens)
invoice.action_post()
except Exception as e:
folio.message_post(body=_("Error in autoinvoicing folio: " + str(e)))