Skip to content

Commit dba3a0b

Browse files
JulioFabioCristianoMafraJunior
authored andcommitted
[MIG] fleet_vehicle_stock: Migration to 18.0
1 parent 4fbc40a commit dba3a0b

6 files changed

Lines changed: 217 additions & 16 deletions

File tree

fleet_vehicle_stock/__manifest__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
"summary": """
88
This module is an add-on for the Fleet application in Odoo. It allows
99
you to track your Fleet Vehicles in stock moves.""",
10-
"version": "16.0.1.0.0",
10+
"version": "18.0.1.0.0",
1111
"license": "AGPL-3",
1212
"author": "Escodoo,Odoo Community Association (OCA)",
1313
"website": "https://github.com/OCA/fleet",
1414
"category": "Human Resources/Fleet",
15-
"images": ["static/description/banner.png"],
1615
"maintainers": ["marcelsavegnago"],
1716
"depends": ["stock", "fleet"],
1817
"data": [

fleet_vehicle_stock/models/fleet_vehicle.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ def _onchange_product(self):
4646
rec.lot_id = False
4747
rec.current_stock_location_id = False
4848

49-
@api.model
50-
def create(self, vals):
51-
res = super().create(vals)
52-
if res.lot_id:
53-
if "lot_id" in vals:
54-
res.lot_id.fleet_vehicle_id = res.id
55-
return res
49+
@api.model_create_multi
50+
def create(self, vals_list):
51+
records = super().create(vals_list)
52+
for record, vals in zip(records, vals_list, strict=False):
53+
if record.lot_id and "lot_id" in vals:
54+
record.lot_id.fleet_vehicle_id = record.id
55+
return records
5656

5757
def write(self, vals):
5858
for rec in self:

fleet_vehicle_stock/models/stock_move.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _action_done(self, cancel_backorder=False):
2626
):
2727
raise UserError(
2828
_(
29-
"The product '%s' is configure to create a fleet "
29+
"The product '%s' is configured to create a fleet "
3030
"vehicle but vehicle model is not configured in the "
3131
"product."
3232
)

fleet_vehicle_stock/tests/test_fleet_vehicle.py

Lines changed: 200 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
45
from 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()

fleet_vehicle_stock/views/product_product.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
<group>
1414
<field
1515
name="fleet_vehicle_model_id"
16-
attrs="{'invisible': [('tracking', '!=', 'serial')], 'required': [('create_fleet_vehicle', '=', True)]}"
16+
invisible="tracking != 'serial'"
17+
required="create_fleet_vehicle == True"
1718
/>
1819
</group>
1920
</page>
@@ -37,7 +38,8 @@
3738
>
3839
<field
3940
name="fleet_vehicle_model_id"
40-
attrs="{'invisible': [('tracking', '!=', 'serial')], 'required': [('create_fleet_vehicle', '=', True)]}"
41+
invisible="tracking != 'serial'"
42+
required="create_fleet_vehicle == True"
4143
/>
4244
</group>
4345
</xpath>

fleet_vehicle_stock/views/product_template.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
<field name="tracking" position="after">
1111
<field
1212
name="create_fleet_vehicle"
13-
attrs="{'invisible': [('tracking', '!=', 'serial')]}"
13+
invisible="tracking != 'serial'"
14+
string="create fleet vehicle"
1415
/>
1516
</field>
1617
<xpath expr="//notebook" position="inside">
@@ -30,7 +31,8 @@
3031
<group>
3132
<field
3233
name="fleet_vehicle_model_id"
33-
attrs="{'invisible': [('product_variant_count', '>', 1)], 'required': [('product_variant_count', '=', 1)]}"
34+
invisible="product_variant_count &gt; 1"
35+
required="product_variant_count == 1"
3436
/>
3537
</group>
3638
</page>

0 commit comments

Comments
 (0)