Advertisement
Guest User

PENJUALAN,LISTBOX,DGV

a guest
May 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.46 KB | None | 0 0
  1. Imports System.Data.OleDb
  2.  
  3. Public Class Penjualan
  4.  
  5.     Sub HitungBarang()
  6.         Dim Hitung As Integer = 0
  7.         For Baris As Integer = 0 To DGV.RowCount - 1
  8.             Hitung = Hitung + DGV.Rows(Baris).Cells(3).Value
  9.         Next
  10.         LBLTotalBarang.Text = Hitung
  11.     End Sub
  12.  
  13.     Sub HitungHarga()
  14.         Dim Hitung As Integer = 0
  15.         For Baris As Integer = 0 To DGV.RowCount - 1
  16.             Hitung = Hitung + DGV.Rows(Baris).Cells(4).Value
  17.         Next
  18.         LBLTotalHarga.Text = Hitung
  19.     End Sub
  20.  
  21.     Sub Bersihkan()
  22.         ComboBox1.Text = ""
  23.         LBLNamaCustomer.Text = ""
  24.         LBLTotalBarang.Text = 0
  25.         LBLTotalHarga.Text = 0
  26.         TXTDibayar.Text = 0
  27.         LBLKembali.Text = 0
  28.         DGV.Rows.Clear()
  29.         ComboBox1.Focus()
  30.     End Sub
  31.  
  32.     Sub FakturOtomatis()
  33.         cmd = New OleDbCommand("Select Faktur from tbl_penjualan order by Faktur desc", conn)
  34.         dr = cmd.ExecuteReader
  35.         dr.Read()
  36.         If Not dr.HasRows Then
  37.             LBLFaktur.Text = Format(Today, "yyMMdd") + "0001"
  38.         Else
  39.             If Microsoft.VisualBasic.Left(dr.Item("Faktur"), 6) = Format(Today, "yyMMdd") Then
  40.                 LBLFaktur.Text = dr.Item("Faktur") + 1
  41.             Else
  42.                 LBLFaktur.Text = Format(Today, "yyMMdd") + "0001"
  43.             End If
  44.         End If
  45.     End Sub
  46.  
  47.     Private Sub Penjualan_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  48.         Call Koneksi()
  49.         Call FakturOtomatis()
  50.         LBLTanggal.Text = Format(Today, "dd-MMM-yyyy")
  51.         Call Bersihkan()
  52.         cmd = New OleDbCommand("Select Kode_Customer from tbl_customer", conn)
  53.         dr = cmd.ExecuteReader
  54.         Do While dr.Read
  55.             ComboBox1.Items.Add(dr.Item("Kode_Customer"))
  56.         Loop
  57.  
  58.         cmd = New OleDbCommand("Select * from tbl_barang", conn)
  59.         dr = cmd.ExecuteReader
  60.         Do While dr.Read
  61.             ListBox1.Items.Add(dr.Item(0) & Space(2) & dr(1))
  62.         Loop
  63.  
  64.         da = New OleDbDataAdapter("Select * from tbl_barang", conn)
  65.         ds = New DataSet
  66.         da.Fill(ds)
  67.         DGVBarang.DataSource = ds.Tables(0)
  68.         DGVBarang.ReadOnly = True
  69.     End Sub
  70.  
  71.     Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
  72.         cmd = New OleDbCommand("Select * from tbl_customer where Kode_Customer='" & ComboBox1.Text & "'", conn)
  73.         dr = cmd.ExecuteReader
  74.         dr.Read()
  75.         If dr.HasRows Then
  76.             LBLNamaCustomer.Text = dr.Item("Nama_Customer")
  77.         End If
  78.     End Sub
  79.  
  80.     Private Sub BTNTutup_Click(sender As Object, e As EventArgs) Handles BTNTutup.Click
  81.         Me.Close()
  82.     End Sub
  83.  
  84.     Private Sub BTNBatal_Click(sender As Object, e As EventArgs) Handles BTNBatal.Click
  85.         Call Bersihkan()
  86.     End Sub
  87.  
  88.     Private Sub DGV_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellEndEdit
  89.  
  90.         If e.ColumnIndex = 0 Then
  91.  
  92.             For BarisAtas As Integer = 0 To DGV.RowCount - 1
  93.                 For BarisBawah As Integer = BarisAtas + 1 To DGV.RowCount - 1
  94.  
  95.                     If DGV.Rows(BarisBawah).Cells(0).Value = DGV.Rows(BarisAtas).Cells(0).Value Then
  96.                         'MsgBox("Kode Ini Sudah Dientri")
  97.                         DGV.Rows(BarisAtas).Cells(3).Value = DGV.Rows(BarisAtas).Cells(3).Value + 1
  98.                         DGV.Rows(BarisAtas).Cells(4).Value = DGV.Rows(BarisAtas).Cells(2).Value * DGV.Rows(BarisAtas).Cells(3).Value
  99.                         Call HitungBarang()
  100.                         Call HitungHarga()
  101.                         SendKeys.Send("{UP}")
  102.                         DGV.Rows(BarisBawah).Cells(0).Value = ""
  103.                         Exit Sub
  104.                     End If
  105.  
  106.                 Next
  107.             Next
  108.             cmd = New OleDbCommand("Select * from tbl_barang where Kode_Barang='" & DGV.Rows(e.RowIndex).Cells(0).Value & "'", conn)
  109.             dr = cmd.ExecuteReader
  110.             dr.Read()
  111.             If dr.HasRows Then
  112.                 DGV.Rows(e.RowIndex).Cells(1).Value = dr.Item("Nama_Barang")
  113.                 DGV.Rows(e.RowIndex).Cells(2).Value = dr.Item("Harga_Jual")
  114.                 DGV.Rows(e.RowIndex).Cells(3).Value = 1
  115.                 DGV.Rows(e.RowIndex).Cells(4).Value = DGV.Rows(e.RowIndex).Cells(2).Value * DGV.Rows(e.RowIndex).Cells(3).Value
  116.             Else
  117.                 MsgBox("Kode Tidak Terdaftar")
  118.                 SendKeys.Send("{down}")
  119.                 DGV.Rows(e.RowIndex).Cells(0).Value = ""
  120.                 DGV.Rows.Remove(DGV.CurrentRow)
  121.             End If
  122.         End If
  123.  
  124.         If e.ColumnIndex = 3 Then
  125.             Try
  126.                 cmd = New OleDbCommand("Select * from tbl_barang where Kode_Barang='" & DGV.Rows(e.RowIndex).Cells(0).Value & "'", conn)
  127.                 dr = cmd.ExecuteReader
  128.                 dr.Read()
  129.                 If DGV.Rows(e.RowIndex).Cells(3).Value > dr.Item("Stok") Then
  130.                     MsgBox("Stok Hanya Ada " & dr.Item("Stok") & "")
  131.                     SendKeys.Send("{UP}")
  132.                     DGV.Rows(e.RowIndex).Cells(3).Value = dr.Item("Stok")
  133.                     DGV.Rows(e.RowIndex).Cells(4).Value = DGV.Rows(e.RowIndex).Cells(2).Value * DGV.Rows(e.RowIndex).Cells(3).Value
  134.                 Else
  135.                     DGV.Rows(e.RowIndex).Cells(4).Value = DGV.Rows(e.RowIndex).Cells(2).Value * DGV.Rows(e.RowIndex).Cells(3).Value
  136.                 End If
  137.             Catch ex As Exception
  138.                 MsgBox("Harus Angka")
  139.                 SendKeys.Send("{UP}")
  140.                 DGV.Rows(e.RowIndex).Cells(3).Value = 1
  141.                 DGV.Rows(e.RowIndex).Cells(4).Value = DGV.Rows(e.RowIndex).Cells(2).Value * DGV.Rows(e.RowIndex).Cells(3).Value
  142.             End Try
  143.         End If
  144.  
  145.         DGV.Columns(2).DefaultCellStyle.Format = "###,###,###"
  146.         DGV.Columns(3).DefaultCellStyle.Format = "###,###,###"
  147.         DGV.Columns(4).DefaultCellStyle.Format = "###,###,###"
  148.  
  149.         DGV.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  150.         DGV.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  151.         DGV.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  152.  
  153.         Call HitungBarang()
  154.         Call HitungHarga()
  155.     End Sub
  156.  
  157.     Private Sub DGV_KeyPress(sender As Object, e As KeyPressEventArgs) Handles DGV.KeyPress
  158.         On Error Resume Next
  159.         If e.KeyChar = Chr(27) Then
  160.             DGV.Rows.Remove(DGV.CurrentRow)
  161.             Call HitungBarang()
  162.             Call HitungHarga()
  163.         End If
  164.  
  165.         If e.KeyChar = Chr(13) Then
  166.             TXTDibayar.Focus()
  167.         End If
  168.     End Sub
  169.  
  170.     Private Sub TXTDibayar_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TXTDibayar.KeyPress
  171.  
  172.         If e.KeyChar = Chr(13) Then
  173.             If Val(TXTDibayar.Text) < Val(LBLTotalHarga.Text) Then
  174.                 MsgBox("Pembayaran Kurang")
  175.                 TXTDibayar.Focus()
  176.                 Exit Sub
  177.             ElseIf Val(TXTDibayar.Text) >= Val(LBLTotalHarga.Text) Then
  178.                 LBLKembali.Text = Val(TXTDibayar.Text) - Val(LBLTotalHarga.Text)
  179.                 BTNSimpan.Focus()
  180.             End If
  181.         End If
  182.  
  183.         If Not (e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = vbBack) Then
  184.             e.Handled = True
  185.         End If
  186.     End Sub
  187.  
  188.     Private Sub BTNSimpan_Click(sender As Object, e As EventArgs) Handles BTNSimpan.Click
  189.  
  190.         If ComboBox1.Text = "" Or TXTDibayar.Text = 0 Then
  191.             MsgBox("Transaksi Belum Lengkap")
  192.             Exit Sub
  193.         End If
  194.  
  195.         Dim SimpanPenjualan As String = "insert into tbl_penjualan values('" & LBLFaktur.Text & "','" & LBLTanggal.Text & "','" & ComboBox1.Text & "','" & LBLTotalBarang.Text & "','" & LBLTotalHarga.Text & "','" & TXTDibayar.Text & "','" & LBLKembali.Text & "','User')"
  196.         cmd = New OleDbCommand(SimpanPenjualan, conn)
  197.         cmd.ExecuteNonQuery()
  198.  
  199.         For Baris As Integer = 0 To DGV.RowCount - 2
  200.             Dim SimpanDetail As String = "insert into tbl_detailjual values('" & LBLFaktur.Text & "','" & DGV.Rows(Baris).Cells(0).Value & "','" & DGV.Rows(Baris).Cells(2).Value & "','" & DGV.Rows(Baris).Cells(3).Value & "','" & DGV.Rows(Baris).Cells(4).Value & "')"
  201.             cmd = New OleDbCommand(SimpanDetail, conn)
  202.             cmd.ExecuteNonQuery()
  203.  
  204.             cmd = New OleDbCommand("Select * from tbl_barang where Kode_Barang ='" & DGV.Rows(Baris).Cells(0).Value & "'", conn)
  205.             dr = cmd.ExecuteReader
  206.             dr.Read()
  207.  
  208.             If dr.HasRows Then
  209.                 Dim KurangiStok As String = "update tbl_barang set Stok='" & dr.Item("Stok") - DGV.Rows(Baris).Cells(3).Value & "' where Kode_Barang='" & DGV.Rows(Baris).Cells(0).Value & "'"
  210.                 cmd = New OleDbCommand(KurangiStok, conn)
  211.                 cmd.ExecuteNonQuery()
  212.             End If
  213.         Next
  214.  
  215.         Call Bersihkan()
  216.         Call FakturOtomatis()
  217.  
  218.     End Sub
  219.  
  220.     Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
  221.         Dim Baris As Integer = DGV.RowCount - 1
  222.         DGV.Rows.Add(Microsoft.VisualBasic.Left(ListBox1.Text, 5))
  223.  
  224.         For barisatas As Integer = 0 To DGV.RowCount - 1
  225.             For barisbawah As Integer = barisatas + 1 To DGV.RowCount - 1
  226.                 If DGV.Rows(barisbawah).Cells(0).Value = DGV.Rows(barisatas).Cells(0).Value Then
  227.                     DGV.Rows(barisatas).Cells(3).Value = DGV.Rows(barisatas).Cells(3).Value + 1
  228.                     DGV.Rows(barisatas).Cells(4).Value = DGV.Rows(barisatas).Cells(2).Value * DGV.Rows(barisatas).Cells(3).Value
  229.                     Call HitungBarang()
  230.                     Call HitungHarga()
  231.                     DGV.Rows.RemoveAt(barisbawah)
  232.                     Exit Sub
  233.                 End If
  234.             Next
  235.         Next
  236.  
  237.         cmd = New OleDbCommand("Select * from tbl_barang where Kode_Barang='" & DGV.Rows(Baris).Cells(0).Value & "'", conn)
  238.         dr = cmd.ExecuteReader
  239.         dr.Read()
  240.         If dr.HasRows Then
  241.             DGV.Rows(Baris).Cells(1).Value = dr.Item("Nama_Barang")
  242.             DGV.Rows(Baris).Cells(2).Value = dr.Item("Harga_Jual")
  243.             DGV.Rows(Baris).Cells(3).Value = 1
  244.             DGV.Rows(Baris).Cells(4).Value = DGV.Rows(Baris).Cells(2).Value * DGV.Rows(Baris).Cells(3).Value
  245.         End If
  246.     End Sub
  247.  
  248.     Private Sub DGVBarang_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DGVBarang.CellMouseClick
  249.         Dim Baris As Integer = DGV.RowCount - 1
  250.  
  251.         DGV.Rows.Add(DGVBarang.Rows(e.RowIndex).Cells(0).Value, DGVBarang.Rows(e.RowIndex).Cells(1).Value, DGVBarang.Rows(e.RowIndex).Cells(4).Value, 1)
  252.         DGV.Rows(Baris).Cells(4).Value = DGV.Rows(Baris).Cells(2).Value * DGV.Rows(Baris).Cells(3).Value
  253.  
  254.         For barisatas As Integer = 0 To DGV.RowCount - 1
  255.             For barisbawah As Integer = barisatas + 1 To DGV.RowCount - 1
  256.                 If DGV.Rows(barisbawah).Cells(0).Value = DGV.Rows(barisatas).Cells(0).Value Then
  257.                     DGV.Rows(barisatas).Cells(3).Value = DGV.Rows(barisatas).Cells(3).Value + 1
  258.                     DGV.Rows(barisatas).Cells(4).Value = DGV.Rows(barisatas).Cells(2).Value * DGV.Rows(barisatas).Cells(3).Value
  259.                     Call HitungBarang()
  260.                     Call HitungHarga()
  261.                     DGV.Rows.RemoveAt(barisbawah)
  262.                     Exit Sub
  263.                 End If
  264.             Next
  265.         Next
  266.     End Sub
  267. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement