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

Public Class rptKontrolleLKW_TELOTEC
    Public datVon As Date
    Public datBis As Date
    Public FIRMA As String = "ATILLA"
    Public BRG As String = ""
    Dim operatorID = "1"
    Dim kundenSQL As New kundenSQL
    Dim SUB_DATA As DataTable = Nothing

    'Public brg As String = "05DE0000000009345," 'default brg
    Public erstelltVon As String = ""
    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-BRG")
        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 whereBRG = " AND GRN IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) "
        If BRG <> "" Then
            whereBRG = " AND GRN IN ( '" & BRG & "') "
        End If


        Dim StatVon As String = "50"
        Dim StatBis As String = "60"


        'If FIRMA = "DURMAZ" Then mandant = "SUW" 

        Dim sqlStr As String = "SELECT isnull([Transp_CrossIdnt],'X') as Kennzeichen,cast(Hea_AccDT as date) AnmeldungsDatum ,COUNT(*) ,SUM(GVal) as GarantieWert
                                FROM  tblTelotec_Sicherheit inner join [tblTelotec_Anmeldung] on telanm_id = telgrt_telanmId
                                WHERE [tblTelotec_Anmeldung].[Hea_AccDT] BETWEEN '" & datVon.ToShortDateString & " 00:00:00' AND '" & datBis.ToShortDateString & " 23:23:59' AND telanm_Status BETWEEN '" & StatVon & "' and '" & StatBis & "' AND [telnam_aktuellsteNachricht]=1  
                               " & whereBRG & "
                                AND tblTelotec_Anmeldung. [Mandant_ID]='" & FIRMA & "'
                                group by cast(Hea_AccDT as date),[Transp_CrossIdnt]
					      	    ORDER BY cast(Hea_AccDT as date)"
        'Dim sqlStr As String = "SELECT [Transp_CrossIdnt] as Kennzeichen,cast(dec_CreateDate as date) AnmeldungsDatum ,COUNT(*) ,SUM(GVal) as GarantieWert
        '                        FROM  tblTelotec_Sicherheit inner join [tblTelotec_Anmeldung] on telanm_id = telgrt_telanmId
        '                        WHERE [tblTelotec_Anmeldung].[CtrlRslt_DLimit] BETWEEN '" & datVon.ToShortDateString & " 00:00:00' AND '" & datBis.ToShortDateString & " 23:23:59' AND telanm_Status BETWEEN '" & StatVon & "' and '" & StatBis & "' AND [telnam_aktuellsteNachricht]=1  
        '                        AND GRN IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) 
        '                        AND tblTelotec_Anmeldung. [Mandant_ID]='" & FIRMA & "'
        '                        group by cast(dec_CreateDate as date),[Transp_CrossIdnt]
        '   	    ORDER BY cast(dec_CreateDate as date)"

        'Dim sqlStr As String = " SELECT   DepIdnt, vw.AnmeldungsDatum,COUNT(*) ,SUM(GarantieWert) as GarantieWert  FROM ezoll.dbo.vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme as vw"
        'sqlStr &= " inner join   [ezoll].[dbo].[zsAnmTransp]  on [zsAnmTransp].AnmID=vw.AnmID  and vw.OperatorID=[zsAnmTransp].OperatorID"
        'sqlStr &= " WHERE cast (vw.[AnmeldungsDatum] as date) BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "'"
        'sqlStr &= " AND GRN IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) "
        'sqlStr &= " and vw.OperatorId IN (" & operatorID & ")"
        'sqlStr &= " group by vw.AnmeldungsDatum,Kennzeichen"
        'sqlStr &= "  order by  vw.AnmeldungsDatum"

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


        Dim sqlStrSUB As String = "  SELECT isnull([Transp_CrossIdnt],'X') as Kennzeichen,cast(Hea_AccDT as date) AnmeldungsDatum, * 
                                FROM  tblTelotec_Sicherheit inner join [tblTelotec_Anmeldung] on telanm_id = telgrt_telanmId
                                WHERE [tblTelotec_Anmeldung].[Hea_AccDT] BETWEEN '" & datVon.ToShortDateString & " 00:00:00' AND '" & datBis.ToShortDateString & " 23:23:59' AND telanm_Status BETWEEN '" & StatVon & "' and '" & StatBis & "' AND [telnam_aktuellsteNachricht]=1  
                                 " & whereBRG & "
                                AND tblTelotec_Anmeldung. [Mandant_ID]='" & FIRMA & "'
                                ORDER BY cast(Hea_AccDT as date)"

        'Dim sqlStrSUB As String = "  SELECT [Transp_CrossIdnt] as Kennzeichen,cast(dec_CreateDate as date) AnmeldungsDatum, * 
        '                        FROM  tblTelotec_Sicherheit inner join [tblTelotec_Anmeldung] on telanm_id = telgrt_telanmId
        '                        WHERE [tblTelotec_Anmeldung].[CtrlRslt_DLimit] BETWEEN '" & datVon.ToShortDateString & " 00:00:00' AND '" & datBis.ToShortDateString & " 23:23:59' AND telanm_Status BETWEEN '" & StatVon & "' and '" & StatBis & "' AND [telnam_aktuellsteNachricht]=1  
        '                        AND GRN IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) 
        '                        AND tblTelotec_Anmeldung. [Mandant_ID]='" & FIRMA & "'
        '                        ORDER BY cast(dec_CreateDate as date)"

        'Dim sqlStrSUB As String = " SELECT   *  FROM ezoll.dbo.vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme as vw"
        'sqlStrSUB &= " inner join   [zsAnmTransp]  on [zsAnmTransp].AnmID=vw.AnmID  and vw.OperatorID=[zsAnmTransp].OperatorID"
        'sqlStrSUB &= " inner join   [zzPersonal]  on PersonalID  =[zsAnmTransp].GeaendertVon"
        'sqlStrSUB &= " WHERE cast (vw.[AnmeldungsDatum] as date) BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "'"
        'sqlStrSUB &= " AND GRN IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) "
        'sqlStrSUB &= " and vw.OperatorId IN (" & operatorID & ")"

        'sqlStrSUB &= "  order by  vw.AnmeldungsDatum"

        SUB_DATA = kundenSQL.loadDgvBySql(sqlStrSUB, "FMZOLL", 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
        Dim GarantieWert As Double = If(Fields.Item("GarantieWert").Value Is DBNull.Value, 0, CDbl(Fields.Item("GarantieWert").Value))
        summeSiBetr += GarantieWert
        lblLKW.Text = CStr(Fields.Item("Kennzeichen").Value)
        lblNr.Text = anzLkw & "."
        lblSiBetragValue.Text = "€ " & GarantieWert.ToString("N2", CultureInfo.InvariantCulture)


        If SUB_DATA IsNot Nothing Then

            Dim subrpt As New subrptKontrolleLKW_Telotec
            subrpt.Kennzeichen = Fields.Item("Kennzeichen").Value
            subrpt.AnmeldungsDatum = Fields.Item("AnmeldungsDatum").Value


            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("AnmeldungsDatum") = subrpt.AnmeldungsDatum And r("Kennzeichen") = subrpt.Kennzeichen 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_Telotec).AnzT

                                                      End Sub

        End If
        'Abwechselnd grauen und weißen Hintergrund
        If (dummycount Mod 2) = 0 Then
            Detail.BackColor = Color.White
        Else
            Detail.BackColor = 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
