[FIX]: base_calendar : Fixed problem of group by state
[odoo/odoo.git] / addons / base_calendar / wizard / base_calendar_set_exrule.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from base_calendar import base_calendar
23 from osv import fields, osv
24 from tools.translate import _
25 import tools
26 import mx.DateTime
27 import re
28
29
30 months = {
31     1: "January", 2: "February", 3: "March", 4: "April", \
32     5: "May", 6: "June", 7: "July", 8: "August", 9: "September", \
33     10: "October", 11: "November", 12: "December"
34 }
35
36 class base_calendar_set_exrule(osv.osv_memory):
37     """
38     Set Exrule.
39     """
40
41     _name = "base.calendar.set.exrule"
42     _description = "Set Exrule"
43
44     _columns = {
45       'freq': fields.selection([('None', 'No Repeat'), \
46                                 ('secondly', 'Secondly'), \
47                                 ('minutely', 'Minutely'), \
48                                 ('hourly', 'Hourly'), \
49                                 ('daily', 'Daily'), \
50                                 ('weekly', 'Weekly'), \
51                                 ('monthly', 'Monthly'), \
52                                 ('yearly', 'Yearly')], 'Frequency',required=True),
53         'interval': fields.integer('Interval'),
54         'count': fields.integer('Count'),
55         'mo': fields.boolean('Mon'),
56         'tu': fields.boolean('Tue'),
57         'we': fields.boolean('Wed'),
58         'th': fields.boolean('Thu'),
59         'fr': fields.boolean('Fri'),
60         'sa': fields.boolean('Sat'),
61         'su': fields.boolean('Sun'),
62         'select1': fields.selection([('date', 'Date of month'), \
63                                     ('day', 'Day of month')], 'Option'),
64         'day': fields.integer('Date of month'),
65         'week_list': fields.selection([('MO', 'Monday'), ('TU', 'Tuesday'), \
66                                    ('WE', 'Wednesday'), ('TH', 'Thursday'), \
67                                    ('FR', 'Friday'), ('SA', 'Saturday'), \
68                                    ('SU', 'Sunday')], 'Weekday'),
69         'byday': fields.selection([('1', 'First'), ('2', 'Second'), \
70                                    ('3', 'Third'), ('4', 'Fourth'), \
71                                    ('5', 'Fifth'), ('-1', 'Last')], 'By day'),
72         'month_list': fields.selection(months.items(),'Month'),
73         'end_date': fields.date('Repeat Until'),
74
75     }
76
77     def view_init(self, cr, uid, fields, context=None):
78         """
79         This function checks for precondition before wizard executes
80         @param self: The object pointer
81         @param cr: the current row, from the database cursor,
82         @param uid: the current user’s ID for security checks,
83         @param fields: List of fields for default value
84         @param context: A standard dictionary for contextual values
85
86         """
87         crm_obj = self.pool.get('crm.meeting')
88         for meeting in crm_obj.browse(cr, uid, context.get('active_ids', [])):
89             if not meeting.rrule:
90                 raise osv.except_osv(_("Warning !"), _("Please Apply Recurrency after Apply Exception Rule"))
91         return False
92
93     def compute_exrule_string(self, cr, uid, ids, context=None):
94         """
95         Compute rule string.
96         @param self: the object pointer
97         @param cr: the current row, from the database cursor,
98         @param uid: the current user’s ID for security checks,
99         @param datas: dictionary of freq and interval value.
100         @return: string value which compute FREQILY;INTERVAL
101         """
102
103         weekdays = ['mo', 'tu', 'we', 'th', 'fr', 'sa', 'su']
104         weekstring = ''
105         monthstring = ''
106         yearstring = ''
107         ex_id = base_calendar.base_calendar_id2real_id(context['active_id'])
108         model = context.get('model', False)
109         model_obj = self.pool.get(model)
110         for datas in self.read(cr, uid, ids, context=context):
111             freq = datas.get('freq')
112             if freq == 'None':
113                 model_obj.write(cr, uid, ex_id,{'exrule': ''})
114                 return{}
115
116             interval_srting = datas.get('interval') and (';INTERVAL=' + str(datas.get('interval'))) or ''
117
118             if freq == 'weekly':
119
120                 byday = map(lambda x: x.upper(), filter(lambda x: datas.get(x) and x in weekdays, datas))
121                 if byday:
122                     weekstring = ';BYDAY=' + ','.join(byday)
123
124             elif freq == 'monthly':
125                 if datas.get('select1')=='date' and (datas.get('day') < 1 or datas.get('day') > 31):
126                     raise osv.except_osv(_('Error!'), ("Please select proper Day of month"))
127                 if datas.get('select1')=='day':
128                     monthstring = ';BYDAY=' + datas.get('byday') + datas.get('week_list')
129                 elif datas.get('select1')=='date':
130                     monthstring = ';BYMONTHDAY=' + str(datas.get('day'))
131
132             elif freq == 'yearly':
133                 if datas.get('select1')=='date' and (datas.get('day') < 1 or datas.get('day') > 31):
134                     raise osv.except_osv(_('Error!'), ("Please select proper Day of month"))
135                 bymonth = ';BYMONTH=' + str(datas.get('month_list'))
136                 if datas.get('select1')=='day':
137                     bystring = ';BYDAY=' + datas.get('byday') + datas.get('week_list')
138                 elif datas.get('select1')=='date':
139                     bystring = ';BYMONTHDAY=' + str(datas.get('day'))
140                 yearstring = bymonth + bystring
141
142             if datas.get('end_date'):
143                 datas['end_date'] = ''.join((re.compile('\d')).findall(datas.get('end_date'))) + '235959Z'
144             enddate = (datas.get('count') and (';COUNT=' + str(datas.get('count'))) or '') +\
145                                  ((datas.get('end_date') and (';UNTIL=' + datas.get('end_date'))) or '')
146
147             exrule_string = 'FREQ=' + freq.upper() + weekstring + interval_srting \
148                                 + enddate + monthstring + yearstring
149
150             model_obj.write(cr, uid, ex_id,{'exrule': exrule_string})
151             return {}
152
153         _defaults = {
154          'freq': lambda *x: 'None',
155          'select1': lambda *x: 'date',
156          'interval': lambda *x: 1,
157     }
158
159 base_calendar_set_exrule()
160
161 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: