* test_repro_cnrcrt_footer.prg
* Repro/regresie pentru eroarea "Property CNRCRT is not found" din
* FRM_MODIFIC2024.PGFARTICOLE.PAGE1._GRDFOOTER1.CALCTOTAL
* (Import extrase bancare > Generare note, ROACONT 2.11.61, 15.07.2026).
*
* Cauza (dovedita prin bisectie, vezi COMUN\docs\depanare_testare_vfp.md):
*  - frm_import_extrase_banca.modificanote crea crsJTVATemp prin
*    SELECT * INTO CURSOR (copia pierde indexul TAG id_jtva creat de update_jtva_coloane);
*  - coloana cExplicatieTVA din frm_modific2024.grdRulaje face Seek pe acel tag in ControlSource;
*  - dupa repararea typo-ului oMinHeaderHeight->nMinHeaderHeight in cusfonthandler
*    (accessibility.vcx, r17869), ApplyFontToGrid atinge coloanele la Init;
*    evaluarea ControlSource-ului cu Seek pe tag inexistent face grid-ul sa-si piarda
*    TOATE coloanele => footer-ul se ataseaza la 0 coloane => calctotal crapa la
*    Getpem(This, cColWithLabel).
*
* Rulare headless (nu tine VFP IDE deschis; nu rula cu roacont.exe pornit):
*   & "C:\Program Files (x86)\Microsoft Visual FoxPro 9\vfp9.exe" -A -T ;
*     "D:\ROA\ROACONT\COMUN\utile\Teste\achizitie_import\test_repro_cnrcrt_footer.prg" [MOD]
* MOD:
*   CUINDEX   (implicit) - fluxul corectat (index pe crsJTVATemp). Asteptat: 63 coloane, OK.
*   FARAINDEX            - fluxul vechi, fara index. Asteptat: 0 coloane + eroarea CNRCRT
*                          (reproduce bug-ul; util ca test de regresie al capcanei).
* Rezultatul se scrie in %TEMP%\test_repro_cnrcrt_footer.txt, ultima linie REZULTAT: OK/BUG.

LPARAMETERS tcMode
tcMode = IIF(EMPTY(tcMode), 'CUINDEX', UPPER(ALLTRIM(tcMode)))

PUBLIC gcLogFile, gnErrCNRCRT
gcLogFile = ADDBS(SYS(2023)) + 'test_repro_cnrcrt_footer.txt'
gnErrCNRCRT = 0
STRTOFILE('START ' + tcMode + ' ' + TTOC(DATETIME()) + CHR(13)+CHR(10), gcLogFile)

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

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

* ===== replicare modificanote din frm_import_extrase_banca (partea non-UI) =====
IF !Used('saft_taxtable')
	update_saft_taxtable()
ENDIF
IF !Used('saft_mecanisme_plati')
	update_saft_mecanisme_plati()
ENDIF
update_jtva_coloane("JV", 'crsExplicatiiTVATemp', 1)
SELECT * FROM crsExplicatiiTVATemp INTO CURSOR crsJTVATemp READWRITE
IF tcMode == 'CUINDEX'
	* fix-ul din modificanote (15.07.2026): copia SELECT INTO pierde indexul
	SELECT crsJTVATemp
	INDEX ON id_jtva_coloana TAG id_jtva
ENDIF

LOCAL llSucces, lcSql
lcSql = [SELECT * FROM vact WHERE 1=2]
llSucces = goExecutor.oExecuta(m.lcSql, 'actactan_temp')
lcSql = [select * from vrul_tot where 1=2]
llSucces = goExecutor.oExecuta(lcSql, 'v_rul')
Select *, valoare + valtva As valoarectva, valoarev + valtvav As valoarevctva From v_rul Into Cursor trul READWRITE
Use In (SELECT('v_rul'))
lcSql = [select * from vrul_obinv_tot where 1=2]
llSucces = goExecutor.oExecuta(lcSql, 'v_rul_obinv')
Select *, valoare + valtva As valoarectva, valoarev + valtvav As valoarevctva From v_rul_obinv Into Cursor trul_obinv READWRITE
Use In (SELECT('v_rul_obinv'))

Select actactan_temp
Select .F. As ales, a.explicatia4 As iban, Iif(!Empty(Nvl(a.id_jtva_coloana, 0)), 1, 0) As Cu_tva, ;
		Cast(0 As N(2)) As tipnota, a.*, b.denumire As explicatie_tva_2 ;
	From actactan_temp a Left Join crsExplicatiiTVATemp b On a.id_jtva_coloana = b.id_jtva_coloana ;
	Into Cursor tact Readwrite
gcAcces = "1;2;3;4;"
Select tact
Go Top

* ===== forma reala + secventa relevanta din Show =====
LOCAL loForm, i, lcNames, lnColSrc, lnColFooter
loForm = CREATEOBJECT([frm_modific2024], 90023, .T.)
IF VARTYPE(loForm) <> 'O'
	DO ReproLog WITH 'EROARE: frm_modific2024 nu s-a instantiat'
	DO ReproLog WITH 'REZULTAT: NEDETERMINAT'
	QUIT
ENDIF
Select tact
loForm.grid1.SetFocus
loForm.gridextra1.setup()
loForm.CalculeazaTotal()

lnColSrc = loForm.pgfArticole.Page1.grdRulaje.ColumnCount
DO ReproLog WITH 'grdRulaje coloane: ' + TRANSFORM(lnColSrc)

loForm.pgfArticole.page1._grdfooter1.attachtogrid(loForm.pgfArticole.page1.grdRulaje)
lnColFooter = loForm.pgfArticole.Page1._grdfooter1.ColumnCount
DO ReproLog WITH 'footer coloane dupa attach: ' + TRANSFORM(lnColFooter)

loForm.pgfArticole.page1._grdfooter1.calcTotal()
loForm.pgfArticole.page2._grdfooter1.attachtogrid(loForm.pgfArticole.page2.grdRulajeObinv)
loForm.pgfArticole.page2._grdfooter1.calcTotal()
loForm.Release()

IF m.lnColSrc > 0 AND m.gnErrCNRCRT = 0
	DO ReproLog WITH 'REZULTAT: OK'
ELSE
	DO ReproLog WITH 'REZULTAT: BUG (coloane pierdute sau eroare CNRCRT)'
ENDIF
QUIT

PROCEDURE ReproErr
	LPARAMETERS tnErr, tcMsg, tcProg, tnLine
	IF 'CNRCRT' $ UPPER(tcMsg)
		gnErrCNRCRT = gnErrCNRCRT + 1
	ENDIF
	DO ReproLog WITH 'ERR ' + TRANSFORM(tnErr) + ' [' + tcProg + ':' + TRANSFORM(tnLine) + '] ' + tcMsg
ENDPROC

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