trytonpsk-staff/contract.py

196 lines
6.4 KiB
Python
Raw Permalink 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
2021-06-02 19:26:32 +02:00
from trytond.i18n import gettext
from trytond.model.exceptions import AccessError
2020-04-16 00:27:15 +02:00
from trytond.transaction import Transaction
2021-06-02 19:26:32 +02:00
from .exceptions import (StaffContractError)
2021-07-13 22:35:06 +02:00
from trytond import backend
from trytond.tools.multivalue import migrate_property
2020-04-16 00:27:15 +02:00
2020-06-13 01:47:21 +02:00
STATES = {'readonly': (Eval('state') != 'draft')}
2020-04-16 00:27:15 +02:00
2021-07-13 22:35:06 +02:00
PAYMENT_TERM = [
('', ''),
('1', 'Cash')
]
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',
2021-11-05 23:44:31 +01:00
select=True, required=True, states=STATES)
2020-04-16 00:27:15 +02:00
company = fields.Many2One('company.company', 'Company', required=True,
2021-11-05 23:44:31 +01:00
select=True, states=STATES)
2020-04-16 00:27:15 +02:00
contract_date = fields.Date('Contract Date', required=True,
2021-11-05 23:44:31 +01:00
states=STATES)
2020-04-16 00:27:15 +02:00
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'])
2023-10-26 22:48:59 +02:00
canceled_date = fields.Date('Canceled Date', states={
'required': Eval('state') == 'canceled',
}, depends=['state'])
2020-04-16 00:27:15 +02:00
salary = fields.Numeric('Salary', digits=(16, 2), states=STATES,
2021-11-05 23:44:31 +01:00
help='Salary monthly of person', required=True)
2020-04-16 00:27:15 +02:00
kind = fields.Selection([
('steady', 'Steady'),
('indefinite', 'Indefinite'),
('job', 'Job'),
2021-04-12 21:09:51 +02:00
('learning', 'learning'),
('internships', 'internships')
2020-04-16 00:27:15 +02:00
], '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'),
2023-10-26 22:48:59 +02:00
('canceled', 'Canceled'),
2020-06-13 01:47:21 +02:00
], 'State', readonly=True, select=True)
2021-11-05 23:44:31 +01:00
payment_term = fields.Selection(
PAYMENT_TERM, 'Payment Term', states=STATES)
2020-04-16 00:27:15 +02:00
comment = fields.Text("Comment", states=STATES)
position = fields.Many2One('staff.position', 'Position',
2021-11-05 23:44:31 +01:00
select=True)
2020-04-16 00:27:15 +02:00
@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'),
2023-10-26 22:48:59 +02:00
('active', 'canceled'),
2020-06-13 01:47:21 +02:00
('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',
},
2023-10-26 22:48:59 +02:00
'canceled': {
'invisible': Eval('state') != 'active',
},
2020-06-13 01:47:21 +02:00
})
2020-04-16 00:27:15 +02:00
2021-07-13 22:35:06 +02:00
@classmethod
def __register__(cls, module_name):
exist = backend.TableHandler.table_exist(cls._table)
super(StaffContract, cls).__register__(module_name)
if not exist:
cls._migrate_property([], [], [])
@classmethod
def _migrate_property(cls, field_names, value_names, fields):
field_names.append('payment_term')
migrate_property(
'staff.contract', field_names, cls, value_names,
fields=fields)
2020-04-16 00:27:15 +02:00
@staticmethod
def default_company():
return Transaction().context.get('company')
@staticmethod
def default_state():
return 'draft'
@staticmethod
def default_kind():
return 'job'
2021-07-13 22:35:06 +02:00
@staticmethod
def default_payment_term():
return '1'
2020-04-16 00:27:15 +02:00
@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,
2021-11-05 23:44:31 +01:00
('number',) + tuple(clause[1:]),
('employee',) + tuple(clause[1:]),
]
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()
2023-10-26 22:48:59 +02:00
@classmethod
@ModelView.button
@Workflow.transition('canceled')
def canceled(cls, records):
for contract in records:
if not contract.canceled_date:
raise StaffContractError(gettext('staff.msg_missing_canceled_date'))
2020-04-16 00:27:15 +02:00
def _check_finish_date(self):
2021-11-05 23:44:31 +01:00
pass
2020-04-16 00:27:15 +02:00
def set_number(self):
'''
Fill the reference field with the request sequence
'''
pool = Pool()
Config = pool.get('staff.configuration')
config = Config(1)
if self.number:
return
if not config.staff_contract_sequence:
2021-11-05 23:44:31 +01:00
raise StaffContractError(gettext('staff.msg_missing_contract_sequence'))
2021-06-02 19:26:32 +02:00
number = config.staff_contract_sequence.get()
2020-04-16 00:27:15 +02:00
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:
2021-11-05 23:44:31 +01:00
raise AccessError(gettext('staff.msg_employee_with_contract_current',
contract=contracts_current[0].employee.rec_name))
2020-04-16 00:27:15 +02:00
return super(StaffContract, cls).create(vlist)
2023-05-26 05:55:01 +02:00
2022-11-15 15:46:02 +01:00
@classmethod
def delete(cls, records):
for contract in records:
if contract.number:
raise AccessError(gettext('staff.msg_dont_delete_contract',
contract=contracts_current[0].employee.rec_name))
return super(StaffContract, cls).delete(records)