* test_dvi_dialog_ui.prg
* Runda30 (F1, plan fizzy-imagining-parnas.md): teste UI pe formularul REAL de adaugare
* factura (import_adauga_factura, COMUN\clase\ointroduceri.vcx), clona a test_adauga_factura_ui.prg,
* focalizata pe comportamentul dialogului pentru blocul DVI (valuta/curs proprii) si discount:
*   1. precompletarea valutei/cursului DVI din factura la Init (mirror automat);
*   2. contractul nou al mock_caut_valuta (goMockValutaCoada, coada FIFO) exercitat prin
*      2 pick-uri diferite in acelasi flux (valuta facturii, apoi valuta DVI) + fallback
*      la contractul vechi (goMockValuta) cand coada se goleste;
*   3. alegerea RON pe DVI (curs fix 1, curs disabled, TVA valuta ascunsa si 0);
*   4. garda de dirty: TVA in valuta DVI editat manual (txtDviTvaVal.Valid) nu mai e
*      rescris de recalc_tva (cazul unei a treia valute, diferita de RON si de factura);
*   5. Runda32: discountul e activ direct pe nDiscBaza<>0 (fara chkAreDiscount); caption-ul
*      txtDiscBaza urmeaza valuta facturii, txtDiscBaza e permanent vizibil;
*   6. dirty-flags: txtDviCurs.Valid seteaza dirty; realegerea valutei DVI reseteaza cursul;
*   7. validari blocante: DVI non-RON cu curs 0; discount negativ; discount >= suma facturii.
*
* ASUMPTII (de confruntat cu implementarea reala):
*   - proprietatile de dialog se numesc nDviIdValuta, cDviValuta, nDviCurs, lDviCursDirty,
*     lDviTvaValDirty, nDiscBaza (verificate pe ointroduceri.vc2, lane F1+F2 dialog,
*     27.07.2026; Runda32 elimina chkAreDiscount/txtDiscTva/nDiscTva/lDiscTvaDirty);
*   - RON e o valuta cu id propriu (1), nu id_valuta=0 (vezi test_repro_valuta_dblclick.prg);
*     picker-ul (cmdValuta/cmdDviValuta.Click) ignora rezultate cu id_valuta=0 (guard
*     !Empty(Nvl(loCauta.id_valuta,0))).
*
* Rulare:
*   powershell -ExecutionPolicy Bypass -File test_dvi_dialog_ui.ps1

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

PUBLIC gcUILog, gcSyncDir
gcUILog  = "D:\ROA\ROAGEST\COMUN\utile\Teste\achizitie_import\test_dvi_dialog_ui_log.txt"
gcSyncDir = "D:\ROA\ROAGEST\COMUN\utile\Teste\achizitie_import\uisync_dvi_dialog\"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcUILog)

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

TRY
	LOCAL gcAppPath, lcPath
	gcAppPath = "D:\ROA\ROAGEST\"
	SET DEFAULT TO (gcAppPath)

	lcPath = gcAppPath + 'Date;' + gcAppPath + 'Include;' + gcAppPath + 'FERESTRE;' + ;
		gcAppPath + 'GRAFICE;' + gcAppPath + 'CLASE;' + gcAppPath + 'MENIURI;' + ;
		gcAppPath + 'PROGRAME;' + gcAppPath + 'RAPOARTE;' + gcAppPath + 'COMUN;' + ;
		gcAppPath + 'COMUN\CLASE;' + gcAppPath + 'COMUN\FERESTRE;' + gcAppPath + 'COMUN\PROGRAME;' + ;
		gcAppPath + 'COMUN\GRAFICE;' + gcAppPath + 'COMUN\RAPOARTE;' + gcAppPath + 'COMUN\MENIURI;' + ;
		gcAppPath + 'COMUN\UTILE\GRIDEXTRAS;' + gcAppPath + 'COMUN\UTILE\CTL32;' + ;
		gcAppPath + 'COMUN\UTILE\HPDF;' + gcAppPath + 'COMUN\UTILE\HPDF\REPORTOUTPUT;' + ;
		gcAppPath + 'COMUN\UTILE\WEB;' + gcAppPath + 'COMUN\UTILE\EXCEL;' + ;
		ADDBS(SUBSTR(gcAppPath,1,RAT([\],gcAppPath,2)))+[COMUNROA\]
	SET PATH TO &lcPath ADDITIVE
	DO DdLog WITH 'SET PATH OK'

	*----- CLASE -----
	SET CLASSLIB TO (gcAppPath + "comun\clase\appwiz") ADDITIVE
	SET CLASSLIB TO GESTIUNI ADDITIVE
	SET CLASSLIB TO CAUT ADDITIVE
	SET CLASSLIB TO BAZA ADDITIVE
	SET CLASSLIB TO comun ADDITIVE
	SET CLASSLIB TO accessibility.vcx ADDITIVE
	SET CLASSLIB TO messagebox ADDITIVE
	SET CLASSLIB TO registry ADDITIVE
	SET CLASSLIB TO cauta_alfa_forms.vcx ADDITIVE
	SET CLASSLIB TO DECABAZA ADDITIVE
	SET CLASSLIB TO onomenclatoare ADDITIVE
	SET CLASSLIB TO stocuri.vcx ADDITIVE
	SET CLASSLIB TO rulaje.vcx ADDITIVE
	SET CLASSLIB TO ointroduceri ADDITIVE
	SET CLASSLIB TO ointroduceri_web ADDITIVE
	SET CLASSLIB TO ointroduceri_depozit ADDITIVE
	SET CLASSLIB TO overificari ADDITIVE
	SET CLASSLIB TO ferestre_oracle ADDITIVE
	SET CLASSLIB TO ferestre_cere_date.vcx ADDITIVE
	SET CLASSLIB TO configurare.vcx ADDITIVE
	SET CLASSLIB TO serii_numere.vcx ADDITIVE
	SET CLASSLIB TO omodificari.vcx ADDITIVE
	SET CLASSLIB TO ocompensari.vcx ADDITIVE
	SET CLASSLIB TO caut_ora ADDITIVE
	SET CLASSLIB TO onote_contabile ADDITIVE
	SET CLASSLIB TO otoolbar ADDITIVE
	SET CLASSLIB TO bon_fisc ADDITIVE
	SET CLASSLIB TO onom_articole ADDITIVE
	SET CLASSLIB TO onom_retete ADDITIVE
	SET CLASSLIB TO orapoarte ADDITIVE
	SET CLASSLIB TO orapoarte_gestiuni ADDITIVE
	SET CLASSLIB TO orapoarte_parametri ADDITIVE
	SET CLASSLIB TO ONOM_CURS ADDITIVE
	SET CLASSLIB TO ctl32_statusbar_fals.vcx ADDITIVE
	SET CLASSLIB TO ctl32_common.vcx ADDITIVE
	SET CLASSLIB TO ctl32_structs.vcx ADDITIVE
	SET CLASSLIB TO ctl32_progressbar.vcx ADDITIVE
	SET CLASSLIB TO ctl32_balloontip.vcx ADDITIVE
	SET CLASSLIB TO ocriterii.vcx ADDITIVE
	SET CLASSLIB TO oavize.vcx ADDITIVE
	SET CLASSLIB TO onomenclatoare2 ADDITIVE
	SET CLASSLIB TO wwdialogs.vcx ADDITIVE
	SET CLASSLIB TO oimportxml.vcx ADDITIVE
	SET CLASSLIB TO excelxml.vcx ADDITIVE
	SET CLASSLIB TO orapoarte_obinv.vcx ADDITIVE
	DO DdLog WITH 'SET CLASSLIB OK'

	*----- PROCEDURI (mock-uri PRIMELE, ui_harness la final) -----
	SET PROCEDURE TO PROCEDURI
	SET PROCEDURE TO "D:\ROA\ROAGEST\COMUN\utile\Teste\mock_amessagebox.prg" ADDITIVE
	SET PROCEDURE TO "D:\ROA\ROAGEST\COMUN\utile\Teste\achizitie_import\mock_caut_valuta.prg" ADDITIVE
	SET PROCEDURE TO acces_meniu ADDITIVE
	SET PROCEDURE TO cauta_alfa ADDITIVE
	SET PROCEDURE TO pmenu ADDITIVE
	SET PROCEDURE TO proceduri_comune ADDITIVE
	SET PROCEDURE TO quitapp ADDITIVE
	SET PROCEDURE TO init_program ADDITIVE
	SET PROCEDURE TO oproceduri_listari.prg ADDITIVE
	SET PROCEDURE TO oproceduri_configurare.prg ADDITIVE
	SET PROCEDURE TO oproceduri_articole.prg ADDITIVE
	SET PROCEDURE TO oproceduri_retete.prg ADDITIVE
	SET PROCEDURE TO orapoarte.prg ADDITIVE
	SET PROCEDURE TO mesaje ADDITIVE
	SET PROCEDURE TO oserii_numere.prg ADDITIVE
	SET PROCEDURE TO oexport.prg ADDITIVE
	SET PROCEDURE TO wwConfig.prg ADDITIVE
	SET PROCEDURE TO proceduri_rapoarte.prg ADDITIVE
	SET PROCEDURE TO OPROCEDURI_CURS.PRG ADDITIVE
	SET PROCEDURE TO validare.prg ADDITIVE
	SET PROCEDURE TO GENCURSOR.PRG ADDITIVE
	SET PROCEDURE TO OPROCEDURI_COMUNE.PRG ADDITIVE
	SET PROCEDURE TO OINIT_OPTIUNI.PRG ADDITIVE
	SET PROCEDURE TO updateserver.prg ADDITIVE
	SET PROCEDURE TO oinainte_de.prg ADDITIVE
	SET PROCEDURE TO ooperatii_comune.prg ADDITIVE
	SET PROCEDURE TO Ocompensari.PRG ADDITIVE
	SET PROCEDURE TO OPROCEDURI_aMS.PRG ADDITIVE
	SET PROCEDURE TO oproceduri_rulaje ADDITIVE
	SET PROCEDURE TO oproceduri_stocuri ADDITIVE
	SET PROCEDURE TO OINTRODUCERI ADDITIVE
	SET PROCEDURE TO oHeader.prg ADDITIVE
	SET PROCEDURE TO proceduri_excel.PRG ADDITIVE
	SET PROCEDURE TO onomenclatoare.PRG ADDITIVE
	SET PROCEDURE TO osecurity ADDITIVE
	SET PROCEDURE TO ocautare ADDITIVE
	SET PROCEDURE TO orefaceri ADDITIVE
	SET PROCEDURE TO ofacturare_comun.PRG ADDITIVE
	SET PROCEDURE TO ofacturare_stoc.PRG ADDITIVE
	SET PROCEDURE TO oproceduri_casademarcat ADDITIVE
	SET PROCEDURE TO oproceduri_casa_marcat_e500 ADDITIVE
	SET PROCEDURE TO oproceduri_casa_marcat_mp500 ADDITIVE
	SET PROCEDURE TO oproceduri_util.prg ADDITIVE
	SET PROCEDURE TO wwxmlhttp.prg ADDITIVE
	SET PROCEDURE TO ini.prg ADDITIVE
	SET PROCEDURE TO odocumente.prg ADDITIVE
	SET PROCEDURE TO regex.prg ADDITIVE
	SET PROCEDURE TO wwutils.prg ADDITIVE
	SET PROCEDURE TO inchidere_k ADDITIVE
	SET PROCEDURE TO iniacces.prg ADDITIVE
	SET PROCEDURE TO oupdate.prg ADDITIVE
	SET PROCEDURE TO procese.prg ADDITIVE
	SET PROCEDURE TO version.prg ADDITIVE
	SET PROCEDURE TO xmlaccess.prg ADDITIVE
	SET PROCEDURE TO xmlparser.prg ADDITIVE
	SET PROCEDURE TO filebringer.prg ADDITIVE
	SET PROCEDURE TO wwcodeupdate.prg ADDITIVE
	SET PROCEDURE TO wwhttp.prg ADDITIVE
	SET PROCEDURE TO xdate.prg ADDITIVE
	SET PROCEDURE TO ofacturare.prg ADDITIVE
	SET PROCEDURE TO oproceduri_obinv.prg ADDITIVE
	SET PROCEDURE TO oproceduri_facturare.prg ADDITIVE
	SET PROCEDURE TO email.prg ADDITIVE
	SET PROCEDURE TO "D:\ROA\ROAGEST\COMUN\utile\Teste\ui_harness.prg" ADDITIVE
	DO DdLog WITH 'SET PROCEDURE OK'

	*----- Globale -----
	PUBLIC gnPc, gnPval, gnPcant, gnPpret, gnPPretval, gnPCurs, gnPVal, gnPPretV, gnPPretVal
	STORE 2 TO gnPc, gnPval, gnPVal
	gnPcant = 3
	STORE 4 TO gnPpret, gnPPretval, gnPPretVal, gnPPretV, gnPCurs
	PUBLIC gl406, gcS, gnAn, gnLuna, gnIdFirma, gnIdSucursala, gcAcces, gcFirma
	gl406 = .T.
	gcS = 'CONTAFIN'
	gnAn = YEAR(DATE())
	gnLuna = MONTH(DATE())
	gnIdFirma = 110
	gnIdSucursala = 110
	gcAcces = "1;2;3;4;5;6;7;8;"
	gcFirma = gcS
	PUBLIC nror(65000)

	PUBLIC gnMockRaspuns, gcMockUltimMesaj, gnMockUltimTip
	gnMockRaspuns = 6
	gcMockUltimMesaj = ''
	gnMockUltimTip = 0

	PUBLIC goExecutor, goApp
	goExecutor = CREATEOBJECT('dummyexecutor')
	goApp = CREATEOBJECT('dummyapp')

	*----- poAct: antetul notei (factura in EUR, curs 5 - ca DVI-ul sa aiba ce mosteni la Init) -----
	PUBLIC poAct
	poAct = CREATEOBJECT('Custom')
	poAct.AddProperty('nume_val', 'EUR')
	poAct.AddProperty('curs', 5)
	poAct.AddProperty('proc_tva', 1.21)
	poAct.AddProperty('nnir', 0)
	poAct.AddProperty('id_valuta', 978)
	poAct.AddProperty('id_fdoc', 1)
	poAct.AddProperty('fdoc', 'Factura')
	poAct.AddProperty('id_partc', 501)
	poAct.AddProperty('partc', 'FURNIZOR EXTERN GMBH')
	poAct.AddProperty('serie_act', '')
	poAct.AddProperty('dataact', DATE())
	poAct.AddProperty('tva_incasare', 0)
	poAct.AddProperty('id_set', 1)
	poAct.AddProperty('id_gestin', 1)

	*----- jtva_coloane2 / saft_taxtable: perechi reale 21% (achizitii interne + import) -----
	IF USED('jtva_coloane2')
		USE IN ('jtva_coloane2')
	ENDIF
	CREATE CURSOR jtva_coloane2 (ordine N(8), id_jtva_coloana N(14), denumire C(100), ;
		jc N(1), jv N(1), afisat N(1), coloana_jc C(20), coloana_jv C(20), cota_tva N(6,2), id_tva N(14))
	INSERT INTO jtva_coloane2 VALUES (0, 0, '', 0, 0, 1, '', '', 0, 0)
	INSERT INTO jtva_coloane2 VALUES (1, 208, 'ACH. INT. 21%', 1, 0, 1, 'RO21B', 'RO21T', 21, 209)
	INSERT INTO jtva_coloane2 VALUES (2, 209, 'TVA ACH. INT. 21%', 1, 0, 1, 'RO21T', '', 21, 0)
	INSERT INTO jtva_coloane2 VALUES (3, 220, 'ACH. IMP. 21%', 1, 0, 1, 'FO21B', 'FO21T', 21, 221)
	INSERT INTO jtva_coloane2 VALUES (4, 221, 'TVA ACH. IMP. 21%', 1, 0, 1, 'FO21T', '', 21, 0)

	IF USED('saft_taxtable')
		USE IN ('saft_taxtable')
	ENDIF
	CREATE CURSOR saft_taxtable (taxname C(250) NULL, typename C(250) NULL, taxcode N(6) NULL, ;
		taxtype N(3) NULL, descriere C(250) NULL, tip C(100) NULL, descriere_tip C(250) NULL, ;
		procent_taxa N(7,2) NULL, procent_prorata N(7,2) NULL, inactiv N(1) NULL, tva N(1) NULL, ;
		wht N(1) NULL, tva_achizitii N(1) NULL, tva_livrari N(1) NULL)
	INDEX ON taxcode TAG taxcode
	INSERT INTO saft_taxtable (taxname, taxcode, descriere, procent_taxa, tva, wht, tva_achizitii, tva_livrari) ;
		VALUES ('', 0, '', 0, 1, 1, 1, 1)
	INSERT INTO saft_taxtable (taxname, taxcode, descriere, procent_taxa, tva, wht, tva_achizitii, tva_livrari) ;
		VALUES ('301204-Import de bunuri cota 21%', 301204, '', 21, 1, 0, 1, 0)

	DO CreeazaIntrodc WITH 'introdc'
	DO CreeazaIntrodc WITH 'introdc_sablon'
	INSERT INTO introdc_sablon (scd, scc, cu_tva, ordine, id_set, participa_valuta, tip_rand, ;
			id_jtva_coloana, ptva, explicatie_tva) ;
		VALUES ('371', '401', 1, 1, 1, .T., 'B', 208, 21, 'ACH. INT. 21%')
	INSERT INTO introdc_sablon (scd, scc, cu_tva, ordine, id_set, participa_valuta, tip_rand, ;
			id_jtva_coloana, ptva, explicatie_tva) ;
		VALUES ('4426', '401', 1, 2, 1, .T., 'T', 209, 21, 'TVA ACH. INT. 21%')
	GO TOP IN introdc_sablon

	IF USED('rul_temp')
		USE IN ('rul_temp')
	ENDIF
	CREATE CURSOR rul_temp (denumire C(100), codmat C(30), cant N(14,3), pret_val N(18,4), ;
		pretd N(18,4), pret N(18,4), pret_lei N(18,4), cont C(10), acont C(20), serie C(30), ;
		lot C(30), adata_expirare C(10), proc_disc N(6,2), cota_tva N(6,2))
	INSERT INTO rul_temp (denumire, codmat, cant, pret, pret_lei, cont, cota_tva) ;
		VALUES ('PLACEHOLDER', 'PH', 1, 1, 1, '371', 21)

	*-- index ord_doc INAINTE de CREATEOBJECT (capcana p, docs/testare-ui-vfp.md)
	SELECT introdc
	INDEX ON Str(Nvl(nr_doc,0),8) + Icase(tip_rand='B','1', tip_rand='S','2', tip_rand='G','3', tip_rand='T','4', tip_rand='D','5', '6') + Padr(Nvl(scd,''),10) TAG ord_doc
	SET ORDER TO ord_doc

	DO DdLog WITH 'cursoare pregatite, CREATEOBJECT(import_nota, .F. - mod import)'
	PUBLIC goObj
	goObj = CREATEOBJECT('IMPORT_nota', .F.)
	IF VARTYPE(goObj) <> 'O'
		DO DdLog WITH 'FAIL: instantiere import_nota'
		DO HarnessDone WITH 'done-error'
		QUIT
	ENDIF
	goObj.lDialogAratat = .T.
	goObj.oact = poAct
	goObj.lSpargeSecundare = .F.
	goObj.WindowType = 0
	goObj.Visible = .T.
	goObj.Show()
	goObj.WindowState = 2
	DOEVENTS FORCE

	SELECT rul_temp
	ZAP
	goObj.gridart.Refresh()
	DOEVENTS FORCE

	PRIVATE pnAsserts, pnPass
	STORE 0 TO pnAsserts, pnPass

	DO HarnessStep WITH 0, 'nota goala afisata, mod import (.F.)'

	*========================= SCENARIU G: coada FIFO a mock-ului, 2 pick-uri in acelasi flux =========================
	*-- valuta facturii (GBP) apoi valuta DVI (CHF) - independente; dupa coada, fallback la
	*-- contractul vechi (goMockValuta fix).
	PUBLIC goDlg
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 6001
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 601
	goDlg.cPartener = 'FURNIZOR UK LTD'
	goDlg.nSuma = 100
	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE

	PUBLIC goMockValuta, goMockValutaCoada
	goMockValuta = CREATEOBJECT('Custom')
	goMockValuta.AddProperty('id_valuta', 978)
	goMockValuta.AddProperty('nume_val', 'EUR')
	goMockValutaCoada = CREATEOBJECT('Collection')
	LOCAL loValGbp, loValChf
	loValGbp = CREATEOBJECT('Custom')
	loValGbp.AddProperty('id_valuta', 826)
	loValGbp.AddProperty('nume_val', 'GBP')
	goMockValutaCoada.Add(loValGbp)
	loValChf = CREATEOBJECT('Custom')
	loValChf.AddProperty('id_valuta', 756)
	loValChf.AddProperty('nume_val', 'CHF')
	goMockValutaCoada.Add(loValChf)

	goDlg.cmdValuta.Click()
	=Assert3('G1-pick1 (valuta facturii) seteaza nIdValuta=826 (GBP)', goDlg.nIdValuta, 826)
	=AssertStr('G2-pick1 cValuta=GBP', ALLTRIM(goDlg.cValuta), 'GBP')
	=Assert3('G3-DVI mirror dupa pick1 (nDviIdValuta=826, nu e dirty inca)', goDlg.nDviIdValuta, 826)

	goDlg.cmdDviValuta.Click()
	=Assert3('G4-pick2 (valuta DVI) seteaza nDviIdValuta=756 (CHF)', goDlg.nDviIdValuta, 756)
	=AssertStr('G5-pick2 cDviValuta=CHF', ALLTRIM(goDlg.cDviValuta), 'CHF')
	=Assert3('G6-valuta facturii ramane GBP (independenta de pick2)', goDlg.nIdValuta, 826)
	=Assert3('G7-coada goala dupa 2 pick-uri (Count=0)', goMockValutaCoada.Count, 0)

	LOCAL loFallback
	loFallback = caut_valuta()
	=Assert3('G8-coada goala: caut_valuta() revine la contractul vechi (goMockValuta EUR=978)', loFallback.id_valuta, 978)

	goDlg.Hide()
	DOEVENTS FORCE
	DO HarnessStep WITH 1, 'scenariul G (coada FIFO mock_caut_valuta) verificat - vezi log'

	*========================= SCENARIU A: precompletare valuta/curs DVI din factura la Init =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	=Assert3('A1-precompletare nDviIdValuta=978 (EUR, ca factura) la Init', goDlg.nDviIdValuta, 978)
	=AssertStr('A2-precompletare cDviValuta=EUR la Init', ALLTRIM(goDlg.cDviValuta), 'EUR')
	=Assert3('A3-precompletare nDviCurs=5 (ca factura) la Init', goDlg.nDviCurs, 5)

	goDlg.optTipTva.Value = 2
	goDlg.optTipTva.InteractiveChange()
	=Assert3('A4-txtDviValuta.Visible=1 in mod TVA DVI', IIF(goDlg.txtDviValuta.Visible,1,0), 1)

	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO HarnessStep WITH 2, 'scenariul A (precompletare DVI la Init) verificat - vezi log'
	goDlg.Hide()
	DOEVENTS FORCE

	*========================= SCENARIU B: alegerea RON pe DVI =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.optTipTva.Value = 2
	goDlg.optTipTva.InteractiveChange()

	PUBLIC goMockValutaCoada
	goMockValutaCoada = CREATEOBJECT('Collection')
	LOCAL loValRon
	loValRon = CREATEOBJECT('Custom')
	loValRon.AddProperty('id_valuta', 1)
	loValRon.AddProperty('nume_val', 'RON')
	goMockValutaCoada.Add(loValRon)

	goDlg.cmdDviValuta.Click()
	=Assert3('B1-alegere RON: nDviIdValuta=1', goDlg.nDviIdValuta, 1)
	=AssertStr('B2-alegere RON: cDviValuta=RON', ALLTRIM(goDlg.cDviValuta), 'RON')
	=Assert3('B3-alegere RON: nDviCurs fixat la 1', goDlg.nDviCurs, 1)
	=Assert3('B4-alegere RON: txtDviCurs.Enabled=0 (disabled)', IIF(goDlg.txtDviCurs.Enabled,1,0), 0)
	=Assert3('B5-alegere RON: txtDviTvaVal.Visible=1 (ramane vizibil, comuta pe lei)', IIF(goDlg.txtDviTvaVal.Visible,1,0), 1)
	=Assert3('B6-alegere RON: nDviTvaVal=0', goDlg.nDviTvaVal, 0)

	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO HarnessStep WITH 3, 'scenariul B (RON pe DVI) verificat - vezi log'
	goDlg.Hide()
	DOEVENTS FORCE

	*========================= SCENARIU C: garda de dirty pe TVA in valuta DVI (a treia valuta) =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.chkInValutaCont.Value = 1
	goDlg.nSuma = 1000
	goDlg.nProcentTva = 21
	goDlg.optTipTva.Value = 2
	goDlg.optTipTva.InteractiveChange()

	PUBLIC goMockValutaCoada
	goMockValutaCoada = CREATEOBJECT('Collection')
	LOCAL loValUsd
	loValUsd = CREATEOBJECT('Custom')
	loValUsd.AddProperty('id_valuta', 840)
	loValUsd.AddProperty('nume_val', 'USD')
	goMockValutaCoada.Add(loValUsd)
	goDlg.cmdDviValuta.Click()
	=AssertStr('C1-DVI in USD (a treia valuta, diferita de EUR factura)', ALLTRIM(goDlg.cDviValuta), 'USD')

	goDlg.txtDviCurs.Value = 4.5
	goDlg.txtDviCurs.Valid()
	goDlg.recalc_tva()
	*-- nSumaLei=1000*5=5000; TvaLei=5000*21%=1050; TvaVal(auto)=Round(1050/4.5,2)=233.33
	=Assert3('C2-baseline auto nDviTvaVal=233.33 (1050/4.5)', goDlg.nDviTvaVal, 233.33)

	goDlg.txtDviTvaVal.Value = 999.99
	goDlg.txtDviTvaVal.Valid()
	=Assert3('C3-dupa editare manuala, nDviTvaVal=999.99', goDlg.nDviTvaVal, 999.99)
	=Assert3('C4-lDviTvaValDirty=1 dupa editare manuala', IIF(goDlg.lDviTvaValDirty,1,0), 1)

	goDlg.recalc_tva()
	=Assert3('C5-dupa recalc_tva() repetat, nDviTvaVal ramane manual (999.99, nu se rescrie)', goDlg.nDviTvaVal, 999.99)

	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO HarnessStep WITH 4, 'scenariul C (garda dirty TVA valuta DVI) verificat - vezi log'
	goDlg.Hide()
	DOEVENTS FORCE

	*========================= SCENARIU D: discount fara bifa (Runda32), caption dupa valuta =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	=Assert3('D1-txtDiscBaza.Visible=1 permanent (fara chkAreDiscount)', IIF(goDlg.txtDiscBaza.Visible,1,0), 1)
	=AssertContains('D2-caption txtDiscBaza urmeaza valuta facturii (EUR)', 'EUR', goDlg.lblDiscBaza.Caption)

	goDlg.chkInValutaCont.Value = 0
	goDlg.chkInValutaCont.InteractiveChange()
	=AssertContains('D3-caption dupa scoaterea valutei: "RON"', 'RON', goDlg.lblDiscBaza.Caption)

	goDlg.txtDiscBaza.Value = 500
	goDlg.txtDiscBaza.Valid()
	=Assert3('D4-nDiscBaza=500 dupa completare', goDlg.nDiscBaza, 500)

	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO HarnessStep WITH 5, 'scenariul D (discount fara bifa, caption dupa valuta) verificat - vezi log'
	goDlg.Hide()
	DOEVENTS FORCE

	*========================= SCENARIU E: dirty-flags txtDviCurs + revert la realegere valuta DVI =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.optTipTva.Value = 2
	goDlg.optTipTva.InteractiveChange()

	goDlg.txtDviCurs.Value = 4.75
	goDlg.txtDviCurs.Valid()
	=Assert3('E1-lDviCursDirty=1 dupa editare manuala curs DVI', IIF(goDlg.lDviCursDirty,1,0), 1)
	=Assert3('E2-nDviCurs=4.75 dupa editare manuala', goDlg.nDviCurs, 4.75)

	PUBLIC goMockValutaCoada
	goMockValutaCoada = CREATEOBJECT('Collection')
	LOCAL loValEur2
	loValEur2 = CREATEOBJECT('Custom')
	loValEur2.AddProperty('id_valuta', 978)
	loValEur2.AddProperty('nume_val', 'EUR')
	goMockValutaCoada.Add(loValEur2)
	goDlg.cmdDviValuta.Click()
	=Assert3('E3-lDviCursDirty resetat (0) dupa realegerea valutei DVI', IIF(goDlg.lDviCursDirty,1,0), 0)

	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO HarnessStep WITH 6, 'scenariul E (dirty-flags txtDviCurs + revert) verificat - vezi log'
	goDlg.Hide()
	DOEVENTS FORCE

	*========================= SCENARIU F1: validare blocanta - DVI non-RON cu curs 0 =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 6002
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 601
	goDlg.cPartener = 'FURNIZOR UK LTD'
	goDlg.nSuma = 100
	goDlg.optTipTva.Value = 2
	goDlg.optTipTva.InteractiveChange()
	goDlg.nNractDvi = 7002
	goDlg.dDviData = DATE()
	goDlg.nDviIdPartener = 900
	goDlg.cDviPartener = 'DIRECTIA VAMALA'
	goDlg.nDviTvaLei = 21
	goDlg.nDviIdValuta = 840
	goDlg.cDviValuta = 'USD'
	goDlg.nDviCurs = 0
	*-- flagurile dirty marcheaza ca DVI a fost ales explicit (picker/curs tastat) - fara ele,
	*-- dvi_valuta_efectiva()/dvi_curs_efectiv() urmeaza factura (EUR), nu USD/0 setate direct
	goDlg.lDviValutaDirty = .T.
	goDlg.lDviCursDirty = .T.

	gcMockUltimMesaj = ''
	LOCAL llOkF1
	llOkF1 = goDlg.valideaza()
	=Assert3('F1a-valideaza() respinge DVI non-RON cu curs 0', IIF(llOkF1,1,0), 0)
	=AssertContains('F1b-mesaj contine "curs"', 'curs', gcMockUltimMesaj)

	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO HarnessStep WITH 7, 'scenariul F1 (DVI curs 0 blocant) verificat - vezi log'
	goDlg.Hide()
	DOEVENTS FORCE

	*========================= SCENARIU F2: validari blocante - discount negativ / discount >= suma =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 6003
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 601
	goDlg.cPartener = 'FURNIZOR UK LTD'
	goDlg.nSuma = 100
	goDlg.nIdExplicatieTva = 208
	goDlg.cExplicatieTva = 'ACH. INT. 21%'
	goDlg.optTipTva.Value = 1
	goDlg.txtDiscBaza.Value = -10
	goDlg.txtDiscBaza.Valid()

	gcMockUltimMesaj = ''
	LOCAL llOkF2a
	llOkF2a = goDlg.valideaza()
	=Assert3('F2a-valideaza() respinge discount negativ', IIF(llOkF2a,1,0), 0)
	=AssertContains('F2b-mesaj contine "discount"', 'discount', gcMockUltimMesaj)

	goDlg.txtDiscBaza.Value = 100
	goDlg.txtDiscBaza.Valid()
	gcMockUltimMesaj = ''
	LOCAL llOkF2c
	llOkF2c = goDlg.valideaza()
	=Assert3('F2c-valideaza() respinge discount >= suma facturii', IIF(llOkF2c,1,0), 0)
	=AssertContains('F2d-mesaj contine "discount"', 'discount', gcMockUltimMesaj)

	goDlg.txtDiscBaza.Value = 50
	goDlg.txtDiscBaza.Valid()
	LOCAL llOkF2e
	llOkF2e = goDlg.valideaza()
	=Assert3('F2e-discount < suma: valideaza() trece', IIF(llOkF2e,1,0), 1)

	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO HarnessStep WITH 8, 'scenariul F2 (discount negativ / >=suma blocante) verificat - vezi log'
	goDlg.Hide()
	DOEVENTS FORCE

	*========================= SCENARIU H: regresie - Tab fara modificare NU inghenuncheaza dirty =========================
	*-- Regresia reala reparata de orchestrator: garda veche (This.Value<>Thisform.nXxx) nu se
	*-- declansa NICIODATA, nici in productie, pentru ca ControlSource sincronizeaza proprietatea
	*-- legata INAINTE ca Valid sa apuce sa compare. Fix: tipar nOldVal (GotFocus retine valoarea
	*-- la intrare, Valid seteaza dirty doar daca s-a schimbat fata de acel moment - cf.
	*-- GridArt.cCant.Text1.GotFocus, deja existent in proiect). Fara acest test, un refactor
	*-- viitor ar putea reintroduce garda veche (mereu "dirty", declansata de intamplare de
	*-- initializarea la 0 a lui nOldVal) fara sa aprinda nimic.
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 6004
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 601
	goDlg.cPartener = 'FURNIZOR UK LTD'
	goDlg.nSuma = 100
	goDlg.optTipTva.Value = 2
	goDlg.optTipTva.InteractiveChange()

	*-- txtDviTvaVal: valoare initiala, dirty resetat, Tab FARA modificare, apoi modificare reala
	goDlg.lDviTvaValDirty = .F.
	goDlg.txtDviTvaVal.Value = 500
	goDlg.txtDviTvaVal.GotFocus()
	goDlg.txtDviTvaVal.Valid()
	=Assert3('H1-txtDviTvaVal: Tab fara modificare NU seteaza dirty', IIF(goDlg.lDviTvaValDirty,1,0), 0)
	goDlg.txtDviTvaVal.Value = 777
	goDlg.txtDviTvaVal.Valid()
	=Assert3('H2-txtDviTvaVal: modificare reala SETEAZA dirty', IIF(goDlg.lDviTvaValDirty,1,0), 1)

	*-- txtDviCurs: acelasi tipar
	goDlg.lDviCursDirty = .F.
	goDlg.txtDviCurs.Value = 5
	goDlg.txtDviCurs.GotFocus()
	goDlg.txtDviCurs.Valid()
	=Assert3('H3-txtDviCurs: Tab fara modificare NU seteaza dirty', IIF(goDlg.lDviCursDirty,1,0), 0)
	goDlg.txtDviCurs.Value = 6
	goDlg.txtDviCurs.Valid()
	=Assert3('H4-txtDviCurs: modificare reala SETEAZA dirty', IIF(goDlg.lDviCursDirty,1,0), 1)

	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO HarnessStep WITH 9, 'scenariul H (Tab fara modificare nu seteaza dirty) verificat - vezi log'
	goDlg.Hide()
	DOEVENTS FORCE

	DO DdLog WITH 'REZULTAT: ' + TRANSFORM(pnPass) + '/' + TRANSFORM(pnAsserts) + ' assert-uri PASS'
	IF pnPass = pnAsserts
		DO DdLog WITH 'REZULTAT: PASS (' + TRANSFORM(pnPass) + '/' + TRANSFORM(pnAsserts) + ')'
	ELSE
		DO DdLog WITH 'REZULTAT: FAIL (' + TRANSFORM(pnPass) + '/' + TRANSFORM(pnAsserts) + ' assert-uri PASS)'
	ENDIF

	DO HarnessStep WITH 10, 'final: ' + TRANSFORM(pnPass) + '/' + TRANSFORM(pnAsserts) + ' PASS'
	DO HarnessDone WITH 'done'

CATCH TO loExc
	DO DdLog WITH 'CATCH ErrorNo=' + TRANSFORM(loExc.ErrorNo) + ' Message=' + loExc.Message + ' Proc=' + loExc.Procedure + ' Line=' + TRANSFORM(loExc.LineNo) + ' [' + loExc.LineContents + ']'
	DO HarnessDone WITH 'done-error'
ENDTRY

QUIT

*==================================================================================
PROCEDURE CreeazaIntrodc
	*-- Runda30 (F1/pas2): coloane NOI rand_dvi N(1), valuta_proprie N(1) fata de structura veche.
	LPARAMETERS tcAlias
	IF USED(tcAlias)
		USE IN (tcAlias)
	ENDIF
	CREATE CURSOR (tcAlias) ( ;
		id_note N(14), explicatia C(100), scd C(10), ascd C(20), scc C(10), ascc C(20), ;
		cu_tva N(1), id_set N(14), ordine N(8), in_valuta N(1), id_jtva_coloana N(14) NULL, ;
		ptva N(6,2), explicatie_tva C(50), tipjurnal C(2), taxcode N(6), paymentcode C(9), ;
		suma N(18,4), suma_val N(18,4), id_partd N(14), id_partc N(14), partd C(100), partc C(100), ;
		id_fact N(14), id_factd N(14), id_factc N(14), pereched N(14), perechec N(14), ;
		nract N(14), serie_act C(10), explicatia4 C(50), explicatia5 C(50), participa_valuta L, ;
		id_valuta N(14), curs N(14,4), nume_val C(10), dataact D, fdoc C(50), id_fdoc N(14), ;
		se_scrie N(1), tva_incasare N(1), doc_color N(10), doc_label C(10), tip_rand C(1), ;
		doc_key C(80), nr_doc N(8), suma_doc N(18,4), suma_doc_val N(18,4), mod_manual N(1), expl_manual N(1), ;
		scd_orig C(20), ascd_orig C(20), rand_dvi N(1), valuta_proprie N(1))
ENDPROC

FUNCTION RECCOUNT_ACTIVE
	LPARAMETERS tcAlias
	LOCAL lnSel, lnCnt
	lnSel = SELECT()
	SELECT (tcAlias)
	COUNT TO lnCnt FOR !DELETED()
	SELECT (lnSel)
	RETURN lnCnt
ENDFUNC

PROCEDURE Assert3
	LPARAMETERS tcLabel, tuActual, tuExpected
	pnAsserts = pnAsserts + 1
	IF ABS(NVL(tuActual,0) - tuExpected) < 0.02
		pnPass = pnPass + 1
		DO DdLog WITH '  PASS ' + tcLabel + ' (=' + TRANSFORM(tuActual) + ')'
	ELSE
		DO DdLog WITH '  FAIL ' + tcLabel + ' (actual=' + TRANSFORM(tuActual) + ' asteptat=' + TRANSFORM(tuExpected) + ')'
	ENDIF
ENDPROC

PROCEDURE AssertStr
	LPARAMETERS tcLabel, tcActual, tcExpected
	pnAsserts = pnAsserts + 1
	IF tcActual == tcExpected
		pnPass = pnPass + 1
		DO DdLog WITH '  PASS ' + tcLabel + ' (=[' + tcActual + '])'
	ELSE
		DO DdLog WITH '  FAIL ' + tcLabel + ' (actual=[' + tcActual + '] asteptat=[' + tcExpected + '])'
	ENDIF
ENDPROC

PROCEDURE AssertContains
	LPARAMETERS tcLabel, tcNeedle, tcHaystack
	pnAsserts = pnAsserts + 1
	IF UPPER(ALLTRIM(tcNeedle)) $ UPPER(ALLTRIM(NVL(tcHaystack,'')))
		pnPass = pnPass + 1
		DO DdLog WITH '  PASS ' + tcLabel + ' ([' + tcNeedle + '] gasit in [' + tcHaystack + '])'
	ELSE
		DO DdLog WITH '  FAIL ' + tcLabel + ' ([' + tcNeedle + '] NEGASIT in [' + tcHaystack + '])'
	ENDIF
ENDPROC

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

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

DEFINE CLASS dummyapp AS Custom
	PROCEDURE ReadIni
		LPARAMETERS tcSection, tcKey
		RETURN ''
	ENDPROC
	PROCEDURE WriteIni
		LPARAMETERS tcSection, tcKey, tcValue
		RETURN .T.
	ENDPROC
ENDDEFINE

DEFINE CLASS dummyexecutor AS Custom
	cEroare = ''
	PROCEDURE oExecute
		LPARAMETERS tcSql, tcCursor
		IF USED(m.tcCursor)
			USE IN (m.tcCursor)
		ENDIF
		IF 'VJTVA_COLOANE' $ UPPER(m.tcSql)
			DO CreeazaJtvaColoaneView WITH m.tcCursor
			RETURN 1
		ENDIF
		IF UPPER(ALLTRIM(m.tcCursor)) == 'RUL_TEMP'
			CREATE CURSOR (m.tcCursor) (denumire C(100), codmat C(30), cant N(14,3), ;
				pret_val N(18,4), pretd N(18,4), pret N(18,4), pret_lei N(18,4), cont C(10), ;
				acont C(20), serie C(30), lot C(30), adata_expirare C(10), proc_disc N(6,2), cota_tva N(6,2))
			RETURN 1
		ENDIF
		IF 'CONFIG_ANALITICE' $ UPPER(m.tcSql)
			CREATE CURSOR (m.tcCursor) (camp_leg_prefix C(30), camp_leg_sufix C(30))
			INSERT INTO (m.tcCursor) VALUES ('', '')
			RETURN 1
		ENDIF
		IF 'FCONTURI_ANALITICE' $ UPPER(m.tcSql)
			CREATE CURSOR (m.tcCursor) (acont C(20))
			INSERT INTO (m.tcCursor) VALUES (IIF(TYPE('gcMockAcontCrt') = 'C', gcMockAcontCrt, ''))
			RETURN 1
		ENDIF
		IF 'VPLCONT_ANALITIC' $ UPPER(m.tcSql)
			CREATE CURSOR (m.tcCursor) (cont C(10), acont C(20))
			INSERT INTO (m.tcCursor) VALUES ('371', '4')
			RETURN 1
		ENDIF
		CREATE CURSOR (m.tcCursor) (id_temp N(1))
		RETURN 1
	ENDPROC
	PROCEDURE oExecuta
		LPARAMETERS tcSql, tcCursor
		IF !EMPTY(m.tcCursor)
			IF USED(m.tcCursor)
				USE IN (m.tcCursor)
			ENDIF
			IF UPPER(m.tcCursor) == 'CJTVACOL4427'
				CREATE CURSOR (m.tcCursor) (id_jtva_coloana N(14))
			ELSE
				CREATE CURSOR (m.tcCursor) (id_temp N(1))
			ENDIF
		ENDIF
		RETURN .T.
	ENDPROC
	PROCEDURE oSelect2Value
		LPARAMETERS tcSql, toValue
		toValue = ''
		RETURN 1
	ENDPROC
	PROCEDURE oReset
	ENDPROC
ENDDEFINE

PROCEDURE CreeazaJtvaColoaneView
	LPARAMETERS tcCursor
	IF USED(m.tcCursor)
		USE IN (m.tcCursor)
	ENDIF
	CREATE CURSOR (m.tcCursor) (id_jtva_coloana N(14), denumire C(100), jc N(1), jv N(1), ;
		coloana_jc C(20), coloana_jv C(20), cota_tva N(6,2), id_tva N(14) NULL, afisat N(1), ;
		id_jtva_ex N(14) NULL, id_jtva_neex N(14) NULL, ordine N(8), ;
		taxcode N(6) NULL, taxcode_neexigibil N(6) NULL, taxcode_n50 N(6) NULL, ;
		taxcode_n50_tvai N(6) NULL, taxcode_n100 N(6) NULL, taxcode_regularizare N(6) NULL, taxcode_tvai N(6) NULL)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, afisat, ordine, cota_tva) VALUES (0, '', 0, 0, 1, 0, 0)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, coloana_jc, cota_tva, id_tva, afisat, ordine, taxcode) ;
		VALUES (208, 'ACH. INT. 21%', 1, 0, 'RO21B', 21, 209, 1, 1, 301104)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, coloana_jc, cota_tva, afisat, ordine, taxcode) ;
		VALUES (209, 'TVA ACH. INT. 21%', 1, 0, 'RO21T', 21, 1, 2, 301104)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, coloana_jc, cota_tva, id_tva, afisat, ordine, taxcode) ;
		VALUES (220, 'ACH. IMP. 21%', 1, 0, 'FO21B', 21, 221, 1, 3, 301204)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, coloana_jc, cota_tva, afisat, ordine, taxcode) ;
		VALUES (221, 'TVA ACH. IMP. 21%', 1, 0, 'FO21T', 21, 1, 4, 301204)
ENDPROC