From 4aad72e44ba41ffc48bca148f8fe6c9450e62ba2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Le=20Ninivin?= <cedric.leninivin@tiolive.com>
Date: Tue, 24 Jan 2017 08:35:18 +0000
Subject: [PATCH] erp5_travel_expense: Fix Creation of transaction to respect
 accounting constraints

---
 ...dationRequest_createPurchaseTransaction.py | 22 +++++++++++++++++++
 .../ExpenseValidationRequest_accept.xml       | 12 +++++-----
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/bt5/erp5_travel_expense/SkinTemplateItem/portal_skins/erp5_hr_request/ExpenseValidationRequest_createPurchaseTransaction.py b/bt5/erp5_travel_expense/SkinTemplateItem/portal_skins/erp5_hr_request/ExpenseValidationRequest_createPurchaseTransaction.py
index e590257bde..7373754892 100644
--- a/bt5/erp5_travel_expense/SkinTemplateItem/portal_skins/erp5_hr_request/ExpenseValidationRequest_createPurchaseTransaction.py
+++ b/bt5/erp5_travel_expense/SkinTemplateItem/portal_skins/erp5_hr_request/ExpenseValidationRequest_createPurchaseTransaction.py
@@ -23,6 +23,7 @@ transaction =  portal.accounting_module.newContent(
   resource=context.getPriceCurrency(),
   created_by_builder=1,  # XXX this prevent init script from creating lines.
   start_date=context.getStartDate(),
+  stop_date=context.getStartDate(),
   causality=context.getRelativeUrl(),
 )
 
@@ -38,6 +39,27 @@ transaction.newContent(
   quantity=(-float(context.getTotalPrice())),
 )
 
+from Products.DCWorkflow.DCWorkflow import ValidationFailed
+from zExceptions import Redirect
+try:
+  transaction.Base_checkConsistency()
+except ValidationFailed, error_message:
+  if getattr(error_message, 'msg', None):
+    # use of Message class to store message+mapping+domain
+    message = error_message.msg
+    if same_type(message, []):
+      message = '. '.join('%s' % x for x in message)
+    else:
+      message = str(message)
+  else:
+    message = str(error_message)
+  if len(message) > 2000: # too long message will generate a too long URI
+                          # that would become an error.
+    message = "%s ..." % message[:(2000 - 4)]
+  raise Redirect, "%s?portal_status_message=%s" % (
+    context.getAbsoluteUrl(),
+    message
+    )
 transaction.stop()
 
 return transaction.getRelativeUrl()
diff --git a/bt5/erp5_travel_expense/WorkflowTemplateItem/portal_workflow/expense_validation_request_interaction_workflow/interactions/ExpenseValidationRequest_accept.xml b/bt5/erp5_travel_expense/WorkflowTemplateItem/portal_workflow/expense_validation_request_interaction_workflow/interactions/ExpenseValidationRequest_accept.xml
index e91167f19e..cafc3902ae 100644
--- a/bt5/erp5_travel_expense/WorkflowTemplateItem/portal_workflow/expense_validation_request_interaction_workflow/interactions/ExpenseValidationRequest_accept.xml
+++ b/bt5/erp5_travel_expense/WorkflowTemplateItem/portal_workflow/expense_validation_request_interaction_workflow/interactions/ExpenseValidationRequest_accept.xml
@@ -27,15 +27,15 @@
         <item>
             <key> <string>after_script_name</string> </key>
             <value>
-              <list>
-                <string>ExpenseValidationRequest_accept</string>
-              </list>
+              <tuple/>
             </value>
         </item>
         <item>
             <key> <string>before_commit_script_name</string> </key>
             <value>
-              <tuple/>
+              <list>
+                <string>ExpenseValidationRequest_accept</string>
+              </list>
             </value>
         </item>
         <item>
@@ -67,9 +67,7 @@
         <item>
             <key> <string>portal_type_filter</string> </key>
             <value>
-              <list>
-                <string>Expense Validation Request</string>
-              </list>
+              <none/>
             </value>
         </item>
         <item>
-- 
2.30.9