<?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 encoding="cdata"><![CDATA[

instance = context\n
portal = instance.getPortalObject()\n
portal_workflow = portal.portal_workflow\n
\n
if portal_workflow.isTransitionPossible(instance, \'converge\'):\n
  instance.converge()\n
\n
  slap_state = instance.getSlapState()\n
\n
  if slap_state == \'draft\':\n
    # Nothing to do except converging\n
    pass\n
  else:\n
    started = "start_requested"\n
    stopped = "stop_requested"\n
    destroyed = "destroy_requested"\n
    assert slap_state in [started, stopped, destroyed]\n
\n
    previous_length = instance.getInvoicingSynchronizationPointer(1)\n
    history_list = portal_workflow.getInfoFor(ob=instance, name=\'history\', wf_id=\'instance_slap_interface_workflow\')\n
    history_length = len(history_list)\n
    history_entry = history_list[previous_length-1]\n
\n
    # no divergence if no new history entry\n
    if (history_length != 1):\n
      assert previous_length != history_length\n
\n
    setup_quantity = 0\n
    update_quantity = 0\n
    destroy_quantity = 0\n
\n
    current_delivery = instance.getCausalityValue()\n
    if current_delivery is None:\n
      # No previous packing list, so, one setup should be created\n
      # Drop all useless draft line\n
      i_in_draft_state = True\n
      i = 0\n
      while i_in_draft_state:\n
        checking_history_entry = history_list[i]\n
        previous_state = checking_history_entry[\'slap_state\']\n
        if previous_state != \'draft\':\n
          i_in_draft_state = False\n
          previous_length = i\n
        else:\n
          setup_quantity += 1\n
        i += 1\n
\n
    if slap_state == destroyed:\n
      # Check if previous pointer was already in destroyed state\n
      previous_state = history_entry[\'slap_state\']\n
      if previous_state != destroyed:\n
        # Let\'s create destroyed packing list\n
        destroy_quantity = 1\n
\n
    # 1 = entry to set document in draft state\n
    update_quantity = history_length - previous_length - setup_quantity - destroy_quantity\n
\n
    # Time to create the PL\n
    delivery_template = portal.restrictedTraverse(\n
        portal.portal_preferences.getPreferredInstanceDeliveryTemplate())\n
    delivery = delivery_template.Base_createCloneDocument(batch_mode=1)\n
\n
    hosting_subscription = instance.getSpecialiseValue(portal_type="Hosting Subscription")\n
    person = hosting_subscription.getDestinationSectionValue(portal_type="Person")\n
\n
    delivery.edit(\n
      title="%s API usage" % instance.getReference(),\n
      destination=person.getRelativeUrl(),\n
      destination_decision=person.getRelativeUrl(),\n
      start_date=history_entry[\'time\'],\n
      stop_date=portal_workflow.getInfoFor(ob=instance, name=\'time\', wf_id=\'instance_slap_interface_workflow\'),\n
    )\n
    line_edit_kw = {\n
      \'aggregate_value_list\': [instance, hosting_subscription],\n
    }\n
\n
    if setup_quantity:\n
      delivery_line_template = portal.restrictedTraverse(\n
          portal.portal_preferences.getPreferredInstanceSetupMovementTemplate())\n
      line = delivery_line_template.Base_createCloneDocument(batch_mode=1,\n
          destination=delivery)\n
      line.edit(\n
        quantity=1,\n
        title="%s setup %s" % (instance.getReference(), setup_quantity),\n
        **line_edit_kw\n
      )\n
\n
    if update_quantity > 0:\n
      delivery_line_template = portal.restrictedTraverse(\n
          portal.portal_preferences.getPreferredInstanceUpdateMovementTemplate())\n
      line = delivery_line_template.Base_createCloneDocument(batch_mode=1,\n
          destination=delivery)\n
      line.edit(\n
        quantity=update_quantity,\n
        title="%s updated %i times" % (instance.getReference(), update_quantity),\n
        **line_edit_kw\n
      )\n
\n
    if destroy_quantity:\n
      delivery_line_template = portal.restrictedTraverse(\n
          portal.portal_preferences.getPreferredInstanceDestroyMovementTemplate())\n
      line = delivery_line_template.Base_createCloneDocument(batch_mode=1,\n
          destination=delivery)\n
      line.edit(\n
        quantity=destroy_quantity,\n
        title="%s destroyed" % instance.getReference(),\n
        **line_edit_kw\n
      )\n
\n
    delivery.confirm()\n
    delivery.start()\n
    delivery.stop()\n
    delivery.deliver()\n
    delivery.startBuilding()\n
\n
    instance.edit(\n
      invoicing_synchronization_pointer=history_length,\n
      causality_value=delivery,\n
    )\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string></string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>Instance_solveInvoicingGeneration</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
