Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved: Convert OrderServices.xml mini-lang to groovyDSL : getOrder…
…edSummaryInformation

(OFBIZ-9984)
Thanks Julien for your contribution
  • Loading branch information
Gil Portenseigne committed Mar 6, 2020
1 parent eba4157 commit 53a8b81
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 71 deletions.
67 changes: 66 additions & 1 deletion applications/order/groovyScripts/order/OrderServices.groovy
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
68 changes: 0 additions & 68 deletions applications/order/minilang/order/OrderServices.xml
Expand Up @@ -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">
Expand Down
4 changes: 2 additions & 2 deletions applications/order/servicedef/services.xml
Expand Up @@ -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 -->
Expand Down

0 comments on commit 53a8b81

Please sign in to comment.