PURPOSE
Make crm.team deletion effect explicit on main models (excluding wizards
or reports). Purpose is to ensure deleting a crm team has known side
effets.
SPECIFICATIONS
M2O on crm.team set to CASCADE
* crm.lead.scoring.frequency: as we remove the team no need to keep the
PLS data -> change to "cascade";
* crm.team.member: when removing a team, remove its members as it prevents
from unlinking the team;
M2O on crm.team set to SET NULL
* account.move: void team_id field but track it to keep history;
* crm.lead: void team_id field as it will be taken back into assign
process (either manual or automatic), and will therefore be managed
by other sales persons. In case of archived / lost leads setting to
void has no real issue;
* crm.stage: void team_id and set stage as shared to avoid loosing stage
information for existing leads in that stage;
* res.partner: void team_id taking care of this customer as this field
is mainly informative anyway;
* crm.iap.lead.mining.request: void team_id as it only impacts generated
leads. Those will be created with a void team which is ok;
* crm.reveal.rule: void team_id as it only impacts generated leads. Those
will be created with a void team which is ok;
* event.lead.rule: void lead_sales_team_id as this impact generated leads.
Those will be created with a void team which is ok;
* pos.config and pos.oser: void crm_team_id field. It is optional as it is
not a core field of this model. Pos configurations and orders are still
valid without team assigned on it;
* sale.order: void team_id field but track it to keep history;
* website: void salesteam_id field;
Prevent unlink when having more than 5 SO not canceled. If more than 5 active
SOs in the team, we consider this team to be actively used. 5 is some random
guess based on "user testing", aka more than testing CRM feature and less
than use it in real life use cases.
LINKS
Task ID-2507750
COM PR odoo/odoo#70645
ENT PR odoo/enterprise#18266
66 lines
2.7 KiB
Python
66 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models
|
|
|
|
|
|
class Partner(models.Model):
|
|
_name = 'res.partner'
|
|
_inherit = 'res.partner'
|
|
|
|
team_id = fields.Many2one('crm.team', string='Sales Team', ondelete="set null")
|
|
opportunity_ids = fields.One2many('crm.lead', 'partner_id', string='Opportunities', domain=[('type', '=', 'opportunity')])
|
|
opportunity_count = fields.Integer("Opportunity", compute='_compute_opportunity_count')
|
|
|
|
@api.model
|
|
def default_get(self, fields):
|
|
rec = super(Partner, self).default_get(fields)
|
|
active_model = self.env.context.get('active_model')
|
|
if active_model == 'crm.lead' and len(self.env.context.get('active_ids', [])) <= 1:
|
|
lead = self.env[active_model].browse(self.env.context.get('active_id')).exists()
|
|
if lead:
|
|
rec.update(
|
|
phone=lead.phone,
|
|
mobile=lead.mobile,
|
|
function=lead.function,
|
|
title=lead.title.id,
|
|
website=lead.website,
|
|
street=lead.street,
|
|
street2=lead.street2,
|
|
city=lead.city,
|
|
state_id=lead.state_id.id,
|
|
country_id=lead.country_id.id,
|
|
zip=lead.zip,
|
|
)
|
|
return rec
|
|
|
|
def _compute_opportunity_count(self):
|
|
# retrieve all children partners and prefetch 'parent_id' on them
|
|
all_partners = self.with_context(active_test=False).search([('id', 'child_of', self.ids)])
|
|
all_partners.read(['parent_id'])
|
|
|
|
opportunity_data = self.env['crm.lead'].with_context(active_test=False).read_group(
|
|
domain=[('partner_id', 'in', all_partners.ids)],
|
|
fields=['partner_id'], groupby=['partner_id']
|
|
)
|
|
|
|
self.opportunity_count = 0
|
|
for group in opportunity_data:
|
|
partner = self.browse(group['partner_id'][0])
|
|
while partner:
|
|
if partner in self:
|
|
partner.opportunity_count += group['partner_id_count']
|
|
partner = partner.parent_id
|
|
|
|
def action_view_opportunity(self):
|
|
'''
|
|
This function returns an action that displays the opportunities from partner.
|
|
'''
|
|
action = self.env['ir.actions.act_window']._for_xml_id('crm.crm_lead_opportunities')
|
|
action['context'] = {'active_test': False}
|
|
if self.is_company:
|
|
action['domain'] = [('partner_id.commercial_partner_id.id', '=', self.id)]
|
|
else:
|
|
action['domain'] = [('partner_id.id', '=', self.id)]
|
|
return action
|