PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/skala/erp/bmp/server/excellexporters/MX18StatReport.java

https://bitbucket.org/ignatenko/bmp
Java | 151 lines | 121 code | 30 blank | 0 comment | 5 complexity | b4b74288446c23b7448b2e3879bd5efb MD5 | raw file
  1. package skala.erp.bmp.server.excellexporters;
  2. import java.io.FileOutputStream;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Arrays;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import org.apache.poi.hpsf.SummaryInformation;
  10. import org.apache.poi.hssf.usermodel.HSSFRow;
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.ss.usermodel.Cell;
  14. import org.apache.poi.ss.usermodel.CellStyle;
  15. import org.apache.poi.ss.usermodel.PrintSetup;
  16. import skala.erp.bmp.server.dao.MX18LineDAO;
  17. import skala.erp.bmp.server.dao.MX18TableDAO;
  18. import skala.erp.bmp.shared.entity.MX18Line;
  19. import skala.erp.bmp.shared.entity.MX18Table;
  20. public class MX18StatReport extends ExcelExporter {
  21. MX18LineDAO mx18LineDAO;
  22. MX18TableDAO mx18TableDAO;
  23. Map<String, Double> totalQties;
  24. public MX18StatReport(MX18TableDAO mx18TableDAO, MX18LineDAO mx18LineDAO) {
  25. this.mx18LineDAO = mx18LineDAO;
  26. this.mx18TableDAO = mx18TableDAO;
  27. }
  28. public String getStatReportInDateRange(Date fromDate, Date toDate,
  29. String dataAreaId) {
  30. totalQties = new HashMap<String, Double>();
  31. SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.YYYY");
  32. String filename = "mx18stat-" + dateFormat.format(fromDate) + "-"
  33. + dateFormat.format(toDate) + ".xls";
  34. HSSFWorkbook hwb = new HSSFWorkbook();
  35. Map<String, CellStyle> styles = createStyles(hwb);
  36. hwb.createInformationProperties();
  37. SummaryInformation summaryInfo = hwb.getSummaryInformation();
  38. summaryInfo.setAuthor("BMP-System");
  39. HSSFSheet sheet = hwb.createSheet("MX-18 - Статистика");
  40. PrintSetup printSetup = sheet.getPrintSetup();
  41. printSetup.setLandscape(false);
  42. sheet.setFitToPage(true);
  43. sheet.setHorizontallyCenter(true);
  44. int rowIndex = 0;
  45. List<MX18Table> list = mx18TableDAO.getAllPostedInDateRange(fromDate,
  46. toDate, dataAreaId);
  47. for (MX18Table mx18 : list) {
  48. addHeaderRow(
  49. styles.get("title"),
  50. sheet,
  51. rowIndex,
  52. "Накладная № " + mx18.getMx18Number() + " от "
  53. + dateFormat.format(mx18.getMx18Date()));
  54. rowIndex++;
  55. HSSFRow inventHeaderRow = sheet.createRow(rowIndex);
  56. inventHeaderRow.setHeightInPoints(30);
  57. Cell cell = inventHeaderRow.createCell(0);
  58. cell.setCellValue("Номенклатура");
  59. cell.setCellStyle(styles.get("header"));
  60. cell = inventHeaderRow.createCell(1);
  61. cell.setCellValue("Количество");
  62. cell.setCellStyle(styles.get("header"));
  63. cell = inventHeaderRow.createCell(2);
  64. cell.setCellValue("Ед. изм.");
  65. cell.setCellStyle(styles.get("header"));
  66. rowIndex++;
  67. List<MX18Line> lines = mx18LineDAO.getAllByNumber(
  68. mx18.getMx18Number(), dataAreaId);
  69. for (MX18Line line : lines) {
  70. HSSFRow currentRow = sheet.createRow(rowIndex);
  71. currentRow.setHeightInPoints(30);
  72. String key = line.getItem().getName() + "-"
  73. + line.getItem().getUOMId();
  74. cell = currentRow.createCell(0);
  75. cell.setCellStyle(styles.get("cell_left"));
  76. cell.setCellValue(line.getItem().getName());
  77. cell = currentRow.createCell(1);
  78. cell.setCellValue(line.getQty());
  79. cell.setCellStyle(styles.get("cell"));
  80. cell = currentRow.createCell(2);
  81. cell.setCellValue(line.getItem().getUOMId());
  82. cell.setCellStyle(styles.get("cell"));
  83. if (!totalQties.containsKey(key)) {
  84. totalQties.put(key, line.getQty());
  85. } else {
  86. double value = totalQties.get(key);
  87. totalQties.put(key, value + line.getQty());
  88. }
  89. rowIndex++;
  90. }
  91. }
  92. rowIndex++;
  93. String[] items = totalQties.keySet().toArray(new String[0]);
  94. Arrays.sort(items);
  95. addHeaderRow(styles.get("title"), sheet, rowIndex,
  96. "Всего передано за период с " + dateFormat.format(fromDate)
  97. + " по " + dateFormat.format(toDate));
  98. rowIndex++;
  99. for (String item : items) {
  100. HSSFRow currentRow = sheet.createRow(rowIndex);
  101. currentRow.setHeightInPoints(30);
  102. String[] splitedKey = item.split("\\-");
  103. Cell cell = currentRow.createCell(0);
  104. cell.setCellStyle(styles.get("simple_text"));
  105. cell.setCellValue(splitedKey[0] + " - "
  106. + String.format("%.2f", totalQties.get(item)) + " "
  107. + splitedKey[1]);
  108. rowIndex++;
  109. }
  110. sheet.autoSizeColumn(0);
  111. sheet.autoSizeColumn(1);
  112. try {
  113. FileOutputStream fileOutputStream = new FileOutputStream(filename);
  114. hwb.write(fileOutputStream);
  115. fileOutputStream.close();
  116. } catch (Exception e) {
  117. e.printStackTrace();
  118. }
  119. return filename;
  120. }
  121. }