From b160bf28c0b39e2e90ba649bd8edf96bb850489b Mon Sep 17 00:00:00 2001
From: joel <joemaglioni1@gmail.com>
Date: Mon, 3 Jun 2024 15:52:55 -0300
Subject: [PATCH] [T4052][T4053][T4288] write partner info and fix notes.

---
 models/calendar_event.py | 39 ++++++++++++++++++++++++++---------
 models/mail_activity.py  | 16 +++++++--------
 models/sale_order.py     | 44 +++++++++++++++++-----------------------
 3 files changed, 55 insertions(+), 44 deletions(-)

diff --git a/models/calendar_event.py b/models/calendar_event.py
index 03575f0..43d354d 100644
--- a/models/calendar_event.py
+++ b/models/calendar_event.py
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-from odoo import api, fields, models
+from odoo import api, fields, models, _
 
 P_VALS = [
     'qualification',
@@ -72,10 +72,17 @@ class CalendarEvent(models.Model):
         readonly=True
     )
 
+    def _get_default_activity(self):
+        activity_id = self.env.context.get('default_activity_type_id')
+        return self.env['mail.activity.type'].browse(activity_id) if activity_id else self.env['mail.activity.type']
+
     def _get_default_category(self):
-        activity_id = self.env.context.get('default_activity_type_id', False)
-        activity = self.env['mail.activity.type'].browse(activity_id)
-        return activity.category
+        activity = self._get_default_activity()
+        return activity.category if activity else False
+
+    def _get_default_note(self):
+        activity = self._get_default_activity()
+        return activity.default_note if activity else False
 
     @api.model
     def default_get(self, fields):
@@ -91,15 +98,27 @@ class CalendarEvent(models.Model):
             defaults['number_norias'] = partner.number_norias or False
             defaults['quality_claim'] = partner.quality_claim or False
             defaults['quality_service'] = partner.quality_service or False
+            defaults['description'] = self._get_default_note()
         return defaults
 
+    def write_partner_vals(self, partner, vals):
+        p_values = {val: vals[val] for val in P_VALS if val in vals}
+        if p_values:
+            partner.write(p_values)
+
     def write(self, values):
         res = super(CalendarEvent, self).write(values)
-        partner = self.partner_ids[0]
-        p_values = {}
-        for val in P_VALS:
-            if val in values:
-                p_values[val] = values[val]
-        partner.write(p_values)
+        if self.partner_ids:
+            partner = self.partner_ids[0]
+            self.write_partner_vals(partner, values)
         return res
 
+    @api.model_create_multi
+    def create(self, vals):
+        events = super(CalendarEvent, self).create(vals)
+        for event in events:
+            if event.partner_ids:
+                partner = event.partner_ids[0]
+                self.write_partner_vals(partner, event)
+        return events
+
diff --git a/models/mail_activity.py b/models/mail_activity.py
index c42066c..2e11a69 100644
--- a/models/mail_activity.py
+++ b/models/mail_activity.py
@@ -28,20 +28,18 @@ class MailActivity(models.Model):
         string='Customer Partner',
         default=lambda self: self._default_partner()
     )
-
     @api.model
     def _default_partner(self):
-        partner = self.env['res.partner']
-        model = self._context.get('default_res_model', False)
-        res_id = self.env.context.get('default_res_id', False)
+        context = self.env.context
+        model = context.get('default_res_model')
+        res_id = context.get('default_res_id')
         if model and res_id:
             obj = self.env[model].browse(res_id)
             if obj._name == 'res.partner':
-                partner |= obj
-            else:
-                if obj.partner_id:
-                    partner |= obj.partner_id
-        return partner
+                return obj
+            elif hasattr(obj, 'partner_id'):
+                return obj.partner_id
+        return self.env['res.partner']
 
     def action_create_calendar_event(self):
         res = super(MailActivity, self).action_create_calendar_event()
diff --git a/models/sale_order.py b/models/sale_order.py
index 8f16567..94b9d79 100644
--- a/models/sale_order.py
+++ b/models/sale_order.py
@@ -224,36 +224,30 @@ class SaleOrder(models.Model):
             result['fields']['monitoring_manager_id']['readonly'] = False
         return result
     def create_activity(self, order):
-        activity = self.env['mail.activity']
+        activity_model = self.env['mail.activity']
         action_type = self.env.context.get('action_type')
-        note = _('Sales order created')
-        if action_type == 'order_confirmed':
-            note = _('Budget confirmed')
-        elif action_type == 'change_delivery_date':
-            note = _('Delivery date')
-        activty_type = self.env['mail.activity.type'].search([('category', '=', action_type)], limit=1)
-        action_name = dict(activty_type.fields_get(allfields=['category'])['category']['selection']).get(action_type)
-        res_model_id = self.env['ir.model']._get_id(self._name)
-        vals = {
-            'partner_id': order.partner_id.id,
-            'date': fields.Date.today(),
-            'summary': _('%s - %s') % (action_name, order.name),
-            'activity_type_id': activty_type.id,
-            'res_model_id': res_model_id,
-            'res_id': order.id,
-            'note': note
-        }
-        activity.create(vals)
-        return True
+        activity_type = self.env['mail.activity.type'].search([('category', '=', action_type)], limit=1)
+        if activity_type:
+            action_name = dict(activity_type.fields_get(allfields=['category'])['category']['selection']).get(action_type, '')
+            res_model_id = self.env['ir.model']._get_id(self._name)
+            vals = {
+                'partner_id': order.partner_id.id,
+                'date': fields.Date.today(),
+                'summary': _('%s - %s') % (action_name, order.name),
+                'activity_type_id': activity_type.id,
+                'res_model_id': res_model_id,
+                'res_id': order.id,
+                'note': activity_type.default_note
+            }
+            activity_model.create(vals)
 
     def action_confirm(self):
         res = super(SaleOrder, self).action_confirm()
-        order = self
-        self.with_context(action_type='order_confirmed').create_activity(order)
+        self.with_context(action_type='order_confirmed').create_activity(self)
         return res
 
     @api.model
     def create(self, vals):
-        result = super(SaleOrder, self).create(vals)
-        self.with_context(action_type='new_quote').create_activity(result)
-        return result
+        order = super(SaleOrder, self).create(vals)
+        order.with_context(action_type='new_quote').create_activity(order)
+        return order
-- 
GitLab