1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20
##############################################################################
22
from datetime import datetime
23
from dateutil.relativedelta import relativedelta
24
from openerp.osv import orm
27
class hr_timesheet_sheet(orm.Model):
28
_inherit = "hr_timesheet_sheet.sheet"
30
def set_previous_timesheet_ids(self, cr, uid, ids, context=None):
32
sheet_obj = self.pool.get('hr_timesheet_sheet.sheet')
33
timesheet_obj = self.pool.get('hr.analytic.timesheet')
35
for sheet in sheet_obj.browse(cr, uid, ids, context=context):
36
date_from = datetime.strptime(sheet.date_from, '%Y-%m-%d')
37
user = self.pool.get('res.users').browse(cr, uid, uid,
39
r = user.company_id and user.company_id.timesheet_range or 'month'
40
delta = relativedelta(months=-1)
42
delta = relativedelta(months=-1)
44
delta = relativedelta(weeks=-1)
46
delta = relativedelta(years=-1)
47
date_from_lw = (date_from + delta).strftime('%Y-%m-%d')
48
emp_id = sheet.employee_id and sheet.employee_id.id or False
51
ga_id = sheet.employee_id.product_id.\
52
property_account_expense.id
54
ga_id = sheet.employee_id.product_id.\
55
categ_id.property_account_expense_categ.id
57
lw_sheet_ids = sheet_obj.search(
58
cr, uid, [('date_to', '>=', date_from_lw),
59
('date_from', '<=', date_from_lw),
60
('employee_id', '=', emp_id)],
63
for lw_sheet in sheet_obj.browse(cr, uid, lw_sheet_ids,
65
for timesheet_id in lw_sheet.timesheet_ids:
66
if timesheet_id.line_id:
67
a_line_ids.append(timesheet_id.line_id.id)
69
cr.execute('SELECT DISTINCT account_id '
70
'FROM account_analytic_line AS L '
72
'GROUP BY account_id', (tuple(a_line_ids),))
73
res = cr.dictfetchall()
76
same_account_id = False
77
for timesheet_id in sheet.timesheet_ids:
80
and timesheet_id.line_id.account_id
81
and timesheet_id.line_id.account_id.id
82
== res_vals['account_id']
84
same_account_id = True
85
if same_account_id is True:
88
'employee_id': sheet.employee_id.id,
89
'journal_id': sheet.employee_id.journal_id.id,
90
'date': sheet.date_from,
91
'account_id': res_vals['account_id'],
93
'product_id': sheet.employee_id.product_id.id,
95
sheet.employee_id.product_id.uom_id.id,
96
'general_account_id': ga_id,
97
'user_id': context.get('user_id') or uid,
100
ts_id = timesheet_obj.create(cr, uid, vals,
102
timesheet_ids.append(ts_id)
105
sheet_obj.write(cr, uid, sheet.id,
106
{'timesheet_ids': [(4, timesheet_id)