diff --git a/project_worksheet/data/mail_template_data.xml b/project_worksheet/data/mail_template_data.xml index 2c117bed..3ce00449 100644 --- a/project_worksheet/data/mail_template_data.xml +++ b/project_worksheet/data/mail_template_data.xml @@ -5,7 +5,7 @@ Worksheet: Approval Request Worksheet ${object.name} - project ${object.project_id.name} - approval required - ${object.partner_approver_id.email} + ${object.partner_approver_id.email},${object.supervisor_id.work_email}

diff --git a/project_worksheet/models/project_worksheet.py b/project_worksheet/models/project_worksheet.py index 9758537f..28761dd1 100644 --- a/project_worksheet/models/project_worksheet.py +++ b/project_worksheet/models/project_worksheet.py @@ -204,8 +204,12 @@ def _generate_timesheets_if_empty(self): self._generate_timesheets() def _send_approval_email(self): - template = self.env.ref("project_worksheet.email_template_worksheet_approval") - template.send_mail(self.id, force_send=True) + template_id = self.env.ref("project_worksheet.email_template_worksheet_approval").id + self.message_post_with_template( + template_id, + composition_mode="comment", + message_type="comment", + ) def _get_remind_client_action(self): template = self.env.ref("project_worksheet.email_template_worksheet_approval") @@ -239,9 +243,31 @@ def _confirm_worksheet(self, source): self._send_confirmation_email() def _send_confirmation_email(self): - template = self.env.ref("project_worksheet.email_template_worksheet_confirmed") + template_id = self._get_confirmation_template_id() for worksheet in self: - template.send_mail(worksheet.id, force_send=True) + worksheet._post_confirmation_message(template_id) + + def _get_confirmation_template_id(self): + return self.env.ref("project_worksheet.email_template_worksheet_confirmed").id + + def _post_confirmation_message(self, template_id): + # Post the message using the appropriate user context + record = self._get_mail_sender_record() + record.message_post_with_template( + template_id, + composition_mode="comment", + message_type="comment", + ) + + def _get_mail_sender_record(self): + # Use root user (OdooBot) if the client is the one confirming the worksheet + if self.approval_source == "client": + return self._get_root_user_record() + return self + + def _get_root_user_record(self): + root_user = self.env.ref("base.user_root") + return self.with_user(root_user) def _generate_timesheets(self): timesheet_model = self.env["account.analytic.line"]