[Erp5-report] r33812 kazuhiko - /erp5/trunk/products/ERP5/Document/TradeModelSolver.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Wed Mar 17 17:00:48 CET 2010
Author: kazuhiko
Date: Wed Mar 17 17:00:47 2010
New Revision: 33812
URL: http://svn.erp5.org?rev=33812&view=rev
Log:
initial commit of Trade Model Solver, that will modify simulation movements and trade model related movements accordingly.
Added:
erp5/trunk/products/ERP5/Document/TradeModelSolver.py
Added: erp5/trunk/products/ERP5/Document/TradeModelSolver.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/Document/TradeModelSolver.py?rev=33812&view=auto
==============================================================================
--- erp5/trunk/products/ERP5/Document/TradeModelSolver.py (added)
+++ erp5/trunk/products/ERP5/Document/TradeModelSolver.py [utf8] Wed Mar 17 17:00:47 2010
@@ -1,0 +1,123 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
+#
+# WARNING: This program as such is intended to be used by professional
+# programmers who take the whole responsibility of assessing all potential
+# consequences resulting from its eventual inadequacies and bugs
+# End users who are looking for a ready-to-use solution with commercial
+# guarantees and support are strongly adviced to contract a Free Software
+# Service Company
+#
+# This program is Free Software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+##############################################################################
+
+import zope.interface
+from AccessControl import ClassSecurityInfo
+from Products.ERP5Type import Permissions, PropertySheet, interfaces
+from Products.ERP5.Document.AcceptSolver import AcceptSolver
+
+class TradeModelSolver(AcceptSolver):
+ """
+ """
+ meta_type = 'ERP5 Trade Model Solver'
+ portal_type = 'Trade Model Solver'
+ add_permission = Permissions.AddPortalContent
+ isIndexable = 0 # We do not want to fill the catalog with objects on which we need no reporting
+
+ # Declarative security
+ security = ClassSecurityInfo()
+ security.declareObjectProtected(Permissions.AccessContentsInformation)
+
+ # Default Properties
+ property_sheets = ( PropertySheet.Base
+ , PropertySheet.XMLObject
+ , PropertySheet.CategoryCore
+ , PropertySheet.DublinCore
+ , PropertySheet.TargetSolver
+ )
+
+ # Declarative interfaces
+ zope.interface.implements(interfaces.ISolver,)
+
+ # ISolver Implementation
+ def solve(self):
+ """
+ Adopt new quantity to simulation movements, with keeping the
+ original one recorded, and then update Trade Model related lines
+ accordingly.
+ """
+ solved_property_list = self.getCausalityValue().getCausalityValue(). \
+ getTestedPropertyList()
+
+ # Here, items of delivery_list should be movements, not deliveries.
+ solved_movement_list = self.getDeliveryValueList()
+ delivery_list = []
+ for solved_movement in solved_movement_list:
+ delivery = solved_movement.getDeliveryValue()
+ if delivery not in delivery_list:
+ delivery_list.append(delivery)
+ all_movement_list = sum([x.getMovementList() for x in delivery_list], [])
+
+ # First, separate movements into invoice lines and trade model
+ # related lines.
+ # XXX is there any better way than using rule's reference?
+ trade_model_related_movement_list = []
+ for movement in all_movement_list:
+ if movement in solved_movement_list:
+ continue
+ applied_rule = movement.getDeliveryRelatedValue().getParentValue()
+ # hard coded reference name
+ if applied_rule.getSpecialiseReference() == 'default_trade_model_rule':
+ trade_model_related_movement_list.append(movement)
+
+ # Second, apply changes on invoice lines.
+ for movement in solved_movement_list:
+ for simulation_movement in movement.getDeliveryRelatedValueList():
+ value_dict = {}
+ for solved_property in solved_property_list:
+ new_value = movement.getProperty(solved_property)
+ if solved_property == 'quantity':
+ new_quantity = new_value * simulation_movement.getDeliveryRatio()
+ value_dict.update({'quantity':new_quantity})
+ else:
+ value_dict.update({solved_property:new_value})
+ self._solveRecursively(simulation_movement, value_dict)
+ simulation_movement.expand()
+
+ # Third, adopt changes on trade model related lines.
+ for movement in self.getDeliveryValueList():
+ for solved_property in solved_property_list:
+ if solved_property == 'quantity':
+ total_quantity = sum(
+ [x.getQuantity() for x in movement.getDeliveryRelatedValueList()])
+ movement.setQuantity(total_quantity)
+ for simulation_movement in movement.getDeliveryRelatedValueList():
+ quantity = simulation_movement.getQuantity()
+ delivery_ratio = quantity / total_quantity
+ delivery_error = total_quantity * delivery_ratio - quantity
+ simulation_movement.edit(delivery_ratio=delivery_ratio,
+ delivery_error=delivery_error)
+ else:
+ # XXX TODO we need to support multiple values for categories or
+ # list type property.
+ simulation_movement = movement.getDeliveryRelatedValue()
+ movement.setProperty(solved_property,
+ simulation_movement.getProperty(solved_property))
+
+ # Finish solving
+ self.succeed()
More information about the Erp5-report
mailing list