Search in sources :

Example 1 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker in project axelor-open-suite by axelor.

the class PrintTemplateLineServiceImpl method initMaker.

@SuppressWarnings("unchecked")
protected TemplateMaker initMaker(Long objectId, String model, String simpleModel, Locale locale) throws ClassNotFoundException {
    String timezone = null;
    Company activeCompany = Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null);
    if (activeCompany != null) {
        timezone = activeCompany.getTimezone();
    }
    TemplateMaker maker = new TemplateMaker(timezone, locale, TEMPLATE_DELIMITER, TEMPLATE_DELIMITER);
    Class<? extends Model> modelClass = (Class<? extends Model>) Class.forName(model);
    maker.setContext(JPA.find(modelClass, objectId), simpleModel);
    return maker;
}
Also used : Company(com.axelor.apps.base.db.Company) TemplateMaker(com.axelor.tool.template.TemplateMaker) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel)

Example 2 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker in project axelor-open-suite by axelor.

the class PrintTemplateServiceImpl method generatePrint.

@SuppressWarnings("unchecked")
@Override
@Transactional(rollbackOn = { AxelorException.class, Exception.class })
public Print generatePrint(Long objectId, PrintTemplate printTemplate) throws AxelorException, IOException, ClassNotFoundException {
    MetaModel metaModel = printTemplate.getMetaModel();
    if (metaModel == null) {
        return null;
    }
    String model = metaModel.getFullName();
    String simpleModel = metaModel.getName();
    // debug
    LOG.debug("");
    LOG.debug("model : {}", model);
    LOG.debug("simpleModel : {}", simpleModel);
    LOG.debug("object id : {}", objectId);
    LOG.debug("printTemplate : {}", printTemplate);
    LOG.debug("");
    Locale locale = Optional.ofNullable(printTemplate.getLanguage()).map(Language::getCode).map(Locale::new).orElseGet(AppFilter::getLocale);
    TemplateMaker maker = initMaker(objectId, model, simpleModel, locale);
    Print print = new Print();
    if (StringUtils.notEmpty(printTemplate.getDocumentName())) {
        maker.setTemplate(printTemplate.getDocumentName());
        print.setDocumentName(maker.make());
    }
    print.setMetaModel(metaModel);
    print.setObjectId(objectId);
    print.setCompany(printTemplate.getCompany());
    print.setLanguage(printTemplate.getLanguage());
    print.setHidePrintSettings(printTemplate.getHidePrintSettings());
    print.setFormatSelect(printTemplate.getFormatSelect());
    print.setDisplayTypeSelect(printTemplate.getDisplayTypeSelect());
    print.setIsEditable(printTemplate.getIsEditable());
    print.setAttach(printTemplate.getAttach());
    print.setMetaFileField(printTemplate.getMetaFileField());
    if (!printTemplate.getHidePrintSettings()) {
        if (StringUtils.notEmpty(printTemplate.getPrintTemplatePdfHeader())) {
            maker.setTemplate(printTemplate.getPrintTemplatePdfHeader());
            print.setPrintPdfHeader(maker.make());
        }
        if (StringUtils.notEmpty(printTemplate.getPrintTemplatePdfFooter())) {
            maker.setTemplate(printTemplate.getPrintTemplatePdfFooter());
            print.setPrintPdfFooter(maker.make());
            print.setFooterFontSize(printTemplate.getFooterFontSize());
            print.setFooterFontType(printTemplate.getFooterFontType());
            print.setFooterTextAlignment(printTemplate.getFooterTextAlignment());
            print.setIsFooterUnderLine(printTemplate.getIsFooterUnderLine());
            print.setFooterFontColor(printTemplate.getFooterFontColor());
        }
        print.setLogoPositionSelect(printTemplate.getLogoPositionSelect());
        print.setLogoWidth(printTemplate.getLogoWidth());
        print.setHeaderContentWidth(printTemplate.getHeaderContentWidth());
    }
    Context scriptContext = null;
    if (StringUtils.notEmpty(model)) {
        Class<? extends Model> modelClass = (Class<? extends Model>) Class.forName(model);
        Model modelObject = JPA.find(modelClass, objectId);
        if (ObjectUtils.notEmpty(modelObject)) {
            scriptContext = new Context(Mapper.toMap(modelObject), modelClass);
        }
    }
    rank = 0;
    int level = 1;
    if (CollectionUtils.isNotEmpty(printTemplate.getPrintTemplateLineList())) {
        processPrintTemplateLineList(printTemplate.getPrintTemplateLineList(), print, null, scriptContext, maker, level);
    }
    if (CollectionUtils.isNotEmpty(printTemplate.getPrintTemplateSet())) {
        for (PrintTemplate subPrintTemplate : printTemplate.getPrintTemplateSet()) {
            Long subObjectId = objectId;
            if (StringUtils.notEmpty(subPrintTemplate.getMetaModelDefaultPath())) {
                Object subMetaModelObject = templateContextService.computeTemplateContext(subPrintTemplate.getMetaModelDefaultPath(), scriptContext);
                Class<? extends Model> subModelClass = (Class<? extends Model>) Class.forName(subPrintTemplate.getMetaModel().getFullName());
                subObjectId = (Long) Mapper.of(subModelClass).get(subMetaModelObject, "id");
            }
            Print subPrint = generatePrint(subObjectId, subPrintTemplate);
            print.addPrintSetItem(subPrint);
        }
    }
    print = printRepo.save(print);
    printService.attachMetaFiles(print, getMetaFiles(maker, printTemplate));
    return print;
}
Also used : Locale(java.util.Locale) TemplateContext(com.axelor.apps.message.db.TemplateContext) Context(com.axelor.rpc.Context) PrintTemplate(com.axelor.apps.base.db.PrintTemplate) Print(com.axelor.apps.base.db.Print) Print(com.axelor.apps.base.db.Print) MetaModel(com.axelor.meta.db.MetaModel) AppFilter(com.axelor.app.internal.AppFilter) Language(com.axelor.apps.base.db.Language) TemplateMaker(com.axelor.tool.template.TemplateMaker) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) Transactional(com.google.inject.persist.Transactional)

Example 3 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker in project axelor-open-suite by axelor.

the class PrintTemplateServiceImpl method initMaker.

@SuppressWarnings("unchecked")
protected TemplateMaker initMaker(Long objectId, String model, String simpleModel, Locale locale) throws ClassNotFoundException {
    String timezone = null;
    Company activeCompany = Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null);
    if (activeCompany != null) {
        timezone = activeCompany.getTimezone();
    }
    TemplateMaker maker = new TemplateMaker(timezone, locale, TEMPLATE_DELIMITER, TEMPLATE_DELIMITER);
    Class<? extends Model> myClass = (Class<? extends Model>) Class.forName(model);
    maker.setContext(JPA.find(myClass, objectId), simpleModel);
    return maker;
}
Also used : Company(com.axelor.apps.base.db.Company) TemplateMaker(com.axelor.tool.template.TemplateMaker) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel)

Example 4 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker in project axelor-open-suite by axelor.

the class EmployeeBonusService method compute.

@Transactional(rollbackOn = { Exception.class })
public void compute(EmployeeBonusMgt bonus) throws AxelorException {
    Map<Employee, EmployeeBonusMgtLine> employeeStatus = new HashMap<>();
    for (EmployeeBonusMgtLine line : bonus.getEmployeeBonusMgtLineList()) {
        employeeStatus.put(line.getEmployee(), line);
    }
    List<Employee> allEmployee = Beans.get(EmployeeRepository.class).all().filter("self.mainEmploymentContract.payCompany = ?1", bonus.getCompany()).fetch();
    TemplateMaker maker = new TemplateMaker(bonus.getCompany().getTimezone(), AppFilter.getLocale(), TEMPLATE_DELIMITER, TEMPLATE_DELIMITER);
    String eval;
    CompilerConfiguration conf = new CompilerConfiguration();
    ImportCustomizer customizer = new ImportCustomizer();
    customizer.addStaticStars("java.lang.Math");
    conf.addCompilationCustomizers(customizer);
    Binding binding = new Binding();
    GroovyShell shell = new GroovyShell(binding, conf);
    Integer employeeBonusStatus = EmployeeBonusMgtRepository.STATUS_CALCULATED;
    for (Employee employee : allEmployee) {
        if (EmployeeHRRepository.isEmployeeFormerNewOrArchived(employee)) {
            continue;
        }
        // check if line is already calculated
        if (employeeStatus.get(employee) != null) {
            if (employeeStatus.get(employee).getStatusSelect().equals(EmployeeBonusMgtLineRepository.STATUS_CALCULATED)) {
                continue;
            } else {
                bonus.removeEmployeeBonusMgtLineListItem(employeeStatus.get(employee));
            }
        }
        maker.setContext(employee, "Employee");
        EmployeeBonusMgtLine line = new EmployeeBonusMgtLine();
        line.setEmployeeBonusMgt(bonus);
        line.setEmployee(employee);
        maker.addInContext("EmployeeBonusMgtLine", line);
        String formula = bonus.getEmployeeBonusType().getApplicationCondition();
        Integer lineStatus = EmployeeBonusMgtLineRepository.STATUS_CALCULATED;
        try {
            formula = replaceExpressionInFormula(formula, bonus.getCompany().getHrConfig(), employee, bonus.getPayPeriod());
        } catch (Exception e) {
            TraceBackService.trace(e);
            formula = "true";
            lineStatus = EmployeeBonusMgtLineRepository.STATUS_ANOMALY;
        }
        maker.setTemplate(formula);
        eval = maker.make();
        if (shell.evaluate(eval).toString().equals("true")) {
            try {
                formula = replaceExpressionInFormula(bonus.getEmployeeBonusType().getFormula(), bonus.getCompany().getHrConfig(), employee, bonus.getPayPeriod());
            } catch (Exception e) {
                lineStatus = EmployeeBonusMgtLineRepository.STATUS_ANOMALY;
            }
            line.setStatusSelect(lineStatus);
            if (lineStatus.equals(EmployeeBonusMgtLineRepository.STATUS_ANOMALY)) {
                employeeBonusStatus = EmployeeBonusMgtRepository.STATUS_ANOMALY;
                employeeBonusMgtLineRepo.save(line);
                continue;
            }
            line.setSeniorityDate(employee.getSeniorityDate());
            line.setCoef(employee.getBonusCoef());
            line.setWeeklyPlanning(employee.getWeeklyPlanning());
            maker.setTemplate(formula);
            eval = maker.make();
            line.setAmount(new BigDecimal(shell.evaluate(eval).toString()));
            employeeBonusMgtLineRepo.save(line);
        }
    }
    bonus.setStatusSelect(employeeBonusStatus);
    employeeBonusMgtRepo.save(bonus);
}
Also used : Binding(groovy.lang.Binding) HashMap(java.util.HashMap) GroovyShell(groovy.lang.GroovyShell) AxelorException(com.axelor.exception.AxelorException) BigDecimal(java.math.BigDecimal) EmployeeRepository(com.axelor.apps.hr.db.repo.EmployeeRepository) Employee(com.axelor.apps.hr.db.Employee) EmployeeBonusMgtLine(com.axelor.apps.hr.db.EmployeeBonusMgtLine) TemplateMaker(com.axelor.tool.template.TemplateMaker) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) Transactional(com.google.inject.persist.Transactional)

Example 5 with TemplateMaker

use of com.axelor.tool.template.TemplateMaker in project axelor-open-suite by axelor.

the class BatchSeniorityLeaveManagement method start.

@Override
protected void start() throws IllegalAccessException {
    super.start();
    if (batch.getHrBatch().getDayNumber() == null || batch.getHrBatch().getDayNumber() == BigDecimal.ZERO || batch.getHrBatch().getLeaveReason() == null)
        TraceBackService.trace(new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.BATCH_MISSING_FIELD)), ExceptionOriginRepository.LEAVE_MANAGEMENT, batch.getId());
    total = 0;
    noValueAnomaly = 0;
    confAnomaly = 0;
    this.maker = new TemplateMaker(batch.getHrBatch().getCompany().getTimezone(), AppFilter.getLocale(), TEMPLATE_DELIMITER, TEMPLATE_DELIMITER);
    hrConfig = Beans.get(HRConfigRepository.class).all().filter("self.company.id = ?1 ", batch.getHrBatch().getCompany().getId()).fetchOne();
    checkPoint();
}
Also used : AxelorException(com.axelor.exception.AxelorException) TemplateMaker(com.axelor.tool.template.TemplateMaker)

Aggregations

TemplateMaker (com.axelor.tool.template.TemplateMaker)11 Model (com.axelor.db.Model)4 AxelorException (com.axelor.exception.AxelorException)4 MetaModel (com.axelor.meta.db.MetaModel)4 Company (com.axelor.apps.base.db.Company)3 Transactional (com.google.inject.persist.Transactional)3 AppFilter (com.axelor.app.internal.AppFilter)2 Language (com.axelor.apps.base.db.Language)2 Context (com.axelor.rpc.Context)2 Binding (groovy.lang.Binding)2 GroovyShell (groovy.lang.GroovyShell)2 BigDecimal (java.math.BigDecimal)2 Locale (java.util.Locale)2 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)2 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)2 Test (org.junit.Test)2 Print (com.axelor.apps.base.db.Print)1 PrintTemplate (com.axelor.apps.base.db.PrintTemplate)1 PrintTemplateLineTest (com.axelor.apps.base.db.PrintTemplateLineTest)1 UnitConversion (com.axelor.apps.base.db.UnitConversion)1