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

"""Calculates the VAT for this invoice, returning a dict with keys:\n
\n
 * total: The amount of VAT\n
 * ratio: The ratio of the VAT\n
 * title: The title of this VAT, actually this is the title of the\n
   account used in the invoice transaction rule.\n
\n
This scripts first look in the accounting lines related to this invoice, and\n
uses the sum of all accounts of type collected_vat. If nothing is found, it\n
will look in the simulation tree to find all movements which uses a\n
collected_vat account as source.\n
\n
TODO: It\'s now clear that this script now needs a complete rewrite, because it\n
doesn\'t support different VAT rates for different lines and retrieving the VAT\n
rate associated with an invoice line.\n
\n
This API will probably change.\n
"""\n
\n
vat_infos = {\n
  \'total\' : 0,\n
}\n
\n
invoice = context\n
portal = context.getPortalObject()\n
accounting_movement_list = invoice.getMovementList(\n
          portal_type=portal.getPortalAccountingMovementTypeList())\n
\n
# 1) the invoice contains accounting lines.\n
if len(accounting_movement_list):\n
  for movement in accounting_movement_list:\n
    account = movement.getSourceValue(portal_type=\'Account\')\n
    if account is None : continue\n
    if account.getAccountTypeId() == \'collected_vat\' :\n
      vat_infos.setdefault(\'title\', account.getTitle())\n
      vat_infos.update({\'total\': vat_infos[\'total\'] + movement.getQuantity()})\n
  vat_infos[\'ratio\'] = vat_infos[\'total\'] /\\\n
        (float(invoice.getTotalPrice(\n
                  portal_type=portal.getPortalInvoiceMovementTypeList(),\n
                  fast=0)) or 1)\n
\n
# 2) no accounting lines => we must look in the simulation\n
if not vat_infos.has_key(\'title\') :\n
  for invoice_movement in invoice.getMovementList(\n
                      portal_type=portal.getPortalInvoiceMovementTypeList() ) :\n
    aggregated_simulation_movements = {}\n
    for simulation_movement in ( invoice_movement.getDeliveryRelatedValueList(\n
                                      portal_type = \'Simulation Movement\')  +\n
                                 invoice_movement.getOrderRelatedValueList(\n
                                      portal_type = \'Simulation Movement\') ):\n
      if aggregated_simulation_movements.has_key(\n
                  simulation_movement.getPath()):\n
        continue\n
      aggregated_simulation_movements[simulation_movement.getPath()] = 1\n
\n
      for applied_rule in simulation_movement.objectValues():\n
        # inside we got accounting movements\n
        for movement in applied_rule.objectValues() :\n
          account = movement.getSourceValue(portal_type=\'Account\')\n
          if account is None : continue\n
          if account.getAccountTypeId() == \'collected_vat\':\n
            vat_infos.setdefault(\'title\', account.getTitle())\n
            vat_infos.update( { \'total\':\n
                    vat_infos[\'total\'] + movement.getQuantity() } )\n
  \n
  vat_infos[\'ratio\'] = vat_infos[\'total\'] /\\\n
        (float(invoice.getTotalPrice(\n
                  portal_type=portal.getPortalInvoiceMovementTypeList(),\n
                  fast=0)) or 1)\n
        \n
vat_infos.setdefault(\'title\', "")\n
vat_infos.setdefault(\'ratio\', 0)\n
return vat_infos\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>_params</string> </key>
            <value> <string></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>0</int> </value>
                    </item>
                    <item>
                        <key> <string>co_varnames</string> </key>
                        <value>
                          <tuple>
                            <string>vat_infos</string>
                            <string>context</string>
                            <string>invoice</string>
                            <string>_getattr_</string>
                            <string>portal</string>
                            <string>accounting_movement_list</string>
                            <string>len</string>
                            <string>_getiter_</string>
                            <string>movement</string>
                            <string>account</string>
                            <string>None</string>
                            <string>_getitem_</string>
                            <string>float</string>
                            <string>_write_</string>
                            <string>invoice_movement</string>
                            <string>aggregated_simulation_movements</string>
                            <string>simulation_movement</string>
                            <string>applied_rule</string>
                          </tuple>
                        </value>
                    </item>
                  </dictionary>
                </state>
              </object>
            </value>
        </item>
        <item>
            <key> <string>func_defaults</string> </key>
            <value>
              <none/>
            </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>SaleInvoiceTransaction_getVAT</string> </value>
        </item>
        <item>
            <key> <string>warnings</string> </key>
            <value>
              <tuple/>
            </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
