Search in sources :

Example 6 with TaxClass

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

the class TaxFacadeImpl method deleteTaxClass.

@Override
public void deleteTaxClass(Long id, MerchantStore store, Language language) {
    Validate.notNull(id, "TaxClass id cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(store.getCode(), "MerchantStore code cannot be null");
    try {
        TaxClass model = taxClassService.getById(id);
        if (model == null) {
            throw new ResourceNotFoundException("TaxClass not found [" + id + "] for store [" + store.getCode() + "]");
        } else {
            if (!model.getMerchantStore().getCode().equals(store.getCode())) {
                throw new UnauthorizedException("MerchantStore [" + store.getCode() + "] cannot delete tax class [" + id + "]");
            }
        }
        taxClassService.delete(model);
    } catch (ServiceException e) {
        LOGGER.error("Error while getting taxClasse [" + id + "] for store [" + store.getCode() + "]", e);
        throw new ServiceRuntimeException("Error while getting taxClasse [" + id + "] for store [" + store.getCode() + "]", e);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) TaxClass(com.salesmanager.core.model.tax.taxclass.TaxClass) ReadableTaxClass(com.salesmanager.shop.model.tax.ReadableTaxClass) PersistableTaxClass(com.salesmanager.shop.model.tax.PersistableTaxClass) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 7 with TaxClass

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

the class TaxFacadeImpl method taxClass.

@Override
public ReadableTaxClass taxClass(String code, MerchantStore store, Language language) {
    Validate.notNull(code, "TaxClass code cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(store.getCode(), "MerchantStore code cannot be null");
    try {
        TaxClass model = taxClassService.getByCode(code, store);
        if (model == null) {
            throw new ResourceNotFoundException("TaxClass not found [" + code + "] for store [" + store.getCode() + "]");
        }
        if (model != null) {
            if (!model.getMerchantStore().getCode().equals(store.getCode())) {
                throw new UnauthorizedException("MerchantStore [" + store.getCode() + "] cannot get tax class [" + code + "]");
            }
        }
        return readableTaxClassMapper.convert(model, store, language);
    } catch (ServiceException e) {
        LOGGER.error("Error while getting taxClass [" + code + "] for store [" + store.getCode() + "]", e);
        throw new ServiceRuntimeException("Error while getting taxClass [" + code + "] for store [" + store.getCode() + "]", e);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) TaxClass(com.salesmanager.core.model.tax.taxclass.TaxClass) ReadableTaxClass(com.salesmanager.shop.model.tax.ReadableTaxClass) PersistableTaxClass(com.salesmanager.shop.model.tax.PersistableTaxClass) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 8 with TaxClass

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

the class TaxFacadeImpl method updateTaxClass.

@Override
public void updateTaxClass(Long id, PersistableTaxClass taxClass, MerchantStore store, Language language) {
    Validate.notNull(taxClass, "TaxClass cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(store.getCode(), "MerchantStore code cannot be null");
    try {
        TaxClass model = taxClassService.getById(id);
        if (model == null) {
            throw new ResourceNotFoundException("TaxClass not found [" + id + "] for store [" + store.getCode() + "]");
        } else {
            if (!model.getMerchantStore().getCode().equals(store.getCode())) {
                throw new UnauthorizedException("MerchantStore [" + store.getCode() + "] cannot update tax class [" + taxClass.getCode() + "]");
            }
        }
        model = persistableTaxClassMapper.convert(taxClass, store, language);
        taxClassService.saveOrUpdate(model);
    } catch (ServiceException e) {
        LOGGER.error("Error while saving taxClass [" + taxClass.getCode() + "] for store [" + store.getCode() + "]", e);
        throw new ServiceRuntimeException("Error while saving taxClass [" + taxClass.getCode() + "] for store [" + store.getCode() + "]", e);
    }
}
Also used : ServiceException(com.salesmanager.core.business.exception.ServiceException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) TaxClass(com.salesmanager.core.model.tax.taxclass.TaxClass) ReadableTaxClass(com.salesmanager.shop.model.tax.ReadableTaxClass) PersistableTaxClass(com.salesmanager.shop.model.tax.PersistableTaxClass) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 9 with TaxClass

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

TaxClass (com.salesmanager.core.model.tax.taxclass.TaxClass)9 PersistableTaxClass (com.salesmanager.shop.model.tax.PersistableTaxClass)6 ServiceException (com.salesmanager.core.business.exception.ServiceException)5 ReadableTaxClass (com.salesmanager.shop.model.tax.ReadableTaxClass)5 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)5 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)4 UnauthorizedException (com.salesmanager.shop.store.api.exception.UnauthorizedException)4 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)2 Country (com.salesmanager.core.model.reference.country.Country)2 Language (com.salesmanager.core.model.reference.language.Language)2 Zone (com.salesmanager.core.model.reference.zone.Zone)2 TaxRate (com.salesmanager.core.model.tax.taxrate.TaxRate)2 Entity (com.salesmanager.shop.model.entity.Entity)2 OperationNotAllowedException (com.salesmanager.shop.store.api.exception.OperationNotAllowedException)2 ArrayList (java.util.ArrayList)2 TaxClassService (com.salesmanager.core.business.services.tax.TaxClassService)1 TaxRateService (com.salesmanager.core.business.services.tax.TaxRateService)1 Manufacturer (com.salesmanager.core.model.catalog.product.manufacturer.Manufacturer)1 ManufacturerDescription (com.salesmanager.core.model.catalog.product.manufacturer.ManufacturerDescription)1 Billing (com.salesmanager.core.model.common.Billing)1