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

Public Class rptKontrolleLKW_Dakosy
    Public datVon As Date
    Public datBis As Date
    Public FIRMA As String = "ATILLA"
    Public BRG As String = ""
    Public NCTSSystemDY As String = "NCTS5"
    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-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 = "T-Kontrolle Dakosy "

        '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 firmaStr = ""
        If FIRMA = "DURMAZ" Then
            firmaStr = " AND (ncts_ObjectName LIKE 'DU%' OR ncts_firma='DURMAZ') "
        ElseIf FIRMA = "IMEX" Then
            firmaStr = " AND (ncts_ObjectName LIKE '402%' OR ncts_ObjectName LIKE '502%' OR ncts_ObjectName LIKE '5501%') "
        ElseIf FIRMA = "AMBAR" Then
            firmaStr = " AND (ncts_ObjectName LIKE 'A20%' OR ncts_ObjectName LIKE '5701%' ) "
        Else 'ATILLA
            'firmaStr = " AND ncts_ObjectName NOT LIKE 'DU%' "
            firmaStr = " AND (ncts_ObjectName LIKE '4801%' OR ncts_ObjectName LIKE '4802%' ) "
        End If

        '     If FIRMA = "ATILLA" Then operatorID = "'5','6'"

        Dim sqlStr As String = ""
        Dim sqlStrSUB As String = ""

        Select Case NCTSSystemDY
            Case "NCTS5"

                Dim whereBRG = " AND nctsGRT_GRN IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) "
                If BRG <> "" Then
                    whereBRG = " AND nctsGRT_GRN IN ( '" & BRG & "') "
                End If

                sqlStr = " SELECT    isnull(ncts_DepartureTransportMeans_1_IdentificationNumber,isnull(ncts_ActiveBorderTransportMeans_1_IdentificationNumber,'')) as Kennzeichen, cast([ncts_Trans_DatumZeit] as date) as AnmeldungsDatum,COUNT(*) ,isnull(SUM(nctsGRT_AmountToBeCovered),0) as GarantieWert  "
                sqlStr &= " FROM tblDakosy_NCTSOut004_Guarantee inner join [tblDakosy_NCTSOut004] on ncts_Id = nctsGRT_NctsId "
                sqlStr &= " WHERE cast ([ncts_Trans_DatumZeit] as date) BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "'"
                sqlStr &= whereBRG
                sqlStr &= " AND ncts_status BETWEEN 50 AND 60 "
                sqlStr &= firmaStr
                '  sqlStr &= " and vw.OperatorId IN (" & operatorID & ")"
                sqlStr &= " group by cast([ncts_Trans_DatumZeit] as date), isnull(ncts_DepartureTransportMeans_1_IdentificationNumber,isnull(ncts_ActiveBorderTransportMeans_1_IdentificationNumber,''))"
                sqlStr &= "  order by  cast([ncts_Trans_DatumZeit] as date)"



                sqlStrSUB = " SELECT   *, nctsGRT_AmountToBeCovered as nctsSA_AbgabenBetrag,nctsGRT_GRN as nctsSA_GRN, isnull(nctsGRT_AmountToBeCovered,0) as GarantieWert,isnull(ncts_DepartureTransportMeans_1_IdentificationNumber,isnull(ncts_ActiveBorderTransportMeans_1_IdentificationNumber,'')) as Kennzeichen,cast([ncts_Trans_DatumZeit] as date) as AnmeldungsDatum "
                sqlStrSUB &= " FROM tblDakosy_NCTSOut004_Guarantee inner join [tblDakosy_NCTSOut004] on ncts_Id = nctsGRT_NctsId "
                sqlStrSUB &= " WHERE cast([ncts_Trans_DatumZeit] as date) BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "'"
                sqlStrSUB &= whereBRG
                sqlStrSUB &= " AND ncts_status BETWEEN 50 AND 60 "
                sqlStrSUB &= firmaStr
                '  sqlStrSUB &= " and vw.OperatorId IN (" & operatorID & ")"

                sqlStrSUB &= "  order by  cast([ncts_Trans_DatumZeit] as date),ncts_ObjectName"
            Case Else

                Dim whereBRG = " AND nctsSA_GRN IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) "
                If BRG <> "" Then
                    whereBRG = " AND nctsSA_GRN IN ( '" & BRG & "') "
                End If

                sqlStr = " SELECT   isnull(ncts_Befoerderungsmittel_KennzeichenAbgang,'') as Kennzeichen, cast([ncts_Trans_DatumZeit] as date) as AnmeldungsDatum,COUNT(*) ,isnull(SUM(nctsSA_AbgabenBetrag),0) as GarantieWert  FROM tblDakosy_NCTSOut_Sicherheitsangaben inner join [VERAG].[dbo].[tblDakosy_NCTSOut] on ncts_Id = nctsSA_NctsId "
                sqlStr &= " WHERE cast ([ncts_Trans_DatumZeit] as date) BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "'"
                sqlStr &= whereBRG
                sqlStr &= " AND ncts_status BETWEEN 50 AND 60 "
                sqlStr &= firmaStr
                '  sqlStr &= " and vw.OperatorId IN (" & operatorID & ")"
                sqlStr &= " group by cast([ncts_Trans_DatumZeit] as date),ncts_Befoerderungsmittel_KennzeichenAbgang"
                sqlStr &= "  order by  cast([ncts_Trans_DatumZeit] as date)"


                sqlStrSUB = " SELECT   *, isnull(nctsSA_AbgabenBetrag,0) as GarantieWert,isnull(ncts_Befoerderungsmittel_KennzeichenAbgang,'') as Kennzeichen,cast([ncts_Trans_DatumZeit] as date) as AnmeldungsDatum  FROM  tblDakosy_NCTSOut_Sicherheitsangaben inner join [VERAG].[dbo].[tblDakosy_NCTSOut] on ncts_Id = nctsSA_NctsId  "
                sqlStrSUB &= " WHERE cast([ncts_Trans_DatumZeit] as date) BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "'"
                sqlStrSUB &= whereBRG
                sqlStrSUB &= " AND ncts_status BETWEEN 50 AND 60 "
                sqlStrSUB &= firmaStr
                '  sqlStrSUB &= " and vw.OperatorId IN (" & operatorID & ")"

                sqlStrSUB &= "  order by  cast([ncts_Trans_DatumZeit] as date),ncts_ObjectName"

        End Select

        ' MsgBox(sqlStr)

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

        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
        summeSiBetr += CDbl(Fields.Item("GarantieWert").Value)
        lblLKW.Text = CStr(Fields.Item("Kennzeichen").Value)
        lblNr.Text = anzLkw & "."
        lblSiBetragValue.Text = "€ " & CDbl(Fields.Item("GarantieWert").Value).ToString("N2", CultureInfo.InvariantCulture)


        If SUB_DATA IsNot Nothing Then

            Dim subrpt As New subrptKontrolleLKW_Dakosy
            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_Dakosy).AnzT

                                                      End Sub

        End If
        '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
