merge
[odoo/odoo.git] / addons / hr_timesheet_invoice / hr_timesheet_invoice.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 from osv import fields, osv
24
25 from tools.translate import _
26
27 class hr_timesheet_invoice_factor(osv.osv):
28     _name = "hr_timesheet_invoice.factor"
29     _description = "Invoice rate"
30     _columns = {
31         'name': fields.char('Internal name', size=128, required=True),
32         'customer_name': fields.char('Visible name', size=128),
33         'factor': fields.float('Discount (%)', required=True),
34     }
35     _defaults = {
36         'factor': lambda *a: 0.0,
37     }
38
39 hr_timesheet_invoice_factor()
40
41
42 class account_analytic_account(osv.osv):
43     def _invoiced_calc(self, cr, uid, ids, name, arg, context={}):
44         res = {}
45         for account in self.browse(cr, uid, ids):
46             invoiced = {}
47             cr.execute('select distinct l.invoice_id from hr_analytic_timesheet h left join account_analytic_line l on (h.line_id=l.id) where account_id=1', (account.id,))
48             invoice_ids = filter(None, map(lambda x: x[0], cr.fetchall()))
49             for invoice in self.pool.get('account.invoice').browse(cr, uid, invoice_ids, context):
50                 res.setdefault(account.id, 0.0)
51                 res[account.id] += invoice.amount_untaxed
52         for id in ids:
53             res[id] = round(res.get(id, 0.0),2)
54         return res
55
56     _inherit = "account.analytic.account"
57     _columns = {
58         'pricelist_id' : fields.many2one('product.pricelist', 'Sale Pricelist'),
59         'amount_max': fields.float('Max. Invoice Price'),
60         'amount_invoiced': fields.function(_invoiced_calc, method=True, string='Invoiced Amount',
61             help="Total invoiced"),
62         'to_invoice': fields.many2one('hr_timesheet_invoice.factor','Reinvoice Costs',
63             help="Check this field if you plan to automatically generate invoices based " \
64             "on the costs in this analytic account: timesheets, expenses, ..." \
65             "You can configure an automatic invoice rate on analytic accounts."),
66     }
67     _defaults = {
68         'pricelist_id': lambda self,cr, uid, ctx: ctx.get('pricelist_id', False),
69     }
70 account_analytic_account()
71
72
73 class account_analytic_line(osv.osv):
74     _inherit = 'account.analytic.line'
75     _columns = {
76         'invoice_id': fields.many2one('account.invoice', 'Invoice'),
77         'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Invoicing'),
78     }
79
80     def unlink(self, cursor, user, ids, context=None):
81         self._check(cursor, user, ids)
82         return super(account_analytic_line,self).unlink(cursor, user, ids,
83                 context=context)
84
85     def write(self, cr, uid, ids, vals, context=None):
86         self._check(cr, uid, ids)
87         return super(account_analytic_line,self).write(cr, uid, ids, vals,
88                 context=context)
89
90     def _check(self, cr, uid, ids):
91         select = ids
92         if isinstance(select, (int, long)):
93             select = [ids]
94         for line in self.browse(cr, uid, select):
95             if line.invoice_id:
96                 raise osv.except_osv(_('Error !'),
97                         _('You can not modify an invoiced analytic line!'))
98         return True
99
100     def copy(self, cursor, user, obj_id, default=None, context=None):
101         if default is None:
102             default = {}
103         default = default.copy()
104         default.update({'invoice_id': False})
105         return super(account_analytic_line, self).copy(cursor, user, obj_id,
106                 default, context)
107
108 account_analytic_line()
109
110
111 class hr_analytic_timesheet(osv.osv):
112     _inherit = "hr.analytic.timesheet"
113     def on_change_account_id(self, cr, uid, ids, account_id):
114         res = {}
115         if not account_id:
116             return res
117         res.setdefault('value',{})
118         acc = self.pool.get('account.analytic.account').browse(cr, uid, account_id)
119         st = acc.to_invoice.id
120         res['value']['to_invoice'] = st or False
121         if acc.state=='pending':
122             res['warning'] = {
123                 'title': 'Warning',
124                 'message': 'The analytic account is in pending state.\nYou should not work on this account !'
125             }
126         return res
127
128     def copy(self, cursor, user, obj_id, default=None, context=None):
129         if default is None:
130             default = {}
131         default = default.copy()
132         default.update({'invoice_id': False})
133         return super(hr_analytic_timesheet, self).copy(cursor, user, obj_id,
134                 default, context)
135
136 hr_analytic_timesheet()
137
138 class account_invoice(osv.osv):
139     _inherit = "account.invoice"
140
141     def _get_analytic_lines(self, cr, uid, id):
142         iml = super(account_invoice, self)._get_analytic_lines(cr, uid, id)
143
144         inv = self.browse(cr, uid, [id])[0]
145         if inv.type == 'in_invoice':
146             for il in iml:
147                 if il['account_analytic_id']:
148                     to_invoice = self.pool.get('account.analytic.account').read(cr, uid, [il['account_analytic_id']], ['to_invoice'])[0]['to_invoice']
149                     if to_invoice:
150                         il['analytic_lines'][0][2]['to_invoice'] = to_invoice[0]
151         return iml
152
153 account_invoice()
154
155 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
156