Files
tryton/work.py
T

217 lines
7.1 KiB
Python
Raw Normal View History

2009-02-28 17:07:12 +01:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from trytond.model import ModelView, ModelSQL, fields
from trytond.wizard import Wizard
2010-01-20 11:05:35 +01:00
from trytond.pyson import PYSONEncoder
2010-08-10 11:59:04 -04:00
from trytond.transaction import Transaction
2008-05-29 17:55:36 +02:00
2009-02-28 17:07:12 +01:00
class Work(ModelSQL, ModelView):
2008-06-09 16:54:33 +02:00
'Work'
_name = 'timesheet.work'
2008-05-29 17:55:36 +02:00
_description = __doc__
name = fields.Char('Name', required=True)
2008-06-02 17:18:11 +02:00
active = fields.Boolean('Active')
2009-06-03 16:32:58 +02:00
parent = fields.Many2One('timesheet.work', 'Parent', left="left",
right="right", select=2, ondelete="RESTRICT")
left = fields.Integer('Left', required=True, select=1)
right = fields.Integer('Right', required=True, select=1)
2009-05-22 19:11:29 +02:00
children = fields.One2Many('timesheet.work', 'parent', 'Children')
2010-03-11 16:55:07 +01:00
hours = fields.Function(fields.Float('Timesheet Hours', digits=(16, 2),
help="Total time spent on this work"), 'get_hours')
2009-06-03 16:33:29 +02:00
timesheet_available = fields.Boolean('Available on timesheets',
help="Allow to fill in timesheets with this work")
2009-05-22 19:11:29 +02:00
company = fields.Many2One('company.company', 'Company', required=True)
2008-06-02 17:18:11 +02:00
def __init__(self):
2008-06-09 16:54:33 +02:00
super(Work, self).__init__()
self._constraints += [
2008-08-05 11:36:49 +02:00
('check_recursion', 'recursive_works'),
2009-05-22 19:11:29 +02:00
('check_parent_company', 'parent_company'),
]
2008-08-05 11:36:49 +02:00
self._error_messages.update({
'recursive_works': 'You can not create recursive works!',
2009-05-22 19:11:29 +02:00
'parent_company': 'Every work must be in the same company '\
'as it\'s parent work!',
2008-08-05 11:36:49 +02:00
})
2010-08-10 11:59:04 -04:00
def default_active(self):
2008-06-02 17:18:11 +02:00
return True
2010-08-10 11:59:04 -04:00
def default_timesheet_available(self):
2009-05-22 19:11:29 +02:00
return True
2008-06-02 17:18:11 +02:00
2010-08-10 11:59:04 -04:00
def default_company(self):
return Transaction().context.get('company') or False
2008-06-09 19:17:07 +02:00
2010-08-10 11:59:04 -04:00
def check_parent_company(self, ids):
for work in self.browse(ids):
2009-05-22 19:11:29 +02:00
if not work.parent:
continue
if work.parent.company.id != work.company.id:
return False
return True
def _tree_qty(self, hours_by_wt, children, ids, to_compute):
2008-06-03 01:57:48 +02:00
res = 0
for h in ids:
2009-05-22 19:11:29 +02:00
if (not children.get(h)) or (not to_compute[h]):
2008-06-03 01:57:48 +02:00
res += hours_by_wt.setdefault(h, 0)
else:
sub_qty = self._tree_qty(
2009-05-22 19:11:29 +02:00
hours_by_wt, children, children[h], to_compute)
2008-06-03 01:57:48 +02:00
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, ids, name):
2010-08-10 11:59:04 -04:00
all_ids = self.search([
('parent', 'child_of', ids),
])
# force inactive ids to be in all_ids
all_ids = list(set(all_ids + ids))
2008-06-09 16:54:33 +02:00
clause = "SELECT work, sum(hours) FROM timesheet_line "\
"WHERE work IN (%s) "\
2009-08-26 15:57:29 +02:00
% ",".join(('%s',) * len(all_ids))
2008-06-03 01:57:48 +02:00
date_cond = ""
args = []
2010-08-10 11:59:04 -04:00
if Transaction().context.get('from_date'):
2008-06-03 15:14:09 +02:00
date_cond = " AND date >= %s"
2010-08-10 11:59:04 -04:00
args.append(Transaction().context['from_date'])
if Transaction().context.get('to_date'):
2008-06-03 15:14:09 +02:00
date_cond += " AND date <= %s"
2010-08-10 11:59:04 -04:00
args.append(Transaction().context['to_date'])
2008-06-09 16:54:33 +02:00
clause += date_cond + " GROUP BY work"
2008-06-03 01:57:48 +02:00
2010-08-10 11:59:04 -04:00
Transaction().cursor.execute(clause, all_ids + args)
2008-06-03 01:57:48 +02:00
2010-08-10 11:59:04 -04:00
hours_by_wt = dict((i[0], i[1]) for i in
Transaction().cursor.fetchall())
2008-06-03 01:57:48 +02:00
to_compute = dict.fromkeys(all_ids, True)
2010-08-10 11:59:04 -04:00
works = self.browse(all_ids)
2009-05-22 19:11:29 +02:00
children = {}
2008-06-09 16:54:33 +02:00
for work in works:
if work.parent:
2009-05-22 19:11:29 +02:00
children.setdefault(work.parent.id, []).append(work.id)
self._tree_qty(hours_by_wt, children, ids, to_compute)
2008-06-03 01:57:48 +02:00
return hours_by_wt
def get_rec_name(self, ids, name):
2008-06-02 17:18:11 +02:00
if not ids:
return {}
res = {}
2008-06-09 16:54:33 +02:00
def _name(work):
if work.parent:
return _name(work.parent) + '\\' + work.name
2008-06-02 17:18:11 +02:00
else:
2008-06-09 16:54:33 +02:00
return work.name
2010-08-10 11:59:04 -04:00
for work in self.browse(ids):
res[work.id] = _name(work)
2008-06-02 17:18:11 +02:00
return res
2010-08-10 11:59:04 -04:00
def write(self, ids, vals):
2008-06-02 17:18:11 +02:00
child_ids = None
if not vals.get('active', True):
2010-08-10 11:59:04 -04:00
child_ids = self.search([
2008-06-02 17:18:11 +02:00
('parent', 'child_of', ids),
2010-08-10 11:59:04 -04:00
])
res = super(Work, self).write(ids, vals)
2008-06-02 17:18:11 +02:00
if child_ids:
2010-08-10 11:59:04 -04:00
self.write(child_ids, {
2008-06-02 17:18:11 +02:00
'active': False,
2010-08-10 11:59:04 -04:00
})
2008-06-02 17:18:11 +02:00
return res
2008-05-29 17:55:36 +02:00
2008-06-09 16:54:33 +02:00
Work()
2008-06-03 01:57:48 +02:00
2009-02-28 17:07:12 +01:00
class OpenWorkInit(ModelView):
'Open Work Init'
2008-06-09 16:54:33 +02:00
_name = 'timesheet.work.open.init'
_description = __doc__
2008-06-03 01:57:48 +02:00
from_date = fields.Date('From Date')
to_date = fields.Date('To Date')
2008-06-09 16:54:33 +02:00
OpenWorkInit()
2008-06-03 01:57:48 +02:00
2008-06-09 16:54:33 +02:00
class OpenWork(Wizard):
'Open Work'
_name = 'timesheet.work.open'
2008-06-03 01:57:48 +02:00
states = {
'init': {
'result': {
'type': 'form',
2008-06-09 16:54:33 +02:00
'object': 'timesheet.work.open.init',
2008-06-03 01:57:48 +02:00
'state': [
2008-06-06 22:50:55 +02:00
('end', 'Cancel', 'tryton-cancel'),
('open', 'Open', 'tryton-ok', True),
2008-06-03 01:57:48 +02:00
],
},
},
'open': {
'result': {
'type': 'action',
2008-06-09 16:54:33 +02:00
'action': '_action_open_work',
2008-06-03 01:57:48 +02:00
'state': 'end',
},
},
}
2010-08-10 11:59:04 -04:00
def _action_open_work(self, data):
2008-06-03 01:57:48 +02:00
model_data_obj = self.pool.get('ir.model.data')
act_window_obj = self.pool.get('ir.action.act_window')
2010-08-10 11:59:04 -04:00
act_window_id = model_data_obj.get_id('timesheet', 'act_work_tree2')
res = act_window_obj.read(act_window_id)
2010-01-20 11:05:35 +01:00
res['pyson_context'] = PYSONEncoder().encode({
2008-06-03 01:57:48 +02:00
'from_date': data['form']['from_date'],
'to_date': data['form']['to_date'],
})
return res
2008-06-09 16:54:33 +02:00
OpenWork()
2008-06-09 16:54:33 +02:00
class OpenWork2(OpenWork):
_name = 'timesheet.work.open2'
2010-08-10 11:59:04 -04:00
def _action_open_work(self, data):
model_data_obj = self.pool.get('ir.model.data')
act_window_obj = self.pool.get('ir.action.act_window')
2010-08-10 11:59:04 -04:00
act_window_id = model_data_obj.get_id('timesheet', 'act_work_form2')
res = act_window_obj.read(act_window_id)
2010-01-20 11:05:35 +01:00
res['pyson_context'] = PYSONEncoder().encode({
'from_date': data['form']['from_date'],
'to_date': data['form']['to_date'],
})
return res
2008-06-09 16:54:33 +02:00
OpenWork2()
2008-06-09 16:54:33 +02:00
class OpenWorkGraph(Wizard):
_name = 'timesheet.work.open_graph'
states = {
'init': {
'result': {
'type': 'action',
2008-06-09 16:54:33 +02:00
'action': '_action_open_work',
'state': 'end',
},
},
}
2010-08-10 11:59:04 -04:00
def _action_open_work(self, data):
model_data_obj = self.pool.get('ir.model.data')
act_window_obj = self.pool.get('ir.action.act_window')
2008-06-09 16:54:33 +02:00
work_obj = self.pool.get('timesheet.work')
2010-08-10 11:59:04 -04:00
act_window_id = model_data_obj.get_id('timesheet', 'act_work_form3')
res = act_window_obj.read(act_window_id)
if 'active_id' in Transaction().context:
work = work_obj.browse(Transaction().context['active_id'])
res['name'] = res['name'] + ' - ' + work.rec_name
return res
2008-06-09 16:54:33 +02:00
OpenWorkGraph()