diff --git a/work_type.py b/work_type.py index e6810935e4..e7af891985 100644 --- a/work_type.py +++ b/work_type.py @@ -15,6 +15,7 @@ class WorkType(OSV): active = fields.Boolean('Active') parent = fields.Many2One('timesheet.work_type', 'Parent', select=2) childs = fields.One2Many('timesheet.work_type', 'parent', 'Childs') + hours = fields.Function('get_hours', digits=(16, 2), string='Hours') type = fields.Selection([ ('view', 'View'), ('normal', 'Normal'), @@ -37,6 +38,45 @@ class WorkType(OSV): res = self.name_get(cursor, user, ids, context=context) return dict(res) + + def _tree_qty(self, hours_by_wt, childs, ids, to_compute): + res = 0 + for h in ids: + if (not childs.get(h)) or (not to_compute[h]): + res += hours_by_wt.setdefault(h, 0) + else: + sub_qty = self._tree_qty( + hours_by_wt, childs, childs[h], to_compute) + hours_by_wt.setdefault(h, 0) + hours_by_wt[h] += sub_qty + res += hours_by_wt[h] + to_compute[h] = False + return res + + def get_hours(self, cursor, user, ids, name, arg, context=None): + all_ids = self.search(cursor, user, [('parent', 'child_of', ids)]) + clause = "SELECT work_type, sum(hours) FROM timesheet_line "\ + "WHERE work_type IN (%s) "\ + % ",".join(["%s" for id in all_ids]) + date_cond = "" + if context.get('from_date'): + date_cond = " AND date >= '%s'"% str(context.get('from_date')) + if context.get('to_date'): + date_cond += " AND date <= '%s'"% str(context.get('to_date')) + clause += date_cond + " GROUP BY work_type" + + cursor.execute(clause, all_ids) + + hours_by_wt = dict([(i[0], i[1]) for i in cursor.fetchall()]) + to_compute = dict.fromkeys(all_ids, True) + work_types = self.browse(cursor, user, all_ids, context=context) + childs = {} + for work_type in work_types: + if work_type.parent: + childs.setdefault(work_type.parent.id, []).append(work_type.id) + self._tree_qty(hours_by_wt, childs, ids, to_compute) + return hours_by_wt + def name_get(self, cursor, user, ids, context=None): if not ids: return [] @@ -67,3 +107,55 @@ class WorkType(OSV): return res WorkType() + + + + +class OpenWorkTypeInit(WizardOSV): + _name = 'timesheet.work_type.open.init' + from_date = fields.Date('From Date') + to_date = fields.Date('To Date') +OpenWorkTypeInit() + + +class OpenWorkType(Wizard): + 'Open Work Types' + _name = 'timesheet.work_type.open' + states = { + 'init': { + 'result': { + 'type': 'form', + 'object': 'timesheet.work_type.open.init', + 'state': [ + ('end', 'Cancel', 'gtk-cancel'), + ('open', 'Open', 'gtk-ok', True), + ], + }, + }, + 'open': { + 'result': { + 'type': 'action', + 'action': '_action_open_work_type', + 'state': 'end', + }, + }, + } + + def _action_open_work_type(self, cursor, user, data, context=None): + model_data_obj = self.pool.get('ir.model.data') + act_window_obj = self.pool.get('ir.action.act_window') + + model_data_ids = model_data_obj.search(cursor, user, [ + ('fs_id', '=', 'act_work_type_tree2'), + ('module', '=', 'timesheet'), + ], limit=1, context=context) + model_data = model_data_obj.browse(cursor, user, model_data_ids[0], + context=context) + res = act_window_obj.read(cursor, user, model_data.db_id, context=context) + res['context'] = str({ + 'from_date': data['form']['from_date'], + 'to_date': data['form']['to_date'], + }) + return res + +OpenWorkType() diff --git a/work_type.xml b/work_type.xml index 4ca1390bd2..3a2bfe29ca 100644 --- a/work_type.xml +++ b/work_type.xml @@ -80,6 +80,41 @@ + + + timesheet.work_type + tree + + childs + + + + + + + ]]> + + + + Work Types + timesheet.work_type + tree + [('parent', '=', False)] + + + + + + + + Open Work Types + timesheet.work_type.open + + +