Imports ClosedXML.Excel

Public Class usrCntlBrgBuchungenVERAG

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Public Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    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("VERAG AG (AT)  - 05AT510000G000FP7", "05AT510000G000FP7"))
        cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAG GmbH (DE)- 17DE0000000071678", "17DE0000000071678"))
        cboBRG.SelectedIndex = 1


        MyComboBox1.Items.Clear()
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAG", "VERAG"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ATILLA", "ATILLA"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IMEX", "IMEX"))
        MyComboBox1.SelectedIndex = 1

        '  cboMonatDurmaz.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        datVon.Value = CDate("01." & Now.AddMonths(-1).Month & "." & Now.AddMonths(-1).Year)
        datBis.Value = datVon.Value.AddMonths(1).AddDays(-1)
    End Sub

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

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

            Dim where = ""
            Dim whereEZOLL = ""

            If MyComboBox1._value <> "" Then
                where &= " and ncts_firma = '" & MyComboBox1._value & "' "
            End If

            If cboNL._value <> "" Then
                where &= " and ncts_niederlassung = '" & cboNL._value & "' "
                Select Case cboNL._value
                    Case "SUB" : whereEZOLL &= " and OperatorId IN (1) "
                    Case "SBG" : whereEZOLL &= " and OperatorId IN (2) "
                    Case "SUW" : whereEZOLL &= " and OperatorId IN (4,5,6,7) "
                    Case "NKD" : whereEZOLL &= " and OperatorId IN (3) "
                End Select


            End If


            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,isnull(ncts_BestimmungsstelleDienststellennummer,'') as Bestimmungsstelle " &
                  "FROM [tblDakosy_NCTSOut] INNER JOIN [tblDakosy_NCTSOut_Sicherheitsangaben] on ncts_Id=[nctsSA_NctsId] "

            sqlatr &= " WHERE nctsSA_GRN='" & 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")
            If dt Is Nothing OrElse dt.Rows.Count = 0 Then lblErr.Visible = True : Exit Sub

            If cbxEZollDaten.Checked And (cboNL._value = "SUB" Or cboNL._value = "SBG" Or cboNL._value = "SUW" Or cboNL._value = "NKD") Then

                'EZOLL:
                sqlatr = " SELECT TOP 100  AnmeldungsDatum as Datum , (select [CrossIdnt] FROM [zsAnmTransp] where [zsAnmTransp].AnmID=vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme.AnmID ) as Kennzeichen, isnull(CRN,'') as MRN, (select [CrossIdntNat] FROM [zsAnmTransp] where [zsAnmTransp].AnmID=vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme.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.AnmID) as Firma, isnull(Bestimmungszollstelle,'') as Bestimmungsstelle   "

                sqlatr &= "  FROM vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme "
                sqlatr &= " WHERE GRN IN (" & cboBRG._value & ") and AnmeldungsDatum BETWEEN '" & datVon.Value.ToShortDateString & "' AND '" & datBis.Value.ToShortDateString & "' "
                sqlatr &= whereEZOLL

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

                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


            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

    Private Sub MyComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles MyComboBox1.SelectedIndexChanged
        cboNL.Items.Clear()
        cboNL.Enabled = False
        If MyComboBox1._value = "VERAG" Then
            cboNL.Enabled = True
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SUB", "SUB"))
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SBG", "SBG"))
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("WAI", "WAI"))
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("NEU", "NEU"))
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("NKD", "NKD"))
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SUW", "SUW"))

            cboNL.SelectedIndex = 0
        ElseIf MyComboBox1._value = "ATILLA" Then
            cboNL.Enabled = True
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SUB", "SUB"))
            cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SBG (Durmaz)", "SBG"))
            cboNL.SelectedIndex = 0
        End If
    End Sub
End Class
