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

"""Validates the transaction for both source and destination section.\n
"""\n
\n
from Products.DCWorkflow.DCWorkflow import ValidationFailed\n
from Products.ERP5Type.Message import Message\n
\n
transaction = state_change[\'object\']\n
N_ = lambda msg, **kw: Message(\'erp5_ui\', msg, **kw)\n
\n
# do we have to check transaction is in openned periods ? \n
skip_period_validation = state_change[\'kwargs\'].get(\n
                              \'skip_period_validation\', 0)\n
transition = state_change[\'transition\']\n
if transition.id in (\'plan_action\', \'confirm_action\') :\n
  skip_period_validation = 1\n
\n
# do we have to care about destination section ?\n
check_destination = (transaction.getPortalType() in\n
                     transaction.getPortalInvoiceTypeList())\n
\n
# Get sections and a currency.\n
source_section = transaction.getSourceSectionValue(\n
                       portal_type=[\'Organisation\', \'Person\'])\n
if source_section is None:\n
  raise ValidationFailed(N_(\'Source Section is not Defined.\'))\n
\n
destination_section = transaction.getDestinationSectionValue(\n
                portal_type=[\'Organisation\', \'Person\'])\n
# if it\'s not an invoice, then we can validate without destination\n
if destination_section is None and check_destination :\n
  raise ValidationFailed(N_(\'Destination Section is not Defined.\'))\n
\n
currency = transaction.getResource(portal_type = \'Currency\')\n
if not currency :\n
  raise ValidationFailed(N_(\'Currency is not Defined.\'))\n
\n
# XXX manually default start date to stop date\n
if not transaction.getStartDate() and transaction.getStopDate():\n
  transaction.setStartDate(transaction.getStopDate())\n
\n
if not transaction.getStartDate() :\n
  raise ValidationFailed(N_(\'Date is not Defined\'))\n
else:\n
  if not skip_period_validation :\n
    valid_date = False\n
    # check the date is in an opened period\n
    transaction_date = transaction.getStartDate().earliestTime()\n
\n
    openned_accounting_period_list = source_section.searchFolder(\n
                               portal_type="Accounting Period",\n
                               # planned is for b/w compatibility\n
                               simulation_state=("planned", \'started\'))\n
    if len(openned_accounting_period_list) == 0 :\n
      # if the entity doesn\'t have any accounting period, we can\n
      # consider that they do not want to use accounting periods or\n
      # we do not account from their side.\n
      valid_date = True\n
    for apd in openned_accounting_period_list:\n
      apd = apd.getObject()\n
      if apd.getStartDate().Date() <= transaction_date.Date() <= apd.getStopDate().Date():\n
        valid_date = True\n
    if not valid_date :\n
      raise ValidationFailed(N_("Date is not in an opened Accounting Period "\n
                                "for source section"))\n
    # do the same for destination section \n
    if check_destination :\n
      valid_date = False\n
      transaction_date = transaction.getStopDate().earliestTime()\n
      openned_accounting_period_list = destination_section.searchFolder(\n
                                 portal_type = "Accounting Period",\n
                                 simulation_state = (\'planned\', \'started\'))\n
      if len(openned_accounting_period_list) == 0:\n
        valid_date = True\n
      for apd in openned_accounting_period_list:\n
        apd = apd.getObject()\n
        if apd.getStartDate().Date() <= transaction_date.Date() <= apd.getStopDate().Date():\n
          valid_date = True\n
      if not valid_date :\n
        raise ValidationFailed(N_("Date is not in an opened Accounting Period "\n
                                  "for destination section"))\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>
                <string>Anonymous</string>
                <string>Assignee</string>
                <string>Assignor</string>
                <string>Associate</string>
                <string>Auditor</string>
                <string>Authenticated</string>
                <string>Author</string>
                <string>Manager</string>
                <string>Member</string>
                <string>Owner</string>
                <string>Reviewer</string>
              </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>Products.DCWorkflow.DCWorkflow</string>
                            <string>ValidationFailed</string>
                            <string>Products.ERP5Type.Message</string>
                            <string>Message</string>
                            <string>_getitem_</string>
                            <string>transaction</string>
                            <string>N_</string>
                            <string>_getattr_</string>
                            <string>skip_period_validation</string>
                            <string>transition</string>
                            <string>check_destination</string>
                            <string>source_section</string>
                            <string>None</string>
                            <string>destination_section</string>
                            <string>currency</string>
                            <string>False</string>
                            <string>valid_date</string>
                            <string>transaction_date</string>
                            <string>openned_accounting_period_list</string>
                            <string>len</string>
                            <string>True</string>
                            <string>_getiter_</string>
                            <string>apd</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>validateTransaction</string> </value>
        </item>
        <item>
            <key> <string>warnings</string> </key>
            <value>
              <tuple/>
            </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
