<?xml version="1.0"?>
<ZopeData>
  <record id="1" aka="AAAAAAAAAAE=">
    <pickle>
      <tuple>
        <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
        <tuple/>
      </tuple>
    </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[

from Products.ERP5Type.Message import translateString\n
from Products.ERP5Type.Log import log\n
portal = context.getPortalObject()\n
params = portal.ERP5Accounting_getParams(selection_name=selection_name)\n
\n
if params.get(\'precision\', None) is not None:\n
  # listbox editable float fields uses request/precision to format the value.\n
  context.REQUEST.set(\'precision\', params[\'precision\'])\n
\n
if not from_date:\n
  from_date = portal.portal_preferences\\\n
                .getPreferredAccountingTransactionFromDate()\n
\n
# this script can be used for Node, Section or Payment\n
if kw.get(\'node_uid\'):\n
  params[\'node_uid\'] = kw[\'node_uid\']\n
if kw.get(\'mirror_section_uid\'):\n
  params[\'mirror_section_uid\'] = kw[\'mirror_section_uid\']\n
if kw.get(\'payment_uid\'):\n
  params[\'payment_uid\'] = kw[\'payment_uid\']\n
if kw.get(\'project_uid\'):\n
  params[\'project_uid\'] = kw[\'project_uid\']\n
if kw.get(\'function_uid\'):\n
  params[\'function_uid\'] = kw[\'function_uid\']\n
if node_category_strict_membership:\n
  params[\'node_category_strict_membership\'] = node_category_strict_membership\n
if node_category:\n
  params[\'node_category\'] = node_category\n
\n
if not \'parent_portal_type\' in params:\n
  params.setdefault(\'portal_type\', portal.getPortalAccountingMovementTypeList())\n
\n
\n
# Create the related accouting line list\n
new_result  = []\n
net_balance = 0.0\n
\n
node_account_type = None\n
if params.get(\'node_uid\'):\n
  if context.getUid() == params[\'node_uid\']:\n
    node_account_type = context.getAccountTypeId()\n
  else:\n
    node = portal.portal_catalog.getObject(params[\'node_uid\'])\n
    node_account_type = node.getAccountTypeId()\n
\n
if from_date or node_account_type in (\'expense\', \'income\'):\n
  period_start_date = None\n
  # Create a new parameter list to get the previous balance\n
  get_inventory_kw = params.copy()\n
\n
  # ignore any at_date that could lay in params\n
  get_inventory_kw.pop(\'at_date\', None)\n
\n
  if params.has_key(\'period_start_date\'):\n
    if node_account_type in (\'expense\', \'income\'):\n
      # if we have on an expense / income account, only take into account\n
      # movements from the current period.\n
      period_start_date = params[\'period_start_date\']\n
      if from_date:\n
        from_date = max(period_start_date, from_date)\n
      else:\n
        from_date = period_start_date\n
    else:\n
      # for all other accounts, we calculate initial balance\n
      period_start_date = params[\'period_start_date\']\n
      if not from_date:\n
        # I don\'t think this should happen\n
        log(\'from_date not passed, defaulting to period_start_date\')\n
        from_date = period_start_date\n
\n
  # Get previous debit and credit\n
  if from_date == period_start_date and \\\n
                      node_account_type in (\'expense\', \'income\'):\n
    previous_total_debit = previous_total_credit = 0\n
  else:\n
    getInventoryAssetPrice = portal.portal_simulation.getInventoryAssetPrice\n
    # first to the balance at the period start date\n
    if node_account_type in (\'expense\', \'income\'):\n
      period_openning_balance = 0\n
    else:\n
      period_openning_balance = getInventoryAssetPrice(\n
                                              to_date=period_start_date,\n
                                              **get_inventory_kw)\n
    # then all movement between period_start_date and from_date\n
    previous_total_debit  = getInventoryAssetPrice(omit_output=True,\n
           from_date=period_start_date,\n
           to_date=from_date,\n
           **get_inventory_kw) + max(period_openning_balance, 0)\n
    previous_total_credit = getInventoryAssetPrice(omit_input=True,\n
           from_date=period_start_date,\n
           to_date=from_date,\n
           **get_inventory_kw) - max(-period_openning_balance, 0)\n
\n
  if previous_total_credit != 0:\n
    previous_total_credit = - previous_total_credit\n
\n
  # Show the previous balance if not empty\n
  if previous_total_credit != 0 or previous_total_debit != 0:\n
    from Products.ERP5Type.Document import newTempBase\n
\n
    net_balance = previous_total_debit - previous_total_credit\n
    previous_balance = newTempBase(portal, \'_temp_accounting_transaction\')\n
    previous_balance.edit(\n
        uid=\'new_000\',\n
        date=from_date,\n
        simulation_state_title="",\n
        credit_price=previous_total_credit,\n
        debit_price=previous_total_debit,\n
        total_price=net_balance,\n
        running_total_price=net_balance,\n
        is_previous_balance=True,\n
        Movement_getSpecificReference=u\'%s\' % translateString(\'Previous Balance\'),\n
        Movement_getExplanationTitle=u\'%s\' % translateString(\'Previous Balance\'),\n
        Movement_getExplanationTranslatedPortalType=\'\',\n
        Movement_getExplanationReference=\'\',\n
        Movement_getMirrorSectionTitle=\'\',\n
        Movement_getNodeGapId=\'\',\n
        Movement_getExplanationUrl=lambda **kw: \'\',\n
        Node_statAccountingBalance=\'\',\n
        getTranslatedSimulationStateTitle=\'\',\n
      )\n
    if context.getPortalType() == \'Account\':\n
      previous_balance.edit(Movement_getExplanationTitle=\'\')\n
\n
    new_result = [previous_balance]\n
    if context.portal_selections.getSelectionParamsFor(selection_name).get(\'hide_grouping\'):\n
      params[\'where_expression\'] = \'catalog.grouping_reference is NULL\'\n
    new_result.extend(\n
      portal.portal_simulation.getMovementHistoryList(\n
                   from_date=from_date,\n
                   initial_running_total_price=net_balance,\n
                   # initial_running_quantity=net_balance, TODO\n
                   selection_domain=context.portal_selections.getSelectionDomainDictFor(selection_name),\n
                   selection_domain_join_column=\'section_uid\',\n
                   sort_on=sort_on,\n
                   ignore_group_by=True,\n
                   **params))\n
    return new_result\n
\n
if context.portal_selections.getSelectionParamsFor(selection_name).get(\'hide_grouping\'):\n
  params[\'where_expression\'] = \'catalog.grouping_reference is NULL\'\n
\n
\n
# We try not to convert to a list, hence the copy & paste\n
return portal.portal_simulation.getMovementHistoryList(\n
                 from_date=from_date,\n
                 initial_running_total_price=net_balance,\n
                 # initial_running_quantity=net_balance, TODO\n
                 selection_domain=context.portal_selections.getSelectionDomainDictFor(selection_name),\n
                 selection_domain_join_column=\'section_uid\',\n
                 sort_on=sort_on,\n
                  ignore_group_by=True,\n
                 src__=src__,\n
                 **params)\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_code</string> </key>
            <value>
              <none/>
            </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>selection=None, sort_on=[], node_category=None, node_category_strict_membership=None, from_date=None, selection_name=None, src__=0, **kw</string> </value>
        </item>
        <item>
            <key> <string>errors</string> </key>
            <value>
              <tuple/>
            </value>
        </item>
        <item>
            <key> <string>func_code</string> </key>
            <value>
              <object>
                <klass>
                  <global name="FuncCode" module="Shared.DC.Scripts.Signature"/>
                </klass>
                <tuple/>
                <state>
                  <dictionary>
                    <item>
                        <key> <string>co_argcount</string> </key>
                        <value> <int>7</int> </value>
                    </item>
                    <item>
                        <key> <string>co_varnames</string> </key>
                        <value>
                          <tuple>
                            <string>selection</string>
                            <string>sort_on</string>
                            <string>node_category</string>
                            <string>node_category_strict_membership</string>
                            <string>from_date</string>
                            <string>selection_name</string>
                            <string>src__</string>
                            <string>kw</string>
                            <string>Products.ERP5Type.Message</string>
                            <string>translateString</string>
                            <string>Products.ERP5Type.Log</string>
                            <string>log</string>
                            <string>_getattr_</string>
                            <string>context</string>
                            <string>portal</string>
                            <string>params</string>
                            <string>None</string>
                            <string>_getitem_</string>
                            <string>_write_</string>
                            <string>new_result</string>
                            <string>net_balance</string>
                            <string>node_account_type</string>
                            <string>node</string>
                            <string>period_start_date</string>
                            <string>get_inventory_kw</string>
                            <string>max</string>
                            <string>previous_total_debit</string>
                            <string>previous_total_credit</string>
                            <string>getInventoryAssetPrice</string>
                            <string>period_openning_balance</string>
                            <string>_apply_</string>
                            <string>True</string>
                            <string>Products.ERP5Type.Document</string>
                            <string>newTempBase</string>
                            <string>previous_balance</string>
                          </tuple>
                        </value>
                    </item>
                  </dictionary>
                </state>
              </object>
            </value>
        </item>
        <item>
            <key> <string>func_defaults</string> </key>
            <value>
              <tuple>
                <none/>
                <list/>
                <none/>
                <none/>
                <none/>
                <none/>
                <int>0</int>
              </tuple>
            </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>Node_getAccountingTransactionList</string> </value>
        </item>
        <item>
            <key> <string>warnings</string> </key>
            <value>
              <tuple/>
            </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
