trytonpsk-staff_payroll_co/employee.py
2020-06-19 22:36:53 -05:00

345 lines
13 KiB
Python

#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
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
from decimal import Decimal
from datetime import datetime
__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'
}
class Employee(metaclass=PoolMeta):
__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__()
cls._error_messages.update({
'party_not_exists': ('The party with document "%s" not exists!'),
'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!'),
})
@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')
Project = pool.get('project.work')
Bank = pool.get('bank')
BankAccount = pool.get('bank.account')
BankAccountParty = pool.get('bank.account-party.party')
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],
'name': row[1],
'first_name': fn,
'second_name': sn,
'first_family_name': ffn,
'second_family_name': sfn,
'birthday': compute_date(row[23]),
'sex': SEX[row[24]],
'marital_status': MARITAL[row[25]],
'blood_group': BLOOD[row[31]],
'subdivision_born': row[26],
'city_born': row[27],
'education': row[29],
'profession': row[28],
'weight': row[30],
'health': row[32],
'notes': row[33],
'addresses': [('create', [{
'street': row[3],
}])],
'contact_mechanisms': [
('create', [
{'type': 'mobile', 'value': row[2]},
])
]
}
party, = Party.create([create_party])
else:
party = parties[0]
banks = Bank.search([('party.id_number', '=', row[22])])
if not banks:
party_bank = Party.search([('id_number', '=', row[22])])
if not party_bank:
cls.raise_user_error('party_not_exists', row[22])
else:
bank, = Bank.create([{'party': party_bank[0].id}])
else:
bank = banks[0]
if row[21].count('AHORRO') > 0:
type_ = 'saving_account'
else:
type_ = 'checking_account'
bk_acct_to_create = [{
'type': type_,
'number': row[20]
}]
bank_account, = BankAccount.create([{
'bank': bank.id,
'numbers': [('create', bk_acct_to_create)]
}])
BankAccountParty.create([{
'owner': party.id,
'account': bank_account.id
}])
categories = Category.search([('name', '=', row[5])])
if not categories:
cls.raise_user_error('category_not_exists', row[5])
category = categories[0]
projects = Project.search([('name', '=', row[6])])
if not projects:
cls.raise_user_error('project_not_exists', row[6])
project = projects[0]
departments = Department.search([('name', '=', row[7])])
if not departments:
cls.raise_user_error('department_not_exists', row[7])
department = departments[0]
employees = cls.search([('party', '=', party.id)])
if employees:
employee = employees[0]
else:
employee, = cls.create([{
'party': party.id,
'contracting_state': 'active',
'department': department.id,
'category': category.id,
}])
values_mand_wage = []
if employee.category and employee.category.wages_default:
current_wages_ids = [m.wage_type.id
for m in employee.mandatory_wages]
accounts = AnalyticAccount.search([
('name', '=', row[8]),
])
if not accounts:
cls.raise_user_error('analytic_account_not_exists', row[8])
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[19])
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
parties = Party.search([('id_number', '=', row[18])])
if not parties and row[18]:
cls.raise_user_error('party_not_exists', row[18])
# parties = _create_party(Party, row[18])
values_mand_wage.append({
'employee': employee.id,
'fix_amount': 0,
'party': parties[0].id,
'wage_type': wage_id,
'analytic_account': accounts[0].id,
})
for wage in employee.category.wages_default:
parties = []
party_id = None
if wage.type_concept == 'health':
parties = Party.search([('id_number', '=', row[15])])
if not parties and row[15]:
parties = _create_party(Party, row[15])
elif wage.type_concept == 'retirement':
parties = Party.search([('id_number', '=', row[16])])
if not parties and row[16]:
parties = _create_party(Party, row[16])
elif wage.type_concept == 'box_family':
parties = Party.search([('id_number', '=', row[17])])
if not parties and row[17]:
parties = _create_party(Party, row[17])
if parties:
party_id = parties[0].id
if wage.name == 'BONIFICACIONES OPERATIVO':
fix_amount = row[14]
else:
fix_amount = None
values_mand_wage.append({
'employee': employee.id,
'fix_amount': fix_amount,
'party': party_id,
'wage_type': wage.id,
'analytic_account': accounts[0].id if accounts else None,
})
MandatoryWage.create(values_mand_wage)
positions = Position.search([('name', 'ilike', row[4])])
if not positions:
positions = Position.create([{
'name': row[4],
}])
contracts = Contract.search([('employee', '=', employee.id)])
if not contracts:
start_date_contract = row[9]
end_date_contract = row[10]
contract, = Contract.create([{
'employee': employee.id,
'position': positions[0].id,
'salary': Decimal(row[12]) if row[12] else 0,
'transport_bonus': Decimal(row[13]) if row[13] else 0,
'kind': KIND_CONT[row[11]],
'contract_date': compute_date(start_date_contract),
'start_date': compute_date(start_date_contract),
'end_date': compute_date(end_date_contract) if end_date_contract else None,
'builder_employee': 'no',
'state': 'draft',
'project': project.id,
}])
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')