PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ignatenko/bmp
Java | 428 lines | 380 code | 47 blank | 1 comment | 11 complexity | 63a180b68b138e3c41d96587b6ae179d 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.Comparator;
  6. import java.util.Date;
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. import java.util.Map.Entry;
  10. import org.apache.poi.hpsf.SummaryInformation;
  11. import org.apache.poi.hssf.usermodel.HSSFRow;
  12. import org.apache.poi.hssf.usermodel.HSSFSheet;
  13. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  14. import org.apache.poi.ss.usermodel.Cell;
  15. import org.apache.poi.ss.usermodel.CellStyle;
  16. import org.apache.poi.ss.usermodel.PrintSetup;
  17. import org.apache.poi.ss.util.CellRangeAddress;
  18. import skala.erp.bmp.baseenums.InventJournalType;
  19. import skala.erp.bmp.server.dao.InventJournalTableDAO;
  20. import skala.erp.bmp.shared.entity.InventJournalTable;
  21. import skala.erp.bmp.shared.entity.InventJournalTrans;
  22. public class InventJournalExcelExporter extends ExcelExporter {
  23. InventJournalTableDAO inventJournalTableDAO;
  24. public InventJournalExcelExporter(
  25. InventJournalTableDAO inventJournalTableDAO) {
  26. this.inventJournalTableDAO = inventJournalTableDAO;
  27. }
  28. public String getExcelFileName(String journalId, String dataAreaId) {
  29. String fileName = "journal-" + journalId + ".xls";
  30. InventJournalTable journal = inventJournalTableDAO.find(journalId,
  31. dataAreaId);
  32. InventJournalTrans[] journalTrans = journal.getJournalTrans().toArray(
  33. new InventJournalTrans[0]);
  34. Arrays.sort(journalTrans, new Comparator<InventJournalTrans>() {
  35. @Override
  36. public int compare(InventJournalTrans o1, InventJournalTrans o2) {
  37. return ((Integer) o1.getLineNum()).compareTo(o2.getLineNum());
  38. }
  39. });
  40. HSSFWorkbook hwb = new HSSFWorkbook();
  41. Map<String, CellStyle> styles = createStyles(hwb);
  42. hwb.createInformationProperties();
  43. SummaryInformation summaryInfo = hwb.getSummaryInformation();
  44. summaryInfo.setAuthor("BMP-System");
  45. HSSFSheet sheet = hwb.createSheet("Журнал");
  46. PrintSetup printSetup = sheet.getPrintSetup();
  47. printSetup.setLandscape(true);
  48. sheet.setFitToPage(true);
  49. sheet.setHorizontallyCenter(true);
  50. // Title row
  51. String journalStateField = "";
  52. if (journal.isPosted()) {
  53. journalStateField = "Разнесен "
  54. + journal.getPostedDateTime().toString();
  55. } else
  56. journalStateField = "Не разнесен";
  57. int rowIndex = 0;
  58. addHeaderRow(styles.get("title"), sheet, rowIndex, "Документ №"
  59. + journalId);
  60. rowIndex++;
  61. addHeaderRow(
  62. styles.get("title"),
  63. sheet,
  64. rowIndex,
  65. "Тип: "
  66. + InventJournalType.getEnumFromInt(
  67. journal.getInventJournalType()).getType());
  68. rowIndex++;
  69. addHeaderRow(styles.get("title"), sheet, rowIndex,
  70. journal.getInventJournalDesc());
  71. rowIndex++;
  72. addHeaderRow(styles.get("title"), sheet, rowIndex, journalStateField);
  73. rowIndex++;
  74. if (journal.getInventJournalType() == InventJournalType.Invent
  75. .getIntValue()) {
  76. HSSFRow inventHeaderRow = sheet.createRow(rowIndex);
  77. inventHeaderRow.setHeightInPoints(30);
  78. Cell cell = inventHeaderRow.createCell(0);
  79. cell.setCellValue("№");
  80. cell.setCellStyle(styles.get("header"));
  81. cell = inventHeaderRow.createCell(1);
  82. cell.setCellValue("Код");
  83. cell.setCellStyle(styles.get("header"));
  84. cell = inventHeaderRow.createCell(2);
  85. cell.setCellValue("Наименование");
  86. cell.setCellStyle(styles.get("header"));
  87. cell = inventHeaderRow.createCell(3);
  88. cell.setCellValue("Склад");
  89. cell.setCellStyle(styles.get("header"));
  90. cell = inventHeaderRow.createCell(4);
  91. cell.setCellValue("Док. количество");
  92. cell.setCellStyle(styles.get("header"));
  93. cell = inventHeaderRow.createCell(5);
  94. cell.setCellValue("Факт. количество");
  95. cell.setCellStyle(styles.get("header"));
  96. cell = inventHeaderRow.createCell(6);
  97. cell.setCellValue("Расхождение");
  98. cell.setCellStyle(styles.get("header"));
  99. cell = inventHeaderRow.createCell(7);
  100. cell.setCellValue("Ед. изм.");
  101. cell.setCellStyle(styles.get("header"));
  102. rowIndex++;
  103. for (InventJournalTrans trans : journalTrans) {
  104. HSSFRow currentRow = sheet.createRow(rowIndex);
  105. currentRow.setHeightInPoints(30);
  106. cell = currentRow.createCell(0);
  107. cell.setCellStyle(styles.get("cell"));
  108. cell.setCellValue(trans.getLineNum());
  109. cell = currentRow.createCell(1);
  110. cell.setCellStyle(styles.get("cell"));
  111. cell.setCellValue(trans.getItemId());
  112. cell = currentRow.createCell(2);
  113. cell.setCellStyle(styles.get("cell"));
  114. cell.setCellValue(trans.getItem().getName());
  115. cell = currentRow.createCell(3);
  116. cell.setCellStyle(styles.get("cell"));
  117. cell.setCellValue(trans.getInventDimTo().getInventLocationId());
  118. cell = currentRow.createCell(4);
  119. cell.setCellStyle(styles.get("cell"));
  120. cell.setCellValue(trans.getExpectQty());
  121. cell = currentRow.createCell(5);
  122. cell.setCellStyle(styles.get("cell"));
  123. cell.setCellValue(trans.getQty());
  124. cell = currentRow.createCell(6);
  125. cell.setCellStyle(styles.get("cell"));
  126. cell.setCellValue(trans.getQty() - trans.getExpectQty());
  127. cell = currentRow.createCell(7);
  128. cell.setCellStyle(styles.get("cell"));
  129. cell.setCellValue(trans.getItem().getUOMId());
  130. rowIndex++;
  131. }
  132. } else if (journal.getInventJournalType() == InventJournalType.Transit
  133. .getIntValue()) {
  134. HSSFRow inventHeaderRow = sheet.createRow(rowIndex);
  135. inventHeaderRow.setHeightInPoints(30);
  136. Cell cell = inventHeaderRow.createCell(0);
  137. cell.setCellValue("№");
  138. cell.setCellStyle(styles.get("header"));
  139. cell = inventHeaderRow.createCell(1);
  140. cell.setCellValue("Код");
  141. cell.setCellStyle(styles.get("header"));
  142. cell = inventHeaderRow.createCell(2);
  143. cell.setCellValue("Наименование");
  144. cell.setCellStyle(styles.get("header"));
  145. cell = inventHeaderRow.createCell(3);
  146. cell.setCellValue("Скл. списания");
  147. cell.setCellStyle(styles.get("header"));
  148. cell = inventHeaderRow.createCell(4);
  149. cell.setCellValue("Скл. назначения");
  150. cell.setCellStyle(styles.get("header"));
  151. cell = inventHeaderRow.createCell(5);
  152. cell.setCellValue("Количество");
  153. cell.setCellStyle(styles.get("header"));
  154. cell = inventHeaderRow.createCell(6);
  155. cell.setCellValue("Ед. изм.");
  156. cell.setCellStyle(styles.get("header"));
  157. rowIndex++;
  158. for (InventJournalTrans trans : journalTrans) {
  159. HSSFRow currentRow = sheet.createRow(rowIndex);
  160. currentRow.setHeightInPoints(30);
  161. cell = currentRow.createCell(0);
  162. cell.setCellStyle(styles.get("cell"));
  163. cell.setCellValue(trans.getLineNum());
  164. cell = currentRow.createCell(1);
  165. cell.setCellStyle(styles.get("cell"));
  166. cell.setCellValue(trans.getItemId());
  167. cell = currentRow.createCell(2);
  168. cell.setCellStyle(styles.get("cell"));
  169. cell.setCellValue(trans.getItem().getName());
  170. cell = currentRow.createCell(3);
  171. cell.setCellStyle(styles.get("cell"));
  172. cell.setCellValue(trans.getInventDimFrom()
  173. .getInventLocationId());
  174. cell = currentRow.createCell(4);
  175. cell.setCellStyle(styles.get("cell"));
  176. cell.setCellValue(trans.getInventDimTo().getInventLocationId());
  177. cell = currentRow.createCell(5);
  178. cell.setCellStyle(styles.get("cell"));
  179. cell.setCellValue(trans.getQty());
  180. cell = currentRow.createCell(6);
  181. cell.setCellStyle(styles.get("cell"));
  182. cell.setCellValue(trans.getItem().getUOMId());
  183. rowIndex++;
  184. }
  185. }
  186. else {
  187. HSSFRow inventHeaderRow = sheet.createRow(rowIndex);
  188. Cell cell = inventHeaderRow.createCell(0);
  189. inventHeaderRow.setHeightInPoints(30);
  190. cell.setCellValue("№");
  191. cell.setCellStyle(styles.get("header"));
  192. cell = inventHeaderRow.createCell(1);
  193. cell.setCellValue("Код");
  194. cell.setCellStyle(styles.get("header"));
  195. cell = inventHeaderRow.createCell(2);
  196. cell.setCellValue("Наименование");
  197. cell.setCellStyle(styles.get("header"));
  198. cell = inventHeaderRow.createCell(3);
  199. cell.setCellValue("Склад");
  200. cell.setCellStyle(styles.get("header"));
  201. cell = inventHeaderRow.createCell(4);
  202. cell.setCellValue("Количество");
  203. cell.setCellStyle(styles.get("header"));
  204. cell = inventHeaderRow.createCell(5);
  205. cell.setCellValue("Ед. изм.");
  206. cell.setCellStyle(styles.get("header"));
  207. rowIndex++;
  208. for (InventJournalTrans trans : journalTrans) {
  209. HSSFRow currentRow = sheet.createRow(rowIndex);
  210. currentRow.setHeightInPoints(30);
  211. cell = currentRow.createCell(0);
  212. cell.setCellStyle(styles.get("cell"));
  213. cell.setCellValue(trans.getLineNum());
  214. cell = currentRow.createCell(1);
  215. cell.setCellStyle(styles.get("cell"));
  216. cell.setCellValue(trans.getItemId());
  217. cell = currentRow.createCell(2);
  218. cell.setCellStyle(styles.get("cell"));
  219. cell.setCellValue(trans.getItem().getName());
  220. cell = currentRow.createCell(3);
  221. cell.setCellStyle(styles.get("cell"));
  222. cell.setCellValue(trans.getInventDimTo().getInventLocationId());
  223. cell = currentRow.createCell(4);
  224. cell.setCellStyle(styles.get("cell"));
  225. cell.setCellValue(trans.getQty());
  226. cell = currentRow.createCell(5);
  227. cell.setCellStyle(styles.get("cell"));
  228. cell.setCellValue(trans.getItem().getUOMId());
  229. rowIndex++;
  230. }
  231. }
  232. sheet.autoSizeColumn(1);
  233. sheet.autoSizeColumn(2, false);
  234. sheet.setColumnWidth(2, 28 * 256);
  235. sheet.setColumnWidth(3, 18 * 256);
  236. sheet.setColumnWidth(4, 14 * 256);
  237. sheet.setColumnWidth(5, 14 * 256);
  238. sheet.setColumnWidth(6, 9 * 256);
  239. try {
  240. FileOutputStream fileOutputStream = new FileOutputStream(fileName);
  241. hwb.write(fileOutputStream);
  242. fileOutputStream.close();
  243. } catch (Exception e) {
  244. e.printStackTrace();
  245. }
  246. return fileName;
  247. }
  248. public String getPeriodReportFilename(Date start, Date finish,
  249. String inventLocationId, String dataAreaId) {
  250. SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY");
  251. String filename = "profitLoss(" + dateFormat.format(start) + " - "
  252. + dateFormat.format(finish) + ").xls";
  253. Map<String, Double> map = inventJournalTableDAO.getProfitLossInPeriod(
  254. start, finish, inventLocationId, dataAreaId);
  255. HSSFWorkbook hwb = new HSSFWorkbook();
  256. hwb.createInformationProperties();
  257. SummaryInformation summaryInfo = hwb.getSummaryInformation();
  258. summaryInfo.setAuthor("BMP-System");
  259. Map<String, CellStyle> styles = createStyles(hwb);
  260. HSSFSheet sheet = hwb.createSheet("Журнал");
  261. PrintSetup printSetup = sheet.getPrintSetup();
  262. printSetup.setLandscape(true);
  263. sheet.setFitToPage(true);
  264. sheet.setHorizontallyCenter(true);
  265. int rowIndex = 0;
  266. addHeaderRow(styles.get("title"), sheet, rowIndex,
  267. "Отчет по суммарным убыткам/излишкам");
  268. rowIndex++;
  269. addHeaderRow(styles.get("title"), sheet, rowIndex, "По складу "
  270. + inventLocationId);
  271. rowIndex++;
  272. addHeaderRow(styles.get("title"), sheet, rowIndex, "За период с "
  273. + dateFormat.format(start) + " по " + dateFormat.format(finish));
  274. rowIndex++;
  275. addFourColumnTableRow(styles.get("header"), sheet, rowIndex, "Код",
  276. "Наименование", "Количество", "Ед. изм");
  277. rowIndex++;
  278. Iterator<Entry<String, Double>> iterator = map.entrySet().iterator();
  279. for (int i = 0; i < map.size(); i++) {
  280. Entry<String, Double> entry = iterator.next();
  281. String[] strings = entry.getKey().split("\\;");
  282. addFourColumnTableRow(styles.get("cell"), sheet, rowIndex,
  283. strings[0], strings[1], entry.getValue(), strings[2]);
  284. rowIndex++;
  285. }
  286. try {
  287. FileOutputStream fileOutputStream = new FileOutputStream(filename);
  288. hwb.write(fileOutputStream);
  289. fileOutputStream.close();
  290. } catch (Exception e) {
  291. e.printStackTrace();
  292. }
  293. return filename;
  294. }
  295. protected void addFourColumnTableRow(CellStyle style, HSSFSheet sheet,
  296. int rowIndex, String col1, String col2, String col3, String col4) {
  297. HSSFRow tableHeaderRow = sheet.createRow(rowIndex);
  298. tableHeaderRow.setHeightInPoints(30);
  299. Cell cell = tableHeaderRow.createCell(0);
  300. cell.setCellValue(col1);
  301. cell.setCellStyle(style);
  302. fillEmptyCells(tableHeaderRow, style, 1, 1);
  303. cell = tableHeaderRow.createCell(2);
  304. cell.setCellValue(col2);
  305. cell.setCellStyle(style);
  306. fillEmptyCells(tableHeaderRow, style, 3, 4);
  307. cell = tableHeaderRow.createCell(5);
  308. cell.setCellValue(col3);
  309. cell.setCellStyle(style);
  310. fillEmptyCells(tableHeaderRow, style, 6, 6);
  311. cell = tableHeaderRow.createCell(7);
  312. cell.setCellValue(col4);
  313. cell.setCellStyle(style);
  314. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$" + (rowIndex + 1)
  315. + ":$B$" + (rowIndex + 1)));
  316. sheet.addMergedRegion(CellRangeAddress.valueOf("$C$" + (rowIndex + 1)
  317. + ":$E$" + (rowIndex + 1)));
  318. sheet.addMergedRegion(CellRangeAddress.valueOf("$F$" + (rowIndex + 1)
  319. + ":$G$" + (rowIndex + 1)));
  320. }
  321. protected void addFourColumnTableRow(CellStyle style, HSSFSheet sheet,
  322. int rowIndex, String col1, String col2, double col3, String col4) {
  323. HSSFRow tableHeaderRow = sheet.createRow(rowIndex);
  324. tableHeaderRow.setHeightInPoints(30);
  325. Cell cell = tableHeaderRow.createCell(0);
  326. cell.setCellValue(col1);
  327. cell.setCellStyle(style);
  328. fillEmptyCells(tableHeaderRow, style, 1, 1);
  329. cell = tableHeaderRow.createCell(2);
  330. cell.setCellValue(col2);
  331. cell.setCellStyle(style);
  332. fillEmptyCells(tableHeaderRow, style, 3, 4);
  333. cell = tableHeaderRow.createCell(5);
  334. cell.setCellValue(col3);
  335. cell.setCellStyle(style);
  336. fillEmptyCells(tableHeaderRow, style, 6, 6);
  337. cell = tableHeaderRow.createCell(7);
  338. cell.setCellValue(col4);
  339. cell.setCellStyle(style);
  340. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$" + (rowIndex + 1)
  341. + ":$B$" + (rowIndex + 1)));
  342. sheet.addMergedRegion(CellRangeAddress.valueOf("$C$" + (rowIndex + 1)
  343. + ":$E$" + (rowIndex + 1)));
  344. sheet.addMergedRegion(CellRangeAddress.valueOf("$F$" + (rowIndex + 1)
  345. + ":$G$" + (rowIndex + 1)));
  346. }
  347. protected void addTwoColumnTableRow(CellStyle style, HSSFSheet sheet,
  348. int rowIndex, String col1, String col2) {
  349. HSSFRow tableHeaderRow = sheet.createRow(rowIndex);
  350. tableHeaderRow.setHeightInPoints(30);
  351. Cell cell = tableHeaderRow.createCell(0);
  352. cell.setCellValue(col1);
  353. cell.setCellStyle(style);
  354. fillEmptyCells(tableHeaderRow, style, 1, 3);
  355. cell = tableHeaderRow.createCell(4);
  356. cell.setCellValue(col2);
  357. cell.setCellStyle(style);
  358. fillEmptyCells(tableHeaderRow, style, 5, 7);
  359. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$" + (rowIndex + 1)
  360. + ":$D$" + (rowIndex + 1)));
  361. sheet.addMergedRegion(CellRangeAddress.valueOf("$E$" + (rowIndex + 1)
  362. + ":$H$" + (rowIndex + 1)));
  363. }
  364. protected void addTwoColumnTableRow(CellStyle style, HSSFSheet sheet,
  365. int rowIndex, String col1, double col2) {
  366. HSSFRow tableHeaderRow = sheet.createRow(rowIndex);
  367. tableHeaderRow.setHeightInPoints(30);
  368. Cell cell = tableHeaderRow.createCell(0);
  369. cell.setCellValue(col1);
  370. cell.setCellStyle(style);
  371. fillEmptyCells(tableHeaderRow, style, 1, 3);
  372. cell = tableHeaderRow.createCell(4);
  373. cell.setCellValue(col2);
  374. cell.setCellStyle(style);
  375. fillEmptyCells(tableHeaderRow, style, 5, 7);
  376. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$" + (rowIndex + 1)
  377. + ":$D$" + (rowIndex + 1)));
  378. sheet.addMergedRegion(CellRangeAddress.valueOf("$E$" + (rowIndex + 1)
  379. + ":$H$" + (rowIndex + 1)));
  380. }
  381. }