1- # Copyright 2022 Telmo Santos <telmo.santos@camptocamp.com>
1+ # Copyright 2022 Camptocamp SA
22# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
3+ from collections import defaultdict
34
45from odoo import api , fields , models
56
67
78class CrmLead (models .Model ):
89 _inherit = "crm.lead"
910
11+ user_id = fields .Many2one (string = "Responsible" )
12+ team_id = fields .Many2one (string = "Team" )
13+
1014 request_type = fields .Selection (
11- [
15+ selection = [
1216 ("customer" , "Customer Lead" ),
1317 ("supplier" , "Supplier Lead" ),
1418 ],
1519 )
1620 purchase_amount_total = fields .Monetary (
17- compute = "_compute_purchase_data " ,
18- string = "Sum of Orders" ,
19- help = "Untaxed Total of Confirmed Orders" ,
21+ compute = "_compute_purchase_amount_total " ,
22+ string = "Sum of Purchase Orders" ,
23+ help = "Untaxed Total of Confirmed Purchase Orders" ,
2024 currency_field = "company_currency" ,
2125 )
2226 request_for_quotation_count = fields .Integer (
23- compute = "_compute_purchase_data" , string = "Number of Quotations"
27+ compute = "_compute_request_for_quotation_count" ,
28+ string = "Number of Request for Quotations" ,
2429 )
2530 purchase_order_count = fields .Integer (
26- compute = "_compute_purchase_data " , string = "Number of Purchase Orders"
31+ compute = "_compute_purchase_order_count " , string = "Number of Purchase Orders"
2732 )
2833 purchase_order_ids = fields .One2many (
29- "purchase.order" , "opportunity_id" , string = "Purchase Orders"
34+ comodel_name = "purchase.order" ,
35+ inverse_name = "opportunity_id" ,
36+ string = "Purchase Orders" ,
3037 )
3138
39+ def _get_lead_purchase_order_domain (self ):
40+ return [("state" , "not in" , ("draft" , "sent" , "cancel" ))]
41+
42+ def _get_lead_request_for_quotation_domain (self ):
43+ return [("state" , "in" , ("draft" , "sent" ))]
44+
45+ @api .depends ("purchase_order_ids.state" )
46+ def _compute_purchase_order_count (self ):
47+ purchase_order_per_lead = {
48+ lead .id : count
49+ for lead , count in self .env ["purchase.order" ]._read_group (
50+ domain = [
51+ ("opportunity_id" , "in" , self .ids ),
52+ * self ._get_lead_purchase_order_domain (),
53+ ],
54+ groupby = ["opportunity_id" ],
55+ aggregates = ["__count" ],
56+ )
57+ }
58+ for lead in self :
59+ lead .purchase_order_count = purchase_order_per_lead .get (lead .id , 0 )
60+
61+ @api .depends ("purchase_order_ids.state" )
62+ def _compute_request_for_quotation_count (self ):
63+ rfq_per_lead = {
64+ lead .id : count
65+ for lead , count in self .env ["purchase.order" ]._read_group (
66+ domain = [
67+ ("opportunity_id" , "in" , self .ids ),
68+ * self ._get_lead_request_for_quotation_domain (),
69+ ],
70+ groupby = ["opportunity_id" ],
71+ aggregates = ["__count" ],
72+ )
73+ }
74+ for lead in self :
75+ lead .request_for_quotation_count = rfq_per_lead .get (lead .id , 0 )
76+
3277 @api .depends (
33- "order_ids .state" ,
34- "order_ids .currency_id" ,
35- "order_ids .amount_untaxed" ,
36- "order_ids .date_order" ,
37- "order_ids .company_id" ,
78+ "purchase_order_ids .state" ,
79+ "purchase_order_ids .currency_id" ,
80+ "purchase_order_ids .amount_untaxed" ,
81+ "purchase_order_ids .date_order" ,
82+ "purchase_order_ids .company_id" ,
3883 )
39- def _compute_purchase_data (self ):
40- for lead in self :
41- total = 0.0
42- rfq_cnt = 0
43- purchase_order_cnt = 0
84+ def _compute_purchase_amount_total (self ):
85+ amount_per_lead = defaultdict (float )
86+
87+ for (
88+ lead ,
89+ currency ,
90+ company ,
91+ date_order ,
92+ amount ,
93+ ) in self .env ["purchase.order" ]._read_group (
94+ domain = [
95+ ("opportunity_id" , "in" , self .ids ),
96+ * self ._get_lead_purchase_order_domain (),
97+ ],
98+ groupby = ["opportunity_id" , "currency_id" , "company_id" , "date_order:day" ],
99+ aggregates = ["amount_untaxed:sum" ],
100+ ):
44101 company_currency = lead .company_currency or self .env .company .currency_id
45- for order in lead .purchase_order_ids :
46- if order .state in ("draft" , "sent" ):
47- rfq_cnt += 1
48- if order .state not in ("draft" , "sent" , "cancel" ):
49- purchase_order_cnt += 1
50- total += order .currency_id ._convert (
51- order .amount_untaxed ,
52- company_currency ,
53- order .company_id ,
54- order .date_order or fields .Date .today (),
55- )
56- lead .purchase_amount_total = total
57- lead .request_for_quotation_count = rfq_cnt
58- lead .purchase_order_count = purchase_order_cnt
102+ amount_per_lead [lead .id ] += currency ._convert (
103+ amount ,
104+ company_currency ,
105+ company ,
106+ date_order or fields .Date .context_today (self ),
107+ )
108+
109+ for lead in self :
110+ lead .purchase_amount_total = amount_per_lead .get (lead .id , 0.0 )
59111
60112 def _create_customer (self ):
61113 """It can be a customer or supplier depending on lead request type"""
@@ -72,10 +124,15 @@ def action_lead_rfq_new(self):
72124
73125 def action_rfq_new (self ):
74126 action = self .env ["ir.actions.actions" ]._for_xml_id ("srm.action_lead_rfq_new" )
75- action ["context" ] = {
127+ action ["context" ] = self ._prepare_rfq_context ()
128+ return action
129+
130+ def _prepare_rfq_context (self ):
131+ self .ensure_one ()
132+ rfq_context = {
76133 "default_partner_id" : self .partner_id .id ,
77134 "default_opportunity_id" : self .id ,
78135 }
79136 if self .user_id :
80- action [ "context" ] ["default_user_id" ] = self .user_id .id
81- return action
137+ rfq_context ["default_user_id" ] = self .user_id .id
138+ return rfq_context
0 commit comments