mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[REF] refactor _prepare_automated_actions_id(), ceate() and write()
This commit is contained in:
@@ -88,7 +88,6 @@ class PmsAutomatedMails(models.Model):
|
|||||||
dict_val = self._prepare_automated_actions_id(
|
dict_val = self._prepare_automated_actions_id(
|
||||||
action, time, moment, properties, is_create
|
action, time, moment, properties, is_create
|
||||||
)
|
)
|
||||||
|
|
||||||
action_server_vals = {
|
action_server_vals = {
|
||||||
"name": name,
|
"name": name,
|
||||||
"state": "email",
|
"state": "email",
|
||||||
@@ -96,28 +95,30 @@ class PmsAutomatedMails(models.Model):
|
|||||||
"model_id": dict_val["model_id"],
|
"model_id": dict_val["model_id"],
|
||||||
}
|
}
|
||||||
action_server = self.env["ir.actions.server"].create(action_server_vals)
|
action_server = self.env["ir.actions.server"].create(action_server_vals)
|
||||||
|
automated_actions_vals = {
|
||||||
|
"active": active,
|
||||||
|
"action_server_id": action_server.id,
|
||||||
|
"trigger": dict_val["trigger"],
|
||||||
|
"filter_domain": dict_val["filter_domain"],
|
||||||
|
"filter_pre_domain": dict_val["filter_pre_domain"],
|
||||||
|
"trg_date_range": dict_val["time"],
|
||||||
|
"trg_date_range_type": date_range_type,
|
||||||
|
"template_id": template_id,
|
||||||
|
}
|
||||||
model_field = dict_val["model_field"]
|
model_field = dict_val["model_field"]
|
||||||
if not model_field:
|
if model_field:
|
||||||
automated_actions_vals = {
|
automated_actions_vals.update(
|
||||||
"active": active,
|
{
|
||||||
"action_server_id": action_server.id,
|
"trg_date_id": dict_val["model_field"].id,
|
||||||
"trigger": dict_val["trigger"],
|
}
|
||||||
"filter_domain": dict_val["filter_domain"],
|
)
|
||||||
"trg_date_range": dict_val["time"],
|
trigger_field = dict_val["trigger_fields"]
|
||||||
"trg_date_range_type": date_range_type,
|
if trigger_field:
|
||||||
"template_id": template_id,
|
automated_actions_vals.update(
|
||||||
}
|
{
|
||||||
else:
|
"trigger_field_ids": dict_val["trigger_fields"].ids,
|
||||||
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)
|
||||||
vals.update({"automated_actions_id": automated_action.id})
|
vals.update({"automated_actions_id": automated_action.id})
|
||||||
return super(PmsAutomatedMails, self).create(vals)
|
return super(PmsAutomatedMails, self).create(vals)
|
||||||
@@ -142,28 +143,30 @@ class PmsAutomatedMails(models.Model):
|
|||||||
"model_id": dict_val["model_id"],
|
"model_id": dict_val["model_id"],
|
||||||
}
|
}
|
||||||
action_server.write(action_server_vals)
|
action_server.write(action_server_vals)
|
||||||
|
automated_actions_vals = {
|
||||||
|
"active": self.active,
|
||||||
|
"action_server_id": action_server.id,
|
||||||
|
"trigger": dict_val["trigger"],
|
||||||
|
"filter_domain": dict_val["filter_domain"],
|
||||||
|
"filter_pre_domain": dict_val["filter_pre_domain"],
|
||||||
|
"trg_date_range": dict_val["time"],
|
||||||
|
"trg_date_range_type": self.time_type,
|
||||||
|
"template_id": self.template_id,
|
||||||
|
}
|
||||||
model_field = dict_val["model_field"]
|
model_field = dict_val["model_field"]
|
||||||
if not model_field:
|
if model_field:
|
||||||
automated_actions_vals = {
|
automated_actions_vals.update(
|
||||||
"active": self.active,
|
{
|
||||||
"action_server_id": action_server.id,
|
"trg_date_id": dict_val["model_field"].id,
|
||||||
"trigger": dict_val["trigger"],
|
}
|
||||||
"filter_domain": dict_val["filter_domain"],
|
)
|
||||||
"trg_date_range": dict_val["time"],
|
trigger_field = dict_val["trigger_fields"]
|
||||||
"trg_date_range_type": self.time_type,
|
if trigger_field:
|
||||||
"template_id": self.template_id,
|
automated_actions_vals.update(
|
||||||
}
|
{
|
||||||
else:
|
"trigger_field_ids": dict_val["trigger_fields"].ids,
|
||||||
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)
|
automated_actions_id.write(automated_actions_vals)
|
||||||
vals.update({"automated_actions_id": automated_actions_id.id})
|
vals.update({"automated_actions_id": automated_actions_id.id})
|
||||||
return result
|
return result
|
||||||
@@ -175,117 +178,182 @@ class PmsAutomatedMails(models.Model):
|
|||||||
action_server.unlink()
|
action_server.unlink()
|
||||||
return super(PmsAutomatedMails, self).unlink()
|
return super(PmsAutomatedMails, self).unlink()
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _get_auto_action_fields_in_creation_action(self, moment, time):
|
||||||
|
model_field = False
|
||||||
|
model_id = self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
|
||||||
|
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", "=", "create_date")]
|
||||||
|
)
|
||||||
|
result = {
|
||||||
|
"model_id": model_id,
|
||||||
|
"trigger": trigger,
|
||||||
|
"model_field": model_field,
|
||||||
|
"time": time,
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _get_auto_action_fields_in_write_or_cancel_action(self, moment, time):
|
||||||
|
model_field = False
|
||||||
|
model_id = self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
|
||||||
|
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")]
|
||||||
|
)
|
||||||
|
result = {
|
||||||
|
"model_id": model_id,
|
||||||
|
"trigger": trigger,
|
||||||
|
"model_field": model_field,
|
||||||
|
"time": time,
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _get_auto_action_fields_in_checkin_action(self, moment, time):
|
||||||
|
model_id = self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
|
||||||
|
trigger = "on_time"
|
||||||
|
model_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "pms.reservation"), ("name", "=", "checkin")]
|
||||||
|
)
|
||||||
|
if moment == "before":
|
||||||
|
time = time * (-1)
|
||||||
|
if moment == "in_act":
|
||||||
|
trigger = "on_write"
|
||||||
|
time = 0
|
||||||
|
result = {
|
||||||
|
"model_id": model_id,
|
||||||
|
"trigger": trigger,
|
||||||
|
"model_field": model_field,
|
||||||
|
"time": time,
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _get_auto_action_fields_in_checkout_action(self, moment, time):
|
||||||
|
model_id = self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
|
||||||
|
trigger = "on_time"
|
||||||
|
model_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "pms.reservation"), ("name", "=", "checkout")]
|
||||||
|
)
|
||||||
|
if moment == "before":
|
||||||
|
time = time * (-1)
|
||||||
|
if moment == "in_act":
|
||||||
|
trigger = "on_write"
|
||||||
|
time = 0
|
||||||
|
result = {
|
||||||
|
"model_id": model_id,
|
||||||
|
"trigger": trigger,
|
||||||
|
"model_field": model_field,
|
||||||
|
"time": time,
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _get_auto_action_fields_in_payment_action(self, moment, time):
|
||||||
|
model_field = False
|
||||||
|
model_id = (
|
||||||
|
self.env["ir.model"]
|
||||||
|
.search([("model", "=", "account.payment"), ("transient", "=", False)])
|
||||||
|
.id
|
||||||
|
)
|
||||||
|
if moment == "in_act":
|
||||||
|
trigger = "on_create"
|
||||||
|
time = 0
|
||||||
|
else:
|
||||||
|
trigger = "on_time"
|
||||||
|
model_field = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "account.payment"), ("name", "=", "date")]
|
||||||
|
)
|
||||||
|
if moment == "before":
|
||||||
|
time = time * (-1)
|
||||||
|
result = {
|
||||||
|
"model_id": model_id,
|
||||||
|
"trigger": trigger,
|
||||||
|
"model_field": model_field,
|
||||||
|
"time": time,
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _get_auto_action_fields_in_invoice_action(self, moment, time):
|
||||||
|
trigger = False
|
||||||
|
model_id = self.env["ir.model"].search([("model", "=", "account.move")]).id
|
||||||
|
if moment == "in_act":
|
||||||
|
trigger = "on_create"
|
||||||
|
time = 0
|
||||||
|
result = {
|
||||||
|
"model_id": model_id,
|
||||||
|
"time": time,
|
||||||
|
"trigger": trigger,
|
||||||
|
"model_field": False,
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
def _prepare_automated_actions_id(
|
def _prepare_automated_actions_id(
|
||||||
self, action, time, moment, properties, is_create
|
self, action, time, moment, properties, is_create
|
||||||
):
|
):
|
||||||
trigger = False
|
|
||||||
model_field = False
|
|
||||||
model_id = False
|
|
||||||
filter_domain = []
|
filter_domain = []
|
||||||
today = fields.Date.today()
|
filter_pre_domain = []
|
||||||
# action: create reservation
|
trigger_fields = False
|
||||||
|
dict_val = {}
|
||||||
if action == "creation":
|
if action == "creation":
|
||||||
model_id = (
|
dict_val = self._get_auto_action_fields_in_creation_action(moment, time)
|
||||||
self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
|
elif action == "write" or action == "cancel":
|
||||||
|
dict_val = self._get_auto_action_fields_in_write_or_cancel_action(
|
||||||
|
moment, time
|
||||||
)
|
)
|
||||||
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", "=", "create_date")]
|
|
||||||
)
|
|
||||||
# action: write and cancel reservation
|
|
||||||
if action == "write" or action == "cancel":
|
|
||||||
model_id = (
|
|
||||||
self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
|
|
||||||
)
|
|
||||||
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")]
|
|
||||||
)
|
|
||||||
if action == "cancel":
|
if action == "cancel":
|
||||||
filter_domain = [
|
filter_domain = [
|
||||||
("state", "=", "cancelled"),
|
("state", "=", "cancelled"),
|
||||||
]
|
]
|
||||||
# action: checkin
|
elif action == "checkin":
|
||||||
if action == "checkin":
|
dict_val = self._get_auto_action_fields_in_checkin_action(moment, time)
|
||||||
model_id = (
|
|
||||||
self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
|
|
||||||
)
|
|
||||||
trigger = "on_time"
|
|
||||||
model_field = self.env["ir.model.fields"].search(
|
|
||||||
[("model", "=", "pms.reservation"), ("name", "=", "checkin")]
|
|
||||||
)
|
|
||||||
if moment == "in_act":
|
if moment == "in_act":
|
||||||
time = 0
|
trigger_fields = self.env["ir.model.fields"].search(
|
||||||
filter_domain = [
|
[("model", "=", "pms.reservation"), ("name", "=", "state")]
|
||||||
("checkin", "=", str(today)),
|
|
||||||
]
|
|
||||||
elif moment == "before":
|
|
||||||
time = time * (-1)
|
|
||||||
# action: checkout
|
|
||||||
if action == "checkout":
|
|
||||||
model_id = (
|
|
||||||
self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
|
|
||||||
)
|
|
||||||
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":
|
|
||||||
model_id = (
|
|
||||||
self.env["ir.model"]
|
|
||||||
.search([("model", "=", "account.payment"), ("transient", "=", False)])
|
|
||||||
.id
|
|
||||||
)
|
|
||||||
if moment == "in_act":
|
|
||||||
trigger = "on_creation"
|
|
||||||
model_field = self.env["ir.model.fields"].search(
|
|
||||||
[("model", "=", "account.payment"), ("name", "=", "create_date")]
|
|
||||||
)
|
)
|
||||||
time = 0
|
filter_pre_domain = [("state", "=", "confirm")]
|
||||||
else:
|
filter_domain = [
|
||||||
trigger = "on_time"
|
("state", "=", "onboard"),
|
||||||
model_field = self.env["ir.model.fields"].search(
|
]
|
||||||
[("model", "=", "account.payment"), ("name", "=", "date")]
|
elif action == "checkout":
|
||||||
|
dict_val = self._get_auto_action_fields_in_checkout_action(moment, time)
|
||||||
|
if moment == "in_act":
|
||||||
|
trigger_fields = self.env["ir.model.fields"].search(
|
||||||
|
[("model", "=", "pms.reservation"), ("name", "=", "state")]
|
||||||
)
|
)
|
||||||
if moment == "before":
|
filter_pre_domain = [("state", "=", "onboard")]
|
||||||
time = time * (-1)
|
filter_domain = [
|
||||||
# TODO: create automated action when the act is 'invoice'
|
("state", "=", "out"),
|
||||||
# action: invoices
|
]
|
||||||
# if action == "invoice":
|
elif action == "payment":
|
||||||
# model_id = self.env["ir.model"].search(
|
dict_val = self._get_auto_action_fields_in_payment_action(moment, time)
|
||||||
# [("model", "=", "account.move")]
|
elif action == "invoice":
|
||||||
# ).id
|
dict_val = self._get_auto_action_fields_in_invoice_action(moment, time)
|
||||||
# filter_domain = [
|
filter_domain = [
|
||||||
# ("folio_ids", "!=", False),
|
("folio_ids", "!=", False),
|
||||||
# ]
|
]
|
||||||
# if moment == "in_act":
|
|
||||||
# trigger = "on_create"
|
|
||||||
# time = 0
|
|
||||||
pms_property_ids = self._get_pms_property_ids(properties, is_create)
|
pms_property_ids = self._get_pms_property_ids(properties, is_create)
|
||||||
if pms_property_ids:
|
if pms_property_ids:
|
||||||
filter_domain.append(("pms_property_id", "in", pms_property_ids))
|
filter_domain.append(("pms_property_id", "in", pms_property_ids))
|
||||||
result = {
|
result = {
|
||||||
"trigger": trigger,
|
"trigger": dict_val["trigger"],
|
||||||
"model_field": model_field,
|
"model_field": dict_val["model_field"],
|
||||||
|
"trigger_fields": trigger_fields,
|
||||||
|
"filter_pre_domain": filter_pre_domain,
|
||||||
"filter_domain": filter_domain,
|
"filter_domain": filter_domain,
|
||||||
"time": time,
|
"time": dict_val["time"],
|
||||||
"model_id": model_id,
|
"model_id": dict_val["model_id"],
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user