Improved: Converted the setPaymentStatus service from XML to groovy DSL. (OFBIZ-11482)

This commit is contained in:
Nicolas Malin
2021-09-15 15:46:55 +02:00
parent f790b7a6ef
commit 052f347bf7
3 changed files with 55 additions and 78 deletions
@@ -660,4 +660,56 @@ def createPaymentApplication() {
return success([amountApplied : paymentAppl.amountApplied,
paymentApplicationId: paymentAppl.paymentApplicationId,
paymentTypeId : payment.paymentTypeId])
}
}
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)
}
@@ -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"/>
@@ -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"/>