/**
* ***************************************************************************
* Copyright (c) 2010 Qcadoo Limited
* Project: Qcadoo MES
* Version: 1.4
*
* This file is part of Qcadoo.
*
* Qcadoo is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation; either version 3 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* ***************************************************************************
*/
package com.qcadoo.mes.materialFlow.print.xls;
import static com.qcadoo.mes.basic.constants.ProductFields.NAME;
import static com.qcadoo.mes.basic.constants.ProductFields.NUMBER;
import static com.qcadoo.mes.basic.constants.ProductFields.UNIT;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qcadoo.localization.api.TranslationService;
import com.qcadoo.mes.materialFlow.MaterialFlowService;
import com.qcadoo.model.api.Entity;
import com.qcadoo.model.api.NumberService;
import com.qcadoo.report.api.xls.XlsDocumentService;
import com.qcadoo.report.api.xls.XlsHelper;
@Service
public final class MaterialFlowXlsService extends XlsDocumentService {
@Autowired
private MaterialFlowService materialFlowService;
@Autowired
private TranslationService translationService;
@Autowired
private NumberService numberService;
@Autowired
private XlsHelper xlsHelper;
@Override
protected void addHeader(final HSSFSheet sheet, final Locale locale, final Entity entity) {
HSSFRow header = sheet.createRow(0);
HSSFCell cell0 = header.createCell(0);
cell0.setCellValue(translationService.translate("materialFlow.materialFlow.report.columnHeader.number", locale));
xlsHelper.setCellStyle(sheet, cell0);
HSSFCell cell1 = header.createCell(1);
cell1.setCellValue(translationService.translate("materialFlow.materialFlow.report.columnHeader.name", locale));
xlsHelper.setCellStyle(sheet, cell1);
HSSFCell cell2 = header.createCell(2);
cell2.setCellValue(translationService.translate("materialFlow.materialFlow.report.columnHeader.quantity", locale));
xlsHelper.setCellStyle(sheet, cell2);
HSSFCell cell3 = header.createCell(3);
cell3.setCellValue(translationService.translate("materialFlow.materialFlow.report.columnHeader.unit", locale));
xlsHelper.setCellStyle(sheet, cell3);
}
@Override
protected void addSeries(final HSSFSheet sheet, final Entity materialsInLocation) {
Map<Entity, BigDecimal> reportData = materialFlowService.calculateMaterialQuantitiesInLocation(materialsInLocation);
int rowNum = 1;
for (Map.Entry<Entity, BigDecimal> data : reportData.entrySet()) {
HSSFRow row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(data.getKey().getStringField(NUMBER));
row.createCell(1).setCellValue(data.getKey().getStringField(NAME));
row.createCell(2).setCellValue(numberService.format(data.getValue()));
row.createCell(3).setCellValue(data.getKey().getStringField(UNIT));
}
sheet.autoSizeColumn((short) 0);
sheet.autoSizeColumn((short) 1);
sheet.autoSizeColumn((short) 2);
sheet.autoSizeColumn((short) 3);
}
@Override
public String getReportTitle(final Locale locale) {
return translationService.translate("materialFlow.materialFlow.report.title", locale);
}
}