[Erp5-report] r15375 - in /erp5/trunk/products/ERP5/Document: Movement.py Resource.py

nobody at svn.erp5.org nobody at svn.erp5.org
Sun Jul 29 23:53:50 CEST 2007


Author: yo
Date: Sun Jul 29 23:53:50 2007
New Revision: 15375

URL: http://svn.erp5.org?rev=15375&view=rev
Log:
Add a new method getPriceCalculationPriceDict to obtain more detailed information from price calculation.

Modified:
    erp5/trunk/products/ERP5/Document/Movement.py
    erp5/trunk/products/ERP5/Document/Resource.py

Modified: erp5/trunk/products/ERP5/Document/Movement.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/Document/Movement.py?rev=15375&r1=15374&r2=15375&view=diff
==============================================================================
--- erp5/trunk/products/ERP5/Document/Movement.py (original)
+++ erp5/trunk/products/ERP5/Document/Movement.py Sun Jul 29 23:53:50 2007
@@ -199,8 +199,9 @@
   # _getPrice is defined in the order / delivery
   # Pricing mehod
   def _getPrice(self, context):
-    # Call a script on the context
-    return context.Movement_lookupPrice()
+    operand_dict = self.getPriceCalculationOperandDict(context=context)
+    if operand_dict is not None:
+      return operand_dict['price']
 
   def _getTotalPrice(self, default=None, context=None):
     price = self.getPrice(context=context)
@@ -210,6 +211,34 @@
       return quantity * price
     else:
       return default
+
+  security.declareProtected(Permissions.AccessContentsInformation, 
+          'getPriceCalculationOperandDict')
+  def getPriceCalculationOperandDict(self, default=None, context=None, **kw):
+    """Return a dict object which contains operands used for price
+    calculation. The returned items depend on a site configuration,
+    because this will invoke a custom script at the end. The only
+    assumption is that the dict must contain a key 'price'
+    which represents the final result of the price calculation.
+    
+    The purpose is to obtain descriptive information to notify the user
+    of how a price is calculated in details, in particular, for invoices
+    and quotations. So a script which is eventually called should provide
+    all values required for generating such reports (e.g. a price,
+    a price without a discount, and a discount).
+    """
+    # First, try a type-based method, and if not present, use 
+    # the good-old-days way (which only returns a final result).
+    if context is None:
+      context = self
+    method = context._getTypeBasedMethod('getPriceCalculationOperandDict')
+    if method is not None:
+      operand_dict = method(**kw)
+      if operand_dict is None:
+        return default
+      assert 'price' in operand_dict 
+      return operand_dict
+    return {'price': context.Movement_lookupPrice()}
 
   security.declareProtected(Permissions.AccessContentsInformation, 'getPrice')
   def getPrice(self, default=None, **kw):

Modified: erp5/trunk/products/ERP5/Document/Resource.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/Document/Resource.py?rev=15375&r1=15374&r2=15375&view=diff
==============================================================================
--- erp5/trunk/products/ERP5/Document/Resource.py (original)
+++ erp5/trunk/products/ERP5/Document/Resource.py Sun Jul 29 23:53:50 2007
@@ -636,29 +636,29 @@
       return float(method())
 
     security.declareProtected(Permissions.AccessContentsInformation, 
-                              'getPrice')
-    def getPrice(self, default=None, context=None, REQUEST=None, **kw):
-      """
-      Return the unit price of a resource in a specific context.
-      """
-      # see Movement.getPrice
-      if isinstance(default, Base) and context is None:
-        msg = 'getPrice first argument is supposed to be the default value'\
-              ' accessor, the context should be passed as with the context='\
-              ' keyword argument'
-        warn(msg, DeprecationWarning)
-        LOG('ERP5', WARNING, msg)
-        context = default
-        default = None
-      
-      # First, try to use a type-based method for the calculation.
+                              'getPriceCalculationOperandDict')
+    def getPriceCalculationOperandDict(self, default=None, context=None,
+            REQUEST=None, **kw):
+      """Return a dictionary which contains operands for price calculation.
+      Consult the doc string in Movement.getPriceCalculationOperandDict
+      for more details.
+      """
+      # First, try to use a new type-based method for the calculation.
       # Note that this is based on self (i.e. a resource) instead of context
       # (i.e. a movement).
+      method = self._getTypeBasedMethod('getPriceCalculationOperandDict')
+      if method is not None:
+        return method(default=default, movement=context, REQUEST=REQUEST, **kw)
+
+      # Next, try an old type-based method which returns only a final result.
       method = self._getTypeBasedMethod('getPrice')
       if method is not None:
-        return method(default=default, movement=context, REQUEST=REQUEST, **kw)
-
-      # This below is used only if the type-based method is not
+        price = method(default=default, movement=context, REQUEST=REQUEST, **kw)
+        if price is not None:
+          return {'price': price}
+        return default
+
+      # This below is used only if any type-based method is not
       # available at all. We should provide the default implementation
       # in a Business Template as Resource_getPrice, thus this will not
       # be used in the future. Kept only for backward compatibility in
@@ -727,7 +727,31 @@
         priced_quantity = self.getPricedQuantity()
         unit_base_price = unit_base_price / priced_quantity
       # Return result
-      return unit_base_price
+      if unit_base_price is not None:
+        return {'price': unit_base_price}
+      return default
+
+    security.declareProtected(Permissions.AccessContentsInformation, 
+                              'getPrice')
+    def getPrice(self, default=None, context=None, REQUEST=None, **kw):
+      """
+      Return the unit price of a resource in a specific context.
+      """
+      # see Movement.getPrice
+      if isinstance(default, Base) and context is None:
+        msg = 'getPrice first argument is supposed to be the default value'\
+              ' accessor, the context should be passed as with the context='\
+              ' keyword argument'
+        warn(msg, DeprecationWarning)
+        LOG('ERP5', WARNING, msg)
+        context = default
+        default = None
+      
+      operand_dict = self.getPriceCalculationOperandDict(default=default, 
+              context=context, REQUEST=REQUEST, **kw)
+      if operand_dict is not None:
+        return operand_dict['price']
+      return default
 
     security.declareProtected(Permissions.AccessContentsInformation, 
                               'getQuantityPrecision')




More information about the Erp5-report mailing list