From 77657d9db6f732a89178633e8dc3480e44edfeed Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Wed, 22 Jul 2015 15:27:38 +0200 Subject: [PATCH] [PORT][REF] new api; --- .../models/pos_order.py | 28 ++-- .../tests/test_pos_order_pricelist_change.py | 157 +++++------------- pos_order_pricelist_change/views/view.xml | 6 +- 3 files changed, 58 insertions(+), 133 deletions(-) diff --git a/pos_order_pricelist_change/models/pos_order.py b/pos_order_pricelist_change/models/pos_order.py index e182af9d..01d66289 100644 --- a/pos_order_pricelist_change/models/pos_order.py +++ b/pos_order_pricelist_change/models/pos_order.py @@ -20,27 +20,27 @@ # ############################################################################## -from openerp.osv.orm import Model -from openerp.tools.translate import _ +from openerp.models import Model +from openerp import fields, api, _ -class pos_order(Model): +class PosOrder(Model): _inherit = 'pos.order' - def action_recompute_pricelist(self, cr, uid, ids, context=None): - pol_obj = self.pool.get('pos.order.line') - for po in self.browse(cr, uid, ids, context=context): + @api.multi + def action_recompute_pricelist(self): + pol_obj = self.env['pos.order.line'] + for po in self: for pol in po.lines: - res = pol_obj.onchange_product_id( - cr, uid, [pol.id], po.pricelist_id.id, pol.product_id.id, - pol.qty, po.partner_id.id) + res = pol.onchange_product_id( + po.pricelist_id.id, pol.product_id.id, qty=pol.qty, + partner_id=po.partner_id.id) if res['value']['price_unit'] != pol.price_unit: - pol_obj.write( - cr, uid, [pol.id], res['value'], context=context) + pol.write(res['value']) - def onchange_pricelist_id( - self, cr, uid, ids, pricelist_id, lines, context=None): - if not pricelist_id or not lines: + @api.onchange('pricelist_id') + def onchange_pricelist_id(self): + if not self.pricelist_id or not self.lines: return {} warning = { 'title': _('Pricelist Warning!'), diff --git a/pos_order_pricelist_change/tests/test_pos_order_pricelist_change.py b/pos_order_pricelist_change/tests/test_pos_order_pricelist_change.py index 2431a5d8..c8eb0ce2 100644 --- a/pos_order_pricelist_change/tests/test_pos_order_pricelist_change.py +++ b/pos_order_pricelist_change/tests/test_pos_order_pricelist_change.py @@ -28,161 +28,90 @@ class TestPosOrderPricelistChange(TransactionCase): def setUp(self): super(TestPosOrderPricelistChange, self).setUp() - self.imd_obj = self.registry('ir.model.data') - self.pp_obj = self.registry('product.product') - self.ppl_obj = self.registry('product.pricelist') - self.po_obj = self.registry('pos.order') - self.pol_obj = self.registry('pos.order.line') - self.pc_obj = self.registry('pos.config') - self.ps_obj = self.registry('pos.session') + self.imd_obj = self.env['ir.model.data'] + self.pp_obj = self.env['product.product'] + self.ppl_obj = self.env['product.pricelist'] + self.po_obj = self.env['pos.order'] + self.pol_obj = self.env['pos.order.line'] + self.pc_obj = self.env['pos.config'] + self.ps_obj = self.env['pos.session'] + + self.pc = self.env.ref('point_of_sale.pos_config_main') + self.rp_c2c = self.env.ref('base.res_partner_12') + self.ppl_c2c = self.env.ref('product.list0') + self.pp_usb = self.env.ref('product.product_product_48') + + self.rp_spe = self.env.ref( + 'pos_order_pricelist_change.partner_surcharge') + self.ppl_spe = self.env.ref( + 'pos_order_pricelist_change.pricelist_surcharge') # Test Section def test_01_default_price_list(self): """[Regression Test] Sale with default Pricelist""" - cr, uid = self.cr, self.uid - # Getting object - pc_id = self.imd_obj.get_object_reference( - cr, uid, 'point_of_sale', 'pos_config_main')[1] - rp_c2c_id = self.imd_obj.get_object_reference( - cr, uid, 'base', 'res_partner_12')[1] - ppl_c2c_id = self.imd_obj.get_object_reference( - cr, uid, 'product', 'list0')[1] - pp_usb_id = self.imd_obj.get_object_reference( - cr, uid, 'product', 'product_product_48')[1] - # Opening Session - self.ps_obj.create(cr, uid, { - 'config_id': pc_id, - }) + self.ps_obj.create({'config_id': self.pc.id}) # create Pos Order - po_id = self.po_obj.create(cr, uid, { - 'partner_id': rp_c2c_id, - 'pricelist_id': ppl_c2c_id, + po = self.po_obj.create({ + 'partner_id': self.rp_c2c.id, + 'pricelist_id': self.ppl_c2c.id, 'lines': [[0, False, { - 'product_id': pp_usb_id, + 'product_id': self.pp_usb.id, 'qty': 1, }]], }) - pp_usb = self.pp_obj.browse(cr, uid, pp_usb_id) - po = self.po_obj.browse(cr, uid, po_id) - - res = self.pol_obj.onchange_product_id( - cr, uid, po.lines[0].id, po.pricelist_id.id, - po.lines[0].product_id.id, po.lines[0].qty) + res = po.lines[0].onchange_product_id( + po.pricelist_id.id, po.lines[0].product_id.id, po.lines[0].qty) self.assertEquals( - res['value']['price_subtotal'], pp_usb.list_price, + res['value']['price_subtotal'], self.pp_usb.list_price, "Incorrect price for default pricelist!") def test_02_partner_with_price_list_before(self): """[Regression Test] Sale with specific Pricelist""" - cr, uid = self.cr, self.uid - # Getting object - pc_id = self.imd_obj.get_object_reference( - cr, uid, 'point_of_sale', 'pos_config_main')[1] - rp_spe_id = self.imd_obj.get_object_reference( - cr, uid, 'pos_order_pricelist_change', 'partner_surcharge')[1] - ppl_spe_id = self.imd_obj.get_object_reference( - cr, uid, 'pos_order_pricelist_change', 'pricelist_surcharge')[1] - pp_usb_id = self.imd_obj.get_object_reference( - cr, uid, 'product', 'product_product_48')[1] - # Opening Session - self.ps_obj.create(cr, uid, { - 'config_id': pc_id, - }) + self.ps_obj.create({'config_id': self.pc.id}) # create Pos Order - po_id = self.po_obj.create(cr, uid, { - 'partner_id': rp_spe_id, - 'pricelist_id': ppl_spe_id, + po = self.po_obj.create({ + 'partner_id': self.rp_spe.id, + 'pricelist_id': self.ppl_spe.id, 'lines': [[0, False, { - 'product_id': pp_usb_id, + 'product_id': self.pp_usb.id, 'qty': 1, }]], }) - pp_usb = self.pp_obj.browse(cr, uid, pp_usb_id) - po = self.po_obj.browse(cr, uid, po_id) - res = self.pol_obj.onchange_product_id( - cr, uid, po.lines[0].id, po.pricelist_id.id, - po.lines[0].product_id.id, po.lines[0].qty) + res = po.lines[0].onchange_product_id( + po.pricelist_id.id, po.lines[0].product_id.id, po.lines[0].qty) self.assertEquals( - res['value']['price_subtotal'], pp_usb.list_price + 10, + res['value']['price_subtotal'], self.pp_usb.list_price + 10, "Incorrect price for specific pricelist!") def test_03_partner_with_price_list_after(self): """[Functional Test] Change pricelist after have set lines.""" - cr, uid = self.cr, self.uid - # Getting object - pc_id = self.imd_obj.get_object_reference( - cr, uid, 'point_of_sale', 'pos_config_main')[1] - ppl_spe_id = self.imd_obj.get_object_reference( - cr, uid, 'pos_order_pricelist_change', 'pricelist_surcharge')[1] - ppl_c2c_id = self.imd_obj.get_object_reference( - cr, uid, 'product', 'list0')[1] - pp_usb_id = self.imd_obj.get_object_reference( - cr, uid, 'product', 'product_product_48')[1] - # Opening Session - self.ps_obj.create(cr, uid, { - 'config_id': pc_id, - }) + self.ps_obj.create({'config_id': self.pc.id}) # create Pos Order - po_id = self.po_obj.create(cr, uid, { - 'pricelist_id': ppl_c2c_id, + po = self.po_obj.create({ + 'pricelist_id': self.ppl_c2c.id, 'lines': [[0, False, { - 'product_id': pp_usb_id, + 'product_id': self.pp_usb.id, 'qty': 1, }]], }) - po = self.po_obj.browse(cr, uid, po_id) - res = self.po_obj.onchange_pricelist_id( - cr, uid, po.id, ppl_spe_id, po.lines) + res = po.onchange_pricelist_id() self.assertNotEquals( res.get('warning', False), False, "Need warning!") - pp_usb = self.pp_obj.browse(cr, uid, pp_usb_id) # Change now pricelist - self.po_obj.write(cr, uid, [po_id], {'pricelist_id': ppl_spe_id}) - self.po_obj.action_recompute_pricelist(cr, uid, [po.id]) - po = self.po_obj.browse(cr, uid, po_id) + po.write({'pricelist_id': self.ppl_spe.id}) + po.action_recompute_pricelist() + po = self.po_obj.browse(po.id) self.assertEquals( - po.amount_total, pp_usb.list_price + 10, - "Recompute with pricelist error") - - def test_04_partner_without_price_list_after(self): - """[Functional Test] Unset pricelist after have set lines.""" - cr, uid = self.cr, self.uid - # Getting object - pc_id = self.imd_obj.get_object_reference( - cr, uid, 'point_of_sale', 'pos_config_main')[1] - pp_usb_id = self.imd_obj.get_object_reference( - cr, uid, 'product', 'product_product_48')[1] - ppl_c2c_id = self.imd_obj.get_object_reference( - cr, uid, 'product', 'list0')[1] - - # Opening Session - self.ps_obj.create(cr, uid, { - 'config_id': pc_id, - }) - - # create Pos Order - po_id = self.po_obj.create(cr, uid, { - 'pricelist_id': ppl_c2c_id, - 'lines': [[0, False, { - 'product_id': pp_usb_id, - 'qty': 1, - }]], - }) - po = self.po_obj.browse(cr, uid, po_id) - - res = self.po_obj.onchange_pricelist_id( - cr, uid, po.id, False, po.lines) - self.assertEquals( - res.get('warning', False), False, - "Doesn't need warning!") + po.amount_total, self.pp_usb.list_price + 10, + "Recompute with pricelist error.") diff --git a/pos_order_pricelist_change/views/view.xml b/pos_order_pricelist_change/views/view.xml index 783a5526..6e8d6234 100644 --- a/pos_order_pricelist_change/views/view.xml +++ b/pos_order_pricelist_change/views/view.xml @@ -19,7 +19,7 @@ - + pos.order.form pos.order @@ -28,11 +28,7 @@
- - onchange_pricelist_id(pricelist_id, lines) -
-