Public Class frmKundenSyska_DatenÜbertragen

    Dim Sql As New VERAG_PROG_ALLGEMEIN.SQL
    Dim AD As VERAG_PROG_ALLGEMEIN.cAdressen
    Dim KD As VERAG_PROG_ALLGEMEIN.cKunde

    Dim KundenNr As Integer = -1

    Sub New(KundenNr)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()
        Me.KundenNr = KundenNr
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Image = Nothing
        If cboFirma._value = "" Then MsgBox("Keine Fimra!") : Exit Sub

        Dim fehler = False
        If vbYes = MsgBox("Möchten Sie den Eintrag in der SYSKA-Datenbank überschreiben?", vbYesNoCancel) Then

            Dim FIRMA_ID = cboFirma._value
            Dim SystembenutzerText = "AVISO_User"

            If cbxUID.Checked Then
                If Not Sql.doSQL("UPDATE [FIBU2].dbo.fibu_konto  SET c_ustidnr='" & lblAVISO_UID.Text & "', c_manipulator = '" & SystembenutzerText & "', dt_manipulation = Getdate() where i_konto='" & KundenNr & "' AND i_firm_refid = '" & FIRMA_ID & "'", "FIBU") Then fehler = True
            End If
            If cbxEMail.Checked Then
                If Not Sql.doSQL("UPDATE [FIBU2].dbo.fibu_konto  SET c_email='" & lblAVISO_EMail.Text & "', c_manipulator = '" & SystembenutzerText & "', dt_manipulation = Getdate()  where i_konto='" & KundenNr & "' AND i_firm_refid = '" & FIRMA_ID & "'", "FIBU") Then fehler = True
            End If
            If cbxFirma.Checked Then
                If Not Sql.doSQL("UPDATE [FIBU2].dbo.fibu_konto  SET c_name='" & lblAVISO_Name1.Text & "', c_manipulator = '" & SystembenutzerText & "', dt_manipulation = Getdate()    where i_konto='" & KundenNr & "' AND i_firm_refid = '" & FIRMA_ID & "'", "FIBU") Then fehler = True
                If Not Sql.doSQL("UPDATE [FIBU2].dbo.fibu_konto  SET c_name2='" & lblAVISO_Name2.Text & "', c_manipulator = '" & SystembenutzerText & "', dt_manipulation = Getdate()   where i_konto='" & KundenNr & "' AND i_firm_refid = '" & FIRMA_ID & "'", "FIBU") Then fehler = True
            End If
            If cbxAdresse.Checked Then
                If Not Sql.doSQL("UPDATE [FIBU2].dbo.fibu_konto  SET c_strasse='" & lblAVISO_Strasse.Text & "', c_manipulator = '" & SystembenutzerText & "', dt_manipulation = Getdate()  where i_konto='" & KundenNr & "' AND i_firm_refid = '" & FIRMA_ID & "'", "FIBU") Then fehler = True
                If Not Sql.doSQL("UPDATE [FIBU2].dbo.fibu_konto  SET c_plz='" & lblAVISO_PLZ.Text & "', c_manipulator = '" & SystembenutzerText & "', dt_manipulation = Getdate()  where i_konto='" & KundenNr & "' AND i_firm_refid = '" & FIRMA_ID & "'", "FIBU") Then fehler = True
                If Not Sql.doSQL("UPDATE [FIBU2].dbo.fibu_konto  SET c_ort='" & lblAVISO_Ort.Text & "', c_manipulator = '" & SystembenutzerText & "', dt_manipulation = Getdate()  where i_konto='" & KundenNr & "' AND i_firm_refid = '" & FIRMA_ID & "'", "FIBU") Then fehler = True
                If Not Sql.doSQL("UPDATE [FIBU2].dbo.fibu_konto  SET c_landid='" & lblAVISO_Land.Text & "', c_manipulator = '" & SystembenutzerText & "', dt_manipulation = Getdate()  where i_konto='" & KundenNr & "' AND i_firm_refid = '" & FIRMA_ID & "'", "FIBU") Then fehler = True
            End If
        End If

        If fehler Then
            Button1.Image = My.Resources.del
        Else
            Button1.Image = My.Resources.ok
        End If
    End Sub

    Private Sub frmKundenSyska_DatenÜbertragen_Load(sender As Object, e As EventArgs) Handles Me.Load
        AD = New VERAG_PROG_ALLGEMEIN.cAdressen(KundenNr)
        KD = New VERAG_PROG_ALLGEMEIN.cKunde(KundenNr)

        If AD Is Nothing Or KD Is Nothing Then
            Me.Close()
            Exit Sub
        End If

        cboFirma.fillWithSQL("SELECT  [Firma_ID],[Firma_Bez] FROM [tblFirma]  ORDER BY Firma_ID  ", True, "FMZOLL", False)
        If KD.FiBuSchnittstelleNr IsNot Nothing AndAlso KD.FiBuSchnittstelleNr > 0 Then
            cboFirma.changeItem(KD.FiBuSchnittstelleNr)
        Else
            cboFirma.changeItem("1")
        End If

    End Sub

    Private Sub cboFirma_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboFirma.SelectedIndexChanged
        Button1.Image = Nothing
        If cboFirma._value = "" Then Exit Sub
        Dim FIRMA_ID = cboFirma._value

        setSperre(False)

        lblSYSKA_UID.Text = ""
        lblSYSKA_EMail.Text = ""
        lblSYSKA_Name1.Text = ""
        lblSYSKA_Name2.Text = ""
        lblSYSKA_Strasse.Text = ""
        lblSYSKA_PLZ.Text = ""
        lblSYSKA_Ort.Text = ""
        lblSYSKA_Land.Text = ""

        ' Dim dt = Sql.loadDgvBySql("Select isnull(UstID,'') as UstID, isnull(EMailadresse,'') as EMailadresse , isnull(Firmenname,'') as Firmenname, isnull(Zusatz,'') as Zusatz, isnull(Strasse,'') as Strasse, isnull(PLZ,'') as PLZ, isnull(Land,'') as Land, isnull(Ort,'') as Ort FROM [Hilfsdatenbank].[dbo].[Kunden] where Kontonummer='" & KundenNr & "'", "FIBU")
            Dim dt = Sql.loadDgvBySql(" select isnull(c_ustidnr,'')c_ustidnr,isnull(c_name,'')c_name,isnull(c_name2,'')c_name2,isnull(c_strasse,'')c_strasse, isnull(c_plz,'')c_plz,isnull(c_ort,'')c_ort,isnull(c_landid,'')c_landid,isnull(c_email,'')c_email FROM [FIBU2].[dbo].[fibu_konto] where i_konto ='" & KundenNr & "' AND i_firm_refid = '" & FIRMA_ID & "'", "FIBU")
        If dt Is Nothing OrElse dt.Rows.Count = 0 Then
            'msgBox("Kein SYSKA-Eintrag gefunden!")
            setSperre(True)
            Exit Sub
        End If

        lblAVISO_UID.Text = If(AD.UstIdKz, "") & If(AD.UstIdNr, "")
        'lblAVISO_EMail.Text = If(AD.UstIdKz, "") & If(AD.UstIdNr, "")
        If lblAVISO_UID.Text = "" Then cbxUID.Checked = False
        If lblAVISO_EMail.Text = "" Then cbxEMail.Checked = False

        lblAVISO_Name1.Text = If(AD.Name_1, "")
        lblAVISO_Name2.Text = If(AD.Name_2, "")
        lblAVISO_Strasse.Text = If(AD.Straße, "")
        lblAVISO_PLZ.Text = If(AD.PLZ, "")
        lblAVISO_Ort.Text = If(AD.Ort, "")
        lblAVISO_Land.Text = cProgramFunctions.getISO2Land(If(AD.LandKz, ""))

        lblSYSKA_UID.Text = dt.Rows(0)("c_ustidnr")
        lblSYSKA_EMail.Text = dt.Rows(0)("c_email")
        lblSYSKA_Name1.Text = dt.Rows(0)("c_name")
        lblSYSKA_Name2.Text = dt.Rows(0)("c_name2")
        lblSYSKA_Strasse.Text = dt.Rows(0)("c_strasse")
        lblSYSKA_PLZ.Text = dt.Rows(0)("c_plz")
        lblSYSKA_Ort.Text = dt.Rows(0)("c_ort")
        lblSYSKA_Land.Text = dt.Rows(0)("c_landid")

    End Sub

    Sub setSperre(sperre)
        Button1.Enabled = Not sperre
        lblErrSyska.Visible = sperre


    End Sub

End Class