Search in sources :

Example 1 with ScriptHelper

use of com.axelor.script.ScriptHelper in project axelor-open-suite by axelor.

the class LogisticalFormServiceImpl method computeTotals.

@Override
public void computeTotals(LogisticalForm logisticalForm) throws LogisticalFormError {
    BigDecimal totalNetMass = BigDecimal.ZERO;
    BigDecimal totalGrossMass = BigDecimal.ZERO;
    BigDecimal totalVolume = BigDecimal.ZERO;
    if (logisticalForm.getLogisticalFormLineList() != null) {
        ScriptHelper scriptHelper = getScriptHelper(logisticalForm);
        LogisticalFormLineService logisticalFormLineService = Beans.get(LogisticalFormLineService.class);
        for (LogisticalFormLine logisticalFormLine : logisticalForm.getLogisticalFormLineList()) {
            StockMoveLine stockMoveLine = logisticalFormLine.getStockMoveLine();
            if (logisticalFormLine.getTypeSelect() != LogisticalFormLineRepository.TYPE_DETAIL) {
                if (logisticalFormLine.getGrossMass() != null) {
                    totalGrossMass = totalGrossMass.add(logisticalFormLine.getGrossMass());
                }
                BigDecimal toAdd = logisticalFormLineService.evalVolume(logisticalFormLine, scriptHelper);
                if (toAdd == null) {
                    throw new LogisticalFormError(logisticalForm, I18n.get(IExceptionMessage.LOGISTICAL_FORM_INVALID_DIMENSIONS));
                } else {
                    totalVolume = totalVolume.add(toAdd);
                }
            } else if (stockMoveLine != null) {
                totalNetMass = totalNetMass.add(logisticalFormLine.getQty().multiply(stockMoveLine.getNetMass()));
            }
        }
        totalVolume = totalVolume.divide(new BigDecimal(1_000_000), 10, RoundingMode.HALF_UP);
        logisticalForm.setTotalNetMass(totalNetMass.setScale(3, RoundingMode.HALF_UP));
        logisticalForm.setTotalGrossMass(totalGrossMass.setScale(3, RoundingMode.HALF_UP));
        logisticalForm.setTotalVolume(totalVolume.setScale(3, RoundingMode.HALF_UP));
    }
}
Also used : StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) LogisticalFormLine(com.axelor.apps.stock.db.LogisticalFormLine) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptHelper(com.axelor.script.ScriptHelper) BigDecimal(java.math.BigDecimal) LogisticalFormError(com.axelor.apps.stock.exception.LogisticalFormError)

Example 2 with ScriptHelper

use of com.axelor.script.ScriptHelper in project axelor-open-suite by axelor.

the class BatchJob method applyBeanPropertiesWithScriptHelper.

private Map<String, Object> applyBeanPropertiesWithScriptHelper(Object bean, Map<String, Object> properties) {
    Context scriptContext = new Context(Mapper.toMap(bean), bean.getClass());
    ScriptHelper scriptHelper = new GroovyScriptHelper(scriptContext);
    return applyBeanProperties(bean, properties, value -> scriptHelper.eval(value.toString()));
}
Also used : JobExecutionContext(org.quartz.JobExecutionContext) Context(com.axelor.rpc.Context) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptHelper(com.axelor.script.ScriptHelper) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper)

Example 3 with ScriptHelper

use of com.axelor.script.ScriptHelper in project axelor-open-suite by axelor.

the class ImportAdvancedImport method importGeneral.

@SuppressWarnings("unchecked")
public Object importGeneral(Object bean, Map<String, Object> values) throws ClassNotFoundException {
    if (bean == null) {
        return bean;
    }
    FileTab fileTab = fileTabRepo.find(Long.valueOf(values.get("fileTabId").toString()));
    ScriptHelper scriptHelper = new GroovyScriptHelper(new ScriptBindings(values));
    List<String> exprs = (List<String>) values.get("ifConditions" + fileTab.getId());
    if (!CollectionUtils.isEmpty(exprs)) {
        if ((boolean) scriptHelper.eval(String.join(" || ", exprs))) {
            return null;
        }
    }
    if (((Model) bean).getId() == null) {
        List<Property> propList = this.getProperties(bean);
        JPA.save((Model) bean);
        this.addJsonObjectRecord(bean, fileTab, fileTab.getMetaModel().getName(), values);
        int fieldSeq = 2;
        int btnSeq = 3;
        for (Property prop : propList) {
            validatorService.createCustomObjectSet(fileTab.getClass().getName(), prop.getTarget().getName(), fieldSeq);
            validatorService.createCustomButton(fileTab.getClass().getName(), prop.getTarget().getName(), btnSeq);
            this.addJsonObjectRecord(prop.get(bean), fileTab, StringUtils.substringAfterLast(prop.getTarget().getName(), "."), values);
            fieldSeq++;
            btnSeq++;
        }
    }
    final String ACTIONS_TO_APPLY = "actionsToApply" + fileTab.getId();
    if (!ObjectUtils.isEmpty(values.get(ACTIONS_TO_APPLY))) {
        bean = actionService.apply(values.get(ACTIONS_TO_APPLY).toString(), bean);
    }
    return bean;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) FileTab(com.axelor.apps.base.db.FileTab) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptHelper(com.axelor.script.ScriptHelper) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptBindings(com.axelor.script.ScriptBindings) Property(com.axelor.db.mapper.Property)

Example 4 with ScriptHelper

use of com.axelor.script.ScriptHelper in project axelor-open-suite by axelor.

the class ConfiguratorServiceImpl method computeFormula.

@Override
public Object computeFormula(String groovyFormula, JsonContext values) {
    User currentUser = AuthUtils.getUser();
    Company company = currentUser != null ? currentUser.getActiveCompany() : null;
    values.put("__user__", currentUser);
    values.put("__date__", appBaseService.getTodayDate(company));
    values.put("__datetime__", appBaseService.getTodayDateTime(company));
    ScriptHelper scriptHelper = new GroovyScriptHelper(values);
    return scriptHelper.eval(groovyFormula);
}
Also used : Company(com.axelor.apps.base.db.Company) User(com.axelor.auth.db.User) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptHelper(com.axelor.script.ScriptHelper) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper)

Aggregations

GroovyScriptHelper (com.axelor.script.GroovyScriptHelper)4 ScriptHelper (com.axelor.script.ScriptHelper)4 Company (com.axelor.apps.base.db.Company)1 FileTab (com.axelor.apps.base.db.FileTab)1 LogisticalFormLine (com.axelor.apps.stock.db.LogisticalFormLine)1 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)1 LogisticalFormError (com.axelor.apps.stock.exception.LogisticalFormError)1 User (com.axelor.auth.db.User)1 Property (com.axelor.db.mapper.Property)1 Context (com.axelor.rpc.Context)1 ScriptBindings (com.axelor.script.ScriptBindings)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JobExecutionContext (org.quartz.JobExecutionContext)1