* test_repro_anaf_path_nfjson.prg
* REPRODUCERE: in ROAGEST, la alegerea partenerului, banda arata mereu
* "ANAF nu a raspuns - verificare sarita", desi ANAF raspunde 200 pe aceeasi masina.
* Ipoteza testata: SET PATH din Programe\roagest.prg NU contine COMUN\UTILE\NFJSON,
* deci VerificareANAF.Init (validare.prg:1877, "Set Procedure To nfjsonread.prg Additive")
* arunca, CREATEOBJECT('VerificareANAF') esueaza in ANAF_VerificaCuiSingle si clasa de
* esec devine FARA_RASPUNS -> banda gri + serviciul ANAF marcat CAZUT pentru 10 minute.
*
* Scenarii:
*   A. path ca in roagest.prg (fara NFJSON) -> CREATEOBJECT esueaza, clasa esec = FARA_RASPUNS
*   B. acelasi path + COMUN\UTILE\NFJSON    -> verdict ANAF real pentru CUI 13791411 (ARAFURA)
*
* Rulare: powershell -ExecutionPolicy Bypass -File test_repro_anaf_path_nfjson.ps1

SET SAFETY OFF
SET TALK OFF
SET DELETED ON
SET EXACT ON
SET CENTURY ON
SET DATE DMY
SET DECIMALS TO 4
SET NULLDISPLAY TO ''
CLOSE DATABASES

PUBLIC gcUILog, gnAsserts, gnPass
gcUILog = "D:\ROA\ROAGEST\COMUN\utile\Teste\partener_anaf\test_repro_anaf_path_nfjson_log.txt"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcUILog)
STORE 0 TO gnAsserts, gnPass

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

LOCAL lcCui, llNegasit, lcClasa, loRez, loEx, lcPath, gcAppPath
lcCui = '13791411'

TRY
	*----- SET PATH exact ca in Programe\roagest.prg (fara COMUN\UTILE\NFJSON) -----
	gcAppPath = "D:\ROA\ROAGEST\"
	SET DEFAULT TO (gcAppPath)
	lcPath = gcAppPath + 'Date;' + gcAppPath + 'Include;' + gcAppPath + 'FERESTRE;' + ;
		gcAppPath + 'GRAFICE;' + gcAppPath + 'CLASE;' + gcAppPath + 'MENIURI;' + ;
		gcAppPath + 'PROGRAME;' + gcAppPath + 'RAPOARTE;' + gcAppPath + 'COMUN;' + ;
		gcAppPath + 'COMUN\CLASE;' + gcAppPath + 'COMUN\FERESTRE;' + gcAppPath + 'COMUN\PROGRAME;' + ;
		gcAppPath + 'COMUN\GRAFICE;' + gcAppPath + 'COMUN\RAPOARTE;' + gcAppPath + 'COMUN\MENIURI;' + ;
		gcAppPath + 'COMUN\UTILE\GRIDEXTRAS;' + gcAppPath + 'COMUN\UTILE\CTL32;' + ;
		gcAppPath + 'COMUN\UTILE\HPDF;' + gcAppPath + 'COMUN\UTILE\HPDF\REPORTOUTPUT;' + ;
		gcAppPath + 'COMUN\UTILE\WEB;' + gcAppPath + 'COMUN\UTILE\EXCEL;' + ;
		ADDBS(SUBSTR(gcAppPath,1,RAT([\],gcAppPath,2)))+[COMUNROA\]
	SET PATH TO &lcPath
	DO PnLog WITH 'SET PATH (ca in roagest.prg) OK'

	SET PROCEDURE TO validare.prg
	SET PROCEDURE TO ocautare.prg ADDITIVE
	SET PROCEDURE TO regex.prg ADDITIVE
	DO PnLog WITH 'SET PROCEDURE OK: ' + SET('Procedure')

	PUBLIC gnIdUtil, goExecutor, goLog, poLog
	gnIdUtil = -3
	goExecutor = CREATEOBJECT('executor_spion')
	LOCAL lcLogApp
	lcLogApp = "D:\ROA\ROAGEST\COMUN\utile\Teste\partener_anaf\test_repro_anaf_path_nfjson_applog.txt"
	STRTOFILE('', lcLogApp)
	poLog = NEWOBJECT("Log_Mesaje", "Log_Mesaje.prg", "", lcLogApp)
	goLog = poLog

	*===== A1: CREATEOBJECT('VerificareANAF') pe path-ul din roagest.prg =====
	loEx = .NULL.
	TRY
		LOCAL loVerif
		loVerif = CREATEOBJECT('VerificareANAF')
		DO PnLog WITH 'A1 CREATEOBJECT(VerificareANAF): OK (nu reproduce)'
	CATCH TO loEx
		DO PnLog WITH 'A1 CREATEOBJECT(VerificareANAF): EXCEPTIE ' + loEx.Message + ;
			' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
	ENDTRY
	DO PnAssert WITH 'A1 VerificareANAF nu se poate instantia fara NFJSON pe path', !ISNULL(loEx)

	*===== A2: fluxul real ANAF_VerificaCuiSingle -> clasa de esec =====
	llNegasit = .F.
	lcClasa = ''
	loRez = ANAF_VerificaCuiSingle(m.lcCui, DATE(), @llNegasit, @lcClasa)
	DO PnLog WITH 'A2 ANAF_VerificaCuiSingle: VARTYPE=' + VARTYPE(loRez) + ;
		' negasit=' + TRANSFORM(llNegasit) + ' clasa_esec=[' + lcClasa + ']'
	DO PnAssert WITH 'A2 clasa esec = FARA_RASPUNS (banda "ANAF nu a raspuns")', lcClasa == 'FARA_RASPUNS'

	*===== A3: banda din ocautare - ANAF_StarePartener pe acelasi cod =====
	LOCAL llNecesita, lcClasa3, loStare3
	llNecesita = .F.
	llNegasit = .F.
	lcClasa3 = ''
	loStare3 = ANAF_StarePartener(m.lcCui, DATE(), .F., @llNecesita, @llNegasit, @lcClasa3, 0)
	DO PnLog WITH 'A3 ANAF_StarePartener: VARTYPE=' + VARTYPE(loStare3) + ' clasa_esec=[' + lcClasa3 + ']' + ;
		' stare_serviciu=' + ANAF_StareServiciu()
	DO PnAssert WITH 'A3 stare serviciu ANAF = CAZUT dupa primul cod', ANAF_StareServiciu() == 'CAZUT'

	*===== B: acelasi mediu + COMUN\UTILE\NFJSON pe path =====
	SET PATH TO (gcAppPath + 'COMUN\UTILE\NFJSON') ADDITIVE
	DO PnLog WITH 'B  SET PATH ... COMUN\UTILE\NFJSON ADDITIVE'
	RELEASE goCacheANAF_Sesiune
	ANAF_SeteazaStareServiciu('NECUNOSCUT', 'reluare scenariu B')

	loEx = .NULL.
	TRY
		LOCAL loVerifB
		loVerifB = CREATEOBJECT('VerificareANAF')
		DO PnLog WITH 'B1 CREATEOBJECT(VerificareANAF): OK'
	CATCH TO loEx
		DO PnLog WITH 'B1 CREATEOBJECT(VerificareANAF): EXCEPTIE ' + loEx.Message
	ENDTRY
	DO PnAssert WITH 'B1 VerificareANAF se instantiaza cu NFJSON pe path', ISNULL(loEx)

	llNegasit = .F.
	lcClasa = ''
	loRez = ANAF_VerificaCuiSingle(m.lcCui, DATE(), @llNegasit, @lcClasa)
	IF VARTYPE(loRez) = 'O'
		DO PnLog WITH 'B2 ANAF_VerificaCuiSingle: denumire=[' + ALLTRIM(NVL(loRez.denumire, '')) + ;
			'] scpTVA=' + TRANSFORM(loRez.scpTVA) + ' clasa_esec=[' + lcClasa + ']'
	ELSE
		DO PnLog WITH 'B2 ANAF_VerificaCuiSingle: VARTYPE=' + VARTYPE(loRez) + ' clasa_esec=[' + lcClasa + ']'
	ENDIF
	DO PnAssert WITH 'B2 verdict ANAF primit pentru ' + m.lcCui, VARTYPE(loRez) = 'O'
	DO PnAssert WITH 'B3 denumire ANAF = ARAFURA', VARTYPE(loRez) = 'O' AND 'ARAFURA' $ UPPER(ALLTRIM(NVL(loRez.denumire, '')))

	*===== C: corpul lui ANAF_VerificaCuiSingle replicat inline, pas cu pas =====
	LOCAL loVerifC, loHttpC, lcJson, loParsed, lnSalvate, llNegC, lcClasaC, loRezC
	loVerifC = CREATEOBJECT('VerificareANAF')
	TRY
		loHttpC = ANAF_CreeazaCerereANAF(INT(VAL(m.lcCui)), DATE(), .F.)
		lcJson = ALLTRIM(loHttpC.responseText)
		DO PnLog WITH 'C1 ANAF_CreeazaCerereANAF: Status=' + TRANSFORM(loHttpC.Status) + ' raspuns(120)=' + LEFT(lcJson, 120)
	CATCH TO loEx
		lcJson = ''
		DO PnLog WITH 'C1 ANAF_CreeazaCerereANAF: EXCEPTIE ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
	ENDTRY
	TRY
		loParsed = nfjsonread(m.lcJson)
		DO PnLog WITH 'C2 nfjsonread: OK, TYPE=' + VARTYPE(loParsed)
	CATCH TO loEx
		DO PnLog WITH 'C2 nfjsonread: EXCEPTIE ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
	ENDTRY
	TRY
		USE IN (SELECT('crsANAFDiag'))
		loVerifC.ParseJsonANAFv8(loParsed, 'crsANAFDiag')
		DO PnLog WITH 'C3 ParseJsonANAFv8: OK, RECCOUNT=' + IIF(USED('crsANAFDiag'), TRANSFORM(RECCOUNT('crsANAFDiag')), '-')
	CATCH TO loEx
		DO PnLog WITH 'C3 ParseJsonANAFv8: EXCEPTIE ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
	ENDTRY
	TRY
		lnSalvate = loVerifC.SalveazaIstoricDinCursor('crsANAFDiag', gnIdUtil)
		DO PnLog WITH 'C4 SalveazaIstoricDinCursor: OK, salvate=' + TRANSFORM(lnSalvate)
	CATCH TO loEx
		DO PnLog WITH 'C4 SalveazaIstoricDinCursor: EXCEPTIE ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
	ENDTRY
	TRY
		llNegC = .F.
		lcClasaC = ''
		loRezC = ANAF_VerdictDinCursor('crsANAFDiag', INT(VAL(m.lcCui)), @llNegC, @lcClasaC)
		DO PnLog WITH 'C5 ANAF_VerdictDinCursor: VARTYPE=' + VARTYPE(loRezC) + ' negasit=' + TRANSFORM(llNegC) + ;
			' clasa=[' + lcClasaC + ']' + IIF(VARTYPE(loRezC) = 'O', ' denumire=[' + ALLTRIM(NVL(loRezC.denumire, '')) + ']', '')
	CATCH TO loEx
		DO PnLog WITH 'C5 ANAF_VerdictDinCursor: EXCEPTIE ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
	ENDTRY

	*===== D: path-ul REAL, construit din blocul lcPath din Programe\roagest.prg =====
	LOCAL lcSursa, lnStart, lnStop, lcBloc, lcScript, lcPathReal, loVerifD, loRezD, lcClasaD, llNegD
	lcSursa = FILETOSTR("D:\ROA\ROAGEST\Programe\roagest.prg")
	lnStart = ATC("lcPath = gcAppPath + 'Date;'", lcSursa)
	lnStop = ATC("COMUNROA\]", SUBSTR(lcSursa, lnStart)) + lnStart - 1
	lcBloc = SUBSTR(lcSursa, lnStart, lnStop - lnStart + LEN("COMUNROA\]"))
	lcScript = "LPARAMETERS gcAppPath" + CHR(13)+CHR(10) + "LOCAL lcPath" + CHR(13)+CHR(10) + ;
		lcBloc + CHR(13)+CHR(10) + "RETURN lcPath"
	lcPathReal = EXECSCRIPT(lcScript, gcAppPath)
	DO PnLog WITH 'D  path din roagest.prg: ' + lcPathReal
	RELEASE PROCEDURE nfjsonread.prg, nfjsoncreate.prg
	RELEASE goCacheANAF_Sesiune
	ANAF_SeteazaStareServiciu('NECUNOSCUT', 'reluare scenariu D')
	SET PATH TO &lcPathReal
	DO PnAssert WITH 'D1 nfjsonread descarcat inainte de test', !('NFJSONREAD' $ UPPER(SET('Procedure')))

	llNegD = .F.
	lcClasaD = ''
	loRezD = ANAF_VerificaCuiSingle(m.lcCui, DATE(), @llNegD, @lcClasaD)
	IF VARTYPE(loRezD) = 'O'
		DO PnLog WITH 'D2 ANAF_VerificaCuiSingle pe path-ul real: denumire=[' + ALLTRIM(NVL(loRezD.denumire, '')) + '] clasa_esec=[' + lcClasaD + ']'
	ELSE
		DO PnLog WITH 'D2 ANAF_VerificaCuiSingle pe path-ul real: VARTYPE=' + VARTYPE(loRezD) + ' clasa_esec=[' + lcClasaD + ']'
	ENDIF
	DO PnAssert WITH 'D2 verdict ANAF pe path-ul din roagest.prg', VARTYPE(loRezD) = 'O'
	DO PnAssert WITH 'D3 fara clasa de esec (banda arata verdictul, nu "ANAF nu a raspuns")', lcClasaD == ''

CATCH TO loEx
	DO PnLog WITH 'EXCEPTIE NEPRINSA: ' + loEx.Message + ' [' + loEx.Procedure + ':' + TRANSFORM(loEx.LineNo) + ']'
ENDTRY

DO PnLog WITH 'REZULTAT: ' + TRANSFORM(gnPass) + '/' + TRANSFORM(gnAsserts) + ' PASS'
QUIT

PROCEDURE PnAssert
	LPARAMETERS tcNume, tlOk
	gnAsserts = gnAsserts + 1
	IF tlOk
		gnPass = gnPass + 1
	ENDIF
	DO PnLog WITH IIF(tlOk, 'PASS ', 'FAIL ') + tcNume
ENDPROC

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

PROCEDURE PnLog
	LPARAMETERS tcMsg
	STRTOFILE(TRANSFORM(tcMsg) + CHR(13)+CHR(10), gcUILog, .T.)
ENDPROC

DEFINE CLASS executor_spion AS Custom
	*-- tine locul lui goExecutor: salvarea in ISTORIC_CODURI_FISCALE nu face obiectul testului
	PROCEDURE oExecuta
		LPARAMETERS tcSql, tcAlias
		RETURN .T.
	ENDPROC
ENDDEFINE
