Imports System.Data.SqlClient
Imports System.IO

Public Class cAllgemein
    Public Shared FIRMA As String
    Public Shared NIEDERLASSUNG As String 'SUB, WAI, ...
    Public Shared ABTEILUNG As String 'QS ZOLL ...
    Public Shared STAMMFILIALE As String 'IMEX,UNISPED;ATILLA...
    Public Shared WARENORT_ZUGANG As Boolean = False
    Public Shared WARENORT As String
    Public Shared ATILLA_SND As Boolean = True
    Public Shared NEWLOGIN As Boolean = False

    Public Shared CLUSTER As String = "" 'Firmenverbund
    Public Shared FIRMEN_GRUPPE As String = "VERAGDCS" 'Firmenverbund-Gruppe
    ' Public Shared SPECIFIC As cSPECIFIC = New cSPECIFIC

    Public Shared ERR_OP_GLOBAL As String = ERROR_OP.SHOW

    ' Public Shared AVISO_SERVER As String = "[BUCHHALTUNG\SQLEXPRESS]"
    'Public Shared AVISO_SERVER As String = "[SQL01.verag.ost.dmn\VERAG]"
    'Public Shared AVISO_SERVER As String = "[SQLGUIDE.verag.ost.dmn\VERAG]"


    Public Shared AVISO_SERVER As String = "[SQLGUIDE01.verag.ost.dmn]" '"[SQLGUIDE01.verag.ost.dmn]"
    Public Shared FMZOLL_SERVER As String = "[SQLGUIDE01.verag.ost.dmn]" '"[sqlcluster\verag]" '"[FMZOLL\SQLFMZOLL]"
    Public Shared SQLGUIDE01_USED As Boolean = True 'FM-ZOLL Ablöse!!!!

    Public Shared AVISO_LAYOUT_NEW As Boolean = False

    Public Shared CBAM_LIST As List(Of String) = Nothing


    'Public Shared FMZOLL_ATLAS_Datensicherung As String = "\\192.168.0.91\f\FMZoll\Datensicherung\atlas\atlas\fssouzb"
    Public Shared FMZOLL_ATLAS_Datensicherung_OLD As String = "\\share01\F\FMZoll\Datensicherung\atlas\atlas\fssouzb"

    Public Shared UK_EORI_LTD = "GB078068385000"

    Public Shared BÜRO As String

    Public Shared PRINTER_BON As String
    Public Shared PRINTER_STANDARD As String
    Public Shared PRINTER_LIST As ArrayList = Nothing


    Public Shared dpi As Integer
    Public Shared TESTSYSTEM As Boolean = False

    Public Shared PROGNAME = ""
    Public Shared PROGID As Integer = 16 'AVISO
    Public Shared PROGVERSION As String = ""

    Public Shared USRNAME As String
    Public Shared ABW_USRNAME As String
    'Public Shared USRBER As Integer
    Public Shared USRID As Integer
    Public Shared USR_VNAME As String
    Public Shared USR_NNAME As String
    Public Shared USRKURZNAME As String
    Public Shared USR_HrFrNAME As String

    Public Shared FIRMA_ID As Integer
    Public Shared FIRMA_NAME As String

    Public Shared AUTO_ABF_NR As Boolean = True

    Public Shared BENUTZER_BERECHTIGUNGS_GRUPPEN As cBerechtigungenBenutzerGruppenZuordnung_List = Nothing
    Public Shared BENUTZER_BERECHTIGUNGS_GRUPPEN_WHEREIN_SDL As String = "('XXXXXXXX')"
    Public Shared PARAMS As cParameterList = Nothing
    Public Shared TRANSLATE As cTranslate_LIST = Nothing
    Public Shared TRANSLATE_AUTO As cTranslateAuto_LIST = Nothing
    Public Shared _LAN As String = "DE" '"EN"

    Public Shared SESSION As New cSession
    Public Shared _DummyCNT As Integer
    Public Shared EMailTOBB_Error As String = "info@verimextransit.com"
    Public Shared EMOJI_PATH As String = ""


    Shared SQL As New SQL
    Public Shared MITARBEITER As cMitarbeiter
    Public Shared FIRMEN As List(Of cFirmen)
    Public Shared FILIALEN As List(Of cFilialen)
    Public Const LeerDatum As Date = #12/30/1899#   'wird als leerer Datumswert verwendet, da sonst Probleme bei Null/Date

    Public Shared Sub LOAD_DATA(Optional switchCompany = False, Optional firmaTmp = False, Optional NLTmp = "")
        MITARBEITER = New cMitarbeiter
        If MITARBEITER.LOAD(USRID) Then
            USR_NNAME = MITARBEITER.mit_nname
            USR_VNAME = MITARBEITER.mit_vname
            _LAN = MITARBEITER.mit_sprache

            USR_HrFrNAME = ""
            Select Case MITARBEITER.mit_geschlecht
                Case "m" : USR_HrFrNAME = "Herr "
                Case "w" : USR_HrFrNAME = "Frau "
            End Select
            USR_HrFrNAME &= MITARBEITER.mit_nname


            If switchCompany Then
                FIRMA = firmaTmp
                NIEDERLASSUNG = If(NLTmp <> "", NLTmp, MITARBEITER.mit_niederlassung)
            Else
                If FIRMA = "" Then FIRMA = If(MITARBEITER.mit_firma = "BEIDE", "VERAG", MITARBEITER.mit_firma)

            End If



            cSPECIFIC.FIRMA_BgColor = Drawing.Color.FromArgb(0, 54, 128)
            Select Case FIRMA
                Case "VERAG"
                    CLUSTER = "FRONTOFFICE"
                Case "IMEX"
                    STAMMFILIALE = "5501"
                    CLUSTER = "FRONTOFFICE"
                Case "UNISPED"
                    STAMMFILIALE = "5601"
                    cSPECIFIC.FIRMA_BgColor = Drawing.Color.FromArgb(200, 0, 0)
                    CLUSTER = "FRONTOFFICE"
                Case "AMBAR"
                    STAMMFILIALE = "5701"
                    CLUSTER = "FRONTOFFICE"
                Case "ATILLA"
                    STAMMFILIALE = "4801"
                Case "FRONTOFFICE"
                    STAMMFILIALE = "5801"
                    CLUSTER = "FRONTOFFICE"

                Case "VERIMEX"
                    STAMMFILIALE = "6001"
                    CLUSTER = "FRONTOFFICE"
            End Select

            USRNAME = (MITARBEITER.mit_vname & " " & MITARBEITER.mit_nname)
            ABW_USRNAME = MITARBEITER.mit_abweichenderAnzeigename
            If NIEDERLASSUNG = "" Then NIEDERLASSUNG = MITARBEITER.mit_niederlassung
            If ABTEILUNG = "" Then ABTEILUNG = MITARBEITER.mit_abteilung
        Else
            MITARBEITER = Nothing
        End If


        LOAD_PROGRAMM()
        LOAD_PARAM()
        LOAD_TRANSLATE()
        LOAD_TRANSLATE_AUTO()
        LOAD_FIRMEN()
        LOAD_FILIALEN()
        LOAD_CHAT_EMOJIS()

        Try 'DRUCKER LADEN
            Dim sPrinters As New ArrayList
            For Each sPrinter As String In System.Drawing.Printing.PrinterSettings.InstalledPrinters
                sPrinters.Add(sPrinter)
            Next
            VERAG_PROG_ALLGEMEIN.cAllgemein.PRINTER_LIST = sPrinters
        Catch ex As Exception
        End Try

        If TESTSYSTEM Then
            AVISO_SERVER = "[DEVELOPER\DEVSQL]"
            FMZOLL_SERVER = "[DEVELOPER\DEVSQL]"
        End If
    End Sub

    Public Shared Sub LOAD_BER()
        BENUTZER_BERECHTIGUNGS_GRUPPEN = New cBerechtigungenBenutzerGruppenZuordnung_List(USRID)
        BENUTZER_BERECHTIGUNGS_GRUPPEN_WHEREIN_SDL = VERAG_PROG_ALLGEMEIN.cBerechtignunen.GetBenutzerBerechtigungsBezeichnungen_FormatWhereIn_SDL()
    End Sub

    Public Shared Sub LOAD_CHAT_EMOJIS()

        Dim EMOJI_SOURCEPATH = DATENVERVER_OPTIONS.getRootDir & "\DOKUMENTE\CHAT_EMOJIS\"
        Dim EMOJI_SOURCELIST = System.IO.Directory.GetFiles(EMOJI_SOURCEPATH).ToList

        EMOJI_PATH = System.IO.Path.GetFullPath(My.Application.Info.DirectoryPath & "\Resources\CHAT_EMOJIS\")

        Try
            If Not System.IO.Directory.Exists(EMOJI_PATH) Then
                System.IO.Directory.CreateDirectory(EMOJI_PATH)
            End If


            For Each f In System.IO.Directory.GetFiles(EMOJI_PATH)
                Dim fi As New System.IO.FileInfo(f)
                If Not System.IO.Directory.GetFiles(EMOJI_PATH).Any(Function(c) c.Equals(fi.Name, StringComparison.OrdinalIgnoreCase)) Then
                    System.IO.File.Delete(f)
                End If

            Next

            For Each f In System.IO.Directory.GetFiles(EMOJI_SOURCEPATH)
                Dim fi As New System.IO.FileInfo(f)
                fi.CopyTo(EMOJI_PATH & fi.Name, False)

            Next


        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try

    End Sub

    Public Shared Sub LOAD_PROGRAMM()
        Dim prog_Name = My.Application.Info.ProductName


        Select Case prog_Name.ToUpper
            Case "AVISO", "VERAG_PROG_ALLGEMEIN"
                PROGID = 16
            Case "SDL"
                PROGID = 7
            Case Else
                PROGID = SQL.getValueTxtBySql("SELECT isnull([prog_id],0)  FROM [tblProgramme] WHERe [prog_codename]='" & prog_Name & "'", "ADMIN")
        End Select
        PROGNAME = SQL.getValueTxtBySql("SELECT prog_name  FROM [tblProgramme] WHERe [prog_id]='" & PROGID & "'", "ADMIN")
    End Sub

    Public Shared Sub LOAD_PARAM()
        PARAMS = New cParameterList(PROGID, FIRMA)
    End Sub
    Public Shared Sub LOAD_FIRMEN()
        FIRMEN = cFirmen.LOAD_LIST
    End Sub
    Public Shared Sub LOAD_FILIALEN()
        FILIALEN = cFilialen.LOAD_LIST
    End Sub

    Public Shared Sub LOAD_TRANSLATE()
        TRANSLATE = New cTranslate_LIST(PROGID)
    End Sub
    Public Shared Sub LOAD_TRANSLATE_AUTO()
        TRANSLATE_AUTO = New cTranslateAuto_LIST()
    End Sub
    Public Shared Function GET_LAND() As String
        Select Case FIRMA
            Case "AMBAR" : Return "DE"
        End Select
        Return "AT"
    End Function

    Public Shared Function checkSteuerschluessel(firmenID As Integer, landKz As String, Optional UIDLandKz As String = "", Optional UstIdNr As String = "", Optional UstIdGeprüft As Object = Nothing) As Integer

        'Return -1

        If firmenID < 1 Then Return -1
        Dim FIRMA_TMP = New VERAG_PROG_ALLGEMEIN.cFirmen(firmenID)


        Dim isEU As Boolean = False
        Dim sql As New SQL

        If landKz Is Nothing OrElse landKz = "" Then Return -1



        Select Case FIRMA_TMP.Firma_LandKz

            Case "A", "AT"

                If landKz = "A" Then Return 2

                isEU = sql.getValueTxtBySql("select MitgliedslandEU from Währungstabelle  where landkz = '" & landKz & "'", "FMZOLL",)

                If isEU Then
                    If UIDLandKz <> "" AndAlso UstIdNr <> "" AndAlso UstIdGeprüft IsNot Nothing AndAlso CDate(UstIdGeprüft) > Today.AddDays(-30) Then
                        Return 10
                    Else
                        Return 2
                    End If
                Else
                    Return 0
                End If

            Case "D", "DE"

                If landKz = "D" Then Return 19

                isEU = sql.getValueTxtBySql("select MitgliedslandEU from Währungstabelle  where landkz = '" & landKz & "'", "FMZOLL",)

                If isEU Then
                    If UIDLandKz <> "" AndAlso UstIdNr <> "" AndAlso UstIdGeprüft IsNot Nothing AndAlso CDate(UstIdGeprüft) > Today.AddDays(-30) Then
                        Return 10
                    Else
                        Return 19
                    End If
                Else
                    Return 0
                End If

            Case "GB"

                If landKz = "GB" Then Return 20

                isEU = sql.getValueTxtBySql("select MitgliedslandEU from Währungstabelle  where landkz = '" & landKz & "'", "FMZOLL",)

                If isEU Then
                    If UIDLandKz <> "" AndAlso UstIdNr <> "" AndAlso UstIdGeprüft IsNot Nothing AndAlso CDate(UstIdGeprüft) > Today.AddDays(-30) Then
                        Return 10
                    Else
                        Return 20
                    End If
                Else
                    Return 0
                End If



                'Case "TR"

                '    If landKz = "TR" Then Return 20

                '    isEU = sql.getValueTxtBySql("select MitgliedslandEU from Währungstabelle where landkz = '" & landKz & "'", "FMZOLL",)

                '    If isEU Then
                '        If UIDLandKz <> "" AndAlso UstIdNr <> "" AndAlso UstIdGeprüft IsNot Nothing Then
                '            Return 0
                '        Else
                '            Return 20
                '        End If
                '    End If


        End Select

        Return -1

    End Function

    Public Shared Sub _TRANSLATE(o As System.Windows.Forms.Control)
        _TRANSLATE(o, o, _LAN)
    End Sub

    Public Shared Sub _TRANSLATE(o As System.Windows.Forms.Control, lan As String)
        _TRANSLATE(o, o, lan)
    End Sub
    Public Shared Function _getTRANSLATE_SUB(oMain As System.Windows.Forms.Control, o As System.Windows.Forms.Control, s As Object, Optional defaultStr As String = "") As String
        If defaultStr = "" Then defaultStr = s.Text
        If _LAN = "DE" Then Return defaultStr
        Dim TextTmp = VERAG_PROG_ALLGEMEIN.cAllgemein.TRANSLATE.list.FindAll(Function(x) x.trs_object = oMain.Name And x.trs_control = o.Name And (x.trs_subControl IsNot DBNull.Value AndAlso x.trs_subControl = s.Name) And x.trs_sprache = _LAN)
        If TextTmp IsNot Nothing And TextTmp.Count > 0 Then
            Return TextTmp(0).trs_text
        End If
        Return defaultStr
    End Function
    Public Shared Function _getTRANSLATE(oMain As System.Windows.Forms.Control, o As System.Windows.Forms.Control, Optional defaultStr As String = "") As String
        If defaultStr = "" Then defaultStr = o.Text
        Dim TextTmp = VERAG_PROG_ALLGEMEIN.cAllgemein.TRANSLATE.list.FindAll(Function(x) x.trs_object = oMain.Name And x.trs_control = o.Name And x.trs_sprache = _LAN)
        If TextTmp IsNot Nothing And TextTmp.Count > 0 Then
            Return TextTmp(0).trs_text
        End If
        Return defaultStr
    End Function
    Public Shared Sub _TRANSLATE(o As System.Windows.Forms.Control, s As System.Windows.Forms.Control, lan As String)
        If lan = "" Then Exit Sub
        If lan = "DE" Then Exit Sub
        'o... Form
        's... SubContainer
        'c... Control
        '  MsgBox(o.GetType.ToString & " - " & o.name)
        Dim TextTmp = VERAG_PROG_ALLGEMEIN.cAllgemein.TRANSLATE.list.FindAll(Function(x) x.trs_object = o.Name And x.trs_sprache = lan)
        If TextTmp IsNot Nothing Then
            For Each TXT As cTranslate In TextTmp
                Dim found = o.Controls.Find(TXT.trs_control, True)

                '   MsgBox(TXT.trs_control)
                If found IsNot Nothing AndAlso found.Count > 0 Then
                    '   MsgBox("JA")
                    Dim obj As System.Windows.Forms.Control = found(0)
                    If obj IsNot Nothing Then
                        If (TypeOf obj Is System.Windows.Forms.Label) Then
                            DirectCast(obj, System.Windows.Forms.Label).Text = TXT.trs_text
                        End If
                        If (TypeOf obj Is System.Windows.Forms.Button) Or (TypeOf obj Is VERAG_PROG_ALLGEMEIN.FlatButton) Then
                            DirectCast(obj, System.Windows.Forms.Button).Text = TXT.trs_text
                        End If
                        If (TypeOf obj Is System.Windows.Forms.CheckBox) Or (TypeOf obj Is VERAG_PROG_ALLGEMEIN.MyCheckbox) Then
                            DirectCast(obj, System.Windows.Forms.CheckBox).Text = TXT.trs_text
                        End If
                        If (TypeOf obj Is System.Windows.Forms.TabPage) Then
                            DirectCast(obj, System.Windows.Forms.TabPage).Text = TXT.trs_text
                        End If
                        If (TypeOf obj Is System.Windows.Forms.RadioButton) Or (TypeOf obj Is VERAG_PROG_ALLGEMEIN.MyRadioButton) Then
                            DirectCast(obj, System.Windows.Forms.RadioButton).Text = TXT.trs_text
                        End If
                        If (TypeOf obj Is System.Windows.Forms.GroupBox) Then
                            DirectCast(obj, System.Windows.Forms.GroupBox).Text = TXT.trs_text
                        End If

                        If (TypeOf obj Is System.Windows.Forms.MenuStrip) Then
                            For Each tb As System.Windows.Forms.ToolStripMenuItem In DirectCast(obj, System.Windows.Forms.MenuStrip).Items
                                If tb.Name = TXT.trs_subControl Then
                                    tb.Text = TXT.trs_text
                                End If
                            Next
                        End If

                        'If (TypeOf obj Is System.Windows.Forms.TabControl) Then -->in Tabpage geregelt
                        '    For Each tb As System.Windows.Forms.TabPage In DirectCast(obj, System.Windows.Forms.TabControl).TabPages
                        '        If tb.Name = TXT.trs_subControl Then
                        '            tb.Text = TXT.trs_text
                        '        End If
                        '    Next
                        'End If

                        If (TypeOf obj Is System.Windows.Forms.ComboBox) Or (TypeOf obj Is VERAG_PROG_ALLGEMEIN.MyComboBox) Then
                            'DirectCast(obj, System.Windows.Forms.Label).Text = TXT.trs_text

                        End If
                        'If (TypeOf obj Is VERAG_PROG_ALLGEMEIN.MyComboBox) Then

                        '    Dim cboItems = VERAG_PROG_ALLGEMEIN.cAllgemein.TRANSLATE.list.FindAll(Function(x) x.trs_object = o.Name And x.trs_sprache = lan And x.trs_control = TXT.trs_control)

                        '    For Each item As VERAG_PROG_ALLGEMEIN.MyListItem In DirectCast(obj, VERAG_PROG_ALLGEMEIN.MyComboBox).Items
                        '        For Each sitem As cTranslate In cboItems
                        '            If item.Text = sitem.trs_subControl Then
                        '                Dim index = DirectCast(obj, VERAG_PROG_ALLGEMEIN.MyComboBox).Items(0).Equals
                        '                DirectCast(DirectCast(obj, VERAG_PROG_ALLGEMEIN.MyComboBox).Items(item), VERAG_PROG_ALLGEMEIN.MyListItem).Text = sitem.trs_text
                        '            End If
                        '        Next
                        '    Next
                        'End If
                    End If
                End If
            Next
        End If

    End Sub


    Public Shared Function getMaxPosNrIncrement(FilialeNr As Integer, year As Integer) As Integer
        Dim cnt = 0
        Dim returnValue = -1
        Dim newMaxwert = CInt(year.ToString.Substring(2, 2) & "000000") '16000000
        Do
            Dim conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL
            Dim sql As String = " begin tran" &
                                " if exists (select * from tblPosNr with (updlock,serializable) WHERE FilialeNr = @FilialeNr AND  Jahr = @Jahr) " &
                                        " begin " &
                                        " UPDATE tblPosNr " &
                                        " SET wert=(select MAX(wert) FROM tblPosNr WHERE FilialeNr = @FilialeNr AND  Jahr = @Jahr)+1 " &
                                        " WHERE FilialeNr = @FilialeNr AND  Jahr = @Jahr " &
                                " End " &
                                " Else " &
                                " begin " &
                                    " INSERT INTO tblPosNr " &
                                        " (wert,FilialeNr,Jahr) " &
                                    " VALUES(@newMaxwert, @FilialeNr,@Jahr) " &
                                    " End " &
                                " commit tran " &
                                " SELECT isnull(wert,-1) FROM tblPosNr WHERE FilialeNr = @FilialeNr AND Jahr = @Jahr"
            Dim dr As SqlDataReader
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@FilialeNr", FilialeNr)
                cmd.Parameters.AddWithValue("@Jahr", year)
                cmd.Parameters.AddWithValue("@newMaxwert", newMaxwert)
                Try
                    dr = cmd.ExecuteReader()
                    If dr.Read Then returnValue = CInt(dr.Item(0))
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try

            End Using
            cnt += 1
        Loop While (SpedBuchExists(FilialeNr, returnValue) And cnt < 100000)
        ' conn.Close()

        Return returnValue
    End Function



    Public Shared Function isCLUSTER() As Boolean
        Return (FIRMA = CLUSTER)
    End Function

    Public Shared Function getFirmaFromFiliale2(FilialenNr As Object, Optional usedefaultFirma As Boolean = True, Optional defaultFIRMA As String = "VERAG") As String
        Dim FirmaTMP = ""
        If FilialenNr IsNot Nothing AndAlso IsNumeric(FilialenNr) Then
            ' FirmaTMP = SQL.getValueTxtBySql("SELECT TOP 1 [Firma] FROM [Filialen] WHERE  FilialenNr='" & FilialenNr & "' ", "FMZOLL")
            If FILIALEN Is Nothing Then LOAD_FILIALEN()
            Dim FILIALE = FILIALEN.Find(Function(x) x.FilialenNr = FilialenNr)
            If FILIALE IsNot Nothing Then FirmaTMP = If(FILIALE.Firma, "")
        End If
        If usedefaultFirma AndAlso FirmaTMP = "" Then FirmaTMP = defaultFIRMA
        Return FirmaTMP
    End Function

    Public Shared Function getClusterFromFiliale(FilialenNr As Object, Optional usedefaultCLUSTER As Boolean = True, Optional defaultCLUSTER As String = "VERAG") As String
        Dim FirmaTMP = ""
        If FilialenNr IsNot Nothing AndAlso IsNumeric(FilialenNr) Then
            ' FirmaTMP = SQL.getValueTxtBySql("SELECT TOP 1 [Firma] FROM [Filialen] WHERE  FilialenNr='" & FilialenNr & "' ", "FMZOLL")
            If FILIALEN Is Nothing Then LOAD_FILIALEN()
            Dim FILIALE = FILIALEN.Find(Function(x) x.FilialenNr = FilialenNr)
            If FILIALE IsNot Nothing Then FirmaTMP = If(FILIALE.Cluster, "")
        End If
        If usedefaultCLUSTER AndAlso FirmaTMP = "" Then FirmaTMP = defaultCLUSTER
        Return FirmaTMP
    End Function


    Public Shared Function getFirmaFromFiliale(FilialenNr As Object, Optional defaultVERAG As Boolean = True) As String
        Dim FirmaTMP = ""
        If FilialenNr IsNot Nothing AndAlso IsNumeric(FilialenNr) Then
            ' FirmaTMP = SQL.getValueTxtBySql("SELECT TOP 1 [Firma] FROM [Filialen] WHERE  FilialenNr='" & FilialenNr & "' AND ( Firma='" & FIRMA & "' OR Cluster='" & CLUSTER & "')", "FMZOLL")
            If FILIALEN Is Nothing Then LOAD_FILIALEN()
            Dim FILIALE = FILIALEN.Find(Function(x) x.FilialenNr = FilialenNr And (x.Firma = FIRMA Or x.Cluster = CLUSTER))
            If FILIALE IsNot Nothing Then FirmaTMP = If(FILIALE.Firma, "")
        End If
        If defaultVERAG AndAlso FirmaTMP = "" Then FirmaTMP = "VERAG"
        Return FirmaTMP
    End Function
    Public Shared Function getFirmaFromKdNr(KundenNr As Integer, Optional defaultVERAG As Boolean = True) As String
        Dim FirmaTMP = SQL.getValueTxtBySql("SELECT TOP 1 [Firma] FROM [Filialen] inner join Kunden ON Kunden.FilialenNr=Filialen.FilialenNr WHERE  KundenNr='" & KundenNr & "' ", "FMZOLL")
        If defaultVERAG AndAlso FirmaTMP = "" Then FirmaTMP = "VERAG"
        Return FirmaTMP
    End Function

    Public Shared Function SpedBuchExists(FilialenNr As Integer, AbfertigungsNr As Integer) As Boolean
        Return (CInt(SQL.getValueTxtBySql("SELECT count(*) FROM Speditionsbuch WHERE FilialenNr=" & FilialenNr & " AND AbfertigungsNr=" & AbfertigungsNr & " ", "FMZOLL")) > 0)

    End Function



    Class cSPECIFIC
        Public Shared FIRMA_BgColor As System.Drawing.Color = Drawing.Color.FromArgb(0, 54, 128)
        Public Shared FIRMA_ForeColor As System.Drawing.Color = Drawing.Color.White
    End Class




    Public Shared Sub buildConnectionDATENARCHIV()
        Exit Sub

        buildConnectionDATENARCHIV_NEW()
        '   buildConnectionDATENARCHIV_OLD()
    End Sub

    Public Shared Sub buildConnectionDATENARCHIV_OLD()
        Try
            Dim netuse As New System.Diagnostics.ProcessStartInfo()

            netuse.FileName = "C:\Windows\system32\net"
            ' netuse.Arguments = "use \\192.168.0.91\Datenarchiv /User:VERAGOST\AVISOSMB BmWr501956"
            If VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "UNISPED" Then
                netuse.Arguments = "use \\192.168.0.91\Datenarchiv /User:VERAGOST\AVISOSMB BmWr501956"
            Else
                netuse.Arguments = "use \\192.168.0.91\Datenarchiv /User:VERAGOST\AVISOSMB BmWr501956"
            End If


            netuse.CreateNoWindow = True
            netuse.WindowStyle = ProcessWindowStyle.Hidden
            System.Diagnostics.Process.Start(netuse)

        Catch ex As Exception
            MsgBox("ERR: DATENARCHIV Netzwerkzugriff fehlgeschlagen...")
        End Try
    End Sub
    Public Shared Sub buildConnectionDATENARCHIV_NEW()
        Try
            Dim netuse As New System.Diagnostics.ProcessStartInfo()

            netuse.FileName = "C:\Windows\system32\net"
            ' netuse.Arguments = "use \\datenarchiv.verag.ost.dmn\Datenarchiv /User:VERAGOST\AVISOSMB BmWr501956"
            If VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "UNISPED" Then
                netuse.Arguments = "use " & VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir(False) & " /User:VERAGOST\AVISOSMB BmWr501956"
            Else
                netuse.Arguments = "use " & VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getRootDir(False) & " /User:VERAGOST\AVISOSMB BmWr501956"
            End If


            netuse.CreateNoWindow = True
            netuse.WindowStyle = ProcessWindowStyle.Hidden
            System.Diagnostics.Process.Start(netuse)

        Catch ex As Exception
            MsgBox("ERR: DATENARCHIV Netzwerkzugriff fehlgeschlagen...")
        End Try
    End Sub
    Public Shared Function getSTAMMFILIALE() As String
        If IsNumeric(VERAG_PROG_ALLGEMEIN.cAllgemein.STAMMFILIALE) Then
            Return VERAG_PROG_ALLGEMEIN.cAllgemein.STAMMFILIALE
        Else
            Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
                Case "VERAG"
                    Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.NIEDERLASSUNG
                        Case "SUB" : Return 4803
                        Case "SBG" : Return 5003
                        Case "WAI" : Return 5103
                        Case "NKD" : Return 5303
                        Case "NEU" : Return 4819
                    End Select
                Case "IMEX" : Return 5501
                Case "UNISPED" : Return 5601
                Case "AMBAR" : Return 5701
                Case "ATILLA" : Return 4801
                Case "FRONTOFFICE" : Return 5801
            End Select
        End If
        Return ""
    End Function

    Public Shared Function ExtractPdfPages(ByVal SourceFile As String, ByVal TargetFile As String, pageStart As Integer, pagesEnd As Integer) As Boolean 'ITEXTSHARP
        Try
            Dim impPage As iTextSharp.text.pdf.PdfImportedPage = Nothing
            If SourceFile.Contains(" ") Then SourceFile = SourceFile
            Dim Reader As New iTextSharp.text.pdf.PdfReader(SourceFile)
            If Not TargetFile.ToLower.EndsWith(".pdf") Then TargetFile = TargetFile & ".pdf"
            Dim srcDoc As New iTextSharp.text.Document() 'Reader.GetPageSizeWithRotation(0))
            Dim PdfCopyProvider As New iTextSharp.text.pdf.PdfCopy(srcDoc, New FileStream(TargetFile, System.IO.FileMode.Create))
            PdfCopyProvider.SetFullCompression()
            srcDoc.Open()
            For p As Integer = pageStart To (pageStart + pagesEnd) - 1
                If p > Reader.NumberOfPages Then
                    MsgBox("Seitenanzahl überschritten!")
                    srcDoc.Close() : Reader.Close()
                    Return False
                End If
                impPage = PdfCopyProvider.GetImportedPage(Reader, p)
                ' ----- Ermitteln der Seitenauflösung und setzen für die neue Seite
                PdfCopyProvider.SetPageSize(New iTextSharp.text.Rectangle(0.0F, 0.0F, impPage.Width, impPage.Height, Reader.GetPageRotation(p)))
                ' ----- PDF Seite in das neue Dokument einfügen
                PdfCopyProvider.AddPage(impPage)

            Next
            ' ----- neues PDF Dokument und den Reader schließen
            srcDoc.Close()
            Reader.Close()
            Return True
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Return False
    End Function

End Class

Public Class cNCTS_GUARANTEES

    Public Shared TOBB() As String = {"17TR0004000032349", "20TR0004000069756", "13TR0004000009323", "21TR0004000097157", "12TR0004000004665", "15TR0004000020917",
        "17TR0004000029518", "20DE0000000078205", "19IT01TR000003323", "21TR0004000079552", "21TR0004000078157", "21TR0004000090364", "21TR0004000092350", "20TR0004000069557",
        "21TR0004000097564", "21TR0004000084967", "19TR0004000056358", "19TR0004000053175", "19TR0004000053356", "22TR0004000126963", "22TR0004000114169", "19TR0004000059957",
        "21TR0004000097555", "23TR0004000151181", "21TR0004000096368", "23TR0004000154183"}
    Public Shared OREGON() As String = {"18DE0000000073276", "19DE0000000075520", "18DE0000000075520", "15TR0004000020500", "ITTR0000000000153", "21DE0000000079716", "21DE0000000081910"}
    Public Shared VERAG() As String = {"17DE0000000071678", "05AT510000G000FP7"}
    Public Shared IMEX() As String = {"16AT520000G000VA6"}
    Public Shared ATILLA() As String = {"20AT520000G000092"}
    Public Shared SGS() As String = {"16DE0000000068410"}
    Public Shared MARS() As String = {"15TR0004000020908"}
    Public Shared EKOL() As String = {"13TR0004000005094"}



    'old: {"15TR0004000020917", "12TR0004000004665", "17TR0004000032349", "17TR0004000029518"f, "21TR0004000079552", "21TR00x04000078157", "21TR0004000092350", "20TR0004000069756", "21TR0004000090364", "21TR0004000097157"}

    'cboBRG.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("TOBB - 15TR0004000020917 / 12TR0004000004665 / 17TR0004000032349 / 17TR0004000029518 / 21TR0004000079552 / 21TR0004000078157 / 21TR0004000092350", " '12TR0004000004665','15TR0004000020917','17TR0004000032349','17TR0004000029518','21TR0004000079552','21TR0004000078157','21TR0004000092350'"))

    Public Shared Function getSQLList(arr() As String) As String
        getSQLList = ""
        For Each i In arr
            getSQLList &= "'" & i & "',"
        Next
        If getSQLList <> "" Then getSQLList = getSQLList.Substring(0, getSQLList.Length - 1)
    End Function
    Public Shared Function getSlashList(arr) As String
        getSlashList = ""
        For Each i In arr
            getSlashList &= "" & i & " / "
        Next
        If getSlashList <> "" Then getSlashList = getSlashList.Substring(0, getSlashList.Length - 3)
    End Function




End Class
