Rounding is a constant recurring issue for my clients.
What I normally do is to just create a document validator that
rounds based on specific rules (such as round to zero decimals on
all SEK sales invoices).
Sometimes I mark sales pricelists as "use rounding" and sometimes I
just hard code as in below example (ROUNDING_CHARGE is defined
outside the code example):
@Override
public String docValidate(PO po, int timing) {
if (po instanceof X_C_Invoice) {
if (timing==ModelValidator.TIMING_BEFORE_COMPLETE) {
MInvoice invoice = (MInvoice)po;
// Never round non-sales invoices
if (!invoice.isSOTrx())
return null;
if
(!"SEK".equals(invoice.getC_Currency().getISO_Code())) {
return null;
}
double grandTotal =
invoice.getGrandTotal().doubleValue();
double rounded = Math.round(grandTotal);
if (rounded!=grandTotal) {
double roundWith = rounded - grandTotal;
MInvoiceLine il = new
MInvoiceLine(invoice.getCtx(), 0, invoice.get_TrxName());
il.setC_Invoice_ID(invoice.get_ID());
il.setQty(1);
il.setC_Charge_ID(ROUNDING_CHARGE);
roundWith = Math.round(roundWith * 100) / 100.0;
il.setPrice(BigDecimal.valueOf(roundWith));
il.saveEx();
invoice.setGrandTotal(BigDecimal.valueOf(rounded));
}
}
}
return null;
}
A good standardized concept for this
would be great to have out of the box in iDempiere.
/Daniel