Search in sources :

Example 1 with TaxItem

use of com.salesmanager.core.model.tax.TaxItem in project shopizer by shopizer-ecommerce.

the class OrderServiceImpl method caculateOrder.

private OrderTotalSummary caculateOrder(OrderSummary summary, Customer customer, final MerchantStore store, final Language language) throws Exception {
    OrderTotalSummary totalSummary = new OrderTotalSummary();
    List<OrderTotal> orderTotals = new ArrayList<OrderTotal>();
    Map<String, OrderTotal> otherPricesTotals = new HashMap<String, OrderTotal>();
    ShippingConfiguration shippingConfiguration = null;
    BigDecimal grandTotal = new BigDecimal(0);
    grandTotal.setScale(2, RoundingMode.HALF_UP);
    // price by item
    /**
     * qty * price
     * subtotal
     */
    BigDecimal subTotal = new BigDecimal(0);
    subTotal.setScale(2, RoundingMode.HALF_UP);
    for (ShoppingCartItem item : summary.getProducts()) {
        BigDecimal st = item.getItemPrice().multiply(new BigDecimal(item.getQuantity()));
        item.setSubTotal(st);
        subTotal = subTotal.add(st);
        // Other prices
        FinalPrice finalPrice = item.getFinalPrice();
        if (finalPrice != null) {
            List<FinalPrice> otherPrices = finalPrice.getAdditionalPrices();
            if (otherPrices != null) {
                for (FinalPrice price : otherPrices) {
                    if (!price.isDefaultPrice()) {
                        OrderTotal itemSubTotal = otherPricesTotals.get(price.getProductPrice().getCode());
                        if (itemSubTotal == null) {
                            itemSubTotal = new OrderTotal();
                            itemSubTotal.setModule(Constants.OT_ITEM_PRICE_MODULE_CODE);
                            itemSubTotal.setTitle(Constants.OT_ITEM_PRICE_MODULE_CODE);
                            itemSubTotal.setOrderTotalCode(price.getProductPrice().getCode());
                            itemSubTotal.setOrderTotalType(OrderTotalType.PRODUCT);
                            itemSubTotal.setSortOrder(0);
                            otherPricesTotals.put(price.getProductPrice().getCode(), itemSubTotal);
                        }
                        BigDecimal orderTotalValue = itemSubTotal.getValue();
                        if (orderTotalValue == null) {
                            orderTotalValue = new BigDecimal(0);
                            orderTotalValue.setScale(2, RoundingMode.HALF_UP);
                        }
                        orderTotalValue = orderTotalValue.add(price.getFinalPrice());
                        itemSubTotal.setValue(orderTotalValue);
                        if (price.getProductPrice().getProductPriceType().name().equals(OrderValueType.ONE_TIME)) {
                            subTotal = subTotal.add(price.getFinalPrice());
                        }
                    }
                }
            }
        }
    }
    // only in order page, otherwise invokes too many processing
    if (OrderSummaryType.ORDERTOTAL.name().equals(summary.getOrderSummaryType().name()) || OrderSummaryType.SHOPPINGCART.name().equals(summary.getOrderSummaryType().name())) {
        // Post processing order total variation modules for sub total calculation - drools, custom modules
        // may affect the sub total
        OrderTotalVariation orderTotalVariation = orderTotalService.findOrderTotalVariation(summary, customer, store, language);
        int currentCount = 10;
        if (CollectionUtils.isNotEmpty(orderTotalVariation.getVariations())) {
            for (OrderTotal variation : orderTotalVariation.getVariations()) {
                variation.setSortOrder(currentCount++);
                orderTotals.add(variation);
                subTotal = subTotal.subtract(variation.getValue());
            }
        }
    }
    totalSummary.setSubTotal(subTotal);
    grandTotal = grandTotal.add(subTotal);
    OrderTotal orderTotalSubTotal = new OrderTotal();
    orderTotalSubTotal.setModule(Constants.OT_SUBTOTAL_MODULE_CODE);
    orderTotalSubTotal.setOrderTotalType(OrderTotalType.SUBTOTAL);
    orderTotalSubTotal.setOrderTotalCode("order.total.subtotal");
    orderTotalSubTotal.setTitle(Constants.OT_SUBTOTAL_MODULE_CODE);
    orderTotalSubTotal.setSortOrder(5);
    orderTotalSubTotal.setValue(subTotal);
    orderTotals.add(orderTotalSubTotal);
    // shipping
    if (summary.getShippingSummary() != null) {
        OrderTotal shippingSubTotal = new OrderTotal();
        shippingSubTotal.setModule(Constants.OT_SHIPPING_MODULE_CODE);
        shippingSubTotal.setOrderTotalType(OrderTotalType.SHIPPING);
        shippingSubTotal.setOrderTotalCode("order.total.shipping");
        shippingSubTotal.setTitle(Constants.OT_SHIPPING_MODULE_CODE);
        shippingSubTotal.setSortOrder(100);
        orderTotals.add(shippingSubTotal);
        if (!summary.getShippingSummary().isFreeShipping()) {
            shippingSubTotal.setValue(summary.getShippingSummary().getShipping());
            grandTotal = grandTotal.add(summary.getShippingSummary().getShipping());
        } else {
            shippingSubTotal.setValue(new BigDecimal(0));
            grandTotal = grandTotal.add(new BigDecimal(0));
        }
        // check handling fees
        shippingConfiguration = shippingService.getShippingConfiguration(store);
        if (summary.getShippingSummary().getHandling() != null && summary.getShippingSummary().getHandling().doubleValue() > 0) {
            if (shippingConfiguration.getHandlingFees() != null && shippingConfiguration.getHandlingFees().doubleValue() > 0) {
                OrderTotal handlingubTotal = new OrderTotal();
                handlingubTotal.setModule(Constants.OT_HANDLING_MODULE_CODE);
                handlingubTotal.setOrderTotalType(OrderTotalType.HANDLING);
                handlingubTotal.setOrderTotalCode("order.total.handling");
                handlingubTotal.setTitle(Constants.OT_HANDLING_MODULE_CODE);
                // handlingubTotal.setText("order.total.handling");
                handlingubTotal.setSortOrder(120);
                handlingubTotal.setValue(summary.getShippingSummary().getHandling());
                orderTotals.add(handlingubTotal);
                grandTotal = grandTotal.add(summary.getShippingSummary().getHandling());
            }
        }
    }
    // tax
    List<TaxItem> taxes = taxService.calculateTax(summary, customer, store, language);
    if (taxes != null && taxes.size() > 0) {
        BigDecimal totalTaxes = new BigDecimal(0);
        totalTaxes.setScale(2, RoundingMode.HALF_UP);
        int taxCount = 200;
        for (TaxItem tax : taxes) {
            OrderTotal taxLine = new OrderTotal();
            taxLine.setModule(Constants.OT_TAX_MODULE_CODE);
            taxLine.setOrderTotalType(OrderTotalType.TAX);
            taxLine.setOrderTotalCode(tax.getLabel());
            taxLine.setSortOrder(taxCount);
            taxLine.setTitle(Constants.OT_TAX_MODULE_CODE);
            taxLine.setText(tax.getLabel());
            taxLine.setValue(tax.getItemPrice());
            totalTaxes = totalTaxes.add(tax.getItemPrice());
            orderTotals.add(taxLine);
            // grandTotal=grandTotal.add(tax.getItemPrice());
            taxCount++;
        }
        grandTotal = grandTotal.add(totalTaxes);
        totalSummary.setTaxTotal(totalTaxes);
    }
    // grand total
    OrderTotal orderTotal = new OrderTotal();
    orderTotal.setModule(Constants.OT_TOTAL_MODULE_CODE);
    orderTotal.setOrderTotalType(OrderTotalType.TOTAL);
    orderTotal.setOrderTotalCode("order.total.total");
    orderTotal.setTitle(Constants.OT_TOTAL_MODULE_CODE);
    // orderTotal.setText("order.total.total");
    orderTotal.setSortOrder(500);
    orderTotal.setValue(grandTotal);
    orderTotals.add(orderTotal);
    totalSummary.setTotal(grandTotal);
    totalSummary.setTotals(orderTotals);
    return totalSummary;
}
Also used : HashMap(java.util.HashMap) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) ShippingConfiguration(com.salesmanager.core.model.shipping.ShippingConfiguration) TaxItem(com.salesmanager.core.model.tax.TaxItem) OrderTotalVariation(com.salesmanager.core.model.order.OrderTotalVariation) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) OrderTotal(com.salesmanager.core.model.order.OrderTotal) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice)

Example 2 with TaxItem

use of com.salesmanager.core.model.tax.TaxItem in project shopizer by shopizer-ecommerce.

the class TaxServiceImpl method calculateTax.

@Override
public List<TaxItem> calculateTax(OrderSummary orderSummary, Customer customer, MerchantStore store, Language language) throws ServiceException {
    if (customer == null) {
        return null;
    }
    List<ShoppingCartItem> items = orderSummary.getProducts();
    List<TaxItem> taxLines = new ArrayList<TaxItem>();
    if (items == null) {
        return taxLines;
    }
    // determine tax calculation basis
    TaxConfiguration taxConfiguration = this.getTaxConfiguration(store);
    if (taxConfiguration == null) {
        taxConfiguration = new TaxConfiguration();
        taxConfiguration.setTaxBasisCalculation(TaxBasisCalculation.SHIPPINGADDRESS);
    }
    Country country = customer.getBilling().getCountry();
    Zone zone = customer.getBilling().getZone();
    String stateProvince = customer.getBilling().getState();
    TaxBasisCalculation taxBasisCalculation = taxConfiguration.getTaxBasisCalculation();
    if (taxBasisCalculation.name().equals(TaxBasisCalculation.SHIPPINGADDRESS)) {
        Delivery shipping = customer.getDelivery();
        if (shipping != null) {
            country = shipping.getCountry();
            zone = shipping.getZone();
            stateProvince = shipping.getState();
        }
    } else if (taxBasisCalculation.name().equals(TaxBasisCalculation.BILLINGADDRESS)) {
        Billing billing = customer.getBilling();
        if (billing != null) {
            country = billing.getCountry();
            zone = billing.getZone();
            stateProvince = billing.getState();
        }
    } else if (taxBasisCalculation.name().equals(TaxBasisCalculation.STOREADDRESS)) {
        country = store.getCountry();
        zone = store.getZone();
        stateProvince = store.getStorestateprovince();
    }
    // do not collect tax on other provinces of same country
    if (!taxConfiguration.isCollectTaxIfDifferentProvinceOfStoreCountry()) {
        if ((zone != null && store.getZone() != null) && (zone.getId().longValue() != store.getZone().getId().longValue())) {
            return null;
        }
        if (!StringUtils.isBlank(stateProvince)) {
            if (store.getZone() != null) {
                if (!store.getZone().getName().equals(stateProvince)) {
                    return null;
                }
            } else if (!StringUtils.isBlank(store.getStorestateprovince())) {
                if (!store.getStorestateprovince().equals(stateProvince)) {
                    return null;
                }
            }
        }
    }
    // collect tax in different countries
    if (taxConfiguration.isCollectTaxIfDifferentCountryOfStoreCountry()) {
        // use store country
        country = store.getCountry();
        zone = store.getZone();
        stateProvince = store.getStorestateprovince();
    }
    if (zone == null && StringUtils.isBlank(stateProvince)) {
        return null;
    }
    Map<Long, TaxClass> taxClasses = new HashMap<Long, TaxClass>();
    // put items in a map by tax class id
    Map<Long, BigDecimal> taxClassAmountMap = new HashMap<Long, BigDecimal>();
    for (ShoppingCartItem item : items) {
        BigDecimal itemPrice = item.getItemPrice();
        TaxClass taxClass = item.getProduct().getTaxClass();
        int quantity = item.getQuantity();
        itemPrice = itemPrice.multiply(new BigDecimal(quantity));
        if (taxClass == null) {
            taxClass = taxClassService.getByCode(DEFAULT_TAX_CLASS);
        }
        BigDecimal subTotal = taxClassAmountMap.get(taxClass.getId());
        if (subTotal == null) {
            subTotal = new BigDecimal(0);
            subTotal.setScale(2, RoundingMode.HALF_UP);
        }
        subTotal = subTotal.add(itemPrice);
        taxClassAmountMap.put(taxClass.getId(), subTotal);
        taxClasses.put(taxClass.getId(), taxClass);
    }
    // tax on shipping ?
    // ShippingConfiguration shippingConfiguration = shippingService.getShippingConfiguration(store);
    /**
     * always calculate tax on shipping *
     */
    // if(shippingConfiguration!=null) {
    // if(shippingConfiguration.isTaxOnShipping()){
    // use default tax class for shipping
    TaxClass defaultTaxClass = taxClassService.getByCode(TaxClass.DEFAULT_TAX_CLASS);
    // taxClasses.put(defaultTaxClass.getId(), defaultTaxClass);
    BigDecimal amnt = taxClassAmountMap.get(defaultTaxClass.getId());
    if (amnt == null) {
        amnt = new BigDecimal(0);
        amnt.setScale(2, RoundingMode.HALF_UP);
    }
    ShippingSummary shippingSummary = orderSummary.getShippingSummary();
    if (shippingSummary != null && shippingSummary.getShipping() != null && shippingSummary.getShipping().doubleValue() > 0) {
        amnt = amnt.add(shippingSummary.getShipping());
        if (shippingSummary.getHandling() != null && shippingSummary.getHandling().doubleValue() > 0) {
            amnt = amnt.add(shippingSummary.getHandling());
        }
    }
    taxClassAmountMap.put(defaultTaxClass.getId(), amnt);
    // }
    // }
    List<TaxItem> taxItems = new ArrayList<TaxItem>();
    // iterate through the tax class and get appropriate rates
    for (Long taxClassId : taxClassAmountMap.keySet()) {
        // get taxRate by tax class
        List<TaxRate> taxRates = null;
        if (!StringUtils.isBlank(stateProvince) && zone == null) {
            taxRates = taxRateService.listByCountryStateProvinceAndTaxClass(country, stateProvince, taxClasses.get(taxClassId), store, language);
        } else {
            taxRates = taxRateService.listByCountryZoneAndTaxClass(country, zone, taxClasses.get(taxClassId), store, language);
        }
        if (taxRates == null || taxRates.size() == 0) {
            continue;
        }
        BigDecimal taxedItemValue = null;
        BigDecimal totalTaxedItemValue = new BigDecimal(0);
        totalTaxedItemValue.setScale(2, RoundingMode.HALF_UP);
        BigDecimal beforeTaxeAmount = taxClassAmountMap.get(taxClassId);
        for (TaxRate taxRate : taxRates) {
            // 5% ... 8% ...
            double taxRateDouble = taxRate.getTaxRate().doubleValue();
            if (taxRate.isPiggyback()) {
                // (compound)
                if (totalTaxedItemValue.doubleValue() > 0) {
                    beforeTaxeAmount = totalTaxedItemValue;
                }
            }
            // else just use nominal taxing (combine)
            double value = (beforeTaxeAmount.doubleValue() * taxRateDouble) / 100;
            double roundedValue = new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).doubleValue();
            taxedItemValue = new BigDecimal(roundedValue).setScale(2, RoundingMode.HALF_UP);
            totalTaxedItemValue = beforeTaxeAmount.add(taxedItemValue);
            TaxItem taxItem = new TaxItem();
            taxItem.setItemPrice(taxedItemValue);
            taxItem.setLabel(taxRate.getDescriptions().get(0).getName());
            taxItem.setTaxRate(taxRate);
            taxItems.add(taxItem);
        }
    }
    Map<String, TaxItem> taxItemsMap = new TreeMap<String, TaxItem>();
    // consolidate tax rates of same code
    for (TaxItem taxItem : taxItems) {
        TaxRate taxRate = taxItem.getTaxRate();
        if (!taxItemsMap.containsKey(taxRate.getCode())) {
            taxItemsMap.put(taxRate.getCode(), taxItem);
        }
        TaxItem item = taxItemsMap.get(taxRate.getCode());
        BigDecimal amount = item.getItemPrice();
        amount = amount.add(taxItem.getItemPrice());
    }
    if (taxItemsMap.size() == 0) {
        return null;
    }
    @SuppressWarnings("rawtypes") Collection<TaxItem> values = taxItemsMap.values();
    @SuppressWarnings("unchecked") List<TaxItem> list = new ArrayList<TaxItem>(values);
    return list;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) TaxRate(com.salesmanager.core.model.tax.taxrate.TaxRate) TaxConfiguration(com.salesmanager.core.model.tax.TaxConfiguration) Zone(com.salesmanager.core.model.reference.zone.Zone) TaxClass(com.salesmanager.core.model.tax.taxclass.TaxClass) TreeMap(java.util.TreeMap) BigDecimal(java.math.BigDecimal) TaxItem(com.salesmanager.core.model.tax.TaxItem) Billing(com.salesmanager.core.model.common.Billing) Country(com.salesmanager.core.model.reference.country.Country) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) TaxBasisCalculation(com.salesmanager.core.model.tax.TaxBasisCalculation) Delivery(com.salesmanager.core.model.common.Delivery)

Aggregations

ShoppingCartItem (com.salesmanager.core.model.shoppingcart.ShoppingCartItem)2 TaxItem (com.salesmanager.core.model.tax.TaxItem)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)1 Billing (com.salesmanager.core.model.common.Billing)1 Delivery (com.salesmanager.core.model.common.Delivery)1 OrderTotal (com.salesmanager.core.model.order.OrderTotal)1 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)1 OrderTotalVariation (com.salesmanager.core.model.order.OrderTotalVariation)1 Country (com.salesmanager.core.model.reference.country.Country)1 Zone (com.salesmanager.core.model.reference.zone.Zone)1 ShippingConfiguration (com.salesmanager.core.model.shipping.ShippingConfiguration)1 ShippingSummary (com.salesmanager.core.model.shipping.ShippingSummary)1 TaxBasisCalculation (com.salesmanager.core.model.tax.TaxBasisCalculation)1 TaxConfiguration (com.salesmanager.core.model.tax.TaxConfiguration)1 TaxClass (com.salesmanager.core.model.tax.taxclass.TaxClass)1 TaxRate (com.salesmanager.core.model.tax.taxrate.TaxRate)1 TreeMap (java.util.TreeMap)1