← Back to team overview

openerp-community-reviewer team mailing list archive

[Merge] lp:~therp-nl/openerp-product-attributes/7-0_fixed_price_extended into lp:openerp-product-attributes

 

You have been requested to review the proposed merge of lp:~therp-nl/openerp-product-attributes/7-0_fixed_price_extended into lp:openerp-product-attributes.

For more details, see:
https://code.launchpad.net/~therp-nl/openerp-product-attributes/7-0_fixed_price_extended/+merge/213378

This enhanced fixed pricelist item with the following;

- Make fixed pricelists items work when set through xmlrpc
- Make sure a fixed pricelist item does not have non zero values for rounding, or min. or max. margin
- Make sure a fixed pricelist item refers to a product
- Make fixed price attribute visible in product pricelist item tree view
- Hide all fields that should have non zero values when a pricelist item is set to fixed price.

-- 
https://code.launchpad.net/~therp-nl/openerp-product-attributes/7-0_fixed_price_extended/+merge/213378
Your team Product Core Editors is requested to review the proposed merge of lp:~therp-nl/openerp-product-attributes/7-0_fixed_price_extended into lp:openerp-product-attributes.
=== modified file 'product_pricelist_fixed_price/model/product_pricelist_item.py'
--- product_pricelist_fixed_price/model/product_pricelist_item.py	2014-02-05 20:46:30 +0000
+++ product_pricelist_fixed_price/model/product_pricelist_item.py	2014-03-30 12:50:44 +0000
@@ -4,6 +4,7 @@
 #    OpenERP, Open Source Management Solution
 #    Copyright (c) 2014 Serv. Tecnol. Avanzados (http://www.serviciosbaeza.com)
 #                       Pedro M. Baeza <pedro.baeza@xxxxxxxxxxxxxxxxxx> 
+#                  2014 Therp BV (http://www.therp.nl)
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU Affero General Public License as published
@@ -19,17 +20,22 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
+'''All functionality to enable fixed prices in pricelists.'''
 from openerp.osv import orm, fields
 from openerp.tools.translate import _
 
 
+FIXED_PRICE_TYPE = -3
+
 class product_pricelist_item(orm.Model):
+    '''Inherit existing model to add functionality for fixed prices'''
     _inherit = 'product.pricelist.item'
 
     def _price_field_get_ext(self, cr, uid, context=None):
-        result = super(product_pricelist_item, self)._price_field_get(
-                                    cr, uid, context=context)
-        result.append((-3, _('Fixed Price')))
+        result = super(
+            product_pricelist_item, self)._price_field_get(
+                cr, uid, context=context)
+        result.append((FIXED_PRICE_TYPE, _('Fixed Price')))
         return result
 
     _columns = {
@@ -41,11 +47,95 @@
         'base_ext': -1,
     }
 
+    def _check_fixed_price(self, cr, uid, ids):
+        '''Ensure fixed prices always refer to a specific product.'''
+        for this_obj in self.browse(cr, uid, ids):
+            if this_obj.base_ext == FIXED_PRICE_TYPE:
+                return True
+            if not this_obj.product_id:
+                raise orm.except_orm(
+                    _('Validation error!'),
+                    _('Product required for fixed price item.')
+                )
+            # Values for price_discount and price_round will not be checked,
+            # because create and write will automagically set appropiate
+            # values.
+        return True
+
+    _constraints = [
+        (_check_fixed_price,
+            'invalid values for fixed price', ['base_ext']),
+    ]
+
+    def _auto_end(self, cr, context=None):
+        '''Make sure that after updating database tables for this module,
+        existing values in pricelist item are set correctly.'''
+        cr.execute(
+            'update product_pricelist_item'
+            ' set base_ext = base'
+            ' where base_ext != -3 and base != base_ext'
+        )
+        return super(product_pricelist_item, self)._auto_end(
+            cr, context=context)
+
+    def _modify_vals(self, cr, uid, vals, browse_obj=None, context=None):
+        '''Ensure consistent values for fixed pricelist items.
+        The passed vals parameter is used for both input and output.
+        base should be 1 if base-ext = -1, in all other cases base and
+        base_ext should be the same. The value passed should be leading,
+        with the exception that a fixed price item should never be changed
+        into something else through base.'''
+        # Check wether any action is needed
+        if not ('base_ext' in vals or 'base' in vals):
+            return
+        # Get base and base_ext values
+        if 'base_ext' in vals:
+            base_ext = vals['base_ext']
+            base = (base_ext == FIXED_PRICE_TYPE) and 1 or base_ext
+        else:
+            # getting here we are sure base is in vals
+            base = vals['base']
+            # check against changing fixed price (should not happen)
+            if browse_obj:
+                assert browse_obj.base_ext != FIXED_PRICE_TYPE, (
+                    _('Can not change fixed pricelist item through base'))
+            base_ext = base
+        # Synchronize base and base_ext values
+        vals.update({
+            'base_ext': base_ext,
+            'base': base,
+        })
+        # Make sure other values valid for fixed price
+        if base_ext == FIXED_PRICE_TYPE:
+            vals.update({
+                'price_discount': -1.0,
+                'price_round': 0.0,
+                'price_min_margin': 0.0,
+                'price_max_margin': 0.0,
+            })
+
+    def create(self, cr, uid, vals, context=None):
+        '''override create to get computed values'''
+        self._modify_vals(cr, uid, vals, browse_obj=None, context=context)
+        return super(product_pricelist_item, self).create(
+            cr, uid, vals, context)
+
+    def write(self, cr, uid, ids, vals, context=None):
+        '''override write to get computed values.
+        We need the loop, because computed values might depend on existing
+        values.'''
+        for object_id in ids:
+            browse_records = self.browse(
+                cr, uid, [object_id], context=context)
+            browse_obj = browse_records[0]
+            self._modify_vals(
+                cr, uid, vals, browse_obj=browse_obj, context=context)
+            super(product_pricelist_item, self).write(
+                cr, uid, [object_id], vals, context=context)
+        return True
+
     def onchange_base_ext(self, cr, uid, ids, base_ext, context=None):
-        if base_ext == -3:
-            # Simulate be based on first found price that allows the trick
-            return {
-                'value': {'base': 1,
-                          'price_discount': -1,}
-            }
-        return {'value': {'base': base_ext}}
+        vals = {'base_ext': base_ext}
+        self._modify_vals(cr, uid, vals, context=context)
+        return {'value': vals}
+

=== modified file 'product_pricelist_fixed_price/view/product_pricelist_item_view.xml'
--- product_pricelist_fixed_price/view/product_pricelist_item_view.xml	2014-02-05 20:46:30 +0000
+++ product_pricelist_fixed_price/view/product_pricelist_item_view.xml	2014-03-30 12:50:44 +0000
@@ -2,6 +2,25 @@
 <openerp>
     <data>
 
+        <record
+            id="product_pricelist_item_fixedprice_tree"
+            model="ir.ui.view">
+            <field name="name">product.pricelist.item.fixedprice.tree</field>
+            <field name="model">product.pricelist.item</field>
+            <field
+                name="inherit_id"
+                ref="product.product_pricelist_item_tree_view"
+            />
+            <field name="arch" type="xml">
+                <field name="base" position="attributes">
+                    <attribute name="invisible">True</attribute>
+                </field>
+                <field name="base" position="after">
+                    <field name="base_ext" />
+                </field>
+            </field>
+        </record>
+
         <record id="product_pricelist_item_fixedprice_form" model="ir.ui.view">
             <field name="name">product.pricelist.item.fixedprice</field>
             <field name="model">product.pricelist.item</field>
@@ -10,6 +29,10 @@
                 <group string="Price Computation" position="attributes">
                     <attribute name="col">6</attribute>
                 </group>
+                <field name="product_id" position="attributes">
+                    <attribute name="attrs"
+                        >{'required':[('base_ext','=',-3)]}</attribute>
+                </field>
                 <field name="base" position="attributes">
                     <attribute name="invisible">True</attribute>
                 </field>
@@ -28,6 +51,18 @@
                 <label string=" ) + " position="attributes">
                     <attribute name="attrs">{'invisible': [('base_ext', '=', -3)]}</attribute>
                 </label>
+                <field name="price_round" position="attributes">
+                    <attribute name="attrs"
+                        >{'invisible': [('base_ext', '=', -3)]}</attribute>
+                </field>
+                <field name="price_min_margin" position="attributes">
+                    <attribute name="attrs"
+                        >{'invisible': [('base_ext', '=', -3)]}</attribute>
+                </field>
+                <field name="price_max_margin" position="attributes">
+                    <attribute name="attrs"
+                        >{'invisible': [('base_ext', '=', -3)]}</attribute>
+                </field>
             </field>
         </record>