Anda di halaman 1dari 10

FACTURA BOLETA

BD. SQL SERVER


CREATE DATABASE SISTEMA GO USE SISTEMA GO CREATE TABLE CLIENTES (IDCLI VARCHAR(5)NOT NULL , NOMBRE VARCHAR(20)NOT NULL, APELLIDOS VARCHAR(50)NOT NULL, SEXO VARCHAR(1)NULL, Primary Key (IDCLI)) GO INSERT INTO CLIENTES VALUES('C0001','LUIS','GOMEZ','M') INSERT INTO CLIENTES VALUES('C0002','CARLOS','SANCHEZ','M') INSERT INTO CLIENTES VALUES('C0003','ELIANA','CASTRO','M') GO CREATE TABLE EMPLEADOS (IDEMP VARCHAR(5)NOT NULL, NOMBRE VARCHAR(20)NOT NULL, APELLIDOS VARCHAR(50)NOT NULL, SEXO VARCHAR(1)NULL, Primary Key (IDEMP)) GO INSERT INTO EMPLEADOS VALUES('E0001','LUIS','GOMEZ','M') INSERT INTO EMPLEADOS VALUES('E0002','FERNANDA','PEREZ','M') INSERT INTO EMPLEADOS VALUES('E0003','LUISA','VERASTEGUI','M') GO --DOCUMENTO-CREATE TABLE CAB_DOCUMENTO (NDOC VARCHAR (5) NOT NULL, TIP_DOC VARCHAR (30) NOT NULL, IDCLI VARCHAR(5) NOT NULL, IDEMP VARCHAR (5)NOT NULL, FECHA DATETIME NOT NULL, SUBTOTAL REAL NULL, IGV REAL NULL, TOTAL REAL NULL, ESTADO VARCHAR(20) NOT NULL, PRIMARY KEY(NDOC,TIP_DOC)) GO CREATE TABLE DETALLE_DOCUMENTO( NDOC VARCHAR (5)NOT NULL, TIP_DOC VARCHAR (30) NOT NULL, ITEM INT NOT NULL, PRODUCTO VARCHAR(20) NOT NULL, PRECIO REAL NULL, CANTIDAD INT NULL, PRIMARY KEY(ITEM,NDOC,TIP_DOC)) GO ALTER TABLE CAB_DOCUMENTO ADD FOREIGN KEY(IDCLI) REFERENCES CLIENTES http://sistemasddm.blogspot.com

ALTER TABLE CAB_DOCUMENTO ADD FOREIGN KEY(IDEMP) REFERENCES EMPLEADOS ALTER TABLE DETALLE_DOCUMENTO ADD FOREIGN KEY (NDOC,TIP_DOC) REFERENCES CAB_DOCUMENTO GO --CREACION DE LA TABLA GENERADOR GO CREATE TABLE GENERADOR( PARAMETRO Varchar(40) Not Null, ULTIMO Int Null ) GO -- INSERCCION DE DATOS EN LA TABLA GENERADOR INSERT INTO GENERADOR values('DOCUMENTO',0) GO CREATE PROCEDURE SP_MANTEDOCUMENTO @NDO VARCHAR (5), @TIP VARCHAR (30), @IDC VARCHAR(5), @IDE VARCHAR (5), @FEC DATETIME , @SUBTOT REAL, @IGV REAL, @TOT REAL, @EST VARCHAR(20) AS BEGIN INSERT INTO CAB_DOCUMENTO VALUES (@NDO, @TIP, @IDC,@IDE, @FEC,@SUBTOT,@IGV,@TOT,@EST) END GO SELECT * FROM CLIENTES SELECT * FROM EMPLEADOS SELECT * FROM CAB_DOCUMENTO SELECT * FROM DETALLE_DOCUMENTO SELECT * FROM GENERADOR

APLICACIN. VISUAL STUDIO.NET

http://sistemasddm.blogspot.com

http://sistemasddm.blogspot.com

CODIGO Facturacion (Form1.vb) Imports System.Data.SqlClient Public Class Facturacion Dim fila As Integer = -1 Dim TIPO As String = "" Dim D As Integer = 0 Dim pre As Double Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.txtFecha.Text = Now End Sub Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click Me.DatosGrid.Rows.Add("") End Sub Private Sub btnQuitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuitar.Click If fila <> -1 Then Me.DatosGrid.Rows.RemoveAt(fila) fila = -1 Else MsgBox("Debe eliminar una fila") End If End Sub Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click Try 'CONDICIONES EN BOLETA If radBoleta.Checked = True Then TIPO = "BOLETA" End If If radFactura.Checked = True Then TIPO = "FACTURA" End If 'CABECERA cn.Open() Dim cmd As New SqlCommand("SP_MANTEDOCUMENTO", cn) Dim prm As New SqlParameter With cmd .CommandType = CommandType.StoredProcedure prm = .Parameters.Add("@NDO", SqlDbType.VarChar, 5) prm.Value = Me.txtNum.Text prm = .Parameters.Add("@TIP", SqlDbType.VarChar, 30) prm.Value = TIPO http://sistemasddm.blogspot.com

prm = .Parameters.Add("@IDC", SqlDbType.VarChar, 5) prm.Value = Me.txtCodCli.Text prm = .Parameters.Add("@IDE", SqlDbType.VarChar, 5) prm.Value = Me.txtCodEmpl.Text prm = .Parameters.Add("@FEC", SqlDbType.DateTime) prm.Value = Me.txtFecha.Text prm = .Parameters.Add("@SUBTOT", SqlDbType.Real) prm.Value = Me.txtSubTotal.Text prm = .Parameters.Add("@IGV", SqlDbType.Real) prm.Value = Me.txtIgv.Text prm = .Parameters.Add("@TOT", SqlDbType.Real) prm.Value = Me.txtTotal.Text prm = .Parameters.Add("@EST", SqlDbType.VarChar, 20) prm.Value = Me.cbEstado.SelectedItem .ExecuteNonQuery() End With cn.Close() cmd.Dispose() 'DETALLE Dim I As Integer Dim prod, precio, cant, imp, sql2 As String For I = 0 To DatosGrid.Rows.Count - 1 prod = DatosGrid.Rows(I).Cells(0).Value precio = DatosGrid.Rows(I).Cells(1).Value cant = DatosGrid.Rows(I).Cells(2).Value imp = DatosGrid.Rows(I).Cells(3).Value sql2 = "INSERT INTO DETALLE_DOCUMENTO VALUES('" + Me.txtNum.Text + "','" + TIPO + "','" + (I + 1).ToString + "', '" + prod + "' , '" + precio + "' , '" + cant + "')" Dim cmd2 As New SqlCommand(sql2, cn) cn.Open() cmd2.ExecuteNonQuery() cn.Close() cmd2.Dispose() Next MsgBox("Documento Almacenado") 'ACTUALIZAR Dim cmd3 As New SqlCommand("UPDATE GENERADOR SET ULTIMO = ULTIMO + 1 WHERE PARAMETRO = 'DOCUMENTO'", cn) cn.Open() cmd3.ExecuteNonQuery() cn.Close() cmd3.Dispose() pre = 0 Catch ex As Exception MsgBox(ex.Message) cn.Close() End Try End Sub http://sistemasddm.blogspot.com

Private Sub btnBusCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBusCliente.Click Try Dim NR As Integer = -1 Dim Codigo As String = "" Codigo = InputBox("Ingrese Cliente:") Dim Da1 As New SqlDataAdapter("select * from CLIENTES where IdCLI = '" + Codigo + "'", cn) Da1.Fill(dsEntorno, "Busq1") NR = dsEntorno.Tables("Busq1").Rows.Count If NR > 0 Then Me.txtCodCli.Text = dsEntorno.Tables("Busq1").Rows(0)(0) Me.txtNomCli.Text = dsEntorno.Tables("Busq1").Rows(0)(1) Else MsgBox("Cliente no Existe") End If dsEntorno.Tables("Busq1").Rows.Clear() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub btnBusEmpleado_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBusEmpleado.Click Try Dim NR As Integer = -1 Dim Codigo As String = "" Codigo = InputBox("Ingrese Empleado:") Dim Da1 As New SqlDataAdapter("select * from EMPLEADOS where IDEMP = '" + Codigo + "'", cn) Da1.Fill(dsEntorno, "Busq2") NR = dsEntorno.Tables("Busq2").Rows.Count If NR > 0 Then Me.txtCodEmpl.Text = dsEntorno.Tables("Busq2").Rows(0)(0) Me.txtNomEmp.Text = dsEntorno.Tables("Busq2").Rows(0)(1) Else MsgBox("Empleado no Existe") End If dsEntorno.Tables("Busq2").Rows.Clear() Catch ex As Exception MsgBox(ex.Message) MsgBox(ex.ToString) End Try End Sub Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click Call limpiar() Me.txtNum.Text = Generadores("DOCUMENTO") End Sub Sub limpiar() Me.txtNum.Text = "" Me.txtCodCli.Text = "" Me.txtNomCli.Text = "" Me.txtCodEmpl.Text = "" Me.txtNomEmp.Text = "" http://sistemasddm.blogspot.com

Me.txtSubTotal.Text = "" Me.txtIgv.Text = "" Me.txtTotal.Text = "" Me.cbEstado.Text = "" Me.DatosGrid.Rows.Clear() Me.DatosGrid.DataSource = Nothing D=0 End Sub Private Sub DatosGrid_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DatosGrid.CellClick fila = e.RowIndex End Sub Private Sub DatosGrid_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DatosGrid.CellEnter Try If (DatosGrid.Rows(D).Cells(2).Value > 0) Then Me.DatosGrid.Rows(D).Cells(3).Value = Me.DatosGrid.Rows(D).Cells(2).Value * Me.DatosGrid.Rows(D).Cells(1).Value End If If (radBoleta.Checked = True) Then If (DatosGrid.Rows(D).Cells(3).Value > 0) Then pre = pre + DatosGrid.Rows(D).Cells(3).Value Me.txtSubTotal.Text = pre Me.txtIgv.Text = 0 Me.txtTotal.Text = Me.txtSubTotal.Text D=D+1 End If ElseIf (radFactura.Checked = True) Then If (DatosGrid.Rows(D).Cells(3).Value > 0) Then pre = pre + DatosGrid.Rows(D).Cells(3).Value Me.txtSubTotal.Text = pre Me.txtIgv.Text = (Val(Me.txtSubTotal.Text) * 0.19) Me.txtTotal.Text = (Val(Me.txtSubTotal.Text) + Val(Me.txtIgv.Text)) D=D+1 End If End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click tipoDocu = cbTipoDocu.SelectedItem codDocu = txtCodigoDocu.Text frmImprimir.Show() End Sub End Class

http://sistemasddm.blogspot.com

MODULO DE CONEXION Y AUTOGENERADOR DE CODIGO (Generar.vb) Imports System.Data.SqlClient Module Generar Public cn As New SqlConnection("server=localhost;database=SISTEMA; integrated security = true") Public dsEntorno As New DataSet Public tipoDocu As String Public codDocu As String Public Function Generadores(ByVal TABLA As String) As String Dim RESULT As String = "" Dim DR1 As SqlDataReader Dim ULT As Integer = 0 Dim CMD As New SqlCommand("SELECT ULTIMO FROM GENERADOR WHERE PARAMETRO = '" + TABLA + "'", cn) cn.Open() DR1 = CMD.ExecuteReader While DR1.Read ULT = Val(DR1("ULTIMO") + 1) End While cn.Close() Dim CEROS As Integer CEROS = 5 - Len(Str(ULT)) Select Case CEROS Case 3 : RESULT = Left(TABLA, 1) + "000" + Trim(Str(ULT)) Case 2 : RESULT = Left(TABLA, 1) + "00" + Trim(Str(ULT)) Case 1 : RESULT = Left(TABLA, 1) + "0" + Trim(Str(ULT)) Case 0 : RESULT = Left(TABLA, 1) + "" + Trim(Str(ULT)) End Select Generadores = RESULT End Function End Module

http://sistemasddm.blogspot.com

MODELO CRISTAL REPORT (Reporte.rpt)

http://sistemasddm.blogspot.com

FORMULARIO PARA VISUALIZAR EL REPORTE (FrmImprimir.vb)

CODIGO DE FrmImprimir Imports System.Data.SqlClient Public Class frmImprimir Dim Cn As New SqlConnection("Server=LocalHost;Uid=sa;Password=123;Database=SISTEMA") Dim INFORME1 As Reporte Dim MITABLA As CrystalDecisions.CrystalReports.Engine.Table Dim MILOGIN As CrystalDecisions.Shared.TableLogOnInfo Private Sub frmImprimir_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try INFORME1 = New Reporte For Each Me.MITABLA In INFORME1.Database.Tables MILOGIN = MITABLA.LogOnInfo MILOGIN.ConnectionInfo.Password = "123" MILOGIN.ConnectionInfo.UserID = "sa" MITABLA.ApplyLogOnInfo(MILOGIN) Next Me.CrystalReportViewer1.ReportSource = INFORME1 INFORME1.RecordSelectionFormula = "{CAB_DOCUMENTO.TIP_DOC}='" + tipoDocu + "' And {DETALLE_DOCUMENTO.NDOC}='" + codDocu + "'" Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub End Class

http://sistemasddm.blogspot.com