[FIX] crm,sale_crm: split action_mark_won

Since https://github.com/odoo/odoo/commit/4330ea02676a4afd88707bb463a007e7de3c8686, action_mark_won was
not api.multi anymore, which made it impossible to mark leads as won in
batch.
The problem is that we do exactly this in the demo data.

As we need to keep the rainbowman in the interface the decision has been
taken to split the method in two parts, one for the interface which
returns the rainbowman (and uses ensure_one) and the second which is a
true api.multi method.

Thanks to @tde-banana-odoo for his advice on the matter
This commit is contained in:
RomainLibert
2018-09-14 16:16:46 +02:00
parent 14a9b988f4
commit 5eae8f7110
3 changed files with 53 additions and 45 deletions
+51 -43
View File
@@ -363,52 +363,60 @@ class Lead(models.Model):
for lead in self:
stage_id = lead._stage_find(domain=[('probability', '=', 100.0), ('on_change', '=', True)])
lead.write({'stage_id': stage_id.id, 'probability': 100})
if lead.user_id and lead.team_id and lead.planned_revenue:
query = """
SELECT
SUM(CASE WHEN user_id = %(user_id)s THEN 1 ELSE 0 END) as total_won,
MAX(CASE WHEN date_closed >= CURRENT_DATE - INTERVAL '30 days' AND user_id = %(user_id)s THEN planned_revenue ELSE 0 END) as max_user_30,
MAX(CASE WHEN date_closed >= CURRENT_DATE - INTERVAL '7 days' AND user_id = %(user_id)s THEN planned_revenue ELSE 0 END) as max_user_7,
MAX(CASE WHEN date_closed >= CURRENT_DATE - INTERVAL '30 days' AND team_id = %(team_id)s THEN planned_revenue ELSE 0 END) as max_team_30,
MAX(CASE WHEN date_closed >= CURRENT_DATE - INTERVAL '7 days' AND team_id = %(team_id)s THEN planned_revenue ELSE 0 END) as max_team_7
FROM crm_lead
WHERE
type = 'opportunity'
AND
active = True
AND
probability = 100
AND
DATE_TRUNC('year', date_closed) = DATE_TRUNC('year', CURRENT_DATE)
AND
(user_id = %(user_id)s OR team_id = %(team_id)s)
"""
lead.env.cr.execute(query, {'user_id': lead.user_id.id,
'team_id': lead.team_id.id})
query_result = self.env.cr.dictfetchone()
message = False
if query_result['total_won'] == 1:
message = _('Go, go, go! Congrats for your first deal.')
elif query_result['max_team_30'] == lead.planned_revenue:
message = _('Boom! Team record for the past 30 days.')
elif query_result['max_team_7'] == lead.planned_revenue:
message = _('Yeah! Deal of the last 7 days for the team.')
elif query_result['max_user_30'] == lead.planned_revenue:
message = _('You just beat your personal record for the past 30 days.')
elif query_result['max_user_7'] == lead.planned_revenue:
message = _('You just beat your personal record for the past 7 days.')
return True
if message:
return {
'effect': {
'fadeout': 'slow',
'message': message,
'img_url': '/web/image/%s/%s/image' % (lead.team_id.user_id._name, lead.team_id.user_id.id) if lead.team_id.user_id.image else '/web/static/src/img/smile.svg',
'type': 'rainbow_man',
}
@api.multi
def action_set_won_rainbowman(self):
self.ensure_one()
self.action_set_won()
if self.user_id and self.team_id and self.planned_revenue:
query = """
SELECT
SUM(CASE WHEN user_id = %(user_id)s THEN 1 ELSE 0 END) as total_won,
MAX(CASE WHEN date_closed >= CURRENT_DATE - INTERVAL '30 days' AND user_id = %(user_id)s THEN planned_revenue ELSE 0 END) as max_user_30,
MAX(CASE WHEN date_closed >= CURRENT_DATE - INTERVAL '7 days' AND user_id = %(user_id)s THEN planned_revenue ELSE 0 END) as max_user_7,
MAX(CASE WHEN date_closed >= CURRENT_DATE - INTERVAL '30 days' AND team_id = %(team_id)s THEN planned_revenue ELSE 0 END) as max_team_30,
MAX(CASE WHEN date_closed >= CURRENT_DATE - INTERVAL '7 days' AND team_id = %(team_id)s THEN planned_revenue ELSE 0 END) as max_team_7
FROM crm_lead
WHERE
type = 'opportunity'
AND
active = True
AND
probability = 100
AND
DATE_TRUNC('year', date_closed) = DATE_TRUNC('year', CURRENT_DATE)
AND
(user_id = %(user_id)s OR team_id = %(team_id)s)
"""
self.env.cr.execute(query, {'user_id': self.user_id.id,
'team_id': self.team_id.id})
query_result = self.env.cr.dictfetchone()
message = False
if query_result['total_won'] == 1:
message = _('Go, go, go! Congrats for your first deal.')
elif query_result['max_team_30'] == self.planned_revenue:
message = _('Boom! Team record for the past 30 days.')
elif query_result['max_team_7'] == self.planned_revenue:
message = _('Yeah! Deal of the last 7 days for the team.')
elif query_result['max_user_30'] == self.planned_revenue:
message = _('You just beat your personal record for the past 30 days.')
elif query_result['max_user_7'] == self.planned_revenue:
message = _('You just beat your personal record for the past 7 days.')
if message:
return {
'effect': {
'fadeout': 'slow',
'message': message,
'img_url': '/web/image/%s/%s/image' % (self.team_id.user_id._name, self.team_id.user_id.id) if self.team_id.user_id.image else '/web/static/src/img/smile.svg',
'type': 'rainbow_man',
}
return True
}
return True
@api.multi
def action_schedule_meeting(self):
+1 -1
View File
@@ -430,7 +430,7 @@
<field name="arch" type="xml">
<form string="Opportunities" class="o_opportunity_form">
<header>
<button name="action_set_won" string="Mark Won"
<button name="action_set_won_rainbowman" string="Mark Won"
type="object" class="oe_highlight"
attrs="{'invisible': ['|', ('active','=',False), ('probability', '=', 100)]}"/>
<button name="%(crm.crm_lead_lost_action)d" string="Mark Lost"
+1 -1
View File
@@ -6,7 +6,7 @@
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
<field name="arch" type="xml">
<xpath expr="//button[@name='action_set_won']" position="before">
<xpath expr="//button[@name='action_set_won_rainbowman']" position="before">
<button string="New Quotation" name="%(sale_action_quotations_new)d" type="action" class="oe_highlight"
context="{'search_default_partner_id': partner_id,
'default_partner_id': partner_id,