Search in sources :

Example 6 with ProductCategory

use of com.axelor.apps.base.db.ProductCategory in project axelor-open-suite by axelor.

the class ProductCategoryServiceImpl method computeDiscountMessage.

@Override
public String computeDiscountMessage(ProductCategory productCategory) throws AxelorException {
    BigDecimal maxDiscount = productCategory.getMaxDiscount();
    if (maxDiscount.signum() <= 0) {
        // field is empty: no messages required
        return "";
    }
    List<ProductCategory> parentCategories = fetchParentCategoryListWithMaxDiscount(productCategory);
    List<ProductCategory> childrenCategories = fetchChildrenCategoryListWithMaxDiscount(productCategory);
    StringBuilder discountMessage = new StringBuilder();
    if (!parentCategories.isEmpty() || !childrenCategories.isEmpty()) {
        discountMessage.append(I18n.get("This maximal discount"));
        discountMessage.append(" ");
    }
    if (!parentCategories.isEmpty()) {
        discountMessage.append(I18n.get("will override discounts applied to parents categories"));
        discountMessage.append(" ");
        discountMessage.append(parentCategories.stream().map(pc -> String.format("%s (%s%%)", pc.getCode(), pc.getMaxDiscount())).collect(Collectors.joining(", ")));
        if (!childrenCategories.isEmpty()) {
            discountMessage.append(" ");
            discountMessage.append(I18n.get("and"));
            discountMessage.append(" ");
        }
    }
    if (!childrenCategories.isEmpty()) {
        discountMessage.append(I18n.get("will not be applied to following children categories"));
        discountMessage.append(" ");
        discountMessage.append(childrenCategories.stream().map(pc -> String.format("%s (%s%%)", pc.getCode(), pc.getMaxDiscount())).collect(Collectors.joining(", ")));
    }
    return discountMessage.toString();
}
Also used : ProductCategory(com.axelor.apps.base.db.ProductCategory) BigDecimal(java.math.BigDecimal)

Example 7 with ProductCategory

use of com.axelor.apps.base.db.ProductCategory in project axelor-open-suite by axelor.

the class ProductCategoryServiceSaleImpl method hasDiscountBecameTooHigh.

protected boolean hasDiscountBecameTooHigh(ProductCategory productCategory, SaleOrderLine saleOrderLine) {
    ProductCategory productCategoryIt = productCategory;
    BigDecimal maxDiscount = productCategory.getMaxDiscount();
    while (maxDiscount.signum() == 0 && productCategoryIt.getParentProductCategory() != null) {
        productCategoryIt = productCategory.getParentProductCategory();
        maxDiscount = productCategoryIt.getMaxDiscount();
    }
    if (maxDiscount.signum() == 0) {
        return false;
    }
    // compute discount percent in sale order line
    BigDecimal saleOrderLineDiscount;
    switch(saleOrderLine.getDiscountTypeSelect()) {
        case PriceListLineRepository.AMOUNT_TYPE_PERCENT:
            saleOrderLineDiscount = saleOrderLine.getDiscountAmount();
            break;
        case PriceListLineRepository.AMOUNT_TYPE_FIXED:
            saleOrderLineDiscount = saleOrderLine.getPrice().signum() != 0 ? saleOrderLine.getDiscountAmount().multiply(new BigDecimal("100")).divide(saleOrderLine.getPrice(), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
            break;
        case PriceListLineRepository.AMOUNT_TYPE_NONE:
        default:
            saleOrderLineDiscount = BigDecimal.ZERO;
            break;
    }
    return saleOrderLineDiscount.compareTo(maxDiscount) > 0;
}
Also used : ProductCategory(com.axelor.apps.base.db.ProductCategory) BigDecimal(java.math.BigDecimal)

Aggregations

ProductCategory (com.axelor.apps.base.db.ProductCategory)7 AxelorException (com.axelor.exception.AxelorException)3 ArrayList (java.util.ArrayList)3 BigDecimal (java.math.BigDecimal)2 ProductCategoryDomainCreatorService (com.axelor.apps.base.service.ProductCategoryDomainCreatorService)1 ProductCategoryService (com.axelor.apps.base.service.ProductCategoryService)1