[IMP] added write method and payment action

This commit is contained in:
braisab
2021-04-14 21:43:32 +02:00
parent 88ba98ce74
commit b8665f83aa
2 changed files with 90 additions and 30 deletions

View File

@@ -14,6 +14,7 @@
"installable": True,
"depends": [
"base",
"base_automation",
"mail",
# "account_payment_return",
# "email_template_qweb",

View File

@@ -6,12 +6,16 @@ class PmsAutomatedMails(models.Model):
_name = "pms.automated.mails"
_description = "Automatic Mails"
name = fields.Char(string="Name")
name = fields.Char(string="Name", required=True)
pms_property_id = fields.Many2one(string="Property", comodel_name="pms.property")
automated_actions_id = fields.Many2one(
string="Automated Actions", comodel_name="base.automation", ondelete="cascade"
string="Automated Actions",
comodel_name="base.automation",
ondelete="cascade",
store=True,
readonly=False,
)
time = fields.Integer(string="Time")
@@ -49,10 +53,6 @@ class PmsAutomatedMails(models.Model):
required=True,
)
trigger = fields.Char(
string="Trigger",
)
moment = fields.Selection(
string="Moment",
selection=[
@@ -74,23 +74,15 @@ class PmsAutomatedMails(models.Model):
template_id = vals.get("template_id")
active = vals.get("active")
moment = vals.get("moment")
model_id = False
model_field = False
if action in ("creation", "write", "cancel"):
if moment == "before":
raise UserError(_("The moment for this action cannot be 'Before'"))
if action in ("creation", "write", "cancel", "checkin", "checkout"):
model_id = self.env["ir.model"].search([("name", "=", "Reservation")])
elif action == "payment":
model_id = self.env["ir.model"].search([("name", "=", "Payments")])
dict_val = self._prepare_automated_actions_id(action, time, moment)
action_server_vals = {
"name": name,
"state": "email",
"usage": "ir_cron",
"model_id": model_id.id,
"model_id": dict_val["model_id"].id,
}
action_server = self.env["ir.actions.server"].create(action_server_vals)
dict_val = self._prepare_creation_write(action, time, moment)
model_field = dict_val["model_field"]
if not model_field:
automated_actions_vals = {
"active": active,
@@ -113,35 +105,85 @@ class PmsAutomatedMails(models.Model):
"template_id": template_id,
}
automated_action = self.env["base.automation"].create(automated_actions_vals)
self.automated_actions_id = automated_action.id
vals.update({"automated_actions_id": automated_action.id})
return super(PmsAutomatedMails, self).create(vals)
def _prepare_creation_write(self, action, time, moment):
def write(self, vals):
result = super(PmsAutomatedMails, self).write(vals)
dict_val = self._prepare_automated_actions_id(
self.action, self.time, self.moment
)
automated_actions_id = self.automated_actions_id
action_server = automated_actions_id.action_server_id
action_server_vals = {
"name": self.name,
"state": "email",
"usage": "ir_cron",
"model_id": dict_val["model_id"].id,
}
action_server.write(action_server_vals)
model_field = dict_val["model_field"]
if not model_field:
automated_actions_vals = {
"active": self.active,
"action_server_id": action_server.id,
"trigger": dict_val["trigger"],
"filter_domain": dict_val["filter_domain"],
"trg_date_range": dict_val["time"],
"trg_date_range_type": self.time_type,
"template_id": self.template_id,
}
else:
automated_actions_vals = {
"active": self.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": self.time_type,
"template_id": self.template_id,
}
automated_actions_id.write(automated_actions_vals)
vals.update({"automated_actions_id": automated_actions_id.id})
return result
def _prepare_automated_actions_id(self, action, time, moment):
trigger = False
model_field = False
filter_domain = False
model_id = False
today = fields.Date.today()
if action in ("creation", "write", "cancel") and moment == "before":
raise UserError(_("The moment for this action cannot be 'Before'"))
if action in ("creation", "write", "cancel", "checkin", "checkout"):
model_id = self.env["ir.model"].search([("name", "=", "Reservation")])
elif action == "payment":
model_id = self.env["ir.model"].search(
[("name", "=", "Payments"), ("transient", "=", False)]
)
# action: create reservation
if action == "creation":
if moment == "in_act":
trigger = "on_create"
time = 0
else:
trigger = "on_time"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "date_order")]
[("model", "=", "pms.reservation"), ("name", "=", "create_date")]
)
# 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"
time = 0
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"
@@ -149,24 +191,41 @@ class PmsAutomatedMails(models.Model):
[("model", "=", "pms.reservation"), ("name", "=", "checkin")]
)
if moment == "in_act":
filter_domain = [("checkin", "=", fields.Date.today())]
time = 0
filter_domain = [("checkin", "=", str(today))]
elif moment == "before":
time = time * (-1)
# action: checkout
if action == "checkout":
# 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":
time = 0
filter_domain = [("checkout", "=", str(today))]
elif moment == "before":
time = time * (-1)
# action: payments
if action == "payment":
if moment == "in_act":
trigger = "on_creation"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "account.payment"), ("name", "=", "create_date")]
)
time = 0
else:
trigger = "on_time"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "checkout")]
[("model", "=", "account.payment"), ("name", "=", "date")]
)
if moment == "in_act":
filter_domain = [("checkout", "=", fields.Date.today())]
elif moment == "before":
if moment == "before":
time = time * (-1)
result = {
"trigger": trigger,
"model_field": model_field,
"filter_domain": filter_domain,
"time": time,
"model_id": model_id,
}
return result