Point of Sale in Visual Basic (Part 3)

Previous |Next

Payment Window (frmPayment)
Design the payment form like the picture :

Add three properties:
    Protected _success As Boolean = False
    Public ReadOnly Property Success As Boolean
        Get
            Return _success
        End Get
    End Property
    Protected _idSale As Integer = 0
    Public ReadOnly Property IdSale As Integer
        Get
            Return _idSale
        End Get
    End Property
    Public Property TotalSale As Double
        Get
            Return varTotal
        End Get
        Set(ByVal value As Double)
            varTotal = value
        End Set
    End Property
    Dim varTotal As Double = 0

This is the function that saves the Sales:
    Protected Function DoSale() As Boolean
        Dim cnn As New OleDbConnection(CnnStr)
        Dim RowCount As Integer = 0
        Dim varFolioVenta As Integer = 0
        _idSale = 0
        Try
            cnn.Open()
            Dim tran As OleDbTransaction = cnn.BeginTransaction()
            Dim cmd As New OleDbCommand
            cmd.Connection = cnn
            cmd.Transaction = tran
            Try
                ''validate if exists product
                cmd.CommandText = "SELECT COUNT(*) " & _
                    "FROM SALE_DETAIL_TMP " & _
                    " WHERE USER_LOGIN=@USER_LOGIN"
                cmd.Parameters.Add("@USER_LOGIN", OleDbType.VarChar, _
                                   50).Value = frmLogin.UserLogin
                RowCount = CInt(cmd.ExecuteScalar)
                cmd.Parameters.Clear() ''Clerar parems
                If (RowCount = 0) Then
                    Throw (New Exception("No Data"))
                End If
                ''insert the sale
                cmd.CommandText = "INSERT INTO SALE" & _
                    "(USER_LOGIN,REG_DATE," & _
                    " SALE_DATE,TOTAL,CASH,CHANGE)" & _
                    " VALUES (@USER_LOGIN,NOW(),NOW()," & _
                    " @TOTAL,@CASH,@CHANGE)"
                cmd.Parameters.Add("@USER_LOGIN", _
                    OleDbType.VarChar, 50).Value = frmLogin.UserLogin
                cmd.Parameters.Add("@TOTAL", _
                    OleDbType.Double).Value = varTotal
                cmd.Parameters.Add("@CASH", _
                    OleDbType.Double).Value = txtCash.Text
                cmd.Parameters.Add("@CHANGE", _
                    OleDbType.Double).Value = _
                CDbl(txtCash.Text) - varTotal
                cmd.ExecuteNonQuery()
                cmd.Parameters.Clear() ''clear params
                ''get the Id
                cmd.CommandText = "SELECT @@IDENTITY"
                varFolioVenta = CInt(cmd.ExecuteScalar())
                ''insert sale detail
                cmd.CommandText = "INSERT INTO SALE_DETAIL" & _
                    " (ID_SALE,ID_PRODUCT,QUANTITY," & _
                    " SALE_PRICE,TAX,BUY_PRICE)" & _
                    " SELECT @ID_SALE, ID_PRODUCT,QUANTITY," & _
                    " SALE_PRICE,TAX," & _
                    " BUY_PRICE FROM SALE_DETAIL_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() ''Clear params
                ''Update the Stock
                cmd.CommandText = "UPDATE PRODUCTS " & _
                " INNER JOIN SALE_DETAIL_TMP " & _
                " ON PRODUCTS.ID_PRODUCT = " & _
                " SALE_DETAIL_TMP.ID_PRODUCT " & _
                " SET PRODUCTS.STOCK = " & _
                " PRODUCTS.STOCK-SALE_DETAIL_TMP.QUANTITY" +
                " WHERE ((SALE_DETAIL_TMP.USER_LOGIN= @USER_LOGIN));"
                cmd.Parameters.Add("@USER_LOGIN", _
                    OleDbType.VarChar, 50).Value = frmLogin.UserLogin
                cmd.ExecuteNonQuery()
                cmd.Parameters.Clear() ''Clear Params
                ''delete the temp table
                cmd.CommandText = "DELETE FROM SALE_DETAIL_TMP " & _
                    "WHERE USER_LOGIN=@USER_LOGIN"
                cmd.Parameters.Add("@USER_LOGIN", _
                    OleDbType.VarChar, 50).Value = frmLogin.UserLogin
                cmd.ExecuteNonQuery()
                _idSale = varFolioVenta ''set the ID
                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

The "Ok" button:
    Private Sub btnOk_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnOk.Click
        Try
            _success = DoSale()
            If (_success) Then
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Information",
               MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

The "Cancel" button:
    Private Sub btnCancel_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnCancel.Click
        _success = False
        Me.Close()
    End Sub

The "txtCash" TextBox, when a key is pressed:
    Private Sub txtCash_TextChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles txtCash.TextChanged
        Try
            Dim varCambio As Double = _
                Convert.ToDouble(txtCash.Text) - varTotal
            If (varCambio >= 0) Then
                txtChange.Text = String.Format("{0:C}",
                    (Convert.ToDouble(txtCash.Text) - varTotal))
                btnOk.Enabled = True
            Else
                btnOk.Enabled = False
            End If
        Catch ex As Exception
            txtCash.Text = ex.Message
            btnOk.Enabled = False
        End Try
    End Sub

The "Form_Load";
    Private Sub frmPayment_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            txtTotal.Text = String.Format("{0:C}", varTotal)
            txtTotal.Enabled = False
            txtChange.Enabled = False
            txtCash.Text = varTotal.ToString()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Information",
             MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

No hay comentarios:

Publicar un comentario