Skip to content

Commit

Permalink
Merge pull request #35 from hotwax/34-add-attempted-order-item-and-br…
Browse files Browse the repository at this point in the history
…okered-order-item-count-in-orderroutingrun-result

Improved: Added order item count and brokered item count in order rou…
  • Loading branch information
dixitdeepak authored Jul 25, 2024
2 parents bd386ff + e48e7a2 commit 9bbef98
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
2 changes: 2 additions & 0 deletions entity/OrderRoutingEntities.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ under the License.
<field name="startDate" type="date-time"><description>Start date of the routing run.</description></field>
<field name="endDate" type="date-time"><description>End date of the routing run.</description></field>
<field name="routingResult" type="text-long"><description>Result or outcome of the routing run.</description></field>
<field name="orderItemCount" type="number-integer"><description>Store the attempted order item count.</description></field>
<field name="brokeredItemCount" type="number-integer"><description>Store the brokered order item count.</description></field>
<index name="IDX_ORRN_ROUTE">
<index-field name="routingGroupId"/>
<index-field name="orderRoutingId"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@
<display text="${(groovy.time.TimeCategory.minus(endDate, startDate))}"/>
</conditional-field>
</field>
<field name="orderItemCount">
<header-field show-order-by="true"></header-field>
<default-field><display/></default-field>
</field>
<field name="brokeredItemCount">
<header-field show-order-by="true"></header-field>
<default-field><display/></default-field>
</field>
<field name="findButton">
<header-field title="Find"><submit/></header-field><default-field>
<display text=" "/></default-field>
Expand Down
49 changes: 40 additions & 9 deletions service/co/hotwax/order/routing/OrderRoutingServices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
in-map="[routingGroupId:routingGroupId, orderRoutingId: orderRouting.orderRoutingId,
productStoreId: productStoreId, startDate:ec.user.nowTimestamp]"
out-map="routingRunResult"/>
<set field="attemptedItemCount" type="Long"/>
<script>
/*
Calling a service in a script with ignore-error="true".
Expand All @@ -139,8 +140,11 @@
try {
def orderRoutingResult = ec.service.sync().name("co.hotwax.order.routing.OrderRoutingServices.run#OrderRouting")
.requireNewTransaction(true)
.parameters([orderRoutingId: orderRouting.orderRoutingId, orderId: orderId, shipGroupSeqId: shipGroupSeqId])
.parameters([orderRoutingId: orderRouting.orderRoutingId, orderId: orderId, shipGroupSeqId: shipGroupSeqId,
routingRunId: routingRunResult.routingRunId])
.call()
attemptedItemCount = orderRoutingResult.attemptedItemCount
brokeredItemCount = orderRoutingResult.brokeredItemCount
} catch (Throwable e) {
ec.logger.error("There is an issue with run#OrderRouting", e)
}
Expand All @@ -159,7 +163,8 @@
</script>

<service-call name="update#co.hotwax.order.routing.OrderRoutingRun" transaction="force-new" ignore-error="true"
in-map="[routingRunId:routingRunResult.routingRunId,hasError:hasError, routingResult:routingResult, endDate:ec.user.nowTimestamp]"
in-map="[routingRunId:routingRunResult.routingRunId,hasError:hasError, routingResult:routingResult,
endDate:ec.user.nowTimestamp, orderItemCount: attemptedItemCount, brokeredItemCount: brokeredItemCount]"
out-map="routingRunResult"/>
</iterate>
<message type="success">Finished order routing for ${orderRoutingGroup.groupName} [${routingGroupId}] routing group.</message>
Expand All @@ -177,7 +182,12 @@
<parameter name="orderId"/>
<parameter name="shipGroupSeqId"/>
<parameter name="changeReasonEnumId"/>
<parameter name="routingRunId"/>
</in-parameters>
<out-parameters>
<parameter name="attemptedItemCount" type="Long"/>
<parameter name="brokeredItemCount" type="Long"/>
</out-parameters>
<actions>
<entity-find-one entity-name="co.hotwax.order.routing.OrderRouting" value-field="orderRouting" cache="true"/>
<if condition="!orderRouting">
Expand Down Expand Up @@ -247,11 +257,25 @@
</else>
</if>
</iterate>
<set field="single_line" value="true" type="Boolean"/>
<set field="queryCount" value="true" type="Boolean"/>
<set field="templateLoc" value="component://order-routing/sql/EligibleOrdersQuery.sql.ftl"/>
<set field="productStore" from="ec.entity.getEntityDefinition('org.apache.ofbiz.product.store.ProductStore')"/>
<set field="attemptedItemCount" value="0" type="Long"/>
<set field="brokeredItemCount" value="0" type="Long"/>
<log message="Fetching order with condition ${orderFilterConditions} for routing ${orderRouting.routingName} [${orderRoutingId}]"/>
<script><![CDATA[
Writer countWriter = new StringWriter()
ec.resourceFacade.template(templateLoc, countWriter)
//ec.logger.info("Eligible orders count sql: ${countWriter}")
ec.getEntity().getConnection(productStore.getEntityGroupName()).withCloseable ({ java.sql.Connection con ->
con.createStatement().withCloseable({ statement ->
statement.executeQuery(countWriter.toString()).withCloseable({resultSet ->
if (resultSet.next()) { attemptedItemCount = resultSet.getLong(1) }
})
})
})
Writer writer = new StringWriter()
queryCount = false;
ec.resourceFacade.template(templateLoc, writer)
//ec.logger.info("Eligible orders sql: ${writer}")
/** Note:
Expand Down Expand Up @@ -282,13 +306,14 @@
if (!ec.message.hasError()) {
actionResult = ec.service.sync().name("co.hotwax.order.routing.OrderRoutingServices.eval#OrderRoutingActions")
.parameters([orderId: nextValue.orderId, shipGroupSeqId: nextValue.shipGroupSeqId,orderItemSeqId: orderItemSeqId, changeReasonEnumId: changeReasonEnumId,
routingRuleId: routingRule.routingRuleId, suggestedFulfillmentLocations: ruleResult.suggestedFulfillmentLocations])
routingRuleId: routingRule.routingRuleId, suggestedFulfillmentLocations: ruleResult.suggestedFulfillmentLocations, routingRunId: routingRunId])
.requireNewTransaction(true)
.call()
if (ec.message.hasError()) {
ec.logger.warn("Ignoring order routing actions errors for ${routingRule.ruleName} [${routingRule.routingRuleId}] " + ec.message.getErrorsString())
ec.message.clearAll()
}
brokeredItemCount += actionResult?.brokeredItemCount ?: 0
if (!actionResult?.runNextRule) {
/* If an order is routed to a facility, all subsequent order routing rule executions should be excluded */
break
Expand Down Expand Up @@ -362,7 +387,8 @@
Writer writer = new StringWriter()
ec.resourceFacade.template(templateLoc, writer)
//ec.logger.info("======writer==${writer}==")
java.sql.Connection con = ec.getEntity().getConnection(productStore.getEntityGroupName())
//java.sql.Connection con = ec.getEntity().getConnection(productStore.getEntityGroupName())
ec.getEntity().getConnection(productStore.getEntityGroupName()).withCloseable ({ java.sql.Connection con ->
con.createStatement().withCloseable({ statement ->
statement.executeQuery(writer.toString()).withCloseable({resultSet ->
while (resultSet.next()) {
Expand All @@ -380,6 +406,7 @@
}
})
})
})
} catch (Exception e) {
ec.logger.error("Error executing order routing rule ${orderRoutingRule.ruleName} [${routingRuleId}] : ${e}")
}
Expand All @@ -397,15 +424,17 @@
<parameter name="shipGroupSeqId" required="true"/>
<parameter name="orderItemSeqId"/>
<parameter name="changeReasonEnumId"/>
<parameter name="routingRunId"/>
<parameter name="routingRuleId" required="true"/>
<parameter name="suggestedFulfillmentLocations" type="List"/>
</in-parameters>
<out-parameters>
<parameter name="runNextRule" type="Boolean"/>
<parameter name="facilityAllocation" type="List"/>
<parameter name="brokeredItemCount" type="Long"/>
</out-parameters>
<actions>
<set field="runNextRule" value="true" type="Boolean"/>
<set field="brokeredItemCount" value="0" type="Long"/>
<entity-find-one entity-name="co.hotwax.order.routing.OrderRoutingRule" value-field="routingRule" cache="true"/>
<entity-find-related-one value-field="routingRule" relationship-name="co.hotwax.order.routing.OrderRouting" to-value-field="orderRouting" cache="true"/>
<entity-find-related-one value-field="orderRouting" relationship-name="co.hotwax.order.routing.OrderRoutingGroup" to-value-field="orderRoutingGroup" cache="true"/>
Expand Down Expand Up @@ -448,6 +477,7 @@
//prepare map for brokered items
def queue = null, autoCancelDate = null, brokeredItemsSeqIds = [] ;
if (suggestedFulfillmentLocations) {
brokeredItemCount = suggestedFulfillmentLocations.size();
if (!changeReasonEnumId) {
changeReasonEnumId = "BROKERED";
}
Expand All @@ -458,7 +488,8 @@
filterMapList(facilityItems, ["facilityId":facilityId], false)
def items = facilityItems.collect { [orderItemSeqId: it.orderItemSeqId] }
facilityAllocation.add([facilityId:facilityId, items: items, comments: comments, routingRule: routingRuleName, changeReasonEnumId: changeReasonEnumId,
routingGroupId: orderRoutingGroup.routingGroupId, orderRoutingId:orderRouting.orderRoutingId, routingRuleId:routingRule.routingRuleId])
routingGroupId: orderRoutingGroup.routingGroupId, orderRoutingId:orderRouting.orderRoutingId, routingRuleId:routingRule.routingRuleId,
routingRunId: routingRunId])
}
}
def unfillableItemSeqIds = orderItems.orderItemSeqId.stream().filter(i -> !brokeredItemsSeqIds.contains(i)).collect(Collectors.toList())
Expand Down Expand Up @@ -491,7 +522,8 @@
if (queue || autoCancelDate || clearAutoCancelDate) {
facilityAllocation.add([facilityId: queue, comments: comments, routingRule:routingRuleName, changeReasonEnumId:changeReasonEnumId,
items: unfillableItemSeqIds.collect { [orderItemSeqId: it, autoCancelDate: autoCancelDate, clearAutoCancelDate: clearAutoCancelDate]},
routingGroupId: orderRoutingGroup.routingGroupId, orderRoutingId:orderRouting.orderRoutingId, routingRuleId:routingRule.routingRuleId])
routingGroupId: orderRoutingGroup.routingGroupId, orderRoutingId:orderRouting.orderRoutingId, routingRuleId:routingRule.routingRuleId,
routingRunId: routingRunId])
}
} else {
//If all items allocated no need to run next rule
Expand Down Expand Up @@ -696,7 +728,6 @@
</else>
</if>
</iterate>
<set field="single_line" value="false" type="Boolean"/>
<set field="templateLoc" value="component://order-routing/sql/EligibleOrdersQuery.sql.ftl"/>
<script><![CDATA[
Writer writer = new StringWriter()
Expand Down
9 changes: 8 additions & 1 deletion sql/EligibleOrdersQuery.sql.ftl
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<@compress single_line=single_line!false>
<@compress single_line=true>
<#macro buildSqlCondition fieldName filterCodnition>
${fieldName} ${Static["co.hotwax.order.routing.OrderRoutingHelper"].makeSqlWhere(filterCodnition)!}
</#macro>
SELECT
<#if queryCount!false>
count( distinct orderId, ORDER_ITEM_SEQ_ID)'itemCount'
<#else>
orderId,
shipGroupSeqId
<#if selectOrderItemSeqId>,ORDER_ITEM_SEQ_ID'orderItemSeqId' <#--conditional field to select --></#if>
</#if>
from
(SELECT
OH.ORDER_ID'orderId',
Expand Down Expand Up @@ -54,6 +58,7 @@ WHERE
</#list>
</#if>
)
<#if !queryCount!false>
GROUP BY
orderId,
shipGroupSeqId
Expand All @@ -73,4 +78,6 @@ ORDER BY
<#else>
orderDate ASC
</#if>
</#if>

</@compress>
2 changes: 1 addition & 1 deletion sql/InventorySourceSelector.sql.ftl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<@compress single_line=single_line!false>
<@compress single_line=true>
<#macro buildSqlCondition value>
${Static["co.hotwax.order.routing.OrderRoutingHelper"].makeSqlWhere(value)!}
</#macro>
Expand Down

0 comments on commit 9bbef98

Please sign in to comment.