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, translate=True),
31 'customer_name': fields.char('Name', size=128, help="Label for 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')
46 cr.execute('SELECT account_id as account_id, l.invoice_id '
47 'FROM hr_analytic_timesheet h LEFT JOIN account_analytic_line l '
48 'ON (h.line_id=l.id) '
49 'WHERE l.account_id = ANY(%s)', (ids,))
50 account_to_invoice_map = {}
51 for rec in cr.dictfetchall():
52 account_to_invoice_map.setdefault(rec['account_id'], []).append(rec['invoice_id'])
54 for account in self.browse(cr, uid, ids, context=context):
55 invoice_ids = filter(None, list(set(account_to_invoice_map.get(account.id, []))))
56 for invoice in obj_invoice.browse(cr, uid, invoice_ids, context=context):
57 res.setdefault(account.id, 0.0)
58 res[account.id] += invoice.amount_untaxed
60 res[id] = round(res.get(id, 0.0),2)
64 _inherit = "account.analytic.account"
66 'pricelist_id': fields.many2one('product.pricelist', 'Pricelist',
67 help="The product to invoice is defined on the employee form, the price will be deducted by this pricelist on the product."),
68 'amount_max': fields.float('Max. Invoice Price',
69 help="Keep empty if this contract is not limited to a total fixed price."),
70 'amount_invoiced': fields.function(_invoiced_calc, string='Invoiced Amount',
71 help="Total invoiced"),
72 'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Timesheet Invoicing Ratio',
73 help="This field allows you to define the rate in case you plan to reinvoice " \
74 "the costs in this analytic account: timesheets, expenses, ..."),
77 'pricelist_id': lambda self, cr, uid, ctx: ctx.get('pricelist_id', False),
80 def on_change_use_timesheets(self, cr, uid, ids, use_timesheets, context=None):
83 ir_model_obj = self.pool.get('ir.model.data')
84 res['value']['to_invoice'] = ir_model_obj.get_object_reference(cr, uid, 'hr_timesheet_invoice', 'timesheet_invoice_factor1')[1]
87 def on_change_partner_id(self, cr, uid, ids,partner_id, name, context=None):
88 res = super(account_analytic_account,self).on_change_partner_id(cr, uid, ids,partner_id, name, context=context)
89 part = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
90 pricelist = part.property_product_pricelist and part.property_product_pricelist.id or False
92 res['value']['pricelist_id'] = pricelist
95 def set_close(self, cr, uid, ids, context=None):
96 self.write(cr, uid, ids, {'state':'close'}, context=context)
97 message = _("Contract has been <b>closed</b>.")
98 self.message_append_note(cr, uid, ids, body=message, context=context)
101 def set_cancel(self, cr, uid, ids, context=None):
102 self.write(cr, uid, ids, {'state':'cancelled'}, context=context)
103 message = _("Contract has been <b>cancelled</b>.")
104 self.message_append_note(cr, uid, ids, body=message, context=context)
107 def set_open(self, cr, uid, ids, context=None):
108 self.write(cr, uid, ids, {'state':'open'}, context=context)
109 message = _("Contract has been <b>opened</b>.")
110 self.message_append_note(cr, uid, ids, body=message, context=context)
113 def set_pending(self, cr, uid, ids, context=None):
114 self.write(cr, uid, ids, {'state':'pending'}, context=context)
115 message = _("Contract has been set as <b>pending</b>.")
116 self.message_append_note(cr, uid, ids, body=message, context=context)
119 account_analytic_account()
122 class account_analytic_line(osv.osv):
123 _inherit = 'account.analytic.line'
125 'invoice_id': fields.many2one('account.invoice', 'Invoice', ondelete="set null"),
126 'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Type of Invoicing', help="It allows to set the discount while making invoice"),
129 def _default_journal(self, cr, uid, context=None):
130 proxy = self.pool.get('hr.employee')
131 record_ids = proxy.search(cr, uid, [('user_id', '=', uid)], context=context)
132 employee = proxy.browse(cr, uid, record_ids[0], context=context)
133 return employee.journal_id and employee.journal_id.id or False
135 def _default_general_account(self, cr, uid, context=None):
136 proxy = self.pool.get('hr.employee')
137 record_ids = proxy.search(cr, uid, [('user_id', '=', uid)], context=context)
138 employee = proxy.browse(cr, uid, record_ids[0], context=context)
139 if employee.product_id and employee.product_id.property_account_income:
140 return employee.product_id.property_account_income.id
144 'journal_id' : _default_journal,
145 'general_account_id' : _default_general_account,
148 def write(self, cr, uid, ids, vals, context=None):
149 self._check_inv(cr, uid, ids, vals)
150 return super(account_analytic_line,self).write(cr, uid, ids, vals,
153 def _check_inv(self, cr, uid, ids, vals):
155 if isinstance(select, (int, long)):
157 if ( not vals.has_key('invoice_id')) or vals['invoice_id' ] == False:
158 for line in self.browse(cr, uid, select):
160 raise osv.except_osv(_('Error!'),
161 _('You cannot modify an invoiced analytic line!'))
164 def copy(self, cursor, user, obj_id, default=None, context=None):
167 default = default.copy()
168 default.update({'invoice_id': False})
169 return super(account_analytic_line, self).copy(cursor, user, obj_id,
170 default, context=context)
172 account_analytic_line()
175 class hr_analytic_timesheet(osv.osv):
176 _inherit = "hr.analytic.timesheet"
177 def on_change_account_id(self, cr, uid, ids, account_id):
181 res.setdefault('value',{})
182 acc = self.pool.get('account.analytic.account').browse(cr, uid, account_id)
183 st = acc.to_invoice.id
184 res['value']['to_invoice'] = st or False
185 if acc.state=='pending':
188 'message': 'The analytic account is in pending state.\nYou should not work on this account !'
192 def copy(self, cursor, user, obj_id, default=None, context=None):
195 default = default.copy()
196 default.update({'invoice_id': False})
197 return super(hr_analytic_timesheet, self).copy(cursor, user, obj_id,
198 default, context=context)
200 hr_analytic_timesheet()
202 class account_invoice(osv.osv):
203 _inherit = "account.invoice"
205 def _get_analytic_lines(self, cr, uid, id, context=None):
206 iml = super(account_invoice, self)._get_analytic_lines(cr, uid, id, context=context)
208 inv = self.browse(cr, uid, [id], context=context)[0]
209 if inv.type == 'in_invoice':
210 obj_analytic_account = self.pool.get('account.analytic.account')
212 if il['account_analytic_id']:
213 # *-* browse (or refactor to avoid read inside the loop)
214 to_invoice = obj_analytic_account.read(cr, uid, [il['account_analytic_id']], ['to_invoice'], context=context)[0]['to_invoice']
216 il['analytic_lines'][0][2]['to_invoice'] = to_invoice[0]
221 class account_move_line(osv.osv):
222 _inherit = "account.move.line"
224 def create_analytic_lines(self, cr, uid, ids, context=None):
225 res = super(account_move_line, self).create_analytic_lines(cr, uid, ids,context=context)
226 analytic_line_obj = self.pool.get('account.analytic.line')
227 for move_line in self.browse(cr, uid, ids, context=context):
228 for line in move_line.analytic_lines:
229 toinv = line.account_id.to_invoice.id
231 analytic_line_obj.write(cr, uid, line.id, {'to_invoice': toinv})
236 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: