2
from datetime import datetime
5
from osv import fields, osv
6
from tools.translate import _
8
class res_weekdays(osv.osv):
11
'name': fields.selection([('Monday','Monday'),('Tuesday','Tuesday'),('Wednesday','Wednesday'),('Thursday','Thursday'),('Friday','Friday'),('Saturday','Saturday'),('Sunday','Sunday')], 'Day'),
16
class res_country(osv.osv):
17
_inherit = 'res.country'
19
'weekend_ids': fields.many2many('res.weekdays', 'rel_weekdays_country', 'country_id', 'week_id', 'Weekends'),
24
class hr_public_holiday_rule(osv.osv):
25
_name = 'hr.public.holiday.rule'
27
'name': fields.char('Name', size=128),
28
'holiday_date': fields.date('Date'),
29
'is_recurring': fields.boolean('Recurring'),
30
'recurring_week': fields.selection([('0','First'),('1','Second'),('2','Third'),('3','Fourth'),('-1','Last')], 'Week'),
31
'recurring_day': fields.many2one('res.weekdays', 'Day'),
32
# 'recurring_day': fields.selection([('MONDAY','Monday'),('TUESDAY','Tuesday'),('WEDNESDAY','Wednesday'),('THURSDAY','Thursday'),('FRIDAY','Friday'),('SATURDAY','Saturday'),('SUNDAY','Sunday')], 'Day'),
33
'recurring_month': fields.selection([('01','January'),('02','February'),('03','March'),('04','April'),('05','May'),('06','June'),('07','July'),('08','August'),('09','September'),('10','October'),('11','November'),('12','December')], 'Month'),
34
'active': fields.boolean('Active'),
35
'country_ids': fields.many2many('res.country', 'country_holiday_rel', 'holiday_id', 'country_id', 'Countries'),
39
'active': lambda *a : 1
42
def check_weekend(self, cr, uid, rule, date1, country, weekend):
44
if rule.recurring_day.id == weekend:
45
dd = datetime.strptime(start_dt, '%Y-%m-%d') + datetime.timedelta(days=1)
46
actual_date = self.check_weekend(cr, uid, rule, dd)
47
srch_hol = self.pool.get('hr.public.holiday').search(cr, uid, [('actual_date', '=', actual_date),('country_id','=',country)])
49
actual_date = self.check_weekend(cr, uid, rule, actual_date)
52
def _fetch_holidays(self, cr, uid, ids=False, context=None):
53
print "ir cron job called"
55
ids = self.search(cr, uid, [])
56
return self.fetch_holiday(cr, uid, ids, context=context)
58
def _fetch_holiday(self, cr, uid, ids, context=None):
61
for rule in self.browse(cr, uid, ids, context=context):
62
#get all the holidays between now and now +1 year
63
start_dt = time.strftime('%Y-%m-%d')
64
date2 = datetime.strptime(start_dt, '%Y-%m-%d') + relativedelta(years=1)
65
end_dt = datetime.strftime(date2, '%Y-%m-%d')
66
dt_time_date = datetime.strptime(start_dt, '%Y-%m-%d')
67
year = dt_time_date.year
68
rec_holidays = self.pool.get('hr.public.holiday').search(cr, uid, [('actual_date','>=',start_dt),('actual_date','<=',end_dt),('rule_id','=',rule.id)])
71
for country in rule.country_ids:
72
for country_weekend in country.weekend_ids:
73
if not int(r.recurring_month) >= dt_time_date.month:
75
c = calendar.monthcalendar(int(year), int(r.recurring_month))
76
if rule.recurring_day.name == 'Monday':
78
elif rule.recurring_day.name == 'Tuesday':
80
elif rule.recurring_day.name == 'Wednesday':
81
cc = calendar.WEDNESDAY
82
elif rule.recurring_day.name == 'Thursday':
83
cc = calendar.THURSDAY
84
elif rule.recurring_day.name == 'Friday':
86
elif rule.recurring_day.name == 'Saturday':
87
cc = calendar.SATURDAY
88
elif rule.recurring_day.name == 'Sunday':
90
d1 = c[int(r.recurring_week)][cc]
91
#to skip the weeks with 0 dates
93
if r.recurring_week == -1:
94
d1 = c[int(r.recurring_week)-1][cc]
96
d1 = c[int(r.recurring_week)+1][cc]
99
date1 = str(year)+'-'+str(r.recurring_month)+'-'+str(d1)
100
actual_date = self.check_weekend(rule,date1, country.id, country_weekend.id)
103
'effective_date': date1,
104
'actual_date': actual_date,
105
'previous_holiday': False,
106
'next_holiday': False,
108
'country_id': country.id
110
self.pool.get('hr.public.holiday').create(cr, uid, vals)
114
hr_public_holiday_rule()
116
class hr_public_holiday(osv.osv):
117
_name = 'hr.public.holiday'
118
_rec_name = 'effective_date'
120
'effective_date': fields.date('Effective Date'),
121
'actual_date': fields.date('Actual Date'),
122
'previous_holiday': fields.date('Previous Holiday'),
123
'next_holiday': fields.date('Next Holiday'),
124
'rule_id': fields.many2one('hr.public.holiday.rule', 'Holiday Rule', help="To know it was auto-generated or manually entered holiday"),
125
'country_id': fields.many2one('res.country', 'Country')
b'\\ No newline at end of file'