* sonda_anaf_single.prg
* Diagnostic (NU repara nimic): pe schema MARIUSM_AUTO, ecranul de cautare partener arata
* pentru toate codurile fiscale banda "ANAF nu a raspuns - verificare sarita"
* (COMUN\programe\ocautare.prg:801), desi ANAF raspunde OK 200 la curl direct pe aceeasi masina.
* Scopul scriptului: izoleaza pasul care arunca exceptie in ANAF_VerificaCuiSingle /
* ANAF_CreeazaCerereANAF (validare.prg), pas cu pas, cu Try/Catch separat pe fiecare bucata.
*
* Rulare headless (nu tine VFP IDE sau roacont.exe deschis):
*   & "C:\Program Files (x86)\Microsoft Visual FoxPro 9\vfp9.exe" -A -T ;
*     "D:\ROA\ROACONT\COMUN\utile\Teste\partener_anaf\sonda_anaf_single.prg"
* Rezultatul se scrie in %TEMP%\sonda_anaf_single.txt

PUBLIC gcLogFile
gcLogFile = ADDBS(SYS(2023)) + 'sonda_anaf_single.txt'
SET SAFETY OFF
SET TALK OFF
STRTOFILE('START ' + TTOC(DATETIME()) + CHR(13)+CHR(10), gcLogFile)
DO SondaLog WITH 'SET(SAFETY)=' + SET('SAFETY') + ' SET(RESOURCE)=' + SET('RESOURCE')

ON ERROR DO SondaErr WITH ERROR(), MESSAGE(), PROGRAM(), LINENO()
ON SHUTDOWN QUIT

DO D:\ROA\ROACONT\COMUN\utile\Teste\test_init_env_auto.prg WITH 'CENTRAL', 'MARIUSM_AUTO', 'ROMFASTSOFT'
IF gnHandle <= 0
	DO SondaLog WITH 'EROARE: conectare Oracle esuata, gnHandle=' + TRANSFORM(gnHandle)
	DO SondaLog WITH 'REZULTAT: NEDETERMINAT'
	QUIT
ENDIF
DO SondaLog WITH 'mediu OK, gnAn=' + TRANSFORM(gnAn) + ' luna=' + TRANSFORM(gnLuna) + ' gcS=' + gcS

* ===== pas 0: instantiere VerificareANAF, ca in ANAF_VerificaCuiSingle - incarca =====
* ===== nfjsonread.prg / nfjsoncreate.prg ca proceduri (VerificareANAF.Init) =====
LOCAL loVerifANAF, loEx1 As Exception
TRY
	loVerifANAF = CREATEOBJECT('VerificareANAF')
	DO SondaLog WITH 'PAS0 CREATEOBJECT(VerificareANAF): OK, nfjsonread/nfjsoncreate incarcate'
CATCH TO loEx1
	DO SondaLog WITH 'PAS0 CREATEOBJECT(VerificareANAF): EXCEPTIE ' + loEx1.Message + ' [' + loEx1.Procedure + ':' + TRANSFORM(loEx1.LineNo) + ']'
ENDTRY

* ===== pas A: doar Createobject('winHTTP.winHTTPrequest.5.1') =====
LOCAL loHttpA, loEx2 As Exception
TRY
	loHttpA = CREATEOBJECT('winHTTP.winHTTPrequest.5.1')
	DO SondaLog WITH 'PAS A winHTTP.winHTTPrequest.5.1: OK, VARTYPE=' + VARTYPE(loHttpA)
CATCH TO loEx2
	DO SondaLog WITH 'PAS A winHTTP.winHTTPrequest.5.1: EXCEPTIE ' + loEx2.Message + ' [' + loEx2.Procedure + ':' + TRANSFORM(loEx2.LineNo) + ']'
ENDTRY

* ===== pas B: nfjsoncreate pe un array cu un obiect (cui + data), ca in ANAF_CreeazaCerereANAF =====
LOCAL loColB As "empty", laCoduriB[1], lcSendB, loEx3 As Exception
TRY
	loColB = CREATEOBJECT('empty')
	ADDPROPERTY(loColB, 'cui', 1879855)
	ADDPROPERTY(loColB, 'data', '2026-08-01')
	laCoduriB[1] = loColB
	lcSendB = nfjsoncreate(@laCoduriB)
	DO SondaLog WITH 'PAS B nfjsoncreate: OK, rezultat=' + TRANSFORM(lcSendB)
CATCH TO loEx3
	DO SondaLog WITH 'PAS B nfjsoncreate: EXCEPTIE ' + loEx3.Message + ' [' + loEx3.Procedure + ':' + TRANSFORM(loEx3.LineNo) + ']'
ENDTRY

* ===== pas C: ANAF_CreeazaCerereANAF(1879855, Date(), .F.) direct, sincron =====
LOCAL loHttpC, loEx4 As Exception
TRY
	loHttpC = ANAF_CreeazaCerereANAF(1879855, DATE(), .F.)
	IF VARTYPE(loHttpC) = 'O'
		DO SondaLog WITH 'PAS C ANAF_CreeazaCerereANAF: OK, Status=' + TRANSFORM(loHttpC.Status) + ' responseText(200)=' + LEFT(ALLTRIM(loHttpC.responseText), 200)
	ELSE
		DO SondaLog WITH 'PAS C ANAF_CreeazaCerereANAF: OK dar VARTYPE=' + VARTYPE(loHttpC) + ' (nu e obiect)'
	ENDIF
CATCH TO loEx4
	DO SondaLog WITH 'PAS C ANAF_CreeazaCerereANAF: EXCEPTIE ' + loEx4.Message + ' [' + loEx4.Procedure + ':' + TRANSFORM(loEx4.LineNo) + ']'
ENDTRY

* ===== pas E: nfjsonread + ParseJsonANAFv8 direct pe raspunsul de la pasul C, izolat =====
LOCAL loParsedE, loEx5 As Exception
IF VARTYPE(loHttpC) = 'O'
	TRY
		loParsedE = nfjsonread(ALLTRIM(loHttpC.responseText))
		DO SondaLog WITH 'PAS E nfjsonread: OK, TYPE=' + TYPE('loParsedE') + ' PEMSTATUS(found_vfpsafe_)=' + TRANSFORM(PEMSTATUS(loParsedE, 'found_vfpsafe_', 5))
		loVerifANAF.ParseJsonANAFv8(loParsedE, 'crsDiagE')
		DO SondaLog WITH 'PAS E ParseJsonANAFv8: OK, RECCOUNT(crsDiagE)=' + TRANSFORM(RECCOUNT('crsDiagE'))
		IF USED('crsDiagE') AND RECCOUNT('crsDiagE') > 0
			SELECT crsDiagE
			GO TOP
			DO SondaLog WITH 'PAS E primul rand: cui=' + TRANSFORM(NVL(cui, 0)) + ' denumire=' + NVL(denumire, '')
		ENDIF
	CATCH TO loEx5
		DO SondaLog WITH 'PAS E nfjsonread/ParseJsonANAFv8: EXCEPTIE ' + loEx5.Message + ' [' + loEx5.Procedure + ':' + TRANSFORM(loEx5.LineNo) + ']'
	ENDTRY
ELSE
	DO SondaLog WITH 'PAS E: sarit, loHttpC nu e obiect (PAS C a esuat)'
ENDIF

* ===== pas F: exact linia "Use In (Select(alias))" din ANAF_VerificaCuiSingle, pe un alias =====
* ===== care NU e deschis (ca la primul apel din sesiune) =====
LOCAL lcAliasF, lnSelF, loEx6 As Exception
lcAliasF = "crsANAFSingleTemp"
lnSelF = SELECT(m.lcAliasF)
DO SondaLog WITH 'PAS F SELECT(' + m.lcAliasF + ')=' + TRANSFORM(lnSelF) + ' inainte de orice Use'
TRY
	Use In (SELECT(m.lcAliasF))
	DO SondaLog WITH 'PAS F Use In (Select(alias)) pe alias neschis: OK'
CATCH TO loEx6
	DO SondaLog WITH 'PAS F Use In (Select(alias)) pe alias neschis: EXCEPTIE ' + loEx6.Message + ' [' + loEx6.Procedure + ':' + TRANSFORM(loEx6.LineNo) + ']'
ENDTRY

* ===== pas D: fluxul real, ANAF_VerificaCuiSingle, pentru cele 3 coduri fiscale =====
LOCAL lcCui, loResult, llNegasit, lcClasaEsec, lnI
LOCAL ARRAY laCui[3]
laCui[1] = '1879855'
laCui[2] = '12751583'
laCui[3] = '11980333'
FOR lnI = 1 TO 3
	lcCui = laCui[lnI]
	llNegasit = .F.
	lcClasaEsec = ''
	loResult = ANAF_VerificaCuiSingle(m.lcCui, DATE(), @llNegasit, @lcClasaEsec)
	IF VARTYPE(loResult) = 'O'
		DO SondaLog WITH 'PAS D CUI=' + m.lcCui + ' -> OBIECT, denumire=' + NVL(loResult.denumire, '') + ' tlNegasit=' + TRANSFORM(llNegasit) + ' tcClasaEsec=' + m.lcClasaEsec
	ELSE
		DO SondaLog WITH 'PAS D CUI=' + m.lcCui + ' -> VARTYPE=' + VARTYPE(loResult) + ' tlNegasit=' + TRANSFORM(llNegasit) + ' tcClasaEsec=' + m.lcClasaEsec
	ENDIF
	IF USED('crsANAFSingleTemp')
		LOCAL lnSelSaveD
		lnSelSaveD = SELECT()
		SELECT crsANAFSingleTemp
		DO SondaLog WITH 'PAS D dupa CUI=' + m.lcCui + ': crsANAFSingleTemp deschis, RECCOUNT=' + TRANSFORM(RECCOUNT()) + ' RECNO=' + TRANSFORM(RECNO()) + ' EOF=' + TRANSFORM(EOF()) + ' denumire=[' + NVL(denumire, '<NULL>') + ']'
		SELECT (m.lnSelSaveD)
	ELSE
		DO SondaLog WITH 'PAS D dupa CUI=' + m.lcCui + ': crsANAFSingleTemp NU e deschis'
	ENDIF
ENDFOR

DO SondaLog WITH 'REZULTAT: TERMINAT ' + TTOC(DATETIME())
QUIT

PROCEDURE SondaErr
	LPARAMETERS tnErr, tcMsg, tcProg, tnLine
	DO SondaLog WITH 'ON ERROR ' + TRANSFORM(tnErr) + ' [' + tcProg + ':' + TRANSFORM(tnLine) + '] ' + tcMsg
ENDPROC

PROCEDURE SondaLog
	LPARAMETERS tcMsg
	LOCAL lcL
	lcL = FILETOSTR(gcLogFile)
	STRTOFILE(lcL + tcMsg + CHR(13)+CHR(10), gcLogFile)
ENDPROC
