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 osv import fields, osv
24 from tools.translate import _
26 class hr_timesheet_invoice_factor(osv.osv):
27 _name = "hr_timesheet_invoice.factor"
28 _description = "Invoice Rate"
30 'name': fields.char('Internal name', size=128, required=True),
31 'customer_name': fields.char('Name', size=128, help="Name of the customer"),
32 'factor': fields.float('Discount (%)', required=True, help="Discount in percentage"),
35 'factor': lambda *a: 0.0,
38 hr_timesheet_invoice_factor()
41 class account_analytic_account(osv.osv):
42 def _invoiced_calc(self, cr, uid, ids, name, arg, context=None):
43 obj_invoice = self.pool.get('account.invoice')
47 for account in self.browse(cr, uid, ids, context=context):
49 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=%s', (account.id,))
50 invoice_ids = filter(None, map(lambda x: x[0], cr.fetchall()))
51 for invoice in obj_invoice.browse(cr, uid, invoice_ids, context=context):
52 res.setdefault(account.id, 0.0)
53 res[account.id] += invoice.amount_untaxed
55 res[id] = round(res.get(id, 0.0),2)
59 _inherit = "account.analytic.account"
61 'pricelist_id' : fields.many2one('product.pricelist', 'Sale Pricelist'),
62 'amount_max': fields.float('Max. Invoice Price'),
63 'amount_invoiced': fields.function(_invoiced_calc, method=True, string='Invoiced Amount',
64 help="Total invoiced"),
65 'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Reinvoice Costs',
66 help="Fill this field if you plan to automatically generate invoices based " \
67 "on the costs in this analytic account: timesheets, expenses, ..." \
68 "You can configure an automatic invoice rate on analytic accounts."),
71 'pricelist_id': lambda self, cr, uid, ctx: ctx.get('pricelist_id', False),
73 account_analytic_account()
76 class account_analytic_line(osv.osv):
77 _inherit = 'account.analytic.line'
79 'invoice_id': fields.many2one('account.invoice', 'Invoice', ondelete="set null"),
80 'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Type of Invoicing'),
83 def unlink(self, cursor, user, ids, context=None):
86 return super(account_analytic_line,self).unlink(cursor, user, ids,
89 def write(self, cr, uid, ids, vals, context=None):
92 self._check_inv(cr, uid, ids, vals)
93 return super(account_analytic_line,self).write(cr, uid, ids, vals,
96 def _check_inv(self, cr, uid, ids, vals):
98 if isinstance(select, (int, long)):
100 if ( not vals.has_key('invoice_id')) or vals['invoice_id' ] == False:
101 for line in self.browse(cr, uid, select):
103 raise osv.except_osv(_('Error !'),
104 _('You can not modify an invoiced analytic line!'))
107 def copy(self, cursor, user, obj_id, default=None, context=None):
112 default = default.copy()
113 default.update({'invoice_id': False})
114 return super(account_analytic_line, self).copy(cursor, user, obj_id,
115 default, context=context)
117 account_analytic_line()
120 class hr_analytic_timesheet(osv.osv):
121 _inherit = "hr.analytic.timesheet"
122 def on_change_account_id(self, cr, uid, ids, account_id):
126 res.setdefault('value',{})
127 acc = self.pool.get('account.analytic.account').browse(cr, uid, account_id)
128 st = acc.to_invoice.id
129 res['value']['to_invoice'] = st or False
130 if acc.state=='pending':
133 'message': 'The analytic account is in pending state.\nYou should not work on this account !'
137 def copy(self, cursor, user, obj_id, default=None, context=None):
142 default = default.copy()
143 default.update({'invoice_id': False})
144 return super(hr_analytic_timesheet, self).copy(cursor, user, obj_id,
145 default, context=context)
147 hr_analytic_timesheet()
149 class account_invoice(osv.osv):
150 _inherit = "account.invoice"
152 def _get_analytic_lines(self, cr, uid, id):
153 iml = super(account_invoice, self)._get_analytic_lines(cr, uid, id)
155 inv = self.browse(cr, uid, [id])[0]
156 if inv.type == 'in_invoice':
157 obj_analytic_account = self.pool.get('account.analytic.account')
159 if il['account_analytic_id']:
160 to_invoice = obj_analytic_account.read(cr, uid, [il['account_analytic_id']], ['to_invoice'])[0]['to_invoice']
162 il['analytic_lines'][0][2]['to_invoice'] = to_invoice[0]
167 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: