Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] hr_employee_lastnames: Added module to split Last Name in Father's Last Name and Mother's Last Name #880

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions hr_employee_firstname/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,27 @@ def split_name(self, name):
clean_name = " ".join(name.split(None)) if name else name
return self.env['res.partner']._get_inverse_name(clean_name)

@api.model
def _get_names_order(self):
return self.env['res.partner']._get_names_order()

@api.multi
def _inverse_name(self):
"""Try to revert the effect of :method:`._compute_name`."""
order = self._get_names_order()
for record in self:
parts = self.env['res.partner']._get_inverse_name(record.name)
if len(parts) > 2:
keys = [item for item in parts.keys() if item not in [
'firstname', 'lastname']]
additional_parts = ''
if order == 'last_first':
field = 'lastname'
else:
field = 'firstname'
for key in keys:
additional_parts += ' ' + parts[key] if parts[key] else ''
parts[field] += additional_parts
record.lastname = parts['lastname']
record.firstname = parts['firstname']

Expand Down
57 changes: 57 additions & 0 deletions hr_employee_lastnames/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: https://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

HR Employee First Name and Two Last Names
=========================================

This module allows you to add firstname and lastnames (Father an Mother Last Name) in employee form,
and concatenate both in name field.

Installation
============

To install this module, you need to:

* clone the branch 10.0 of the repository https://github.com/OCA/hr
* add the path to this repository in your configuration (addons-path)
* update the module list
* search for "HR Employee First Name and Two Last Names" in your addons
* install the module

Usage
=====

On the employee form view you will have 3 separate fields, one for Firstname,
second for Lastname, both required and Mother's Last Name (optional).

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
`here <https://github.com/OCA/hr/issues/new?body=module:%20hr_employee_lastnames%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Credits
=======

Contributors
------------

* Luis Escobar (Vauxoo) <[email protected]>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

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.

To contribute to this module, please visit https://odoo-community.org.
3 changes: 3 additions & 0 deletions hr_employee_lastnames/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from . import models
from .hook import post_init_hook
24 changes: 24 additions & 0 deletions hr_employee_lastnames/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': "HR Employee First Name and Two Last Names",
'version': '10.0.1.0.1',
'author': "Vauxoo, "
"Odoo Community Association (OCA)",
'maintainer': 'Vauxoo',
'website': 'https://github.com/OCA/hr/',
'license': 'AGPL-3',
'category': 'Human Resources',
'summary': "Split Name in First Name, Father's Last Name and Mother's Last Name",
'depends': [
'partner_second_lastname',
'hr_employee_firstname',
],
'data': [
'views/hr_views.xml',
],
'post_init_hook': "post_init_hook",
'demo': [],
'test': [],
'installable': True,
}
30 changes: 30 additions & 0 deletions hr_employee_lastnames/hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
from odoo import SUPERUSER_ID
from odoo.api import Environment


def post_init_hook(cr, _):
# This SQL statement is necessary to call _install_employee_lastnames() and
# set name fields correctly.
#
# After the installation, previously the dependency hr_employee_firstname
# splitting the name into two parts: firstname and lastname, so for this
# module to be able to process the new field lastmane2 it is necessary to
# reset the values to empty to be able to correctly set the three fields
# (firstname, lastname and lastname2).
#
# For example:
# After install hr_employee_fisrtname and before install hr_employee_lastnames:
# name = 'John Peterson Clinton'
# firstname = 'John'
# lastname = 'Peterson Clinton'
#
# After install hr_employee_lastnames:
# name = 'John Peterson Clinton'
# firstname = 'John'
# lastname = 'Peterson'
# lastname2 = 'Clinton'
cr.execute('UPDATE hr_employee SET firstname = NULL, lastname = NULL')
env = Environment(cr, SUPERUSER_ID, {})
env['hr.employee']._install_employee_lastnames()
env['ir.config_parameter'].sudo().set_param('partner_names_order', 'first_last')
43 changes: 43 additions & 0 deletions hr_employee_lastnames/i18n/es.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * hr_employee_lastnames
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 10.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-08-31 12:31+0000\n"
"PO-Revision-Date: 2020-08-31 12:31+0000\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: hr_employee_lastnames
#: model:ir.model,name:hr_employee_lastnames.model_hr_employee
msgid "Employee"
msgstr "Empleado"

#. module: hr_employee_lastnames
#: model:ir.model.fields,field_description:hr_employee_lastnames.field_hr_employee_firstname
msgid "First name"
msgstr "Nombre Propio"

#. module: hr_employee_lastnames
#: model:ir.model.fields,field_description:hr_employee_lastnames.field_hr_employee_lastname
msgid "Last name"
msgstr "Primer Apellido"

#. module: hr_employee_lastnames
#: code:addons/hr_employee_lastnames/models/hr_employee.py:32
#: code:addons/hr_employee_lastnames/models/hr_employee.py:112
#, python-format
msgid "No name set."
msgstr "Sin nombre establecido."

#. module: hr_employee_lastnames
#: model:ir.model.fields,field_description:hr_employee_lastnames.field_hr_employee_lastname2
msgid "Second last name"
msgstr "Segundo Apellido"
2 changes: 2 additions & 0 deletions hr_employee_lastnames/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import hr_employee
106 changes: 106 additions & 0 deletions hr_employee_lastnames/models/hr_employee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-
import logging

from odoo import api, fields, models, _
from odoo.exceptions import UserError
from odoo.addons.hr_employee_firstname.models.hr_employee import UPDATE_PARTNER_FIELDS

_logger = logging.getLogger(__name__)

UPDATE_PARTNER_FIELDS += ['lastname2']


class HrEmployee(models.Model):
_inherit = 'hr.employee'

firstname = fields.Char("First name")
lastname = fields.Char("Last name")
lastname2 = fields.Char("Second last name")

@api.model
def _get_name_lastnames(self, lastname, firstname, lastname2=None):
return self.env['res.partner']._get_computed_name(
lastname, firstname, lastname2)

def _prepare_vals_on_create_firstname_lastname(self, vals):
values = vals.copy()
res = super(HrEmployee, self)._prepare_vals_on_create_firstname_lastname(values)
if any([field in vals for field in 'firstname', 'lastname', 'lastname2']):
vals['name'] = self._get_name_lastnames(
vals.get('lastname'), vals.get('firstname'), vals.get('lastname2'))
elif vals.get('name'):
name_splitted = self.split_name(vals['name'])
vals['firstname'] = name_splitted['firstname']
vals['lastname'] = name_splitted['lastname']
vals['lastname2'] = name_splitted['lastname2']
else:
raise UserError(_('No name set.'))
return res

def _prepare_vals_on_write_firstname_lastname(self, vals):
values = vals.copy()
res = super(HrEmployee, self)._prepare_vals_on_write_firstname_lastname(values)
if any([field in vals for field in 'firstname', 'lastname', 'lastname2']):
if 'lastname' in vals:
lastname = vals['lastname']
else:
lastname = self.lastname
if 'firstname' in vals:
firstname = vals['firstname']
else:
firstname = self.firstname
if 'lastname2' in vals:
lastname2 = vals['lastname2']
else:
lastname2 = self.lastname2
vals['name'] = self._get_name_lastnames(lastname, firstname, lastname2)
elif vals.get('name'):
name_splitted = self.split_name(vals['name'])
vals['lastname'] = name_splitted['lastname']
vals['firstname'] = name_splitted['firstname']
vals['lastname2'] = name_splitted['lastname2']
return res

def _update_partner_firstname(self):
for employee in self:
partners = employee.mapped('user_id.partner_id')
partners |= employee.mapped('address_home_id')
partners.write({
'firstname': employee.firstname,
'lastname': employee.lastname,
'lastname2': employee.lastname2,
})
return

@api.multi
def _inverse_name(self):
"""Try to revert the effect of :method:`._compute_name`."""
for record in self:
parts = self.env['res.partner']._get_inverse_name(record.name)
record.write({
'lastname': parts['lastname'],
'firstname': parts['firstname'],
'lastname2': parts['lastname2'],

})

@api.model
def _install_employee_lastnames(self):
"""Save names correctly in the database.
Before installing the module, field ``name`` contains all full names.
When installing it, this method parses those names and saves them
correctly into the database. This can be called later too if needed.
"""
# Find records with empty firstname and lastnames
records = self.search([("firstname", "=", False),
("lastname", "=", False)])

# Force calculations there
records._inverse_name()
_logger.info("%d employees updated installing module.", len(records))

@api.onchange('firstname', 'lastname', 'lastname2')
def _onchange_firstname_lastname(self):
if self.firstname or self.lastname or self.lastname2:
self.name = self._get_name_lastnames(
self.lastname, self.firstname, self.lastname2)
2 changes: 2 additions & 0 deletions hr_employee_lastnames/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Luis Escobar <[email protected]>
* Hugo Adan <[email protected]>
2 changes: 2 additions & 0 deletions hr_employee_lastnames/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows you to add firstname and lastnames (Father an Mother Last Name) in employee form,
and concatenate both in name field.
2 changes: 2 additions & 0 deletions hr_employee_lastnames/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#. Go to *Employees*
#. On the employee form view you will have 3 separate fields, one for Firstname, second for Lastname, both required and Mother's Last Name (optional).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions hr_employee_lastnames/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Savoir-faire Linux. All Rights Reserved.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from . import test_hr_employee_lastnames
Loading