* test_import_zip_efactura.prg
* Test UI pentru importul manual al unei arhive zip eFactura in borderou
* (COMUN\clase\anaf_efactura.vcx > clasa anaf_efactura + AnafeFacturaServer.ImportZipLocal).
* Mediu: CENTRAL / MARIUSM_AUTO, perioada ianuarie 2026 (data facturii din xml).
*
* Ce acopera:
*   T1 - garda pe cod fiscal: arhiva nu apartine firmei de test => import respins, 0 randuri
*   T2 - import complet cu codul fiscal al beneficiarului din xml => anaf_efactura + detalii
*   T3 - factura apare in grila "Facturi primite" a borderoului real (captura de ecran)
* GETFILE (alegerea fisierului) NU poate fi automatizat headless - functie nativa VFP,
* nu poate fi umbrita de un mock - de aceea arhiva se transmite ca parametru.
*
* Rulare: powershell -ExecutionPolicy Bypass -File test_import_zip_efactura.ps1

SET SAFETY OFF
SET TALK OFF

PUBLIC gcUILog, gcSyncDir, gnMockRaspuns, gcTestDir, gcZipFile, gcIdInc, gcCifBenef
PUBLIC poFacturiEmise, poFacturiPrimite, poFacturiTrimise, poFacturiDetalii
PUBLIC loAnaf, loForm, gnPass, gnFail

gcTestDir = 'D:\ROA\ROACONT\COMUN\utile\Teste\efactura_import\'
gcUILog = gcTestDir + 'test_import_zip_efactura_log.txt'
gcSyncDir = gcTestDir + 'uisync_importzip\'
gcZipFile = 'D:\ROA\ROACONT\5967557693.zip'
gcIdInc = '5967557693'
gcCifBenef = '29923675'
gnMockRaspuns = 6
gnPass = 0
gnFail = 0

STRTOFILE('START ' + TTOC(DATETIME()) + CHR(13) + CHR(10), gcUILog)

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

* Mock-urile se incarca INAINTE de test_init_env_auto: la nume duplicat de procedura
* castiga fisierul incarcat PRIMUL (vezi docs\testare-ui-vfp.md, punctul j)
SET PROCEDURE TO ('D:\ROA\ROACONT\COMUN\utile\Teste\mock_amessagebox.prg') ADDITIVE
SET PROCEDURE TO ('D:\ROA\ROACONT\COMUN\utile\Teste\ui_harness.prg') ADDITIVE

DO ('D:\ROA\ROACONT\COMUN\utile\Teste\test_init_env_auto.prg') WITH 'CENTRAL', 'MARIUSM_AUTO', 'ROMFASTSOFT', 2026, 1

DO HarnessLog WITH 'INIT gnHandle=' + TRANSFORM(gnHandle) + ' gcS=' + TRANSFORM(gcS) + ' perioada=' + TRANSFORM(gnLuna) + '/' + TRANSFORM(gnAn)
DO HarnessLog WITH 'FIRMA=' + TRANSFORM(gcFirma) + ' cod fiscal=' + TRANSFORM(goFirma.codfiscalfro)

* Curatenie: sterg importul unei rulari anterioare, ca testul sa fie repetabil
goExecutor.oExecuta("delete from anaf_efactura_detalii where id_efactura in (select id from anaf_efactura where id_incarcare = '" + gcIdInc + "')")
goExecutor.oExecuta("delete from anaf_efactura where id_incarcare = '" + gcIdInc + "'")
DO HarnessLog WITH 'CURATENIE randuri anterioare OK'

loAnaf = CREATEOBJECT('ANAFeFactura')
DO HarnessLog WITH 'ANAFeFactura creat; cod fiscal server=' + TRANSFORM(loAnaf.AnafeFacturaServer.cCodFiscal) + ' lTest=' + TRANSFORM(loAnaf.AnafeFacturaServer.lTest)

* ---------- cursoarele borderoului, copie exacta din ANAFeFactura.vizeFactura (anaf_efactura.prg:2867-2942) ----------
		* FACTURI PRIMITE
		lcData1 = '01' + PADL(INT(m.gnLuna),2,'0') + ALLTRIM(STR(m.gnAn))
		Text To lcSchema Noshow
ales N(1), id N(20), id_fact N(20), data_act D, data_scad D, numar_act C(30), xfurnizor C(200), xclient C(200), id_incarcare C(36), id_descarcare c(36) null, tip_mesaj_raspuns C(50) null,mesaj_raspuns C(250) null, data_raspuns D null, trimis N(1) null, data_trimis T null, cod_fiscal_emitent C(30) null, cod_fiscal_beneficiar C(30) null, detalii M null, total_fara_tva N(16,4), total_tva N(16,4), total_tva_ron N(16,4), total_cu_tva N(16,4), discount_fara_tva N(16,4), taxe_fara_tva N(16,4), valoare_fara_tva N(16,4), total_de_plata N(16,4), nume_valuta C(5), test N(1) null, jtotctva N(16,4), descriere M null, detalii_plata M null, diferenta N(16,4), procesat N(1), tip_persoana N(1) null, descarcat N(1) null, filepath c(250) null, procesat_server N(1) null, creditnote N(1)
		Endtext
		Text To lcSelect Noshow
SELECT 0 as ales, id, id_fact, data_act, data_scad, numar_act, xfurnizor, xclient, id_incarcare, id_descarcare, tip_mesaj_raspuns, mesaj_raspuns, data_raspuns, NVL(trimis,0) as trimis, data_trimis, cod_fiscal_emitent, cod_fiscal_beneficiar, '' as detalii, total_fara_tva, total_tva, total_tva_ron, total_cu_tva, discount_fara_tva, taxe_fara_tva, valoare_fara_tva, total_de_plata, nume_valuta, test, jtotctva, descriere, detalii_plata, decode(creditnote,0,1,-1) * NVL(total_cu_tva, 0.00)-NVL(jtotctva, 0.00) as diferenta, procesat, tip_persoana, descarcat, filepath, procesat_server, creditnote FROM anaf_vefactura_primit
		Endtext
		lcOrder = [data_act,numar_act,data_raspuns]
		lcgroup = []
		* Aduc inregistrarile cand se activeaza prima data pagina cu facturi primite
		lcFiltru = [1=2] && [(extract(year from data_act) = ?gnAn and extract(month from data_act) = ?gnLuna) or (data_act is null and data_raspuns >= to_date('] + m.lcData1 + [','ddmmyyyy'))]	
		lcFiltruOriginal = []
		llModParam = .T.
		llAfiseaza = .F.

		gencursor('poFacturiPrimite', 'crsFacturiPrimite', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza,  lcgroup, llModParam, lcFiltruOriginal)
		poFacturiPrimite.ca_baza1.afisare()
		Select crsFacturiPrimite
		Go Top

		* FACTURI TRIMISE
		Text To lcSchema Noshow
ales N(1), id N(20), id_fact N(20), data_act D, data_scad D, numar_act C(30), xfurnizor C(200), xclient C(200), id_incarcare C(36), id_descarcare c(36) null, mesaj_trimis C(250) null, tip_mesaj_raspuns C(50) null,mesaj_raspuns C(250) null, data_raspuns D null, trimis N(1) null, data_trimis T null, cod_fiscal_emitent C(30) null, cod_fiscal_beneficiar C(30) null, detalii M null, total_fara_tva N(16,4), total_tva N(16,4), total_tva_ron N(16,4), total_cu_tva N(16,4), discount_fara_tva N(16,4), taxe_fara_tva N(16,4), valoare_fara_tva N(16,4), total_de_plata N(16,4), nume_valuta C(5), test N(1) null, jtotctva N(16,4), descriere M null, detalii_plata M null, diferenta N(16,4), procesat N(1), tip_persoana N(1) null, descarcat N(1) null, filepath c(250) null, procesat_server N(1) null, creditnote N(1)
		Endtext
		Text To lcSelect Noshow
SELECT 0 as ales, id, id_fact, data_act, data_scad, numar_act, xfurnizor, xclient, id_incarcare, id_descarcare, mesaj_trimis, tip_mesaj_raspuns, mesaj_raspuns, data_raspuns, NVL(trimis,0) as trimis, data_trimis, cod_fiscal_emitent, cod_fiscal_beneficiar, '' as detalii, total_fara_tva, total_tva, total_tva_ron, total_cu_tva, discount_fara_tva, taxe_fara_tva, valoare_fara_tva, total_de_plata, nume_valuta, test, jtotctva, descriere, detalii_plata, decode(creditnote,0,1,-1) * NVL(total_cu_tva, 0.00)-NVL(jtotctva, 0.00) as diferenta, procesat, tip_persoana, descarcat, filepath, procesat_server, creditnote FROM anaf_vefactura_trimis
		Endtext
		lcOrder = [data_act,numar_act,data_trimis,data_raspuns]
		lcgroup = []
		* Aduc inregistrarile cand se activeaza prima data pagina cu facturi trimise
		lcFiltru = [1=2] && [(extract(year from data_act) = ?gnAn and extract(month from data_act) = ?gnLuna) or (data_act is null and data_raspuns >= to_date('] + m.lcData1 + [','ddmmyyyy'))]	
		lcFiltruOriginal = []
		llModParam = .T.
		llAfiseaza = .F.

		gencursor('poFacturiTrimise', 'crsFacturiTrimise', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza,  lcgroup, llModParam, lcFiltruOriginal)
		poFacturiTrimise.ca_baza1.afisare()
		Select crsFacturiTrimise
		Go Top

		* FACTURI EMISE
		Text To lcSchema Noshow
ales N(1), id N(20), id_fact N(20), id_vanzare N(10), data_act D, numar_act N(14), in_valuta N(1) null, curs N(10,4) null, valval N(16,2), tvaval N(16,2),totval N(16,2), total_fara_tva N(16,2), total_tva N(16,2), total_cu_tva N(16,2), nume_valuta C(5) null, client C(100), id_client N(10), cod_fiscal C(30), tip_persoana N(1) null, trimis N(1) null, data_trimis T null, stare C(100) null, id_incarcare C(36) null, mesaj_trimis C(250) null, id_descarcare c(36) null, tip_mesaj_raspuns C(50) null,mesaj_raspuns C(250) null, data_raspuns D null, cod_fiscal_emitent C(30) null, cod_fiscal_beneficiar C(30) null, detalii M null, xfurnizor C(200), xclient C(200), xdata_act D, xnumar_act C(30), xtotal_fara_tva N(16,4), xtotal_tva N(16,4), xtotal_tva_ron N(16,4), xtotal_cu_tva N(16,4), xdiscount_fara_tva N(16,4), xtaxe_fara_tva N(16,4), xvaloare_fara_tva N(16,4), xtotal_de_plata N(16,4), xnume_valuta C(5), test N(1) null, id_util N(10), utilizator C(100), jtotctva N(16,4), diferenta N(16,4), procesat  N(1), procesat_server N(1), creditnote N(1)
		Endtext
		Text To lcSelect Noshow
SELECT 0 as ales, id, id_fact, id_vanzare, data_act, numar_act, in_valuta, curs, valval, tvaval, totval, total_fara_tva, total_tva,total_cu_tva, nume_valuta, client, id_client, cod_fiscal, NVL(tip_persoana,1) as tip_persoana, NVL(trimis,0) as trimis, data_trimis, stare, id_incarcare, mesaj_trimis, id_descarcare, tip_mesaj_raspuns, mesaj_raspuns, data_raspuns, cod_fiscal_emitent, cod_fiscal_beneficiar, '' as detalii, xfurnizor, xclient, xdata_act, xnumar_act, xtotal_fara_tva, xtotal_tva, xtotal_tva_ron, xtotal_cu_tva, xdiscount_fara_tva, xtaxe_fara_tva, xvaloare_fara_tva, xtotal_de_plata, xnume_valuta, test, id_util, utilizator, jtotctva, decode(creditnote,0,1,-1) * ROUND(NVL(xtotal_cu_tva , 0.00) * decode(in_valuta,1,curs,1), 2)-NVL(jtotctva, 0.00) as diferenta, procesat, procesat_server,creditnote FROM anaf_vefactura_emis
		Endtext
		lcOrder = [data_act,numar_act]
		lcgroup = []
		lcFiltru = [(extract(year from data_act)=?gnAn and extract(month from data_act)=?gnLuna) OR (trimis = 1 and tip_mesaj_raspuns is null and extract(year from data_act) >=2023)]	
		lcFiltruOriginal = []
		llModParam = .T.
		llAfiseaza = .F.
		
		* Afisez toate facturile emise, ca sa le pot vizualiza pe cele netrimise sau trimise cu starea lor (trimis, eroare etc.)
		gencursor('poFacturiEmise', 'crsFacturiEmise', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza,  lcgroup, llModParam, lcFiltruOriginal)
		poFacturiEmise.ca_baza1.afisare()


* DETALII FACTURI PRIMITE/TRIMISE/EMISE
		Text To lcSchema Noshow
id  N(20), id_efactura N(20), nr N(5), articol V(250), descriere V(250), detalii M, cantitate N(18,6), um V(20), pret N(20,6), proctva N(7,2), valoarefaratva N(20,6), discountfaratva N(20,6)
		Endtext
		Text To lcSelect Noshow
select id, id_efactura, nr, articol, descriere, detalii, cantitate, um, pret, proctva, valoarefaratva, discountfaratva from anaf_vefactura_detalii
		Endtext
		lcOrder = [nr]
		lcgroup = []
		lcFiltru = [1=2] && IIF(!EMPTY(NVL(crsFacturiPrimite.id,0)), [id_efactura=] + ALLTRIM(STR(crsFacturiPrimite.id)), [1=2])
		lcFiltruOriginal = []
		llModParam = .T.
		llAfiseaza = .F.

		gencursor('poFacturiDetalii', 'crsDetaliiFacturi', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza,  lcgroup, llModParam, lcFiltruOriginal)
		poFacturiDetalii.ca_baza1.afisare()

* ---------- Formularul REAL al borderoului, non-modal (Show(1) ar bloca testul) ----------
Select crsFacturiEmise
loForm = CREATEOBJECT('anaf_efactura', loAnaf)
loForm.WindowType = 0
loForm.Show()
loForm.WindowState = 2
DO HarnessLog WITH 'BORDEROU deschis: [' + TRANSFORM(loForm.Caption) + ']'
DO HarnessStep WITH 0, 'borderou_deschis'

* ---------- T1: garda pe cod fiscal (firma de test nu e nici emitent, nici beneficiar) ----------
llRezultat = loForm.oAnaf.AnafeFacturaServer.ImportZipLocal(gcZipFile)
lnRanduri = 0
goExecutor.oSelecteaza2Value("select count(*) from anaf_efactura where id_incarcare = '" + gcIdInc + "'", @lnRanduri)
DO TVerdict WITH !llRezultat AND NVL(lnRanduri, 0) = 0, 'T1 garda cod fiscal', 'import respins=' + TRANSFORM(!llRezultat) + ' randuri in anaf_efactura=' + TRANSFORM(lnRanduri)
DO HarnessStep WITH 1, 'garda_cod_fiscal'

* ---------- T2: import complet, cu codul fiscal al beneficiarului din xml ----------
loForm.oAnaf.AnafeFacturaServer.cCodFiscal = gcCifBenef
llRezultat = loForm.oAnaf.AnafeFacturaServer.ImportZipLocal(gcZipFile)
DO TVerdict WITH llRezultat, 'T2 import', 'ImportZipLocal a returnat ' + TRANSFORM(llRezultat)

* SQL in TEXT/ENDTEXT: un literal string mai lung de 255 caractere nu compileaza
TEXT TO lcSqlVerif NOSHOW
select id, xnumar_act, xdata_act, xdata_scad, xtotal_fara_tva, xtotal_tva, xtotal_cu_tva, xnume_valuta,
       xfurnizor, xclient, tip_mesaj_raspuns, factura_emisa, cod_fiscal_emitent, cod_fiscal_beneficiar,
       descarcat, procesat, creditnote, filepath,
       nvl(length(detalii),0) as len_detalii, nvl(length(detalii_zip),0) as len_zip
  from anaf_efactura
 where id_incarcare = ?gcIdInc
ENDTEXT
goExecutor.oExecuta(lcSqlVerif, 'cVerif')

IF USED('cVerif') AND RECCOUNT('cVerif') = 1
	SELECT cVerif
	GO TOP
	DO HarnessLog WITH 'ANAF_EFACTURA id=' + TRANSFORM(cVerif.id) + ' numar=[' + TRANSFORM(cVerif.xnumar_act) + '] data=' + TRANSFORM(cVerif.xdata_act) + ' scadenta=' + TRANSFORM(cVerif.xdata_scad)
	DO HarnessLog WITH '  furnizor=[' + TRANSFORM(cVerif.xfurnizor) + '] client=[' + TRANSFORM(cVerif.xclient) + ']'
	DO HarnessLog WITH '  fara tva=' + TRANSFORM(cVerif.xtotal_fara_tva) + ' tva=' + TRANSFORM(cVerif.xtotal_tva) + ' cu tva=' + TRANSFORM(cVerif.xtotal_cu_tva) + ' valuta=' + TRANSFORM(cVerif.xnume_valuta)
	DO HarnessLog WITH '  tip=[' + TRANSFORM(cVerif.tip_mesaj_raspuns) + '] emisa=' + TRANSFORM(cVerif.factura_emisa) + ' cif emitent=' + TRANSFORM(cVerif.cod_fiscal_emitent) + ' cif benef=' + TRANSFORM(cVerif.cod_fiscal_beneficiar)
	DO HarnessLog WITH '  descarcat=' + TRANSFORM(cVerif.descarcat) + ' procesat=' + TRANSFORM(cVerif.procesat) + ' creditnote=' + TRANSFORM(cVerif.creditnote) + ' len(detalii)=' + TRANSFORM(cVerif.len_detalii) + ' len(zip)=' + TRANSFORM(cVerif.len_zip)
	DO HarnessLog WITH '  filepath=[' + TRANSFORM(cVerif.filepath) + '] exista pe disc=' + TRANSFORM(FILE(ALLTRIM(NVL(cVerif.filepath, ''))))

	DO TVerdict WITH ALLTRIM(NVL(cVerif.xnumar_act, '')) == 'SRC/0000809', 'T2.1 numar factura', TRANSFORM(cVerif.xnumar_act)
	DO TVerdict WITH cVerif.xdata_act = {^2026-01-28}, 'T2.2 data factura', TRANSFORM(cVerif.xdata_act)
	DO TVerdict WITH ROUND(NVL(cVerif.xtotal_cu_tva, 0), 2) = -5166.63, 'T2.3 total cu tva', TRANSFORM(cVerif.xtotal_cu_tva)
	DO TVerdict WITH ROUND(NVL(cVerif.xtotal_fara_tva, 0), 2) = -4269.94, 'T2.4 total fara tva', TRANSFORM(cVerif.xtotal_fara_tva)
	DO TVerdict WITH ALLTRIM(UPPER(NVL(cVerif.tip_mesaj_raspuns, ''))) == 'FACTURA PRIMITA' AND NVL(cVerif.factura_emisa, -1) = 0, 'T2.5 tip factura primita', TRANSFORM(cVerif.tip_mesaj_raspuns)
	DO TVerdict WITH ALLTRIM(NVL(cVerif.cod_fiscal_emitent, '')) == '17043227' AND ALLTRIM(NVL(cVerif.cod_fiscal_beneficiar, '')) == gcCifBenef, 'T2.6 coduri fiscale', TRANSFORM(cVerif.cod_fiscal_emitent) + '/' + TRANSFORM(cVerif.cod_fiscal_beneficiar)
	DO TVerdict WITH NVL(cVerif.len_detalii, 0) > 4000, 'T2.7 xml salvat in detalii', TRANSFORM(cVerif.len_detalii) + ' caractere'
	DO TVerdict WITH FILE(ALLTRIM(NVL(cVerif.filepath, ''))), 'T2.8 arhiva copiata local', TRANSFORM(cVerif.filepath)

	lnDetalii = 0
	goExecutor.oSelecteaza2Value("select count(*) from anaf_efactura_detalii where id_efactura = " + ALLTRIM(STR(cVerif.id)), @lnDetalii)
	DO TVerdict WITH NVL(lnDetalii, 0) > 0, 'T2.9 linii in anaf_efactura_detalii', TRANSFORM(lnDetalii) + ' linii'

	goExecutor.oExecuta("select nr, articol, cantitate, um, pret, proctva, valoarefaratva from anaf_efactura_detalii where id_efactura = " + ALLTRIM(STR(cVerif.id)) + " order by nr", 'cLinii')
	IF USED('cLinii')
		SELECT cLinii
		SCAN
			DO HarnessLog WITH '  linia ' + TRANSFORM(cLinii.nr) + ': [' + TRANSFORM(cLinii.articol) + '] cant=' + TRANSFORM(cLinii.cantitate) + ' ' + TRANSFORM(cLinii.um) + ' pret=' + TRANSFORM(cLinii.pret) + ' tva=' + TRANSFORM(cLinii.proctva) + ' valoare=' + TRANSFORM(cLinii.valoarefaratva)
		ENDSCAN
		USE IN (SELECT('cLinii'))
	ENDIF
ELSE
	DO TVerdict WITH .F., 'T2.1-T2.9 verificare in baza de date', 'nu am gasit exact 1 rand in anaf_efactura'
ENDIF

USE IN (SELECT('cVerif'))
DO HarnessStep WITH 2, 'dupa_import'

* ---------- T3: factura in grila reala "Facturi primite" din borderou ----------
* ATENTIE: pagina "Facturi primite in SPV" e obiectul Page2, dar are PageOrder = 3,
* iar ActivePage merge pe PageOrder - deci activarea ei se face cu 3, nu cu 2.
loForm.pgfeFactura.ActivePage = 3
loForm.Refresh()
lnGasit = 0
IF USED('crsFacturiPrimite')
	SELECT crsFacturiPrimite
	COUNT FOR ALLTRIM(NVL(numar_act, '')) == 'SRC/0000809' TO lnGasit
	DO HarnessLog WITH 'crsFacturiPrimite: ' + TRANSFORM(RECCOUNT('crsFacturiPrimite')) + ' randuri, din care factura importata: ' + TRANSFORM(lnGasit)
ENDIF
DO TVerdict WITH lnGasit = 1, 'T3 factura vizibila in grila Facturi primite', TRANSFORM(lnGasit) + ' randuri gasite'
DO HarnessStep WITH 3, 'grila_facturi_primite'

DO HarnessLog WITH 'REZULTAT FINAL: PASS=' + TRANSFORM(gnPass) + ' FAIL=' + TRANSFORM(gnFail)
DO HarnessDone WITH IIF(gnFail = 0, 'done OK', 'done CU ESECURI')

QUIT

***********************
PROCEDURE TVerdict
	LPARAMETERS tlOk, tcNume, tcDetalii
	IF tlOk
		gnPass = gnPass + 1
	ELSE
		gnFail = gnFail + 1
	ENDIF
	DO HarnessLog WITH IIF(tlOk, 'PASS ', 'FAIL ') + tcNume + ' | ' + TRANSFORM(tcDetalii)
ENDPROC

***********************
PROCEDURE TErr
	LPARAMETERS tnError, tcMessage, tcProgram, tnLine
	DO HarnessLog WITH 'EROARE ' + TRANSFORM(tnError) + ' [' + TRANSFORM(tcMessage) + '] in ' + TRANSFORM(tcProgram) + ' linia ' + TRANSFORM(tnLine)
ENDPROC
