<?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>Python_magic</string> </key>
            <value>
              <none/>
            </value>
        </item>
        <item>
            <key> <string>Script_magic</string> </key>
            <value> <int>3</int> </value>
        </item>
        <item>
            <key> <string>__ac_local_roles__</string> </key>
            <value>
              <none/>
            </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[

"""Returns the difference between the original total payable price of this\n
invoice and all payments that have been made on this invoice.\n
For this, we use both causality relation between transactions and grouping\n
reference for lines.\n
\n
If `detailed` argument is set to a true value, instead of returning the price\n
as a float value, it returns a mapping (node, mirror_section) -> total_price.\n
\n
The `at_date` paremeter is when you want to know the remaining amount at a\n
particuliar date. This simply ignores related payments or transactions after\n
this date.\n
\n
The `account_id` parameter can be use to filter receivable / payable lines to a\n
specific account.\n
\n
The `simulation_state` parameter is a list that can be used to take into account\n
only transactions in those states. By default it will use all but \'draft\', \n
, \'rejected\', \'cancelled\' and \'deleted\'.\n
\n
The `mirror_section_relative_url` parameter must be passed explicitly if the\n
context invoice has multiple sections.\n
"""\n
portal = context.getPortalObject()\n
total_payable_price_per_node_section = dict()\n
\n
if simulation_state is None:\n
  state_list = [x[1] for x in context.Base_getTranslatedWorkflowStateItemList(wf_id=\'accounting_workflow\')]\n
  simulation_state = [x for x in state_list\n
                      if x not in (\'draft\', \'cancelled\', \'deleted\', \'rejected\')]\n
\n
# remember payable / receivable lines in context.\n
accounts_in_context = []\n
\n
if isinstance(account_id, str):\n
  account_id = [account_id]\n
\n
accounting_transaction_type_list = [x for x in\n
            portal.getPortalAccountingTransactionTypeList()\n
              if x != \'Balance Transfer Transaction\']\n
\n
def getIsSourceMovementItemList(invoice):\n
  """Returns all movements inside the invoice, and a flag to know if we are\n
  source on this movement\n
  Handle the (very ad hoc) case of Balance Transfer Transactions\n
  """\n
  is_source = invoice.AccountingTransaction_isSourceView()\n
  movement_item_list = [(is_source, m) for m in invoice.getMovementList(\n
          portal_type=portal.getPortalAccountingMovementTypeList())]\n
  for btt in context.getCausalityRelatedValueList(\n
                        portal_type=\'Balance Transfer Transaction\'):\n
    if simulation_state and btt.getSimulationState() not in simulation_state:\n
      continue\n
    btt_is_source = btt.AccountingTransaction_isSourceView()\n
    for btt_movement in btt.getMovementList(\n
                  portal_type=portal.getPortalAccountingMovementTypeList()):\n
        movement_item_list.append((btt_is_source, btt_movement))\n
\n
  return movement_item_list\n
\n
\n
# calculate the total price of this invoice (according to accounting\n
# transaction lines)\n
for is_source, line in getIsSourceMovementItemList(context):\n
\n
  if is_source:\n
    node_value = line.getSourceValue(portal_type=\'Account\')\n
    line_section = line.getSourceSection()\n
    mirror_section = line.getDestinationSection()\n
    amount = line.getSourceInventoriatedTotalAssetPrice() or 0\n
  else:\n
    node_value = line.getDestinationValue(portal_type=\'Account\')\n
    line_section = line.getDestinationSection()\n
    mirror_section = line.getSourceSection()\n
    amount = line.getDestinationInventoriatedTotalAssetPrice() or 0\n
\n
  if at_date is None and line.getGroupingReference():\n
    continue\n
  \n
  if node_value is not None:\n
    if account_id is not None and node_value.getId() not in account_id:\n
      continue\n
    if account_id is None and node_value.getAccountTypeId() not in (\'payable\', \'receivable\'):\n
      continue\n
    key = (node_value.getRelativeUrl(), mirror_section)\n
    total_payable_price_per_node_section[key] =\\\n
          total_payable_price_per_node_section.get(key, 0) + amount\n
    accounts_in_context.append(node_value)\n
\n
\n
related_transaction_list = context.getCausalityRelatedValueList(\n
                          portal_type=accounting_transaction_type_list)\n
\n
# substract all causalities\n
for related_transaction in related_transaction_list:\n
  if related_transaction.getSimulationState() not in simulation_state:\n
    continue\n
  \n
  # if we have a payment related to multiple invoices, we cannot say the\n
  # remaining price on those invoices.\n
  for other_invoice in [ tr for tr in related_transaction.getCausalityValueList(\n
                         portal_type=accounting_transaction_type_list)\n
                         if tr not in related_transaction_list + [context]]:\n
    other_invoice_is_source = \\\n
                    other_invoice.AccountingTransaction_isSourceView()\n
    for other_line in other_invoice.getMovementList(\n
            portal_type=portal.getPortalAccountingMovementTypeList()):\n
      if other_line.getGroupingReference():\n
        continue\n
      if other_invoice_is_source:\n
        other_invoice_line_account = other_line.getSourceValue()\n
        other_invoice_line_mirror_section = other_line.getDestinationSection()\n
      else:\n
        other_invoice_line_account = other_line.getDestinationValue()\n
        other_invoice_line_mirror_section = other_line.getSourceSection()\n
        \n
      if other_invoice_line_account in accounts_in_context:\n
        # unless this line is for another mirror_section, we cannot calculate\n
        if mirror_section_relative_url is None or \\\n
              other_invoice_line_mirror_section == mirror_section_relative_url:\n
          raise ValueError(\'Unable to calculate %s\' % context.getPath())\n
  \n
  related_transaction_is_source = related_transaction.\\\n
                                        AccountingTransaction_isSourceView()\n
  for line in related_transaction.getMovementList(\n
            portal_type=portal.getPortalAccountingMovementTypeList()):\n
    \n
    if at_date is None and line.getGroupingReference():\n
      continue\n
    \n
    if related_transaction_is_source:\n
      node_value = line.getSourceValue(portal_type=\'Account\')\n
      line_section = line.getSourceSection()\n
      mirror_section = line.getDestinationSection()\n
      amount = line.getSourceInventoriatedTotalAssetPrice() or 0\n
      date = line.getStartDate().earliestTime()\n
    else:\n
      node_value = line.getDestinationValue(portal_type=\'Account\')\n
      line_section = line.getDestinationSection()\n
      mirror_section = line.getSourceSection()\n
      amount = line.getDestinationInventoriatedTotalAssetPrice() or 0\n
      date = line.getStopDate().earliestTime()\n
    \n
    if node_value is not None:\n
      if account_id is not None and node_value.getId() not in account_id:\n
        continue\n
      if account_id is None and node_value.getAccountTypeId() not in (\'payable\', \'receivable\'):\n
        continue\n
      if at_date and date > at_date:\n
        continue\n
      if node_value in accounts_in_context:\n
        key = (node_value.getRelativeUrl(), mirror_section)\n
        total_payable_price_per_node_section[key] =\\\n
            total_payable_price_per_node_section.get(key, 0) + amount\n
\n
if detailed:\n
  return total_payable_price_per_node_section\n
else:\n
  if mirror_section_relative_url:\n
    total_amount = 0\n
    for (node, mirror_section), amount in total_payable_price_per_node_section.items():\n
      if mirror_section == mirror_section_relative_url:\n
        total_amount += amount\n
    return total_amount\n
  return sum(total_payable_price_per_node_section.values())\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_code</string> </key>
            <value>
              <none/>
            </value>
        </item>
        <item>
            <key> <string>_filepath</string> </key>
            <value>
              <none/>
            </value>
        </item>
        <item>
            <key> <string>_owner</string> </key>
            <value>
              <none/>
            </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>detailed=0, at_date=None, account_id=None, simulation_state=None, mirror_section_relative_url=None</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>5</int> </value>
                    </item>
                    <item>
                        <key> <string>co_varnames</string> </key>
                        <value>
                          <tuple>
                            <string>detailed</string>
                            <string>at_date</string>
                            <string>account_id</string>
                            <string>simulation_state</string>
                            <string>mirror_section_relative_url</string>
                            <string>_getattr_</string>
                            <string>context</string>
                            <string>portal</string>
                            <string>dict</string>
                            <string>total_payable_price_per_node_section</string>
                            <string>None</string>
                            <string>append</string>
                            <string>$append0</string>
                            <string>_getiter_</string>
                            <string>x</string>
                            <string>_getitem_</string>
                            <string>state_list</string>
                            <string>accounts_in_context</string>
                            <string>isinstance</string>
                            <string>str</string>
                            <string>accounting_transaction_type_list</string>
                            <string>getIsSourceMovementItemList</string>
                            <string>is_source</string>
                            <string>line</string>
                            <string>node_value</string>
                            <string>line_section</string>
                            <string>mirror_section</string>
                            <string>amount</string>
                            <string>key</string>
                            <string>_write_</string>
                            <string>related_transaction_list</string>
                            <string>related_transaction</string>
                            <string>tr</string>
                            <string>other_invoice</string>
                            <string>other_invoice_is_source</string>
                            <string>other_line</string>
                            <string>other_invoice_line_account</string>
                            <string>other_invoice_line_mirror_section</string>
                            <string>ValueError</string>
                            <string>related_transaction_is_source</string>
                            <string>date</string>
                            <string>total_amount</string>
                            <string>node</string>
                            <string>_inplacevar_</string>
                            <string>sum</string>
                          </tuple>
                        </value>
                    </item>
                  </dictionary>
                </state>
              </object>
            </value>
        </item>
        <item>
            <key> <string>func_defaults</string> </key>
            <value>
              <tuple>
                <int>0</int>
                <none/>
                <none/>
                <none/>
                <none/>
              </tuple>
            </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>Invoice_getRemainingTotalPayablePrice</string> </value>
        </item>
        <item>
            <key> <string>warnings</string> </key>
            <value>
              <tuple/>
            </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
