[IMP] crm: replace meeting count by date for lead stat btn

Instead of having a count field for meetings linked to
a crm.lead, show a (start) date instead:
- If there is a next meeting, show its date + "Next Meeting"
- Otherwise, show last meeting date + "Last Meeting"
- If no meeting, show no date and display "No Meeting"
- hide button for new records

This commit removes the field "calendar_event_count" that
has no use anymore. A test using the count field is adapted.
Another is added to assert new fields behavior.

UPG PR odoo/upgrade#4935

Task-3418397

closes odoo/odoo#128366

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
Noe Antoine
2023-08-29 12:36:00 +00:00
parent fa8f68ce05
commit b96512a23c
3 changed files with 58 additions and 11 deletions
+23 -6
View File
@@ -237,9 +237,10 @@ class Lead(models.Model):
index=True, ondelete='restrict', tracking=True)
# Statistics
calendar_event_ids = fields.One2many('calendar.event', 'opportunity_id', string='Meetings')
calendar_event_count = fields.Integer('# Meetings', compute='_compute_calendar_event_count')
duplicate_lead_ids = fields.Many2many("crm.lead", compute="_compute_potential_lead_duplicates", string="Potential Duplicate Lead", context={"active_test": False})
duplicate_lead_count = fields.Integer(compute="_compute_potential_lead_duplicates", string="Potential Duplicate Lead Count")
meeting_display_date = fields.Date(compute="_compute_meeting_display")
meeting_display_label = fields.Char(compute="_compute_meeting_display")
# UX
partner_email_update = fields.Boolean('Partner Email will Update', compute='_compute_partner_email_update')
partner_phone_update = fields.Boolean('Partner Phone will Update', compute='_compute_partner_phone_update')
@@ -537,13 +538,29 @@ class Lead(models.Model):
for lead in self:
lead.recurring_revenue_prorated = (lead.recurring_revenue or 0.0) * (lead.probability or 0) / 100.0
def _compute_calendar_event_count(self):
@api.depends('calendar_event_ids', 'calendar_event_ids.start')
def _compute_meeting_display(self):
now = fields.Datetime.now()
meeting_data = self.env['calendar.event'].sudo()._read_group([
('opportunity_id', 'in', self.ids)
], ['opportunity_id'], ['__count'])
mapped_data = {opportunity.id: count for opportunity, count in meeting_data}
('opportunity_id', 'in', self.ids),
], ['opportunity_id'], ['start:array_agg', 'start:max'])
mapped_data = {
lead: {
'last_meeting_date': last_meeting_date,
'next_meeting_date': min([dt for dt in meeting_start_dates if dt > now] or [False]),
} for lead, meeting_start_dates, last_meeting_date in meeting_data
}
for lead in self:
lead.calendar_event_count = mapped_data.get(lead.id, 0)
lead_meeting_info = mapped_data.get(lead)
if not lead_meeting_info:
lead.meeting_display_date = False
lead.meeting_display_label = _('No Meeting')
elif lead_meeting_info['next_meeting_date']:
lead.meeting_display_date = lead_meeting_info['next_meeting_date']
lead.meeting_display_label = _('Next Meeting')
else:
lead.meeting_display_date = lead_meeting_info['last_meeting_date']
lead.meeting_display_label = _('Last Meeting')
@api.depends('email_domain_criterion', 'email_normalized', 'partner_id',
'phone_sanitized')
+32 -1
View File
@@ -4,6 +4,7 @@
from datetime import datetime
from freezegun import freeze_time
from odoo import fields
from odoo.addons.base.tests.test_format_address_mixin import FormatAddressCase
from odoo.addons.crm.models.crm_lead import PARTNER_FIELDS_TO_SYNC, PARTNER_ADDRESS_FIELDS_TO_SYNC
from odoo.addons.crm.tests.common import TestCrmCommon, INCOMING_EMAIL
@@ -302,6 +303,36 @@ class TestCRMLead(TestCrmCommon):
lead.action_set_lost()
self.assertEqual(lead.date_closed, datetime.now(), "Closed date is updated after marking lead as lost")
@users('user_sales_manager')
def test_crm_lead_meeting_display_fields(self):
lead = self.env['crm.lead'].create({'name': 'Lead With Meetings'})
meeting_1, meeting_2, meeting_3 = self.env['calendar.event'].create([{
'name': 'Meeting 1 of Lead',
'opportunity_id': lead.id,
'start': '2022-07-12 08:00:00',
'stop': '2022-07-12 10:00:00',
}, {
'name': 'Meeting 2 of Lead',
'opportunity_id': lead.id,
'start': '2022-07-14 08:00:00',
'stop': '2022-07-14 10:00:00',
}, {
'name': 'Meeting 3 of Lead',
'opportunity_id': lead.id,
'start': '2022-07-15 08:00:00',
'stop': '2022-07-15 10:00:00',
}])
with freeze_time('2022-07-13 11:00:00'):
self.assertEqual(lead.meeting_display_date, fields.Date.from_string('2022-07-14'))
self.assertEqual(lead.meeting_display_label, 'Next Meeting')
(meeting_2 | meeting_3).unlink()
self.assertEqual(lead.meeting_display_date, fields.Date.from_string('2022-07-12'))
self.assertEqual(lead.meeting_display_label, 'Last Meeting')
meeting_1.unlink()
self.assertFalse(lead.meeting_display_date)
self.assertEqual(lead.meeting_display_label, 'No Meeting')
@users('user_sales_manager')
def test_crm_lead_partner_sync(self):
lead, partner = self.lead_1.with_user(self.env.user), self.contact_2
@@ -560,7 +591,7 @@ class TestCRMLead(TestCrmCommon):
'stop': '2022-07-13 10:00:00',
}
])
self.assertEqual(lead.calendar_event_count, 1)
self.assertEqual(len(lead.calendar_event_ids), 1)
self.assertEqual(meetings.opportunity_id, lead)
self.assertEqual(meetings.mapped('res_id'), [lead.id, lead.id])
self.assertEqual(meetings.mapped('res_model'), ['crm.lead', 'crm.lead'])
+3 -4
View File
@@ -29,11 +29,10 @@
<button name="action_schedule_meeting" type="object"
class="oe_stat_button" icon="fa-calendar"
context="{'partner_id': partner_id}"
invisible="type == 'lead'">
invisible="not id or type == 'lead'">
<div class="o_stat_info">
<field name="calendar_event_count" class="o_stat_value"/>
<span class="o_stat_text" invisible="calendar_event_count &lt; 2"> Meetings</span>
<span class="o_stat_text" invisible="calendar_event_count &gt; 1"> Meeting</span>
<span class="o_stat_text"><field name="meeting_display_label"/></span>
<field name="meeting_display_date" class="o_stat_value" invisible="not meeting_display_date"/>
</div>
</button>
<button name="action_show_potential_duplicates" type="object"