Punto de Venta Visual Basic (Parte 3)

Ejercicio Anterior|Ejercicio Siguiente
Pantalla de Cobranza (frmCobrar)
Diseñamos la ventana de cobranza (frmCobrar) de la siguiente manera:
Agregamos tres propiedades:
    Protected _success As Boolean = False
    Public ReadOnly Property Success As Boolean
        Get
            Return _success
        End Get
    End Property
    Protected _folioVenta As Integer = 0
    Public ReadOnly Property FolioVenta As Integer
        Get
            Return _folioVenta
        End Get
    End Property
    Public Property TotalVenta As Double
        Get
            Return varTotal
        End Get
        Set(ByVal value As Double)
            varTotal = value
        End Set
    End Property
    Dim varTotal As Double = 0

Esta es la función que guarda las ventas:
    Protected Function RealizarVenta() As Boolean
        Dim cnn As New OleDbConnection(CnnStr)
        Dim RowCount As Integer = 0
        Dim varFolioVenta As Integer = 0
        _folioVenta = 0
        Try
            cnn.Open()
            Dim tran As OleDbTransaction = cnn.BeginTransaction()
            Dim cmd As New OleDbCommand
            cmd.Connection = cnn
            cmd.Transaction = tran
            Try
                ''validamos si existe el producto
                cmd.CommandText = "SELECT COUNT(*) " & _
                    "FROM VENTA_DETALLE_TMP " & _
                    " WHERE USER_LOGIN=@USER_LOGIN"
                cmd.Parameters.Add("@USER_LOGIN", OleDbType.VarChar, _
                                   50).Value = frmLogin.UserLogin
                RowCount = CInt(cmd.ExecuteScalar)
                cmd.Parameters.Clear() ''Limpiamos parámetros
                If (RowCount = 0) Then
                    Throw (New Exception("No hay registros"))
                End If
                ''insertamos la venta
                cmd.CommandText = "INSERT INTO VENTA" & _
                    "(USER_LOGIN,FECHA_REGISTRO," & _
                    " FECHA_VENTA,TOTAL,EFECTIVO,CAMBIO)" & _
                    " VALUES (@USER_LOGIN,NOW(),NOW()," & _
                    " @TOTAL,@EFECTIVO,@CAMBIO)"
                cmd.Parameters.Add("@USER_LOGIN", _
                    OleDbType.VarChar, 50).Value = frmLogin.UserLogin
                cmd.Parameters.Add("@TOTAL", _
                    OleDbType.Double).Value = varTotal
                cmd.Parameters.Add("@EFECTIVO", _
                    OleDbType.Double).Value = txtEfectivo.Text
                cmd.Parameters.Add("@CAMBIO", _
                    OleDbType.Double).Value = _
                CDbl(txtEfectivo.Text) - varTotal
                cmd.ExecuteNonQuery()
                cmd.Parameters.Clear() ''Limpiamos parametros
                ''obtenemos el folio
                cmd.CommandText = "SELECT @@IDENTITY"
                varFolioVenta = CInt(cmd.ExecuteScalar())
                ''insertamos el detalle de venta
                cmd.CommandText = "INSERT INTO VENTA_DETALLE" & _
                    "(ID_VENTA,ID_PRODUCTO,CANTIDAD," & _
                    "PRECIO_VENTA,IMPUESTO,PRECIO_COMPRA)" & _
                    "SELECT @ID_VENTA, ID_PRODUCTO,CANTIDAD," & _
                    "PRECIO_VENTA,IMPUESTO," & _
                    "PRECIO_COMPRA FROM VENTA_DETALLE_TMP " & _
                    " WHERE USER_LOGIN=@USER_LOGIN"
                cmd.Parameters.Add("@ID_VENTA", _
                    OleDbType.Integer).Value = varFolioVenta
                cmd.Parameters.Add("@USER_LOGIN", _
                    OleDbType.VarChar, 50).Value = frmLogin.UserLogin
                cmd.ExecuteNonQuery()
                cmd.Parameters.Clear() ''Limpiamos parametros
                ''Actualizamos existencias
                cmd.CommandText = "UPDATE PRODUCTOS " & _
                " INNER JOIN VENTA_DETALLE_TMP " & _
                " ON PRODUCTOS.ID_PRODUCTO = " & _
                " VENTA_DETALLE_TMP.ID_PRODUCTO " & _
                " SET PRODUCTOS.STOCK = " & _
                " PRODUCTOS.STOCK-VENTA_DETALLE_TMP.CANTIDAD" +
                " WHERE ((VENTA_DETALLE_TMP.USER_LOGIN= @USER_LOGIN));"
                cmd.Parameters.Add("@USER_LOGIN", _
                    OleDbType.VarChar, 50).Value = frmLogin.UserLogin
                cmd.ExecuteNonQuery()
                cmd.Parameters.Clear() ''Limpiamos parametros
                ''borramos la tabla temporal
                cmd.CommandText = "DELETE FROM VENTA_DETALLE_TMP " & _
                    "WHERE USER_LOGIN=@USER_LOGIN"
                cmd.Parameters.Add("@USER_LOGIN", _
                    OleDbType.VarChar, 50).Value = frmLogin.UserLogin
                cmd.ExecuteNonQuery()
                _folioVenta = varFolioVenta ''Asignamos el folio
                tran.Commit()
                Return True
            Catch ex1 As OleDb.OleDbException
                tran.Rollback()
                Throw (ex1)
            End Try
        Catch ex As Exception
            Throw (ex)
        Finally
            cnn.Close()
        End Try
    End Function

Botón "Aceptar":
Private Sub btnOk_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnOk.Click
        Try
            _success = RealizarVenta()
            If (_success) Then
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Información del Sistema",
               MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
End Sub

Botón "Cancelar":
    Private Sub btnCancel_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

txtEfectivo (Cuando se presiona una tecla se actualizará txtCambio):
    Private Sub txtEfectivo_TextChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles txtEfectivo.TextChanged
        Try
            Dim varCambio As Double = _
                Convert.ToDouble(txtEfectivo.Text) - varTotal
            If (varCambio >= 0) Then
                txtCambio.Text = String.Format("{0:C}",
                    (Convert.ToDouble(txtEfectivo.Text) - varTotal))
                btnOk.Enabled = True
            Else
                btnOk.Enabled = False
            End If
        Catch ex As Exception
            txtCambio.Text = ex.Message
            btnOk.Enabled = False
        End Try
    End Sub
Form_Load:
    Private Sub frmCobrar_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            txtTotalPagar.Text = String.Format("{0:C}", varTotal)
            txtTotalPagar.Enabled = False
            txtCambio.Enabled = False
            txtEfectivo.Text = varTotal.ToString()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Información del Sistema",
             MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

No hay comentarios:

Publicar un comentario en la entrada