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 13:47:53 UTC

[ofbiz-framework] branch trunk updated: Improved: Converted the setPaymentStatus service from XML to groovy DSL. (OFBIZ-11482)

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


The following commit(s) were added to refs/heads/trunk by this push:
     new 052f347  Improved: Converted the setPaymentStatus service from XML to groovy DSL. (OFBIZ-11482)
052f347 is described below

commit 052f347bf7228bd021efdfdbf1f1d4c0bda8693f
Author: Nicolas Malin <ni...@nereide.fr>
AuthorDate: Wed Sep 15 15:38:02 2021 +0200

    Improved: Converted the setPaymentStatus service from XML to groovy DSL. (OFBIZ-11482)
---
 .../groovyScripts/payment/PaymentServices.groovy   | 54 +++++++++++++++-
 .../minilang/payment/PaymentServices.xml           | 75 ----------------------
 .../accounting/servicedef/services_payment.xml     |  4 +-
 3 files changed, 55 insertions(+), 78 deletions(-)

diff --git a/applications/accounting/groovyScripts/payment/PaymentServices.groovy b/applications/accounting/groovyScripts/payment/PaymentServices.groovy
index de95465..fbc0edd 100644
--- a/applications/accounting/groovyScripts/payment/PaymentServices.groovy
+++ b/applications/accounting/groovyScripts/payment/PaymentServices.groovy
@@ -660,4 +660,56 @@ def createPaymentApplication() {
     return success([amountApplied       : paymentAppl.amountApplied,
                     paymentApplicationId: paymentAppl.paymentApplicationId,
                     paymentTypeId       : payment.paymentTypeId])
-}
\ No newline at end of file
+}
+
+def setPaymentStatus() {
+    GenericValue payment = from("Payment").where("paymentId", parameters.paymentId).queryOne()
+    if (!payment) {
+        return error("No payment found with ID ${parameters.paymentId}")
+    }
+    String oldStatusId = payment.statusId
+    GenericValue statusItem = from("StatusItem").where("statusId", parameters.statusId).cache().queryOne()
+    if (!statusItem) {
+        return error("No status found with status ID ${parameters.statusId}")
+    }
+
+    if (oldStatusId != parameters.statusId) {
+        GenericValue statusChange = from("StatusValidChange").where("statusId", oldStatusId, "statusIdTo", parameters.statusId).cache().queryOne()
+        if (! statusChange) {
+            return error(label("CommonUiLabels", "CommonErrorNoStatusValidChange"))
+        }
+
+        // payment method is mandatory when set to sent or received
+        if (["PMNT_RECEIVED", "PMNT_SENT"].contains(parameters.statusId) && !payment.paymentMethodId) {
+            return failure(label("AccountingUiLabels", "AccountingMissingPaymentMethod", [statusItem: statusItem]))
+        }
+
+        // check if the payment fully applied when set to confirmed
+        if ("PMNT_CONFIRMED" == parameters.statusId &&
+                PaymentWorker.getPaymentNotApplied(payment) != 0) {
+            return failure(label("AccountingUiLabels", "AccountingPSNotConfirmedNotFullyApplied"))
+        }
+    }
+
+    // if new status is cancelled delete existing payment applications
+    if ("PMNT_CANCELLED" == parameters.statusId) {
+        from("PaymentApplication")
+                .where(paymentId: payment.paymentId)
+                .queryList()
+                .each {
+                    run service: 'removePaymentApplication', with: [paymentApplicationId: it.paymentApplicationId]
+                }
+
+        // if new status is cancelled and the payment is associated to an OrderPaymentPreference, update the status of that record too
+        GenericValue orderPayPref = payment.getRelatedOne("OrderPaymentPreference", false)
+        if (orderPayPref) {
+            run service: 'updateOrderPaymentPreference', with: [orderPaymentPreferenceId: orderPayPref.orderPaymentPreferenceId,
+                                                                statusId                : "PAYMENT_CANCELLED"]
+        }
+    }
+
+    // everything ok, so now change the status field
+    payment.statusId = parameters.statusId
+    payment.store()
+    return success(oldStatusId: oldStatusId)
+}
diff --git a/applications/accounting/minilang/payment/PaymentServices.xml b/applications/accounting/minilang/payment/PaymentServices.xml
index c91e74f..a878bc9 100644
--- a/applications/accounting/minilang/payment/PaymentServices.xml
+++ b/applications/accounting/minilang/payment/PaymentServices.xml
@@ -89,81 +89,6 @@ under the License.
         </if-not-empty>
     </simple-method>
 
-    <simple-method method-name="setPaymentStatus" short-description="Set The Payment Status">
-        <entity-one entity-name="Payment" value-field="payment"/>
-        <entity-one entity-name="StatusItem" value-field="statusItem">
-            <field-map field-name="statusId" from-field="parameters.statusId"/>
-        </entity-one>
-        <field-to-result field="payment.statusId" result-name="oldStatusId"/>
-
-        <if-compare-field field="payment.statusId" to-field="parameters.statusId" operator="not-equals">
-            <entity-one entity-name="StatusValidChange" value-field="statusChange" auto-field-map="false">
-                <field-map field-name="statusId" from-field="payment.statusId"/>
-                <field-map field-name="statusIdTo" from-field="parameters.statusId"/>
-            </entity-one>
-            <if-empty field="statusChange">
-                <add-error>
-                    <fail-property resource="AccountingUiLabels" property="AccountingPSInvalidStatusChange"/>
-                </add-error>
-                <log level="error" message="Cannot change from ${payment.statusId} to ${parameters.statusId}"/>
-                <check-errors/>
-                <else>
-                    <!-- payment method is mandatory when set to sent or received. -->
-                    <if>
-                        <condition>
-                            <and>
-                                <or>
-                                    <if-compare field="parameters.statusId" operator="equals" value="PMNT_RECEIVED"/>
-                                    <if-compare field="parameters.statusId" operator="equals" value="PMNT_SENT"/>
-                                </or>
-                                <if-empty field="payment.paymentMethodId"/>
-                            </and>
-                        </condition>
-                        <then>
-                            <add-error>
-                                <fail-property resource="AccountingUiLabels" property="AccountingMissingPaymentMethod"/>
-                            </add-error>
-                            <log level="error" message="Cannot set status to ${parameters.statusId} on payment ${payment.paymentId}: payment method is missing"/>
-                            <check-errors/>
-                        </then>
-                    </if>
-
-                    <!-- check if the payment fully applied when set to confirmed-->
-                    <if-compare field="parameters.statusId" operator="equals" value="PMNT_CONFIRMED">
-                        <set field="notYetApplied" value="${groovy:org.apache.ofbiz.accounting.payment.PaymentWorker.getPaymentNotApplied(payment)}"/>
-                        <if-compare field="notYetApplied" operator="greater" value="0.00" type="BigDecimal">
-                            <add-error>
-                                <fail-property resource="AccountingUiLabels" property="AccountingPSNotConfirmedNotFullyApplied"/>
-                            </add-error>
-                            <log level="error" message="Cannot change from ${payment.statusId} to ${parameters.statusId}, payment not fully applied: ${notYetapplied}"/>
-                            <check-errors/>
-                        </if-compare>
-                    </if-compare>
-
-                    <if-compare field="parameters.statusId" operator="equals" value="PMNT_CANCELLED">
-                        <!-- if new status is cancelled delete existing payment applications. -->
-                        <get-related value-field="payment" relation-name="PaymentApplication" list="paymentApplications"/>
-                        <iterate list="paymentApplications" entry="paymentApplication">
-                            <set field="removePaymentApplicationMap.paymentApplicationId" from-field="paymentApplication.paymentApplicationId"/>
-                            <call-service service-name="removePaymentApplication" in-map-name="removePaymentApplicationMap"/>
-                        </iterate>
-                        <!-- if new status is cancelled and the payment is associated to an OrderPaymentPreference, update the status of that record too. -->
-                        <get-related-one value-field="payment" relation-name="OrderPaymentPreference" to-value-field="orderPaymentPreference"/>
-                        <if-not-empty field="orderPaymentPreference">
-                            <set field="updateOrderPaymentPreferenceMap.orderPaymentPreferenceId" from-field="orderPaymentPreference.orderPaymentPreferenceId"/>
-                            <set field="updateOrderPaymentPreferenceMap.statusId" value="PAYMENT_CANCELLED"/>
-                            <call-service service-name="updateOrderPaymentPreference" in-map-name="updateOrderPaymentPreferenceMap"/>
-                        </if-not-empty>
-                    </if-compare>
-
-                    <!-- everything ok so now change the status field -->
-                    <set from-field="parameters.statusId" field="payment.statusId"/>
-                    <store-value value-field="payment"/>
-                </else>
-            </if-empty>
-        </if-compare-field>
-    </simple-method>
-
     <simple-method method-name="voidPayment" short-description="Service to void a payment">
         <entity-one entity-name="Payment" value-field="payment"/>
         <field-to-result field="payment.finAccountTransId" result-name="finAccountTransId"/>
diff --git a/applications/accounting/servicedef/services_payment.xml b/applications/accounting/servicedef/services_payment.xml
index 3941c9d..8a667e0 100644
--- a/applications/accounting/servicedef/services_payment.xml
+++ b/applications/accounting/servicedef/services_payment.xml
@@ -44,8 +44,8 @@ under the License.
         <auto-attributes include="nonpk" mode="IN" optional="true"/>
     </service>
 
-    <service name="setPaymentStatus" engine="simple" default-entity-name="Payment"
-        location="component://accounting/minilang/payment/PaymentServices.xml" invoke="setPaymentStatus" auth="true">
+    <service name="setPaymentStatus" engine="groovy" default-entity-name="Payment"
+        location="component://accounting/groovyScripts/payment/PaymentServices.groovy" invoke="setPaymentStatus" auth="true">
         <description>Change the status of a Payment</description>
         <permission-service service-name="acctgPaymentPermissionCheck" main-action="UPDATE"/>
         <auto-attributes include="pk" mode="IN" optional="false"/>