Primeramente, se tenia por objetivo reescribir el informe para que este fuera mas claro y para lograrlo se penso en que en vez de realizar 2 querys grandes en las que se tomaban todos los datos necesarios y se mapeaban un objeto el cual se añadia a un array, se realizaran multiples querys separadas, en las que se irian obteniendo los datos individualmente y sean juntados en un objeto. Para esto se debio reescribir parte de SQLiteIngresoDAO, por una parte, para añadir las querys que obtenian los numeros de boletas y de Z en una caja para un tipo de ingreso en ademas de la query para obtener el total de ingresos por un tipo de ingreso en un mes. Junto con esto, al toparme con un bug, reescribi el como se realizaban las querys en todo el objeto DAO, dado a que creia que se debia a un problema donde no se estaban cerrando bien los ResultSets y los PreparedStatement, aunque al final no fue eso y era simplemente el que no habia un resultado en la query que se habia realizado. A partir de aca no sabria bien como describir lo que se realizo, pero se puede resumir en que encontre redundante el tener 2 paquetes de informe y un objeto DAO para generar el informe cuando realmente no lo era, siendo mas un "builder" creo .w. Por lo que separe todo eso y lo deje en 3 objetos, el LibroDeVentas, el cual contiene la instancia de un dia de informe, el InformLibroDeVentas, el cual es un wraper para un hashmap que contiene el libro de ventas y ademas tiene el metodo que genera el libro, y finalmente el InformeLibroDeVentasToExcel, el cual pasa el informe a un archivo excel para la lectura del usuario final. Finalmente separe el que se guardara el informe automaticamente al generarlo. para ello cree un objeto aparte, el cual tendra metodos estaticos para todos los objetos que tenga que guardar eventulamente, por ahora como solo necesito guardar un Workbook, eso es lo que guarda. Creo que eso seria todo :3
378 lines
18 KiB
Java
378 lines
18 KiB
Java
package danielcortes.xyz.controllers;
|
|
|
|
import danielcortes.xyz.data.Configuration;
|
|
import danielcortes.xyz.data.DAOManager;
|
|
import danielcortes.xyz.informes.InformeEstadoResultado;
|
|
import danielcortes.xyz.models.estado_resultado.EstadoResultado;
|
|
import danielcortes.xyz.models.estado_resultado.EstadoResultadoDAO;
|
|
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
|
|
import danielcortes.xyz.utils.StringUtils;
|
|
import danielcortes.xyz.views.EstadoResultadoView;
|
|
import danielcortes.xyz.views.dialogs.InformeGeneratedConfirmation;
|
|
import danielcortes.xyz.views.dialogs.XLSFileChooser;
|
|
import danielcortes.xyz.views.listeners.FocusLostListener;
|
|
import java.nio.file.Path;
|
|
import java.time.YearMonth;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
public class EstadoResultadoController extends BaseController {
|
|
|
|
private EstadoResultadoView view;
|
|
private EstadoResultado estadoResultado;
|
|
private YearMonth mes;
|
|
|
|
public EstadoResultadoController(EstadoResultadoView view) {
|
|
this.view = view;
|
|
this.setupViewEvents();
|
|
this.updateMonth();
|
|
}
|
|
|
|
public void update() {
|
|
this.updateMonth();
|
|
}
|
|
|
|
private void setupViewEvents() {
|
|
this.view.getMonthCombo().addActionListener(e -> this.updateMonth());
|
|
this.view.getYearSpinner().addChangeListener(e -> this.updateMonth());
|
|
|
|
this.setupUpdateViewEvents();
|
|
this.setupMovementViewEvents();
|
|
|
|
this.view.getGuardarButton()
|
|
.addActionListener(e -> EstadoResultadoController.this.guardarListener());
|
|
this.view.getExportarButton()
|
|
.addActionListener(e -> EstadoResultadoController.this.exportarListener());
|
|
}
|
|
|
|
private void setupUpdateViewEvents() {
|
|
this.view.getGastosGeneralesCuentaCorrienteFactura().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosGenerales());
|
|
this.view.getGastosGeneralesCuentaCorrienteBoleta().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosGenerales());
|
|
this.view.getGastosGeneralesCuentaCorrienteSinRespaldo().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosGenerales());
|
|
|
|
this.view.getServiciosAgua().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateServicios());
|
|
this.view.getServiciosLuz().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateServicios());
|
|
this.view.getServiciosGas().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateServicios());
|
|
this.view.getServiciosTelefono().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateServicios());
|
|
this.view.getServiciosOtro().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateServicios());
|
|
|
|
this.view.getGastosOperacionalesCostoVenta().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosOperacionales());
|
|
this.view.getGastosOperacionalesRemuneraciones().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosOperacionales());
|
|
this.view.getGastosOperacionalesFiniquitos().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosOperacionales());
|
|
this.view.getGastosOperacionalesAguinaldo().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosOperacionales());
|
|
this.view.getGastosOperacionalesBonos().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosOperacionales());
|
|
this.view.getGastosOperacionalesHonorariosContador().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosOperacionales());
|
|
this.view.getGastosOperacionalesArriendo().addFocusListener(
|
|
(FocusLostListener) e -> EstadoResultadoController.this.updateGastosOperacionales());
|
|
|
|
this.view.getResumenIVAFavor()
|
|
.addFocusListener((FocusLostListener) e -> EstadoResultadoController.this.updateResumen());
|
|
this.view.getResumenPPM()
|
|
.addFocusListener((FocusLostListener) e -> EstadoResultadoController.this.updateResumen());
|
|
}
|
|
|
|
private void setupMovementViewEvents() {
|
|
|
|
moveTo(this.view.getGastosOperacionalesCostoVenta(),
|
|
this.view.getGastosOperacionalesRemuneraciones());
|
|
moveTo(this.view.getGastosOperacionalesRemuneraciones(),
|
|
this.view.getGastosOperacionalesFiniquitos());
|
|
moveTo(this.view.getGastosOperacionalesFiniquitos(),
|
|
this.view.getGastosOperacionalesAguinaldo());
|
|
moveTo(this.view.getGastosOperacionalesAguinaldo(), this.view.getGastosOperacionalesBonos());
|
|
moveTo(this.view.getGastosOperacionalesBonos(),
|
|
this.view.getGastosOperacionalesHonorariosContador());
|
|
moveTo(this.view.getGastosOperacionalesHonorariosContador(),
|
|
this.view.getGastosOperacionalesArriendo());
|
|
moveTo(this.view.getGastosOperacionalesArriendo(), this.view.getServiciosAgua());
|
|
|
|
moveTo(this.view.getServiciosAgua(), this.view.getServiciosLuz());
|
|
moveTo(this.view.getServiciosLuz(), this.view.getServiciosGas());
|
|
moveTo(this.view.getServiciosGas(), this.view.getServiciosTelefono());
|
|
moveTo(this.view.getServiciosTelefono(), this.view.getServiciosOtro());
|
|
moveTo(this.view.getServiciosOtro(), this.view.getGastosGeneralesCuentaCorrienteFactura());
|
|
|
|
moveTo(this.view.getGastosGeneralesCuentaCorrienteFactura(),
|
|
this.view.getGastosGeneralesCuentaCorrienteBoleta());
|
|
moveTo(this.view.getGastosGeneralesCuentaCorrienteBoleta(),
|
|
this.view.getGastosGeneralesCuentaCorrienteSinRespaldo());
|
|
moveTo(this.view.getGastosGeneralesCuentaCorrienteSinRespaldo(), this.view.getResumenPPM());
|
|
|
|
moveTo(this.view.getResumenPPM(), this.view.getResumenIVAFavor());
|
|
}
|
|
|
|
private void guardarListener() {
|
|
EstadoResultadoDAO dao = DAOManager.getEstadoResultadoDAO();
|
|
dao.updateEstadoResultado(this.estadoResultado);
|
|
}
|
|
|
|
private void exportarListener() {
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy");
|
|
String formatedName = mes.format(formatter);
|
|
String capitalized = StringUtils.capitalize(formatedName);
|
|
|
|
Path saveFile = new XLSFileChooser(
|
|
Configuration.get("base_save_directory") + "Estado Resultado " + capitalized).execute();
|
|
|
|
if (saveFile == null) {
|
|
return;
|
|
}
|
|
|
|
InformeEstadoResultado estadoResultado = new InformeEstadoResultado(this.mes, saveFile);
|
|
estadoResultado.generarInforme();
|
|
new InformeGeneratedConfirmation(saveFile).execute();
|
|
}
|
|
|
|
private void updateMonth() {
|
|
this.mes = this.view.getMonth();
|
|
this.estadoResultado = DAOManager.getEstadoResultadoDAO().findByMonth(this.mes);
|
|
if (estadoResultado == null) {
|
|
this.estadoResultado = EstadoResultado.emptyEstadoResultado;
|
|
this.estadoResultado.setMes(this.mes);
|
|
DAOManager.getEstadoResultadoDAO().insertEstadoResultado(estadoResultado);
|
|
}
|
|
|
|
this.fillVentas();
|
|
this.fillGastosGenerales();
|
|
this.fillServicios();
|
|
this.fillGastosOperacionales();
|
|
this.fillResumen();
|
|
}
|
|
|
|
private void fillVentas() {
|
|
int ventaBruta = DAOManager.getIngresoDAO().getTotalIngresoMes(this.mes);
|
|
int ventaExentas = DAOManager.getIngresoDAO().getTotalExentasMes(this.mes);
|
|
int ventaNeta = (int) Math.round((double) ventaBruta / 1.19d);
|
|
int ventaIVA = ventaBruta - ventaNeta;
|
|
int ventaNetaYExentas = ventaExentas + ventaNeta;
|
|
|
|
this.view.getVentaBrutaField().setValue(ventaBruta);
|
|
this.view.getVentaIVAField().setValue(ventaIVA);
|
|
this.view.getVentaNetaField().setValue(ventaNeta);
|
|
this.view.getVentaExentasField().setValue(ventaExentas);
|
|
this.view.getVentasNetaExentasField().setValue(ventaNetaYExentas);
|
|
}
|
|
|
|
private void fillGastosGenerales() {
|
|
TipoEgreso facturaGastosGenerales = DAOManager.getTipoEgresoDAO()
|
|
.findByNombre("Factura Gastos Generales").get(0);
|
|
TipoEgreso gastoGeneralConBoleta = DAOManager.getTipoEgresoDAO()
|
|
.findByNombre("Gasto General Con Boleta").get(0);
|
|
TipoEgreso gastoGeneralSinRespaldo = DAOManager.getTipoEgresoDAO()
|
|
.findByNombre("Gasto General Sin Respaldo").get(0);
|
|
|
|
int cuentaCorrienteFactura = this.estadoResultado.getCuentaCorrienteFactura();
|
|
int cuentaCorrienteBoleta = this.estadoResultado.getCuentaCorrienteBoleta();
|
|
int cuentaCorrienteSinRespaldo = this.estadoResultado.getCuentaCorrienteSinRespaldo();
|
|
int efectivoFacturaGastosGenerales = DAOManager.getEgresoDAO()
|
|
.getTotalEgresoMesPorTipo(this.mes, facturaGastosGenerales);
|
|
int efectivoGastoGeneralConBoleta = DAOManager.getEgresoDAO()
|
|
.getTotalEgresoMesPorTipo(this.mes, gastoGeneralConBoleta);
|
|
int efectivoGastoGeneralSinRespaldo = DAOManager.getEgresoDAO()
|
|
.getTotalEgresoMesPorTipo(this.mes, gastoGeneralSinRespaldo);
|
|
int gastoTotal = efectivoFacturaGastosGenerales + efectivoGastoGeneralConBoleta
|
|
+ efectivoGastoGeneralSinRespaldo + cuentaCorrienteBoleta + cuentaCorrienteFactura
|
|
+ cuentaCorrienteSinRespaldo;
|
|
|
|
this.view.getGastosGeneralesEfectivoFacturaField().setValue(efectivoFacturaGastosGenerales);
|
|
this.view.getGastosGeneralesEfectivoBoletaField().setValue(efectivoGastoGeneralConBoleta);
|
|
this.view.getGastosGeneralesEfectivoSinRespaldo().setValue(efectivoGastoGeneralSinRespaldo);
|
|
this.view.getGastosGeneralesCuentaCorrienteFactura().setValue(cuentaCorrienteFactura);
|
|
this.view.getGastosGeneralesCuentaCorrienteBoleta().setValue(cuentaCorrienteBoleta);
|
|
this.view.getGastosGeneralesCuentaCorrienteSinRespaldo().setValue(cuentaCorrienteSinRespaldo);
|
|
this.view.getGastosGeneralesTotal().setValue(gastoTotal);
|
|
}
|
|
|
|
private void fillGastosOperacionales() {
|
|
TipoEgreso tipoPagoPartime = DAOManager.getTipoEgresoDAO().findByNombre("Pago Partime").get(0);
|
|
|
|
int costoVenta = this.estadoResultado.getCostoVenta();
|
|
int remuneraciones = this.estadoResultado.getRemuneraciones();
|
|
int finiquitos = this.estadoResultado.getFiniquitos();
|
|
int aguinaldo = this.estadoResultado.getAguinaldo();
|
|
int bonosPersonal = this.estadoResultado.getBonosPersonal();
|
|
int honorariosContador = this.estadoResultado.getHonorariosContador();
|
|
int arriendo = this.estadoResultado.getArriendo();
|
|
int partime = DAOManager.getEgresoDAO().getTotalEgresoMesPorTipo(this.mes, tipoPagoPartime);
|
|
int ventaBruta = DAOManager.getIngresoDAO().getTotalIngresoMes(this.mes);
|
|
|
|
int total = costoVenta + remuneraciones + finiquitos + aguinaldo + bonosPersonal
|
|
+ honorariosContador + arriendo + partime;
|
|
double porcentajeCostoVenta = (double) costoVenta / (double) ventaBruta * 100d;
|
|
|
|
this.view.getGastosOperacionalesCostoVenta().setValue(costoVenta);
|
|
this.view.getGastosOperacionesPorcentajeCostoVenta().setValue(porcentajeCostoVenta);
|
|
this.view.getGastosOperacionalesRemuneraciones().setValue(remuneraciones);
|
|
this.view.getGastosOperacionalesFiniquitos().setValue(finiquitos);
|
|
this.view.getGastosOperacionalesAguinaldo().setValue(aguinaldo);
|
|
this.view.getGastosOperacionalesBonos().setValue(bonosPersonal);
|
|
this.view.getGastosOperacionalesHonorariosContador().setValue(honorariosContador);
|
|
this.view.getGastosOperacionalesArriendo().setValue(arriendo);
|
|
this.view.getGastosOperacionalesPartime().setValue(partime);
|
|
|
|
this.view.getGastosOperacionalesTotal().setValue(total);
|
|
}
|
|
|
|
private void fillServicios() {
|
|
int agua = this.estadoResultado.getAgua();
|
|
int luz = this.estadoResultado.getLuz();
|
|
int gas = this.estadoResultado.getGas();
|
|
int telefono = this.estadoResultado.getTelefono();
|
|
int otro = this.estadoResultado.getOtroServicio();
|
|
|
|
int total = agua + luz + gas + telefono + otro;
|
|
|
|
this.view.getServiciosAgua().setValue(agua);
|
|
this.view.getServiciosLuz().setValue(luz);
|
|
this.view.getServiciosGas().setValue(gas);
|
|
this.view.getServiciosTelefono().setValue(telefono);
|
|
this.view.getServiciosOtro().setValue(otro);
|
|
this.view.getServiciosTotal().setValue(total);
|
|
}
|
|
|
|
private void fillResumen() {
|
|
double ppm = this.estadoResultado.getPpm();
|
|
int aFavor = this.estadoResultado.getIvaFavor();
|
|
|
|
this.view.getResumenPPM().setValue(ppm);
|
|
this.view.getResumenIVAFavor().setValue(aFavor);
|
|
|
|
this.updateResumen();
|
|
}
|
|
|
|
private void updateGastosGenerales() {
|
|
int oldCuentaCorrienteFactura = this.estadoResultado.getCuentaCorrienteFactura();
|
|
int oldCuentaCorrienteBoleta = this.estadoResultado.getCuentaCorrienteBoleta();
|
|
int oldCuentaCorrienteSinRespaldo = this.estadoResultado.getCuentaCorrienteSinRespaldo();
|
|
int oldTotal = this.view.getGastosGeneralesTotal().getValue();
|
|
|
|
int cuentaCorrienteFactura = this.view.getGastosGeneralesCuentaCorrienteFactura().getValue();
|
|
int cuentaCorrienteBoleta = this.view.getGastosGeneralesCuentaCorrienteBoleta().getValue();
|
|
int cuentaCorrienteSinRespaldo = this.view.getGastosGeneralesCuentaCorrienteSinRespaldo()
|
|
.getValue();
|
|
|
|
this.estadoResultado.setCuentaCorrienteFactura(cuentaCorrienteFactura);
|
|
this.estadoResultado.setCuentaCorrienteBoleta(cuentaCorrienteBoleta);
|
|
this.estadoResultado.setCuentaCorrienteSinRespaldo(cuentaCorrienteSinRespaldo);
|
|
|
|
int total = oldTotal
|
|
- (oldCuentaCorrienteFactura + oldCuentaCorrienteBoleta + oldCuentaCorrienteSinRespaldo)
|
|
+ (cuentaCorrienteFactura + cuentaCorrienteBoleta + cuentaCorrienteSinRespaldo);
|
|
|
|
this.view.getGastosGeneralesTotal().setValue(total);
|
|
|
|
this.updateResumen();
|
|
}
|
|
|
|
private void updateServicios() {
|
|
int oldAgua = this.estadoResultado.getAgua();
|
|
int oldLuz = this.estadoResultado.getLuz();
|
|
int oldGas = this.estadoResultado.getGas();
|
|
int oldTelefono = this.estadoResultado.getTelefono();
|
|
int oldOtro = this.estadoResultado.getOtroServicio();
|
|
int oldTotal = this.view.getServiciosTotal().getValue();
|
|
|
|
int agua = this.view.getServiciosAgua().getValue();
|
|
int luz = this.view.getServiciosLuz().getValue();
|
|
int gas = this.view.getServiciosGas().getValue();
|
|
int telefono = this.view.getServiciosTelefono().getValue();
|
|
int otro = this.view.getServiciosOtro().getValue();
|
|
|
|
this.estadoResultado.setAgua(agua);
|
|
this.estadoResultado.setLuz(luz);
|
|
this.estadoResultado.setGas(gas);
|
|
this.estadoResultado.setTelefono(telefono);
|
|
this.estadoResultado.setOtroServicio(otro);
|
|
|
|
int total = oldTotal
|
|
- (oldAgua + oldLuz + oldGas + oldTelefono + oldOtro)
|
|
+ (agua + luz + gas + telefono + otro);
|
|
|
|
this.view.getServiciosTotal().setValue(total);
|
|
|
|
this.updateResumen();
|
|
}
|
|
|
|
private void updateGastosOperacionales() {
|
|
int oldCostoVenta = this.estadoResultado.getCostoVenta();
|
|
int oldRemuneraciones = this.estadoResultado.getRemuneraciones();
|
|
int oldFiniquitos = this.estadoResultado.getFiniquitos();
|
|
int oldAguinaldo = this.estadoResultado.getAguinaldo();
|
|
int oldBonos = this.estadoResultado.getBonosPersonal();
|
|
int oldHonorarios = this.estadoResultado.getHonorariosContador();
|
|
int oldArriendo = this.estadoResultado.getArriendo();
|
|
int oldTotal = this.view.getGastosOperacionalesTotal().getValue();
|
|
|
|
int costoVenta = this.view.getGastosOperacionalesCostoVenta().getValue();
|
|
int remuneraciones = this.view.getGastosOperacionalesRemuneraciones().getValue();
|
|
int finiquitos = this.view.getGastosOperacionalesFiniquitos().getValue();
|
|
int aguinaldo = this.view.getGastosOperacionalesAguinaldo().getValue();
|
|
int bonos = this.view.getGastosOperacionalesBonos().getValue();
|
|
int honorarios = this.view.getGastosOperacionalesHonorariosContador().getValue();
|
|
int arriendo = this.view.getGastosOperacionalesArriendo().getValue();
|
|
|
|
int ventaBruta = DAOManager.getIngresoDAO().getTotalIngresoMes(this.mes);
|
|
|
|
this.estadoResultado.setCostoVenta(costoVenta);
|
|
this.estadoResultado.setRemuneraciones(remuneraciones);
|
|
this.estadoResultado.setFiniquitos(finiquitos);
|
|
this.estadoResultado.setAguinaldo(aguinaldo);
|
|
this.estadoResultado.setBonosPersonal(bonos);
|
|
this.estadoResultado.setHonorariosContador(honorarios);
|
|
this.estadoResultado.setArriendo(arriendo);
|
|
|
|
int total = oldTotal
|
|
- (oldCostoVenta + oldRemuneraciones + oldFiniquitos + oldAguinaldo + oldBonos
|
|
+ oldHonorarios + oldArriendo)
|
|
+ (costoVenta + remuneraciones + finiquitos + aguinaldo + bonos + honorarios + arriendo);
|
|
double porcentajeCostoVenta = (double) costoVenta / (double) ventaBruta * 100d;
|
|
|
|
this.view.getGastosOperacionalesTotal().setValue(total);
|
|
this.view.getGastosOperacionesPorcentajeCostoVenta().setValue(porcentajeCostoVenta);
|
|
|
|
this.updateResumen();
|
|
}
|
|
|
|
private void updateResumen() {
|
|
int bruto = this.view.getVentaBrutaField().getValue();
|
|
int totalGastosOperacionales = this.view.getGastosOperacionalesTotal().getValue();
|
|
int totalGastosGenerales = this.view.getGastosGeneralesTotal().getValue();
|
|
int totalServicios = this.view.getServiciosTotal().getValue();
|
|
int netoExentas = this.view.getVentasNetaExentasField().getValue();
|
|
int iva = this.view.getVentaIVAField().getValue();
|
|
double ppm = this.view.getResumenPPM().getValue();
|
|
int ivaFavor = this.view.getResumenIVAFavor().getValue();
|
|
|
|
int utilidad = bruto - totalGastosGenerales - totalGastosOperacionales - totalServicios;
|
|
int ppmMes = (int) Math.round(ppm * (double) netoExentas / 100d);
|
|
int IVAPPM = iva + ppmMes;
|
|
int aPagar = IVAPPM - ivaFavor;
|
|
int resultado = utilidad - aPagar;
|
|
|
|
this.view.getResumenUtilidad().setValue(utilidad);
|
|
this.view.getResumenPPMMes().setValue(ppmMes);
|
|
this.view.getResumenIVAMes().setValue(iva);
|
|
this.view.getResumenIVAPPM().setValue(IVAPPM);
|
|
this.view.getResumenAPagar().setValue(aPagar);
|
|
this.view.getResumenResultado().setValue(resultado);
|
|
|
|
this.estadoResultado.setPpm(ppm);
|
|
this.estadoResultado.setIvaFavor(ivaFavor);
|
|
}
|
|
|
|
}
|
|
|