You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by pg...@apache.org on 2020/03/06 14:49:16 UTC

[ofbiz-framework] 02/02: Improved: Convert OrderServices.xml mini-lang to groovyDSL : getOrderedSummaryInformation

This is an automated email from the ASF dual-hosted git repository.

pgil pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git

commit 53a8b812607f9987a1063f18062a5318cafe444c
Author: Gil Portenseigne <gi...@nereide.fr>
AuthorDate: Fri Mar 6 15:17:47 2020 +0100

    Improved: Convert OrderServices.xml mini-lang to groovyDSL : getOrderedSummaryInformation
    
    (OFBIZ-9984)
    Thanks Julien for your contribution
---
 .../order/groovyScripts/order/OrderServices.groovy | 67 ++++++++++++++++++++-
 .../order/minilang/order/OrderServices.xml         | 68 ----------------------
 applications/order/servicedef/services.xml         |  4 +-
 3 files changed, 68 insertions(+), 71 deletions(-)

diff --git a/applications/order/groovyScripts/order/OrderServices.groovy b/applications/order/groovyScripts/order/OrderServices.groovy
index 323b8a8..9fd9b8f 100644
--- a/applications/order/groovyScripts/order/OrderServices.groovy
+++ b/applications/order/groovyScripts/order/OrderServices.groovy
@@ -18,7 +18,7 @@
  */
 
 
-import org.apache.ofbiz.base.util.UtilDateTime
+import groovy.time.TimeCategory
 import org.apache.ofbiz.entity.GenericValue
 import org.apache.ofbiz.entity.condition.EntityConditionBuilder
 
@@ -74,4 +74,69 @@ def getNextOrderId() {
     return result
 }
 
+/**
+ * Service to get Summary Information About Orders for a Customer
+ */
+def getOrderedSummaryInformation() {
+    /*
+    // The permission checking is commented out to make this service work also when triggered from ecommerce
+    if (!security.hasEntityPermission('ORDERMGR', '_VIEW', session && !parameters.partyId.equals(userLogin.partyId))) {
+        Map result = error('To get order summary information you must have the ORDERMGR_VIEW permission, or
+        be logged in as the party to get the summary information for.')
+        return result
+    }
+    */
+    Timestamp fromDate = null, thruDate = null
+    Date now = new Date()
+    if (monthsToInclude) {
+        use(TimeCategory) {
+            thruDate = now.toTimestamp()
+            fromDate = (now - monthsToInclude.months).toTimestamp()
+        }
+    }
+
+    roleTypeId = roleTypeId ?: 'PLACING_CUSTOMER'
+    orderTypeId = orderTypeId ?: 'SALES_ORDER'
+    statusId = statusId ?: 'ORDER_COMPLETED'
+
+    //find the existing exchange rates
+    exprBldr = new EntityConditionBuilder()
+
+    def condition = exprBldr.AND() {
+        EQUALS(partyId: partyId)
+        EQUALS(roleTypeId: roleTypeId)
+        EQUALS(orderTypeId: orderTypeId)
+        EQUALS(statusId: statusId)
+    }
+
+    if (fromDate) {
+        condition = exprBldr.AND(condition) {
+            condition
+            exprBldr.OR() {
+                GREATER_THAN_EQUAL_TO(orderDate: fromDate)
+                EQUALS(orderDate: null)
+            }
+        }
+    }
+
+    if (thruDate) {
+        condition = exprBldr.AND(condition) {
+            condition
+            exprBldr.OR() {
+                LESS_THAN_EQUAL_TO(orderDate: thruDate)
+                EQUALS(orderDate: null)
+            }
+        }
+    }
+
+    orderInfo = select('partyId', 'roleTypeId', 'totalGrandAmount', 'totalSubRemainingAmount', 'totalOrders')
+            .from('OrderHeaderAndRoleSummary').where(condition).queryFirst()
+
+    // first set the required OUT fields to zero
+    result = success()
+    result.totalGrandAmount = orderInfo ? orderInfo.totalGrandAmount : BigDecimal.ZERO
+    result.totalSubRemainingAmount = orderInfo ? orderInfo.totalSubRemainingAmount : BigDecimal.ZERO
+    result.totalOrders = orderInfo ? orderInfo.totalOrders : 0l
+
+    return result
 }
diff --git a/applications/order/minilang/order/OrderServices.xml b/applications/order/minilang/order/OrderServices.xml
index d357c0a..b4ab784 100644
--- a/applications/order/minilang/order/OrderServices.xml
+++ b/applications/order/minilang/order/OrderServices.xml
@@ -20,74 +20,6 @@ under the License.
 
 <simple-methods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://ofbiz.apache.org/Simple-Method" xsi:schemaLocation="http://ofbiz.apache.org/Simple-Method http://ofbiz.apache.org/dtds/simple-methods.xsd">
-    <simple-method method-name="getOrderedSummaryInformation" short-description="Get Summary Information About Orders for a Customer">
-        <!-- The permission checking is commented out to make this service work also when triggered from ecommerce -->
-        <!--if>
-            <condition>
-                <and>
-                    <not><if-has-permission permission="ORDERMGR" action="_VIEW"/></not>
-                    <if-compare-field field="parameters.partyId" to-field="userLogin.partyId" operator="not-equals"/>
-                </and>
-            </condition>
-            <then>
-                <string-to-list string="To get order summary information you must have the ORDERMGR_VIEW permission, or be logged in as the party to get the summary information for." list="error_list"/>
-            </then>
-        </if>
-        <check-errors/>
-       -->
-        <if-not-empty field="monthsToInclude">
-            <now-timestamp field="nowTimestamp"/>
-            <!-- TODO: Change this to use the <set-calendar> operation -->
-            <script>groovy:
-                calendar = com.ibm.icu.util.Calendar.getInstance()
-                calendar.setTimeInMillis(nowTimestamp.getTime())
-                calendar.add(com.ibm.icu.util.Calendar.MONTH, -monthsToInclude.intValue())
-                parameters.put("fromDate", new Timestamp(calendar.getTimeInMillis()))
-            </script>
-            <set from-field="nowTimestamp" field="parameters.thruDate"/>
-        </if-not-empty>
-
-        <if-empty field="parameters.roleTypeId">
-            <set value="PLACING_CUSTOMER" field="parameters.roleTypeId"/>
-        </if-empty>
-        <if-empty field="parameters.orderTypeId">
-            <set value="SALES_ORDER" field="parameters.orderTypeId"/>
-        </if-empty>
-        <if-empty field="parameters.statusId">
-            <set value="ORDER_COMPLETED" field="parameters.statusId"/>
-        </if-empty>
-
-        <entity-condition entity-name="OrderHeaderAndRoleSummary" list="orderInfoList">
-            <condition-list combine="and">
-                <condition-expr field-name="partyId" operator="equals" from-field="parameters.partyId"/>
-                <condition-expr field-name="roleTypeId" operator="equals" from-field="parameters.roleTypeId"/>
-                <condition-expr field-name="orderTypeId" operator="equals" from-field="parameters.orderTypeId"/>
-                <condition-expr field-name="statusId" operator="equals" from-field="parameters.statusId"/>
-                <condition-expr field-name="orderDate" operator="greater-equals" from-field="parameters.fromDate" ignore-if-null="true"/>
-                <condition-expr field-name="orderDate" operator="less-equals" from-field="parameters.thruDate" ignore-if-null="true"/>
-            </condition-list>
-            <select-field field-name="partyId"/>
-            <select-field field-name="roleTypeId"/>
-            <select-field field-name="totalGrandAmount"/>
-            <select-field field-name="totalSubRemainingAmount"/>
-            <select-field field-name="totalOrders"/>
-        </entity-condition>
-
-        <!-- first set the required OUT fields to zero -->
-        <calculate field="plainDoubleZero"><number value="0.0"/></calculate>
-        <calculate field="plainLongZero" type="Long"><number value="0"/></calculate>
-        <field-to-result field="plainDoubleZero" result-name="totalGrandAmount"/>
-        <field-to-result field="plainDoubleZero" result-name="totalSubRemainingAmount"/>
-        <field-to-result field="plainLongZero" result-name="totalOrders"/>
-
-        <!-- because we specified the partyId and the roleTypeId, should only be one item in list returned -->
-        <first-from-list list="orderInfoList" entry="orderInfo"/>
-        <if-not-empty field="orderInfo">
-            <field-to-result field="orderInfo.totalGrandAmount" result-name="totalGrandAmount"/>
-            <field-to-result field="orderInfo.totalSubRemainingAmount" result-name="totalSubRemainingAmount"/>
-            <field-to-result field="orderInfo.totalOrders" result-name="totalOrders"/>
-        </if-not-empty>
-    </simple-method>
 
     <!-- order requirement methods -->
     <simple-method method-name="createRequirementAndCommitment" short-description="create a requirement and commitment for it">
diff --git a/applications/order/servicedef/services.xml b/applications/order/servicedef/services.xml
index 25d68c0..7084e79 100644
--- a/applications/order/servicedef/services.xml
+++ b/applications/order/servicedef/services.xml
@@ -475,8 +475,8 @@ under the License.
         <attribute name="orderId" type="String" mode="IN"/>
     </service>
     <!-- Order View Services -->
-    <service name="getOrderedSummaryInformation" engine="simple"
-            location="component://order/minilang/order/OrderServices.xml" invoke="getOrderedSummaryInformation">
+    <service name="getOrderedSummaryInformation" engine="groovy"
+            location="component://order/groovyScripts/order/OrderServices.groovy" invoke="getOrderedSummaryInformation">
         <description>Get Ordered Summary Information</description>
         <attribute name="partyId" type="String" mode="IN" optional="false"/>
         <attribute name="roleTypeId" type="String" mode="IN" optional="true"/> <!-- defaults to PLACING_CUSTOMER -->