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): def get_real_free_rooms(self, checkin, checkout, current_lines=False):
self.ensure_one() self.ensure_one()
Avail = self.env["pms.availability"] 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) room_type_id = self.env.context.get("room_type_id", False)
if room_type_id: if room_type_id:
@@ -349,7 +353,7 @@ class PmsProperty(models.Model):
domain_rooms.append( domain_rooms.append(
("id", "not in", rooms_not_avail_ids), ("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( @api.depends_context(
"checkin", "checkin",
@@ -661,7 +665,7 @@ class PmsProperty(models.Model):
return True return True
@api.model @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 This method is used to invoicing automatically the folios
and validate the draft invoices created by 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): def autovalidate_folio_invoice(self, invoice):
try: try:
invoice.action_post() with self.env.cr.savepoint():
invoice.action_post()
except Exception as e: except Exception as e:
invoice.message_post(body=_("Error in autovalidate invoice: " + str(e))) invoice.message_post(body=_("Error in autovalidate invoice: " + str(e)))
def autoinvoice_folio(self, folio): def autoinvoice_folio(self, folio):
try: try:
# REVIEW: folio sale line "_compute_auotinvoice_date" sometimes with self.env.cr.savepoint():
# dont work in services (probably cache issue¿?), we ensure that the date is # REVIEW: folio sale line "_compute_auotinvoice_date" sometimes
# set or recompute this # dont work in services (probably cache issue¿?), we ensure that the date is
for line in folio.sale_line_ids.filtered(lambda l: not l.autoinvoice_date): # set or recompute this
line._compute_autoinvoice_date() for line in folio.sale_line_ids.filtered(
# REVIEW: Reverse downpayment invoices if the downpayment is not included lambda l: not l.autoinvoice_date
# 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( line._compute_autoinvoice_date()
lambda p: p._check_enought_invoice_data() # REVIEW: Reverse downpayment invoices if the downpayment is not included
).mapped("id") # in the service invoice (qty_to_invoice < 0)
if hosts_to_invoice: downpayment_invoices = (
invoice.partner_id = hosts_to_invoice[0] folio.sale_line_ids.filtered(
invoice.journal_id = ( lambda l: l.is_downpayment and l.qty_to_invoice < 0
invoice.pms_property_id.journal_normal_invoice_id )
.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: if hosts_to_invoice:
mens = _( invoice.partner_id = hosts_to_invoice[0]
"The total amount of the simplified invoice is higher than the " invoice.journal_id = (
"maximum amount allowed for simplified invoices, and dont have " invoice.pms_property_id.journal_normal_invoice_id
"enought data in hosts to create a normal invoice." )
) else:
folio.message_post(body=mens) mens = _(
raise ValidationError(mens) "The total amount of the simplified invoice is higher than the "
invoice.action_post() "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: except Exception as e:
folio.message_post(body=_("Error in autoinvoicing folio: " + str(e))) folio.message_post(body=_("Error in autoinvoicing folio: " + str(e)))