Search in sources :

Example 1 with TaxDetail

use of org.broadleafcommerce.core.order.domain.TaxDetail in project BroadleafCommerce by BroadleafCommerce.

the class TotalActivity method setTaxSums.

protected void setTaxSums(Order order) {
    if (order.getTaxOverride()) {
        Money zeroMoney = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
        for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
            if (fg.getTaxes() != null) {
                fg.getTaxes().clear();
            }
            fg.setTotalTax(zeroMoney);
            for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
                if (fgi.getTaxes() != null) {
                    fgi.getTaxes().clear();
                }
                fgi.setTotalTax(zeroMoney);
            }
            for (FulfillmentGroupFee fee : fg.getFulfillmentGroupFees()) {
                if (fee.getTaxes() != null) {
                    fee.getTaxes().clear();
                }
                fee.setTotalTax(zeroMoney);
            }
            fg.setTotalFulfillmentGroupTax(zeroMoney);
            fg.setTotalItemTax(zeroMoney);
            fg.setTotalFeeTax(zeroMoney);
        }
        order.setTotalTax(zeroMoney);
        return;
    }
    Money orderTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
    for (FulfillmentGroup fg : order.getFulfillmentGroups()) {
        Money fgTotalFgTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
        Money fgTotalItemTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
        Money fgTotalFeeTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
        // Add in all FG specific taxes (such as shipping tax)
        if (fg.getTaxes() != null) {
            for (TaxDetail tax : fg.getTaxes()) {
                fgTotalFgTax = fgTotalFgTax.add(tax.getAmount());
            }
        }
        for (FulfillmentGroupItem item : fg.getFulfillmentGroupItems()) {
            Money itemTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
            // Add in all taxes for this item
            if (item.getTaxes() != null) {
                for (TaxDetail tax : item.getTaxes()) {
                    itemTotalTax = itemTotalTax.add(tax.getAmount());
                }
            }
            item.setTotalTax(itemTotalTax);
            fgTotalItemTax = fgTotalItemTax.add(itemTotalTax);
        }
        for (FulfillmentGroupFee fee : fg.getFulfillmentGroupFees()) {
            Money feeTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency());
            // Add in all taxes for this fee
            if (fee.getTaxes() != null) {
                for (TaxDetail tax : fee.getTaxes()) {
                    feeTotalTax = feeTotalTax.add(tax.getAmount());
                }
            }
            fee.setTotalTax(feeTotalTax);
            fgTotalFeeTax = fgTotalFeeTax.add(feeTotalTax);
        }
        Money fgTotalTax = BroadleafCurrencyUtils.getMoney(BigDecimal.ZERO, order.getCurrency()).add(fgTotalFgTax).add(fgTotalItemTax).add(fgTotalFeeTax);
        // Set the fulfillment group tax sums
        fg.setTotalFulfillmentGroupTax(fgTotalFgTax);
        fg.setTotalItemTax(fgTotalItemTax);
        fg.setTotalFeeTax(fgTotalFeeTax);
        fg.setTotalTax(fgTotalTax);
        orderTotalTax = orderTotalTax.add(fgTotalTax);
    }
    order.setTotalTax(orderTotalTax);
}
Also used : Money(org.broadleafcommerce.common.money.Money) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) FulfillmentGroupFee(org.broadleafcommerce.core.order.domain.FulfillmentGroupFee) TaxDetail(org.broadleafcommerce.core.order.domain.TaxDetail)

Example 2 with TaxDetail

use of org.broadleafcommerce.core.order.domain.TaxDetail in project BroadleafCommerce by BroadleafCommerce.

the class SimpleTaxProvider method calculateTaxForOrder.

@Override
public Order calculateTaxForOrder(Order order, ModuleConfiguration config) throws TaxException {
    if (!order.getCustomer().isTaxExempt()) {
        for (FulfillmentGroup fulfillmentGroup : order.getFulfillmentGroups()) {
            // Set taxes on the fulfillment group items
            for (FulfillmentGroupItem fgItem : fulfillmentGroup.getFulfillmentGroupItems()) {
                if (isItemTaxable(fgItem)) {
                    BigDecimal factor = determineItemTaxRate(fulfillmentGroup.getAddress());
                    if (factor != null && factor.compareTo(BigDecimal.ZERO) != 0) {
                        TaxDetail tax;
                        checkDetail: {
                            for (TaxDetail detail : fgItem.getTaxes()) {
                                if (detail.getType().equals(TaxType.COMBINED)) {
                                    tax = detail;
                                    break checkDetail;
                                }
                            }
                            tax = entityConfig.createEntityInstance(TaxDetail.class.getName(), TaxDetail.class);
                            tax.setType(TaxType.COMBINED);
                            fgItem.getTaxes().add(tax);
                        }
                        tax.setRate(factor);
                        tax.setAmount(fgItem.getTotalItemTaxableAmount().multiply(factor));
                    }
                }
            }
            for (FulfillmentGroupFee fgFee : fulfillmentGroup.getFulfillmentGroupFees()) {
                if (isFeeTaxable(fgFee)) {
                    BigDecimal factor = determineItemTaxRate(fulfillmentGroup.getAddress());
                    if (factor != null && factor.compareTo(BigDecimal.ZERO) != 0) {
                        TaxDetail tax;
                        checkDetail: {
                            for (TaxDetail detail : fgFee.getTaxes()) {
                                if (detail.getType().equals(TaxType.COMBINED)) {
                                    tax = detail;
                                    break checkDetail;
                                }
                            }
                            tax = entityConfig.createEntityInstance(TaxDetail.class.getName(), TaxDetail.class);
                            tax.setType(TaxType.COMBINED);
                            fgFee.getTaxes().add(tax);
                        }
                        tax.setRate(factor);
                        tax.setAmount(fgFee.getAmount().multiply(factor));
                    }
                }
            }
            BigDecimal factor = determineTaxRateForFulfillmentGroup(fulfillmentGroup);
            if (factor != null && factor.compareTo(BigDecimal.ZERO) != 0) {
                TaxDetail tax;
                checkDetail: {
                    for (TaxDetail detail : fulfillmentGroup.getTaxes()) {
                        if (detail.getType().equals(TaxType.COMBINED)) {
                            tax = detail;
                            break checkDetail;
                        }
                    }
                    tax = entityConfig.createEntityInstance(TaxDetail.class.getName(), TaxDetail.class);
                    tax.setType(TaxType.COMBINED);
                    fulfillmentGroup.getTaxes().add(tax);
                }
                tax.setRate(factor);
                tax.setAmount(fulfillmentGroup.getFulfillmentPrice().multiply(factor));
            }
        }
    }
    return order;
}
Also used : FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) TaxDetail(org.broadleafcommerce.core.order.domain.TaxDetail) FulfillmentGroupFee(org.broadleafcommerce.core.order.domain.FulfillmentGroupFee) BigDecimal(java.math.BigDecimal)

Aggregations

FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)2 FulfillmentGroupFee (org.broadleafcommerce.core.order.domain.FulfillmentGroupFee)2 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)2 TaxDetail (org.broadleafcommerce.core.order.domain.TaxDetail)2 BigDecimal (java.math.BigDecimal)1 Money (org.broadleafcommerce.common.money.Money)1