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());
}
Aggregations