fix access rules
[odoo/odoo.git] / addons / hr_timesheet_invoice / hr_timesheet_invoice.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
14 #
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 #
29 ##############################################################################
30
31 from osv import fields, osv
32
33 from tools.translate import _
34
35 class hr_timesheet_invoice_factor(osv.osv):
36     _name = "hr_timesheet_invoice.factor"
37     _description = "Invoice rate"
38     _columns = {
39         'name': fields.char('Internal name', size=128, required=True),
40         'customer_name': fields.char('Visible name', size=128),
41         'factor': fields.float('Discount (%)', required=True),
42     }
43     _defaults = {
44         'factor': lambda *a: 0.0,
45     }
46
47 hr_timesheet_invoice_factor()
48
49
50 class account_analytic_account(osv.osv):
51     def _invoiced_calc(self, cr, uid, ids, name, arg, context={}):
52         res = {}
53         for account in self.browse(cr, uid, ids):
54             invoiced = {}
55             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,))
56             invoice_ids = filter(None, map(lambda x: x[0], cr.fetchall()))
57             for invoice in self.pool.get('account.invoice').browse(cr, uid, invoice_ids, context):
58                 res.setdefault(account.id, 0.0)
59                 res[account.id] += invoice.amount_untaxed
60         for id in ids:
61             res[id] = round(res.get(id, 0.0),2)
62         return res
63
64     _inherit = "account.analytic.account"
65     _columns = {
66         'pricelist_id' : fields.many2one('product.pricelist', 'Sale Pricelist'),
67         'amount_max': fields.float('Max. Invoice Price'),
68         'amount_invoiced': fields.function(_invoiced_calc, method=True, string='Invoiced Amount'),
69         'to_invoice': fields.many2one('hr_timesheet_invoice.factor','Invoicing'),
70     }
71 account_analytic_account()
72
73
74 class account_analytic_line(osv.osv):
75     _inherit = 'account.analytic.line'
76     _columns = {
77         'invoice_id': fields.many2one('account.invoice', 'Invoice'),
78         'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Invoicing'),
79     }
80
81     def unlink(self, cursor, user, ids, context=None):
82         self._check(cursor, user, ids)
83         return super(account_analytic_line,self).unlink(cursor, user, ids,
84                 context=context)
85
86     def write(self, cr, uid, ids, vals, context=None):
87         self._check(cr, uid, ids)
88         return super(account_analytic_line,self).write(cr, uid, ids, vals,
89                 context=context)
90
91     def _check(self, cr, uid, ids):
92         select = ids
93         if isinstance(select, (int, long)):
94             select = [ids]
95         for line in self.browse(cr, uid, select):
96             if line.invoice_id:
97                 raise osv.except_osv(_('Error !'),
98                         _('You can not modify an invoiced analytic line!'))
99         return True
100
101     def copy(self, cursor, user, obj_id, default=None, context=None):
102         if default is None:
103             default = {}
104         default = default.copy()
105         default.update({'invoice_id': False})
106         return super(account_analytic_line, self).copy(cursor, user, obj_id,
107                 default, context)
108
109 account_analytic_line()
110
111
112 class hr_analytic_timesheet(osv.osv):
113     _inherit = "hr.analytic.timesheet"
114     def on_change_account_id(self, cr, uid, ids, account_id):
115         res = {}
116         if not account_id:
117             return res
118         res.setdefault('value',{})
119         st = self.pool.get('account.analytic.account').browse(cr, uid,
120                 account_id).to_invoice.id
121         res['value']['to_invoice'] = st or False
122         return res
123
124     def copy(self, cursor, user, obj_id, default=None, context=None):
125         if default is None:
126             default = {}
127         default = default.copy()
128         default.update({'invoice_id': False})
129         return super(hr_analytic_timesheet, self).copy(cursor, user, obj_id,
130                 default, context)
131
132 hr_analytic_timesheet()
133
134 class account_invoice(osv.osv):
135     _inherit = "account.invoice"
136
137     def _get_analityc_lines(self, cr, uid, id):
138         iml = super(account_invoice, self)._get_analityc_lines(cr, uid, id)
139
140         inv = self.browse(cr, uid, [id])[0]
141         if inv.type == 'in_invoice':
142             for il in iml:
143                 if il['account_analytic_id']:
144                     to_invoice = self.pool.get('account.analytic.account').read(cr, uid, [il['account_analytic_id']], ['to_invoice'])[0]['to_invoice']
145                     if to_invoice:
146                         il['analytic_lines'][0][2]['to_invoice'] = to_invoice[0]
147         return iml
148
149 account_invoice()
150
151 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
152