~hr-core-editors/hr-timesheet/7.0-git

« back to all changes in this revision

Viewing changes to hr_timesheet_sheet_import_accounts/model/hr_timesheet_sheet.py

  • Committer: Pedro M. Baeza
  • Author(s): Jordi Ballester Alomar
  • Date: 2016-10-14 12:46:47 UTC
  • Revision ID: git-v1:34be4c4484a7155eb0d6f9c27bef1a4e5c655d1c
[ADD] Module 'hr_timesheet_sheet_import_accounts' (#20)

[ADD] Module 'hr_timesheet_sheet_import_accounts'

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 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 datetime import datetime
 
23
from dateutil.relativedelta import relativedelta
 
24
from openerp.osv import orm
 
25
 
 
26
 
 
27
class hr_timesheet_sheet(orm.Model):
 
28
    _inherit = "hr_timesheet_sheet.sheet"
 
29
 
 
30
    def set_previous_timesheet_ids(self, cr, uid, ids, context=None):
 
31
 
 
32
        sheet_obj = self.pool.get('hr_timesheet_sheet.sheet')
 
33
        timesheet_obj = self.pool.get('hr.analytic.timesheet')
 
34
 
 
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,
 
38
                                                     context=context)
 
39
            r = user.company_id and user.company_id.timesheet_range or 'month'
 
40
            delta = relativedelta(months=-1)
 
41
            if r == 'month':
 
42
                delta = relativedelta(months=-1)
 
43
            elif r == 'week':
 
44
                delta = relativedelta(weeks=-1)
 
45
            elif r == 'year':
 
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
 
49
            if not emp_id:
 
50
                return False
 
51
            ga_id = sheet.employee_id.product_id.\
 
52
                property_account_expense.id
 
53
            if not ga_id:
 
54
                ga_id = sheet.employee_id.product_id.\
 
55
                    categ_id.property_account_expense_categ.id
 
56
 
 
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)],
 
61
                context=context)
 
62
            a_line_ids = []
 
63
            for lw_sheet in sheet_obj.browse(cr, uid, lw_sheet_ids,
 
64
                                             context=context):
 
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)
 
68
            if a_line_ids:
 
69
                cr.execute('SELECT DISTINCT account_id '
 
70
                           'FROM account_analytic_line AS L '
 
71
                           'WHERE L.id IN %s '
 
72
                           'GROUP BY account_id', (tuple(a_line_ids),))
 
73
                res = cr.dictfetchall()
 
74
                timesheet_ids = []
 
75
                for res_vals in res:
 
76
                    same_account_id = False
 
77
                    for timesheet_id in sheet.timesheet_ids:
 
78
                        if (
 
79
                            timesheet_id.line_id
 
80
                            and timesheet_id.line_id.account_id
 
81
                            and timesheet_id.line_id.account_id.id
 
82
                            == res_vals['account_id']
 
83
                        ):
 
84
                            same_account_id = True
 
85
                    if same_account_id is True:
 
86
                        continue
 
87
                    vals = {
 
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'],
 
92
                        'name': '/',
 
93
                        'product_id': sheet.employee_id.product_id.id,
 
94
                        'product_uom_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,
 
98
                    }
 
99
 
 
100
                    ts_id = timesheet_obj.create(cr, uid, vals,
 
101
                                                 context=context)
 
102
                    timesheet_ids.append(ts_id)
 
103
 
 
104
                if timesheet_ids:
 
105
                    sheet_obj.write(cr, uid, sheet.id,
 
106
                                    {'timesheet_ids': [(4, timesheet_id)
 
107
                                                       for timesheet_id
 
108
                                                       in timesheet_ids]})
 
109
 
 
110
        return False