Resubido todo

This commit is contained in:
Daniel Cortes
2018-12-29 01:15:35 -03:00
parent 29d7f910d7
commit 7e94207da3
91 changed files with 10525 additions and 0 deletions

View File

@@ -0,0 +1,324 @@
/*
* MIT License
*
* Copyright (c) 2018 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.controllers;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.documentos.Documentos;
import danielcortes.xyz.models.documentos.DocumentosDAO;
import danielcortes.xyz.models.efectivo.Efectivo;
import danielcortes.xyz.models.efectivo.EfectivoDAO;
import danielcortes.xyz.models.egreso.EgresoDAO;
import danielcortes.xyz.models.ingreso.IngresoDAO;
import danielcortes.xyz.views.ArqueoView;
import javax.swing.*;
import java.awt.*;
public class ArqueoController {
private ArqueoView view;
private Caja caja;
private Efectivo efectivo;
private Documentos documentos;
private EfectivoDAO efectivoDAO;
private DocumentosDAO documentosDAO;
private IngresoDAO ingresoDAO;
private EgresoDAO egresoDAO;
public ArqueoController(ArqueoView view, EfectivoDAO efectivoDAO, DocumentosDAO documentosDAO, IngresoDAO ingresoDAO, EgresoDAO egresoDAO) {
this.view = view;
this.efectivoDAO = efectivoDAO;
this.documentosDAO = documentosDAO;
this.ingresoDAO = ingresoDAO;
this.egresoDAO = egresoDAO;
this.setUpViewEvents();
}
public void updateCaja(Caja caja) {
this.caja = caja;
fillDocumentos();
fillEfectivo();
fillResumen();
}
public void updateResumen() {
this.fillResumen();
}
private void fillEfectivo() {
this.efectivo = this.efectivoDAO.findByCaja(this.caja);
this.view.getVeinteMilField().setText(String.valueOf(efectivo.getVeinteMil()));
this.view.getDiezMilField().setText(String.valueOf(efectivo.getDiezMil()));
this.view.getCincoMilField().setText(String.valueOf(efectivo.getCincoMil()));
this.view.getDosMilField().setText(String.valueOf(efectivo.getDosMil()));
this.view.getMilField().setText(String.valueOf(efectivo.getMil()));
this.view.getQuinientosField().setText(String.valueOf(efectivo.getQuinientos()));
this.view.getCienField().setText(String.valueOf(efectivo.getCien()));
this.view.getCincuentaField().setText(String.valueOf(efectivo.getCincuenta()));
this.view.getDiezField().setText(String.valueOf(efectivo.getDiez()));
}
private void fillDocumentos() {
this.documentos = this.documentosDAO.findByCaja(caja);
this.view.getTarjetasField().setText(String.valueOf(documentos.getTarjetas()));
this.view.getChequesField().setText(String.valueOf(documentos.getCheques()));
}
private void fillResumen() {
this.updateResumenEfectivo();
this.updateResumenDocumentos();
this.updateResumenIngresos();
this.updateResumenEgresos();
this.updateResumenArqueo();
}
private void updateResumenEfectivo() {
JTextField efectivoField = this.view.getEfectivoField();
int total = 0;
total += this.efectivo.getDiez();
total += this.efectivo.getCincuenta();
total += this.efectivo.getCien();
total += this.efectivo.getQuinientos();
total += this.efectivo.getMil();
total += this.efectivo.getDosMil();
total += this.efectivo.getCincoMil();
total += this.efectivo.getDiezMil();
total += this.efectivo.getVeinteMil();
efectivoField.setText(String.valueOf(total));
}
private void updateResumenDocumentos() {
JTextField documentosField = this.view.getDocumentosField();
int total = 0;
total += this.documentos.getCheques();
total += this.documentos.getTarjetas();
documentosField.setText(String.valueOf(total));
}
private void updateResumenIngresos() {
int total = this.ingresoDAO.getTotalIngreso(this.caja);
this.view.getIngresosField().setText(String.valueOf(total));
}
private void updateResumenEgresos() {
int total = this.egresoDAO.getTotalEgreso(this.caja);
this.view.getEgresosField().setText(String.valueOf(total));
}
private void updateResumenArqueo() {
int totalEfectivo = Integer.parseInt(this.view.getEfectivoField().getText());
int totalDocumentos = Integer.parseInt(this.view.getDocumentosField().getText());
int totalIngresos = Integer.parseInt(this.view.getIngresosField().getText());
int totalEgresos = Integer.parseInt(this.view.getEgresosField().getText());
int arqueo = totalDocumentos + totalEfectivo + totalEgresos;
int ajuste = arqueo - totalIngresos;
this.view.getArqueoField().setText(String.valueOf(arqueo));
this.view.getRendidoField().setText(String.valueOf(totalIngresos));
this.view.getAjusteField().setText(String.valueOf(ajuste));
if(ajuste < 0) {
this.view.getAjusteField().setForeground(new Color(255,0,0));
}else{
this.view.getAjusteField().setForeground(new Color(0,0,0));
}
}
private void setUpViewEvents() {
this.view.getGuardarEfectivoButton().addActionListener(e -> {
this.normalizeEfectivoInput();
this.guardarEfectivo();
});
this.view.getGuardarDocumentosButton().addActionListener(e -> {
this.normalizeDocumentosInput();
this.guardarDocumentos();
});
}
private void guardarEfectivo() {
String diez = this.view.getDiezField().getText();
String cincuenta = this.view.getCincuentaField().getText();
String cien = this.view.getCienField().getText();
String quinientos = this.view.getQuinientosField().getText();
String mil = this.view.getMilField().getText();
String dosMil = this.view.getDosMilField().getText();
String cincoMil = this.view.getCincoMilField().getText();
String diezMil = this.view.getDiezMilField().getText();
String veinteMil = this.view.getVeinteMilField().getText();
if (this.validateEfectivoInput(diez, cincuenta, cien, quinientos, mil, dosMil, cincoMil, diezMil, veinteMil)) {
this.efectivo.setDiez(Integer.valueOf(diez));
this.efectivo.setCincuenta(Integer.valueOf(cincuenta));
this.efectivo.setCien(Integer.valueOf(cien));
this.efectivo.setQuinientos(Integer.valueOf(quinientos));
this.efectivo.setMil(Integer.valueOf(mil));
this.efectivo.setDosMil(Integer.valueOf(dosMil));
this.efectivo.setCincoMil(Integer.valueOf(cincoMil));
this.efectivo.setDiezMil(Integer.valueOf(diezMil));
this.efectivo.setVeinteMil(Integer.valueOf(veinteMil));
this.efectivoDAO.updateEfectivo(efectivo);
this.updateResumenEfectivo();
this.updateResumenArqueo();
}
}
private void guardarDocumentos() {
String tarjetas = this.view.getTarjetasField().getText();
String cheques = this.view.getChequesField().getText();
if (this.validateDocumentosInput(tarjetas, cheques)) {
this.documentos.setTarjetas(Integer.valueOf(tarjetas));
this.documentos.setCheques(Integer.valueOf(cheques));
this.documentosDAO.updateDocumentos(documentos);
this.updateResumenDocumentos();
this.updateResumenArqueo();
}
}
private boolean validateEfectivoInput(String diez, String cincuenta, String cien, String quinientos, String mil, String dosMil, String cincoMil, String diezMil, String veinteMil) {
this.hiddeEfectivoErrorMessages();
boolean diezValidation = validateEfectivoMoneda(diez, this.view.getErrorDiez());
boolean cincuentaValidation = validateEfectivoMoneda(cincuenta, this.view.getErrorCincuenta());
boolean cienValidation = validateEfectivoMoneda(cien, this.view.getErrorCien());
boolean quinientosValidation = validateEfectivoMoneda(quinientos, this.view.getErrorQuinientos());
boolean milValidation = validateEfectivoMoneda(mil, this.view.getErrorMil());
boolean dosMilValidation = validateEfectivoMoneda(dosMil, this.view.getErrorDosMil());
boolean cincoMilValidation = validateEfectivoMoneda(cincoMil, this.view.getErrorCincoMil());
boolean diezMilValidation = validateEfectivoMoneda(diezMil, this.view.getErrorDiezMil());
boolean veinteMilValidation = validateEfectivoMoneda(veinteMil, this.view.getErrorVeinteMil());
return diezValidation && cincuentaValidation && cienValidation && quinientosValidation && milValidation && dosMilValidation && cincoMilValidation && diezMilValidation && veinteMilValidation;
}
private boolean validateDocumentosInput(String tarjetas, String cheques) {
this.hiddeDocumentosErrorMessages();
boolean tarjetasValidation = validateDocumentosValor(tarjetas, this.view.getErrorTarjetas());
boolean chequesValidation = validateDocumentosValor(cheques, this.view.getErrorCheques());
return tarjetasValidation && chequesValidation;
}
private boolean validateEfectivoMoneda(String valor, JLabel errorLabel) {
if (valor == null) {
errorLabel.setText("Hubo un problema con los datos");
errorLabel.setVisible(true);
return false;
}
if (valor.isEmpty()) {
errorLabel.setText("El campo esta vacio");
errorLabel.setVisible(true);
return false;
}
if (!valor.chars().allMatch(Character::isDigit)) {
errorLabel.setText("Deben ser numeros");
errorLabel.setVisible(true);
return false;
}
if (valor.length() > 10) {
errorLabel.setText("El numero ingresado es demasiado largo");
errorLabel.setVisible(true);
return false;
}
return true;
}
private boolean validateDocumentosValor(String valor, JLabel errorLabel) {
if (valor == null) {
errorLabel.setText("Hubo un problema con los datos");
errorLabel.setVisible(true);
return false;
}
if (valor.isEmpty()) {
errorLabel.setText("El campo esta vacio");
errorLabel.setVisible(true);
return false;
}
if (!valor.chars().allMatch(Character::isDigit)) {
errorLabel.setText("Deben ser numeros");
errorLabel.setVisible(true);
return false;
}
if (valor.length() > 10) {
errorLabel.setText("El numero ingresado es demasiado largo");
errorLabel.setVisible(true);
return false;
}
return true;
}
private void hiddeEfectivoErrorMessages() {
this.view.getErrorDiez().setVisible(false);
this.view.getErrorCincuenta().setVisible(false);
this.view.getErrorCien().setVisible(false);
this.view.getErrorQuinientos().setVisible(false);
this.view.getErrorMil().setVisible(false);
this.view.getErrorDosMil().setVisible(false);
this.view.getErrorCincoMil().setVisible(false);
this.view.getErrorDiezMil().setVisible(false);
this.view.getErrorVeinteMil().setVisible(false);
}
private void hiddeDocumentosErrorMessages(){
this.view.getErrorTarjetas().setVisible(false);
this.view.getErrorCheques().setVisible(false);
}
private void normalizeEfectivoInput() {
this.view.getDiezField().setText(this.view.getDiezField().getText().trim());
this.view.getCincuentaField().setText(this.view.getCincuentaField().getText().trim());
this.view.getCienField().setText(this.view.getCienField().getText().trim());
this.view.getQuinientosField().setText(this.view.getQuinientosField().getText().trim());
this.view.getMilField().setText(this.view.getMilField().getText().trim());
this.view.getDosMilField().setText(this.view.getDosMilField().getText().trim());
this.view.getCincoMilField().setText(this.view.getCincoMilField().getText().trim());
this.view.getDiezMilField().setText(this.view.getDiezMilField().getText().trim());
this.view.getVeinteMilField().setText(this.view.getVeinteMilField().getText().trim());
}
private void normalizeDocumentosInput() {
this.view.getChequesField().setText(this.view.getChequesField().getText().trim());
this.view.getTarjetasField().setText(this.view.getTarjetasField().getText().trim());
}
}

View File

@@ -0,0 +1,319 @@
/*
* MIT License
*
* Copyright (c) 2018 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.controllers;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.egreso.Egreso;
import danielcortes.xyz.models.egreso.EgresoDAO;
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
import danielcortes.xyz.models.tipo_egreso.TipoEgresoDAO;
import danielcortes.xyz.views.EgresosView;
import danielcortes.xyz.views.components.EgresosTableModel;
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class EgresosController {
private EgresosView view;
private EgresoDAO egresoDAO;
private TipoEgresoDAO tipoEgresoDAO;
private Caja caja;
private int editingId;
private boolean editing;
private Egreso editingEgreso;
public EgresosController(EgresosView view, EgresoDAO egresoDAO, TipoEgresoDAO tipoEgresoDAO) {
this.view = view;
this.egresoDAO = egresoDAO;
this.tipoEgresoDAO = tipoEgresoDAO;
this.setUpViewEvents();
this.fillTipoEgresoCombo();
this.updateButtonsEnabled();
}
public EgresoDAO getEgresoDAO() {
return egresoDAO;
}
public TipoEgresoDAO getTipoEgresoDAO() {
return tipoEgresoDAO;
}
public void updateCaja(Caja caja){
this.caja = caja;
this.fillEgresosTable();
this.updateTotalEgresos();
}
private void fillTipoEgresoCombo() {
JComboBox<TipoEgreso> tipoCombo = view.getTipoCombo();
for (TipoEgreso tipoEgreso : this.tipoEgresoDAO.findAll()) {
tipoCombo.addItem(tipoEgreso);
}
}
private void fillEgresosTable() {
EgresosTableModel egresosTableModel = view.getEgresosTableModel();
egresosTableModel.removeRows();
for (Egreso egreso : this.egresoDAO.findByCaja(this.caja)) {
egresosTableModel.addRow(egreso);
}
}
private void setUpViewEvents() {
this.view.getEgresosTable().getSelectionModel().addListSelectionListener(e -> onSelectTableRowListener());
this.view.getGuardarButton().addActionListener(e -> guardarActionListener());
this.view.getEliminarButton().addActionListener(e -> eliminarActionListener());
this.view.getEditarButton().addActionListener(e -> editarActionListener());
this.view.getDescripcionField().addActionListener(e -> guardarActionListener());
this.view.getNroField().addActionListener(e -> guardarActionListener());
this.view.getValorField().addActionListener(e -> guardarActionListener());
this.view.getTipoCombo().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
guardarActionListener();
}
}
});
this.view.getEgresosTable().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
JTable table = (JTable) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) {
EgresosController.this.editarActionListener();
}
}
});
}
private void guardarActionListener() {
this.normalizeInputs();
String nro = this.view.getNroField().getText();
String descripcion = this.view.getDescripcionField().getText();
String valor = this.view.getValorField().getText();
TipoEgreso tipo = (TipoEgreso) this.view.getTipoCombo().getSelectedItem();
if(editing){
this.editarEgreso(nro, descripcion, valor, tipo, this.caja);
}else {
this.guardarEgreso(nro, descripcion, valor, tipo, this.caja);
}
this.resetFocus();
}
private void eliminarActionListener() {
int selectedID = this.view.getEgresosTable().getSelectedRow();
if (selectedID >= 0) {
Egreso egreso = this.view.getEgresosTableModel().getEgreso(selectedID);
this.view.getEgresosTableModel().removeRow(selectedID);
this.egresoDAO.deleteEgreso(egreso);
this.updateTotalEgresos();
this.updateButtonsEnabled();
}
}
private void editarActionListener() {
int selectedID = this.view.getEgresosTable().getSelectedRow();
if (selectedID >= 0) {
Egreso egreso = this.view.getEgresosTableModel().getEgreso(selectedID);
this.editingId = selectedID;
this.editingEgreso = egreso;
this.editing = true;
this.view.getNroField().setText(egreso.getNro());
this.view.getDescripcionField().setText(egreso.getDescripcion());
this.view.getValorField().setText(String.valueOf(egreso.getValor()));
this.view.getTipoCombo().setSelectedItem(egreso.getTipoEgreso());
}
}
private void onSelectTableRowListener() {
this.view.getEliminarButton().setEnabled(true);
this.view.getEditarButton().setEnabled(true);
}
private void updateTotalEgresos() {
int total = this.egresoDAO.getTotalEgreso(this.caja);
this.view.getTotalEgresosField().setText(String.valueOf(total));
}
private void updateButtonsEnabled() {
if (this.view.getEgresosTable().getSelectedRow() >= 0) {
this.view.getEliminarButton().setEnabled(true);
this.view.getEditarButton().setEnabled(true);
} else {
this.view.getEliminarButton().setEnabled(false);
this.view.getEditarButton().setEnabled(false);
}
}
private void guardarEgreso(String nro, String descripcion, String valor, TipoEgreso tipo, Caja caja) {
if (this.validateInput(nro, descripcion, valor, tipo, caja)) {
Egreso egreso = new Egreso();
egreso.setValor(Integer.valueOf(valor));
egreso.setDescripcion(descripcion);
egreso.setNro(nro);
egreso.setTipoEgreso(tipo);
egreso.setCaja(caja);
egresoDAO.insertEgreso(egreso);
this.view.getEgresosTableModel().addRow(egreso);
this.updateTotalEgresos();
this.clearInputs();
}
}
private void editarEgreso(String nro, String descripcion, String valor, TipoEgreso tipo, Caja caja) {
if (this.validateInput(nro, descripcion, valor, tipo, caja)) {
this.editingEgreso.setValor(Integer.valueOf(valor));
this.editingEgreso.setDescripcion(descripcion);
this.editingEgreso.setNro(nro);
this.editingEgreso.setTipoEgreso(tipo);
egresoDAO.updateEgreso(this.editingEgreso);
this.view.getEgresosTableModel().setEgreso(this.editingId, this.editingEgreso);
this.updateTotalEgresos();
this.clearInputs();
this.editing = false;
}
}
private boolean validateInput(String nro, String descripcion, String valor, TipoEgreso tipoEgreso, Caja caja) {
this.hideErrorMessages();
boolean nroValidation = this.validateNro(nro);
boolean descripcionValidation = this.validateDescripcion(descripcion);
boolean valorValidation = this.validateValor(valor);
boolean tipoEgresoValidation = this.validateTipoEgreso(tipoEgreso);
boolean cajaValidation = this.validateCaja(caja);
return nroValidation && descripcionValidation && valorValidation && tipoEgresoValidation;
}
private boolean validateNro(String nro) {
if (nro == null) {
this.view.getErrorNumero().setText("Hubo un problema con los datos");
this.view.getErrorNumero().setVisible(true);
return false;
}
nro = nro.trim();
if (nro.isEmpty()) {
this.view.getErrorNumero().setText("El campo esta vacio");
this.view.getErrorNumero().setVisible(true);
return false;
}
return true;
}
private boolean validateDescripcion(String descripcion) {
if (descripcion == null) {
this.view.getErrorDescripcion().setText("Hubo un problema con los datos");
this.view.getErrorDescripcion().setVisible(true);
return false;
}
descripcion = descripcion.trim();
if (descripcion.isEmpty()) {
this.view.getErrorDescripcion().setText("El campo esta vacio");
this.view.getErrorDescripcion().setVisible(true);
return false;
}
return true;
}
private boolean validateValor(String valor) {
if (valor == null) {
this.view.getErrorValor().setText("Hubo un problema con los datos");
this.view.getErrorValor().setVisible(true);
return false;
}
valor = valor.trim();
if (valor.isEmpty()) {
this.view.getErrorValor().setText("El campo esta vacio");
this.view.getErrorValor().setVisible(true);
return false;
}
if (!valor.chars().allMatch(Character::isDigit)) {
this.view.getErrorValor().setText("Deben ser numeros");
this.view.getErrorValor().setVisible(true);
return false;
}
if (valor.length() > 10) {
this.view.getErrorValor().setText("El numero ingresado es demasiado largo");
this.view.getErrorValor().setVisible(true);
return false;
}
return true;
}
private boolean validateTipoEgreso(TipoEgreso tipoEgreso) {
if (tipoEgreso == null) {
this.view.getErrorTipoEgreso().setText("Hubo un problema con los datos");
this.view.getErrorTipoEgreso().setVisible(true);
return false;
}
return true;
}
private boolean validateCaja(Caja caja){
return caja != null;
}
private void hideErrorMessages() {
this.view.getErrorTipoEgreso().setVisible(false);
this.view.getErrorValor().setVisible(false);
this.view.getErrorDescripcion().setVisible(false);
this.view.getErrorNumero().setVisible(false);
}
private void clearInputs() {
this.view.getTipoCombo().setSelectedIndex(0);
this.view.getNroField().setText("");
this.view.getValorField().setText("");
this.view.getDescripcionField().setText("");
}
private void normalizeInputs(){
this.view.getValorField().setText(this.view.getValorField().getText().trim());
this.view.getNroField().setText(this.view.getNroField().getText().trim());
this.view.getDescripcionField().setText(this.view.getDescripcionField().getText().trim());
}
private void resetFocus() {
this.view.getNroField().requestFocus();
}
}

View File

@@ -0,0 +1,321 @@
/*
* MIT License
*
* Copyright (c) 2018 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.controllers;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.ingreso.Ingreso;
import danielcortes.xyz.models.ingreso.IngresoDAO;
import danielcortes.xyz.models.tipo_ingreso.TipoIngreso;
import danielcortes.xyz.models.tipo_ingreso.TipoIngresoDAO;
import danielcortes.xyz.views.IngresosView;
import danielcortes.xyz.views.components.IngresosTableModel;
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class IngresosController {
private IngresosView view;
private IngresoDAO ingresoDAO;
private TipoIngresoDAO tipoIngresoDAO;
private Caja caja;
private int editingId;
private Ingreso editingIngreso;
private boolean editing;
public IngresosController(IngresosView view, IngresoDAO ingresoDAO, TipoIngresoDAO tipoIngresoDAO) {
this.view = view;
this.ingresoDAO = ingresoDAO;
this.tipoIngresoDAO = tipoIngresoDAO;
this.fillTipoIngresoCombo();
this.setupViewEvents();
this.updateButtonsEnabled();
}
public IngresoDAO getIngresoDAO() {
return ingresoDAO;
}
public TipoIngresoDAO getTipoIngresoDAO() {
return tipoIngresoDAO;
}
public void updateCaja(Caja caja){
this.caja = caja;
this.fillIngresosTable();
this.updateTotalIngresos();
}
private void fillTipoIngresoCombo() {
JComboBox<TipoIngreso> tipoCombo = this.view.getTipoCombo();
for (TipoIngreso tipo : this.tipoIngresoDAO.findAll()) {
tipoCombo.addItem(tipo);
}
}
private void fillIngresosTable() {
IngresosTableModel ingresosTableModel = this.view.getIngresosTableModel();
ingresosTableModel.removeRows();
for (Ingreso ingreso : this.ingresoDAO.findByCaja(this.caja)) {
ingresosTableModel.addRow(ingreso);
}
}
private void setupViewEvents() {
this.view.getIngresosTable().getSelectionModel().addListSelectionListener(e -> onSelectTableRowListener());
this.view.getGuardarButton().addActionListener(e -> guardarActionListener());
this.view.getValorField().addActionListener(e -> guardarActionListener());
this.view.getNroInicialField().addActionListener(e -> guardarActionListener());
this.view.getNroFinalField().addActionListener(e -> guardarActionListener());
this.view.getEliminarButton().addActionListener(e -> eliminarActionListener());
this.view.getEditarButton().addActionListener(e -> editarActionListener());
this.view.getTipoCombo().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
guardarActionListener();
}
}
});
this.view.getIngresosTable().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
JTable table = (JTable) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) {
IngresosController.this.editarActionListener();
}
}
});
}
private void guardarActionListener() {
this.normalizeInputs();
String valor = this.view.getValorField().getText();
String nroInicial = this.view.getNroInicialField().getText();
String nroFinal = this.view.getNroFinalField().getText();
TipoIngreso tipoIngreso = (TipoIngreso) this.view.getTipoCombo().getSelectedItem();
System.out.println(nroInicial);
System.out.println(nroFinal);
if(editing) {
this.editarIngreso(valor, nroInicial, nroFinal, tipoIngreso, this.caja);
} else {
this.guardarIngreso(valor, nroInicial, nroFinal, tipoIngreso, this.caja);
}
this.resetFocus();
}
private void eliminarActionListener() {
int selectedId = this.view.getIngresosTable().getSelectedRow();
if(selectedId >= 0){
Ingreso ingreso = this.view.getIngresosTableModel().getIngreso(selectedId);
this.view.getIngresosTableModel().removeRow(selectedId);
this.ingresoDAO.deleteIngreso(ingreso);
this.updateTotalIngresos();
this.updateButtonsEnabled();
}
}
private void editarActionListener() {
int selectedID = this.view.getIngresosTable().getSelectedRow();
if(selectedID >= 0) {
Ingreso ingreso = this.view.getIngresosTableModel().getIngreso(selectedID);
this.editingId = selectedID;
this.editingIngreso = ingreso;
this.editing = true;
this.view.getTipoCombo().setSelectedItem(ingreso.getTipoIngreso());
this.view.getValorField().setText(String.valueOf(ingreso.getValor()));
this.view.getNroInicialField().setText(String.valueOf(ingreso.getNroInicial()));
this.view.getNroFinalField().setText(String.valueOf(ingreso.getNroFinal()));
}
}
private void onSelectTableRowListener(){
this.view.getEliminarButton().setEnabled(true);
this.view.getEditarButton().setEnabled(true);
}
private void updateTotalIngresos(){
int total = this.ingresoDAO.getTotalIngreso(this.caja);
this.view.getTotalIngresoField().setText(String.valueOf(total));
}
private void updateButtonsEnabled() {
if(this.view.getIngresosTable().getSelectedRow()>=0){
this.view.getEliminarButton().setEnabled(true);
this.view.getEditarButton().setEnabled(true);
}else{
this.view.getEliminarButton().setEnabled(false);
this.view.getEditarButton().setEnabled(false);
}
}
private void guardarIngreso(String valor, String nroInicial, String nroFinal, TipoIngreso tipoIngreso, Caja caja){
if(this.validateInput(valor, nroInicial, nroFinal, tipoIngreso, caja)){
Ingreso ingreso = new Ingreso();
ingreso.setTipoIngreso(tipoIngreso);
ingreso.setCaja(caja);
ingreso.setValor(Integer.valueOf(valor));
ingreso.setNroInicial(nroInicial);
ingreso.setNroFinal(nroFinal);
this.ingresoDAO.insertIngreso(ingreso);
this.view.getIngresosTableModel().addRow(ingreso);
this.clearInputs();
this.updateTotalIngresos();
}
}
private void editarIngreso(String valor, String nroInicial, String nroFinal, TipoIngreso tipoIngreso, Caja caja){
if(this.validateInput(valor, nroInicial, nroFinal, tipoIngreso, caja)){
this.editingIngreso.setTipoIngreso(tipoIngreso);
this.editingIngreso.setValor(Integer.valueOf(valor));
this.editingIngreso.setNroInicial(nroInicial);
this.editingIngreso.setNroFinal(nroFinal);
this.ingresoDAO.updateIngreso(this.editingIngreso);
this.view.getIngresosTableModel().setIngreso(this.editingId, this.editingIngreso);
this.updateTotalIngresos();
this.clearInputs();
this.editing = false;
}
}
private boolean validateInput(String valor, String nroInicial, String nroFinal, TipoIngreso tipoIngreso, Caja caja) {
this.hideErrorMessages();
boolean valorValidation = this.validateValor(valor);
boolean nroInicialValidation = this.validateNroInicial(nroInicial);
boolean nroFinalValidation = this.validateNroFinal(nroFinal);
boolean tipoIngresoValidation = this.validateTipoIngreso(tipoIngreso);
boolean cajaValidation = this.validateCaja(caja);
return valorValidation && tipoIngresoValidation && cajaValidation;
}
private boolean validateCaja(Caja caja) {
return caja != null;
}
private boolean validateValor(String valor) {
if (valor == null) {
this.view.getErrorValor().setText("Hubo un problema con los datos");
this.view.getErrorValor().setVisible(true);
return false;
}
valor = valor.trim();
if (valor.isEmpty()) {
this.view.getErrorValor().setText("El campo esta vacio");
this.view.getErrorValor().setVisible(true);
return false;
}
if (!valor.chars().allMatch(Character::isDigit)) {
this.view.getErrorValor().setText("Deben ser numeros");
this.view.getErrorValor().setVisible(true);
return false;
}
if(valor.length() > 10){
this.view.getErrorValor().setText("El numero ingresado es demasiado largo");
this.view.getErrorValor().setVisible(true);
return false;
}
return true;
}
private boolean validateNroInicial(String nroInicial){
if (nroInicial == null) {
this.view.getErrorNroInicial().setText("Hubo un problema con los datos");
this.view.getErrorNroInicial().setVisible(true);
return false;
}
if (nroInicial.isEmpty()) {
this.view.getErrorNroInicial().setText("El campo esta vacio");
this.view.getErrorNroInicial().setVisible(true);
return false;
}
return true;
}
private boolean validateNroFinal(String nroFinal){
if (nroFinal == null) {
this.view.getErrorNroFinal().setText("Hubo un problema con los datos");
this.view.getErrorNroFinal().setVisible(true);
return false;
}
if (nroFinal.isEmpty()) {
this.view.getErrorNroFinal().setText("El campo esta vacio");
this.view.getErrorNroFinal().setVisible(true);
return false;
}
return true;
}
private boolean validateTipoIngreso(TipoIngreso tipoIngreso) {
if (tipoIngreso == null) {
this.view.getErrorTipoIngreso().setText("Hubo un problema con los datos");
this.view.getErrorTipoIngreso().setVisible(true);
return false;
}
return true;
}
private void hideErrorMessages() {
this.view.getErrorTipoIngreso().setVisible(false);
this.view.getErrorValor().setVisible(false);
this.view.getErrorNroInicial().setVisible(false);
this.view.getErrorNroFinal().setVisible(false);
}
private void clearInputs() {
this.view.getTipoCombo().setSelectedIndex(0);
this.view.getValorField().setText("");
this.view.getNroInicialField().setText("");
this.view.getNroFinalField().setText("");
}
private void normalizeInputs(){
this.view.getValorField().setText(this.view.getValorField().getText().trim());
this.view.getNroInicialField().setText(this.view.getNroInicialField().getText().trim());
this.view.getNroFinalField().setText(this.view.getNroFinalField().getText().trim());
}
private void resetFocus(){
this.view.getTipoCombo().requestFocus();
}
}

View File

@@ -0,0 +1,163 @@
/*
* MIT License
*
* Copyright (c) 2018 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.controllers;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.caja.CajaDAO;
import danielcortes.xyz.models.documentos.Documentos;
import danielcortes.xyz.models.documentos.DocumentosDAO;
import danielcortes.xyz.models.documentos.MysqlDocumentosDAO;
import danielcortes.xyz.models.efectivo.Efectivo;
import danielcortes.xyz.models.efectivo.EfectivoDAO;
import danielcortes.xyz.models.efectivo.MysqlEfectivoDAO;
import danielcortes.xyz.models.egreso.EgresoDAO;
import danielcortes.xyz.models.ingreso.IngresoDAO;
import danielcortes.xyz.models.ingreso.MysqlIngresoDAO;
import danielcortes.xyz.models.tipo_egreso.TipoEgresoDAO;
import danielcortes.xyz.models.egreso.MysqlEgresoDAO;
import danielcortes.xyz.models.tipo_egreso.MysqlTipoEgresoDAO;
import danielcortes.xyz.models.tipo_ingreso.MysqlTipoIngresoDAO;
import danielcortes.xyz.models.tipo_ingreso.TipoIngreso;
import danielcortes.xyz.models.tipo_ingreso.TipoIngresoDAO;
import danielcortes.xyz.views.ArqueoView;
import danielcortes.xyz.views.EgresosView;
import danielcortes.xyz.views.IngresosView;
import danielcortes.xyz.views.ManagerView;
import javax.swing.*;
import java.awt.*;
import java.time.LocalDate;
public class ManagerController {
private ManagerView view;
private CajaDAO cajaDAO;
private DocumentosDAO documentosDAO;
private EfectivoDAO efectivoDAO;
private EgresoDAO egresoDAO;
private IngresoDAO ingresoDAO;
private TipoEgresoDAO tipoEgresoDAO;
private TipoIngresoDAO tipoIngresoDAO;
private IngresosController ingresosController;
private EgresosController egresosController;
private ArqueoController arqueoController;
public ManagerController(ManagerView view, CajaDAO cajaDAO, DocumentosDAO documentosDAO, EfectivoDAO efectivoDAO, EgresoDAO egresoDAO, IngresoDAO ingresoDAO, TipoEgresoDAO tipoEgresoDAO, TipoIngresoDAO tipoIngresoDAO) {
this.view = view;
this.cajaDAO = cajaDAO;
this.documentosDAO = documentosDAO;
this.efectivoDAO = efectivoDAO;
this.egresoDAO = egresoDAO;
this.ingresoDAO = ingresoDAO;
this.tipoEgresoDAO = tipoEgresoDAO;
this.tipoIngresoDAO = tipoIngresoDAO;
this.loadCardContents();
this.setUpDate();
this.setUpViewEvents();
this.pressInitialButton();
}
private void setUpDate(){
this.view.getDatePicker().setDateToToday();
this.updateCaja();
}
private void setUpViewEvents() {
this.view.getEgresosButton().addActionListener(e -> {
CardLayout layout = (CardLayout) this.view.getCardPanel().getLayout();
layout.show(this.view.getCardPanel(), "EGRESOS");
});
this.view.getIngresosButton().addActionListener(e -> {
CardLayout layout = (CardLayout) this.view.getCardPanel().getLayout();
layout.show(this.view.getCardPanel(), "INGRESOS");
});
this.view.getArqueoButton().addActionListener(e -> {
this.arqueoController.updateResumen();
CardLayout layout = (CardLayout) this.view.getCardPanel().getLayout();
layout.show(this.view.getCardPanel(), "ARQUEO");
});
this.view.getDatePicker().addDateChangeListener(e -> updateCaja());
}
private void updateCaja(){
LocalDate selectedDate = this.view.getDatePicker().getDate();
Caja caja = this.cajaDAO.findByFecha(selectedDate);
if(caja == null){
caja = new Caja();
caja.setFecha(selectedDate);
this.cajaDAO.insertCaja(caja);
Efectivo efectivo = new Efectivo();
efectivo.setCaja(caja);
this.efectivoDAO.insertDefaultEfectivo(efectivo);
Documentos documentos = new Documentos();
documentos.setCaja(caja);
this.documentosDAO.insertDefaultDocumentos(documentos);
}
this.ingresosController.updateCaja(caja);
this.egresosController.updateCaja(caja);
this.arqueoController.updateCaja(caja);
}
private void loadCardContents() {
this.loadEgresosView();
this.loadIngresosView();
this.loadArqueoView();
}
private void loadIngresosView() {
IngresosView ingresosView = new IngresosView();
this.view.getCardPanel().add(ingresosView.getContentPanel(), "INGRESOS");
this.ingresosController = new IngresosController(ingresosView, this.ingresoDAO, this.tipoIngresoDAO);
}
private void loadEgresosView() {
EgresosView egresosView = new EgresosView();
this.view.getCardPanel().add(egresosView.getContentPanel(), "EGRESOS");
this.egresosController = new EgresosController(egresosView, this.egresoDAO, this.tipoEgresoDAO);
}
private void loadArqueoView() {
ArqueoView arqueoView = new ArqueoView();
this.view.getCardPanel().add(arqueoView.getContentPanel(), "ARQUEO");
this.arqueoController = new ArqueoController(arqueoView, this.efectivoDAO, this.documentosDAO, this.ingresoDAO, this.egresoDAO);
}
private void pressInitialButton() {
this.view.getIngresosButton().doClick();
}
}