33from collections import defaultdict
44
55from odoo import api , fields , models
6+ from odoo .fields import Domain
67
78
89class CrmLead (models .Model ):
@@ -37,20 +38,25 @@ class CrmLead(models.Model):
3738 )
3839
3940 def _get_lead_purchase_order_domain (self ):
40- return [ ("state" , "not in" , ("draft" , "sent" , "cancel" ))]
41+ return Domain ("state" , "not in" , ("draft" , "sent" , "cancel" ))
4142
4243 def _get_lead_request_for_quotation_domain (self ):
43- return [("state" , "in" , ("draft" , "sent" ))]
44+ return Domain ("state" , "in" , ("draft" , "sent" ))
45+
46+ def _get_purchase_order_lead_domain (self ):
47+ return Domain ("opportunity_id" , "in" , self .ids )
4448
4549 @api .depends ("purchase_order_ids.state" )
4650 def _compute_purchase_order_count (self ):
4751 purchase_order_per_lead = {
4852 lead .id : count
4953 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+ domain = Domain .AND (
55+ [
56+ self ._get_purchase_order_lead_domain (),
57+ self ._get_lead_purchase_order_domain (),
58+ ]
59+ ),
5460 groupby = ["opportunity_id" ],
5561 aggregates = ["__count" ],
5662 )
@@ -63,10 +69,12 @@ def _compute_request_for_quotation_count(self):
6369 rfq_per_lead = {
6470 lead .id : count
6571 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- ],
72+ domain = Domain .AND (
73+ [
74+ self ._get_purchase_order_lead_domain (),
75+ self ._get_lead_request_for_quotation_domain (),
76+ ]
77+ ),
7078 groupby = ["opportunity_id" ],
7179 aggregates = ["__count" ],
7280 )
@@ -84,17 +92,15 @@ def _compute_request_for_quotation_count(self):
8492 def _compute_purchase_amount_total (self ):
8593 amount_per_lead = defaultdict (float )
8694
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- ],
95+ for lead , currency , company , date_order , amount in self .env [
96+ "purchase.order"
97+ ]._read_group (
98+ domain = Domain .AND (
99+ [
100+ self ._get_purchase_order_lead_domain (),
101+ self ._get_lead_purchase_order_domain (),
102+ ]
103+ ),
98104 groupby = ["opportunity_id" , "currency_id" , "company_id" , "date_order:day" ],
99105 aggregates = ["amount_untaxed:sum" ],
100106 ):
@@ -109,12 +115,14 @@ def _compute_purchase_amount_total(self):
109115 for lead in self :
110116 lead .purchase_amount_total = amount_per_lead .get (lead .id , 0.0 )
111117
112- def _create_customer (self ):
118+ def _create_customer (self , with_parent = None ):
113119 """It can be a customer or supplier depending on lead request type"""
120+ self .ensure_one ()
114121 self = self .with_context (res_partner_search_mode = self .request_type )
115- return super ()._create_customer ()
122+ return super ()._create_customer (with_parent = with_parent )
116123
117124 def action_lead_rfq_new (self ):
125+ self .ensure_one ()
118126 if not self .partner_id :
119127 return self .env ["ir.actions.actions" ]._for_xml_id (
120128 "srm.srm_rfq_partner_action"
@@ -123,6 +131,7 @@ def action_lead_rfq_new(self):
123131 return self .action_rfq_new ()
124132
125133 def action_rfq_new (self ):
134+ self .ensure_one ()
126135 action = self .env ["ir.actions.actions" ]._for_xml_id ("srm.action_lead_rfq_new" )
127136 action ["context" ] = self ._prepare_rfq_context ()
128137 return action
0 commit comments