
Imports GrapeCity.ActiveReports
Imports GrapeCity.ActiveReports.Document
Imports System.Globalization

Public Class rptKontrolleLKW
    Public datVon As Date
    Public datBis As Date
    Public brg As String
    Public FIRMA As String = "DURMAZ"
    'Public brg As String = "05DE0000000009345," 'default brg
    Public erstelltVon As String = ""
    Dim kundenSQL As New kundenSQL
    Dim SUB_DATA As DataTable = Nothing

    Private Sub PageFooter1_Format(sender As System.Object, e As System.EventArgs) Handles PageFooter.Format
        lblDetails.Text = String.Format("erstellt am {0} um {1} von {2}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(), erstelltVon)

    End Sub

    Private Sub rptAuswertung_NoData(sender As Object, e As System.EventArgs) Handles Me.NoData
        MsgBox("Es wurden keine Daten zum Drucken gefunden.", vbInformation, "Druck Aviso-Auswertung")
        Me.Cancel()  'ev. abbrechen, falls noch aufgebaut wird
    End Sub

    '''''''' ADD AT RUNTIME:
    'Dim d As New GrapeCity.ActiveReports.SectionReportModel.TextBox
    Private Sub rptAuswertung_ReportStart(sender As System.Object, e As System.EventArgs) Handles MyBase.ReportStart

        '''''''' ADD AT RUNTIME:
        'd.Location = New Point(2, 0)
        'd.Text = "asd"
        'Me.Detail.Controls.Add(d)


        Me.Name = "Auswertung offene Anträge mit Vorauszahlung"

        'lblHeader.Text = Me.Name

        'Format + Ränder setzen
        Me.PageSettings.Orientation = Section.PageOrientation.Portrait
        Me.PageSettings.Margins.Left = 0.3
        Me.PageSettings.Margins.Right = 0.3
        Me.PageSettings.Margins.Top = 0.5
        Me.PageSettings.Margins.Bottom = 0.4

        Dim brg_AT As String = "05AT510000G000FP7"
        Dim brg_DE As String = "05DE0000000009345"
        Dim brg_DE_NEU As String = "17DE0000000071678"



        Dim mandant = FIRMA
        If FIRMA = "DURMAZ" Then mandant = "SUW"
        If FIRMA = "ATILLA" Then mandant = "SUW"
        Dim sqlStr As String = " SELECT  count(*) as [Anzahl Verz], max(veoant_bfabkz) as [LKW-Kz], max(veoant_stat) as Status, max(veoant_beznr) as Bezugsnummer,max(veoant_lfdnr) as LfdNr,sum(veoant_anzpos)as Pos,max(veoant_arbnr)as ArbNr,max(veoant_mrn) as MRN,max(veoant_andat) as Antragsdatum,max(veoant_sb) as Sachbearbeiter,max(veoant_wgdat) as Gestellungsdatum,sum(veoerz_sicbtg) as [Sichertheits-Betrag] , max([veoant_korant]) as Korant " &
                           " FROM [atlas].[dbo].[vwVERAG_NCTS_DatenbereitstellungLKW] " &
                      " WHERE ( veoant_andat BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "') " &
                           " AND  veoant_stat IN (50,53,60) " &
                           " AND veoerz_sicbsc IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) " &
                           " AND  basman_nl='" & mandant & "' "
        If FIRMA = "DURMAZ" Then
            sqlStr &= " AND veoant_beznr LIKE 'DU%' "
        ElseIf FIRMA = "ATILLA" Then
            sqlStr &= " AND veoant_beznr NOT LIKE 'DU%' "
        End If

        sqlStr &= " GROUP BY veoant_bfabkz ,veoant_andat "
        sqlStr &= " ORDER BY Antragsdatum "

        Me.DataSource = kundenSQL.loadDgvBySql(sqlStr, "ATLAS", 1000)




        Dim sqlStrSUB As String = "  SELECT    max(ant.veoant_beznr) as BezugsNr, max( ant.[veoant_korant]) as Korant, max(veoant_andat) as Antragsdatum, max(veoant_bfabkz) as [LKW-Kz], max(ant.veoant_lfdnr) as LfdNr, max(ant.veoant_arbnr) as ArbNr, max(ant.veoant_mrn) as MRN, max(ant.veoant_anzpos) as Pos, min(ant.veoant_andat) as AnDat, max(ant.veoant_wgdat) as GestllungsDat, max(ant.veoant_sb) as Sachbearbeiter, sum(erz.veoerz_sicbtg) as Sicherheitsbetrag, max(erz.veoerz_sicbsc) as Brg " &
                            " FROM    dbo.veoant AS ant INNER JOIN " &
                                    " dbo.veopos AS pos ON ant.basman_nr = pos.basman_nr AND ant.basman_nl = pos.basman_nl AND ant.veoant_beznr = pos.veoant_beznr AND ant.veoant_lfdnr = pos.veoant_lfdnr AND  " &
                                    " ant.veoant_korant = pos.veoant_korant LEFT OUTER JOIN " &
                                    " dbo.veoerz AS erz ON pos.basman_nr = erz.basman_nr AND pos.basman_nl = erz.basman_nl AND pos.veoant_beznr = erz.veoant_beznr AND pos.veoant_lfdnr = erz.veoant_lfdnr AND  " &
                                    " pos.veopos_posnr = erz.veopos_posnr And pos.veoant_korant = erz.veoant_korant " &
                            " WHERE   ant.basman_nr ='VERA' " &
                                " AND ant.basman_nl ='" & mandant & "' " &
                                " AND ( veoant_andat BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "')  " &
                                " AND erz.veoerz_sicbsc IN ( '" & brg_AT & "' , '" & brg_DE & "'  , '" & brg_DE_NEU & "' ) " &
                                " and erz.veoerz_typ=2 " &
                                " AND  ant.veoant_stat IN (50,53,60) "
        If FIRMA = "DURMAZ" Then
            sqlStrSUB &= " AND ant.veoant_beznr LIKE 'DU%' "
        ElseIf FIRMA = "ATILLA" Then
            sqlStrSUB &= " AND ant.veoant_beznr NOT LIKE 'DU%' "
        End If
        sqlStrSUB &= " group by veoant_mrn"
        SUB_DATA = kundenSQL.loadDgvBySql(sqlStrSUB, "ATLAS", 1000)


        lblText1.Text = lblText1.Text & " - " & FIRMA
        Label6.Text = "Zeitraum: von " & datVon.ToShortDateString & " bis " & datBis.ToShortDateString
        Label7.Text = "" '"Bürgschaft: " & brg
    End Sub


    Dim dummycount As Integer = 0
    Dim anzLkw As Integer = 0
    Dim anzT As Integer = 0
    Dim summeSiBetr As Double = 0
    Dim nr As Integer = 1
    Private Sub Detail1_Format(sender As System.Object, e As System.EventArgs) Handles Detail.Format
        dummycount = dummycount + 1
        anzLkw += 1

        lblLKW.Text = CStr(Fields.Item("LKW-Kz").Value)
        lblNr.Text = anzLkw & "."
        'lblPos.Text = CStr(Fields.Item("Pos").Value)
        lblSiBetragValue.Text = "€ " & CDbl(Fields.Item("Sichertheits-Betrag").Value).ToString("N2", CultureInfo.InvariantCulture)

        'lblAnzTValue.Text = CStr(Fields.Item("Anzahl Verz").Value)
        ' lblArbNrValue.Text = CStr(Fields.Item("ArbNr").Value)
        'lblLKW.Text = CStr(Fields.Item("Bezugsnummer").Value)
        ''lblGestDatValue.Text = CStr(Fields.Item("Gestellungsdatum").Value)
        'lblMRNValue.Text = CStr(Fields.Item("MRN").Value)
        'lblSBValue.Text = CStr(Fields.Item("Sachbearbeiter").Value)
        'lblSiBetragValue.Text = "€ " & CDbl(Fields.Item("Sichertheits-Betrag").Value).ToString("N2", CultureInfo.InvariantCulture)
        'lblStatValue.Text = CStr(Fields.Item("Pos").Value)
        'anzLkw += 1
        'anzT += CInt(lblAnzTValue.Text)
        summeSiBetr += CDbl(Fields.Item("Sichertheits-Betrag").Value)


        Dim subrpt As New subrptKontrolleLKW
        subrpt.kennzeichen = Fields.Item("LKW-Kz").Value
        subrpt.veoant_lfdnr = Fields.Item("LfdNr").Value
        subrpt.veoant_korant = Fields.Item("Korant").Value
        subrpt.datVon = datVon
        subrpt.firma = FIRMA
        subrpt.datBis = datBis
        subrpt.veoant_andat = Fields.Item("Antragsdatum").Value
        'subrpt = datVon

        Dim dt As DataTable = SUB_DATA.Clone
        dt.Rows.Clear()
        'For Each c As DataColumn In dt.Columns
        'c.AllowDBNull = True
        'Next
        For Each r In SUB_DATA.Rows
            If r("LKW-Kz") = subrpt.kennzeichen And r("LfdNr") = subrpt.veoant_lfdnr And r("Korant") = subrpt.veoant_korant And r("Antragsdatum") = subrpt.veoant_andat Then
                ' dt.Rows.Add(r)
                dt.ImportRow(r)
            End If
        Next
        subrpt.DataSource = dt




        Me.SubReport.Report = subrpt

        AddHandler Me.SubReport.Report.ReportEnd, Sub()
                                                      anzT += DirectCast(Me.SubReport.Report, subrptKontrolleLKW).AnzT
                                                  End Sub

        'Abwechselnd grauen und weißen Hintergrund
        If (dummycount Mod 2) = 0 Then
            Detail.BackColor = System.Drawing.Color.White
        Else
            Detail.BackColor = System.Drawing.Color.WhiteSmoke
        End If

    End Sub






    Private Sub ReportFooter1_Format(sender As Object, e As EventArgs) Handles ReportFooter1.Format
        lblSummeSiBetr.Text = "€ " & summeSiBetr.ToString("N2", CultureInfo.InvariantCulture)
        lblAnzLKWs.Text = anzLkw
        lblAnzT.Text = anzT

    End Sub
End Class
