# 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 from sql import 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'), ('learning', 'learning'), ('internships', 'internships') ], '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) # pool = Pool() # Employee = pool.get('company.employee') # cursor = Transaction().connection.cursor() # sql_table = cls.__table__() # employee = Employee.__table__() # # # Migration: # # - Migration position from employee into contract # sub_query = employee.select( # employee.id, employee.position, # where=(employee.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)