Search in sources :

Example 1 with ProductCategory

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

the class ProductCategoryServiceImpl method fetchParentCategoryList.

public List<ProductCategory> fetchParentCategoryList(ProductCategory productCategory) throws AxelorException {
    // security in case of code error to avoid infinite loop
    int i = 0;
    List<ProductCategory> parentProductCategoryList = new ArrayList<>();
    if (productCategory.getId() != null) {
        productCategory = productCategoryRepository.find(productCategory.getId());
    }
    ProductCategory parentProductCategory = productCategory.getParentProductCategory();
    while (parentProductCategory != null && i < MAX_ITERATION) {
        if (parentProductCategoryList.contains(parentProductCategory) || parentProductCategory.equals(productCategory)) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.PRODUCT_CATEGORY_PARENTS_CIRCULAR_DEPENDENCY), parentProductCategory.getCode());
        }
        parentProductCategoryList.add(parentProductCategory);
        parentProductCategory = parentProductCategory.getParentProductCategory();
        i++;
    }
    return parentProductCategoryList;
}
Also used : AxelorException(com.axelor.exception.AxelorException) ProductCategory(com.axelor.apps.base.db.ProductCategory) ArrayList(java.util.ArrayList)

Example 2 with ProductCategory

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

the class ProductCategoryServiceImpl method fetchChildrenCategoryList.

public List<ProductCategory> fetchChildrenCategoryList(ProductCategory productCategory) throws AxelorException {
    // security in case of code error to avoid infinite loop
    int i = 0;
    List<ProductCategory> descendantsProductCategoryList = new ArrayList<>();
    if (productCategory.getId() == null) {
        // if product category is not saved, then it cannot have children
        return descendantsProductCategoryList;
    }
    List<ProductCategory> childrenProductCategoryList = fetchChildren(productCategory);
    while (!childrenProductCategoryList.isEmpty() && i < MAX_ITERATION) {
        List<ProductCategory> nextChildrenProductCategoryList = new ArrayList<>();
        for (ProductCategory childProductCategory : childrenProductCategoryList) {
            if (descendantsProductCategoryList.contains(childProductCategory) || childProductCategory.equals(productCategory)) {
                throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.PRODUCT_CATEGORY_CHILDREN_CIRCULAR_DEPENDENCY), childProductCategory.getCode());
            }
            descendantsProductCategoryList.add(childProductCategory);
            nextChildrenProductCategoryList.addAll(fetchChildren(childProductCategory));
        }
        childrenProductCategoryList.clear();
        childrenProductCategoryList.addAll(nextChildrenProductCategoryList);
        nextChildrenProductCategoryList.clear();
        i++;
    }
    return descendantsProductCategoryList;
}
Also used : AxelorException(com.axelor.exception.AxelorException) ProductCategory(com.axelor.apps.base.db.ProductCategory) ArrayList(java.util.ArrayList)

Example 3 with ProductCategory

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

the class ProductCategoryServiceSaleImpl method fetchImpactedChildrenProductCategories.

/**
 * Fetch impacted children product categories on a category change. To find impacted product
 * categories, we fetch children of given product category. If these children do not have a max
 * discount, they are impacted and their own children can be impacted following the same pattern.
 *
 * @param productCategory a product category
 * @return the computed list of children impacted by a max discount change in given category.
 * @throws AxelorException if there is a configuration error
 */
protected List<ProductCategory> fetchImpactedChildrenProductCategories(ProductCategory productCategory) throws AxelorException {
    // security in case of code error to avoid infinite loop
    int i = 0;
    List<ProductCategory> descendantsProductCategoryList = new ArrayList<>();
    if (productCategory.getId() == null) {
        // if product category is not saved, then it cannot have children
        return descendantsProductCategoryList;
    }
    // product categories with max discounts are not be impacted
    List<ProductCategory> childrenProductCategoryList = fetchChildrenWitNoMaxDiscount(productCategory);
    while (!childrenProductCategoryList.isEmpty() && i < MAX_ITERATION) {
        List<ProductCategory> nextChildrenProductCategoryList = new ArrayList<>();
        for (ProductCategory childProductCategory : childrenProductCategoryList) {
            if (descendantsProductCategoryList.contains(childProductCategory) || childProductCategory.equals(productCategory)) {
                throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.PRODUCT_CATEGORY_CHILDREN_CIRCULAR_DEPENDENCY), childProductCategory.getCode());
            }
            descendantsProductCategoryList.add(childProductCategory);
            nextChildrenProductCategoryList.addAll(fetchChildrenWitNoMaxDiscount(childProductCategory));
        }
        childrenProductCategoryList.clear();
        childrenProductCategoryList.addAll(nextChildrenProductCategoryList);
        nextChildrenProductCategoryList.clear();
        i++;
    }
    return descendantsProductCategoryList;
}
Also used : AxelorException(com.axelor.exception.AxelorException) ProductCategory(com.axelor.apps.base.db.ProductCategory) ArrayList(java.util.ArrayList)

Example 4 with ProductCategory

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

the class ProductCategoryController method filterParentCategory.

public void filterParentCategory(ActionRequest request, ActionResponse response) {
    try {
        ProductCategory productCategory = request.getContext().asType(ProductCategory.class);
        String domain = Beans.get(ProductCategoryDomainCreatorService.class).createProductCategoryDomainFilteringChildren(productCategory);
        response.setAttr("parentProductCategory", "domain", domain);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : ProductCategoryDomainCreatorService(com.axelor.apps.base.service.ProductCategoryDomainCreatorService) ProductCategory(com.axelor.apps.base.db.ProductCategory)

Example 5 with ProductCategory

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

the class ProductCategoryController method showExistingDiscounts.

/**
 * Called from product category view on maxDiscount change. Call {@link
 * ProductCategoryService#computeDiscountMessage(ProductCategory)}.
 *
 * @param request
 * @param response
 */
public void showExistingDiscounts(ActionRequest request, ActionResponse response) {
    try {
        ProductCategory productCategory = request.getContext().asType(ProductCategory.class);
        String discountsMessage = Beans.get(ProductCategoryService.class).computeDiscountMessage(productCategory);
        if (!"".equals(discountsMessage)) {
            response.setFlash(discountsMessage);
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : ProductCategory(com.axelor.apps.base.db.ProductCategory) ProductCategoryService(com.axelor.apps.base.service.ProductCategoryService)

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