trytonpsk-staff/contract.py

176 lines
5.9 KiB
Python
Raw Normal View History

2020-04-16 00:27:15 +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 ModelView, Workflow, ModelSQL, fields
from trytond.pyson import Eval
from trytond.pool import Pool
from trytond.transaction import Transaction
2020-06-13 01:47:21 +02:00
from sql import Null
2020-04-16 00:27:15 +02:00
__all__ = ['StaffContract']
2020-06-13 01:47:21 +02:00
STATES = {'readonly': (Eval('state') != 'draft')}
2020-04-16 00:27:15 +02:00
class StaffContract(Workflow, ModelSQL, ModelView):
"Staff Contract"
__name__ = "staff.contract"
_rec_name = 'number'
number = fields.Char('Number', select=True, states=STATES)
employee = fields.Many2One('company.employee', 'Employee',
select=True, required=True, states=STATES)
company = fields.Many2One('company.company', 'Company', required=True,
select=True, states=STATES)
contract_date = fields.Date('Contract Date', required=True,
states=STATES)
start_date = fields.Date('Start Contract', required=True, states=STATES)
end_date = fields.Date('End Contract', states={
'required': Eval('state') == 'finished',
'readonly': Eval('state') == 'finished',
}, depends=['state'])
salary = fields.Numeric('Salary', digits=(16, 2), states=STATES,
help='Salary monthly of person', required=True)
kind = fields.Selection([
('steady', 'Steady'),
('indefinite', 'Indefinite'),
('job', 'Job'),
('specific_labour', 'Specific and Particular Labour'),
], 'Kind', select=True, states=STATES)
kind_string = kind.translated('kind')
state = fields.Selection([
2020-06-13 01:47:21 +02:00
('draft', 'Draft'),
('active', 'Active'),
('finished', 'Finished'),
], 'State', readonly=True, select=True)
2020-04-16 00:27:15 +02:00
payment_term = fields.Char('Payment Term', states=STATES)
comment = fields.Text("Comment", states=STATES)
position = fields.Many2One('staff.position', 'Position',
select=True)
@classmethod
def __setup__(cls):
super(StaffContract, cls).__setup__()
cls._order.insert(0, ('number', 'DESC'))
cls._transitions |= set((
2020-06-13 01:47:21 +02:00
('draft', 'active'),
('active', 'draft'),
('active', 'finished'),
))
2020-04-16 00:27:15 +02:00
cls._buttons.update({
2020-06-13 01:47:21 +02:00
'draft': {
'invisible': Eval('state') != 'active',
},
'active': {
'invisible': Eval('state') != 'draft',
},
'finished': {
'invisible': Eval('state') != 'active',
},
})
2020-04-16 00:27:15 +02:00
cls._error_messages.update({
2020-05-28 02:38:39 +02:00
'employee_with_contract_current': ('The employee %s already has a contract in draft or active!'),
'missing_contract_sequence': ('The contract sequence is missing on configuration!'),
'finish_contract_out_date': ('You can not to finish a contract with end date on future!'),
})
2020-04-16 00:27:15 +02:00
@classmethod
def __register__(cls, module_name):
super(StaffContract, cls).__register__(module_name)
pool = Pool()
2020-05-28 02:38:39 +02:00
Employee = pool.get('company.employee')
2020-04-16 00:27:15 +02:00
cursor = Transaction().connection.cursor()
sql_table = cls.__table__()
2020-05-28 02:38:39 +02:00
employee = Employee.__table__()
2020-04-16 00:27:15 +02:00
# Migration:
# - Migration position from employee into contract
2020-05-28 02:38:39 +02:00
sub_query = employee.select(
employee.id, employee.position,
where=(employee.position != Null)
2020-04-16 00:27:15 +02:00
)
cursor.execute(*sub_query)
for employee_id, position in cursor.fetchall():
cursor.execute(*sql_table.update(
columns=[sql_table.position],
values=[position],
where=(sql_table.employee == employee_id)
& (sql_table.position == Null)
))
@staticmethod
def default_company():
return Transaction().context.get('company')
@staticmethod
def default_state():
return 'draft'
@staticmethod
def default_kind():
return 'job'
@classmethod
def search_rec_name(cls, name, clause):
if clause[1].startswith('!') or clause[1].startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
return [bool_op,
('number',) + tuple(clause[1:]),
('employee',) + tuple(clause[1:]),
2020-06-13 01:47:21 +02:00
]
2020-04-16 00:27:15 +02:00
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, records):
pass
@classmethod
@ModelView.button
@Workflow.transition('active')
def active(cls, records):
for contract in records:
contract.set_number()
@classmethod
@ModelView.button
@Workflow.transition('finished')
def finished(cls, records):
for contract in records:
contract._check_finish_date()
def _check_finish_date(self):
today = Pool().get('ir.date').today()
if self.end_date and self.end_date > today:
self.raise_user_error('finish_contract_out_date')
def set_number(self):
'''
Fill the reference field with the request sequence
'''
pool = Pool()
Sequence = pool.get('ir.sequence')
Config = pool.get('staff.configuration')
config = Config(1)
if self.number:
return
if not config.staff_contract_sequence:
self.raise_user_error('missing_contract_sequence')
number = Sequence.get_id(config.staff_contract_sequence.id)
self.write([self], {'number': number})
@classmethod
def create(cls, vlist):
vlist = [v.copy() for v in vlist]
for values in vlist:
contracts_current = cls.search([
('employee', '=', values['employee']),
('state', 'in', ('active', 'draft')),
])
if contracts_current:
cls.raise_user_error('employee_with_contract_current',
contracts_current[0].employee.rec_name)
return super(StaffContract, cls).create(vlist)