[WIP] action: send mail after x min create reservation

This commit is contained in:
braisab
2021-04-12 18:59:56 +02:00
parent fedfa93715
commit 6f33cb3c3a
2 changed files with 99 additions and 44 deletions

View File

@@ -1,28 +1,24 @@
from odoo import fields, models, api from odoo import _, api, fields, models
from odoo.exceptions import UserError
class PmsAutomatedMails(models.Model): class PmsAutomatedMails(models.Model):
_name = 'pms.automated.mails' _name = "pms.automated.mails"
_description = 'Automatic Mails' _description = "Automatic Mails"
name = fields.Char( name = fields.Char(string="Name")
string="Name"
pms_property_id = fields.Many2one(string="Property", comodel_name="pms.property")
reservation_id = fields.Many2one(
string="Reservations",
comodel_name="pms.reservation",
) )
pms_property_id = fields.Many2one(
string="Property",
comodel_name="pms.property"
)
automated_actions_id = fields.Many2one( automated_actions_id = fields.Many2one(
string="Automated Actions", string="Automated Actions", comodel_name="base.automation", ondelete="cascade"
comodel_name="base.automation"
) )
time = fields.Integer( time = fields.Integer(string="Time")
string="Time",
required=True
)
time_type = fields.Selection( time_type = fields.Selection(
string="Time Range", string="Time Range",
@@ -30,26 +26,78 @@ class PmsAutomatedMails(models.Model):
("minutes", "Minutes"), ("minutes", "Minutes"),
("hour", "Hour"), ("hour", "Hour"),
("day", "Days"), ("day", "Days"),
("month", "Months") ("month", "Months"),
], ],
default="day", default="day",
required=True
) )
template_id = fields.Many2one( template_id = fields.Many2one(
string="Template", string="Template", comodel_name="mail.template", required=True
comodel_name="mail.template", )
required=True
model_id = fields.Many2one(
string="Model", comodel_name="ir.model", compute="_compute_model_id", store=True
) )
reservation_date_fields_id = fields.Many2one( reservation_date_fields_id = fields.Many2one(
string="Action", string="Action",
comodel_name="ir.model.fields", comodel_name="ir.model.fields",
domain="[('model', '=', 'pms.reservation'),('ttype', 'in', ('date', 'datetime'))]"
) )
action = fields.Selection(
string="Action",
selection=[
("creation", "Reservation creation"),
("write", "Reservation modification"),
("cancel", "Reservation cancellation"),
("checkin", "Checkin"),
("checkout", "Checkout"),
("payment", "Payment"),
("invoice", "Invoice"),
],
default="creation",
required=True,
)
trigger = fields.Char(
string="Trigger",
)
moment = fields.Selection(
string="Moment",
selection=[
("before", "Before"),
("after", "After"),
("in_act", "In the act"),
],
default="before",
)
active = fields.Boolean(string="Active", default=True)
@api.model @api.model
def create(self, vals): def create(self, vals):
name = vals.get("name") name = vals.get("name")
model_id = self.env["ir.model"].search([("name", "=", "Reservation")]) action = vals.get("action")
model_field = vals.get("reservation_date_fields_id")
time = vals.get("time")
moment = vals.get("moment")
date_range_type = vals.get("time_type")
template_id = vals.get("template_id")
active = vals.get("active")
model_id = False
trigger = "on_time"
filter_domain = False
if action == "creation":
if moment == "before":
raise UserError(_("The moment for this action cannot be 'Before'"))
model_field = self.env["ir.model.fields"].search(
[("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")])
elif action == "payment":
model_id = self.env["ir.model"].search([("name", "=", "Payments")])
action_server_vals = { action_server_vals = {
"name": name, "name": name,
"state": "email", "state": "email",
@@ -57,18 +105,17 @@ 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)
model_field = vals.get("reservation_date_fields_id")
time = vals.get("time")
date_range_type = vals.get("time_type")
template_id = vals.get("template_id")
automated_actions_vals = { automated_actions_vals = {
"active": active,
"action_server_id": action_server.id, "action_server_id": action_server.id,
"trigger": "on_time", "trigger": trigger,
"filter_domain": [("checkin", "<", "2021-12-31")],
"trg_date_id": model_field.id, "trg_date_id": model_field.id,
"filter_domain": filter_domain,
"trg_date_range": time, "trg_date_range": time,
"trg_date_range_type": date_range_type, "trg_date_range_type": date_range_type,
"template_id": template_id "template_id": template_id,
} }
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
return super(PmsAutomatedMails, self).create(vals) return super(PmsAutomatedMails, self).create(vals)

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<odoo> <odoo>
<record model="ir.ui.view" id="pms_automated_mails_view"> <record model="ir.ui.view" id="pms_automated_mails_view">
<field name="name">pms.automated_mails_view_form</field> <field name="name">pms.automated_mails_view_form</field>
@@ -7,22 +7,30 @@
<form string="Automated Mails" class="pt-1"> <form string="Automated Mails" class="pt-1">
<sheet> <sheet>
<div class="col-5"> <div class="col-5">
<label for="name"/> <label for="name" />
<group> <group>
<h2><field name="name"/></h2> <h2><field name="name" /></h2>
</group> </group>
</div> </div>
<div class="row"> <div class="row">
<div class="col-5"> <div class="col-5">
<group> <group>
<field name="template_id"/> <field name="active" widget="boolean_toggle" />
<field name="reservation_date_fields_id"/> <field name="template_id" />
<field name="action" />
</group> </group>
</div> </div>
<div class="col-5"> <div class="col-5">
<group> <group>
<field name="time"/> <field name="moment" />
<field name="time_type"/> <field
name="time"
attrs="{'invisible':[('moment','=','in_act')]}"
/>
<field
name="time_type"
attrs="{'invisible':[('moment','=','in_act')]}"
/>
</group> </group>
</div> </div>
</div> </div>
@@ -35,17 +43,17 @@
<field name="model">pms.automated.mails</field> <field name="model">pms.automated.mails</field>
<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="time" />
<field name="time_type"/> <field name="time_type" />
<field name="template_id"/> <field name="template_id" />
</tree> </tree>
</field> </field>
</record> </record>
<record model="ir.actions.act_window" id="open_pms_automated_mails_tree"> <record model="ir.actions.act_window" id="open_pms_automated_mails_tree">
<field name="name">Automated Mails</field> <field name="name">Automated Mails</field>
<field name="res_model">pms.automated.mails</field> <field name="res_model">pms.automated.mails</field>
<field name="view_id" ref="view_automated_mails_tree"/> <field name="view_id" ref="view_automated_mails_tree" />
<field name="view_mode">tree,form</field> <field name="view_mode">tree,form</field>
</record> </record>
<menuitem <menuitem