[WIP]added creation of auto actions when actions are: write reservation, cancel reservation, checkin and checkout

This commit is contained in:
braisab
2021-04-13 19:37:14 +02:00
parent 6f33cb3c3a
commit 88ba98ce74
2 changed files with 82 additions and 32 deletions

View File

@@ -10,10 +10,6 @@ class PmsAutomatedMails(models.Model):
pms_property_id = fields.Many2one(string="Property", comodel_name="pms.property") pms_property_id = fields.Many2one(string="Property", comodel_name="pms.property")
reservation_id = fields.Many2one(
string="Reservations",
comodel_name="pms.reservation",
)
automated_actions_id = fields.Many2one( automated_actions_id = fields.Many2one(
string="Automated Actions", comodel_name="base.automation", ondelete="cascade" string="Automated Actions", comodel_name="base.automation", ondelete="cascade"
) )
@@ -38,11 +34,6 @@ class PmsAutomatedMails(models.Model):
string="Model", comodel_name="ir.model", compute="_compute_model_id", store=True string="Model", comodel_name="ir.model", compute="_compute_model_id", store=True
) )
reservation_date_fields_id = fields.Many2one(
string="Action",
comodel_name="ir.model.fields",
)
action = fields.Selection( action = fields.Selection(
string="Action", string="Action",
selection=[ selection=[
@@ -78,23 +69,17 @@ class PmsAutomatedMails(models.Model):
def create(self, vals): def create(self, vals):
name = vals.get("name") name = vals.get("name")
action = vals.get("action") action = vals.get("action")
model_field = vals.get("reservation_date_fields_id")
time = vals.get("time") time = vals.get("time")
moment = vals.get("moment")
date_range_type = vals.get("time_type") date_range_type = vals.get("time_type")
template_id = vals.get("template_id") template_id = vals.get("template_id")
active = vals.get("active") active = vals.get("active")
moment = vals.get("moment")
model_id = False model_id = False
trigger = "on_time" model_field = False
filter_domain = False if action in ("creation", "write", "cancel"):
if action == "creation":
if moment == "before": if moment == "before":
raise UserError(_("The moment for this action cannot be 'Before'")) raise UserError(_("The moment for this action cannot be 'Before'"))
model_field = self.env["ir.model.fields"].search( if action in ("creation", "write", "cancel", "checkin", "checkout"):
[("model", "=", "pms.reservation"), ("name", "=", "date_order")]
)
filter_domain = [("date_order", "=", fields.Date.today())]
if action in ("creation", "write", "cancellation", "checkin", "checkout"):
model_id = self.env["ir.model"].search([("name", "=", "Reservation")]) model_id = self.env["ir.model"].search([("name", "=", "Reservation")])
elif action == "payment": elif action == "payment":
model_id = self.env["ir.model"].search([("name", "=", "Payments")]) model_id = self.env["ir.model"].search([("name", "=", "Payments")])
@@ -105,17 +90,83 @@ class PmsAutomatedMails(models.Model):
"model_id": model_id.id, "model_id": model_id.id,
} }
action_server = self.env["ir.actions.server"].create(action_server_vals) action_server = self.env["ir.actions.server"].create(action_server_vals)
dict_val = self._prepare_creation_write(action, time, moment)
automated_actions_vals = { if not model_field:
"active": active, automated_actions_vals = {
"action_server_id": action_server.id, "active": active,
"trigger": trigger, "action_server_id": action_server.id,
"trg_date_id": model_field.id, "trigger": dict_val["trigger"],
"filter_domain": filter_domain, "filter_domain": dict_val["filter_domain"],
"trg_date_range": time, "trg_date_range": dict_val["time"],
"trg_date_range_type": date_range_type, "trg_date_range_type": date_range_type,
"template_id": template_id, "template_id": template_id,
} }
else:
automated_actions_vals = {
"active": active,
"action_server_id": action_server.id,
"trigger": dict_val["trigger"],
"trg_date_id": dict_val["model_field"].id,
"filter_domain": dict_val["filter_domain"],
"trg_date_range": dict_val["time"],
"trg_date_range_type": date_range_type,
"template_id": template_id,
}
automated_action = self.env["base.automation"].create(automated_actions_vals) automated_action = self.env["base.automation"].create(automated_actions_vals)
self.automated_actions_id = automated_action.id self.automated_actions_id = automated_action.id
return super(PmsAutomatedMails, self).create(vals) return super(PmsAutomatedMails, self).create(vals)
def _prepare_creation_write(self, action, time, moment):
trigger = False
model_field = False
filter_domain = False
# action: create reservation
if action == "creation":
if moment == "in_act":
trigger = "on_create"
else:
trigger = "on_time"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "date_order")]
)
# action: write and cancel reservation
if action == "write" or action == "cancel":
if action == "cancel":
filter_domain = [("state", "=", "cancelled")]
if moment == "in_act":
trigger = "on_write"
else:
trigger = "on_time"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "write_date")]
)
# action: checkin
if action == "checkin":
trigger = "on_time"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "checkin")]
)
if moment == "in_act":
filter_domain = [("checkin", "=", fields.Date.today())]
elif moment == "before":
time = time * (-1)
# action: checkout
if action == "checkout":
trigger = "on_time"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "checkout")]
)
if moment == "in_act":
filter_domain = [("checkout", "=", fields.Date.today())]
elif moment == "before":
time = time * (-1)
result = {
"trigger": trigger,
"model_field": model_field,
"filter_domain": filter_domain,
"time": time,
}
return result

View File

@@ -44,8 +44,7 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="Automated Mails"> <tree string="Automated Mails">
<field name="name" /> <field name="name" />
<field name="time" /> <field name="action" />
<field name="time_type" />
<field name="template_id" /> <field name="template_id" />
</tree> </tree>
</field> </field>