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

Public Class rptKontrolleLKW_NCTSTR
    Public datVon As Date
    Public datBis As Date
    Public FIRMA As String = "ATILLA"
    Dim operatorID = "1"
    Dim kundenSQL As New kundenSQL
    Dim SUB_DATA As DataTable = Nothing
    Dim LIST_NCTSABR As VERAG_PROG_ALLGEMEIN.cAbrechnung_NCTSBrg_LIST = Nothing
    Public GRN As String = "" '"05DE0000000009345," 'default brg
    Public erstelltVon As String = ""
    Dim groupKz = True 'Es kann nach Kennzeichen groupiert werden

    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 brg_UNISPED As String = "17DE0000000071270"
        'Dim brg_DE_IMEX As String = 
        '   Dim brg_DE_ATILLA As String = "20AT520000G000092"

        Dim firmaStr = ""
        If FIRMA = "VERIMEX_MAVI" Then
            firmaStr = " AND [ncts_Partnersystem] IN('MAVI') "
            LIST_NCTSABR = New VERAG_PROG_ALLGEMEIN.cAbrechnung_NCTSBrg_LIST("VERIMEX_MAVI")
            Label10.Text = " Auswertung MAVI"
        ElseIf FIRMA = "VERIMEX_OREGON" Then
            firmaStr = " AND [ncts_Partnersystem] IN('OREGON') "
            LIST_NCTSABR = New VERAG_PROG_ALLGEMEIN.cAbrechnung_NCTSBrg_LIST("VERIMEX_OREGON")
            Label10.Text = " Auswertung OREGON"
        ElseIf FIRMA = "VERIMEX_ATEZ" Then
            firmaStr = " AND [ncts_Partnersystem] IN('ATEZ') "
            LIST_NCTSABR = New VERAG_PROG_ALLGEMEIN.cAbrechnung_NCTSBrg_LIST("VERIMEX_ATEZ")
            Label10.Text = " Auswertung ATEZ"
        ElseIf FIRMA = "ATEZ" Then
            firmaStr = " AND [ncts_Partnersystem] IN('ATEZ') "
            Label10.Text = " Auswertung ATEZ"
        ElseIf FIRMA = "MIC_UNISPED" Then
            firmaStr = " AND [ncts_Partnersystem] IN('MIC') AND ncts_firma='UNISPED' "
            LIST_NCTSABR = New VERAG_PROG_ALLGEMEIN.cAbrechnung_NCTSBrg_LIST("MIC_UNISPED")
            Label10.Text = " Auswertung UNISPED"
        ElseIf FIRMA = "MIC_AWOR" Then
            firmaStr = " AND [ncts_Partnersystem] IN('MIC') AND ncts_firma='AWOR' "
            LIST_NCTSABR = New VERAG_PROG_ALLGEMEIN.cAbrechnung_NCTSBrg_LIST("MIC_AWOR")
            Label10.Text = " Auswertung AWOR"
            groupKz = False
        End If

        If True Then '"OREGON"
        End If

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

        Dim sqlStr As String = " SELECT  " & If(Not groupKz, " ncts_id, ", "") & " isnull(ncts_Befoerderungsmittel_KennzeichenAbgang,'') as Kennzeichen, cast(ncts_Eroeffnung as date) as AnmeldungsDatum,COUNT(*) ,isnull(SUM(nctsSA_AbgabenBetrag),0) as GarantieWert,isnull(SUM(ncts_VERAG_WeiterverrechungSatz),0) ncts_VERAG_WeiterverrechungSatz  FROM tblNCTS_TR_Sicherheitsangaben inner join tblNCTS_TR on ncts_Id = nctsSA_NctsId "
        sqlStr &= " WHERE cast (ncts_Eroeffnung as date) BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "'"
        If GRN <> "" Then
            sqlStr &= " AND nctsSA_GRN IN ( '" & GRN & "'  ) "
        Else
            sqlStr &= " AND nctsSA_GRN IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) "
        End If
        sqlStr &= " AND ncts_status BETWEEN 50 AND 60 "
        'sqlStr &= " AND ncts_status BETWEEN 50 AND 80 "
        sqlStr &= firmaStr
        '  sqlStr &= " and vw.OperatorId IN (" & operatorID & ")"
        sqlStr &= " group by cast(ncts_Eroeffnung as date),ncts_Befoerderungsmittel_KennzeichenAbgang" & If(Not groupKz, ", ncts_id ", "") '--> wenn nciht nach Kennezcihen groupioert werden kann, einfach die ID hinzufügen--> nur mehr einzelene Zeilen möglich
        sqlStr &= "  order by  cast(ncts_Eroeffnung as date)"
        ' MsgBox(sqlStr)
        Me.DataSource = kundenSQL.loadDgvBySql(sqlStr, "FMZOLL", 1000)


        Dim sqlStrSUB As String = " SELECT   *, isnull(nctsSA_AbgabenBetrag,0) as GarantieWert,isnull(ncts_Befoerderungsmittel_KennzeichenAbgang,'') as Kennzeichen,cast(ncts_Eroeffnung as date) as AnmeldungsDatum,cast(ncts_Ankunft as date) as ncts_Ankunft,cast(ncts_Erledigung as date) as ncts_Erledigung,ncts_VERAG_WeiterverrechungSatz  FROM  tblNCTS_TR_Sicherheitsangaben inner join tblNCTS_TR on ncts_Id = nctsSA_NctsId  "
        sqlStrSUB &= " WHERE cast(ncts_Eroeffnung as date) BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "'"
        If GRN <> "" Then
            sqlStr &= " AND nctsSA_GRN IN ( '" & GRN & "'  ) "
        Else
            sqlStr &= " AND nctsSA_GRN IN ( '" & brg_AT & "' , '" & brg_DE & "' , '" & brg_DE_NEU & "' ) "
        End If
        sqlStrSUB &= " AND ncts_status BETWEEN 50 AND 60 "
        'sqlStrSUB &= " AND ncts_status BETWEEN 50 AND 80 "
        sqlStrSUB &= firmaStr
        '  sqlStrSUB &= " and vw.OperatorId IN (" & operatorID & ")"

        sqlStrSUB &= "  order by  cast(ncts_Eroeffnung as date),ncts_ObjectName"

        ' MsgBox(sqlStr)
        ' MsgBox(sqlStrSUB)
        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)

        '   MsgBox(Fields.Item("ncts_id").Value)

        If SUB_DATA IsNot Nothing Then

            Dim subrpt As New subrptKontrolleLKW_Dakosy
            subrpt.Kennzeichen = Fields.Item("Kennzeichen").Value
            subrpt.AnmeldungsDatum = Fields.Item("AnmeldungsDatum").Value
            subrpt.ncts_id = If(Fields.Item("ncts_id") Is Nothing, -1, Fields.Item("ncts_id").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

                Dim pruefNctsId = False
                If Not groupKz Then
                    If r("ncts_id") = subrpt.ncts_id Then
                        pruefNctsId = True
                    End If
                End If

                If r("AnmeldungsDatum") = subrpt.AnmeldungsDatum And ((r("Kennzeichen") = "" And pruefNctsId) Or (r("Kennzeichen") <> "" And r("Kennzeichen") = subrpt.Kennzeichen)) Then
                    ' dt.Rows.Add(r) 
                    ' If groupKz Or (r("ncts_id") = subrpt.ncts_id) Then End If

                    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


        If LIST_NCTSABR IsNot Nothing Then

            'Spezial
            Dim SpezialFound = False
            Dim AllSpezial = True
            If SUB_DATA IsNot Nothing Then
                For Each r In SUB_DATA.Rows

                    'Dim LKW_Kz_Pruef = True 'OB nach LKW gesplittet werden kann, oder dieses Feld immer leer ist.
                    'If Not groupKz Then
                    '    If r("ncts_id") = Fields.Item("ncts_id").Value Then
                    '        LKW_Kz_Pruef = True
                    '    Else
                    '        LKW_Kz_Pruef = False
                    '    End If
                    'End If

                    Dim pruefNctsId = False
                    If Not groupKz Then
                        If r("ncts_id") = Fields.Item("ncts_id").Value Then
                            pruefNctsId = True
                        End If
                    End If

                    If r("AnmeldungsDatum") = Fields.Item("AnmeldungsDatum").Value And ((r("Kennzeichen") = "" And pruefNctsId) Or (r("Kennzeichen") <> "" And r("Kennzeichen") = Fields.Item("Kennzeichen").Value)) Then
                        If r("ncts_VERAG_WeiterverrechungSatz") IsNot DBNull.Value And r("ncts_VERAG_WeiterverrechungSatz") IsNot Nothing Then
                            SpezialFound = True 'Kennzeichen, dass spezial

                            Dim VERAG_WeiterverrechungSatz As Double = CDbl(r("ncts_VERAG_WeiterverrechungSatz"))
                            Dim L As New VERAG_PROG_ALLGEMEIN.cAbrechnung_NCTSBrg(-1)
                            L.abrg_Partner = FIRMA
                            L.abrg_artAbrechnung = "SPEZIAL"
                            L.abrg_GarantiewertBis = CDbl(r("GarantieWert"))
                            L.abrg_bertrag = VERAG_WeiterverrechungSatz
                            L.abrg_prozent = Nothing
                            L._ANZAHL = 1
                            L._BETRAG = VERAG_WeiterverrechungSatz

                            LIST_NCTSABR.LIST.Add(L)
                        Else
                            AllSpezial = False ' Es sind nicht alle spezial
                        End If

                    End If
                Next
            End If

            If Not SpezialFound OrElse Not AllSpezial Then

                'per LKW
                For Each l In LIST_NCTSABR.LIST

                    Dim GarantieWertLKW As Double = CDbl(Fields.Item("GarantieWert").Value)
                    Select Case l.abrg_artAbrechnung
                        Case "LKW"
                            If GarantieWertLKW < l.abrg_GarantiewertBis Then
                                l._ANZAHL += 1 : Exit For
                            End If
                    'Case "Versandschein"
                    '    If SUB_DATA IsNot Nothing Then
                    '        For Each r In SUB_DATA.Rows
                    '            Dim GarantieWertSND As Double = CDbl(r("GarantieWert"))
                    '            If r("AnmeldungsDatum") = Fields.Item("AnmeldungsDatum").Value And r("Kennzeichen") = Fields.Item("Kennzeichen").Value Then
                    '                If GarantieWertSND < l.abrg_GarantiewertBis Then
                    '                    l._ANZAHL += 1
                    '                End If
                    '            End If
                    '        Next
                    '    End If
                        Case "%" 'LKW
                            If GarantieWertLKW < l.abrg_GarantiewertBis Then
                                l._ANZAHL += 1
                                Dim prozent As Double = 0.001
                                If l.abrg_prozent IsNot Nothing Then prozent = CDbl(l.abrg_prozent)
                                l._BETRAG += (CDbl(Fields.Item("GarantieWert").Value)) * prozent
                                Exit For
                            End If
                    End Select

                Next
            End If

            'per VERSANDSCHEIN
            If SUB_DATA IsNot Nothing Then
                For Each r In SUB_DATA.Rows
                    'Dim next_SND = False
                    For Each l In LIST_NCTSABR.LIST
                        'If Not next_SND Then
                        Select Case l.abrg_artAbrechnung
                            Case "Versandschein"
                                Dim GarantieWertSND As Double = CDbl(r("GarantieWert"))
                                Dim pruefNctsId = False
                                If Not groupKz Then
                                    If r("ncts_id") = Fields.Item("ncts_id").Value Then
                                        pruefNctsId = True
                                    End If
                                End If

                                If r("ncts_VERAG_WeiterverrechungSatz") Is DBNull.Value Or r("ncts_VERAG_WeiterverrechungSatz") Is Nothing Then 'Wenn nicht durch Speziel-Satz verrechnet
                                    If r("AnmeldungsDatum") = Fields.Item("AnmeldungsDatum").Value And ((r("Kennzeichen") = "" And pruefNctsId) Or (r("Kennzeichen") <> "" And r("Kennzeichen") = Fields.Item("Kennzeichen").Value)) Then
                                        If GarantieWertSND < l.abrg_GarantiewertBis Then
                                            l._ANZAHL += 1
                                            'next_SND = True
                                            Exit For
                                        End If
                                    End If
                                End If
                        End Select
                        ' End If
                    Next
                Next
            End If
        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

        If LIST_NCTSABR IsNot Nothing Then
            Dim dt As New DataTable
            dt.Columns.Add("GARANTIEBIS")
            dt.Columns.Add("ANZAHL")
            dt.Columns.Add("EINZALBETRAG")
            dt.Columns.Add("GESAMTBETRAG")
            dt.Columns.Add("BETRAG")
            dt.Columns.Add("ART")

            For Each l In LIST_NCTSABR.LIST
                If l._ANZAHL > 0 Then dt.Rows.Add(l.abrg_GarantiewertBis, l._ANZAHL, l.abrg_bertrag, l.abrg_bertrag * l._ANZAHL, l._BETRAG, l.abrg_artAbrechnung)
            Next


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

            subrpt.DataSource = dt

            Me.SubReport2.Report = subrpt

        End If


    End Sub
End Class
