You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ofbiz.apache.org by GitBox <gi...@apache.org> on 2020/07/14 05:17:04 UTC

[GitHub] [ofbiz-framework] dixitdeepak commented on a change in pull request #158: [Improved] Convert createPaymentApplication service from mini-lang to groovy DSL (OFBIZ-11481)

dixitdeepak commented on a change in pull request #158:
URL: https://github.com/apache/ofbiz-framework/pull/158#discussion_r454106321



##########
File path: applications/accounting/groovyScripts/payment/PaymentServices.groovy
##########
@@ -134,3 +136,77 @@ def updatePaymentContent() {
     }
 }
 
+def createPaymentApplication() {
+    // Create a Payment Application
+    if (!parameters.invoiceId && !parameters.billingAccountId && !parameters.taxAuthGeoId && !parameters.toPaymentId) {
+        return error(UtilProperties.getResourceBundleMap("AccountingUiLabels", locale)?.AccountingPaymentApplicationParameterMissing)
+    }
+    GenericValue paymentAppl = delegator.makeValue("PaymentApplication")
+    paymentAppl.setNonPKFields(parameters)
+
+    GenericValue payment = from("Payment").where("paymentId", parameters.paymentId).queryOne()
+    if (!payment) {
+        return error(UtilProperties.getResourceBundleMap("AccountingUiLabels", locale)?.AccountingPaymentApplicationParameterMissing)
+    }
+
+    BigDecimal notAppliedPayment = PaymentWorker.getPaymentNotApplied(payment)
+
+    if (parameters.invoiceId) {
+        // get the invoice and do some further validation against it
+        GenericValue invoice = from("Invoice").where("invoiceId", parameters.invoiceId).queryOne()
+        // check the currencies if they are compatible
+        if (invoice.currencyUomId != payment.currencyUomId && invoice.currencyUomId != payment.actualCurrencyUomId) {
+            return error(UtilProperties.getResourceBundleMap("AccountingUiLabels", locale)?.AccountingCurrenciesOfInvoiceAndPaymentNotCompatible)
+        }
+        if (invoice.currencyUomId != payment.currencyUomId && invoice.currencyUomId == payment.actualCurrencyUomId) {
+            // if required get the payment amount in foreign currency (local we already have)
+            Boolean actual = true
+            notAppliedPayment = PaymentWorker.getPaymentNotApplied(payment, actual)
+        }
+        // get the amount that has not been applied yet for the invoice (outstanding amount)
+        BigDecimal notAppliedInvoice = InvoiceWorker.getInvoiceNotApplied(invoice)
+        if (notAppliedInvoice <= notAppliedPayment) {
+            paymentAppl.amountApplied = notAppliedInvoice
+        } else {
+            paymentAppl.amountApplied = notAppliedPayment
+        }
+
+        if (invoice.billingAccountId) {
+            paymentAppl.billingAccountId = invoice.billingAccountId
+        }
+    }
+
+    if (parameters.toPaymentId) {
+        // get the to payment and check the parent types are compatible
+        GenericValue toPayment = from("Payment").where("paymentId", parameters.toPaymentId).queryOne()
+        if (toPayment) {
+            toPaymentType = from("PaymentType").where("paymentTypeId", toPayment.paymentTypeId).queryOne()
+        }
+        paymentType = from("PaymentType").where("paymentTypeId", payment.paymentTypeId).queryOne()

Review comment:
       Use cache. 

##########
File path: applications/accounting/groovyScripts/payment/PaymentServices.groovy
##########
@@ -134,3 +136,77 @@ def updatePaymentContent() {
     }
 }
 
+def createPaymentApplication() {
+    // Create a Payment Application
+    if (!parameters.invoiceId && !parameters.billingAccountId && !parameters.taxAuthGeoId && !parameters.toPaymentId) {
+        return error(UtilProperties.getResourceBundleMap("AccountingUiLabels", locale)?.AccountingPaymentApplicationParameterMissing)
+    }
+    GenericValue paymentAppl = delegator.makeValue("PaymentApplication")
+    paymentAppl.setNonPKFields(parameters)
+
+    GenericValue payment = from("Payment").where("paymentId", parameters.paymentId).queryOne()
+    if (!payment) {
+        return error(UtilProperties.getResourceBundleMap("AccountingUiLabels", locale)?.AccountingPaymentApplicationParameterMissing)
+    }
+
+    BigDecimal notAppliedPayment = PaymentWorker.getPaymentNotApplied(payment)
+
+    if (parameters.invoiceId) {
+        // get the invoice and do some further validation against it
+        GenericValue invoice = from("Invoice").where("invoiceId", parameters.invoiceId).queryOne()
+        // check the currencies if they are compatible
+        if (invoice.currencyUomId != payment.currencyUomId && invoice.currencyUomId != payment.actualCurrencyUomId) {
+            return error(UtilProperties.getResourceBundleMap("AccountingUiLabels", locale)?.AccountingCurrenciesOfInvoiceAndPaymentNotCompatible)
+        }
+        if (invoice.currencyUomId != payment.currencyUomId && invoice.currencyUomId == payment.actualCurrencyUomId) {
+            // if required get the payment amount in foreign currency (local we already have)
+            Boolean actual = true
+            notAppliedPayment = PaymentWorker.getPaymentNotApplied(payment, actual)
+        }
+        // get the amount that has not been applied yet for the invoice (outstanding amount)
+        BigDecimal notAppliedInvoice = InvoiceWorker.getInvoiceNotApplied(invoice)
+        if (notAppliedInvoice <= notAppliedPayment) {
+            paymentAppl.amountApplied = notAppliedInvoice
+        } else {
+            paymentAppl.amountApplied = notAppliedPayment
+        }
+
+        if (invoice.billingAccountId) {
+            paymentAppl.billingAccountId = invoice.billingAccountId
+        }
+    }
+
+    if (parameters.toPaymentId) {
+        // get the to payment and check the parent types are compatible
+        GenericValue toPayment = from("Payment").where("paymentId", parameters.toPaymentId).queryOne()
+        if (toPayment) {
+            toPaymentType = from("PaymentType").where("paymentTypeId", toPayment.paymentTypeId).queryOne()

Review comment:
       we can use getRelated method with cache. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org