You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by nm...@apache.org on 2021/09/15 15:37:13 UTC

[ofbiz-framework] branch trunk updated (052f347 -> 2ebf14e)

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

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


    from 052f347  Improved: Converted the setPaymentStatus service from XML to groovy DSL. (OFBIZ-11482)
     new 7230aa8  Improved: Convert createMatchingPaymentApplication service from mini-lang to groovy DSL (OFBIZ-11500)
     new 2ebf14e  Improved: Convert SystemInfoServices.xml mini lang to groovy (OFBIZ-11919)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../groovyScripts/payment/PaymentServices.groovy   | 117 +++++++++++++++
 .../minilang/payment/PaymentServices.xml           | 135 -----------------
 .../accounting/servicedef/services_payment.xml     |   4 +-
 .../ofbizsetup/SystemInfoServices.groovy           | 104 ++++++++++++++
 .../commonext/minilang/SystemInfoServices.xml      | 159 ---------------------
 applications/commonext/servicedef/services.xml     |  24 ++--
 6 files changed, 235 insertions(+), 308 deletions(-)
 create mode 100644 applications/commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy
 delete mode 100644 applications/commonext/minilang/SystemInfoServices.xml

[ofbiz-framework] 02/02: Improved: Convert SystemInfoServices.xml mini lang to groovy (OFBIZ-11919)

Posted by nm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 2ebf14eddd0d75ba5630b26c8e451e82703397e9
Author: Nicolas Malin <ni...@nereide.fr>
AuthorDate: Wed Sep 15 17:35:49 2021 +0200

    Improved: Convert SystemInfoServices.xml mini lang to groovy (OFBIZ-11919)
    
    Thanks to Rohit Koushal to submit the patch
---
 .../ofbizsetup/SystemInfoServices.groovy           | 104 ++++++++++++++
 .../commonext/minilang/SystemInfoServices.xml      | 159 ---------------------
 applications/commonext/servicedef/services.xml     |  24 ++--
 3 files changed, 116 insertions(+), 171 deletions(-)

diff --git a/applications/commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy b/applications/commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy
new file mode 100644
index 0000000..25071cb
--- /dev/null
+++ b/applications/commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy
@@ -0,0 +1,104 @@
+/*
+ * 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.
+ */
+
+import org.apache.ofbiz.base.util.UtilDateTime
+import org.apache.ofbiz.entity.GenericValue
+
+def createSystemInfoNote() {
+    parameters.noteParty = parameters.noteParty ?: userLogin.partyId
+    GenericValue noteData = makeValue("NoteData", parameters)
+    noteData.noteDateTime = UtilDateTime.nowTimestamp()
+    noteData.noteName = "SYSTEMNOTE"
+    noteData.setNextSeqId()
+    noteData.create()
+    return success()
+}
+
+def deleteSystemInfoNote() {
+    GenericValue noteData = from("NoteData").where(noteId: parameters.noteId).queryOne()
+    noteData.removeRelated('CustRequestItemNote')
+    noteData.removeRelated('CustRequestNote')
+    noteData.removeRelated('MarketingCampaignNote')
+    noteData.removeRelated('OrderHeaderNote')
+    noteData.removeRelated('PartyNote')
+    noteData.removeRelated('QuoteNote')
+    noteData.removeRelated('WorkEffortNote')
+    noteData.remove()
+    return success()
+}
+
+def deleteAllSystemNotes() {
+    delegator.removeByAnd("NoteData", [noteParty: userLogin.partyId, noteName: "SYSTEMNOTE"])
+    return success()
+}
+
+def getSystemInfoNotes() {
+    List systemInfoNotes = from("NoteData")
+            .where(noteParty: userLogin.partyId,
+                    noteName: "SYSTEMNOTE")
+            .orderBy("-noteDateTime")
+            .queryList()
+    if (systemInfoNotes) {
+        return success(systemInfoNotes: systemInfoNotes)
+    }
+    return success()
+}
+
+def getLastSystemInfoNote() {
+    Map result = success()
+    List systemInfoNotes = from("NoteData")
+            .where(noteParty: userLogin ? userLogin.partyId : "_NA_",
+                    noteName: "SYSTEMNOTE")
+            .orderBy("-noteDateTime")
+            .queryList()
+    if (systemInfoNotes) {
+        result.lastSystemInfoNote1 = systemInfoNotes[0]
+        if (systemInfoNotes.size() == 2) {
+            result.lastSystemInfoNote2 = systemInfoNotes[1]
+        } else if (systemInfoNotes.size() > 2) {
+            result.lastSystemInfoNote2 = systemInfoNotes[1]
+            result.lastSystemInfoNote3 = systemInfoNotes[2]
+        }
+    }
+    return result
+}
+
+def getSystemInfoStatus() {
+    List systemInfoStatus = []
+    List comms = from("CommunicationEventAndRole")
+            .where(partyId: userLogin.partyId,
+                    statusId: "COM_ROLE_COMPLETED")
+            .orderBy("-entryDate")
+            .queryList()
+    if (comms) {
+        systemInfoStatus << [noteInfo: "Open communication events: " + comms.size(), noteDateTime: comms[0].entryDate]
+    }
+
+    List assigns = from("WorkEffortAndPartyAssign")
+            .where(partyId: userLogin.partyId,
+                    statusId: "PAS_ASSIGNED",
+                    workEffortTypeId: "TASK")
+            .orderBy("-fromDate")
+            .filterByDate()
+            .queryList()
+    if (assigns) {
+        systemInfoStatus << [noteInfo: "Assigned and not completed tasks: " + assigns.size(), noteDateTime: assigns[0].fromDate]
+    }
+    return success(systemInfoStatus: systemInfoStatus)
+}
\ No newline at end of file
diff --git a/applications/commonext/minilang/SystemInfoServices.xml b/applications/commonext/minilang/SystemInfoServices.xml
deleted file mode 100644
index d199a7e..0000000
--- a/applications/commonext/minilang/SystemInfoServices.xml
+++ /dev/null
@@ -1,159 +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="createSystemInfoNote" short-description="Create a system to to a specific party">
-        <set field="parameters.partyId" from-field="parameters.partyId" default-value="userLogin.partyId"/>
-        <make-value entity-name="NoteData" value-field="noteData"/>
-        <set-nonpk-fields map="parameters" value-field="noteData"/>
-        <now-timestamp field="noteData.noteDateTime"/>
-        <sequenced-id sequence-name="NoteData" field="noteData.noteId"/>
-        <set field="noteData.noteName" value="SYSTEMNOTE"/>
-        <create-value value-field="noteData"/>
-    </simple-method>
-
-    <simple-method method-name="getPortletAttributeMap" short-description="get the attributes of the SystemInfoNotes portlet for a userlogin">
-        <!-- get the portlet settings: works only if portlet is used only one time and the user only has a single userLogin -->
-        <entity-and entity-name="UserLogin" list="ulList">
-            <field-map field-name="partyId" from-field="partyId"/>
-        </entity-and>
-        <if-not-empty field="ulList">
-            <set field="haveUserLogin" value="true"/>
-            <first-from-list list="ulList" entry="userLogin"/>
-            <set field="paMap.ownerUserLoginId" from-field="userLogin.userLoginId"/>
-            <set field="paMap.portalPortletId" value="SystemInfoNotes"/>
-            <call-service service-name="getPortletAttributes" in-map-name="paMap">
-                <result-to-field result-name="attributeMap" field="attributeMap"/>
-            </call-service>
-        </if-not-empty>
-    </simple-method>
-
-    <simple-method method-name="deleteSystemInfoNote" short-description="Delete SystemInfo Note">
-        <entity-one entity-name="NoteData" value-field="noteData"/>
-        <remove-related relation-name="CustRequestItemNote" value-field="noteData"/>
-        <remove-related relation-name="CustRequestNote" value-field="noteData"/>
-        <remove-related relation-name="MarketingCampaignNote" value-field="noteData"/>
-        <remove-related relation-name="OrderHeaderNote" value-field="noteData"/>
-        <remove-related relation-name="PartyNote" value-field="noteData"/>
-        <remove-related relation-name="QuoteNote" value-field="noteData"/>
-        <remove-related relation-name="WorkEffortNote" value-field="noteData"/>
-        <remove-value value-field="noteData"/>
-    </simple-method>
-
-    <simple-method method-name="deleteAllSystemNotes" short-description="delete all system notes from a particular user">
-        <set field="removeData.noteParty" from-field="parameters.userLogin.partyId"/>
-        <set field="removeData.noteName" value="SYSTEMNOTE"/>
-        <remove-by-and entity-name="NoteData" map="removeData"/>
-    </simple-method>
-
-    <simple-method method-name="getSystemInfoStatus" short-description="">
-        <!-- communication events -->
-        <entity-count entity-name="CommunicationEventRole" count-field="comCount">
-            <condition-list combine="and">
-                <condition-expr field-name="partyId" from-field="userLogin.partyId"/>
-                <condition-expr field-name="statusId" operator="not-equals" value="COM_ROLE_COMPLETED"/>    
-            </condition-list>
-        </entity-count>
-        <entity-condition entity-name="CommunicationEventAndRole" list="comms">
-            <condition-list combine="and">
-                <condition-expr field-name="partyId" from-field="userLogin.partyId"/>
-                <condition-expr field-name="statusId" operator="not-equals" value="COM_ROLE_COMPLETED"/>    
-            </condition-list>
-            <order-by field-name="-entryDate"/>
-        </entity-condition>
-        <if-compare field="comCount" value="0" operator="greater">
-            <set field="status.noteInfo" value="Open communication events: ${comCount}"/>
-            <first-from-list list="comms" entry="comm"/>
-            <set field="status.noteDateTime" from-field="comm.entryDate"/>
-            <field-to-list field="status" list="systemInfoStatus"/>
-            <clear-field field="status"/>
-        </if-compare>
-        <!-- assigned tasks -->
-        <entity-and entity-name="WorkEffortAndPartyAssign" list="assigns" filter-by-date="true">
-            <field-map field-name="partyId" from-field="parameters.userLogin.partyId"/>
-            <field-map field-name="statusId" value="PAS_ASSIGNED"/>
-            <field-map field-name="workEffortTypeId" value="TASK"/>
-            <order-by field-name="-fromDate"/>
-        </entity-and>
-        <entity-count entity-name="WorkEffortAndPartyAssign" count-field="assignCount">
-            <condition-list combine="and">
-                <condition-expr field-name="partyId"  operator="equals" from-field="parameters.userLogin.partyId"/>
-                <condition-expr field-name="statusId" operator="equals" value="PAS_ASSIGNED"/>
-                <condition-expr field-name="workEffortTypeId" operator="equals" value="TASK"/>
-            </condition-list>
-        </entity-count>            
-        <if-compare field="assignCount" value="0" operator="greater">
-            <set field="status.noteInfo" value="Assigned and not completed tasks: ${assignCount}"/>
-            <first-from-list list="assigns" entry="assign"/>
-            <set field="status.noteDateTime" from-field="assign.fromDate"/>
-            <field-to-list field="status" list="systemInfoStatus"/>
-        </if-compare>
-        
-        <!-- return result -->
-        <if-not-empty field="systemInfoStatus">
-            <field-to-result field="systemInfoStatus"/>
-        </if-not-empty>
-        
-    </simple-method>
-    <simple-method method-name="getSystemInfoNotes" short-description="">
-        <entity-and entity-name="NoteData" list="systemInfoNotes">
-            <field-map field-name="noteParty" from-field="parameters.userLogin.partyId"/>
-            <field-map field-name="noteName" value="SYSTEMNOTE"/>
-            <order-by field-name="-noteDateTime"/>
-        </entity-and>
-        <if-not-empty field="systemInfoNotes">
-            <field-to-result field="systemInfoNotes"/>
-        </if-not-empty>
-    </simple-method>
-
-    <simple-method method-name="getLastSystemInfoNote"
-        short-description="Get the last 3 system info notes"
-        login-required="false">
-        <if-not-empty field="parameters.userLogin">
-            <entity-and entity-name="NoteData" list="systemInfoNotes">
-                <field-map field-name="noteParty"
-                    from-field="parameters.userLogin.partyId"/>
-                <field-map field-name="noteName" value="SYSTEMNOTE"/>
-                <order-by field-name="-noteDateTime"/>
-            </entity-and>
-            <else>
-                <entity-and entity-name="NoteData" list="systemInfoNotes">
-                    <field-map field-name="noteParty" value="_NA_"/>
-                    <field-map field-name="noteName" value="SYSTEMNOTE"/>
-                    <order-by field-name="-noteDateTime"/>
-                </entity-and>
-            </else>
-        </if-not-empty>
-        <if-not-empty field="systemInfoNotes">
-            <set field="lastSystemInfoNote1" from-field="systemInfoNotes[0]"/>
-            <field-to-result field="lastSystemInfoNote1"/>
-            <if-not-empty field="systemInfoNotes[2]">
-                <set field="lastSystemInfoNote2" from-field="systemInfoNotes[1]"/>
-                <field-to-result field="lastSystemInfoNote2"/>
-            </if-not-empty>
-            <if-not-empty field="systemInfoNotes[3]">
-                <set field="lastSystemInfoNote3" from-field="systemInfoNotes[2]"/>
-                <field-to-result field="lastSystemInfoNote3"/>
-            </if-not-empty>
-        </if-not-empty>
-    </simple-method>
-</simple-methods>
diff --git a/applications/commonext/servicedef/services.xml b/applications/commonext/servicedef/services.xml
index d71ee4b..804079a 100644
--- a/applications/commonext/servicedef/services.xml
+++ b/applications/commonext/servicedef/services.xml
@@ -24,34 +24,34 @@ under the License.
     <vendor>OFBiz</vendor>
     <version>1.0</version>
 
-    <service name="createSystemInfoNote" engine="simple" default-entity-name="NoteData"
-        location="component://commonext/minilang/SystemInfoServices.xml" invoke="createSystemInfoNote">
+    <service name="createSystemInfoNote" engine="groovy" default-entity-name="NoteData"
+        location="component://commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy" invoke="createSystemInfoNote">
         <description>Create a system info note</description>
         <auto-attributes include="nonpk" mode="IN" optional="true"/>
     </service>
-    <service name="deleteSystemInfoNote" engine="simple" default-entity-name="NoteData"
-        location="component://commonext/minilang/SystemInfoServices.xml" invoke="deleteSystemInfoNote">
+    <service name="deleteSystemInfoNote" engine="groovy" default-entity-name="NoteData"
+        location="component://commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy" invoke="deleteSystemInfoNote">
         <description>Delete a system info note</description>
         <auto-attributes include="pk" mode="IN" optional="false"/>
     </service>
-    <service name="deleteAllSystemNotes" engine="simple" auth="true"
-        location="component://commonext/minilang/SystemInfoServices.xml" invoke="deleteAllSystemNotes">
+    <service name="deleteAllSystemNotes" engine="groovy" auth="true"
+        location="component://commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy" invoke="deleteAllSystemNotes">
         <description>Delete all system notes for the logged on party</description>
     </service>
-    <service name="getSystemInfoNotes" engine="simple" auth="true"
-        location="component://commonext/minilang/SystemInfoServices.xml" invoke="getSystemInfoNotes">
+    <service name="getSystemInfoNotes" engine="groovy" auth="true"
+        location="component://commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy" invoke="getSystemInfoNotes">
         <description>Get system notes for the logged on party</description>
         <attribute name="systemInfoNotes" type="List" mode="OUT" optional="true"/>
     </service>
-    <service name="getLastSystemInfoNote" engine="simple" auth="false"
-        location="component://commonext/minilang/SystemInfoServices.xml" invoke="getLastSystemInfoNote">
+    <service name="getLastSystemInfoNote" engine="groovy" auth="false"
+        location="component://commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy" invoke="getLastSystemInfoNote">
         <description>Get last system note for the logged on party</description>
         <attribute name="lastSystemInfoNote1" type="GenericValue" mode="OUT" optional="true"/>
         <attribute name="lastSystemInfoNote2" type="GenericValue" mode="OUT" optional="true"/>
         <attribute name="lastSystemInfoNote3" type="GenericValue" mode="OUT" optional="true"/>
     </service>
-    <service name="getSystemInfoStatus" engine="simple" auth="true"
-        location="component://commonext/minilang/SystemInfoServices.xml" invoke="getSystemInfoStatus">
+    <service name="getSystemInfoStatus" engine="groovy" auth="true"
+        location="component://commonext/groovyScripts/ofbizsetup/SystemInfoServices.groovy" invoke="getSystemInfoStatus">
         <description>Get system status for the logged on party</description>
         <attribute name="systemInfoStatus" type="List" mode="OUT" optional="true"/>
     </service>

[ofbiz-framework] 01/02: Improved: Convert createMatchingPaymentApplication service from mini-lang to groovy DSL (OFBIZ-11500)

Posted by nm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 7230aa804664cbeb1a70efdea3940b7e559fe72d
Author: Nicolas Malin <ni...@nereide.fr>
AuthorDate: Wed Sep 15 17:34:58 2021 +0200

    Improved: Convert createMatchingPaymentApplication service from mini-lang to groovy DSL (OFBIZ-11500)
    
    Thanks to Rohit Koushal to submit the patch
---
 .../groovyScripts/payment/PaymentServices.groovy   | 117 ++++++++++++++++++
 .../minilang/payment/PaymentServices.xml           | 135 ---------------------
 .../accounting/servicedef/services_payment.xml     |   4 +-
 3 files changed, 119 insertions(+), 137 deletions(-)

diff --git a/applications/accounting/groovyScripts/payment/PaymentServices.groovy b/applications/accounting/groovyScripts/payment/PaymentServices.groovy
index fbc0edd..cfa29b8 100644
--- a/applications/accounting/groovyScripts/payment/PaymentServices.groovy
+++ b/applications/accounting/groovyScripts/payment/PaymentServices.groovy
@@ -26,6 +26,8 @@ import org.apache.ofbiz.entity.condition.EntityCondition
 import org.apache.ofbiz.entity.condition.EntityConditionBuilder
 import org.apache.ofbiz.entity.condition.EntityOperator
 import org.apache.ofbiz.entity.GenericValue
+import org.apache.ofbiz.entity.util.EntityTypeUtil
+import org.apache.ofbiz.entity.util.EntityUtilProperties
 import org.apache.ofbiz.service.ServiceUtil
 
 import java.sql.Timestamp
@@ -713,3 +715,118 @@ def setPaymentStatus() {
     payment.store()
     return success(oldStatusId: oldStatusId)
 }
+
+def createMatchingPaymentApplication() {
+    String autoCreate = EntityUtilProperties.getPropertyValue("accounting", "accounting.payment.application.autocreate", "Y", delegator)
+    if ("Y" != autoCreate) {
+        logInfo("payment application not automatically created because config is not set to Y")
+        return success()
+    }
+
+    Map createPaymentApplicationCtx = [:]
+    if (parameters.invoiceId) {
+        GenericValue invoice = from("Invoice").where("invoiceId", parameters.invoiceId).queryOne()
+        if (invoice) {
+            BigDecimal invoiceTotal = InvoiceWorker.getInvoiceTotal(invoice)
+
+            Map isInvoiceInForeignCurrencyResp = run service: 'isInvoiceInForeignCurrency', with: [invoiceId: invoice.invoiceId]
+            if (ServiceUtil.isError(isInvoiceInForeignCurrencyResp)) return isInvoiceInForeignCurrencyResp
+
+            EntityConditionBuilder exprBldr = new EntityConditionBuilder()
+            EntityCondition expr = exprBldr.AND() {
+                NOT_EQUAL(statusId: 'PMNT_CONFIRMED')
+                EQUALS(partyIdFrom: invoice.partyId)
+                EQUALS(partyIdTo: invoice.partyIdFrom)
+            }
+            if (isInvoiceInForeignCurrencyResp.isForeign) {
+                expr = exprBldr.AND(expr) {
+                    EQUALS(actualCurrencyAmount: invoiceTotal)
+                    EQUALS(actualCurrencyUomId: invoice.currencyUomId)
+                }
+            } else {
+                expr = exprBldr.AND(expr) {
+                    EQUALS(amount: invoiceTotal)
+                    EQUALS(currencyUomId: invoice.currencyUomId)
+                }
+            }
+
+            GenericValue payment = from('Payment')
+                    .where(expr)
+                    .orderBy('effectiveDate')
+                    .queryFirst()
+
+            if (payment && from('PaymentApplication')
+                    .where('paymentId', payment.paymentId)
+                    .queryCount() == 0) {
+                createPaymentApplicationCtx.paymentId = payment.paymentId
+                createPaymentApplicationCtx.invoiceId = parameters.invoiceId
+                createPaymentApplicationCtx.amountApplied = isInvoiceInForeignCurrencyResp.isForeign
+                        ? payment.actualCurrencyAmount
+                        : payment.amount
+            }
+        }
+    }
+
+    if (parameters.paymentId) {
+        GenericValue payment = from("Payment").where(paymentId: parameters.paymentId).queryOne()
+
+        if (payment) {
+            EntityCondition expr = new EntityConditionBuilder().AND() {
+                NOT_IN(statusId: ['INVOICE_READY','INVOICE_PAID','INVOICE_CANCELLED','INVOICE_WRITEOFF'])
+                EQUALS(partyIdFrom: payment.partyIdTo)
+                EQUALS(partyId: payment.partyIdFrom)
+            }
+
+            List invoices = from('Invoice')
+                    .where(expr)
+                    .orderBy('invoiceDate')
+                    .queryList()
+            String invoiceId
+            BigDecimal amountApplied
+            for (GenericValue invoice: invoices) {
+                boolean isPurchaseInvoice = EntityTypeUtil.hasParentType(delegator, 'InvoiceType', 'invoiceTypeId', invoice.invoiceTypeId, 'parentTypeId', 'PURCHASE_INVOICE')
+                boolean isSalesInvoice = EntityTypeUtil.hasParentType(delegator, 'InvoiceType', 'invoiceTypeId', invoice.invoiceTypeId, 'parentTypeId', 'SALES_INVOICE')
+
+                if (isPurchaseInvoice || isSalesInvoice) {
+                    BigDecimal invoiceTotal = InvoiceWorker.getInvoiceTotal(invoice)
+
+                    Map isInvoiceInForeignCurrencyResp = run service: 'isInvoiceInForeignCurrency', with: [invoiceId: invoice.invoiceId]
+                    if (ServiceUtil.isError(isInvoiceInForeignCurrencyResp)) return isInvoiceInForeignCurrencyResp
+
+                    if (isInvoiceInForeignCurrencyResp.isForeign
+                            && invoiceTotal.compareTo(payment.actualCurrencyAmount) == 0
+                            && invoice.currencyUomId == payment.actualCurrencyUomId) {
+                        invoiceId = invoice.invoiceId
+                        amountApplied = payment.actualCurrencyAmount
+                    } else if (invoiceTotal.compareTo(payment.amount) == 0 && invoice.currencyUomId == payment.currencyUomId) {
+                        invoiceId = invoice.invoiceId
+                        amountApplied = payment.amount
+                    }
+
+                }
+            }
+
+            if (invoiceId) {
+                if (from('PaymentApplication')
+                        .where(invoiceId: invoiceId)
+                        .queryCount()) {
+                    createPaymentApplicationCtx.paymentId = parameters.paymentId
+                    createPaymentApplicationCtx.invoiceId = invoiceId
+                    createPaymentApplicationCtx.amountApplied = amountApplied
+                }
+            }
+        }
+    }
+
+    if (createPaymentApplicationCtx.paymentId &&
+            createPaymentApplicationCtx.invoiceId) {
+        Map createPaymentApplicationResp = run service: 'createPaymentApplication', with: createPaymentApplicationCtx
+        if (ServiceUtil.isError(createPaymentApplicationResp)) return createPaymentApplicationResp
+
+        logInfo("payment application automatically created between invoiceId: $createPaymentApplicationCtx.invoiceId}" +
+                " and paymentId: ${createPaymentApplicationCtx.paymentId} for" +
+                " the amount: ${createPaymentApplicationCtx.amountApplied} (can be disabled in accounting.properties)")
+    }
+    return success()
+
+}
\ No newline at end of file
diff --git a/applications/accounting/minilang/payment/PaymentServices.xml b/applications/accounting/minilang/payment/PaymentServices.xml
index a878bc9..d7f112b 100644
--- a/applications/accounting/minilang/payment/PaymentServices.xml
+++ b/applications/accounting/minilang/payment/PaymentServices.xml
@@ -190,139 +190,4 @@ under the License.
         </else>
         </if-not-empty>
     </simple-method>
-    <simple-method method-name="createMatchingPaymentApplication" short-description="Create a payment application if either the invoice of payment could be found">
-        <property-to-field resource="accounting" property="accounting.payment.application.autocreate" field="autoCreate" default="Y"/>
-        <if-compare operator="not-equals" value="Y" field="autoCreate">
-            <log level="info" message="payment application not automatically created because config is not set to Y"/>
-            <return/>
-        </if-compare>
-
-        <if-not-empty field="parameters.invoiceId">
-            <entity-one entity-name="Invoice" value-field="invoice"/>
-            <if-not-empty field="invoice">
-                <call-class-method method-name="getInvoiceTotal" class-name="org.apache.ofbiz.accounting.invoice.InvoiceWorker" ret-field="invoiceTotal">
-                    <field field="invoice" type="GenericValue"/>
-                </call-class-method>
-                
-                <set field="checkInvoice.invoiceId" from="invoice.invoiceId"/>
-                <call-service service-name="isInvoiceInForeignCurrency" in-map-name="checkInvoice">
-                    <result-to-field result-name="isForeign"/>
-                </call-service>
-                <if-compare operator="equals" value="true" field="isForeign">
-                    <entity-condition entity-name="Payment" list="payments">
-                        <condition-list combine="and">
-                            <condition-expr field-name="statusId" value="PMNT_CONFIRMED" operator="not-equals"/>
-                            <condition-expr field-name="partyIdFrom" operator="equals" from-field="invoice.partyId"/>
-                            <condition-expr field-name="partyIdTo" operator="equals" from-field="invoice.partyIdFrom"/>
-                            <condition-expr field-name="actualCurrencyAmount" operator="equals" from-field="invoiceTotal"/>
-                            <condition-expr field-name="actualCurrencyUomId" operator="equals" from-field="invoice.currencyUomId"/>
-                        </condition-list>
-                        <order-by field-name="effectiveDate"/>
-                    </entity-condition>
-                    <else>
-                        <entity-condition entity-name="Payment" list="payments">
-                            <condition-list combine="and">
-                                <condition-expr field-name="statusId" value="PMNT_CONFIRMED" operator="not-equals"/>
-                                <condition-expr field-name="partyIdFrom" operator="equals" from-field="invoice.partyId"/>
-                                <condition-expr field-name="partyIdTo" operator="equals" from-field="invoice.partyIdFrom"/>
-                                <condition-expr field-name="amount" operator="equals" from-field="invoiceTotal"/>
-                                <condition-expr field-name="currencyUomId" operator="equals" from-field="invoice.currencyUomId"/>
-                            </condition-list>
-                            <order-by field-name="effectiveDate"/>
-                        </entity-condition>
-                    </else>
-                </if-compare>
-
-                <if-not-empty field="payments">
-                    <!-- check if already applied -->
-                    <entity-and entity-name="PaymentApplication" list="paymentAppls">
-                        <field-map field-name="paymentId" from-field="payments[0].paymentId"/>
-                    </entity-and>
-                    <if-empty field="paymentAppls">
-                        <set field="createAppl.paymentId" from-field="payments[0].paymentId"/>
-                        <set field="createAppl.invoiceId" from-field="parameters.invoiceId"/>
-                        <if-compare operator="equals" value="true" field="isForeign">
-                            <set field="createAppl.amountApplied" from-field="payments[0].actualCurrencyAmount"/>
-                            <else>
-                                <set field="createAppl.amountApplied" from-field="payments[0].amount"/>
-                            </else>
-                        </if-compare>
-                    </if-empty>
-                </if-not-empty>
-            </if-not-empty>
-        </if-not-empty>
-
-        <if-not-empty field="parameters.paymentId">
-            <entity-one entity-name="Payment" value-field="payment"/>
-            <if-not-empty field="payment">
-                        
-                <entity-condition entity-name="Invoice" list="invoices">
-                    <condition-list combine="and">
-                        <condition-expr field-name="statusId" value="INVOICE_READY" operator="not-equals"/>
-                        <condition-expr field-name="statusId" value="INVOICE_PAID" operator="not-equals"/>
-                        <condition-expr field-name="statusId" value="INVOICE_CANCELLED" operator="not-equals"/>
-                        <condition-expr field-name="statusId" value="INVOICE_WRITEOFF" operator="not-equals"/>
-                        <condition-expr field-name="partyIdFrom" from-field="payment.partyIdTo"/>
-                        <condition-expr field-name="partyId" from-field="payment.partyIdFrom"/>
-                    </condition-list>
-                    <order-by field-name="invoiceDate"/>
-                </entity-condition>
-                <iterate list="invoices" entry="invoice">
-                    <set field="isPurchaseInvoice" value="${groovy: org.apache.ofbiz.entity.util.EntityTypeUtil.hasParentType(delegator, 'InvoiceType', 'invoiceTypeId', invoice.getString('invoiceTypeId'), 'parentTypeId', 'PURCHASE_INVOICE')}" type="Boolean"/>
-                    <set field="isSalesInvoice" value="${groovy: org.apache.ofbiz.entity.util.EntityTypeUtil.hasParentType(delegator, 'InvoiceType', 'invoiceTypeId', invoice.getString('invoiceTypeId'), 'parentTypeId', 'SALES_INVOICE')}" type="Boolean"/>
-                    <if>
-                        <condition>
-                            <or>
-                                <if-compare field="isPurchaseInvoice" operator="equals" value="true" type="Boolean"/>
-                                <if-compare field="isSalesInvoice" operator="equals" value="true" type="Boolean"/>
-                            </or>
-                        </condition>
-                        <then>
-                            <call-class-method method-name="getInvoiceTotal" class-name="org.apache.ofbiz.accounting.invoice.InvoiceWorker" ret-field="invoiceTotal">
-                                <field field="invoice" type="GenericValue"/>
-                            </call-class-method>
-        
-                            <set field="checkInvoice.invoiceId" from="invoice.invoiceId"/>
-                            <call-service service-name="isInvoiceInForeignCurrency" in-map-name="checkInvoice">
-                                <result-to-field result-name="isForeign"/>
-                            </call-service>
-                            <if-compare operator="equals" value="true" field="isForeign">
-                                <if-compare-field field="invoiceTotal" operator="equals" to-field="payment.actualCurrencyAmount">
-                                    <if-compare-field field="invoice.currencyUomId" operator="equals" to-field="payment.actualCurrencyUomId">
-                                        <set field="invoiceId" from-field="invoice.invoiceId"/>
-                                        <set field="amountApplied" from-field="payment.actualCurrencyAmount"/>
-                                    </if-compare-field>
-                                </if-compare-field>
-                                <else>
-                                    <if-compare-field field="invoiceTotal" operator="equals" to-field="payment.amount">
-                                        <if-compare-field field="invoice.currencyUomId" operator="equals" to-field="payment.currencyUomId">
-                                            <set field="invoiceId" from-field="invoice.invoiceId"/>
-                                            <set field="amountApplied" from-field="payment.amount"/>
-                                        </if-compare-field>
-                                    </if-compare-field>
-                                </else>
-                            </if-compare>
-                        </then>
-                    </if>
-                </iterate>
-                <if-not-empty field="invoiceId">
-                    <entity-and entity-name="PaymentApplication" list="paymentAppls">
-                        <field-map field-name="invoiceId" from-field="invoiceId"/>
-                    </entity-and>
-                    <if-empty field="paymentAppls">
-                        <set field="createAppl.paymentId" from-field="parameters.paymentId"/>
-                        <set field="createAppl.invoiceId" from-field="invoiceId"/>
-                        <set field="createAppl.amountApplied" from-field="amountApplied"/>
-                    </if-empty>
-                </if-not-empty>
-            </if-not-empty>
-        </if-not-empty>
-
-        <if-not-empty field="createAppl.paymentId">
-            <if-not-empty field="createAppl.invoiceId">
-                <call-service service-name="createPaymentApplication" in-map-name="createAppl"/>
-                <log level="info" message="payment application automatically created between invoiceId: ${createAppl.invoiceId} and paymentId: ${createAppl.paymentId} for the amount: ${createAppl.appliedAmount} (can be disabled in accounting.properties)"/>
-            </if-not-empty>
-        </if-not-empty>
-    </simple-method>
 </simple-methods>
diff --git a/applications/accounting/servicedef/services_payment.xml b/applications/accounting/servicedef/services_payment.xml
index 8a667e0..c244812 100644
--- a/applications/accounting/servicedef/services_payment.xml
+++ b/applications/accounting/servicedef/services_payment.xml
@@ -244,8 +244,8 @@ under the License.
         <attribute name="comments" type="String" mode="IN" optional="true"/>
     </service>
 
-    <service name="createMatchingPaymentApplication" engine="simple"
-        location="component://accounting/minilang/payment/PaymentServices.xml" invoke="createMatchingPaymentApplication" auth="true">
+    <service name="createMatchingPaymentApplication" engine="groovy"
+        location="component://accounting/groovyScripts/payment/PaymentServices.groovy" invoke="createMatchingPaymentApplication" auth="true">
         <description>Create a payment application if either the invoice of payment could be found</description>
         <attribute name="paymentId" type="String" mode="IN" optional="true"/>
         <attribute name="invoiceId" type="String" mode="IN" optional="true"/>