diff --git a/applications/accounting/groovyScripts/payment/PaymentServices.groovy b/applications/accounting/groovyScripts/payment/PaymentServices.groovy
index de954658ed..fbc0edd17c 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 c91e74fd1b..a878bc93e2 100644
--- a/applications/accounting/minilang/payment/PaymentServices.xml
+++ b/applications/accounting/minilang/payment/PaymentServices.xml
@@ -89,81 +89,6 @@ under the License.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/applications/accounting/servicedef/services_payment.xml b/applications/accounting/servicedef/services_payment.xml
index 3941c9db6d..8a667e02c4 100644
--- a/applications/accounting/servicedef/services_payment.xml
+++ b/applications/accounting/servicedef/services_payment.xml
@@ -44,8 +44,8 @@ under the License.
-
+
Change the status of a Payment