Search in sources :

Example 1 with PaymentToLink

use of de.metas.banking.payment.BankStatementLineMultiPaymentLinkRequest.PaymentToLink in project metasfresh-webui-api by metasfresh.

the class ReconcilePaymentsCommand method computeBankStatementLineReconcileRequest.

private ExplainedOptional<BankStatementLineMultiPaymentLinkRequest> computeBankStatementLineReconcileRequest() {
    final BankStatementLineRow bankStatementLineRow = request.getSelectedBankStatementLine();
    if (bankStatementLineRow == null) {
        return ExplainedOptional.emptyBecause("no bank statement line selected");
    }
    if (bankStatementLineRow.isReconciled()) {
        return ExplainedOptional.emptyBecause("bank statement line was already reconciled");
    }
    final List<PaymentToReconcileRow> paymentRows = request.getSelectedPaymentsToReconcile();
    if (paymentRows.isEmpty()) {
        return ExplainedOptional.emptyBecause("no payment rows selected");
    }
    final Amount statementLineAmt = bankStatementLineRow.getStatementLineAmt();
    final CurrencyCode currencyCode = statementLineAmt.getCurrencyCode();
    Amount statementLineAmtReconciled = Amount.zero(currencyCode);
    final ArrayList<PaymentToLink> paymentsToReconcile = new ArrayList<>();
    for (final PaymentToReconcileRow paymentRow : paymentRows) {
        if (paymentRow.isReconciled()) {
            return ExplainedOptional.emptyBecause("Payment `" + paymentRow.getDocumentNo() + "` was already reconciled");
        }
        final Amount payAmt = paymentRow.getPayAmtNegateIfOutbound();
        if (!payAmt.getCurrencyCode().equals(currencyCode)) {
            return ExplainedOptional.emptyBecause("Payment `" + paymentRow.getDocumentNo() + "` shall be in `" + currencyCode + "` instead of `" + payAmt.getCurrencyCode() + "`");
        }
        statementLineAmtReconciled = statementLineAmtReconciled.add(payAmt);
        paymentsToReconcile.add(PaymentToLink.builder().paymentId(paymentRow.getPaymentId()).statementLineAmt(payAmt).build());
    }
    final Amount statementLineAmtToReconcile = statementLineAmt.subtract(statementLineAmtReconciled);
    if (!statementLineAmtToReconcile.isZero()) {
        return ExplainedOptional.emptyBecause(msgBL.getTranslatableMsgText(MSG_StatementLineAmtToReconcileIs, statementLineAmtToReconcile));
    }
    return ExplainedOptional.of(BankStatementLineMultiPaymentLinkRequest.builder().bankStatementLineId(bankStatementLineRow.getBankStatementLineId()).paymentsToLink(paymentsToReconcile).build());
}
Also used : CurrencyCode(de.metas.currency.CurrencyCode) PaymentToLink(de.metas.banking.payment.BankStatementLineMultiPaymentLinkRequest.PaymentToLink) Amount(de.metas.currency.Amount) ArrayList(java.util.ArrayList) BankStatementLineRow(de.metas.ui.web.bankstatement_reconciliation.BankStatementLineRow) PaymentToReconcileRow(de.metas.ui.web.bankstatement_reconciliation.PaymentToReconcileRow)

Aggregations

PaymentToLink (de.metas.banking.payment.BankStatementLineMultiPaymentLinkRequest.PaymentToLink)1 Amount (de.metas.currency.Amount)1 CurrencyCode (de.metas.currency.CurrencyCode)1 BankStatementLineRow (de.metas.ui.web.bankstatement_reconciliation.BankStatementLineRow)1 PaymentToReconcileRow (de.metas.ui.web.bankstatement_reconciliation.PaymentToReconcileRow)1 ArrayList (java.util.ArrayList)1