/** * @param customerInvoiceDetail * @param paymentApplicationDocument * @return */ public static boolean validateAmountAppliedToCustomerInvoiceDetailByPaymentApplicationDocument( CustomerInvoiceDetail customerInvoiceDetail, PaymentApplicationDocument paymentApplicationDocument, KualiDecimal totalFromControl) throws WorkflowException { boolean isValid = true; // This let's us highlight a specific invoice detail line String propertyName = MessageFormat.format( ArPropertyConstants.PaymentApplicationDocumentFields.AMOUNT_TO_BE_APPLIED_LINE_N, customerInvoiceDetail.getSequenceNumber().toString()); KualiDecimal amountAppliedByAllOtherDocuments = customerInvoiceDetail.getAmountAppliedExcludingAnyAmountAppliedBy( paymentApplicationDocument.getDocumentNumber()); KualiDecimal amountAppliedByThisDocument = customerInvoiceDetail.getAmountAppliedBy(paymentApplicationDocument.getDocumentNumber()); KualiDecimal totalAppliedAmount = amountAppliedByAllOtherDocuments.add(amountAppliedByThisDocument); // Can't apply more than the total amount of the detail if (!totalAppliedAmount.isLessEqual(totalFromControl)) { isValid = false; GlobalVariables.getMessageMap() .putError( propertyName, ArKeyConstants.PaymentApplicationDocumentErrors .AMOUNT_TO_BE_APPLIED_EXCEEDS_AMOUNT_OUTSTANDING); } // Can't apply a negative amount. if (KualiDecimal.ZERO.isGreaterThan(amountAppliedByThisDocument)) { isValid = false; GlobalVariables.getMessageMap() .putError( propertyName, ArKeyConstants.PaymentApplicationDocumentErrors .AMOUNT_TO_BE_APPLIED_MUST_BE_GREATER_THAN_ZERO); } // Can't apply more than the total amount outstanding on the cash control document. CashControlDocument cashControlDocument = paymentApplicationDocument.getCashControlDocument(); if (ObjectUtils.isNotNull(cashControlDocument)) { if (cashControlDocument.getCashControlTotalAmount().isLessThan(amountAppliedByThisDocument)) { isValid = false; GlobalVariables.getMessageMap() .putError( propertyName, ArKeyConstants.PaymentApplicationDocumentErrors .CANNOT_APPLY_MORE_THAN_BALANCE_TO_BE_APPLIED); } } return isValid; }