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
CARGAR EN FORMULARIO
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
'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
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
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
BOTÓN IMPRIMIR:
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