diff options
Diffstat (limited to 'addons/analytic_user_function')
| -rw-r--r-- | addons/analytic_user_function/analytic_user_function.py | 135 | ||||
| -rw-r--r-- | addons/analytic_user_function/analytic_user_function_view.xml | 42 |
2 files changed, 74 insertions, 103 deletions
diff --git a/addons/analytic_user_function/analytic_user_function.py b/addons/analytic_user_function/analytic_user_function.py index 855ee4ced47..bbcff141a47 100644 --- a/addons/analytic_user_function/analytic_user_function.py +++ b/addons/analytic_user_function/analytic_user_function.py @@ -19,22 +19,24 @@ # ############################################################################## -from openerp.osv import fields,osv +from openerp.osv import fields, osv from openerp.tools.translate import _ import openerp.addons.decimal_precision as dp from openerp.exceptions import UserError + class analytic_user_funct_grid(osv.osv): - _name="analytic.user.funct.grid" - _description= "Price per User" - _rec_name="user_id" - _columns={ + _name = "analytic.user.funct.grid" + _description = "Price per User" + _rec_name = "user_id" + _columns = { 'user_id': fields.many2one("res.users", "User", required=True,), 'product_id': fields.many2one("product.product", "Service", required=True,), 'account_id': fields.many2one("account.analytic.account", "Analytic Account", required=True,), 'uom_id': fields.related("product_id", "uom_id", relation="product.uom", string="Unit of Measure", type="many2one", readonly=True), 'price': fields.float('Price', digits_compute=dp.get_precision('Product Price'), help="Price per hour for this user.", required=True), } + def onchange_user_product_id(self, cr, uid, ids, user_id, product_id, context=None): if not user_id: return {} @@ -46,7 +48,7 @@ class analytic_user_funct_grid(osv.osv): value = {} prod = False if product_id: - prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context) + prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context) emp = emp_obj.browse(cr, uid, emp_id[0], context=context) if emp.product_id and not product_id: value['product_id'] = emp.product_id.id @@ -64,92 +66,73 @@ class account_analytic_account(osv.osv): } -class hr_analytic_timesheet(osv.osv): - _inherit = "hr.analytic.timesheet" +class account_analytic_line(osv.osv): + _inherit = "account.analytic.line" + # Look in account, if no value for the user => look in parent until there is no more parent to look # Take the first found... if nothing found => return False - def _get_related_user_account_recursiv(self, cr, uid, user_id, account_id): - temp=self.pool.get('analytic.user.funct.grid').search(cr, uid, [('user_id', '=', user_id),('account_id', '=', account_id) ]) - account=self.pool.get('account.analytic.account').browse(cr, uid, account_id) - if temp: - return temp + def _get_related_user_account_recursiv(self, cr, uid, user_id, account_id, context): + user_function_id = self.pool.get('analytic.user.funct.grid').search(cr, uid, [('user_id', '=', user_id), ('account_id', '=', account_id)]) + account = self.pool['account.analytic.account'].browse(cr, uid, account_id) + if user_function_id: + return user_function_id + elif account.parent_id: + return self._get_related_user_account_recursiv(cr, uid, user_id, account.parent_id.id, context) else: - if account.parent_id: - return self._get_related_user_account_recursiv(cr, uid, user_id, account.parent_id.id) - else: - return False + return False - def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0): + def on_change_account_id(self, cr, uid, ids, account_id, user_id=False, unit_amount=0, is_timesheet=False, context=None): res = {} if not (account_id): - #avoid a useless call to super return res - if not (user_id): - return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id) + return super(account_analytic_line, self).on_change_account_id(cr, uid, ids, account_id, user_id, is_timesheet, context) - #get the browse record related to user_id and account_id - temp = self._get_related_user_account_recursiv(cr, uid, user_id, account_id) - if not temp: - #if there isn't any record for this user_id and account_id - return super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id) + # get the browse record related to user_id and account_id + user_function_id = self._get_related_user_account_recursiv(cr, uid, user_id, account_id, context) + if not user_function_id: + # if there isn't any record for this user_id and account_id + return super(account_analytic_line, self).on_change_account_id(cr, uid, ids, account_id, user_id, is_timesheet, context) else: - #get the old values from super and add the value from the new relation analytic_user_funct_grid - r = self.pool.get('analytic.user.funct.grid').browse(cr, uid, temp)[0] - res.setdefault('value',{}) - res['value']= super(hr_analytic_timesheet, self).on_change_account_id(cr, uid, ids, account_id)['value'] - res['value']['product_id'] = r.product_id.id - res['value']['product_uom_id'] = r.product_id.uom_id.id - - #the change of product has to impact the amount, uom and general_account_id - a = r.product_id.property_account_expense.id - if not a: - a = r.product_id.categ_id.property_account_expense_categ.id - if not a: - raise UserError(_('There is no expense account defined for this product: "%s" (id:%d)') % (r.product_id.name, r.product_id.id,)) - # Compute based on pricetype - if unit_amount: - amount_unit = self.on_change_unit_amount(cr, uid, ids, - r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount'] - amount = unit_amount * amount_unit - res ['value']['amount']= - round(amount, 2) - res ['value']['general_account_id']= a + # get the old values from super and add the value from the new relation analytic_user_funct_grid + res['value'] = super(account_analytic_line, self).on_change_account_id(cr, uid, ids, account_id, user_id, is_timesheet, context)['value'] + res['value'].update(self._get_values_based_on_user_function(cr, uid, ids, user_function_id, unit_amount, context=context)) return res - def on_change_user_id(self, cr, uid, ids, user_id, account_id, unit_amount=0): - res = super(hr_analytic_timesheet, self).on_change_user_id(cr, uid, ids, user_id) + def on_change_user_id(self, cr, uid, ids, user_id, unit_amount=0, account_id=None, is_timesheet=False, context=None): + res = {} + res = super(account_analytic_line, self).on_change_user_id(cr, uid, ids, user_id, is_timesheet, context=context) if account_id: - #get the browse record related to user_id and account_id - temp = self._get_related_user_account_recursiv(cr, uid, user_id, account_id) - if temp: - #add the value from the new relation analytic_user_funct_grid - r = self.pool.get('analytic.user.funct.grid').browse(cr, uid, temp)[0] - res['value']['product_id'] = r.product_id.id - - #the change of product has to impact the amount, uom and general_account_id - a = r.product_id.property_account_expense.id - if not a: - a = r.product_id.categ_id.property_account_expense_categ.id - if not a: - raise UserError( - _('There is no expense account defined ' \ - 'for this product: "%s" (id:%d)') % \ - (r.product_id.name, r.product_id.id,)) - # Compute based on pricetype - if unit_amount: - amount_unit = self.on_change_unit_amount(cr, uid, ids, - r.product_id.id, unit_amount, False, r.product_id.uom_id.id)['value']['amount'] - - amount = unit_amount * amount_unit - res ['value']['amount']= - round(amount, 2) - res ['value']['general_account_id']= a + # get the browse record related to user_id and account_id + user_function_id = self._get_related_user_account_recursiv(cr, uid, user_id, account_id, context) + if user_function_id: + res['value'].update(self._get_values_based_on_user_function(cr, uid, ids, user_function_id, unit_amount, context=context)) return res -class account_analytic_line(osv.osv): - _inherit = "account.analytic.line" - def _get_invoice_price(self, cr, uid, account, product_id, user_id, qty, context = {}): + # Returns appropriate values for an analytic line if the user has a specific user_function + def _get_values_based_on_user_function(self, cr, uid, ids, user_function_id, unit_amount=0, context=None): + res = {} + user_function = self.pool['analytic.user.funct.grid'].browse(cr, uid, user_function_id, context=context) + product = user_function.product_id + res['product_id'] = product.id + res['uom_id'] = product.uom_id.id + + expense_account = product.property_account_expense.id + if not expense_account: + expense_account = product.categ_id.property_account_expense_categ.id + if not expense_account: + raise UserError(_('There is no expense account defined for this product: "%s" (id:%d)') % (product.name, product.id,)) + res['general_account_id'] = expense_account + + if unit_amount: + new_amount = self.on_change_unit_amount(cr, uid, ids, product.id, unit_amount, False, product.uom_id.id) + res['amount'] = new_amount['value']['amount'] + + return res + + def _get_invoice_price(self, cr, uid, account, product_id, user_id, qty, context={}): for grid in account.user_product_ids: - if grid.user_id.id==user_id: + if grid.user_id.id == user_id: return grid.price return super(account_analytic_line, self)._get_invoice_price(cr, uid, account, product_id, user_id, qty, context) diff --git a/addons/analytic_user_function/analytic_user_function_view.xml b/addons/analytic_user_function/analytic_user_function_view.xml index 132f44ebef5..6010970b0ce 100644 --- a/addons/analytic_user_function/analytic_user_function_view.xml +++ b/addons/analytic_user_function/analytic_user_function_view.xml @@ -66,7 +66,7 @@ <field name="inherit_id" ref="hr_timesheet_sheet.hr_timesheet_sheet_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='timesheet_ids']/tree/field[@name='account_id']" position="attributes"> - <attribute name="on_change">on_change_account_id(account_id, user_id, unit_amount)</attribute> + <attribute name="on_change">on_change_account_id(account_id, user_id, unit_amount, is_timesheet, context)</attribute> </xpath> </field> </record> @@ -79,8 +79,7 @@ <field name="inherit_id" ref="hr_timesheet_sheet.hr_timesheet_sheet_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='timesheet_ids']/form/field[@name='account_id']" position="attributes"> - <attribute name="domain">[('type','=','normal'),('state', '<>', 'close')]</attribute> - <attribute name="on_change">on_change_account_id(account_id, user_id, unit_amount)</attribute> + <attribute name="on_change">on_change_account_id(account_id, user_id, unit_amount, is_timesheet, context)</attribute> <attribute name="context">{'default_invoice_on_timesheets': 1}</attribute> </xpath> </field> @@ -88,50 +87,39 @@ <!-- hr.analytic.timesheet inherited views --> <record model="ir.ui.view" id="hr_timesheet_line_form_inherit"> - <field name="name">hr.analytic.timesheet.form</field> - <field name="model">hr.analytic.timesheet</field> - <field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_form"/> + <field name="name">account.analytic.line.form</field> + <field name="model">account.analytic.line</field> + <field name="inherit_id" ref="hr_timesheet_invoice.hr_timesheet_line_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='account_id']" position="attributes"> - <attribute name="domain">[('type','=','normal'),('state', '<>', 'close')]</attribute> - <attribute name="on_change">on_change_account_id(account_id, user_id, unit_amount)</attribute> + <attribute name="on_change">on_change_account_id(account_id, user_id, unit_amount, is_timesheet, context)</attribute> </xpath> </field> </record> <record model="ir.ui.view" id="hr_timesheet_line_form_inherit_2"> - <field name="name">hr.analytic.timesheet.form</field> - <field name="model">hr.analytic.timesheet</field> - <field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_form"/> + <field name="name">account.analytic.line.form</field> + <field name="model">account.analytic.line</field> + <field name="inherit_id" ref="hr_timesheet_invoice.hr_timesheet_line_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='user_id']" position="replace"> - <field name="user_id" required="1" on_change="on_change_user_id(user_id, account_id, unit_amount)"/> + <field name="user_id" required="1" on_change="on_change_user_id(user_id, unit_amount, account_id, is_timesheet, context)"/> </xpath> </field> </record> <record model="ir.ui.view" id="hr_timesheet_line_tree_inherit"> - <field name="name">hr.analytic.timesheet.tree</field> - <field name="model">hr.analytic.timesheet</field> - <field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_tree"/> + <field name="name">account.analytic.line.tree</field> + <field name="model">account.analytic.line</field> + <field name="inherit_id" ref="hr_timesheet_invoice.hr_timesheet_line_tree"/> <field name="arch" type="xml"> <xpath expr="/tree/field[@name='account_id']" position="attributes"> - <attribute name="domain">[('type','=','normal'),('state', '<>', 'close')]</attribute> - <attribute name="on_change">on_change_account_id(account_id, user_id, unit_amount)</attribute> + <attribute name="on_change">on_change_account_id(account_id, user_id, unit_amount, is_timesheet, context)</attribute> </xpath> - </field> - </record> - - <record model="ir.ui.view" id="hr_timesheet_line_tree_inherit_2"> - <field name="name">hr.analytic.timesheet.tree</field> - <field name="model">hr.analytic.timesheet</field> - <field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_tree"/> - <field name="arch" type="xml"> <xpath expr="/tree/field[@name='user_id']" position="replace"> - <field name="user_id" required="1" on_change="on_change_user_id(user_id, account_id, unit_amount)" context="{'default_groups_ref': ['base.group_user']}"/> + <field name="user_id" required="1" on_change="on_change_user_id(user_id, unit_amount, account_id, is_timesheet, context)" context="{'default_groups_ref': ['base.group_user']}"/> </xpath> </field> </record> - </data> </openerp> |
