* test_repro_anaf_label_client_contract.prg
* Reproduce raportul utilizatorului (Marius, 03.08.2026): la alegerea clientului pe factura
* (ROAACNPRO, frm_factura.do_cauta_partener), nu apare label-ul de verificare ANAF a codului
* fiscal, desi acesta a fost adaugat in COMUN (ocautare.prg: clasa anaf_verif_cautare, activata
* de caut_partener() prin parametrul tlVerificaANAF). Cauza: frm_factura foloseste in schimb
* caut_client_contract_activ() (ocautare.prg), care nu are deloc acest parametru.
*
* Testeaza DOAR mecanismul comun (caut_client_contract_activ + anaf_verif_cautare.Activeaza),
* nu UI-ul ROAACNPRO: inlocuieste cauta_alfa() cu un shadow (mock_cauta_alfa_anaf_label.prg) care
* replica liniile 211-220 din cauta_alfa.prg (poVerifAlegere.Activeaza/Dezactiveaza) pe un formular
* REAL cauta_alfa_form, PRE-CONSTRUIT in acest script (capcana l), fara sa mai deschida fereastra
* (Show() modal ar bloca headless). Foloseste un partener REAL din nom_parteneri, ca verificarea
* ANAF (goExecutor + serviciul ANAF/cache) sa ruleze pe fluxul real.
*
* Rulare: vfp9.exe -A -T "test_repro_anaf_label_client_contract.prg"
* Verdict in gcUILog. Ruleaza INAINTE de fix (astept gcTestAnafActivat=.F.) si DUPA fix (astept .T.
* + label populat), cu semnatura caut_client_contract_activ() ajustata mai jos (tlVerificaANAF).

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
gcUILog = "D:\ROA\ROAACNPRO\COMUN\utile\Teste\test_repro_anaf_label_client_contract_log.txt"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcUILog)

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

TRY
	*-- shadow-ul TREBUIE incarcat PRIMUL, inainte ca test_init_env_auto sa incarce cauta_alfa real
	SET PROCEDURE TO "D:\ROA\ROAACNPRO\COMUN\utile\Teste\mock_cauta_alfa_anaf_label.prg" ADDITIVE
	DO PrLogLocal WITH 'shadow cauta_alfa incarcat'

	PUBLIC gnButon
	gnButon = 0

	DO "D:\ROA\ROAACNPRO\COMUN\utile\Teste\test_init_env_auto_acnpro.prg" WITH 'CENTRAL', 'MARIUSM_AUTO', 'ROMFASTSOFT'
	IF gnHandle <= 0
		DO PrLogLocal WITH 'FAIL: conectare Oracle esuata (gnHandle=' + TRANSFORM(gnHandle) + ')'
		QUIT
	ENDIF
	DO PrLogLocal WITH 'mediu initializat, gnHandle=' + TRANSFORM(gnHandle)

	*-- un partener real, cu cod fiscal completat, pentru fluxul ANAF real
	LOCAL lcSelPart, lnSuccesPart
	IF USED('crsPartTest')
		USE IN ('crsPartTest')
	ENDIF
	lcSelPart = "SELECT * FROM (SELECT id_part, denumire, cod_fiscal, tip_persoana FROM nom_parteneri " + ;
		"WHERE sters = 0 AND inactiv = 0 AND cod_fiscal IS NOT NULL AND LENGTH(TRIM(cod_fiscal)) > 0 " + ;
		"ORDER BY id_part) WHERE ROWNUM = 1"
	lnSuccesPart = goExecutor.oExecute(lcSelPart, "crsPartTest")
	IF lnSuccesPart < 0 OR RECCOUNT('crsPartTest') = 0
		DO PrLogLocal WITH 'FAIL: nu am gasit niciun partener cu cod fiscal pentru test'
		QUIT
	ENDIF
	SELECT crsPartTest
	LOCAL lnIdPartTest, lcCodFiscalTest, lcDenumireTest
	lnIdPartTest = id_part
	lcCodFiscalTest = ALLTRIM(cod_fiscal)
	lcDenumireTest = ALLTRIM(denumire)
	USE IN ('crsPartTest')
	DO PrLogLocal WITH 'partener test: id_part=' + TRANSFORM(lnIdPartTest) + ' cod_fiscal=' + lcCodFiscalTest + ' denumire=' + lcDenumireTest

	*-- cursorul cu FORMA EXACTA produsa de caut_client_contract_activ (ocautare.prg ~4054-4060):
	*-- id_ctr, id_part, denumire, cod_fiscal, data, numar, data_inceput, data_sfarsit, nr_zile_scadenta, tip_curs
	LOCAL lcCurTest
	lcCurTest = SYS(2015)
	IF USED(lcCurTest)
		USE IN (lcCurTest)
	ENDIF
	CREATE CURSOR (lcCurTest) (id_ctr N(10), id_part N(10), denumire C(100), cod_fiscal C(50), ;
		data D, numar C(100), data_inceput D, data_sfarsit D, nr_zile_scadenta N(10), tip_curs N(10))
	INSERT INTO (lcCurTest) (id_part, denumire, cod_fiscal) VALUES (lnIdPartTest, lcDenumireTest, lcCodFiscalTest)
	GO TOP IN (lcCurTest)

	*-- goDlgTest: formularul REAL cauta_alfa_form, PRE-CONSTRUIT aici (capcana l: CREATEOBJECT
	*-- dintr-o procedura intoarce obiect stricat in acest runtime headless)
	PUBLIC goDlgTest
	goDlgTest = CREATEOBJECT('cauta_alfa_form', lcCurTest, '', ;
		'Denumire,Cod fiscal,Contract,Data', 'denumire,cod_fiscal,numar,data', .F., 'denumire', 1)
	goDlgTest.crs_cursor = lcCurTest
	goDlgTest.WindowType = 0
	DO PrLogLocal WITH 'goDlgTest construit (cauta_alfa_form real, cursor=' + lcCurTest + ')'

	PUBLIC gcTestAnafActivat, gcTestAnafLabelType, gcTestAnafLabelVisible, gcTestAnafLabelCaption, gcTestDiag

	*-- ================= apelul real testat =================
	LOCAL loCauta
	loCauta = caut_client_contract_activ('', 0, 0, .T., DATE())

	DO PrLogLocal WITH 'caut_client_contract_activ() finalizat'
	DO PrLogLocal WITH 'DIAG: ' + gcTestDiag
	DO PrLogLocal WITH 'gcTestAnafActivat=' + TRANSFORM(gcTestAnafActivat) + ;
		' gcTestAnafLabelType=' + gcTestAnafLabelType + ;
		' gcTestAnafLabelVisible=' + TRANSFORM(gcTestAnafLabelVisible) + ;
		' gcTestAnafLabelCaption=[' + gcTestAnafLabelCaption + ']'

	IF gcTestAnafActivat AND gcTestAnafLabelType == 'O'
		DO PrLogLocal WITH 'PASS: label-ul ANAF (lb_anaf) e creat si activat de caut_client_contract_activ'
	ELSE
		DO PrLogLocal WITH 'FAIL (asteptat inainte de fix): label-ul ANAF NU e creat - caut_client_contract_activ nu instantiaza poVerifAlegere'
	ENDIF

	DO PrLogLocal WITH 'GATA'
CATCH TO loExcMain
	DO PrLogLocal WITH 'CATCH ErrorNo=' + TRANSFORM(loExcMain.ErrorNo) + ' Message=' + loExcMain.Message + ;
		' Proc=' + loExcMain.Procedure + ' Line=' + TRANSFORM(loExcMain.LineNo) + ' [' + loExcMain.LineContents + ']'
ENDTRY

QUIT

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

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