<?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[

from Products.DCWorkflow.DCWorkflow import ValidationFailed\n
""" This scripts validate that this transaction fits in the defined budget. It requires erp5_budget.\n
"""\n
transaction = state_change[\'object\']\n
\n
# checks if there is enough budget before validating this transaction\n
AccountingTransaction_viewAccountingTransactionLineList = transaction.contentValues(\n
                    filter={\'portal_type\': (\'Accounting Transaction Line\', \n
                                            \'Sale Invoice Transaction Line\', \n
                                            \'Pay Sheet Transaction Line\',\n
                                            \'Purchase Invoice Transaction Line\' )})\n
\n
# delete all objects which are mapped values (predicates) in the budget module\n
id_list = []\n
for objects in transaction.getObject().getPortalObject().budget_module.objectValues():\n
  if objects.getPortalType() == \'Mapped Value\':\n
    id_list.append(objects.getId())\n
context.budget_module.manage_delObjects(id_list)\n
\n
# Create predicates (mapped values) from budget cells\n
for objects in transaction.getObject().getPortalObject().budget_module.objectValues():\n
  if objects.getPortalType() == \'Budget\':\n
    for obj_line in objects.objectValues():\n
      if obj_line.getPortalType() == \'Budget Line\':\n
        for obj in obj_line.objectValues():\n
          financial_section = obj.getMembershipCriterionCategoryList()[0]\n
          function = obj.getMembershipCriterionCategoryList()[1]\n
          group = obj.getMembershipCriterionCategoryList()[2]\n
          budget = obj.getQuantity() + obj.getCurrentInventory() - obj.getConsumedBudget()\n
          budget_module = obj.getPortalObject().budget_module\n
          predicate = budget_module.newContent(portal_type=\'Mapped Value\')\n
          predicate.setMembershipCriterionBaseCategoryList([\'financial_section\',\'function\',\'group\'])\n
          predicate.setMembershipCriterionCategoryList([financial_section,function,group])\n
          predicate.setCriterionPropertyList([\'quantity\',\'stop_date\'])\n
          predicate.setCriterion(\'quantity\',max=budget)\n
          predicate.setCriterion(\'stop_date\',min=obj.getStartDate(),max=obj.getStopDate())\n
          predicate.immediateReindexObject()\n
\n
for line in AccountingTransaction_viewAccountingTransactionLineList:\n
  if line.getSource() and line.getSourceDebit()>0 and line.getPortalType() != "Sale Invoice Transaction Line":\n
    new_id = \'atl\' + str(line.getId())\n
    financial_section = line.getSourceValue().getFinancialSectionValue().getCategoryRelativeUrl(base=0)\n
    function_category = line.getParent().getSourceFunction()\n
    function = context.portal_categories.getCategoryValue(function_category).getCategoryRelativeUrl(base=0)\n
    group = line.getParent().getSourceSectionValue().getGroup() \n
    quantity = -line.getQuantity()\n
    temp_obj = line.asContext(\n
                id                    = new_id\n
               , title                = line.getTitle()\n
               , start_date           = line.getStartDate()\n
               , stop_date            = line.getStopDate()\n
               , quantity             = quantity\n
               , financial_section    = financial_section\n
               , function             = function\n
               , group                = group\n
               )\n
\n
    temp_obj.setFinancialSection(financial_section)\n
    temp_obj.setFunction(function)\n
    temp_obj.setGroup(group)\n
    temp_obj.setQuantity(quantity)\n
    enough_budget = 0\n
    for objects in context.getPortalObject().budget_module.objectValues():\n
      if objects.getPortalType() == \'Mapped Value\': \n
         res = objects.test(temp_obj)\n
         if res: \n
            enough_budget = 1   \n
    if not enough_budget :\n
      error_message = \'Insuficient or Undefined Budget for the Transaction Source Account\'\n
      raise ValidationFailed, \'Impossible Action: \'+error_message\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>state_change</string> </value>
        </item>
        <item>
            <key> <string>_proxy_roles</string> </key>
            <value>
              <tuple/>
            </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>1</int> </value>
                    </item>
                    <item>
                        <key> <string>co_varnames</string> </key>
                        <value>
                          <tuple>
                            <string>state_change</string>
                            <string>_getitem_</string>
                            <string>transaction</string>
                            <string>_getattr_</string>
                            <string>AccountingTransaction_viewAccountingTransactionLineList</string>
                            <string>id_list</string>
                            <string>_getiter_</string>
                            <string>objects</string>
                            <string>context</string>
                            <string>obj_line</string>
                            <string>obj</string>
                            <string>financial_section</string>
                            <string>function</string>
                            <string>group</string>
                            <string>budget</string>
                            <string>budget_module</string>
                            <string>predicate</string>
                            <string>line</string>
                            <string>str</string>
                            <string>new_id</string>
                            <string>function_category</string>
                            <string>quantity</string>
                            <string>temp_obj</string>
                            <string>enough_budget</string>
                            <string>res</string>
                            <string>error_message</string>
                            <string>ValidationFailed</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>validateBudget</string> </value>
        </item>
        <item>
            <key> <string>warnings</string> </key>
            <value>
              <tuple/>
            </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
