point of sale in visual basic
TRANSCRIPT
-
7/30/2019 Point of Sale in Visual Basic
1/12
Point of Sale in Visual Basic (Part 1)Next
Hello tyrodeveloper's, here is my first english publication, i hope that you like.
Requirements: Microsoft Visual Studio 2010
Microsoft Access 2007/2010
Data Base Design
We need to create a database like is showed in the picture:
Project creation
We create a project called PointOfSale:
Here is the project organization:
http://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-2.htmlhttp://4.bp.blogspot.com/-X6n8cp4BhA8/T1kmgsNCBaI/AAAAAAAABZY/pRr9MdxNLk4/s1600/PointOfSale02.jpghttp://4.bp.blogspot.com/-b-BAgsdL_jA/T1kmgZj7GJI/AAAAAAAABZM/EwdXLOVLFQQ/s1600/PointOfSale01.jpghttp://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-2.html -
7/30/2019 Point of Sale in Visual Basic
2/12
We have three folders. Add two forms to the Forms folder and a module in the Modules
folder like is showed in the picture.
Login Form (frmLogin)
Add this Imports directives:
Imports System.DataImports System.Data.OleDb
Add this Variables:
''Variables Dim Attempts AsInteger = 0 PublicShared UserLogin AsString
PublicShared FirstName AsString PublicShared LastName AsString PublicShared Sales AsBoolean PublicShared Reporting AsBoolean PublicShared Management AsBoolean PublicShared Catalog AsBoolean PublicShared Access AsBoolean
Open the modMain.vb module and add this code:
Module modMain Public CnnStr AsString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=C:\Users\ddi-gcastillo\" & _ "Documents\tyrodeveloper\PointOfSale\PointOfSale.accdb;" & _ "Persist Security Info=False;" Sub Main() Dim loginFrm AsNew frmLogin
loginFrm.StartPosition = FormStartPosition.CenterScreenApplication.Run(loginFrm)
If (frmLogin.Access = True) Then Dim mainFrm AsNew mdiMain
mainFrm.StartPosition = FormStartPosition.CenterScreenmainFrm.WindowState = FormWindowState.MaximizedApplication.Run(mainFrm)
ElseApplication.Exit()
EndIf EndSubEndModule
Design the login form like this:
http://2.bp.blogspot.com/-rU_qSrwA54o/T1kmg2gfpDI/AAAAAAAABZk/4HS5fkVaq98/s1600/PointOfSale03.jpg -
7/30/2019 Point of Sale in Visual Basic
3/12
On the frmLogin.vb form, add this function:
ProtectedFunction Login() AsBoolean Dim cnn AsNew OleDbConnection(CnnStr) Try
cnn.Open() Dim cmd AsNew OleDbCommand
cmd.Connection = cnncmd.CommandText = "SELECT * FROM USERS " & _
" WHERE USER_LOGIN=@user_login " & _ " AND USER_PASSWORD=@user_password"
cmd.Parameters.Add("@user_login",OleDbType.VarChar, 50).Value = txtUserLogin.Text
cmd.Parameters.Add("@user_password",OleDbType.VarChar, 255).Value = txtUserPassword.Text
Dim dr As OleDbDataReader = cmd.ExecuteReader If dr.Read Then
UserLogin = dr("USER_LOGIN").ToString()FirstName = dr("FIRST_NAME").ToString()LastName = dr("LAST_NAME").ToString()Sales = CBool(dr("SALES"))Reporting = CBool(dr("REPORTING"))Management = CBool(dr("MANAGEMENT"))Catalog = CBool(dr("CATALOG"))
Else
Attempts += 1 Throw (New Exception( _ "Username or password incorrect")) EndIf
dr.Close() ReturnTrue Catch ex As Exception Throw (ex) Finally
cnn.Close() EndTry EndFunction
And here is the code for the Ok button:
PrivateSub btnOk_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _ Handles btnOk.Click Try If (Attempts >= 3) Then
Access = FalseMessageBox.Show("Attempts exceeded", _
"System Information", _MessageBoxButtons.OK, _MessageBoxIcon.Error)
Me.Close() Else
Access = Login() If (Access) Then Me.Close() EndIf EndIf
Catch ex As ExceptionMessageBox.Show(ex.Message,
"System Information", _
http://2.bp.blogspot.com/-hHrXZ3AaJlk/T1kmgx1Du2I/AAAAAAAABZs/TydIM4ydVl0/s1600/PointOfSale04.jpg -
7/30/2019 Point of Sale in Visual Basic
4/12
MessageBoxButtons.OK,MessageBoxIcon.Error)
EndTry EndSub
Go to the project configuration and set:
Point of Sale in Visual Basic (Part 2)Previous|Next
Add two new forms (frmSale, frmPayment) like:
Design mdiMain like the picture:
http://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-1.htmlhttp://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-3.htmlhttp://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-3.htmlhttp://4.bp.blogspot.com/-hkJim8KsLhg/T1kvAGx0IUI/AAAAAAAABaI/t0meoHhA0C8/s1600/PointOfSale06.jpghttp://1.bp.blogspot.com/-8okttBHyT9g/T1kmhMPpsgI/AAAAAAAABZ8/DyLweSR-VaM/s1600/PointOfSale05.jpghttp://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-1.htmlhttp://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-3.html -
7/30/2019 Point of Sale in Visual Basic
5/12
Add this code on the "Form_Load":
''AllowsmnuSales.Visible = frmLogin.SalesbtnSales.Visible = frmLogin.SalesmnuReporting.Visible = frmLogin.ReportingmnuManagement.Visible = frmLogin.ManagementmnuCatalog.Visible = frmLogin.Catalog
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 AsBoolean = False PublicReadOnlyProperty Success AsBoolean Get
Return _success EndGet EndProperty
http://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-2.htmlhttp://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-4.htmlhttp://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-4.htmlhttp://4.bp.blogspot.com/-rfG72R84Ap0/T1k7bEzYRsI/AAAAAAAABag/Bs4skG_Irvc/s1600/PointOfSale08.jpghttp://4.bp.blogspot.com/-PD3J9E8qCt8/T1kvATCLPXI/AAAAAAAABaU/Lkri4srs5Yk/s1600/PointOfSale07.jpghttp://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-2.htmlhttp://www.tyrodeveloper.com/2012/03/point-of-sale-in-visual-basic-part-4.html -
7/30/2019 Point of Sale in Visual Basic
6/12
Protected _idSale AsInteger = 0 PublicReadOnlyProperty IdSale AsInteger Get Return _idSale EndGet EndProperty PublicProperty TotalSale AsDouble Get Return varTotal EndGet Set(ByVal value AsDouble)
varTotal = value EndSet EndProperty Dim varTotal AsDouble = 0
This is the function that saves the Sales:
ProtectedFunction DoSale() AsBoolean Dim cnn AsNew OleDbConnection(CnnStr) Dim RowCount AsInteger = 0 Dim varFolioVenta AsInteger = 0
_idSale = 0
Trycnn.Open()
Dim tran As OleDbTransaction = cnn.BeginTransaction() Dim cmd AsNew OleDbCommand
cmd.Connection = cnncmd.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")) EndIf ''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 Idcmd.CommandText = "SELECT @@IDENTITY"varFolioVenta = CInt(cmd.ExecuteScalar())
''insert sale detailcmd.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.UserLogincmd.ExecuteNonQuery()cmd.Parameters.Clear() ''Clear params
-
7/30/2019 Point of Sale in Visual Basic
7/12
''Update the Stockcmd.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 tablecmd.CommandText = "DELETE FROM SALE_DETAIL_TMP " & _
"WHERE USER_LOGIN=@USER_LOGIN"cmd.Parameters.Add("@USER_LOGIN", _
OleDbType.VarChar, 50).Value = frmLogin.UserLogincmd.ExecuteNonQuery()_idSale = varFolioVenta ''set the IDtran.Commit()
ReturnTrue Catch ex1 As OleDb.OleDbException
tran.Rollback() Throw (ex1)
EndTry Catch ex As Exception Throw (ex) Finally
cnn.Close() EndTry EndFunction
The "Ok" button:
PrivateSub btnOk_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnOk.Click Try
_success = DoSale() If (_success) Then
Me.Close() EndIf Catch ex As Exception
MessageBox.Show(ex.Message, "System Information",MessageBoxButtons.OK, MessageBoxIcon.Error)
EndTry EndSub
The "Cancel" button:
PrivateSub btnCancel_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCancel.Click
_success = False Me.Close() EndSub
The "txtCash" TextBox, when a key is pressed:
PrivateSub txtCash_TextChanged(ByVal sender AsObject, _ ByVal e As System.EventArgs) Handles txtCash.TextChanged Try Dim varCambio AsDouble = _
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 EndIf Catch ex As Exception
txtCash.Text = ex.MessagebtnOk.Enabled = False
EndTry
-
7/30/2019 Point of Sale in Visual Basic
8/12
EndSub
The "Form_Load";
PrivateSub frmPayment_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) HandlesMyBase.Load Try
txtTotal.Text = String.Format("{0:C}", varTotal)txtTotal.Enabled = FalsetxtChange.Enabled = FalsetxtCash.Text = varTotal.ToString()
Catch ex As ExceptionMessageBox.Show(ex.Message, "System Information",MessageBoxButtons.OK, MessageBoxIcon.Error)
EndTry EndSub
Point of Sale in Visual Basic (Part 4)Previous|Next
Sales window (frmSale)
Design the form like the picture:
Declare a variable to store the total:
Dim varTotal AsDouble = 0
The Sales logic process:
1.Type the Product ID and the Quantity
2.Validate if the Product exists on the Products Catalog.
3.
4.
If the Product does not exists generate an exception (Throw)
5.6.
7.Validate if the product exists in the temporal sales list.
8.
9.
http://www.blogger.com/point-of-sale-in-visual-basic-part-3.htmlhttp://1.bp.blogspot.com/-vnrxgxTSQ9M/T1k-XHn9dEI/AAAAAAAABas/8XjtIJImEkY/s1600/PointOfSale09.jpghttp://www.blogger.com/point-of-sale-in-visual-basic-part-3.html -
7/30/2019 Point of Sale in Visual Basic
9/12
If not exists, add new.
10.
If exists, update the Quantity
11.12.
The function to add items to the temporal sales list:
ProtectedFunction AddItem() AsBoolean Dim cnn AsNew OleDbConnection(CnnStr) Dim RowCount AsInteger = 0 Try
cnn.Open() Dim cmd AsNew OleDbCommand
cmd.Connection = cnn ''Validate if the product exists
cmd.CommandText = "SELECT COUNT(*) FROM PRODUCTS " & _
"WHERE ID_PRODUCT=@ID_PRODUCT"cmd.Parameters.Add("@ID_PRODUCT", _OleDbType.VarChar, 50).Value = txtIdProduct.Text
RowCount = CInt(cmd.ExecuteScalar)cmd.Parameters.Clear() ''Clear params
If (RowCount = 0) Then Throw (New Exception("The product does not exists")) EndIf ''Validate if the product is in the sales list
cmd.CommandText = "SELECT COUNT(*) " & _ " FROM SALE_DETAIL_TMP " & _ " WHERE ID_PRODUCT=@ID_PRODUCT" & _ " AND USER_LOGIN=@USER_LOGIN"
cmd.Parameters.Add("@ID_PRODUCT", _OleDbType.VarChar, 50).Value = txtIdProduct.Text
cmd.Parameters.Add("@USER_LOGIN", _OleDbType.VarChar, 50).Value = frmLogin.UserLogin
RowCount = CInt(cmd.ExecuteScalar)cmd.Parameters.Clear() ''Clear params
If (RowCount = 0) Then ''Insert New
cmd.CommandText = "INSERT INTO SALE_DETAIL_TMP " & _ " (USER_LOGIN, ID_PRODUCT,QUANTITY," & _ "SALE_PRICE,BUY_PRICE,TAX) " & _ " SELECT @USER_LOGIN, ID_PRODUCT, @QTY, " & _ " SALE_PRICE, BUY_PRICE, TAX " & _ " FROM PRODUCTS WHERE ID_PRODUCT=@ID_PRODUCT"
cmd.Parameters.Add("@USER_LOGIN", _OleDbType.VarChar, 50).Value = frmLogin.UserLogin
cmd.Parameters.Add("@QTY", _OleDbType.Double).Value = txtQuantity.Text
cmd.Parameters.Add("@ID_PRODUCT", _OleDbType.VarChar, 50).Value = txtIdProduct.Text
Else ''Edit Quantity
cmd.CommandText = "UPDATE SALE_DETAIL_TMP " & _ " SET QUANTITY = QUANTITY + @QTY " & _ " WHERE ID_PRODUCT=@ID_PRODUCTO " & _ " AND USER_LOGIN=@USER_LOGIN "
cmd.Parameters.Add("@QTY",OleDbType.Double).Value = txtQuantity.Text
cmd.Parameters.Add("@ID_PRODUCT",OleDbType.VarChar, 50).Value = txtIdProduct.Text
cmd.Parameters.Add("@USER_LOGIN",OleDbType.VarChar, 50).Value = frmLogin.UserLogin
EndIfcmd.ExecuteNonQuery()
ReturnTrue Catch ex As Exception Throw (ex) Finally
cnn.Close()
-
7/30/2019 Point of Sale in Visual Basic
10/12
EndTry EndFunction
The code to cancel the sales is:
ProtectedFunction CancelSale() AsBoolean Dim cnn AsNew OleDbConnection(CnnStr) Try
cnn.Open() Dim cmd AsNew OleDbCommand
cmd.Connection = cnncmd.CommandText = "DELETE FROM SALE_DETAIL_TMP " & _
"WHERE USER_LOGIN=@USER_LOGIN"cmd.Parameters.Add("@USER_LOGIN",
OleDbType.VarChar, 50).Value = frmLogin.UserLogincmd.ExecuteNonQuery()
ReturnTrue Catch ex As Exception Throw (ex) Finally
cnn.Close() EndTry EndFunction
To generate the ListView headers:
ProtectedSub Headers() With lvSale
.View = View.Details
.FullRowSelect = True
.GridLines = True
.HideSelection = False
.Columns.Add("Id", 50)
.Columns.Add("Product", 250)
.Columns.Add("Qty", 50, HorizontalAlignment.Right)
.Columns.Add("Price", 50, HorizontalAlignment.Right)
.Columns.Add("Total", 80, HorizontalAlignment.Right) EndWith
EndSub
To display the temporal sale items:
ProtectedSub DisplaySale() Dim cnn AsNew OleDbConnection(CnnStr)
varTotal = 0 Try
cnn.Open() Dim cmd AsNew OleDbCommand
cmd.Connection = cnncmd.CommandText = "SELECT P.ID_PRODUCT, " & _
"P.PRODUCT, T.QUANTITY, T.SALE_PRICE," & _ "(T.QUANTITY * T.SALE_PRICE) AS TOTAL " & _ " FROM SALE_DETAIL_TMP T, PRODUCTS P " & _ " WHERE T.ID_PRODUCT=P.ID_PRODUCT " & _
" AND T.USER_LOGIN=@USER_LOGIN"cmd.Parameters.Add("@USER_LOGIN",
OleDbType.VarChar, 50).Value = frmLogin.UserLogin Dim dr As OleDbDataReader = cmd.ExecuteReader Dim i AsInteger = 0
lvSale.Items.Clear() While (dr.Read()) With lvSale
.Items.Add(dr("ID_PRODUCT").ToString())
.Items(i).SubItems.Add(dr("PRODUCT").ToString())
.Items(i).SubItems.Add(String.Format("{0:N}",dr("QUANTITY")))
.Items(i).SubItems.Add(String.Format("{0:C}",dr("SALE_PRICE")))
.Items(i).SubItems.Add(String.Format("{0:C}",dr("TOTAL")))
EndWithvarTotal += CDbl(dr("TOTAL"))i += 1
-
7/30/2019 Point of Sale in Visual Basic
11/12
EndWhiledr.Close()lblTotal.Text = String.Format("{0:C}", varTotal)
Catch ex As Exception Throw (ex) Finally
cnn.Close() EndTry EndSub
The "Add" button:
PrivateSub btnAdd_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnAdd.Click Try If (AddItem()) Then
DisplaySale()txtQuantity.Text = "1"txtIdProduct.Text = ""txtIdProduct.Focus()
EndIf Catch ex As Exception
MessageBox.Show(ex.Message, "System Information",
MessageBoxButtons.OK, MessageBoxIcon.Error) EndTry EndSub
The "Cancel" button:
PrivateSub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click Try If (CancelSale()) Then
lvSale.Items.Clear() EndIf Catch ex As Exception
MessageBox.Show(ex.Message, "System Information",MessageBoxButtons.OK, MessageBoxIcon.Error)
EndTry EndSub
The "Payment" button:
PrivateSub btnPayment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPayment.Click If (lvSale.Items.Count > 0) Then Dim frm AsNew frmPayment
frm.TotalSale = varTotalfrm.StartPosition = FormStartPosition.CenterScreenfrm.ShowInTaskbar = Falsefrm.ShowDialog()
If (frm.Success) ThenlvSale.Items.Clear()
EndIf
ElseMessageBox.Show("No items", "System Information",
MessageBoxButtons.OK, MessageBoxIcon.Error) EndIf EndSub
The "Form_Load" method:
PrivateSub frmSale_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load Try
Headers()DisplaySale()
Catch ex As ExceptionMessageBox.Show(ex.Message, "System Information", _
MessageBoxButtons.OK, MessageBoxIcon.Error) EndTry EndSub
-
7/30/2019 Point of Sale in Visual Basic
12/12
It is the end of the Sales process. After we do the reports using the Visual Studio Reporting
options (rdlc reports).
Thanks...