@@ -1,9 +1,15 @@
Imports System . Data . SqlClient
Imports System . IO
Imports System . Net
Imports System . Numerics
Imports System . Reflection
Imports System . Runtime . InteropServices . ComTypes
Imports System . Text . RegularExpressions
Imports System . Threading
Imports System . Web . UI
Imports Microsoft . VisualBasic . ApplicationServices
Imports Org . BouncyCastle . Ocsp
Public Class cCreditSafeAPI
@@ -96,7 +102,7 @@ Public Class cCreditSafeAPI
Shared Function SendGetRequestWithAuthHeader ( url As String , company As Company , acceptContentType As String , method As String , authenticationToken As String , ByRef failureDesc As String ) As String
Shared Function SendGetRequestWithAuthHeader ( url As String , company As Company , acceptContentType As String , method As String , authenticationToken As String , ByRef failureDesc As String , Optional isMonitoring As Boolean = False , Optional csvImport As Boolean = False , Optional csvFile As String = " " ) As String
Try
VERAG_PROG_ALLGEMEIN . cChilkat_Helper . UnlockCilkat ( )
@@ -117,20 +123,101 @@ Public Class cCreditSafeAPI
rest . ClearAllQueryParams ( )
If company IsNot Nothing Then
If company IsNot Nothing AndAlso Not isMonitoring Then
setSearchParam ( rest , company )
End If
rest . AddHeader ( " Content-Type " , " application/json " )
If isMonitoring AndAlso csvFile <> " " Then
rest . PartSelector = " 0 "
rest . AddHeader ( " Content-Type " , " multipart/form-data " )
rest . AddHeader ( " Expect " , " 100-continue " )
Else
rest . AddHeader ( " Content-Type " , " application/json " )
rest . AddHeader ( " Accept " , acceptContentType )
End If
rest . AddHeader ( " Authorization " , " Bearer " & authenticationToken )
rest . AddHeader ( " Accept " , acceptContentType )
Dim responseJson As String
Dim pdfData As New Chilkat . BinData
If isMonitoring Then
If csvImport Then
Dim fileInfo As New FileInfo ( csvFile )
Dim content As String = File . ReadAllText ( csvFile )
'content = content.Replace(";", ",")
'File.WriteAllText(csvFile, content)
Dim fileStream As New Chilkat . Stream
fileStream . SourceFile = csvFile
rest . PartSelector = " 1 "
rest . AddHeader ( " Content-Disposition " , " form-data; name= "" importcsv "" " )
rest . AddHeader ( " Content-Type " , " file " )
'rest.SetMultipartBodyString(content)
rest . SetMultipartBodyStream ( fileStream )
rest . PartSelector = " 0 "
Dim strResponseBody As String = rest . FullRequestMultipart ( method , url . Replace ( API_STRING , " " ) )
If ( rest . LastMethodSuccess <> True ) Then
Debug . WriteLine ( rest . LastErrorText )
failureDesc = rest . LastErrorText
Return failureDesc
Else
If ( rest . ResponseStatusCode <> 200 ) Then
failureDesc = rest . ResponseStatusText & IIf ( responseJson <> " " , vbNewLine & responseJson , " " )
Return failureDesc
Else
Return responseJson
End If
End If
Else
Dim json As New Chilkat . JsonObject
success = json . UpdateString ( " id " , company . creditSafeId )
success = json . UpdateString ( " personalReference " , " VERAG AG " )
success = json . UpdateString ( " freeText " , " hinzugefügt am " & Today . ToShortDateString )
success = json . UpdateString ( " personalLimit " , " " )
Debug . WriteLine ( json . Emit ( ) )
Dim sbRequestBody As New Chilkat . StringBuilder
json . EmitSb ( sbRequestBody )
Dim sbResponseBody As New Chilkat . StringBuilder
success = rest . FullRequestSb ( method , url , sbRequestBody , sbResponseBody )
If ( success <> True ) Then
Debug . WriteLine ( rest . LastErrorText )
failureDesc = rest . LastErrorText
Return failureDesc
Else
If ( rest . ResponseStatusCode <> 200 ) Then
failureDesc = rest . ResponseStatusText & IIf ( responseJson <> " " , vbNewLine & responseJson , " " )
Return failureDesc
Else
Return responseJson
End If
End If
End If
End If
If acceptContentType . Contains ( " application/pdf " ) Then
responseJson = rest . FullRequestNoBodyBd ( method , url , pdfData )
@@ -193,6 +280,10 @@ Public Class cCreditSafeAPI
End Function
Shared Function authenticate ( Optional username As String = " " , Optional password As String = " " ) As String
Dim myUri As String = API_STRING & " /v1/authenticate "
@@ -208,6 +299,26 @@ Public Class cCreditSafeAPI
End Function
Shared Function AddCompanyToPortfolio ( company As Company , Optional PortfolioID As String = " 1662419 " ) As String
Dim myUri As String = API_STRING & " /v1/monitoring/portfolios/ " & PortfolioID & " /companies " 'Default
Dim jsonRespString = SendGetRequestWithAuthHeader ( myUri , company , " application/json " , " POST " , token , " " , True )
Return jsonRespString
End Function
Shared Function AddCompaniesCSVToPortfolio ( csvFilePath As String , Optional PortfolioID As String = " 1662419 " ) As String
Dim myUri As String = API_STRING & " /v1/monitoring/portfolios/ " & PortfolioID & " /import " 'Default
Dim jsonRespString = SendGetRequestWithAuthHeader ( myUri , Nothing , " application/json " , " POST " , token , " " , True , True , csvFilePath )
Return jsonRespString
End Function
Shared Function searchCompanies ( company As Company , ByRef dataTable As DataTable ) As String
@@ -308,12 +419,12 @@ Public Class cCreditSafeAPI
End Function
Shared Function getCompanyEvents ( company As Company , ByRef dtEvents As DataTable ) As String
Shared Function getCompanyEvents ( company As Company , ByRef dtEvents As DataTable , Optional checklastChecked As Boolean = False ) As String
VERAG_PROG_ALLGEMEIN . cChilkat_Helper . UnlockCilkat ( )
Dim failureDesc As String
Dim myUrl As String = API_STRING & " /v1/monitoring/companies/ " & company . creditSafeId & " /events "
Dim myUrl As String = API_STRING & " /v1/monitoring/companies/ " & company . creditSafeId & " /events " & IIf ( checklastChecked , " ?startDate= " & company . lastChecked , " " )
Dim jsonRespString = SendGetRequestWithAuthHeader ( myUrl , company , " application/json " , " GET " , token , failureDesc )
@@ -351,6 +462,11 @@ Public Class cCreditSafeAPI
Dim numCompanies As Integer = companieData . Size
Dim CL As Double = - 1
Dim CR As Integer = - 1
Dim CL_Date As Date = company . lastChecked
Dim CR_Date As Date = company . lastChecked
For i = 0 To 1
@@ -377,20 +493,152 @@ Public Class cCreditSafeAPI
R ( " ruleName " ) = compObj . StringOf ( " ruleName " )
R ( " localEventCode " ) = compObj . StringOf ( " localEventCode " )
R ( " globalEventCode " ) = compObj . StringOf ( " globalEventCode " )
R ( " newValue " ) = compObj . StringOf ( " newValue " )
R ( " oldValue " ) = compObj . StringOf ( " oldValue " )
R ( " newValue " ) = compObj . StringOf ( " newValue " )
R ( " eventDate " ) = dateTime . GetAsTimestamp ( getAsLocal )
R ( " createdDate " ) = dateTime . GetAsTimestamp ( getAsLocal )
dtEvents . Rows . Add ( R )
Dim cEvent As New cCreditsafeEvent ( CLng ( R ( " eventId " ) ) )
cEvent . eventId = R ( " eventId " )
cEvent . companyId = R ( " companyId " )
cEvent . portfolioId = R ( " portfolioId " )
cEvent . ruleName = R ( " ruleName " )
cEvent . localEventCode = R ( " localEventCode " ) . trim ( )
cEvent . globalEventCode = R ( " globalEventCode " ) . trim ( )
cEvent . eventDate = R ( " eventDate " )
cEvent . createdDate = R ( " createdDate " )
cEvent . importDate = Now ( )
cEvent . Sachbearbeiter = IIf ( VERAG_PROG_ALLGEMEIN . cAllgemein . USRKURZNAME Is Nothing , " AUTO " , VERAG_PROG_ALLGEMEIN . cAllgemein . USRKURZNAME )
cEvent . oldValue = R ( " oldValue " )
cEvent . newValue = R ( " newValue " )
'OLD-VALUES
If cEvent . oldValue IsNot Nothing AndAlso IsNumeric ( cEvent . oldValue ) AndAlso Not cEvent . oldValue . ToString . Contains ( " € " ) Then
If cEvent . localEventCode = " CL " Or cEvent . globalEventCode = " CL " Then
cEvent . oldValueCL = cEvent . oldValue
ElseIf cEvent . localEventCode = " CR " Or cEvent . globalEventCode = " CR " Then
cEvent . oldValueCR = cEvent . oldValue
End If
Else
If cEvent . localEventCode = " CL " Or cEvent . globalEventCode = " CL " Then
cEvent . oldValue = Regex . Replace ( cEvent . oldValue , " \s+ " , " " )
cEvent . oldValue = Regex . Replace ( cEvent . oldValue , " [^\d] " , " " )
cEvent . oldValueCL = cEvent . oldValue
ElseIf cEvent . localEventCode = " CR " Or cEvent . globalEventCode = " CR " Then
cEvent . oldValue = Regex . Replace ( cEvent . oldValue , " \s+ " , " " )
cEvent . oldValue = Regex . Replace ( cEvent . oldValue , " [^\d] " , " " )
cEvent . oldValueCR = cEvent . oldValue
End If
End If
'NEW-VALUES
If cEvent . newValue IsNot Nothing AndAlso IsNumeric ( cEvent . newValue ) AndAlso Not cEvent . newValue . ToString . Contains ( " € " ) Then
If cEvent . localEventCode = " CL " Or cEvent . globalEventCode = " CL " Then
cEvent . newValueCL = cEvent . newValue
setCL_CR_Entry ( CDate ( cEvent . eventDate ) , " CL " , cEvent . newValueCL , CL , CR , CL_Date , CR_Date )
ElseIf cEvent . localEventCode = " CR " Or cEvent . globalEventCode = " CR " Then
cEvent . newValueCR = cEvent . newValue
setCL_CR_Entry ( CDate ( cEvent . eventDate ) , " CR " , cEvent . newValueCR , CL , CR , CL_Date , CR_Date )
End If
Else
If cEvent . localEventCode = " CL " Or cEvent . globalEventCode = " CL " Then
cEvent . newValue = Regex . Replace ( cEvent . newValue , " \s+ " , " " )
cEvent . newValue = Regex . Replace ( cEvent . newValue , " [^\d] " , " " )
cEvent . newValueCL = cEvent . newValue
setCL_CR_Entry ( CDate ( cEvent . eventDate ) , " CL " , cEvent . newValueCL , CL , CR , CL_Date , CR_Date )
ElseIf cEvent . localEventCode = " CR " Or cEvent . globalEventCode = " CR " Then
cEvent . newValue = Regex . Replace ( cEvent . newValue , " \s+ " , " " )
cEvent . newValue = Regex . Replace ( cEvent . newValue , " [^\d] " , " " )
cEvent . newValueCR = cEvent . newValue
setCL_CR_Entry ( CDate ( cEvent . eventDate ) , " CR " , cEvent . newValueCR , CL , CR , CL_Date , CR_Date )
End If
End If
If cEvent . bonId Is Nothing AndAlso company . csBonID > 0 Then
cEvent . bonId = company . csBonID
End If
cEvent . kundenNr = company . kdNr
cEvent . SAVE ( )
j = j + 1
End While
Next
If dtEvents . Rows . Count > 1 Then
dtEvents . DefaultView . Sort = " eventDate ASC "
dtEvents = dtEvents . DefaultView . ToTable
'viewEvents = New DataView(dtEvents, "localEventCode IN ('CL','CR') or e.globalEventCode IN ('CL','CR')", "", DataViewRowState.CurrentRows)
End If
If company . kdNr > 0 Then
Dim KUNDE As New cKunde ( company . kdNr )
If KUNDE . Bonitätsdatum <= CL_Date Or KUNDE . Bonitätsdatum <= CR_Date Then
If KUNDE . Bonität <> CR Then
KUNDE . Bonität = CR
KUNDE . Bonitätsdatum = CDate ( CR_Date )
End If
If KUNDE . Höchstkredit <> CL Then
KUNDE . Höchstkredit = CL
KUNDE . Bonitätsdatum = CDate ( CL_Date )
End If
KUNDE . SAVE ( )
End If
End If
Return " Anzahl gefundener Datensätze: " & numCompanies
End Function
Private Shared Sub setCL_CR_Entry ( cs_date As Date , type As String , value As Object , CL As Double , CR As Integer , CL_Date As Date , CR_Date As Date )
Select Case type
Case " CR "
Dim saveCR As Boolean = False
If CR_Date <= cs_date Then
cs_date = CR_Date
saveCR = True
End If
If saveCR Then CR = value
Case " CL "
Dim saveCL As Boolean = False
If CL_Date <= cs_date Then
cs_date = CL_Date
saveCL = True
End If
If saveCL Then CL = value
End Select
End Sub
Shared Function getReport ( ByRef company As Company , withPDF As Boolean ) As String 'Shared Function getReport(ByRef company As Company, withPDF As Boolean, ByRef bytes As Byte()) As String
Dim failureDesc As String = " "
Dim myUrl As String = API_STRING & " /v1/companies/ " & company . creditSafeId & " / "
@@ -674,7 +922,6 @@ Public Class cCreditSafeAPI
rest . AddQueryParam ( " countries " , company . country )
If company . creditsafeNo <> " " Then 'Eindeutiger Schlüssel
rest . AddQueryParam ( " safeNo " , company . creditsafeNo )
@@ -750,6 +997,7 @@ Public Class cCreditSafeAPI
Public Property creditSafeId As String
Public Property name As String
Public Property kdNr As Integer
Public Property vatNo As String
Public Property country As String
Public Property creditsafeNo As String
@@ -770,12 +1018,12 @@ Public Class cCreditSafeAPI
Public Property csCEO As String
Public Property csTurnover As String
Public Property language As String
Public Property csBank As String
Public Property csBonID As Integer = - 1
Public Sub New ( _language As String , _name As String , _vatNo As String , _country As String , _creditsafeNo As String , _creditSafeId As String , _lastChecked As Date , _street As String , _postalCode As String , _city As String , _sumEmployees As String , _businessPurpose As String , _shareholder As String , _ceo As String , _bank As String , _csTurnover As String )
Public Sub New ( _language As String , _name As String , _vatNo As String , _country As String , _creditsafeNo As String , _creditSafeId As String , _lastChecked As Date , _street As String , _postalCode As String , _city As String , _sumEmployees As String , _businessPurpose As String , _shareholder As String , _ceo As String , _bank As String , _csTurnover As String , _kdNr As Integer )
creditSafeId = _creditSafeId
name = _name
vatNo = _vatNo
@@ -792,10 +1040,149 @@ Public Class cCreditSafeAPI
csBank = _bank
language = _language
csTurnover = _csTurnover
kdNr = _kdNr
End Sub
Public Sub New ( )
End Sub
End Class
Public Class cCreditsafeEvent
Property eventId As Long
Property companyId As Object = Nothing
Property portfolioId As Object = Nothing
Property ruleName As Object = Nothing
Property localEventCode As Object = Nothing
Property globalEventCode As Object = Nothing
Property oldValue As Object = Nothing
Property newValue As Object = Nothing
Property eventDate As Object = Nothing
Property createdDate As Object = Nothing
Property importDate As Object = Nothing
Property bonId As Object = Nothing
Property Sachbearbeiter As Object = Nothing
Property oldValueCR As Object = Nothing
Property newValueCR As Object = Nothing
Property oldValueCL As Object = Nothing
Property newValueCL As Object = Nothing
Property kundenNr As Integer
Public hasEntry = False
Sub New ( eventId )
Me . eventId = eventId
LOAD ( )
End Sub
Function getParameterList ( ) As List ( Of VERAG_PROG_ALLGEMEIN . SQLVariable )
Dim list As New List ( Of VERAG_PROG_ALLGEMEIN . SQLVariable )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " eventId " , eventId , , True ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " companyId " , companyId ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " portfolioId " , portfolioId ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " ruleName " , ruleName ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " localEventCode " , localEventCode ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " globalEventCode " , globalEventCode ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " oldValue " , oldValue ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " newValue " , newValue ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " eventDate " , eventDate ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " createdDate " , createdDate ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " importDate " , importDate ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " bonId " , bonId ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " Sachbearbeiter " , Sachbearbeiter ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " oldValueCR " , oldValueCR ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " newValueCR " , newValueCR ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " oldValueCL " , oldValueCL ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " newValueCL " , newValueCL ) )
list . Add ( New VERAG_PROG_ALLGEMEIN . SQLVariable ( " kundenNr " , kundenNr ) )
Return list
End Function
Public Function SAVE ( ) As Boolean
Dim list As List ( Of VERAG_PROG_ALLGEMEIN . SQLVariable ) = getParameterList ( )
Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblCreditsafeEvents WHERE eventId=@eventId) " &
" BEGIN " & getUpdateCmd ( ) & " END " &
" Else " &
" BEGIN " & getInsertCmd ( ) & " END " &
" commit tran "
Return SQL . doSQLVarList ( sqlstr , " FMZOLL " , , list )
End Function
Public Sub LOAD ( )
Try
hasEntry = False
Using conn As SqlConnection = SQL . GetNewOpenConnectionFMZOLL ( )
Using cmd As New SqlCommand ( " SELECT * FROM tblCreditsafeEvents WHERE eventId=@eventId " , conn )
cmd . Parameters . AddWithValue ( " @eventId " , eventId )
Dim dr = cmd . ExecuteReader ( )
If dr . Read Then
For Each li In getParameterList ( )
Dim propInfo As PropertyInfo = Me . GetType . GetProperty ( li . Scalarvariable )
If dr . Item ( li . Text ) Is DBNull . Value Then
propInfo . SetValue ( Me , Nothing )
Else
propInfo . SetValue ( Me , dr . Item ( li . Text ) )
End If
Next
hasEntry = True
End If
dr . Close ( )
End Using
End Using
Catch ex As Exception
VERAG_PROG_ALLGEMEIN . cErrorHandler . ERR ( ex . Message , ex . StackTrace , System . Reflection . MethodInfo . GetCurrentMethod . Name )
End Try
End Sub
Public Function getUpdateCmd ( ) As String
Try
Dim list As List ( Of VERAG_PROG_ALLGEMEIN . SQLVariable ) = getParameterList ( )
Dim str As String = " "
For Each i In list
If Not i . isPrimaryParam Then
str &= " [ " & i . Text & " ] = @ " & i . Scalarvariable & " , " '.Replace("-", "").Replace(" ", "") & ","
End If
Next
str = str . Substring ( 0 , str . Length - 1 ) 'wg. ','
Return ( " UPDATE tblCreditsafeEvents SET " & str & " WHERE eventId=@eventId " )
Catch ex As Exception
VERAG_PROG_ALLGEMEIN . cErrorHandler . ERR ( ex . Message , ex . StackTrace , System . Reflection . MethodInfo . GetCurrentMethod . Name )
End Try
Return " "
End Function
Public Function getInsertCmd ( ) As String
Try
Dim list As List ( Of VERAG_PROG_ALLGEMEIN . SQLVariable ) = getParameterList ( )
Dim str As String = " "
Dim values As String = " "
For Each i In list
'If Not i.isPrimaryParam Then
str &= " [ " & i . Text & " ], "
values &= " @ " & i . Scalarvariable & " , " '.Replace("-", "").Replace(" ", "") & ","
'End If
Next
str = str . Substring ( 0 , str . Length - 1 ) 'wg. ','
values = values . Substring ( 0 , values . Length - 1 ) 'wg. ','
Return ( " INSERT INTO tblCreditsafeEvents ( " & str & " ) VALUES( " & values & " ) " )
Catch ex As Exception
VERAG_PROG_ALLGEMEIN . cErrorHandler . ERR ( ex . Message , ex . StackTrace , System . Reflection . MethodInfo . GetCurrentMethod . Name )
End Try
Return " "
End Function
End Class
End Class