[IMP] barcodes: use builtin python sets when it makes sense
[odoo/odoo.git] / addons / analytic_user_function / analytic_user_function.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
22 from openerp.osv import fields,osv
23 from openerp.tools.translate import _
24 import openerp.addons.decimal_precision as dp
25
26 class analytic_user_funct_grid(osv.osv):
27     _name="analytic.user.funct.grid"
28     _description= "Price per User"
29     _rec_name="user_id"
30     _columns={
31         'user_id': fields.many2one("res.users", "User", required=True,),
32         'product_id': fields.many2one("product.product", "Service", required=True,),
33         'account_id': fields.many2one("account.analytic.account", "Analytic Account", required=True,),
34         'uom_id': fields.related("product_id", "uom_id", relation="product.uom", string="Unit of Measure", type="many2one", readonly=True),
35         'price': fields.float('Price', digits_compute=dp.get_precision('Product Price'), help="Price per hour for this user.", required=True),
36     }
37     def onchange_user_product_id(self, cr, uid, ids, user_id, product_id, context=None):
38         if not user_id:
39             return {}
40         emp_obj = self.pool.get('hr.employee')
41         emp_id = emp_obj.search(cr, uid, [('user_id', '=', user_id)], context=context)
42         if not emp_id:
43             return {}
44
45         value = {}
46         prod = False
47         if product_id:
48            prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
49         emp = emp_obj.browse(cr, uid, emp_id[0], context=context)
50         if emp.product_id and not product_id:
51             value['product_id'] = emp.product_id.id
52             prod = emp.product_id
53         if prod:
54             value['price'] = prod.list_price
55             value['uom_id'] = prod.uom_id.id
56         return {'value': value}
57
58
59 class account_analytic_account(osv.osv):
60     _inherit = "account.analytic.account"
61     _columns = {
62         'user_product_ids': fields.one2many('analytic.user.funct.grid', 'account_id', 'Users/Products Rel.', copy=True),
63     }
64
65
66 class hr_analytic_timesheet(osv.osv):
67     _inherit = "hr.analytic.timesheet"
68     # Look in account, if no value for the user => look in parent until there is no more parent to look
69     # Take the first found... if nothing found => return False
70     def _get_related_user_account_recursiv(self, cr, uid, user_id, account_id):
71         temp=self.pool.get('analytic.user.funct.grid').search(cr, uid, [('user_id', '=', user_id),('account_id', '=', account_id) ])
72         account=self.pool.get('account.analytic.account').browse(cr, uid, account_id)
73         if temp:
74             return temp
75         else:
76             if account.parent_id:
77                 return self._get_related_user_account_recursiv(cr, uid, user_id, account.parent_id.id)
78             else:
79                 return False
80
81     def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0):
82         res = {}
83         if not (account_id):
84             #avoid a useless call to super
85             return res
86
87         if not (user_id):
88             return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)
89
90         #get the browse record related to user_id and account_id
91         temp = self._get_related_user_account_recursiv(cr, uid, user_id, account_id)
92         if not temp:
93             #if there isn't any record for this user_id and account_id
94             return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)
95         else:
96             #get the old values from super and add the value from the new relation analytic_user_funct_grid
97             r = self.pool.get('analytic.user.funct.grid').browse(cr, uid, temp)[0]
98             res.setdefault('value',{})
99             res['value']= super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)['value']
100             res['value']['product_id'] = r.product_id.id
101             res['value']['product_uom_id'] = r.product_id.uom_id.id
102
103             #the change of product has to impact the amount, uom and general_account_id
104             a = r.product_id.property_account_expense.id
105             if not a:
106                 a = r.product_id.categ_id.property_account_expense_categ.id
107             if not a:
108                 raise osv.except_osv(_('Error!'),
109                         _('There is no expense account defined ' \
110                                 'for this product: "%s" (id:%d)') % \
111                                 (r.product_id.name, r.product_id.id,))
112             # Compute based on pricetype
113             if unit_amount:
114                 amount_unit = self.on_change_unit_amount(cr, uid, ids,
115                             r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount']
116                 amount = unit_amount *  amount_unit
117                 res ['value']['amount']= - round(amount, 2)
118             res ['value']['general_account_id']= a
119         return res
120
121     def on_change_user_id(self, cr, uid, ids, user_id, account_id, unit_amount=0):
122         res = super(hr_analytic_timesheet, self).on_change_user_id(cr, uid, ids, user_id)
123
124         if account_id:
125             #get the browse record related to user_id and account_id
126             temp = self._get_related_user_account_recursiv(cr, uid, user_id, account_id)
127             if temp:
128                 #add the value from the new relation analytic_user_funct_grid
129                 r = self.pool.get('analytic.user.funct.grid').browse(cr, uid, temp)[0]
130                 res['value']['product_id'] = r.product_id.id
131
132                 #the change of product has to impact the amount, uom and general_account_id
133                 a = r.product_id.property_account_expense.id
134                 if not a:
135                     a = r.product_id.categ_id.property_account_expense_categ.id
136                 if not a:
137                     raise osv.except_osv(_('Error!'),
138                             _('There is no expense account defined ' \
139                                     'for this product: "%s" (id:%d)') % \
140                                     (r.product_id.name, r.product_id.id,))
141                 # Compute based on pricetype
142                 if unit_amount:
143                     amount_unit = self.on_change_unit_amount(cr, uid, ids,
144                         r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount']
145
146                     amount = unit_amount * amount_unit
147                     res ['value']['amount']= - round(amount, 2)
148                 res ['value']['general_account_id']= a
149         return res
150
151 class account_analytic_line(osv.osv):
152     _inherit = "account.analytic.line"
153     def _get_invoice_price(self, cr, uid, account, product_id, user_id, qty, context = {}):
154         for grid in account.user_product_ids:
155             if grid.user_id.id==user_id:
156                 return grid.price
157         return super(account_analytic_line, self)._get_invoice_price(cr, uid, account, product_id, user_id, qty, context)
158