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