Search in sources :

Example 1 with TaxConfiguration

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

the class TaxServiceImpl method getTaxConfiguration.

@Override
public TaxConfiguration getTaxConfiguration(MerchantStore store) throws ServiceException {
    MerchantConfiguration configuration = merchantConfigurationService.getMerchantConfiguration(TAX_CONFIGURATION, store);
    TaxConfiguration taxConfiguration = null;
    if (configuration != null) {
        String value = configuration.getValue();
        ObjectMapper mapper = new ObjectMapper();
        try {
            taxConfiguration = mapper.readValue(value, TaxConfiguration.class);
        } catch (Exception e) {
            throw new ServiceException("Cannot parse json string " + value);
        }
    }
    return taxConfiguration;
}
Also used : TaxConfiguration(com.salesmanager.core.model.tax.TaxConfiguration) ServiceException(com.salesmanager.core.business.exception.ServiceException) MerchantConfiguration(com.salesmanager.core.model.system.MerchantConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 2 with TaxConfiguration

use of com.salesmanager.core.model.tax.TaxConfiguration 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

TaxConfiguration (com.salesmanager.core.model.tax.TaxConfiguration)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ServiceException (com.salesmanager.core.business.exception.ServiceException)1 Billing (com.salesmanager.core.model.common.Billing)1 Delivery (com.salesmanager.core.model.common.Delivery)1 Country (com.salesmanager.core.model.reference.country.Country)1 Zone (com.salesmanager.core.model.reference.zone.Zone)1 ShippingSummary (com.salesmanager.core.model.shipping.ShippingSummary)1 ShoppingCartItem (com.salesmanager.core.model.shoppingcart.ShoppingCartItem)1 MerchantConfiguration (com.salesmanager.core.model.system.MerchantConfiguration)1 TaxBasisCalculation (com.salesmanager.core.model.tax.TaxBasisCalculation)1 TaxItem (com.salesmanager.core.model.tax.TaxItem)1 TaxClass (com.salesmanager.core.model.tax.taxclass.TaxClass)1 TaxRate (com.salesmanager.core.model.tax.taxrate.TaxRate)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1