From f400674c7daf17c835ca6dfbbb85545d529180a9 Mon Sep 17 00:00:00 2001
From: Sebastien Robin <seb@nexedi.com>
Date: Tue, 22 May 2007 21:07:37 +0000
Subject: [PATCH] fixed bug in Base_checkCheck, add more checks in
 Base_checkOrCreateCheck

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@14554 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../Base_checkCheck.xml                       | 26 ++++---------------
 .../Base_checkOrCreateCheck.xml               | 25 ++++++++++++++++--
 bt5/erp5_banking_check/bt/revision            |  2 +-
 3 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/bt5/erp5_banking_check/SkinTemplateItem/portal_skins/erp5_banking_check_operation/Base_checkCheck.xml b/bt5/erp5_banking_check/SkinTemplateItem/portal_skins/erp5_banking_check_operation/Base_checkCheck.xml
index bbe78c05687..de35974fc67 100644
--- a/bt5/erp5_banking_check/SkinTemplateItem/portal_skins/erp5_banking_check_operation/Base_checkCheck.xml
+++ b/bt5/erp5_banking_check/SkinTemplateItem/portal_skins/erp5_banking_check_operation/Base_checkCheck.xml
@@ -65,24 +65,13 @@
         </item>
         <item>
             <key> <string>_body</string> </key>
-            <value> <string encoding="cdata"><![CDATA[
-
-from Products.ERP5Type.Message import Message\n
+            <value> <string>from Products.ERP5Type.Message import Message\n
 from Products.DCWorkflow.DCWorkflow import ValidationFailed\n
 \n
 # Check that a check exists for given bank account and reference.\n
-result = context.Base_checkOrCreateCheck(reference=reference, \n
+check = context.Base_checkOrCreateCheck(reference=reference, \n
                                          bank_account=bank_account,\n
                                          resource=resource)\n
-result_len = len(result)\n
-if result_len == 0:\n
-  msg = Message(domain=\'ui\', message="No such check")\n
-  raise ValidationFailed, (msg,)\n
-elif result_len > 1:\n
-  msg = Message(domain=\'ui\', message=\'Duplicate checks for the same check number\')\n
-  raise ValidationFailed, (msg,)\n
-\n
-check = result[0].getObject()\n
 \n
 bad_simulation_state_dict = {\'draft\': \'The check is not issued yet.\',\n
                              \'cancelled\': \'The check has been stopped.\',\n
@@ -98,9 +87,7 @@ if simulation_state != \'confirmed\':\n
   raise ValidationFailed, (msg,)\n
 \n
 return check\n
-
-
-]]></string> </value>
+</string> </value>
         </item>
         <item>
             <key> <string>_code</string> </key>
@@ -157,14 +144,11 @@ return check\n
                             <string>ValidationFailed</string>
                             <string>_getattr_</string>
                             <string>context</string>
-                            <string>result</string>
-                            <string>len</string>
-                            <string>result_len</string>
-                            <string>msg</string>
-                            <string>_getitem_</string>
                             <string>check</string>
                             <string>bad_simulation_state_dict</string>
                             <string>simulation_state</string>
+                            <string>_getitem_</string>
+                            <string>msg</string>
                           </tuple>
                         </value>
                     </item>
diff --git a/bt5/erp5_banking_check/SkinTemplateItem/portal_skins/erp5_banking_check_operation/Base_checkOrCreateCheck.xml b/bt5/erp5_banking_check/SkinTemplateItem/portal_skins/erp5_banking_check_operation/Base_checkOrCreateCheck.xml
index 8768e65e216..55c078d79c5 100644
--- a/bt5/erp5_banking_check/SkinTemplateItem/portal_skins/erp5_banking_check_operation/Base_checkOrCreateCheck.xml
+++ b/bt5/erp5_banking_check/SkinTemplateItem/portal_skins/erp5_banking_check_operation/Base_checkOrCreateCheck.xml
@@ -121,6 +121,9 @@ check_list = []\n
 bank_account_uid = bank_account.getUid()\n
 resource_value = None\n
 generic_model = None\n
+reference_dict = {}\n
+# First we must parse everyting to make sure there is no error,\n
+# this is safer because we catch Validation in workflow scripts\n
 for check_reference in reference_list:\n
   message_tag = \'check_%s_%s_%s\' % (resource, bank_account_uid, check_reference)\n
   # just raise an error.\n
@@ -130,11 +133,26 @@ for check_reference in reference_list:\n
   result = context.portal_catalog(portal_type = \'Check\', reference = check_reference, \n
                                   destination_payment_uid = bank_account.getUid(),\n
                                   default_resource_relative_url = resource)\n
-  if len(result) == 0:\n
+  result_len = len(result)\n
+  if result_len == 0:\n
     if not context.Base_isAutomaticCheckCreationAllowed():\n
       msg = Message(domain = "ui", message="Sorry, this reference does not exist")\n
       raise ValidationFailed, (msg,)\n
 \n
+  elif result_len >= 1:\n
+    msg = Message(domain=\'ui\', message=\'Duplicate checks for the same check number\')\n
+    raise ValidationFailed, (msg,)\n
+\n
+  reference_dict[check_reference] = {}\n
+  reference_dict[check_reference][\'result\'] = result\n
+  reference_dict[check_reference][\'result_len\'] = result_len\n
+  reference_dict[check_reference][\'message_tag\'] = message_tag\n
+\n
+for check_reference in reference_list:\n
+  result_len = reference_dict[check_reference][\'result_len\']\n
+  result = reference_dict[check_reference][\'result\']\n
+  message_tag = reference_dict[check_reference][\'message_tag\']\n
+  if result_len == 0:\n
     # This happens only if automatic creation is allowed. So create a new check at this point.\n
     # Get a checkbook for this bank account.\n
     checkbook = None\n
@@ -260,13 +278,16 @@ return check_list\n
                             <string>bank_account_uid</string>
                             <string>resource_value</string>
                             <string>generic_model</string>
+                            <string>reference_dict</string>
                             <string>check_reference</string>
                             <string>message_tag</string>
                             <string>result</string>
                             <string>len</string>
+                            <string>result_len</string>
+                            <string>_write_</string>
+                            <string>_getitem_</string>
                             <string>checkbook</string>
                             <string>composition_related_list</string>
-                            <string>_getitem_</string>
                             <string>brain</string>
                             <string>obj</string>
                             <string>checkbook_tag</string>
diff --git a/bt5/erp5_banking_check/bt/revision b/bt5/erp5_banking_check/bt/revision
index 1199cd493e5..fca7fbef0e3 100644
--- a/bt5/erp5_banking_check/bt/revision
+++ b/bt5/erp5_banking_check/bt/revision
@@ -1 +1 @@
-219
\ No newline at end of file
+223
\ No newline at end of file
-- 
2.30.9