
Imports System.Globalization
Imports System.Linq.Expressions
Imports System.Net
Imports System.Text.RegularExpressions
Imports com.sun.tools.doclets.internal.toolkit.util
Imports Spire.Pdf.Graphics
Imports VERAG_PROG_ALLGEMEIN

Public Class usrcntlFiskaluebersicht

    Public kdNr As Integer = -1
    Dim SQL As New SQL
    Public KUNDE As VERAG_PROG_ALLGEMEIN.cKunde
    Public KUNDE_ERW As VERAG_PROG_ALLGEMEIN.cKundenErweitert
    Public ADRESSE As VERAG_PROG_ALLGEMEIN.cAdressen
    Dim fk As VERAG_PROG_ALLGEMEIN.cFiskalkunden
    Dim FirmaTmp = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
    Dim DS As VERAG_PROG_ALLGEMEIN.cDATENSERVER
    Dim BA As VERAG_PROG_ALLGEMEIN.cBonitaetsauskunft
    Dim UID As cUIDPruefung
    Dim EORI As cEORIPruefung
    Dim VM As cKundenVollmachten
    Dim UVM As cKundenVollmachten
    Dim year As Integer
    Dim daysWithinDocumentAreValid = 30
    Sub initDGVFiskaluebersicht()


        With dgvFiskaluebersicht
            .Columns.Clear()
            .DataSource = SQL.loadDgvBySql("SELECT FK_Id , FK_Datum, FK_Art, FK_locked, FK_Kdnr FROM tblFiskalkunden  WHERE FK_Kdnr='" & kdNr & "' ORDER BY FK_Datum ", "FMZOLL")

            .Columns("FK_Datum").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("FK_Datum").HeaderText = "Datum"
            .Columns("FK_Art").Visible = False
            .Columns("FK_locked").Visible = False
            .Columns("FK_Id").Visible = False
            .Columns("FK_Kdnr").Visible = False

        End With

    End Sub

    Sub initdgvLieferanten()

        Dim where As String = ""

        Select Case cbxFiskalart._value
            Case "DE"
                where = " And FilialenNr = 4811"
            Case "AT"
                where = " And FilialenNr = 4805"
        End Select


        Label2.Text = "Top 5 Lieferanten (" & year & ")"
        With dgvLieferanten
            .Columns.Clear()
            .DataSource() = SQL.loadDgvBySql("SELECT isnull(KdNr,'') as Kdnr, Firma, Anzahl
                                                    FROM    (SELECT [AbsenderKundenNr] as KdNr ,MAX([Absender]) as Firma, COUNT(*) as Anzahl
                                                    FROM [Speditionsbuch]
                                                WHERE year(Abfertigungsdatum) =  '" & year & "'" & where & " AND isnull([AbsenderKundenNr],'')<>'' AND " & kdNr & " In([AbsenderKundenNr], [EmpfängerKundenNr], [FrachtführerKundenNr], [VermittlerKundenNr], [EndempfängerKundenNr]) GROUP BY AbsenderKundenNr
                                                UNION ALL
                                                   SELECT MAX(AbsenderKundenNr)  as KdNr, [Absender] as Firma, COUNT(*) as Anzahl
                                                    FROM [Speditionsbuch]
                                                    WHERE year(Abfertigungsdatum) =  '" & year & "'" & where & " AND isnull([AbsenderKundenNr],'')='' AND " & kdNr & " In([AbsenderKundenNr], [EmpfängerKundenNr], [FrachtführerKundenNr], [VermittlerKundenNr], [EndempfängerKundenNr]) GROUP BY Absender) t
                                                    ORDER BY Anzahl desc", "FMZOLL")
            .Columns("KdNr").Width = 50
            .Columns("Firma").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("Anzahl").Width = 40


        End With

    End Sub

    Sub initdgvEmfpaenger()

        Dim where As String = ""

        Select Case cbxFiskalart._value
            Case "DE"
                where = " And FilialenNr = 4811"
            Case "AT"
                where = " And FilialenNr = 4805"
        End Select

        Label3.Text = "Top 5 Empfänger (" & year & ")"
        With dgvEmpfaenger
            .Columns.Clear()
            .DataSource() = SQL.loadDgvBySql("SELECT isnull(KdNr,'') as Kdnr, Firma, Anzahl
                                                    FROM    (SELECT [EmpfängerKundenNr] as KdNr ,MAX([Empfänger]) as Firma, COUNT(*) as Anzahl
                                                    FROM [Speditionsbuch]
                                                WHERE year(Abfertigungsdatum) =  '" & year & "'" & where & " AND isnull([EmpfängerKundenNr],'')<>'' AND " & kdNr & " In([AbsenderKundenNr], [EmpfängerKundenNr], [FrachtführerKundenNr], [VermittlerKundenNr], [EndempfängerKundenNr]) GROUP BY EmpfängerKundenNr
                                                UNION ALL
                                                   SELECT MAX(EmpfängerKundenNr)  as KdNr, [Empfänger] as Firma, COUNT(*) as Anzahl
                                                    FROM [Speditionsbuch]
                                                    WHERE year(Abfertigungsdatum) =  '" & year & "'" & where & " AND isnull([EmpfängerKundenNr],'')='' AND " & kdNr & " In([AbsenderKundenNr], [EmpfängerKundenNr], [FrachtführerKundenNr], [VermittlerKundenNr], [EndempfängerKundenNr]) GROUP BY Empfänger) t
                                                    ORDER BY Anzahl desc", "FMZOLL")
            '.DataSource() = SQL.loadDgvBySql("SELECT Top(5) isnull(cast([EmpfängerKundenNr]as nvarchar(7)) + ' ','')  as KdNr, CASE WHEN [EmpfängerKundenNr] IS NULL THEN '-' ELSE MAX(Empfänger) END as Firma, COUNT(*) as Anzahl FROM [Speditionsbuch]  WHERE year(Abfertigungsdatum) =  '" & year & "'" & where & "AND " & kdNr & " IN([AbsenderKundenNr],[EmpfängerKundenNr],[FrachtführerKundenNr],[VermittlerKundenNr],[EndempfängerKundenNr]) GROUP BY EmpfängerKundenNr, Empfänger ORDER BY Anzahl DESC ", "FMZOLL")
            .Columns("KdNr").Width = 50
            .Columns("Firma").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("Anzahl").Width = 40
        End With

    End Sub

    Sub initdgvZwischenhaendler()

        Dim where As String = ""

        Select Case cbxFiskalart._value
            Case "DE"
                where = " And FilialenNr = 4811"
            Case "AT"
                where = " And FilialenNr = 4805"
        End Select

        Label6.Text = "Top 5 Auftraggeber (" & year & ")"
        With dgvZwischenh
            .Columns.Clear()
            .DataSource() = SQL.loadDgvBySql("SELECT  isnull(KdNr,'') as Kdnr, Firma, Anzahl
                                                    FROM    (SELECT [VermittlerKundenNr] as KdNr ,MAX([Vermittler]) as Firma, COUNT(*) as Anzahl
                                                    FROM [Speditionsbuch]
                                                WHERE year(Abfertigungsdatum) =  '" & year & "'" & where & " AND isnull([VermittlerKundenNr],'')<>'' AND " & kdNr & " In([AbsenderKundenNr], [EmpfängerKundenNr], [FrachtführerKundenNr], [VermittlerKundenNr], [EndempfängerKundenNr]) GROUP BY VermittlerKundenNr
                                                UNION ALL
                                                   SELECT MAX(VermittlerKundenNr)  as KdNr, [Vermittler] as Firma, COUNT(*) as Anzahl
                                                    FROM [Speditionsbuch]
                                                    WHERE year(Abfertigungsdatum) =  '" & year & "'" & where & " AND isnull([VermittlerKundenNr],'')='' AND " & kdNr & " In([AbsenderKundenNr], [EmpfängerKundenNr], [FrachtführerKundenNr], [VermittlerKundenNr], [EndempfängerKundenNr]) GROUP BY Vermittler) t
                                                    ORDER BY Anzahl desc", "FMZOLL")
            '.DataSource() = SQL.loadDgvBySql("SELECT Top(5) isnull(cast([VermittlerKundenNr]as nvarchar(7)) + ' ','')  as KdNr, CASE WHEN [VermittlerKundenNr] IS NULL THEN '-' ELSE MAX(Vermittler) END as Firma, COUNT(*) as Anzahl FROM [Speditionsbuch]   WHERE year(Abfertigungsdatum) =  '" & year & "'" & where & " AND " & kdNr & " IN([AbsenderKundenNr],[EmpfängerKundenNr],[FrachtführerKundenNr],[VermittlerKundenNr],[EndempfängerKundenNr]) GROUP BY VermittlerKundenNr, Vermittler ORDER BY Anzahl DESC ", "FMZOLL")
            .Columns("KdNr").Width = 50
            .Columns("Firma").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("Anzahl").Width = 40

        End With

    End Sub


    Sub initDGVUnterlagen()

        With dgvUnterlagen
            .DataSource = SQL.loadDgvBySql("SELECT [fka_id],[fka_KdNr],[fka_Name],[fka_Datum] , [fka_Art], [fka_docId]  FROM [tblFiskalkundenAnhaenge] " &
                                            " WHERE [fka_KdNr]='" & fk.FK_Kdnr & "' AND fka_fkId='" & fk.FK_Id & "' ORDER BY fka_Datum desc ", "AVISO")
            .Columns("fka_id").Visible = False
            .Columns("fka_KdNr").Visible = False
            .Columns("fka_docId").Visible = False
            .Columns("fka_Name").HeaderText = "Name"
            .Columns("fka_Datum").HeaderText = "Datum"
            .Columns("fka_Art").HeaderText = "Doku-Art"
            .Columns("fka_Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            .ClearSelection()
            AddHandler .CellDoubleClick, Sub()
                                             If dgvUnterlagen.SelectedRows.Count > 0 Then
                                                 dgvUnterlagen.Cursor = Cursors.WaitCursor
                                                 Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(dgvUnterlagen.SelectedRows(0).Cells("fka_docId").Value)
                                                 DS.OPEN_SINGLE(True)
                                                 dgvUnterlagen.Cursor = Cursors.Default

                                             End If
                                         End Sub

        End With


    End Sub



    Sub init(_kdnr As Integer)

        cbxFiskalart.Items.Clear()
        cbxFiskalart.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AT", "AT"))
        cbxFiskalart.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DE", "DE"))
        cbxFiskalart.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))


        cbxAttachmentArt.Items.Clear()
        cbxAttachmentArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Passkopie", "Passkopie"))
        cbxAttachmentArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Gewerbeanmeldung", "GW-Anmeldung"))
        cbxAttachmentArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Handelsregister", "HR"))
        cbxAttachmentArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Sonstiges", "Sonstiges"))
        cbxAttachmentArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Website", "Website"))
        cbxAttachmentArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Website-Steuerberater", "Website-Steuerberater"))


        kdNr = _kdnr
        KUNDE = New VERAG_PROG_ALLGEMEIN.cKunde(kdNr)
        KUNDE_ERW = New VERAG_PROG_ALLGEMEIN.cKundenErweitert(kdNr)
        ADRESSE = New VERAG_PROG_ALLGEMEIN.cAdressen(kdNr)
        BA = VERAG_PROG_ALLGEMEIN.cBonitaetsauskunft.LOADByKdNrWithingDate(kdNr)
        lblUnterlagen.Text = "Unterlagen"

        For Each c As Control In MyPanel2.Controls
            If TypeOf (c) Is MyTextBox Then
                AddHandler CType(c, MyTextBox).TextChanged, AddressOf somethingChanged
            ElseIf TypeOf (c) Is TextBox Then
                AddHandler CType(c, TextBox).TextChanged, AddressOf somethingChanged
            End If
        Next

        'If ADRESSE.LandKz = "A" Or "AT" Then
        '    cbxFiskalart.changeItem("DE")
        'Else
        '    cbxFiskalart.changeItem("AT")
        'End If

        initDGVFiskaluebersicht()

    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

        If cbxFiskalart._value = "" Then
            lblWarning.Text = "Fiskalart auswählen!"
            Exit Sub
        ElseIf ADRESSE.LandKz = "" Then
            lblWarning.Text = "Land im Kunden hinterlegen!"
            Exit Sub
        End If

        If cbxFiskalart._value = "DE" And ADRESSE.LandKz = "D" Or cbxFiskalart._value = "AT" And ADRESSE.LandKz = "A" Then
            lblWarning.Text = "Fiskalart " & cbxFiskalart._value & " für " & ADRESSE.LandKz & " nicht möglich!"
            Exit Sub
        End If

        lblWarning.Text = ""

        Dim input = InputBox("Bitte geben Sie das Datum der neuen Fiskal-Übersicht ein: ", "Fiskal-Übersicht anlegen")
        If input <> "" Then
            Dim hint As String
            If checkDateIsValid(input, hint) Then
                fk = New VERAG_PROG_ALLGEMEIN.cFiskalkunden()
                fk.FK_Datum = CDate(input)
                fk.FK_Kdnr = kdNr
                fk.FK_Art = cbxFiskalart._value
                fk.SAVE()
                dgvFiskaluebersicht.GetOrder()
                initDGVFiskaluebersicht()
                dgvFiskaluebersicht.SetOrder()
            Else
                MsgBox(hint)
            End If

        Else
            MsgBox("Kein Datum angegeben!")
        End If


    End Sub

    Private Function checkDateIsValid(ByRef checkDate As Date, ByRef hint As String) As Boolean

        If Not IsDate(checkDate) Then
            hint = checkDate & " ist kein Datumswert"
            Return False
        End If

        For Each row As DataGridViewRow In dgvFiskaluebersicht.Rows
            If Not IsDBNull(row.Cells("FK_Datum").Value) Then
                If CDate(row.Cells("FK_Datum").Value) = CDate(checkDate) Then
                    hint = "Es existiert bereits ein Eintrag mit " & checkDate
                    Return False
                End If
            End If

        Next


        Return True

    End Function

    Private Sub btnPDFReport_Click(sender As Object, e As EventArgs) Handles btnPDFReport.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim listeUnterlagen As New List(Of String)
            If dgvUnterlagen.Rows.Count > 0 Then


                For Each row As DataGridViewRow In dgvUnterlagen.Rows
                    listeUnterlagen.Add(row.Cells("fka_Art").Value.ToString)
                Next
            End If


            Dim tmpPath = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPPath("Fiskalanalyse", ".pdf", , False)
            Dim rptFiskalkunden As New SDL.rptFiskalkunde(kdNr, fk.FK_Id, VM, BA, EORI, UID, UVM, fk.FK_VollmachtOriginal, listeUnterlagen)

            Dim p As New GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport
            rptFiskalkunden.Run(False)
            p.NeverEmbedFonts = ""
            p.Export(rptFiskalkunden.Document, tmpPath)

            Dim files As New List(Of String)
            Dim outputFile As String = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPPath("Merge.pdf", ".pdf", True,, "Merge")

            files.Add(tmpPath)


            If VM IsNot Nothing Then
                If VM.kdvm_datenarchivId > 0 Then files.Add(VERAG_PROG_ALLGEMEIN.cDATENSERVER.GET_PDFPath_BY_DocID(VM.kdvm_datenarchivId))
                If VM.kdvm_emailId > 0 Then

                    Dim MailFileVM = VERAG_PROG_ALLGEMEIN.cDATENSERVER.GET_PDFPath_BY_DocID(VM.kdvm_emailId)

                    If Not System.IO.File.Exists(MailFileVM) Then
                        MsgBox("Die Datei Vollmacht existiert nicht!")
                    Else
                        If MailFileVM.ToString.ToLower.EndsWith(".msg") Then ' MSG - Mails --> lokale Kopie, sonst Fehler  (bereits von Benutzer soUndSo geöffnet)
                            Dim docPathTMP = VERAG_PROG_ALLGEMEIN.cFormularManager.getTMPPath_MSG()
                            System.IO.File.Copy(MailFileVM, docPathTMP)
                            Process.Start(docPathTMP)
                        Else
                            Process.Start(MailFileVM)
                        End If
                    End If

                End If
            End If

            Dim MailFileUVM
            If UVM IsNot Nothing Then
                If UVM.kdvm_datenarchivId > 0 Then files.Add(VERAG_PROG_ALLGEMEIN.cDATENSERVER.GET_PDFPath_BY_DocID(UVM.kdvm_datenarchivId))
                If UVM.kdvm_emailId > 0 Then
                    MailFileUVM = VERAG_PROG_ALLGEMEIN.cDATENSERVER.GET_PDFPath_BY_DocID(UVM.kdvm_emailId)

                    If Not System.IO.File.Exists(MailFileUVM) Then
                        MsgBox("Die Datei Untervollmacht existiert nicht!")
                    Else
                        If MailFileUVM.ToString.ToLower.EndsWith(".msg") Then ' MSG - Mails --> lokale Kopie, sonst Fehler  (bereits von Benutzer soUndSo geöffnet)
                            Dim docPathTMP = VERAG_PROG_ALLGEMEIN.cFormularManager.getTMPPath_MSG()
                            System.IO.File.Copy(MailFileUVM, docPathTMP)
                            Process.Start(docPathTMP)
                        Else
                            Process.Start(MailFileUVM)
                        End If
                    End If


                End If
            End If
            If BA IsNot Nothing Then
                If BA.ba_datenarchivId > 0 Then files.Add(VERAG_PROG_ALLGEMEIN.cDATENSERVER.GET_PDFPath_BY_DocID(BA.ba_datenarchivId))
            End If
            For Each r As DataGridViewRow In dgvUnterlagen.Rows
                files.Add(VERAG_PROG_ALLGEMEIN.cDATENSERVER.GET_PDFPath_BY_DocID(r.Cells("fka_docId").Value))
            Next
            VERAG_PROG_ALLGEMEIN.cFormularManager.mergePDFs(files, outputFile)

            Process.Start(outputFile)
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        If vbYes = MsgBox("Möchten Sie den Fiskalakt vom " & dgvFiskaluebersicht.SelectedRows(0).Cells("FK_Datum").Value & " abschließen?" & vbNewLine & "Er kann anschließend nicht mehr verändert werden", vbYesNo) Then
            getFields()

            If checkEntries() Then
                fk.FK_locked = True
                fk.FK_Abschlussdatum = Now()
                fk.FK_MaId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
                fk.SAVE()
                initDGVFiskaluebersicht()
                btnOK.Enabled = False
                btnSave.Enabled = False
            End If

        End If
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        lblWarning.Text = ""
        getFields()
        If fk.SAVE() Then
            btnSave.Enabled = False
        Else
            btnSave.Enabled = True
        End If

    End Sub


    Private Sub dgvFiskaluebersicht_SelectionChanged(sender As Object, e As EventArgs) Handles dgvFiskaluebersicht.SelectionChanged

        clearTXT()

        If dgvFiskaluebersicht.SelectedRows.Count > 0 Then

            fk = New cFiskalkunden(dgvFiskaluebersicht.SelectedRows(0).Cells("FK_Id").Value)

            If checkNullStr(fk.FK_Datum) <> "" Then year = CDate(fk.FK_Datum).Year

            Dim locked As Boolean = IIf(fk.FK_locked Is Nothing, False, fk.FK_locked)
            setFields()
            initDGVUnterlagen()
            picLocked.Visible = locked
            btnOK.Enabled = Not locked
            btnSave.Enabled = Not locked
            setUIenabled(locked)
            Button4.Enabled = True
            setAbschlusstxt(locked)

            BA = VERAG_PROG_ALLGEMEIN.cBonitaetsauskunft.LOADByKdNrWithingDate(kdNr)
            setControlButtons()

        Else

            Button4.Enabled = False
            'clearTXT()
        End If

        'BA = VERAG_PROG_ALLGEMEIN.cBonitaetsauskunft.LOADByKdNrWithingDate(kdNr)
        'setControlButtons()


    End Sub

    Private Sub setFields()


        If checkNullStr(fk.FK_Vollmacht) <> "" Then
            VM = New cKundenVollmachten(fk.FK_Vollmacht)
            txtVM.Text = VM.kdvm_erhalten_Datum.ToShortDateString
        End If

        If checkNullStr(fk.FK_Untervollmacht) <> "" Then
            UVM = New cKundenVollmachten(fk.FK_Untervollmacht)
            txtUVM.Text = UVM.kdvm_erhalten_Datum.ToShortDateString
        End If


        If checkNullStr(fk.FK_EORI) <> "" Then
            EORI = New cEORIPruefung(fk.FK_EORI)
            txtEORI.Text = EORI.eori_datum.ToShortDateString
        End If

        If checkNullStr(fk.FK_UID) <> "" Then
            UID = New cUIDPruefung(fk.FK_UID)
            txtVM.Text = UID.uid_Datum.ToShortDateString
        End If


        If checkNullStr(fk.FK_Bonitaet) <> "" Then
            BA = New cBonitaetsauskunft(fk.FK_Bonitaet)
            txtBon.Text = BA.ba_Datum.ToShortDateString
        End If


        cbxFiskalart._value = checkNullStr(fk.FK_Art)
        txtHomepage.Text = checkNullStr(fk.FK_Website_URL)
        rtbAnmerkung.Text = checkNullStr(fk.FK_Anmerkung)
        rtfLieferanten.Text = checkNullStr(fk.FK_Lieferanten)
        rtfEmpfaenger.Text = checkNullStr(fk.FK_Empfaenger)
        rtfZwischenh.Text = checkNullStr(fk.FK_Zwischenhaendler)
        txtGruendungsjahr.Text = checkNullStr(fk.FK_Gruendungsdatum)
        txtGeschaeftszweck.Text = checkNullStr(fk.FK_Geschaeftszweck)
        txtKredit.Text = checkNullStr(fk.FK_KreditMax)
        txtWaren.Text = checkNullStr(fk.FK_Waren)
        txtAnzMA.Text = checkNullStr(fk.FK_AnzahlMitarbeiter)
        txtGF.Text = checkNullStr(fk.FK_Geschaeftsfuehrer)
        txtWebsiteSt.Text = checkNullStr(fk.FK_WebsiteSt_URL)
        cbxVMOriginal.Checked = checkNullStr(fk.FK_VollmachtOriginal)
        txtSteuerberater.Text = checkNullStr(fk.FK_Steuerberater)
        txtUmsatz.Text = checkNullStr(fk.FK_Umsatz)

        If txtBon.Text = "" Then
            Dim baid = SQL.getValueTxtBySql("SELECT TOP(1) ba_id FROM [tblBonitaetsauskunft] where ba_KundenNr=" & kdNr & " ORDER BY ba_Datum DESC", "FMZOLL")
            If baid <> "" Then BA = New cBonitaetsauskunft(baid)
            If BA IsNot Nothing Then

                txtBon.Text = BA.ba_Datum.ToShortDateString

                If txtGruendungsjahr.Text = "" Then txtGruendungsjahr.Text = BA.ba_GruendundsDatum

                If txtKredit.Text = "" Then txtKredit.Text = BA.ba_Hoechstkredit

                lblUmsatz.Text = IIf(BA.ba_Umsatz <> "", BA.ba_Umsatz, "")

                If txtGF.Text = "" Then txtGF.Text = BA.ba_GFName

                If txtGeschaeftszweck.Text = "" Then txtGeschaeftszweck.Text = BA.ba_Geschaeftszweck

                If txtAnzMA.Text = "" Then txtAnzMA.Text = BA.ba_Mitarbeiter

                If ADRESSE.LandKz = "AT" Or ADRESSE.LandKz = "A" Then
                    lblBon.Text = If(BA.ba_CreditSaveBonitaetsScore, "")
                Else
                    lblBon.Text = If(BA.ba_CreditSaveBonitaetsIndex, "")
                End If
            End If
        End If

        If txtWaren.Text = "" Then
            txtWaren.Text = SQL.getValueTxtBySql("SELECT Top(1) [Packstücke] as Bemerkungen FROM [Speditionsbuch] WHERE " & kdNr & "  In([AbsenderKundenNr], [EmpfängerKundenNr], [FrachtführerKundenNr], [VermittlerKundenNr], [EndempfängerKundenNr]) AND ISNULL(CAST([Bemerkungen] as NVARCHAR(max)),'') <> '' Order by Abfertigungsdatum desc", "FMZOLL")
        End If

        If txtUID.Text = "" Then

            Dim UIDId = SQL.getValueTxtBySql("Select TOP(1) [uid_id] FROM [tblUIDPruefung] where uid_KundenNr='" & kdNr & "' AND UID_valid = 1 AND uid_stufe = 2 ORDER BY uid_Datum desc", "FMZOLL")

            If UIDId <> "" Then

                UID = New cUIDPruefung(UIDId)
                If UID IsNot Nothing And IsDate(UID.uid_Datum) Then
                    txtUID.Text = CDate(UID.uid_Datum).ToShortDateString
                End If
            End If

        End If

        If txtVM.Text = "" And cbxFiskalart._value <> "" Then

            Dim vmArt As Integer = -1

            Select Case FirmaTmp
                Case "VERAG"
                    If cbxFiskalart._value = "DE" Then
                        vmArt = 2
                    ElseIf cbxFiskalart._value = "AT" Then
                        vmArt = 4
                    End If

                Case "IMEX"
                    If cbxFiskalart._value = "DE" Then
                        vmArt = 8
                    ElseIf cbxFiskalart._value = "AT" Then
                        vmArt = 9
                    End If

                Case "UNISPED"
                    If cbxFiskalart._value = "DE" Then
                        vmArt = 22
                    ElseIf cbxFiskalart._value = "AT" Then
                        vmArt = 23
                    End If

            End Select

            If vmArt = -1 Then
                lblWarning.Text = "Vollmachtart für " & FirmaTmp & " kann nicht ermittelt werden"
            Else
                Dim vmId = SQL.getValueTxtBySql("SELECT Top(1) abf.[kdvm_id] FROM [tblKundenVollmachtenArt] as art INNER  Join(select * from [tblKundenVollmachten]) as abf on art.[kdvma_Id]=abf.[kdvm_kdvmaId] WHERE art.kdvma_visible = 1 And abf.kdvm_erhalten = 1 And ISNULL(abf.[kdvm_geloescht], 0)!= 1 And [kdvm_KundenNr] ='" & kdNr & "' AND [kdvm_kdvmaId]='" & vmArt & "' Order BY abf.kdvm_erhalten_Datum desc", "FMZOLL")

                If vmId <> "" Then
                    VM = New cKundenVollmachten(vmId)
                    If IsDate(VM.kdvm_erhalten_Datum) Then txtVM.Text = CDate(VM.kdvm_erhalten_Datum).ToShortDateString
                End If

            End If
        End If

        If txtUVM.Text = "" And cbxFiskalart._value <> "" Then
            Dim uvmId = SQL.getValueTxtBySql("SELECT Top(1) abf.[kdvm_id] FROM [tblKundenVollmachtenArt] as art INNER  Join(select * from [tblKundenVollmachten]) as abf on art.[kdvma_Id]=abf.[kdvm_kdvmaId] WHERE art.kdvma_visible = 1 And abf.kdvm_erhalten = 1 And ISNULL(abf.[kdvm_geloescht], 0)!= 1 And [kdvm_KundenNr] ='" & kdNr & "' AND [kdvm_kdvmaId]=6 Order BY abf.kdvm_erhalten_Datum desc", "FMZOLL")

            If uvmId <> "" Then
                UVM = New cKundenVollmachten(uvmId)
                If IsDate(UVM.kdvm_erhalten_Datum) Then txtUVM.Text = CDate(UVM.kdvm_erhalten_Datum).ToShortDateString
            End If

        End If




        If txtHomepage.Text <> "" Then
            cbxHomepage.Checked = IsValidURL(txtHomepage.Text)
        Else
            cbxHomepage.Checked = False
            picOK.Visible = False
        End If


        If txtWebsiteSt.Text = "" Then picOKSt.Visible = False

        If checkNullStr(KUNDE.EORITIN) <> "" Then

            EORI = cEORIPruefung.LOADByKdNrDate(kdNr)
            If EORI IsNot Nothing Then
                txtEORI.Text = CDate(EORI.eori_datum).ToShortDateString
                lblWarning.Text = ""
            End If

        Else
            txtEORI.Text = ""
            'lblWarning.Text = "EORI-NR im Kunden fehlt!"
        End If


        If rtfEmpfaenger.Text = "" Then
            rtfEmpfaenger.Visible = False
            initdgvEmfpaenger()
        Else
            rtfEmpfaenger.Visible = True

        End If

        If rtfLieferanten.Text = "" Then
            rtfLieferanten.Visible = False
            initdgvLieferanten()
        Else
            rtfLieferanten.Visible = True
        End If


        If rtfZwischenh.Text = "" Then
            rtfZwischenh.Visible = False
            initdgvZwischenhaendler()
        Else
            rtfZwischenh.Visible = True
        End If


    End Sub

    Private Sub getFields()
        fk.FK_Art = cbxFiskalart._value

        If BA IsNot Nothing Then fk.FK_Bonitaet = BA.ba_id

        If VM IsNot Nothing Then fk.FK_Vollmacht = VM.kdvm_Id

        If UVM IsNot Nothing Then fk.FK_Untervollmacht = UVM.kdvm_Id

        If EORI IsNot Nothing Then fk.FK_EORI = EORI.eori_id

        If UID IsNot Nothing Then fk.FK_UID = UID.uid_id

        fk.FK_Website_URL = txtHomepage.Text
        fk.FK_WebsiteSt_URL = txtWebsiteSt.Text
        fk.FK_Anmerkung = rtbAnmerkung.Text
        fk.FK_VollmachtOriginal = cbxVMOriginal.Checked

        If txtKredit.Text <> "" Then
            fk.FK_KreditMax = Double.Parse(txtKredit.Text)
        End If

        If txtUmsatz.Text <> "" Then
            fk.FK_Umsatz = Double.Parse(txtUmsatz.Text)
        End If

        fk.FK_Geschaeftsfuehrer = txtGF.Text
        fk.FK_Geschaeftszweck = txtGeschaeftszweck.Text
        fk.FK_AnzahlMitarbeiter = txtAnzMA.Text
        fk.FK_Steuerberater = txtSteuerberater.Text
        fk.FK_Gruendungsdatum = txtGruendungsjahr.Text
        fk.FK_Waren = txtWaren.Text

        If checkNullStr(fk.FK_Empfaenger) = "" Then
            Dim txtEmpfaenger As String
            For Each row As DataGridViewRow In dgvEmpfaenger.Rows
                For i As Integer = 0 To row.Cells.Count - 1
                    txtEmpfaenger &= row.Cells(i).Value.ToString & ", "
                Next
                fk.FK_Empfaenger &= txtEmpfaenger
                If row.Index < dgvEmpfaenger.Rows.Count Then
                    fk.FK_Empfaenger &= vbNewLine
                End If

            Next
        End If



        If checkNullStr(fk.FK_Lieferanten) = "" Then
            Dim txtLieferanten As String
            For Each row As DataGridViewRow In dgvLieferanten.Rows
                For i As Integer = 0 To row.Cells.Count - 1
                    txtLieferanten &= row.Cells(i).Value.ToString & ", "
                Next
                fk.FK_Lieferanten &= txtLieferanten
                If row.Index < dgvLieferanten.Rows.Count Then
                    fk.FK_Lieferanten &= vbNewLine
                End If

            Next
        End If

        If checkNullStr(fk.FK_Zwischenhaendler) = "" Then
            Dim txtZwischenhaendler As String
            For Each row As DataGridViewRow In dgvZwischenh.Rows
                For i As Integer = 0 To row.Cells.Count - 1
                    txtZwischenhaendler &= row.Cells(i).Value.ToString & ", "
                Next
                fk.FK_Zwischenhaendler &= txtZwischenhaendler
                If row.Index < dgvZwischenh.Rows.Count Then
                    fk.FK_Zwischenhaendler &= vbNewLine
                End If

            Next
        End If




    End Sub

    Private Sub txtBon_TextChanged(sender As Object, e As EventArgs) Handles txtBon.TextChanged
        If txtBon._value <> "" Then
            Dim a As Date = Date.ParseExact(txtBon._value, "dd.MM.yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
            If a > fk.FK_Datum.AddDays(-daysWithinDocumentAreValid) Then
                cbxBonitaet.Checked = True
                txtBon.ForeColor = Color.Black
            Else
                cbxBonitaet.Checked = False
                txtBon.ForeColor = Color.Red
            End If
        Else
            cbxBonitaet.Checked = False
        End If

    End Sub

    Private Sub txtVM_TextChanged(sender As Object, e As EventArgs) Handles txtVM.TextChanged
        If txtVM._value <> "" Then
            Dim a As Date = Date.ParseExact(txtVM._value, "dd.MM.yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
            If a > fk.FK_Datum.AddYears(-2) Then 'Vollmacahten sollten grundsätzlich 2 Jahre ab Austellung gültig sein!
                cbxVollmacht.Checked = True
                txtVM.ForeColor = Color.Black
            Else
                cbxVollmacht.Checked = False
                txtVM.ForeColor = Color.Red
            End If
        Else
            cbxVollmacht.Checked = False
        End If
    End Sub

    Private Sub txtUVM_TextChanged(sender As Object, e As EventArgs) Handles txtUVM.TextChanged
        If txtUVM._value <> "" Then
            Dim a As Date = Date.ParseExact(txtUVM._value, "dd.MM.yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
            If a > fk.FK_Datum.AddDays(-daysWithinDocumentAreValid) Then
                cbxUntervm.Checked = True
                txtUVM.ForeColor = Color.Black
            Else
                cbxUntervm.Checked = False
                txtUVM.ForeColor = Color.Red
            End If
        Else
            cbxUntervm.Checked = False
        End If
    End Sub


    Private Sub txtUID_TextChanged(sender As Object, e As EventArgs) Handles txtUID.TextChanged
        If txtUID._value <> "" Then
            Dim a As Date = Date.ParseExact(txtUID._value, "dd.MM.yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
            If a > fk.FK_Datum.AddDays(-daysWithinDocumentAreValid) Then
                cbxUID.Checked = True
                txtUID.ForeColor = Color.Black
            Else
                cbxUID.Checked = False
                txtUID.ForeColor = Color.Red
            End If
        Else
            cbxUID.Checked = False
        End If
    End Sub

    Private Sub txtEORI_TextChanged(sender As Object, e As EventArgs) Handles txtEORI.TextChanged
        If txtEORI._value <> "" Then
            Dim a As Date = Date.ParseExact(txtEORI._value, "dd.MM.yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
            If a > fk.FK_Datum.AddDays(-daysWithinDocumentAreValid) Then
                cbxEORI.Checked = True
                txtEORI.ForeColor = Color.Black
            Else
                cbxEORI.Checked = False
                txtEORI.ForeColor = Color.Black
            End If
        Else
            cbxEORI.Checked = False
        End If
    End Sub

    Private Sub txtHomepage_TextChanged(sender As Object, e As EventArgs) Handles txtHomepage.TextChanged
        Dim valid
        If txtHomepage.Text <> "" Then
            If fk Is Nothing Then
                valid = IsValidURL(txtHomepage.Text)
                cbxHomepage.Checked = valid
                picOK.Visible = valid
            Else
                If fk.FK_Website_URL <> txtHomepage.Text Then
                    valid = IsValidURL(txtHomepage.Text)
                    cbxHomepage.Checked = valid
                    picOK.Visible = valid
                End If

            End If

        Else
            cbxHomepage.Checked = False
            picOK.Visible = False

        End If

    End Sub


    Function checkNullStr(o As Object) As String
        If Not IsDBNull(o) Then
            Return o
        End If
        Return ""
    End Function

    Private Function checkEntries() As Boolean

        If Not cbxBonitaet.Checked Then
            lblWarning.Text = "Bonität prüfen!"
            Return False
        End If

        If Not cbxHomepage.Checked Then
            lblWarning.Text = "Homepage prüfen!"
            Return False
        End If

        If Not cbxVollmacht.Checked Then
            lblWarning.Text = "Vollmacht prüfen!"
            Return False
        End If

        If Not cbxUID.Checked Then
            lblWarning.Text = "UID prüfen!"
            Return False
        End If

        If Not cbxEORI.Checked Then
            lblWarning.Text = "EORI-Nr prüfen!"
            Return False
        End If


        lblWarning.Text = ""
        Return True



    End Function

    Private Sub usrcntlFiskaluebersicht_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        setUIenabled(Not dgvFiskaluebersicht.SelectedRows.Count > 0)
        setControlButtons()

    End Sub

    Private Sub setUIenabled(locked As Boolean)

        btnSave.Enabled = Not locked
        btnOK.Enabled = Not locked


        For Each c As Control In MyPanel2.Controls
            If (Not c.GetType.FullName = "System.Windows.Forms.CheckBox" Or c.Name = "cbxVMOriginal") Then c.Enabled = Not locked
        Next



    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If dgvFiskaluebersicht.SelectedRows.Count > 0 Then
            If fk.FK_locked Then
                MsgBox("Fiskalübersicht ist bereits abgeschlossen und kann nicht gelöscht werden!")
                Exit Sub
            End If
            If vbYes = MsgBox("Möchten Sie die Fiskalübersicht vom  " & dgvFiskaluebersicht.SelectedRows(0).Cells("fk_datum").Value & " inkl. Anhänge wirklich löschen?", vbYesNo) Then
                Dim anh_list = New cFiskalkundenAnhaenge()
                anh_list.DELETEALL(dgvFiskaluebersicht.SelectedRows(0).Cells("fk_Kdnr").Value, dgvFiskaluebersicht.SelectedRows(0).Cells("fk_Id").Value)
                fk.deleteEntry(fk.FK_Id)
                'If dgvFiskaluebersicht.Rows.Count > 1 Then
                '    initDGVFiskaluebersicht()
                'ElseIf dgvFiskaluebersicht.Rows.Count = 1 Then
                '    init(kdNr)
                '    initDGVFiskaluebersicht()
                'End If

                init(kdNr)
                initDGVFiskaluebersicht()
            End If
        End If

    End Sub

    Public Sub clearTXT()
        Dim a As New List(Of Windows.Forms.Control)
        For Each c As Control In MyPanel2.Controls
            If (c.GetType.FullName = "System.Windows.Forms.TextBox" Or c.GetType.FullName = "VERAG_PROG_ALLGEMEIN.MyTextBox" Or c.GetType.FullName = "System.Windows.Forms.RichTextBox") Then c.Text = ""
            If c.GetType.FullName = "VERAG_PROG_ALLGEMEIN.MyDatagridview" Then
                Dim dgv As MyDatagridview = c
                dgv = DirectCast(dgv, MyDatagridview)
                dgv.Columns.Clear()

            End If
        Next
    End Sub

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

        Me.Cursor = Cursors.WaitCursor
        Dim url As String = ""
        lblWarning.Text = ""
        txtHomepage.ForeColor = Color.Black


        If fk.FK_Website_URL = txtHomepage.Text Then
            Dim anh_list = New cFiskalkundenAnhaenge().LOAD_LIST_ByKdNr(fk.FK_Kdnr, fk.FK_Id, "Website")
            If anh_list.Count > 0 Then
                DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER(anh_list.ElementAt(0).fka_docId)
                DS.OPEN_SINGLE()
                Me.Cursor = Cursors.Default
                Exit Sub
            End If

        End If

        If txtHomepage.Text = "" Then
            picOK.Visible = False
            Me.Cursor = Cursors.Default
            Exit Sub
        End If

        If Not IsValidURL(txtHomepage.Text) Then Exit Sub

        If Not checkWebsite(txtHomepage.Text) Then Exit Sub

        Dim pdf = VERAG_PROG_ALLGEMEIN.cFormularManager.getPDFViaSpirePDF_FromURL(txtHomepage.Text, DATENVERVER_OPTIONS.TMP_PATH & Guid.NewGuid().ToString() & ".pdf") 'UU langsam....

        DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "FISKALKUNDENANHAENGE", fk.FK_Datum, "", "", "Website", kdNr)
        Dim fi As New System.IO.FileInfo(pdf)
        Dim path = DS.uploadDataToDATENSERVER(pdf, fi.Name, fi.Extension)

        Dim ANH = New VERAG_PROG_ALLGEMEIN.cFiskalkundenAnhaenge()
        ANH.fka_Art = "Website"
        ANH.fka_docId = DS.da_id
        ANH.fka_Datum = Now()
        ANH.fka_KdNr = kdNr
        ANH.fka_fkId = fk.FK_Id
        ANH.fka_Name = "Website"
        ANH.SAVE()

        'If path Then
        '    fk.FK_Homepage_PDF = DS.da_id
        'End If

        fk.FK_Website_URL = txtHomepage.Text
        fk.SAVE()

        If Not cbxHomepage.Checked Then cbxHomepage.Checked = True

        initDGVUnterlagen()
        Me.Cursor = Cursors.Default

    End Sub

    Private Function IsValidURL(ByVal URL As String) As Boolean
        Dim Pattern As String = "^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$"
        Dim Rgx As Regex = New Regex(Pattern, RegexOptions.Compiled Or RegexOptions.IgnoreCase)

        Dim isValid = Rgx.IsMatch(URL)

        Return isValid

    End Function


    Private Sub dgvAnhaenge_KeyUp(sender As Object, e As KeyEventArgs) Handles dgvUnterlagen.KeyUp
        If e.KeyCode = Keys.Delete Then
            If dgvUnterlagen.SelectedRows.Count > 0 Then
                If vbYes = MsgBox("Möchten Sie die Datei wirklich löschen?", vbYesNoCancel) Then
                    Dim anhId = dgvUnterlagen.SelectedRows(0).Cells("fka_id").Value
                    Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(dgvUnterlagen.SelectedRows(0).Cells("fka_docId").Value)
                    If DS.DELETE_COMPLETE() Then
                        Dim anh As New cFiskalkundenAnhaenge(anhId)
                        anh.DELETE()
                        initDGVUnterlagen()
                    End If




                End If
            End If
        End If
    End Sub


    Private Sub btnAddAttachments_Click(sender As Object, e As EventArgs) Handles btnAddAttachments.Click

        If cbxAttachmentArt._value = "" Then Exit Sub
        Dim ArtId As String = cbxAttachmentArt._value


        Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "FISKALKUNDENANHAENGE", fk.FK_Datum, "", "", ArtId, kdNr)
        If DS.uploadDataToDATENSERVERFileDialog(, ".pdf", , "PDF") Then

            Dim ANH = New VERAG_PROG_ALLGEMEIN.cFiskalkundenAnhaenge()
            ANH.fka_Art = ArtId
            ANH.fka_docId = DS.da_id
            ANH.fka_Datum = Now()
            ANH.fka_KdNr = kdNr
            ANH.fka_fkId = fk.FK_Id
            ANH.fka_Name = DS.da_name
            ANH.SAVE()
            initDGVUnterlagen()
        End If

    End Sub



    Private Sub cbxAttachmentArt_SelectedValueChanged(sender As Object, e As EventArgs) Handles cbxAttachmentArt.SelectedValueChanged
        btnAddAttachments.Enabled = cbxAttachmentArt._value <> ""
    End Sub

    Private Sub setAbschlusstxt(locked As Boolean)

        If locked Then
            Dim mit = New VERAG_PROG_ALLGEMEIN.cMitarbeiter(fk.FK_MaId)
            lblAbschluss.Text = mit.Fullname

            If IsDate(fk.FK_Abschlussdatum) Then
                'Dim abschlussZP As DateTime
                'abschlussZP = DateTime.ParseExact(fk.FK_Abschlussdatum, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture)
                lblAbschluss.Text &= " " & fk.FK_Abschlussdatum
            End If

        Else
            lblAbschluss.Text = ""
        End If
    End Sub

    Private Sub panelChanged()

        For Each c As Control In MyPanel1.Controls
            If TypeOf (c) Is MyTextBox Then
                AddHandler CType(c, MyTextBox).TextChanged, AddressOf somethingChanged
            ElseIf TypeOf (c) Is TextBox Then
                AddHandler CType(c, TextBox).TextChanged, AddressOf somethingChanged
            ElseIf TypeOf (c) Is RichTextBox Then
                AddHandler CType(c, RichTextBox).TextChanged, AddressOf somethingChanged
            End If
        Next
    End Sub

    Private Sub somethingChanged()
        If fk.FK_locked Then
            btnSave.Enabled = False
            btnOK.Enabled = False
            Exit Sub
        End If
        If Not btnSave.Enabled Then btnSave.Enabled = True
    End Sub

    Private Sub setControlButtons()

        If fk IsNot Nothing Then

            If fk.FK_locked Then
                btnSave.Enabled = False
                btnOK.Enabled = False
                Exit Sub
            End If
        End If


        For Each c As Control In MyPanel1.Controls

            c.Enabled = dgvFiskaluebersicht.SelectedRows.Count > 0

        Next
    End Sub

    Private Sub txtSteuerberaterWS_TextChanged(sender As Object, e As EventArgs) Handles txtWebsiteSt.TextChanged
        If txtWebsiteSt.Text <> "" Then
            If fk Is Nothing Then
                picOKSt.Visible = IsValidURL(txtWebsiteSt.Text)
            Else
                If fk.FK_WebsiteSt_URL <> txtWebsiteSt.Text Then
                    picOKSt.Visible = IsValidURL(txtWebsiteSt.Text)
                End If

            End If

        Else
            picOKSt.Visible = False

        End If
    End Sub

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click

        Dim url As String = ""
        lblWarning.Text = ""
        txtWebsiteSt.ForeColor = Color.Black


        If fk.FK_WebsiteSt_URL = txtWebsiteSt.Text Then
            Dim anh_list = New cFiskalkundenAnhaenge().LOAD_LIST_ByKdNr(fk.FK_Kdnr, fk.FK_Id, "Website-Steuerberater")
            If anh_list.Count > 0 Then
                DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER(anh_list.ElementAt(0).fka_docId)
                DS.OPEN_SINGLE()
                Exit Sub
            End If

        End If

        If txtWebsiteSt.Text = "" Then
            picOKSt.Visible = False
            Exit Sub
        End If

        If Not IsValidURL(txtWebsiteSt.Text) Then Exit Sub

        If Not checkWebsite(txtWebsiteSt.Text) Then Exit Sub

        Dim pdf = VERAG_PROG_ALLGEMEIN.cFormularManager.getPDFViaSpirePDF_FromURL(txtWebsiteSt.Text) 'UU langsam....

        DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "FISKALKUNDENANHAENGE", fk.FK_Datum, "", "", "Website-Steuerberater", kdNr)
        Dim fi As New System.IO.FileInfo(pdf)
        Dim path = DS.uploadDataToDATENSERVER(pdf, fi.Name, fi.Extension)

        Dim ANH = New VERAG_PROG_ALLGEMEIN.cFiskalkundenAnhaenge()
        ANH.fka_Art = "Website-Steuerberater"
        ANH.fka_docId = DS.da_id
        ANH.fka_Datum = Now()
        ANH.fka_KdNr = kdNr
        ANH.fka_fkId = fk.FK_Id
        ANH.fka_Name = "Website-Steuerberater"
        ANH.SAVE()


        fk.FK_WebsiteSt_URL = txtWebsiteSt.Text
        fk.SAVE()

        initDGVUnterlagen()

    End Sub

    Private Function checkWebsite(url As String) As Boolean

        Dim req As System.Net.HttpWebRequest
        Dim res As System.Net.HttpWebResponse

        Try
            url = txtHomepage.Text
            req = System.Net.HttpWebRequest.Create(url)
            req.Timeout = 5000
            req.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
            res = req.GetResponse()
        Catch ex As WebException
            lblWarning.Text = ex.Message
            txtHomepage.ForeColor = Color.Red
            picOK.Visible = False
            Me.Cursor = Cursors.Default
            Return False
        End Try

        Return True

    End Function


End Class
