← Back to team overview

openerp-expert-framework team mailing list archive

price hook

 

Hello !

I have prepared a patch for sales (similar for PO,Invoice...) to come
against trunk 3393 and would like to ask for review/advice before carrying on.

the idea is to have a hook for qty*price both in
sale.py
product.py

to allow easy manipulation of qty and price/unit at product and 
sale/purchase/invoice level

Thank you for looking into this
-- 
regards
Ferdinand Gassauer
ChriCar Beteiligungs- und Beratungs- GmbH
Official OpenERP Partner
=== modified file 'account_voucher/__openerp__.py' (properties changed: -x to +x)
=== modified file 'product/product.py'
--- product/product.py	2010-04-18 15:32:44 +0000
+++ product/product.py	2010-04-30 21:34:32 +0000
@@ -578,6 +578,14 @@
         else:
             return super(product_product, self).copy(cr, uid, id, default=default,
                     context=context)
+
+    # this should be called to allow qty and price changes by other modules like the new price_unit
+    def compute_value(self,cr, uid, ids, field_name, arg, context):
+        if context.get('product_qty',False) and context.get('price_unit',False):
+            value =  context.get('product_qty') * context.get('price_unit')
+            context.update({'value':value})
+        return     
+ 
 product_product()
 
 class product_packaging(osv.osv):

=== modified file 'sale/sale.py'
--- sale/sale.py	2010-04-19 06:16:56 +0000
+++ sale/sale.py	2010-04-30 21:34:24 +0000
@@ -29,6 +29,7 @@
 
 import decimal_precision as dp
 
+import sys
 
 class sale_shop(osv.osv):
     _name = "sale.shop"
@@ -790,13 +791,40 @@
             res[line.id] = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
         return res
 
+    # this should be called to allow specific qty and price changes of the sales module by other modules
+    # similar code in PO and invoice line etc.
+    def _compute_value(self, cr, uid, ids, field_name, arg, context=None):
+        if context is None:
+            context = {}
+        res = {}
+        for line in self.browse(cr, uid, ids, context=context):
+          res[line.id] = line.product_uom_qty * self._amount_line_net(cr, uid, ids, field_name, arg, context)[line.id]
+          # the following code would be used for price_unit_id
+          if not line.product_id:
+            res[line.id] = line.product_uom_qty * self._amount_line_net(cr, uid, ids, field_name, arg, context)[line.id]
+          else:
+            price_unit = self._amount_line_net(cr, uid, ids, field_name, arg, context)[line.id]
+            context.update({'product_id': line.product_id.id, 'product_qty' : line.product_uom_qty, 'price_unit' : price_unit})
+            #print >>sys.stderr, 'context 2a', context
+            p = self.pool.get('product.product').compute_value(cr, uid, ids, field_name, arg, context)
+            #print >>sys.stderr, 'context 2b', context
+            res[line.id] = context.get('value',False)
+
+        return res
+
     def _amount_line(self, cr, uid, ids, field_name, arg, context=None):
         if context is None:
             context = {}
         res = {}
         cur_obj = self.pool.get('res.currency')
         for line in self.browse(cr, uid, ids, context=context):
-            res[line.id] = line.price_unit * line.product_uom_qty * (1 - (line.discount or 0.0) / 100.0)
+            #original should be fixed anyway to call _amount_line_net
+            #res[line.id] = line.price_unit * line.product_uom_qty * (1 - (line.discount or 0.0) / 100.0)
+            res[line.id] = self._compute_value(cr, uid, ids, field_name, arg, context)[line.id]
+            #print >>sys.stderr, 'fieldname ', field_name
+            #print >>sys.stderr, 'arg ', arg
+            #print >>sys.stderr, 'context ', context
+            #print >>sys.stderr, 'test ',self._compute_value(cr, uid, ids, field_name, arg, context)[line.id]
             cur = line.order_id.pricelist_id.currency_id
             res[line.id] = cur_obj.round(cr, uid, cur, res[line.id])
         return res