Search in sources :

Example 1 with ConfiguratorFormula

use of com.axelor.apps.sale.db.ConfiguratorFormula in project axelor-open-suite by axelor.

the class ConfiguratorCreatorServiceImpl method updateIndicators.

@Transactional
public void updateIndicators(ConfiguratorCreator creator) throws AxelorException {
    List<MetaJsonField> indicators = Optional.ofNullable(creator.getIndicators()).orElse(Collections.emptyList());
    // add missing formulas
    List<? extends ConfiguratorFormula> formulas;
    if (creator.getGenerateProduct()) {
        formulas = creator.getConfiguratorProductFormulaList();
    } else {
        formulas = creator.getConfiguratorSOLineFormulaList();
    }
    for (ConfiguratorFormula formula : formulas) {
        addIfMissing(formula, creator);
    }
    // remove formulas
    List<MetaJsonField> fieldsToRemove = new ArrayList<>();
    for (MetaJsonField indicator : indicators) {
        if (isNotInFormulas(indicator, creator, formulas)) {
            fieldsToRemove.add(indicator);
        }
    }
    for (MetaJsonField indicatorToRemove : fieldsToRemove) {
        creator.removeIndicator(indicatorToRemove);
    }
    updateIndicatorsAttrs(creator, formulas);
    configuratorCreatorRepo.save(creator);
}
Also used : ConfiguratorFormula(com.axelor.apps.sale.db.ConfiguratorFormula) ArrayList(java.util.ArrayList) MetaJsonField(com.axelor.meta.db.MetaJsonField) Transactional(com.google.inject.persist.Transactional)

Example 2 with ConfiguratorFormula

use of com.axelor.apps.sale.db.ConfiguratorFormula in project axelor-open-suite by axelor.

the class ConfiguratorServiceImpl method fillOneToManyFields.

protected void fillOneToManyFields(Configurator configurator, Model model, JsonContext jsonAttributes) throws AxelorException {
    try {
        ConfiguratorCreator creator = configurator.getConfiguratorCreator();
        List<? extends ConfiguratorFormula> configuratorFormulaList;
        Class[] methodArg = new Class[1];
        if (creator.getGenerateProduct()) {
            configuratorFormulaList = creator.getConfiguratorProductFormulaList();
            methodArg[0] = Product.class;
        } else {
            configuratorFormulaList = creator.getConfiguratorSOLineFormulaList();
            methodArg[0] = SaleOrderLine.class;
        }
        configuratorFormulaList = configuratorFormulaList.stream().filter(configuratorFormula -> "OneToMany".equals(configuratorFormula.getMetaField().getRelationship())).collect(Collectors.toList());
        for (ConfiguratorFormula formula : configuratorFormulaList) {
            List<? extends Model> computedValue = (List<? extends Model>) computeFormula(formula.getFormula(), jsonAttributes);
            if (computedValue == null) {
                continue;
            }
            Method setMappedByMethod = computeMappedByMethod(formula);
            for (Model listElement : computedValue) {
                setMappedByMethod.invoke(listElement, model);
                JPA.save(listElement);
            }
        }
    } catch (InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
        throw new AxelorException(e, TraceBackRepository.CATEGORY_INCONSISTENCY);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) ConfiguratorCreator(com.axelor.apps.sale.db.ConfiguratorCreator) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfiguratorFormula(com.axelor.apps.sale.db.ConfiguratorFormula) Model(com.axelor.db.Model) List(java.util.List)

Example 3 with ConfiguratorFormula

use of com.axelor.apps.sale.db.ConfiguratorFormula in project axelor-open-suite by axelor.

the class ConfiguratorFormulaController method checkGroovyFormula.

/**
 * Check the groovy script in the context
 *
 * @param request
 * @param response
 */
public void checkGroovyFormula(ActionRequest request, ActionResponse response) {
    ConfiguratorFormula configuratorFormula = request.getContext().asType(ConfiguratorFormula.class);
    ConfiguratorCreator creator = request.getContext().getParent().asType(ConfiguratorCreator.class);
    try {
        Beans.get(ConfiguratorFormulaService.class).checkFormula(configuratorFormula, creator);
        response.setAlert(I18n.get(IExceptionMessage.CONFIGURATOR_CREATOR_SCRIPT_WORKING));
    } catch (Exception e) {
        response.setError(e.getMessage());
    }
}
Also used : ConfiguratorCreator(com.axelor.apps.sale.db.ConfiguratorCreator) ConfiguratorFormula(com.axelor.apps.sale.db.ConfiguratorFormula) ConfiguratorFormulaService(com.axelor.apps.sale.service.configurator.ConfiguratorFormulaService)

Example 4 with ConfiguratorFormula

use of com.axelor.apps.sale.db.ConfiguratorFormula in project axelor-open-suite by axelor.

the class ConfiguratorServiceImpl method computeIndicatorValue.

/**
 * Compute the value of one indicator. Using the corresponding formula and the values in {@link
 * Configurator#attributes}
 *
 * @param configurator
 * @param indicatorName
 * @param jsonAttributes
 * @return
 */
protected Object computeIndicatorValue(Configurator configurator, String indicatorName, JsonContext jsonAttributes) {
    ConfiguratorCreator creator = configurator.getConfiguratorCreator();
    List<? extends ConfiguratorFormula> formulas;
    if (creator.getGenerateProduct()) {
        formulas = creator.getConfiguratorProductFormulaList();
    } else {
        formulas = creator.getConfiguratorSOLineFormulaList();
    }
    String groovyFormula = null;
    for (ConfiguratorFormula formula : formulas) {
        String fieldName = indicatorName;
        fieldName = fieldName.substring(0, fieldName.indexOf('_'));
        MetaField metaField = formula.getMetaField();
        if (metaField.getName().equals(fieldName)) {
            groovyFormula = formula.getFormula();
            break;
        }
    }
    if (groovyFormula == null || jsonAttributes == null) {
        return null;
    }
    return computeFormula(groovyFormula, jsonAttributes);
}
Also used : ConfiguratorCreator(com.axelor.apps.sale.db.ConfiguratorCreator) ConfiguratorFormula(com.axelor.apps.sale.db.ConfiguratorFormula) MetaField(com.axelor.meta.db.MetaField)

Aggregations

ConfiguratorFormula (com.axelor.apps.sale.db.ConfiguratorFormula)4 ConfiguratorCreator (com.axelor.apps.sale.db.ConfiguratorCreator)3 ConfiguratorFormulaService (com.axelor.apps.sale.service.configurator.ConfiguratorFormulaService)1 Model (com.axelor.db.Model)1 AxelorException (com.axelor.exception.AxelorException)1 MetaField (com.axelor.meta.db.MetaField)1 MetaJsonField (com.axelor.meta.db.MetaJsonField)1 Transactional (com.google.inject.persist.Transactional)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1