trytonpsk-staff/contract.py

183 lines
6.3 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 datetime import datetime
from trytond.model import ModelView, Workflow, ModelSQL, fields
from trytond.pyson import Eval
from trytond.pool import Pool
from trytond.transaction import Transaction
from trytond import backend
from sql import Table, Null
__all__ = ['StaffContract']
STATES = {'readonly': (Eval('state') != 'draft'),}
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([
('draft', 'Draft'),
('active', 'Active'),
('finished', 'Finished'),
], 'State', readonly=True, select=True)
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((
('draft', 'active'),
('active', 'draft'),
('active', 'finished'),
))
cls._buttons.update({
'draft': {
'invisible': Eval('state') != 'active',
},
'active': {
'invisible': Eval('state') != 'draft',
},
'finished': {
'invisible': Eval('state') != 'active',
},
})
cls._error_messages.update({
'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!'),
}
)
@classmethod
def __register__(cls, module_name):
super(StaffContract, cls).__register__(module_name)
TableHandler = backend.get('TableHandler')
pool = Pool()
Identifier = pool.get('company.employee')
cursor = Transaction().connection.cursor()
sql_table = cls.__table__()
identifier = Identifier.__table__()
# Migration:
# - Migration position from employee into contract
sub_query = identifier.select(
identifier.id, identifier.position,
where=(identifier.position != Null)
)
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:]),
]
@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)