|
| 1 | +# Copyright 2024 - TODAY, Kaynnan Lemes <[email protected]> |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 3 | +# flake8: noqa: B950 |
| 4 | + |
| 5 | +from odoo import _, fields, models |
| 6 | + |
| 7 | + |
| 8 | +class ProjectProject(models.Model): |
| 9 | + _inherit = "project.project" |
| 10 | + |
| 11 | + sla_deadline = fields.Datetime(string="SLA Deadline") |
| 12 | + sla_expired = fields.Boolean(string="SLA Expired") |
| 13 | + |
| 14 | + def _prepare_sla_message(self, sla_line, sla): |
| 15 | + """Prepare the message to notify about the SLA status, including reached_date, |
| 16 | + SLA line ID, current stage, and estimated stage from project.status.sla.""" |
| 17 | + current_stage = ( |
| 18 | + self.project_status.name |
| 19 | + ) # Assuming project_status is the current stage |
| 20 | + sla_stage = ( |
| 21 | + sla.stage_id.name |
| 22 | + ) # Fetching the estimated stage from project.status.sla |
| 23 | + |
| 24 | + # Format the dates explicitly |
| 25 | + estimated_date = ( |
| 26 | + sla_line.sla_deadline.strftime("%d/%m/%Y %H:%M") |
| 27 | + if sla_line.sla_deadline |
| 28 | + else "N/A" |
| 29 | + ) |
| 30 | + reached_date = ( |
| 31 | + sla_line.reached_date.strftime("%d/%m/%Y %H:%M") |
| 32 | + if sla_line.reached_date |
| 33 | + else "N/A" |
| 34 | + ) |
| 35 | + |
| 36 | + if self.sla_expired: |
| 37 | + message = _( |
| 38 | + "<p>The SLA for Project <b>%s</b> in Stage <b>%s</b> has been exceeded.</p>" |
| 39 | + "<p>Current Stage: <b>%s</b></p>" |
| 40 | + "<p>Estimated Stage: <b>%s</b></p>" |
| 41 | + "<p>SLA ID: <b>%s</b></p>" |
| 42 | + "<p>Estimated Date: <b>%s</b></p>" |
| 43 | + "<p>Reached Date: <b>%s</b></p>" |
| 44 | + % ( |
| 45 | + self.name, |
| 46 | + current_stage, |
| 47 | + current_stage, |
| 48 | + sla_stage, |
| 49 | + sla.id, |
| 50 | + estimated_date, |
| 51 | + reached_date, |
| 52 | + ) |
| 53 | + ) |
| 54 | + else: |
| 55 | + message = _( |
| 56 | + "<p>The SLA for Project <b>%s</b> in Stage <b>%s</b> has been successfully met within the proposed deadline.</p>" |
| 57 | + "<p>Current Stage: <b>%s</b></p>" |
| 58 | + "<p>Estimated Stage: <b>%s</b></p>" |
| 59 | + "<p>SLA ID: <b>%s</b></p>" |
| 60 | + "<p>Estimated Date: <b>%s</b></p>" |
| 61 | + "<p>Reached Date: <b>%s</b></p>" |
| 62 | + % ( |
| 63 | + self.name, |
| 64 | + current_stage, |
| 65 | + current_stage, |
| 66 | + sla_stage, |
| 67 | + sla.id, |
| 68 | + estimated_date, |
| 69 | + reached_date, |
| 70 | + ) |
| 71 | + ) |
| 72 | + return message |
| 73 | + |
| 74 | + def _post_sla_message(self, sla_line, sla): |
| 75 | + """Post a message to notify users about the SLA status, including additional information.""" |
| 76 | + message = self._prepare_sla_message(sla_line, sla) |
| 77 | + self.message_post( |
| 78 | + body=message, message_type="notification", subtype="mail.mt_comment" |
| 79 | + ) |
| 80 | + |
| 81 | + def _prepare_sla_line_values(self, sla): |
| 82 | + """Prepare the values to create an SLA line for the current project.""" |
| 83 | + status = "not_met" if self.sla_expired else "met" |
| 84 | + return { |
| 85 | + "project_id": self.id, |
| 86 | + "stage_id": self.project_status.id, # Assuming project_status is the current stage |
| 87 | + "sla_id": sla.id, |
| 88 | + "sla_deadline": self.sla_deadline, |
| 89 | + "sla_expired": self.sla_expired, |
| 90 | + "reached_date": fields.Datetime.now(), |
| 91 | + "status": status, |
| 92 | + } |
| 93 | + |
| 94 | + def _create_sla_line(self, sla): |
| 95 | + """Create a new SLA line record for the project.""" |
| 96 | + sla_line_vals = self._prepare_sla_line_values(sla) |
| 97 | + return self.env["project.status.sla.line"].create(sla_line_vals) |
| 98 | + |
| 99 | + def _create_project_sla_history(self, sla): |
| 100 | + """Create SLA history records for the project if not already present.""" |
| 101 | + # Check if SLA history already exists for this project and SLA |
| 102 | + existing_history_count = self.env["project.status.sla.line"].search_count( |
| 103 | + [("sla_id", "=", sla.id)] |
| 104 | + ) |
| 105 | + |
| 106 | + # If history exists, do not create another one |
| 107 | + if existing_history_count: |
| 108 | + return |
| 109 | + |
| 110 | + # Create new SLA line and post a notification message |
| 111 | + sla_line = self._create_sla_line(sla) |
| 112 | + self._post_sla_message(sla_line, sla) |
0 commit comments