Public Class frmUSTVoffeneAntraege

    Public LIEFERANT = ""
    Dim datumBis As Date = Nothing
    Dim datumVon As Date = Nothing
    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        initLieferant("PLOSE", sender)
    End Sub

    Sub New(datumVon_ As Date, datumBis_ As Date)
        datumVon = datumVon_
        datumBis = datumBis_

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

        dat_Sum_Von.Value = datumVon
        dat_Sum_Bis.Value = datumBis

    End Sub

    Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Private Sub initLieferant(l As String, btn As Button)



        MyDatagridview1.DataSource = Nothing
        Me.LIEFERANT = l

        For Each c As Button In FlowLayoutPanel.Controls
            If c Is btn Then
                c.BackColor = Color.Wheat
            Else
                c.BackColor = Color.FloralWhite
            End If
        Next

        Select Case Me.LIEFERANT
            Case "PLOSE"
                'Kundenauswertungen
            Case "RMC"
                'Kundenauswertungen
        End Select

        init()


    End Sub

    Sub initDgv_PLOSE(anz As Integer)
        With MyDatagridview1
            .Columns.Clear()


            Dim top = ""
            If anz > 0 Then top = "TOP (" & anz & ")"

            .SET_SQL("select " & top & " plose_POLSEKundennummer, Adressen.[Name 1] ,[plose_Lieferant],[plose_RechnungsDatum],[plose_RechnungsNr], sum([plose_MWSTBetrag])[plose_MWSTBetrag] FROM [tblPLOSE_Details]  INNER JOIN Adressen on PLOSEKundenNr=plose_POLSEKundennummer
                    WHERE plose_Archiv <> 1 AND plose_LieferantCode IN (0,1,7) AND cast(plose_RechnungsDatum as date) between '" & dat_Sum_Von.Value & "' and '" & dat_Sum_Bis.Value & "' and plose_RechnungsNr is not null
					group by  plose_POLSEKundennummer, Adressen.[Name 1] ,[plose_RechnungsDatum],[plose_RechnungsNr],[plose_Lieferant]
                    having sum(plose_MWSTBetrag)>0", "FMZOLL")
            .LOAD()
            If .Columns.Count > 0 Then

                .Columns("plose_POLSEKundennummer").HeaderText = "KdNr"
                .Columns("Name 1").HeaderText = "Kunde"
                .Columns("plose_Lieferant").HeaderText = "PLOSE-Lieferant"
                .Columns("plose_RechnungsDatum").HeaderText = "RE-Datum"
                .Columns("plose_RechnungsNr").HeaderText = "RE-Nr"
                .Columns("Name 1").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("plose_MWSTBetrag").HeaderText = "MWST"

                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

            End If

            Dim sumMwSt As Double = 0
            For Each r As DataGridViewRow In .Rows
                sumMwSt += If(IsNumeric(r.Cells("plose_MWSTBetrag").Value), CDbl(r.Cells("plose_MWSTBetrag").Value), 0)
            Next

            txtAnzahl.Text = .Rows.Count
            txtMWST.Text = sumMwSt

            .ClearSelection()

        End With

    End Sub

    Sub initDgv_RMC(anz As Integer)

        Dim top = ""
        If anz > 0 Then top = "TOP (" & anz & ")"

        With MyDatagridview1
            .Columns.Clear()
            'Exit Sub
            'MsgBox(.sql)
            Dim sqlstring = "Select " & top & " [rmc_kdNr], [rmc_kdName], [rmc_landKZ], [rmc_reDatum], [rmc_reNr], [rmc_waehrung], [rmc_betragMWST],rmc_daid  FROM [tblRMCImport] 
                        where cast(rmc_reDatum As Date) between '" & dat_Sum_Von.Value & "' And '" & dat_Sum_Bis.Value & "' And rmc_archiv <> 1 
                        group by  rmc_reDatum, rmc_kdNr,[rmc_kdName], [rmc_landKZ],[rmc_reNr], [rmc_waehrung], [rmc_betragMWST], rmc_daid  having [rmc_betragMWST] > 0"

            .SET_SQL(sqlstring, "FMZOLL")
            .LOAD()
            If .Columns.Count > 0 Then

                .Columns("rmc_kdNr").HeaderText = "KdNr"
                .Columns("rmc_kdName").HeaderText = "Kunde"
                .Columns("rmc_landKZ").HeaderText = "Land"
                .Columns("rmc_reDatum").HeaderText = "RE-Datum"
                .Columns("rmc_reNr").HeaderText = "RE-Nr"
                .Columns("rmc_betragMWST").HeaderText = "MWSt"
                .Columns("rmc_waehrung").HeaderText = "Währung"
                .Columns("rmc_daid").Visible = False
                .Columns("rmc_kdName").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

            End If

            Dim sumMwSt As Double = 0

            Dim c As New DataGridViewImageColumn
            c.Name = "clmnPDF" : c.HeaderText = "RE"
            '    c.Image = Nothing
            c.DefaultCellStyle.NullValue = Nothing
            c.ImageLayout = DataGridViewImageCellLayout.Zoom
            c.Width = 40 : c.DefaultCellStyle.Padding = New Padding(2) : c.DefaultCellStyle.BackColor = Color.White
            'c.DefaultCellStyle.Tag = "Therefore anzeigen"

            .Columns.Add(c)

            For Each r As DataGridViewRow In .Rows
                sumMwSt += If(IsNumeric(r.Cells("rmc_betragMWST").Value), CDbl(r.Cells("rmc_betragMWST").Value), 0)

                If Not IsDBNull(r.Cells("rmc_daid").Value) AndAlso r.Cells("rmc_daid").Value > 0 Then DirectCast(r.Cells("clmnPDF"), DataGridViewImageCell).Value = My.Resources.pdf
            Next

            txtAnzahl.Text = .Rows.Count
            txtMWST.Text = sumMwSt


        End With

    End Sub

    Sub initDgv_MSE(anz As Integer)

        Dim top = ""
        If anz > 0 Then top = "TOP (" & anz & ")"

        With MyDatagridview1
            .Columns.Clear()
            'Exit Sub
            'MsgBox(.sql)
            Dim sqlstring = "Select " & top & " customer_number, customer_name, country, invoice_date, invoice_id, sum(total_amount_euro) as total_amount_euro, sum(total_vat_amount_euro) as total_vat_amount_euro, daId  FROM [VERAG].[dbo].[tblMSEInvoices] inner join [tblMSECustomers] on [customer_number] = [customer_id]  
                        where cast(invoice_date As Date) between '" & dat_Sum_Von.Value & "' And '" & dat_Sum_Bis.Value & "' And imported <> 1 
                        group by  invoice_date, customer_number,customer_name, country, invoice_id, daId "

            .SET_SQL(sqlstring, "FMZOLL")
            .LOAD()
            If .Columns.Count > 0 Then

                .Columns("customer_number").HeaderText = "KdNr"
                .Columns("customer_name").HeaderText = "Kunde"
                .Columns("country").HeaderText = "Land"
                .Columns("invoice_date").HeaderText = "RE-Datum"
                .Columns("invoice_id").HeaderText = "RE-Nr"
                .Columns("total_amount_euro").HeaderText = "Gesamtsumme Euro"
                .Columns("total_vat_amount_euro").HeaderText = "MWSt Euro"
                .Columns("daId").Visible = False
                .Columns("customer_name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

            End If

            Dim sumMwSt As Double = 0

            Dim c As New DataGridViewImageColumn
            c.Name = "clmnPDF" : c.HeaderText = "RE"
            '    c.Image = Nothing
            c.DefaultCellStyle.NullValue = Nothing
            c.ImageLayout = DataGridViewImageCellLayout.Zoom
            c.Width = 40 : c.DefaultCellStyle.Padding = New Padding(2) : c.DefaultCellStyle.BackColor = Color.White
            'c.DefaultCellStyle.Tag = "Therefore anzeigen"

            .Columns.Add(c)

            For Each r As DataGridViewRow In .Rows
                sumMwSt += If(IsNumeric(r.Cells("total_vat_amount_euro").Value), CDbl(r.Cells("total_vat_amount_euro").Value), 0)

                If Not IsDBNull(r.Cells("daId").Value) AndAlso r.Cells("daId").Value > 0 Then DirectCast(r.Cells("clmnPDF"), DataGridViewImageCell).Value = My.Resources.pdf

            Next

            txtAnzahl.Text = .Rows.Count
            txtMWST.Text = sumMwSt




        End With

    End Sub

    Sub initDgv_UTA(anz As Integer)

        Dim top = ""
        If anz > 0 Then top = "TOP (" & anz & ")"

        With MyDatagridview1
            .Columns.Clear()
            'Exit Sub
            'MsgBox(.sql)
            Dim sqlstring = "Select " & top & " [Kundennummer], [Lieferland], [Rechnungsdatum], [Abrechnungsnummer], sum([Gesamtbetrag_Brutto_in_Darstellwährung]) as [Gesamtbetrag_Brutto_in_Darstellwährung], sum([Gesamtbetrag_Umsatzsteuer_in_Darstellwährung]) as [Gesamtbetrag_Umsatzsteuer_in_Darstellwährung], da_id  FROM [tblUTAImport]  
                        where cast([Rechnungsdatum] As Date) between '" & dat_Sum_Von.Value & "' And '" & dat_Sum_Bis.Value & "' And [archiv] <> 1 
                        group by  [Rechnungsdatum], Kundennummer, [Lieferland], [Abrechnungsnummer], da_id "

            .SET_SQL(sqlstring, "FMZOLL")
            .LOAD()
            If .Columns.Count > 0 Then

                .Columns("Kundennummer").HeaderText = "KdNr"
                .Columns("Lieferland").HeaderText = "Land"
                .Columns("Rechnungsdatum").HeaderText = "RE-Datum"
                .Columns("Abrechnungsnummer").HeaderText = "RE-Nr"
                .Columns("Gesamtbetrag_Brutto_in_Darstellwährung").HeaderText = "Gesamtsumme Euro"
                .Columns("Gesamtbetrag_Umsatzsteuer_in_Darstellwährung").HeaderText = "MWSt Euro"
                .Columns("da_id").Visible = False
                .Columns("Kundennummer").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

            End If

            Dim sumMwSt As Double = 0

            Dim c As New DataGridViewImageColumn
            c.Name = "clmnPDF" : c.HeaderText = "RE"
            '    c.Image = Nothing
            c.DefaultCellStyle.NullValue = Nothing
            c.ImageLayout = DataGridViewImageCellLayout.Zoom
            c.Width = 40 : c.DefaultCellStyle.Padding = New Padding(2) : c.DefaultCellStyle.BackColor = Color.White
            'c.DefaultCellStyle.Tag = "Therefore anzeigen"

            .Columns.Add(c)


            For Each r As DataGridViewRow In .Rows
                sumMwSt += If(IsNumeric(r.Cells("Gesamtbetrag_Umsatzsteuer_in_Darstellwährung").Value), CDbl(r.Cells("Gesamtbetrag_Umsatzsteuer_in_Darstellwährung").Value), 0)
                If Not IsDBNull(r.Cells("da_id").Value) AndAlso r.Cells("da_id").Value > 0 Then DirectCast(r.Cells("clmnPDF"), DataGridViewImageCell).Value = My.Resources.pdf
            Next

            txtAnzahl.Text = .Rows.Count
            txtMWST.Text = sumMwSt


        End With

    End Sub

    Sub init()
        Select Case LIEFERANT
            Case "PLOSE" : initDgv_PLOSE(txtmaxAnz.Text)
            Case "RMC" : initDgv_RMC(txtmaxAnz.Text)
            Case "MSE" : initDgv_MSE(txtmaxAnz.Text)
            Case "UTA" : initDgv_UTA(txtmaxAnz.Text)
        End Select
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        initLieferant("RMC", sender)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        initLieferant("UTA", sender)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        initLieferant("MSE", sender)
    End Sub
End Class