Merge lp:~openerp-dev/openobject-addons/6.1-opw-591566-ado into lp:openobject-addons/6.1

Proposed by Amit Dodiya (OpenERP)
Status: Needs review
Proposed branch: lp:~openerp-dev/openobject-addons/6.1-opw-591566-ado
Merge into: lp:openobject-addons/6.1
Diff against target: 70 lines (+42/-1)
1 file modified
sale_margin/sale_margin.py (+42/-1)
To merge this branch: bzr merge lp:~openerp-dev/openobject-addons/6.1-opw-591566-ado
Reviewer Review Type Date Requested Status
Xavier ALT Pending
Naresh(OpenERP) Pending
Review via email: mp+163462@code.launchpad.net

Description of the change

Hello,

"[FIX]:sale_margin: cost price calculation in sale order line goes wrong while using multicompany & multicurrency"

Issue:
While using multicurrency with sale margin the cost(purchse_price) price calculation for sale order line goes wrong.
For e.g: We have 2 companies(A,B), the price(cost/sales) on the product form is in EUR. The company B is in the currency USD, defined with the sales price list and purchase price list in USD too.
When a user logins to the company B to create a SO with price list in USD you will see on sale order line the cost price is same as defined in product form(which is defined on the product form and in “EUR” ), the cost price on the SO with USD should be converted from EUR to USD value like price unit.

Steps:
1). Install sale,sale margin with demo data
2). Now create a new company with currency USD and configured necessary shop,warehouse and locations, now assigned this to a user who has rights as admin have(Now you have 2 company A for admin ,B for demo)
3). Create a new product with sale price = 10, cost price = 5 (which is in EUR becuase you login with admin for company A)
4). Create new pricelist for USD which will be used for company B in sale order
5). Now login with the new user of company B which has currency USD, create a new sale order with USD pricelist and select the newly created product in sale order line, you will see in sale order line price unit = 12.83, cost price = 5.0

Regards,
Amit

To post a comment you must log in.

Unmerged revisions

7205. By Amit Dodiya (OpenERP)

[FIX]:sale_margin: the cost price calculation in sale order line goes wrong while using multicompany & multicurrency

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'sale_margin/sale_margin.py'
2--- sale_margin/sale_margin.py 2012-09-25 14:48:26 +0000
3+++ sale_margin/sale_margin.py 2013-05-13 06:33:27 +0000
4@@ -19,9 +19,43 @@
5 ##############################################################################
6
7 from osv import fields, osv
8+import time
9
10 class sale_order_line(osv.osv):
11 _inherit = "sale.order.line"
12+
13+ def get_pricelist_base(self, cr, uid, pricelist_ids, qty, product, context=None):
14+ if context is None:
15+ context = {}
16+ date = time.strftime('%Y-%m-%d')
17+ if 'date' in context:
18+ date = context['date']
19+
20+ # product.pricelist.version:
21+ if not pricelist_ids:
22+ pricelist_ids = self.pool.get('product.pricelist').search(cr, uid, [], context=context)
23+
24+ pricelist_version_ids = self.pool.get('product.pricelist.version').search(cr, uid, [
25+ ('pricelist_id', 'in', pricelist_ids),
26+ '|',
27+ ('date_start', '=', False),
28+ ('date_start', '<=', date),
29+ '|',
30+ ('date_end', '=', False),
31+ ('date_end', '>=', date),
32+ ])
33+ if len(pricelist_ids) != len(pricelist_version_ids):
34+ raise osv.except_osv(_('Warning!'), _("At least one pricelist has no active version !\nPlease create or activate one."))
35+
36+ for pricelist_id in pricelist_ids:
37+ cr.execute(
38+ 'SELECT i.base FROM product_pricelist_item AS i, '
39+ 'product_pricelist_version AS v, product_pricelist AS pl '
40+ 'WHERE (price_version_id = %s) '
41+ 'AND i.price_version_id = v.id AND v.pricelist_id = pl.id '
42+ 'ORDER BY sequence', (pricelist_version_ids[0],))
43+ res = cr.dictfetchall()
44+ return res
45
46 def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
47 uom=False, qty_uos=0, uos=False, name='', partner_id=False,
48@@ -31,14 +65,21 @@
49 lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
50 if not pricelist:
51 return res
52+
53+ price_type_obj = self.pool.get('product.price.type')
54 frm_cur = self.pool.get('res.users').browse(cr, uid, uid).company_id.currency_id.id
55 to_cur = self.pool.get('product.pricelist').browse(cr, uid, [pricelist])[0].currency_id.id
56+
57 if product:
58+ base_data = self.get_pricelist_base(cr, uid, pricelist_ids=[pricelist], qty=qty, product=product, context=context)
59+ base_id = [ base.get('base') for base in base_data ]
60+ if base_id:
61+ frm_cur = price_type_obj.browse(cr, uid, base_id)[0].currency_id.id
62 purchase_price = self.pool.get('product.product').browse(cr, uid, product).standard_price
63 price = self.pool.get('res.currency').compute(cr, uid, frm_cur, to_cur, purchase_price, round=False)
64 res['value'].update({'purchase_price': price})
65 return res
66-
67+
68 def _product_margin(self, cr, uid, ids, field_name, arg, context=None):
69 res = {}
70 for line in self.browse(cr, uid, ids, context=context):