b4db228c938cd561cb38be222219906f47525fd6
[odoo/odoo.git] / addons / hr_timesheet_invoice / wizard / hr_timesheet_analytic_profit.py
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 import datetime
22
23 from osv import osv, fields
24 from tools.translate import _
25
26 class account_analytic_profit(osv.osv_memory):
27     _name = 'hr.timesheet.analytic.profit'
28     _description = 'Print Timesheet Profit'
29     _columns = {
30         'date_from': fields.date('From', required=True),
31         'date_to': fields.date('To', required=True),
32         'journal_ids': fields.many2many('account.analytic.journal', 'analytic_profit_journal_rel', 'analytic_id', 'journal_id', 'Journal', required=True),
33         'employee_ids': fields.many2many('res.users', 'analytic_profit_emp_rel', 'analytic_id', 'emp_id', 'User', required=True),
34     }
35
36     def _date_from(*a):
37         return datetime.date.today().replace(day=1).strftime('%Y-%m-%d')
38
39     def _date_to(*a):
40         return datetime.date.today().strftime('%Y-%m-%d')
41
42     _defaults = {
43         'date_from': _date_from,
44         'date_to': _date_to
45     }
46
47     def print_report(self, cr, uid, ids, context=None):
48         line_obj = self.pool.get('account.analytic.line')
49         data = {}
50         data['form'] = self.read(cr, uid , ids, [], context=context)[0]
51         ids_chk = line_obj.search(cr, uid, [
52                 ('date', '>=', data['form']['date_from']),
53                 ('date', '<=', data['form']['date_to']),
54                 ('journal_id', 'in', data['form']['journal_ids']),
55                 ('user_id', 'in', data['form']['employee_ids']),
56                 ], context=context)
57         if not ids_chk:
58             raise osv.except_osv(_('Data Insufficient!'), _('No record(s) found for Report!'))
59
60         data['form']['journal_ids'] = [(6, 0, data['form']['journal_ids'])] # Improve me => Change the rml/sxw so that it can support withou [0][2]
61         data['form']['employee_ids'] = [(6, 0, data['form']['employee_ids'])]
62         datas = {
63              'ids': [],
64              'model': 'account.analytic.line',
65              'form': data['form']
66              }
67         return {
68             'type': 'ir.actions.report.xml',
69             'report_name': 'account.analytic.profit',
70             'datas': datas,
71              }
72
73 account_analytic_profit()
74
75 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: