Example usage for java.math RoundingMode FLOOR

List of usage examples for java.math RoundingMode FLOOR

Introduction

In this page you can find the example usage for java.math RoundingMode FLOOR.

Prototype

RoundingMode FLOOR

To view the source code for java.math RoundingMode FLOOR.

Click Source Link

Document

Rounding mode to round towards negative infinity.

Usage

From source file:org.yes.cart.payment.impl.PostFinancePaymentGatewayImpl.java

private void populateItems(final Payment payment, final Map<String, String> params) {

    BigDecimal totalItemsGross = Total.ZERO;

    for (final PaymentLine item : payment.getOrderItems()) {
        totalItemsGross = totalItemsGross.add(item.getQuantity().multiply(item.getUnitPrice()));
    }/* w  w w .j  a  v  a2s .  c  om*/

    final int it = payment.getOrderItems().size();

    BigDecimal orderDiscountRemainder = Total.ZERO;
    BigDecimal orderDiscountPercent = Total.ZERO;
    final BigDecimal payGross = payment.getPaymentAmount();
    if (payGross.compareTo(totalItemsGross) < 0) {
        orderDiscountRemainder = totalItemsGross.subtract(payGross);
        orderDiscountPercent = orderDiscountRemainder.divide(totalItemsGross, 10, RoundingMode.HALF_UP);
    }

    int i = 1;
    boolean hasOrderDiscount = MoneyUtils.isFirstBiggerThanSecond(orderDiscountRemainder, Total.ZERO);
    for (final PaymentLine item : payment.getOrderItems()) {

        final BigDecimal itemGrossAmount = item.getUnitPrice().multiply(item.getQuantity())
                .setScale(Total.ZERO.scale(), RoundingMode.HALF_UP);
        params.put("ITEMID" + i,
                item.getSkuCode().length() > ITEMID ? item.getSkuCode().substring(0, ITEMID - 1) + "~"
                        : item.getSkuCode());
        params.put("ITEMNAME" + i,
                item.getSkuName().length() > ITEMNAME ? item.getSkuName().substring(0, ITEMNAME - 1) + "~"
                        : item.getSkuName());
        params.put("ITEMQUANT" + i, item.getQuantity().toPlainString());
        if (hasOrderDiscount && MoneyUtils.isFirstBiggerThanSecond(orderDiscountRemainder, Total.ZERO)
                && MoneyUtils.isFirstBiggerThanSecond(itemGrossAmount, Total.ZERO)) {
            BigDecimal discount;
            if (i == it) {
                // last item
                discount = orderDiscountRemainder;
            } else {
                BigDecimal itemDiscount = itemGrossAmount.multiply(orderDiscountPercent)
                        .setScale(Total.ZERO.scale(), RoundingMode.CEILING);
                if (MoneyUtils.isFirstBiggerThanSecond(orderDiscountRemainder, itemDiscount)) {
                    discount = itemDiscount;
                    orderDiscountRemainder = orderDiscountRemainder.subtract(itemDiscount);
                } else {
                    discount = orderDiscountRemainder;
                    orderDiscountRemainder = Total.ZERO;
                }

            }
            final BigDecimal scaleRate = discount.divide(itemGrossAmount.subtract(discount), 10,
                    RoundingMode.CEILING);
            final BigDecimal scaledTax = item.getTaxAmount().multiply(scaleRate).setScale(Total.ZERO.scale(),
                    RoundingMode.FLOOR);
            params.put("ITEMDISCOUNT" + i, discount.toPlainString());
            params.put("ITEMPRICE" + i, itemGrossAmount.subtract(discount).subtract(item.getTaxAmount())
                    .add(scaledTax).toPlainString());
            params.put("ITEMVAT" + i, item.getTaxAmount().subtract(scaledTax).toPlainString());
        } else {
            params.put("ITEMPRICE" + i, itemGrossAmount.subtract(item.getTaxAmount()).toPlainString());
            params.put("ITEMVAT" + i, item.getTaxAmount().toPlainString());
        }
        i++;
    }
}