* shot_dialog_anaf.prg - captureaza dialogul REAL (AMESSAGEBOX neinlocuit) generat de
* anaf_verif_cautare.VerificaAlegere() (ocautare.prg), pt. o captura de ecran cu mesajul de
* PRODUCTIE real (AntetDialog/ListaCorecti/TextAnaf) - nu text scris de mana in script.
* Mock ANAF (raspunsuri preconfigurate, fara apel real la anaf.ro) ca in suita principala, dar
* FARA mock_amessagebox.prg: AMESSAGEBOX ramane cea reala din oproceduri_comune.prg. Acea functie
* suporta DOUA ramuri: formularul MessageBox.vcx (daca acea clasa e incarcata) sau MESSAGEBOX()
* nativ altfel (fereastra Windows separata, clasa #32770) - formularul VCX nu se randeaza in acest
* mediu minimal headless (confirmat: se blocheaza fara eroare, indiferent de suita testata), asa ca
* acest script elibereaza clasa (RELEASE CLASSLIB, doar in sesiunea proprie, niciun fisier atins)
* ca AMESSAGEBOX sa aleaga singura ramura nativa. Fereastra nativa BLOCHEAZA scriptul pana e inchisa
* din exterior (shot_dialog_anaf.ps1 o gaseste dupa semnalul ready_0, o captureaza cu PrintWindow
* si o inchide cu WM_CLOSE).
SET SAFETY OFF
SET TALK OFF
SET DELETED ON
SET EXACT ON
SET CENTURY ON
SET DATE DMY
SET NULLDISPLAY TO ''
CLOSE DATABASES

PUBLIC gcUILog, gcSyncDir
gcUILog = "D:\ROA\ROACONT\COMUN\utile\Teste\partener_anaf\shot_dialog_anaf_log.txt"
gcSyncDir = "D:\ROA\ROACONT\COMUN\utile\Teste\partener_anaf\uisync_dialog\"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcUILog)

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

*-- doar mock-ul ANAF (raspunsuri) - FARA mock_amessagebox: AMESSAGEBOX ramane cea reala din productie
SET PROCEDURE TO "D:\ROA\ROACONT\COMUN\utile\Teste\ui_harness.prg" ADDITIVE
SET PROCEDURE TO "D:\ROA\ROACONT\COMUN\utile\Teste\partener_anaf\mock_anaf_verificare.prg" ADDITIVE
DO VpaLogD WITH 'mock ANAF incarcat (fara mock amessagebox - dialog REAL)'

DO D:\ROA\ROACONT\COMUN\utile\Teste\test_init_env_auto.prg WITH 'CENTRAL', 'MARIUSM_AUTO', 'ROMFASTSOFT'
IF gnHandle <= 0
	DO VpaLogD WITH 'EROARE: conectare Oracle esuata - GATA'
	DO HarnessDone WITH 'done-error'
	QUIT
ENDIF
DO VpaLogD WITH 'mediu OK, schema=' + gcS

*-- test_init_env_auto incarca "Set Classlib To Messagebox Additive" (ca in roacont.prg) - cu ea
*-- incarcata, AMESSAGEBOX (productie) afiseaza formularul MessageBox.vcx (nu s-a randat in acest
*-- mediu minimal headless, fara legatura cu suita testata). RELEASE CLASSLIB doar in acest script
*-- de captura (nu atinge niciun fisier) - AMESSAGEBOX ramane codul REAL, foloseste doar cealalta
*-- ramura pe care el insusi o suporta: MESSAGEBOX() nativ, fereastra Windows separata (#32770).
RELEASE CLASSLIB Messagebox
DO VpaLogD WITH 'Set(Classlib) dupa RELEASE: [' + SET('Classlib') + ']'

PUBLIC goANAFMockData, gnANAFCallCount, gdANAFUltimaData
goANAFMockData = CREATEOBJECT('Collection')
gnANAFCallCount = 0
gdANAFUltimaData = {}
LOCAL loRespDiscord
loRespDiscord = CREATEOBJECT('Empty')
ADDPROPERTY(loRespDiscord, 'cui', '7654321')
ADDPROPERTY(loRespDiscord, 'scpTVA', .T.)
ADDPROPERTY(loRespDiscord, 'statusInactivi', .F.)
ADDPROPERTY(loRespDiscord, 'denumire', 'DISCORD SRL ANAF')
ADDPROPERTY(loRespDiscord, 'mesaj', '')
goANAFMockData.Add(loRespDiscord, 'C7654321')
DO VpaLogD WITH 'mock ANAF: CUI 7654321 configurat'

PUBLIC gnIdDiscord, gnIdPereche

TRY
	DO CurataDateTestD
	DO CreeazaDateTestD

	LOCAL lnOk
	lnOk = goExecutor.oExecute([SELECT DISTINCT DENUMIRE, NUME, COD_FISCAL, ID_PART, INACTIV FROM NOM_PARTENERI ] + ;
		[WHERE ID_PART IN (] + TRANSFORM(gnIdDiscord) + [,] + TRANSFORM(gnIdPereche) + [) AND INACTIV = 0 ORDER BY ID_PART], 'ctestdialog')

	PUBLIC goFormD, poVerifD
	goFormD = CREATEOBJECT('cauta_alfa_form', 'ctestdialog', '', 'Denumire,Nume,Cod fiscal,Id,Inactiv', ;
		'denumire,nume,cod_fiscal,id_part,inactiv', .F., '', 1, '', '', '', '')
	goFormD.crs_cursor = 'ctestdialog'
	goFormD.lAles = .F.
	poVerifD = CREATEOBJECT('anaf_verif_cautare')
	ADDPROPERTY(goFormD, 'oVerifAlegere', poVerifD)
	poVerifD.Activeaza(goFormD)
	goFormD.WindowType = 0
	goFormD.Show()
	SELECT ctestdialog
	LOCATE FOR id_part = gnIdDiscord
	goFormD._GRID1.AfterRowColChange(1)
	poVerifD.VerificaRand(.T.)
	DO VpaLogD WITH 'stare pregatita, label=[' + ALLTRIM(goFormD.lb_anaf.Caption) + ']'

	DO HarnessReady WITH 0, 'gata sa afisez dialogul REAL'
	*-- BLOCHEAZA aici (MESSAGEBOX() nativ) pana harnessul extern inchide fereastra (WM_CLOSE)
	LOCAL llRez
	llRez = poVerifD.VerificaAlegere(goFormD)
	DO VpaLogD WITH 'dialog inchis, VerificaAlegere a intors ' + TRANSFORM(llRez)

	DO HarnessDone WITH 'done'
CATCH TO loExcD
	DO VpaLogD WITH 'CATCH ErrorNo=' + TRANSFORM(loExcD.ErrorNo) + ' Message=' + loExcD.Message + ' Line=' + TRANSFORM(loExcD.LineNo)
	DO HarnessDone WITH 'done-error'
FINALLY
	DO CurataDateTestD
ENDTRY

QUIT

PROCEDURE CurataDateTestD
	LOCAL lnOk
	lnOk = goExecutor.oExecuta([DELETE FROM NOM_PARTENERI WHERE COD_FISCAL IN ('7654321','RO7654321')])
	=goExecutor.oExecuta('COMMIT')
	DO VpaLogD WITH 'CurataDateTestD: sters=' + TRANSFORM(m.lnOk)
ENDPROC

PROCEDURE CreeazaDateTestD
	LOCAL lcIns
	lcIns = [INSERT INTO NOM_PARTENERI (NUME,DENUMIRE,COD_FISCAL,STERS,ID_MOD,INACTIV,TIP_PERSOANA,INSTITUTIE_PUBLICA,ENTITATE_AFILIATA) VALUES ] + ;
		[('TEST ANAF DISCORD SRL','TEST ANAF DISCORD SRL','7654321',0,0,0,1,0,0)]
	=goExecutor.oExecuta(m.lcIns)
	lcIns = [INSERT INTO NOM_PARTENERI (NUME,DENUMIRE,COD_FISCAL,STERS,ID_MOD,INACTIV,TIP_PERSOANA,INSTITUTIE_PUBLICA,ENTITATE_AFILIATA) VALUES ] + ;
		[('TEST ANAF DISCORD SRL','TEST ANAF DISCORD SRL','RO7654321',0,0,1,1,0,0)]
	=goExecutor.oExecuta(m.lcIns)
	=goExecutor.oExecuta('COMMIT')
	gnIdDiscord = 0
	gnIdPereche = 0
	=goExecutor.oSelect2Value([SELECT ID_PART AS NR FROM NOM_PARTENERI WHERE COD_FISCAL = '7654321'], @gnIdDiscord)
	=goExecutor.oSelect2Value([SELECT ID_PART AS NR FROM NOM_PARTENERI WHERE COD_FISCAL = 'RO7654321'], @gnIdPereche)
	DO VpaLogD WITH 'CreeazaDateTestD: discord=' + TRANSFORM(gnIdDiscord) + ' pereche=' + TRANSFORM(gnIdPereche)
ENDPROC

PROCEDURE VpaErrD
	LPARAMETERS tnErr, tcMsg, tcProg, tnLine
	DO VpaLogD WITH 'ON ERROR ' + TRANSFORM(tnErr) + ' [' + tcProg + ':' + TRANSFORM(tnLine) + '] ' + tcMsg
	TRY
		DO CurataDateTestD
	CATCH
	ENDTRY
	QUIT
ENDPROC

PROCEDURE VpaLogD
	LPARAMETERS tcMsg
	SET SAFETY OFF
	LOCAL lcL
	lcL = IIF(FILE(gcUILog), FILETOSTR(gcUILog), '')
	STRTOFILE(lcL + tcMsg + CHR(13)+CHR(10), gcUILog)
ENDPROC
