Improved: Convert SampleCommissionServices.xml from mini-lang to Groovy (OFBIZ-13086)

Convert the service sampleInvoiceAffiliateCommission from minilang to groovy.

As is a sample service for example, I moved it to a dedicate file definition
diff --git a/applications/accounting/minilang/invoice/SampleCommissionServices.xml b/applications/accounting/minilang/invoice/SampleCommissionServices.xml
deleted file mode 100644
index 8823d63..0000000
--- a/applications/accounting/minilang/invoice/SampleCommissionServices.xml
+++ /dev/null
@@ -1,81 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-Licensed to the Apache Software Foundation (ASF) under one
-or more contributor license agreements.  See the NOTICE file
-distributed with this work for additional information
-regarding copyright ownership.  The ASF licenses this file
-to you under the Apache License, Version 2.0 (the
-"License"); you may not use this file except in compliance
-with the License.  You may obtain a copy of the License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing,
-software distributed under the License is distributed on an
-"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-KIND, either express or implied.  See the License for the
-specific language governing permissions and limitations
-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="sampleCalculateAffiliateCommission" short-description="Sample Calculate Affiliate Commission">
-        <entity-one entity-name="Payment" value-field="payment"/>
-
-        <!-- find affiliate or commission partner for payment.partyIdFrom; will be relationship from CUSTOMER to AFFILIATE, type SALES_AFFILIATE -->
-        <entity-and entity-name="PartyRelationship" list="affiliatePartyRelationshipList" filter-by-date="true">
-            <field-map field-name="partyIdFrom" from-field="payment.partyIdFrom"/>
-            <field-map field-name="roleTypeIdFrom" value="CUSTOMER"/>
-            <field-map field-name="partyRelationshipTypeId" value="SALES_AFFILIATE"/><!-- this constraint could be optional if not being set in company data -->
-        </entity-and>
-
-        <iterate list="affiliatePartyRelationshipList" entry="affiliatePartyRelationship">
-            <!-- calculate a commission for each commission partner, identified by affiliatePartyRelationship.partyIdTo -->
-            <if>
-                <condition><if-compare field="affiliatePartyRelationship.roleTypeIdTo" operator="equals" value="AFFILIATE"/></condition>
-                <then>
-                    <!-- calculate a simple commission with a base amount + a percentage of the payment -->
-                    <calculate field="commissionAmount">
-                        <calcop operator="add">
-                            <number value="10.0"/><!-- base amount -->
-                            <calcop operator="multiply">
-                                <calcop operator="get" field="payment.amount"/>
-                                <number value="0.15"/><!-- commission percentage -->
-                            </calcop>
-                        </calcop>
-                    </calculate>
-
-                    <set field="commissionPartyId" from-field="affiliatePartyRelationship.partyIdTo"/>
-                    <call-simple-method method-name="createCommissionInvoiceInline"/>
-                </then>
-                <else-if>
-                    <!-- NOTE: this is just an example of another type of commission partner associated with a customer, doesn't really exist -->
-                    <condition><if-compare field="affiliatePartyRelationship.roleTypeIdTo" operator="equals" value="TIERED_COMMISSION"/></condition>
-                    <then></then>
-                </else-if>
-            </if>
-        </iterate>
-    </simple-method>
-
-    <simple-method method-name="createCommissionInvoiceInline" short-description="Create Commission Invoice Inline">
-        <!-- should have in the context: payment, commissionAmount, commissionPartyId -->
-        <!-- this will create an invoice on behalf of the commission party, ie from the commissionPartyId to the company (payment.partyIdTo) -->
-
-        <set field="createInvoiceMap.invoiceTypeId" value="COMMISSION_INVOICE"/>
-        <set field="createInvoiceMap.statusId" value="INVOICE_RECEIVED"/>
-        <set field="createInvoiceMap.partyIdFrom" from-field="commissionPartyId"/>
-        <set field="createInvoiceMap.partyId" from-field="payment.partyIdTo"/>
-        <call-service service-name="createInvoice" in-map-name="createInvoiceMap">
-            <result-to-field result-name="createdInvoiceId"/>
-        </call-service>
-
-        <set field="createInvoiceItemMap.invoiceId" from-field="createdInvoiceId"/>
-        <set field="createInvoiceItemMap.invoiceItemTypeId" value="COMM_INV_ITEM"/>
-        <set field="createInvoiceItemMap.amount" from-field="commissionAmount"/>
-        <set field="createInvoiceItemMap.quantity" value="1" type="BigDecimal"/>
-        <set field="createInvoiceItemMap.description" value="Commission for Received Customer Payment [${payment.paymentId}]"/>
-        <call-service service-name="createInvoiceItem" in-map-name="createInvoiceItemMap"/>
-    </simple-method>
-</simple-methods>
diff --git a/applications/accounting/ofbiz-component.xml b/applications/accounting/ofbiz-component.xml
index 2d4f4be..4f3ca68 100644
--- a/applications/accounting/ofbiz-component.xml
+++ b/applications/accounting/ofbiz-component.xml
@@ -46,6 +46,7 @@
     <service-resource type="model" loader="main" location="servicedef/services_tax.xml"/>
     <service-resource type="model" loader="main" location="servicedef/services_rate.xml"/>
     <service-resource type="model" loader="main" location="servicedef/services_cost.xml"/>
+    <service-resource type="model" loader="main" location="servicedef/services_sample.xml"/>
 
     <!-- Payment Processor/Gateway Service Definitions -->
     <service-resource type="model" loader="main" location="servicedef/services_sagepay.xml"/>
diff --git a/applications/accounting/servicedef/services_invoice.xml b/applications/accounting/servicedef/services_invoice.xml
index bdfd721..0bd0690 100644
--- a/applications/accounting/servicedef/services_invoice.xml
+++ b/applications/accounting/servicedef/services_invoice.xml
@@ -223,16 +223,6 @@
         <attribute name="invoiceIds" type="List" mode="IN"/>
         <attribute name="invoicesCreated" type="List" mode="OUT" optional="true"/>
     </service>
-    <service name="sampleInvoiceAffiliateCommission" engine="simple"
-        location="component://accounting/minilang/invoice/SampleCommissionServices.xml" invoke="sampleCalculateAffiliateCommission" auth="true">
-        <description>
-            A sample/example service to calculate an affiliate commission (direct relationship to customer) and create
-            and invoice for it on behalf of the affiliate, ie an invoice from the affiliate to the company that can
-            then be paid by the company to balance it.
-        </description>
-        <attribute name="paymentId" type="String" mode="IN"/>
-        <attribute name="invoiceId" type="String" mode="OUT"/>
-    </service>
     <service name="readyInvoices" engine="java"
         location="org.apache.ofbiz.accounting.invoice.InvoiceServices" invoke="readyInvoices">
         <description>
diff --git a/applications/accounting/servicedef/services_sample.xml b/applications/accounting/servicedef/services_sample.xml
new file mode 100644
index 0000000..baab24b
--- /dev/null
+++ b/applications/accounting/servicedef/services_sample.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+        xsi:noNamespaceSchemaLocation="https://ofbiz.apache.org/dtds/services.xsd">
+    <description>Accounting Sample Services</description>
+    <vendor>OFBiz</vendor>
+    <version>1.0</version>
+
+    <service name="sampleInvoiceAffiliateCommission" engine="groovy" auth="true"
+        location="component://accounting/src/main/groovy/org/apache/ofbiz/accounting/invoice/SampleServices.groovy" invoke="sampleCalculateAffiliateCommission">
+        <description>
+            A sample/example service to calculate an affiliate commission (direct relationship to customer) and create
+            and invoice for it on behalf of the affiliate, ie an invoice from the affiliate to the company that can
+            then be paid by the company to balance it.
+        </description>
+        <attribute name="paymentId" type="String" mode="IN"/>
+        <attribute name="invoiceIds" type="List" mode="OUT"/>
+    </service>
+</services>
diff --git a/applications/accounting/src/main/groovy/org/apache/ofbiz/accounting/invoice/SampleServices.groovy b/applications/accounting/src/main/groovy/org/apache/ofbiz/accounting/invoice/SampleServices.groovy
new file mode 100644
index 0000000..84cf13c
--- /dev/null
+++ b/applications/accounting/src/main/groovy/org/apache/ofbiz/accounting/invoice/SampleServices.groovy
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+*/
+package org.apache.ofbiz.accounting.invoice
+
+import org.apache.ofbiz.entity.GenericValue
+
+/**
+ * Sample Calculate Affiliate Commission
+ * @return Success response
+ */
+Map sampleCalculateAffiliateCommission() {
+    List invoiceIds = []
+    GenericValue payment = from('Payment').where(parameters).queryOne()
+
+    // find affiliate or commission partner for payment.partyIdFrom; will be relationship from CUSTOMER to AFFILIATE, type SALES_AFFILIATE
+    from('PartyRelationship')
+            .where(partyIdFrom: payment.partyIdFrom,
+                    roleTypeIdFrom: 'CUSTOMER',
+                    partyRelationshipTypeId: 'SALES_AFFILIATE')
+            .filterByDate()
+            .queryList()
+            .each {
+                // Calculate a commission for each commission partner, identified by affiliatePartyRelationship.partyIdTo
+                if (it.roleTypeIdTo == 'AFFILIATE') {
+                    BigDecimal commissionAmount = 10.0 + payment.amount * 0.15
+                    invoiceIds << createCommissionInvoiceInline(payment, it.partyIdTo, commissionAmount).invoiceId
+                } else if (it.roleTypeIdTo == 'TIERED_COMMISSION') {
+                    // NOTE: this is just an example of another type of commission partner associated with a customer, doesn't really exist
+                    invoiceIds << 'something to do'
+                }
+            }
+    return success([invoiceIds: invoiceIds])
+}
+
+Map createCommissionInvoiceInline(GenericValue payment, String commissionPartyId, BigDecimal commissionAmount) {
+    Map serviceResult = run service: 'createInvoice', with: [invoiceTypeId: 'COMMISSION_INVOICE',
+                                                             statusId: 'INVOICE_RECEIVED',
+                                                             partyIdFrom: commissionPartyId,
+                                                             partyId: payment.partyIdTo]
+    run service: 'createInvoiceItem', with: [invoiceId: serviceResult.invoiceId,
+                                             invoiceItemTypeId: 'COMM_INV_ITEM',
+                                             amount: commissionAmount,
+                                             quantity: 1,
+                                             description: "Commission for Received Customer Payment [${payment.paymentId}]"]
+    return serviceResult
+}