diff --git a/sale_crm_usability/__openerp__.py b/sale_crm_usability/__openerp__.py index 92c053d..8718b4e 100644 --- a/sale_crm_usability/__openerp__.py +++ b/sale_crm_usability/__openerp__.py @@ -18,6 +18,8 @@ This module adds a One2many link from opportunities to sale orders. When a sale order linked to an opportunity is confirmed, the opportunity is automatically moved to the *Won* step. +When you click on the button *Mark as lost* on an opportunity, the related quotations (*draft* or *sent* state) are automatically cancelled. + This module has been written by Alexis de Lattre from Akretion . """, diff --git a/sale_crm_usability/sale_crm.py b/sale_crm_usability/sale_crm.py index 91d1f7c..8c6a67a 100644 --- a/sale_crm_usability/sale_crm.py +++ b/sale_crm_usability/sale_crm.py @@ -3,7 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # @author Alexis de Lattre -from openerp import models, fields, api, _ +from openerp import models, fields, api, _, workflow from openerp.exceptions import Warning as UserError @@ -59,3 +59,31 @@ class CrmLead(models.Model): else: raise UserError(_( 'There are no quotations linked to this opportunity')) + + @api.model + def create(self, vals): + if vals is None: + vals = {} + if self._context.get('usability_default_stage_xmlid'): + stage = self.env.ref(self._context['usability_default_stage_xmlid']) + vals['stage_id'] = stage.id + return super(CrmLead, self).create(vals) + + @api.multi + def case_mark_lost(self): + """When opportunity is marked as lost, cancel the related quotations + I don't inherit the write but the button, because it leaves a waty to + mask lead as lost and not cancel the quotations + """ + res = super(CrmLead, self).case_mark_lost() + sales = self.env['sale.order'].search([ + ('lead_id', 'in', self.ids), + ('state', 'in', ('draft', 'sent'))]) + for so in sales: + workflow.trg_validate( + self._uid, 'sale.order', so.id, 'cancel', self._cr) + so.message_post(_( + 'The related opportunity has been marked as lost, ' + 'therefore this quotation has been automatically cancelled.')) + return res + diff --git a/sale_crm_usability/sale_crm_view.xml b/sale_crm_usability/sale_crm_view.xml index 85009e0..9398d8d 100644 --- a/sale_crm_usability/sale_crm_view.xml +++ b/sale_crm_usability/sale_crm_view.xml @@ -16,7 +16,7 @@ + context="{'default_type': 'opportunity', 'default_partner_id': partner_id, 'default_user_id': uid, 'stage_type': 'opportunity', 'usability_default_stage_xmlid': 'crm.stage_lead4'}"/>