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);
}
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);
}
}
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());
}
}
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);
}
Aggregations