Ejemplo de un Sistema basico en JAVA con IDE NETBEANS. Con imagenes de los formularios. Tiene Insertar Registros ABM, Consultas y más
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
public class Articulo extends javax.swing.JInternalFrame { ResultSet rs; ResultSet rsArti; char accion='z'; ConeccionBD bd = new ConeccionBD();
public Articulo() { initComponents(); setTitle("Formulario de Articulos"); ConeccionBD bd = new ConeccionBD(); cargacombo(); DefaultTableModel dm= new DefaultTableModel(){ public boolean isCellEditable(int row,int col){ return false; } }; dm.setDataVector( new Object[][]{}, new String[]{"Codigo","Nombre Articulo","Clasificacion"}); grdArticulo.setModel(dm); grdArticulo.getTableHeader().setReorderingAllowed(false); grdArticulo.getColumnModel().getColumn(0).setPreferredWidth(30); grdArticulo.getColumnModel().getColumn(1).setPreferredWidth(200); grdArticulo.getColumnModel().getColumn(1).setPreferredWidth(170); this.actualizarGrilla(); this.habilitar(false); } private void habilitar(boolean estado){ this.TxtNombre.setEditable(estado); this.cboClasificacion.setEnabled(estado); this.cmdNuevo.setEnabled(!estado); this.cmdModificar.setEnabled(!estado); this.cmdEliminar.setEnabled(!estado); this.cmdGuardar.setEnabled(false); this.cmdCancelar.setEnabled(estado); this.TxtCodigo.setText(""); this.TxtNombre.setText("");
public class InfVentas extends javax.swing.JInternalFrame { char opc = 'Z'; String variable; boolean bisiestor, valida;
public InfVentas() { initComponents(); ConeccionBD bd = new ConeccionBD(); bd.cargarCombo("select * from clientes order by id_cliente",cboCliente); bd.cargarCombo("select * from Articulo order by codigo",cboArticulo); }private void cmdVistaActionPerformed(java.awt.event.ActionEvent evt) { String consulta;consulta = "SELECT venta_cab.`nro_factura` AS Factura,"+ "venta_cab.`fecha` AS Fecha,"+ "clientes.`cliente` AS Cliente,"+ "venta_det.`id_articulo` AS Cod_Articulo,"+ "articulo.`articulo` AS Articulo,"+ "venta_det.`cantidad` AS Cantidad,"+ "venta_det.`precio` AS Precio,"+ "venta_det.`subtotal` AS Subtotal,"+ "venta_cab.`total` AS Total FROM"+ "`venta_cab` venta_cab INNER JOIN `venta_det` venta_det ON venta_cab.`nro_factura` = venta_det.`nro_factura`"+ "INNER JOIN `clientes` clientes ON venta_cab.`id_cliente` = clientes.`id_cliente`"+ "INNER JOIN `articulo` articulo ON venta_det.`id_articulo` = articulo.`codigo`";Integer numOpc = this.Opciones.getSelectedIndex();switch (numOpc){case 0:
- 5 -
opc = 'F'; break;case 1: opc = 'C'; break;case 2: opc = 'A'; break;case 3: opc = 'T'; break;case 4: opc = 'M'; break;} switch (opc){case 'F': if (txtFecIni.getText().isEmpty()){ txtFecIni.setText(txtFecFin.getText()); } if (txtFecFin.getText().isEmpty()){ txtFecFin.setText(txtFecIni.getText()); } String FechaIni = txtFecIni.getText(); cambiarFecha(FechaIni); FechaIni = variable; String FechaFin = txtFecFin.getText(); cambiarFecha(FechaFin); FechaFin = variable; consulta = "SELECT venta_cab.`nro_factura` AS Factura,"+ "venta_cab.`fecha` AS Fecha,"+ "clientes.`cliente` AS Cliente,"+ "venta_det.`id_articulo` AS Cod_Articulo,"+ "articulo.`articulo` AS Articulo,"+ "venta_det.`cantidad` AS Cantidad,"+ "venta_det.`precio` AS Precio,"+ "venta_det.`subtotal` AS Subtotal,"+ "venta_cab.`total` AS Total FROM"+ "`venta_cab` venta_cab INNER JOIN `venta_det` venta_det ON venta_cab.`nro_factura` = venta_det.`nro_factura`"+ "INNER JOIN `clientes` clientes ON venta_cab.`id_cliente` = clientes.`id_cliente`"+ "INNER JOIN `articulo` articulo ON venta_det.`id_articulo` = articulo.`codigo`"+ "WHERE fecha BETWEEN '"+FechaIni+"' and '"+FechaFin+"' ORDER BY venta_cab.nro_factura" ; break;case 'C': String clientebusca = (((String[])this.cboCliente.getSelectedItem())[1]); consulta = "SELECT venta_cab.`nro_factura` AS Factura,"+ "venta_cab.`fecha` AS Fecha,"+ "clientes.`cliente` AS Cliente,"+ "venta_det.`id_articulo` AS Cod_Articulo,"+ "articulo.`articulo` AS Articulo,"+ "venta_det.`cantidad` AS Cantidad,"+ "venta_det.`precio` AS Precio,"+ "venta_det.`subtotal` AS Subtotal,"+ "venta_cab.`total` AS Total FROM"+ "`venta_cab` venta_cab INNER JOIN `venta_det` venta_det ON venta_cab.`nro_factura` = venta_det.`nro_factura`"+ "INNER JOIN `clientes` clientes ON venta_cab.`id_cliente` = clientes.`id_cliente`"+ "INNER JOIN `articulo` articulo ON venta_det.`id_articulo` = articulo.`codigo`"+ "WHERE cliente LIKE '%"+clientebusca+"%' ORDER BY venta_cab.nro_factura" ;break;case 'A': if (!this.txtCodArticulo.getText().isEmpty()){ String codarticulobusca = String.valueOf(this.txtCodArticulo.getText()); consulta = "SELECT venta_cab.`nro_factura` AS Factura,"+ "venta_cab.`fecha` AS Fecha,"+ "clientes.`cliente` AS Cliente,"+ "venta_det.`id_articulo` AS Cod_Articulo,"+
- 6 -
"articulo.`articulo` AS Articulo,"+ "venta_det.`cantidad` AS Cantidad,"+ "venta_det.`precio` AS Precio,"+ "venta_det.`subtotal` AS Subtotal,"+ "venta_cab.`total` AS Total FROM"+ "`venta_cab` venta_cab INNER JOIN `venta_det` venta_det ON venta_cab.`nro_factura` = venta_det.`nro_factura`"+ "INNER JOIN `clientes` clientes ON venta_cab.`id_cliente` = clientes.`id_cliente`"+ "INNER JOIN `articulo` articulo ON venta_det.`id_articulo` = articulo.`codigo`"+ "WHERE id_articulo = '"+codarticulobusca+"' ORDER BY venta_cab.nro_factura" ; }else{ String articulobusca = (((String[])this.cboArticulo.getSelectedItem())[1]); consulta = "SELECT venta_cab.`nro_factura` AS Factura,"+ "venta_cab.`fecha` AS Fecha,"+ "clientes.`cliente` AS Cliente,"+ "venta_det.`id_articulo` AS Cod_Articulo,"+ "articulo.`articulo` AS Articulo,"+ "venta_det.`cantidad` AS Cantidad,"+ "venta_det.`precio` AS Precio,"+ "venta_det.`subtotal` AS Subtotal,"+ "venta_cab.`total` AS Total FROM"+ "`venta_cab` venta_cab INNER JOIN `venta_det` venta_det ON venta_cab.`nro_factura` = venta_det.`nro_factura`"+ "INNER JOIN `clientes` clientes ON venta_cab.`id_cliente` = clientes.`id_cliente`"+ "INNER JOIN `articulo` articulo ON venta_det.`id_articulo` = articulo.`codigo`"+ "WHERE articulo = '"+articulobusca+"' ORDER BY venta_cab.nro_factura" ; }break;case 'T': if (!this.txtFactura.getText().isEmpty()){ Integer facturabusca = Integer.valueOf(this.txtFactura.getText()); consulta = "SELECT venta_cab.`nro_factura` AS Factura,"+ "venta_cab.`fecha` AS Fecha,"+ "clientes.`cliente` AS Cliente,"+ "venta_det.`id_articulo` AS Cod_Articulo,"+ "articulo.`articulo` AS Articulo,"+ "venta_det.`cantidad` AS Cantidad,"+ "venta_det.`precio` AS Precio,"+ "venta_det.`subtotal` AS Subtotal,"+ "venta_cab.`total` AS Total FROM"+ "`venta_cab` venta_cab INNER JOIN `venta_det` venta_det ON venta_cab.`nro_factura` = venta_det.`nro_factura`"+ "INNER JOIN `clientes` clientes ON venta_cab.`id_cliente` = clientes.`id_cliente`"+ "INNER JOIN `articulo` articulo ON venta_det.`id_articulo` = articulo.`codigo`"+ "WHERE venta_cab.`nro_factura` ="+facturabusca+" ORDER BY venta_cab.nro_factura" ; }else{ }break;case 'M': String Fecha = txtFecha.getText(); if (txtFecha.getText().isEmpty()){ java.util.Date fechas = new Date(); Fecha = String.valueOf(fechas.getDate() + "-" + String.valueOf(fechas.getMonth() + 1) + "-" + String.valueOf(fechas.getYear() + 1900)); } cambiarFecha(Fecha);
Fecha = variable; consulta = "SELECT venta_cab.`nro_factura` AS Factura,"+ "venta_cab.`fecha` AS Fecha,"+ "clientes.`cliente` AS Cliente,"+ "venta_det.`id_articulo` AS Cod_Articulo,"+ "articulo.`articulo` AS Articulo,"+ "venta_det.`cantidad` AS Cantidad,"+ "venta_det.`precio` AS Precio,"+ "venta_det.`subtotal` AS Subtotal,"+ "venta_cab.`total` AS Total FROM"+ "`venta_cab` venta_cab INNER JOIN `venta_det` venta_det ON venta_cab.`nro_factura` = venta_det.`nro_factura`"+ "INNER JOIN `clientes` clientes ON venta_cab.`id_cliente` = clientes.`id_cliente`"+ "INNER JOIN `articulo` articulo ON venta_det.`id_articulo` = articulo.`codigo`"+
- 7 -
"WHERE fecha BETWEEN '"+Fecha+"' and '"+Fecha+"' ORDER BY venta_cab.nro_factura" ;break;}
Listado lista = new Listado(); try { lista.mostrar("C:\\Julio_Project\\Informes\\Inf_Ventas.jasper",consulta); } catch (Exception e) { JOptionPane.showMessageDialog(null, "NO se encontro Archivo .jasper", "ERROR", JOptionPane.OK_OPTION); } }
public class Ventas extends javax.swing.JInternalFrame { ConeccionBD bd = new ConeccionBD(); ResultSet rsCab; ResultSet rsDet; ResultSet rsCliente; ResultSet rsArticulo; /** Creates new form Ventas */ public Ventas() { initComponents(); setTitle("Formulario de Ventas - Articulos"); if (!bd.estaConectado()) { JOptionPane.showMessageDialog(null, "Error en la coneccion", "Base de Datos - ERROR!!!", JOptionPane.ERROR_MESSAGE); }else{ this.cboCliente.removeAllItems(); bd.cargarCombo("select * from clientes order by cliente", cboCliente); this.cboArticulo.removeAllItems(); bd.cargarCombo("select * from articulo", cboArticulo); } DefaultTableModel modelo = new DefaultTableModel(){ @Override public boolean isCellEditable (int row, int col){ return false; } }; this.habilitar(false);
- 10 -
modelo.setDataVector( new Object [][]{}, new String[]{"Codigo","Cant.","Articulo","Precio","Subtotal"}); this.gridDetalle.setModel(modelo); this.gridDetalle.getTableHeader().setReorderingAllowed(false); this.gridDetalle.getColumnModel().getColumn(0).setPreferredWidth(80); this.gridDetalle.getColumnModel().getColumn(1).setPreferredWidth(60); this.gridDetalle.getColumnModel().getColumn(2).setPreferredWidth(300); this.gridDetalle.getColumnModel().getColumn(3).setPreferredWidth(80); this.gridDetalle.getColumnModel().getColumn(4).setPreferredWidth(90); this.gridDetalle.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); final DecimalFormat formatter = new DecimalFormat("###,##0,00"); this.gridDetalle.getColumn("Cant.").setCellRenderer(new DecimalRenderer (formatter)); this.gridDetalle.getColumn("Precio").setCellRenderer(new DecimalRenderer (formatter)); this.gridDetalle.getColumn("Subtotal").setCellRenderer(new DecimalRenderer (formatter)); }private void establecerFecha(){ Calendar calendario = Calendar.getInstance(); String anho = String.valueOf(calendario.get(Calendar.YEAR)); String mes; int numMes = Integer.valueOf(calendario.get(Calendar.MONTH)+1); if (numMes<10){ mes = "0"+String.valueOf(numMes); }else{ mes = String.valueOf(numMes); } String dia; int numDia = Integer.valueOf(calendario.get(Calendar.DAY_OF_MONTH)); if (numDia<10){ dia = "0"+String.valueOf(numDia); }else{ dia = String.valueOf(numDia); } this.txtFecha.setText(anho+"-"+mes+"-"+dia); } private void habilitar(boolean estado){ this.txtFactura.setEditable(false); this.txtCantidad.setEditable(estado); this.cboArticulo.setEnabled(estado); this.cboCliente.setEnabled(estado); this.cmdAnular.setEnabled(!estado); bd.cargarCombo("select * from articulo", cboArticulo); } private void limpiar(){ this.txtCantidad.setText(""); this.txtDireccion.setText(""); this.txtFactura.setText(""); this.txtPrecio.setText(""); this.txtRuc.setText(""); this.txtTotal.setText("0"); } private void vaciarGrilla(){ DefaultTableModel modelo = (DefaultTableModel) this.gridDetalle.getModel(); this.gridDetalle.selectAll(); int[] cant_filas = this.gridDetalle.getSelectedRows(); for (int i=(cant_filas.length - 1); i>=0;i--){ modelo.removeRow(cant_filas[i]); } } private void controlError (String msj) { JOptionPane.showMessageDialog(null, "Error al intentar acceder a la Base de Datos\n"+msj, "ERROR DE CONECTIVIDAD", JOptionPane.ERROR_MESSAGE); this.cmdCancelarActionPerformed(null); }
private void gridDetalleFocusGained(java.awt.event.FocusEvent evt) { // si la cantidad de registros del grid es mayot a 0, habilita el boton quitarif (this.gridDetalle.getRowCount() > 0){ this.cmdQuitar.setEnabled(true);}}
- 12 -
AGREGARprivate void cmdAgregarActionPerformed(java.awt.event.ActionEvent evt) { ResultSet rsStock;if (this.txtCantidad.getText().isEmpty()){ JOptionPane.showMessageDialog(null, "Vuelva a introducir la cantidad", "Ventas", JOptionPane.INFORMATION_MESSAGE); this.txtCantidad.requestFocus();}else{ boolean yaexiste= false, sobrepasa= false; DefaultTableModel modelo = (DefaultTableModel) this.gridDetalle.getModel(); int cant = Integer.parseInt(this.txtCantidad.getText()); int precio = Integer.parseInt(this.txtPrecio.getText()); int subtotal = cant * precio; String cod_art = (((String [])this.cboArticulo.getSelectedItem())[0]); String nom_art = (((String [])this.cboArticulo.getSelectedItem())[1]); rsStock = bd.dameLista("select cantidad from articulo where codigo= "+cod_art); try{ rsStock.first(); if (cant>rsStock.getInt("cantidad")){ sobrepasa = true; } }catch (SQLException ex){ this.controlError(ex.toString()); } Integer cant_grilla, sub_grilla; String cod_grilla; Integer cant_registros = this.gridDetalle.getRowCount(); if (cant_registros > 0 && !sobrepasa){ for (int i=0; i< cant_registros; i++){ cod_grilla = (String) gridDetalle.getValueAt(i, 0); if (cod_grilla.equalsIgnoreCase(cod_art)){ cant_grilla = (Integer) this.gridDetalle.getValueAt(i, 1); sub_grilla = (Integer) this.gridDetalle.getValueAt(i, 4); int nuevaCant = (cant + cant_grilla); try{ if (nuevaCant <= rsStock.getInt("cantidad")) { int nuevoSub = subtotal + sub_grilla; this.gridDetalle.setValueAt((Object)nuevaCant, i, 1); this.gridDetalle.setValueAt((Object)nuevoSub, i, 4); }else{ sobrepasa = true; } }catch (SQLException ex) { this.controlError(ex.toString()); } yaexiste = true; } } } if (sobrepasa){ try{ JOptionPane.showMessageDialog(null, "Ha sobrepasado la cantidad existente en Stock: " + rsStock.getString("cantidad"), "Ventas", JOptionPane.INFORMATION_MESSAGE); }catch (SQLException ex) { this.controlError(ex.toString()); } }else{ if (!yaexiste){ modelo.addRow(new Object[]{cod_art, cant, nom_art, precio, subtotal}); this.cmdGuardar.setEnabled(true); } int v_total = Integer.parseInt(this.txtTotal.getText()) + subtotal; this.txtTotal.setText(String.valueOf(v_total)); } this.txtCantidad.setText(""); this.cmdAgregar.setEnabled(false); this.cboArticulo.requestFocus(); }}
- 13 -
QUITARprivate void cmdQuitarActionPerformed(java.awt.event.ActionEvent evt) { int [] filaSeleccionada = this.gridDetalle.getSelectedRows();DefaultTableModel modelo = (DefaultTableModel) this.gridDetalle.getModel();if (filaSeleccionada.length >0){ for (int i = (filaSeleccionada.length - 1); i >=0; i--){ modelo.removeRow(filaSeleccionada[i]); } if (this.gridDetalle.getRowCount() < 1){ this.txtTotal.setText("0"); this.cmdGuardar.setEnabled(false); }else{ int subtotal = 0; int filas = this.gridDetalle.getRowCount(); for(int i = 0; i<filas; i++){ int sub_grilla = (Integer) this.gridDetalle.getValueAt(i, 4); subtotal = subtotal + sub_grilla; } this.txtTotal.setText(String.valueOf(subtotal)); } this.cboArticulo.requestFocus(); this.cmdQuitar.setEnabled(false);}else{ JOptionPane.showMessageDialog(null, "Para QUITAR un Articulo debe seleccionarlo primero", "QUITAR ARTICULO",JOptionPane.INFORMATION_MESSAGE); }}
private void txtFacturaFocusLost(java.awt.event.FocusEvent evt) { if (!this.txtFactura.getText().isEmpty()){ rsCab = bd.dameLista("select * from venta_cab where anulado = 0 and nro_factura= "+this.txtFactura.getText()); try{ if (rsCab.first()){ this.txtFecha.setText(rsCab.getString("fecha")); Object cboValores; for (int i = 0; i<=this.cboCliente.getItemCount();i++){ cboValores = this.cboCliente.getItemAt(i); if ((((String[])cboValores)[0]).equals(rsCab.getString("id_cliente"))){ this.cboCliente.setSelectedIndex(i); break; } } this.cboClienteFocusLost(null); rsDet = bd.dameLista("select * from venta_det where nro_factura= "+ this.txtFactura.getText()); DefaultTableModel modelo = (DefaultTableModel) this.gridDetalle.getModel(); int total=0; while (rsDet.next()){ rsArticulo = bd.dameLista("select articulo from articulo where codigo= "+ rsDet.getString("id_articulo")); rsArticulo.first(); String nom_art = rsArticulo.getString("articulo"); String id_art = rsDet.getString("id_articulo"); String cant = rsDet.getString("cantidad"); String precio = rsDet.getString("precio"); String subtotal = rsDet.getString("subtotal"); total = total + rsDet.getInt("subtotal"); modelo.addRow(new Object[]{id_art,cant,nom_art,precio,subtotal}); } this.txtTotal.setText(String.valueOf(total)); Object[] opciones={"Si","No"}; int resp= JOptionPane.showOptionDialog(null, "Esta seguro de ANULAR la factura? ", "Ventas", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, opciones, opciones[0]); if(resp==JOptionPane.YES_OPTION){ rsDet.beforeFirst(); while (rsDet.next()){ bd.actualizarRegistro("articulo", "cantidad= cantidad + "+ rsDet.getString("cantidad"), "codigo= "+ rsDet.getString("id_articulo")); }
- 14 -
} }else{ JOptionPane.showMessageDialog(null, "No se encuentra la Factura digitada o ya está anulada", "Ventas",JOptionPane.INFORMATION_MESSAGE); } }catch (Exception e){ this.controlError(e.toString()); }}this.establecerFecha();this.cmdCancelarActionPerformed(null);this.limpiar();}