Imports System.Windows.Forms
Imports System.Xml.Linq

Public Class cNCTS_Helper


    Shared Function getXLS_FremdBRG(_COMPANY As String, _BRG_TEXT As String, _BRG_VALUE As String, datVon As Date, datBis As Date, _EZOLL As Boolean) As String 'MyComboBox1._value ,cboBRG.TEXT,_BRG_VALUE,datVon.Value ,datBis.Value.ToShortDateString ,cbxEZollDaten.Checked
        Try
            Dim SQL As New SQL

            ' lblErr.Visible = False
            If _BRG_VALUE = "" Then Return ""

            Dim where = ""
            Select Case _COMPANY
                Case "IMEX" : where = " and ncts_firma = 'IMEX' "
                Case "UNISPED" : where = " and ncts_firma = 'UNISPED' "
                Case "VERAG/ATILLA" : where = " and (ncts_firma is null OR ncts_firma IN ('VERAG','ATILLA','DURMAZ','FRONTOFFICE')) "
                Case "VERIMEX" : where = " and ncts_firma = 'VERIMEX' "
            End Select


            'DAKOSY:

            'DAKOSY NCTS 004:
            Dim sqlatr = " SELECT (SELECT TOP 1 CONVERT(varchar, [dySt_ergZeitpunkt] ,104 )  FROM [tblDakosy_Statusmeldungen] WHERE [dySt_dyId]=ncts_dyaAnmID AND  [dySt_status] = 50  ) as Datum, ncts_ActiveBorderTransportMeans_1_IdentificationNumber as Kennzeichen, isnull(ncts_MRN,'') as MRN,nctsGRT_AmountToBeCovered as GarantieBetrag /* '   ' as Land, , '' as Firma */" &
                 " " & If(_BRG_TEXT.Contains("TOBB") Or _BRG_TEXT.Contains("OREGON"), ", isnull(ncts_CustomsOfficeOfDestinationDeclared,'') as Bestimmungsstelle ", "") & "  " &
                 " ,ncts_ObjectName as Bezugsnummer " &
                 "FROM [tblDakosy_NCTSOut004] INNER JOIN tblDakosy_NCTSOut004_Guarantee on ncts_Id=[nctsGRT_NctsId] "

            sqlatr &= " WHERE [nctsGRT_GRN] IN (" & _BRG_VALUE & ")  and ncts_Status between 50 and 60  " ' CONVERT(date, ncts_Trans_DatumZeit ,104 )  BETWEEN '" & datVon.Value.ToShortDateString & "' AND '" & datBis.Value.ToShortDateString & "'"
            sqlatr &= where
            sqlatr &= "  AND  ncts_Eroeffnung BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "'  "
            sqlatr &= "  ORDER BY Datum "

            Dim dt As DataTable = SQL.loadDgvBySql(sqlatr, "FMZOLL", 120)

            Dim C As New DataColumn
            C.ColumnName = "Firma"
            C.MaxLength = "30"
            dt.Columns.Add(C)

            C = New DataColumn
            C.ColumnName = "Land"
            C.MaxLength = "3"
            dt.Columns.Add(C)

            'Dim sqlatr = " SELECT (SELECT TOP 1 CONVERT(varchar, [dySt_ergZeitpunkt] ,104 )  FROM [tblDakosy_Statusmeldungen] WHERE [dySt_dyId]=ncts_dyaAnmID AND  [dySt_status] = 50  ) as Datum, ncts_Befoerderungsmittel_KennzeichenAbgang as Kennzeichen, isnull(ncts_MRN,'') as MRN,  ncts_SumA_Befoerderer_Land as Land, nctsSA_AbgabenBetrag as GarantieBetrag, ncts_SumA_Befoerderer_Name as Firma " &
            '     " " & If(_BRG_TEXT.Contains("TOBB") Or _BRG_TEXT.Contains("OREGON"), ", isnull(ncts_BestimmungsstelleDienststellennummer,'') as Bestimmungsstelle ", "") & "  " &
            '     " ,ncts_ObjectName as Bezugsnummer " &
            '     "  , nctsSA_GRN as GRN " &
            '   " FROM [tblDakosy_NCTSOut] INNER JOIN [tblDakosy_NCTSOut_Sicherheitsangaben] on ncts_Id=[nctsSA_NctsId] "

            'sqlatr &= " WHERE nctsSA_GRN IN (" & _BRG_VALUE & ") and ncts_Status between 50 and 60  " ' CONVERT(date, ncts_Trans_DatumZeit ,104 )  BETWEEN '" & datVon.Value.ToShortDateString & "' AND '" & datBis.Value.ToShortDateString & "'"
            'sqlatr &= where
            'sqlatr &= "  AND    (SELECT COUNT(*) FROM [tblDakosy_Statusmeldungen] WHERE [dySt_dyId]=ncts_dyaAnmID AND  [dySt_status] = 50  AND	CONVERT(date, [dySt_ergZeitpunkt] ,104 ) BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "')>0 "
            'sqlatr &= "  ORDER BY Datum "



            ' MsgBox(sqlatr)
            'Bei IMEX kein E-ZOLL
            If _COMPANY = "VERAG/ATILLA" Then
                If _EZOLL Then

                    'EZOLL:
                    sqlatr = " Select TOP 100  AnmeldungsDatum As Datum , (Select [CrossIdnt] FROM [zsAnmTransp] where [zsAnmTransp].AnmID=vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme_Eroeffnungneu.AnmID ) As Kennzeichen, isnull(CRN,'') as MRN, cast(GarantieWert  as  Decimal(17,2))  as GarantieBetrag, (select [CrossIdntNat] FROM [zsAnmTransp] where [zsAnmTransp].AnmID=vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme_Eroeffnungneu.AnmID ) as Land,(SELECT  [Na]  FROM [ezoll].[dbo].[zsAnmCarrTra]  where [zsAnmCarrTra].AnmID=vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme_Eroeffnungneu.AnmID) as Firma  "
                    sqlatr &= " " & If(_BRG_TEXT.Contains("TOBB") Or _BRG_TEXT.Contains("OREGON"), ", isnull(Bestimmungszollstelle,'') as Bestimmungsstelle ", "") & "  "
                    sqlatr &= " ,LRN as Bezugsnummer "
                    sqlatr &= "  , GRN  "
                    sqlatr &= "  FROM vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme_Eroeffnungneu "
                        sqlatr &= " WHERE GRN IN (" & _BRG_VALUE & ") and cast( AnmeldungsDatum as date)  BETWEEN '" & datVon.ToShortDateString & "' AND '" & datBis.ToShortDateString & "' "
                    sqlatr &= "  ORDER BY Datum "

                    Dim dt2 As DataTable = SQL.loadDgvBySql(sqlatr, "EZOLL", 120)

                    If dt Is Nothing Then
                        dt = dt2
                    Else
                        'zusammenführen:
                        If dt2 IsNot Nothing Then
                            dt.Merge(dt2, True, MissingSchemaAction.Add)

                            'For Each r As DataRow In dt2.Rows
                            '    dt.Rows.Add(r)
                            'Next
                        End If
                    End If

                End If
            End If


            If dt Is Nothing OrElse dt.Rows.Count = 0 Then Return "NO_RESULT" 'lblErr.Visible = True :
            getEmptyFraechter(dt)
            dt.Columns.Remove("Bezugsnummer")
            Dim optfileName = "NCTS_TOBB_" & datVon.ToShortDateString & "-" & datBis.ToShortDateString
            Return cProgramFunctions.genExcelFromDT_NEW(dt, {"E1:E" & (dt.Rows.Count + 1)}, , "Bürgschaftsbuchungen " & _BRG_TEXT, "Datum: " & Now.ToShortDateString, False, optfileName,, )


        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
            Return ""
        End Try
    End Function

    Shared Sub getEmptyFraechter(ByRef dt As DataTable)
        Dim SQL As New SQL
        If dt IsNot Nothing Then
            For Each r In dt.Rows
                Try
                    If r("Firma") Is DBNull.Value OrElse r("Firma").trim = "" Then
                        Dim Bezugsnummer = If(r("Bezugsnummer"), "")
                        Dim FirmaTmp = ""
                        Dim FirmaNat = ""
                        Dim FirmaTmpKdNR = SQL.DLookup("isnull([FrachtführerKundenNr],'')", "Speditionsbuch", " ([AtlasBezNrEZA]='" & Bezugsnummer & "' OR [AtlasBezNrNCTS]='" & Bezugsnummer & "')", "FMZOLL")
                        If IsNumeric(FirmaTmpKdNR) Then
                            Dim AD As New VERAG_PROG_ALLGEMEIN.cAdressen(FirmaTmpKdNR)
                            If AD.hasEntry Then
                                FirmaTmp = AD.Name_1
                                FirmaNat = AD.LandKz
                            End If
                        Else
                            FirmaTmp = SQL.DLookup("isnull([Frachtführer],'')", "Speditionsbuch", " ([AtlasBezNrEZA]='" & Bezugsnummer & "' OR [AtlasBezNrNCTS]='" & Bezugsnummer & "')", "FMZOLL")

                            If FirmaTmp = "" And Bezugsnummer.ToString.Length >= 13 Then
                                FirmaTmp = SQL.DLookup("isnull([Frächter],'')", "Aviso inner join tblSendungen on AvisoID=tblSnd_AvisoID", " cast(FilialenNr as varchar(4))+'/'+cast(AbfertigungsNr as varchar(10))='" & Bezugsnummer.ToString.Substring(0, 13) & "' ", "AVISO") '  and Datum >=DATEADD(month,-2, getdate())
                                '   MsgBox("cast(FilialenNr as varchar(4))+'/'+cast(AbfertigungsNr as varchar(10))='" & Bezugsnummer.ToString.Substring(0, 13) & "'")
                            End If
                            'SELECT FROM [AVISO].[dbo].[Aviso] inner join tblSendungen on AvisoID=tblSnd_AvisoID where cast(FilialenNr as varchar(4))+'/'+cast(AbfertigungsNr as varchar(10))like '4801/23000002%'
                        End If

                        If FirmaTmp.ToString.Contains(";") Then FirmaTmp = FirmaTmp.ToString.Split(";")(0)

                        If FirmaTmp.Length > 30 Then
                            FirmaTmp = FirmaTmp.ToString.Substring(0, 30)
                        End If
                        r("Firma") = FirmaTmp
                        r("Land") = FirmaNat
                    End If

                Catch ex As Exception
                    MsgBox(ex.Message & ex.StackTrace)
                End Try
            Next
        End If


    End Sub

End Class
