From cad3242b7fecb1c706a62e51a0213b9ed4c1923f Mon Sep 17 00:00:00 2001 From: "qdp-launchpad@tinyerp.com" <> Date: Tue, 21 Sep 2010 14:19:33 +0200 Subject: [PATCH] [FIX] account: onchange on analytic lines bzr revid: qdp-launchpad@tinyerp.com-20100921121933-yazeuffn1mpkbiw2 --- addons/account/account_analytic_line.py | 67 +++++++++++++------------ 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/addons/account/account_analytic_line.py b/addons/account/account_analytic_line.py index be1e5ec65e6..7a66dd4f175 100644 --- a/addons/account/account_analytic_line.py +++ b/addons/account/account_analytic_line.py @@ -71,18 +71,22 @@ class account_analytic_line(osv.osv): # Compute the cost based on the price type define into company # property_valuation_price_type property - def on_change_unit_amount(self, cr, uid, id, prod_id, unit_amount,company_id, + def on_change_unit_amount(self, cr, uid, id, prod_id, quantity, company_id, unit=False, journal_id=False, context=None): if context==None: context={} - uom_obj = self.pool.get('product.uom') + if not journal_id or not prod_id: + return {} product_obj = self.pool.get('product.product') - company_obj=self.pool.get('res.company') - analytic_journal_obj=self.pool.get('account.analytic.journal') - product_price_type_obj = self.pool.get('product.price.type') - if prod_id: - result = 0.0 - prod = product_obj.browse(cr, uid, prod_id) + analytic_journal_obj =self.pool.get('account.analytic.journal') + j_id = analytic_journal_obj.browse(cr, uid, journal_id, context=context) + prod = product_obj.browse(cr, uid, prod_id) + if not company_id: + company_id = j_id.company_id.id + result = 0.0 + is_purchase = False + + if j_id.type == 'purchase': a = prod.product_tmpl_id.property_account_expense.id if not a: a = prod.categ_id.property_account_expense_categ.id @@ -91,32 +95,31 @@ class account_analytic_line(osv.osv): _('There is no expense account defined ' \ 'for this product: "%s" (id:%d)') % \ (prod.name, prod.id,)) - if not company_id: - company_id=company_obj._company_default_get(cr, uid, 'account.analytic.line', context) - flag = False - # Compute based on pricetype - pricetype=product_price_type_obj.browse(cr, uid, company_obj.browse(cr,uid,company_id).property_valuation_price_type.id) - if journal_id: - journal = analytic_journal_obj.browse(cr, uid, journal_id) - if journal.type == 'sale': - product_price_type_ids = product_price_type_obj.search(cr, uid, [('field','=','list_price')], context) - if product_price_type_ids: - pricetype = product_price_type_obj.browse(cr, uid, product_price_type_ids, context)[0] - # Take the company currency as the reference one - if pricetype.field == 'list_price': - flag = True - amount_unit = prod.price_get(pricetype.field, context)[prod.id] - amount = amount_unit*unit_amount or 1.0 - prec = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account') - amount = amount_unit*unit_amount or 1.0 - result = round(amount, prec) - if not flag: - result *= -1 - return {'value': { + amount_unit = prod.price_get('standard_price', context)[prod.id] + is_purchase = True + + else: + a = prod.product_tmpl_id.property_account_income.id + if not a: + a = prod.categ_id.property_account_income_categ.id + if not a: + raise osv.except_osv(_('Error !'), + _('There is no income account defined ' \ + 'for this product: "%s" (id:%d)') % \ + (prod.name, prod_id,)) + amount_unit = prod.price_get('list_price', context)[prod_id] + + prec = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account') + amount = amount_unit * quantity or 1.0 + result = round(amount, prec) + if is_purchase: + result *= -1 + return { + 'value': { 'amount': result, 'general_account_id': a, - }} - return {} + } + } def view_header_get(self, cr, user, view_id, view_type, context): if context.get('account_id', False):