* test_margine_tvaincasare_frontiera.prg - S13, test de REGRESIE pentru fixul de 01.08.2026 la
* marginea tvaincasare (validare.prg:2708, "nvl(data_sfarsit_tvainc - 1, ...)" - acum EXCLUSIV).
* Scris initial (versiune veche, pastrata in istoricul git) ca sa EXPUNA divergenta cache/RTVAI la
* frontiera - a reusit, si a produs sectiunea A-ter din handoff (decizie Marius: cache aliniat la
* conventia -1 folosita deja de pack_roartvai.PartenerInRTVAI). Dupa fix, testul verifica ACORDUL.
*
* CAZUL: CUI real 1768121, o singura perioada RTVAI continua 01.01.2013 -> 01.01.2021, FARA nicio
* reintrare ulterioara in rtvai_istoric (un singur rand I si un singur rand D, nimic dupa). Data
* 01.01.2021 e eveniment real (publicat 15.10.2020, inainte ca importul RTVAI sa moara pe
* 23.10.2020) - nu artefact de import oprit.
*
* Fara CREATEOBJECT/ADDPROPERTY in interiorul unei PROCEDURE (capcana l, depanare_testare_vfp.md) -
* obiectele se construiesc INLINE in programul principal.

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

PUBLIC gcTestLog
gcTestLog = "D:\ROA\ROACONT\COMUN\utile\Teste\cache_anaf\test_margine_tvaincasare_frontiera_log.txt"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcTestLog)

ON ERROR DO VpaErrTvainc 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 WriteTestLogTvainc WITH 'EROARE: conectare Oracle esuata - GATA'
	QUIT
ENDIF
DO WriteTestLogTvainc WITH 'mediu OK, schema=' + gcS

*!* 01.08.2026 marius.mutu: watermark pe ID, inainte de orice scriere - garda pt DELETE-ul din CurataIstoricTvaincTest
PUBLIC gnIdMaxIstoricTvainc
gnIdMaxIstoricTvainc = 0
IF goExecutor.oExecuta('SELECT NVL(MAX(ID),0) AS IDMAX FROM ISTORIC_CODURI_FISCALE', 'crsWmIstoricTvainc')
	SELECT crsWmIstoricTvainc
	GO TOP
	gnIdMaxIstoricTvainc = IDMAX
	USE IN (SELECT('crsWmIstoricTvainc'))
ENDIF
*!* 01.08.2026 marius.mutu: watermark invalid = SELECT esuat - avertisment explicit, stergerile se vor sari
IF gnIdMaxIstoricTvainc <= 0
	DO WriteTestLogTvainc WITH 'ATENTIE: watermark invalid (0) - stergerile din ISTORIC_CODURI_FISCALE vor fi SARITE'
ENDIF
DO WriteTestLogTvainc WITH 'watermark ISTORIC_CODURI_FISCALE id_max=' + TRANSFORM(gnIdMaxIstoricTvainc)

PUBLIC goVerifTvainc
goVerifTvainc = CREATEOBJECT('VerificareANAF')

DO CurataIstoricTvaincTest

LOCAL lnCui, ldInceput, ldSfarsit
lnCui = 1768121
ldInceput = {^2013-01-01}
ldSfarsit = {^2021-01-01}

* -- seed cache cu EXACT aceeasi perioada ca in rtvai_istoric (aceeasi forma ca crsANAF) --
IF USED('crsSeedTvainc')
	USE IN crsSeedTvainc
ENDIF
CREATE CURSOR crsSeedTvainc (cui N(13), data D, denumire V(100), firma V(100), adresa C(244), nrRegCom C(30), ;
	scpTVA L, data_inceput_ScpTVA D, data_sfarsit_ScpTVA D, data_anul_imp_ScpTVA D, mesaj_ScpTVA C(244), ;
	dataInceputTvaInc D, dataSfarsitTvaInc D, dataActualizareTvaInc D, dataPublicareTvaInc D, tipActTvaInc C(100), statusTvaIncasare L, ;
	dataInactivare D, dataReactivare D, dataPublicare D, dataRadiere D, statusInactivi L, ;
	dataInceputSplitTVA D, dataAnulareSplitTVA D, tva L, tvaincasare L, splittva L, inactiv L, ;
	mesaj V(100), judet V(100), strada V(100), nr V(20), localitate V(100), Bloc V(20), scara V(20), etaj V(20), apart V(20), ;
	sursa C(20), data_sursa D)
INSERT INTO crsSeedTvainc (cui, data, dataInceputTvaInc, dataSfarsitTvaInc, tvaincasare) ;
	VALUES (lnCui, DATE(), ldInceput, ldSfarsit, .T.)
goVerifTvainc.SalveazaIstoricDinCursor('crsSeedTvainc', gnIdUtil)
USE IN crsSeedTvainc
DO WriteTestLogTvainc WITH 'seed cache: cui=' + TRANSFORM(lnCui) + ' dataInceputTvaInc=' + DTOC(ldInceput) + ' dataSfarsitTvaInc=' + DTOC(ldSfarsit)

* -- 4 date de proba: interior, o zi inainte de sfarsit, EXACT sfarsitul (marginea care era
* divergenta inainte de fix), o zi dupa sfarsit - toate patru trebuie ACORD dupa fix --
IF USED('crsProbeTvainc')
	USE IN crsProbeTvainc
ENDIF
CREATE CURSOR crsProbeTvainc (eticheta C(40), proba D, asteptare C(20))
INSERT INTO crsProbeTvainc VALUES ('interior (8 ani in perioada)', {^2020-01-01}, 'ACORD')
INSERT INTO crsProbeTvainc VALUES ('o zi inainte de sfarsit',      ldSfarsit - 1, 'ACORD')
INSERT INTO crsProbeTvainc VALUES ('EXACT sfarsitul (frontiera)',  ldSfarsit,     'ACORD')
INSERT INTO crsProbeTvainc VALUES ('o zi dupa sfarsit',            ldSfarsit + 1, 'ACORD')

LOCAL lnTotalProbe, lnAcordCorect, lnNeasteptat
lnTotalProbe = 0
lnAcordCorect = 0
lnNeasteptat = 0

SELECT crsProbeTvainc
SCAN
	LOCAL lcEticheta, ldProba, lcAsteptare, llCacheTvainc, llCacheAbtine, lnRtvaiVal
	lcEticheta = eticheta
	ldProba = proba
	lcAsteptare = asteptare

	* -- termen (a): CACHE REAL, formula acum exclusiva (validare.prg:2708, "- 1", fixul de 01.08.2026) --
	IF USED('crsCacheTvainc')
		USE IN crsCacheTvainc
	ENDIF
	goVerifTvainc.CitesteIstoricPentruCoduri(TRANSFORM(lnCui), ldProba, 'crsCacheTvainc')
	llCacheAbtine = .T.
	llCacheTvainc = .F.
	IF USED('crsCacheTvainc')
		SELECT crsCacheTvainc
		LOCATE FOR cui = lnCui
		IF FOUND()
			llCacheAbtine = .F.
			llCacheTvainc = tvaincasare
		ENDIF
	ENDIF

	* -- termen (c): RTVAI REAL, pack_roartvai.PartenerInRTVAI (neschimbata, foloseste -1 la sfarsit) --
	lnRtvaiVal = ObtineVerdictRTVAITvainc(lnCui, ldProba)

	lnTotalProbe = lnTotalProbe + 1
	IF !llCacheAbtine AND (IIF(llCacheTvainc,1,0) = lnRtvaiVal)
		lnAcordCorect = lnAcordCorect + 1
		DO WriteTestLogTvainc WITH '  ' + lcEticheta + ' (' + DTOC(ldProba) + '): ACORD (asteptat) - cache(tvaincasare=' + TRANSFORM(llCacheTvainc) + ') rtvai=' + TRANSFORM(lnRtvaiVal)
	ELSE
		lnNeasteptat = lnNeasteptat + 1
		DO WriteTestLogTvainc WITH '  ' + lcEticheta + ' (' + DTOC(ldProba) + '): NEASTEPTAT - cache abtine=' + TRANSFORM(llCacheAbtine) + ' cache(tvaincasare=' + TRANSFORM(llCacheTvainc) + ') rtvai=' + TRANSFORM(lnRtvaiVal)
	ENDIF

	IF USED('crsCacheTvainc')
		USE IN crsCacheTvainc
	ENDIF
	SELECT crsProbeTvainc
ENDSCAN

DO WriteTestLogTvainc WITH 'REZULTAT: total=' + TRANSFORM(lnTotalProbe) + ' acord=' + TRANSFORM(lnAcordCorect) + '/4 (asteptat 4 - regresie fix, inclusiv EXACT sfarsitul)' + ;
	' neasteptat=' + TRANSFORM(lnNeasteptat) + ' (asteptat 0)'

DO CurataIstoricTvaincTest

DO WriteTestLogTvainc WITH 'GATA'
QUIT

*-- verdict "TVA la incasare la data D" din rtvai_istoric COMPLET (istoricul importat, nu ANAF),
*-- prin procedura Oracle REALA pack_roartvai.PartenerInRTVAI (neschimbata) - aceeasi ca in
*-- test_echivalenta_cache_rtvai.prg::ObtineVerdictRTVAI --
PROCEDURE ObtineVerdictRTVAITvainc
	LPARAMETERS tnCui, tdData
	PRIVATE pnCodFiscal, pdData, pcDenumire, pnRTVAI
	pnCodFiscal = tnCui
	pdData = tdData
	pcDenumire = ''
	pnRTVAI = 0
	LOCAL lcSql
	lcSql = [begin pack_roartvai.PartenerInRTVAI(?pnCodFiscal, ?pdData, ?@pcDenumire, ?@pnRTVAI); end;]
	IF !goExecutor.oExecuta(lcSql)
		RETURN -1
	ENDIF
	RETURN pnRTVAI
ENDPROC

PROCEDURE WriteTestLogTvainc
	LPARAMETERS tcMesaj
	STRTOFILE(TTOC(DATETIME()) + ' ' + tcMesaj + CHR(13)+CHR(10), gcTestLog, 1)
ENDPROC

PROCEDURE VpaErrTvainc
	LPARAMETERS tnErr, tcMsg, tcProg, tnLine
	DO WriteTestLogTvainc WITH 'ON ERROR nEroare=' + TRANSFORM(tnErr) + ' mesaj=' + tcMsg + ' program=' + tcProg + ' linia=' + TRANSFORM(tnLine)
ENDPROC

*-- sterge DOAR CUI-ul de test al acestui fisier din ISTORIC_CODURI_FISCALE --
PROCEDURE CurataIstoricTvaincTest
	LOCAL lnSterse
	*!* 01.08.2026 marius.mutu: watermark invalid - nu sterg nimic (fara el, DELETE ar lovi tot istoricul comun)
	IF TYPE('gnIdMaxIstoricTvainc') <> 'N' OR gnIdMaxIstoricTvainc <= 0
		DO WriteTestLogTvainc WITH 'CurataIstoricTvaincTest: SARIT - watermark invalid, nu sterg nimic'
		RETURN
	ENDIF
	*!* 01.08.2026 marius.mutu: garda pe ID > watermark (nu SCHEMA_SURSA - stergea randuri de referinta proprii)
	lnSterse = goExecutor.oExecuta([DELETE FROM ISTORIC_CODURI_FISCALE WHERE ID > ] + TRANSFORM(gnIdMaxIstoricTvainc) + [ AND COD_FISCAL_N IN (1768121)])
	=goExecutor.oExecuta('COMMIT')
	DO WriteTestLogTvainc WITH 'CurataIstoricTvaincTest: sters=' + TRANSFORM(lnSterse)
ENDPROC
