001.
Option
Explicit
On
002.
Option
Strict
On
003.
Imports
System.Data
004.
Imports
System.Data.OleDb
005.
Public
Class
frmOrder
006.
Dim
Conn
As
New
OleDbConnection
007.
Dim
da
As
OleDbDataAdapter
008.
Dim
ds
As
New
DataSet
009.
Dim
strConn
As
String
010.
Dim
lsFindCustomer
As
Boolean
=
False
011.
Dim
lsFindFood
As
Boolean
=
False
012.
Private
Sub
order1_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
013.
strConn =
"Provider = Microsoft.Jet.OLEDB.4.0;Data Source=C:\ร้านอาหาร11.mdb"
014.
015.
lsvProductList.Columns.Add(
"รหัสสินค้า"
, 60, HorizontalAlignment.Left)
016.
lsvProductList.Columns.Add(
"รหัสประเภท"
, 60, HorizontalAlignment.Left)
017.
lsvProductList.Columns.Add(
"ชื่อสินค้า"
, 150, HorizontalAlignment.Left)
018.
lsvProductList.Columns.Add(
"ราคาขาย"
, 65, HorizontalAlignment.Right)
019.
lsvProductList.Columns.Add(
"จำนวน"
, 50, HorizontalAlignment.Right)
020.
lsvProductList.Columns.Add(
"รวมเป็นเงิน"
, 70, HorizontalAlignment.Right)
021.
022.
lsvProductList.View = View.Details
023.
lsvProductList.GridLines =
True
024.
lsvProductList.FullRowSelect =
True
025.
026.
With
Conn
027.
If
.State = ConnectionState.Open
Then
.Close()
028.
.ConnectionString = strConn
029.
.Open()
030.
End
With
031.
ClearFoodData()
032.
lblNet.Text =
"0"
033.
End
Sub
034.
035.
Private
Sub
ClearFoodData()
036.
txtFoodID.Text =
""
037.
lblType.Text =
""
038.
lblFoodName.Text =
""
039.
lblSalePrice.Text =
""
040.
txtAmount.Text =
""
041.
lblTotal.Text =
""
042.
End
Sub
043.
044.
Private
Sub
txtFoodID_TextChanged(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
txtFoodID.TextChanged
045.
Dim
sqlfood
As
String
046.
If
txtFoodID.Text =
""
Then
Exit
Sub
047.
sqlfood =
"select * from food where food_id='"
& txtFoodID.Text &
"'"
048.
If
lsFindFood =
True
Then
049.
ds.Tables(
"food"
).Clear()
050.
End
If
051.
da =
New
OleDbDataAdapter(sqlfood, Conn)
052.
da.Fill(ds,
"food"
)
053.
If
ds.Tables(
"food"
).Rows.Count <> 0
Then
054.
lsFindFood =
True
055.
056.
txtFoodID.Text =
CStr
(ds.Tables(
"food"
).Rows(0).Item(
"food_id"
))
057.
lblType.Text =
CStr
(ds.Tables(
"food"
).Rows(0).Item(
"type_id"
))
058.
lblFoodName.Text =
CStr
(ds.Tables(
"food"
).Rows(0).Item(
"food_name"
))
059.
lblSalePrice.Text =
CStr
(ds.Tables(
"food"
).Rows(0).Item(
"food_price"
))
060.
061.
CalculateTotal()
062.
063.
txtAmount.Focus()
064.
Else
065.
lsFindFood =
False
066.
MessageBox.Show(
"รหัสสินค้าที่คุณป้อนไม่ถูกต้อง"
,
"ผลการตรวจสอบ"
, MessageBoxButtons.OK, MessageBoxIcon.Information)
067.
ClearFoodData()
068.
txtFoodID.Focus()
069.
End
If
070.
End
Sub
071.
Private
Sub
CalculateTotal()
072.
Dim
Total
As
String
073.
Total =
CStr
(
CDbl
(lblSalePrice.Text) *
CDbl
(txtAmount.Text))
074.
lblTotal.Text =
CStr
((Total))
075.
End
Sub
076.
077.
Private
Sub
cmdAdd_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
cmdAdd.Click
078.
If
(txtFoodID.Text =
""
)
Or
(lblFoodName.Text =
""
)
Then
079.
txtFoodID.Focus()
080.
Exit
Sub
081.
End
If
082.
If
CInt
(txtAmount.Text) = 0
Then
083.
txtAmount.Focus()
084.
Exit
Sub
085.
End
If
086.
Dim
i
As
Integer
= 0
087.
Dim
lvi
As
ListViewItem
088.
Dim
tmpFoodID
As
Integer
= 0
089.
For
i = 0
To
lsvProductList.Items.Count - 1
090.
tmpFoodID =
CInt
(lsvProductList.Items(i).SubItems(0).Text)
091.
If
CInt
(txtFoodID.Text) = tmpFoodID
Then
092.
MessageBox.Show(
"คุณเลือกรายการอาหารซ้ำกัน กรุณาเลือกใหม่!!!"
,
"ผลการตรวจสอบ"
, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Information)
093.
ClearFoodData()
094.
txtFoodID.Focus()
095.
txtFoodID.SelectAll()
096.
Exit
Sub
097.
End
If
098.
Next
099.
Dim
anyData()
As
String
100.
anyData =
New
String
() {txtFoodID.Text, lblFoodName.Text, lblSalePrice.Text, txtAmount.Text, lblTotal.Text}
101.
lvi =
New
ListViewItem(anyData)
102.
lsvProductList.Items.Add(lvi)
103.
CalculateNet()
104.
ClearFoodData()
105.
cmdSave.Enabled =
True
106.
txtFoodID.Focus()
107.
End
Sub
108.
109.
Private
Sub
lsvProductList_SelectedIndexChanged(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
lsvProductList.SelectedIndexChanged
110.
Dim
i
As
Integer
111.
112.
For
i = 0
To
lsvProductList.SelectedItems.Count - 1
113.
Dim
lvi
As
ListViewItem
114.
lvi = lsvProductList.SelectedItems(i)
115.
lsvProductList.Items.Remove(lvi)
116.
Next
117.
CalculateNet()
118.
txtAmount.Focus()
119.
End
Sub
120.
Private
Sub
CalculateNet()
121.
Dim
i
As
Integer
= 0
122.
Dim
tmpNetTotal
As
Double
= 0
123.
124.
For
i = 0
To
lsvProductList.Items.Count - 1
125.
126.
tmpNetTotal +=
CDbl
(lsvProductList.Items(i).SubItems(4).Text)
127.
Next
128.
129.
lblNet.Text = tmpNetTotal.ToString(
"#,##0.00"
)
130.
End
Sub
131.
132.
Private
Sub
cmdClear_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
cmdClear.Click
133.
lsvProductList.Items.Clear()
134.
lblNet.Text =
"0"
135.
txtFoodID.Focus()
136.
End
Sub
137.
138.
Private
Sub
cmdSave_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
cmdSave.Click
139.
If
lsvProductList.Items.Count <> 0
Then
140.
If
MessageBox.Show(
"คุณต้องการบันทึกรายการอาหาร ใช่หรือไม่ ?"
,
"คำยืนยัน"
, MessageBoxButtons.OK, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes
Then
141.
Dim
i
As
Integer
142.
Dim
sqlsave
As
String
143.
Dim
LastOrderID
As
Long
144.
Dim
comSave
As
New
OleDbCommand
145.
Dim
drLastOrderID
As
OleDbDataReader
146.
147.
sqlsave =
"insert into order(customer_name,order_date)"
148.
sqlsave = sqlsave &
" values('"
& txtCustomerName.Text &
"',"
149.
sqlsave = sqlsave &
"'"
&
Date
.Today &
"')"
150.
With
Conn
151.
If
.State = ConnectionState.Open
Then
.Close()
152.
.ConnectionString = strConn
153.
.Open()
154.
End
With
155.
With
comSave
156.
.CommandType = CommandType.Text
157.
.CommandText = sqlsave
158.
.Connection = Conn
159.
.ExecuteNonQuery()
160.
End
With
161.
162.
sqlsave =
"select top 1 order_id from order"
163.
sqlsave = sqlsave &
"Order by order_id DESC"
164.
With
Conn
165.
If
.State = ConnectionState.Open
Then
.Close()
166.
.ConnectionString = strConn
167.
.Open()
168.
End
With
169.
With
comSave
170.
.CommandType = CommandType.Text
171.
.CommandText = sqlsave
172.
.Connection = Conn
173.
174.
drLastOrderID = .ExecuteReader()
175.
drLastOrderID.Read()
176.
177.
LastOrderID =
CLng
(drLastOrderID.Item(
"order_id"
))
178.
End
With
179.
drLastOrderID.Close()
180.
181.
For
i = 0
To
lsvProductList.Items.Count - 1
182.
183.
sqlsave =
"insert into (order_detail)(order_id,food_id,"
184.
sqlsave = sqlsave &
"unitPrice,quantity,discount)"
185.
186.
sqlsave = sqlsave &
" values("
& LastOrderID &
","
187.
sqlsave = sqlsave &
"'"
&
CStr
(lsvProductList.Items(i).SubItems(0).Text) &
"',"
188.
sqlsave = sqlsave &
CInt
(lsvProductList.Items(i).SubItems(2).Text) &
","
189.
sqlsave = sqlsave &
CInt
(lsvProductList.Items(i).SubItems(3).Text) &
","
190.
sqlsave = sqlsave &
"0)"
191.
With
comSave
192.
.CommandText = sqlsave
193.
.ExecuteNonQuery()
194.
End
With
195.
Next
196.
MessageBox.Show(
"บันทึกรายการอาหารที่สั่งซื้อเรียบร้อยแล้ว !!!"
,
"ผลการทำงาน"
, MessageBoxButtons.OK, MessageBoxIcon.Information)
197.
lsvProductList.Clear()
198.
ClearFoodData()
199.
lblNet.Text =
"0"
200.
txtCustomerName.Focus()
201.
End
If
202.
End
If
203.
End
Sub
204.
End
Class