11# Copyright (C) 2021 - TODAY, Marcel Savegnago <marcel.savegnago@escodoo.com.br>
22# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33
4+ from odoo .exceptions import UserError
45from odoo .tests .common import TransactionCase
56
67
@@ -21,15 +22,17 @@ def setUpClass(cls):
2122 cls .product1 = cls .env ["product.product" ].create (
2223 {
2324 "name" : "Product 1" ,
24- "type" : "product" ,
25+ "is_storable" : True ,
26+ "type" : "consu" ,
2527 "fleet_vehicle_model_id" : cls .vehicle_model1 .id ,
2628 "tracking" : "serial" ,
2729 }
2830 )
2931 cls .product2 = cls .env ["product.product" ].create (
3032 {
3133 "name" : "Product 2" ,
32- "type" : "product" ,
34+ "is_storable" : True ,
35+ "type" : "consu" ,
3336 "fleet_vehicle_model_id" : cls .vehicle_model1 .id ,
3437 "tracking" : "serial" ,
3538 }
@@ -89,8 +92,203 @@ def test_compute_current_stock_loc_id(self):
8992
9093 def test_inverse_fleet_vehicle_model_id (self ):
9194 product2 = self .product2
95+ self .assertEqual (len (product2 .product_variant_ids ), 1 )
96+
9297 product2 .fleet_vehicle_model_id = self .vehicle_model2
9398 self .assertEqual (
9499 product2 .product_variant_ids .fleet_vehicle_model_id ,
95100 product2 .fleet_vehicle_model_id ,
96101 )
102+
103+ def test_create_product_template_with_model (self ):
104+ product_tmpl = self .env ["product.template" ].create (
105+ {
106+ "name" : "Product Template With Model" ,
107+ "fleet_vehicle_model_id" : self .vehicle_model1 .id ,
108+ "tracking" : "serial" ,
109+ }
110+ )
111+ self .assertEqual (product_tmpl .fleet_vehicle_model_id , self .vehicle_model1 )
112+ self .assertEqual (len (product_tmpl .product_variant_ids ), 1 )
113+ self .assertEqual (
114+ product_tmpl .product_variant_ids .fleet_vehicle_model_id , self .vehicle_model1
115+ )
116+
117+ def test_stock_move_create_vehicle (self ):
118+ picking_type = self .env ["stock.picking.type" ].create (
119+ {
120+ "name" : "Test Picking Type" ,
121+ "code" : "incoming" ,
122+ "sequence_code" : "TPT" ,
123+ "create_fleet_vehicle" : True ,
124+ }
125+ )
126+ self .product1 .product_tmpl_id .create_fleet_vehicle = True
127+
128+ move = self .env ["stock.move" ].create (
129+ {
130+ "name" : "Test Move" ,
131+ "product_id" : self .product1 .id ,
132+ "product_uom_qty" : 1.0 ,
133+ "product_uom" : self .product1 .uom_id .id ,
134+ "location_id" : self .env .ref ("stock.stock_location_suppliers" ).id ,
135+ "location_dest_id" : self .stock_location .id ,
136+ "picking_type_id" : picking_type .id ,
137+ }
138+ )
139+ move ._action_confirm ()
140+
141+ lot = self .env ["stock.lot" ].create (
142+ {
143+ "name" : "new_serial" ,
144+ "product_id" : self .product1 .id ,
145+ "company_id" : self .env .user .company_id .id ,
146+ }
147+ )
148+
149+ move .move_line_ids .write ({"quantity" : 1.0 , "lot_id" : lot .id })
150+
151+ move .picked = True
152+ move ._action_done ()
153+
154+ self .assertTrue (lot .fleet_vehicle_id )
155+ self .assertEqual (lot .fleet_vehicle_id .model_id , self .vehicle_model1 )
156+ self .assertEqual (lot .fleet_vehicle_id .product_id , self .product1 )
157+ self .assertEqual (
158+ lot .fleet_vehicle_id .current_stock_location_id , self .stock_location
159+ )
160+
161+ def test_stock_move_skip_vehicle_creation (self ):
162+ picking_type_no_create = self .env ["stock.picking.type" ].create (
163+ {
164+ "name" : "Test Picking Type No Create" ,
165+ "code" : "incoming" ,
166+ "sequence_code" : "TPTNC" ,
167+ "create_fleet_vehicle" : False ,
168+ }
169+ )
170+ self .product1 .product_tmpl_id .create_fleet_vehicle = True
171+
172+ move1 = self .env ["stock.move" ].create (
173+ {
174+ "name" : "Test Move Skip 1" ,
175+ "product_id" : self .product1 .id ,
176+ "product_uom_qty" : 1.0 ,
177+ "product_uom" : self .product1 .uom_id .id ,
178+ "location_id" : self .env .ref ("stock.stock_location_suppliers" ).id ,
179+ "location_dest_id" : self .stock_location .id ,
180+ "picking_type_id" : picking_type_no_create .id ,
181+ }
182+ )
183+ move1 ._action_confirm ()
184+ lot1 = self .env ["stock.lot" ].create (
185+ {
186+ "name" : "serial_skip_1" ,
187+ "product_id" : self .product1 .id ,
188+ "company_id" : self .env .user .company_id .id ,
189+ }
190+ )
191+ move1 .move_line_ids .write ({"quantity" : 1.0 , "lot_id" : lot1 .id })
192+ move1 .picked = True
193+ move1 ._action_done ()
194+ self .assertFalse (
195+ lot1 .fleet_vehicle_id ,
196+ "Vehicle should not be created if picking type disabled" ,
197+ )
198+
199+ picking_type_create = self .env ["stock.picking.type" ].create (
200+ {
201+ "name" : "Test Picking Type Create" ,
202+ "code" : "incoming" ,
203+ "sequence_code" : "TPTC" ,
204+ "create_fleet_vehicle" : True ,
205+ }
206+ )
207+ self .product1 .product_tmpl_id .create_fleet_vehicle = False
208+
209+ move2 = self .env ["stock.move" ].create (
210+ {
211+ "name" : "Test Move Skip 2" ,
212+ "product_id" : self .product1 .id ,
213+ "product_uom_qty" : 1.0 ,
214+ "product_uom" : self .product1 .uom_id .id ,
215+ "location_id" : self .env .ref ("stock.stock_location_suppliers" ).id ,
216+ "location_dest_id" : self .stock_location .id ,
217+ "picking_type_id" : picking_type_create .id ,
218+ }
219+ )
220+ move2 ._action_confirm ()
221+ lot2 = self .env ["stock.lot" ].create (
222+ {
223+ "name" : "serial_skip_2" ,
224+ "product_id" : self .product1 .id ,
225+ "company_id" : self .env .user .company_id .id ,
226+ }
227+ )
228+ move2 .move_line_ids .write ({"quantity" : 1.0 , "lot_id" : lot2 .id })
229+ move2 .picked = True
230+ move2 ._action_done ()
231+ self .assertFalse (
232+ lot2 .fleet_vehicle_id ,
233+ "Vehicle should not be created if product template disabled" ,
234+ )
235+
236+ def test_create_fleet_vehicle_updates_lot (self ):
237+ lot = self .env ["stock.lot" ].create (
238+ {
239+ "name" : "serial_vehicle_create" ,
240+ "product_id" : self .product1 .id ,
241+ "company_id" : self .env .user .company_id .id ,
242+ }
243+ )
244+
245+ vehicle = self .env ["fleet.vehicle" ].create (
246+ {
247+ "model_id" : self .vehicle_model1 .id ,
248+ "product_id" : self .product1 .id ,
249+ "lot_id" : lot .id ,
250+ }
251+ )
252+
253+ self .assertEqual (
254+ lot .fleet_vehicle_id , vehicle , "Lot should be linked to the created vehicle"
255+ )
256+
257+ def test_create_fleet_vehicle_no_lot (self ):
258+ vehicle1 = self .env ["fleet.vehicle" ].create (
259+ {
260+ "model_id" : self .vehicle_model1 .id ,
261+ "product_id" : self .product1 .id ,
262+ }
263+ )
264+ self .assertFalse (vehicle1 .lot_id , "Vehicle should have no lot" )
265+ vehicle2 = self .env ["fleet.vehicle" ].create (
266+ {
267+ "model_id" : self .vehicle_model1 .id ,
268+ "product_id" : self .product1 .id ,
269+ "lot_id" : False ,
270+ }
271+ )
272+ self .assertFalse (vehicle2 .lot_id , "Vehicle should have no lot" )
273+
274+ def test_stock_move_error (self ):
275+ product_no_model = self .env ["product.product" ].create (
276+ {
277+ "name" : "Product No Model" ,
278+ "is_storable" : True ,
279+ "create_fleet_vehicle" : True ,
280+ }
281+ )
282+
283+ move = self .env ["stock.move" ].create (
284+ {
285+ "name" : "Test Move Error" ,
286+ "product_id" : product_no_model .id ,
287+ "product_uom_qty" : 1.0 ,
288+ "location_id" : self .env .ref ("stock.stock_location_suppliers" ).id ,
289+ "location_dest_id" : self .stock_location .id ,
290+ }
291+ )
292+
293+ with self .assertRaises (UserError ):
294+ move ._action_done ()
0 commit comments