# 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.model import Unique, DeactivableMixin from trytond.pool import Pool, PoolMeta from trytond.pyson import Eval __all__ = ['Calendar', 'WorkPlace', 'Employee', 'CalendarDay', 'WorkPlaceCalendar'] 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']) country = fields.Many2One('country.country', 'Country') subdivision = fields.Many2One("country.subdivision", 'Subdivision', domain=[('country', '=', Eval('country'))], depends=['country']) city = fields.Char('City') days = fields.One2Many('staff.calendar.day', 'calendar', 'Days', order=[('date_', 'ASC')]) rest_day = fields.Selection([ (None, ''), ('1', 'Monday'), ('2', 'Tuesday'), ('3', 'Wednesday'), ('4', 'Thursday'), ('5', 'Friday'), ('6', 'Saturday'), ('7', 'Sunday')], 'Rest Day', sort=False) 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 self.rest_day and date.isoweekday() == int(self.rest_day): return 'rest' res = calendarDay.search_read([ ('calendar', '=', self), ('date_', '=', date)], fields_names=['day_type'], limit=1) if res: return res[0]['day_type'] return 'work' class WorkPlace(DeactivableMixin, ModelView, ModelSQL): """WorkPlace""" __name__ = 'staff.workplace' 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') def date_type(self, date): for calendar in self.calendars: if calendar.year == date.year: return calendar.date_type(date) return 'work' 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), 'Combination of Workplace and Calendar must be unique.') ] cls._error_messages.update({ 'unique_year': ('Cannot add to Workplace "%s" many calendars ' 'for a year.') }) @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]: cls.raise_user_error('unique_year', record.workplace.rec_name) values[record.workplace.id].append(record.calendar.year) others = cls.search([ ('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]: cls.raise_user_error('unique_year', other.workplace.rec_name) values[record.workplace.id].append(other.calendar.year) class Employee(metaclass=PoolMeta): __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}, depends=['id']) date_ = fields.Date('Date', required=True) 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_), 'Date must be unique per Calendar.') ] cls._order.insert(0, ('date_', 'ASC'))