11# Copyright 2016 ACSONE SA/NV
22# Copyright 2024 Tecnativa - Víctor Martínez
33# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
4- from odoo . tests import Form
4+ from odoo import Command
55
66from odoo .addons .base .tests .common import BaseCommon
77
@@ -51,12 +51,10 @@ def setUpClass(cls):
5151 "categ_id" : cls .category1 .id ,
5252 "standard_price" : 100 ,
5353 "attribute_line_ids" : [
54- (
55- 0 ,
56- 0 ,
54+ Command .create (
5755 {
5856 "attribute_id" : cls .attribute1 .id ,
59- "value_ids" : [( 6 , 0 , [cls .value1 .id , cls .value2 .id ])],
57+ "value_ids" : [Command . set ( [cls .value1 .id , cls .value2 .id ])],
6058 },
6159 )
6260 ],
@@ -112,62 +110,6 @@ def test_onchange_product_tmpl_id_01(self):
112110 f"{ self .product_template_no .name } \n { self .product_template_no .description_purchase } " ,
113111 )
114112
115- def test_onchange_product_tmpl_id_02 (self ):
116- order_form = Form (self .env ["purchase.order" ])
117- order_form .partner_id = self .supplier
118- with order_form .order_line .new () as line_form :
119- line_form .product_tmpl_id = self .product_template_yes
120- order = order_form .save ()
121- line = order .order_line
122- self .assertFalse (line .product_id )
123- self .assertIn ("Product template 1" , line .name )
124- self .assertIn ("Purchase Description" , line .name )
125- self .assertEqual (line .product_uom , self .product_template_yes .uom_id )
126- self .assertEqual (line .price_unit , 90 )
127- self .assertEqual (line .product_qty , 11 )
128- self .assertTrue (line .date_planned )
129- order .button_confirm ()
130- self .assertTrue (line .product_id )
131- self .assertIn ("Product template 1" , line .name )
132- self .assertIn ("Purchase Description" , line .name )
133-
134- def test_onchange_product_attribute_ids (self ):
135- product = self .product_product .create (
136- {
137- "name" : "Test product 01" ,
138- "product_tmpl_id" : self .product_template_yes .id ,
139- "product_attribute_ids" : [
140- (
141- 0 ,
142- 0 ,
143- {
144- "product_tmpl_id" : self .product_template_yes .id ,
145- "attribute_id" : self .attribute1 .id ,
146- "value_id" : self .value1 .id ,
147- },
148- )
149- ],
150- }
151- )
152- order_form = Form (self .env ["purchase.order" ])
153- order_form .partner_id = self .supplier
154- with order_form .order_line .new () as line_form :
155- line_form .product_tmpl_id = self .product_template_yes
156- with line_form .product_attribute_ids .edit (0 ) as pa_form :
157- pa_form .value_id = self .value1
158- order = order_form .save ()
159- line = order .order_line
160- self .assertEqual (line .product_id , product )
161- expected_domain = [
162- ("product_tmpl_id" , "=" , self .product_template_yes .id ),
163- (
164- "product_template_attribute_value_ids" ,
165- "=" ,
166- product .product_template_attribute_value_ids [0 ].id ,
167- ),
168- ]
169- self .assertEqual (line .product_id_configurator_domain , expected_domain )
170-
171113 def test_can_create_product_variant (self ):
172114 line = self .purchase_order_line .new (
173115 {
@@ -197,33 +139,6 @@ def test_can_create_product_variant(self):
197139 self .assertTrue (line .product_id )
198140 self .assertFalse (line .create_product_variant )
199141
200- def test_onchange_product_id (self ):
201- product = self .product_product .create (
202- {
203- "name" : "Test product 02" ,
204- "product_tmpl_id" : self .product_template_yes .id ,
205- "product_attribute_ids" : [
206- (
207- 0 ,
208- 0 ,
209- {
210- "product_tmpl_id" : self .product_template_yes .id ,
211- "attribute_id" : self .attribute1 .id ,
212- "value_id" : self .value1 .id ,
213- },
214- )
215- ],
216- }
217- )
218- order_form = Form (self .env ["purchase.order" ])
219- order_form .partner_id = self .supplier
220- with order_form .order_line .new () as line_form :
221- line_form .product_id = product
222- order = order_form .save ()
223- line = order .order_line
224- self .assertEqual (len (line .product_attribute_ids ), 1 )
225- self .assertEqual (line .product_tmpl_id , self .product_template_yes )
226-
227142 def test_button_confirm_01 (self ):
228143 order = self .purchase_order .create ({"partner_id" : self .supplier .id })
229144 line_1 = self .purchase_order_line .new (
@@ -235,9 +150,7 @@ def test_button_confirm_01(self):
235150 "date_planned" : "2016-01-01" ,
236151 "product_uom" : self .product_template_yes .uom_id .id ,
237152 "product_attribute_ids" : [
238- (
239- 0 ,
240- 0 ,
153+ Command .create (
241154 {
242155 "product_tmpl_id" : self .product_template_yes .id ,
243156 "attribute_id" : self .attribute1 .id ,
@@ -282,37 +195,48 @@ def test_button_confirm_01(self):
282195 "All purchase lines must have a product" ,
283196 )
284197
285- def test_button_confirm_02 (self ):
286- order_form = Form (self .env ["purchase.order" ])
287- order_form .partner_id = self .supplier
288- with order_form .order_line .new () as line_form :
289- line_form .product_tmpl_id = self .product_template_yes
290- with line_form .product_attribute_ids .edit (0 ) as pa_form :
291- pa_form .value_id = self .value1
292- order = order_form .save ()
293- line1 = order .order_line
294- self .assertFalse (line1 .product_id )
295- order .button_confirm ()
296- self .assertTrue (line1 .product_id )
297- purchase = Form (order )
298- with purchase .order_line .new () as line_form :
299- line_form .product_tmpl_id = self .product_template_yes
300- with line_form .product_attribute_ids .edit (0 ) as pa_form :
301- pa_form .value_id = self .value2
302- purchase .save ()
303- line2 = order .order_line - line1
304- self .assertTrue (line2 .product_id )
305- self .assertNotEqual (line1 .product_id , line2 .product_id )
198+ def test_compute_product_id_is_required (self ):
199+ # Set the company configuration to True
200+ self .env .company .po_confirm_create_variant = True
201+ line = self .purchase_order_line .new (
202+ {
203+ "product_tmpl_id" : self .product_template_yes .id ,
204+ "product_uom" : self .product_template_yes .uom_id .id ,
205+ "product_qty" : 5 ,
206+ "company_id" : self .env .company .id , # Explicitly set the company
207+ }
208+ )
209+ line ._compute_product_id_is_required ()
210+ self .assertFalse (
211+ line .product_id_is_required ,
212+ "Product ID should not be required when po_confirm_create_variant is True." ,
213+ )
214+
215+ # Set the company configuration to False
216+ self .env .company .po_confirm_create_variant = False
217+ line ._compute_product_id_is_required ()
218+ self .assertTrue (
219+ line .product_id_is_required ,
220+ "Product ID should be required when po_confirm_create_variant is False." ,
221+ )
222+
223+ def test_create_purchase_order_line_with_variant_creation (self ):
224+ # Create a purchase order in 'purchase' state
225+ order = self .purchase_order .create ({"partner_id" : self .supplier .id })
226+ order .button_confirm () # Move the order to 'purchase' state
306227
307- def test_copy (self ):
308- old_date = "2017-01-01"
309- order_form = Form (self .env ["purchase.order" ])
310- order_form .partner_id = self .supplier
311- with order_form .order_line .new () as line_form :
312- line_form .product_tmpl_id = self .product_template_yes
313- line_form .date_planned = old_date
314- with line_form .product_attribute_ids .edit (0 ) as pa_form :
315- pa_form .value_id = self .value1
316- order = order_form .save ()
317- new_order = order .copy ()
318- self .assertNotEqual (new_order .order_line .date_planned , old_date )
228+ # Create a line without a product but with a product template
229+ line_vals = {
230+ "order_id" : order .id ,
231+ "product_tmpl_id" : self .product_template_yes .id ,
232+ "product_uom" : self .product_template_yes .uom_id .id ,
233+ "product_qty" : 1 ,
234+ "name" : "Line with variant creation" ,
235+ "date_planned" : "2024-01-01" ,
236+ }
237+ line = self .purchase_order_line .create ([line_vals ])
238+
239+ # Ensure a product was created
240+ self .assertTrue (
241+ line .product_id , "Product variant should be created for the line."
242+ )
0 commit comments