Imports System.Data.SqlClient
Public Class frmTransaction
    Dim x As String = ""
    Dim rand As Random = New Random()
    Dim change As Double = 0
    Dim paymode As String = ""
  
    Private Sub frmTransaction_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        connection()
        Timer1.Start()
        getPassengerList()
        payment()
        generate()
        generateOR()
        txtCargo.Enabled = False
        txtCP.Enabled = False
        cm = New SqlCommand("select * from tblCapacity where capacity like '" & lblCount.Text & "'", cn)
        dr = cm.ExecuteReader
        dr.Read()
        If dr.HasRows Then
            MessageBox.Show("Sorry. You reach the maximum capacity(" & lblCapacity.Text & ") of this boat!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
            dr.Close()
            GroupBox1.Enabled = False
            GroupBox2.Enabled = False
            btnPay.Enabled = False
            ListView1.Enabled = False
            cmbPassenger.Enabled = False
            rbOrdinary.Enabled = False
            rbSenior.Enabled = False
            rbStudent.Enabled = False
            txtSearch.Enabled = False
            Return
        Else
            GroupBox1.Enabled = True
            GroupBox2.Enabled = True
            btnPay.Enabled = True
            ListView1.Enabled = True
            cmbPassenger.Enabled = True
            rbOrdinary.Enabled = True
            rbSenior.Enabled = True
            rbStudent.Enabled = True
            txtSearch.Enabled = True
        End If
        dr.Close()
        getSeat()
        txtCash.Enabled = False
        ' txtSearch.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblTime.Text = DateTime.Now.ToString("hh:mm:ss tt")
        lbldate.Text = DateTime.Now.ToString("MM/dd/yy")
    End Sub

    Sub generate()
        For y As Integer = 1 To 3
            If y = 3 Then
                x += "POWER-"
            End If
        Next

        For y As Integer = 1 To 4
            x += Convert.ToString(rand.Next(0, 9))
            If y = 4 Then
                x += ""
            End If
        Next

        For y As Integer = 1 To 3
            x += Convert.ToString(rand.Next(0, 9))
        Next

        txtID.Text = x
        If txtID.Text = x Then
            x = Nothing
        End If
    End Sub

    Sub generateOR()
        For y As Integer = 1 To 5
            If y = 5 Then
                x += "00000"
            End If
        Next

        For y As Integer = 1 To 5
            If y = 5 Then
                x += Convert.ToString(rand.Next(0, 9))
            End If
        Next

        For y As Integer = 1 To 3
            x += Convert.ToString(rand.Next(0, 9))
        Next

        lblOR.Text = x
        If lblOR.Text = x Then
            x = Nothing
        End If
    End Sub

    Sub getSeat()
        cmbPassenger.Items.Clear()
        Try
            cm = New SqlCommand("select * from tblSeat where status like 'Active' order by seatNo", cn)
            dr = cm.ExecuteReader
            While dr.Read
                cmbPassenger.Items.Add(dr.Item(1).ToString)
            End While
            dr.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub rbOrdinary_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbOrdinary.CheckedChanged
        txtPayment.Text = lblOrdinary.Text
        txttotal.Text = lblOrdinary.Text
        txtCash.Enabled = True
        TextBox1.Text = "Ordinary Passenger"
    End Sub

    Private Sub rbStudent_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbStudent.CheckedChanged
        txtPayment.Text = lblStudent.Text
        txttotal.Text = lblStudent.Text
        txtCash.Enabled = True
        TextBox1.Text = "Student"
    End Sub

    Private Sub rbSenior_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSenior.CheckedChanged
        txtPayment.Text = lblSenior.Text
        txttotal.Text = lblSenior.Text
        txtCash.Enabled = True
        TextBox1.Text = "Senior Citizen"
    End Sub

    Sub payment()
        Try
            cm = New SqlCommand("select * from tblPayment where passenger like 'Ordinary Passenger'", cn)
            dr = cm.ExecuteReader
            While dr.Read
                lblOrdinary.Text = String.Format("{0:0.0}", dr.Item(2))
            End While
            dr.Close()

            cm = New SqlCommand("select * from tblPayment where passenger like 'Student'", cn)
            dr = cm.ExecuteReader
            While dr.Read
                lblStudent.Text = String.Format("{0:0.0}", dr.Item(2))
            End While
            dr.Close()

            cm = New SqlCommand("select * from tblPayment where passenger like 'Senior Citizen'", cn)
            dr = cm.ExecuteReader
            While dr.Read
                lblSenior.Text = String.Format("{0:0.0}", dr.Item(2))
            End While
            dr.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub txtCash_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCash.TextChanged
        If txtCash.Text = "" Then
            txtCash.Text = "0.0"
            Return
        End If
        If txtPayment.Text = "" Then
            txtPayment.Text = "0.0"
            Return
        End If

        change = CDbl(txtCash.Text) - CDbl(txttotal.Text)
        txtChange.Text = String.Format("{0:0.0}", change)
        If CDbl(txtCash.Text) < CDbl(txttotal.Text) Then
            txtChange.Text = "0.0"
        End If
    End Sub

    Private Sub txtCash_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCash.KeyPress
        If Not Char.IsNumber(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then e.KeyChar = ""
    End Sub

    Private Sub txtCash_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCash.Leave
        If txtCash.Text = "" Then
            txtCash.Text = "0.0"
        End If
    End Sub

    Private Sub txtCash_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCash.Enter
        If txtCash.Text = "0.0" Then
            txtCash.Text = ""
        End If
    End Sub

    Private Sub btnPay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPay.Click
        If is_empty(txtName) = True Then Exit Sub
        If is_empty(txtAddress) = True Then Exit Sub
        If is_empty(txtAge) = True Then Exit Sub
        If is_empty_combo(cmbGender) = True Then Exit Sub
        If is_empty_combo(cmbPassenger) = True Then Exit Sub
        If is_empty(txtCash) = True Then Exit Sub

        If CDbl(txtCash.Text) < CDbl(txttotal.Text) Then
            MessageBox.Show("Kuyang imu kwarta brad!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        cm = New SqlCommand("select * from tblCapacity where capacity like '" & CInt(lblCount.Text) & "'", cn)
        dr = cm.ExecuteReader
        dr.Read()
        If dr.HasRows Then
            MessageBox.Show("You reach the maximum capacity(" & lblCapacity.Text & ") of this boat.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            dr.Close()
            GroupBox1.Enabled = False
            GroupBox2.Enabled = False
            btnPay.Enabled = False
            ListView1.Enabled = False
            cmbPassenger.Enabled = False
            rbOrdinary.Enabled = False
            rbSenior.Enabled = False
            rbStudent.Enabled = False
            txtSearch.Enabled = False
        End If
        dr.Close()

        Try
            
            cm = cn.CreateCommand
            cm.CommandText = "EXECUTE spTransaction @pID,@name,@address,@age,@orNo,@seat,@classification,@date,@time,@payment,@change,@cash,@cargopay,@total,@cargo,@gender"
            cm.Parameters.Add("@pID", SqlDbType.VarChar, 50).Value = txtID.Text
            cm.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = txtName.Text
            cm.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = txtAddress.Text
            cm.Parameters.Add("@age", SqlDbType.Int, 50).Value = CInt(txtAge.Text)
            cm.Parameters.Add("@orNo", SqlDbType.VarChar, 50).Value = lblOR.Text
            cm.Parameters.Add("@seat", SqlDbType.VarChar, 50).Value = cmbPassenger.Text
            cm.Parameters.Add("@classification", SqlDbType.VarChar, 50).Value = TextBox1.Text
            cm.Parameters.Add("@date", SqlDbType.VarChar, 50).Value = lbldate.Text
            cm.Parameters.Add("@time", SqlDbType.VarChar, 50).Value = lblTime.Text
            cm.Parameters.Add("@payment", SqlDbType.Real, 50).Value = CDbl(txtPayment.Text)
            cm.Parameters.Add("@change", SqlDbType.Real, 50).Value = CDbl(txtChange.Text)
            cm.Parameters.Add("@cash", SqlDbType.Real, 50).Value = CDbl(txtCash.Text)
            cm.Parameters.Add("@cargopay", SqlDbType.Real, 50).Value = CDbl(txtCP.Text)
            cm.Parameters.Add("@total", SqlDbType.Real, 50).Value = CDbl(txttotal.Text)
            cm.Parameters.Add("@cargo", SqlDbType.VarChar, 50).Value = txtCargo.Text
            cm.Parameters.Add("@gender", SqlDbType.VarChar, 50).Value = cmbGender.Text
            cm.ExecuteNonQuery()
            ' MessageBox.Show("Successfully pay the bill", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)

            dr.Close()
            cm = New SqlCommand("update tblSeat set status = 'Inactive' where seatNo like '" & cmbPassenger.Text & "'", cn)
            cm.ExecuteNonQuery()
            dr.Close()

            frmCenter.auditTrail(uID, "Release passenger ticket to Mr/Mrs. " & txtName.Text & " and have a seat number " & cmbPassenger.Text & "")
            frmCenter.dawat(uID, loginDate)

            If rbOrdinary.Checked = True Then
                paymode = "Ordinary passenger"
            ElseIf rbSenior.Checked = True Then
                paymode = "Senior Citizen passenger"
            ElseIf rbStudent.Checked = True Then
                paymode = "Student passenger"
            End If
            frmTicket.lblOR.Text = lblOR.Text
            frmTicket.lblTime.Text = lblTime.Text
            frmTicket.lblDate.Text = lbldate.Text
            frmTicket.lblName.Text = txtName.Text
            frmTicket.lblGender.Text = cmbGender.Text
            frmTicket.lblClassification.Text = paymode
            frmTicket.lblAge.Text = txtAge.Text
            frmTicket.lblAddress.Text = txtAddress.Text
            frmTicket.lblSeatNo.Text = cmbPassenger.Text
            frmTicket.lblCargo.Text = txtCargo.Text
            frmTicket.lblTot.Text = txttotal.Text
            frmTicket.lblPay.Text = txtPayment.Text
            frmTicket.lblCP.Text = txtCP.Text
            frmTicket.lblCash.Text = txtCash.Text
            frmTicket.lblChange.Text = txtChange.Text
            frmTicket.ShowDialog()
            getPassengerList()
            generate()
            generateOR()
            getSeat()
            clear()
            txtPayment.Text = "0.00"
          

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub getPassengerList()
        ListView1.Items.Clear()
        lbldate.Text = DateTime.Now.ToString("MM/dd/yy")
        Try
            cm = New SqlCommand("select * from vwTransaction where date like '" & lbldate.Text & "'order by name", cn)
            dr = cm.ExecuteReader
            While dr.Read
                lst = ListView1.Items.Add(dr.Item(0))
                lst.SubItems.Add(dr.Item(1))
                lst.SubItems.Add(dr.Item(2))
                lst.SubItems.Add(dr.Item(3))
            End While
            dr.Close()

            cm = New SqlCommand("select * from tblCapacity", cn)
            dr = cm.ExecuteReader
            While dr.Read
                lblCapacity.Text = dr.Item(0).ToString
            End While
            dr.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        lblCount.Text = recordCount("select count(name) from vwTransaction where date like '" & lbldate.Text & "'")
    End Sub

    Sub clear()
        txtName.Clear()
        txtCargo.Clear()
        txtAddress.Clear()
        txtAge.Clear()
        txtPayment.Text = "0.0"
        txtCash.Text = "0.0"
        txtChange.Text = "0.0"
        rbOrdinary.Checked = False
        rbSenior.Checked = False
        rbStudent.Checked = False
    End Sub

    'Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
    '    'getPassengerList()
    '    'txtSearch.Enabled = True
    'End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        ListView1.Items.Clear()
        Try
            cm = New SqlCommand("select * from vwTransaction where name like '" & txtSearch.Text & "%' and date like '" & lbldate.Text & "'", cn)
            dr = cm.ExecuteReader
            While dr.Read
                lst = ListView1.Items.Add(dr.Item(0))
                lst.SubItems.Add(dr.Item(1))
                lst.SubItems.Add(dr.Item(2))
                lst.SubItems.Add(dr.Item(3))
            End While
            dr.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub txtCP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCP.TextChanged

        Try
            Dim tot As Double = CDbl(txtPayment.Text) + CDbl(txtCP.Text)
            txttotal.Text = String.Format("{0:0.0}", tot)
        Catch ex As Exception
            txttotal.Text = txtPayment.Text
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Add" Then
            txtCargo.Enabled = True
            txtCP.Enabled = True
            Button1.Text = "Cancel"
        ElseIf Button1.Text = "Cancel" Then
            txtCargo.Enabled = False
            txtCP.Enabled = False
            Button1.Text = "Add"
        End If
    End Sub
End Class