[IMP] mass_mailing_crm: Improve lead/opportunity computation
Prior to this commit, two fields were used for lead and opportunities count. As both of these fields were set with the same values and only existed for labelling purposes, this commit replaces them by a single field and handles the the labels in the xml. This commit also improves the way lead count is computed by using a read_group instead of a search in a loop. LINKS: closes odoo/odoo#38707 Taskid: 2086863 Pr: #38707 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
committed by
Thibault Delavallée
parent
66ce1e6302
commit
b4cbad1bed
@@ -10,50 +10,26 @@ class MassMailing(models.Model):
|
||||
_name = 'mailing.mailing'
|
||||
_inherit = 'mailing.mailing'
|
||||
|
||||
crm_lead_activated = fields.Boolean('Use Leads', compute='_compute_crm_lead_activated')
|
||||
crm_lead_count = fields.Integer('Lead Count', groups='sales_team.group_sale_salesman', compute='_compute_crm_lead_and_opportunities_count')
|
||||
crm_opportunities_count = fields.Integer('Opportunities Count', groups='sales_team.group_sale_salesman', compute='_compute_crm_lead_and_opportunities_count')
|
||||
use_leads = fields.Boolean('Use Leads', compute='_compute_use_leads')
|
||||
crm_lead_count = fields.Integer('Leads/Opportunities Count', groups='sales_team.group_sale_salesman', compute='_compute_crm_lead_count')
|
||||
|
||||
def _compute_crm_lead_activated(self):
|
||||
def _compute_use_leads(self):
|
||||
for mass_mailing in self:
|
||||
mass_mailing.crm_lead_activated = self.env.user.has_group('crm.group_use_lead')
|
||||
mass_mailing.use_leads = self.env.user.has_group('crm.group_use_lead')
|
||||
|
||||
@api.depends('crm_lead_activated')
|
||||
def _compute_crm_lead_and_opportunities_count(self):
|
||||
def _compute_crm_lead_count(self):
|
||||
lead_data = self.env['crm.lead'].with_context(active_test=False).read_group(
|
||||
[('source_id', 'in', self.source_id.ids)],
|
||||
['source_id'], ['source_id']
|
||||
)
|
||||
mapped_data = {datum['source_id'][0]: datum['source_id_count'] for datum in lead_data}
|
||||
for mass_mailing in self:
|
||||
lead_and_opportunities_count = mass_mailing.crm_lead_count = self.env['crm.lead'] \
|
||||
.with_context(active_test=False) \
|
||||
.search_count(self._get_crm_utm_domain())
|
||||
if mass_mailing.crm_lead_activated:
|
||||
mass_mailing.crm_lead_count = lead_and_opportunities_count
|
||||
mass_mailing.crm_opportunities_count = 0
|
||||
else:
|
||||
mass_mailing.crm_lead_count = 0
|
||||
mass_mailing.crm_opportunities_count = lead_and_opportunities_count
|
||||
mass_mailing.crm_lead_count = mapped_data.get(mass_mailing.source_id.id, 0)
|
||||
|
||||
def action_redirect_to_leads(self):
|
||||
action = self.env.ref('crm.crm_lead_all_leads').read()[0]
|
||||
action['domain'] = self._get_crm_utm_domain()
|
||||
action['context'] = {'default_type': 'lead', 'active_test': False, 'create': False}
|
||||
return action
|
||||
|
||||
def action_redirect_to_opportunities(self):
|
||||
action = self.env.ref('crm.crm_lead_opportunities').read()[0]
|
||||
def action_redirect_to_leads_and_opportunities(self):
|
||||
view = 'crm.crm_lead_all_leads' if self.use_leads else 'crm.crm_lead_opportunities'
|
||||
action = self.env.ref(view).read()[0]
|
||||
action['view_mode'] = 'tree,kanban,graph,pivot,form,calendar'
|
||||
action['domain'] = self._get_crm_utm_domain()
|
||||
action['domain'] = [('source_id', 'in', self.source_id.ids)]
|
||||
action['context'] = {'active_test': False, 'create': False}
|
||||
return action
|
||||
|
||||
def _get_crm_utm_domain(self):
|
||||
""" We want all records that match the UTMs """
|
||||
domain = []
|
||||
if self.campaign_id:
|
||||
domain = expression.AND([domain, [('campaign_id', '=', self.campaign_id.id)]])
|
||||
if self.source_id:
|
||||
domain = expression.AND([domain, [('source_id', '=', self.source_id.id)]])
|
||||
if self.medium_id:
|
||||
domain = expression.AND([domain, [('medium_id', '=', self.medium_id.id)]])
|
||||
if not domain:
|
||||
domain = expression.AND([domain, [(0, '=', 1)]])
|
||||
|
||||
return domain
|
||||
|
||||
@@ -6,22 +6,18 @@
|
||||
<field name="inherit_id" ref="mass_mailing.view_mail_mass_mailing_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//button[@id='button_view_sent']" position="before">
|
||||
<field name="crm_lead_activated" invisible="1"/>
|
||||
<button name="action_redirect_to_leads"
|
||||
<button name="action_redirect_to_leads_and_opportunities"
|
||||
type="object"
|
||||
icon="fa-star"
|
||||
class="oe_stat_button"
|
||||
groups="sales_team.group_sale_salesman"
|
||||
attrs="{'invisible': ['|', ('state', '=', 'draft'), ('crm_lead_activated', '=', False)]}" >
|
||||
<field name="crm_lead_count" string="Leads" widget="statinfo"/>
|
||||
</button>
|
||||
<button name="action_redirect_to_opportunities"
|
||||
type="object"
|
||||
icon="fa-star"
|
||||
class="oe_stat_button"
|
||||
groups="sales_team.group_sale_salesman"
|
||||
attrs="{'invisible': ['|', ('state', '=', 'draft'), ('crm_lead_activated', '=', True)]}" >
|
||||
<field name="crm_opportunities_count" string="Opportunities" widget="statinfo"/>
|
||||
attrs="{'invisible': [('state', '=', 'draft')]}">
|
||||
<div class="o_field_widget o_stat_info">
|
||||
<field name="use_leads" invisible="1"/>
|
||||
<span class="o_stat_value"><field nolabel="1" name="crm_lead_count"/></span>
|
||||
<span class="o_stat_text" attrs="{'invisible': [('use_leads', '=', False)]}">Leads</span>
|
||||
<span class="o_stat_text" attrs="{'invisible': [('use_leads', '=', True)]}">Opportunities</span>
|
||||
</div>
|
||||
</button>
|
||||
</xpath>
|
||||
</field>
|
||||
|
||||
Reference in New Issue
Block a user