Skip to content

Commit

Permalink
Merge 646b39d into 5b16d46
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Apr 24, 2019
2 parents 5b16d46 + 646b39d commit 5cca294
Show file tree
Hide file tree
Showing 15 changed files with 637 additions and 212 deletions.
79 changes: 63 additions & 16 deletions hr_employee_data_from_work_address/README.rst
@@ -1,12 +1,36 @@
User data from employee
=======================
===================================
User and partner data from employee
===================================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fhr-lightgray.png?logo=github
:target: https://github.com/OCA/hr/tree/10.0/hr_employee_data_from_work_address
:alt: OCA/hr
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/hr-10-0/hr-10-0-hr_employee_data_from_work_address
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/116/10.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

When using HR, there are a couple of partners involved per employee: The employee's work address, the employee's private address and the partner assigned to the employee's user (if any). The latter is used to store some data about the user, like the email address. In many cases, it would be more convenient to edit this data from the employee record. This module assumes you assign a (different) partner for the work address to every employee, and synchronizes its values with the employee record. Then you should use this partner record also for the user record. When assigning an existing user, the module will replace this user's partner with the current work address's partner.

Installation
============
**Table of contents**

As it would be problematic to have work addresses pointing to the company's partner with this module, all employees are updated with either a new partner as work address in case they are not linked to a user, or with the user's partner otherwise. This may take some time if you have a lot of employees. For existing work addresses, the partner's data win, so if your employee as ha different email/phone number/image than the corresponding partner, those fields will be overwritten - but only if it is set in the partner.
.. contents::
:local:

Usage
=====
Expand All @@ -15,28 +39,51 @@ After installation, updating an employee's `work_email`, `work_phone`, `mobile_p

When creating a user, you can select the existing partner record for your employee, so it will be updated transparently too. Creating a user via the user field in the employee form preselects this partner record.

As it would be problematic to have work addresses pointing to the company's partner with this module, all employees are updated with either a new partner as work address in case they are not linked to a user, or with the user's partner otherwise. This may take some time if you have a lot of employees. For existing work addresses, the partner's data win, so if your employee as ha different email/phone number/image than the corresponding partner, those fields will be overwritten - but only if it is set in the partner.

Also multiple employees pointing to the same partner is problematic. This is fixed by creating new partners for all employees involved, and flagging all of them with the label 'Duplicate work address' and the newly created ones with 'Duplicate work address / Newly created'. Then after installation, search for partners with this labels to do whatever cleanup you consider necessary. After this, it's safe to delete the labels again.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/hr/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/hr/issues/new?body=module:%20hr_employee_data_from_work_address%0Aversion:%2010.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Therp BV

Contributors
------------
~~~~~~~~~~~~

* Holger Brunn <hbrunn@therp.nl>

Acknowledgements
----------------
Other credits
~~~~~~~~~~~~~

* Icon courtesy of http://www.picol.org (refresh.svg) and https://github.com/odoo/odoo/blob/8.0/addons/hr/static/description/icon.png

Maintainer
----------

.. image:: http://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: http://odoo-community.org
Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/hr <https://github.com/OCA/hr/tree/10.0/hr_employee_data_from_work_address>`_ project on GitHub.

To contribute to this module, please visit http://odoo-community.org.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
20 changes: 1 addition & 19 deletions hr_employee_data_from_work_address/__init__.py
@@ -1,22 +1,4 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# Copyright 2015 Therp BV <http://therp.nl>
from . import models
from .init_hook import pre_init_hook, post_init_hook, uninstall_hook
35 changes: 6 additions & 29 deletions hr_employee_data_from_work_address/__manifest__.py
@@ -1,46 +1,23 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# Copyright 2015 Therp BV <http://therp.nl>
{
"name": "User and partner data from employee",
"version": "8.0.1.0.0",
"author": "Therp BV",
"version": "10.0.1.0.0",
"author": "Odoo Community Association (OCA),Therp BV",
"license": "AGPL-3",
"website": "https://github.com/OCA/hr",
"category": "Human Resources",
"summary": "Update user's and partner's data fields from employee record",
"depends": [
'hr',
],
"data": [
"data/res_partner_category.xml",
'views/hr_employee.xml',
'views/res_users.xml',
],
"test": [
],
"pre_init_hook": "pre_init_hook",
"post_init_hook": "post_init_hook",
"uninstall_hook": "uninstall_hook",
"auto_install": False,
'installable': False,
"application": False,
"external_dependencies": {
'python': [],
},
'installable': True,
}
12 changes: 12 additions & 0 deletions hr_employee_data_from_work_address/data/res_partner_category.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="category_duplicate" model="res.partner.category" forcecreate="False">
<field name="name">Duplicate work address</field>
</record>
<record id="category_duplicate_created" model="res.partner.category" forcecreate="False">
<field name="parent_id" ref="category_duplicate" />
<field name="name">Newly created</field>
</record>
</data>
</openerp>
66 changes: 35 additions & 31 deletions hr_employee_data_from_work_address/init_hook.py
@@ -1,23 +1,5 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# Copyright 2015 Therp BV <http://therp.nl>
from openerp import SUPERUSER_ID
from openerp.api import Environment

Expand Down Expand Up @@ -45,8 +27,7 @@ def adjust_employee_partners_pre(cr):
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),
image=coalesce(p.image, e.image)
mobile=coalesce(nullif(trim(p.mobile), ''), e.mobile_phone)
from
hr_employee e
where e.address_id=p.id
Expand All @@ -60,18 +41,12 @@ def adjust_employee_partners_pre(cr):
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')
# recalculate images for partners we possibly touched in pre-init
recalculate_ids = env['hr.employee'].with_context(active_test=False)\
.search([('address_id', 'not in', company_partners.ids)])\
.mapped('address_id.id')
env['res.partner']._model._store_set_values(
env.cr, env.uid, recalculate_ids, ['image_small', 'image_medium'],
env.context)
# 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')
Expand All @@ -80,21 +55,50 @@ def adjust_employee_partners_post(env):
if not employees.ids:
return
env.cr.execute(
'select work_phone, work_email, mobile_phone, image '
'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,
'name': employee.name or employee.display_name,
'phone': db_data['work_phone'],
'email': db_data['work_email'],
'mobile': db_data['mobile_phone'],
'image': db_data['image'],
'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')
20 changes: 1 addition & 19 deletions hr_employee_data_from_work_address/models/__init__.py
@@ -1,21 +1,3 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# Copyright 2015 Therp BV <http://therp.nl>
from . import hr_employee

0 comments on commit 5cca294

Please sign in to comment.