From 5d1df0b654f97fe4c662558648b66a0b0c6d7363 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 4 Dec 2017 16:10:23 +0100 Subject: [PATCH] Port sale_crm_usability to v10 --- sale_crm_usability/__manifest__.py | 11 ++- sale_crm_usability/sale_crm.py | 95 ++++++------------- sale_crm_usability/sale_crm_view.xml | 37 ++------ sale_crm_usability/wizard/__init__.py | 2 +- sale_crm_usability/wizard/crm_lead_lost.py | 29 ++++++ .../wizard/crm_lead_lost_view.xml | 22 +++++ sale_crm_usability/wizard/crm_make_sale.py | 22 ----- 7 files changed, 96 insertions(+), 122 deletions(-) create mode 100644 sale_crm_usability/wizard/crm_lead_lost.py create mode 100644 sale_crm_usability/wizard/crm_lead_lost_view.xml delete mode 100644 sale_crm_usability/wizard/crm_make_sale.py diff --git a/sale_crm_usability/__manifest__.py b/sale_crm_usability/__manifest__.py index e5dfce9..46c5c1f 100644 --- a/sale_crm_usability/__manifest__.py +++ b/sale_crm_usability/__manifest__.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- -# © 2016 Akretion (http://www.akretion.com) +# © 2016-2017 Akretion (http://www.akretion.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # @author Alexis de Lattre { 'name': 'Sale CRM Usability', - 'version': '8.0.1.0.0', + 'version': '10.0.1.0.0', 'category': 'Customer Relationship Management', 'license': 'AGPL-3', 'summary': 'Link between opportunities and sale orders', @@ -26,6 +26,9 @@ This module has been written by Alexis de Lattre from Akretion 'author': 'Akretion', 'website': 'http://www.akretion.com', 'depends': ['sale_crm'], - 'data': ['sale_crm_view.xml'], - 'installable': False, + 'data': [ + 'sale_crm_view.xml', + 'wizard/crm_lead_lost_view.xml', + ], + 'installable': True, } diff --git a/sale_crm_usability/sale_crm.py b/sale_crm_usability/sale_crm.py index 8c6a67a..26b946a 100644 --- a/sale_crm_usability/sale_crm.py +++ b/sale_crm_usability/sale_crm.py @@ -3,87 +3,50 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # @author Alexis de Lattre -from openerp import models, fields, api, _, workflow -from openerp.exceptions import Warning as UserError +from odoo import models, api, _ class SaleOrder(models.Model): _inherit = 'sale.order' - lead_id = fields.Many2one( - 'crm.lead', string='Opportunity') - @api.multi - def action_button_confirm(self): - res = super(SaleOrder, self).action_button_confirm() - won_stage = self.env.ref('crm.stage_lead6') - for order in self: - if order.lead_id: - order.lead_id.stage_id = won_stage - order.lead_id.message_post(_( - "Stage automatically updated to Won upon " - "confirmation of the quotation %s" % order.name)) + def action_confirm(self): + res = super(SaleOrder, self).action_confirm() + won_stages = self.env['crm.stage'].search( + [('probability', '=', 100)]) + if won_stages: + won_stage = won_stages[0] + for order in self: + if order.opportunity_id: + order.opportunity_id.stage_id = won_stage + order.opportunity_id.message_post(_( + "Stage automatically updated to %s upon " + "confirmation of the quotation %s") + % (won_stage.name, order.name)) return res class CrmLead(models.Model): _inherit = 'crm.lead' - sale_ids = fields.One2many( - 'sale.order', 'lead_id', string='Quotations', readonly=True) - - @api.multi - def view_sale_orders(self): - self.ensure_one() - if self.sale_ids: - action = { - 'name': _('Quotations'), - 'type': 'ir.actions.act_window', - 'res_model': 'sale.order', - 'target': 'current', - 'context': - "{'default_partner_id': %s, 'default_lead_id': %s}" % ( - self.partner_id.id or False, self[0].id), - } - if len(self.sale_ids) == 1: - action.update({ - 'view_mode': 'form,tree,calendar,graph', - 'res_id': self.sale_ids[0].id, - }) - else: - action.update({ - 'view_mode': 'tree,form,calendar,graph', - 'domain': "[('id', 'in', %s)]" % self.sale_ids.ids, - }) - return action - else: - raise UserError(_( - 'There are no quotations linked to this opportunity')) + @api.model + def opportunity_from_quote_get_stage(self): + '''Designed to be inherited''' + res = False + stages = self.env['crm.stage'].search([ + ('probability', '<', 100), + ('probability', '>', 0), + ], order='sequence desc', limit=1) + if stages: + res = stages + return res @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 + if self._context.get('opportunity_from_quote'): + stage = self.opportunity_from_quote_get_stage() + if stage: + 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 9398d8d..1111345 100644 --- a/sale_crm_usability/sale_crm_view.xml +++ b/sale_crm_usability/sale_crm_view.xml @@ -5,41 +5,20 @@ The licence is in the file __openerp__.py --> - - + - + + sale_crm_usability.sale.order.form sale.order - + - - + + [('type', '=', 'opportunity'), ('partner_id', '=', partner_id)] + {'default_type': 'opportunity', 'default_partner_id': partner_id, 'default_user_id': uid, 'opportunity_from_quote': True} - - sale_crm_usability.opportunity.form - crm.lead - - 100 - - - -