diff --git a/helpdesk_mgmt/__manifest__.py b/helpdesk_mgmt/__manifest__.py index 9bb93c2a90..7d3520df89 100644 --- a/helpdesk_mgmt/__manifest__.py +++ b/helpdesk_mgmt/__manifest__.py @@ -17,6 +17,7 @@ "depends": ["mail", "portal"], "data": [ "data/helpdesk_data.xml", + "data/ir_config_parameter.xml", "security/helpdesk_security.xml", "security/ir.model.access.csv", "views/res_partner_views.xml", diff --git a/helpdesk_mgmt/controllers/main.py b/helpdesk_mgmt/controllers/main.py index 4c43176d0c..8dbdade976 100644 --- a/helpdesk_mgmt/controllers/main.py +++ b/helpdesk_mgmt/controllers/main.py @@ -66,6 +66,7 @@ def _prepare_submit_ticket_vals(self, **kw): "description": plaintext2html(kw.get("description")), "name": kw.get("subject"), "attachment_ids": False, + "stage_id": request.env.ref("helpdesk_mgmt.helpdesk_ticket_stage_new").id, "channel_id": request.env["helpdesk.ticket.channel"] .sudo() .search([("name", "=", "Web")]) diff --git a/helpdesk_mgmt/data/helpdesk_data.xml b/helpdesk_mgmt/data/helpdesk_data.xml index 5e43f8eea7..5fc5aad88d 100644 --- a/helpdesk_mgmt/data/helpdesk_data.xml +++ b/helpdesk_mgmt/data/helpdesk_data.xml @@ -14,7 +14,7 @@ ${object.company_id.partner_id.email} ${not object.partner_id and object.partner_email or ''|safe}, The ticket ${object.number} has been closed. ${object.partner_id.id} @@ -22,60 +22,46 @@ ${object.partner_id.lang} - - - - - - - - - - - - - - - - - -
- - - - -
- ${object.company_id.name} -
-
- - - - -
-

Hello ${object.partner_id.name or object.partner_name or ''},

-

The ticket "${object.display_name}" has been closed.

-
-
- - - - - - -
- ${object.company_id.phone} - - ${object.company_id.email} - - ${object.company_id.website} -
-
- - + > +

Hello ${object.partner_id.name or object.partner_name or ''},

+

The ticket "${object.display_name or 'n/a'}" has been closed.

+ % if object.partner_can_access(): +
+ + View Ticket + +
+ % endif + Thank you,

+ ${user.signature | safe}
+ ]]>
+ + Helpdesk Created Ticket Notification Email + + ${object.company_id.partner_id.email} + ${('' if object.partner_id.email and object.partner_id.email == object.partner_email else object.email) | safe}, + The ticket ${object.number} has been created. + ${object.partner_id.id if object.partner_id.email and object.partner_id.email == object.partner_email else False} + + ${object.partner_id.lang} + +
+

Hello ${object.partner_id.name or object.partner_name or ''},

+

The ticket "${object.display_name or 'n/a'}" has been created.

+

You can reply to this email to add information to the ticket.

+
+ ]]> +
+
Helpdesk Changed Stage notification Email @@ -90,57 +76,10 @@ - - - - - - - - - - - - - - - - - -
- - - - -
- -
-
- - - - -
-

Hello ${object.partner_id.name or object.partner_name or ''},

-

The ticket "${object.display_name}" stage has changed to ${object.stage_id.name}.

-
-
- - - - - - -
- ${object.company_id.phone} - - ${object.company_id.email} - - ${object.company_id.website} -
-
- - +
+

Hello ${object.partner_id.name or object.partner_name or ''},

+

The ticket "${object.display_name}" stage has changed to ${object.stage_id.name}.

+
]]>
diff --git a/helpdesk_mgmt/data/ir_config_parameter.xml b/helpdesk_mgmt/data/ir_config_parameter.xml new file mode 100644 index 0000000000..184737a7a1 --- /dev/null +++ b/helpdesk_mgmt/data/ir_config_parameter.xml @@ -0,0 +1,9 @@ + + + + + helpdesk_mgmt.created_ticket_template_name + created_ticket_template + + + diff --git a/helpdesk_mgmt/models/helpdesk_ticket.py b/helpdesk_mgmt/models/helpdesk_ticket.py index f417a3d72a..8f0d87826d 100644 --- a/helpdesk_mgmt/models/helpdesk_ticket.py +++ b/helpdesk_mgmt/models/helpdesk_ticket.py @@ -1,3 +1,5 @@ +from email.utils import getaddresses + from odoo import _, api, fields, models, tools from odoo.exceptions import AccessError @@ -111,6 +113,26 @@ def _read_group_stage_ids(self, stages, domain, order): ) active = fields.Boolean(default=True) + def _send_mail_to_partner(self, template_name): + """Send a feedback message to the contact linked to this newly created ticket + (whatever it is a real partner or just its e-mail). + Note: The purpose of this function is to replace the stage's `mail_template_id` + role (that just doesn't work when the ticket is created from the portal or when + `partner_id` is set)""" + created_ticket_template = self.env.ref(template_name, raise_if_not_found=False) + if created_ticket_template: + created_ticket_template.send_mail( + self.id, force_send=True, notif_layout="mail.mail_notification_light" + ) + + @api.model + def _get_created_ticket_template_name(self): + return ( + self.env["ir.config_parameter"] + .sudo() + .get_param("helpdesk_mgmt.created_ticket_template_name") + ) + def name_get(self): res = [] for rec in self: @@ -134,6 +156,15 @@ def _onchange_partner_id(self): def _creation_subtype(self): return self.env.ref("helpdesk_mgmt.hlp_tck_created") + def _message_auto_subscribe_followers(self, updated_values, default_subtype_ids): + """Auto-subscribe ticket partner.""" + result = super()._message_auto_subscribe_followers( + updated_values, default_subtype_ids + ) + if updated_values.get("partner_id"): + result.append((self.partner_id.id, default_subtype_ids, False)) + return result + @api.model_create_multi def create(self, vals_list): for vals in vals_list: @@ -143,7 +174,13 @@ def create(self, vals_list): vals["team_id"] = self._prepare_team_id(vals) if vals.get("user_id") and not vals.get("assigned_date"): vals["assigned_date"] = fields.Datetime.now() - return super().create(vals_list) + res = super().create(vals_list) + created_ticket_template_name = self._get_created_ticket_template_name() + if created_ticket_template_name: + for rec in res: + if rec.partner_id or rec.partner_email: + rec._send_mail_to_partner(created_ticket_template_name) + return res def copy(self, default=None): self.ensure_one() @@ -176,11 +213,6 @@ def _prepare_ticket_number(self, values): seq = seq.with_company(values["company_id"]) return seq.next_by_code("helpdesk.ticket.sequence") or "/" - def _compute_access_url(self): - super()._compute_access_url() - for item in self: - item.access_url = "/my/ticket/%s" % (item.id) - def _prepare_team_id(self, values): category = self.env["helpdesk.ticket.category"].browse(values["category_id"]) if category.default_team_id: @@ -219,11 +251,15 @@ def message_new(self, msg, custom_values=None): "partner_email": msg.get("from"), "partner_id": msg.get("author_id"), } + msg_from = msg.get("from", "") + if msg_from: + res = getaddresses([msg_from]) + if res: + defaults["partner_name"] = res[0][0] + defaults["partner_email"] = res[0][1] defaults.update(custom_values) - # Write default values coming from msg ticket = super().message_new(msg, custom_values=defaults) - # Use mail gateway tools to search for partners to subscribe email_list = tools.email_split( (msg.get("to") or "") + "," + (msg.get("cc") or "") @@ -236,7 +272,6 @@ def message_new(self, msg, custom_values=None): if p ] ticket.message_subscribe(partner_ids) - return ticket def message_update(self, msg, update_vals=None): @@ -318,3 +353,38 @@ def action_do_send_email(self): "target": "new", "context": ctx, } + + # --------------------------------------------------- + # Portal + # --------------------------------------------------- + + def _compute_access_url(self): + super(HelpdeskTicket, self)._compute_access_url() + for ticket in self: + ticket.access_url = "/my/ticket/%s" % (ticket.id) + + def _notify_get_groups(self, msg_vals=None): + groups = super(HelpdeskTicket, self)._notify_get_groups(msg_vals=msg_vals) + self.ensure_one() + for group_name, _group_method, group_data in groups: + if group_name == "portal": + group_data["has_button_access"] = True + return groups + + def partner_can_access(self): + if not self.partner_id: + return False + user = ( + self.env["res.users"] + .sudo() + .search([("partner_id", "=", self.partner_id.id)]) + ) + if not user or not self.with_user(user).check_access_rights( + "read", raise_exception=False + ): + return False + return True + + def get_access_link(self): + # _notify_get_action_link is not callable from email template + return self._notify_get_action_link("view") diff --git a/helpdesk_mgmt/readme/CONFIGURE.rst b/helpdesk_mgmt/readme/CONFIGURE.rst index a99b90ab25..712eb9b2af 100644 --- a/helpdesk_mgmt/readme/CONFIGURE.rst +++ b/helpdesk_mgmt/readme/CONFIGURE.rst @@ -37,7 +37,7 @@ Stages #. Edit or create a new stage. #. Set the name for the stage. #. Set the sequence order for the stage. -#. You can select an Email template. +#. You can select an email template that will be automatically sent when a ticket reaches this stage. #. Mark the Unattended checkbox if the stage contains unattended tickets. #. Mark the Closed checkbox if the stage contains closed tickets. #. You can add a description for the stage. diff --git a/helpdesk_mgmt/readme/CONTRIBUTORS.rst b/helpdesk_mgmt/readme/CONTRIBUTORS.rst index 129e42eba4..a502a7972a 100644 --- a/helpdesk_mgmt/readme/CONTRIBUTORS.rst +++ b/helpdesk_mgmt/readme/CONTRIBUTORS.rst @@ -33,6 +33,10 @@ * Marcel Savegnago * Eduardo Aparício +* `TAKOBI `_: + + * Lorenzo Battistini + * `Obertix `_: * Vicent Cubells diff --git a/helpdesk_mgmt/static/description/Stage.PNG b/helpdesk_mgmt/static/description/Stages.PNG similarity index 100% rename from helpdesk_mgmt/static/description/Stage.PNG rename to helpdesk_mgmt/static/description/Stages.PNG diff --git a/helpdesk_mgmt/tests/common.py b/helpdesk_mgmt/tests/common.py index 0a9e22e587..aa5eabc5f9 100644 --- a/helpdesk_mgmt/tests/common.py +++ b/helpdesk_mgmt/tests/common.py @@ -32,6 +32,9 @@ def setUpClass(cls): groups="helpdesk_mgmt.group_helpdesk_user", context=ctx, ) + cls.admin = cls.env.ref("base.user_admin") + cls.user_portal = cls.env.ref("base.demo_user0") + cls.user_demo = cls.env.ref("base.user_demo") cls.stage_closed = cls.env.ref("helpdesk_mgmt.helpdesk_ticket_stage_done") cls.team_a = helpdesk_ticket_team.create( {"name": "Team A", "user_ids": [(6, 0, [cls.user_own.id, cls.user.id])]} diff --git a/helpdesk_mgmt/tests/test_helpdesk_ticket.py b/helpdesk_mgmt/tests/test_helpdesk_ticket.py index 2f1dc8f0c6..1e2997b24f 100644 --- a/helpdesk_mgmt/tests/test_helpdesk_ticket.py +++ b/helpdesk_mgmt/tests/test_helpdesk_ticket.py @@ -1,5 +1,7 @@ import time +from odoo.tests import tagged + from .common import TestHelpdeskTicketBase @@ -97,3 +99,77 @@ def test_helpdesk_ticket_message_new(self): self.assertEqual( t.name, title, "The ticket should have the correct (new) title." ) + + def test_helpdesk_ticket_multiple(self): + Ticket = self.env["helpdesk.ticket"] + Partner = self.env["res.partner"] + + partner_1 = Partner.create({"name": "Partner 1", "email": "partner1@test.com"}) + partner_2 = Partner.create({"name": "Partner 2", "email": "partner2@test.com"}) + tickets = Ticket.create( + [ + { + "name": "Test 1", + "description": "Ticket test number 1", + "user_id": self.user.id, + "partner_email": "partner@domain.com", + }, + { + "name": "Test 2", + "description": "Ticket test number 2", + "user_id": self.user.id, + "partner_id": partner_1.id, + }, + { + "name": "Test 3", + "description": "Ticket test number 3", + "user_id": self.user_demo.id, + "partner_id": partner_2.id, + }, + ] + ) + self.assertEqual(len(tickets), 3) + + def test_helpdesk_ticket_access(self): + Ticket = self.env["helpdesk.ticket"] + + t = Ticket.create( + { + "name": "Test 1", + "description": "Ticket test", + } + ) + self.assertEqual(t.access_url, "/my/ticket/%s" % t.id) + t.partner_id = self.user_portal.partner_id.id + self.assertTrue(t.partner_can_access()) + + +@tagged("post_install", "-at_install") +class TestHelpdeskTicketPostInstall(TestHelpdeskTicketBase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + def test_helpdesk_ticket_assign_notify(self): + Ticket = self.env["helpdesk.ticket"] + Message = self.env["mail.message"] + + t = Ticket.create( + { + "name": "Test 1", + "description": "Ticket test", + } + ) + t.user_id = self.user_demo.id + m = Message.search( + [ + ("model", "=", "helpdesk.ticket"), + ("res_id", "=", t.id), + ("message_type", "=", "user_notification"), + ] + ) + self.assertTrue( + "You have been assigned to the Helpdesk Ticket %s" % t.display_name + in m.body + ) + self.assertTrue("res_id=%s" % t.id in m.body)