use of org.compiere.model.MInvoicePaySchedule in project adempiere by adempiere.
the class InvoicePayScheduleValidate method doIt.
// prepare
/**
* Perform process.
* @return Message (clear text)
* @throws Exception if not successful
*/
protected String doIt() throws Exception {
log.info("C_InvoicePaySchedule_ID=" + getRecord_ID());
MInvoicePaySchedule[] schedule = MInvoicePaySchedule.getInvoicePaySchedule(getCtx(), 0, getRecord_ID(), null);
if (schedule.length == 0)
throw new IllegalArgumentException("InvoicePayScheduleValidate - No Schedule");
// Get Invoice
MInvoice invoice = new MInvoice(getCtx(), schedule[0].getC_Invoice_ID(), null);
if (invoice.get_ID() == 0)
throw new IllegalArgumentException("InvoicePayScheduleValidate - No Invoice");
//
BigDecimal total = Env.ZERO;
for (int i = 0; i < schedule.length; i++) {
BigDecimal due = schedule[i].getDueAmt();
if (due != null)
total = total.add(due);
}
boolean valid = invoice.getGrandTotal().compareTo(total) == 0;
invoice.setIsPayScheduleValid(valid);
invoice.saveEx();
// Schedule
for (int i = 0; i < schedule.length; i++) {
if (schedule[i].isValid() != valid) {
schedule[i].setIsValid(valid);
schedule[i].saveEx();
}
}
String msg = "@OK@";
if (!valid)
msg = "@GrandTotal@ = " + invoice.getGrandTotal() + " <> @Total@ = " + total + " - @Difference@ = " + invoice.getGrandTotal().subtract(total);
return Msg.parseTranslation(getCtx(), msg);
}
Aggregations