* test_dvi_bifa_valuta.prg
* Runda32 (control nou chkDviInValuta, sectiunea TVA DVI): factura de import IN VALUTA (EUR)
* cu bifa "Valuta" a DVI-ului SCOASA - DVI-ul e platit doar in lei, desi factura insasi e in
* valuta ("factura in valuta, DVI doar in lei", cazul nou din plan).
*
* Verifica:
*   - la Init (factura in valuta), bifa porneste BIFATA (chkInValutaCont=1, valuta <> RON);
*   - dupa scoaterea bifei: lblDviValuta/txtDviValuta/cmdDviValuta/lblDviCurs/txtDviCurs
*     ascunse, nDviCurs=1; txtDviTvaVal ramane vizibil, comuta pe lei (nDviTvaVal recalculat, nu 0);
*   - randul T (DVI) creat de do_adauga_factura: FARA valuta/curs/suma_val (in_valuta=0,
*     id_valuta=0, curs=0, nume_val='', suma_val=0), suma = nDviTvaLei (introdus direct);
*   - valuta_proprie=1 (starea de valuta a DVI-ului difera de a facturii - factura are valuta,
*     DVI nu mai are, dupa scoaterea bifei).
*
* Flux REAL: dialogul `import_adauga_factura` (toDlg construit inline in programul principal,
* capcana l - CREATEOBJECT/ADDPROPERTY nu merg dintr-o procedura in acest runtime headless).
*
* CAPCANA l (mock_caut_valuta.prg, confirmata si in test_e2e_dvi_ron_discount.prg): apelul REAL
* goDlg.construieste_todlg() arunca eroarea 11 pe primul Addproperty in acest runtime headless
* (CREATEOBJECT/ADDPROPERTY nu merg corect apelate dintr-o METODA, doar din programul PRINCIPAL) -
* nu poate fi exercitat direct. toDlg se construieste inline in program, cu formula COPIATA
* EXACT din ointroduceri.vc2 PROCEDURE construieste_todlg (~6157-6202, verificata in cache dupa
* ce lane-ul de dialog a terminat Runda32):
*   lcDviVal = Iif(lDviValutaDirty, cDviValuta, cValuta) -- dvi_valuta_efectiva()
*   llDviFaraVal = (chkDviInValuta.Value=0) Or Empty(lcDviVal) Or Upper(lcDviVal)=='RON'
*   llFactFaraVal = Empty(cValuta) Or Upper(cValuta)=='RON'
*   llDviPropriu = (dvi_id_valuta_efectiva()<>nIdValuta Or dvi_curs_efectiv()<>nCurs)
*   dvi_valuta_proprie = Iif(llDviFaraVal And llFactFaraVal, 0, Iif(llDviFaraVal<>llFactFaraVal, 1, Iif(llDviPropriu,1,0)))
* IMPORTANT: dvi_curs_efectiv()/dvi_id_valuta_efectiva() NU citesc nDviCurs/nDviIdValuta direct -
* doar cand lDviValutaDirty/lDviCursDirty sunt adevarate (alegere explicita prin picker/curs
* tastat). In acest test bifa e scoasa fara sa se atinga picker-ul/cursul DVI, deci flagurile
* dirty raman .F. si valorile EFECTIVE raman cele ale facturii (nIdValuta/nCurs), NU nDviCurs=1
* fortat de actualizeaza_stare_dvi_valuta() pe widget - acel 1 e doar pentru afisaj, nefolosit
* de formula toDlg.
* SCOPUL testului ramane do_adauga_factura() (crearea randului T dintr-un toDlg dat) - NU
* verifica daca dialogul real produce chiar acest toDlg (blocat de capcana l).
*
* Rulare:
*   powershell -ExecutionPolicy Bypass -File test_dvi_bifa_valuta.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_bifa_valuta_log.txt"
gcSyncDir = "D:\ROA\ROAGEST\COMUN\utile\Teste\achizitie_import\uisync_dvi_bifa_valuta\"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcUILog)

ON ERROR DO BvErr 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 BvLog 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 BvLog 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 BvLog 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) -----
	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', 601)
	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% -----
	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, 220, 'ACH. IMP. 21%', 1, 0, 1, 'FO21B', 'FO21T', 21, 221)
	INSERT INTO jtva_coloane2 VALUES (2, 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', 220, 21, 'ACH. IMP. 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', 221, 21, 'TVA ACH. IMP. 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)

	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 BvLog WITH 'cursoare pregatite, CREATEOBJECT(import_nota, .F.)'
	PUBLIC goObj
	goObj = CREATEOBJECT('IMPORT_nota', .F.)
	IF VARTYPE(goObj) <> 'O'
		DO BvLog 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

	*========================= DIALOG: factura EUR, mod TVA DVI =========================
	PUBLIC goDlg
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 18000
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 601
	goDlg.cPartener = 'FURNIZOR EXTERN GMBH'
	goDlg.cExplicatia = 'Import marfa, DVI fara valuta proprie'
	goDlg.nSuma = 1000
	goDlg.nSumaLei = 0
	goDlg.nCurs = 5
	goDlg.nIdValuta = 978
	goDlg.cValuta = 'EUR'
	goDlg.chkInValutaCont.Value = 1
	goDlg.chkParticipaValuta.Value = 1
	goDlg.nIdExplicatieTva = 220
	goDlg.cExplicatieTva = 'ACH. IMP. 21%'
	goDlg.nProcentTva = 21

	goDlg.cSerieDvi = ''
	goDlg.nNractDvi = 19000
	goDlg.dDviData = DATE()
	goDlg.nIdFdocDVI = 2
	goDlg.cFelDocDvi = 'DVI'
	goDlg.nDviIdPartener = 900
	goDlg.cDviPartener = 'DIRECTIA VAMALA'

	goDlg.optTipTva.Value = 2
	goDlg.optTipTva.InteractiveChange()

	*========================= bifa Valuta DVI: porneste BIFATA (factura are valuta) =========================
	=Assert3('1-la Init, factura in valuta: chkDviInValuta.Value=1 (bifata)', goDlg.chkDviInValuta.Value, 1)
	=Assert3('2-la Init: txtDviValuta.Visible=1 (sectiune vizibila)', IIF(goDlg.txtDviValuta.Visible,1,0), 1)

	*========================= scoatere bifa: sectiunea valuta DVI se ascunde, curs=1, tva val=0 =========================
	goDlg.chkDviInValuta.Value = 0
	goDlg.chkDviInValuta.InteractiveChange()
	=Assert3('3-dupa scoatere bifa: txtDviValuta.Visible=0', IIF(goDlg.txtDviValuta.Visible,1,0), 0)
	=Assert3('4-dupa scoatere bifa: cmdDviValuta.Visible=0', IIF(goDlg.cmdDviValuta.Visible,1,0), 0)
	=Assert3('5-dupa scoatere bifa: txtDviCurs.Visible=0', IIF(goDlg.txtDviCurs.Visible,1,0), 0)
	=Assert3('6-dupa scoatere bifa: txtDviTvaVal.Visible=1 (ramane vizibil, comuta pe lei)', IIF(goDlg.txtDviTvaVal.Visible,1,0), 1)
	=Assert3('7-dupa scoatere bifa: nDviCurs=1', goDlg.nDviCurs, 1)
	=Assert3('8-dupa scoatere bifa: nDviTvaVal=1050 (TVA recalculat in lei, nu 0)', goDlg.nDviTvaVal, 1050)

	*-- TVA DVI in lei, introdusa direct (proprietate reala, ca in restul suitei - vezi
	*-- test_dvi_dialog_ui.prg scenariul F1)
	goDlg.nDviTvaLei = 900.00

	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO HarnessStep WITH 0, 'dialog completat, bifa Valuta DVI scoasa'

	*========================= toDlg construit inline, do_adauga_factura REAL =========================
	*-- formula copiata exact din construieste_todlg (vezi CAPCANA l in antet) - lDviValutaDirty/
	*-- lDviCursDirty raman .F. (nu s-a atins picker-ul/cursul DVI), deci valorile efective ale
	*-- DVI-ului raman cele mostenite de la factura (nIdValuta/nCurs), nu nDviCurs=1 fortat de
	*-- actualizeaza_stare_dvi_valuta() pe widget.
	PUBLIC goT
	LOCAL lcDviValEfectiv, llDviFaraVal, llFactFaraVal, llDviPropriu, lnDviIdValutaEfectiv, lnDviCursEfectiv, llValutaProprie
	lnDviIdValutaEfectiv = IIF(goDlg.lDviValutaDirty, goDlg.nDviIdValuta, goDlg.nIdValuta)
	lnDviCursEfectiv = IIF(goDlg.lDviValutaDirty OR goDlg.lDviCursDirty, goDlg.nDviCurs, goDlg.nCurs)
	lcDviValEfectiv = IIF(goDlg.lDviValutaDirty, ALLTRIM(NVL(goDlg.cDviValuta,'')), ALLTRIM(NVL(goDlg.cValuta,'')))
	llDviFaraVal = (goDlg.chkDviInValuta.Value = 0) OR EMPTY(lcDviValEfectiv) OR UPPER(lcDviValEfectiv) == 'RON'
	llFactFaraVal = (EMPTY(goDlg.cValuta) OR UPPER(ALLTRIM(goDlg.cValuta)) == 'RON')
	llDviPropriu = (lnDviIdValutaEfectiv <> goDlg.nIdValuta OR lnDviCursEfectiv <> goDlg.nCurs)
	llValutaProprie = IIF(llDviFaraVal AND llFactFaraVal, 0, IIF(llDviFaraVal <> llFactFaraVal, 1, IIF(llDviPropriu,1,0)))

	goT = CREATEOBJECT('Empty')
	=ADDPROPERTY(goT,'serie_act','')
	=ADDPROPERTY(goT,'nract',goDlg.nNract)
	=ADDPROPERTY(goT,'dataact',goDlg.dDataAct)
	=ADDPROPERTY(goT,'fdoc',goDlg.cFelDoc)
	=ADDPROPERTY(goT,'id_fdoc',goDlg.nIdFdoc)
	=ADDPROPERTY(goT,'id_partc',goDlg.nIdPartener)
	=ADDPROPERTY(goT,'partc',goDlg.cPartener)
	=ADDPROPERTY(goT,'explicatia',ALLTRIM(NVL(goDlg.cExplicatia,'')))
	=ADDPROPERTY(goT,'suma',ROUND(goDlg.nSuma*goDlg.nCurs,gnPC))
	=ADDPROPERTY(goT,'suma_val',goDlg.nSuma)
	=ADDPROPERTY(goT,'in_valuta',1)
	=ADDPROPERTY(goT,'participa_valuta',.T.)
	=ADDPROPERTY(goT,'id_valuta',goDlg.nIdValuta)
	=ADDPROPERTY(goT,'curs',goDlg.nCurs)
	=ADDPROPERTY(goT,'nume_val',ALLTRIM(NVL(goDlg.cValuta,'')))
	=ADDPROPERTY(goT,'cTipTva','D')
	=ADDPROPERTY(goT,'id_jtva_coloana',goDlg.nIdExplicatieTva)
	=ADDPROPERTY(goT,'dvi_serie',ALLTRIM(NVL(goDlg.cSerieDvi,'')))
	=ADDPROPERTY(goT,'dvi_nract',goDlg.nNractDvi)
	=ADDPROPERTY(goT,'dvi_dataact',goDlg.dDviData)
	=ADDPROPERTY(goT,'dvi_id_fdoc',goDlg.nIdFdocDVI)
	=ADDPROPERTY(goT,'dvi_fdoc',goDlg.cFelDocDvi)
	=ADDPROPERTY(goT,'dvi_id_partc',goDlg.nDviIdPartener)
	=ADDPROPERTY(goT,'dvi_partc',goDlg.cDviPartener)
	=ADDPROPERTY(goT,'dvi_scc','401')
	=ADDPROPERTY(goT,'dvi_tva_lei',goDlg.nDviTvaLei)
	=ADDPROPERTY(goT,'dvi_tva_val',goDlg.nDviTvaVal)
	*-- bifa scoasa => dvi_in_valuta=.F. (llDviFaraVal), indiferent de valuta efectiva mostenita
	=ADDPROPERTY(goT,'dvi_in_valuta',!llDviFaraVal)
	=ADDPROPERTY(goT,'dvi_id_valuta',lnDviIdValutaEfectiv)
	=ADDPROPERTY(goT,'dvi_curs',lnDviCursEfectiv)
	=ADDPROPERTY(goT,'dvi_nume_val',lcDviValEfectiv)
	=ADDPROPERTY(goT,'dvi_valuta_proprie',llValutaProprie)
	=ADDPROPERTY(goT,'disc_baza_lei',0)
	=ADDPROPERTY(goT,'disc_baza_val',0)

	LOCAL llOkAdauga
	llOkAdauga = goObj.do_adauga_factura(goT)
	=Assert3('9-do_adauga_factura(goT) reuseste', IIF(llOkAdauga,1,0), 1)
	goDlg.Hide()
	DOEVENTS FORCE
	DO HarnessStep WITH 1, 'document creat, dupa Termin'

	*========================= randul T (DVI): fara valuta/curs/suma_val =========================
	SELECT introdc
	SET ORDER TO
	LOCATE FOR Nvl(rand_dvi,0) = 1
	=Assert3('10-randul T-DVI (rand_dvi=1) gasit', IIF(FOUND(),1,0), 1)
	IF FOUND()
		=Assert3('11-T-DVI.suma=900.00 (nDviTvaLei introdus)', suma, 900.00)
		=Assert3('12-T-DVI.suma_val=0 (fara valuta)', NVL(suma_val,0), 0)
		=Assert3('13-T-DVI.in_valuta=0', NVL(in_valuta,0), 0)
		=Assert3('14-T-DVI.id_valuta=0', NVL(id_valuta,0), 0)
		=Assert3('15-T-DVI.curs=0', NVL(curs,0), 0)
		=AssertStr('16-T-DVI.nume_val=""', ALLTRIM(NVL(nume_val,'?')), '')
		=Assert3('17-T-DVI.valuta_proprie=1 (factura are valuta, DVI nu mai are)', NVL(valuta_proprie,0), 1)
	ELSE
		pnAsserts = pnAsserts + 7
		DO BvLog WITH '  FAIL 11-17: T-DVI negasit'
	ENDIF

	*-- randul B (baza facturii) ramane in valuta - discountul/bifa DVI nu-l afecteaza
	SELECT introdc
	LOCATE FOR nract = 18000 AND tip_rand == 'B'
	=Assert3('18-randul B (baza) gasit', IIF(FOUND(),1,0), 1)
	IF FOUND()
		=Assert3('19-B.in_valuta=1 (factura ramane in valuta)', NVL(in_valuta,0), 1)
		=Assert3('20-B.suma_val=1000.00', NVL(suma_val,0), 1000.00)
	ELSE
		pnAsserts = pnAsserts + 2
		DO BvLog WITH '  FAIL 19-20: B negasit'
	ENDIF

	DO HarnessStep WITH 2, 'randul T-DVI fara valuta verificat - vezi log'

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

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

CATCH TO loExc
	DO BvLog 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
	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

PROCEDURE Assert3
	LPARAMETERS tcLabel, tuActual, tuExpected
	pnAsserts = pnAsserts + 1
	IF ABS(NVL(tuActual,0) - tuExpected) < 0.02
		pnPass = pnPass + 1
		DO BvLog WITH '  PASS ' + tcLabel + ' (=' + TRANSFORM(tuActual) + ')'
	ELSE
		DO BvLog 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 BvLog WITH '  PASS ' + tcLabel + ' (=[' + tcActual + '])'
	ELSE
		DO BvLog WITH '  FAIL ' + tcLabel + ' (actual=[' + tcActual + '] asteptat=[' + tcExpected + '])'
	ENDIF
ENDPROC

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

PROCEDURE BvLog
	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 ('')
			RETURN 1
		ENDIF
		IF 'VPLCONT_ANALITIC' $ UPPER(m.tcSql)
			CREATE CURSOR (m.tcCursor) (cont C(10), acont C(20))
			RETURN 1
		ENDIF
		IF 'VCONFIG_CONT_IREG' $ UPPER(m.tcSql)
			CREATE CURSOR (m.tcCursor) (contpart C(10), fel_cont N(1), cu_inregistrari N(1), ;
				explic_cont C(100), exceptie C(10), pozitie N(1), invers N(1), gasit N(1))
			INSERT INTO (m.tcCursor) VALUES ('401', 0, 0, 'Furnizori', '', 0, 0, 0)
			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 (220, 'ACH. IMP. 21%', 1, 0, 'FO21B', 21, 221, 1, 1, 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, 2, 301204)
ENDPROC
