mirror of
https://bitbucket.org/presik/trytonpsk-staff_payroll_co.git
synced 2023-12-14 06:42:56 +01:00
352 lines
14 KiB
Python
352 lines
14 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[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],
|
|
'addresses': [('create', [{
|
|
'street': row[4],
|
|
}])],
|
|
'contact_mechanisms': [
|
|
('create', [
|
|
{'type': 'mobile', 'value': row[2]},
|
|
{'type': 'email', 'value': row[3]},
|
|
])
|
|
]
|
|
}
|
|
party, = Party.create([create_party])
|
|
else:
|
|
party = parties[0]
|
|
if row[22]:
|
|
banks = Bank.search([('party.id_number', '=', row[23])])
|
|
if not banks:
|
|
party_bank = Party.search([('id_number', '=', row[23])])
|
|
if not party_bank:
|
|
cls.raise_user_error('party_not_exists', row[23])
|
|
else:
|
|
bank, = Bank.create([{'party': party_bank[0].id}])
|
|
else:
|
|
bank = banks[0]
|
|
|
|
if row[22].count('AHORRO') > 0:
|
|
type_ = 'saving_account'
|
|
else:
|
|
type_ = 'checking_account'
|
|
|
|
bk_acct_to_create = [{
|
|
'type': type_,
|
|
'number': row[21]
|
|
|
|
}]
|
|
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[6])])
|
|
if not categories:
|
|
cls.raise_user_error('category_not_exists', row[6])
|
|
category = categories[0]
|
|
projects = Project.search([('name', '=', row[7])])
|
|
if not projects:
|
|
cls.raise_user_error('project_not_exists', row[7])
|
|
project = projects[0]
|
|
departments = Department.search([('name', '=', row[8])])
|
|
if not departments:
|
|
cls.raise_user_error('department_not_exists', row[8])
|
|
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[9]),
|
|
])
|
|
if not accounts:
|
|
cls.raise_user_error('analytic_account_not_exists', row[9])
|
|
|
|
# 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,
|
|
# })
|
|
|
|
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]:
|
|
cls.raise_user_error('party_not_exists', row[16])
|
|
elif wage.type_concept == 'retirement':
|
|
parties = Party.search([('id_number', '=', row[17])])
|
|
if not parties and row[17]:
|
|
cls.raise_user_error('party_not_exists', row[17])
|
|
elif wage.type_concept == 'box_family':
|
|
parties = Party.search([('id_number', '=', row[18])])
|
|
if not parties and row[18]:
|
|
cls.raise_user_error('party_not_exists', row[18])
|
|
elif wage.type_concept == 'risk':
|
|
parties = Party.search([('id_number', '=', row[19])])
|
|
if not parties and row[19]:
|
|
cls.raise_user_error('party_not_exists', row[19])
|
|
|
|
if parties:
|
|
party_id = parties[0].id
|
|
|
|
if wage.name == 'BONIFICACIONES OPERATIVO':
|
|
fix_amount = row[15]
|
|
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[5])])
|
|
if not positions:
|
|
positions = Position.create([{
|
|
'name': row[5],
|
|
}])
|
|
contracts = Contract.search([('employee', '=', employee.id)])
|
|
|
|
if not contracts:
|
|
start_date_contract = row[10]
|
|
end_date_contract = row[11]
|
|
|
|
contract, = Contract.create([{
|
|
'employee': employee.id,
|
|
'position': positions[0].id,
|
|
'salary': Decimal(row[13]) if row[13] else 0,
|
|
'transport_bonus': Decimal(row[14]) if row[14] else 0,
|
|
'kind': KIND_CONT[row[12]],
|
|
'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')
|