* test_regresie_d394_baseline.prg - S13, Testul 2: captureaza baseline-ul D394 (randuri crsJTVA +
* timp) INAINTE de S2, pe o luna REALA deja depusa (MARIUSM_AUTO are un istoric real de facturi,
* 2006-2026 - nu e o schema goala). Luna aleasa: 03/2026 (30 facturi JC, 25 cu cod_fiscal RO, din
* care 2 CUI apar si in rtvai_istoric - vezi docs\diff-S13-teste-echivalenta.md pentru interogarile
* care au ales luna).
*
* S2 (pack_roartvai.PartenerInRTVAI cu precedenta pe istoric) NU e inca aplicata (handoff_cache_anaf.md:
* "MUTARE" in lucru, S2 asteapta rescrierea dupa mutarea tabelei) - nu exista "dupa" de comparat acum.
* Acest script CAPTUREAZA baseline-ul "inainte" si lasa harness-ul gata de rulat identic dupa S2.
*
* Ce face:
*  1. Reconstruieste manual setup-ul clasei D394_2025 (oUnitate + cursoarele create in ::List(),
*     Programe\oproceduri_decont.prg:8366-8517), FARA formularul modal (frm_decl394_2025.Show(1)
*     ar bloca headless) si FARA scrie_optiune (evita scrieri in OPTIUNI, tabela partajata cu alte
*     povesti care ruleaza in paralel pe aceeasi schema).
*  2. Cheama This.citeste_date_jtva() si This.configureaza_date_jtva() - PIPELINE REAL, neschimbat -
*     configureaza_date_jtva contine Update-ul per-rand cu VERIFICA_RTVAI_DATA (oproceduri_decont.prg:8929),
*     exact linia pe care S2 o afecteaza indirect (schimba raspunsul lui pack_roartvai.PartenerInRTVAI,
*     apelat de VERIFICA_RTVAI_DATA din oproceduri_comune.prg:5126).
*  3. Continua cu sectiunile 1_2/4_5/6/7/amef - pipeline-ul complet, ca sa poata compara si acolo.
*  4. Masoara timpul separat per etapa (citeste/configureaza/sectiuni), nu doar total.
*  5. Exporta CRSJTVA complet (COPY TO .dbf) - fisier baseline langa acest script, comparabil rand
*     cu rand (dupa SEQ) cu o rulare identica de dupa S2.
*
* NU modifica oproceduri_decont.prg (nu e fisierul meu - D394_2025 nu apare in nicio banda din
* stories-cache-anaf.md). Foloseste doar metode publice existente, neschimbate.

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_regresie_d394_baseline_log.txt"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcTestLog)

ON ERROR DO VpaErrD394 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', 2026, 3
IF gnHandle <= 0
	DO WriteTestLogD394 WITH 'EROARE: conectare Oracle esuata - GATA'
	QUIT
ENDIF
DO WriteTestLogD394 WITH 'mediu OK, schema=' + gcS + ' gnAn=' + TRANSFORM(gnAn) + ' gnLuna=' + TRANSFORM(gnLuna)

LOCAL loD394
loD394 = CREATEOBJECT('D394_2025')

* --- reconstructie manuala a setup-ului din D394_2025::List() (oproceduri_decont.prg:8366-8517),
* FARA formular si FARA scrie_optiune ---
* pounitate trebuie PRIVATE (nu Local) - pregateste_sectiunea_amef (:9395) o refera direct intr-un
* SELECT nativ VFP ca 'pounitate.nramef'; VFP rezolva asta doar daca variabila e vizibila in josul
* stivei de apeluri (Private), exact ca la capcana 2 din handoff_cache_anaf.md (SQL Oracle cu ?)
PRIVATE pounitate
pounitate = CREATEOBJECT("empty")
ADDPROPERTY(pounitate, "tva_incasare", goCalendar.tva_incasare)
ADDPROPERTY(pounitate, "an", gnAn)
ADDPROPERTY(pounitate, "luna1", gnLuna)
ADDPROPERTY(pounitate, "luna2", gnLuna)
ADDPROPERTY(pounitate, "tip", 'LUNAR')
ADDPROPERTY(pounitate, "luniextra", 0)
ADDPROPERTY(pounitate, "tvanedeductibil", 0)
ADDPROPERTY(pounitate, "codi", STRTRAN(STRTRAN(ALLTRIM(UPPER(NVL(goFirma.cod_fiscal, ''))), 'RO', ''), 'R', ''))
ADDPROPERTY(pounitate, "deni", NVL(ALLTRIM(goFirma.firma), ''))
ADDPROPERTY(pounitate, "adr", NVL(ALLTRIM(goFirma.adresa), ''))
pounitate.adr = PADR(pounitate.adr, 150, ' ')
ADDPROPERTY(pounitate, "tel", '')
ADDPROPERTY(pounitate, "fax", '')
ADDPROPERTY(pounitate, "email", '')
ADDPROPERTY(pounitate, "taraR", "")
ADDPROPERTY(pounitate, "codiR", "")
ADDPROPERTY(pounitate, "deniR", "")
ADDPROPERTY(pounitate, "adrR", "")
ADDPROPERTY(pounitate, "telR", "")
ADDPROPERTY(pounitate, "faxR", "")
ADDPROPERTY(pounitate, "emailR", "")
ADDPROPERTY(pounitate, "numeD", "")
ADDPROPERTY(pounitate, "functieD", "")
ADDPROPERTY(pounitate, "operatiuni", 1)
ADDPROPERTY(pounitate, "tvaincasare", goCalendar.tva_incasare)
ADDPROPERTY(pounitate, "nr_facturi_emise", 0)
ADDPROPERTY(pounitate, "nr_facturi_beneficiari", 0)
ADDPROPERTY(pounitate, "nr_facturi_terti", 0)
ADDPROPERTY(pounitate, "lPerioadaDoarRO", .F.)
ADDPROPERTY(pounitate, "ExportSectiuneaI", 1)
ADDPROPERTY(pounitate, "PersoaneAfiliate", 0)
ADDPROPERTY(pounitate, "DenRepr", '')
ADDPROPERTY(pounitate, "AdrRepr", '')
pounitate.AdrRepr = PADR(pounitate.AdrRepr, 150, ' ')
ADDPROPERTY(pounitate, "CuiRepr", '')
ADDPROPERTY(pounitate, "TipRepr", '')
ADDPROPERTY(pounitate, "TelRepr", '')
ADDPROPERTY(pounitate, "FaxRepr", '')
ADDPROPERTY(pounitate, "EmailRepr", '')
ADDPROPERTY(pounitate, "DenIntocmit", '')
ADDPROPERTY(pounitate, "CuiIntocmit", '')
ADDPROPERTY(pounitate, "PJIntocmit", 1)
ADDPROPERTY(pounitate, "CalitateIntocmit", PADR('', 100, ' '))
ADDPROPERTY(pounitate, "Calitate2Intocmit", PADR('', 100, ' '))
ADDPROPERTY(pounitate, "FunctieIntocmit", PADR('', 100, ' '))
ADDPROPERTY(pounitate, "NrAMEF", 1)
ADDPROPERTY(pounitate, "OptiuneANAF", 0)
ADDPROPERTY(pounitate, "cTextFacturiEmiseAlocate", "")

loD394.oUnitate = pounitate

IF USED('crsFdocTip')
	USE IN crsFdocTip
ENDIF
CREATE CURSOR crsFdocTip (id_fdoc N(10) null, fel_document c(100) null, tip_doc c(10) Null, bifa N(1))

LOCAL llSucces
llSucces = update_serii(.F., [isautofactura = 1 or isbeneficiari = 1 or isterti = 1 or isfurnizori = 1])
IF !llSucces
	IF USED('crsSerii')
		USE IN crsSerii
	ENDIF
	CREATE CURSOR crsSerii (seria c(20), isautofactura N(1), isbeneficiari N(1), isterti N(1), isfurnizori N(1))
ENDIF

IF USED('crsSectiuneaI')
	USE IN crsSectiuneaI
ENDIF
CREATE CURSOR crsSectiuneaI (punct c(4) Null, baza N(20) Null, TVA N(20) Null, proctva N(2) Null)
IF USED('crsSectiuneaI4')
	USE IN crsSectiuneaI4
ENDIF
CREATE CURSOR crsSectiuneaI4 (punct c(4) Null, TVA21 N(20) Null, TVA11 N(20) Null, TVA19 N(20) Null, TVA09 N(20) Null, TVA05 N(20) Null, TVA24 N(20) Null, TVA20 N(20) Null)
IF USED('crsSectiuneaI6')
	USE IN crsSectiuneaI6
ENDIF
CREATE CURSOR crsSectiuneaI6 (punct c(4) Null, incasari N(20) Null, costuri N(20) Null, marja N(20) Null, TVA N(20) Null)
IF USED('crsSectiuneaI7')
	USE IN crsSectiuneaI7
ENDIF
CREATE CURSOR crsSectiuneaI7 (caen c(4) Null, TIP c(10), baza N(20), TVA21 N(20) Null, TVA11 N(20) Null, TVA19 N(20) Null, TVA09 N(20) Null, TVA05 N(20) Null, TVA24 N(20) Null, TVA20 N(20) Null)

IF USED('crsFacturiAlocate')
	USE IN crsFacturiAlocate
ENDIF
CREATE CURSOR crsFacturiAlocate (seria c(20) Null, nr1 N(20), nr2 N(20))
IF USED('crsFacturiEmise')
	USE IN crsFacturiEmise
ENDIF
CREATE CURSOR crsFacturiEmise (seria c(20) Null, nr1 N(20), nr2 N(20))
IF USED('crsFacturiEmiseTip')
	USE IN crsFacturiEmiseTip
ENDIF
CREATE CURSOR crsFacturiEmiseTip (TIP c(20), ntip N(1), seria c(20) Null, nr N(20))
IF USED('crsAutoFacturi')
	USE IN crsAutoFacturi
ENDIF
CREATE CURSOR crsAutoFacturi (seria c(20) Null, nr N(20), baza21 N(16,4) Null, baza11 N(16,4) Null, baza19 N(16,4) Null, baza09 N(16,4) Null, baza05 N(16,4) Null, baza24 N(16,4) Null, baza20 N(16,4) Null, ;
	TVA21 N(16,4) Null, TVA11 N(16,4) Null, TVA19 N(16,4) Null, TVA09 N(16,4) Null, TVA05 N(16,4) Null, TVA24 N(16,4) Null, TVA20 N(16,4) Null)

IF USED('crsFacturiTerti')
	USE IN crsFacturiTerti
ENDIF
CREATE CURSOR crsFacturiTerti (punct c(4) Null, seria c(20) Null, nr1 N(20), nr2 N(20), denumire c(100) Null, cod_fiscal c(20) Null)
IF USED('crsSeriiFacturi')
	USE IN crsSeriiFacturi
ENDIF
CREATE CURSOR crsSeriiFacturi (TIP c(20), seria c(20), descriere c(100))
INSERT INTO crsSeriiFacturi (TIP, seria, descriere) ;
	SELECT 'AUTOFACTURA' As TIP, serie As seria, 'x' As descriere FROM crsSerii WHERE isautofactura = 1 ;
	UNION SELECT 'BENEFICIARI', serie, 'x' FROM crsSerii WHERE isbeneficiari = 1 ;
	UNION SELECT 'TERTI', serie, 'x' FROM crsSerii WHERE isterti = 1 ;
	UNION SELECT 'FURNIZORI', serie, 'x' FROM crsSerii WHERE isfurnizori = 1

ADDPROPERTY(pounitate, "serie_autofacturare", cursor2lista("crsSerii", "serie", ",", [isautofactura=1]))
ADDPROPERTY(pounitate, "serie_facturi_beneficiari", cursor2lista("crsSerii", "serie", ",", [isbeneficiari=1]))
ADDPROPERTY(pounitate, "serie_facturi_terti", cursor2lista("crsSerii", "serie", ",", [isterti=1]))
ADDPROPERTY(pounitate, "serie_facturi_furnizori", cursor2lista("crsSerii", "serie", ",", [isfurnizori=1]))
close_serii()

IF USED('crsAMEF')
	USE IN crsAMEF
ENDIF
CREATE CURSOR crsAMEF (TIP N(1), luna N(2), nramef N(10), nrbonuri N(10), incasari N(16,4), ;
	baza21 N(16,4) Null, baza11 N(16,4) Null, baza19 N(16,4) Null, baza09 N(16,4) Null, baza05 N(16,4) Null, baza20 N(16,4) Null, ;
	TVA21 N(16,4) Null, TVA11 N(16,4) Null, TVA19 N(16,4) Null, TVA09 N(16,4) Null, TVA05 N(16,4) Null, TVA20 N(16,4) Null)

IF USED('crsJTVA')
	USE IN crsJTVA
ENDIF
CREATE CURSOR crsJTVA(seq N(20), tipj c(2), tippart N(1), tipdoc c(2) Null, TIP c(2), dataact D Null, dataireg D Null, serie_act c(10) Null, nract N(20) Null, id_fdoc N(12) Null, fel_document c(30) Null, id_sucursala N(7) Null, sucursala c(100) Null, ;
	iscif N(1) Null, cifvalid N(1) Null, id_part N(20) Null, cod_fiscal c(30) Null, cod_fiscalfro c(30) Null, atribut_fiscal c(2) Null, denumire c(100) Null, TIP_PERSOANA N(1) Null, AFILIAT N(5) Null, CODTARA V(5) NULL, ;
	TARA c(100) Null, COD_TARA c(10) Null, JUDET c(100) Null, COD_JUDET c(10) Null, LOCALITATE c(100) Null, STRADA c(100) Null, NUMAR c(20) Null, Bloc c(20) Null, SCARA c(50) Null, APART c(100) Null, ;
	baza N(16,4) Null, TVA N(16,4) Null, tibaza N(16,4) Null, titva N(16,4) Null, ;
	baza21 N(16,4) Null, baza11 N(16,4) Null, baza19 N(16,4) Null, baza09 N(16,4) Null, baza05 N(16,4) Null, baza24 N(16,4) Null, baza20 N(16,4) Null, baza00 N(16,4) Null, ;
	TVA21 N(16,4) Null, TVA11 N(16,4) Null, TVA19 N(16,4) Null, TVA09 N(16,4) Null, TVA05 N(16,4) Null, TVA24 N(16,4) Null, TVA20 N(16,4) Null, ;
	tibaza21 N(16,4) Null, tibaza11 N(16,4) Null, tibaza19 N(16,4) Null, tibaza09 N(16,4) Null, tibaza05 N(16,4) Null, tibaza24 N(16,4) Null, tibaza20 N(16,4) Null, ;
	titva21 N(16,4) Null, titva11 N(16,4) Null, titva19 N(16,4) Null, titva09 N(16,4) Null, titva05 N(16,4) Null, titva24 N(16,4) Null, titva20 N(16,4) Null, ;
	tva_incasare N(1), FACT_TVA_INCASARE N(1), NRFACT N(1), EMISA_PERIOADA N(1), ;
	ro21t N(16,4) Null, ro11t N(16,4) Null, ro19t N(16,4) Null, RO09T N(16,4) Null, ro05t N(16,4) Null, ro24t N(16,4) Null, ro20t N(16,4) Null)

* --- rulare pipeline REAL, neschimbat, cu masurare de timp per etapa ---
TRY
	LOCAL lnT0, lnT1, lnT2, lnT3, loExcD394
	lnT0 = SECONDS()
	llSucces = loD394.citeste_date_jtva()
	lnT1 = SECONDS()
	DO WriteTestLogD394 WITH 'citeste_date_jtva: succes=' + TRANSFORM(llSucces) + ' timp=' + TRANSFORM(lnT1-lnT0) + 's randuri crsJTVA=' + TRANSFORM(IIF(USED('crsJTVA'),RECCOUNT('crsJTVA'),-1))

	IF llSucces AND USED('crsJTVA')
		llSucces = loD394.configureaza_date_jtva()
	ENDIF
	lnT2 = SECONDS()
	DO WriteTestLogD394 WITH 'configureaza_date_jtva (contine Update...VERIFICA_RTVAI_DATA per rand, oproceduri_decont.prg:8929): succes=' + TRANSFORM(llSucces) + ' timp=' + TRANSFORM(lnT2-lnT1) + 's'

	IF llSucces
		llSucces = loD394.pregateste_sectiunea_1_2()
	ENDIF
	IF llSucces
		llSucces = loD394.pregateste_sectiunea_4_5()
	ENDIF
	IF llSucces
		llSucces = loD394.pregateste_sectiunea_6()
	ENDIF
	IF llSucces
		llSucces = loD394.pregateste_sectiunea_7()
	ENDIF
	IF llSucces
		llSucces = loD394.pregateste_sectiunea_amef()
	ENDIF
	lnT3 = SECONDS()
	DO WriteTestLogD394 WITH 'sectiuni 1_2/4_5/6/7/amef: succes=' + TRANSFORM(llSucces) + ' timp=' + TRANSFORM(lnT3-lnT2) + 's'
	DO WriteTestLogD394 WITH 'TOTAL echivalent do_citeste_date: ' + TRANSFORM(lnT3-lnT0) + 's'

	IF USED('crsJTVA')
		LOCAL lnRandCrsJTVA, lnAI, lcBaseline
		lnRandCrsJTVA = RECCOUNT('crsJTVA')
		SELECT COUNT(*) AS nr FROM crsJTVA WHERE TIP = 'AI' INTO CURSOR crsNrAI
		lnAI = crsNrAI.nr
		USE IN crsNrAI
		DO WriteTestLogD394 WITH 'crsJTVA: randuri=' + TRANSFORM(lnRandCrsJTVA) + ' randuri TIP=AI (achizitii TVA la incasare, afectate de VERIFICA_RTVAI_DATA)=' + TRANSFORM(lnAI)

		lcBaseline = "D:\ROA\ROACONT\COMUN\utile\Teste\cache_anaf\baseline_d394_2026_03_crsjtva_inainte_s2.dbf"
		IF FILE(lcBaseline)
			DELETE FILE (lcBaseline)
		ENDIF
		SELECT crsJTVA
		GO TOP
		COPY TO (lcBaseline)
		DO WriteTestLogD394 WITH 'baseline salvat: ' + lcBaseline
	ELSE
		DO WriteTestLogD394 WITH 'EROARE: crsJTVA nu exista dupa pipeline'
	ENDIF

CATCH TO loExcD394
	DO WriteTestLogD394 WITH 'CATCH ErrorNo=' + TRANSFORM(loExcD394.ErrorNo) + ' Message=' + loExcD394.Message + ' Proc=' + loExcD394.Procedure + ' Line=' + TRANSFORM(loExcD394.LineNo)
ENDTRY

DO WriteTestLogD394 WITH 'GATA'
QUIT

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

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