Syntax Form Transaksi di VB.net

Frmtransaksi.vb
Public Class frmtransaksi
    Dim proses As New ClsKoneksi
    Dim tbcustomer As DataTable
    Dim tbtransaksi As DataTable
    Dim tbbarang As DataTable
    Dim SQL As String
    Dim ismember As Boolean

    Private Sub frmtransaksi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        init_form()

    End Sub
    Private Sub init_form()
        ismember = False
        auto_code_customer()
        auto_code_transaksi()

        txttanggal.Text = Now.Date
        txtkasir.Text = gl_user_login
        txtnamacustomer.Clear()
        txttelepon.Clear()
        txtidbarang.Clear()
        txtnabar.Clear()
        txtmerk.Clear()
        txtharga.Clear()
        txtqty.Clear()
        txttotal.Clear()
        txtbayar.Clear()
        txttobay.Clear()
        txtkembali.Clear()
        txtpotongan.Clear()
        txttotal.Text = "0"

        If DGVTtansaksi.RowCount <> 0 Then
            DGVTtansaksi.Rows.Clear()
        End If

        txtnotrans.Enabled = False
        txttanggal.Enabled = False
        txtkasir.Enabled = False
        txtidcustomer.Enabled = False
        txtnamacustomer.Enabled = True
        txttelepon.Enabled = True

        txtidbarang.Enabled = False
        txtnabar.Enabled = False
        txtmerk.Enabled = False
        txtharga.Enabled = False

        txtqty.Enabled = True
        txttobay.Enabled = False
        txtbayar.Enabled = False
        txtkembali.Enabled = False
        txtpotongan.Enabled = False

    End Sub

    Private Sub btncaribarang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncaribarang.Click
        frmcaribarang.ShowDialog()
        If frmcaribarang.DialogResult = Windows.Forms.DialogResult.OK Then
            txtidbarang.Text = gl_kode_Barang
            txtnabar.Text = gl_nama_Barang
            txtmerk.Text = gl_merk_barang
            txtharga.Text = gl_harga_Barang
            txtqty.Focus()
        End If
    End Sub

    Private Sub btnhapus1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        auto_code_customer()
        txtnamacustomer.Clear()
        txttelepon.Clear()
        txtnamacustomer.Enabled = True
        txttelepon.Enabled = True
        txtnamacustomer.Focus()
        ismember = False
        txtmember.Visible = False
        get_total()
    End Sub
    Private Sub auto_code_customer()
        tbcustomer = proses.ExecuteQuery("select * from tb_customer order by id_customer desc")
        If tbcustomer.Rows.Count = 0 Then
            txtidcustomer.Text = "CS001"
        Else
            With tbcustomer.Rows(0)
                txtidcustomer.Text = .Item("id_customer")
            End With

            txtidcustomer.Text = Val(Microsoft.VisualBasic.Mid(txtidcustomer.Text, 3, 3)) + 1
            If Len(txtidcustomer.Text) = 1 Then
                txtidcustomer.Text = "CS00" & txtidcustomer.Text & ""
            ElseIf Len(txtidcustomer.Text) = 2 Then
                txtidcustomer.Text = "CS0" & txtidcustomer.Text & ""
            ElseIf Len(txtidcustomer.Text) = 3 Then
                txtidcustomer.Text = "CS" & txtidcustomer.Text & ""
            End If
        End If

    End Sub
    Private Sub auto_code_transaksi()
        Dim dateno, tgl, bln, thn As String
        Dim code As String
        tgl = Format(Now.Date, "dd")
        bln = Format(Now.Date, "mm")
        thn = Format(Now.Date, "yy")

        dateno = "TRX" & bln & tgl & thn
        tbtransaksi = proses.ExecuteQuery("select id_transaksi from tb_transaksi order by Cast(SUBSTRING(id_transaksi, 12,100) as INT) desc")
        If tbtransaksi.Rows.Count = 0 Then
            txtnotrans.Text = dateno & "1 "
        Else
            With tbtransaksi.Rows(0)
                code = .Item("id_transaksi")
            End With
            Dim trans As String
            trans = dateno & Val(Integer.Parse(Microsoft.VisualBasic.Mid(code, 9))) + 1
            txtnotrans.Text = trans

        End If
    End Sub
    Private Sub Add()
        If txtidbarang.Text = "" Then
            MsgBox("Tidak ada barang yang dibeli", MsgBoxStyle.Critical, "Error")
            btncaribarang.Focus()
            Return
        End If
        If txtqty.Text = "" Then
            MsgBox("Silahkan isi julah beli", MsgBoxStyle.Critical, "Error")
            txtqty.Focus()
            Return
        End If
        If txtqty.Text = "0" Then
            MsgBox("minimal pembelian 1", MsgBoxStyle.Critical, "Error")
            txtqty.Focus()
            Return
        End If
        If DGVTtansaksi.RowCount <> 0 Then
            Dim i As Integer
            For i = 0 To DGVTtansaksi.RowCount - 1
                If (txtidbarang.Text = DGVTtansaksi.Rows(i).Cells(0).Value.ToString) Then
                    MsgBox("id barang" & txtidbarang.Text & " sudah ada", MsgBoxStyle.Critical, "Error")
                    Return
                End If
            Next
        End If

        tbbarang = proses.ExecuteQuery("select stock from tb_barang where id_barang = '" & txtidbarang.Text & "' ")
        Dim stock As Integer
        If tbbarang.Rows.Count <> 0 Then
            stock = Val(tbbarang.Rows(0).Item("stock").ToString)
            If stock < Val(txtqty.Text) Then
                MsgBox("id barang " & txtidbarang.Text & " ada " & stock, MsgBoxStyle.Critical, "Stock tidak mencukupi")
                Return
            End If
        Else
            MsgBox("data barang tidak valid", MsgBoxStyle.Critical, "Error")
            Return
        End If

        Dim row As String() = New String() {txtidbarang.Text, txtnabar.Text, txtmerk.Text, txtharga.Text, txtqty.Text, Val(txtqty.Text) * Val(txtharga.Text)}
        DGVTtansaksi.Rows.Add(row)
        txtidbarang.Clear()
        txtnabar.Clear()
        txtharga.Clear()
        txtmerk.Clear()
        txtqty.Clear()
        get_total()
    End Sub

    Private Sub btncaricustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmcaricustomer.ShowDialog()
        If frmcaricustomer.DialogResult = Windows.Forms.DialogResult.OK Then
            txtidcustomer.Text = gl_kode_Customer
            txtnamacustomer.Text = gl_nama_Customer
            txttelepon.Text = gl_telepon_Customer
            txtnamacustomer.Enabled = False
            txttelepon.Enabled = False
            If gl_status_Customer = "member" Then
                ismember = True
                txtmember.Visible = Visible
            Else
                ismember = False
                txtmember.Visible = False
            End If

            get_total()
        End If
    End Sub

    Private Sub btntambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntambah.Click
        Add()
    End Sub

    Private Sub txtqty_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtqty.KeyPress
        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack Or e.KeyChar = Chr(13)) Then e.Handled() = True
        If e.KeyChar = Chr(13) Then
            Add()
        End If
    End Sub

    Private Sub get_total()
        If DGVTtansaksi.RowCount <> 0 Then
            Dim i As Integer
            Dim total As Integer
            For i = 0 To DGVTtansaksi.RowCount - 1
                If (DGVTtansaksi.Rows(i).Cells(0).Value.ToString <> "") Then
                    total += Val(DGVTtansaksi.Rows(i).Cells(5).Value.ToString)
                End If
            Next
            txttotal.Text = total
        Else
            txttotal.Text = "0"
        End If

        If ismember Then
            txtpotongan.Text = Val(txttotal.Text) * (10 / 100)
        Else
            txtpotongan.Text = "0"
        End If
        txttobay.Text = Val(txttotal.Text) - Val(txtpotongan.Text)
        txtbayar.Enabled = True

    End Sub

    Private Sub DGVTtansaksi_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVTtansaksi.CellContentClick
        If e.RowIndex < 0 Then
            Exit Sub
        End If
        If MessageBox.Show("anda yakin ingin hapus ", "konfirmasi", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Return
        Else
            DGVTtansaksi.Rows.RemoveAt(e.RowIndex)
            get_total()
        End If

    End Sub
    Private Sub save()
        If Val(txtbayar.Text) < Val(txttobay.Text) Then
            MsgBox("Uang bayar kurang ", MsgBoxStyle.Critical, "Error")
            Return
        Else
            txtkembali.Text = Val(txtbayar.Text) - Val(txttobay.Text)
        End If

        If txtidcustomer.Text = "" Then
            MsgBox("Customer tidak boleh kosong", MsgBoxStyle.Critical, "Error")
            Return
        End If

        If DGVTtansaksi.RowCount < 1 Then
            MsgBox("Barang yang dibeli tidak boleh kosong", MsgBoxStyle.Critical, "Error")
            Return
        End If

        If txtbayar.Text = "" Then
            MsgBox("Silahkan bayar terlebih dahulu", MsgBoxStyle.Critical, "Error")
            Return
        End If

        If txtbayar.Text = "" Then
            MsgBox("silahkan bayar terlebih dahulu", MsgBoxStyle.Critical, "Error")
            Return
        End If

        Try
            SQL = "insert into tb_transaksi values('" & txtnotrans.Text & "','" & CDate(Now) & "', '" & txtidcustomer.Text & "', '" & txtpotongan.Text & "', '" & txttobay.Text & "', '" & gl_user_login & "')"
            proses.ExecuteNonQuery(SQL)

            tbcustomer = proses.ExecuteQuery("select id_customer from tb_customer where id_customer = '" & txtidcustomer.Text & "' ")
            If tbcustomer.Rows.Count = 0 Then
                SQL = "insert into tb_customer values('" & txtidcustomer.Text & "','" & txtnamacustomer.Text & "', null, '" & txttelepon.Text & "',' non member')"
                proses.ExecuteNonQuery(SQL)
            End If

            Dim i As Integer
            For i = 0 To DGVTtansaksi.RowCount - 1
                SQL = "insert into tb_detail_transaksi ( id_transaksi, id_barang, jml_beli, harga_satuan,sub_total) values ('" & txtnotrans.Text & "', '" & DGVTtansaksi.Rows(i).Cells(0).Value.ToString & "', '" & DGVTtansaksi.Rows(i).Cells(4).Value.ToString & "','" & DGVTtansaksi.Rows(i).Cells(3).Value.ToString & "', '" & DGVTtansaksi.Rows(i).Cells(5).Value.ToString & "' )"
                proses.ExecuteNonQuery(SQL)

                SQL = "update tb_barang set stock = stock - " & Val(DGVTtansaksi.Rows(i).Cells(4).Value.ToString) & " where id_barang = '" & DGVTtansaksi.Rows(i).Cells(0).Value.ToString & "'  "
                proses.ExecuteNonQuery(SQL)


            Next
            If MessageBox.Show("transaksi berhasil, ingin cetak ?", "konfirmasi", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                init_form()
                'cetak(txtnotrans.Text)

            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub txtbayar_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbayar.KeyPress
        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack Or e.KeyChar = Chr(13)) Then e.Handled() = True
        If e.KeyChar = Chr(13) Then
            save()
        End If
    End Sub


    Private Sub txttelepon_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txttelepon.KeyPress
        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled() = True
    End Sub

    Private Sub btncetak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncetak.Click
        save()

    End Sub

    Private Sub txtbaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbaru.Click
        init_form()
    End Sub

    Private Sub btnkeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkeluar.Click
        MsgBox("Ingin Kembali Ke menu Utama ? ", MsgBoxStyle.YesNo)

        If vbYes Then
            menuutama1.Show()
            Me.Hide()
        Else
            Application.Exit()
            'Me.Close()
        End If

    End Sub

    Private Sub txtbayar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbayar.TextChanged

    End Sub
End Class

Komentar

Postingan Populer