Facturacion en Java
-
Upload
alberto-moreno-cueva -
Category
Documents
-
view
19.740 -
download
2
description
Transcript of Facturacion en Java
-1- LPI
Prof. Ing. Alberto Moreno C
APLICACIÓN ELABORACIÓN DE UNA FACTURA.
Objetivo.- En esta aplicación de la elaboración de una factura se aplica todos losprocesos vistos en clases así como consultas , inserción , eliminación de registrosComo se muestra en la figura.
Esta aplicación es utilizando la base de datos BDVENTAS , el diseño es como semuestra en la figura 2.Los controles empleados :
Etiquetas (jlabel): lbfe (fecha) , lbpre (precio) , lbrno( factura)
JtextBox : txtcan ( cantidad)
Jcombobox : combo1 ( para la Descripción)
JTabla : tabla1 ( para mostrar los productos)
-2- LPI
Prof. Ing. Alberto Moreno C
Codificación :Organización de los paquetes:
package GUI;import List.*;import java.util.*;import Clases.Articulos;import javax.swing.table.DefaultTableModel;import java.text.SimpleDateFormat;import javax.swing.JOptionPane;public class FrFactura extends javax.swing.JFrame { ArrayList lista=new ArrayList(); DefaultTableModel dt=new DefaultTableModel();double pre=0; public FrFactura() { initComponents();
-3- LPI
Prof. Ing. Alberto Moreno C
tabla1.setModel(dt); dt.addColumn("Codigo"); dt.addColumn("Descripcion"); dt.addColumn("Precio"); dt.addColumn("Cantidad"); dt.addColumn("Total"); FormateaGrid(); LlenaCombo(); }
//para dar ancho a cada columna que por defecto es de 10 caracteres. void FormateaGrid(){ tabla1.getColumnModel().getColumn(0).setPreferredWidth(50); tabla1.getColumnModel().getColumn(1).setPreferredWidth(180); tabla1.getColumnModel().getColumn(2).setPreferredWidth(80); tabla1.getColumnModel().getColumn(2).setPreferredWidth(80); tabla1.getColumnModel().getColumn(2).setPreferredWidth(80); } void LlenaCombo(){ lista=(ArrayList) ArticulosDB.ListaArt(); Articulos x; this.combo1.removeAllItems(); for(int f=0; f<lista.size();f++){
combo1.addItem(ArticulosDB.LeerArt(f).getNomar()); combo1.insertItemAt(ArticulosDB.LeerArt(f).getNomar(),f );
} } private void btnGrabaActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here: String factura=ArticulosDB.GeneraFac(); this.lbfac.setText(factura); ArticulosDB.InsertaFactura(factura,this.lbfecha.getText()); //insertar detalle String coda; int can; for(int f=0; f<dt.getRowCount();f++){ coda=dt.getValueAt(f,0).toString(); can=(Integer)(dt.getValueAt(f,3)); ArticulosDB.InsertaDeta(factura,coda,can); }
JOptionPane.showMessageDialog(this,"Registro Grabado");
}
private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {
-4- LPI
Prof. Ing. Alberto Moreno C
// TODO add your handling code here: this.txtcan.setText(""); this.lbstot.setText(""); dt.setRowCount(0);// coloca la tabla a cero filas this.txtcan.requestFocus();
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {// TODO add your handling code here: Date fecha=new Date(); SimpleDateFormat sd=new SimpleDateFormat("dd/MM/yyyy");
lbfecha.setText(""+sd.format(fecha));
}
private void btnBorraActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:
int nro= this.tabla1.getSelectedRow(); if(nro==-1) return; dt.removeRow(nro); Totaliza();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//agregar datos a la tabla int can; double tot; int pos=this.combo1.getSelectedIndex(); String codp=ArticulosDB.LeerArt(pos).getCoda(); can=Integer.parseInt(this.txtcan.getText()); tot=can*pre; dt.addRow(new Object[]{codp, this.combo1.getSelectedItem(), pre,can,tot
});
Totaliza(); }
void Totaliza(){ double sm=0; for(int f=0;f<dt.getRowCount();f++){ sm += (Double)(dt.getValueAt(f,4));
-5- LPI
Prof. Ing. Alberto Moreno C
} this.lbstot.setText(""+sm);
}
private void combo1ItemStateChanged(java.awt.event.ItemEvent evt) {// TODO add your handling code here: int posicion= combo1.getSelectedIndex(); try{
pre=ArticulosDB.LeerArt(posicion).getPrecio(); this.lbprecio.setText(""+pre); }
catch(Exception ex){
}
}
/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new FrFactura().setVisible(true); } }); }
la clase ArticulosDB
package List;import Clases.Articulos;import java.util.*;import Data.DataBase;import java.sql.*;import java.text.DecimalFormat;public class ArticulosDB { public static int nro=0; public static ArrayList ListaArt(){ Connection cn; ArrayList lista=new ArrayList(); try{ cn= DataBase.GetConnection();
-6- LPI
Prof. Ing. Alberto Moreno C
Statement cmd=cn.createStatement(); ResultSet rs=cmd.executeQuery(" select art_cod,art_nom,art_pre fromArticulos order by art_cod"); while(rs.next()){ Articulos a=new Articulos(); a.setCoda(rs.getString(1)); a.setNomar(rs.getString(2)); a.setPrecio(rs.getDouble(3)); lista.add(a);
} }catch(SQLException ex){ ex.printStackTrace(); } return lista; }
public static Articulos LeerArt(int pos){ return (Articulos)ListaArt().get(pos); }
public static boolean InsertaFactura(String fac,String fec){ boolean sw=false; Connection cn=null; PreparedStatement cmd=null; try{ cn=DataBase.GetConnection(); cmd=cn.prepareStatement("insert into fac_cabe values(?,?,?,?,?)"); cmd.setString(1,fac); cmd.setString(2,fec); cmd.setString(3,"c0002"); cmd.setString(4,"S"); cmd.setInt(5,2); cmd.executeUpdate(); cmd.close(); cn.close(); sw=true; }catch(SQLException ex){
sw=false; } return sw;
}
public static boolean InsertaDeta(String fac,String coda,int can){ boolean sw=false; Connection cn=null;
-7- LPI
Prof. Ing. Alberto Moreno C
PreparedStatement cmd=null; try{ cn=DataBase.GetConnection(); cmd=cn.prepareStatement("insert into fac_deta values(?,?,?)"); cmd.setString(1,fac); cmd.setString(2,coda); cmd.setInt(3,can); cmd.executeUpdate(); cmd.close(); cn.close(); sw=true; }catch(SQLException ex){
sw=false; } return sw;
}
public static String GeneraFac(){ String cod=""; Connection cn=null; String cad, sql="select max(fac_num) from fac_cabe"; DecimalFormat sd=new DecimalFormat("0000"); int nro; try{ cn=DataBase.GetConnection(); Statement cmd=cn.createStatement(); ResultSet rs=cmd.executeQuery(sql); if(rs.next()){ cad=rs.getString(1).substring(1).trim(); nro=Integer.parseInt(cad)+1; cod="F"+sd.format(nro);
}
}catch(SQLException ex){ ex.printStackTrace(); }
return cod;
}
-8- LPI
Prof. Ing. Alberto Moreno C
}
la clase conexión
package Data;import java.sql.*;
import javax.swing.JOptionPane;public class DataBase {
public static Connection GetConnection(){ Connection cn=null; try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn=DriverManager.getConnection("jdbc:odbc:odventas","sa",""); System.out.print("conexion ok!"); } catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage()); //System.out.print("conexion incorrecta !!"+ex.getMessage()); } return cn; }
public static void CierraConex(Connection cn){ try{ if(cn!=null) cn.close(); }catch(SQLException ex){ ex.printStackTrace(); }
}
-9- LPI
Prof. Ing. Alberto Moreno C