mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[REF]pms: refactoring of sending manual mails through mail.compose
This commit is contained in:
@@ -9,11 +9,28 @@ class MailComposeMessage(models.TransientModel):
|
||||
|
||||
def send_mail(self, auto_commit=False):
|
||||
res = super(MailComposeMessage, self).send_mail(auto_commit=auto_commit)
|
||||
if self._context.get("record_id"):
|
||||
folio = self.env["pms.folio"].search(
|
||||
[("id", "=", self._context.get("record_id"))]
|
||||
)
|
||||
if (
|
||||
self._context.get("default_model") == "pms.folio"
|
||||
and self._context.get("active_model") == "pms.reservation"
|
||||
):
|
||||
folio = self.env["pms.folio"].browse(self._context.get("default_res_id"))
|
||||
reservations = folio.reservation_ids
|
||||
for reservation in reservations:
|
||||
reservation.to_send_mail = False
|
||||
elif (
|
||||
self._context.get("default_model") == "pms.reservation"
|
||||
or self._context.get("default_model") == "pms.checkin.partner"
|
||||
) and self._context.get("active_model") == "pms.reservation":
|
||||
reservation = self.env["pms.reservation"].browse(
|
||||
self._context.get("active_id")
|
||||
)
|
||||
reservation.to_send_mail = False
|
||||
elif (
|
||||
self._context.get("default_model") == "pms.checkin.partner"
|
||||
and self._context.get("active_model") == "pms.reservation"
|
||||
):
|
||||
reservation = self.env["pms.reservation"].search(
|
||||
self._context.get("default_res_id")
|
||||
)
|
||||
reservation.to_send_mail = False
|
||||
return res
|
||||
|
||||
@@ -14,8 +14,6 @@ from odoo.exceptions import AccessError, UserError, ValidationError
|
||||
from odoo.tools import float_compare, float_is_zero
|
||||
from odoo.tools.misc import get_lang
|
||||
|
||||
from odoo.addons.base.models.ir_mail_server import MailDeliveryException
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -1525,157 +1523,109 @@ class PmsFolio(models.Model):
|
||||
|
||||
# CHECKIN/OUT PROCESS
|
||||
|
||||
@api.model
|
||||
def send_confirmation_mail(self):
|
||||
folios = self.env["pms.folio"].search(
|
||||
[
|
||||
("pms_property_id.is_confirmed_auto_mail", "=", True),
|
||||
("reservation_ids.to_send_mail", "=", True),
|
||||
("reservation_ids.is_modified_reservation", "=", False),
|
||||
("reservation_ids.state", "!=", "cancel"),
|
||||
]
|
||||
)
|
||||
for folio in folios:
|
||||
if folio.email and folio.create_date.date() == fields.Date.today():
|
||||
template = folio.pms_property_id.property_confirmed_template
|
||||
try:
|
||||
template.send_mail(
|
||||
folio.id, force_send=True, email_values={"auto_delete": False}
|
||||
)
|
||||
except MailDeliveryException:
|
||||
self.env["ir.logging"].create(
|
||||
{
|
||||
"name": "Failed to send confirmation email to "
|
||||
+ folio.email,
|
||||
"type": "server",
|
||||
"path": "pms/pms/models/pms_folio.py",
|
||||
"line": "1281",
|
||||
"func": "send_confirmation_email",
|
||||
"message": "Confirmation Mail Delivery Failed",
|
||||
}
|
||||
)
|
||||
for reservation in folio.reservation_ids:
|
||||
reservation.to_send_mail = False
|
||||
|
||||
@api.model
|
||||
def send_modification_mail(self):
|
||||
folios = self.env["pms.folio"].search(
|
||||
[
|
||||
("pms_property_id.is_modified_auto_mail", "=", True),
|
||||
("reservation_ids.to_send_mail", "=", True),
|
||||
("reservation_ids.is_modified_reservation", "=", True),
|
||||
("reservation_ids.state", "!=", "cancel"),
|
||||
]
|
||||
)
|
||||
for folio in folios:
|
||||
if folio.email:
|
||||
template = folio.pms_property_id.property_modified_template
|
||||
try:
|
||||
template.send_mail(
|
||||
folio.id, force_send=True, email_values={"auto_delete": False}
|
||||
)
|
||||
except MailDeliveryException:
|
||||
self.env["ir.logging"].create(
|
||||
{
|
||||
"name": "Failed to send modification email to "
|
||||
+ folio.email,
|
||||
"type": "server",
|
||||
"path": "pms/pms/models/pms_folio.py",
|
||||
"line": "1311",
|
||||
"func": "send_modification_email",
|
||||
"message": "Modification Mail Delivery Failed",
|
||||
}
|
||||
)
|
||||
for reservation in folio.reservation_ids:
|
||||
reservation.to_send_mail = False
|
||||
|
||||
@api.model
|
||||
def send_cancelation_mail(self):
|
||||
folios = self.env["pms.folio"].search(
|
||||
[("pms_property_id.is_canceled_auto_mail", "=", True)]
|
||||
)
|
||||
for folio in folios:
|
||||
reservations = folio.reservation_ids.filtered(lambda r: r.state in "cancel")
|
||||
for reservation in reservations:
|
||||
if reservation.email:
|
||||
if (
|
||||
not reservation.to_send_mail
|
||||
and reservation.email
|
||||
and reservation.state not in "out"
|
||||
):
|
||||
template = (
|
||||
reservation.pms_property_id.property_canceled_template
|
||||
)
|
||||
try:
|
||||
template.send_mail(
|
||||
reservation.id,
|
||||
force_send=True,
|
||||
email_values={"auto_delete": False},
|
||||
)
|
||||
except MailDeliveryException:
|
||||
self.env["ir.logging"].create(
|
||||
{
|
||||
"name": "Failed to send cancellation email to "
|
||||
+ reservation.email,
|
||||
"type": "server",
|
||||
"path": "pms/pms/models/pms_folio.py",
|
||||
"line": "1345",
|
||||
"func": "send_cancelation_email",
|
||||
"message": "Cancellation Mail Delivery Failed",
|
||||
}
|
||||
)
|
||||
reservation.to_send_mail = False
|
||||
|
||||
def action_open_mail_composer(self):
|
||||
self.ensure_one()
|
||||
template = False
|
||||
pms_property = self.pms_property_id
|
||||
if (
|
||||
all(reservation.to_send_mail for reservation in self.reservation_ids)
|
||||
and not all(
|
||||
reservation.is_modified_reservation
|
||||
for reservation in self.reservation_ids
|
||||
)
|
||||
and all(
|
||||
reservation.state not in "cancel"
|
||||
for reservation in self.reservation_ids
|
||||
)
|
||||
res_ids = []
|
||||
partner_ids = []
|
||||
if all(
|
||||
reservation.to_send_mail
|
||||
and not reservation.is_modified_reservation
|
||||
and reservation.state in "confirm"
|
||||
for reservation in self.reservation_ids
|
||||
):
|
||||
if pms_property.property_confirmed_template:
|
||||
template = pms_property.property_confirmed_template
|
||||
elif (
|
||||
any(reservation.to_send_mail for reservation in self.reservation_ids)
|
||||
and any(
|
||||
reservation.is_modified_reservation
|
||||
for reservation in self.reservation_ids
|
||||
)
|
||||
and all(
|
||||
reservation.state not in "cancel"
|
||||
for reservation in self.reservation_ids
|
||||
)
|
||||
):
|
||||
if pms_property.property_modified_template:
|
||||
template = pms_property.property_modified_template
|
||||
if self.pms_property_id.property_confirmed_template:
|
||||
template = self.pms_property_id.property_confirmed_template
|
||||
else:
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You must select a confirmation template "
|
||||
"in the email configuration menu of the property"
|
||||
)
|
||||
)
|
||||
model = "pms.folio"
|
||||
partner_ids = [self.partner_id.id]
|
||||
res_id = self.id
|
||||
composition_mode = "comment"
|
||||
elif any(
|
||||
reservation.to_send_mail for reservation in self.reservation_ids
|
||||
) and any(
|
||||
reservation.state in "cancel" for reservation in self.reservation_ids
|
||||
reservation.to_send_mail and reservation.is_modified_reservation
|
||||
for reservation in self.reservation_ids
|
||||
) and all(
|
||||
reservation.state not in "cancel" for reservation in self.reservation_ids
|
||||
):
|
||||
if pms_property.property_canceled_template:
|
||||
template = pms_property.property_canceled_template
|
||||
if self.pms_property_id.property_modified_template:
|
||||
template = self.pms_property_id.property_modified_template
|
||||
else:
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You must select a modification template "
|
||||
"in the email configuration menu of the property"
|
||||
)
|
||||
)
|
||||
model = "pms.folio"
|
||||
partner_ids = [self.partner_id.id]
|
||||
res_id = self.id
|
||||
composition_mode = "comment"
|
||||
elif any(
|
||||
reservation.to_send_mail and reservation.state in "cancel"
|
||||
for reservation in self.reservation_ids
|
||||
):
|
||||
if self.pms_property_id.property_canceled_template:
|
||||
template = self.pms_property_id.property_canceled_template
|
||||
else:
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You must select a cancelation template "
|
||||
"in the email configuration menu of the property"
|
||||
)
|
||||
)
|
||||
model = "pms.reservation"
|
||||
composition_mode = "mass_mail"
|
||||
for reservation in self.reservation_ids:
|
||||
if reservation.state in "cancel" and reservation.to_send_mail:
|
||||
partner_ids.append(reservation.partner_id.id)
|
||||
res_ids.append(reservation.id)
|
||||
elif any(
|
||||
reservation.to_send_mail and reservation.state in "done"
|
||||
for reservation in self.reservation_ids
|
||||
):
|
||||
if self.pms_property_id.property_exit_template:
|
||||
template = self.pms_property_id.property_exit_template
|
||||
else:
|
||||
raise ValidationError(
|
||||
_(
|
||||
"You must select a exit template in "
|
||||
"the email configuration menu of the property"
|
||||
)
|
||||
)
|
||||
model = "pms.checkin.partner"
|
||||
composition_mode = "mass_mail"
|
||||
for checkin_partner in self.checkin_partner_ids:
|
||||
if (
|
||||
checkin_partner.state == "done"
|
||||
and checkin_partner.reservation_id.to_send_mail
|
||||
):
|
||||
partner_ids.append(checkin_partner.partner_id.id)
|
||||
res_ids.append(checkin_partner.id)
|
||||
compose_form = self.env.ref(
|
||||
"mail.email_compose_message_wizard_form", raise_if_not_found=False
|
||||
)
|
||||
ctx = dict(
|
||||
model="pms.folio",
|
||||
default_model="pms.folio",
|
||||
default_res_id=self.id,
|
||||
model=model,
|
||||
default_model=model,
|
||||
default_template_id=template and template.id or False,
|
||||
composition_mode="comment",
|
||||
partner_ids=[self.partner_id.id],
|
||||
default_composition_mode=composition_mode,
|
||||
partner_ids=partner_ids,
|
||||
force_email=True,
|
||||
record_id=self.id,
|
||||
)
|
||||
if composition_mode == "comment":
|
||||
ctx.update(
|
||||
default_res_id=res_id,
|
||||
record_id=res_id,
|
||||
)
|
||||
else:
|
||||
ctx.update(
|
||||
active_ids=res_ids,
|
||||
)
|
||||
return {
|
||||
"name": _("Send Mail "),
|
||||
"type": "ir.actions.act_window",
|
||||
|
||||
@@ -126,6 +126,11 @@ class PmsProperty(models.Model):
|
||||
comodel_name="mail.template",
|
||||
)
|
||||
|
||||
property_exit_template = fields.Many2one(
|
||||
string="Exit Email",
|
||||
comodel_name="mail.template",
|
||||
)
|
||||
|
||||
property_canceled_template = fields.Many2one(
|
||||
string="Cancellation Email",
|
||||
help="Cancellation email template",
|
||||
@@ -134,6 +139,7 @@ class PmsProperty(models.Model):
|
||||
|
||||
is_confirmed_auto_mail = fields.Boolean(string="Auto Send Confirmation Mail")
|
||||
is_modified_auto_mail = fields.Boolean(string="Auto Send Modification Mail")
|
||||
is_exit_auto_mail = fields.Boolean(string="Auto Send Exit Mail")
|
||||
is_canceled_auto_mail = fields.Boolean(string="Auto Send Cancellation Mail")
|
||||
|
||||
default_invoicing_policy = fields.Selection(
|
||||
|
||||
@@ -655,10 +655,11 @@ class PmsReservation(models.Model):
|
||||
inverse_name="reservation_possible_customer_id",
|
||||
)
|
||||
to_send_mail = fields.Boolean(
|
||||
string="Mail Sent",
|
||||
string="To Send Mail",
|
||||
compute="_compute_to_send_mail",
|
||||
readonly=False,
|
||||
store=True,
|
||||
default=False,
|
||||
)
|
||||
|
||||
is_modified_reservation = fields.Boolean(
|
||||
@@ -1515,9 +1516,13 @@ class PmsReservation(models.Model):
|
||||
for record in self:
|
||||
if record.state in "draft":
|
||||
record.is_modified_reservation = False
|
||||
elif record.state in ("confirm", "onboard") and not record.to_send_mail:
|
||||
elif (
|
||||
record._origin.checkin != record.checkin
|
||||
or record._origin.checkout != record.checkout
|
||||
) and not record.to_send_mail:
|
||||
record.is_modified_reservation = True
|
||||
record.to_send_mail = True
|
||||
for reservations in record.folio_id.reservation_ids:
|
||||
reservations.to_send_mail = True
|
||||
else:
|
||||
record.is_modified_reservation = False
|
||||
|
||||
@@ -1529,13 +1534,13 @@ class PmsReservation(models.Model):
|
||||
else:
|
||||
record.lang = self.env["res.lang"].get_installed()
|
||||
|
||||
@api.depends("reservation_type")
|
||||
@api.depends("reservation_type", "state")
|
||||
def _compute_to_send_mail(self):
|
||||
for record in self:
|
||||
if record.state in ("confirm", "done", "cancel"):
|
||||
record.to_send_mail = True
|
||||
if record.reservation_type == "out":
|
||||
record.to_send_mail = False
|
||||
else:
|
||||
record.to_send_mail = True
|
||||
|
||||
def _search_allowed_checkin(self, operator, value):
|
||||
if operator not in ("=",):
|
||||
@@ -1996,7 +2001,6 @@ class PmsReservation(models.Model):
|
||||
else:
|
||||
record.state = "cancel"
|
||||
record.folio_id._compute_amount()
|
||||
record.to_send_mail = True
|
||||
|
||||
def action_assign(self):
|
||||
for record in self:
|
||||
|
||||
@@ -47,7 +47,7 @@
|
||||
name="action_open_mail_composer"
|
||||
string="Send Confirmation Email "
|
||||
type="object"
|
||||
attrs="{'invisible':['|','|','|',('to_send_mail', '=', False),('is_modified_reservation', '=', True),('state', 'in', 'cancel'),('reservation_type', 'in', 'out')]}"
|
||||
attrs="{'invisible':['|','|','|',('to_send_mail', '=', False),('is_modified_reservation', '=', True),('state', 'not in', 'confirm'),('reservation_type', 'in', 'out')]}"
|
||||
/>
|
||||
<button
|
||||
name="action_open_mail_composer"
|
||||
@@ -55,6 +55,12 @@
|
||||
type="object"
|
||||
attrs="{'invisible':['|','|','|',('to_send_mail', '=', False), ('is_modified_reservation', '=', False), ('state', 'in', 'cancel'),('reservation_type', 'in', 'out')]}"
|
||||
/>
|
||||
<button
|
||||
name="action_open_mail_composer"
|
||||
string="Send Exit Email "
|
||||
type="object"
|
||||
attrs="{'invisible':['|','|',('to_send_mail', '=', False), ('state', 'not in', 'done'),('reservation_type', 'in', 'out')]}"
|
||||
/>
|
||||
<button
|
||||
name="action_open_mail_composer"
|
||||
string="Send Cancellation Email "
|
||||
@@ -62,8 +68,8 @@
|
||||
attrs="{'invisible':['|','|',('to_send_mail', '=', False), ('state', 'not in', 'cancel'),('reservation_type', 'in', 'out')]}"
|
||||
/>
|
||||
<field name="state" widget="statusbar" />
|
||||
<field name="to_send_mail" invisible="1" />
|
||||
<field name="is_modified_reservation" invisible="1" />
|
||||
<field name="to_send_mail" invisible="0" />
|
||||
<field name="is_modified_reservation" invisible="0" />
|
||||
</header>
|
||||
<div
|
||||
class="alert alert-info"
|
||||
|
||||
Reference in New Issue
Block a user