Public Class Form2
    Dim db_informatique As New class1
    Dim db_informatique2 As New class2
    Dim deltp, deltc As Integer
    Dim prig1, prig As Double


    Private Sub TabPage5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Form1.Show()
        Me.Hide()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Form3.Show()
        Me.Hide()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Form4.Show()
        Me.Hide()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Form5.Show()
        Me.Hide()
    End Sub

    Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Form1.Show()
        Me.Hide()
    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form3.Show()
        Me.Hide()
    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form4.Show()
        Me.Hide()
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form5.Show()
        Me.Hide()
    End Sub

    Private Sub TableLayoutPanel2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TableLayoutPanel2.Paint

    End Sub

    Private Sub d1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles d1.ValueChanged
        If d1.Value > d2.Value Then
            d1.Text = Today

        End If
    End Sub

    Private Sub d2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles d2.ValueChanged
        If d2.Value < d1.Value Then
            d2.Text = d1.Value

        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        ListView2.Items.Clear()

        db_informatique.Myreq = "select*from listefacture where datf >='" & Me.d1.Text & "' and datf<='" & Me.d2.Text & "'"
        db_informatique.Connecting_in_informatique()
        ListView1.Items.Clear()
        Dim sommlist As Double = 0
        Do While db_informatique.rdr.Read
            ListView1.Items.Add(New ListViewItem({(db_informatique.rdr(0).ToString), (db_informatique.rdr(1).ToString), (db_informatique.rdr(2).ToString)}))
            sommlist = sommlist + db_informatique.rdr(2).ToString
        Loop
        db_informatique.Disconnecting_in_informatique()
        TextBox1.Text = sommlist

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        ListView2.Items.Clear()

        db_informatique.Myreq = "select*from listefacture where datf >='" & Me.d1.Text & "' and datf<='" & Me.d2.Text & "' and numf='" & Me.t9.Text & "'"
        db_informatique.Connecting_in_informatique()
        ListView1.Items.Clear()
        Dim sommlist As Double = 0
        Do While db_informatique.rdr.Read
            ListView1.Items.Add(New ListViewItem({(db_informatique.rdr(0).ToString), (db_informatique.rdr(1).ToString), (db_informatique.rdr(2).ToString)}))
            sommlist = sommlist + db_informatique.rdr(2).ToString

        Loop
        db_informatique.Disconnecting_in_informatique()
        TextBox1.Text = sommlist
    End Sub

    Private Sub t9_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles t9.KeyPress
        If (Not Char.IsControl(e.KeyChar) And Not Char.IsDigit(e.KeyChar)) Then
            t9.BackColor = Color.Red
            e.Handled = True
        Else
            t9.BackColor = Color.White
        End If
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        If ListView1.SelectedItems.Count > 0 Then
            db_informatique.Myreq = "select*from factureachat where numfac ='" & ListView1.SelectedItems(0).Text & "' and datf<='" & ListView1.SelectedItems(0).SubItems(1).Text & "'"
            db_informatique.Connecting_in_informatique()
            ListView2.Items.Clear()

            Do While db_informatique.rdr.Read
                ListView2.Items.Add(New ListViewItem({(db_informatique.rdr(0).ToString), (db_informatique.rdr(1).ToString), (db_informatique.rdr(2).ToString), (db_informatique.rdr(3).ToString), (db_informatique.rdr(4).ToString), (db_informatique.rdr(5).ToString), (db_informatique.rdr(6).ToString), (db_informatique.rdr(7).ToString), (db_informatique.rdr(11).ToString)}))

            Loop
            db_informatique.Disconnecting_in_informatique()
        End If

    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If ListView1.SelectedItems.Count > 0 Then
            If MessageBox.Show(" هل تريد حذف الفواتير المحددة نهائيا؟  ", "حذف المحدد", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes Then

                Do While ListView1.SelectedItems.Count > 0
                    db_informatique.Myreq = "Delete From listefacture where numf='" & ListView1.SelectedItems(0).Text & "' and datf='" & ListView1.SelectedItems(0).SubItems(1).Text & "'"
                    db_informatique.Connecting_in_informatique()
                    db_informatique.Disconnecting_in_informatique()

                    Do While ListView2.Items.Count > 0
                        db_informatique.Myreq = "select*from factureachat  where codep='" & ListView2.Items(0).Text & "' and numfac = '" & Me.ListView1.SelectedItems(0).Text & "' and datf='" & Me.ListView1.SelectedItems(0).SubItems(1).Text & "'"
                        db_informatique.Connecting_in_informatique()
                        Do While db_informatique.rdr.Read
                            prig1 = (db_informatique.rdr(12).ToString)
                        Loop
                        db_informatique.Disconnecting_in_informatique()
                        db_informatique.Myreq = "Delete From factureachat where codep='" & ListView2.Items(0).Text & "' and numfac = '" & Me.ListView1.SelectedItems(0).Text & "' and datf='" & Me.ListView1.SelectedItems(0).SubItems(1).Text & "'"
                        db_informatique.Connecting_in_informatique()
                        db_informatique.Disconnecting_in_informatique()
                        'من هنا
                        db_informatique2.Myreq = "select*from factureachat  where codep='" & ListView2.Items(0).Text & "'"
                        db_informatique2.Connecting_in_informatique()
                        Do While db_informatique2.rdr.Read
                            deltp = (db_informatique2.rdr(4).ToString)
                            deltc = (db_informatique2.rdr(2).ToString)
                            prig = (db_informatique2.rdr(12).ToString)
                        Loop
                        db_informatique2.Disconnecting_in_informatique()



                        db_informatique2.Myreq = "update factureachat set nbregpiece='" & deltp - ListView2.Items(0).SubItems(4).Text & "',nbrecarton='" & deltc - ListView2.Items(0).SubItems(2).Text & "',prixg='" & prig - prig1 & "' where codep='" & ListView2.Items(0).Text & "'"
                        db_informatique2.Connecting_in_informatique()
                        db_informatique2.Disconnecting_in_informatique()

                        Dim element2 As New ListViewItem
                        element2 = ListView2.Items(0)
                        ListView2.Items.Remove(element2)
                    Loop
                    Dim element3 As New ListViewItem
                    element3 = ListView1.SelectedItems(0)
                    ListView1.Items.Remove(element3)
                Loop
            End If
        End If



    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        If ListView2.SelectedItems.Count > 0 Then
            If MessageBox.Show(" هل تريد حذف الصنف   ", "حذف الصنف المحدد", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes Then

                Do While ListView2.SelectedItems.Count > 0
                    db_informatique.Myreq = "select*from factureachat  where codep='" & ListView2.SelectedItems(0).Text & "' and numfac = '" & Me.ListView1.SelectedItems(0).Text & "' and datf='" & Me.ListView1.SelectedItems(0).SubItems(1).Text & "'"
                    db_informatique.Connecting_in_informatique()
                    Do While db_informatique.rdr.Read
                        prig1 = (db_informatique.rdr(12).ToString)
                    Loop
                    db_informatique.Disconnecting_in_informatique()
                    db_informatique.Myreq = "Delete From factureachat where codep='" & ListView2.SelectedItems(0).Text & "' and numfac = '" & Me.ListView1.SelectedItems(0).Text & "' and datf='" & Me.ListView1.SelectedItems(0).SubItems(1).Text & "'"
                    db_informatique.Connecting_in_informatique()
                    db_informatique.Disconnecting_in_informatique()
                    'من هنا
                    db_informatique2.Myreq = "select*from factureachat  where codep='" & ListView2.SelectedItems(0).Text & "'"
                    db_informatique2.Connecting_in_informatique()
                    Do While db_informatique2.rdr.Read
                        deltp = (db_informatique2.rdr(4).ToString)
                        deltc = (db_informatique2.rdr(2).ToString)
                        prig = (db_informatique2.rdr(12).ToString)
                    Loop
                    db_informatique2.Disconnecting_in_informatique()



                    db_informatique2.Myreq = "update factureachat set nbregpiece='" & deltp - ListView2.SelectedItems(0).SubItems(4).Text & "',nbrecarton='" & deltc - ListView2.SelectedItems(0).SubItems(2).Text & "',prixg='" & prig - prig1 & "' where codep='" & ListView2.SelectedItems(0).Text & "'"
                    db_informatique2.Connecting_in_informatique()
                    db_informatique2.Disconnecting_in_informatique()

                    Dim element As New ListViewItem
                    element = ListView2.SelectedItems(0)
                    ListView2.Items.Remove(element)
                    Dim some = 0
                    For i = 0 To ListView2.Items.Count - 1 Step 1
                        some = some + (ListView2.Items(i).SubItems(2).Text * ListView2.Items(i).SubItems(5).Text)
                    Next

                    db_informatique.Myreq = "update listefacture set somm='" & some & "' where numf='" & ListView1.SelectedItems(0).Text & "' and datf='" & ListView1.SelectedItems(0).SubItems(1).Text & "'"
                    db_informatique.Connecting_in_informatique()
                    db_informatique.Disconnecting_in_informatique()
                    ListView1.SelectedItems(0).SubItems(2).Text = some

                Loop

            End If
        End If


    End Sub
    Private Sub affichelistview()
        db_informatique.Myreq = "select*from factureachat where numfac ='" & ListView1.SelectedItems(0).Text & "' and datf<='" & ListView1.SelectedItems(0).SubItems(1).Text & "'"
        db_informatique.Connecting_in_informatique()
        ListView2.Items.Clear()

        Do While db_informatique.rdr.Read
            ListView2.Items.Add(New ListViewItem({(db_informatique.rdr(0).ToString), (db_informatique.rdr(1).ToString), (db_informatique.rdr(2).ToString), (db_informatique.rdr(3).ToString), (db_informatique.rdr(4).ToString), (db_informatique.rdr(6).ToString), (db_informatique.rdr(7).ToString), (db_informatique.rdr(11).ToString)}))

        Loop
        db_informatique.Disconnecting_in_informatique()
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        NotifyIcon1.Visible = True
        NotifyIcon1.ShowBalloonTip(5)
    End Sub

    Private Sub Form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Form1.Close()
    End Sub

    Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged

    End Sub
End Class
