ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 467|ตอบกลับ: 0

[VB.NET] การคิดค่าบริการทั้งแบบ Service Charge และ None Service Charge ในรายการเดียว

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6066

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6066




โค้ด VB .NET (2010) กับการคิดค่าบริการ (Service Charge) และคำนวณหาภาษีทั้งแบบไม่คิดภาษี แบบรวมภาษี และแบบแยกภาษี ... อันนี้ไม่ใช่ดราม่านะครับ แต่เป็นการคิดคำนวณ Service Charge เพราะจริงๆแล้วตามโรงแรมต่างๆ หรือร้านอาหารใหญ่ๆ เขาไม่ได้คิดไปหมดทุกรายการ หลักๆก็จะเป็นค่าอาหาร ส่วนพวกห้องพัก หรือห้องคาราโอเกะพวกนี้จะไม่คิด ดังนั้นการคำนวณก็จะต้องแยกออกจากกัน เช่น ตัวอย่างรายการแรกคิด Service Charge เพียงค่าเดียว ก็จะเอาค่า 1,000 บาท (ไม่ใช่รวมหมด 3,000) มาหา 10% ของ Service Charge = 100 ก็จะได้ค่ารวม 1,100 บาท จากนั้นก็จะนำไปรวมกับค่าที่ไม่คิดอีก 2,000 ค่าที่ได้ทั้ง 2 มารวมกันเพื่อนำไปคำนวณหาภาษีอีกทีครับ ซึ่งปกติจะคิดแบบแยกภาษี (ภาระของผู้บริโภคนั่นเอง)

ในงานจริงเราจะต้องกำหนดสินค้า/บริการมาไว้ก่อนล่วงหน้าแล้ว ว่าอันไหนคิดหรือไม่คิด Service Charge สำหรับโค้ดชุดนี้ผมเลยนำเสนอวิธีการควบคุม #DataGridView ด้วยการเขียนโค้ดทั้งหมดล้วนๆ หรือที่เรียกกันว่า Run Time (สั่งรันถึงจะเห็นผลลัพธ์) ไว้ให้สำหรับชาว VB ได้ศึกษากัน


มาดูโค้ดกันเถอะ ...
  1. Public Class frmServiceChargeVat

  2.     ' / --------------------------------------------------------------------------------
  3.     '/ Don't forget to set Form has KeyPreview = True
  4.     Private Sub frmServiceChargeVat_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  5.         '/ การกดฟังค์ชั่นคีย์
  6.         Select Case e.KeyCode
  7.             Case Keys.F7
  8.                 '/ Add Row
  9.                 Call btnAddRow_Click(sender, e)
  10.             Case Keys.F8
  11.                 '/ Remove Row
  12.                 Call btnRemoveRow_Click(sender, e)
  13.         End Select
  14.     End Sub

  15.     ' / --------------------------------------------------------------------------------
  16.     Private Sub frmServiceChargeVat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  17.         Me.KeyPreview = True  '/ สามารถกดปุ่มฟังค์ชั่นคีย์ลงในฟอร์มได้
  18.         With cmbTax
  19.             .Items.Add("None Vat")
  20.             .Items.Add("Include Vat")
  21.             .Items.Add("Exclude Vat")
  22.         End With
  23.         cmbTax.SelectedIndex = 2
  24.         '//
  25.         Call InitializeGrid()
  26.         Call FillDataSample()   '/ ตัวอย่างสมมุติ
  27.         Call CalSumTotal()  '/ รวมจำนวนเงิน
  28.         '//
  29.         Label7.Anchor = AnchorStyles.Bottom + AnchorStyles.Left
  30.         cmbTax.Anchor = AnchorStyles.Bottom + AnchorStyles.Left
  31.     End Sub

  32.     ' / --------------------------------------------------------------------------------
  33.     ' / การตั้งค่า DataGridView แบบ @Run Time หรือ Dynamically.
  34.     Private Sub InitializeGrid()
  35.         With dgvData
  36.             .RowHeadersVisible = False
  37.             .AllowUserToAddRows = False
  38.             .AllowUserToDeleteRows = False
  39.             .AllowUserToResizeRows = False
  40.             .MultiSelect = False
  41.             .ReadOnly = False
  42.             .RowTemplate.MinimumHeight = 27
  43.             .RowTemplate.Height = 27
  44.             .Font = New Font("Tahoma", 10)
  45.             '/ Columns Specified
  46.             '/ Index = 0
  47.             .Columns.Add("PK", "Primary Key")
  48.             With .Columns("PK")
  49.                 .ReadOnly = True
  50.                 .DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
  51.                 .Visible = True 'False '/ ปกติหลัก Primary Key จะต้องถูกซ่อนไว้
  52.             End With
  53.             .Columns.Add("ProductID", "Product ID") '/ Index = 1
  54.             .Columns.Add("ProductName", "Product Name") '/ Index = 2
  55.             '/ Read Only
  56.             .Columns(1).ReadOnly = True '/ รหัสสินค้า
  57.             .Columns(2).ReadOnly = True '/ ชื่อสินค้า
  58.             '/
  59.             .Columns.Add("Quantity", "Quantity") '/ Index = 3
  60.             .Columns("Quantity").ValueType = GetType(Integer)
  61.             .Columns.Add("UnitPrice", "Unit Price") '/ Index = 4
  62.             .Columns("UnitPrice").ValueType = GetType(Double)
  63.             '/ Index = 5 (CheckBox) ... เลือกว่าคิด Service Charge หรือไม่
  64.             Dim chkServiceCharge As New DataGridViewCheckBoxColumn
  65.             .Columns.Add(chkServiceCharge)
  66.             chkServiceCharge.HeaderText = "Service Charge"
  67.             chkServiceCharge.Name = "chkServiceCharge"
  68.             '/ Index = 6 ... Total = (จำนวน x ราคา)
  69.             .Columns.Add("Total", "Total")
  70.             .Columns("Total").ValueType = GetType(Double)
  71.             '/ Total Column
  72.             With .Columns("Total")
  73.                 .ReadOnly = True
  74.                 .DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
  75.                 .DefaultCellStyle.ForeColor = Color.Blue
  76.                 .DefaultCellStyle.Font = New Font(dgvData.Font, FontStyle.Bold)
  77.             End With
  78.             '/ Alignment MiddleRight only columns 3, 4 and 6
  79.             For i As Byte = 3 To 6
  80.                 If i <> 5 Then
  81.                     .Columns(i).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
  82.                     .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  83.                     '// กรณีหลัก 5 เป็น CheckBox ให้จัดตำแหน่งตรงกึ่งกลางของเซลล์
  84.                 Else
  85.                     .Columns(i).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
  86.                     .Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  87.                 End If
  88.             Next
  89.             '/ Auto size column width of each main by sorting the field.
  90.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  91.             '/ Adjust Header Styles
  92.             With .ColumnHeadersDefaultCellStyle
  93.                 .BackColor = Color.Crimson
  94.                 .ForeColor = Color.White
  95.                 .Font = New Font("Tahoma", 10, FontStyle.Bold)
  96.             End With
  97.             .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
  98.             .ColumnHeadersHeight = 36
  99.             '/ กำหนดให้ EnableHeadersVisualStyles = False เพื่อให้ยอมรับการเปลี่ยนแปลงสีพื้นหลังของ Header
  100.             .EnableHeadersVisualStyles = False
  101.         End With

  102.     End Sub

  103.     ' / --------------------------------------------------------------------------------
  104.     ' / DATA SAMPLE ... ทำตัวอย่างให้ง่ายต่อการคำนวณ
  105.     Private Sub FillDataSample()
  106.         Try
  107.             '/ Primary Key, Product ID, Product Name, Quantity, UnitPrice, ServiceCharge, Total
  108.             Dim row As String() = New String() _
  109.                 {"1", "PRO00001", "Product 1", "1", "1000.00", CBool(1), "0.00"}
  110.             dgvData.Rows.Add(row)
  111.             row = New String() _
  112.                 {"2", "PRO00002", "Product 2", "1", "1000.00", CBool(0), "0.00"}
  113.             dgvData.Rows.Add(row)
  114.             row = New String() _
  115.                 {"3", "PRO00003", "Product 3", "1", "1000.00", CBool(0), "0.00"}
  116.             dgvData.Rows.Add(row)
  117.         Catch ex As Exception
  118.             MessageBox.Show(ex.Message)
  119.         End Try
  120.     End Sub

  121.     ' / --------------------------------------------------------------------------------
  122.     ' / Add new row
  123.     Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  124.         Dim Position As Integer = dgvData.Rows.Count - 1
  125.         Dim PK As Integer = 1   '/ Initialize value if without rows.
  126.         '/ Get value at the last row
  127.         Dim LastRow As New DataGridViewRow
  128.         '/ ตรวจสอบค่าแถวสุดท้ายว่ามีค่า Primary Key เท่าไหร่ก็ให้บวก 1 (เป็นการจำลองการทำงาน โดยไม่ติดต่อกับ DataBase)
  129.         '/ กรณีใช้ฐานข้อมูลจริงๆ ให้ตัดส่วนนี้ทิ้งแล้วใช้ Primary Key ของสินค้าจากฐานข้อมูลแทน
  130.         If Position >= 0 Then
  131.             '/ ไปแถวสุดท้าย
  132.             LastRow = dgvData.Rows.OfType(Of DataGridViewRow).Last()
  133.             '/ จากนั้นให้เพิ่มค่าขึ้น +1 (Column Index = 0)
  134.             PK = LastRow.Cells(0).Value + 1
  135.         End If
  136.         Dim RandomClass As New Random()
  137.         '/ Sample data
  138.         '/ Primary Key, Product ID, Product Name, Quantity, UnitPrice, ServiceCharge, Total
  139.         Dim row As String() = New String() {PK, "PRO0000" & PK, "Product " & PK, "1", Format(RandomClass.Next(100, 2000), "0.00"), RandomClass.Next(2) = 0, "0.00"}
  140.         dgvData.Rows.Add(row)
  141.         '/ โฟกัสไปที่ Column(3) หรือ Quantity (จำนวน)
  142.         dgvData.CurrentCell = dgvData.Rows(dgvData.RowCount - 1).Cells(3)
  143.         dgvData.Focus()

  144.         '/ ไปคำนวณหาค่าผลรวม
  145.         Call CalSumTotal()
  146.     End Sub

  147.     ' / --------------------------------------------------------------------------------
  148.     ' / Remove select row
  149.     Private Sub btnRemoveRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
  150.         If dgvData.RowCount = 0 Then Exit Sub
  151.         '/
  152.         dgvData.Rows.Remove(dgvData.CurrentRow)
  153.         dgvData.Refresh()
  154.         '/ เมื่อแถวรายการถูกลบออกไป และยังคงมีแถวรายการอยู่ ต้องไปคำนวณหาค่าผลรวมใหม่
  155.         If dgvData.RowCount > 0 Then Call CalSumTotal()
  156.     End Sub

  157.     ' / --------------------------------------------------------------------------------
  158.     ' / Calcualte sum of Total (Column Index = 6)
  159.     ' / ทำทุกครั้งที่มีการเพิ่มหรือลบแถวรายการ และมีการเปลี่ยนแปลงค่าในเซลล์ Quantity, UnitPrice และ Service Charge
  160.     Private Sub CalSumTotal()
  161.         Dim ServiceCharge As Double = 0
  162.         Dim NoneServiceCharge As Double = 0
  163.         'txtTotal.Text = "0.00"
  164.         'txtVat.Text = "0.00"
  165.         'txtNetTotal.Text = "0.00"
  166.         'txtServiceCharge.Text = "0.00"
  167.         'txtServiceChargeSum.Text = "0.00"
  168.         'txtNonServiceCharge.Text = "0.00"

  169.         '// Set all TextBox Control to Zero.
  170.         For Each tb As TextBox In Me.Controls.OfType(Of TextBox)()
  171.             tb.Text = "0.00"
  172.         Next
  173.         '/ วนรอบตามจำนวนแถวที่มีอยู่ปัจจุบัน
  174.         For nRow As Integer = 0 To dgvData.RowCount - 1
  175.             '// หากติ๊กเครื่องหมายถูกหลักที่ 6 (Index = 5) จะคำนวณหาส่วนของสินค้า/บริการที่คิด Service Charge
  176.             If CBool(dgvData.Rows(nRow).Cells(5).Value) Then
  177.                 '// [จำนวน x ราคา]
  178.                 ServiceCharge = Format(dgvData.Rows(nRow).Cells(3).Value * dgvData.Rows(nRow).Cells(4).Value, "#,##0.00")
  179.                 '// รวมราคาที่คิด Service Charge เป็นค่าที่ยังไม่ได้คิด 10%
  180.                 txtServiceCharge.Text = Format(CDbl(txtServiceCharge.Text) + ServiceCharge, "#,##0.00")
  181.                 '// คิด Service Charge 10% ของราคา
  182.                 ServiceCharge = ServiceCharge + (ServiceCharge * 10 / 100)
  183.                 '// รวมจำนวนราคาและ Service Charge 10%
  184.                 txtServiceChargeSum.Text = Format(CDbl(txtServiceChargeSum.Text) + ServiceCharge, "#,##0.00")

  185.                 '// None Service Charge
  186.             Else
  187.                 NoneServiceCharge = Format(dgvData.Rows(nRow).Cells(3).Value * dgvData.Rows(nRow).Cells(4).Value, "#,##0.00")
  188.                 txtNonServiceCharge.Text = Format(CDbl(txtNonServiceCharge.Text) + NoneServiceCharge, "#,##0.00")
  189.             End If
  190.             '/ หลักสุดท้ายของตารางกริด = [จำนวน x ราคา]
  191.             dgvData.Rows(nRow).Cells(6).Value = Format(dgvData.Rows(nRow).Cells(3).Value * dgvData.Rows(nRow).Cells(4).Value, "#,##0.00")
  192.         Next

  193.         '// รวมราคาสินค้า/บริการ = Service Charge + None Service Charge
  194.         Dim SumPrice As Double = Format(CDbl(txtServiceChargeSum.Text) + CDbl(txtNonServiceCharge.Text), "#,##0.00")
  195.         '// TAX - การคิดภาษี
  196.         Select Case cmbTax.SelectedIndex
  197.             '// ไม่คิดภาษี
  198.             Case 0
  199.                 txtTotal.Text = Format(SumPrice, "#,##0.00")
  200.                 txtVat.Text = "0.00"
  201.                 '// รวมจำนวนเงินทั้งหมด
  202.                 txtNetTotal.Text = Format(CDbl(txtTotal.Text) + CDbl(txtVat.Text), "#,##0.00")
  203.                 '// หรือ txtNetTotal.Text = Format(SumPrice, "#,##0.00")
  204.                 lblTotal.Text = "Total (1+2) :"
  205.                 lblVat.Text = "None Vat : "
  206.                 lblNetTotal.Text = "Net Total (Total) :"

  207.                 '// รวมภาษี (Include Tax)
  208.             Case 1
  209.                 '// คิดภาษี 7% (Include VAT)
  210.                 txtVat.Text = Format(SumPrice - (SumPrice / 1.07), "#,##0.00")
  211.                 '// หาราคาสินค้าที่แท้จริง ... ราคาสินค้าทั้งหมดลบออกจากภาษี
  212.                 txtTotal.Text = Format(SumPrice - CDbl(txtVat.Text), "#,##0.00")
  213.                 '// รวมจำนวนเงินทั้งหมด
  214.                 txtNetTotal.Text = Format(SumPrice, "#,##0.00")
  215.                 lblTotal.Text = "Total (1+2-Vat) :"
  216.                 lblVat.Text = "Include Vat (7%) : "
  217.                 lblNetTotal.Text = "Net Total (Total+Vat) :"

  218.                 '// แยกภาษี (Exclude Tax)
  219.             Case 2
  220.                 txtTotal.Text = Format(SumPrice, "#,##0.00")
  221.                 '// คิดแยกภาษี 7% (Exclude VAT)
  222.                 txtVat.Text = Format(CDbl(txtTotal.Text) * 7 / 100, "#,##0.00")
  223.                 '// รวมจำนวนเงินทั้งหมด
  224.                 txtNetTotal.Text = Format(CDbl(txtTotal.Text) + CDbl(txtVat.Text), "#,##0.00")
  225.                 lblTotal.Text = "Total (1+2) :"
  226.                 lblVat.Text = "Exclude Vat (7%) : "
  227.                 lblNetTotal.Text = "Net Total (Total+Vat) :"
  228.         End Select
  229.     End Sub

  230.     ' / --------------------------------------------------------------------------------
  231.     ' / หลังจากการแก้ไขค่าในเซลล์และเกิดการกด Enter
  232.     Private Sub dgvData_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellEndEdit
  233.         '/ เกิดการเปลี่ยนแปลงค่าในหลัก Index ที่ 3 หรือ 4
  234.         Select Case e.ColumnIndex
  235.             Case 3, 4 '/ Column Index = 3 (Quantity), Column Index = 4 (UnitPrice)
  236.                 '/ Quantity
  237.                 '/ การดัก Error กรณีมีค่า Null Value ให้ใส่ค่า 0 ลงไปแทน
  238.                 If IsDBNull(dgvData.Rows(e.RowIndex).Cells(3).Value) Then dgvData.Rows(e.RowIndex).Cells(3).Value = "0"
  239.                 Dim Quantity As Integer = dgvData.Rows(e.RowIndex).Cells(3).Value

  240.                 '/ UnitPrice
  241.                 '/ If Null Value
  242.                 If IsDBNull(dgvData.Rows(e.RowIndex).Cells(4).Value) Then dgvData.Rows(e.RowIndex).Cells(4).Value = "0.00"
  243.                 dgvData.Rows(e.RowIndex).Cells(4).Value = Format(CDbl(dgvData.Rows(e.RowIndex).Cells(4).Value), "0.00")
  244.                 Dim UnitPrice As Double = dgvData.Rows(e.RowIndex).Cells(4).Value
  245.                 '/ Quantity x UnitPrice ในหลักที่ 7 (Index = 6)
  246.                 dgvData.Rows(e.RowIndex).Cells(6).Value = (Quantity * UnitPrice).ToString("#,##0.00")
  247.                 '/ Calculate Summary
  248.                 Call CalSumTotal()
  249.         End Select
  250.     End Sub

  251.     ' / --------------------------------------------------------------------------------
  252.     ' / Validate Cell ... Quantity = Integer, UnitPrice = Double
  253.     Private Sub dgvData_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvData.EditingControlShowing
  254.         Select Case dgvData.Columns(dgvData.CurrentCell.ColumnIndex).Name
  255.             ' / Can use both Colume Index or Field Name
  256.             Case "Quantity", "UnitPrice"
  257.                 '/ Stop and Start event handler
  258.                 RemoveHandler e.Control.KeyPress, AddressOf ValidKeyPress
  259.                 AddHandler e.Control.KeyPress, AddressOf ValidKeyPress
  260.         End Select
  261.     End Sub

  262.     ' / --------------------------------------------------------------------------------
  263.     Private Sub ValidKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
  264.         Dim tb As TextBox = sender
  265.         Select Case dgvData.CurrentCell.ColumnIndex
  266.             Case 3  ' Quantity is Integer
  267.                 Select Case e.KeyChar
  268.                     Case "0" To "9"   ' digits 0 - 9 allowed
  269.                     Case ChrW(Keys.Back)    ' backspace allowed for deleting (Delete key automatically overrides)

  270.                     Case Else ' everything else ....
  271.                         ' True = CPU cancel the KeyPress event
  272.                         e.Handled = True ' and it's just like you never pressed a key at all
  273.                 End Select

  274.             Case 4  ' UnitPrice is Double
  275.                 Select Case e.KeyChar
  276.                     Case "0" To "9"
  277.                         ' Allowed "."
  278.                     Case "."
  279.                         ' can present "." only one
  280.                         If InStr(tb.Text, ".") Then e.Handled = True

  281.                     Case ChrW(Keys.Back)
  282.                         '/ Return False is Default value

  283.                     Case Else
  284.                         e.Handled = True

  285.                 End Select
  286.         End Select
  287.     End Sub

  288.     ' / --------------------------------------------------------------------------------
  289.     '// เหตุการณ์ในการคลิ๊กเมาส์ลงในเซลล์ของหลักที่ 6 (Index = 5)
  290.     Private Sub dgvData_CellContentClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellContentClick
  291.         '// Column Index = 5
  292.         If dgvData.Columns(e.ColumnIndex).Name = "chkServiceCharge" Then
  293.             Dim isChecked As Boolean = dgvData.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
  294.             If isChecked = False Then
  295.                 dgvData.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = True
  296.             Else
  297.                 dgvData.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = False
  298.             End If
  299.         End If
  300.         '//
  301.         '// คำนวณหาราคาใหม่
  302.         Call CalSumTotal()
  303.     End Sub

  304.     Private Sub cmbTax_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbTax.SelectedIndexChanged
  305.         Call CalSumTotal()
  306.     End Sub

  307.     Private Sub frmServiceChargeVat_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  308.         Me.Dispose()
  309.         GC.SuppressFinalize(Me)
  310.         Application.Exit()
  311.     End Sub

  312. End Class
คัดลอกไปที่คลิปบอร์ด



ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-5-3 05:19 , Processed in 0.194411 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้