-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_hook.py
104 lines (92 loc) · 3.87 KB
/
init_hook.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
# Copyright 2015 Therp BV <http://therp.nl>
from openerp import SUPERUSER_ID
from openerp.api import Environment
def pre_init_hook(cr):
adjust_employee_partners_pre(cr)
def adjust_employee_partners_pre(cr):
cr.execute('update hr_employee e set address_id=null '
'from res_company c where address_id=c.partner_id')
# set work address to user's partner if any
cr.execute(
'''update hr_employee e
set
address_id=u.partner_id
from
resource_resource r,
res_users u
where e.resource_id=r.id and r.user_id=u.id and e.address_id is null
''')
# copy unset fields on the linked partner records from the employee
cr.execute(
'''update res_partner p
set
phone=coalesce(nullif(trim(p.phone), ''), e.work_phone),
email=coalesce(nullif(trim(p.email), ''), e.work_email),
mobile=coalesce(nullif(trim(p.mobile), ''), e.mobile_phone)
from
hr_employee e
where e.address_id=p.id
''')
# point all remaining employees without address_id to a company partner to
# satisfy newly introduced non null constraint
cr.execute('update hr_employee e set address_id=c.partner_id '
'from res_company c where e.address_id is null')
def post_init_hook(cr, pool):
env = Environment(cr, SUPERUSER_ID, {})
adjust_employee_partners_post(env)
fix_nonunique_employee_partners(env)
def adjust_employee_partners_post(env):
companies = env['res.company'].with_context(active_test=False).search([])
company_partners = companies.mapped('partner_id')
# create a new partner for all employees pointing to a company address
employees = env['hr.employee'].with_context(active_test=False).search(
[('address_id', 'in', company_partners.ids)], order='id')
# we need to read related values from the database because the related
# fields already cover our fields
if not employees.ids:
return
env.cr.execute(
'select work_phone, work_email, mobile_phone '
'from hr_employee where id in %s order by id',
(tuple(employees.ids),))
employee_db_data = env.cr.dictfetchall()
for employee, db_data in zip(employees, employee_db_data):
employee.address_id = env['res.partner'].create({
'employee': True,
'name': employee.name or employee.display_name,
'phone': db_data['work_phone'],
'email': db_data['work_email'],
'mobile': db_data['mobile_phone'],
'active': employee.active,
})
def fix_nonunique_employee_partners(env):
'''If some employees point to the same partner, this will yield weird
results. Create new partners here, and label duplicates'''
category_duplicate = env.ref(
'hr_employee_data_from_work_address.category_duplicate'
)
category_duplicate_created = env.ref(
'hr_employee_data_from_work_address.category_duplicate_created'
)
env.cr.execute(
'select address_id, employee_ids from '
'(select address_id, array_agg(id) employee_ids, '
'count(address_id) amount from hr_employee group by address_id) '
'employee_amount where amount > 1'
)
for partner_id, employee_ids in env.cr.fetchall():
partner = env['res.partner'].browse(partner_id)
partner.write({
'category_id': [(4, category_duplicate.id)],
})
employees = env['hr.employee'].browse(employee_ids)
for employee in employees:
employee.write({
'address_id': partner.copy(default={
'category_id': [(4, category_duplicate_created.id)],
'name': employee.name or employee.display_name,
}).id,
})
def uninstall_hook(cr, pool):
cr.execute('alter table hr_employee alter column address_id drop not null')