Imports VERAG_PROG_ALLGEMEIN


Public Class usrcntlCreditsafe
    Dim sql As New VERAG_PROG_ALLGEMEIN.SQL
    Dim user, pw As String
    Dim land As String
    Dim dataTable As New DataTable()
    Public comp As Company
    Dim uidDefault As String
    Public createCostumer As Boolean = False
    Dim creditsafeAPI As New cCreditSafeAPI("creditsafe")
    Sub New()

        InitializeComponent()
        createCostumer = True

    End Sub


    Sub New(_UID As String, _name As String, _land As String, _PLZ As String, _Strasse As String, _Ort As String)

        InitializeComponent()
        uidDefault = _UID
        _name = _name.Replace(Chr(34), "") 'Anführungszeichen entfernen
        _name = _name.Replace(Chr(176), "") '° entfernen
        txtFirma.Text = _name
        txtPLZ.Text = _PLZ
        txtOrt.Text = _Ort
        txtStrasse.Text = _Strasse
        land = _land

    End Sub

    Private Sub init()

        user = creditsafeAPI.dataTablecs.Rows(0).Item("api_user")
        pw = creditsafeAPI.dataTablecs.Rows(0).Item("api_password")

        cboLand.fillWithSQL(" SELECT [Länderverzeichnis für die Außenhandelsstatistik].LandKz,[Länderverzeichnis für die Außenhandelsstatistik].LandBez  " &
                                      " FROM [Länderverzeichnis für die Außenhandelsstatistik] LEFT JOIN [Währungstabelle] ON [Länderverzeichnis für die Außenhandelsstatistik].LandNr = [Währungstabelle].[Währungsschlüssel] " &
                                      " WHERE ([Währungstabelle].[MitgliedslandEU] = 1 Or [Länderverzeichnis für die Außenhandelsstatistik].LandKz  = 'TR' Or [Länderverzeichnis für die Außenhandelsstatistik].LandKz  = 'GB' Or [Länderverzeichnis für die Außenhandelsstatistik].LandKz  = 'RS' Or [Länderverzeichnis für die Außenhandelsstatistik].LandKz  = 'CH') " &
                                      " ORDER BY [Länderverzeichnis für die Außenhandelsstatistik].LandKz ", , "FMZOLL", True)

        If land = "" Then land = "AT"

        cbxSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("EN", "EN"))
        cbxSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DE", "DE"))
        cbxSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem(" ", ""))
        cbxSprache.changeItem("")

        cboLand.changeItem(land)
        initDataTable()
        btnUebernehmen.Enabled = False
        Cursor = Cursors.Default

        If uidDefault <> "" Then
            uidDefault = uidDefault.Replace(" ", "")
            uidDefault = uidDefault.Trim()

            If uidDefault.ToUpper.StartsWith("ATU") Then
                uidDefault = uidDefault.Replace("ATU", "")
                txtUID.Text = "ATU " & uidDefault
            Else

                txtUID.Text = uidDefault
            End If

        End If



    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Try

            If user = "" Or pw = "" Then
                lblWarning.Text = "keine Zugangsdaten hinterlegt!"
                Exit Sub
            End If


            If cboLand._value = "" Then
                lblWarning.Text = "kein Land hinterlegt!"
                Exit Sub
            End If

            If txtFirma.Text = "" AndAlso txtUID.Text = "" Then
                lblWarning.Text = "Firma und/oder UID-Nr hinterlegen!"
                Exit Sub
            End If

            dataTable.Clear()

            Dim cs = New cCreditSafeAPI("creditsafe")
            Cursor = Cursors.WaitCursor

            If cs.authenticate() = "200" Then

                Dim company = New cCreditSafeAPI.Company(cbxSprache._value, txtFirma.Text, txtUID.Text, cboLand.SelectedItem.Value, "", "", Nothing, txtStrasse.Text, txtPLZ.Text, txtOrt.Text, "", "", "", "", "", "", -1)
                lblWarning.Text = cs.searchCompanies(company, dataTable).ToString
                initDGV(dataTable)
                Cursor = Cursors.Default

            End If

            dgvCreditsafe.ClearSelection()

        Catch ex As Exception
            Cursor = Cursors.Default
            MsgBox(ex.Message)
        End Try

    End Sub



    Sub initDataTable()

        dataTable.Columns.Add("id", GetType(String))
        dataTable.Columns.Add("name", GetType(String))
        dataTable.Columns.Add("country", GetType(String))
        dataTable.Columns.Add("safeNo", GetType(String))
        dataTable.Columns.Add("vatNo", GetType(String))
        dataTable.Columns.Add("regNo", GetType(String))
        dataTable.Columns.Add("street", GetType(String))
        dataTable.Columns.Add("city", GetType(String))
        dataTable.Columns.Add("postCode", GetType(String))
        dataTable.Columns.Add("status", GetType(String))
        dataTable.Columns.Add("phoneNo", GetType(String))
        dataTable.Columns.Add("dateOfLatestChange", GetType(DateTime))

    End Sub

    Sub initDGV(dt As DataTable)

        dgvCreditsafe.DataSource = dt


        If dgvCreditsafe.Columns.Count > 0 Then
            With dgvCreditsafe
                .RowHeadersWidth = 10
                .Columns("id").Visible = False
                .Columns("country").Visible = False
                .Columns("name").HeaderText = "Name"
                .Columns("name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("safeNo").Width = 100
                .Columns("safeNo").HeaderText = "Nr"
                .Columns("safeNo").Visible = False
                .Columns("vatNo").Width = 100
                .Columns("vatNo").HeaderText = "UID"
                .Columns("regNo").Width = 100
                .Columns("regNo").HeaderText = "FB-Nr."
                .Columns("street").Width = 100
                .Columns("street").HeaderText = "Strasse"
                .Columns("city").Width = 100
                .Columns("city").HeaderText = "Ort"
                .Columns("postCode").Width = 75
                .Columns("postCode").HeaderText = "PLZ"
                .Columns("phoneNo").Width = 100
                .Columns("phoneNo").HeaderText = "Tel-Nr"
                .Columns("status").Width = 100
                .Columns("status").HeaderText = "Status"
                .Columns("dateOfLatestChange").Width = 100
                .Columns("dateOfLatestChange").HeaderText = "letzte Änderung"
            End With

            For Each row As DataGridViewRow In dgvCreditsafe.Rows
                If Not IsDBNull(row.Cells("status").Value) Then
                    Dim akiv As String = (row.Cells("status").Value.ToString).ToLower
                    If akiv = "active" Then
                        row.DefaultCellStyle.BackColor = Color.LightGreen
                    ElseIf akiv = "nonactive" Then
                        row.DefaultCellStyle.BackColor = Color.IndianRed
                    Else
                        row.DefaultCellStyle.BackColor = Color.LightYellow
                    End If
                End If
            Next

        End If


    End Sub

    Private Sub btnUebernehmen_Click(sender As Object, e As EventArgs) Handles btnUebernehmen.Click

        Dim o As Form = Me.FindForm
        '   If Me.Parent.GetType() Is GetType(Form) Then o = Me.FindForm

        For Each row As DataGridViewRow In dgvCreditsafe.SelectedRows
            comp = New Company(IIf(IsDBNull(row.Cells("name").Value), "", row.Cells("name").Value),
                               IIf(IsDBNull(row.Cells("vatNo").Value), "", row.Cells("vatNo").Value),
                            (cboLand._value),
                            IIf(IsDBNull(row.Cells("safeNo").Value), "", row.Cells("safeNo").Value),
                               IIf(IsDBNull(row.Cells("regNo").Value), "", row.Cells("regNo").Value),
                               IIf(IsDBNull(row.Cells("street").Value), "", row.Cells("street").Value),
                               IIf(IsDBNull(row.Cells("city").Value), "", row.Cells("city").Value),
                               IIf(IsDBNull(row.Cells("postCode").Value), "", row.Cells("postCode").Value),
                               IIf(IsDBNull(row.Cells("id").Value), "", row.Cells("id").Value),
                               IIf(IsDBNull(row.Cells("phoneNo").Value), "", row.Cells("phoneNo").Value),
                               IIf(IsDBNull(row.Cells("dateOfLatestChange").Value), "", row.Cells("dateOfLatestChange").Value),
                               IIf(IsDBNull(row.Cells("status").Value), "", row.Cells("status").Value))
        Next

        If comp.safeNo Is Nothing Or comp.safeNo = "" Then
            If Not createCostumer Then
                lblWarning.Text = "Daten von Creditsafe sind unvollständig, es kann kein Creditreport erstellt werden!"
                o.DialogResult = DialogResult.None
            Else
                Dim a As MsgBoxResult = MsgBox("Die Daten des Unternehmens " & comp.name & " sind unvollständig " & vbNewLine & "Trotzdem fortfahren?", vbYesNo)
                If a = vbYes Then
                    o.DialogResult = DialogResult.OK
                    Exit Sub
                Else
                    o.DialogResult = DialogResult.None
                End If
            End If

        ElseIf comp.status.ToLower <> "active" Then
            Dim a As MsgBoxResult = MsgBox("Das Unternehmen " & comp.name & " besitzt den Status: " & comp.status & vbNewLine & "Trotzdem fortfahren?", vbYesNo)
            If a = vbYes Then
                o.DialogResult = DialogResult.OK
                Exit Sub
            Else
                o.DialogResult = DialogResult.None
            End If
        Else
            lblWarning.Text = ""
            o.DialogResult = DialogResult.OK
            Exit Sub
        End If



    End Sub



    Public Class Company

        Public Property credissafeId As String
        Public Property name As String
        Public Property vatNo As String
        Public Property country As String
        Public Property safeNo As String
        Public Property regNo As String
        Public Property street As String
        Public Property city As String
        Public Property postCode As String
        Public Property phoneNo As String
        Public Property dateOfLatestChange As Date
        Public Property status As String


        Public Sub New(_name As String, _vatNo As String, _country As String, _safeNo As String, _regNo As String, _street As String, _city As String, _postCode As String, _credissafeId As String, _phoneNo As String, _dateOfLatestChange As DateTime, _status As String)
            name = _name
            vatNo = _vatNo
            country = _country
            safeNo = _safeNo
            street = _street
            regNo = _regNo
            city = _city
            postCode = _postCode
            phoneNo = _phoneNo
            dateOfLatestChange = dateOfLatestChange
            status = _status
            If safeNo <> "" And safeNo IsNot Nothing Then
                credissafeId = _credissafeId
            Else
                credissafeId = ""
            End If

        End Sub


    End Class

    Private Sub dgvCreditsafe_SelectionChanged(sender As Object, e As EventArgs) Handles dgvCreditsafe.SelectionChanged
        If dgvCreditsafe.SelectedRows.Count <> 1 Then
            btnUebernehmen.Enabled = False
        Else
            btnUebernehmen.Enabled = True
        End If

    End Sub


    Private Sub usrcntlCreditsafe_Load(sender As Object, e As EventArgs) Handles Me.Load
        init()
    End Sub

    Private Sub txtFirma_KeyDown(sender As Object, e As KeyEventArgs) Handles txtFirma.KeyDown, cboLand.KeyDown, txtUID.KeyDown
        If e.KeyValue = Keys.Return Then
            btnSearch.PerformClick()
        End If
    End Sub


    Private Sub txtUID_TextChanged(sender As Object, e As EventArgs) Handles txtUID.TextChanged
        If txtUID.Text <> "" Then

            txtFirma.Enabled = False
            txtOrt.Enabled = False
            txtPLZ.Enabled = False
            txtStrasse.Enabled = False

        Else

            txtFirma.Enabled = True
            txtOrt.Enabled = True
            txtPLZ.Enabled = True
            txtStrasse.Enabled = True

        End If
    End Sub

    Private Sub cbxSprache_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxSprache.SelectedIndexChanged
        dataTable.Clear()
    End Sub

    Private Sub dgvCreditsafe_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvCreditsafe.CellDoubleClick
        If dgvCreditsafe.SelectedRows.Count = 1 Then
            btnUebernehmen.PerformClick()
        End If
    End Sub
End Class
