diff --git a/pms/models/pms_automated_mails.py b/pms/models/pms_automated_mails.py index 3c12d0099..df9dacef0 100644 --- a/pms/models/pms_automated_mails.py +++ b/pms/models/pms_automated_mails.py @@ -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): - _name = 'pms.automated.mails' - _description = 'Automatic Mails' + _name = "pms.automated.mails" + _description = "Automatic Mails" - name = fields.Char( - string="Name" + name = fields.Char(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( - string="Automated Actions", - comodel_name="base.automation" + string="Automated Actions", comodel_name="base.automation", ondelete="cascade" ) - time = fields.Integer( - string="Time", - required=True - ) + time = fields.Integer(string="Time") time_type = fields.Selection( string="Time Range", @@ -30,26 +26,78 @@ class PmsAutomatedMails(models.Model): ("minutes", "Minutes"), ("hour", "Hour"), ("day", "Days"), - ("month", "Months") + ("month", "Months"), ], default="day", - required=True ) template_id = fields.Many2one( - string="Template", - comodel_name="mail.template", - required=True + string="Template", 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( string="Action", 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 def create(self, vals): 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 = { "name": name, "state": "email", @@ -57,18 +105,17 @@ class PmsAutomatedMails(models.Model): "model_id": model_id.id, } 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 = { + "active": active, "action_server_id": action_server.id, - "trigger": "on_time", - "filter_domain": [("checkin", "<", "2021-12-31")], + "trigger": trigger, "trg_date_id": model_field.id, + "filter_domain": filter_domain, "trg_date_range": time, "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) diff --git a/pms/views/pms_automated_mails_views.xml b/pms/views/pms_automated_mails_views.xml index 9740a9f21..98f000fe3 100644 --- a/pms/views/pms_automated_mails_views.xml +++ b/pms/views/pms_automated_mails_views.xml @@ -1,4 +1,4 @@ - + pms.automated_mails_view_form @@ -7,22 +7,30 @@
-
- - + + +
- - + + +
@@ -35,17 +43,17 @@ pms.automated.mails - - - - + + + + Automated Mails pms.automated.mails - + tree,form