[REF] crm : some code improvements
- remove backward compatibility method `case_mark_won` and `case_mark_lost` - change signature of `find_stage` method, by setting team_id optional, since we can get if from `self.team_id` - remove unused methods in crm_lead.py - remove unsed code in base_merge_partner.py - remove unused method in res_partner.py
This commit is contained in:
@@ -65,7 +65,7 @@ class Lead(FormatAddress, models.Model):
|
||||
|
||||
def _default_stage_id(self):
|
||||
team = self.env['crm.team'].sudo()._get_default_team_id(user_id=self.env.uid)
|
||||
return self.stage_find(team.id, [('fold', '=', False)])
|
||||
return self.stage_find(team_id=team.id, domain=[('fold', '=', False)])
|
||||
|
||||
name = fields.Char('Opportunity', required=True, index=True)
|
||||
partner_id = fields.Many2one('res.partner', string='Customer', track_visibility='onchange', index=True,
|
||||
@@ -172,6 +172,7 @@ class Lead(FormatAddress, models.Model):
|
||||
# restore order of the search
|
||||
order_mapping = dict((item[0], stage_ids.index(item[0])) for item in result) # match stage_id with its index
|
||||
result.sort(key=lambda item: order_mapping.get(item[0]))
|
||||
|
||||
fold = {}
|
||||
for stage in stages:
|
||||
fold[stage.id] = stage.fold or False
|
||||
@@ -363,10 +364,6 @@ class Lead(FormatAddress, models.Model):
|
||||
""" Lost semantic: probability = 0, active = False """
|
||||
return self.write({'probability': 0, 'active': False})
|
||||
|
||||
# TODO JEM : check, nad remove this
|
||||
# Backward compatibility
|
||||
case_mark_lost = action_set_lost
|
||||
|
||||
@api.multi
|
||||
def action_set_active(self):
|
||||
return self.write({'active': True})
|
||||
@@ -379,14 +376,10 @@ class Lead(FormatAddress, models.Model):
|
||||
def action_set_won(self):
|
||||
""" Won semantic: probability = 100 (active untouched) """
|
||||
for lead in self:
|
||||
stage_id = lead.stage_find(lead.team_id.id, [('probability', '=', 100.0), ('on_change', '=', True)])
|
||||
stage_id = lead.stage_find(domain=[('probability', '=', 100.0), ('on_change', '=', True)])
|
||||
lead.write({'stage_id': stage_id, 'probability': 100})
|
||||
return True
|
||||
|
||||
# TODO JEM : check, nad remove this
|
||||
# Backward compatibility
|
||||
case_mark_won = action_set_won
|
||||
|
||||
@api.multi
|
||||
def action_schedule_meeting(self):
|
||||
""" Open meeting's calendar view to schedule meeting on current opportunity.
|
||||
@@ -414,9 +407,9 @@ class Lead(FormatAddress, models.Model):
|
||||
# ----------------------------------------
|
||||
# Business Methods
|
||||
# ----------------------------------------
|
||||
# TODO JEM : team_id should be optional
|
||||
|
||||
@api.multi
|
||||
def stage_find(self, team_id, domain=None, order='sequence'):
|
||||
def stage_find(self, team_id=False, domain=None, order='sequence'):
|
||||
""" Determine the stage of the current lead with its teams, the given domain and the given team_id
|
||||
:param team_id
|
||||
:param domain : base search domain for stage
|
||||
@@ -435,7 +428,8 @@ class Lead(FormatAddress, models.Model):
|
||||
else:
|
||||
search_domain = [('team_id', '=', False)]
|
||||
# AND with the domain in parameter
|
||||
search_domain += list(domain)
|
||||
if domain:
|
||||
search_domain += list(domain)
|
||||
# perform search, return the first found
|
||||
stage = self.env['crm.stage'].search(search_domain, order=order, limit=1)
|
||||
if stage:
|
||||
@@ -711,7 +705,7 @@ class Lead(FormatAddress, models.Model):
|
||||
'date_conversion': fields.Datetime.now(),
|
||||
}
|
||||
if not self.stage_id:
|
||||
stage_id = self.stage_find(team_id, [])
|
||||
stage_id = self.stage_find(team_id=team_id)
|
||||
value['stage_id'] = stage_id
|
||||
if stage_id:
|
||||
value['probability'] = self.env['crm.stage'].browse(stage_id).probability
|
||||
@@ -1012,11 +1006,10 @@ class Lead(FormatAddress, models.Model):
|
||||
max_date = fields.Datetime.to_string(datetime.now() + timedelta(days=8))
|
||||
meetings_domain = [
|
||||
('start', '>=', min_date),
|
||||
('start', '<=', max_date)
|
||||
('start', '<=', max_date),
|
||||
('partner_ids', 'in', [self.env.user.partner_id.id])
|
||||
]
|
||||
#TODO JEM : remove this stupid context key
|
||||
# We need to add 'mymeetings' in the context for the search to be correct.
|
||||
meetings = self.env['calendar.event'].with_context(mymeetings=1).search(meetings_domain)
|
||||
meetings = self.env['calendar.event'].search(meetings_domain)
|
||||
for meeting in meetings:
|
||||
if meeting['start']:
|
||||
start = datetime.strptime(meeting['start'], tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
|
||||
|
||||
@@ -37,43 +37,6 @@ class Partner(models.Model):
|
||||
for partner in self:
|
||||
partner.activities_count = mapped_data.get(partner.id, 0)
|
||||
|
||||
# TODO JEM : it is still used ?
|
||||
@api.v7
|
||||
def redirect_partner_form(self, cr, uid, partner_id, context=None):
|
||||
search_view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'view_res_partner_filter')
|
||||
value = {
|
||||
'domain': "[]",
|
||||
'view_type': 'form',
|
||||
'view_mode': 'form,tree',
|
||||
'res_model': 'res.partner',
|
||||
'res_id': int(partner_id),
|
||||
'view_id': False,
|
||||
'context': context,
|
||||
'type': 'ir.actions.act_window',
|
||||
'search_view_id': search_view and search_view[1] or False
|
||||
}
|
||||
return value
|
||||
|
||||
# TODO JEM : it is still used ?
|
||||
@api.v7
|
||||
def make_opportunity(self, cr, uid, ids, opportunity_summary, planned_revenue=0.0, probability=0.0, partner_id=None, context=None):
|
||||
lead_obj = self.pool.get('crm.lead')
|
||||
tag_ids = self.pool['crm.lead.tag'].search(cr, uid, [])
|
||||
opportunity_ids = {}
|
||||
for partner in self.browse(cr, uid, ids, context=context):
|
||||
if not partner_id:
|
||||
partner_id = partner.id
|
||||
opportunity_id = lead_obj.create(cr, uid, {
|
||||
'name' : opportunity_summary,
|
||||
'planned_revenue' : planned_revenue,
|
||||
'probability' : probability,
|
||||
'partner_id' : partner_id,
|
||||
'tag_ids' : tag_ids and tag_ids[0] or [],
|
||||
'type': 'opportunity'
|
||||
}, context=context)
|
||||
opportunity_ids[partner_id] = opportunity_id
|
||||
return opportunity_ids
|
||||
|
||||
@api.multi
|
||||
def schedule_meeting(self):
|
||||
partner_ids = self.ids
|
||||
|
||||
@@ -40,7 +40,7 @@ class TestCRMLead(TestCrmCases):
|
||||
# Now I change the stage of opportunity to won.
|
||||
lead.action_set_won()
|
||||
# I check stage of opp should won, after change stage.
|
||||
stage_id = lead._stage_find(domain=[('probability', '=', 100.0)])
|
||||
stage_id = lead.stage_find(domain=[('probability', '=', 100.0)])
|
||||
self.assertEqual(stage_id, lead.stage_id, "Stage of opportunity is incorrect!")
|
||||
|
||||
def test_crm_lead_message(self):
|
||||
|
||||
@@ -57,7 +57,7 @@ class TestLead2opportunity2win(TestCrmCases):
|
||||
self.assertEqual(crm_case_2.stage_id.id, default_stage_id, "Stage of probability is incorrect!")
|
||||
|
||||
# I loose the second opportunity
|
||||
crm_case_2.case_mark_lost()
|
||||
crm_case_2.action_set_lost()
|
||||
|
||||
# I check details of the opportunity after the loose
|
||||
self.assertEqual(crm_case_2.probability, 0.0, "Revenue probability should be 0.0!")
|
||||
|
||||
@@ -673,7 +673,7 @@
|
||||
<field name="state">code</field>
|
||||
<field name="code">
|
||||
if context.get('active_model') == 'crm.lead' and context.get('active_ids'):
|
||||
self.case_mark_lost(cr, uid, context['active_ids'], context=context)
|
||||
self.action_set_lost(cr, uid, context['active_ids'], context=context)
|
||||
</field>
|
||||
</record>
|
||||
|
||||
|
||||
@@ -336,60 +336,6 @@ class MergePartnerAutomatic(models.TransientModel):
|
||||
# delete source partner, since they are merged
|
||||
src_partners.unlink()
|
||||
|
||||
#TODO JEM : remove me, since it is not used anymore
|
||||
def clean_emails(self, cr, uid, context=None):
|
||||
"""
|
||||
Clean the email address of the partner, if there is an email field with
|
||||
a mimum of two addresses, the system will create a new partner, with the
|
||||
information of the previous one and will copy the new cleaned email into
|
||||
the email field.
|
||||
"""
|
||||
context = dict(context or {})
|
||||
|
||||
proxy_model = self.pool['ir.model.fields']
|
||||
field_ids = proxy_model.search(cr, uid, [('model', '=', 'res.partner'),
|
||||
('ttype', 'like', '%2many')],
|
||||
context=context)
|
||||
fields = proxy_model.read(cr, uid, field_ids, context=context)
|
||||
reset_fields = dict((field['name'], []) for field in fields)
|
||||
|
||||
proxy_partner = self.pool['res.partner']
|
||||
context['active_test'] = False
|
||||
ids = proxy_partner.search(cr, uid, [], context=context)
|
||||
|
||||
fields = ['name', 'var' 'partner_id' 'is_company', 'email']
|
||||
partners = proxy_partner.read(cr, uid, ids, fields, context=context)
|
||||
|
||||
partners.sort(key=operator.itemgetter('id'))
|
||||
partners_len = len(partners)
|
||||
|
||||
_logger.info('partner_len: %r', partners_len)
|
||||
|
||||
for idx, partner in enumerate(partners):
|
||||
if not partner['email']:
|
||||
continue
|
||||
|
||||
percent = (idx / float(partners_len)) * 100.0
|
||||
_logger.info('idx: %r', idx)
|
||||
_logger.info('percent: %r', percent)
|
||||
try:
|
||||
emails = sanitize_email(partner['email'])
|
||||
head, tail = emails[:1], emails[1:]
|
||||
email = head[0] if head else False
|
||||
|
||||
proxy_partner.write(cr, uid, [partner['id']],
|
||||
{'email': email}, context=context)
|
||||
|
||||
for email in tail:
|
||||
values = dict(reset_fields, email=email)
|
||||
proxy_partner.copy(cr, uid, partner['id'], values,
|
||||
context=context)
|
||||
|
||||
except Exception:
|
||||
_logger.exception("There is a problem with this partner: %r", partner)
|
||||
raise
|
||||
return True
|
||||
|
||||
# ----------------------------------------
|
||||
# Helpers
|
||||
# ----------------------------------------
|
||||
@@ -716,50 +662,3 @@ class MergePartnerAutomatic(models.TransientModel):
|
||||
self.current_line_id.unlink()
|
||||
|
||||
return self._next_screen()
|
||||
|
||||
|
||||
# TODO JEM : remove me since it is not used ?
|
||||
def auto_set_parent_id(self, cr, uid, ids, context=None):
|
||||
|
||||
# select partner who have one least invoice
|
||||
partner_treated = ['@gmail.com']
|
||||
cr.execute(""" SELECT p.id, p.email
|
||||
FROM res_partner as p
|
||||
LEFT JOIN account_invoice as a
|
||||
ON p.id = a.partner_id AND a.state in ('open','paid')
|
||||
WHERE p.grade_id is NOT NULL
|
||||
GROUP BY p.id
|
||||
ORDER BY COUNT(a.id) DESC
|
||||
""")
|
||||
re_email = re.compile(r".*@")
|
||||
for id, email in cr.fetchall():
|
||||
# check email domain
|
||||
email = re_email.sub("@", email or "")
|
||||
if not email or email in partner_treated:
|
||||
continue
|
||||
partner_treated.append(email)
|
||||
|
||||
# don't update the partners if they are more of one who have invoice
|
||||
cr.execute(""" SELECT *
|
||||
FROM res_partner as p
|
||||
WHERE p.id != %s AND p.email LIKE %s AND
|
||||
EXISTS (SELECT * FROM account_invoice as a WHERE p.id = a.partner_id AND a.state in ('open','paid'))
|
||||
""", (id, '%' + email))
|
||||
|
||||
if len(cr.fetchall()) > 1:
|
||||
_logger.info("%s MORE OF ONE COMPANY", email)
|
||||
continue
|
||||
|
||||
# to display changed values
|
||||
cr.execute(""" SELECT id,email
|
||||
FROM res_partner
|
||||
WHERE parent_id != %s AND id != %s AND email LIKE %s
|
||||
""", (id, id, '%' + email))
|
||||
_logger.info("%r", cr.fetchall())
|
||||
|
||||
# upgrade
|
||||
cr.execute(""" UPDATE res_partner
|
||||
SET parent_id = %s
|
||||
WHERE id != %s AND email LIKE %s
|
||||
""", (id, id, '%' + email))
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user