Se reescribio el informe de egresos

Para lograrlo se realizo el mismo procedimiento que con el informe de
libro de ventas

Primero que nada se elmino el objeto de InformeEgresosContent que se
tenia porque realmente no aportaba nada, era basicamente lo mismo que el
objeto egresos con un campo de fecha añadido y creo que un campo menos.
Por lo que se paso a utilizar el objeto de Egresos directamente.

Debido a lo anterior no era necesario tener una query especial para
juntar los datos, ya que solo eran los egresos de todas las cajas de un
tipo en especifico, por lo que solo se hizo una query nueva en el
EgresoDAO en el que se satisfacia esta necesidad.

Luego fue solo hacer compatible el InformeEgresosToExcel y listo!
This commit is contained in:
Daniel Cortes
2019-03-06 23:49:07 -03:00
parent d1d2f9c2fa
commit 6ea175f06a
10 changed files with 361 additions and 520 deletions

BIN
dist/Programa Caja.jar vendored

Binary file not shown.

View File

@@ -25,7 +25,7 @@
package danielcortes.xyz.controllers; package danielcortes.xyz.controllers;
import danielcortes.xyz.data.Configuration; import danielcortes.xyz.data.Configuration;
import danielcortes.xyz.informes.InformeEgresos; import danielcortes.xyz.informes.InformeEgresosToExcel;
import danielcortes.xyz.informes.InformeLibroDeVentasToExcel; import danielcortes.xyz.informes.InformeLibroDeVentasToExcel;
import danielcortes.xyz.models.tipo_egreso.TipoEgreso; import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
import danielcortes.xyz.utils.SaveFile; import danielcortes.xyz.utils.SaveFile;
@@ -96,7 +96,7 @@ public class InformesSideBarController {
return; return;
} }
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM YYYY"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy");
String formatedMonth = month.format(formatter); String formatedMonth = month.format(formatter);
Path saveFile = new XLSFileChooser( Path saveFile = new XLSFileChooser(
@@ -106,8 +106,9 @@ public class InformesSideBarController {
return; return;
} }
InformeEgresos informe = new InformeEgresos(tipoEgreso.getId(), month, saveFile); InformeEgresosToExcel informe = new InformeEgresosToExcel(tipoEgreso, month);
Path generatedFile = informe.generarInforme(); Workbook wb = informe.generarInforme();
SaveFile.save(wb, saveFile);
new InformeGeneratedConfirmation(saveFile).execute(); new InformeGeneratedConfirmation(saveFile).execute();
} }

View File

@@ -36,8 +36,6 @@ import danielcortes.xyz.models.egreso.EgresoDAO;
import danielcortes.xyz.models.egreso.SQLiteEgresoDAO; import danielcortes.xyz.models.egreso.SQLiteEgresoDAO;
import danielcortes.xyz.models.estado_resultado.EstadoResultadoDAO; import danielcortes.xyz.models.estado_resultado.EstadoResultadoDAO;
import danielcortes.xyz.models.estado_resultado.SQLiteEstadoResultadoDAO; import danielcortes.xyz.models.estado_resultado.SQLiteEstadoResultadoDAO;
import danielcortes.xyz.models.informes.egresos.InformeEgresosContentDAO;
import danielcortes.xyz.models.informes.egresos.SQLiteInformeEgresosContentDAO;
import danielcortes.xyz.models.ingreso.IngresoDAO; import danielcortes.xyz.models.ingreso.IngresoDAO;
import danielcortes.xyz.models.ingreso.SQLiteIngresoDAO; import danielcortes.xyz.models.ingreso.SQLiteIngresoDAO;
import danielcortes.xyz.models.tipo_egreso.SQLiteTipoEgresoDAO; import danielcortes.xyz.models.tipo_egreso.SQLiteTipoEgresoDAO;
@@ -54,7 +52,6 @@ public class DAOManager {
private static final DocumentosDAO documentosDAO; private static final DocumentosDAO documentosDAO;
private static final EfectivoDAO efectivoDAO; private static final EfectivoDAO efectivoDAO;
private static final EgresoDAO egresoDAO; private static final EgresoDAO egresoDAO;
private static final InformeEgresosContentDAO egresosContentDAO;
private static final IngresoDAO ingresoDAO; private static final IngresoDAO ingresoDAO;
private static final TipoEgresoDAO tipoEgresoDAO; private static final TipoEgresoDAO tipoEgresoDAO;
private static final TipoIngresoDAO tipoIngresoDAO; private static final TipoIngresoDAO tipoIngresoDAO;
@@ -67,7 +64,6 @@ public class DAOManager {
documentosDAO = new SQLiteDocumentosDAO(); documentosDAO = new SQLiteDocumentosDAO();
efectivoDAO = new SQLiteEfectivoDAO(); efectivoDAO = new SQLiteEfectivoDAO();
egresoDAO = new SQLiteEgresoDAO(); egresoDAO = new SQLiteEgresoDAO();
egresosContentDAO = new SQLiteInformeEgresosContentDAO();
ingresoDAO = new SQLiteIngresoDAO(); ingresoDAO = new SQLiteIngresoDAO();
tipoEgresoDAO = new SQLiteTipoEgresoDAO(); tipoEgresoDAO = new SQLiteTipoEgresoDAO();
tipoIngresoDAO = new SQLiteTipoIngresoDAO(); tipoIngresoDAO = new SQLiteTipoIngresoDAO();
@@ -95,9 +91,6 @@ public class DAOManager {
return egresoDAO; return egresoDAO;
} }
public static InformeEgresosContentDAO getEgresosContentDAO() {
return egresosContentDAO;
}
public static IngresoDAO getIngresoDAO() { public static IngresoDAO getIngresoDAO() {
return ingresoDAO; return ingresoDAO;

View File

@@ -1,324 +1,54 @@
/*
* MIT License
*
* Copyright (c) 2018-2019 Daniel Cortes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package danielcortes.xyz.informes; package danielcortes.xyz.informes;
import danielcortes.xyz.models.caja.SQLiteCajaDAO; import danielcortes.xyz.data.DAOManager;
import danielcortes.xyz.models.informes.egresos.InformeEgresosContent; import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.informes.egresos.SQLiteInformeEgresosContentDAO; import danielcortes.xyz.models.caja.CajaDAO;
import danielcortes.xyz.utils.Pair; import danielcortes.xyz.models.egreso.Egreso;
import java.io.IOException; import danielcortes.xyz.models.egreso.EgresoDAO;
import java.io.OutputStream; import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
import java.nio.file.Files; import java.time.LocalDate;
import java.nio.file.Path;
import java.time.YearMonth; import java.time.YearMonth;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderExtent;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.PropertyTemplate;
public class InformeEgresos { public class InformeEgresos {
private final String[] titles = { private HashMap<LocalDate, List<Egreso>> informe;
"FECHA",
"",
"DESCRIPCION",
"VALOR"
};
private List<InformeEgresosContent> informe; private InformeEgresos() {
private Path saveFile;
//Filas donde se almacenaran los totales
private ArrayList<Row> totalRows;
//Filas donde se almacenaran los egresos.
private ArrayList<Row> dataRows;
//Fila que contiene el total final;
private Row totalFinal;
//Rango que corresponde a un dia de egresos;
private ArrayList<Pair<Integer, Integer>> totalRange;
private Workbook wb;
private Sheet sheet;
private CreationHelper createHelper;
private HashMap<String, CellStyle> styles;
public InformeEgresos(int tipoEgresoId, YearMonth mes, Path saveFile) {
new SQLiteCajaDAO().createCajasForMonth(mes);
this.informe = new SQLiteInformeEgresosContentDAO()
.getInformeEgresosFactuasMateriaPrima(mes, tipoEgresoId);
this.saveFile = saveFile;
this.wb = new HSSFWorkbook();
this.sheet = wb.createSheet();
this.createHelper = wb.getCreationHelper();
this.totalRows = new ArrayList<>();
this.totalRange = new ArrayList<>();
this.dataRows = new ArrayList<>();
this.styles = this.generateStyles();
} }
private void fillHeaders() { public List<Egreso> get(LocalDate localDate) {
Row titles = sheet.createRow(0); return informe.get(localDate);
for (int x = 0; x < this.titles.length; x++) {
titles.createCell(x).setCellValue(this.titles[x]);
}
} }
private void fillData() { private void put(LocalDate localDate, List<Egreso> egresos) {
int rowCounter = 1; informe.put(localDate, egresos);
int dayStart = rowCounter;
int dayEnd;
for (int informeID = 0; informeID < informe.size(); informeID++) {
InformeEgresosContent data = informe.get(informeID);
int cellCounter = 0;
Row dataRow = sheet.createRow(rowCounter);
dataRows.add(dataRow);
Date fecha = Date.from(data.getFecha().atStartOfDay(ZoneId.systemDefault()).toInstant());
dataRow.createCell(cellCounter++).setCellValue(fecha);
dataRow.createCell(cellCounter++).setCellValue(data.getNro());
dataRow.createCell(cellCounter++).setCellValue(data.getDescripcion());
dataRow.createCell(cellCounter).setCellValue(data.getValor());
// Comprueba si es que el siguiente elemento en la lista de informe es del siguiente dia, si es asi, agrega una linea con los totales diarios.
if (informeID + 1 >= informe.size() || !data.getFecha()
.equals(informe.get(informeID + 1).getFecha())) {
totalRows.add(sheet.createRow(rowCounter + 1));
dayEnd = rowCounter;
totalRange.add(new Pair<>(dayStart, dayEnd));
dayStart = rowCounter + 2;
rowCounter++;
}
rowCounter++;
}
} }
private void fillTotales() { public int size(){
StringBuilder sumTotalFinal = new StringBuilder(); return informe.size();
//Se aprovechara la iteracion para agregar los totales individuales y para construir la suma del total final
for (int x = 0; x < totalRows.size(); x++) {
Row row = totalRows.get(x);
Pair<Integer, Integer> range = totalRange.get(x);
row.createCell(2).setCellValue("TOTAL DIARIO");
row.createCell(3)
.setCellFormula("SUM(D" + (range.getLeft() + 1) + ":D" + (range.getRight() + 1) + ")");
sumTotalFinal.append("D").append(row.getRowNum() + 1);
if (x + 1 != totalRows.size()) {
sumTotalFinal.append("+");
} else {
this.totalFinal = sheet.createRow(row.getRowNum() + 1);
this.totalFinal.createCell(2).setCellValue("TOTAL MENSUAL");
this.totalFinal.createCell(3).setCellFormula(sumTotalFinal.toString());
}
}
} }
private void freezeCells() { public static InformeEgresos generate(YearMonth mes, TipoEgreso tipoEgreso) {
this.sheet.createFreezePane(0, 1); InformeEgresos informeEgresos = new InformeEgresos();
} informeEgresos.informe = new HashMap<>();
private void addBorders() { LocalDate currentDate = mes.atDay(1);
int rows = this.totalRows.size() + this.dataRows.size() + 1; LocalDate endDatePlusOne = mes.atEndOfMonth().plusDays(1);
PropertyTemplate pt = new PropertyTemplate();
//Bordes Internos CajaDAO cajaDAO = DAOManager.getCajaDAO();
pt.drawBorders(new CellRangeAddress(0, rows, 0, 3), BorderStyle.THIN, BorderExtent.ALL); EgresoDAO egresoDAO = DAOManager.getEgresoDAO();
//Bordes de los Headers while (currentDate.isBefore(endDatePlusOne)) {
pt.drawBorders(new CellRangeAddress(0, 0, 0, 3), BorderStyle.MEDIUM, BorderExtent.OUTSIDE); Caja caja = cajaDAO.getByFecha(currentDate).orElse(Caja.EMPTY);
//Bordes de los Totales List<Egreso> egresos = egresoDAO.getByTipoEgresoEnCaja(tipoEgreso, caja);
for (Row row : this.totalRows) { informeEgresos.put(currentDate, egresos);
int index = row.getRowNum();
pt.drawBorders(new CellRangeAddress(index, index, 0, 3), BorderStyle.NONE, BorderExtent.ALL); currentDate = currentDate.plusDays(1);
pt.drawBorders(new CellRangeAddress(index, index, 0, 3), BorderStyle.MEDIUM,
BorderExtent.OUTSIDE);
} }
//Borde del Total Final return informeEgresos;
pt.drawBorders(
new CellRangeAddress(this.totalFinal.getRowNum(), this.totalFinal.getRowNum(), 0, 3),
BorderStyle.NONE, BorderExtent.ALL);
pt.drawBorders(
new CellRangeAddress(this.totalFinal.getRowNum(), this.totalFinal.getRowNum(), 0, 3),
BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
//Borde externo
pt.drawBorders(new CellRangeAddress(0, rows, 0, 3), BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
pt.applyBorders(this.sheet);
}
private void setStyles() {
//Estilo para el header
this.sheet.getRow(0).getCell(0).setCellStyle(this.styles.get("header"));
this.sheet.getRow(0).getCell(1).setCellStyle(this.styles.get("header"));
this.sheet.getRow(0).getCell(2).setCellStyle(this.styles.get("header"));
this.sheet.getRow(0).getCell(3).setCellStyle(this.styles.get("header"));
//Estilo para el Total Final
this.totalFinal.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("gray"));
this.totalFinal.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("gray"));
this.totalFinal.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("gray"));
this.totalFinal.getCell(3).setCellStyle(this.styles.get("total_final"));
//Estilo para las filas de datos
for (Row row : this.dataRows) {
row.getCell(0).setCellStyle(this.styles.get("date"));
row.getCell(1).setCellStyle(this.styles.get("regular"));
row.getCell(2).setCellStyle(this.styles.get("regular"));
row.getCell(3).setCellStyle(this.styles.get("money"));
}
//Estilo para las filas de totales
for (Row row : this.totalRows) {
row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("not_so_gray"));
row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("not_so_gray"));
row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("not_so_gray"));
row.getCell(3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("total"));
}
//Setea el alto de las filas
this.sheet.getRow(0).setHeightInPoints(30);
this.totalFinal.setHeightInPoints(20);
for (Row row : this.dataRows) {
row.setHeightInPoints(15);
}
for (Row row : this.totalRows) {
row.setHeightInPoints(18);
}
//Setea el ancho de las columnas
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);
sheet.autoSizeColumn(3);
}
private HashMap<String, CellStyle> generateStyles() {
Font font = this.wb.createFont();
font.setBold(true);
font.setColor(IndexedColors.WHITE.getIndex());
CellStyle regularStyle = this.wb.createCellStyle();
CellStyle grayStyle = this.wb.createCellStyle();
grayStyle.setFont(font);
grayStyle.setVerticalAlignment(VerticalAlignment.CENTER);
grayStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
grayStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle notSoGrayStyle = this.wb.createCellStyle();
notSoGrayStyle.setFont(font);
notSoGrayStyle.setVerticalAlignment(VerticalAlignment.CENTER);
notSoGrayStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
notSoGrayStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle dateStyle = this.wb.createCellStyle();
dateStyle.setDataFormat(this.createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
CellStyle moneyStyle = this.wb.createCellStyle();
moneyStyle
.setDataFormat(this.createHelper.createDataFormat().getFormat("\"$\"#,##0_);(\"$\"#,##0)"));
CellStyle headerStyle = this.wb.createCellStyle();
headerStyle.cloneStyleFrom(grayStyle);
headerStyle.setAlignment(HorizontalAlignment.CENTER);
CellStyle totalStyle = this.wb.createCellStyle();
totalStyle.cloneStyleFrom(notSoGrayStyle);
totalStyle
.setDataFormat(this.createHelper.createDataFormat().getFormat("\"$\"#,##0_);(\"$\"#,##0)"));
CellStyle totalFinalStyle = this.wb.createCellStyle();
totalFinalStyle.cloneStyleFrom(grayStyle);
totalFinalStyle
.setDataFormat(this.createHelper.createDataFormat().getFormat("\"$\"#,##0_);(\"$\"#,##0)"));
HashMap<String, CellStyle> styles = new HashMap<>();
styles.put("regular", regularStyle);
styles.put("gray", grayStyle);
styles.put("not_so_gray", notSoGrayStyle);
styles.put("date", dateStyle);
styles.put("money", moneyStyle);
styles.put("header", headerStyle);
styles.put("total", totalStyle);
styles.put("total_final", totalFinalStyle);
return styles;
}
public Path generarInforme() {
fillHeaders();
fillData();
fillTotales();
freezeCells();
setStyles();
addBorders();
try (OutputStream fileOut = Files.newOutputStream(this.saveFile)) {
wb.write(fileOut);
return this.saveFile;
} catch (IOException e) {
e.printStackTrace();
}
return null;
} }
} }

View File

@@ -0,0 +1,302 @@
/*
* MIT License
*
* Copyright (c) 2018-2019 Daniel Cortes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package danielcortes.xyz.informes;
import danielcortes.xyz.models.egreso.Egreso;
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
import danielcortes.xyz.utils.Pair;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderExtent;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.PropertyTemplate;
public class InformeEgresosToExcel {
private final String[] titles = {
"FECHA",
"",
"DESCRIPCION",
"VALOR"
};
private InformeEgresos informe;
private YearMonth mes;
//Filas donde se almacenaran los totales
private ArrayList<Row> totalRows;
//Filas donde se almacenaran los egresos.
private ArrayList<Row> dataRows;
//Fila que contiene el total final;
private Row totalFinal;
//Rango que corresponde a un dia de egresos;
private ArrayList<Pair<Integer, Integer>> totalRange;
private Workbook wb;
private Sheet sheet;
private CreationHelper createHelper;
private HashMap<String, CellStyle> styles;
public InformeEgresosToExcel(TipoEgreso tipoEgreso, YearMonth mes) {
this.informe = InformeEgresos.generate(mes, tipoEgreso);
this.mes = mes;
this.wb = new HSSFWorkbook();
this.sheet = wb.createSheet();
this.createHelper = wb.getCreationHelper();
this.totalRows = new ArrayList<>();
this.totalRange = new ArrayList<>();
this.dataRows = new ArrayList<>();
this.styles = this.generateStyles();
}
private void fillHeaders() {
Row titles = sheet.createRow(0);
for (int x = 0; x < this.titles.length; x++) {
titles.createCell(x).setCellValue(this.titles[x]);
}
}
private void fillData() {
int currentRow = 1;
LocalDate currentDay = mes.atDay(1);
LocalDate endDayPlusOne = mes.atEndOfMonth().plusDays(1);
while (currentDay.isBefore(endDayPlusOne)) {
int firstRow = currentRow;
Date fecha = Date.from(currentDay.atStartOfDay(ZoneId.systemDefault()).toInstant());
List<Egreso> egresos = informe.get(currentDay);
for (Egreso egreso : egresos) {
Row dataRow = sheet.createRow(currentRow);
this.dataRows.add(dataRow);
dataRow.createCell(0).setCellValue(fecha);
dataRow.createCell(1).setCellValue(egreso.getNro());
dataRow.createCell(2).setCellValue(egreso.getDescripcion());
dataRow.createCell(3).setCellValue(egreso.getValor());
currentRow++;
}
int lastRow = currentRow - 1;
if (egresos.size() > 0) {
Row totalRow = sheet.createRow(currentRow++);
totalRows.add(totalRow);
totalRow.createCell(2).setCellValue("TOTAL DIARIO");
totalRow.createCell(3)
.setCellFormula("SUM(D" + (firstRow + 1) + ":D" + (lastRow + 1) + ")");
}
currentDay = currentDay.plusDays(1);
}
}
private void fillTotales() {
String operation = totalRows.stream().map(row -> "D" + (row.getRowNum() + 1))
.reduce((a, b) -> String.join("+", a, b)).get();
this.totalFinal = sheet.createRow(this.dataRows.get(this.dataRows.size()-1).getRowNum() + 2);
this.totalFinal.createCell(2).setCellValue("TOTAL MENSUAL");
this.totalFinal.createCell(3).setCellFormula(operation);
}
private void freezeCells() {
this.sheet.createFreezePane(0, 1);
}
private void addBorders() {
int rows = this.totalRows.size() + this.dataRows.size() + 1;
PropertyTemplate pt = new PropertyTemplate();
//Bordes Internos
pt.drawBorders(new CellRangeAddress(0, rows, 0, 3), BorderStyle.THIN, BorderExtent.ALL);
//Bordes de los Headers
pt.drawBorders(new CellRangeAddress(0, 0, 0, 3), BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
//Bordes de los Totales
for (Row row : this.totalRows) {
int index = row.getRowNum();
pt.drawBorders(new CellRangeAddress(index, index, 0, 3), BorderStyle.NONE, BorderExtent.ALL);
pt.drawBorders(new CellRangeAddress(index, index, 0, 3), BorderStyle.MEDIUM,
BorderExtent.OUTSIDE);
}
//Borde del Total Final
pt.drawBorders(
new CellRangeAddress(this.totalFinal.getRowNum(), this.totalFinal.getRowNum(), 0, 3),
BorderStyle.NONE, BorderExtent.ALL);
pt.drawBorders(
new CellRangeAddress(this.totalFinal.getRowNum(), this.totalFinal.getRowNum(), 0, 3),
BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
//Borde externo
pt.drawBorders(new CellRangeAddress(0, rows, 0, 3), BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
pt.applyBorders(this.sheet);
}
private void setStyles() {
//Estilo para el header
this.sheet.getRow(0).getCell(0).setCellStyle(this.styles.get("header"));
this.sheet.getRow(0).getCell(1).setCellStyle(this.styles.get("header"));
this.sheet.getRow(0).getCell(2).setCellStyle(this.styles.get("header"));
this.sheet.getRow(0).getCell(3).setCellStyle(this.styles.get("header"));
//Estilo para el Total Final
this.totalFinal.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("gray"));
this.totalFinal.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("gray"));
this.totalFinal.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("gray"));
this.totalFinal.getCell(3).setCellStyle(this.styles.get("total_final"));
//Estilo para las filas de datos
for (Row row : this.dataRows) {
row.getCell(0).setCellStyle(this.styles.get("date"));
row.getCell(1).setCellStyle(this.styles.get("regular"));
row.getCell(2).setCellStyle(this.styles.get("regular"));
row.getCell(3).setCellStyle(this.styles.get("money"));
}
//Estilo para las filas de totales
for (Row row : this.totalRows) {
row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("not_so_gray"));
row.getCell(1, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("not_so_gray"));
row.getCell(2, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("not_so_gray"));
row.getCell(3, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK)
.setCellStyle(this.styles.get("total"));
}
//Setea el alto de las filas
this.sheet.getRow(0).setHeightInPoints(30);
this.totalFinal.setHeightInPoints(20);
for (Row row : this.dataRows) {
row.setHeightInPoints(15);
}
for (Row row : this.totalRows) {
row.setHeightInPoints(18);
}
//Setea el ancho de las columnas
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);
sheet.autoSizeColumn(3);
}
private HashMap<String, CellStyle> generateStyles() {
Font font = this.wb.createFont();
font.setBold(true);
font.setColor(IndexedColors.WHITE.getIndex());
CellStyle regularStyle = this.wb.createCellStyle();
CellStyle grayStyle = this.wb.createCellStyle();
grayStyle.setFont(font);
grayStyle.setVerticalAlignment(VerticalAlignment.CENTER);
grayStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
grayStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle notSoGrayStyle = this.wb.createCellStyle();
notSoGrayStyle.setFont(font);
notSoGrayStyle.setVerticalAlignment(VerticalAlignment.CENTER);
notSoGrayStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
notSoGrayStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle dateStyle = this.wb.createCellStyle();
dateStyle.setDataFormat(this.createHelper.createDataFormat().getFormat("dd/mm/yyyy"));
CellStyle moneyStyle = this.wb.createCellStyle();
moneyStyle
.setDataFormat(this.createHelper.createDataFormat().getFormat("\"$\"#,##0_);(\"$\"#,##0)"));
CellStyle headerStyle = this.wb.createCellStyle();
headerStyle.cloneStyleFrom(grayStyle);
headerStyle.setAlignment(HorizontalAlignment.CENTER);
CellStyle totalStyle = this.wb.createCellStyle();
totalStyle.cloneStyleFrom(notSoGrayStyle);
totalStyle
.setDataFormat(this.createHelper.createDataFormat().getFormat("\"$\"#,##0_);(\"$\"#,##0)"));
CellStyle totalFinalStyle = this.wb.createCellStyle();
totalFinalStyle.cloneStyleFrom(grayStyle);
totalFinalStyle
.setDataFormat(this.createHelper.createDataFormat().getFormat("\"$\"#,##0_);(\"$\"#,##0)"));
HashMap<String, CellStyle> styles = new HashMap<>();
styles.put("regular", regularStyle);
styles.put("gray", grayStyle);
styles.put("not_so_gray", notSoGrayStyle);
styles.put("date", dateStyle);
styles.put("money", moneyStyle);
styles.put("header", headerStyle);
styles.put("total", totalStyle);
styles.put("total_final", totalFinalStyle);
return styles;
}
public Workbook generarInforme() {
fillHeaders();
fillData();
fillTotales();
freezeCells();
setStyles();
addBorders();
return this.wb;
}
}

View File

@@ -55,6 +55,8 @@ public abstract class EgresoDAO {
public abstract List<Egreso> findByTipoEgreso(TipoEgreso tipoEgreso); public abstract List<Egreso> findByTipoEgreso(TipoEgreso tipoEgreso);
public abstract List<Egreso> getByTipoEgresoEnCaja(TipoEgreso tipoEgreso, Caja caja);
public abstract boolean insertEgreso(Egreso egreso); public abstract boolean insertEgreso(Egreso egreso);
public abstract boolean updateEgreso(Egreso egreso); public abstract boolean updateEgreso(Egreso egreso);

View File

@@ -150,6 +150,29 @@ public class SQLiteEgresoDAO extends EgresoDAO {
return egresoList; return egresoList;
} }
@Override
public List<Egreso> getByTipoEgresoEnCaja(TipoEgreso tipoEgreso, Caja caja) {
List<Egreso> egresoList = new ArrayList<>();
if (caja == Caja.EMPTY) {
return egresoList;
}
String query = "select * from egresos where tipo_egreso_id = ? and caja_id = ?";
try (Connection conn = connectionHolder.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setInt(1, tipoEgreso.getId());
ps.setInt(2, caja.getId());
try (ResultSet rs = ps.executeQuery()) {
egresoList = this.egresosFromResultSet(rs);
}
}
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return egresoList;
}
@Override @Override
public boolean insertEgreso(Egreso egreso) { public boolean insertEgreso(Egreso egreso) {
int updates; int updates;

View File

@@ -1,77 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2018-2019 Daniel Cortes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package danielcortes.xyz.models.informes.egresos;
import java.time.LocalDate;
public class InformeEgresosContent {
private LocalDate fecha;
private String nro;
private String descripcion;
private int valor;
public LocalDate getFecha() {
return fecha;
}
public void setFecha(LocalDate fecha) {
this.fecha = fecha;
}
public String getNro() {
return nro;
}
public void setNro(String nro) {
this.nro = nro;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public int getValor() {
return valor;
}
public void setValor(int valor) {
this.valor = valor;
}
@Override
public String toString() {
return "InformeEgresosContent{" +
"fecha=" + fecha +
", nro='" + nro + '\'' +
", descripcion='" + descripcion + '\'' +
", valor=" + valor +
'}';
}
}

View File

@@ -1,43 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2018-2019 Daniel Cortes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package danielcortes.xyz.models.informes.egresos;
import danielcortes.xyz.data.ConnectionHolder;
import java.time.YearMonth;
import java.util.List;
public abstract class InformeEgresosContentDAO {
protected ConnectionHolder connectionHolder;
/**
* Genera el informe con nombre muy largo
*
* @param mes mes sobre el cual se quiere le informe
* @return lista del objeto que contiene los datos necesarios para el informe
*/
public abstract List<InformeEgresosContent> getInformeEgresosFactuasMateriaPrima(YearMonth mes,
int tipoEgresoId);
}

View File

@@ -1,90 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2018-2019 Daniel Cortes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package danielcortes.xyz.models.informes.egresos;
import danielcortes.xyz.data.SQLiteConnectionHolder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLiteInformeEgresosContentDAO extends InformeEgresosContentDAO {
private static final Logger LOGGER = Logger.getLogger(InformeEgresosContentDAO.class.getName());
private List<InformeEgresosContent> list;
public SQLiteInformeEgresosContentDAO() {
this.connectionHolder = new SQLiteConnectionHolder();
}
@Override
public List<InformeEgresosContent> getInformeEgresosFactuasMateriaPrima(YearMonth mes,
int tipoEgresoId) {
list = new ArrayList<>();
try (Connection conn = connectionHolder.getConnection()) {
String query = "select caja.fecha as \"fecha\"," +
"egresos.nro as \"nro\"," +
"egresos.descripcion as \"descripcion\"," +
"egresos.valor as \"valor\"" +
"from egresos inner join caja on egresos.caja_id = caja.id " +
"where caja.fecha between date(?) and date(?) and egresos.tipo_egreso_id = ? " +
"order by caja.fecha;";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, mes.atDay(1).toString());
ps.setString(2, mes.atEndOfMonth().toString());
ps.setInt(3, tipoEgresoId);
ResultSet rs = ps.executeQuery();
LOGGER.log(Level.FINE, "QUERY: {0} | values: [{1},{2},{3}]",
new Object[]{mes.atDay(1), mes.atEndOfMonth(), tipoEgresoId});
this.fillInforme(rs);
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
}
return list;
}
private void fillInforme(ResultSet rs) throws SQLException {
while (rs.next()) {
InformeEgresosContent content = new InformeEgresosContent();
content.setFecha(LocalDate.parse(rs.getString("fecha")));
content.setDescripcion(rs.getString("descripcion"));
content.setNro(rs.getString("nro"));
content.setValor(rs.getInt("valor"));
LOGGER.log(Level.FINER, "Se a creado: {0}", content);
list.add(content);
}
}
}