| 1 |
|
| 2 |
|
| 3 |
from odoo import api, fields, models, _ |
| 4 |
from odoo.fields import Domain |
| 5 |
|
| 6 |
|
| 7 |
class CrmLead(models.Model): |
| 8 |
_inherit = 'crm.lead' |
| 9 |
|
| 10 |
sale_amount_total = fields.Monetary(compute='_compute_sale_data', string="Sum of Orders", help="Untaxed Total of Confirmed Orders", currency_field='company_currency') |
| 11 |
quotation_count = fields.Integer(compute='_compute_sale_data', string="Number of Quotations") |
| 12 |
sale_order_count = fields.Integer(compute='_compute_sale_data', string="Number of Sale Orders") |
| 13 |
order_ids = fields.One2many('sale.order', 'opportunity_id', string='Orders') |
| 14 |
|
| 15 |
@api.depends('order_ids.state', 'order_ids.currency_id', 'order_ids.amount_untaxed', 'order_ids.date_order', 'order_ids.company_id') |
| 16 |
def _compute_sale_data(self): |
| 17 |
for lead in self: |
| 18 |
company_currency = lead.company_currency or self.env.company.currency_id |
| 19 |
sale_orders = lead.order_ids.filtered_domain(self._get_lead_sale_order_domain()) |
| 20 |
lead.sale_amount_total = sum( |
| 21 |
order.currency_id._convert( |
| 22 |
order.amount_untaxed, company_currency, order.company_id, order.date_order or fields.Date.today() |
| 23 |
) |
| 24 |
for order in sale_orders |
| 25 |
) |
| 26 |
lead.quotation_count = len(lead.order_ids.filtered_domain(self._get_lead_quotation_domain())) |
| 27 |
lead.sale_order_count = len(sale_orders) |
| 28 |
|
| 29 |
def action_sale_quotations_new(self): |
| 30 |
if not self.partner_id: |
| 31 |
return self.env["ir.actions.actions"]._for_xml_id("sale_crm.crm_quotation_partner_action") |
| 32 |
else: |
| 33 |
return self.action_new_quotation() |
| 34 |
|
| 35 |
def action_new_quotation(self): |
| 36 |
action = self.env["ir.actions.actions"]._for_xml_id("sale_crm.sale_action_quotations_new") |
| 37 |
action['context'] = self._prepare_opportunity_quotation_context() |
| 38 |
action['context']['search_default_opportunity_id'] = self.id |
| 39 |
return action |
| 40 |
|
| 41 |
def action_view_sale_quotation(self): |
| 42 |
self.ensure_one() |
| 43 |
action = self.env["ir.actions.actions"]._for_xml_id("sale.action_quotations_with_onboarding") |
| 44 |
action['context'] = self._prepare_opportunity_quotation_context() |
| 45 |
action['context']['search_default_draft'] = 1 |
| 46 |
action['domain'] = Domain.AND([[('opportunity_id', '=', self.id)], self._get_action_view_sale_quotation_domain()]) |
| 47 |
quotations = self.order_ids.filtered_domain(self._get_action_view_sale_quotation_domain()) |
| 48 |
if len(quotations) == 1: |
| 49 |
action['views'] = [(self.env.ref('sale.view_order_form').id, 'form')] |
| 50 |
action['res_id'] = quotations.id |
| 51 |
return action |
| 52 |
|
| 53 |
def action_view_sale_order(self): |
| 54 |
self.ensure_one() |
| 55 |
action = self.env["ir.actions.actions"]._for_xml_id("sale.action_orders") |
| 56 |
action['context'] = { |
| 57 |
'search_default_partner_id': self.partner_id.id, |
| 58 |
'default_partner_id': self.partner_id.id, |
| 59 |
'default_opportunity_id': self.id, |
| 60 |
} |
| 61 |
action['domain'] = Domain.AND([[('opportunity_id', '=', self.id)], self._get_lead_sale_order_domain()]) |
| 62 |
orders = self.order_ids.filtered_domain(self._get_lead_sale_order_domain()) |
| 63 |
if len(orders) == 1: |
| 64 |
action['views'] = [(self.env.ref('sale.view_order_form').id, 'form')] |
| 65 |
action['res_id'] = orders.id |
| 66 |
return action |
| 67 |
|
| 68 |
def _get_action_view_sale_quotation_domain(self): |
| 69 |
return [('state', 'in', ('draft', 'sent', 'cancel'))] |
| 70 |
|
| 71 |
def _get_lead_quotation_domain(self): |
| 72 |
return [('state', 'in', ('draft', 'sent'))] |
| 73 |
|
| 74 |
def _get_lead_sale_order_domain(self): |
| 75 |
return [('state', 'not in', ('draft', 'sent', 'cancel'))] |
| 76 |
|
| 77 |
def _prepare_opportunity_quotation_context(self): |
| 78 |
""" Prepares the context for a new quotation (sale.order) by sharing the values of common fields """ |
| 79 |
self.ensure_one() |
| 80 |
quotation_context = { |
| 81 |
'default_opportunity_id': self.id, |
| 82 |
'default_partner_id': self.partner_id.id, |
| 83 |
'default_campaign_id': self.campaign_id.id, |
| 84 |
'default_medium_id': self.medium_id.id, |
| 85 |
'default_origin': self.name, |
| 86 |
'default_source_id': self.source_id.id, |
| 87 |
'default_company_id': self.company_id.id or self.env.company.id, |
| 88 |
'default_tag_ids': [(6, 0, self.tag_ids.ids)] |
| 89 |
} |
| 90 |
if self.team_id: |
| 91 |
quotation_context['default_team_id'] = self.team_id.id |
| 92 |
if self.user_id: |
| 93 |
quotation_context['default_user_id'] = self.user_id.id |
| 94 |
return quotation_context |
| 95 |
|
| 96 |
def _merge_get_fields_specific(self): |
| 97 |
fields_info = super(CrmLead, self)._merge_get_fields_specific() |
| 98 |
|
| 99 |
fields_info['order_ids'] = lambda fname, leads: [(4, order.id) for order in leads.order_ids] |
| 100 |
return fields_info |
| 101 |
|
| 102 |
def _update_revenues_from_so(self, order): |
| 103 |
for opportunity in self: |
| 104 |
if ( |
| 105 |
(opportunity.expected_revenue or 0) < order.amount_untaxed |
| 106 |
and order.currency_id == opportunity.company_id.currency_id |
| 107 |
): |
| 108 |
opportunity.expected_revenue = order.amount_untaxed |
| 109 |
opportunity._track_set_log_message(_('Expected revenue has been updated based on the linked Sales Orders.')) |