Creo que esta funciona jajaja, inclusion de cajas en Egresos
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
|
||||
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;
|
||||
@@ -41,6 +42,7 @@ public class EgresosController {
|
||||
private EgresosView view;
|
||||
private EgresoDAO egresoDAO;
|
||||
private TipoEgresoDAO tipoEgresoDAO;
|
||||
private Caja caja;
|
||||
|
||||
private int editingId;
|
||||
private boolean editing;
|
||||
@@ -50,11 +52,9 @@ public class EgresosController {
|
||||
this.view = view;
|
||||
this.egresoDAO = egresoDAO;
|
||||
this.tipoEgresoDAO = tipoEgresoDAO;
|
||||
// this.setUpViewEvents();
|
||||
// this.fillEgresosTable();
|
||||
// this.fillTipoEgresoCombo();
|
||||
// this.updateTotalEgresos();
|
||||
// this.updateButtonsEnabled();
|
||||
this.setUpViewEvents();
|
||||
this.fillTipoEgresoCombo();
|
||||
this.updateButtonsEnabled();
|
||||
}
|
||||
|
||||
public EgresoDAO getEgresoDAO() {
|
||||
@@ -65,6 +65,12 @@ public class EgresosController {
|
||||
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()) {
|
||||
@@ -74,7 +80,8 @@ public class EgresosController {
|
||||
|
||||
private void fillEgresosTable() {
|
||||
EgresosTableModel egresosTableModel = view.getEgresosTableModel();
|
||||
for (Egreso egreso : this.egresoDAO.findAll()) {
|
||||
egresosTableModel.removeRows();
|
||||
for (Egreso egreso : this.egresoDAO.findByCaja(this.caja)) {
|
||||
egresosTableModel.addRow(egreso);
|
||||
}
|
||||
}
|
||||
@@ -111,9 +118,9 @@ public class EgresosController {
|
||||
String valor = this.view.getValorField().getText();
|
||||
TipoEgreso tipo = (TipoEgreso) this.view.getTipoCombo().getSelectedItem();
|
||||
if(editing){
|
||||
this.editarEgreso(nro, descripcion, valor, tipo);
|
||||
this.editarEgreso(nro, descripcion, valor, tipo, this.caja);
|
||||
}else {
|
||||
this.guardarEgreso(nro, descripcion, valor, tipo);
|
||||
this.guardarEgreso(nro, descripcion, valor, tipo, this.caja);
|
||||
}
|
||||
this.resetFocus();
|
||||
}
|
||||
@@ -151,7 +158,7 @@ public class EgresosController {
|
||||
}
|
||||
|
||||
private void updateTotalEgresos() {
|
||||
int total = this.egresoDAO.getTotalEgreso();
|
||||
int total = this.egresoDAO.getTotalEgreso(this.caja);
|
||||
this.view.getTotalEgresosField().setText(String.valueOf(total));
|
||||
}
|
||||
|
||||
@@ -165,13 +172,14 @@ public class EgresosController {
|
||||
}
|
||||
}
|
||||
|
||||
private void guardarEgreso(String nro, String descripcion, String valor, TipoEgreso tipo) {
|
||||
if (this.validateInput(nro, descripcion, valor, tipo)) {
|
||||
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.setTipo(tipo);
|
||||
egreso.setTipoEgreso(tipo);
|
||||
egreso.setCaja(caja);
|
||||
egresoDAO.insertEgreso(egreso);
|
||||
this.view.getEgresosTableModel().addRow(egreso);
|
||||
this.updateTotalEgresos();
|
||||
@@ -179,12 +187,12 @@ public class EgresosController {
|
||||
}
|
||||
}
|
||||
|
||||
private void editarEgreso(String nro, String descripcion, String valor, TipoEgreso tipo) {
|
||||
if (this.validateInput(nro, descripcion, valor, tipo)) {
|
||||
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.setTipo(tipo);
|
||||
this.editingEgreso.setTipoEgreso(tipo);
|
||||
egresoDAO.updateEgreso(this.editingEgreso);
|
||||
this.view.getEgresosTableModel().setEgreso(this.editingId, this.editingEgreso);
|
||||
this.updateTotalEgresos();
|
||||
@@ -193,13 +201,14 @@ public class EgresosController {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean validateInput(String nro, String descripcion, String valor, TipoEgreso tipoEgreso) {
|
||||
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;
|
||||
}
|
||||
@@ -276,6 +285,10 @@ public class EgresosController {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean validateCaja(Caja caja){
|
||||
return caja != null;
|
||||
}
|
||||
|
||||
private void hideErrorMessages() {
|
||||
this.view.getErrorTipoEgreso().setVisible(false);
|
||||
this.view.getErrorValor().setVisible(false);
|
||||
|
||||
@@ -85,7 +85,7 @@ public class ManagerController {
|
||||
this.cajaDAO.insertCaja(caja);
|
||||
}
|
||||
this.ingresosController.updateCaja(caja);
|
||||
// this.egresosController.updateCaja(caja);
|
||||
this.egresosController.updateCaja(caja);
|
||||
// this.arqueoController.updateCaja(caja);
|
||||
|
||||
});
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
package danielcortes.xyz.models.egreso;
|
||||
|
||||
import danielcortes.xyz.models.caja.Caja;
|
||||
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
|
||||
|
||||
public class Egreso {
|
||||
@@ -32,24 +33,8 @@ public class Egreso {
|
||||
private String nro;
|
||||
private String descripcion;
|
||||
private int valor;
|
||||
private TipoEgreso tipo;
|
||||
|
||||
public Egreso(int id, String nro, String descripcion, int valor, TipoEgreso tipo) {
|
||||
this.id = id;
|
||||
this.nro = nro;
|
||||
this.descripcion = descripcion;
|
||||
this.valor = valor;
|
||||
this.tipo = tipo;
|
||||
}
|
||||
|
||||
public Egreso(String nro, String descripcion, int valor, TipoEgreso tipo) {
|
||||
this.nro = nro;
|
||||
this.descripcion = descripcion;
|
||||
this.valor = valor;
|
||||
this.tipo = tipo;
|
||||
}
|
||||
|
||||
public Egreso(){}
|
||||
private TipoEgreso tipoEgreso;
|
||||
private Caja caja;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
@@ -84,10 +69,18 @@ public class Egreso {
|
||||
}
|
||||
|
||||
public TipoEgreso getTipoEgreso() {
|
||||
return tipo;
|
||||
return tipoEgreso;
|
||||
}
|
||||
|
||||
public void setTipo(TipoEgreso tipo) {
|
||||
this.tipo = tipo;
|
||||
public void setTipoEgreso(TipoEgreso tipoEgreso) {
|
||||
this.tipoEgreso = tipoEgreso;
|
||||
}
|
||||
|
||||
public Caja getCaja() {
|
||||
return caja;
|
||||
}
|
||||
|
||||
public void setCaja(Caja caja) {
|
||||
this.caja = caja;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,17 +24,23 @@
|
||||
|
||||
package danielcortes.xyz.models.egreso;
|
||||
|
||||
import danielcortes.xyz.models.caja.Caja;
|
||||
import danielcortes.xyz.models.egreso.Egreso;
|
||||
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EgresoDAO {
|
||||
List<Egreso> findAll();
|
||||
List<Egreso> findById(int id);
|
||||
List<Egreso> findByCaja(Caja caja);
|
||||
List<Egreso> findByNro(String nro);
|
||||
List<Egreso> findByTipoEgreso(TipoEgreso tipoEgreso);
|
||||
|
||||
boolean insertEgreso(Egreso egreso);
|
||||
boolean updateEgreso(Egreso egreso);
|
||||
boolean deleteEgreso(Egreso egreso);
|
||||
int getTotalEgreso();
|
||||
|
||||
int getTotalEgreso(Caja caja);
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@
|
||||
package danielcortes.xyz.models.egreso;
|
||||
|
||||
import danielcortes.xyz.data.MysqlConnection;
|
||||
import danielcortes.xyz.models.caja.Caja;
|
||||
import danielcortes.xyz.models.caja.CajaDAO;
|
||||
import danielcortes.xyz.models.caja.MysqlCajaDAO;
|
||||
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
|
||||
import danielcortes.xyz.models.tipo_egreso.TipoEgresoDAO;
|
||||
import danielcortes.xyz.models.tipo_egreso.MysqlTipoEgresoDAO;
|
||||
@@ -62,6 +65,26 @@ public class MysqlEgresoDAO implements EgresoDAO {
|
||||
return egresoList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Egreso> findByCaja(Caja caja) {
|
||||
List<Egreso> egresoList = new ArrayList<>();
|
||||
try {
|
||||
Connection conn = mysqlConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement("select * from egresos where caja_id = ?");
|
||||
ps.setInt(1, caja.getId());
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
egresoList = this.egresosFromResultSet(rs);
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return egresoList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Egreso> findById(int id) {
|
||||
List<Egreso> egresoList = new ArrayList<>();
|
||||
@@ -102,16 +125,38 @@ public class MysqlEgresoDAO implements EgresoDAO {
|
||||
return egresoList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Egreso> findByTipoEgreso(TipoEgreso tipoEgreso) {
|
||||
List<Egreso> egresoList = new ArrayList<>();
|
||||
try {
|
||||
Connection conn = mysqlConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement("select * from egresos where tipo_egreso_id = ?");
|
||||
ps.setInt(1, tipoEgreso.getId());
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
egresoList = this.egresosFromResultSet(rs);
|
||||
|
||||
rs.close();
|
||||
ps.close();
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return egresoList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertEgreso(Egreso egreso) {
|
||||
int updates;
|
||||
try {
|
||||
Connection conn = mysqlConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement("insert into egresos (nro, descripcion, valor, tipo_egreso_id) values (?,?,?,?)");
|
||||
PreparedStatement ps = conn.prepareStatement("insert into egresos (nro, descripcion, valor, tipo_egreso_id, caja_id) values (?,?,?,?,?)");
|
||||
ps.setString(1,egreso.getNro());
|
||||
ps.setString(2,egreso.getDescripcion());
|
||||
ps.setInt(3,egreso.getValor());
|
||||
ps.setInt(4,egreso.getTipoEgreso().getId());
|
||||
ps.setInt(5, egreso.getCaja().getId());
|
||||
|
||||
updates = ps.executeUpdate();
|
||||
ps.close();
|
||||
|
||||
@@ -134,12 +179,13 @@ public class MysqlEgresoDAO implements EgresoDAO {
|
||||
int updates;
|
||||
try {
|
||||
Connection conn = mysqlConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement("update egresos set nro = ?, descripcion = ?, valor = ?, tipo_egreso_id = ? where id = ? ");
|
||||
PreparedStatement ps = conn.prepareStatement("update egresos set nro = ?, descripcion = ?, valor = ?, tipo_egreso_id = ?, caja_id = ? where id = ? ");
|
||||
ps.setString(1,egreso.getNro());
|
||||
ps.setString(2,egreso.getDescripcion());
|
||||
ps.setInt(3,egreso.getValor());
|
||||
ps.setInt(4,egreso.getTipoEgreso().getId());
|
||||
ps.setInt(5, egreso.getId());
|
||||
ps.setInt(5, egreso.getCaja().getId());
|
||||
ps.setInt(6, egreso.getId());
|
||||
|
||||
updates = ps.executeUpdate();
|
||||
|
||||
@@ -172,11 +218,12 @@ public class MysqlEgresoDAO implements EgresoDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalEgreso() {
|
||||
public int getTotalEgreso(Caja caja) {
|
||||
int total = 0;
|
||||
try {
|
||||
Connection conn = mysqlConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement("select sum(valor) from egresos");
|
||||
PreparedStatement ps = conn.prepareStatement("select sum(valor) from egresos where caja_id = ?");
|
||||
ps.setInt(1, caja.getId());
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
rs.next();
|
||||
@@ -198,13 +245,18 @@ public class MysqlEgresoDAO implements EgresoDAO {
|
||||
TipoEgresoDAO tipoEgresoDAO = new MysqlTipoEgresoDAO();
|
||||
TipoEgreso tipoEgreso = tipoEgresoDAO.findById(tipoEgresoId).get(0);
|
||||
|
||||
int cajaId = rs.getInt("caja_id");
|
||||
CajaDAO cajaDAO = new MysqlCajaDAO();
|
||||
Caja caja = cajaDAO.findById(cajaId);
|
||||
|
||||
Egreso egreso = new Egreso();
|
||||
|
||||
egreso.setId(rs.getInt("id"));
|
||||
egreso.setNro(rs.getString("nro"));
|
||||
egreso.setDescripcion(rs.getString("descripcion"));
|
||||
egreso.setValor(rs.getInt("valor"));
|
||||
egreso.setTipo(tipoEgreso);
|
||||
egreso.setTipoEgreso(tipoEgreso);
|
||||
egreso.setCaja(caja);
|
||||
|
||||
egresoList.add(egreso);
|
||||
}
|
||||
|
||||
@@ -143,6 +143,7 @@ public class MysqlIngresoDAO implements IngresoDAO{
|
||||
rs.next();
|
||||
ingreso.setId(rs.getInt(1));
|
||||
|
||||
rs.close();
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
@@ -161,7 +162,9 @@ public class MysqlIngresoDAO implements IngresoDAO{
|
||||
ps.setInt(2, ingreso.getTipoIngreso().getId());
|
||||
ps.setInt(3, ingreso.getCaja().getId());
|
||||
ps.setInt(4, ingreso.getId());
|
||||
|
||||
updates = ps.executeUpdate();
|
||||
|
||||
ps.close();
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
|
||||
@@ -53,12 +53,26 @@ public class EgresosTableModel extends AbstractTableModel {
|
||||
|
||||
public void addRow(Egreso egreso) {
|
||||
rows.add(egreso);
|
||||
this.fireTableRowsInserted(0,getRowCount()-1);
|
||||
this.fireTableRowsInserted(getRowCount()-1, getRowCount()-1);
|
||||
|
||||
}
|
||||
|
||||
public void removeRow(int row){
|
||||
this.rows.remove(row);
|
||||
this.fireTableRowsDeleted(0,getRowCount()-1);
|
||||
this.fireTableRowsDeleted(row,row);
|
||||
}
|
||||
|
||||
public void removeRows(){
|
||||
int rowCount = getRowCount();
|
||||
if(rowCount > 0){
|
||||
this.rows.clear();
|
||||
this.fireTableRowsDeleted(0, rowCount-1);
|
||||
}
|
||||
}
|
||||
|
||||
public void setEgreso(int editingId, Egreso egreso) {
|
||||
this.rows.set(editingId, egreso);
|
||||
this.fireTableRowsUpdated(0,getRowCount()-1);
|
||||
}
|
||||
|
||||
public Object getValueAt(int row, int col) {
|
||||
@@ -79,8 +93,4 @@ public class EgresosTableModel extends AbstractTableModel {
|
||||
return rows.get(row);
|
||||
}
|
||||
|
||||
public void setEgreso(int editingId, Egreso egreso) {
|
||||
this.rows.set(editingId, egreso);
|
||||
this.fireTableRowsUpdated(0,getRowCount()-1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,12 +65,10 @@ public class IngresosTableModel extends AbstractTableModel {
|
||||
int rowCount = getRowCount();
|
||||
if(rowCount > 0){
|
||||
this.rows.clear();
|
||||
System.out.println("deleted from " + 0 + " to " + rowCount);
|
||||
this.fireTableRowsDeleted(0, rowCount-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object getValueAt(int row, int col) {
|
||||
switch (col) {
|
||||
case 0:
|
||||
|
||||
Reference in New Issue
Block a user