diff --git a/product/ERP5/DeliverySolver/MinPrice.py b/product/ERP5/DeliverySolver/MinPrice.py index ddb4bdc71974e6bdc7ded341f47360dd11a39856..043f822fff0bd21c74c3633baaaebb02b2206b9b 100644 --- a/product/ERP5/DeliverySolver/MinPrice.py +++ b/product/ERP5/DeliverySolver/MinPrice.py @@ -27,11 +27,52 @@ # ############################################################################## +import zope.interface +from Products.ERP5Type import interfaces -from DeliverySolver import DeliverySolver +from FIFO import FIFO -class MinPrice(DeliverySolver): +class MinPrice(FIFO): """ - The MinPrice deliver solver distributes quantity in order to minimise price. + The MinPrice deliver solver distributes quantity in order to minimise + price. """ + # Declarative interfaces + zope.interface.implements(interfaces.IDeliverySolver) + title = 'MinPrice Solver' + + def setTotalQuantity(self, new_quantity): + """ + """ + result = [] + simulation_movement_list = self._getSimulationMovementList() + remaining_quantity = self.getTotalQuantity() - new_quantity + if remaining_quantity > 0: + # In case of reducing the quantity, we should start from the more + # expensive price. + simulation_movement_list.reverse() + for movement in simulation_movement_list: + if remaining_quantity: + quantity = movement.getQuantity() + if quantity < remaining_quantity: + result.append((movement, quantity)) + remaining_quantity -= quantity + movement.setQuantity(0) + else: + result.append((movement, remaining_quantity)) + movement.setQuantity(quantity - remaining_quantity) + remaining_quantity = 0 + # Return movement, split_quantity tuples + for movement in simulation_movement_list: + movement.setDeliveryRatio(movement.getQuantity() / new_quantity) + return result + + def _getSimulationMovementList(self): + """ + Returns a list of simulation movement sorted from the lower price. + """ + simulation_movement_list = self.simulation_movement_list[:] + if len(simulation_movement_list): + simulation_movement_list.sort(key=lambda x:x.getPrice()) + return simulation_movement_list diff --git a/product/ERP5/Tool/SolverTool.py b/product/ERP5/Tool/SolverTool.py index a5fefce66f352b67311295da1507104146d5a9f0..4800505f1079d6ea1409995258d300bb3df4ff3b 100644 --- a/product/ERP5/Tool/SolverTool.py +++ b/product/ERP5/Tool/SolverTool.py @@ -95,7 +95,7 @@ class SolverTool(BaseTool): """ # XXX Hardcoded for now. We need a new registration system for # delivery solvers. - return ['FIFO', 'FILO',] + return ['FIFO', 'FILO', 'MinPrice',] def getDeliverySolverTranslatedItemList(self, class_name_list=None): """