trytonpsk-staff_payroll_co/employee.py

353 lines
14 KiB
Python
Raw Normal View History

2020-04-16 00:38:42 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from trytond.model import fields
2020-05-09 20:45:19 +02:00
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
2020-05-09 23:55:19 +02:00
from decimal import Decimal
2020-06-13 01:50:41 +02:00
from datetime import datetime
2020-04-16 00:38:42 +02:00
__all__ = ['Employee']
SEX = {
'': '',
'hombre': 'male',
'mujer': 'female'
}
MARITAL = {
'': '',
'VIUDO(A)': 'widow',
'CASADO(A)': 'married',
'SOLTERO(A)': 'single',
'UNION_LIBRE': 'free_union',
'DIVORCIADO(A)': 'divorced'
}
BLOOD = {
'': '',
'O+': 'rh_op',
'O-': 'rh_on',
'A+': 'rh_ap',
'A-': 'rh_an',
'B+': 'rh_bp',
'B-': 'rh_bn',
'AB+': 'rh_abp',
'AB-': 'rh_abn'
}
KIND_CONT = {
'': '',
'fijo': 'indefinite',
'obra_o_labor': 'job',
'termino_indefinido': 'steady'
}
2020-04-16 00:38:42 +02:00
class Employee(metaclass=PoolMeta):
2020-04-16 00:38:42 +02:00
__name__ = 'company.employee'
party_health = fields.Function(fields.Many2One('party.party', 'Party Health'),
'get_party_concept')
party_retirement = fields.Function(fields.Many2One('party.party', 'Party Retirement'),
'get_party_concept')
party_risk = fields.Function(fields.Many2One('party.party', 'Party Risk'),
'get_party_concept')
party_box_family = fields.Function(fields.Many2One('party.party', 'Party Box Family'),
'get_party_concept')
party_bank = fields.Function(fields.Many2One('party.party', 'Party Bank'),
'get_party_bank')
@classmethod
def __setup__(cls):
super(Employee, cls).__setup__()
2020-06-18 05:21:44 +02:00
cls._error_messages.update({
'party_not_exists': ('The party with document "%s" not exists!'),
2020-06-18 17:42:22 +02:00
'department_not_exists': ('The department with name "%s" not exists!'),
'project_not_exists': ('The project with name "%s" not exists!'),
'category_not_exists': ('The category with name "%s" not exists!'),
'analytic_account_not_exists': ('The analytic account with name "%s" not exists!'),
2020-06-18 05:21:44 +02:00
})
2020-04-16 00:38:42 +02:00
@classmethod
def import_data(cls, fields_names, data):
pool = Pool()
Contract = pool.get('staff.contract')
Position = pool.get('staff.position')
Party = pool.get('party.party')
Department = pool.get('company.department')
Category = pool.get('staff.employee_category')
WageType = pool.get('staff.wage_type')
AnalyticAccount = pool.get('analytic_account.account')
MandatoryWage = pool.get('staff.payroll.mandatory_wage')
2020-06-18 05:21:44 +02:00
Project = pool.get('project.work')
Bank = pool.get('bank')
BankAccount = pool.get('bank.account')
BankAccountParty = pool.get('bank.account-party.party')
2020-04-16 00:38:42 +02:00
cont = 0
def compute_date(date_str):
format_str = '%d/%m/%Y' # The format
datetime_obj = datetime.strptime(date_str, format_str)
return datetime_obj.date()
def _create_party(class_, id_number):
parties = class_.create([{
'name': 'pendiente',
'id_number': id_number,
}])
return parties
for row in data[1:]:
parties = Party.search([('id_number', '=', row[0])])
if not parties:
names_ = row[1].split(' ')
fn, sn, ffn, sfn = '', '', '', ''
if len(names_) > 3:
fn = names_[0]
sn = names_[1]
ffn = names_[2]
sfn = names_[3]
elif len(names_) == 3:
fn = names_[0]
ffn = names_[1]
sfn = names_[2]
elif len(names_) == 2:
fn = names_[0]
ffn = names_[1]
create_party = {
'type_document': '13',
'id_number': row[0],
2020-04-16 00:38:42 +02:00
'name': row[1],
'first_name': fn,
'second_name': sn,
'first_family_name': ffn,
'second_family_name': sfn,
2020-08-04 19:39:26 +02:00
'birthday': compute_date(row[24]) if row[24] else None,
'sex': SEX[row[25]],
'marital_status': MARITAL[row[26]],
'blood_group': BLOOD[row[32]],
'subdivision_born': row[27] or '',
'city_born': row[28],
'education': row[30],
'profession': row[29],
'weight': float(row[31]) if row[31] else None,
'health': row[33],
'notes': row[34],
2020-04-16 00:38:42 +02:00
'addresses': [('create', [{
2020-08-04 19:39:26 +02:00
'street': row[4],
2020-04-16 00:38:42 +02:00
}])],
'contact_mechanisms': [
('create', [
{'type': 'mobile', 'value': row[2]},
2020-08-04 19:39:26 +02:00
{'type': 'email', 'value': row[3]},
2020-04-16 00:38:42 +02:00
])
]
}
party, = Party.create([create_party])
else:
party = parties[0]
2020-06-20 19:21:28 +02:00
if row[22]:
2020-08-04 19:39:26 +02:00
banks = Bank.search([('party.id_number', '=', row[23])])
2020-06-20 19:21:28 +02:00
if not banks:
2020-08-04 19:39:26 +02:00
party_bank = Party.search([('id_number', '=', row[23])])
2020-06-20 19:21:28 +02:00
if not party_bank:
2020-08-04 19:39:26 +02:00
cls.raise_user_error('party_not_exists', row[23])
2020-06-20 19:21:28 +02:00
else:
bank, = Bank.create([{'party': party_bank[0].id}])
2020-06-18 05:21:44 +02:00
else:
2020-06-20 19:21:28 +02:00
bank = banks[0]
2020-06-18 05:21:44 +02:00
2020-08-04 19:39:26 +02:00
if row[22].count('AHORRO') > 0:
2020-06-20 19:21:28 +02:00
type_ = 'saving_account'
else:
type_ = 'checking_account'
2020-06-18 05:21:44 +02:00
2020-06-20 19:21:28 +02:00
bk_acct_to_create = [{
'type': type_,
2020-08-04 19:39:26 +02:00
'number': row[21]
2020-06-18 05:21:44 +02:00
2020-06-20 19:21:28 +02:00
}]
bank_account, = BankAccount.create([{
'bank': bank.id,
'numbers': [('create', bk_acct_to_create)]
}])
2020-06-18 05:21:44 +02:00
2020-06-20 19:21:28 +02:00
BankAccountParty.create([{
'owner': party.id,
'account': bank_account.id
}])
2020-06-18 05:21:44 +02:00
2020-08-04 19:39:26 +02:00
categories = Category.search([('name', '=', row[6])])
2020-06-18 17:42:22 +02:00
if not categories:
2020-08-04 19:39:26 +02:00
cls.raise_user_error('category_not_exists', row[6])
2020-06-19 22:56:50 +02:00
category = categories[0]
2020-08-04 19:39:26 +02:00
projects = Project.search([('name', '=', row[7])])
2020-06-18 17:42:22 +02:00
if not projects:
2020-08-04 19:39:26 +02:00
cls.raise_user_error('project_not_exists', row[7])
2020-06-19 22:56:50 +02:00
project = projects[0]
2020-08-04 19:39:26 +02:00
departments = Department.search([('name', '=', row[8])])
2020-06-18 17:42:22 +02:00
if not departments:
2020-08-04 19:39:26 +02:00
cls.raise_user_error('department_not_exists', row[8])
2020-06-19 22:56:50 +02:00
department = departments[0]
2020-04-16 00:38:42 +02:00
employees = cls.search([('party', '=', party.id)])
if employees:
employee = employees[0]
else:
employee, = cls.create([{
'party': party.id,
'contracting_state': 'active',
2020-06-19 22:56:50 +02:00
'department': department.id,
'category': category.id,
2020-04-16 00:38:42 +02:00
}])
values_mand_wage = []
if employee.category and employee.category.wages_default:
2020-10-01 18:26:08 +02:00
# current_wages_ids = [m.wage_type.id
# for m in employee.mandatory_wages]
2020-04-16 00:38:42 +02:00
accounts = AnalyticAccount.search([
2020-08-04 19:39:26 +02:00
('name', '=', row[9]),
2020-04-16 00:38:42 +02:00
])
if not accounts:
2020-08-04 19:39:26 +02:00
cls.raise_user_error('analytic_account_not_exists', row[9])
2020-04-16 00:38:42 +02:00
2020-09-18 22:05:11 +02:00
# mandatory_wages = {
# '1': 'ARL CLASE I OPERATIVO',
# '2': 'ARL CLASE II OPERATIVO',
# '3': 'ARL CLASE III OPERATIVO',
# '4': 'ARL CLASE IV OPERATIVO',
# '5': 'ARL CLASE V OPERATIVO',
# }
# key_mw = str(row[20])
# if key_mw:
# arl_wt = mandatory_wages[key_mw]
# if category.name.count('ADMIN') > 0:
# arl_wt = arl_wt.replace(' OPERATIVO', '')
# wages = WageType.search([
# ('name', '=', arl_wt)
# ])
# if wages:
# wage_id = wages[0].id
# if wage_id not in current_wages_ids:
# if row[19]:
# parties = Party.search([('id_number', '=', row[19])])
# if not parties and row[18]:
# cls.raise_user_error('party_not_exists', row[19])
# # parties = _create_party(Party, row[18])
# party_id = parties[0].id
# else:
# party_id = None
#
# values_mand_wage.append({
# 'employee': employee.id,
# 'fix_amount': 0,
# 'party': party_id,
# 'wage_type': wage_id,
# 'analytic_account': accounts[0].id,
# })
2020-04-16 00:38:42 +02:00
for wage in employee.category.wages_default:
parties = []
party_id = None
if wage.type_concept == 'health':
parties = Party.search([('id_number', '=', row[16])])
if not parties and row[16]:
2020-10-01 18:26:08 +02:00
cls.raise_user_error('party_not_exists', row[16])
2020-08-04 19:39:26 +02:00
elif wage.type_concept == 'retirement':
2020-04-16 00:38:42 +02:00
parties = Party.search([('id_number', '=', row[17])])
if not parties and row[17]:
2020-10-01 18:26:08 +02:00
cls.raise_user_error('party_not_exists', row[17])
2020-08-04 19:39:26 +02:00
elif wage.type_concept == 'box_family':
parties = Party.search([('id_number', '=', row[18])])
if not parties and row[18]:
2020-10-01 18:26:08 +02:00
cls.raise_user_error('party_not_exists', row[18])
2020-09-18 22:28:32 +02:00
elif wage.type_concept == 'risk':
parties = Party.search([('id_number', '=', row[19])])
if not parties and row[19]:
2020-10-01 18:26:08 +02:00
cls.raise_user_error('party_not_exists', row[19])
2020-04-16 00:38:42 +02:00
if parties:
party_id = parties[0].id
if wage.name == 'BONIFICACIONES OPERATIVO':
2020-08-04 19:39:26 +02:00
fix_amount = row[15]
2020-04-16 00:38:42 +02:00
else:
2020-06-18 05:21:44 +02:00
fix_amount = None
2020-04-16 00:38:42 +02:00
values_mand_wage.append({
'employee': employee.id,
2020-06-18 05:21:44 +02:00
'fix_amount': fix_amount,
2020-04-16 00:38:42 +02:00
'party': party_id,
'wage_type': wage.id,
'analytic_account': accounts[0].id if accounts else None,
})
MandatoryWage.create(values_mand_wage)
2020-08-04 19:39:26 +02:00
positions = Position.search([('name', 'ilike', row[5])])
2020-04-16 00:38:42 +02:00
if not positions:
positions = Position.create([{
2020-08-04 19:39:26 +02:00
'name': row[5],
2020-04-16 00:38:42 +02:00
}])
contracts = Contract.search([('employee', '=', employee.id)])
2020-05-24 01:28:22 +02:00
2020-04-16 00:38:42 +02:00
if not contracts:
2020-08-04 19:39:26 +02:00
start_date_contract = row[10]
end_date_contract = row[11]
2020-04-16 00:38:42 +02:00
contract, = Contract.create([{
'employee': employee.id,
'position': positions[0].id,
2020-08-04 19:39:26 +02:00
'salary': Decimal(row[13]) if row[13] else 0,
'transport_bonus': Decimal(row[14]) if row[14] else 0,
'kind': KIND_CONT[row[12]],
2020-04-16 00:38:42 +02:00
'contract_date': compute_date(start_date_contract),
'start_date': compute_date(start_date_contract),
2020-06-18 05:21:44 +02:00
'end_date': compute_date(end_date_contract) if end_date_contract else None,
'builder_employee': 'no',
2020-04-16 00:38:42 +02:00
'state': 'draft',
2020-06-19 22:56:50 +02:00
'project': project.id,
2020-04-16 00:38:42 +02:00
}])
Contract.active([contract])
cont += 1
return cont
def get_party_concept(self, name=None):
res = None
name = name[6:]
for mw in self.mandatory_wages:
if mw.party and mw.wage_type.type_concept == name:
res = mw.party.id
break
return res
def get_party_bank(self, name=None):
return None
class MandatoryWage(metaclass=PoolMeta):
__name__ = 'staff.payroll.mandatory_wage'
@classmethod
def __register__(cls, module_name):
super(MandatoryWage, cls).__register__(module_name)
cursor = Transaction().connection.cursor()
sql_table = cls.__table__()
string_query = "SELECT EXISTS(" \
" SELECT column_name FROM information_schema.columns" \
" WHERE table_schema='public'" \
" and table_name='staff_payroll_mandatory_wage'" \
" and column_name='defect_amount')"
cursor.execute(string_query)
res = cursor.fetchall()
if res and res[0][0]:
# try:
cursor.execute(*sql_table.update(
columns=[sql_table.fix_amount],
values=[sql_table.defect_amount]))
cursor.execute('ALTER TABLE staff_payroll_mandatory_wage DROP COLUMN IF EXISTS defect_amount')
# cursor.execute('UPDATE staff_payroll_mandatory_wage set fix_amount=defect_amount')
# except:
# print('# WARNING: The column defect_amount not exists')