<?xml version="1.0"?>
<ZopeData>
  <record id="1" aka="AAAAAAAAAAE=">
    <pickle>
      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
    </pickle>
    <pickle>
      <dictionary>
        <item>
            <key> <string>Script_magic</string> </key>
            <value> <int>3</int> </value>
        </item>
        <item>
            <key> <string>_bind_names</string> </key>
            <value>
              <object>
                <klass>
                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
                </klass>
                <tuple/>
                <state>
                  <dictionary>
                    <item>
                        <key> <string>_asgns</string> </key>
                        <value>
                          <dictionary>
                            <item>
                                <key> <string>name_container</string> </key>
                                <value> <string>container</string> </value>
                            </item>
                            <item>
                                <key> <string>name_context</string> </key>
                                <value> <string>context</string> </value>
                            </item>
                            <item>
                                <key> <string>name_m_self</string> </key>
                                <value> <string>script</string> </value>
                            </item>
                            <item>
                                <key> <string>name_subpath</string> </key>
                                <value> <string>traverse_subpath</string> </value>
                            </item>
                          </dictionary>
                        </value>
                    </item>
                  </dictionary>
                </state>
              </object>
            </value>
        </item>
        <item>
            <key> <string>_body</string> </key>
            <value> <string>from zExceptions import Unauthorized\n
if REQUEST is not None:\n
  raise Unauthorized\n
\n
document = context\n
portal = document.getPortalObject()\n
result = []\n
\n
if context.getValidationState() in ["cancelled", "shared"]:\n
  return\n
\n
try:\n
  tioxml_dict = document.ComputerConsumptionTioXMLFile_parseXml()\n
except KeyError:\n
  document.reject(comment="Fail")\n
  return \n
\n
if tioxml_dict is None:\n
  document.reject(comment="Not usable TioXML data")\n
else:\n
\n
  computer = context.getContributorValue(portal_type="Computer")\n
  delivery_title = tioxml_dict[\'title\']\n
\n
  movement_list = []\n
  for movement in tioxml_dict["movement"]:\n
    reference = movement[\'reference\']\n
\n
    # It had been reported for the computer itself so it is pure\n
    # informative.\n
    if computer.getReference() == reference:\n
      aggregate_value_list = [computer]\n
      person = computer.getSourceAdministrationValue(portal_type="Person")\n
    else:\n
      if reference.startswith("slapuser"):\n
        reference = reference.replace("slapuser", "slappart") \n
      # Find the partition / software instance / user\n
      partition = portal.portal_catalog.getResultValue(\n
        parent_uid=computer.getUid(),\n
        reference=reference,\n
        portal_type="Computer Partition",\n
        validation_state="validated")\n
\n
      if partition.getSlapState() != \'busy\':\n
        continue\n
\n
      assert partition.getSlapState() == \'busy\', "partition %s is not busy" % reference\n
\n
      instance = portal.portal_catalog.getResultValue(\n
        default_aggregate_uid=partition.getUid(),\n
        portal_type="Software Instance",\n
        validation_state="validated")\n
\n
      if instance is None:\n
        # There is no software instance for this partition anymore\n
        # so we just skip this partial consumption.\n
        continue\n
\n
      subscription = instance.getSpecialiseValue(\n
        portal_type="Hosting Subscription")\n
\n
      try:\n
        person = subscription.getDestinationSectionValue(\n
          portal_type="Person")\n
      except:\n
        raise ValueError(instance.getRelativeUrl())\n
\n
      aggregate_value_list = [partition, instance, subscription]\n
\n
    movement_list.append(dict(\n
                        title=movement[\'title\'],\n
                        quantity=movement[\'quantity\'],\n
                        aggregate_value_list=aggregate_value_list,\n
                        resource=movement[\'resource\'],\n
                        person=person.getRelativeUrl()\n
                    )\n
        )\n
\n
  # Time to create the PL\n
  person = computer.getSourceAdministrationValue(portal_type="Person")\n
  delivery_template = portal.restrictedTraverse(\n
      portal.portal_preferences.getPreferredInstanceDeliveryTemplate())\n
  delivery = delivery_template.Base_createCloneDocument(batch_mode=1)\n
\n
  delivery.edit(\n
    title=delivery_title,\n
    destination=person.getRelativeUrl(),\n
    destination_decision=person.getRelativeUrl(),\n
    start_date=context.getCreationDate(),\n
  )\n
\n
  for movement in movement_list:\n
    service = portal.restrictedTraverse(movement[\'resource\'])\n
    delivery.newContent(\n
      portal_type="Sale Packing List Line",\n
      title=movement[\'title\'],\n
      quantity=movement[\'quantity\'],\n
      aggregate_value_list=movement[\'aggregate_value_list\'],\n
      destination=movement[\'person\'],\n
      destination_decision=movement[\'person\'],\n
      destination_section=movement[\'person\'],\n
      resource_value=service,\n
      quantity_unit=service.getQuantityUnit(),\n
    )\n
  delivery.confirm(comment="Created from %s" % context.getRelativeUrl())\n
  delivery.start()\n
  delivery.stop()\n
  delivery.deliver()\n
  delivery.startBuilding()\n
\n
  result.append(delivery.getRelativeUrl())\n
  document.share(comment="Created packing list: %s" % result)\n
\n
return result\n
</string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>REQUEST=None</string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>ComputerConsumptionTioXMLFile_solveInvoicingGeneration</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
