trytond-staff_workplace/workplace.py
2016-05-19 09:15:31 +02:00

74 lines
2.4 KiB
Python

# The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
__metaclass__ = PoolMeta
__all__ = ['Calendar', 'WorkPlace', 'Employee', 'CalendarDay']
class Calendar(ModelView, ModelSQL):
"""Calendar"""
__name__ = 'staff.calendar'
name = fields.Char('Name')
year = fields.Integer('Year', required=True, states={'readonly': Eval('id', 0) > 0}, depends=['id'])
days = fields.One2Many('staff.calendar.day', 'calendar', 'Days')
rest_day = fields.Selection([('6', 'Saturday'), ('7', 'Sunday')], 'Rest Day'
, states={'readonly': Eval('id', 0) > 0}, depends=['id'])
@classmethod
def __setup__(cls):
super(Calendar, cls).__setup__()
cls._sql_constraints = [('year_uk', 'UNIQUE(year)', 'Year must be unique.')]
def get_rec_name(self, name):
return str(self.name) + ' - ' + str(self.year)
@staticmethod
def default_rest_day():
return '7'
def date_type(self, date):
pool = Pool()
calendarDay = pool.get('staff.calendar.day')
if date.isoweekday() == int(self.rest_day):
return 'rest'
res = calendarDay.search_read([('calendar', '=', self), ('date_', '=', date)], fields_names=['day_type'])
if res:
return res.day_type
return 'work'
class WorkPlace(ModelView, ModelSQL):
"""WorkPlace"""
__name__ = 'staff.workplace'
code = fields.Char('Code', required=True)
name = fields.Char('Name', required=True)
active = fields.Boolean('Active')
calendar = fields.Many2One('staff.calendar', 'Calendar')
def date_type(self, date):
if not self.calendar:
return 'work'
return self.calendar.date_type(date)
class Employee:
__name__ = 'company.employee'
workplace = fields.Many2One('staff.workplace', 'WorkPlace')
class CalendarDay(ModelView, ModelSQL):
"""Calendar Day"""
__name__ = 'staff.calendar.day'
name = fields.Char('Name')
calendar = fields.Many2One('staff.calendar', 'Calendar', required=True,
states={'readonly': Eval('id', 0) > 0})
date_ = fields.Date('Date')
day_type = fields.Selection(
[('holiday', 'holiday'), ('local_holiday', 'Local Holiday'), ('rest', 'Rest')], 'Day Type')