From 942ac8de0bdf91cd528bd4524e57cb6261ecf713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Grand-Guillaume?= Date: Thu, 25 Mar 2010 11:59:36 +0100 Subject: [PATCH] [IMP] Add the multi-currency support on product costs. You can now have different costs price for each company, but in the same currency even if your company are in different one bzr revid: joel.grandguillaume@camptocamp.com-20100325105936-btwdqg96jt723l2r --- addons/account/account_analytic_line.py | 3 ++- addons/hr_expense/hr_expense.py | 1 + addons/product/company.py | 10 ---------- addons/product/product.py | 11 ++++++++++- addons/stock/stock.py | 16 ++++++++++++---- addons/stock/wizard/wizard_partial_picking.py | 3 ++- 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/addons/account/account_analytic_line.py b/addons/account/account_analytic_line.py index 08f36f65e53..036a8fa3ce2 100644 --- a/addons/account/account_analytic_line.py +++ b/addons/account/account_analytic_line.py @@ -146,8 +146,9 @@ class account_analytic_line(osv.osv): # Compute based on pricetype pricetype=self.pool.get('product.price.type').browse(cr,uid,company_obj.browse(cr,uid,company_id).property_valuation_price_type.id) + # Take the company currency as the reference one + context['currency_id']=company_obj.browse(cr,uid,company_id).currency_id.id amount_unit=prod.price_get(pricetype.field, context)[prod.id] - amount=amount_unit*unit_amount or 1.0 return {'value': { 'amount': - round(amount, 2), diff --git a/addons/hr_expense/hr_expense.py b/addons/hr_expense/hr_expense.py index 5e71e4614df..62988a94edb 100644 --- a/addons/hr_expense/hr_expense.py +++ b/addons/hr_expense/hr_expense.py @@ -218,6 +218,7 @@ class hr_expense_line(osv.osv): # Compute based on pricetype of employee company pricetype_id = self.pool.get('hr.employee').browse(cr,uid,employee_id).user_id.company_id.property_valuation_price_type.id + context['currency_id']=self.pool.get('hr.employee').browse(cr,uid,employee_id).user_id.company_id.currency_id.id pricetype=self.pool.get('product.price.type').browse(cr,uid,pricetype_id) amount_unit=product.price_get(pricetype.field, context)[product.id] diff --git a/addons/product/company.py b/addons/product/company.py index f23d010b600..0f248dba309 100644 --- a/addons/product/company.py +++ b/addons/product/company.py @@ -36,16 +36,6 @@ class res_company(osv.osv): help="The price type field in the selected price type will be used, instead of the default one, \ for valuation of product in the current company"), } - - def _check_currency(self, cr, uid, ids): - for rec in self.browse(cr, uid, ids): - if rec.currency_id.id <> rec.property_valuation_price_type.currency_id.id: - return False - return True - - _constraints = [ - (_check_currency, 'Error! You can not chooes a pricetype in a different currency than your company (Not supported now).', ['property_valuation_price_type']) - ] res_company() diff --git a/addons/product/product.py b/addons/product/product.py index a4a30cba863..8d6b08b69df 100644 --- a/addons/product/product.py +++ b/addons/product/product.py @@ -522,7 +522,6 @@ class product_product(osv.osv): def price_get(self, cr, uid, ids, ptype='list_price', context={}): res = {} product_uom_obj = self.pool.get('product.uom') - for product in self.browse(cr, uid, ids, context=context): res[product.id] = product[ptype] or 0.0 if ptype == 'list_price': @@ -532,6 +531,16 @@ class product_product(osv.osv): uom = product.uos_id or product.uom_id res[product.id] = product_uom_obj._compute_price(cr, uid, uom.id, res[product.id], context['uom']) + # Convert from price_type currency to asked one + if 'currency_id' in context: + pricetype_obj = self.pool.get('product.price.type') + # Take the price_type currency from the product field + # This is right cause a field cannot be in more than one currency + price_type_id = pricetype_obj.search(cr,ui,['field','=',ptype])[0] + price_type_currency_id = pricetype_obj.browse(cr,uid,price_type_id).currency_id.id + res[product.id] = self.pool.get('res.currency').compute(cr, uid, price_type_currency_id, + context['currency_id'], res[product.id],context=context) + return res def copy(self, cr, uid, id, default=None, context=None): diff --git a/addons/stock/stock.py b/addons/stock/stock.py index 310e993146d..579b637769d 100644 --- a/addons/stock/stock.py +++ b/addons/stock/stock.py @@ -117,8 +117,13 @@ class stock_location(osv.osv): for r in result: c = (context or {}).copy() c['location'] = id - product = self.pool.get('product.product').read(cr, uid, r['product_id'], [field_to_read, pricetype.field], context=c) - final_value += (product[field_to_read] * product[pricetype.field]) + product = self.pool.get('product.product').read(cr, uid, r['product_id'], [field_to_read], context=c) + # Compute the amount_unit in right currency + + context['currency_id']=self.pool.get('res.users').browse(cr,uid,uid).company_id.currency_id.id + amount_unit=self.pool.get('product.product').browse(cr,uid,r['product_id']).price_get(pricetype.field, context)[r['product_id']] + + final_value += (product[field_to_read] * amount_unit) return final_value def _product_value(self, cr, uid, ids, field_names, arg, context={}): @@ -218,6 +223,7 @@ class stock_location(osv.osv): # Take the user company and pricetype price_type_id=self.pool.get('res.users').browse(cr,uid,uid).company_id.property_valuation_price_type.id pricetype=self.pool.get('product.price.type').browse(cr,uid,price_type_id) + context['currency_id']=self.pool.get('res.users').browse(cr,uid,uid).company_id.currency_id.id if not product_ids: product_ids = product_obj.search(cr, uid, []) @@ -661,7 +667,9 @@ class stock_picking(osv.osv): if type in ('in_invoice', 'in_refund'): # Take the user company and pricetype price_type_id=self.pool.get('res.users').browse(cr,users,users).company_id.property_valuation_price_type.id - pricetype=self.pool.get('product.price.type').browse(cr,uid,price_type_id) + pricetype=self.pool.get('product.price.type').browse(cr,uid,price_type_id) + context['currency_id']=move_line.company_id.currency_id.id + amount_unit=move_line.product_id.price_get(pricetype.field, context)[move_line.product_id.id] return amount_unit else: @@ -1433,7 +1441,7 @@ class stock_move(osv.osv): # Base computation on valuation price type else: company_id=move.company_id.id - + context['currency_id']=move.company_id.currency_id.id pricetype=self.pool.get('product.price.type').browse(cr,uid,move.company_id.property_valuation_price_type.id) amount_unit=move.product_id.price_get(pricetype.field, context)[move.product_id.id] amount=amount_unit * q or 1.0 diff --git a/addons/stock/wizard/wizard_partial_picking.py b/addons/stock/wizard/wizard_partial_picking.py index 389d6b8f5b6..a0cd1c78711 100644 --- a/addons/stock/wizard/wizard_partial_picking.py +++ b/addons/stock/wizard/wizard_partial_picking.py @@ -126,7 +126,8 @@ def _do_split(self, cr, uid, data, context): product = product_obj.browse(cr, uid, [move.product_id.id])[0] user = users_obj.browse(cr, uid, [uid])[0] - + context['currency_id']=move.company_id.currency_id.id + qty = data['form']['move%s' % move.id] uom = data['form']['uom%s' % move.id] price = data['form']['price%s' % move.id]