Copy.py 3.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
##############################################################################
#
# Copyright (c) 2008,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.
##############################################################################

from TargetSolver import TargetSolver

class Copy(TargetSolver):
  """
  This solver will copy properties and calculate quantities for
  specified divergence list.
  """
  def solveMovement(self, movement):
    """
      Solves a movement.
    """
    movement_relative_url = movement.getRelativeUrl()
    for divergence in self.divergence_list:
      if movement_relative_url == divergence.getProperty(
          'object_relative_url'):
        self._acceptDecision(divergence)

  def _acceptDecision(self, divergence):
    """
    Accept decision according to movement group
    """
    scope = divergence.getProperty('divergence_scope')
    simulation_movement = divergence.getProperty('simulation_movement')
    delivery = simulation_movement.getDeliveryValue()
    value_dict = {}
    quantity_ratio = None
    if scope == 'quantity':
      property_id = 'quantity'
      delivery_quantity = delivery.getQuantity()
56
      delivery_ratio = simulation_movement.getDeliveryRatio()
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
      old_quantity = simulation_movement.getQuantity(0.0)
      new_quantity = delivery_quantity * delivery_ratio
      quantity_ratio = 0
      if old_quantity != 0.0:
        quantity_ratio = new_quantity / old_quantity
      quantity = old_quantity * quantity_ratio
      quantity_error = delivery_quantity * delivery_ratio - quantity
      value_dict['delivery_error'] = quantity_error
      value_dict['quantity'] = quantity
    elif scope == 'category':
      property_id = divergence.getProperty('tested_property')
      new_value_list = delivery.getPropertyList(property_id)
      # variation_category should be edited as variation_category_list
      if property_id == 'variation_category':
        property_id = 'variation_category_list'
      value_dict[property_id] = new_value_list
    else: # otherwise we assume that scope is 'property'
      property_id = divergence.getProperty('tested_property')
      new_value = delivery.getProperty(property_id)
      value_dict[property_id] = new_value

    if not simulation_movement.isPropertyRecorded(property_id):
      simulation_movement.recordProperty(property_id)
    simulation_movement.edit(**value_dict)

    # XXX can we use activity for further expand?
    simulation_movement.expand()