<?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>"""\n
  This scripts add the balance of every gap account in the list \'accounts\'\n
  it use portal_simulation.getInventoryAssetPrice.\n
  The following REQUEST keys are mandatory:\n
      at_date\n
\n
  those are optional:\n
      gap_base\n
      simulation_state\n
      section_category\n
\n
  those are ignored from the request and should explicitely passed as keywords args to this script:\n
      from_date\n
\n
  parameters keywords to this script overrides REQUEST keys\n
"""\n
request = context.REQUEST\n
\n
gap_base = request.get("gap_base", "gap/fr/pcg/")\n
getURL   = lambda gap_id: context.GAPCategory_getURLFromId(gap_id, gap_base)\n
\n
precision = context.Base_getPreferredPrecision()\n
r_ = lambda x: context.Base_getRoundValue(x, precision)\n
\n
section        = context.restrictedTraverse(request.get("organisation"))\n
section_region = section.getRegion()\n
\n
# \'getInventory\' common parameters\n
params = { \'omit_simulation\' : True\n
         , \'simulation_state\': request.get("simulation_state", [\'stopped\', \'delivered\'])\n
         , \'section_uid\'     : section.getUid()\n
         , \'at_date\'         : request[\'at_date\']\n
         , \'where_expression\': " section.portal_type = \'Organisation\' "\n
         }\n
params.update(kw)\n
\n
# \'net_balance\' is the sum of balance of all accounts found under the \'gap_id_list\' criterion\n
net_balance = 0.0\n
\n
ctool = context.getPortalObject().portal_categories\n
for gap_id in gap_id_list:\n
  gap_path = getURL(gap_id)\n
\n
  # Checks the node category exists\n
  if ctool.restrictedTraverse(gap_path, None) is not None:\n
    params["node_category"] = gap_path\n
    new_balance = 0.0\n
\n
    if not section_region_filtering:\n
      new_balance = context.portal_simulation.getInventoryAssetPrice(**params) or 0.0\n
\n
    else:\n
      # Use section\'s region membership to decide if a transaction is export or not\n
      transaction_list = context.portal_simulation.getInventoryList(**params) or []\n
\n
      # Test each transaction\'s third party region against section\'s region\n
      for transaction in transaction_list:\n
        transaction_line_path = transaction.path\n
        line = context.restrictedTraverse(transaction_line_path)\n
\n
        # Get the third party\n
        third_party = line.getDestinationSectionValue()\n
        if third_party in (None, \'\'):\n
          # TODO: is this log should be replaced by \'raise\' ?\n
          context.log( \'FiscalReportError\'\n
                     , "\'%s\' need a third party." % (transaction_line_path)\n
                     )\n
          # Skip current transaction\n
          continue\n
\n
        # Get the third party region\n
        region = third_party.getRegion()\n
        if region in (None, \'\'):\n
          # TODO: is this log should be replaced by \'raise\' ?\n
          context.log( \'FiscalReportError\'\n
                     , "\'%s\' third party (aka \'%s\') need a region." % ( transaction_line_path\n
                                                                      , third_party.getPath()\n
                                                                      )\n
                     )\n
          # Skip current transaction\n
          continue\n
\n
        # Get the transaction\'s balance\n
        if not region.startswith(section_region):\n
          new_balance = r_(r_(new_balance) + r_(transaction.total_price)) or 0.0\n
\n
    # Update the general balance\n
    net_balance = r_(r_(net_balance) + r_(new_balance))\n
\n
return r_(net_balance)\n
</string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>gap_id_list, section_region_filtering=False, **kw</string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>FiscalReportCell_doGetInventory</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
