Da li je neko imao iskustvo sa automatizacijom download-a izlaznih faktura u PDF-u sa SEF-a.
E-fakture download izlaznih faktura u PDF-u sa SEF-a
Zaključano
Zakačeno
svih.
Moze svih, moze pojedinacno.
Onako na brzinu
Potrebne reference
WinHttpRequest.5.1,
Microsoft Scripting Runtime
Potrebno je preuzeti vba modul JsonConverter.bas sa sajta: [url=]https://github.com/VBA-tools/VBA-JSON[/url]
Pozdrav
Potrebne reference
WinHttpRequest.5.1,
Microsoft Scripting Runtime
Potrebno je preuzeti vba modul JsonConverter.bas sa sajta: [url=]https://github.com/VBA-tools/VBA-JSON[/url]
Code:
Public Function PurchaseInvoiceIds()
Dim http As Object
Dim strUrl As String
strUrl = "https://efaktura.mfin.gov.rs/api/publicApi/purchase-invoice/ids"
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
With http
.Open "Post", strUrl, False
.setRequestHeader "ApiKey", "tvoj api key"
.setRequestHeader "Content-Type", "*/*"
.Send
If .Status = 200 Then
Dim strOdgovor As String
strOdgovor = http.ResponseText
Debug.Print strOdgovor
Debug.Print JsonConverter.ConvertToJson(strOdgovor, Whitespace:=2)
PurchaseInvoiceIds = JsonConverter.ConvertToJson(strOdgovor, Whitespace:=2)
Else
Debug.Print .Status
PurchaseInvoiceIds = ""
End If
End With
Set http = Nothing
On Error GoTo 0
Exit Function
End Function
Public Function PurchaseInvoiceIds()
Dim http As Object
Dim strUrl As String
strUrl = "https://efaktura.mfin.gov.rs/api/publicApi/purchase-invoice/ids"
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
With http
.Open "Post", strUrl, False
.setRequestHeader "ApiKey", "tvoj api key"
.setRequestHeader "Content-Type", "*/*"
.Send
If .Status = 200 Then
Dim strOdgovor As String
strOdgovor = http.ResponseText
Debug.Print strOdgovor
Debug.Print JsonConverter.ConvertToJson(strOdgovor, Whitespace:=2)
PurchaseInvoiceIds = JsonConverter.ConvertToJson(strOdgovor, Whitespace:=2)
Else
Debug.Print .Status
PurchaseInvoiceIds = ""
End If
End With
Set http = Nothing
On Error GoTo 0
Exit Function
End Function
Code:
Public Function PurchaseInvoiceById(ByVal IdEfak As LongPtr)
Dim http As Object
Dim strUrl As String
strUrl = "https://efaktura.mfin.gov.rs/a...pi/purchase-invoice?invoiceid=" & IdEfak
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
With http
.Open "Get", strUrl, False
.setRequestHeader "ApiKey", "tvoj api key"
.setRequestHeader "Content-Type", "*/*"
.Send
If .Status = 200 Then
Dim strOdgovor As String
strOdgovor = http.ResponseText
Debug.Print strOdgovor
Debug.Print JsonConverter.ConvertToJson(strOdgovor, Whitespace:=2)
PurchaseInvoiceById = JsonConverter.ConvertToJson(strOdgovor, Whitespace:=2)
Else
Debug.Print .Status
PurchaseInvoiceById = ""
End If
End With
Set http = Nothing
On Error GoTo 0
Exit Function
End Function
Public Function PurchaseInvoiceById(ByVal IdEfak As LongPtr)
Dim http As Object
Dim strUrl As String
strUrl = "https://efaktura.mfin.gov.rs/a...pi/purchase-invoice?invoiceid=" & IdEfak
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
With http
.Open "Get", strUrl, False
.setRequestHeader "ApiKey", "tvoj api key"
.setRequestHeader "Content-Type", "*/*"
.Send
If .Status = 200 Then
Dim strOdgovor As String
strOdgovor = http.ResponseText
Debug.Print strOdgovor
Debug.Print JsonConverter.ConvertToJson(strOdgovor, Whitespace:=2)
PurchaseInvoiceById = JsonConverter.ConvertToJson(strOdgovor, Whitespace:=2)
Else
Debug.Print .Status
PurchaseInvoiceById = ""
End If
End With
Set http = Nothing
On Error GoTo 0
Exit Function
End Function
Pozdrav
Malo smernice u radu sa json odgovorima Sef api-ja na prethoodom primeru
PurchaseInvoiceIds drugi pristup što bi rekli "u sitna crevca"
Nadam se da če primer malo pojasniti kako se radi sa json objektima.
PurchaseInvoiceIds drugi pristup što bi rekli "u sitna crevca"
Code:
Public Function PurchaseInvoiceIds(strApiKey As String) ' Ovde smo stavili Api key kao parametar funkcije
Dim http As Object
Dim strUrl As String
Dim aNiz() As Long
strUrl = "https://efaktura.mfin.gov.rs/api/publicApi/purchase-invoice/ids"
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
With http
.Open "Post", strUrl, False
.setRequestHeader "ApiKey", strApiKey
.setRequestHeader "Content-Type", "*/*"
.Send
If .Status = 200 Then
Dim strOdgovor As String
strOdgovor = http.responseText
Dim jsonObject As Object
Dim strString As Variant
Dim output As String
Dim i As LongPtr
Dim Parsed As Scripting.Dictionary
Dim Parsed2 As VBA.Collection
Dim InvoiceIds As Scripting.Dictionary
Dim lngBroj As LongPtr
Dim Item As Object
Set jsonObject = JsonConverter.ParseJson(.responseText)
If TypeOf jsonObject Is Scripting.Dictionary Then ' Proveravamo dalije jsonObjrkt json
Debug.Print "Korak 1" ' Ovaj korak se dešava ako postoji .responseText
Set Parsed = jsonObject
If jsonObject.Exists("PurchaseInvoiceIds") Then
Debug.Print "Korak 2" ' Ovaj korak se dešava ako ima ID ulaznih računa
Set Parsed = jsonObject
If TypeOf jsonObject.Item("PurchaseInvoiceIds") Is Scripting.Dictionary Then
Debug.Print "Korak 3" ' Ovaj korak se neće dogoditi u našeoj funkciji jer vrednost ključa PurchaseInvoiceIds nije json, ali sam ipak naveo kako se radi ako jeste
Set Parsed = jsonObject.Item("PurchaseInvoiceIds")
lngBroj = Parsed.Count
ReDim aNiz(lngBroj)
i = 0
For Each Item In Parsed
Debug.Print Item
aNiz(i) = Item
i = i + 1
Next Item
'
Else
Debug.Print "jsonObject vraca JSON Objekat sa PurchaseInvoiceIds key čija vrednost nije JSON Objekat, već je VBA kolekcija"
Set Parsed2 = jsonObject.Item("PurchaseInvoiceIds")
lngBroj = Parsed2.Count
Debug.Print lngBroj
ReDim aNiz(lngBroj)
For i = 1 To Parsed2.Count
aNiz(i) = Parsed2(i)
Debug.Print aNiz(i)
Next i
End If
Else
Debug.Print "jsonObject je vratio JSON Object bez PurchaseInvoiceIds ključa, znači nema ulaznih faktura"
PurchaseInvoiceIds = .""
Exit Function
End If
Else
Debug.Print "jsonObject nije vratio JSON Objekat"
PurchaseInvoiceIds = .""
Exit Function
End If
PurchaseInvoiceIds = .responseText
Else
Debug.Print .Status
End If
End With
Set http = Nothing
On Error GoTo 0
Exit Function
End Function
Public Function PurchaseInvoiceIds(strApiKey As String) ' Ovde smo stavili Api key kao parametar funkcije
Dim http As Object
Dim strUrl As String
Dim aNiz() As Long
strUrl = "https://efaktura.mfin.gov.rs/api/publicApi/purchase-invoice/ids"
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
With http
.Open "Post", strUrl, False
.setRequestHeader "ApiKey", strApiKey
.setRequestHeader "Content-Type", "*/*"
.Send
If .Status = 200 Then
Dim strOdgovor As String
strOdgovor = http.responseText
Dim jsonObject As Object
Dim strString As Variant
Dim output As String
Dim i As LongPtr
Dim Parsed As Scripting.Dictionary
Dim Parsed2 As VBA.Collection
Dim InvoiceIds As Scripting.Dictionary
Dim lngBroj As LongPtr
Dim Item As Object
Set jsonObject = JsonConverter.ParseJson(.responseText)
If TypeOf jsonObject Is Scripting.Dictionary Then ' Proveravamo dalije jsonObjrkt json
Debug.Print "Korak 1" ' Ovaj korak se dešava ako postoji .responseText
Set Parsed = jsonObject
If jsonObject.Exists("PurchaseInvoiceIds") Then
Debug.Print "Korak 2" ' Ovaj korak se dešava ako ima ID ulaznih računa
Set Parsed = jsonObject
If TypeOf jsonObject.Item("PurchaseInvoiceIds") Is Scripting.Dictionary Then
Debug.Print "Korak 3" ' Ovaj korak se neće dogoditi u našeoj funkciji jer vrednost ključa PurchaseInvoiceIds nije json, ali sam ipak naveo kako se radi ako jeste
Set Parsed = jsonObject.Item("PurchaseInvoiceIds")
lngBroj = Parsed.Count
ReDim aNiz(lngBroj)
i = 0
For Each Item In Parsed
Debug.Print Item
aNiz(i) = Item
i = i + 1
Next Item
'
Else
Debug.Print "jsonObject vraca JSON Objekat sa PurchaseInvoiceIds key čija vrednost nije JSON Objekat, već je VBA kolekcija"
Set Parsed2 = jsonObject.Item("PurchaseInvoiceIds")
lngBroj = Parsed2.Count
Debug.Print lngBroj
ReDim aNiz(lngBroj)
For i = 1 To Parsed2.Count
aNiz(i) = Parsed2(i)
Debug.Print aNiz(i)
Next i
End If
Else
Debug.Print "jsonObject je vratio JSON Object bez PurchaseInvoiceIds ključa, znači nema ulaznih faktura"
PurchaseInvoiceIds = .""
Exit Function
End If
Else
Debug.Print "jsonObject nije vratio JSON Objekat"
PurchaseInvoiceIds = .""
Exit Function
End If
PurchaseInvoiceIds = .responseText
Else
Debug.Print .Status
End If
End With
Set http = Nothing
On Error GoTo 0
Exit Function
End Function
Nadam se da če primer malo pojasniti kako se radi sa json objektima.
ovo su sve primeri kako da skinemo fakturu u json formatu. postoje primeri kako to i da se uradi u XML formatu. Da li postoji mogućnost da se skine PDF koji se generiše na SEFu? Pretpostavljam da oni direktno na serveru konvertuju XML u PDF tj imaju predefinisanu formu u koju vrv iz baze upisuju podatke i generišu PDF. Ali da li neko zna da li je moguće to skinuti jer i oni daju download?
Kada skineš XML primljene fakture u njemu imaš red:
pri čemu je string base64encoded, Stavio sam tri tačke jer je string prilično veliki.
Taj string treba da encoduješ i sačuvaš kao pdf.
U primeru je kod modula basBase64 koji ja koristim za Besr64 enkodovanje i dekodovanje:
Treba uključiti biblioteke Microsoft XML, V6 i Microsoft ActiveX Data Objects x.x Library
Pozdrav i sretno kodiranje
Code:
<env:DocumentPdf mimeCode="application/pdf">JVBERi0xLjQKJ ... ==</env:DocumentPdf>
<env:DocumentPdf mimeCode="application/pdf">JVBERi0xLjQKJ ... ==</env:DocumentPdf>
pri čemu je string base64encoded, Stavio sam tri tačke jer je string prilično veliki.
Taj string treba da encoduješ i sačuvaš kao pdf.
U primeru je kod modula basBase64 koji ja koristim za Besr64 enkodovanje i dekodovanje:
Treba uključiti biblioteke Microsoft XML, V6 i Microsoft ActiveX Data Objects x.x Library
Code:
Option Compare Database
Option Explicit
' https://social.msdn.microsoft....31b16/pdf-to-base64?forum=vsto
' https://social.msdn.microsoft....eld-conversion?forum=accessdev
' http://web.archive.org/web/200...o-encode-decode-base64-vb6.asp
' http://cwestblog.com/2013/09/2...ript-convert-image-to-base-64/
' https://medium.com/cainhill/ho...a-file-to-base-64-d124c9b2958a
Sub TestConvert()
Dim bytes
Dim B64String
With CreateObject("ADODB.Stream")
.Open
.Type = ADODB.adTypeBinary
.LoadFromFile "C:\Users\v-guaxu\Desktop\Test.pdf"
bytes = .Read
.Close
End With
B64String = EncodeBase64(bytes)
Dim str As String
str = B64String
Dim Base64Byte() As Byte
Base64Byte = decodeBase64(str)
With CreateObject("ADODB.Stream")
.Open
.Type = adTypeBinary
.Write Base64Byte
.SaveToFile "C:\Users\v-guaxu\Desktop\Converted.pdf"
.Close
End With
End Sub
Private Function EncodeBase64(bytes) As String
Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = bytes
EncodeBase64 = objNode.Text
Set objNode = Nothing
Set objXML = Nothing
End Function
Private Function decodeBase64(ByVal strData As String) As Byte()
Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.Text = strData
decodeBase64 = objNode.nodeTypedValue
Set objNode = Nothing
Set objXML = Nothing
End Function
Option Compare Database
Option Explicit
' https://social.msdn.microsoft....31b16/pdf-to-base64?forum=vsto
' https://social.msdn.microsoft....eld-conversion?forum=accessdev
' http://web.archive.org/web/200...o-encode-decode-base64-vb6.asp
' http://cwestblog.com/2013/09/2...ript-convert-image-to-base-64/
' https://medium.com/cainhill/ho...a-file-to-base-64-d124c9b2958a
Sub TestConvert()
Dim bytes
Dim B64String
With CreateObject("ADODB.Stream")
.Open
.Type = ADODB.adTypeBinary
.LoadFromFile "C:\Users\v-guaxu\Desktop\Test.pdf"
bytes = .Read
.Close
End With
B64String = EncodeBase64(bytes)
Dim str As String
str = B64String
Dim Base64Byte() As Byte
Base64Byte = decodeBase64(str)
With CreateObject("ADODB.Stream")
.Open
.Type = adTypeBinary
.Write Base64Byte
.SaveToFile "C:\Users\v-guaxu\Desktop\Converted.pdf"
.Close
End With
End Sub
Private Function EncodeBase64(bytes) As String
Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = bytes
EncodeBase64 = objNode.Text
Set objNode = Nothing
Set objXML = Nothing
End Function
Private Function decodeBase64(ByVal strData As String) As Byte()
Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement
Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.Text = strData
decodeBase64 = objNode.nodeTypedValue
Set objNode = Nothing
Set objXML = Nothing
End Function
Pozdrav i sretno kodiranje
Nisam uspeo da uradim nista sa ovim Codovima. Prekopiram ih u VB ms Acceasa, aktiviram sve potrebne reference. Podesim da se funkcije aktiviraju na event Onclick dugme forme. Nista se ne desava.
Meni je potrebna pomoc da kada uspesno posaljem fakturu u SEF automatski stampam pdf fakturu koji SEF napravi tj preuzmem pdf fakturu iz SEF-a i stampam je.
Ako neko moze da mi pomogne, Hvala.
Meni je potrebna pomoc da kada uspesno posaljem fakturu u SEF automatski stampam pdf fakturu koji SEF napravi tj preuzmem pdf fakturu iz SEF-a i stampam je.
Ako neko moze da mi pomogne, Hvala.
Ako je dokument uspesno poslat (zaprimljen) na SEF-u isti vraca invoiceid, purchaseinvoiceid i salesinvoiceid.
U zavisnosti sta se hoce, ako recimo u pitanju izlazni dokument onda se koristi salesinvoiceid tj. invoiceid koji se dobio nakon uspesnog slanja na SEF.
Pozivanje odgovarajuce API f-je za preuzimanje dokumenta uz odgovarajuci invoiceid i ostale stvari koje su potrebe po tehnickom uputstvu dobija se ubl (xml) dokument.
Po dobijanju ubl dokumenta potrebno je isti ucitati i izdvojiti pdf file koji se nalazi u headeru. U istom file ako je bilo priloga takodje isto je moguce izdvojiti i koirstiti priloge.
Nakon sto se izdvoji zeljeni file (u ovom slucaju generisani dokument iz zaglavlja) isti se moze snimiti, stampati i sta se vec hoce...
U zavisnosti sta se hoce, ako recimo u pitanju izlazni dokument onda se koristi salesinvoiceid tj. invoiceid koji se dobio nakon uspesnog slanja na SEF.
Pozivanje odgovarajuce API f-je za preuzimanje dokumenta uz odgovarajuci invoiceid i ostale stvari koje su potrebe po tehnickom uputstvu dobija se ubl (xml) dokument.
Po dobijanju ubl dokumenta potrebno je isti ucitati i izdvojiti pdf file koji se nalazi u headeru. U istom file ako je bilo priloga takodje isto je moguce izdvojiti i koirstiti priloge.
Nakon sto se izdvoji zeljeni file (u ovom slucaju generisani dokument iz zaglavlja) isti se moze snimiti, stampati i sta se vec hoce...
izmenjeno 2 puta, poslednji put savkov 04.04.2023. u 11:49
Izmene poruke
- savkov04.04.2023. u 11:49zapis iz teksta poruke
- savkov04.04.2023. u 11:48zapis iz teksta poruke
ja sam za moje potrebe uradio program za preuzimanje poslatih faktura kopirati direktorijum C:\efiskal
Potpis
Igor
Pozdrav Igore, Da li je moguće dostaviti ažuriranu verziju programa, verovatno se nešto menjalo u međuvremenu pa sad ne radi. Hteo sam da probam da skinem nekako sa sef-a primljene i poslate fakture
da promenjeno je moguce ali za koji dan trenutno bas radim azuriranje.
Potpis
Igor
Igore pozdrav
Da li si uspeo nešto da uradis?
Da li si uspeo nešto da uradis?