blob: 677df2d0eff49c0de9b0301a1b3b237f2c654603 [file] [log] [blame]
<?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"
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/simple-methods-v2.xsd">
<simple-method method-name="createPayment" short-description="Create a Payment">
<if>
<condition>
<and>
<not><if-has-permission permission="ACCOUNTING" action="_CREATE"/></not>
<not><if-has-permission permission="PAY_INFO" action="_CREATE"/></not>
<not><if-compare-field field="userLogin.partyId" to-field="parameters.partyIdFrom" operator="equals"/></not>
<not><if-compare-field field="userLogin.partyId" to-field="parameters.partyIdTo" operator="equals"/></not>
</and>
</condition>
<then>
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingCreatePaymentPermissionError"/>
</add-error>
</then>
</if>
<check-errors/>
<make-value entity-name="Payment" value-field="payment"/>
<if-empty field="parameters.paymentId">
<sequenced-id sequence-name="Payment" field="payment.paymentId"/>
<else>
<set field="payment.paymentId" from-field="parameters.paymentId"/>
</else>
</if-empty>
<field-to-result field="payment.paymentId" result-name="paymentId"/>
<if-empty field="parameters.statusId">
<set field="parameters.statusId" value="PMNT_NOT_PAID"/>
</if-empty>
<if-not-empty field="parameters.paymentMethodId">
<entity-one entity-name="PaymentMethod" value-field="paymentMethod">
<field-map field-name="paymentMethodId" from-field="parameters.paymentMethodId"/>
</entity-one>
<if-compare-field field="parameters.paymentMethodTypeId" operator="not-equals" to-field="paymentMethod.paymentMethodTypeId">
<log level="info" message="Replacing passed payment method type [${parameters.paymentMethodTypeId}] with payment method type [${paymentMethod.paymentMethodTypeId}] for payment method [${parameters.paymentMethodId}]"/>
<set field="parameters.paymentMethodTypeId" from-field="paymentMethod.paymentMethodTypeId"/>
</if-compare-field>
</if-not-empty>
<if-not-empty field="parameters.paymentPreferenceId">
<entity-one value-field="orderPaymentPreference" entity-name="OrderPaymentPreference">
<field-map field-name="orderPaymentPreferenceId" from-field="parameters.paymentPreferenceId"/>
</entity-one>
<if-empty field="parameters.paymentMethodId">
<set field="parameters.paymentMethodId" from-field="orderPaymentPreference.paymentMethodId"/>
</if-empty>
<if-empty field="parameters.paymentMethodTypeId">
<set field="parameters.paymentMethodTypeId" from-field="orderPaymentPreference.paymentMethodTypeId"/>
</if-empty>
</if-not-empty>
<if-empty field="parameters.paymentMethodTypeId">
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingPaymentMethodIdPaymentMethodTypeIdNullError"/>
</add-error>
</if-empty>
<set-nonpk-fields map="parameters" value-field="payment"/>
<if-empty field="payment.effectiveDate">
<now-timestamp field="payment.effectiveDate"/>
</if-empty>
<create-value value-field="payment"/>
</simple-method>
<simple-method method-name="updatePayment" short-description="Update a Payment">
<make-value entity-name="Payment" value-field="lookupPayment"/>
<set-pk-fields value-field="lookupPayment" map="parameters"/>
<find-by-primary-key entity-name="Payment" map="lookupPayment" value-field="payment"/>
<if>
<condition>
<and>
<not><if-has-permission permission="ACCOUNTING" action="_UPDATE"/></not>
<not><if-has-permission permission="PAY_INFO" action="_UPDATE"/></not>
<not><if-compare-field field="userLogin.partyId" to-field="payment.partyIdFrom" operator="equals"/></not>
<not><if-compare-field field="userLogin.partyId" to-field="payment.partyIdTo" operator="equals"/></not>
</and>
</condition>
<then>
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingUpdatePaymentPermissionError"/>
</add-error>
</then>
</if>
<check-errors/>
<if-compare field="payment.statusId" value="PMNT_NOT_PAID" operator="not-equals">
<!-- check if only status change -->
<make-value value-field="newPayment" entity-name="Payment"/>
<make-value value-field="oldPayment" entity-name="Payment"/>
<set-nonpk-fields value-field="newPayment" map="payment"/>
<set-nonpk-fields value-field="oldPayment" map="payment"/>
<set-nonpk-fields value-field="newPayment" map="parameters"/>
<!-- fields :- comments, paymentRefNum, finAccountTransId, statusId can editable for Payment -->
<set field="oldPayment.statusId" from-field="newPayment.statusId"/>
<set field="oldPayment.comments" from-field="newPayment.comments"/>
<set field="oldPayment.paymentRefNum" from-field="newPayment.paymentRefNum" set-if-null="true"/>
<set field="oldPayment.finAccountTransId" from-field="newPayment.finAccountTransId" set-if-null="true"/>
<if-compare-field field="oldPayment" to-field="newPayment" operator="not-equals">
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingPSUpdateNotAllowedBecauseOfStatus"/>
</add-error>
</if-compare-field>
</if-compare>
<check-errors/>
<set field="statusIdSave" from-field="payment.statusId"/> <!-- do not allow status change here -->
<set-nonpk-fields map="parameters" value-field="payment"/>
<set field="payment.statusId" from-field="statusIdSave"/> <!-- do not allow status change here -->
<if-empty field="payment.effectiveDate">
<now-timestamp field="payment.effectiveDate"/>
</if-empty>
<if-not-empty field="payment.paymentMethodId">
<entity-one entity-name="PaymentMethod" value-field="paymentMethod">
<field-map field-name="paymentMethodId" from-field="payment.paymentMethodId"/>
</entity-one>
<if-compare-field field="payment.paymentMethodTypeId" to-field="paymentMethod.paymentMethodTypeId" operator="not-equals">
<log level="info" message="Replacing passed payment method type [${payment.paymentMethodTypeId}] with payment method type [${paymentMethod.paymentMethodTypeId}] for payment method [${payment.paymentMethodId}]"/>
</if-compare-field>
<set field="payment.paymentMethodTypeId" from-field="paymentMethod.paymentMethodTypeId"/>
</if-not-empty>
<store-value value-field="payment"/>
<if-not-empty field="parameters.statusId">
<if-compare-field field="parameters.statusId" to-field="statusIdSave" operator="not-equals">
<set-service-fields service-name="setPaymentStatus" map="parameters" to-map="param"/>
<call-service service-name="setPaymentStatus" in-map-name="param"/>
<check-errors/>
</if-compare-field>
</if-not-empty>
</simple-method>
<simple-method method-name="createPaymentApplication" short-description="Create a Payment Application">
<if-empty field="parameters.invoiceId">
<if-empty field="parameters.billingAccountId">
<if-empty field="parameters.taxAuthGeoId">
<if-empty field="parameters.toPaymentId">
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingPaymentApplicationParameterMissing"/>
</add-error>
<check-errors/>
</if-empty>
</if-empty>
</if-empty>
</if-empty>
<make-value entity-name="PaymentApplication" value-field="paymentAppl"/>
<set-nonpk-fields map="parameters" value-field="paymentAppl"/>
<entity-one entity-name="Payment" value-field="payment"/>
<if-empty field="payment">
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingPaymentApplicationParameterMissing"/>
</add-error>
<check-errors/>
</if-empty>
<call-class-method method-name="getPaymentNotApplied" class-name="org.ofbiz.accounting.payment.PaymentWorker" ret-field="notAppliedPayment">
<field field="payment" type="GenericValue"/>
</call-class-method>
<if-not-empty field="parameters.invoiceId">
<!-- get the invoice and do some further validation against it -->
<entity-one entity-name="Invoice" value-field="invoice"/>
<!-- check the currencies if they are compatible -->
<if>
<condition>
<and>
<if-compare-field field="invoice.currencyUomId" operator="not-equals" to-field="payment.currencyUomId"/>
<if-compare-field field="invoice.currencyUomId" operator="not-equals" to-field="payment.actualCurrencyUomId"/>
</and>
</condition>
<then>
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingCurrenciesOfInvoiceAndPaymentNotCompatible"/>
</add-error>
</then>
</if>
<check-errors/>
<if>
<condition>
<and>
<if-compare-field field="invoice.currencyUomId" operator="not-equals" to-field="payment.currencyUomId"/>
<if-compare-field field="invoice.currencyUomId" operator="equals" to-field="payment.actualCurrencyUomId"/>
</and>
</condition>
<then><!-- if required get the payment amount in foreign currency (local we already have) -->
<set field="actual" value="true" type="Boolean"/>
<call-class-method method-name="getPaymentNotApplied" class-name="org.ofbiz.accounting.payment.PaymentWorker" ret-field="notAppliedPayment">
<field field="payment" type="GenericValue"/>
<field field="actual" type="Boolean"/>
</call-class-method>
</then>
</if>
<!-- get the amount that has not been applied yet for the invoice (outstanding amount) -->
<call-class-method method-name="getInvoiceNotApplied" class-name="org.ofbiz.accounting.invoice.InvoiceWorker" ret-field="notAppliedInvoice">
<field field="invoice" type="GenericValue"/>
</call-class-method>
<if-compare-field field="notAppliedInvoice" operator="less-equals" to-field="notAppliedPayment" type="BigDecimal">
<set field="paymentAppl.amountApplied" from-field="notAppliedInvoice"/>
<else>
<set field="paymentAppl.amountApplied" from-field="notAppliedPayment"/>
</else>
</if-compare-field>
<if-not-empty field="invoice.billingAccountId">
<set field="paymentAppl.billingAccountId" from-field="invoice.billingAccountId"/>
</if-not-empty>
</if-not-empty>
<if-not-empty field="parameters.toPaymentId">
<!-- get the to payment and check the parent types are compatible -->
<entity-one entity-name="Payment" value-field="toPayment">
<field-map field-name="paymentId" from-field="parameters.toPaymentId"/>
</entity-one>
<entity-one value-field="toPaymentType" entity-name="PaymentType">
<field-map field-name="paymentTypeId" from-field="toPayment.paymentTypeId"/>
</entity-one>
<entity-one entity-name="Payment" value-field="payment">
<field-map field-name="paymentId" from-field="parameters.paymentId"/>
</entity-one>
<entity-one value-field="paymentType" entity-name="PaymentType">
<field-map field-name="paymentTypeId" from-field="payment.paymentTypeId"/>
</entity-one>
<!-- when amount not provided use the the lowest value available -->
<if-empty field="parameters.amountApplied">
<call-class-method method-name="getPaymentNotApplied" class-name="org.ofbiz.accounting.payment.PaymentWorker" ret-field="notAppliedPayment">
<field field="payment" type="GenericValue"/>
</call-class-method>
<call-class-method method-name="getPaymentNotApplied" class-name="org.ofbiz.accounting.payment.PaymentWorker" ret-field="notAppliedToPayment">
<field field="toPayment" type="GenericValue"/>
</call-class-method>
<if-compare-field operator="less" field="notAppliedPayment" to-field="notAppliedToPayment">
<set field="paymentAppl.amountApplied" from-field="notAppliedPayment"/>
<else>
<set field="paymentAppl.amountApplied" from-field="notAppliedToPayment"/>
</else>
</if-compare-field>
</if-empty>
</if-not-empty>
<if-not-empty field="parameters.billingAccountId">
<if-empty field="paymentAppl.amountApplied">
<set field="paymentAppl.amountApplied" from-field="notAppliedPayment"/>
</if-empty>
</if-not-empty>
<if-not-empty field="parameters.taxAuthGeoId">
<if-empty field="paymentAppl.amountApplied">
<set field="paymentAppl.amountApplied" from-field="notAppliedPayment"/>
</if-empty>
</if-not-empty>
<sequenced-id sequence-name="PaymentApplication" field="paymentAppl.paymentApplicationId"/>
<field-to-result field="paymentAppl.amountApplied" result-name="amountApplied"/>
<field-to-result field="paymentAppl.paymentApplicationId" result-name="paymentApplicationId"/>
<create-value value-field="paymentAppl"/>
<entity-one value-field="payment" entity-name="Payment"/>
<field-to-result field="payment.paymentTypeId" result-name="paymentTypeId"/>
</simple-method>
<simple-method method-name="setPaymentStatus" short-description="Set The Payment Status">
<check-permission permission="ACCOUNTING" action="_UPDATE">
<alt-permission permission="ACCOUNTING_ROLE" action="_UPDATE"/>
<fail-property resource="AccountingUiLabels" property="AccountingPermissionError"/>
</check-permission>
<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.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="quickSendPayment" short-description="Update a Payment then set it to status PMNT_SENT">
<call-service service-name="updatePayment" in-map-name="parameters"/>
<set field="param.paymentId" from-field="parameters.paymentId"/>
<set field="param.statusId" value="PMNT_SENT"/>
<call-service service-name="setPaymentStatus" in-map-name="param"/>
</simple-method>
<simple-method method-name="createPaymentAndApplication" short-description="Create a payment and a payment application for the full amount">
<set-service-fields service-name="createPayment" map="parameters" to-map="createPaymentInMap"/>
<call-service service-name="createPayment" in-map-name="createPaymentInMap">
<result-to-field field="paymentId" result-name="paymentId"/>
</call-service>
<check-errors/>
<set-service-fields service-name="createPaymentApplication" map="parameters" to-map="createPaymentAppInMap"/>
<set field="createPaymentAppInMap.paymentId" from-field="paymentId"/>
<set field="createPaymentAppInMap.amountApplied" from-field="parameters.amount"/>
<call-service service-name="createPaymentApplication" in-map-name="createPaymentAppInMap">
<result-to-field field="paymentApplicationId" result-name="paymentApplicationId"/>
</call-service>
<check-errors/>
<field-to-result field="paymentId" result-name="paymentId"/>
<field-to-result field="paymentApplicationId" result-name="paymentApplicationId"/>
</simple-method>
<simple-method method-name="getInvoicePaymentInfoList" short-description="Create a list with information on payment due dates and amounts for the invoice">
<if-empty field="parameters.invoice">
<entity-one entity-name="Invoice" value-field="invoice"/>
<else>
<set field="invoice" from-field="parameters.invoice"/>
</else>
</if-empty>
<call-class-method class-name="org.ofbiz.accounting.invoice.InvoiceWorker" method-name="getInvoiceTotal" ret-field="invoiceTotalAmount">
<field field="invoice" type="org.ofbiz.entity.GenericValue"/>
</call-class-method>
<call-class-method class-name="org.ofbiz.accounting.invoice.InvoiceWorker" method-name="getInvoiceApplied" ret-field="invoiceTotalAmountPaid">
<field field="invoice" type="org.ofbiz.entity.GenericValue"/>
</call-class-method>
<get-related relation-name="InvoiceTerm" value-field="invoice" list="invoiceTerms"/>
<set field="remainingAppliedAmount" from-field="invoiceTotalAmountPaid" type="BigDecimal"/>
<set field="computedTotalAmount" value="0.0" type="BigDecimal"/>
<iterate list="invoiceTerms" entry="invoiceTerm">
<get-related-one relation-name="TermType" value-field="invoiceTerm" to-value-field="termType" use-cache="true"/>
<if-compare field="termType.parentTypeId" operator="equals" value="FIN_PAYMENT_TERM">
<clear-field field="invoicePaymentInfo"/>
<set field="invoicePaymentInfo.invoiceId" from-field="invoice.invoiceId"/>
<set field="invoicePaymentInfo.invoiceTermId" from-field="invoiceTerm.invoiceTermId"/>
<set field="invoicePaymentInfo.termTypeId" from-field="invoiceTerm.termTypeId"/>
<call-class-method class-name="org.ofbiz.base.util.UtilDateTime" method-name="getDayEnd" ret-field="invoicePaymentInfo.dueDate">
<field field="invoice.invoiceDate" type="Timestamp"/>
<field field="invoiceTerm.termDays" type="Long"/>
</call-class-method>
<calculate field="invoiceTermAmount" type="BigDecimal">
<calcop field="invoiceTotalAmount" operator="multiply">
<calcop field="invoiceTerm.termValue" operator="get"/>
</calcop>
</calculate>
<calculate field="invoiceTermAmount" type="BigDecimal">
<calcop field="invoiceTermAmount" operator="divide">
<number value="100"/>
</calcop>
</calculate>
<set field="invoicePaymentInfo.amount" from-field="invoiceTermAmount"/>
<calculate field="computedTotalAmount" type="BigDecimal">
<calcop field="computedTotalAmount" operator="add">
<calcop field="invoicePaymentInfo.amount" operator="get"/>
</calcop>
</calculate>
<if-compare-field field="remainingAppliedAmount" to-field="invoiceTermAmount" operator="greater-equals" type="BigDecimal">
<set field="invoicePaymentInfo.paidAmount" from-field="invoiceTermAmount" type="BigDecimal"/>
<calculate field="remainingAppliedAmount" type="BigDecimal">
<calcop field="remainingAppliedAmount" operator="subtract">
<calcop field="invoiceTermAmount" operator="get"/>
</calcop>
</calculate>
<else>
<set field="invoicePaymentInfo.paidAmount" from-field="remainingAppliedAmount" type="BigDecimal"/>
<set field="remainingAppliedAmount" value="0.0" type="BigDecimal"/>
</else>
</if-compare-field>
<calculate field="invoicePaymentInfo.outstandingAmount" type="BigDecimal">
<calcop field="invoicePaymentInfo.amount" operator="subtract">
<calcop field="invoicePaymentInfo.paidAmount" operator="get"/>
</calcop>
</calculate>
<set field="invoicePaymentInfoList[]" from-field="invoicePaymentInfo"/>
</if-compare>
</iterate>
<if>
<condition>
<or>
<if-compare field="remainingAppliedAmount" operator="greater" value="0.0" type="BigDecimal"/>
<if-compare field="invoiceTotalAmount" operator="less-equals" value="0.0" type="BigDecimal"/>
<if-compare-field field="computedTotalAmount" to-field="invoiceTotalAmount" operator="less" type="BigDecimal"/>
</or>
</condition>
<then>
<clear-field field="invoicePaymentInfo"/>
<set field="andMap.termTypeId" value="FIN_PAYMENT_TERM"/>
<filter-list-by-and list="invoiceTerms" map="andMap"/>
<first-from-list list="invoiceTerms" entry="invoiceTerm"/>
<if-not-empty field="invoiceTerm">
<set field="invoicePaymentInfo.termTypeId" from-field="invoiceTerm.termTypeId"/>
<call-class-method class-name="org.ofbiz.base.util.UtilDateTime" method-name="getDayEnd" ret-field="invoicePaymentInfo.dueDate">
<field field="invoice.invoiceDate" type="Timestamp"/>
<field field="invoiceTerm.termDays" type="Long"/>
</call-class-method>
<else>
<call-class-method class-name="org.ofbiz.base.util.UtilDateTime" method-name="getDayEnd" ret-field="invoicePaymentInfo.dueDate">
<field field="invoice.invoiceDate" type="Timestamp"/>
</call-class-method>
</else>
</if-not-empty>
<set field="invoicePaymentInfo.invoiceId" from-field="invoice.invoiceId"/>
<calculate field="invoicePaymentInfo.amount" type="BigDecimal">
<calcop field="invoiceTotalAmount" operator="subtract">
<calcop field="computedTotalAmount" operator="get"/>
</calcop>
</calculate>
<set field="invoicePaymentInfo.paidAmount" from-field="remainingAppliedAmount" type="BigDecimal"/>
<calculate field="invoicePaymentInfo.outstandingAmount" type="BigDecimal">
<calcop field="invoicePaymentInfo.amount" operator="subtract">
<calcop field="invoicePaymentInfo.paidAmount" operator="get"/>
</calcop>
</calculate>
<set field="invoicePaymentInfoList[]" from-field="invoicePaymentInfo"/>
</then>
</if>
<field-to-result field="invoicePaymentInfoList" result-name="invoicePaymentInfoList"/>
</simple-method>
<simple-method method-name="getInvoicePaymentInfoListByDueDateOffset" short-description="Select a list with information on payment due dates and amounts for invoices.">
<now-timestamp field="nowTimestamp"/>
<call-class-method class-name="org.ofbiz.base.util.UtilDateTime" method-name="getDayEnd" ret-field="asOfDate">
<field field="nowTimestamp" type="Timestamp"/>
<field field="parameters.daysOffset" type="Long"/>
</call-class-method>
<entity-condition entity-name="Invoice" list="invoices">
<condition-list combine="and">
<condition-expr field-name="invoiceTypeId" operator="equals" from-field="parameters.invoiceTypeId"/>
<condition-expr field-name="partyId" operator="equals" from-field="parameters.partyId" ignore-if-empty="true"/>
<condition-expr field-name="partyIdFrom" operator="equals" from-field="parameters.partyIdFrom" ignore-if-empty="true"/>
<condition-expr field-name="statusId" operator="not-equals" value="INVOICE_CANCELLED"/>
<condition-expr field-name="statusId" operator="not-equals" value="INVOICE_PAID"/>
</condition-list>
<order-by field-name="invoiceDate"/>
</entity-condition>
<iterate list="invoices" entry="invoice">
<clear-field field="getInvoicePaymentInfoListInMap"/>
<set field="getInvoicePaymentInfoListInMap.invoice" from-field="invoice"/>
<call-service service-name="getInvoicePaymentInfoList" in-map-name="getInvoicePaymentInfoListInMap">
<result-to-field field="invoicePaymentInfoList" result-name="invoicePaymentInfoList"/>
</call-service>
<iterate list="invoicePaymentInfoList" entry="invoicePaymentInfo">
<if>
<condition>
<and>
<if-compare field="invoicePaymentInfo.outstandingAmount" operator="greater" value="0.0" type="BigDecimal"/>
<if-compare-field field="invoicePaymentInfo.dueDate" to-field="asOfDate" operator="less" type="Timestamp"/>
</and>
</condition>
<then>
<set field="selectedInvoicePaymentInfoList[]" from-field="invoicePaymentInfo"/>
</then>
</if>
</iterate>
</iterate>
<field-to-result field="selectedInvoicePaymentInfoList" result-name="invoicePaymentInfoList"/>
</simple-method>
<simple-method method-name="voidPayment" short-description="Service to void a payment">
<check-permission permission="ACCOUNTING" action="_UPDATE">
<alt-permission permission="ACCOUNTING_ROLE" action="_UPDATE"/>
<fail-property resource="AccountingUiLabels" property="AccountingPermissionError"/>
</check-permission>
<entity-one entity-name="Payment" value-field="payment"/>
<field-to-result field="payment.finAccountTransId" result-name="finAccountTransId"/>
<set field="transStatusId" value="FINACT_TRNS_CANCELED"/>
<field-to-result field="transStatusId" result-name="statusId"/>
<if-empty field="payment">
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingNoPaymentsfound"/>
</add-error>
<check-errors/>
</if-empty>
<set field="paymentId" from-field="parameters.paymentId"/>
<set field="paymentStatusCtx.paymentId" from-field="paymentId"/>
<set field="paymentStatusCtx.statusId" value="PMNT_VOID"/>
<call-service service-name="setPaymentStatus" in-map-name="paymentStatusCtx"/>
<get-related relation-name="PaymentApplication" value-field="payment" list="paymentApplications"/>
<iterate list="paymentApplications" entry="paymentApplication">
<get-related-one relation-name="Invoice" value-field="paymentApplication" to-value-field="updateInvoiceCtx"/>
<if-compare field="updateInvoiceCtx.statusId" operator="equals" value="INVOICE_PAID">
<set-service-fields service-name="updateInvoice" map="updateInvoiceCtx" to-map="invoiceStatusCtx"/>
<set field="invoiceStatusCtx.paidDate" type="Timestamp" value=""/>
<set field="invoiceStatusCtx.statusId" value="INVOICE_READY"/>
<call-service service-name="setInvoiceStatus" in-map-name="invoiceStatusCtx"/>
</if-compare>
<set field="removePaymentApplicationCtx.paymentApplicationId" from-field="paymentApplication.paymentApplicationId"/>
<call-service service-name="removePaymentApplication" in-map-name="removePaymentApplicationCtx"/>
</iterate>
<entity-condition entity-name="AcctgTrans" list="acctgTransPaymentList">
<condition-list combine="and">
<condition-expr field-name="invoiceId" operator="equals" from-field="nullField"/>
<condition-expr field-name="paymentId" from-field="paymentId"/>
</condition-list>
</entity-condition>
<iterate list="acctgTransPaymentList" entry="acctgTransPayment">
<clear-field field="copyAcctgTransCtx"/>
<set field="copyAcctgTransCtx.fromAcctgTransId" from-field="acctgTransPayment.acctgTransId"/>
<set field="copyAcctgTransCtx.revert" value="Y"/>
<call-service service-name="copyAcctgTransAndEntries" in-map-name="copyAcctgTransCtx">
<result-to-field field="postAcctgTransMap.acctgTransId" result-name="acctgTransId"/>
</call-service>
<if-compare field="acctgTransPayment.isPosted" operator="equals" value="Y">
<call-service service-name="postAcctgTrans" in-map-name="postAcctgTransMap"/>
</if-compare>
<clear-field field="postAcctgTransMap"/>
</iterate>
</simple-method>
<simple-method method-name="getPaymentRunningTotal" short-description="calculate running total for payments">
<set field="paymentIds" from-field="parameters.paymentIds"/>
<set field="runningTotal" type="BigDecimal" value="0"/>
<entity-condition entity-name="Payment" list="payments">
<condition-expr field-name="paymentId" operator="in" from-field="paymentIds"/>
</entity-condition>
<iterate list="payments" entry="payment">
<set field="runningTotal" value="${runningTotal + payment.amount}" type="BigDecimal"/>
</iterate>
<set-service-fields service-name="getPartyAccountingPreferences" map="parameters" to-map="getPartyAccountingPreferencesMap"/>
<call-service service-name="getPartyAccountingPreferences" in-map-name="getPartyAccountingPreferencesMap">
<result-to-field result-name="partyAccountingPreference"/>
</call-service>
<set field="currencyUomId" from-field="partyAccountingPreference.baseCurrencyUomId"/>
<if-empty field="currencyUomId">
<property-to-field resource="general" property="currency.uom.id.default" field="currencyUomId"/>
</if-empty>
<set field="paymentRunningTotal" value="${groovy:org.ofbiz.base.util.UtilFormatOut.formatCurrency(runningTotal, currencyUomId, parameters.locale)}"/>
<field-to-result field="paymentRunningTotal"/>
</simple-method>
<simple-method method-name="cancelPaymentBatch" short-description="cancel payment batch">
<entity-and entity-name="PmtGrpMembrPaymentAndFinAcctTrans" list="paymentGroupMemberAndTransList">
<field-map field-name="paymentGroupId" from-field="parameters.paymentGroupId"/>
</entity-and>
<if-not-empty field="paymentGroupMemberAndTransList">
<first-from-list list="paymentGroupMemberAndTransList" entry="paymentGroupMemberAndTrans"/>
<if-compare field="paymentGroupMemberAndTrans.finAccountTransStatusId" operator="equals" value="FINACT_TRNS_APPROVED">
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingTransactionIsAlreadyReconciled"/>
</add-error>
<check-errors/>
</if-compare>
<iterate list="paymentGroupMemberAndTransList" entry="paymentGroupMemberAndTrans">
<set-service-fields service-name="expirePaymentGroupMember" map="paymentGroupMemberAndTrans" to-map="expirePaymentGroupMemberMap"/>
<call-service service-name="expirePaymentGroupMember" in-map-name="expirePaymentGroupMemberMap"/>
<entity-one entity-name="FinAccountTrans" value-field="finAccountTrans">
<field-map field-name="finAccountTransId" from-field="paymentGroupMemberAndTrans.finAccountTransId"/>
</entity-one>
<if-not-empty field="finAccountTrans">
<set-service-fields service-name="setFinAccountTransStatus" map="finAccountTrans" to-map="setFinAccountTransStatusMap"/>
<set field="setFinAccountTransStatusMap.statusId" value="FINACT_TRNS_CANCELED"/>
<call-service service-name="setFinAccountTransStatus" in-map-name="setFinAccountTransStatusMap"/>
</if-not-empty>
</iterate>
</if-not-empty>
</simple-method>
<simple-method method-name="createPaymentAndPaymentGroupForInvoices" short-description="Creates Payments, PaymentApplications and PaymentGroup for the same">
<entity-one entity-name="PaymentMethod" value-field="paymentMethod" auto-field-map="true"/>
<entity-one entity-name="FinAccount" value-field="finAccount" auto-field-map="false">
<field-map field-name="finAccountId" from-field="paymentMethod.finAccountId"/>
</entity-one>
<if-compare field="finAccount.statusId" operator="equals" value="FNACT_MANFROZEN">
<add-error>
<fail-property resource="AccountingErrorUiLabels.xml" property="AccountingFinAccountInactiveStatusError"/>
</add-error>
</if-compare>
<if-compare field="finAccount.statusId" operator="equals" value="FNACT_CANCELLED">
<add-error>
<fail-property resource="AccountingErrorUiLabels.xml" property="AccountingFinAccountStatusNotValidError"/>
</add-error>
</if-compare>
<check-errors/>
<set field="invoices" type="List"/>
<iterate list="parameters.invoiceIds" entry="invoiceId">
<entity-one entity-name="Invoice" value-field="invoice" auto-field-map="true"/>
<clear-field field="invoices"/>
<set field="invoices" from-field="partyInvoices.${invoice.partyIdFrom}"/>
<field-to-list field="invoice" list="invoices"/>
<set field="partyInvoices.${invoice.partyIdFrom}" from-field="invoices"/>
</iterate>
<clear-field field="invoices"/>
<iterate-map map="partyInvoices" key="partyId" value="invoices">
<set-service-fields service-name="createPaymentAndApplicationForParty" map="parameters" to-map="createPaymentAndApplicationForPartyMap"/>
<set field="createPaymentAndApplicationForPartyMap.paymentMethodTypeId" from-field="paymentMethod.paymentMethodTypeId"/>
<set field="createPaymentAndApplicationForPartyMap.finAccountId" from-field="paymentMethod.finAccountId"/>
<set field="createPaymentAndApplicationForPartyMap.partyId" from-field="partyId"/>
<set field="createPaymentAndApplicationForPartyMap.invoices" from-field="invoices"/>
<if-not-empty field="parameters.checkStartNumber">
<set field="parameters.checkStartNumber" value="${parameters.checkStartNumber + 1}" type="Long"/>
</if-not-empty>
<call-service service-name="createPaymentAndApplicationForParty" in-map-name="createPaymentAndApplicationForPartyMap">
<result-to-field result-name="paymentId" field="paymentId"/>
</call-service>
<field-to-list field="paymentId" list="paymentIds"/>
</iterate-map>
<if-not-empty field="paymentIds">
<set field="createPaymentGroupAndMemberMap.paymentIds" from-field="paymentIds"/>
<set field="createPaymentGroupAndMemberMap.paymentGroupTypeId" value="CHECK_RUN"/>
<set field="createPaymentGroupAndMemberMap.paymentGroupName" value="Payment group for Check Run(InvoiceIds-${parameters.invoiceIds})"/>
<call-service service-name="createPaymentGroupAndMember" in-map-name="createPaymentGroupAndMemberMap">
<result-to-field result-name="paymentGroupId"/>
<result-to-result result-name="paymentGroupId"/>
</call-service>
</if-not-empty>
<if-empty field="paymentGroupId">
<property-to-field property="AccountingNoInvoicesReadyOrOutstandingAmountZero" resource="AccountingUiLabels" field="errorMessage"/>
<field-to-result field="errorMessage" result-name="errorMessage"/>
</if-empty>
</simple-method>
<simple-method method-name="createPaymentAndApplicationForParty" short-description="create Payment and PaymentApplications for multiple invoices for one party">
<set field="paymentAmount" type="BigDecimal" value="0"/>
<iterate list="parameters.invoices" entry="invoice">
<if-compare field="invoice.statusId" operator="equals" value="INVOICE_READY" type="String">
<set-service-fields service-name="getInvoicePaymentInfoList" map="invoice" to-map="getInvoicePaymentInfoListCtx"/>
<call-service service-name="getInvoicePaymentInfoList" in-map-name="getInvoicePaymentInfoListCtx">
<result-to-field result-name="invoicePaymentInfoList"/>
</call-service>
<first-from-list list="invoicePaymentInfoList" entry="invoicePaymentInfo"/>
<set field="paymentAmount" value="${paymentAmount + invoicePaymentInfo.outstandingAmount}"/>
<else>
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingInvoicesRequiredInReadyStatus"/>
</add-error>
<check-errors/>
</else>
</if-compare>
</iterate>
<if-compare field="paymentAmount" operator="greater" type="BigDecimal" value="0">
<set-service-fields service-name="getPartyAccountingPreferences" map="parameters" to-map="getPartyAccountingPreferencesMap"/>
<call-service service-name="getPartyAccountingPreferences" in-map-name="getPartyAccountingPreferencesMap">
<result-to-field result-name="partyAccountingPreference" field="partyAcctgPreference"/>
</call-service>
<set field="createPaymentMap.paymentTypeId" value="VENDOR_PAYMENT"/>
<set field="createPaymentMap.partyIdFrom" from-field="parameters.organizationPartyId"/>
<set field="createPaymentMap.currencyUomId" from-field="partyAcctgPreference.baseCurrencyUomId"/>
<set field="createPaymentMap.partyIdTo" from-field="parameters.partyId"/>
<set field="createPaymentMap.statusId" value="PMNT_SENT"/>
<set field="createPaymentMap.amount" type="BigDecimal" from-field="paymentAmount"/>
<set field="createPaymentMap.paymentMethodTypeId" from-field="parameters.paymentMethodTypeId"/>
<set field="createPaymentMap.paymentMethodId" from-field="parameters.paymentMethodId"/>
<set field="createPaymentMap.paymentRefNum" type="String" from-field="parameters.checkStartNumber"/>
<call-service service-name="createPayment" in-map-name="createPaymentMap">
<result-to-field result-name="paymentId"/>
<result-to-result result-name="paymentId"/>
</call-service>
<iterate list="parameters.invoices" entry="invoice">
<if-compare field="invoice.statusId" operator="equals" value="INVOICE_READY">
<set-service-fields service-name="getInvoicePaymentInfoList" map="invoice" to-map="getInvoicePaymentInfoListCtx"/>
<call-service service-name="getInvoicePaymentInfoList" in-map-name="getInvoicePaymentInfoListCtx">
<result-to-field result-name="invoicePaymentInfoList" field="invoicePaymentInfoList"/>
</call-service>
<first-from-list list="invoicePaymentInfoList" entry="invoicePaymentInfo"/>
<if-compare field="invoicePaymentInfo.outstandingAmount" operator="greater" value="0">
<set field="createPaymentApplicationMap.paymentId" from-field="paymentId"/>
<set field="createPaymentApplicationMap.amountApplied" from-field="invoicePaymentInfo.outstandingAmount"/>
<set field="createPaymentApplicationMap.invoiceId" from-field="invoice.invoiceId"/>
<call-service service-name="createPaymentApplication" in-map-name="createPaymentApplicationMap"/>
</if-compare>
</if-compare>
<field-to-list field="invoice.invoiceId" list="invoiceIds"/>
<clear-field field="createPaymentApplicationMap"/>
</iterate>
</if-compare>
<field-to-result field="invoiceIds"/>
<set field="amount" type="BigDecimal" from-field="paymentAmount"/>
<field-to-result field="amount"/>
</simple-method>
<simple-method method-name="createFinAccoutnTransFromPayment" short-description="Creates a record for FinAccountTrans on creation of payment.">
<set-service-fields service-name="createFinAccountTrans" map="parameters" to-map="createFinAccountTransMap"/>
<set field="createFinAccountTransMap.finAccountTransTypeId" value="WITHDRAWAL"/>
<set field="createFinAccountTransMap.partyId" from-field="parameters.organizationPartyId"/>
<now-timestamp field="createFinAccountTransMap.transactionDate"/>
<now-timestamp field="createFinAccountTransMap.entryDate"/>
<!-- set field="createFinAccountTransMap.statusId" value="CREATE"/> --> <!-- TODO: It will uncomment when status Id field will add in FinAccountTrans Entity. -->
<set field="createFinAccountTransMap.comments" value="Pay to ${parameters.partyId} for invoice Ids - ${parameters.invoiceIds}"/>
<call-service service-name="createFinAccountTrans" in-map-name="createFinAccountTransMap">
<result-to-field result-name="finAccountTransId"/>
</call-service>
<set field="updatePaymentMap.finAccountTransId" from-field="finAccountTransId"/>
<set field="updatePaymentMap.paymentId" from-field="parameters.paymentId"/>
<call-service service-name="updatePayment" in-map-name="updatePaymentMap"/>
</simple-method>
<simple-method method-name="createPaymentGroupAndMember" short-description="creates PaymentGroup and PaymentGroupMembers">
<if-empty field="parameters.fromDate">
<now-timestamp field="parameters.fromDate"/>
</if-empty>
<set-service-fields service-name="createPaymentGroup" map="parameters" to-map="createPaymentGroupMap"/>
<if-empty field="parameters.paymentGroupName">
<set field="createPaymentGroupMap.paymentGroupName" value="Payment Group Name"/>
</if-empty>
<call-service service-name="createPaymentGroup" in-map-name="createPaymentGroupMap">
<result-to-field result-name="paymentGroupId"/>
<result-to-result result-name="paymentGroupId"/>
</call-service>
<set field="createPaymentGroupMemberMap.paymentGroupId" from-field="paymentGroupId"/>
<set field="createPaymentGroupMemberMap.fromDate" from-field="parameters.fromDate"/>
<iterate list="parameters.paymentIds" entry="paymentId">
<set field="createPaymentGroupMemberMap.paymentId" from-field="paymentId"/>
<call-service service-name="createPaymentGroupMember" in-map-name="createPaymentGroupMemberMap"/>
</iterate>
</simple-method>
<simple-method method-name="cancelCheckRunPayments" short-description="Cancel all payments for payment group">
<entity-and entity-name="PmtGrpMembrPaymentAndFinAcctTrans" list="paymentGroupMemberAndTransList">
<field-map field-name="paymentGroupId" from-field="parameters.paymentGroupId"/>
</entity-and>
<first-from-list list="paymentGroupMemberAndTransList" entry="paymentGroupMemberAndTrans"/>
<if-compare field="paymentGroupMemberAndTrans.finAccountTransStatusId" operator="not-equals" value="FINACT_TRNS_APPROVED">
<iterate list="paymentGroupMemberAndTransList" entry="paymentGroupMemberAndTrans">
<entity-one entity-name="Payment" value-field="payment">
<field-map field-name="paymentId" from-field="paymentGroupMemberAndTrans.paymentId"/>
</entity-one>
<set-service-fields service-name="voidPayment" map="payment" to-map="voidPaymentMap"/>
<call-service service-name="voidPayment" in-map-name="voidPaymentMap"/>
<set-service-fields service-name="expirePaymentGroupMember" map="paymentGroupMemberAndTrans" to-map="expirePaymentGroupMemberMap"/>
<call-service service-name="expirePaymentGroupMember" in-map-name="expirePaymentGroupMemberMap"/>
<clear-field field="voidPaymentMap"/>
<clear-field field="expirePaymentGroupMemberMap"/>
</iterate>
<else>
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingCheckIsAlreadyIssued"/>
</add-error>
<check-errors/>
</else>
</if-compare>
</simple-method>
<simple-method method-name="getPayments" short-description="Get list of payment">
<set field="paymentGroupId" from-field="parameters.paymentGroupId"/>
<if-not-empty field="paymentGroupId">
<entity-and entity-name="PaymentGroupMember" list="paymentGroupMembers" filter-by-date="true">
<field-map field-name="paymentGroupId"/>
</entity-and>
<set field="paymentIds" value="${groovy:org.ofbiz.entity.util.EntityUtil.getFieldListFromEntityList(paymentGroupMembers, 'paymentId', true);}" type="List"/>
<entity-condition entity-name="Payment" list="payments">
<condition-expr field-name="paymentId" operator="in" from-field="paymentIds"/>
</entity-condition>
</if-not-empty>
<set field="finAccountTransId" from-field="parameters.finAccountTransId"/>
<if-not-empty field="finAccountTransId">
<entity-and entity-name="Payment" list="payments">
<field-map field-name="finAccountTransId"/>
</entity-and>
</if-not-empty>
<field-to-result field="payments"/>
</simple-method>
<simple-method method-name="getPaymentGroupReconciliationId" short-description="Get ReconciliationId associated to paymentGroup">
<set field="paymentGroupId" from-field="parameters.paymentGroupId"/>
<entity-and entity-name="PaymentGroupMember" list="paymentGroupMembers">
<field-map field-name="paymentGroupId"/>
</entity-and>
<if-not-empty field="paymentGroupMembers">
<first-from-list list="paymentGroupMembers" entry="paymentGroupMember"/>
<get-related-one relation-name="Payment" value-field="paymentGroupMember" to-value-field="payment"/>
<get-related-one relation-name="FinAccountTrans" value-field="payment" to-value-field="finAccountTrans"/>
<if-not-empty field="finAccountTrans">
<if-not-empty field="finAccountTrans.glReconciliationId">
<set field="glReconciliationId" from-field="finAccountTrans.glReconciliationId"/>
</if-not-empty>
</if-not-empty>
</if-not-empty>
<field-to-result field="glReconciliationId"/>
</simple-method>
<simple-method method-name="checkAndCreateBatchForValidPayments" short-description="Check the valid(unbatched) payment and create batch for same">
<set field="paymentIds" from-field="parameters.paymentIds"/>
<entity-condition entity-name="Payment" list="payments">
<condition-expr field-name="paymentId" operator="in" from-field="paymentIds"/>
</entity-condition>
<iterate list="payments" entry="payment">
<set field="isReceipt" value="${groovy:org.ofbiz.accounting.util.UtilAccounting.isReceipt(payment)}" type="Boolean"/>
<if-compare field="isReceipt" operator="equals" value="false" type="Boolean">
<field-to-list field="payment.paymentId" list="disbursementPaymentIds"/>
</if-compare>
</iterate>
<if-not-empty field="disbursementPaymentIds">
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingCannotIncludeApPaymentError"/>
</add-error>
<check-errors/>
</if-not-empty>
<entity-condition entity-name="PaymentGroupMember" list="paymentGroupMembers" filter-by-date="true">
<condition-expr field-name="paymentId" operator="in" from-field="paymentIds"/>
</entity-condition>
<if-not-empty field="paymentGroupMembers">
<set field="batchPaymentIds" value="${groovy:org.ofbiz.entity.util.EntityUtil.getFieldListFromEntityList(paymentGroupMembers, 'paymentId', true);}" type="List"/>
<add-error>
<fail-property resource="AccountingUiLabels" property="AccountingPaymentsAreAlreadyBatchedError"/>
</add-error>
<check-errors/>
<else>
<set-service-fields service-name="createPaymentGroupAndMember" map="parameters" to-map="createPaymentGroupAndMemberMap"/>
<call-service service-name="createPaymentGroupAndMember" in-map-name="createPaymentGroupAndMemberMap">
<result-to-result result-name="paymentGroupId"/>
</call-service>
</else>
</if-not-empty>
</simple-method>
<simple-method method-name="massChangePaymentStatus" short-description="Service set status of Payments in bulk.">
<iterate list="parameters.paymentIds" entry="paymentId">
<set field="setPaymentStatusMap.paymentId" from-field="paymentId"/>
<set field="setPaymentStatusMap.statusId" from-field="parameters.statusId"/>
<call-service service-name="setPaymentStatus" in-map-name="setPaymentStatusMap"/>
<clear-field field="setPaymentStatusMap"/>
</iterate>
</simple-method>
<simple-method method-name="createPaymentFromOrder" short-description="Service auto create Payment from Order when payment does exist yet and not diabled by accountingconfig">
<entity-one value-field="orderHeader" entity-name="OrderHeader"/>
<if-compare operator="equals" value="PURCHASE_ORDER" field="orderHeader.orderTypeId">
<property-to-field resource="AccountingConfig" property="accounting.payment.purchaseorder.autocreate" field="purchaseAutoCreate" default="Y"/>
<if-compare operator="not-equals" value="Y" field="purchaseAutoCreate">
<log level="info" message="payment not created from approved order because config (accounting.payment.purchaseorder.autocreate) is not set to Y (AccountingConfig.properties)"/>
<return/>
</if-compare>
</if-compare>
<if-compare operator="equals" value="SALES_ORDER" field="orderHeader.orderTypeId">
<property-to-field resource="AccountingConfig" property="accounting.payment.salesorder.autocreate" field="salesAutoCreate" default="Y"/>
<if-compare operator="not-equals" value="Y" field="salesAutoCreate">
<log level="info" message="payment not created from approved order because config (accounting.payment.salesorder.autocreate) is not set to Y (AccountingConfig.properties)"/>
<return/>
</if-compare>
</if-compare>
<!-- check if orderPaymentPreference with payment already exist, if yes do not re-create -->
<entity-condition entity-name="OrderPaymentPrefAndPayment" list="orderPaymentPrefAndPayments">
<condition-list combine="and">
<condition-expr field-name="orderId" operator="equals" from-field="orderHeader.orderId"/>
<condition-expr field-name="statusId" operator="not-equals" value="PAYMENT_CANCELLED"/>
</condition-list>
</entity-condition>
<if-not-empty field="orderPaymentPrefAndPayments">
<log level="info" message="Payment not created for order ${orderHeader.orderId}, at least a single payment already exists"/>
<return/>
</if-not-empty>
<entity-and list="orderRoleToList" entity-name="OrderRole">
<field-map field-name="orderId" from-field="orderHeader.orderId"/>
<field-map field-name="roleTypeId" value="BILL_FROM_VENDOR"/>
</entity-and>
<first-from-list entry="orderRoleTo" list="orderRoleToList"/>
<entity-and list="orderRoleFromList" entity-name="OrderRole">
<field-map field-name="orderId" from-field="orderHeader.orderId"/>
<field-map field-name="roleTypeId" value="BILL_TO_CUSTOMER"/>
</entity-and>
<first-from-list entry="orderRoleFrom" list="orderRoleFromList"/>
<if-compare operator="equals" value="PURCHASE_ORDER" field="orderHeader.orderTypeId">
<entity-and list="agreementList" entity-name="Agreement" filter-by-date="true">
<field-map field-name="partyIdFrom" from-field="orderRoleFrom.partyId"/>
<field-map field-name="partyIdTo" from-field="orderRoleTo.partyId"/>
<field-map field-name="agreementTypeId" value="PURCHASE_AGREEMENT"/>
</entity-and>
<set field="parameters.paymentTypeId" value="VENDOR_PAYMENT"/>
<set field="organizationPartyId" from-field="orderRoleFrom.partyId"/>
<else>
<entity-and list="agreementList" entity-name="Agreement" filter-by-date="true">
<field-map field-name="partyIdFrom" from-field="orderRoleFrom.partyId"/>
<field-map field-name="partyIdTo" from-field="orderRoleTo.partyId"/>
<field-map field-name="agreementTypeId" value="SALES_AGREEMENT"/>
</entity-and>
<set field="parameters.paymentTypeId" value="CUSTOMER_PAYMENT"/>
<set field="organizationPartyId" from-field="orderRoleTo.partyId"/>
</else>
</if-compare>
<first-from-list entry="agreement" list="agreementList"/>
<if-not-empty field="agreement">
<entity-and list="orderTermList" entity-name="OrderTerm">
<field-map field-name="orderId" from-field="orderHeader.orderId"/>
<field-map field-name="termTypeId" value="FIN_PAYMENT_TERM"/>
</entity-and>
<first-from-list entry="orderTerm" list="orderTermList"/>
<if-not-empty field="orderTerm.termDays">
<set field="days" from-field="orderTerm.termDays" type="Integer"/>
<now-timestamp field="start"/>
<call-class-method class-name="org.ofbiz.base.util.UtilDateTime" method-name="addDaysToTimestamp" ret-field="parameters.effectiveDate">
<field field="start" type="java.sql.Timestamp"/>
<field field="days" type="int"/>
</call-class-method>
</if-not-empty>
</if-not-empty>
<if-empty field="parameters.effectiveDate">
<now-timestamp field="parameters.effectiveDate"/>
</if-empty>
<!-- check currency and when required use invoice currency rate or convert when invoice not available -->
<set field="partyAccountingPreferencesMap.organizationPartyId" from-field="organizationPartyId"/>
<call-service service-name="getPartyAccountingPreferences" in-map-name="partyAccountingPreferencesMap">
<result-to-field result-name="partyAccountingPreference" field="partyAcctgPreference"/>
</call-service>
<if>
<condition>
<and>
<not>
<if-empty field="partyAcctgPreference.baseCurrencyUomId" />
</not>
<if-compare-field to-field="partyAcctgPreference.baseCurrencyUomId" operator="equals" field="orderHeader.currencyUom" />
</and>
</condition>
<then>
<set field="parameters.currencyUomId" from-field="orderHeader.currencyUom" />
<set field="parameters.amount" from-field="orderHeader.grandTotal" />
<!-- get conversion rate from related invoice when exists -->
<entity-and list="invoices" entity-name="OrderItemBillingAndInvoiceAndItem">
<field-map field-name="orderId" from-field="orderId" />
</entity-and>
<if-not-empty field="invoices">
<entity-one entity-name="Invoice" value-field="invoice">
<field-map field-name="invoiceId" from-field="invoices[0].invoiceId" />
</entity-one>
<set field="convertUomInMap.asOfDate" from-field="invoice.invoiceDate" />
</if-not-empty>
<set field="convertUomInMap.originalValue" from-field="orderHeader.grandTotal" />
<set field="convertUomInMap.uomId" from-field="orderHeader.currencyUom" />
<set field="convertUomInMap.uomIdTo" from-field="partyAcctgPreference.baseCurrencyUomId" />
<log message="convertUomInMap = ${convertUomInMap}" level="info" />
<call-service service-name="convertUom" in-map-name="convertUomInMap">
<result-to-field result-name="convertedValue" field="parameters.amount" />
</call-service>
<set field="parameters.actualCurrencyAmount" from-field="orderHeader.grandTotal" />
<set field="parameters.actualCurrencyUomId" from-field="orderHeader.currencyUom" />
<set field="parameters.currencyUomId" from-field="partyAcctgPreference.baseCurrencyUomId" />
</then>
<else>
<set field="parameters.currencyUomId" from-field="orderHeader.currencyUom" />
<set field="parameters.amount" from-field="orderHeader.grandTotal" />
</else>
</if>
<set field="parameters.partyIdFrom" from-field="orderRoleFrom.partyId"/>
<set field="parameters.partyIdTo" from-field="orderRoleTo.partyId"/>
<set field="parameters.paymentMethodTypeId" value="COMPANY_ACCOUNT"/>
<set field="parameters.statusId" value="PMNT_NOT_PAID"/>
<set-service-fields service-name="createPayment" to-map="createPayment" map="parameters"/>
<call-service service-name="createPayment" in-map-name="createPayment">
<result-to-field result-name="paymentId" field="parameters.paymentId"/>
</call-service>
<set field="parameters.orderId" from-field="orderHeader.orderId"/>
<set field="parameters.maxAmount" from-field="orderHeader.grandTotal"/>
<set-service-fields service-name="createOrderPaymentPreference" to-map="newOrderPaymentPreference" map="parameters"/>
<call-service service-name="createOrderPaymentPreference" in-map-name="newOrderPaymentPreference">
<result-to-field result-name="orderPaymentPreferenceId" field="parameters.paymentPreferenceId"/>
</call-service>
<set-service-fields service-name="updatePayment" to-map="updatePayment" map="parameters"/>
<call-service service-name="updatePayment" in-map-name="updatePayment"/>
<field-to-result field="parameters.paymentId" result-name="paymentId"/>
<log level="info" message="payment ${parameters.paymentId} with the not-paid status automatically created from order: ${parameters.orderId} (can be disabled in AccountingConfig.properties)"/>
</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="AccountingConfig" 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 value-field="invoice" entity-name="Invoice"/>
<if-not-empty field="invoice">
<call-class-method method-name="getInvoiceTotal" class-name="org.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 list="payments" entity-name="Payment">
<condition-list combine="and">
<condition-expr field-name="statusId" value="PMNT_CONFIRMED" operator="not-equals"/>
<condition-expr field-name="partyIdFrom" from-field="invoice.partyId" operator="equals"/>
<condition-expr field-name="partyIdTo" from-field="invoice.partyIdFrom" operator="equals"/>
<condition-expr field-name="actualCurrencyAmount" from-field="invoiceTotal" operator="equals"/>
<condition-expr field-name="actualCurrencyUomId" from-field="invoice.currencyUomId" operator="equals"/>
</condition-list>
<order-by field-name="effectiveDate"/>
</entity-condition>
<else>
<entity-condition list="payments" entity-name="Payment">
<condition-list combine="and">
<condition-expr field-name="statusId" value="PMNT_CONFIRMED" operator="not-equals"/>
<condition-expr field-name="partyIdFrom" from-field="invoice.partyId" operator="equals"/>
<condition-expr field-name="partyIdTo" from-field="invoice.partyIdFrom" operator="equals"/>
<condition-expr field-name="amount" from-field="invoiceTotal" operator="equals"/>
<condition-expr field-name="currencyUomId" from-field="invoice.currencyUomId" operator="equals"/>
</condition-list>
<order-by field-name="effectiveDate"/>
</entity-condition>
</else>
</if-compare>
<if-not-empty field="payments">
<!-- check if already applied -->
<entity-and list="paymentAppls" entity-name="PaymentApplication">
<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 value-field="payment" entity-name="Payment"/>
<if-not-empty field="payment">
<entity-condition list="invoices" entity-name="Invoice">
<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 entry="invoice" list="invoices">
<call-class-method method-name="getInvoiceTotal" class-name="org.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 operator="equals" field="invoiceTotal" to-field="payment.actualCurrencyAmount">
<if-compare-field operator="equals" field="invoice.currencyUomId" 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 operator="equals" field="invoiceTotal" to-field="payment.amount">
<if-compare-field operator="equals" field="invoice.currencyUomId" 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>
</iterate>
<if-not-empty field="invoiceId">
<entity-and list="paymentAppls" entity-name="PaymentApplication">
<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 AccountingConfig.properties)"/>
</if-not-empty>
</if-not-empty>
</simple-method>
<!-- PaymentContent -->
<simple-method method-name="createPaymentContent" short-description="Create Content For Payment">
<make-value value-field="newEntity" entity-name="PaymentContent"/>
<set-pk-fields map="parameters" value-field="newEntity"/>
<set-nonpk-fields map="parameters" value-field="newEntity"/>
<if-empty field="newEntity.fromDate">
<now-timestamp field="nowTimestamp"/>
<set field="newEntity.fromDate" from-field="nowTimestamp"/>
</if-empty>
<create-value value-field="newEntity"/>
<set-service-fields service-name="updateContent" map="parameters" to-map="updateContent"/>
<call-service service-name="updateContent" in-map-name="updateContent"/>
<field-to-result field="newEntity.contentId" result-name="contentId"/>
<field-to-result field="newEntity.paymentId" result-name="paymentId"/>
<field-to-result field="newEntity.paymentContentTypeId" result-name="paymentContentTypeId"/>
</simple-method>
<simple-method method-name="updatePaymentContent" short-description="Update Content For Payment">
<make-value value-field="lookupPKMap" entity-name="PaymentContent"/>
<set-pk-fields map="parameters" value-field="lookupPKMap"/>
<find-by-primary-key map="lookupPKMap" value-field="lookedUpValue"/>
<set-nonpk-fields map="parameters" value-field="lookedUpValue"/>
<store-value value-field="lookedUpValue"/>
<set-service-fields service-name="updateContent" map="parameters" to-map="updateContent"/>
<call-service service-name="updateContent" in-map-name="updateContent"/>
</simple-method>
<simple-method method-name="removePaymentContent" short-description="Remove Content From Payment">
<make-value value-field="lookupPKMap" entity-name="PaymentContent"/>
<set-pk-fields map="parameters" value-field="lookupPKMap"/>
<find-by-primary-key map="lookupPKMap" value-field="lookedUpValue"/>
<remove-value value-field="lookedUpValue"/>
</simple-method>
</simple-methods>