* sonda_anaf_inline.prg
* Diagnostic (NU repara nimic): PACK_PARTENERI e VALID pe MARIUSM_AUTO, cu semnatura
* SAVE_ISTORIC_COD_FISCAL identica (27 parametri, aceeasi ordine/tip) cu apelul din
* SalveazaIstoricDinCursor (validare.prg:2639-2642) - vezi sonda_pack_parteneri.txt.
* Ipoteza PACK_PARTENERI INVALID e infirmata. Acest script REPLICA inline corpul lui
* ANAF_VerificaCuiSingle (validare.prg:1640), FARA sa modifice validare.prg, cu
* Try/Catch separat pe fiecare din cele trei sub-apeluri (ParseJsonANAFv8,
* SalveazaIstoricDinCursor, ANAF_VerdictDinCursor), ca sa vedem exact care arunca.
*
* Rulare headless:
*   & "C:\Program Files (x86)\Microsoft Visual FoxPro 9\vfp9.exe" -A -T ;
*     "D:\ROA\ROACONT\COMUN\utile\Teste\partener_anaf\sonda_anaf_inline.prg"
* Rezultatul se scrie in %TEMP%\sonda_anaf_inline.txt

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

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)
	QUIT
ENDIF
DO SondaLog WITH 'mediu OK, gcS=' + gcS
DO SondaLog WITH 'TYPE(gnIdUtil)=' + TYPE('gnIdUtil') + ' gnIdUtil=' + TRANSFORM(gnIdUtil)

LOCAL loVerifANAF, loHttpG, loEx As Exception
LOCAL lcJsonStringG, lcAliasG, lnCuiCautatG, ldDataG
LOCAL llNegasitG, lcClasaEsecG, loResultG

lcAliasG = 'crsANAFSingleTemp'
lnCuiCautatG = 1879855
ldDataG = DATE()
llNegasitG = .F.
lcClasaEsecG = ''
loResultG = .NULL.
lcJsonStringG = ''

loVerifANAF = CREATEOBJECT('VerificareANAF')
DO SondaLog WITH 'PAS G Createobject(VerificareANAF): OK'

TRY
	loHttpG = ANAF_CreeazaCerereANAF(m.lnCuiCautatG, m.ldDataG, .F.)
	lcJsonStringG = ALLTRIM(loHttpG.responseText)
	DO SondaLog WITH 'PAS G ANAF_CreeazaCerereANAF: OK, Status=' + TRANSFORM(loHttpG.Status) + ' len(json)=' + TRANSFORM(LEN(m.lcJsonStringG))
CATCH TO loEx
	DO SondaLog WITH 'PAS G ANAF_CreeazaCerereANAF: EXCEPTIE ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
ENDTRY

IF !EMPTY(m.lcJsonStringG) AND LEFT(m.lcJsonStringG, 1) = '{'

	USE IN (SELECT(m.lcAliasG))

	TRY
		loVerifANAF.ParseJsonANAFv8(nfjsonread(m.lcJsonStringG), m.lcAliasG)
		DO SondaLog WITH 'PAS G ParseJsonANAFv8: OK, alias deschis=' + TRANSFORM(USED(m.lcAliasG)) + ' RECCOUNT=' + IIF(USED(m.lcAliasG), TRANSFORM(RECCOUNT(m.lcAliasG)), '-')
	CATCH TO loEx
		DO SondaLog WITH 'PAS G ParseJsonANAFv8: EXCEPTIE ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
	ENDTRY

	TRY
		loVerifANAF.SalveazaIstoricDinCursor(m.lcAliasG, gnIdUtil)
		DO SondaLog WITH 'PAS G SalveazaIstoricDinCursor: OK, alias deschis=' + TRANSFORM(USED(m.lcAliasG)) + ' RECCOUNT=' + IIF(USED(m.lcAliasG), TRANSFORM(RECCOUNT(m.lcAliasG)), '-')
		IF USED(m.lcAliasG)
			LOCAL lnSelSaveG
			lnSelSaveG = SELECT()
			SELECT (m.lcAliasG)
			DO SondaLog WITH 'PAS G dupa Salveaza - pozitie cursor: RECNO=' + TRANSFORM(RECNO()) + ' EOF=' + TRANSFORM(EOF()) + ' denumire=[' + NVL(denumire, '<NULL>') + '] cui=' + TRANSFORM(NVL(cui, -1))
			SELECT (m.lnSelSaveG)
		ENDIF
	CATCH TO loEx
		DO SondaLog WITH 'PAS G SalveazaIstoricDinCursor: EXCEPTIE ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
	ENDTRY

	TRY
		loResultG = ANAF_VerdictDinCursor(m.lcAliasG, m.lnCuiCautatG, @llNegasitG, @lcClasaEsecG)
		DO SondaLog WITH 'PAS G ANAF_VerdictDinCursor: OK, VARTYPE(loResultG)=' + VARTYPE(loResultG) + ' tlNegasit=' + TRANSFORM(llNegasitG) + ' tcClasaEsec=' + m.lcClasaEsecG
	CATCH TO loEx
		DO SondaLog WITH 'PAS G ANAF_VerdictDinCursor: EXCEPTIE ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
	ENDTRY
ELSE
	DO SondaLog WITH 'PAS G: raspuns gol sau nu incepe cu {, sarim parsarea'
ENDIF

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
