| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import api, fields, models, _ |
| 5 |
from odoo.exceptions import UserError |
| 6 |
|
| 7 |
|
| 8 |
class CrmQuotationPartner(models.TransientModel): |
| 9 |
_name = 'crm.quotation.partner' |
| 10 |
_description = 'Create new or use existing Customer on new Quotation' |
| 11 |
|
| 12 |
@api.model |
| 13 |
def default_get(self, fields): |
| 14 |
result = super().default_get(fields) |
| 15 |
|
| 16 |
active_model = self.env.context.get('active_model') |
| 17 |
if active_model != 'crm.lead': |
| 18 |
raise UserError(_('You can only apply this action from a lead.')) |
| 19 |
|
| 20 |
lead = False |
| 21 |
if result.get('lead_id'): |
| 22 |
lead = self.env['crm.lead'].browse(result['lead_id']) |
| 23 |
elif 'lead_id' in fields and self.env.context.get('active_id'): |
| 24 |
lead = self.env['crm.lead'].browse(self.env.context['active_id']) |
| 25 |
if lead: |
| 26 |
result['lead_id'] = lead.id |
| 27 |
partner_id = result.get('partner_id') or lead._find_matching_partner().id |
| 28 |
if 'action' in fields and not result.get('action'): |
| 29 |
result['action'] = 'exist' if partner_id else 'create' |
| 30 |
if 'partner_id' in fields and not result.get('partner_id'): |
| 31 |
result['partner_id'] = partner_id |
| 32 |
|
| 33 |
return result |
| 34 |
|
| 35 |
action = fields.Selection([ |
| 36 |
('create', 'Create a new customer'), |
| 37 |
('exist', 'Link to an existing customer'), |
| 38 |
('nothing', 'Do not link to a customer') |
| 39 |
], string='Quotation Customer', required=True) |
| 40 |
lead_id = fields.Many2one('crm.lead', "Associated Lead", required=True) |
| 41 |
partner_id = fields.Many2one('res.partner', 'Customer') |
| 42 |
|
| 43 |
def action_apply(self): |
| 44 |
""" Convert lead to opportunity or merge lead and opportunity and open |
| 45 |
the freshly created opportunity view. |
| 46 |
""" |
| 47 |
self.ensure_one() |
| 48 |
if self.action == 'create': |
| 49 |
self.lead_id._handle_partner_assignment(create_missing=True) |
| 50 |
elif self.action == 'exist': |
| 51 |
self.lead_id._handle_partner_assignment(force_partner_id=self.partner_id.id, create_missing=False) |
| 52 |
return self.lead_id.action_new_quotation() |