Imports VERAGMonitoring.cGlobal
Imports VERAGMonitoring.cUserSettings

Public Class frmMain
    Public allowEinzahlung As Boolean = False
    Public brgusr_AdminUser = False
    Public brgusr_allowBelaustungAufheben As Boolean = False
    Public firma As String = "VERAG"
    Dim cClient As New cClient
    Public buergschaft As New cBuergschaft
    Private BRG As New cBrgDb
    Dim cMonitoringBrg As cMonitoringBrg
    Dim ico As NotifyIcon
    Dim threads As New List(Of Object)
    Dim loaded = False

    Private Sub frmMain_FormClosing(sender As Object, e As EventArgs) Handles Me.FormClosing
        Try : ico.Visible = Not Visible : Catch : End Try
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM = False
        VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "VERAG"

        lblVersion.Text = "Version " & Application.ProductVersion

        'auf UPDATES prüfen:
        Dim LOGIN As New cLogin
        If CInt(LOGIN.getAktuelleVersion().Replace(".", "")) > CInt(Application.ProductVersion.Replace(".", "")) Then
            Me.Enabled = False
            If vbYes = MsgBox("Es ist eine neue Programm-Version verfügbar. Das Programm muss aktualisiert werden, bevor Sie es weiter verwenden können. " & vbNewLine & vbNewLine & "Update jetzt durchführen?", CType(vbQuestion + vbYesNo, MsgBoxStyle), "Update verfügbar") Then
                UpdateVERAGMonitoringUPDATER()
            Else
                Application.Exit()
            End If
        End If


        ico = icoVERAGMonitoring
        AddHandler ico.BalloonTipClicked, AddressOf showMe

        If Not (BRG.checkUser(System.Environment.MachineName, Environment.UserName) Or BRG.checkUser("SERVER", Environment.UserName)) Then MsgBox("Sie sind für das Monitoring-Programm nicht freigeschaltet!") : Application.Exit()


        '    Me.firma = "VERIMEX"
         

        ' If allowEinzahlung Then btnEinzahlung.Visible = True
        If allowEinzahlung Then btnZusBel.Visible = True
        If brgusr_allowBelaustungAufheben Then btnBelastungAufheben.Visible = True
        cMonitoringBrg = BRG.getMonitoringData()
        btnKtoAkt.Visible = allowEinzahlung
        btnEinzahlung.Visible = allowEinzahlung
        'Me.Show()
        btnZusBel.Visible = False
        Me.WindowState = FormWindowState.Normal
        ' Me.Visible = True
        ' Me.Top = My.Computer.Screen.WorkingArea.Height - Me.Height
        ' Me.Left = My.Computer.Screen.WorkingArea.Width - Me.Width
        Me.Top = 10
        Me.Left = 10
        '  Me.BringToFront()
        hideme()


        'Me.WindowState = FormWindowState.Normal
        'Me.WindowState = FormWindowState.Minimized
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.ControlBox = False
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None

        '   initBrg()
        '  initAufschub()
        ' initWarnstufen()

        allowCBX(cbxAufschub_at_VeragZS, False)
        allowCBX(cbxAufschub_at_VeragCS, False)
        allowCBX(cbxAufschub_at_VeragAG, False)
        allowCBX(cbxAufschub_de_VeragAG, False)
        allowCBX(cbxBrg_at_VeragAG, False)
        allowCBX(cbxBrg_de2_VeragAG, False)
        allowCBX(cbxAufschub_de_Imex, False)
        allowCBX(cbxBrg_at_Imex, False)
        allowCBX(cbxBrg_at_Atilla, False)
        allowCBX(cbxAufschubEUST_de_Unisped, False)
        allowCBX(cbxAufschub_de_Unisped, False)
        allowCBX(cbxAufschubEUST_de_VeragAG, False)
        allowCBX(cbxAufschubEUST_de_Imex, False)
        allowCBX(cbxBrg_at_UNISPED, False)
        allowCBX(cbxBrg_at_VERAG_CS, False)
        allowCBX(cbxAufschub_at_IMEX, False)
        allowCBX(cbxAufschubEUST_de_Global, False)
        allowCBX(cbxAufschubEUST_de_UnispedDE, False)
        allowCBX(cbxAufschub_de_UnispedDE, False)

        If Environment.UserName.ToString.ToLower = "developer1" Or Environment.UserName.ToString.ToLower = "al" Then
            allowCBX(cbxAufschubEUST_de_Unisped, True)
            allowCBX(cbxAufschub_de_Unisped, True)
            allowCBX(cbxBrg_at_UNISPED, True)
            allowCBX(cbxBrg_at_VERAG_CS, True)
            allowCBX(cbxAufschubEUST_de_UnispedDE, True)
            allowCBX(cbxAufschub_de_UnispedDE, True)
            allowCBX(cbxAufschubEUST_de_KARACA, True)
        End If

        If brgusr_AdminUser Then
            allowCBX(cbxAufschub_at_VeragZS, True)
            allowCBX(cbxAufschub_at_VeragCS, True)
            allowCBX(cbxAufschub_at_VeragAG, True)
            allowCBX(cbxAufschub_de_VeragAG, True)
            allowCBX(cbxBrg_at_VeragAG, True)
            allowCBX(cbxBrg_de2_VeragAG, True)
            allowCBX(cbxAufschub_de_Imex, True)
            allowCBX(cbxBrg_at_Imex, True)
            allowCBX(cbxBrg_at_Atilla, True)
            allowCBX(cbxAufschubEUST_de_VeragAG, True)
            allowCBX(cbxAufschubEUST_de_Imex, True)
            allowCBX(cbxAufschub_at_IMEX, True)
            allowCBX(cbxAufschubEUST_de_Global, True)


            allowCBX(cbxAufschubEUST_de_Unisped, True)
            allowCBX(cbxAufschub_de_Unisped, True)
            allowCBX(cbxBrg_at_UNISPED, True)
            allowCBX(cbxBrg_at_VERAG_CS, True)
            allowCBX(cbxAufschubEUST_de_UnispedDE, True)
            allowCBX(cbxAufschub_de_UnispedDE, True)

        Else
            Select Case Me.firma
                Case "ATILLA"
                    'cbxAufschub_de_VeragAG.Checked = False
                    'cbxAufschub_at_VeragAG.Checked = False
                    'cbxAufschub_at_VeragCS.Checked = False
                    'cbxAufschub_at_VeragZS.Checked = False
                    'cbxAufschub_de_VeragAG.Enabled = False
                    'cbxAufschub_at_VeragAG.Enabled = False
                    'cbxAufschub_at_VeragCS.Enabled = False
                    'cbxAufschub_at_VeragZS.Enabled = False

                    'cbxAufschub_de_Imex.Checked = False
                    'cbxAufschub_de_Imex.Enabled = False
                    'cbxBrg_at_Imex.Enabled = False
                    Button2.Visible = False
                    Button4.Enabled = False

                    allowCBX(cbxBrg_at_Imex, True)
                    allowCBX(cbxBrg_at_VeragAG, True)
                    allowCBX(cbxBrg_de2_VeragAG, True)
                    allowCBX(cbxBrg_at_VeragAG, True)
                    allowCBX(cbxBrg_de2_VeragAG, True)
                    allowCBX(cbxBrg_at_Atilla, True)

                Case "IMEX"
                    'cbxBrg_at_VeragAG.Enabled = False
                    'cbxBrg_de2_VeragAG.Enabled = False
                    'cbxBrg_at_VeragAG.Checked = False
                    'cbxBrg_de2_VeragAG.Checked = False
                    'cbxBrg_at_Atilla.Enabled = False
                    'cbxAufschub_de_VeragAG.Checked = False
                    'cbxAufschub_at_VeragAG.Checked = False
                    'cbxAufschub_at_VeragCS.Checked = False
                    'cbxAufschub_at_VeragZS.Checked = False
                    'cbxAufschub_de_VeragAG.Enabled = False
                    'cbxAufschub_at_VeragAG.Enabled = False
                    'cbxAufschub_at_VeragCS.Enabled = False
                    'cbxAufschub_at_VeragZS.Enabled = False
                    Button2.Visible = False
                    Button4.Enabled = False
                    'cbxAufschub_de_Imex.Checked = True
                    'cbxAufschub_de_Imex.Enabled = True

                    allowCBX(cbxBrg_at_VeragAG, True)
                    allowCBX(cbxBrg_de2_VeragAG, True)
                    allowCBX(cbxBrg_at_VeragAG, True)
                    allowCBX(cbxBrg_de2_VeragAG, True)
                    allowCBX(cbxAufschub_de_Imex, True)
                    allowCBX(cbxBrg_at_Imex, True)
                    allowCBX(cbxAufschubEUST_de_Imex, True)
                    allowCBX(cbxAufschubEUST_de_Global, True)
                    allowCBX(cbxAufschub_at_IMEX, True)
                Case "UNISPED"
                    'cbxBrg_at_VeragAG.Enabled = False
                    'cbxBrg_de2_VeragAG.Enabled = False
                    'cbxBrg_at_VeragAG.Checked = False
                    'cbxBrg_de2_VeragAG.Checked = False
                    'cbxBrg_at_Atilla.Enabled = False
                    'cbxAufschub_de_VeragAG.Checked = False
                    'cbxAufschub_at_VeragAG.Checked = False
                    'cbxAufschub_at_VeragCS.Checked = False
                    'cbxAufschub_at_VeragZS.Checked = False
                    'cbxAufschub_de_VeragAG.Enabled = False
                    'cbxAufschub_at_VeragAG.Enabled = False
                    'cbxAufschub_at_VeragCS.Enabled = False
                    'cbxAufschub_at_VeragZS.Enabled = False
                    Button2.Visible = False
                    Button4.Enabled = False
                    'cbxAufschub_de_Imex.Checked = False
                    'cbxAufschub_de_Imex.Enabled = False

                    'cbxAufschub_de_Unisped.Checked = True
                    'cbxAufschub_de_Unisped.Enabled = True

                    'cbxAufschub_de_Unisped.Checked = True
                    'cbxAufschub_de_Unisped.Enabled = True
                    'cbxAufschubEUST_de_Unisped.Checked = True
                    'cbxAufschubEUST_de_Unisped.Enabled = True

                    allowCBX(cbxAufschubEUST_de_Unisped, True)
                    allowCBX(cbxAufschub_de_Unisped, True)
                    allowCBX(cbxBrg_at_UNISPED, True)
                    allowCBX(cbxBrg_at_VERAG_CS, True)
                    allowCBX(cbxAufschubEUST_de_UnispedDE, True)
                    allowCBX(cbxAufschub_de_UnispedDE, True)

                Case "VERIMEX"
                    'cbxBrg_at_VeragAG.Enabled = False
                    'cbxBrg_de2_VeragAG.Enabled = False
                    'cbxBrg_at_VeragAG.Checked = False
                    'cbxBrg_de2_VeragAG.Checked = False
                    allowCBX(cbxBrg_at_VeragAG, True)
                    allowCBX(cbxBrg_de2_VeragAG, True)
                    allowCBX(cbxBrg_at_VeragAG, True)
                    allowCBX(cbxBrg_de2_VeragAG, True)
                    allowCBX(cbxBrg_at_Imex, True)
                    allowCBX(cbxBrg_at_Atilla, True)

                    'cbxAufschub_de_VeragAG.Checked = False
                    'cbxAufschub_at_VeragAG.Checked = False
                    'cbxAufschub_at_VeragCS.Checked = False
                    'cbxAufschub_at_VeragZS.Checked = False
                    'cbxAufschub_de_VeragAG.Enabled = False
                    'cbxAufschub_at_VeragAG.Enabled = False
                    'cbxAufschub_at_VeragCS.Enabled = False
                    'cbxAufschub_at_VeragZS.Enabled = False
                    Button2.Visible = False
                    Button4.Enabled = False
                Case Else 'VERAG'

                    allowCBX(cbxAufschub_at_VeragZS, True)
                    allowCBX(cbxAufschub_at_VeragCS, True)
                    allowCBX(cbxAufschub_at_VeragAG, True)
                    allowCBX(cbxAufschub_de_VeragAG, True)
                    allowCBX(cbxBrg_at_VeragAG, True)
                    allowCBX(cbxBrg_de2_VeragAG, True)
                    allowCBX(cbxAufschub_de_Imex, True)
                    allowCBX(cbxBrg_at_Imex, True)
                    allowCBX(cbxBrg_at_Atilla, True)
                    allowCBX(cbxAufschubEUST_de_Unisped, False)
                    allowCBX(cbxAufschub_de_Unisped, False)
                    allowCBX(cbxAufschubEUST_de_VeragAG, True)
                    allowCBX(cbxAufschubEUST_de_Imex, True)
                    allowCBX(cbxBrg_at_UNISPED, False)
                    allowCBX(cbxBrg_at_VERAG_CS, True)
                    allowCBX(cbxAufschub_at_IMEX, True)
                    allowCBX(cbxAufschubEUST_de_Global, True)
                    allowCBX(cbxAufschubEUST_de_UnispedDE, False)
                    allowCBX(cbxAufschub_de_UnispedDE, False)




                    '--------------------------------------------------

                    If Me.firma.Contains("ATILLA") Or Me.firma.Contains("ALL") Then

                        allowCBX(cbxBrg_at_Imex, True)
                        allowCBX(cbxBrg_at_VeragAG, True)
                        allowCBX(cbxBrg_de2_VeragAG, True)
                        allowCBX(cbxBrg_at_VeragAG, True)
                        allowCBX(cbxBrg_de2_VeragAG, True)
                        allowCBX(cbxBrg_at_Atilla, True)
                    End If
                    If Me.firma.Contains("IMEX") Or Me.firma.Contains("ALL") Then


                        allowCBX(cbxBrg_at_VeragAG, True)
                        allowCBX(cbxBrg_de2_VeragAG, True)
                        allowCBX(cbxBrg_at_VeragAG, True)
                        allowCBX(cbxBrg_de2_VeragAG, True)
                        allowCBX(cbxAufschub_de_Imex, True)
                        allowCBX(cbxBrg_at_Imex, True)
                        allowCBX(cbxAufschubEUST_de_Imex, True)
                        allowCBX(cbxAufschubEUST_de_Global, True)
                        allowCBX(cbxAufschub_at_IMEX, True)
                    End If
                    If Me.firma.Contains("UNISPED") Or Me.firma.Contains("ALL") Then

                        allowCBX(cbxAufschubEUST_de_Unisped, True)
                        allowCBX(cbxAufschub_de_Unisped, True)
                        allowCBX(cbxBrg_at_UNISPED, True)
                        allowCBX(cbxBrg_at_VERAG_CS, True)
                        allowCBX(cbxAufschubEUST_de_UnispedDE, True)
                        allowCBX(cbxAufschub_de_UnispedDE, True)
                    End If
            End Select
        End If



        Timer.Start()
        loaded = True
    End Sub

    Sub allowCBX(cbx As CheckBox, allow As Boolean)
        cbx.Enabled = allow
        cbx.Checked = allow
    End Sub
    Private Sub hideme()
        'Me.Visible = False
        Me.Hide()
        Try
            ico.BalloonTipText = "VERAG Monitoring" & vbNewLine & "Klicken Sie auf das Logo, um den aktuellen Status anzuzeigen..."
            ico.ShowBalloonTip(2000)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer.Tick 'alle 10 Minuten
        'Me.Refresh()
        If allThreadsClosed() Then
            allinGray()
            Dim ta As New System.Threading.Thread(AddressOf initAll)
            ta.IsBackground = True
            ta.Start()
            setLabel(lblAkt, "Aktualisierung läuft ...")
        End If
        Exit Sub

        Me.Cursor = Cursors.WaitCursor

        Dim t As New System.Threading.Thread(AddressOf initAll)
        t.IsBackground = True
        t.Start()

        Me.Cursor = Cursors.Default
        Dim g As Graphics = Me.CreateGraphics
        g.DrawRectangle(New Pen(Color.Black, Width = 2), New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
    End Sub


    Private Sub SchließenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SchließenToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        hideMe()

    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        hideMe()
        frmSettings.Show()
    End Sub

    Public Sub showMe()
        'me.Visible = True
        Me.Show()
        Me.WindowState = FormWindowState.Normal
    End Sub



    Private Sub ContextMenuStrip1_Click(sender As Object, e As MouseEventArgs) Handles icoVERAGMonitoring.MouseClick
        If (e.Button = Windows.Forms.MouseButtons.Left) Then
            showMe() 'Me.Show()
            Me.BringToFront()
        ElseIf (e.Button = Windows.Forms.MouseButtons.Right) Then
            Try : ico.ContextMenuStrip = mneNotifyIcon : Catch ex As Exception : End Try
        End If
    End Sub


    Private Sub initBrg()

        ' buergschaft.brg_datum = Now.ToString("dd.MM.yyyy")
        Dim brg_AT As String = "05AT510000G000FP7"
        Dim brg_DE As String = "05DE0000000009345"
        Dim brg_DE_NEU As String = "17DE0000000071678"
        Dim brg_AT_IMEX As String = "16AT520000G000VA6"
        Dim brg_AT_ATILLA As String = "20AT520000G000092"
        Dim brg_AT_VERAG_CS As String = "22AT600000G0000H4"
        Dim brg_DE_UNISPED As String = "17DE0000000071270"

        'If cbxBrg_de_VeragAG.Checked Then 'DE
        '    'DE WOCHE
        '    Dim woche_de As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_DE)
        '    setLabel(lblDeWoche, String.Format("{0:C}", CDbl(woche_de)))
        '    'DE TAG
        '    Dim tag_de As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddMonths(-3).ToShortDateString, Now.ToShortDateString, "50", "50", brg_DE)
        '    setLabel(lblDeTag, String.Format("{0:C}", CDbl(tag_de)))
        'Else
        '    setLabel(lblDeWoche, "0.0")
        '    setLabel(lblDeTag, "0.0")
        'End If
        If cbxBrg_de2_VeragAG.Checked Then 'DE

            Dim DE_DIFFERENZ As Double = 1000000 ' UNBEKANNT DIFFENREZ!!! 1000000 + 2000000 (unbekannt??????)

            'DE WOCHE
            Dim woche_de2 As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_DE_NEU)
            'NCTS_TR DE WOCHE
            Dim DeWoche_nctsTR As Double = BRG.getBrgSumFromFMZOLL_NCTS_TR(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_DE_NEU)
            setLabel(lblDe2Woche, String.Format("{0:C}", CDbl(woche_de2 + DeWoche_nctsTR + DE_DIFFERENZ)))

            'DE TAG
            Dim tag_de2 As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddMonths(-3).ToShortDateString, Now.ToShortDateString, "50", "50", brg_DE_NEU)
            'NCTS_TR AT TAG
            Dim DetTag_nctsTR As Double = BRG.getBrgSumFromFMZOLL_NCTS_TR(Now.AddMonths(-3).ToShortDateString, Now.ToShortDateString, "50", "50", brg_DE_NEU)

            Dim saldo = tag_de2 + DetTag_nctsTR

            setLabel(lblDe2Tag, String.Format("{0:C}", CDbl(tag_de2 + DetTag_nctsTR + DE_DIFFERENZ)))
        Else
            setLabel(lblDe2Woche, "0.0")
            setLabel(lblDe2Tag, "0.0")
        End If
        If cbxBrg_at_VeragAG.Checked Then 'AT
            'ATLAS AT WOCHE
            Dim AtWoche_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT)
            'ZOLARIS AT WOCHE
            Dim AtWoche_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT)
            'MsgBox(AtWoche_zolaris)
            'TELOTEC AT WOCHE
            Dim AtWoche_TELOTEC As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT)
            'NCTS_TR AT WOCHE
            Dim AtWoche_nctsTR As Double = BRG.getBrgSumFromFMZOLL_NCTS_TR(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT)

            'AT WOCHE
            Dim woche_at As Double = AtWoche_atlas + AtWoche_zolaris + AtWoche_TELOTEC + AtWoche_nctsTR
            setLabel(lblAtWoche, String.Format("{0:C}", CDbl(woche_at)))


            'In AT nur 1 Monat zurück, da Behörde manuell storniert!
            'ZOLARIS AT TAG
            Dim AtTag_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT, " AND DatumBestimmungErreicht IS NULL ")
            'ATLAS AT TAG
            Dim AtTag_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT)
            'TELOTEC AT TAG
            Dim AtTag_telotec As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT)
            'NCTS_TR AT TAG
            Dim AtTag_nctsTR As Double = BRG.getBrgSumFromFMZOLL_NCTS_TR(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT)

            'MsgBox("AtTag_zolaris: " & AtTag_zolaris)
            'MsgBox("AtTag_atlas: " & AtTag_atlas)
            'MsgBox("AtTag_telotec: " & AtTag_telotec)
            'MsgBox("AtTag_nctsTR: " & AtTag_nctsTR)
            'AT TAG
            Dim tag_at As Double = AtTag_zolaris + AtTag_atlas + AtTag_telotec + AtTag_nctsTR
            setLabel(lblAtTag, String.Format("{0:C}", CDbl(tag_at)))
        Else
            setLabel(lblAtWoche, "0.0")
            setLabel(lblAtTag, "0.0")
        End If

        If cbxBrg_at_Imex.Checked Then 'AT IMEX
            'ATLAS AT WOCHE
            Dim AtWoche_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_IMEX)
            'ZOLARIS AT WOCHE
            Dim AtWoche_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_IMEX)
            'TELOTEC AT WOCHE
            Dim AtWoche_TELOTEC As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_IMEX)
            'NCTS FREMD AT WOCHE
            Dim AtWoche_NCTS_TR As Double = BRG.getBrgSumFromFMZOLL_NCTS_TR(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_IMEX)
            'AT WOCHE
            Dim woche_at As Double = AtWoche_atlas + AtWoche_zolaris + AtWoche_TELOTEC + AtWoche_NCTS_TR
            setLabel(lblAtWocheImex, String.Format("{0:C}", CDbl(woche_at)))

            'ZOLARIS AT TAG
            Dim AtTag_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_IMEX, " AND DatumBestimmungErreicht IS NULL ")
            'ATLAS AT TAG
            Dim AtTag_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_IMEX)
            'TELOTEC AT TAG
            Dim AtTag_telotec As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_IMEX)
            'NCTS FREMD AT TAG
            Dim AtTag_NCTS_FREMD As Double = BRG.getBrgSumFromFMZOLL_NCTS_TR(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_IMEX)
            'AT TAG
            Dim tag_at As Double = AtTag_zolaris + AtTag_atlas + AtTag_telotec + AtTag_NCTS_FREMD
            setLabel(lblAtTagImex, String.Format("{0:C}", CDbl(tag_at)))
        Else
            setLabel(lblAtWocheImex, "0.0")
            setLabel(lblAtTagImex, "0.0")
        End If
        If cbxBrg_at_Imex.Checked Then setAlarm("IMEX | AT - gVV - HZA Linz/Wels Tagessaldo", "9", Me.lblAtTagImex, cMonitoringBrg.brgusrset_gvvAt_imex_tag_warn1, cMonitoringBrg.brgusrset_gvvAt_imex_tag_warn2, cMonitoringBrg.brgusrset_gvvAt_imex_de_summe, lblAt_tag_StatusImex)
        If cbxBrg_at_Imex.Checked Then setAlarm("IMEX | AT - gVV - HZA Linz/Wels Wochenreferenzsumme ", "9", Me.lblAtWocheImex, cMonitoringBrg.brgusrset_gvvAt_imex_woche_warn1, cMonitoringBrg.brgusrset_gvvAt_imex_woche_warn2, cMonitoringBrg.brgusrset_gvvAt_imex_de_summe, lblAt_woche_StatusImex)



        If cbxBrg_at_Atilla.Checked Then 'AT ATILLA
            'ATLAS AT WOCHE
            Dim AtWoche_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_ATILLA)
            'ZOLARIS AT WOCHE
            Dim AtWoche_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_ATILLA)
            'TELOTEC AT WOCHE
            Dim AtWoche_TELOTEC As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_ATILLA)
            'AT WOCHE
            Dim woche_at As Double = AtWoche_atlas + AtWoche_zolaris + AtWoche_TELOTEC
            setLabel(lblAtWocheAtilla, String.Format("{0:C}", CDbl(woche_at)))

            'ZOLARIS AT TAG
            Dim AtTag_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_ATILLA, " AND DatumBestimmungErreicht IS NULL ")
            'ATLAS AT TAG
            Dim AtTag_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_ATILLA)
            'TELOTEC AT TAG
            Dim AtTag_telotec As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_ATILLA)
            'AT TAG
            Dim tag_at As Double = AtTag_zolaris + AtTag_atlas + AtTag_telotec
            setLabel(lblAtTagAtilla, String.Format("{0:C}", CDbl(tag_at)))
        Else
            setLabel(lblAtWocheAtilla, "0.0")
            setLabel(lblAtTagAtilla, "0.0")
        End If
        If cbxBrg_at_Atilla.Checked Then setAlarm("ATILLA | AT - gVV - HZA Linz/Wels Tagessaldo", "9", Me.lblAtTagAtilla, cMonitoringBrg.brgusrset_gvvAt_ATILLA_tag_warn1, cMonitoringBrg.brgusrset_gvvAt_ATILLA_tag_warn2, cMonitoringBrg.brgusrset_gvvAt_ATILLA_de_summe, lblAt_tag_StatusAtilla)
        If cbxBrg_at_Atilla.Checked Then setAlarm("ATILLA | AT - gVV - HZA Linz/Wels Wochenreferenzsumme ", "9", Me.lblAtWocheAtilla, cMonitoringBrg.brgusrset_gvvAt_ATILLA_woche_warn1, cMonitoringBrg.brgusrset_gvvAt_ATILLA_woche_warn2, cMonitoringBrg.brgusrset_gvvAt_ATILLA_de_summe, lblAt_woche_StatusAtilla)



        If cbxBrg_at_VERAG_CS.Checked Then 'AT VERAG CS
            'ATLAS AT WOCHE
            Dim AtWoche_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_verag_cs)
            'ZOLARIS AT WOCHE
            Dim AtWoche_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_verag_cs)
            'TELOTEC AT WOCHE
            Dim AtWoche_TELOTEC As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT_verag_cs)
            'AT WOCHE
            Dim woche_at As Double = AtWoche_atlas + AtWoche_zolaris + AtWoche_TELOTEC
            setLabel(lblAtWocheVERAG_CS, String.Format("{0:C}", CDbl(woche_at)))

            'ZOLARIS AT TAG
            Dim AtTag_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_VERAG_CS, " AND DatumBestimmungErreicht IS NULL ")
            'ATLAS AT TAG
            Dim AtTag_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_VERAG_CS)
            'TELOTEC AT TAG
            Dim AtTag_telotec As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT_VERAG_CS)
            'AT TAG
            Dim tag_at As Double = AtTag_zolaris + AtTag_atlas + AtTag_telotec
            setLabel(lblAtTagVERAG_CS, String.Format("{0:C}", CDbl(tag_at)))
        Else
            setLabel(lblAtWocheVERAG_CS, "0.0")
            setLabel(lblAtTagVERAG_CS, "0.0")
        End If

        If cbxBrg_at_UNISPED.Checked Then 'AT VERAG CS
            'ATLAS AT WOCHE
            Dim AtWoche_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_DE_UNISPED)
            'ZOLARIS AT WOCHE
            Dim AtWoche_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_DE_UNISPED)
            'TELOTEC AT WOCHE
            Dim AtWoche_TELOTEC As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_DE_UNISPED)
            'AT WOCHE
            Dim woche_at As Double = AtWoche_atlas + AtWoche_zolaris + AtWoche_TELOTEC
            setLabel(lblAtWocheUNISPED, String.Format("{0:C}", CDbl(woche_at)))

            'ZOLARIS AT TAG
            Dim AtTag_zolaris As Double = BRG.getBrgSumFromFMZOLL_Zolaris(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_DE_UNISPED, " AND DatumBestimmungErreicht IS NULL ")
            'ATLAS AT TAG
            Dim AtTag_atlas As Double = BRG.getBrgSumFromFMZOLL_Zabis(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_DE_UNISPED)
            'TELOTEC AT TAG
            Dim AtTag_telotec As Double = BRG.getBrgSumFromFMZOLL_TELOTEC(Now.AddMonths(-1).ToShortDateString, Now.ToShortDateString, "50", "50", brg_DE_UNISPED)
            'AT TAG
            Dim tag_at As Double = AtTag_zolaris + AtTag_atlas + AtTag_telotec
            setLabel(lblAtTagUNISPED, String.Format("{0:C}", CDbl(tag_at)))
        Else
            setLabel(lblAtWocheUNISPED, "0.0")
            setLabel(lblAtTagUNISPED, "0.0")
        End If

        '   If cbxBrg_at_UNISPED.Checked Then setAlarm("UNISPED - gVV - HZA München UNISPED", "19", Me.lblAtTagUNISPED, cMonitoringBrg.brgusrset_gvvAt_UNISPED_tag_warn1, cMonitoringBrg.brgusrset_gvvAt_UNISPED_tag_warn2, cMonitoringBrg.brgusrset_gvvAt_UNISPED_de_summe, lblAt_tag_StatusUNISPED)
        '    If cbxBrg_at_UNISPED.Checked Then setAlarm("UNISPED - gVV - HZA München UNISPED", "19", Me.lblAtWocheUNISPED, cMonitoringBrg.brgusrset_gvvAt_UNISPED_woche_warn1, cMonitoringBrg.brgusrset_gvvAt_UNISPED_woche_warn2, cMonitoringBrg.brgusrset_gvvAt_UNISPED_de_summe, lblAt_woche_StatusUNISPED)

        If cbxBrg_at_VERAG_CS.Checked Then setAlarm("VERAG CS | AT - gVV - HZA Linz/Wels Tagessaldo", "19", Me.lblAtTagVERAG_CS, cMonitoringBrg.brgusrset_gvvAt_CS_tag_warn1, cMonitoringBrg.brgusrset_gvvAt_CS_tag_warn2, cMonitoringBrg.brgusrset_gvvAt_CS_at_summe, lblAt_tag_StatusVERAG_CS)
        If cbxBrg_at_VERAG_CS.Checked Then setAlarm("VERAG CS | AT - gVV - HZA Linz/Wels Wochenreferenzsumme ", "19", Me.lblAtWocheVERAG_CS, cMonitoringBrg.brgusrset_gvvAt_CS_woche_warn1, cMonitoringBrg.brgusrset_gvvAt_CS_woche_warn2, cMonitoringBrg.brgusrset_gvvAt_CS_at_summe, lblAt_woche_StatusVERAG_CS)



        If cbxBrg_at_VeragAG.Checked Then setAlarm("AT - gVV - HZA Linz/Wels Tagessaldo", "6_1", Me.lblAtTag, cMonitoringBrg.brgusrset_gvvAt_tag_warn1, cMonitoringBrg.brgusrset_gvvAt_tag_warn2, cMonitoringBrg.brgusrset_gvvAt_summe, lblAt_tag_Status)
        ' If cbxBrg_de_VeragAG.Checked Then setAlarm("DE - gVV - HZA München Tagessaldo", "6_2", Me.lblDeTag, cMonitoringBrg.brgusrset_gvvDe_tag_warn1, cMonitoringBrg.brgusrset_gvvDe_tag_warn2, cMonitoringBrg.brgusrset_gvvDe_summe, lblDe_tag_Status)
        If cbxBrg_de2_VeragAG.Checked Then setAlarm("DE NEU - gVV - HZA München Tagessaldo", "6_2", Me.lblDe2Tag, cMonitoringBrg.brgusrset_gvvDe2_tag_warn1, cMonitoringBrg.brgusrset_gvvDe2_tag_warn2, cMonitoringBrg.brgusrset_gvvDe2_summe, lblDe2_tag_Status)
        If cbxBrg_at_VeragAG.Checked Then setAlarm("AT - gVV - HZA Linz/Wels Wochenreferenzsumme ", "7_1", Me.lblAtWoche, cMonitoringBrg.brgusrset_gvvAt_woche_warn1, cMonitoringBrg.brgusrset_gvvAt_woche_warn2, cMonitoringBrg.brgusrset_gvvAt_summe, lblAt_woche_Status)
        '  If cbxBrg_de_VeragAG.Checked Then setAlarm("DE - gVV - HZA München Wochenreferenzsumme ", "7_2", lblDeWoche, cMonitoringBrg.brgusrset_gvvDe_woche_warn1, cMonitoringBrg.brgusrset_gvvDe_woche_warn2, cMonitoringBrg.brgusrset_gvvDe_summe, lblDe_woche_Status)
        If cbxBrg_de2_VeragAG.Checked Then setAlarm("DE NEU - gVV - HZA München Wochenreferenzsumme ", "7_2", lblDe2Woche, cMonitoringBrg.brgusrset_gvvDe2_woche_warn1, cMonitoringBrg.brgusrset_gvvDe2_woche_warn2, cMonitoringBrg.brgusrset_gvvDe2_summe, lblDe2_woche_Status)

    End Sub


    Private Sub initAufschub()
        Dim brg_DE_VeragAG As String = "1" ' aus eigener DB
        Dim brg_AT_VeragAG_Aufschub As String = "2"
        Dim brg_AT_VeragZS_Aufschub As String = "4"
        Dim brg_AT_VeragCS_Aufschub As String = "3"
        Dim brg_DE_Imex As String = "8" ' aus eigener DB

        Dim brg_AT_VeragAG As String = "3025365"
        Dim brg_AT_VeragZS As String = "3026400"
        Dim brg_AT_VeragCS As String = "3517233"


        Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
        Dim vonbrg As Date = von
        Dim vonFaell As Date = von
        ' Dim vonbrg26 As Date = von
        If Now.Day <= 15 Then vonbrg = von.AddMonths(-1)
        If Now.Day > 16 Then vonFaell = von.AddMonths(1)
        'If Now.Day <= 26 Then vonbrg26 = von.AddMonths(-1)

        ''Aufschub DE  VERAG AG
        'Dim threadAufschub_de_VeragAG As _
        '    New System.Threading.Thread(Sub()
        '                                    If cbxAufschub_de_VeragAG.Checked Then

        '                                        ' Dim betragAktuell As Double = BRG.getBrgSumFromBrgKto(von.ToShortDateString, Now.ToShortDateString, brg_DE_VeragAG)
        '                                        ' setLabel(lblAufschubAkt_de_VeragAG, String.Format("{0:C}", CDbl(betragAktuell)))

        '                                        ' Dim betragBrg_16 As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_DE_VeragAG,,, "16")
        '                                        ' Dim betragBrg_26 As Double = BRG.getBrgSumFromBrgKto(vonbrg.AddMonths(-1).ToShortDateString, Now.ToShortDateString, brg_DE_VeragAG,,, "26")
        '                                        Dim betragBrg_16 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_VeragAG,,, , CDate("16" & vonFaell.ToString(".MM.yyyy")))
        '                                        Dim betragBrg_26 As Double = 0
        '                                        If Now.Day <= 26 Then betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_VeragAG,, , , CDate("26" & Now.ToString(".MM.yyyy"))) '9
        '                                        betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_VeragAG,, , , CDate("26" & Now.AddMonths(1).ToString(".MM.yyyy"))) '9
        '                                        betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_VeragAG,, , , CDate("26" & Now.AddMonths(2).ToString(".MM.yyyy"))) '10

        '                                        setLabel(lblAufschubBrg_de_VeragAG, String.Format("{0:C}", CDbl(betragBrg_16) + CDbl(betragBrg_26)))
        '                                        setAlarm("Aufschubkonto DE - VERAG Spedition AG - HZA München", "1", Me.lblAufschubBrg_de_VeragAG, cMonitoringBrg.brgusrset_aufschub_AG_de_warn1, cMonitoringBrg.brgusrset_aufschub_AG_de_warn2, cMonitoringBrg.brgusrset_aufschub_AG_de_summe, lblAufschubStatus_de_VeragAG)

        '                                        'Dim betragBrg_oE_16 As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_DE_VeragAG,, False, "16")
        '                                        'Dim betragBrg_oE_26 As Double = BRG.getBrgSumFromBrgKto(vonbrg.AddMonths(-1).ToShortDateString, Now.ToShortDateString, brg_DE_VeragAG,, False, "26")
        '                                        Dim betragBrg_oE_16 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_VeragAG,, False, , CDate("16" & vonFaell.ToString(".MM.yyyy")))
        '                                        Dim betragBrg_oE_26 As Double = 0
        '                                        If Now.Day <= 26 Then betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_VeragAG,, False, , CDate("26" & Now.ToString(".MM.yyyy"))) '9
        '                                        betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_VeragAG,, False, , CDate("26" & Now.AddMonths(1).ToString(".MM.yyyy"))) '9
        '                                        betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_VeragAG,, False, , CDate("26" & Now.AddMonths(2).ToString(".MM.yyyy"))) '10

        '                                        setLabel(lblAufschubBrg_de_VeragAG_oE, String.Format("{0:C}", CDbl(betragBrg_oE_16) + CDbl(betragBrg_oE_26)))
        '                                    Else
        '                                        '  setLabel(lblAufschubAkt_de_VeragAG, "0.0")
        '                                        setLabel(lblAufschubBrg_de_VeragAG, "0.0")
        '                                        setLabel(lblAufschubBrg_de_VeragAG_oE, "0.0")

        '                                    End If
        '                                End Sub)
        'threadAufschub_de_VeragAG.IsBackground = True
        'threadAufschub_de_VeragAG.Start()
        'threads.Add(threadAufschub_de_VeragAG)

        'lblAufschubAkt_de_VeragAG.ForeColor = Color.Black

        ''Aufschub DE  IMEX
        'Dim threadAufschub_de_Imex As _
        '    New System.Threading.Thread(Sub()
        '                                    If cbxAufschub_de_Imex.Checked Then

        '                                        ' Dim betragAktuell As Double = BRG.getBrgSumFromBrgKto(von.ToShortDateString, Now.ToShortDateString, brg_DE_Imex)
        '                                        'setLabel(lblAufschubAkt_de_Imex, String.Format("{0:C}", CDbl(betragAktuell)))

        '                                        'Dim betragBrg_16 As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_DE_Imex,,, "16")
        '                                        'Dim betragBrg_26 As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_DE_Imex,,, "26")

        '                                        Dim betragBrg_16 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_Imex,,, , CDate("16" & vonFaell.ToString(".MM.yyyy")))
        '                                        Dim betragBrg_26 As Double = 0
        '                                        If Now.Day <= 26 Then betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_Imex,, , , CDate("26" & Now.ToString(".MM.yyyy"))) '9
        '                                        betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_Imex,, , , CDate("26" & Now.AddMonths(1).ToString(".MM.yyyy"))) '9
        '                                        betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_Imex,, , , CDate("26" & Now.AddMonths(2).ToString(".MM.yyyy"))) '10


        '                                        setLabel(lblAufschubBrg_de_Imex, String.Format("{0:C}", CDbl(betragBrg_16) + CDbl(betragBrg_26)))
        '                                        setAlarm("Aufschubkonto DE - Imex Customs Service GmbH - HZA München", "8", Me.lblAufschubBrg_de_Imex, cMonitoringBrg.brgusrset_aufschub_imex_de_warn1, cMonitoringBrg.brgusrset_aufschub_imex_de_warn2, cMonitoringBrg.brgusrset_aufschub_imex_de_summe, lblAufschubStatus_de_Imex)

        '                                        ' Dim betragBrg_oE_16 As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_DE_Imex,, False, "16")
        '                                        ' Dim betragBrg_oE_26 As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_DE_Imex,, False, "26")
        '                                        Dim betragBrg_oE_16 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_Imex,, False, , CDate("16" & vonFaell.ToString(".MM.yyyy")))
        '                                        Dim betragBrg_oE_26 As Double = 0
        '                                        If Now.Day <= 26 Then betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_Imex,, False, , CDate("26" & Now.ToString(".MM.yyyy"))) '9
        '                                        betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_Imex,, False, , CDate("26" & Now.AddMonths(1).ToString(".MM.yyyy"))) '9
        '                                        betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, brg_DE_Imex,, False, , CDate("26" & Now.AddMonths(2).ToString(".MM.yyyy"))) '10
        '                                        setLabel(lblAufschubBrg_de_Imex_oE, String.Format("{0:C}", CDbl(betragBrg_oE_16) + CDbl(betragBrg_oE_26)))
        '                                    Else
        '                                        '  setLabel(lblAufschubAkt_de_Imex, "0.0")
        '                                        setLabel(lblAufschubBrg_de_Imex, "0.0")
        '                                        setLabel(lblAufschubBrg_de_Imex_oE, "0.0")
        '                                    End If
        '                                End Sub)
        'threadAufschub_de_Imex.IsBackground = True
        'threadAufschub_de_Imex.Start()
        'threads.Add(threadAufschub_de_Imex)
        'lblAufschubAkt_de_Imex.ForeColor = Color.Black

        Dim von_new = New Date(2015, 1, 1) 'KTOSTAND von Begin der Aufzeichnungen an...
        Dim vonbrg_new = von_new
        'Aufschub AT  VERAG AG
        Dim threadAufschub_at_VeragAG As _
            New System.Threading.Thread(Sub()
                                            If cbxAufschub_at_VeragAG.Checked Then

                                                Dim betragAktuell As Double = CDbl(BRG.getBrgSumFromZolaris(von_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragAG, True)) _
                                                + CDbl(BRG.getBrgSumFromBrgKto(von_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragAG_Aufschub))

                                                setLabel(lblAufschubAkt_at_VeragAG, String.Format("{0:C}", CDbl(betragAktuell)))

                                                Dim betragBrg As Double = -CDbl(BRG.getBrgSumFromZolaris(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragAG, True)) _
                                                + (-1 * CDbl(BRG.getBrgSumFromBrgKto(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragAG_Aufschub)))

                                                'Dim betragBrg As Double = BRG.getBrgSumFromBrgKto(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragAG_Aufschub)
                                                'Dim zolaris As Double = BRG.getBrgSumFromZolaris(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragAG, True)
                                                'betragBrg += zolaris
                                                setLabel(lblAufschubBrg_at_VeragAG, String.Format("{0:C}", CDbl(betragBrg)))
                                                setAlarm2("Aufschubkonto AT - VERAG Spedition AG - HZA Linz/Wels", "2", Me.lblAufschubBrg_at_VeragAG, cMonitoringBrg.brgusrset_aufschub_AG_at_warn1, cMonitoringBrg.brgusrset_aufschub_AG_at_warn2, cMonitoringBrg.brgusrset_aufschub_AG_at_summe, lblAufschubStatus_at_VeragAG)

                                                'Dim betragBrg_oE As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_AT_VeragAG,, False)
                                                'betragBrg_oE += zolaris
                                                'setLabel(lblAufschubBrg_at_VeragAG_oE, String.Format("{0:C}", CDbl(betragBrg_oE)))
                                            Else
                                                setLabel(lblAufschubAkt_at_VeragAG, "0.0")
                                                setLabel(lblAufschubBrg_at_VeragAG, "0.0")
                                                setLabel(lblAufschubBrg_at_VeragAG_oE, "0.0")
                                            End If
                                        End Sub)
        threadAufschub_at_VeragAG.IsBackground = True
        threadAufschub_at_VeragAG.Start()
        threads.Add(threadAufschub_at_VeragAG)


        'Aufschub AT  VERAG Customs Service GmbH
        'Aufschub AT  VERAG AG
        Dim threadAufschub_at_VeragCS As _
            New System.Threading.Thread(Sub()
                                            If cbxAufschub_at_VeragCS.Checked Then

                                                Dim betragAktuell As Double = CDbl(BRG.getBrgSumFromZolaris(von_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragCS, True)) _
                                                  + CDbl(BRG.getBrgSumFromBrgKto(von_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragCS_Aufschub))
                                                setLabel(lblAufschubAkt_at_VeragCS, String.Format("{0:C}", CDbl(betragAktuell)))

                                                Dim betragBrg As Double = -CDbl(BRG.getBrgSumFromZolaris(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragCS, True)) _
                                                + (-1 * CDbl(BRG.getBrgSumFromBrgKto(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragCS_Aufschub)))
                                                'Dim betragBrg As Double = BRG.getBrgSumFromBrgKto(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragCS_Aufschub)
                                                'Dim zolaris As Double = BRG.getBrgSumFromZolaris(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragCS, True)
                                                'MsgBox(betragBrg)
                                                'betragBrg += zolaris
                                                'MsgBox(zolaris)
                                                setLabel(lblAufschubBrg_at_VeragCS, String.Format("{0:C}", CDbl(betragBrg)))
                                                '   MsgBox(betragBrg & " -  " & cMonitoringBrg.brgusrset_aufschub_CS_at_warn1)
                                                setAlarm2("Aufschubkonto AT - VERAG Customs Sercvie GmbH - HZA Salzburg", "3", Me.lblAufschubBrg_at_VeragCS, cMonitoringBrg.brgusrset_aufschub_CS_at_warn1, cMonitoringBrg.brgusrset_aufschub_CS_at_warn2, cMonitoringBrg.brgusrset_aufschub_CS_at_summe, lblAufschubStatus_at_VeragCS)


                                                'Dim betragBrg_oE As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_AT_VeragCS,, False)
                                                'betragBrg_oE += zolaris
                                                'setLabel(lblAufschubBrg_at_VeragCS_oE, String.Format("{0:C}", CDbl(betragBrg_oE)))
                                            Else
                                                setLabel(lblAufschubAkt_at_VeragCS, "0.0")
                                                setLabel(lblAufschubBrg_at_VeragCS, "0.0")
                                                setLabel(lblAufschubBrg_at_VeragCS_oE, "0.0")
                                            End If
                                        End Sub)
        threadAufschub_at_VeragCS.IsBackground = True
        threadAufschub_at_VeragCS.Start()
        threads.Add(threadAufschub_at_VeragCS)

        'Aufschub AT  VERAG Zollservice Service GmbH
        Dim threadAufschub_at_VeragZS As _
           New System.Threading.Thread(Sub()
                                           If cbxAufschub_at_VeragZS.Checked Then

                                               Dim betragAktuell As Double = CDbl(BRG.getBrgSumFromZolaris(von_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS, True)) _
                                                + CDbl(BRG.getBrgSumFromBrgKto(von_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS_Aufschub))
                                               setLabel(lblAufschubAkt_at_VeragZS, String.Format("{0:C}", CDbl(betragAktuell)))

                                               Dim betragBrg As Double = -CDbl(BRG.getBrgSumFromZolaris(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS, True)) _
                                                + (-1 * CDbl(BRG.getBrgSumFromBrgKto(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS_Aufschub)))
                                               'Dim betragBrg As Double = BRG.getBrgSumFromBrgKto(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS_Aufschub)
                                               'Dim zolaris As Double = BRG.getBrgSumFromZolaris(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS, True)
                                               'betragBrg += zolaris
                                               setLabel(lblAufschubBrg_at_VeragZS, String.Format("{0:C}", CDbl(betragBrg)))
                                               setAlarm2("Aufschubkonto AT - VERAG Zollsercvie GmbH - HZA Linz/Wels", "4", Me.lblAufschubBrg_at_VeragZS, cMonitoringBrg.brgusrset_aufschub_ZS_at_warn1, cMonitoringBrg.brgusrset_aufschub_ZS_at_warn2, cMonitoringBrg.brgusrset_aufschub_ZS_at_summe, lblAufschubStatus_at_VeragZS)


                                               'Dim betragBrg_oE As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS,, False)
                                               'betragBrg_oE += zolaris
                                               'setLabel(lblAufschubBrg_at_VeragZS_oE, String.Format("{0:C}", CDbl(betragBrg_oE)))
                                           Else
                                               setLabel(lblAufschubAkt_at_VeragZS, "0.0")
                                               setLabel(lblAufschubBrg_at_VeragZS, "0.0")
                                               setLabel(lblAufschubBrg_at_VeragZS_oE, "0.0")
                                           End If
                                       End Sub)
        threadAufschub_at_VeragZS.IsBackground = True
        threadAufschub_at_VeragZS.Start()
        threads.Add(threadAufschub_at_VeragZS)

        'Aufschub AT  VERAG Zollservice Service GmbH
        Dim threadAufschub_at_IMEX As _
           New System.Threading.Thread(Sub()
                                           If cbxAufschub_at_IMEX.Checked Then

                                               'Dim betragAktuell As Double = CDbl(BRG.getBrgSumFromZolaris(von_new.ToShortDateString, Now.ToShortDateString, brg_AT_IMEX, True)) _
                                               ' + CDbl(BRG.getBrgSumFromBrgKto(von_new.ToShortDateString, Now.ToShortDateString, brg_AT_IMEX_Aufschub))
                                               'setLabel(lblAufschubAkt_at_IMEX, String.Format("{0:C}", CDbl(betragAktuell)))

                                               Dim betragBrg As Double = -CDbl(BRG.getBrgSumFromZolaris(vonbrg_new.ToShortDateString, Now.ToShortDateString, "3027279", True)) _
                                                + (-1 * CDbl(BRG.getBrgSumFromBrgKto(vonbrg_new.ToShortDateString, Now.ToShortDateString, 13)))
                                               'Dim betragBrg As Double = BRG.getBrgSumFromBrgKto(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_IMEX_Aufschub)
                                               'Dim zolaris As Double = BRG.getBrgSumFromZolaris(vonbrg_new.ToShortDateString, Now.ToShortDateString, brg_AT_IMEX, True)
                                               'betragBrg += zolaris
                                               setLabel(lblAufschubBrg_at_IMEX, String.Format("{0:C}", CDbl(betragBrg)))
                                               setAlarm2("Aufschubkonto AT - VERAG Zollsercvie GmbH - HZA Linz/Wels", "4", Me.lblAufschubBrg_at_IMEX, cMonitoringBrg.brgusrset_aufschub_ZS_at_warn1, cMonitoringBrg.brgusrset_aufschub_ZS_at_warn2, cMonitoringBrg.brgusrset_aufschub_ZS_at_summe, lblAufschubStatus_at_IMEX)


                                               'Dim betragBrg_oE As Double = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_AT_IMEX,, False)
                                               'betragBrg_oE += zolaris
                                               'setLabel(lblAufschubBrg_at_IMEX_oE, String.Format("{0:C}", CDbl(betragBrg_oE)))
                                           Else
                                               ' setLabel(lblAufschubAkt_at_IMEX, "0.0")
                                               setLabel(lblAufschubBrg_at_IMEX, "0.0")
                                               '    setLabel(lblAufschubBrg_at_IMEX_oE, "0.0")
                                           End If
                                       End Sub)
        threadAufschub_at_IMEX.IsBackground = True
        threadAufschub_at_IMEX.Start()
        threads.Add(threadAufschub_at_IMEX)

        doAufschubCalc("ZOLL", 1, cbxAufschub_de_VeragAG, lblAufschubBrg_de_VeragAG, lblAufschubBrg_de_VeragAG_oE, lblAufschub_de_VeragAG, lblAufschubStatus_de_VeragAG) 'VERAG AG DE - ZOLL 
        doAufschubCalc("ZOLL", 8, cbxAufschub_de_Imex, lblAufschubBrg_de_Imex, lblAufschubBrg_de_Imex_oE, lblAufschub_de_Imex, lblAufschubStatus_de_Imex) 'IMEX DE - ZOLL 

        doAufschubCalc("ZOLL", 16, cbxAufschub_de_Unisped, lblAufschubBrg_de_UNISPED, lblAufschubBrg_de_UNISPED_oE, lblAufschub_de_UNISPED, lblAufschubStatus_de_UNISPED) 'UNISPED DE - ZOLL 
        doAufschubCalc("EUST", 17, cbxAufschubEUST_de_Unisped, lblAufschubBrgEUST_de_UNISPED, lblAufschubBrgEUST_de_UNISPED_oE, lblAufschubEUST_de_UNISPED, lblAufschubStatusEUST_de_UNISPED) 'UNISPED DE - EUST 

        doAufschubCalc("EUST", 12, cbxAufschubEUST_de_Imex, lblAufschubBrgEUST_de_Imex, lblAufschubBrgEUST_de_Imex_oE, lblAufschubEUST_de_Imex, lblAufschubStatusEUST_de_Imex) 'imex DE - EUST 
        doAufschubCalc("EUST", 10, cbxAufschubEUST_de_Global, lblAufschubBrgEUST_de_global, lblAufschubBrgEUST_de_global_oE, lblAufschubEUST_de_Global, lblAufschubStatusEUST_de_global) 'GLOBAL DE - EUST 
        doAufschubCalc("EUST", 11, cbxAufschubEUST_de_VeragAG, lblAufschubBrgEUST_de_VeragAG, lblAufschubBrgEUST_de_VeragAG_oE, lblAufschubEUST_de_VeragAG, lblAufschubStatusEUST_de_VeragAG) 'VERAG DE - EUST 

        doAufschubCalc("ZOLL", 14, cbxAufschub_de_UnispedDE, lblAufschubBrg_de_UNISPEDDE, lblAufschubBrg_de_UNISPEDDE_oE, lblAufschub_de_UNISPEDDE, lblAufschubStatus_de_UNISPEDDE) 'UNISPED(DE) DE - ZOLL 
        doAufschubCalc("EUST", 15, cbxAufschubEUST_de_UnispedDE, lblAufschubBrgEUST_de_UNISPEDDE, lblAufschubBrgEUST_de_UNISPEDDE_oE, lblAufschubEUST_de_UNISPEDDE, lblAufschubStatusEUST_de_UNISPEDDE) 'UNISPED(DE) DE - EUST 

        'KARACA
        doAufschubCalc("ZOLL", 27, cbxAufschubEUST_de_KARACA, lblAufschubBrgEUST_de_KARACA, lblAufschubBrgEUST_de_KARACA_oE, lblAufschubBrgEUST_de_KARACA, lblAufschubStatusEUST_de_KARACA) 'UNISPED(DE) DE - ZOLL 


        doAufschubCalc_AT("GESAMT", 13, cbxAufschub_at_IMEX, lblAufschubBrg_at_IMEX, Nothing, lblAufschub_at_IMEX, lblAufschubStatus_at_IMEX) 'IMEX AT - GESAMT

    End Sub


    Sub doAufschubCalc(ART As String, BRG_ID As Integer, cbx As CheckBox, lblBRG As Label, lblBRG_oE As Label, lblAufschub As Label, lblAStatus As Label)

        Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
        Dim vonbrg As Date = von
        Dim vonFaell As Date = von
        Dim vonFaell_1 As Date = von

        If Now.Day <= 15 Then vonFaell_1 = von '.AddMonths(-1) ': vonFaell_1 = von
        If Now.Day > 16 Then vonFaell_1 = von  ': vonFaell_1 = von.AddMonths(-1)

        lblAufschub.ForeColor = Color.Gray
        lblBRG.ForeColor = Color.Gray
        If lblBRG_oE IsNot Nothing Then lblBRG_oE.ForeColor = Color.Gray

        Dim cAufschubKonten As New VERAG_PROG_ALLGEMEIN.cAufschubKonten(BRG_ID)


        setLabel(lblAufschub, cAufschubKonten.brgakto_bez & " - " & CDbl(cAufschubKonten.brgakto_betrag).ToString("C0"))

        Dim threadAufschub As _
            New System.Threading.Thread(Sub()
                                            If cbx.Checked Then

                                                Dim betrag As Double = 0
                                                Dim betrag_oE As Double = 0

                                                If ART = "ZOLL" Or ART = "EUST" Then
                                                    Dim betragBrg_16 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,,, , CDate("16" & vonFaell.ToString(".MM.yyyy")))
                                                    Dim betragBrg_16_1 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,,, , CDate("16" & vonFaell.AddMonths(1).ToString(".MM.yyyy")))
                                                    Dim betragBrg_26 As Double = 0
                                                    If Now.Day <= 26 Then betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, , , CDate("26" & vonFaell_1.ToString(".MM.yyyy"))) '9
                                                    betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, , , CDate("26" & vonFaell_1.AddMonths(1).ToString(".MM.yyyy"))) '9
                                                    betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, , , CDate("26" & vonFaell_1.AddMonths(2).ToString(".MM.yyyy"))) '10
                                                    betrag = CDbl(betragBrg_16) + CDbl(betragBrg_16_1) + CDbl(betragBrg_26)

                                                    Dim betragBrg_oE_16 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, False, , CDate("16" & vonFaell.ToString(".MM.yyyy")))
                                                    Dim betragBrg_oE_16_1 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, False, , CDate("16" & vonFaell.AddMonths(1).ToString(".MM.yyyy")))
                                                    Dim betragBrg_oE_26 As Double = 0
                                                    If Now.Day <= 26 Then betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, False, , CDate("26" & vonFaell_1.ToString(".MM.yyyy"))) '9
                                                    betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, False, , CDate("26" & vonFaell_1.AddMonths(1).ToString(".MM.yyyy"))) '9
                                                    betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, False, , CDate("26" & vonFaell_1.AddMonths(2).ToString(".MM.yyyy"))) '10
                                                    betrag_oE = CDbl(betragBrg_oE_16) + CDbl(betragBrg_oE_16_1) + CDbl(betragBrg_oE_26)

                                                    'ElseIf ART = "EUST" Then
                                                    '    Dim betragBrg_16 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,,, , CDate("16" & vonFaell.ToString(".MM.yyyy")))
                                                    '    Dim betragBrg_26 As Double = 0
                                                    '    If Now.Day <= 26 Then betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, , , CDate("26" & Now.ToString(".MM.yyyy"))) '9
                                                    '    betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, , , CDate("26" & Now.AddMonths(1).ToString(".MM.yyyy"))) '9
                                                    '    betragBrg_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, , , CDate("26" & Now.AddMonths(2).ToString(".MM.yyyy"))) '10
                                                    '    betrag = CDbl(betragBrg_16) + CDbl(betragBrg_26)

                                                    '    Dim betragBrg_oE_16 As Double = BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, False, , CDate("16" & vonFaell.ToString(".MM.yyyy")))
                                                    '    Dim betragBrg_oE_26 As Double = 0
                                                    '    If Now.Day <= 26 Then betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, False, , CDate("26" & Now.ToString(".MM.yyyy"))) '9
                                                    '    betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, False, , CDate("26" & Now.AddMonths(1).ToString(".MM.yyyy"))) '9
                                                    '    betragBrg_oE_26 += BRG.getBrgSumFromBrgKto(Nothing, Nothing, BRG_ID,, False, , CDate("26" & Now.AddMonths(2).ToString(".MM.yyyy"))) '10
                                                    '    betrag_oE = CDbl(betragBrg_oE_16) + CDbl(betragBrg_oE_26)

                                                Else
                                                    betrag = BRG.getBrgSumFromBrgKto_FelligZukunft(BRG_ID)
                                                    betrag_oE = BRG.getBrgSumFromBrgKto_FelligZukunft(BRG_ID,, False)
                                                    'betrag = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, BRG_ID)
                                                    'betrag_oE = BRG.getBrgSumFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, BRG_ID,, False)
                                                End If


                                                setLabel(lblBRG, String.Format("{0:C}", betrag))
                                                setAlarm(cAufschubKonten.brgakto_bez, BRG_ID, lblBRG, cAufschubKonten.brgakto_warn1, cAufschubKonten.brgakto_warn2, cAufschubKonten.brgakto_betrag, lblAStatus)

                                                setLabel(lblBRG_oE, String.Format("{0:C}", betrag_oE))
                                                setLabelvisible(lblAStatus, True)
                                            Else
                                                setLabel(lblBRG, "0.0")
                                                setLabel(lblBRG_oE, "0.0")
                                                setLabelvisible(lblAStatus, False)

                                            End If

                                            lblAufschub.ForeColor = Color.Black
                                            lblBRG.ForeColor = Color.Black
                                            lblBRG_oE.ForeColor = Color.Black
                                        End Sub)
        threadAufschub.IsBackground = True
        threadAufschub.Start()
        threads.Add(threadAufschub)


    End Sub
    Function getAufschubDT(ART As String, BRG_ID As Integer) As DataTable 'NICHT FERTIG!!!!

        Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
        Dim vonbrg As Date = von
        ' Dim vonFaell As Date = von

        'If Now.Day <= 15 Then vonbrg = von.AddMonths(-1)
        'If Now.Day > 16 Then vonFaell = von.AddMonths(1)

        Dim vonFaell As Date = von
        Dim vonFaell_1 As Date = von

        If Now.Day <= 15 Then vonFaell_1 = von '.AddMonths(-1) ': vonFaell_1 = von
        If Now.Day > 16 Then vonFaell_1 = von  ': vonFaell_1 = von.AddMonths(-1)





        Try
            Dim dt As DataTable

            If ART = "ZOLL" Or ART = "EUST" Then
                Dim dt_betragBrg_16 As DataTable = BRG.getBrgFromBrgKto(Nothing, Nothing, BRG_ID,,, , CDate("16" & vonFaell.ToString(".MM.yyyy")))
                Dim dt_betragBrg_16_1 As DataTable = BRG.getBrgFromBrgKto(Nothing, Nothing, BRG_ID,,, , CDate("16" & vonFaell.AddMonths(1).ToString(".MM.yyyy")))
                Dim dt_betragBrg_26 As New DataTable
                If Now.Day <= 26 Then dt_betragBrg_26 = BRG.getBrgFromBrgKto(Nothing, Nothing, BRG_ID,, , , CDate("26" & vonFaell_1.ToString(".MM.yyyy"))) '9
                Dim dt_betragBrg_26_2 As DataTable = BRG.getBrgFromBrgKto(Nothing, Nothing, BRG_ID,, , , CDate("26" & vonFaell_1.AddMonths(1).ToString(".MM.yyyy"))) '9
                Dim dt_betragBrg_26_3 As DataTable = BRG.getBrgFromBrgKto(Nothing, Nothing, BRG_ID,, , , CDate("26" & vonFaell_1.AddMonths(2).ToString(".MM.yyyy"))) '10

                dt = dt_betragBrg_16
                If dt_betragBrg_16_1 IsNot Nothing Then dt.Merge(dt_betragBrg_16_1)
                If dt_betragBrg_26 IsNot Nothing Then dt.Merge(dt_betragBrg_26)
                If dt_betragBrg_26_2 IsNot Nothing Then dt.Merge(dt_betragBrg_26_2)
                If dt_betragBrg_26_3 IsNot Nothing Then dt.Merge(dt_betragBrg_26_3)

            Else
                dt = BRG.getBrgFromBrgKto(vonbrg.ToShortDateString, Now.ToShortDateString, BRG_ID)
            End If

            Return dt

        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
        Return Nothing
    End Function
    Sub doAufschubCalc_AT(ART As String, BRG_ID As Integer, cbx As CheckBox, lblBRG As Label, lblBRG_oE As Label, lblAufschub As Label, lblAStatus As Label)



        Dim von_new = New Date(2015, 1, 1) 'KTOSTAND von Begin der Aufzeichnungen an...
        Dim vonbrg_new = von_new

        '  lblAufschub.ForeColor = Color.Gray
        lblBRG.ForeColor = Color.Gray
        If lblBRG_oE IsNot Nothing Then lblBRG_oE.ForeColor = Color.Gray

        Dim cAufschubKonten As New VERAG_PROG_ALLGEMEIN.cAufschubKonten(BRG_ID)

        'Aufschub AT  VERAG Zollservice Service GmbH
        Dim threadAufschub As _
           New System.Threading.Thread(Sub()
                                           If cbx.Checked Then

                                               Dim betragBrg As Double = -CDbl(BRG.getBrgSumFromZolaris(vonbrg_new.ToShortDateString, Now.ToShortDateString, cAufschubKonten.brgakto_ktonr, True)) _
                                                + (-1 * CDbl(BRG.getBrgSumFromBrgKto(vonbrg_new.ToShortDateString, Now.ToShortDateString, BRG_ID)))
                                               setLabel(lblBRG, String.Format("{0:C}", CDbl(betragBrg)))

                                               setAlarm2(cAufschubKonten.brgakto_bez, BRG_ID, lblBRG, cAufschubKonten.brgakto_warn1, cAufschubKonten.brgakto_warn2, cAufschubKonten.brgakto_betrag, lblAStatus)

                                           Else
                                               setLabel(lblBRG, "0.0")

                                           End If
                                       End Sub)
        threadAufschub.IsBackground = True
        threadAufschub.Start()
        threads.Add(threadAufschub)


    End Sub


    'threadsicherer Aufruf
    Delegate Sub setLabelCallback(l As Label, t As String)
    Private Sub setLabel(l As Label, t As String)
        If Me.InvokeRequired Then
            Dim d As New setLabelCallback(AddressOf setLabel)
            Me.Invoke(d, New Object() {l, t})
        Else
            l.Text = t
        End If
    End Sub

    'threadsicherer Aufruf
    Delegate Sub setLabelCallbackvisible(l As Label, visible As Boolean)
    Private Sub setLabelvisible(l As Label, visible As Boolean)
        If Me.InvokeRequired Then
            Dim d As New setLabelCallbackvisible(AddressOf setLabelvisible)
            Me.Invoke(d, New Object() {l, visible})
        Else
            l.Visible = visible
        End If
    End Sub


    Sub allinGray()
        Me.lblAtTag.ForeColor = Color.Gray
        Me.lblAtTagImex.ForeColor = Color.Gray
        '  Me.lblDeTag.ForeColor = Color.Gray
        Me.lblAtWoche.ForeColor = Color.Gray
        Me.lblAtWocheImex.ForeColor = Color.Gray
        ' Me.lblDeWoche.ForeColor = Color.Gray
        Me.lblDe2Tag.ForeColor = Color.Gray
        Me.lblDe2Woche.ForeColor = Color.Gray
        Me.lblAufschubBrg_de_VeragAG.ForeColor = Color.Gray
        Me.lblAufschubAkt_de_VeragAG.ForeColor = Color.Gray
        Me.lblAufschubBrg_de_Imex.ForeColor = Color.Gray
        Me.lblAufschubAkt_de_Imex.ForeColor = Color.Gray
        Me.lblAufschubBrg_at_VeragAG.ForeColor = Color.Gray
        Me.lblAufschubBrg_at_VeragZS.ForeColor = Color.Gray
        Me.lblAufschubBrg_at_VeragCS.ForeColor = Color.Gray
    End Sub
    'threadsicherer Aufruf

    Delegate Sub setAlarmCallback(brgsch As String, kontoNr As String, lbl_wert As Label, warn1 As Double, warn2 As Double, summe As Double, lblStatus As Label)
    Private Sub setAlarm(brgsch As String, kontoNr As String, lbl_wert As Label, warn1 As Double, warn2 As Double, summe As Double, lblStatus As Label)
        If Me.InvokeRequired Then
            Dim d As New setAlarmCallback(AddressOf setAlarm)
            Me.Invoke(d, New Object() {brgsch, kontoNr, lbl_wert, warn1, warn2, summe, lblStatus})
        Else
            Dim wert As Double = lbl_wert.Text
            lblStatus.Text = "OK"
            lblStatus.ForeColor = Color.White
            lblStatus.BackColor = Color.Green
            ToolTip1.SetToolTip(lblStatus, "Die Bürgschaftssumme ist auf einem sicherem Niveau")

            lbl_wert.ForeColor = Color.Black
            Dim stufe As Integer = -1
            If wert > warn1 Then
                lblStatus.Text = "HOCH"
                lblStatus.ForeColor = Color.White
                lblStatus.BackColor = Color.DarkOrange
                lbl_wert.ForeColor = Color.OrangeRed
                ToolTip1.SetToolTip(lblStatus, "Die Bürgschaftssumme ist bereits sehr hoch, bitte im Auge behalten!")
                stufe = 1
            End If
            If wert > warn2 Then
                lblStatus.Text = "WARN"
                lblStatus.ForeColor = Color.White
                lblStatus.BackColor = Color.Red
                lbl_wert.ForeColor = Color.Red
                ToolTip1.SetToolTip(lblStatus, "Die Bürgschaftssumme ist unmittelbar vor der Überschreitung. Bitte unverzüglich Maßnamen setzen!")
                stufe = 2
            End If

            If wert > summe Then
                lblStatus.Text = "ÜBER"
                lblStatus.ForeColor = Color.White
                lblStatus.BackColor = Color.DarkRed
                lbl_wert.ForeColor = Color.DarkRed
                ToolTip1.SetToolTip(lblStatus, "Die Bürgschaftssumme wurde überschritten!")
                stufe = 3
            End If

            If stufe > 0 Then
                If Not BRG.existsMeldung(Now.ToShortDateString, kontoNr, stufe, System.Environment.MachineName & "\" & Environment.UserName) Then
                    If Not alertAlreadyOpen(stufe, kontoNr) Then
                        Dim f As New frmAlert
                        f.buergschaft = brgsch
                        f.referenzbetrag = String.Format("{0:C}", CDbl(summe))
                        f.aktWert = String.Format("{0:C}", CDbl(wert))
                        f.stufe = stufe
                        f.kontoNr = kontoNr
                        f.Show()
                        '  Me.Visible=False
                    End If
                End If
            End If
        End If
    End Sub

    Delegate Sub setAlarm2Callback(brgsch As String, kontoNr As String, lbl_wert As Label, warn1 As Double, warn2 As Double, summe As Double, lblStatus As Label)
    Private Sub setAlarm2(brgsch As String, kontoNr As String, lbl_wert As Label, warn1 As Double, warn2 As Double, summe As Double, lblStatus As Label)
        If Me.InvokeRequired Then
            Dim d As New setAlarm2Callback(AddressOf setAlarm2)
            Me.Invoke(d, New Object() {brgsch, kontoNr, lbl_wert, warn1, warn2, summe, lblStatus})
        Else
            Dim wert As Double = lbl_wert.Text
            lblStatus.Text = "OK"
            lblStatus.ForeColor = Color.White
            lblStatus.BackColor = Color.Green
            ToolTip1.SetToolTip(lblStatus, "Kontostand ist auf einem sicherem Niveau")

            lbl_wert.ForeColor = Color.Black
            Dim stufe As Integer = -1
            If wert < warn1 Then
                lblStatus.Text = "WARN"
                lblStatus.ForeColor = Color.White
                lblStatus.BackColor = Color.DarkOrange
                lbl_wert.ForeColor = Color.OrangeRed
                ToolTip1.SetToolTip(lblStatus, "Kontostand ist bereits sehr niedrig!")
                stufe = 1
            End If
            If wert < warn2 Then
                lblStatus.Text = "WARN"
                lblStatus.ForeColor = Color.White
                lblStatus.BackColor = Color.Red
                lbl_wert.ForeColor = Color.Red
                ToolTip1.SetToolTip(lblStatus, "Das Konto ist fast erschöpft!")
                stufe = 2
            End If

            If wert < summe Then
                lblStatus.Text = "ÜBER"
                lblStatus.ForeColor = Color.White
                lblStatus.BackColor = Color.DarkRed
                lbl_wert.ForeColor = Color.DarkRed
                ToolTip1.SetToolTip(lblStatus, "Das Konto wurde überzogen!")
                stufe = 3
            End If

            If stufe > 0 Then
                If Not BRG.existsMeldung(Now.ToShortDateString, kontoNr, stufe, System.Environment.MachineName & "\" & Environment.UserName) Then
                    If Not alertAlreadyOpen(stufe, kontoNr) Then
                        Dim f As New frmAlert
                        f.buergschaft = brgsch
                        f.referenzbetrag = String.Format("{0:C}", CDbl(summe))
                        f.aktWert = String.Format("{0:C}", CDbl(wert))
                        f.stufe = stufe
                        f.kontoNr = kontoNr
                        f.Show()
                        '  Me.Visible=False
                    End If
                End If
            End If
        End If
    End Sub

    Function alertAlreadyOpen(stufe, kontoNr) As Boolean
        For Each f As Form In My.Application.OpenForms
            ' Use a thread-safe method to get all form titles.
            If f.Name = "frmAlert" Then
                Dim fAlert As frmAlert = DirectCast(f, frmAlert)
                If fAlert.stufe = stufe And fAlert.kontoNr = kontoNr Then
                    Return True
                End If
            End If
        Next
        Return False
    End Function


    Private Sub frmMain_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        lblAkt.Text = ""
        loaded = False
        For Each c In Me.Controls
            If c.GetType.Name = "CheckBox" Then
                If getProperty(c.Name) <> "" Then c.Checked = getProperty(c.Name)
            End If
        Next
        allinGray()
        loaded = True
        If cbxBrg_at_VeragAG.Checked Then lblBrg_at_VeragAG.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_gvvAt_summe) & ")"
        ' lblBrg_de_VeragAG.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_gvvDe_summe) & ")"
        If cbxBrg_de2_VeragAG.Checked Then lblBrg_de2_VeragAG.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_gvvDe2_summe) & ")"
        lblBrg_at_Imex.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_gvvAt_imex_de_summe) & ")"
        lblBrg_at_Atilla.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_gvvAt_ATILLA_de_summe) & ")"
        lblBrg_at_VERAG_CS.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_gvvAt_CS_at_summe) & ")"
        'Label33lblBrg_at_UNISPED.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.unis) & ")"


        lblAufschub_de_VeragAG.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_aufschub_AG_de_summe) & ")"
        lblAufschub_at_VeragAG.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_aufschub_AG_at_summe) & ")"
        lblAufschub_at_VeragZS.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_aufschub_ZS_at_summe) & ")"
        lblAufschub_at_VeragCS.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_aufschub_CS_at_summe) & ")"
        lblAufschub_de_Imex.Text &= " (" & String.Format("{0:C}", cMonitoringBrg.brgusrset_aufschub_imex_de_summe) & ")"

        Dim g As Graphics = Me.CreateGraphics
        g.DrawRectangle(New Pen(Color.Black, Width = 2), New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))

        Me.WindowState = FormWindowState.Normal
        ' Me.Visible = True
        Me.Top = My.Computer.Screen.WorkingArea.Height - Me.Height
        Me.Left = My.Computer.Screen.WorkingArea.Width - Me.Width

        'Me.BringToFront()
        Me.Cursor = Cursors.WaitCursor


        Dim t As New System.Threading.Thread(AddressOf initAll)
        t.IsBackground = True
        System.Threading.Thread.Sleep(1000)
        t.Start()

        Me.Cursor = Cursors.Default
    End Sub

    Sub initAll()
        'System.Threading.Thread.Sleep(10000)
        cMonitoringBrg = BRG.getMonitoringData()

        initBrg()
        initAufschub()
        ' initWarnstufen()
        ' MsgBox("test")
        'setLabel(lblAkt, "aktualisiert, " & Now.ToString("dd.MM. - HH:mm:ss"))
        Dim t As New System.Threading.Thread(AddressOf aktTreads)
        t.IsBackground = True
        t.Start()
    End Sub

    Sub aktTreads()
        While allThreadsClosed() = False
            System.Threading.Thread.Sleep(500)
        End While
        setLabel(lblAkt, "aktualisiert, " & Now.ToString("dd.MM. - HH:mm:ss"))
    End Sub
    Function allThreadsClosed() As Boolean
        Try
            For Each t As System.Threading.Thread In threads
                If t.IsAlive Then
                    Return False
                Else
                    '  threads.Remove(t)
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return True
    End Function
    Private Sub cbxBrg_de_VeragAG_CheckedChanged(sender As Object, e As EventArgs) Handles cbxBrg_de2_VeragAG.CheckedChanged, cbxBrg_at_VeragAG.CheckedChanged, cbxAufschub_de_VeragAG.CheckedChanged, cbxAufschub_at_VeragAG.CheckedChanged, cbxAufschub_at_VeragCS.CheckedChanged, cbxAufschub_at_VeragZS.CheckedChanged, cbxAufschubEUST_de_VeragAG.CheckedChanged, cbxAufschubEUST_de_Imex.CheckedChanged, cbxAufschubEUST_de_Global.CheckedChanged, cbxAufschub_de_Unisped.CheckedChanged, cbxAufschubEUST_de_Unisped.CheckedChanged, cbxAufschub_de_UnispedDE.CheckedChanged, cbxAufschubEUST_de_UnispedDE.CheckedChanged, cbxAufschub_at_IMEX.CheckedChanged, cbxBrg_at_UNISPED.CheckedChanged, cbxBrg_at_VERAG_CS.CheckedChanged, cbxBrg_at_Atilla.CheckedChanged, cbxBrg_at_Imex.CheckedChanged, cbxAufschubEUST_de_KARACA.CheckedChanged
        'MsgBox(sender.name)
        Me.Cursor = Cursors.WaitCursor
        If loaded Then
            If sender.name <> "" Then setProperty(sender.Name, sender.Checked)
            Me.Cursor = Cursors.WaitCursor

            Dim t As New System.Threading.Thread(AddressOf initAll)
            t.IsBackground = True
            t.Start()

            Me.Cursor = Cursors.Default
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub HauptfensterToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HauptfensterToolStripMenuItem.Click
        frmAlert.Close()
        frmSettings.Close()
        ' Me.Show()
        showMe()
        Me.BringToFront()
    End Sub

    Private Sub EinstellungenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EinstellungenToolStripMenuItem.Click
        hideme()

        frmSettings.Show()
        frmSettings.BringToFront()
    End Sub

    Private Sub UpdateToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UpdateToolStripMenuItem.Click
        UpdateVERAGMonitoringUPDATER()
    End Sub
    Public Sub UpdateVERAGMonitoringUPDATER()
        If Not System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory & "VERAGMonitoringUPDATER.exe") Then
            MsgBox("ERROR_UPDATE: Update konnte nicht fortgesetzt werden." & vbNewLine & vbNewLine & "Update-Datei existiert nicht.", vbCritical)
            Application.Exit()
        Else
            Process.Start("VERAGMonitoringUPDATER.exe")
            Application.Exit()
        End If
    End Sub

    Private Sub lblAufschubAkt_at_VeragAG_Click(sender As Object, e As EventArgs) Handles lblAufschubAkt_at_VeragAG.Click
        Dim brg_AT_VeragAG As String = "3025365"
        Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
        frmShowData.Show()
        frmShowData.dgv.DataSource = BRG.loadDgvBySqlZOLARIS(von.ToShortDateString, Now.ToShortDateString, brg_AT_VeragAG)
        frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
    End Sub

    Private Sub lblAufschubBrg_at_VeragAG_Click(sender As Object, e As EventArgs) Handles lblAufschubBrg_at_VeragAG.Click
        If cbxAufschub_at_VeragAG.Checked Then
            Dim brg_AT_VeragAG As String = "3025365"
            Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
            Dim vonbrg As Date = von
            If Now.Day <= 15 Then vonbrg = von.AddMonths(-1)
            frmShowData.Show()
            frmShowData.dgv.DataSource = BRG.loadDgvBySqlZOLARIS(vonbrg.ToShortDateString, Now.ToShortDateString, brg_AT_VeragAG)
            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
        End If
    End Sub

    Private Sub lblAufschubAkt_at_VeragCS_Click(sender As Object, e As EventArgs) Handles lblAufschubAkt_at_VeragCS.Click
        Dim brg_AT_VeragCS As String = "3517233"
        Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
        frmShowData.Show()
        frmShowData.dgv.DataSource = BRG.loadDgvBySqlZOLARIS(von.ToShortDateString, Now.ToShortDateString, brg_AT_VeragCS)
        frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
    End Sub

    Private Sub lblAufschubBrg_at_VeragCS_Click(sender As Object, e As EventArgs) Handles lblAufschubBrg_at_VeragCS.Click
        If cbxAufschub_at_VeragCS.Checked Then
            Dim brg_AT_VeragCS As String = "3517233"
            Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
            Dim vonbrg As Date = von
            If Now.Day <= 15 Then vonbrg = von.AddMonths(-1)
            frmShowData.Show()
            frmShowData.dgv.DataSource = BRG.loadDgvBySqlZOLARIS(vonbrg.ToShortDateString, Now.ToShortDateString, brg_AT_VeragCS)
            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
        End If
    End Sub

    Private Sub lblAufschubAkt_at_VeragZS_Click(sender As Object, e As EventArgs) Handles lblAufschubAkt_at_VeragZS.Click
        Dim brg_AT_VeragZS As String = "3026400"
        Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
        frmShowData.Show()
        frmShowData.dgv.DataSource = BRG.loadDgvBySqlZOLARIS(von.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS)
        frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
    End Sub

    Private Sub lblAufschubBrg_at_VeragZS_Click(sender As Object, e As EventArgs) Handles lblAufschubBrg_at_VeragZS.Click
        If cbxAufschub_at_VeragZS.Checked Then
            Dim brg_AT_VeragZS As String = "3026400"
            Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
            Dim vonbrg As Date = von
            If Now.Day <= 15 Then vonbrg = von.AddMonths(-1)
            frmShowData.Show()
            frmShowData.dgv.DataSource = BRG.loadDgvBySqlZOLARIS(vonbrg.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS)
            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
        End If
    End Sub

    Private Sub lblAufschubAkt_de_VeragAG_Click(sender As Object, e As EventArgs) Handles lblAufschubAkt_de_VeragAG.Click
        Dim brg_DE_VeragAG As String = "1"
        Dim von As Date = DateSerial(Now.Year, Now.Month, 1)

        frmShowData.Show()
        frmShowData.dgv.DataSource = BRG.loadDgvBySqlFromAufschubKto(von.ToShortDateString, Now.ToShortDateString, brg_DE_VeragAG)
        frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
    End Sub

    Private Sub lblAufschubBrg_de_VeragAG_Click(sender As Object, e As EventArgs) Handles lblAufschubBrg_de_VeragAG.Click
        If cbxAufschub_de_VeragAG.Checked Then

            Dim brg_DE_VeragAG As String = "1"
            Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
            Dim vonbrg As Date = von
            Dim vonFaell As Date = von
            Dim vonFaell_1 As Date = von

            If Now.Day <= 15 Then vonFaell_1 = von '.AddMonths(-1) ': vonFaell_1 = von
            If Now.Day > 16 Then vonFaell_1 = von  ': vonFaell_1 = von.AddMonths(-1)
            frmShowData.Show()

            Dim faelligkeitWhere = " AND brgak_faelligkeitsdatum IN ("
            faelligkeitWhere &= "'" & CDate("16" & vonFaell.ToString(".MM.yyyy")).ToShortDateString & "'"
            faelligkeitWhere &= ",'" & CDate("16" & vonFaell.AddMonths(1).ToString(".MM.yyyy")).ToShortDateString & "'"

            If Now.Day <= 26 Then faelligkeitWhere &= ",'" & CDate("26" & vonFaell_1.ToString(".MM.yyyy")).ToShortDateString & "'"
            faelligkeitWhere &= ",'" & CDate("26" & vonFaell_1.AddMonths(1).ToString(".MM.yyyy")).ToShortDateString & "'"
            faelligkeitWhere &= ",'" & CDate("26" & vonFaell_1.AddMonths(2).ToString(".MM.yyyy")).ToShortDateString & "'"
            faelligkeitWhere &= ") "

            frmShowData.dgv.DataSource = BRG.loadDgvBySqlFromAufschubKto_Faelligkeit(faelligkeitWhere, brg_DE_VeragAG)
            ' frmShowData.dgv.DataSource = BRG.loadDgvBySqlFromAufschubKto(vonbrg.ToShortDateString, Now.ToShortDateString, brg_DE_VeragAG)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
        End If
    End Sub

    Private Sub lblDeTag_Click(sender As Object, e As EventArgs)
        Dim brg_DE As String = "05DE0000000009345"
        Dim frmShowData = New frmShowData("BRG", brg_DE, Now.AddMonths(-3), Now)
        frmShowData.Show()
    End Sub


    Private Sub lblDeWoche_Click(sender As Object, e As EventArgs)
        Dim brg_DE As String = "05DE0000000009345"
        Dim frmShowData = New frmShowData("BRG", brg_DE, Now.AddDays(-6), Now)
        frmShowData.Show()
    End Sub

    Private Sub lblAtTag_Click(sender As Object, e As EventArgs) Handles lblAtTag.Click
        If cbxBrg_at_VeragAG.Checked Then
            Dim brg_AT As String = "05AT510000G000FP7"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddMonths(-3), Now)
            frmShowData.Show()

        End If
        ' frmShowDataDouble.dgv1.DataSource = BRG.loadDgvBySqlFromBrgKtoAtlas(Now.AddMonths(-3).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT)
        ' frmShowDataDouble.dgv2.DataSource = BRG.loadDgvBySqlFromBrgKtoUnionEZoll(Now.AddMonths(-3).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT)
        '  frmShowDataDouble.Label2.Text = "Anzahl: " & (frmShowDataDouble.dgv2.RowCount + frmShowDataDouble.dgv2.RowCount)
    End Sub

    Private Sub lblAtTagimex_Click(sender As Object, e As EventArgs) Handles lblAtTagImex.Click
        If cbxBrg_at_Imex.Checked Then

            Dim brg_AT As String = "16AT520000G000VA6"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddMonths(-3), Now)
            frmShowData.Show()

        End If
        ' frmShowDataDouble.dgv1.DataSource = BRG.loadDgvBySqlFromBrgKtoAtlas(Now.AddMonths(-3).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT)
        ' frmShowDataDouble.dgv2.DataSource = BRG.loadDgvBySqlFromBrgKtoUnionEZoll(Now.AddMonths(-3).ToShortDateString, Now.ToShortDateString, "50", "50", brg_AT)
        '  frmShowDataDouble.Label2.Text = "Anzahl: " & (frmShowDataDouble.dgv2.RowCount + frmShowDataDouble.dgv2.RowCount)
    End Sub

    Private Sub lblAtWoche_Click(sender As Object, e As EventArgs) Handles lblAtWoche.Click
        If cbxBrg_at_VeragAG.Checked Then

            Dim brg_AT As String = "05AT510000G000FP7"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddDays(-6), Now)
            frmShowData.Show()

        End If
        '   frmShowDataDouble.Show()
        ' frmShowDataDouble.dgv1.DataSource = BRG.loadDgvBySqlFromBrgKtoAtlas(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT)
        ' frmShowDataDouble.dgv2.DataSource = BRG.loadDgvBySqlFromBrgKtoUnionEZoll(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT)
        ' frmShowDataDouble.Label2.Text = "Anzahl: " & (frmShowDataDouble.dgv2.RowCount + frmShowDataDouble.dgv2.RowCount)
    End Sub
    Private Sub lblAtWocheimex_Click(sender As Object, e As EventArgs) Handles lblAtWocheImex.Click
        If cbxBrg_at_Imex.Checked Then

            Dim brg_AT As String = "16AT520000G000VA6"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddDays(-6), Now)
            frmShowData.Show()

        End If

        '   frmShowDataDouble.Show()
        ' frmShowDataDouble.dgv1.DataSource = BRG.loadDgvBySqlFromBrgKtoAtlas(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT)
        ' frmShowDataDouble.dgv2.DataSource = BRG.loadDgvBySqlFromBrgKtoUnionEZoll(Now.AddDays(-6).ToShortDateString, Now.ToShortDateString, "50", "60", brg_AT)
        ' frmShowDataDouble.Label2.Text = "Anzahl: " & (frmShowDataDouble.dgv2.RowCount + frmShowDataDouble.dgv2.RowCount)
    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        'lblImport.Visible = True
        cClient.begin()
    End Sub

    '   Delegate Sub initSuccessCallback()
    '  Public Sub initSuccess()
    '      If Me.InvokeRequired Then
    '          Me.Invoke(New initSuccessCallback(AddressOf initSuccess))
    '          Exit Sub
    '      Else
    '          Me.btnAktualisieren.PerformClick()
    '         MsgBox("Einlesen erfolgreich!" & Me.lblImport.Visible, vbSystemModal, "Aufschubkonten")
    '  Me.lblImport.Text = "fertig"
    '   Me.lblImport.Visible = False
    '    End If
    'End Sub

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles btnEinzahlung.Click
        frmEinzahlung.Show()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnZusBel.Click
        frmZusBelast.Show()
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        refreshMe()
    End Sub
    Private Sub refreshMe()
        If allThreadsClosed() Then
            allinGray()
            Dim t As New System.Threading.Thread(AddressOf initAll)
            t.IsBackground = True
            t.Start()
            setLabel(lblAkt, "Aktualisierung läuft ...")
        End If
    End Sub

    Private Sub Button3_Click_2(sender As Object, e As EventArgs) Handles btnKtoAkt.Click
        frmKtoStandAkt.Show()
    End Sub

    Private Sub lblDe2Tag_Click(sender As Object, e As EventArgs) Handles lblDe2Tag.Click
        If cbxBrg_de2_VeragAG.Checked Then
            Dim brg_DE_NEU As String = "17DE0000000071678"
            Dim frmShowData = New frmShowData("BRG", brg_DE_NEU, Now.AddMonths(-3), Now)
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblDe2Woche_Click(sender As Object, e As EventArgs) Handles lblDe2Woche.Click
        If cbxBrg_de2_VeragAG.Checked Then
            Dim brg_DE_NEU As String = "17DE0000000071678"
            Dim frmShowData = New frmShowData("BRG", brg_DE_NEU, Now.AddDays(-6), Now)
            frmShowData.Show()
        End If
    End Sub

    Private Sub Button3_Click_3(sender As Object, e As EventArgs) Handles btnBelastungAufheben.Click
        frmBelastAufheben.ShowDialog(Me)
        refreshMe()
    End Sub

    Private Sub cbxAufschub_de_Imex_CheckedChanged(sender As Object, e As EventArgs) Handles cbxAufschub_de_Imex.CheckedChanged
        'MsgBox(sender.name)
        Me.Cursor = Cursors.WaitCursor
        If loaded Then
            If sender.name <> "" Then setProperty(sender.Name, sender.Checked)
            Me.Cursor = Cursors.WaitCursor

            Dim t As New System.Threading.Thread(AddressOf initAll)
            t.IsBackground = True
            t.Start()

            Me.Cursor = Cursors.Default
        End If
        Me.Cursor = Cursors.Default
    End Sub



    Private Sub lblAufschubAkt_de_Imex_Click(sender As Object, e As EventArgs) Handles lblAufschubAkt_de_Imex.Click
        Dim brg_DE_Imex As String = "8"
        Dim von As Date = DateSerial(Now.Year, Now.Month, 1)

        frmShowData.Show()
        frmShowData.dgv.DataSource = BRG.loadDgvBySqlFromAufschubKto(von.ToShortDateString, Now.ToShortDateString, brg_DE_Imex)
        frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
    End Sub

    Private Sub lblAufschubBrg_de_Imex_Click(sender As Object, e As EventArgs) Handles lblAufschubBrg_de_Imex.Click
        If cbxAufschub_de_Imex.Checked Then

            Dim brg_DE_Imex As String = "8"
            Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
            Dim vonbrg As Date = von
            Dim vonFaell As Date = von
            Dim vonFaell_1 As Date = von

            If Now.Day <= 15 Then vonFaell_1 = von '.AddMonths(-1) ': vonFaell_1 = von
            If Now.Day > 16 Then vonFaell_1 = von  ': vonFaell_1 = von.AddMonths(-1)
            frmShowData.Show()

            Dim faelligkeitWhere = " AND brgak_faelligkeitsdatum IN ("
            faelligkeitWhere &= "'" & CDate("16" & vonFaell.ToString(".MM.yyyy")).ToShortDateString & "'"
            faelligkeitWhere &= ",'" & CDate("16" & vonFaell.AddMonths(1).ToString(".MM.yyyy")).ToShortDateString & "'"

            If Now.Day <= 26 Then faelligkeitWhere &= ",'" & CDate("26" & vonFaell_1.ToString(".MM.yyyy")).ToShortDateString & "'"
            faelligkeitWhere &= ",'" & CDate("26" & vonFaell_1.AddMonths(1).ToString(".MM.yyyy")).ToShortDateString & "'"
            faelligkeitWhere &= ",'" & CDate("26" & vonFaell_1.AddMonths(2).ToString(".MM.yyyy")).ToShortDateString & "'"
            faelligkeitWhere &= ") "

            frmShowData.dgv.DataSource = BRG.loadDgvBySqlFromAufschubKto_Faelligkeit(faelligkeitWhere, brg_DE_Imex)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
        End If
    End Sub

    Private Sub lblAtTagAtilla_Click(sender As Object, e As EventArgs) Handles lblAtTagAtilla.Click
        If cbxBrg_at_Atilla.Checked Then
            Dim brg_AT As String = "20AT520000G000092"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddMonths(-3), Now)
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAtWocheAtilla_Click(sender As Object, e As EventArgs) Handles lblAtWocheAtilla.Click
        If cbxBrg_at_Atilla.Checked Then
            Dim brg_AT As String = "20AT520000G000092"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddDays(-6), Now)
            frmShowData.Show()
        End If
    End Sub

    Dim secondmonitorSelected = False
    Private Sub Button3_Click_4(sender As Object, e As EventArgs) Handles Button3.Click
        Dim secondaryMonitor = Screen.AllScreens.FirstOrDefault(Function(x) Not x.Primary)
        If secondmonitorSelected Then
            secondaryMonitor = Screen.AllScreens.FirstOrDefault(Function(x) x.Primary) 'Hauptbildschirm
        End If
        If secondaryMonitor IsNot Nothing Then
            Me.WindowState = FormWindowState.Normal
            Dim newLocation = secondaryMonitor.Bounds.Location
            'newLocation.Offset(0, 0) ' adjust as needed
            Me.Location = newLocation

            ' Me.WindowState = FormWindowState.Normal
            ' Me.Visible = True
            ' MsgBox(secondaryMonitor.Bounds.Location.Y & " - " & MsgBox(secondaryMonitor.Bounds.Location.X))
            Me.Top = secondaryMonitor.Bounds.Location.Y + secondaryMonitor.WorkingArea.Height - Me.Height
            Me.Left = secondaryMonitor.Bounds.Location.X + secondaryMonitor.WorkingArea.Width - Me.Width


            Me.Refresh()
            'Me.WindowState = FormWindowState.Maximized
            ' Also see Me.Size and Me.Bounds
        End If
        secondmonitorSelected = Not secondmonitorSelected
    End Sub

    Private Sub lblAufschubBrgEUST_de_UNISPEDDE_Click(sender As Object, e As EventArgs) Handles lblAufschubBrgEUST_de_UNISPEDDE.Click
        If cbxAufschubEUST_de_UnispedDE.Checked Then
            Dim frmShowData As New frmShowData
            frmShowData.dgv.DataSource = getAufschubDT("EUST", 15)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAufschubBrgEUST_de_VeragAG_Click(sender As Object, e As EventArgs) Handles lblAufschubBrgEUST_de_VeragAG.Click
        If cbxAufschubEUST_de_VeragAG.Checked Then
            Dim frmShowData As New frmShowData
            frmShowData.dgv.DataSource = getAufschubDT("EUST", 11)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
            frmShowData.Show()
        End If
    End Sub

    Private Sub Panel2_Paint(sender As Object, e As PaintEventArgs) Handles Panel2.Paint

    End Sub

    Private Sub lblAufschubBrgEUST_de_global_Click(sender As Object, e As EventArgs) Handles lblAufschubBrgEUST_de_global.Click
        If cbxAufschubEUST_de_Global.Checked Then
            Dim frmShowData As New frmShowData
            frmShowData.dgv.DataSource = getAufschubDT("EUST", 10)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAufschubBrgEUST_de_Imex_Click(sender As Object, e As EventArgs) Handles lblAufschubBrgEUST_de_Imex.Click
        If cbxAufschubEUST_de_Imex.Checked Then
            Dim frmShowData As New frmShowData
            frmShowData.dgv.DataSource = getAufschubDT("EUST", 12)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAufschubBrgEUST_de_UNISPED_Click(sender As Object, e As EventArgs) Handles lblAufschubBrgEUST_de_UNISPED.Click
        If cbxAufschubEUST_de_Unisped.Checked Then
            Dim frmShowData As New frmShowData
            frmShowData.dgv.DataSource = getAufschubDT("EUST", 17)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAufschubBrg_de_UNISPED_Click(sender As Object, e As EventArgs) Handles lblAufschubBrg_de_UNISPED.Click
        If cbxAufschub_de_Unisped.Checked Then
            Dim frmShowData As New frmShowData
            frmShowData.dgv.DataSource = getAufschubDT("ZOLL", 16)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAufschubBrg_de_UNISPEDDE_Click(sender As Object, e As EventArgs) Handles lblAufschubBrg_de_UNISPEDDE.Click
        If cbxAufschub_de_UnispedDE.Checked Then
            Dim frmShowData As New frmShowData
            frmShowData.dgv.DataSource = getAufschubDT("ZOLL", 14)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAtTagUNISPED_Click(sender As Object, e As EventArgs) Handles lblAtTagUNISPED.Click
        If cbxBrg_at_UNISPED.Checked Then
            Dim brg_AT As String = "17DE0000000071270"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddMonths(-3), Now)
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAtTagVERAG_CS_Click(sender As Object, e As EventArgs) Handles lblAtTagVERAG_CS.Click
        If cbxBrg_at_VERAG_CS.Checked Then
            Dim brg_AT As String = "22AT600000G0000H4"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddMonths(-3), Now)
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAtWocheVERAG_CS_Click(sender As Object, e As EventArgs) Handles lblAtWocheVERAG_CS.Click
        If cbxBrg_at_VERAG_CS.Checked Then
            Dim brg_AT As String = "22AT600000G0000H4"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddDays(-6), Now)
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAtWocheUNISPED_Click(sender As Object, e As EventArgs) Handles lblAtWocheUNISPED.Click
        If cbxBrg_at_UNISPED.Checked Then
            Dim brg_AT As String = "17DE0000000071270"
            Dim frmShowData = New frmShowData("BRG", brg_AT, Now.AddDays(-6), Now)
            frmShowData.Show()
        End If
    End Sub

    Private Sub lblAufschubBrg_at_IMEX_Click(sender As Object, e As EventArgs) Handles lblAufschubBrg_at_IMEX.Click
        If cbxAufschub_at_IMEX.Checked Then
            Dim brg_AT_VeragZS As String = "3027279"
            Dim von As Date = DateSerial(Now.Year, Now.Month, 1)
            Dim vonbrg As Date = von
            If Now.Day <= 15 Then vonbrg = von.AddMonths(-1)
            frmShowData.Show()
            frmShowData.dgv.DataSource = BRG.loadDgvBySqlZOLARIS(vonbrg.ToShortDateString, Now.ToShortDateString, brg_AT_VeragZS)
            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
        End If
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim f As New frmMRNStorno
        f.ShowDialog(Me)
    End Sub

    Private Sub lblAufschubBrgEUST_de_KARACA_oE_Click(sender As Object, e As EventArgs) Handles lblAufschubBrgEUST_de_KARACA_oE.Click
        If cbxAufschubEUST_de_UnispedDE.Checked Then
            Dim frmShowData As New frmShowData
            frmShowData.dgv.DataSource = getAufschubDT("EUST", 27)

            frmShowData.Label2.Text = "Anzahl: " & frmShowData.dgv.RowCount
            frmShowData.Show()
        End If
    End Sub
End Class



