Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Para pasar los valores principalmente cdigos crearemos una clase Global
Public Class Globales
Private Shared m_CodigoClienteSeleccionado As String
Private Shared m_CodigoProductoSeleccionado As String
Public Shared Property CodigoClienteSeleccionado() As String
Get
Return m_CodigoClienteSeleccionado
End Get
Set(ByVal value As String)
m_CodigoClienteSeleccionado = value
End Set
End Property
Public Shared Property CodigoProductoSeleccionado() As String
Get
Return m_CodigoProductoSeleccionado
End Get
Set(ByVal value As String)
m_CodigoProductoSeleccionado = value
End Set
End Property
End Class
En el botn aceptar del formulario bsqueda de productos codificamos:
Private Sub btnaceptar_Click(sender As Object, e As EventArgs) Handles btnaceptar.Click
Globales.CodigoProductoSeleccionado = lvwbuscar.SelectedItems(0).Text
Close()
End Sub
Para verificar la cantidad de stock que existe para poder vender productos
Debemos codificar en la CAPA DE DATOS
Public Function VerificarStock(ByVal codigoproducto As String) As DataSet
Dim strSQL As String
Dim dt As New DataTable
strSQL = "select stock from productos where codigoproducto= '" & codigoproducto & "'"
Dim da As New SqlDataAdapter(strSQL, My.Settings.Conexion)
Dim ds As New DataSet
da.Fill(ds, 0)
da.Dispose()
Return ds
End Function
Y en la CAPA LGICA DE NEGOCIO
Public Function VerificarStockProducto(ByVal codigoproducto As String) As DataSet
Try
Dim obj As New ProductosDA
Return obj.VerificarStock(codigoproducto)
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Function
Ahora codificamos en el botn agregar del formulario documento de venta
'Verifica si existe en stock
Dim objprod As New LogicaNegocio.ProductosCN
Dim ds As New DataSet
ds = objprod.VerificarStockProducto(Txtcodproducto.Text)
Dim stockactual As Integer
stockactual = ds.Tables(0).Rows(0).Item("Stock")
If Integer.Parse(Txtcantidad.Text) > stockactual Then 'verifica el stock
MessageBox.Show("No puede ingresar una cantidad mayor al stock", "ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
Else
'Agregar al listview pero verifica que el producto no se halla vendido
Dim item1 As ListViewItem = LvwDetalle.FindItemWithText(Txtcodproducto.Text)
If (item1 IsNot Nothing) Then
MessageBox.Show("Debe seleccionar otro Producto no puede volver a vender ese producto", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
LvwDetalle.Items.Add(Txtcodproducto.Text)
LvwDetalle.Items(fila).SubItems.Add(Txtdescripcion.Text)
LvwDetalle.Items(fila).SubItems.Add(Txtpventa.Text)
LvwDetalle.Items(fila).SubItems.Add(Txtcantidad.Text)
LvwDetalle.Items(fila).SubItems.Add(Txtcantidad.Text * Txtpventa.Text)
fila = fila + 1
Call calculatotaldetc()
End If
End If
Debemos indicar las instrucciones del procedimiento calculatotaldetc
Sub calculatotaldetc()
Dim total, tot As Decimal
Dim r As Integer
For r = 0 To LvwDetalle.Items.Count - 1
tot = Decimal.Parse(LvwDetalle.Items(r).SubItems(4).Text)
total = total + tot
Next
Txtsubtotal.Text = total.ToString("0.00")
If Mid(CboTipo.Text, 1, 1) = "F" Then
Txtigv.Text = 0.18 * total
Txttotal.Text = Txtigv.Text + total
ElseIf Mid(CboTipo.Text, 1, 1) = "B" Then
Txtigv.Text = 0
Txttotal.Text = Txtigv.Text + total
End If
End Sub
Tambin es necesario codificar el botn quitar
Dim renglon As Integer
If LvwDetalle.Items.Count > 0 Then
renglon = Integer.Parse(LvwDetalle.SelectedItems(0).Index)
If renglon >= 0 Then
LvwDetalle.Items.RemoveAt(renglon)
End If
fila = fila - 1
Call calculatotaldetc()
End If
En el evento actvate del formulario :
Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
TxtCodcliente.Text = Globales.CodigoClienteSeleccionado
Txtcodproducto.Text = Globales.CodigoProductoSeleccionado
End Sub
En el formulario en el evento LOAD este cdigo permitir crear un numero de documento siguiente :
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim objdoc As New DocumentoCN
Txtnrodocumento.Text = objdoc.Obtener_UltimoNumero()
End Sub
Debemos crear en la CAPA DE DATOS
Public Function Obtener_UltimoNumero() As String
Dim numero_recibo As String
Try
Dim cnn As New SqlConnection(My.Settings.Conexion)
cnn.Open()
Dim Sqlcmd As New SqlCommand("pa_Documento_ObtenerUltimo", cnn)
Sqlcmd.CommandType = CommandType.StoredProcedure
Sqlcmd.Parameters.Add("@nrodocumento", SqlDbType.VarChar, 11)
Sqlcmd.Parameters("@nrodocumento").Direction = ParameterDirection.Output
Sqlcmd.ExecuteNonQuery()
cnn.Close()
cnn.Dispose()
numero_recibo = Sqlcmd.Parameters("@nrodocumento").Value
Return numero_recibo
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Function
3. CAPA
ACCESO A
DATOS
Podemos ver que se est utilizando un procedimiento almacenado el cual debemos crearlo
CREATE PROCEDURE [dbo].[pa_Documento_ObtenerUltimo]
@nrodocumento varchar(10) out
AS
BEGIN
if not exists (select nrodocumento from Documento)
begin
set @nrodocumento='0000000001'
end
else
begin
set @nrodocumento=(select top 1 nrodocumento from Documento order by nrodocumento desc )
set @nrodocumento=RIGHT('0000000000'+LTRIM((convert(varchar(10),@nrodocumento+1))) ,10)
end
END
Tambin debemos codificar en la LOGICA DE NEGOCIO
Public Function Obtener_UltimoNumero() As String
Dim numero As String
Try
Dim obj As New DocumentoDA
numero = obj.Obtener_UltimoNumero()
4. CAPA LOGICA
DE NEGOCIO
Return numero
Catch ex As Exception
Throw New Exception(ex.Message)
Return ""
End Try
End Function
Para grabar el documento de venta debemos crear dos procedimientos almacenados uno para la tabla documento
create PROCEDURE [dbo].[pa_Documento_Insertar]
@nrodocumento varchar(10),
@tipodocumento char(1),
@fechadoc datetime,
@codigocliente varchar(5),
@subtotal decimal(18,2),
@igv decimal(18,2),
@total decimal(18,2)
AS
insert into Documento
(
NroDocumento,
TipoDocumento,
FechaDoc,
Codigocliente,
subtotal,
igv,
total
)
values
(
@nrodocumento,
@tipodocumento,
@fechadoc,
@codigocliente,
@subtotal,
@igv,
@total
)
Y otro procedimiento para detalledocumentos
CREATE PROCEDURE [dbo].[pa_DetalleDocumentos_Insertar]
@nrodocumento varchar(10),
@tipodocumento char(1),
@codigoproducto varchar(5),
@precioventa decimal(18,2),
@cantidad decimal(18,2)
AS
insert into DetalleDocumentos
(
NroDocumento,
TipoDocumento,
Codigoproducto,
Precioventa,
Cantidad
)
values
(
@nrodocumento,
@tipodocumento,
@codigoproducto,
@precioventa,
@cantidad
)
End Property
End Class
Public Class DetalleDocumento
Private _nrodocumento As String
Private _tipodocumento As String
Private _codigoproducto As String
Private _precioventa As Double
Private _cantidad As Double
Public Sub New(ByVal cnrodocumento As String, ByVal ctipodocumento As String, _
ByVal ccodigoproducto As String, ByVal cprecioventa As Double, _
ByVal ccantidad As Double)
_nrodocumento = cnrodocumento
_tipodocumento = ctipodocumento
_codigoproducto = ccodigoproducto
_precioventa = cprecioventa
_cantidad = ccantidad
End Sub
Public Property nrodocumento As String
Get
Return _nrodocumento
End Get
Set(ByVal value As String)
_nrodocumento = value
End Set
End Property
Public Property tipodocumento As String
Get
Return _tipodocumento
End Get
Set(ByVal value As String)
_tipodocumento = value
End Set
2. capa entidad
End Property
Public Property codigoproducto As String
Get
Return _codigoproducto
End Get
Set(ByVal value As String)
_codigoproducto = value
End Set
End Property
Public Property precioventa As Double
Get
Return _precioventa
End Get
Set(ByVal value As Double)
_precioventa = value
End Set
End Property
Public Property cantidad As Double
Get
Return _cantidad
End Get
Set(ByVal value As Double)
_cantidad = value
End Set
End Property
End Class
En la capa de datos crear la clase DocumentoDA y agregar el siguiente mtodo :
Public Function Insertar(ByVal Documento As Documento) As Boolean
Dim Exito As Boolean
Try
Dim cnn As New SqlConnection(My.Settings.Conexion)
cnn.Open()
Dim Sqlcmd As New SqlCommand("pa_Documento_insertar", cnn)
Sqlcmd.CommandType = CommandType.StoredProcedure
Sqlcmd.Parameters.Add("@nrodocumento", SqlDbType.VarChar, 10).Value = Documento.nrodocumento
Sqlcmd.Parameters.Add("@tipodocumento", SqlDbType.Char, 1).Value = Documento.tipodocumento
Sqlcmd.Parameters.Add("@fechadoc", SqlDbType.DateTime).Value = Documento.fechadocumento
Sqlcmd.Parameters.Add("@codigocliente", SqlDbType.VarChar, 10).Value = Documento.codigocliente
Sqlcmd.Parameters.Add("@subtotal", SqlDbType.Decimal).Value = Documento.subtotal
Sqlcmd.Parameters.Add("@igv", SqlDbType.Decimal).Value = Documento.igv