Αναζήτηση ΑΦΜ από ΑΑΔΕ
Δημοσιεύτηκε: Τετ Ιαν 17, 2024 6:55 pm
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Net
Imports System.Net.Http
Imports System.Text
Imports System.Threading.Tasks
Imports System.Web
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Linq
Public Class CheckAFMClass
Private Structure result_eu
Dim address As String
Dim countryCode As String
Dim name As String
Dim requestDate As DateTime
Dim valid As Boolean
Dim vatNumber As String
End Structure
Private Structure RgWsPublicBasicRt_out
''' <summary>
''' ΑΦΜ
''' </summary>
Dim afm As String
Dim stopDate As Object
Dim postalAddressNo As String
Dim doyDescr As String
Dim doy As Object
Dim onomasia As String
Dim legalStatusDescr As String
Dim registDate As DateTime
Dim deactivationFlag As Integer
Dim deactivationFlagDescr As String
Dim postalAddress As String
Dim firmFlagDescr As String
''' <summary>
''' Διακριτικός τίτλος
''' </summary>
Dim commerTitle As String
Dim postalAreaDescription As String
Dim INIFlagDescr As String
Dim postalZipCode As String
End Structure
Private Structure RgWsPublicFirmActRtUser
Dim firmActDescr As String
Dim firmActKind As Integer
Dim firmActKindDescr As String
Dim firmActCode As String
End Structure
Private Structure result
Dim RgWsPublicBasicRt_out As RgWsPublicBasicRt_out
Dim arrayOfRgWsPublicFirmActRt_out As Newtonsoft.Json.Linq.JObject
Dim RgWsPublicFirmActRt_out As List(Of RgWsPublicFirmActRtUser)
End Structure
Private Structure MainResult
Dim result_eu As result_eu
Dim result As result
Sub New(s As Boolean)
result.RgWsPublicFirmActRt_out = New List(Of RgWsPublicFirmActRtUser)
End Sub
End Structure
Structure OutputData
Dim BrandName As String
Dim Address As String
Dim JobName As String
Dim AreaName As String
Dim PostCode As String
Dim AFM As String
Dim TaxOffice As String
Dim IsActive As Boolean
Dim DateStart As DateTime
Dim HasError As Boolean
End Structure
Public Function Search(myAFM As String, AfmSearch As String, Optional username As String = "", Optional Password As String = "") As OutputData
Dim output As New OutputData
Try
Dim fl As String = My.Computer.FileSystem.GetTempFileName
System.IO.File.WriteAllText(fl, SOAPManual(AfmSearch, username, Password))
Dim ds As New DataSet
ds.ReadXml(fl)
Dim BsBasic As New BindingSource(ds, "basic_rec")
If Trim(BsBasic.Current("i_ni_flag_descr")) = "ΦΠ" Then
output.BrandName = NZ(BsBasic.Current("onomasia"), "")
Else
output.BrandName = NZ(BsBasic.Current("commer_title"), "")
If DenExiKati(output.BrandName) Then output.BrandName = NZ(BsBasic.Current("onomasia"), "")
End If
output.Address = NZ(BsBasic.Current("postal_address"), "") & " " & NZ(BsBasic.Current("postal_address_no"), "")
output.AreaName = NZ(BsBasic.Current("postal_area_description"), "")
output.PostCode = NZ(BsBasic.Current("postal_zip_code"), "")
output.AFM = NZ(BsBasic.Current("afm"), "")
output.TaxOffice = NZ(BsBasic.Current("doy_descr"), "")
output.IsActive = Not NZ(BsBasic.Current("deactivation_flag"), 0)
output.DateStart = BsBasic.Current("regist_date")
Dim Bsitem As New BindingSource(ds, "item")
output.JobName = Mid(Bsitem.Current("firm_act_descr"), 1, 75)
output.HasError = False
Bsitem.Dispose()
BsBasic.Dispose()
ds.Dispose()
Catch ex As Exception
WriteLogFile(ex.Message)
output.HasError = True
End Try
Return output
End Function
Public Function SOAPManual(AfmSearch As String, username As String, Password As String) As String
Const url As String = "https://www1.gsis.gr/wsaade/RgWsPublic2/RgWsPublic2"
Const action As String = "POST"
Dim soapEnvelopeXml As XmlDocument = CreateSoapEnvelope(AfmSearch, username, Password)
Dim webRequest As HttpWebRequest = CreateWebRequest(url, action)
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest)
Dim result As String
Using response As WebResponse = webRequest.GetResponse()
Using rd As StreamReader = New StreamReader(response.GetResponseStream())
result = rd.ReadToEnd()
End Using
End Using
Return result
End Function
Private Shared Function CreateWebRequest(ByVal url As String, ByVal action As String) As HttpWebRequest
System.Net.ServicePointManager.SecurityProtocol = 3072
Dim WebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
WebRequest.Headers.Add("SOAPAction", action)
webRequest.ContentType = "application/soap+xml;charset=""utf-8"""
webRequest.Accept = "application/xml"
webRequest.Method = "POST"
Return webRequest
End Function
Private Shared Function CreateSoapEnvelope(AfmSearch As String, username As String, Password As String) As XmlDocument
Dim soapEnvelopeXml As XmlDocument = New XmlDocument()
Dim xmls As String = "<?xml version=""1.0""?>
<env:Envelope xmlns:ns3=""http://rgwspublic2/RgWsPublic2"" xmlns:ns2=""http://rgwspublic2/RgWsPublic2Service"" xmlns:ns1=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:env=""http://www.w3.org/2003/05/soap-envelope"">
<env:Header>
<ns1:Security>
<ns1:UsernameToken>
<ns1:Username>@USERNAME@</ns1:Username>
<ns1:Password>@PASSWORD@</ns1:Password>
</ns1:UsernameToken>
</ns1:Security>
</env:Header>
<env:Body>
<ns2:rgWsPublic2AfmMethod>
<ns2:INPUT_REC>
<ns3:afm_called_by/>
<ns3:afm_called_for>@SEARCHAFM@</ns3:afm_called_for>
</ns2:INPUT_REC>
</ns2:rgWsPublic2AfmMethod>
</env:Body>
</env:Envelope>"
xmls = xmls.Replace("@USERNAME@", username)
xmls = xmls.Replace("@PASSWORD@", Password)
xmls = xmls.Replace("@SEARCHAFM@", AfmSearch)
soapEnvelopeXml.LoadXml(xmls)
Return soapEnvelopeXml
End Function
Private Shared Sub InsertSoapEnvelopeIntoWebRequest(ByVal soapEnvelopeXml As XmlDocument, ByVal webRequest As HttpWebRequest)
Using stream As Stream = webRequest.GetRequestStream()
soapEnvelopeXml.Save(stream)
End Using
End Sub
End Class
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Net
Imports System.Net.Http
Imports System.Text
Imports System.Threading.Tasks
Imports System.Web
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Linq
Public Class CheckAFMClass
Private Structure result_eu
Dim address As String
Dim countryCode As String
Dim name As String
Dim requestDate As DateTime
Dim valid As Boolean
Dim vatNumber As String
End Structure
Private Structure RgWsPublicBasicRt_out
''' <summary>
''' ΑΦΜ
''' </summary>
Dim afm As String
Dim stopDate As Object
Dim postalAddressNo As String
Dim doyDescr As String
Dim doy As Object
Dim onomasia As String
Dim legalStatusDescr As String
Dim registDate As DateTime
Dim deactivationFlag As Integer
Dim deactivationFlagDescr As String
Dim postalAddress As String
Dim firmFlagDescr As String
''' <summary>
''' Διακριτικός τίτλος
''' </summary>
Dim commerTitle As String
Dim postalAreaDescription As String
Dim INIFlagDescr As String
Dim postalZipCode As String
End Structure
Private Structure RgWsPublicFirmActRtUser
Dim firmActDescr As String
Dim firmActKind As Integer
Dim firmActKindDescr As String
Dim firmActCode As String
End Structure
Private Structure result
Dim RgWsPublicBasicRt_out As RgWsPublicBasicRt_out
Dim arrayOfRgWsPublicFirmActRt_out As Newtonsoft.Json.Linq.JObject
Dim RgWsPublicFirmActRt_out As List(Of RgWsPublicFirmActRtUser)
End Structure
Private Structure MainResult
Dim result_eu As result_eu
Dim result As result
Sub New(s As Boolean)
result.RgWsPublicFirmActRt_out = New List(Of RgWsPublicFirmActRtUser)
End Sub
End Structure
Structure OutputData
Dim BrandName As String
Dim Address As String
Dim JobName As String
Dim AreaName As String
Dim PostCode As String
Dim AFM As String
Dim TaxOffice As String
Dim IsActive As Boolean
Dim DateStart As DateTime
Dim HasError As Boolean
End Structure
Public Function Search(myAFM As String, AfmSearch As String, Optional username As String = "", Optional Password As String = "") As OutputData
Dim output As New OutputData
Try
Dim fl As String = My.Computer.FileSystem.GetTempFileName
System.IO.File.WriteAllText(fl, SOAPManual(AfmSearch, username, Password))
Dim ds As New DataSet
ds.ReadXml(fl)
Dim BsBasic As New BindingSource(ds, "basic_rec")
If Trim(BsBasic.Current("i_ni_flag_descr")) = "ΦΠ" Then
output.BrandName = NZ(BsBasic.Current("onomasia"), "")
Else
output.BrandName = NZ(BsBasic.Current("commer_title"), "")
If DenExiKati(output.BrandName) Then output.BrandName = NZ(BsBasic.Current("onomasia"), "")
End If
output.Address = NZ(BsBasic.Current("postal_address"), "") & " " & NZ(BsBasic.Current("postal_address_no"), "")
output.AreaName = NZ(BsBasic.Current("postal_area_description"), "")
output.PostCode = NZ(BsBasic.Current("postal_zip_code"), "")
output.AFM = NZ(BsBasic.Current("afm"), "")
output.TaxOffice = NZ(BsBasic.Current("doy_descr"), "")
output.IsActive = Not NZ(BsBasic.Current("deactivation_flag"), 0)
output.DateStart = BsBasic.Current("regist_date")
Dim Bsitem As New BindingSource(ds, "item")
output.JobName = Mid(Bsitem.Current("firm_act_descr"), 1, 75)
output.HasError = False
Bsitem.Dispose()
BsBasic.Dispose()
ds.Dispose()
Catch ex As Exception
WriteLogFile(ex.Message)
output.HasError = True
End Try
Return output
End Function
Public Function SOAPManual(AfmSearch As String, username As String, Password As String) As String
Const url As String = "https://www1.gsis.gr/wsaade/RgWsPublic2/RgWsPublic2"
Const action As String = "POST"
Dim soapEnvelopeXml As XmlDocument = CreateSoapEnvelope(AfmSearch, username, Password)
Dim webRequest As HttpWebRequest = CreateWebRequest(url, action)
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest)
Dim result As String
Using response As WebResponse = webRequest.GetResponse()
Using rd As StreamReader = New StreamReader(response.GetResponseStream())
result = rd.ReadToEnd()
End Using
End Using
Return result
End Function
Private Shared Function CreateWebRequest(ByVal url As String, ByVal action As String) As HttpWebRequest
System.Net.ServicePointManager.SecurityProtocol = 3072
Dim WebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
WebRequest.Headers.Add("SOAPAction", action)
webRequest.ContentType = "application/soap+xml;charset=""utf-8"""
webRequest.Accept = "application/xml"
webRequest.Method = "POST"
Return webRequest
End Function
Private Shared Function CreateSoapEnvelope(AfmSearch As String, username As String, Password As String) As XmlDocument
Dim soapEnvelopeXml As XmlDocument = New XmlDocument()
Dim xmls As String = "<?xml version=""1.0""?>
<env:Envelope xmlns:ns3=""http://rgwspublic2/RgWsPublic2"" xmlns:ns2=""http://rgwspublic2/RgWsPublic2Service"" xmlns:ns1=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:env=""http://www.w3.org/2003/05/soap-envelope"">
<env:Header>
<ns1:Security>
<ns1:UsernameToken>
<ns1:Username>@USERNAME@</ns1:Username>
<ns1:Password>@PASSWORD@</ns1:Password>
</ns1:UsernameToken>
</ns1:Security>
</env:Header>
<env:Body>
<ns2:rgWsPublic2AfmMethod>
<ns2:INPUT_REC>
<ns3:afm_called_by/>
<ns3:afm_called_for>@SEARCHAFM@</ns3:afm_called_for>
</ns2:INPUT_REC>
</ns2:rgWsPublic2AfmMethod>
</env:Body>
</env:Envelope>"
xmls = xmls.Replace("@USERNAME@", username)
xmls = xmls.Replace("@PASSWORD@", Password)
xmls = xmls.Replace("@SEARCHAFM@", AfmSearch)
soapEnvelopeXml.LoadXml(xmls)
Return soapEnvelopeXml
End Function
Private Shared Sub InsertSoapEnvelopeIntoWebRequest(ByVal soapEnvelopeXml As XmlDocument, ByVal webRequest As HttpWebRequest)
Using stream As Stream = webRequest.GetRequestStream()
soapEnvelopeXml.Save(stream)
End Using
End Sub
End Class