Imports ClosedXML.Excel

Public Class usrCntlBrgBuchungenFremd

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Private Sub usrCntlBrgBuchungenFremd_Load(sender As Object, e As EventArgs) Handles Me.Load
        cboBRG.Items.Clear()
        cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        'cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("TOBB - 15TR0004000020917 / 12TR0004000004665 / 17TR0004000032349 / 17TR0004000029518 / 21TR0004000079552 / 21TR0004000078157 / 21TR0004000092350 / 21TR0004000097157", " '12TR0004000004665','15TR0004000020917','17TR0004000032349','17TR0004000029518','21TR0004000079552','21TR0004000078157','21TR0004000092350','21TR0004000097157'"))
        'cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SGS - 16DE0000000068410", "'16DE0000000068410'"))
        'cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("MARS - 15TR0004000020908", "'15TR0004000020908'"))
        'cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("EKOL - 13TR0004000005094", "'13TR0004000005094'"))
        'cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("OREGON - 18DE0000000073276 / 19DE0000000075520 / 18DE0000000075520 / 15TR0004000020500 / 21DE0000000079716", "'18DE0000000073276','19DE0000000075520','18DE0000000075520','15TR0004000020500','21DE0000000079716'"))

        cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("TOBB - " & VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSlashList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.TOBB), VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSQLList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.TOBB)))
        cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SGS - " & VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSlashList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.SGS), VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSQLList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.SGS)))
        cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("MARS - " & VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSlashList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.MARS), VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSQLList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.MARS)))
        cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("EKOL - " & VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSlashList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.EKOL), VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSQLList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.EKOL)))
        cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("OREGON - " & VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSlashList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.OREGON), VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.getSQLList(VERAG_PROG_ALLGEMEIN.cNCTS_GUARANTEES.OREGON)))



        cboBRG.SelectedIndex = 1

        MyComboBox1.Items.Clear()
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "IMEX"
                MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IMEX", "IMEX"))
            Case "UNISPED"
                MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("UNISPED", "UNISPED"))
            Case Else
                MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAG/ATILLA", "VERAG/ATILLA"))
                MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IMEX", "IMEX"))
                MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("UNISPED", "UNISPED"))
        End Select
        MyComboBox1.SelectedIndex = 1

        '  cboMonatDurmaz.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        datVon.Value = Now.AddDays(-1)
        datBis.Value = Now.AddDays(-1)
    End Sub

    Private Sub btnNCTSDurmaz_Click(sender As Object, e As EventArgs) Handles btnNCTSDurmaz.Click

        lblErr.Visible = False
        Dim _erg = VERAG_PROG_ALLGEMEIN.cNCTS_Helper.getXLS_FremdBRG(MyComboBox1._value, cboBRG.Text, cboBRG._value, datVon.Value, datBis.Value, cbxEZollDaten.Checked)
        ' MsgBox(_erg)
        Select Case _erg
            Case "NO_RESULT"
                MsgBox("Keine Daten!")
                lblErr.Visible = False
            Case ""
            Case Else
                Process.Start(_erg)
        End Select

        Exit Sub

        Try

            lblErr.Visible = False
            If cboBRG._value = "" Then Exit Sub

            Dim where = ""
            Select Case MyComboBox1._value
                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')) "
            End Select


            'DAKOSY:
            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(cboBRG.Text.Contains("TOBB") Or cboBRG.Text.Contains("OREGON"), ", isnull(ncts_BestimmungsstelleDienststellennummer,'') as Bestimmungsstelle ", "") & "  " &
                 " ,ncts_ObjectName as Bezugsnummer " &
                 "FROM [tblDakosy_NCTSOut] INNER JOIN [tblDakosy_NCTSOut_Sicherheitsangaben] on ncts_Id=[nctsSA_NctsId] "

            sqlatr &= " WHERE nctsSA_GRN IN (" & cboBRG._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.Value.ToShortDateString & "' AND '" & datBis.Value.ToShortDateString & "')>0 "
            Dim dt As DataTable = SQL.loadDgvBySql(sqlatr, "FMZOLL", 120)


            'Bei IMEX kein E-ZOLL
            If MyComboBox1._value = "VERAG/ATILLA" Then
                If cbxEZollDaten.Checked Then

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

                    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 lblErr.Visible = True : Exit Sub
            getEmptyFraechter(dt)
            dt.Columns.Remove("Bezugsnummer")
            cProgramFunctions.genExcelFromDT_NEW(dt, {"E1:E" & (dt.Rows.Count + 1)}, , "Bürgschaftsbuchungen " & cboBRG.Text, "Datum: " & Now.ToShortDateString)


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

    Sub getEmptyFraechter(ByRef dt As DataTable)
        If dt IsNot Nothing Then
            For Each r In dt.Rows
                Try
                    If r("Firma") Is DBNull.Value OrElse r("Firma") = "" 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")
                        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

    Private Sub MyComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles MyComboBox1.SelectedIndexChanged
        cbxEZollDaten.Enabled = (MyComboBox1._value = "VERAG/ATILLA")
    End Sub
End Class
