trytond-staff_workplace/workplace.py

186 lines
6.3 KiB
Python
Raw Permalink Normal View History

2016-05-04 10:10:53 +02:00
# The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from sql import Null
2016-05-04 10:10:53 +02:00
from trytond.model import ModelSQL, ModelView, fields
2018-08-01 14:01:00 +02:00
from trytond.model import Unique, DeactivableMixin
2016-05-19 00:18:58 +02:00
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
2022-05-11 14:26:33 +02:00
from trytond.exceptions import UserError
from trytond.i18n import gettext
from trytond.transaction import Transaction
from trytond import backend
2016-05-19 00:18:58 +02:00
class Calendar(ModelView, ModelSQL):
"""Calendar"""
__name__ = 'staff.calendar'
name = fields.Char('Name')
2016-09-28 14:20:47 +02:00
year = fields.Integer('Year', required=True,
2022-09-08 12:33:05 +02:00
states={'readonly': Eval('id', 0) > 0})
country = fields.Many2One('country.country', 'Country')
subdivision = fields.Many2One("country.subdivision",
2022-09-08 12:33:05 +02:00
'Subdivision', domain=[('country', '=', Eval('country'))])
city = fields.Char('City')
days = fields.One2Many('staff.calendar.day', 'calendar', 'Days',
order=[('date_', 'ASC')])
rest_days = fields.MultiSelection([
2017-04-12 08:35:05 +02:00
('1', 'Monday'),
('2', 'Tuesday'),
('3', 'Wednesday'),
('4', 'Thursday'),
('5', 'Friday'),
2016-09-28 14:20:47 +02:00
('6', 'Saturday'),
('7', 'Sunday')], 'Rest Days', sort=False)
2016-05-19 00:18:58 +02:00
@classmethod
def __register__(cls, module_name):
exist = backend.TableHandler.table_exist(cls._table)
table_sql = cls.__table_handler__(module_name)
field_exist = table_sql.column_exist('rest_days')
super().__register__(module_name)
table = cls.__table__()
if exist and not field_exist:
cursor = Transaction().connection.cursor()
cursor.execute(*table.select(
table.id,
table.rest_day,
where=(table.rest_day != Null)))
for id_, rest_day in cursor.fetchall():
value = cls.rest_days.sql_format([rest_day])
cursor.execute(*table.update(
columns=[table.rest_days],
values=[value],
where=table.id == id_))
table_sql.drop_column('rest_day')
2016-05-19 00:18:58 +02:00
@staticmethod
def default_rest_days():
return ['7']
def get_rec_name(self, name):
return str(self.name) + ' - ' + str(self.year)
2016-05-19 00:18:58 +02:00
def date_type(self, date):
pool = Pool()
calendarDay = pool.get('staff.calendar.day')
if self.rest_days and date.isoweekday() in self.rest_days_values:
2016-05-19 00:18:58 +02:00
return 'rest'
2016-09-28 14:20:47 +02:00
res = calendarDay.search_read([
('calendar', '=', self),
('date_', '=', date)], fields_names=['day_type'], limit=1)
2016-05-19 00:18:58 +02:00
if res:
2016-09-28 14:20:47 +02:00
return res[0]['day_type']
2016-05-19 00:18:58 +02:00
return 'work'
2016-05-04 10:10:53 +02:00
@property
def rest_days_values(self):
return set(int(day) for day in self.rest_days)
2016-05-04 10:10:53 +02:00
2018-08-01 14:01:00 +02:00
class WorkPlace(DeactivableMixin, ModelView, ModelSQL):
2016-05-04 10:10:53 +02:00
"""WorkPlace"""
__name__ = 'staff.workplace'
2016-05-19 00:18:58 +02:00
code = fields.Char('Code', required=True)
name = fields.Char('Name', required=True)
calendars = fields.Many2Many('staff.workplace.calendar', 'workplace',
'calendar', 'Calendars')
calendar = fields.Function(fields.Many2One('staff.calendar', 'Calendar'),
'get_calendar')
2016-05-19 00:18:58 +02:00
def date_type(self, date):
for calendar in self.calendars:
if calendar.year == date.year:
return calendar.date_type(date)
return 'work'
2016-05-04 10:10:53 +02:00
def get_calendar(self, name=None):
pool = Pool()
Date = pool.get('ir.date')
cal = self._get_calendar(Date.today().year)
return cal.id if cal else None
def _get_calendar(self, year):
for calendar in self.calendars:
if calendar.year == year:
return calendar
return None
class WorkPlaceCalendar(ModelSQL):
"""WorkPlace - Calendar"""
__name__ = 'staff.workplace.calendar'
workplace = fields.Many2One('staff.workplace', 'WorkPlace', required=True)
calendar = fields.Many2One('staff.calendar', 'Calendar', required=True)
@classmethod
def __setup__(cls):
super(WorkPlaceCalendar, cls).__setup__()
t = cls.__table__()
cls._sql_constraints = [
('wp_cal_uk', Unique(t, t.workplace, t.calendar),
2022-05-11 14:26:33 +02:00
'staff_workplace.msg_staff_workplace_calendar_wp_cal_uk')
]
@classmethod
def validate(cls, records):
super(WorkPlaceCalendar, cls).validate(records)
cls.check_validation(records)
@classmethod
def check_validation(cls, records):
values = {}
for record in records:
values.setdefault(record.workplace.id, [])
if record.calendar.year in values[record.workplace.id]:
2022-05-11 14:26:33 +02:00
raise UserError(gettext(
'staff_workplace.msg_staff_workplace_calendar_unique_year',
workplace=record.workplace.rec_name))
values[record.workplace.id].append(record.calendar.year)
others = cls.search([
2019-02-28 13:56:37 +01:00
('id', 'not in', list(map(int, records))),
('workplace', 'in', [r.workplace.id for r in records])])
for other in others:
if other.calendar.year in values[other.workplace.id]:
2022-05-11 14:26:33 +02:00
raise UserError(gettext(
'staff_workplace.msg_staff_workplace_calendar_unique_year',
workplace=other.workplace.rec_name))
values[record.workplace.id].append(other.calendar.year)
2016-05-04 10:10:53 +02:00
2019-02-28 13:56:37 +01:00
class Employee(metaclass=PoolMeta):
2016-05-04 10:10:53 +02:00
__name__ = 'company.employee'
workplace = fields.Many2One('staff.workplace', 'WorkPlace')
2016-05-19 00:18:58 +02:00
class CalendarDay(ModelView, ModelSQL):
"""Calendar Day"""
__name__ = 'staff.calendar.day'
2016-09-28 14:20:47 +02:00
2016-05-19 00:18:58 +02:00
name = fields.Char('Name')
calendar = fields.Many2One('staff.calendar', 'Calendar', required=True,
2022-09-08 12:33:05 +02:00
states={'readonly': Eval('id', 0) > 0})
date_ = fields.Date('Date', required=True)
2016-09-28 14:20:47 +02:00
day_type = fields.Selection([
('holiday', 'holiday'),
('local_holiday', 'Local Holiday'),
('rest', 'Rest')], 'Day Type', required=True)
@classmethod
def __setup__(cls):
super(CalendarDay, cls).__setup__()
t = cls.__table__()
cls._sql_constraints = [
('calendar_date_uk', Unique(t, t.calendar, t.date_),
2022-05-11 14:26:33 +02:00
'staff_workplace.msg_staff_calendar_day_calendar_date_uk')
]
cls._order.insert(0, ('date_', 'ASC'))