* test_tva_secundare.prg
* Test UI (runda30) pentru clasa IMPORT_nota (COMUN\clase\ointroduceri.vcx): doua fixuri.
*   Bug 1 (Grid1.cScc.Text1.Valid): daca grila scrie valoarea in introdc.scc INAINTE ca Valid
*     sa ruleze (cazul Enter), garda trebuie sa arme resincronizarea pe baza lui Thisform.nOldVal,
*     indiferent daca campul e deja scris in cursor.
*   Bug 2 (sparge_document/recalc_tva_document): la documentele SECUNDARE (transport/taxe),
*     randurile T (TVA) trebuie regrupate pe creditorii (scc/ascc) randurilor S, proportional cu
*     bazele S per creditor, pastrand totalul T - nu doar la documentul principal.
*
* Scenariu (flux REAL, nu Append direct in cursoare):
*   Factura PRINCIPALA (nract 15000, RAJA, 1000 EUR, curs 5.23, TVA pe DVI) + 2 articole
*   (ART-BULD-212 cont 212 baza 900, ART-A8-371 cont 371 baza 100) prin do_adauga_articol() REAL.
*   Factura SECUNDARA (nract 12000, TRANS OIL, 1000 LEI, TVA la furnizor F) - transport, aceleasi
*   articole (baza proportionala). lSpargeSecundare = .T., sincronizeaza().
*   Pas1: stare de baza (S-uri mostenite scc=401, cate un singur T pe fiecare document).
*   Pas2 (Bug2): editez scc pe S(212) al SECUNDAREI 401->404 prin Grid1.cScc, astept timer/fallback,
*     astept 2 T-uri pe secundara (404=189, 401=21 lei).
*   Pas3 (Bug1): pe S(212) al PRINCIPALEI simulez scrierea in cursor INAINTE de Valid (cazul Enter),
*     astept lSyncPending=.T. imediat dupa Valid, apoi 2 T-uri pe principala (404=189, 401=21 EUR).
*   Pas4: persistenta - sincronizeaza() inca o data fara alte editari, splitul ramane identic.
*   Pas5: recalc_tva_document pe secundara cu totalul dublat (2000 lei) - T-urile se recalculeaza
*     proportional (404=378, 401=42), nu ambele la totalul intreg.
*
* Vezi test_tva_creditori.prg (sablon) si docs/testare-ui-vfp.md.
*
* Rulare (prin harness):
*   powershell -ExecutionPolicy Bypass -File test_tva_secundare.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_tva_secundare_log.txt"
gcSyncDir = "D:\ROA\ROAGEST\COMUN\utile\Teste\achizitie_import\uisync_tva_secundare\"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcUILog)

ON ERROR DO TfErr 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 TfLog 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 TfLog WITH 'SET CLASSLIB OK'

	*----- PROCEDURI (mock amessagebox + mock cauta_nom_articole 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\mock_cauta_nom_articole.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 TfLog 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 gnButon
	STORE 2 TO gnButon

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

	*----- poAct: antetul notei; dialogul mosteneste din el la Init -----
	PUBLIC poAct
	poAct = CREATEOBJECT('Custom')
	poAct.AddProperty('nume_val', '')
	poAct.AddProperty('curs', 1)
	poAct.AddProperty('proc_tva', 1.21)
	poAct.AddProperty('nnir', 0)
	poAct.AddProperty('id_valuta', 0)
	poAct.AddProperty('id_fdoc', 1)
	poAct.AddProperty('fdoc', 'Factura')
	poAct.AddProperty('id_partc', 500)
	poAct.AddProperty('partc', 'FURNIZOR ANTET SRL')
	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 (explicatii TVA): achizitii interne 21% - perechea folosita de sablon -----
	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)

	*----- saft_taxtable: coduri reale (vsaft_taxtable) - achizitii interne 21% -----
	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 ('301104-Achizitii de bunuri si servicii taxabile cu cota de 21%, altele decat importurile si cele supuse masurilor de simplificare', 301104, '', 21, 1, 0, 1, 0)

	*----- introdc (RecordSource Grid1) - porneste GOL, ca in fluxul real -----
	DO CreeazaIntrodc WITH 'introdc'

	*-- INDEX ord_doc (docs/testare-ui-vfp.md capcana p): fara el, sincronizari repetate cu 2+
	*-- documente (principala+secundara) pot pierde identitatea documentului "principal" dupa
	*-- ce S-urile sunt sterse/reasezate la coada de fiecare sparge_document().
	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

	*----- introdc_sablon: perechea-sablon (B 371/401 + T 4426/401) pe care do_adauga_factura o
	*      cloneaza (21%, ACH. INT. 21%). -----
	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

	*----- rul_temp cu 1 rand INAINTE de instantiere (capcana k) - structura FIDELA VRUL. -----
	DO CreeazaRulTemp
	INSERT INTO rul_temp (denumire, codmat, cant, pret, pret_lei, cont, cota_tva) ;
		VALUES ('PLACEHOLDER', 'PH', 1, 1, 1, '371', 21)

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

	*-- golesc placeholder-ul cu ZAP (nu DELETE ALL, capcana k)
	SELECT rul_temp
	ZAP
	goObj.gridart.Refresh()
	DOEVENTS FORCE

	*========================= DOC PRINCIPAL: FACTURA 15000, 1000 EUR, curs 5.23, TVA pe DVI =========================
	PUBLIC goDlg, goT
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 15000
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 700
	goDlg.cPartener = 'RAJA'
	goDlg.cExplicatia = 'Factura test TVA secundare - principala'
	goDlg.nSuma = 1000
	goDlg.nSumaLei = 0
	goDlg.nCurs = 5.23
	goDlg.nIdValuta = 978
	goDlg.cValuta = 'EUR'
	*-- ACH. INT. 21% (id 208), TVA pe DVI (D)
	goDlg.nIdExplicatieTva = 208
	goDlg.cExplicatieTva = 'ACH. INT. 21%'
	goDlg.optTipTva.Value = 2 && D = DVI
	goDlg.chkInValutaCont.Value = 1
	goDlg.chkParticipaValuta.Value = 1
	*-- date DVI
	goDlg.cSerieDvi = ''
	goDlg.nNractDvi = 101
	goDlg.dDviData = DATE()
	goDlg.nIdFdocDVI = 2
	goDlg.cFelDocDvi = 'DVI'
	goDlg.nDviIdPartener = 800
	goDlg.cDviPartener = 'BIROUL VAMAL'
	goDlg.nDviTvaVal = 210
	goDlg.nDviTvaLei = ROUND(210*5.23,2)
	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO TfLog WITH 'DOC principal: valideaza (pe dialog) = ' + TRANSFORM(goDlg.valideaza())
	DO ConstruiesteToDlg
	DO TfLog WITH 'DOC principal: do_adauga_factura = ' + TRANSFORM(goObj.do_adauga_factura(goT))
	goDlg.Hide()
	DOEVENTS FORCE

	LOCAL llB1, llT1
	SELECT introdc
	LOCATE FOR nract = 15000 AND INLIST(tip_rand,'B','S')
	llB1 = FOUND()
	LOCATE FOR nr_doc = 1 AND tip_rand == 'T'
	llT1 = FOUND()
	IF llB1 AND llT1
		DO TfLog WITH 'PASS: factura principala 15000 adaugata (B+T)'
	ELSE
		DO TfLog WITH 'FAIL: factura principala 15000 - B?=' + TRANSFORM(llB1) + ' T?=' + TRANSFORM(llT1)
	ENDIF

	*========================= ADAUGARE 2 ARTICOLE PRIN FLUXUL REAL: do_adauga_articol() =========================
	*-- ART-BULD-212 (cont 212, baza 900) + ART-A8-371 (cont 371, baza 100)
	PUBLIC gcMockArtCodmat
	gcMockArtCodmat = 'ART-BULD-212'
	goObj.do_adauga_articol()
	DOEVENTS FORCE
	SELECT rul_temp
	LOCATE FOR ALLTRIM(codmat) == 'ART-BULD-212'
	IF FOUND()
		REPLACE cant WITH 1, pret_val WITH 900, pretd WITH 900, pret WITH ROUND(900*5.23,gnPc), pret_lei WITH pret
		DO TfLog WITH 'articol 1 (ART-BULD-212, cont 212) adaugat prin do_adauga_articol() REAL, baza 900'
	ELSE
		DO TfLog WITH 'FAIL: ART-BULD-212 nu a fost gasit in rul_temp dupa do_adauga_articol()'
	ENDIF

	gcMockArtCodmat = 'ART-A8-371'
	goObj.do_adauga_articol()
	DOEVENTS FORCE
	SELECT rul_temp
	LOCATE FOR ALLTRIM(codmat) == 'ART-A8-371'
	IF FOUND()
		REPLACE cant WITH 1, pret_val WITH 100, pretd WITH 100, pret WITH ROUND(100*5.23,gnPc), pret_lei WITH pret
		DO TfLog WITH 'articol 2 (ART-A8-371, cont 371) adaugat prin do_adauga_articol() REAL, baza 100'
	ELSE
		DO TfLog WITH 'FAIL: ART-A8-371 nu a fost gasit in rul_temp dupa do_adauga_articol()'
	ENDIF
	goObj.gridart.Refresh()
	DOEVENTS FORCE

	*========================= DOC SECUNDAR: FACTURA 12000 (transport), 1000 LEI, TVA la furnizor =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 12000
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 710
	goDlg.cPartener = 'TRANS OIL'
	goDlg.cExplicatia = 'Factura test TVA secundare - transport'
	goDlg.nSuma = 1000
	goDlg.nSumaLei = 0
	goDlg.nCurs = 0
	goDlg.nIdExplicatieTva = 208
	goDlg.cExplicatieTva = 'ACH. INT. 21%'
	goDlg.optTipTva.Value = 1 && F = furnizor
	goDlg.chkInValutaCont.Value = 0
	goDlg.chkParticipaValuta.Value = 1
	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO TfLog WITH 'DOC secundar: valideaza (pe dialog) = ' + TRANSFORM(goDlg.valideaza())
	DO ConstruiesteToDlg
	DO TfLog WITH 'DOC secundar: do_adauga_factura = ' + TRANSFORM(goObj.do_adauga_factura(goT))
	goDlg.Hide()
	DOEVENTS FORCE

	LOCAL llB2, llT2
	SELECT introdc
	LOCATE FOR nract = 12000 AND INLIST(tip_rand,'B','S')
	llB2 = FOUND()
	LOCATE FOR nract = 12000 AND tip_rand == 'T'
	llT2 = FOUND()
	IF llB2 AND llT2
		DO TfLog WITH 'PASS: factura secundara 12000 adaugata (B+T)'
	ELSE
		DO TfLog WITH 'FAIL: factura secundara 12000 - B?=' + TRANSFORM(llB2) + ' T?=' + TRANSFORM(llT2)
	ENDIF

	*========================= PAS 0: setup gata, activez spargerea secundarelor si sincronizez =========================
	goObj.lSpargeSecundare = .T.
	goObj.sincronizeaza()
	DOEVENTS FORCE
	goObj.grid1.Refresh()
	DOEVENTS FORCE

	DO TfLog WITH 'DIAG dump introdc dupa sincronizare#1 (stare de baza):'
	DO DumpIntrodc

	*-- VERIFICARE stare de baza: principala sparta S(212)=900 S(371)=100 (scc=401 mostenit), 1 T
	*-- (scc=401, suma_val=210 EUR); secundara sparta S(212)=900 lei S(371)=100 lei (scc=401), 1 T
	*-- (scc=401, suma=210 lei, 21% din 1000)
	LOCAL llS212p, llS371p, lcS212SccP, lcS371SccP, lnCntTp, lnTValP
	SELECT introdc
	LOCATE FOR tip_rand == 'S' AND ALLTRIM(scd) == '212' AND nract = 15000
	llS212p = FOUND()
	IF llS212p
		lcS212SccP = ALLTRIM(NVL(scc,''))
	ENDIF
	LOCATE FOR tip_rand == 'S' AND ALLTRIM(scd) == '371' AND nract = 15000
	llS371p = FOUND()
	IF llS371p
		lcS371SccP = ALLTRIM(NVL(scc,''))
	ENDIF
	COUNT FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1 TO lnCntTp
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1
	lnTValP = suma_val
	IF llS212p AND llS371p AND lcS212SccP == '401' AND lcS371SccP == '401' AND lnCntTp = 1 AND ABS(lnTValP - 210) < 0.01
		DO TfLog WITH 'PASS pas0 principala: S212.scc=401 S371.scc=401, 1 T, suma_val=' + TRANSFORM(lnTValP) + ' (astept 210)'
	ELSE
		DO TfLog WITH 'FAIL pas0 principala: S212.scc=' + lcS212SccP + ' S371.scc=' + lcS371SccP + ' nrT=' + TRANSFORM(lnCntTp) + ' T.suma_val=' + TRANSFORM(lnTValP) + ' (astept 401/401/1/210)'
	ENDIF

	LOCAL llS212s, llS371s, lcS212SccS, lcS371SccS, lnS212SumaS, lnS371SumaS, lnCntTs, lnTSumaS
	SELECT introdc
	LOCATE FOR tip_rand == 'S' AND ALLTRIM(scd) == '212' AND nract = 12000
	llS212s = FOUND()
	IF llS212s
		lcS212SccS = ALLTRIM(NVL(scc,''))
		lnS212SumaS = suma
	ENDIF
	LOCATE FOR tip_rand == 'S' AND ALLTRIM(scd) == '371' AND nract = 12000
	llS371s = FOUND()
	IF llS371s
		lcS371SccS = ALLTRIM(NVL(scc,''))
		lnS371SumaS = suma
	ENDIF
	COUNT FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 TO lnCntTs
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000
	lnTSumaS = suma
	IF llS212s AND llS371s AND lcS212SccS == '401' AND lcS371SccS == '401' AND ABS(lnS212SumaS - 900) < 0.01 ;
			AND ABS(lnS371SumaS - 100) < 0.01 AND lnCntTs = 1 AND ABS(lnTSumaS - 210) < 0.01
		DO TfLog WITH 'PASS pas0 secundara: S212=' + TRANSFORM(lnS212SumaS) + ' S371=' + TRANSFORM(lnS371SumaS) + ' scc=401/401, 1 T, suma=' + TRANSFORM(lnTSumaS) + ' (astept 900/100/1/210)'
	ELSE
		DO TfLog WITH 'FAIL pas0 secundara: S212=' + TRANSFORM(lnS212SumaS) + '(scc=' + lcS212SccS + ') S371=' + TRANSFORM(lnS371SumaS) + '(scc=' + lcS371SccS + ') nrT=' + TRANSFORM(lnCntTs) + ' T.suma=' + TRANSFORM(lnTSumaS)
	ENDIF

	*========================= PAS 0 (HarnessStep 0): stare de baza dupa sincronizare#1 =========================
	DO HarnessStep WITH 0, 'stare_baza: principala T.suma_val=' + TRANSFORM(lnTValP) + ' secundara S212=' + TRANSFORM(lnS212SumaS) + ' S371=' + TRANSFORM(lnS371SumaS) + ' T.suma=' + TRANSFORM(lnTSumaS)

	*========================= PAS 1 (BUG 2): editez scc pe S(212) al SECUNDAREI, 401 -> 404 =========================
	SELECT introdc
	LOCATE FOR tip_rand == 'S' AND ALLTRIM(scd) == '212' AND nract = 12000
	GO RECNO()
	*-- GotFocus REAL: seteaza Thisform.nOldVal = valoarea curenta a celulei
	goObj.Grid1.cScc.Text1.Value = scc
	goObj.Grid1.cScc.Text1.GotFocus()
	DO TfLog WITH 'dupa GotFocus real pe S(212) secundara: Thisform.nOldVal=' + TRANSFORM(goObj.nOldVal) + ' (astept 401)'
	goObj.Grid1.cScc.Text1.Value = '404'
	TRY
		goObj.Grid1.cScc.Text1.Valid()
		DO TfLog WITH 'PASS apel REAL: Grid1.cScc.Text1.Valid() executat pe S(212) secundara'
	CATCH TO loExc1
		DO TfLog WITH 'PAS1 EROARE ' + TRANSFORM(loExc1.ErrorNo) + ' [' + loExc1.Message + '] in ' + loExc1.Procedure + ':' + TRANSFORM(loExc1.LineNo) + ' (' + loExc1.LineContents + ')'
	ENDTRY

	*-- verific armarea sincronizarii amanate, apoi las timerul sa traga (fallback direct daca nu apuca)
	IF goObj.lSyncPending
		DO TfLog WITH 'PASS pas1: goObj.lSyncPending=.T. dupa Valid pe S(212) secundara'
	ELSE
		DO TfLog WITH 'FAIL pas1: goObj.lSyncPending=.F. dupa Valid pe S(212) secundara (astept .T.)'
	ENDIF

	LOCAL i
	FOR i = 1 TO 50
		DOEVENTS FORCE
		IF !goObj.lSyncPending
			EXIT
		ENDIF
	ENDFOR
	IF goObj.lSyncPending
		DO TfLog WITH 'INFO: tmrSync nu a tras sub DOEVENTS - fallback apel direct goObj.tmrSync.Timer()'
		goObj.tmrSync.Timer()
	ENDIF
	goObj.grid1.Refresh()
	DOEVENTS FORCE

	DO TfLog WITH 'DIAG dump introdc dupa scc secundara 401->404 pe S(212) + resincronizare:'
	DO DumpIntrodc

	*-- VERIFICARE bug2: secundara are acum 2 T-uri 4426 - scc=404 (baza 212=900 lei) suma=189,
	*-- scc=401 (baza 371=100 lei) suma=21; principala ramane neschimbata (1 T, suma_val=210)
	LOCAL llT404s1, llT401s1, lnT404ValS1, lnT401ValS1, lnCntTs1
	SELECT introdc
	COUNT FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 TO lnCntTs1
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 AND ALLTRIM(NVL(scc,'')) == '404'
	llT404s1 = FOUND()
	IF llT404s1
		lnT404ValS1 = suma
	ENDIF
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 AND ALLTRIM(NVL(scc,'')) == '401'
	llT401s1 = FOUND()
	IF llT401s1
		lnT401ValS1 = suma
	ENDIF
	IF lnCntTs1 = 2 AND llT404s1 AND llT401s1 AND ABS(lnT404ValS1 - 189) < 0.01 AND ABS(lnT401ValS1 - 21) < 0.01
		DO TfLog WITH 'PASS pas1 bug2 secundara regrupata: T(scc=404).suma=' + TRANSFORM(lnT404ValS1) + ' (astept 189) T(scc=401).suma=' + TRANSFORM(lnT401ValS1) + ' (astept 21)'
	ELSE
		DO TfLog WITH 'FAIL pas1 bug2 secundara regrupata: nrT=' + TRANSFORM(lnCntTs1) + ' T404?=' + TRANSFORM(llT404s1) + '(suma=' + TRANSFORM(IIF(llT404s1,lnT404ValS1,-1)) + ') T401?=' + TRANSFORM(llT401s1) + '(suma=' + TRANSFORM(IIF(llT401s1,lnT401ValS1,-1)) + ')'
	ENDIF

	LOCAL lnCntTp1, lnTValP1
	COUNT FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1 TO lnCntTp1
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1
	lnTValP1 = suma_val
	IF lnCntTp1 = 1 AND ABS(lnTValP1 - 210) < 0.01
		DO TfLog WITH 'PASS pas1 principala neschimbata: 1 T, suma_val=' + TRANSFORM(lnTValP1) + ' (astept 210)'
	ELSE
		DO TfLog WITH 'FAIL pas1 principala neschimbata: nrT=' + TRANSFORM(lnCntTp1) + ' T.suma_val=' + TRANSFORM(lnTValP1) + ' (astept 1/210)'
	ENDIF

	*========================= PAS 1 (HarnessStep 1): bug2 regrupare secundara =========================
	DO HarnessStep WITH 1, 'bug2_regrupare_secundara: T(404).suma=' + TRANSFORM(IIF(llT404s1,lnT404ValS1,-1)) + ' T(401).suma=' + TRANSFORM(IIF(llT401s1,lnT401ValS1,-1)) + ' principala.T.suma_val=' + TRANSFORM(lnTValP1)

	*========================= PAS 2 (BUG 1): S(212) al PRINCIPALEI - scrierea in cursor INAINTE de Valid =========================
	SELECT introdc
	LOCATE FOR tip_rand == 'S' AND ALLTRIM(scd) == '212' AND nract = 15000
	GO RECNO()
	*-- GotFocus REAL: seteaza Thisform.nOldVal
	goObj.Grid1.cScc.Text1.Value = scc
	goObj.Grid1.cScc.Text1.GotFocus()
	DO TfLog WITH 'dupa GotFocus real pe S(212) principala: Thisform.nOldVal=' + TRANSFORM(goObj.nOldVal) + ' (astept 401)'
	*-- SIMULEZ scrierea grilei in cursor INAINTE de Valid (cazul Enter, capcana bug1)
	SELECT introdc
	REPLACE scc WITH '404'
	goObj.Grid1.cScc.Text1.Value = '404'
	TRY
		goObj.Grid1.cScc.Text1.Valid()
		DO TfLog WITH 'PASS apel REAL: Grid1.cScc.Text1.Valid() executat pe S(212) principala (dupa scriere anticipata in cursor)'
	CATCH TO loExc2
		DO TfLog WITH 'PAS2 EROARE ' + TRANSFORM(loExc2.ErrorNo) + ' [' + loExc2.Message + '] in ' + loExc2.Procedure + ':' + TRANSFORM(loExc2.LineNo) + ' (' + loExc2.LineContents + ')'
	ENDTRY

	*-- ASSERT imediat (regresie bug1): pe codul vechi lSyncPending ar fi ramas .F. aici, pentru ca
	*-- garda compara This.Value cu introdc.scc (deja scris) in loc de Thisform.nOldVal
	IF goObj.lSyncPending
		DO TfLog WITH 'PASS pas2 bug1: goObj.lSyncPending=.T. dupa Valid pe S(212) principala (cu scc deja scris in cursor)'
	ELSE
		DO TfLog WITH 'FAIL pas2 bug1: goObj.lSyncPending=.F. dupa Valid pe S(212) principala (regresie - garda veche nu arma sincronizarea)'
	ENDIF

	FOR i = 1 TO 50
		DOEVENTS FORCE
		IF !goObj.lSyncPending
			EXIT
		ENDIF
	ENDFOR
	IF goObj.lSyncPending
		DO TfLog WITH 'INFO: tmrSync nu a tras sub DOEVENTS - fallback apel direct goObj.tmrSync.Timer()'
		goObj.tmrSync.Timer()
	ENDIF
	goObj.grid1.Refresh()
	DOEVENTS FORCE

	DO TfLog WITH 'DIAG dump introdc dupa scc principala 401->404 pe S(212) + resincronizare:'
	DO DumpIntrodc

	*-- VERIFICARE: principala are acum 2 T-uri 4426 - scc=404 (baza 212=900 EUR) suma_val=189,
	*-- scc=401 (baza 371=100 EUR) suma_val=21; secundara ramane 189/21 lei neschimbata
	LOCAL llT404p2, llT401p2, lnT404ValP2, lnT401ValP2, lnCntTp2
	SELECT introdc
	COUNT FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1 TO lnCntTp2
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1 AND ALLTRIM(NVL(scc,'')) == '404'
	llT404p2 = FOUND()
	IF llT404p2
		lnT404ValP2 = suma_val
	ENDIF
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1 AND ALLTRIM(NVL(scc,'')) == '401'
	llT401p2 = FOUND()
	IF llT401p2
		lnT401ValP2 = suma_val
	ENDIF
	IF lnCntTp2 = 2 AND llT404p2 AND llT401p2 AND ABS(lnT404ValP2 - 189) < 0.01 AND ABS(lnT401ValP2 - 21) < 0.01
		DO TfLog WITH 'PASS pas2 bug1 principala regrupata: T(scc=404).suma_val=' + TRANSFORM(lnT404ValP2) + ' (astept 189) T(scc=401).suma_val=' + TRANSFORM(lnT401ValP2) + ' (astept 21)'
	ELSE
		DO TfLog WITH 'FAIL pas2 bug1 principala regrupata: nrT=' + TRANSFORM(lnCntTp2) + ' T404?=' + TRANSFORM(llT404p2) + '(suma_val=' + TRANSFORM(IIF(llT404p2,lnT404ValP2,-1)) + ') T401?=' + TRANSFORM(llT401p2) + '(suma_val=' + TRANSFORM(IIF(llT401p2,lnT401ValP2,-1)) + ')'
	ENDIF

	LOCAL llT404s2, llT401s2, lnT404ValS2, lnT401ValS2, lnCntTs2
	COUNT FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 TO lnCntTs2
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 AND ALLTRIM(NVL(scc,'')) == '404'
	llT404s2 = FOUND()
	IF llT404s2
		lnT404ValS2 = suma
	ENDIF
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 AND ALLTRIM(NVL(scc,'')) == '401'
	llT401s2 = FOUND()
	IF llT401s2
		lnT401ValS2 = suma
	ENDIF
	IF lnCntTs2 = 2 AND llT404s2 AND llT401s2 AND ABS(lnT404ValS2 - 189) < 0.01 AND ABS(lnT401ValS2 - 21) < 0.01
		DO TfLog WITH 'PASS pas2 secundara neschimbata: T(404).suma=' + TRANSFORM(lnT404ValS2) + ' T(401).suma=' + TRANSFORM(lnT401ValS2) + ' (astept 189/21)'
	ELSE
		DO TfLog WITH 'FAIL pas2 secundara neschimbata: nrT=' + TRANSFORM(lnCntTs2) + ' T404?=' + TRANSFORM(llT404s2) + '(suma=' + TRANSFORM(IIF(llT404s2,lnT404ValS2,-1)) + ') T401?=' + TRANSFORM(llT401s2) + '(suma=' + TRANSFORM(IIF(llT401s2,lnT401ValS2,-1)) + ')'
	ENDIF

	*========================= PAS 2 (HarnessStep 2): bug1 garda + regrupare principala =========================
	DO HarnessStep WITH 2, 'bug1_garda_scc: principala T(404)=' + TRANSFORM(IIF(llT404p2,lnT404ValP2,-1)) + ' T(401)=' + TRANSFORM(IIF(llT401p2,lnT401ValP2,-1)) + ' secundara T(404)=' + TRANSFORM(IIF(llT404s2,lnT404ValS2,-1)) + ' T(401)=' + TRANSFORM(IIF(llT401s2,lnT401ValS2,-1))

	*========================= PAS 3: persistenta - inca o sincronizare (F5), fara alte editari =========================
	goObj.sincronizeaza()
	DOEVENTS FORCE
	goObj.grid1.Refresh()
	DOEVENTS FORCE

	DO TfLog WITH 'DIAG dump introdc dupa sincronizare#2 (persistenta):'
	DO DumpIntrodc

	LOCAL llT404p3, llT401p3, lnT404ValP3, lnT401ValP3, lnCntTp3
	SELECT introdc
	COUNT FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1 TO lnCntTp3
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1 AND ALLTRIM(NVL(scc,'')) == '404'
	llT404p3 = FOUND()
	IF llT404p3
		lnT404ValP3 = suma_val
	ENDIF
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nr_doc = 1 AND ALLTRIM(NVL(scc,'')) == '401'
	llT401p3 = FOUND()
	IF llT401p3
		lnT401ValP3 = suma_val
	ENDIF
	LOCAL llT404s3, llT401s3, lnT404ValS3, lnT401ValS3, lnCntTs3
	COUNT FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 TO lnCntTs3
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 AND ALLTRIM(NVL(scc,'')) == '404'
	llT404s3 = FOUND()
	IF llT404s3
		lnT404ValS3 = suma
	ENDIF
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 AND ALLTRIM(NVL(scc,'')) == '401'
	llT401s3 = FOUND()
	IF llT401s3
		lnT401ValS3 = suma
	ENDIF
	IF lnCntTp3 = 2 AND llT404p3 AND llT401p3 AND ABS(lnT404ValP3 - 189) < 0.01 AND ABS(lnT401ValP3 - 21) < 0.01 ;
			AND lnCntTs3 = 2 AND llT404s3 AND llT401s3 AND ABS(lnT404ValS3 - 189) < 0.01 AND ABS(lnT401ValS3 - 21) < 0.01
		DO TfLog WITH 'PASS pas3 persistenta: splitul ramane identic dupa resincronizare (principala 189/21 EUR, secundara 189/21 lei, ambele pe 404/401)'
	ELSE
		DO TfLog WITH 'FAIL pas3 persistenta: principala T404=' + TRANSFORM(IIF(llT404p3,lnT404ValP3,-1)) + ' T401=' + TRANSFORM(IIF(llT401p3,lnT401ValP3,-1)) + ' secundara T404=' + TRANSFORM(IIF(llT404s3,lnT404ValS3,-1)) + ' T401=' + TRANSFORM(IIF(llT401s3,lnT401ValS3,-1))
	ENDIF

	*========================= PAS 3 (HarnessStep 3): persistenta regrupare =========================
	DO HarnessStep WITH 3, 'persistenta_regrupare: principala T404=' + TRANSFORM(IIF(llT404p3,lnT404ValP3,-1)) + ' T401=' + TRANSFORM(IIF(llT401p3,lnT401ValP3,-1)) + ' secundara T404=' + TRANSFORM(IIF(llT404s3,lnT404ValS3,-1)) + ' T401=' + TRANSFORM(IIF(llT401s3,lnT401ValS3,-1))

	*========================= PAS 4: recalc_tva_document pe secundara sparta, total dublat (2000 lei) =========================
	LOCAL lcDocKeySec
	lcDocKeySec = ''
	SELECT introdc
	LOCATE FOR nract = 12000 AND !EMPTY(NVL(doc_key,''))
	IF FOUND()
		lcDocKeySec = doc_key
	ENDIF
	DO TfLog WITH 'doc_key secundara = [' + lcDocKeySec + ']'

	SELECT introdc
	REPLACE suma_doc WITH 2000 FOR nract = 12000 AND INLIST(tip_rand,'S','T')
	DO TfLog WITH 'secundara: suma_doc dublat la 2000 lei pe randurile S si T'

	goObj.recalc_tva_document(lcDocKeySec)
	DOEVENTS FORCE
	goObj.grid1.Refresh()
	DOEVENTS FORCE

	DO TfLog WITH 'DIAG dump introdc dupa recalc_tva_document(secundara, total 2000):'
	DO DumpIntrodc

	*-- VERIFICARE bug2 (recalc_tva_document): T-urile secundarei se recalculeaza proportional cu
	*-- ponderea creditorului (404=90% baza, 401=10% baza), nu ambele la totalul intreg*21%
	LOCAL llT404s4, llT401s4, lnT404ValS4, lnT401ValS4, lnCntTs4
	SELECT introdc
	COUNT FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 TO lnCntTs4
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 AND ALLTRIM(NVL(scc,'')) == '404'
	llT404s4 = FOUND()
	IF llT404s4
		lnT404ValS4 = suma
	ENDIF
	LOCATE FOR tip_rand == 'T' AND ALLTRIM(scd) == '4426' AND nract = 12000 AND ALLTRIM(NVL(scc,'')) == '401'
	llT401s4 = FOUND()
	IF llT401s4
		lnT401ValS4 = suma
	ENDIF
	IF lnCntTs4 = 2 AND llT404s4 AND llT401s4 AND ABS(lnT404ValS4 - 378) < 0.01 AND ABS(lnT401ValS4 - 42) < 0.01
		DO TfLog WITH 'PASS pas4 recalc_tva_document proportional: T(scc=404).suma=' + TRANSFORM(lnT404ValS4) + ' (astept 378) T(scc=401).suma=' + TRANSFORM(lnT401ValS4) + ' (astept 42)'
	ELSE
		DO TfLog WITH 'FAIL pas4 recalc_tva_document proportional: nrT=' + TRANSFORM(lnCntTs4) + ' T404?=' + TRANSFORM(llT404s4) + '(suma=' + TRANSFORM(IIF(llT404s4,lnT404ValS4,-1)) + ') T401?=' + TRANSFORM(llT401s4) + '(suma=' + TRANSFORM(IIF(llT401s4,lnT401ValS4,-1)) + ') (pe codul vechi ambele ar fi 420)'
	ENDIF

	*========================= PAS 4 (HarnessStep 4): recalc_tva_document proportional pe secundara =========================
	DO HarnessStep WITH 4, 'recalc_tva_secundara: T(404).suma=' + TRANSFORM(IIF(llT404s4,lnT404ValS4,-1)) + ' T(401).suma=' + TRANSFORM(IIF(llT401s4,lnT401ValS4,-1))

	DO TfLog WITH 'GATA - scenariu runda30 (TVA secundare + garda scc)'
	DO HarnessDone WITH 'done'

CATCH TO loExc
	DO TfLog 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 ConstruiesteToDlg
	*-- toDlg construit INLINE in programul PRINCIPAL (ADDPROPERTY da eroare 11 din proceduri in
	*-- acest runtime headless - vezi docs/testare-ui-vfp.md capcana l).
	goT = CREATEOBJECT('Empty')
	=ADDPROPERTY(goT,'serie_act',ALLTRIM(NVL(goDlg.cSerieAct,'')))
	=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',IIF(goDlg.chkInValutaCont.Value=1 AND goDlg.nCurs<>0,IIF(goDlg.nSumaLei<>0,goDlg.nSumaLei,ROUND(goDlg.nSuma*goDlg.nCurs,gnPC)),goDlg.nSuma))
	=ADDPROPERTY(goT,'suma_val',IIF(goDlg.chkInValutaCont.Value=1 AND goDlg.nCurs<>0,goDlg.nSuma,0))
	=ADDPROPERTY(goT,'in_valuta',goDlg.chkInValutaCont.Value)
	=ADDPROPERTY(goT,'participa_valuta',(goDlg.chkParticipaValuta.Value=1))
	=ADDPROPERTY(goT,'id_valuta',IIF(goDlg.chkInValutaCont.Value=1,goDlg.nIdValuta,0))
	=ADDPROPERTY(goT,'curs',IIF(goDlg.chkInValutaCont.Value=1,goDlg.nCurs,0))
	=ADDPROPERTY(goT,'nume_val',IIF(goDlg.chkInValutaCont.Value=1,ALLTRIM(NVL(goDlg.cValuta,'')),''))
	=ADDPROPERTY(goT,'cTipTva',IIF(goDlg.optTipTva.Value=1,'F',IIF(goDlg.optTipTva.Value=2,'D','N')))
	=ADDPROPERTY(goT,'id_jtva_coloana',goDlg.nIdExplicatieTva)
	=ADDPROPERTY(goT,'dvi_serie',CHRTRAN(ALLTRIM(NVL(goDlg.cSerieDvi,'')),CHR(34),''))
	=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)
ENDPROC

PROCEDURE CreeazaRulTemp
	*-- structura reala a rul_temp (vezi ointroduceri.vc2:11030 si test_adauga_repere_212.prg).
	IF USED('rul_temp')
		USE IN ('rul_temp')
	ENDIF
	CREATE CURSOR rul_temp ( ;
		id_rul N(20,0), an N(4,0), luna N(2,0), cod N(20,0);
		, serie C(100), nnir N(20,0), id_articol N(10,0), denumire C(100);
		, codmat C(50), codmatf C(50), codbare C(50), um C(20);
		, id_gestiune N(5,0), nume_gestiune C(50), cgest C(20), nr_pag N(2,0);
		, gestionar C(50), comisie_receptie1 C(50), comisie_receptie2 C(50), comisie_receptie3 C(50);
		, cant N(14,3), cante N(14,3), cont C(10), acont C(20);
		, pret N(18,4), tva N(18,4), valoare N(18,4), valtva N(18,4);
		, proc_tva N(5,2), pretv N(18,4), pretvtva N(18,4), tvav N(18,4);
		, valoarev N(18,4), valtvav N(18,4), proc_tvav N(5,2), adaos N(18,4);
		, adaos_tva N(18,4), valoare_adaos N(18,4), valtva_adaos N(18,4), procent N(10,3);
		, discunitar N(18,4), discount_tva N(18,4), valoare_discount N(18,4), valtva_discount N(18,4);
		, proc_disc N(7,3), pretd N(14,4), curs N(18,4), id_valuta N(10,0);
		, nume_val C(20), id_lucrare N(20,0), nrord C(100), id_responsabil N(10,0);
		, nresp C(100), resp_marca N(18,4), dataact T, id_util N(5,0);
		, util C(30), dataora T, id_sectie N(5,0), sectie C(50);
		, csectie C(10), alcooltutun N(1,0), datain T, dataout T;
		, id_fact N(20,0), id_gestiunec N(10,0), nume_gestiunec C(50), cgestc C(20);
		, id_responsabilc N(5,0), id_reteta N(10,0), reteta C(50), nrespc C(100);
		, respc_marca N(18,4), id_set N(20,0), id_tip_rulaj N(10,0), tip_rulaj C(50);
		, id_obiect_depozit N(10,0), id_rul_aux N(10,0), nract N(20,0), serie_act C(10);
		, dataoras T, id_utils N(10,0), lot C(30), adata_expirare T;
		, id_sucursala N(5,0), sucursala C(250), id_part_rez N(10,0), part_rez C(100);
		, id_lucrare_rez N(10,0), nrord_rez C(100), subgrupa C(100), grupa C(100);
		, id_subgrupa N(5,0), id_grupa N(5,0), in_stoc N(1,0), in_crm N(1,0);
		, id_furnizor N(10,0), furnizor C(100), id_jtva_coloana N(4,0), explicatie_tva C(100);
		, taxcode N(6,0), taxname C(254), saft_procent N(18,4), codnc8 C(20);
		, id_tara_origine N(4,0), cod_tara_origine C(5), tara_origine C(50), greutate N(12,4);
		, fdoc C(30), id_fdoc N(18,4);
		, pret_lei N(18,4), pret_val N(14,4), cota_tva N(6,2))
ENDPROC

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 DumpIntrodc
	LOCAL lnSel
	lnSel = SELECT()
	SELECT introdc
	SCAN
		DO TfLog WITH '  recno=' + TRANSFORM(RECNO()) + ' nract=' + TRANSFORM(NVL(nract,0)) + ' nr_doc=' + TRANSFORM(NVL(nr_doc,0)) + ' tip_rand=' + tip_rand + ;
			' scd=' + ALLTRIM(scd) + ' scc=' + ALLTRIM(NVL(scc,'')) + ' ptva=' + TRANSFORM(ptva) + ;
			' suma=' + TRANSFORM(suma) + ' suma_val=' + TRANSFORM(suma_val) + ;
			' mod_manual=' + TRANSFORM(NVL(mod_manual,0)) + ' partc=[' + ALLTRIM(NVL(partc,'')) + ']'
	ENDSCAN
	SELECT (lnSel)
ENDPROC

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

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

PROCEDURE TfLog
	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'
			DO CreeazaRulTemp
		ELSE
			CREATE CURSOR (m.tcCursor) (id_temp N(1))
		ENDIF
		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)
ENDPROC
