Merge lp:~therp-nl/openobject-addons/trunk-lp754339 into lp:openobject-addons

Proposed by Holger Brunn (Therp)
Status: Needs review
Proposed branch: lp:~therp-nl/openobject-addons/trunk-lp754339
Merge into: lp:openobject-addons
Diff against target: 226 lines (+182/-20)
3 files modified
account/account_invoice.py (+84/-18)
account/tests/__init__.py (+4/-2)
account/tests/test_product_id_change.py (+94/-0)
To merge this branch: bzr merge lp:~therp-nl/openobject-addons/trunk-lp754339
Reviewer Review Type Date Requested Status
OpenERP Core Team Pending
Review via email: mp+191211@code.launchpad.net

Commit message

[ADD] get price on invoice line from pricelist if there is one

Description of the change

Use pricelists for creating invoices. If there's no pricelist configured, nothing should change.

Also add tests for that.

To post a comment you must log in.
8942. By Holger Brunn (Therp)

[ADD] test

8943. By Stefan Rijnhart (Opener)

[MRG] Merged with trunk

Unmerged revisions

8943. By Stefan Rijnhart (Opener)

[MRG] Merged with trunk

8942. By Holger Brunn (Therp)

[ADD] test

8941. By Holger Brunn (Therp)

[ADD] get price on invoice line from pricelist if there is one

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'account/account_invoice.py'
--- account/account_invoice.py 2013-09-23 17:13:10 +0000
+++ account/account_invoice.py 2013-10-15 15:07:53 +0000
@@ -1540,26 +1540,92 @@
15401540
1541 domain = {'uos_id':[('category_id','=',res.uom_id.category_id.id)]}1541 domain = {'uos_id':[('category_id','=',res.uom_id.category_id.id)]}
15421542
1543 res_final = {'value':result, 'domain':domain}1543 warning = {}
15441544 # When product changes, price ALWAYS need to be reset. If not price
1545 if not company_id or not currency_id:1545 # found in product, or pricelist, it should become False. Only if
1546 return res_final1546 # product_id has been cleared by user, we will leave price_unit as is.
15471547 if product:
1548 company = self.pool.get('res.company').browse(cr, uid, company_id, context=context)1548 price_unit, pu_warning = self._price_unit_get(
1549 currency = self.pool.get('res.currency').browse(cr, uid, currency_id, context=context)1549 cr, uid, product, uom_id, qty, type, partner_id,
15501550 currency_id, context=context)
1551 if company.currency_id.id != currency.id:1551 result['price_unit'] = price_unit # might be False
1552 if type in ('in_invoice', 'in_refund'):1552 warning.update(pu_warning)
1553 res_final['value']['price_unit'] = res.standard_price1553
1554 new_price = res_final['value']['price_unit'] * currency.rate1554 res_final = {'value': result, 'domain': domain, 'warning': warning}
1555 res_final['value']['price_unit'] = new_price
1556
1557 if result['uos_id'] and result['uos_id'] != res.uom_id.id:
1558 selected_uom = self.pool.get('product.uom').browse(cr, uid, result['uos_id'], context=context)
1559 new_price = self.pool.get('product.uom')._compute_price(cr, uid, res.uom_id.id, res_final['value']['price_unit'], result['uos_id'])
1560 res_final['value']['price_unit'] = new_price
1561 return res_final1555 return res_final
15621556
1557 def _price_unit_get(
1558 self, cr, uid, product_id, uom_id, qty, invoice_type, partner_id,
1559 currency_id, context=None):
1560 price_unit = False
1561 warning = {}
1562 standard_currency_id = currency_id
1563 partner_model = self.pool.get('res.partner')
1564 partner_obj = partner_model.browse(
1565 cr, uid, partner_id, context=context)
1566 assert partner_obj, _('No partner found for id %d') % partner_id
1567 if invoice_type in ('in_invoice', 'in_refund'):
1568 field = 'list_price'
1569 pricelist_property = 'property_product_pricelist_purchase'
1570 else:
1571 field = 'standard_price'
1572 pricelist_property = 'property_product_pricelist'
1573 if pricelist_property in partner_obj:
1574 pricelist_id = partner_obj[pricelist_property].id
1575 else:
1576 pricelist_id = False
1577 # Check whether standard price p.u. modified by pricelist
1578 if pricelist_id:
1579 pricelist_model = self.pool.get('product.pricelist')
1580 price_unit = pricelist_model.price_get(
1581 cr, uid, [pricelist_id], product_id, qty or 1.0,
1582 partner=partner_id,
1583 context=dict(context, uom=uom_id))[pricelist_id]
1584 if price_unit is False: # 0.0 is OK, we night have free products
1585 warning = {
1586 'title': _('No valid pricelist line found!'),
1587 'message':
1588 _("Couldn't find a pricelist line matching this product"
1589 " and quantity.\n"
1590 "You have to change either the product, the quantity or"
1591 " the pricelist.")
1592 }
1593 # Pricelist converts price from standard currency to pricelist
1594 # currency. We have to convert this to the invoice currency.
1595 # In practice that will often mean undoing the conversion
1596 # done by the pricelist object
1597 pricelist_obj = pricelist_model.browse(
1598 cr, uid, pricelist_id, context=context)
1599 if pricelist_obj and pricelist_obj.currency_id:
1600 standard_currency_id = pricelist_obj.currency_id.id
1601 else:
1602 # Take standard price per unit directly from product
1603 product_model = self.pool.get('product.product')
1604 product_obj = product_model.browse(
1605 cr, uid, product_id, context=context)
1606 assert product_obj, _('No product found for id %d') % product_id
1607 assert field in product_obj, _(
1608 'Field %s not found in product') % field
1609 price_unit = product_obj.uom_id._compute_price(
1610 cr, uid, product_obj.uom_id.id, product_obj[field], uom_id)
1611 # When price not taken from pricelist, the currency is
1612 # determined by the price_type:
1613 price_type_model = self.pool.get('product.price.type')
1614 price_type_ids = price_type_model.search(
1615 cr, uid, [('field', '=', field)], context=context)
1616 if price_type_ids:
1617 price_type_obj = price_type_model.browse(
1618 cr, uid, price_type_ids[0], context=context)
1619 if price_type_obj and price_type_obj.currency_id:
1620 standard_currency_id = price_type_obj.currency_id.id
1621 # convert price_unit to currency of invoice
1622 if standard_currency_id != currency_id:
1623 currency_model = self.pool.get('res.currency')
1624 price_unit = currency_model.compute(
1625 cr, uid, standard_currency_id, currency_id,
1626 price_unit, round=True, context=context)
1627 return price_unit, warning
1628
1563 def uos_id_change(self, cr, uid, ids, product, uom, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, currency_id=False, context=None, company_id=None):1629 def uos_id_change(self, cr, uid, ids, product, uom, qty=0, name='', type='out_invoice', partner_id=False, fposition_id=False, price_unit=False, currency_id=False, context=None, company_id=None):
1564 if context is None:1630 if context is None:
1565 context = {}1631 context = {}
15661632
=== modified file 'account/tests/__init__.py'
--- account/tests/__init__.py 2012-11-01 20:39:35 +0000
+++ account/tests/__init__.py 2013-10-15 15:07:53 +0000
@@ -1,4 +1,6 @@
1from . import test_tax1from . import test_tax, test_product_id_change
22
3fast_suite = [test_tax,3fast_suite = [
4 test_tax,
5 test_product_id_change,
4 ]6 ]
57
=== added file 'account/tests/test_product_id_change.py'
--- account/tests/test_product_id_change.py 1970-01-01 00:00:00 +0000
+++ account/tests/test_product_id_change.py 2013-10-15 15:07:53 +0000
@@ -0,0 +1,94 @@
1from openerp.tests.common import TransactionCase
2
3class TestProductIdChange(TransactionCase):
4 """Test for product_id_change on account.invoice.line
5 """
6
7 def setUp(self):
8 super(TestProductIdChange, self).setUp()
9 self.line_model = self.registry('account.invoice.line')
10
11 def get_line(self):
12 line_id = self.line_model.create(
13 self.cr,
14 self.uid,
15 {
16 'name': 'testline',
17 })
18 return self.line_model.browse(
19 self.cr, self.uid, line_id)
20
21
22 def get_partner(self):
23 partner_id = self.registry('res.partner').search(
24 self.cr,
25 self.uid,
26 [('customer', '=', True)],
27 limit=1)[0]
28
29 return self.registry('res.partner').browse(
30 self.cr, self.uid, partner_id)
31
32 def get_product(self):
33 product_id = self.registry('product.product').search(
34 self.cr,
35 self.uid,
36 [('uom_id', '!=', False)],
37 limit=1)[0]
38 return self.registry('product.product').browse(
39 self.cr, self.uid, product_id)
40
41 def test_random_product(self):
42 line = self.get_line()
43 product = self.get_product()
44 partner = self.get_partner()
45
46 values = line.product_id_change(
47 product.id, None,
48 partner_id=partner.id)['value']
49
50 self.assertEquals(values['price_unit'], product.list_price)
51 self.assertEquals(values['uos_id'], product.uom_id.id)
52
53 def test_with_pricelist(self):
54 line = self.get_line()
55 product = self.get_product()
56
57 pricelist_id = self.registry('product.pricelist').create(
58 self.cr,
59 self.uid,
60 {
61 'name': 'testpricelist',
62 'type': self.browse_ref('product.pricelist_type_sale').key,
63 'version_id': [
64 (0, 0, {
65 'name': 'testversion',
66 'items_id': [
67 (0, 0, {
68 'name': 'testitem',
69 'product_id': product.id,
70 'price_discount': .5,
71 'price_surcharge': 42,
72 'base': self.browse_ref(
73 'product.list_price').id,
74 }),
75 ],
76 }),
77 ],
78 })
79
80 partner_id = self.registry('res.partner').create(
81 self.cr,
82 self.uid,
83 {
84 'name': 'testcustomer',
85 'customer': True,
86 'property_product_pricelist':
87 'product.pricelist,%d' % pricelist_id,
88 })
89
90 values = line.product_id_change(
91 product.id, None,
92 partner_id=partner_id)['value']
93
94 self.assertEquals(values['price_unit'], product.list_price * 1.5 + 42)

Subscribers

People subscribed via source and target branches

to all changes: