1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
24
from osv import fields, osv
26
class cci_timesheet_grant(osv.osv):
28
_name="cci_timesheet.grant"
29
_description="CCI Timesheet Grant"
31
'name': fields.char('Grant Name', size=128, required=True),
32
'line_ids': fields.one2many('cci_timesheet.line', 'grant_id', 'Timesheet Lines',),
33
'affectation_ids': fields.one2many('cci_timesheet.affectation', 'grant_id', 'Affectation Lines',),
37
class cci_timesheet(osv.osv):
40
_description="CCI Timesheet"
43
'name': fields.char('Name', size=128, required=True,readonly=True,states={'draft':[('readonly',False)]}),
44
'date_from' : fields.date('From Date', required=True,readonly=False,states={'validated':[('readonly',True)],'cancelled':[('readonly',True)]}),
45
'date_to' : fields.date('To Date', required=True,readonly=False,states={'validated':[('readonly',True)],'cancelled':[('readonly',True)]}),
46
'grant_id': fields.many2one('cci_timesheet.grant', 'Grant',readonly=True, required=True,states={'draft':[('readonly',False)]}),
47
'state': fields.selection([('draft', 'Draft'), ('confirmed', 'Confirmed'), ('validated', 'Validated'),('cancelled', 'Cancelled')], 'State', readonly=True, required=True),
48
'sending_date' : fields.date('Sending Date'),
49
'asked_amount' : fields.float('Asked Amount',digits=(16,2)),
50
'accepted_amount' : fields.float('Accepted Amount',digits=(16,2)),
51
'line_ids': fields.one2many('cci_timesheet.line', 'timesheet_id', 'Timesheet Lines',readonly=False,states={'validated':[('readonly',True)],'cancelled':[('readonly',True)]}),
54
'state' : lambda *a: 'draft',
57
def check_timesheet_line(self, cr, uid, ids):
60
for data in self.browse(cr, uid, ids):
61
for lines in data.line_ids:
62
if not lines.grant_id:
64
if (data.grant_id.id != lines.grant_id.id) or lines.day_date < data.date_from or lines.day_date > data.date_to:
68
def set_to_draft(self, cr, uid, ids, *args):
69
self.write(cr, uid, ids, {'state':'draft'})
72
def set_to_validate(self, cr, uid, ids, *args):
73
self.write(cr, uid, ids, {'state':'validated'})
77
def set_to_confirm(self, cr, uid, ids, *args):
78
self.write(cr, uid, ids, {'state':'confirmed'})
81
def set_to_cancel(self, cr, uid, ids, *args):
82
self.write(cr, uid, ids, {'state':'cancelled'})
85
_constraints = [(check_timesheet_line, 'Warning: Date of Timesheet Line should be bewteen the Two dates of the Timesheet and Grant of Timesheet Line should be the same as the Timesheet', ['Timesheet lines'])]
89
class cci_timesheet_line(osv.osv):
92
def _get_diff_hours(self, cr, uid, ids, name, args, context=None):
94
for line in self.browse(cr, uid, ids, context):
95
res[line.id] = line.hour_to - line.hour_from
98
def check_timesheet(self, cr, uid, ids):
101
for lines in self.browse(cr, uid, ids):
102
if lines.timesheet_id:
103
if not lines.grant_id:
105
if (lines.timesheet_id.grant_id.id != lines.grant_id.id) or lines.day_date < lines.timesheet_id.date_from or lines.day_date > lines.timesheet_id.date_to:
109
#TODO: by default, the grant_id should be the grant defined on the timesheet
111
_name="cci_timesheet.line"
112
_description="CCI Timesheet Line"
115
'name': fields.char('Name', size=128, required=True),
116
'day_date' : fields.date('Date of the Day', required=True),
117
'hour_from' : fields.float('Hour From', required=True),
118
'hour_to' : fields.float('Hour To',required=True),
119
'user_id': fields.many2one('res.users', 'User', required=True),
120
'grant_id': fields.many2one('cci_timesheet.grant', 'Grant',),
121
'timesheet_id': fields.many2one('cci.timesheet', 'Timesheet', ondelete='cascade'),
122
'zip_id': fields.many2one('res.partner.zip', 'Zip'),
123
'partner_id': fields.many2one('res.partner', 'Partner'),
124
'contact_id': fields.many2one('res.partner.contact', 'Contact'),
125
'description': fields.text('Description'),
126
'suppl_cost' : fields.float('Supplementary Cost',digits=(16,2)),
127
'kms' : fields.integer('Kilometers'),
128
'diff_hours' : fields.function(_get_diff_hours, method=True, string='Hour To - Hour From',type='float'),
130
_constraints = [(check_timesheet, 'Warning: Date of Timesheet Line should be bewteen the Two dates of the Timesheet and Grant of Timesheet Line should be the same as the Timesheet', ['Timesheet lines'])]
133
class cci_timesheet_affectation(osv.osv):
134
_name="cci_timesheet.affectation"
135
_description="Timesheet Affectation"
138
'name': fields.char('Name', size=128, required=True),
139
'user_id': fields.many2one('res.users', 'User', required=True),
140
'grant_id': fields.many2one('cci_timesheet.grant', 'Grant', required=True),
141
'percentage' : fields.float('Percentage', digits=(16,2),required=True),
142
'hours_per_week' : fields.float('Hours Per Week',size=9, required=True),
143
'date_from' : fields.date('From Date', required=True),
144
'date_to' : fields.date('To Date', required=True),
145
'rate' : fields.float('Rate', digits=(16,2), required=True),
148
cci_timesheet_affectation()
151
class report_timesheet_affectation(osv.osv):
152
_name = "report.timesheet.affectation"
153
_description = "Report on Timesheet and Affectation"
156
'name': fields.char('Name', size=128),
157
'day_date' : fields.date('Date of the Day'),
158
'hour_from' : fields.float('Hour From'),
159
'hour_to' : fields.float('Hour To'),
160
'user_name': fields.char('Employee', size=32),
161
'grant_name': fields.char('Grant', size=128),
162
'timesheet_id': fields.integer('Timesheet Ref'),
163
'description': fields.text('Description'),
164
'diff_hours' : fields.float('Hours'),
166
'affectation_name' : fields.char('Affectation', size=128),
167
'th_percentage' : fields.float('Percentage'),
168
'hours_per_week' : fields.float('Hours Per Week'),
169
'date_from' : fields.date('From Date'),
170
'date_to' : fields.date('To Date'),
171
'rate' : fields.float('Rate'),
176
create or replace view report_timesheet_affectation as (
180
line.day_date as day_date,
181
line.hour_from as hour_from,
182
line.hour_to as hour_to,
184
g.name as grant_name,
185
line.timesheet_id as timesheet_id,
186
line.description as description,
187
(line.hour_to - line.hour_from) as diff_hours,
188
affect.name as affectation_name,
189
affect.percentage as th_percentage,
190
affect.hours_per_week as hours_per_week,
191
affect.date_from as date_from,
192
affect.date_to as date_to,
195
cci_timesheet_line line,
196
cci_timesheet_affectation affect,
198
cci_timesheet_grant g
200
line.user_id = affect.user_id
201
AND line.user_id = u.id
202
AND line.grant_id = affect.grant_id
203
AND line.grant_id = g.id
204
AND (line.day_date <= affect.date_to AND line.day_date >= affect.date_from)
207
report_timesheet_affectation()
209
class crm_case(osv.osv):
211
_inherit = 'crm.case'
212
_description = 'crm case'
214
'grant_id' : fields.many2one('cci_timesheet.grant','Grant'),
215
'zip_id' : fields.many2one('res.partner.zip','Zip'),
216
'timesheet_line_id':fields.many2one('cci_timesheet.line','Timesheet Line')
219
def onchange_partner_id(self, cr, uid, ids, part, email=False):
220
data = super(crm_case, self).onchange_partner_id(cr, uid, ids, part, email)
223
addr = self.pool.get('res.partner').address_get(cr, uid, [part])
225
data['value']['zip_id'] = self.pool.get('res.partner.address').browse(cr, uid, addr['default']).zip_id.id
229
class project_work(osv.osv):
230
_inherit = "project.task.work"
231
_description = "Task Work"
233
def create(self, cr, uid, vals, *args, **kwargs):
234
res = super(project_work, self).create(cr, uid, vals)
235
self.write(cr, uid, [res], vals)
238
def write(self, cr, uid, ids, vals, *args, **kwargs):
242
for work in self.browse(cr, uid, ids):
243
if (not work.zip_id) and ('zip_id' not in vals or not vals['zip_id']):
244
if work.task_id.project_id.partner_id:
245
temp = self.pool.get('res.partner').address_get(cr, uid, [work.task_id.project_id.partner_id.id])
246
vals['zip_id'] = self.pool.get('res.partner.address').browse(cr, uid, temp['default']).zip_id.id
247
if (not work.partner_id) and ('partner_id' not in vals or not vals['partner_id']):
248
if work.task_id.project_id.partner_id:
249
vals['partner_id'] = work.task_id.project_id.partner_id.id
251
if (not work.contact_id) and ('contact_id' not in vals or not vals['contact_id']):
252
if work.task_id.project_id.contact_id2:
253
vals['contact_id'] = work.task_id.project_id.contact_id2.id
254
return super(project_work, self).write(cr, uid, ids, vals)
258
'grant_id' : fields.many2one('cci_timesheet.grant','Grant'),
259
'zip_id' : fields.many2one('res.partner.zip','Zip'),
260
'partner_id' : fields.many2one('res.partner','Partner'),
261
'contact_id' : fields.many2one('res.partner.contact','Contact'),
262
'timesheet_line_id':fields.many2one('cci_timesheet.line','Timesheet Line')
265
# 'zip_id': _get_zip_id,
266
# 'partner_id': _get_partner_id,
267
# 'contact_id': _get_contact_id,
273
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: