* test_import_nota_ui.prg
* Test UI VIZIBIL pentru clasa IMPORT_nota (COMUN\clase\ointroduceri.vcx,
* parent frm_termin_renunt OF _frm_child.vcx). Spre deosebire de
* test_repro_import_nota.prg (care doar instantiaza headless), acesta:
*   1. Populeaza introdc (RecordSource Grid1) si rul_temp (RecordSource GridArt)
*      cu randuri realiste (2 documente, TVA 19/21%, 2 articole).
*   2. Afiseaza formularul REAL, maximizat (modeless).
*   3. Executa actiuni cu verificare PASS/FAIL a efectului in date.
*   4. Handshake cu harness-ul PowerShell prin procedurile din ui_harness.prg
*      (HarnessStep/HarnessDone) - screenshot dupa fiecare pas; auto-continue daca
*      ruleaza fara harness.
* Pasi: 0 afisare, 1 recalculeaza, 2 sincronizeaza, 3 sterge articol (direct),
*       4 sterge articol prin BUTONUL real (mock amessagebox) - verifica bugul
*       "Record is out of range" din do_calculeaza_diferente.
*
* Rulare (prin harness):
*   powershell -ExecutionPolicy Bypass -File vfp_ui_harness.ps1 -TestPrg <acest.prg> -Steps @(...)
* Log: test_import_nota_ui_log.txt

*-- SET SAFETY OFF + TALK OFF PRIMELE, INAINTE de orice STRTOFILE: altfel prima scriere a
*-- log-ului (daca fisierul exista deja) scoate dialogul modal "overwrite?" care blocheaza
*-- rularea headless; iar TALK ON face SUM/CALCULATE sa deseneze text parazit peste formular.
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_import_nota_ui_log.txt"
gcSyncDir = "D:\ROA\ROAGEST\COMUN\utile\Teste\achizitie_import\uisync\"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcUILog)

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

	*----- PROCEDURI -----
	SET PROCEDURE TO PROCEDURI
	*-- mock amessagebox INCARCAT PRIMUL (inaintea oproceduri_comune.prg, care are amessagebox
	*-- real): in VFP, la nume duplicat de procedura, castiga fisierul cautat PRIMUL in lista.
	SET PROCEDURE TO "D:\ROA\ROAGEST\COMUN\utile\Teste\mock_amessagebox.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

	*----- Harness UI reutilizabil (mock amessagebox e incarcat mai sus, primul). -----
	SET PROCEDURE TO "D:\ROA\ROAGEST\COMUN\utile\Teste\ui_harness.prg" ADDITIVE

	DO UILog WITH 'SET PROCEDURE OK'

	*----- Globale minimale cerute de definitia clasei / Init -----
	PUBLIC gnPc, gnPval, gnPcant, gnPpret, gnPPretval, gnPCurs, gnPVal, gnPPretV, gnPPretVal
	gnPc = 2
	gnPval = 2
	gnPVal = 2
	gnPcant = 3
	gnPpret = 4
	gnPPretval = 4
	gnPPretVal = 4
	gnPPretV = 4
	gnPCurs = 4

	PUBLIC gl406, gcS, gnAn, gnLuna, gnIdFirma, gnIdSucursala, gcAcces, gcFirma
	*-- SAFT (406) mereu activ in teste: exercita GetTaxCode/GetTaxCodeIdPart/taxcode pe randuri.
	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)

	*----- goExecutor dummy: creeaza cursoarele cerute fara Oracle real -----
	PUBLIC goExecutor
	goExecutor = CREATEOBJECT('dummyexecutor')
	DO UILog WITH 'goExecutor dummy creat'

	*----- goApp dummy: _frm_base.Init cere goApp.ReadIni (FONTSIZEPLUS/FONTNAME) -----
	PUBLIC goApp
	goApp = CREATEOBJECT('dummyapp')
	DO UILog WITH 'goApp dummy creat'

	*----- poAct dummy: obiectul act (curs valutar, procent TVA) -----
	PUBLIC poAct
	poAct = CREATEOBJECT('Custom')
	poAct.AddProperty('nume_val', 'EUR')
	poAct.AddProperty('curs', 5)
	poAct.AddProperty('proc_tva', 1.19)
	poAct.AddProperty('nnir', 0)
	*-- ceruta de UpdateExplicatieSAFT (PAS 5: gl406=.T. => aplica_explicatie_tva -> UpdateExplicatieSAFT -> poAct.dataact)
	poAct.AddProperty('dataact', DATE())

	*----- jtva_coloane2: structura din ointroduceri.prg (SELECT ... INTO CURSOR). Perechi
	*      baza/TVA REALE (Oracle dev, vjtva_coloane, 17.07.2026), regimul curent 21%/11%
	*      (post 01.08.2025) + import (DVI): id_tva pe baza leaga spre coloana T (aplica_explicatie_tva). -----
	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, 212, 'ACH. INT. 11%', 1, 0, 1, 'RO11B', 'RO11T', 11, 213)
	INSERT INTO jtva_coloane2 VALUES (4, 213, 'TVA ACH. INT. 11%', 1, 0, 1, 'RO11T', '', 11, 0)
	INSERT INTO jtva_coloane2 VALUES (5, 220, 'ACH. IMP. 21%', 1, 0, 1, 'FO21B', 'FO21T', 21, 221)
	INSERT INTO jtva_coloane2 VALUES (6, 221, 'TVA ACH. IMP. 21%', 1, 0, 1, 'FO21T', '', 21, 0)
	DO UILog WITH 'jtva_coloane2 creat (7 randuri, perechi reale 21%/11%/import)'

	*----- saft_taxtable: coduri reale (vsaft_taxtable, achizitii interne + import, cota 21%/11%) -----
	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)
	INSERT INTO saft_taxtable (taxname, taxcode, descriere, procent_taxa, tva, wht, tva_achizitii, tva_livrari) ;
		VALUES ('301105-Achizitii de bunuri si servicii taxabile cu cota de 11%, altele decat importurile si cele supuse masurilor de simplificare', 301105, '', 11, 1, 0, 1, 0)
	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 UILog WITH 'saft_taxtable creat (coduri reale 301104/301105/301204)'

	*----- introdc: RecordSource Grid1. id_jtva_coloana NULL (recalc_diferente il seteaza Null pe D). -----
	IF USED('introdc')
		USE IN ('introdc')
	ENDIF
	CREATE CURSOR introdc ( ;
		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))

	*-- Document unic (nract=1001/serie A pentru toate randurile => acelasi doc_key). doc_key
	*-- populat explicit (padded C(80) la stocare, ca in fluxul real) - vezi PAS 5: comparatia
	*-- ==Thisform.calc_doc_key() (trim) NU se potriveste niciodata cu campul stocat padded
	*-- (bug documentat in docs\handoff_teste_ui_import.md).
	*-- 2 randuri Baza (B) 21%+11% + 2 randuri TVA (T). Baza totala lei = 1500, TVA total = 265
	*-- (1000*21%=210 + 500*11%=55) - regimul curent post 01.08.2025.
	INSERT INTO introdc (tip_rand, scd, scc, ascc, suma, id_jtva_coloana, ptva, explicatie_tva, ;
			in_valuta, participa_valuta, nr_doc, nract, serie_act, fdoc, partc, explicatia, ;
			cu_tva, dataact, suma_doc, doc_key) ;
		VALUES ('B','371','401','FURN01', 1000, 208, 21, 'ACH. INT. 21%', ;
			0, .T., 1, 1001, 'A', 'Factura', 'FURNIZOR ABC SRL', 'Marfa achizitionata 21%', ;
			1, DATE(), 1500, 'A|1001|0|0')
	INSERT INTO introdc (tip_rand, scd, scc, ascc, suma, id_jtva_coloana, ptva, explicatie_tva, ;
			in_valuta, participa_valuta, nr_doc, nract, serie_act, fdoc, partc, explicatia, ;
			cu_tva, dataact, suma_doc, doc_key) ;
		VALUES ('B','371','401','FURN01', 500, 212, 11, 'ACH. INT. 11%', ;
			0, .T., 1, 1001, 'A', 'Factura', 'FURNIZOR ABC SRL', 'Marfa achizitionata 11%', ;
			1, DATE(), 1500, 'A|1001|0|0')
	INSERT INTO introdc (tip_rand, scd, scc, ascc, suma, id_jtva_coloana, ptva, explicatie_tva, ;
			in_valuta, participa_valuta, nr_doc, nract, serie_act, fdoc, partc, explicatia, ;
			cu_tva, dataact, suma_doc, doc_key) ;
		VALUES ('T','4426','401','FURN01', 210, 209, 21, 'TVA ACH. INT. 21%', ;
			0, .T., 1, 1001, 'A', 'Factura', 'FURNIZOR ABC SRL', 'TVA deductibil 21%', ;
			1, DATE(), 1500, 'A|1001|0|0')
	INSERT INTO introdc (tip_rand, scd, scc, ascc, suma, id_jtva_coloana, ptva, explicatie_tva, ;
			in_valuta, participa_valuta, nr_doc, nract, serie_act, fdoc, partc, explicatia, ;
			cu_tva, dataact, suma_doc, doc_key) ;
		VALUES ('T','4426','401','FURN01', 55, 213, 11, 'TVA ACH. INT. 11%', ;
			0, .T., 1, 1001, 'A', 'Factura', 'FURNIZOR ABC SRL', 'TVA deductibil 11%', ;
			1, DATE(), 1500, 'A|1001|0|0')
	GO TOP IN introdc
	DO UILog WITH 'introdc populat (' + TRANSFORM(RECCOUNT('introdc')) + ' randuri)'

	*----- rul_temp: RecordSource GridArt. Pre-populat INAINTE de CREATEOBJECT =>
	*      Init nu il recreeaza din Oracle (llRulTempPlin=.T.). Total lei = 1500 = baza. -----
	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))
	*-- cota_tva 21 (nu 19): trebuie sa existe in jtva_coloane2 ca sparge_document sa gaseasca
	*-- coloana TVA pentru cota (altfel "Nu am gasit coloana TVA pentru cota ..." in sincronizeaza).
	INSERT INTO rul_temp (denumire, codmat, cant, pret_val, pretd, pret, pret_lei, ;
			cont, acont, serie, lot, adata_expirare, proc_disc, cota_tva) ;
		VALUES ('Laptop Dell Latitude', 'ART001', 2, 0, 0, 500, 500, ;
			'371', '01', 'SN-LAP-001', 'L001', '', 0, 21)
	INSERT INTO rul_temp (denumire, codmat, cant, pret_val, pretd, pret, pret_lei, ;
			cont, acont, serie, lot, adata_expirare, proc_disc, cota_tva) ;
		VALUES ('Mouse optic USB', 'ART002', 10, 0, 0, 50, 50, ;
			'371', '01', '', 'L002', '', 0, 21)
	GO TOP IN rul_temp
	DO UILog WITH 'rul_temp populat (' + TRANSFORM(RECCOUNT('rul_temp')) + ' articole)'

	*----- Desktop VFP vizibil, maximizat, ca formularul sa apara pe tot ecranul -----
	_SCREEN.Visible = .T.
	_SCREEN.WindowState = 2
	_SCREEN.Caption = 'TEST UI import_nota'

	DO UILog WITH 'mediu pregatit, CREATEOBJECT(import_nota, .T.)'

	SELECT introdc
	PUBLIC goObj
	goObj = CREATEOBJECT('IMPORT_nota', .T.)

	IF VARTYPE(goObj) <> 'O'
		DO UILog WITH 'FAIL: CREATEOBJECT a returnat ' + TRANSFORM(goObj)
		DO HarnessDone WITH 'done-error'
		QUIT
	ENDIF
	*-- oact folosit de recalculeaza/do_reface (This.oact.Curs)
	goObj.oact = poAct
	DO UILog WITH 'PASS: instantiere OK, afisez formularul'

	*-- WindowType=0 (modeless): altfel formularul e modal si Show() nu returneaza,
	*-- blocand pasii de test. Modeless => Show() revine, iar HarnessWaitContinue (cu DOEVENTS)
	*-- tine formularul viu si receptiv intre pasi.
	goObj.WindowType = 0
	goObj.Visible = .T.
	goObj.Show()
	goObj.WindowState = 2
	DOEVENTS FORCE

	*========================= PAS 0: formular afisat cu date =========================
	DO HarnessStep WITH 0, 'Formular afisat: Grid1=' + TRANSFORM(RECCOUNT('introdc')) + ' note, GridArt=' + TRANSFORM(RECCOUNT('rul_temp')) + ' articole'

	*========================= PAS 1: recalculeaza() =========================
	*-- Efect asteptat: nbaza_lei=1500 (1000+500), ntva_lei=265 (210+55, regim 21%/11%).
	goObj.recalculeaza()
	goObj.actualizeaza_banda()
	goObj.grid1.Refresh()
	DOEVENTS FORCE
	IF ABS(goObj.nbaza_lei - 1500) < 0.01 AND ABS(goObj.ntva_lei - 265) < 0.01
		DO UILog WITH 'PASS pas1 recalculeaza: nbaza_lei=' + TRANSFORM(goObj.nbaza_lei) + ' ntva_lei=' + TRANSFORM(goObj.ntva_lei)
	ELSE
		DO UILog WITH 'FAIL pas1 recalculeaza: nbaza_lei=' + TRANSFORM(goObj.nbaza_lei) + ' (astept 1500) ntva_lei=' + TRANSFORM(goObj.ntva_lei) + ' (astept 265)'
	ENDIF
	DO HarnessStep WITH 1, 'recalculeaza: baza=' + TRANSFORM(goObj.nbaza_lei) + ' tva=' + TRANSFORM(goObj.ntva_lei)

	*========================= PAS 2: sincronizeaza() =========================
	*-- Baza 1500 = total articole 1500 => sincronizat (banda verde, cmesajsync gol).
	*-- PASS = fara mesaje blocante; eventualul rand D auto-creat e comportament valid.
	LOCAL lnIntroBefore
	lnIntroBefore = RECCOUNT_ACTIVE('introdc')
	goObj.sincronizeaza()
	DOEVENTS FORCE
	DO UILog WITH 'diag pas2 tip_rand-uri introdc: ' + TipuriRanduri('introdc')
	IF EMPTY(goObj.cmesajsync)
		DO UILog WITH 'PASS pas2 sincronizeaza: sincronizat (cmesajsync gol, note=' + TRANSFORM(RECCOUNT_ACTIVE('introdc')) + ', dif_lei=' + TRANSFORM(goObj.ndif_lei) + ')'
	ELSE
		DO UILog WITH 'FAIL pas2 sincronizeaza: cmesajsync=[' + goObj.cmesajsync + '] note=' + TRANSFORM(RECCOUNT_ACTIVE('introdc')) + ' (era ' + TRANSFORM(lnIntroBefore) + ')'
	ENDIF
	DO HarnessStep WITH 2, 'sincronizeaza: ' + IIF(EMPTY(goObj.cmesajsync),'SINCRONIZAT','NESINCRONIZAT')

	*========================= PAS 3: sterge articol + resincronizare =========================
	*-- Sterg articolul Mouse din rul_temp. Total articole -> 1000, baza 1500 => diferenta 500,
	*-- cont dominant 371 (clasa 3) => recalc_diferente adauga rand D (DIFERENTE, scd 607).
	LOCAL lnRulBefore, lnIntroBefore3
	lnRulBefore = RECCOUNT_ACTIVE('rul_temp')
	lnIntroBefore3 = RECCOUNT_ACTIVE('introdc')
	SELECT rul_temp
	LOCATE FOR ALLTRIM(codmat) == 'ART002'
	IF FOUND()
		DELETE
	ENDIF
	*-- GO TOP: in aplicatia reala grila GridArt repozitioneaza singura pointerul pe un
	*-- rand valid dupa Refresh; aici stergem direct, deci il pozitionam noi ca do_reface ->
	*-- do_calculeaza_diferente sa nu faca GO pe un recno past-EOF (eroare 5).
	GO TOP IN rul_temp
	goObj.gridart.Refresh()
	goObj.sincronizeaza()
	DOEVENTS FORCE
	LOCAL lnRulAfter, lnHasD
	lnRulAfter = RECCOUNT_ACTIVE('rul_temp')
	SELECT introdc
	LOCATE FOR tip_rand == 'D'
	lnHasD = IIF(FOUND(), 1, 0)
	IF lnRulAfter = lnRulBefore - 1 AND lnHasD = 1
		DO UILog WITH 'PASS pas3 sterge articol: articole=' + TRANSFORM(lnRulAfter) + ' (era ' + TRANSFORM(lnRulBefore) + '), rand D DIFERENTE creat (dif=' + TRANSFORM(goObj.ndif_lei) + ')'
	ELSE
		DO UILog WITH 'FAIL pas3 sterge articol: articole=' + TRANSFORM(lnRulAfter) + ' (era ' + TRANSFORM(lnRulBefore) + '), randD=' + TRANSFORM(lnHasD)
	ENDIF
	DO HarnessStep WITH 3, 'sterge articol: articole=' + TRANSFORM(lnRulAfter) + ' randD=' + TRANSFORM(lnHasD) + ' dif=' + TRANSFORM(goObj.ndif_lei)

	*========================= PAS 4: stergere prin BUTONUL real (flux complet) =========================
	*-- Verific bugul "Record is out of range" (do_calculeaza_diferente: GO tnRecNo past-EOF dupa
	*-- DELETE). Apas chiar But_sterge_art.Click() -> do_sterge_articol -> amessagebox (mock -> 6)
	*-- -> Delete -> gridart.Refresh() -> sincronizeaza -> do_reface -> do_calculeaza_diferente.
	*-- Intrebarea: dupa DELETE, grila repozitioneaza pointerul (OK) sau ramane past-EOF (eroare 5)?
	*-- Intai verific ca mock-ul amessagebox chiar umbreste functia reala (altfel s-ar bloca pe modal).
	LOCAL lnVerifMock
	lnVerifMock = amessagebox('verificare rezolutie mock', 4+32, 'test')
	DO UILog WITH 'verif rezolutie amessagebox = ' + TRANSFORM(lnVerifMock) + ;
		IIF(lnVerifMock = 6, ' (MOCK castiga - sigur sa apas butonul)', ' (REAL castiga - risc blocaj pe modal!)')

	IF lnVerifMock <> 6
		DO UILog WITH 'PAS4 SKIP: mock-ul nu umbreste amessagebox real; nu apas butonul (as bloca pe dialog).'
	ELSE
		*-- Repopulez rul_temp cu 2 articole si pozitionez pe ULTIMUL (cazul cel mai expus la past-EOF).
		SELECT rul_temp
		DELETE ALL
		INSERT INTO rul_temp (denumire, codmat, cant, pret_val, pretd, pret, pret_lei, ;
				cont, acont, serie, lot, adata_expirare, proc_disc, cota_tva) ;
			VALUES ('Tastatura mecanica', 'ART010', 3, 0, 0, 200, 200, '371', '01', '', 'L010', '', 0, 21)
		INSERT INTO rul_temp (denumire, codmat, cant, pret_val, pretd, pret, pret_lei, ;
				cont, acont, serie, lot, adata_expirare, proc_disc, cota_tva) ;
			VALUES ('Casti audio', 'ART011', 5, 0, 0, 80, 80, '371', '01', '', 'L011', '', 0, 21)
		goObj.gridart.Refresh()
		goObj.sincronizeaza()
		DOEVENTS FORCE
		LOCAL lnRul4Before, lnRecInainte
		lnRul4Before = RECCOUNT_ACTIVE('rul_temp')
		SELECT rul_temp
		GO BOTTOM
		lnRecInainte = RECNO()
		DO UILog WITH 'PAS4: rul_temp=' + TRANSFORM(lnRul4Before) + ' articole, pozitionat pe ultimul (recno=' + TRANSFORM(lnRecInainte) + '), apas But_sterge_art.Click()'
		TRY
			goObj.But_sterge_art.Click()
			DOEVENTS FORCE
			DO UILog WITH 'PAS4 PASS (fara eroare): articole ramase=' + TRANSFORM(RECCOUNT_ACTIVE('rul_temp')) + ;
				' -> grila a repozitionat pointerul; do_calculeaza_diferente NU a crapat. Comportament corect.'
		CATCH TO loExc4
			DO UILog WITH 'PAS4 EROARE ' + TRANSFORM(loExc4.ErrorNo) + ' [' + loExc4.Message + '] in ' + ;
				loExc4.Procedure + ':' + TRANSFORM(loExc4.LineNo) + ' -> BUG REAL confirmat (' + loExc4.LineContents + ')'
		ENDTRY
	ENDIF
	DO HarnessStep WITH 4, 'sterge prin buton: articole=' + TRANSFORM(RECCOUNT_ACTIVE('rul_temp')) + ' mock=' + TRANSFORM(lnVerifMock)

	*========================= PAS 5: explicatie TVA + taxcode SAFT IN TANDEM (aplica_explicatie_tva) =========================
	*-- Documentul 1001 a fost deja "spart" de PAS 2/3 (sincronizeaza inlocuieste B/T cu randuri S/D) -
	*-- inserez o pereche B/T PROASPATA (nract=9999), neatinsa de sincronizeaza, doar pentru acest test.
	*-- Apelez METODA REALA aplica_explicatie_tva pe randul B (nu populez explicatie/taxcode manual pe T).
	*-- Asteptat: B primeste explicatie_tva/taxcode direct (randul curent); T ar trebui sa le primeasca
	*-- IN TANDEM prin Locate doc_key==calc_doc_key() - dar bugul doc_key (C(80) padded vs trim, ==
	*-- strict) face ca Locate-ul din aplica_explicatie_tva sa NU gaseasca randul T => T ramane
	*-- neschimbat. FAIL asteptat pe tandem, vezi docs\handoff_teste_ui_import.md.
	SELECT introdc
	INSERT INTO introdc (tip_rand, scd, scc, ascc, suma, id_jtva_coloana, ptva, explicatie_tva, ;
			in_valuta, participa_valuta, nr_doc, nract, serie_act, fdoc, partc, explicatia, ;
			cu_tva, dataact, suma_doc, doc_key) ;
		VALUES ('B','371','401','FURN02', 2000, 0, 0, '', ;
			0, .T., 2, 9999, 'P', 'Factura', 'FURNIZOR PAS5 SRL', 'Marfa test PAS5', ;
			0, DATE(), 2000, 'P|9999|0|0')
	INSERT INTO introdc (tip_rand, scd, scc, ascc, suma, id_jtva_coloana, ptva, explicatie_tva, ;
			in_valuta, participa_valuta, nr_doc, nract, serie_act, fdoc, partc, explicatia, ;
			cu_tva, dataact, suma_doc, doc_key) ;
		VALUES ('T','4426','401','FURN02', 0, 0, 0, '', ;
			0, .T., 2, 9999, 'P', 'Factura', 'FURNIZOR PAS5 SRL', 'TVA test PAS5', ;
			0, DATE(), 2000, 'P|9999|0|0')
	SELECT introdc
	LOCATE FOR nract = 9999 AND tip_rand == 'B'
	IF FOUND()
		goObj.aplica_explicatie_tva(208)
		SELECT introdc
		LOCATE FOR nract = 9999 AND tip_rand == 'B'
		IF FOUND() AND ALLTRIM(NVL(explicatie_tva,'')) == 'ACH. INT. 21%' AND NVL(taxcode,0) = 301104
			DO UILog WITH 'PASS pas5a (randul B, direct): explicatie_tva=[' + ALLTRIM(explicatie_tva) + '] taxcode=' + TRANSFORM(NVL(taxcode,0)) + ' (SAFT 301104 aplicat de UpdateExplicatieSAFT via GetTaxCodeIdPart)'
		ELSE
			DO UILog WITH 'FAIL pas5a (randul B): gasit=' + TRANSFORM(FOUND()) + ' explicatie_tva=[' + ALLTRIM(NVL(explicatie_tva,'?')) + '] taxcode=' + TRANSFORM(NVL(taxcode,-1))
		ENDIF
		*-- randul T IN TANDEM (asteptat de aplica_explicatie_tva prin Locate doc_key==...)
		LOCATE FOR nract = 9999 AND tip_rand == 'T'
		IF FOUND() AND ALLTRIM(NVL(explicatie_tva,'')) == 'TVA ACH. INT. 21%' AND NVL(taxcode,0) = 301104
			DO UILog WITH 'PASS pas5b (randul T, tandem): explicatie_tva/taxcode propagate corect - bugul doc_key ar fi FOST corectat'
		ELSE
			DO UILog WITH 'FAIL asteptat pas5b (randul T, tandem): bug doc_key (vezi handoff) - Locate For doc_key==Thisform.calc_doc_key() din aplica_explicatie_tva NU gaseste randul T (campul stocat C(80) padded vs calc_doc_key() trim, == strict). explicatie_tva ramasa=[' + ALLTRIM(NVL(explicatie_tva,'?')) + '] taxcode ramas=' + TRANSFORM(NVL(taxcode,-1))
		ENDIF
	ELSE
		DO UILog WITH 'PAS5 SKIP: nu am gasit randul B (nract=9999) de test'
	ENDIF
	DO HarnessStep WITH 5, 'tandem TVA/SAFT: verificat (vezi log pentru PASS/FAIL asteptat)'

	DO UILog WITH 'GATA - toate pasii executati'
	DO HarnessDone WITH 'done'

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

*==================================================================================
FUNCTION RECCOUNT_ACTIVE
	*-- numar de randuri NEsterse (SET DELETED ON => RECCOUNT le include totusi)
	LPARAMETERS tcAlias
	LOCAL lnSel, lnCnt
	lnSel = SELECT()
	SELECT (tcAlias)
	COUNT TO lnCnt FOR !DELETED()
	SELECT (lnSel)
	RETURN lnCnt
ENDFUNC

FUNCTION TipuriRanduri
	*-- concateneaza tip_rand-urile nesterse (diagnostic)
	LPARAMETERS tcAlias
	LOCAL lnSel, lcRez
	lnSel = SELECT()
	SELECT (tcAlias)
	lcRez = ''
	SCAN FOR !DELETED()
		lcRez = lcRez + ALLTRIM(NVL(tip_rand,'?')) + ' '
	ENDSCAN
	SELECT (lnSel)
	RETURN ALLTRIM(lcRez)
ENDFUNC

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

PROCEDURE UILog
	LPARAMETERS tcMsg
	LOCAL lcL
	lcL = FILETOSTR(gcUILog)
	STRTOFILE(lcL + tcMsg + CHR(13)+CHR(10), gcUILog)
ENDPROC

DEFINE CLASS dummyapp AS Custom
	*-- inlocuieste obiectul aplicatiei (wzApplication) pentru citirea settings.ini
	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
		*-- update_jtva_coloane (SELECT * FROM vjtva_coloane ...): mock cu date reale (jtva_coloane,
		*-- jtva_coloane_taxcode - mod 6, cu campurile taxcode*) - vezi GetTaxCode/GetTaxCodeIdPart.
		IF 'VJTVA_COLOANE' $ UPPER(m.tcSql)
			DO CreeazaJtvaColoaneView WITH m.tcCursor
			RETURN 1
		ENDIF
		*-- rul_temp e pre-populat in test => Init nu ajunge aici pentru el.
		*-- Structura generica minimala pentru orice alt cursor cerut.
		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
			*-- Init cere cJtvaCol4427 si INDEXeaza pe ID_JTVA_COLOANA => campul trebuie sa existe.
			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
		*-- GetCodFiscalPartenerById (oproceduri_comune.prg): cod fiscal gol => VERIFICA_RTVAI
		*-- vede llIsCIF=.F. si sare peste apelul PACK_ROARTVAI (fara Oracle real).
		LPARAMETERS tcSql, toValue
		toValue = ''
		RETURN 1
	ENDPROC
	PROCEDURE oReset
		*-- update_jtva_coloane() il apeleaza necondit dupa oExecute (updateserver.prg:611).
	ENDPROC
ENDDEFINE

PROCEDURE CreeazaJtvaColoaneView
	*-- Mock pentru "select * from vjtva_coloane ..." (update_jtva_coloane): perechi reale
	*-- (Oracle dev, 17.07.2026) baza/TVA 21%/11% + import, cu taxcode SAFT (vsaft_taxtable).
	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 (212, 'ACH. INT. 11%', 1, 0, 'RO11B', 11, 213, 1, 3, 301105)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, coloana_jc, cota_tva, afisat, ordine, taxcode) ;
		VALUES (213, 'TVA ACH. INT. 11%', 1, 0, 'RO11T', 11, 1, 4, 301105)
	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, 5, 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, 6, 301204)
ENDPROC
