Verificare ANAF: raspunsul 404 citit, intrerupator cand serviciul nu raspunde
Serviciul ANAF intoarce HTTP 404 cu corp complet pentru codurile inexistente
({"found":[],"notFound":[...]}), iar wrapper-ul citea corpul doar pe 200 - deci
verdictul "cod inexistent" nu aparea niciodata. Corpul se citeste acum la orice
status, iar verdictul se da doar cand notFound contine chiar codul interogat;
orice alt corp neinteles inseamna "nu a raspuns", nu acuzatie.
Timeout-uri reale pe apelurile web (2/2/3/3): garda Pemstatus din jurul lui
SetTimeouts intoarce .F. pe obiectul COM legat tarziu, deci timeout-urile nu se
aplicau, iar o gazda care inghite pachetele bloca interfata ~21 s per rand.
Aceleasi timeout-uri si pe drumul batch. Fallback-ul Microsoft.XMLHTTP, fara
timeout, a fost scos de pe calea single.
Cand serviciul nu raspunde, verificarile se opresc 10 minute si banda arata ca
au fost sarite, in loc sa taca. La deschiderea formularului pleaca o sonda
asincrona de disponibilitate, cu termen de viata, guard de reintrare si Abort()
la inchidere; randul care asteapta verdictul nu face apel propriu. Cache doar pe
rezultate pozitive. Codul fiscal nenumeric primeste mesaj propriu, in banda si
in detaliile F4.
ParseJsonANAFv8 restaureaza formatul datei si pe calea de eroare (o exceptie
lasa toata sesiunea pe YMD).
Teste: suita headless 70/70, cu sonda si clasele de esec mock-uite (fara retea)
si fixture peste corpurile 404 masurate pe serviciul real.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DhJwsrFFe1ZNAAtS8Twuti
This commit is contained in:
@@ -59,6 +59,14 @@
|
||||
*!* marius.mutu
|
||||
*!* NormalizeazaCUI + ANAF_VerificaCuiSingle (verificare la alegerea partenerului, la data documentului)
|
||||
|
||||
*!* 27.07.2026
|
||||
*!* marius.mutu
|
||||
*!* ANAF_VerificaCuiSingle: raspunsul ANAF se citeste si cand serviciul intoarce cod 404, iar codul e
|
||||
*!* declarat inexistent doar cand raspunsul chiar contine codul cautat; apelurile web (single si lot)
|
||||
*!* folosesc timeout-uri reale, iar formatul datei se restaureaza si daca interpretarea raspunsului esueaza;
|
||||
*!* sonda asincrona de disponibilitate (ANAF_SondaPorneste/AuSositRaspunsul/AReusit/Opreste) verifica in
|
||||
*!* fundal, la nevoie, daca serviciul ANAF a revenit, fara sa blocheze interfata
|
||||
|
||||
*!* http://ec.europa.eu/taxation_customs/vies/viesquer.do
|
||||
*!* http://localhost/contafinupdate/verificarecod.aspx/verificarecod/verificare/CIF/1879855/MFIN
|
||||
*!* http://localhost/contafinupdate/verificarecod.aspx/verificarecod/verificare/CNP/1810228134187
|
||||
@@ -1576,19 +1584,23 @@ Function NormalizeazaCUI
|
||||
Endfunc && NormalizeazaCUI
|
||||
|
||||
*************************************
|
||||
*** Wrapper single-CUI peste VerificareANAF: tdData = data documentului (lipsa -> data curenta), degradare tacuta (.NULL.)
|
||||
*** Wrapper single-CUI peste VerificareANAF: tdData = data documentului (lipsa -> data curenta), degradare tacuta (.NULL.),
|
||||
*** tlNegasit (prin referinta) = ANAF a raspuns ca nu exista codul, spre deosebire de lipsa raspunsului
|
||||
*************************************
|
||||
Function ANAF_VerificaCuiSingle
|
||||
Lparameters tcCui, tdData
|
||||
Lparameters tcCui, tdData, tlNegasit, tcClasaEsec
|
||||
|
||||
Local lcCuiNormalizat, loHTTP, loEx As Exception, loResult, lcServer, lcSend, lcJsonString, lcDataFormatted, ldData
|
||||
Local loVerificareANAF As "VerificareANAF", loCol As "empty", laCoduri[1], lcAliasTemp, lnReccountTemp, lnI, llValid
|
||||
Local lcCuiNormalizat, loHTTP, loEx As Exception, loResult, lcJsonString, ldData
|
||||
Local loVerificareANAF As "VerificareANAF", lcAliasTemp, lnI, llValid
|
||||
Local llRaspunsPrimit, lnCuiCautat
|
||||
|
||||
If Type('gvANAF_EroareLogataSesiune') = 'U'
|
||||
Public gvANAF_EroareLogataSesiune
|
||||
gvANAF_EroareLogataSesiune = .F.
|
||||
Endif
|
||||
|
||||
tcClasaEsec = ''
|
||||
tlNegasit = .F.
|
||||
lcCuiNormalizat = NormalizeazaCUI(m.tcCui)
|
||||
llValid = !Empty(m.lcCuiNormalizat)
|
||||
If m.llValid
|
||||
@@ -1605,54 +1617,23 @@ Function ANAF_VerificaCuiSingle
|
||||
|
||||
loResult = .Null.
|
||||
lcAliasTemp = "crsANAFSingleTemp"
|
||||
llRaspunsPrimit = .F.
|
||||
ldData = Iif(Vartype(m.tdData) = 'D' And !Empty(m.tdData), m.tdData, Date())
|
||||
lnCuiCautat = Int(Val(m.lcCuiNormalizat))
|
||||
|
||||
Try
|
||||
loVerificareANAF = Createobject("VerificareANAF") && incarca nfjsonread/nfjsoncreate (Init)
|
||||
|
||||
Try
|
||||
loHTTP = Createobject('winHTTP.winHTTPrequest.5.1')
|
||||
If Pemstatus(m.loHTTP, 'SetTimeouts', 5)
|
||||
loHTTP.SetTimeouts(3000, 3000, 5000, 5000) && rezolvare/conectare 3s, trimitere/primire 5s - de revizuit dupa masuratori (Tema)
|
||||
Endif
|
||||
Catch To loEx
|
||||
loHTTP = Createobject('Microsoft.XMLHTTP')
|
||||
Endtry
|
||||
|
||||
ldData = Iif(Vartype(m.tdData) = 'D' And !Empty(m.tdData), m.tdData, Date())
|
||||
lcDataFormatted = Left(Dtos(m.ldData), 4) + "-" + Substr(Dtos(m.ldData), 5, 2) + "-" + Substr(Dtos(m.ldData), 7, 2)
|
||||
|
||||
loCol = Createobject("empty")
|
||||
Addproperty(loCol, "cui", Int(Val(m.lcCuiNormalizat)))
|
||||
Addproperty(loCol, "data", m.lcDataFormatted)
|
||||
laCoduri[1] = loCol
|
||||
lcSend = nfjsoncreate(@laCoduri)
|
||||
|
||||
lcServer = 'https://webservicesp.anaf.ro/api/PlatitorTvaRest/v9/tva'
|
||||
|
||||
lcJsonString = ''
|
||||
loHTTP.Open('POST', lcServer, .F.)
|
||||
loHTTP.setRequestHeader("Content-Type", "application/json;")
|
||||
loHTTP.Send(lcSend)
|
||||
If loHTTP.Status = 200
|
||||
lcJsonString = Alltrim(loHTTP.responseText)
|
||||
Endif
|
||||
loHTTP = ANAF_CreeazaCerereANAF(m.lnCuiCautat, m.ldData, .F.)
|
||||
llRaspunsPrimit = .T.
|
||||
lcJsonString = Alltrim(loHTTP.responseText)
|
||||
|
||||
If !Empty(m.lcJsonString) And Left(m.lcJsonString, 1) = '{'
|
||||
Use In (Select(m.lcAliasTemp))
|
||||
loVerificareANAF.ParseJsonANAFv8(nfjsonread(m.lcJsonString), m.lcAliasTemp)
|
||||
If Used(m.lcAliasTemp)
|
||||
Select (m.lcAliasTemp)
|
||||
lnReccountTemp = Reccount()
|
||||
If m.lnReccountTemp = 1 And !Empty(denumire)
|
||||
loResult = Createobject("empty")
|
||||
Addproperty(loResult, "cui", cui)
|
||||
Addproperty(loResult, "scpTVA", Nvl(scpTVA, .F.))
|
||||
Addproperty(loResult, "statusInactivi", Nvl(statusInactivi, .F.))
|
||||
Addproperty(loResult, "denumire", Nvl(denumire, ""))
|
||||
Addproperty(loResult, "mesaj", Nvl(mesaj, ""))
|
||||
Endif
|
||||
Use In (Select(m.lcAliasTemp))
|
||||
Endif
|
||||
loResult = ANAF_VerdictDinCursor(m.lcAliasTemp, m.lnCuiCautat, @tlNegasit, @tcClasaEsec)
|
||||
Else
|
||||
tcClasaEsec = 'RASPUNS_NEINTELES'
|
||||
Endif
|
||||
Catch To loEx
|
||||
If !gvANAF_EroareLogataSesiune
|
||||
@@ -1662,11 +1643,157 @@ Function ANAF_VerificaCuiSingle
|
||||
gvANAF_EroareLogataSesiune = .T.
|
||||
Endif
|
||||
loResult = .Null.
|
||||
tlNegasit = .F.
|
||||
tcClasaEsec = Iif(m.llRaspunsPrimit, 'RASPUNS_NEINTELES', 'FARA_RASPUNS')
|
||||
Endtry
|
||||
|
||||
Return m.loResult
|
||||
Endfunc && ANAF_VerificaCuiSingle
|
||||
|
||||
*************************************
|
||||
*** Alege intre obiect de rezultat / tlNegasit (exact 1 rand, denumire goala, cui = CUI-ul cautat) /
|
||||
*** tcClasaEsec = 'RASPUNS_NEINTELES', pe baza cursorului lasat de ParseJsonANAFv8; ambele prin referinta
|
||||
*************************************
|
||||
Function ANAF_VerdictDinCursor
|
||||
Lparameters tcAlias, tnCuiCautat, tlNegasit, tcClasaEsec
|
||||
|
||||
Local loResult, lnReccountTemp
|
||||
|
||||
loResult = .Null.
|
||||
tlNegasit = .F.
|
||||
tcClasaEsec = ''
|
||||
|
||||
If Used(m.tcAlias)
|
||||
Select (m.tcAlias)
|
||||
lnReccountTemp = Reccount()
|
||||
If m.lnReccountTemp = 1 And !Empty(denumire)
|
||||
loResult = Createobject("empty")
|
||||
Addproperty(loResult, "cui", cui)
|
||||
Addproperty(loResult, "scpTVA", Nvl(scpTVA, .F.))
|
||||
Addproperty(loResult, "statusInactivi", Nvl(statusInactivi, .F.))
|
||||
Addproperty(loResult, "denumire", Nvl(denumire, ""))
|
||||
Addproperty(loResult, "mesaj", Nvl(mesaj, ""))
|
||||
Else
|
||||
If m.lnReccountTemp = 1 And Empty(denumire) And Nvl(cui, 0) = m.tnCuiCautat
|
||||
tlNegasit = .T.
|
||||
Else
|
||||
tcClasaEsec = 'RASPUNS_NEINTELES'
|
||||
Endif
|
||||
Endif
|
||||
Use In (Select(m.tcAlias))
|
||||
Else
|
||||
tcClasaEsec = 'RASPUNS_NEINTELES'
|
||||
Endif
|
||||
|
||||
Return m.loResult
|
||||
Endfunc && ANAF_VerdictDinCursor
|
||||
|
||||
*************************************
|
||||
*** Construieste, deschide (sincron sau asincron) si trimite o cerere PlatitorTvaRest v9 pentru un singur CUI;
|
||||
*** folosita atat de wrapper-ul de verificare cat si de sonda de disponibilitate
|
||||
*************************************
|
||||
Function ANAF_CreeazaCerereANAF
|
||||
Lparameters tnCui, tdData, tlAsync
|
||||
|
||||
Local loHTTP, loEx As Exception, loCol As "empty", laCoduri[1], lcSend, lcServer, ldData, lcDataFormatted
|
||||
|
||||
Try
|
||||
loHTTP = Createobject('winHTTP.winHTTPrequest.5.1')
|
||||
Try
|
||||
loHTTP.SetTimeouts(2000, 2000, 3000, 3000) && rezolvare/conectare 2s, trimitere/primire 3s
|
||||
Catch To loEx
|
||||
Endtry
|
||||
Catch To loEx
|
||||
loHTTP = .Null.
|
||||
Endtry
|
||||
|
||||
ldData = Iif(Vartype(m.tdData) = 'D' And !Empty(m.tdData), m.tdData, Date())
|
||||
lcDataFormatted = Left(Dtos(m.ldData), 4) + "-" + Substr(Dtos(m.ldData), 5, 2) + "-" + Substr(Dtos(m.ldData), 7, 2)
|
||||
|
||||
loCol = Createobject("empty")
|
||||
Addproperty(loCol, "cui", Int(Val(Transform(m.tnCui))))
|
||||
Addproperty(loCol, "data", m.lcDataFormatted)
|
||||
laCoduri[1] = loCol
|
||||
lcSend = nfjsoncreate(@laCoduri)
|
||||
|
||||
lcServer = 'https://webservicesp.anaf.ro/api/PlatitorTvaRest/v9/tva'
|
||||
|
||||
loHTTP.Open('POST', lcServer, m.tlAsync)
|
||||
loHTTP.setRequestHeader("Content-Type", "application/json;")
|
||||
loHTTP.Send(lcSend)
|
||||
|
||||
Return m.loHTTP
|
||||
Endfunc && ANAF_CreeazaCerereANAF
|
||||
|
||||
*************************************
|
||||
*** Sonda asincrona de disponibilitate ANAF: porneste o cerere pe un CUI constant (1879855, ROMFAST SRL,
|
||||
*** deja folosit ca CUI de test in acest fisier), fara sa scrie in cache si fara sa produca vreun verdict de partener
|
||||
*************************************
|
||||
Function ANAF_SondaPorneste
|
||||
Local loEx As Exception
|
||||
|
||||
ANAF_SondaOpreste()
|
||||
If Type('goANAF_SondaHTTP') = 'U'
|
||||
Public goANAF_SondaHTTP, glANAF_SondaReusita
|
||||
Endif
|
||||
goANAF_SondaHTTP = .Null.
|
||||
glANAF_SondaReusita = .F.
|
||||
Try
|
||||
goANAF_SondaHTTP = ANAF_CreeazaCerereANAF(1879855, Date(), .T.)
|
||||
Catch To loEx
|
||||
goANAF_SondaHTTP = .Null.
|
||||
Endtry
|
||||
|
||||
Return (Vartype(goANAF_SondaHTTP) = 'O')
|
||||
Endfunc && ANAF_SondaPorneste
|
||||
|
||||
*************************************
|
||||
*** .T. daca sonda in curs si-a primit raspunsul (indiferent de rezultat) sau nu exista nicio sonda activa
|
||||
*************************************
|
||||
Function ANAF_SondaAuSositRaspunsul
|
||||
Local loEx As Exception, llSosit
|
||||
|
||||
If Vartype(goANAF_SondaHTTP) <> 'O'
|
||||
Return .T.
|
||||
Endif
|
||||
llSosit = .F.
|
||||
Try
|
||||
llSosit = goANAF_SondaHTTP.WaitForResponse(0)
|
||||
Catch To loEx
|
||||
glANAF_SondaReusita = .F.
|
||||
Return .T.
|
||||
Endtry
|
||||
If m.llSosit
|
||||
glANAF_SondaReusita = .T.
|
||||
Endif
|
||||
Return m.llSosit
|
||||
Endfunc && ANAF_SondaAuSositRaspunsul
|
||||
|
||||
*************************************
|
||||
*** Verdictul sondei, valabil dupa ce ANAF_SondaAuSositRaspunsul() a intors .T.
|
||||
*************************************
|
||||
Function ANAF_SondaAReusit
|
||||
Return (Vartype(glANAF_SondaReusita) = 'L' And glANAF_SondaReusita)
|
||||
Endfunc && ANAF_SondaAReusit
|
||||
|
||||
*************************************
|
||||
*** Abandoneaza si elibereaza sonda in curs, daca exista; intoarce .T. daca chiar era o sonda in aer
|
||||
*************************************
|
||||
Function ANAF_SondaOpreste
|
||||
Local loEx As Exception, llOprita
|
||||
|
||||
llOprita = (Vartype(goANAF_SondaHTTP) = 'O')
|
||||
If m.llOprita
|
||||
Try
|
||||
goANAF_SondaHTTP.Abort()
|
||||
Catch To loEx
|
||||
Endtry
|
||||
goANAF_SondaHTTP = .Null.
|
||||
Endif
|
||||
|
||||
Return m.llOprita
|
||||
Endfunc && ANAF_SondaOpreste
|
||||
|
||||
*********************************************
|
||||
*** Verificare coduri fiscale folosind serviciul web ANAF, cate 500 de coduri per interogare
|
||||
*********************************************
|
||||
@@ -1751,6 +1878,10 @@ Define Class VerificareANAF As Custom
|
||||
goLog.Log('lnRecords: ' + Transform(m.lnRecords), 'ANAF_SincronWebService_PlatitorTva')
|
||||
Try
|
||||
loHTTP = Createobject('winHTTP.winHTTPrequest.5.1')
|
||||
Try
|
||||
loHTTP.SetTimeouts(2000, 2000, 3000, 3000)
|
||||
Catch To loEx
|
||||
Endtry
|
||||
Catch To loEx
|
||||
ENDTRY
|
||||
|
||||
@@ -1890,6 +2021,7 @@ Define Class VerificareANAF As Custom
|
||||
|
||||
lcSave_SetDate = Set("Date")
|
||||
Set Date To YMD
|
||||
Try
|
||||
*
|
||||
lnRecords = Alen(loJsonObject.found_vfpsafe_, 1)
|
||||
For lnRec = 1 To m.lnRecords
|
||||
@@ -1995,7 +2127,9 @@ Define Class VerificareANAF As Custom
|
||||
Endif
|
||||
Endfor
|
||||
*
|
||||
Set Date To &lcSave_SetDate
|
||||
Finally
|
||||
Set Date To &lcSave_SetDate
|
||||
Endtry
|
||||
Else
|
||||
goLog.Log('JsonObject is null', 'ParseJsonANAF')
|
||||
Endif
|
||||
|
||||
Reference in New Issue
Block a user