forked from OCA/field-service
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hooks.py
66 lines (58 loc) · 2.31 KB
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (C) 2019 Brian McMaster
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import SUPERUSER_ID, api
def pre_init_hook(cr):
# Check for existing fsm vehicles
cr.execute('SELECT * FROM fsm_vehicle')
vehicles = []
vehicles = cr.dictfetchall()
if vehicles:
# Add new columns to hold values
cr.execute('ALTER TABLE fsm_vehicle ADD fleet_vehicle_id INT;')
cr.execute('ALTER TABLE fleet_vehicle ADD is_fsm_vehicle BOOLEAN;')
# Get a fleet vehicle model to set on the new Fleet vehicle(s)
env = api.Environment(cr, SUPERUSER_ID, {})
model_id = env['fleet.vehicle.model'].search([], limit=1).id
# Create a new Fleet vehicle for each FSM vehicle
for veh in vehicles:
# Get the FSM worker to set as the Fleet driver
fsm_person_id = veh.get('person_id', False)
driver_id = False
if fsm_person_id:
driver_id = env['fsm.person'].browse(
fsm_person_id).partner_id.id
cr.execute("""
INSERT INTO fleet_vehicle (
name,
model_id,
driver_id,
is_fsm_vehicle,
odometer_unit,
active)
VALUES (
%s,
%s,
%s,
True,
'kilometers',
True);""",
(
veh.get('name'),
model_id,
driver_id
)
)
# Set this new Fleet vehicle on the existing FSM vehicle
cr.execute("""
SELECT id
FROM fleet_vehicle
ORDER BY id desc
LIMIT 1
""")
fleet = cr.dictfetchone()
cr.execute("""
UPDATE fsm_vehicle
SET fleet_vehicle_id = %s
WHERE id = %s;""",
(fleet.get('id'), veh.get('id'))
)