~credativ/credativ-openerp/addons-6.1

« back to all changes in this revision

Viewing changes to public_holidays/public_holiday.py

  • Committer: Sofia Anwar
  • Date: 2012-09-10 10:17:05 UTC
  • mto: This revision was merged to the branch mainline in revision 65.
  • Revision ID: sofia.anwar@credativ.in-20120910101705-a1ghm84x8rgef02e
[ADD] public_holidays (ref: redmine ticket 2773)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import time
 
2
from datetime import datetime
 
3
import calendar
 
4
 
 
5
from osv import fields, osv
 
6
from tools.translate import _
 
7
 
 
8
class res_weekdays(osv.osv):
 
9
    _name = 'res.weekdays'
 
10
    _columns = {
 
11
        'name': fields.selection([('Monday','Monday'),('Tuesday','Tuesday'),('Wednesday','Wednesday'),('Thursday','Thursday'),('Friday','Friday'),('Saturday','Saturday'),('Sunday','Sunday')], 'Day'),
 
12
    }
 
13
    
 
14
res_weekdays()
 
15
 
 
16
class res_country(osv.osv):
 
17
    _inherit = 'res.country'
 
18
    _columns = {
 
19
        'weekend_ids': fields.many2many('res.weekdays', 'rel_weekdays_country', 'country_id', 'week_id', 'Weekends'),
 
20
    }
 
21
    
 
22
res_country()
 
23
 
 
24
class hr_public_holiday_rule(osv.osv):
 
25
    _name = 'hr.public.holiday.rule'
 
26
    _columns = {
 
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'),
 
36
    }
 
37
 
 
38
    _defaults = {
 
39
         'active': lambda *a : 1
 
40
    }
 
41
 
 
42
    def check_weekend(self, cr, uid, rule, date1, country, weekend):
 
43
        actual_date = date1
 
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)])
 
48
        if srch_hol:
 
49
            actual_date = self.check_weekend(cr, uid, rule, actual_date)
 
50
        return actual_date
 
51
                
 
52
    def _fetch_holidays(self, cr, uid, ids=False, context=None):
 
53
        print "ir cron job called"
 
54
        if not ids:
 
55
            ids = self.search(cr, uid, [])
 
56
        return self.fetch_holiday(cr, uid, ids, context=context)
 
57
 
 
58
    def _fetch_holiday(self, cr, uid, ids, context=None):
 
59
        if context is None:
 
60
            context = {}
 
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)])
 
69
            if not rec_holidays:
 
70
                if rule.is_recurring:
 
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:
 
74
                                year += 1
 
75
                            c = calendar.monthcalendar(int(year), int(r.recurring_month))
 
76
                            if rule.recurring_day.name == 'Monday':
 
77
                                cc = calendar.MONDAY
 
78
                            elif rule.recurring_day.name == 'Tuesday':
 
79
                                cc = calendar.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':
 
85
                                cc = calendar.FRIDAY
 
86
                            elif rule.recurring_day.name == 'Saturday':
 
87
                                cc = calendar.SATURDAY
 
88
                            elif rule.recurring_day.name == 'Sunday':
 
89
                                cc = calendar.SUNDAY
 
90
                            d1 = c[int(r.recurring_week)][cc]
 
91
                            #to skip the weeks with 0 dates
 
92
                            if d1 == 0:
 
93
                                if r.recurring_week == -1:
 
94
                                    d1 = c[int(r.recurring_week)-1][cc]
 
95
                                else:
 
96
                                    d1 = c[int(r.recurring_week)+1][cc]
 
97
                            if d1 < 10:
 
98
                                d1 = '0'+str(d1)
 
99
                            date1 = str(year)+'-'+str(r.recurring_month)+'-'+str(d1)
 
100
                            actual_date = self.check_weekend(rule,date1, country.id, country_weekend.id)
 
101
                            if not srch_hol:
 
102
                                vals = {
 
103
                                    'effective_date': date1,
 
104
                                    'actual_date': actual_date,
 
105
                                    'previous_holiday': False,
 
106
                                    'next_holiday': False,
 
107
                                    'rule_id': rule.id,
 
108
                                    'country_id': country.id
 
109
                                }
 
110
                                self.pool.get('hr.public.holiday').create(cr, uid, vals)
 
111
 
 
112
        return True
 
113
            
 
114
hr_public_holiday_rule()
 
115
 
 
116
class hr_public_holiday(osv.osv):
 
117
    _name = 'hr.public.holiday'
 
118
    _rec_name = 'effective_date'
 
119
    _columns = {
 
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')
 
126
    }
 
127
    
 
128
hr_public_holiday()
 
 
b'\\ No newline at end of file'