Переглянути джерело

fix(helpdesk): correct request type codes in priority mapping

odoo 2 місяців тому
батько
коміт
fc485686e8
1 змінених файлів з 57 додано та 15 видалено
  1. 57 15
      helpdesk_extras/models/helpdesk_ticket.py

+ 57 - 15
helpdesk_extras/models/helpdesk_ticket.py

@@ -119,29 +119,71 @@ class HelpdeskTicket(models.Model):
         - Improvement + High = 1 (Normal)
         - Improvement + Normal = 0 (Low)
         """
-        if not self.request_type_id or not self.business_impact:
-            return
+        priority = self._compute_priority_from_impact()
+        if priority is not None:
+            self.priority = priority
+
+    def _compute_priority_from_impact(self, request_type_id=None, business_impact=None):
+        """
+        Helper method to compute priority from request type and business impact.
+        Can be used by onchange, create, and write methods.
+        Returns the priority string or None if not applicable.
+        """
+        # Use provided values or instance values
+        if request_type_id is None:
+            request_type = self.request_type_id
+        else:
+            request_type = self.env['helpdesk.request.type'].browse(request_type_id) if isinstance(request_type_id, int) else request_type_id
+        
+        if business_impact is None:
+            business_impact = self.business_impact
         
-        # Determine if it's an incident (code 'INC') or improvement
-        is_incident = self.request_type_id.code == 'INC'
+        if not request_type or not business_impact:
+            return None
+        
+        type_code = request_type.code if hasattr(request_type, 'code') else ''
+        if not type_code:
+            return None
         
         # Priority mapping based on business_impact
         # business_impact: '0' = Critical, '1' = High, '2' = Normal
         # priority: '0' = Low, '1' = Normal, '2' = High, '3' = Urgent
         priority_map = {
             # Incidents get +1 priority boost
-            ('INC', '0'): '3',  # Incident + Critical = Urgent
-            ('INC', '1'): '2',  # Incident + High = High
-            ('INC', '2'): '1',  # Incident + Normal = Normal
+            ('incident', '0'): '3',  # Incident + Critical = Urgent
+            ('incident', '1'): '2',  # Incident + High = High
+            ('incident', '2'): '1',  # Incident + Normal = Normal
             # Improvements have standard mapping
-            ('IMP', '0'): '2',  # Improvement + Critical = High
-            ('IMP', '1'): '1',  # Improvement + High = Normal
-            ('IMP', '2'): '0',  # Improvement + Normal = Low
+            ('improvement', '0'): '2',  # Improvement + Critical = High
+            ('improvement', '1'): '1',  # Improvement + High = Normal
+            ('improvement', '2'): '0',  # Improvement + Normal = Low
         }
         
-        type_code = self.request_type_id.code or ''
-        key = (type_code, self.business_impact)
-        
-        if key in priority_map:
-            self.priority = priority_map[key]
+        key = (type_code, business_impact)
+        return priority_map.get(key)
+
+    @api.model_create_multi
+    def create(self, vals_list):
+        """Override create to auto-calculate priority for website form submissions."""
+        for vals in vals_list:
+            # Only calculate if priority not explicitly set and we have both fields
+            if 'priority' not in vals or vals.get('priority') == '0':
+                request_type_id = vals.get('request_type_id')
+                business_impact = vals.get('business_impact')
+                if request_type_id and business_impact:
+                    priority = self._compute_priority_from_impact(request_type_id, business_impact)
+                    if priority is not None:
+                        vals['priority'] = priority
+        return super().create(vals_list)
 
+    def write(self, vals):
+        """Override write to recalculate priority when request_type or business_impact changes."""
+        result = super().write(vals)
+        # If either field was updated, recalculate priority for affected records
+        if 'request_type_id' in vals or 'business_impact' in vals:
+            for record in self:
+                priority = record._compute_priority_from_impact()
+                if priority is not None and record.priority != priority:
+                    # Use super().write to avoid recursion
+                    super(HelpdeskTicket, record).write({'priority': priority})
+        return result