* test_adauga_repere_212.prg
* Extensie a test_scenariu_import_212.prg: Marius a testat manual in aplicatia reala si a
* gasit ca populatea directa a rul_temp (INSERT INTO) era INSUFICIENTA - nu trece prin fluxul
* real de adaugare articole (butonul "Adauga repere" -> import_nota.do_adauga_articol,
* COMUN\clase\ointroduceri.vcx). Aici articolul BULDOEXCAVATOR (cont 212) se adauga apeland
* REAL goObj.do_adauga_articol() dupa ce dialogul de cautare (modal, nu poate rula headless)
* e inlocuit cu mock_cauta_nom_articole.prg (returneaza acelasi format XML ca fluxul real,
* cauta_alfa.prg ~227/510) - vezi docs/testare-ui-vfp.md pentru reteta generala.
*
* Scenariu identic cu test_scenariu_import_212.prg pana la cele 3 documente (FACTURA 100 RAJA
* DVI valuta EUR curs 5.23, FACTURA 102 TRANS OIL MOTO DVI lei, FACTURA 103 BIROUL VAMAL TVA la
* furnizor), apoi:
*   1. goObj.do_adauga_articol() REAL (nu INSERT direct) - verifica daca articolul apare in
*      rul_temp/grila dupa fluxul real (Problema 1 raportata de Marius: nu aparea deloc).
*   2. daca articolul a fost adaugat, completez cant/pret pe rand (do_adauga_articol NU seteaza
*      cantitati/preturi - doar campurile din nomenclator+antet; utilizatorul le completeaza
*      manual in grid dupa - simulez exact asta).
*   3. sincronizeaza() - verific mesajul benzii (Problema 2: repetare per document).
*
* Rulare:
*   powershell -ExecutionPolicy Bypass -File test_adauga_repere_212.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_adauga_repere_212_log.txt"
gcSyncDir = "D:\ROA\ROAGEST\COMUN\utile\Teste\achizitie_import\uisync_repere212\"
STRTOFILE("START " + TTOC(DATETIME()) + CHR(13)+CHR(10), gcUILog)

ON ERROR DO ArErr 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 ArLog 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 ArLog 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 ArLog 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
	*-- 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)

	*-- gnButon: variabila "gola" folosita in tot lantul cauta_alfa->CAUT->do_adauga_articol.
	*-- In aplicatia reala e declarata PRIVATE o singura data la pornire (Programe/roagest.prg:496)
	*-- si ramane vizibila tot restul rularii; aici o declar PUBLIC pentru acelasi efect.
	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% + import (DVI) 21% - perechi
	*      reale (Oracle dev, vjtva_coloane, 17.07.2026), regimul curent 21%/11% post 01.08.2025. -----
	IF USED('jtva_coloane2')
		USE IN ('jtva_coloane2')
	ENDIF
	CREATE CURSOR jtva_coloane2 (ordine N(8), id_jtva_coloana N(14), denumire C(100), ;
		jc N(1), jv N(1), afisat N(1), coloana_jc C(20), coloana_jv C(20), cota_tva N(6,2), id_tva N(14))
	INSERT INTO jtva_coloane2 VALUES (0, 0, '', 0, 0, 1, '', '', 0, 0)
	INSERT INTO jtva_coloane2 VALUES (1, 208, 'ACH. INT. 21%', 1, 0, 1, 'RO21B', 'RO21T', 21, 209)
	INSERT INTO jtva_coloane2 VALUES (2, 209, 'TVA ACH. INT. 21%', 1, 0, 1, 'RO21T', '', 21, 0)
	INSERT INTO jtva_coloane2 VALUES (3, 220, 'ACH. IMP. 21%', 1, 0, 1, 'FO21B', 'FO21T', 21, 221)
	INSERT INTO jtva_coloane2 VALUES (4, 221, 'TVA ACH. IMP. 21%', 1, 0, 1, 'FO21T', '', 21, 0)

	*----- saft_taxtable: coduri reale (vsaft_taxtable) - achizitii interne 21% vs import 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)
	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)

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

	*----- introdc_sablon: perechea-sablon (B 371/401 + T 4426/401) pe care do_adauga_factura o
	*      cloneaza; sablonul e la 21% (cota folosita in toate cele 3 scenarii de mai jos). -----
	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: altfel import_nota.Init il recreeaza din
	*      Oracle (rul_temp gol => llRulTempPlin=.F.) si GridArt isi pierde coloanele (eroare 1925).
	*      Randul se sterge dupa Show (ZAP, nu DELETE ALL - vezi docs/testare-ui-vfp.md capcana k).
	*      STRUCTURA FIDELA: rul_temp real = select r.*, r.pret as pret_lei, r.pretd as pret_val,
	*      000.00 as cota_tva from vrul r where 1=2 (ointroduceri.vc2:11030) - coloanele VRUL sunt
	*      preluate direct din Oracle dev (all_tab_columns, owner=MARIUSM_AUTO, table=VRUL,
	*      17.07.2026), nu aproximate; PROC_TVA exista in VRUL - eroarea 12 dintr-o rulare
	*      anterioara a testului ("Variable 'PROC_TVA' is not found") era artefact al unui mock
	*      trunchiat, nu bug de aplicatie (GATHER NAME loAct la :10107 populeaza rul_temp.proc_tva
	*      din poAct.proc_tva - camp valid pe randul curent, nu variabila globala). -----
	DO CreeazaRulTemp
	INSERT INTO rul_temp (denumire, codmat, cant, pret, pret_lei, cont, cota_tva) ;
		VALUES ('PLACEHOLDER', 'PH', 1, 1, 1, '371', 21)

	DO ArLog WITH 'cursoare pregatite, CREATEOBJECT(import_nota, .F.) - lIntern=.F. (import)'
	SELECT introdc
	PUBLIC goObj
	*-- lIntern=.F. (import): sparge_document grupeaza articolele in valuta (pret_val*cant), ca
	*-- in scenariul real al lui Marius. Activate() ar deschide automat dialogul modal "adauga
	*-- factura" (This.do_adauga(), Show(1)) pentru !lIntern - blocheaza headless; il dezarmez
	*-- cu lDialogAratat=.T. INAINTE de Show(), documentele se adauga manual mai jos.
	goObj = CREATEOBJECT('IMPORT_nota', .F.)
	IF VARTYPE(goObj) <> 'O'
		DO ArLog 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, vezi capcana k)
	SELECT rul_temp
	ZAP
	goObj.gridart.Refresh()
	DOEVENTS FORCE

	*========================= DOC 1: FACTURA 100, RAJA, DVI valuta (EUR curs 5.23) =========================
	PUBLIC goDlg, goT
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 100
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 601
	goDlg.cPartener = 'RAJA'
	goDlg.cExplicatia = 'Import marfa RAJA'
	goDlg.nSuma = 1000
	goDlg.nSumaLei = 0
	goDlg.nCurs = 5.23
	goDlg.nIdValuta = 978
	goDlg.cValuta = 'EUR'
	*-- ACH. IMP. 21% (id 220) - coloana de import (SCD 371/SCC 401, taxcode SAFT 301204)
	goDlg.nIdExplicatieTva = 220
	goDlg.cExplicatieTva = 'ACH. IMP. 21%'
	goDlg.optTipTva.Value = 2 && D = DVI
	goDlg.chkInValutaCont.Value = 1
	goDlg.chkParticipaValuta.Value = 1
	*-- DVI: TVA achitat in vama (BIROUL VAMAL), doc 101, cont 4426/401
	goDlg.cSerieDvi = ''
	goDlg.nNractDvi = 101
	goDlg.dDviData = DATE()
	goDlg.nIdFdocDVI = 2
	goDlg.cFelDocDvi = 'DVI'
	goDlg.nDviIdPartener = 900
	goDlg.cDviPartener = 'BIROUL VAMAL'
	goDlg.nDviTvaLei = 1098.30
	goDlg.nDviTvaVal = 210.00
	goDlg.actualizeaza_stare_dvi()
	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO ArLog WITH 'DOC1: valideaza (pe dialog) = ' + TRANSFORM(goDlg.valideaza())
	DO ConstruiesteToDlg
	DO ArLog WITH 'DOC1: do_adauga_factura = ' + TRANSFORM(goObj.do_adauga_factura(goT))
	goDlg.Hide()
	DOEVENTS FORCE
	DO VerificaPereche WITH 100, 5230, 1000, 'EUR', .T.
	DO VerificaDvi WITH 100, 101, 5230, 1098.30

	*========================= DOC 2: FACTURA 102, TRANS OIL MOTO, DVI lei (acelasi doc vamal 101) =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 102
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 602
	goDlg.cPartener = 'TRANS OIL MOTO'
	goDlg.cExplicatia = 'Achizitie TRANS OIL MOTO'
	goDlg.nSuma = 1000
	goDlg.nSumaLei = 0
	goDlg.nCurs = 0
	goDlg.nIdExplicatieTva = 220
	goDlg.cExplicatieTva = 'ACH. IMP. 21%'
	goDlg.optTipTva.Value = 2 && D = DVI
	goDlg.chkInValutaCont.Value = 0
	goDlg.chkParticipaValuta.Value = 1
	*-- ACELASI document vamal (nr 101, BIROUL VAMAL) ca la doc 1 - randul T se re-cheie pe
	*-- acelasi doc_key ca T-ul doc 1 (identitate = serie_act+nract+id_fdoc+id_partc, calc_doc_key).
	goDlg.cSerieDvi = ''
	goDlg.nNractDvi = 101
	goDlg.dDviData = DATE()
	goDlg.nIdFdocDVI = 2
	goDlg.cFelDocDvi = 'DVI'
	goDlg.nDviIdPartener = 900
	goDlg.cDviPartener = 'BIROUL VAMAL'
	goDlg.nDviTvaLei = 210.00
	goDlg.nDviTvaVal = 0
	goDlg.actualizeaza_stare_dvi()
	goDlg.WindowType = 0
	goDlg.Visible = .T.
	goDlg.Show()
	DOEVENTS FORCE
	DO ArLog WITH 'DOC2: valideaza (pe dialog) = ' + TRANSFORM(goDlg.valideaza())
	DO ConstruiesteToDlg
	DO ArLog WITH 'DOC2: do_adauga_factura = ' + TRANSFORM(goObj.do_adauga_factura(goT))
	goDlg.Hide()
	DOEVENTS FORCE
	DO VerificaPereche WITH 102, 1000, 0, '', .T.
	DO VerificaSumaGrupVama WITH 101, 1308.30, 2

	*========================= DOC 3: FACTURA 103, BIROUL VAMAL, TVA la furnizor (nu DVI) =========================
	goDlg = CREATEOBJECT('import_adauga_factura', goObj)
	goDlg.cFelDoc = 'Factura'
	goDlg.nIdFdoc = 1
	goDlg.nNract = 103
	goDlg.dDataAct = DATE()
	goDlg.nIdPartener = 900
	goDlg.cPartener = 'BIROUL VAMAL'
	goDlg.cExplicatia = 'Factura BIROUL VAMAL'
	goDlg.nSuma = 500
	goDlg.nSumaLei = 0
	goDlg.nCurs = 0
	*-- ACH. INT. 21% (id 208) - achizitie normala (nu import), TVA la furnizor
	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 ArLog WITH 'DOC3: valideaza (pe dialog) = ' + TRANSFORM(goDlg.valideaza())
	DO ConstruiesteToDlg
	DO ArLog WITH 'DOC3: do_adauga_factura = ' + TRANSFORM(goObj.do_adauga_factura(goT))
	goDlg.Hide()
	DOEVENTS FORCE
	DO VerificaPereche WITH 103, 500, 0, ''

	DO ArLog WITH 'DIAG dump introdc dupa cele 3 documente (inainte de articol):'
	DO DumpIntrodc

	*========================= PAS 0: cele 3 documente, structura B/T intacta (rul_temp gol) =========================
	DO HarnessStep WITH 0, 'dupa 3 documente: introdc=' + TRANSFORM(RECCOUNT_ACTIVE('introdc')) + ' randuri (astept 6, 3 perechi B/T), rul_temp=' + TRANSFORM(RECCOUNT_ACTIVE('rul_temp')) + ' articole'

	*========================= ADAUGARE ARTICOL PRIN FLUXUL REAL: do_adauga_articol() =========================
	*-- gnButon/XML furnizate de mock_cauta_nom_articole.prg (dialogul real e modal, nu ruleaza
	*-- headless) - dar callback-ul care scrie in rul_temp e chiar do_adauga_articol REAL din clasa.
	LOCAL lnRandInainte, lnRandDupa, llEroareAdauga
	lnRandInainte = RECCOUNT_ACTIVE('rul_temp')
	llEroareAdauga = .F.
	TRY
		goObj.do_adauga_articol()
	CATCH TO loExcAdd
		llEroareAdauga = .T.
		DO ArLog WITH 'EROARE la do_adauga_articol() ' + TRANSFORM(loExcAdd.ErrorNo) + ' [' + loExcAdd.Message + ;
			'] in ' + loExcAdd.Procedure + ':' + TRANSFORM(loExcAdd.LineNo) + ' (' + loExcAdd.LineContents + ')'
	ENDTRY
	DOEVENTS FORCE
	goObj.gridart.Refresh()
	DOEVENTS FORCE
	lnRandDupa = RECCOUNT_ACTIVE('rul_temp')
	DO ArLog WITH 'do_adauga_articol(): randuri rul_temp INAINTE=' + TRANSFORM(lnRandInainte) + ' DUPA=' + TRANSFORM(lnRandDupa) + ' eroare=' + TRANSFORM(llEroareAdauga) + ' gnButon=' + TRANSFORM(gnButon)

	*-- VERIFICARE 0 (Problema 1 raportata de Marius): articolul BULDOEXCAVATOR apare in rul_temp?
	LOCAL llArticolAdaugat
	llArticolAdaugat = .F.
	SELECT rul_temp
	LOCATE FOR ALLTRIM(codmat) == 'ART-BULD-212'
	IF FOUND()
		llArticolAdaugat = .T.
		DO ArLog WITH 'PASS verificare0: BULDOEXCAVATOR gasit in rul_temp dupa do_adauga_articol() REAL - denumire=' + ALLTRIM(denumire) + ' cont=[' + ALLTRIM(NVL(cont,'')) + '] cant=' + TRANSFORM(cant) + ' pret=' + TRANSFORM(pret) + ' pret_val=' + TRANSFORM(pret_val) + ' cota_tva=' + TRANSFORM(cota_tva) + ' id_gestiune=' + TRANSFORM(NVL(id_gestiune,-1)) + ' acont=[' + ALLTRIM(NVL(acont,'')) + ']'
	ELSE
		DO ArLog WITH 'FAIL verificare0: BULDOEXCAVATOR NU a fost gasit in rul_temp dupa do_adauga_articol() REAL - articolul se pierde in fluxul real (Problema 1 confirmata)'
	ENDIF

	*-- VERIFICARE 2 (runda4c dedup): sincronizeaza() a rulat deja in do_adauga_articol cu
	*-- cant/pret 0 -> mesajul "cantitati/preturi" trebuie sa apara EXACT o data (nu per document)
	LOCAL lnAparitiiPre
	lnAparitiiPre = 0
	DO WHILE AT('nu au inca cantitati', goObj.cmesajsync, lnAparitiiPre + 1) > 0
		lnAparitiiPre = lnAparitiiPre + 1
	ENDDO
	IF lnAparitiiPre = 1
		DO ArLog WITH 'PASS verificare2 (runda4c dedup, la cant=0): mesajul "cantitati/preturi" apare O SINGURA DATA in cmesajsync'
	ELSE
		DO ArLog WITH 'FAIL verificare2 (runda4c dedup, la cant=0): mesajul apare de ' + TRANSFORM(lnAparitiiPre) + ' ori (astept 1)'
	ENDIF

	*-- captura grila DUPA do_adauga_articol, INAINTE de completarea manuala cant/pret (developer
	*-- vede exact ce lasa fluxul real: randul apare cu cant/pret 0 sau lipseste complet)
	DO HarnessStep WITH 1, 'dupa do_adauga_articol() REAL: articol_adaugat=' + TRANSFORM(llArticolAdaugat) + ' rul_temp=' + TRANSFORM(lnRandDupa) + ' randuri (eroare=' + TRANSFORM(llEroareAdauga) + ')'

	*-- daca articolul a fost adaugat, completez cant/pret (do_adauga_articol NU le seteaza -
	*-- utilizatorul le tasteaza manual in grid; simulez exact asta, ca in scenariul lui Marius:
	*-- 1 buc, 5230 lei / 1000 EUR, curs 5.23)
	IF llArticolAdaugat
		SELECT rul_temp
		LOCATE FOR ALLTRIM(codmat) == 'ART-BULD-212'
		REPLACE cant WITH 1, pret WITH 5230, pret_lei WITH 5230, pret_val WITH 1000, pretd WITH 1000
		goObj.gridart.Refresh()
		DOEVENTS FORCE
		DO ArLog WITH 'cant/pret completate manual pe randul BULDOEXCAVATOR (simuleaza tastarea in grid)'
	ENDIF

	*========================= sincronizeaza() =========================
	LOCAL llEroareSync
	llEroareSync = .F.
	TRY
		goObj.sincronizeaza()
	CATCH TO loExcSync
		llEroareSync = .T.
		DO ArLog WITH 'EROARE la sincronizeaza() ' + TRANSFORM(loExcSync.ErrorNo) + ' [' + loExcSync.Message + ;
			'] in ' + loExcSync.Procedure + ':' + TRANSFORM(loExcSync.LineNo) + ' (' + loExcSync.LineContents + ')'
	ENDTRY
	DOEVENTS FORCE
	goObj.grid1.Refresh()
	DOEVENTS FORCE

	IF !llEroareSync
		DO ArLog WITH 'PASS verificare1: sincronizeaza() a rulat fara eroare'
	ELSE
		DO ArLog WITH 'FAIL verificare1: sincronizeaza() a aruncat eroare - vezi mesajul de mai sus'
	ENDIF

	LOCAL lnLenCaption, lcCaption, lcMesajSync, lnAparitiiMesaj
	lcCaption = goObj.lblSync.Caption
	lcMesajSync = goObj.cmesajsync
	lnLenCaption = LEN(lcCaption)
	DO ArLog WITH 'cmesajsync (' + TRANSFORM(LEN(lcMesajSync)) + ' caractere) = [' + lcMesajSync + ']'
	DO ArLog WITH 'lblSync.Caption (' + TRANSFORM(lnLenCaption) + ' caractere) = [' + lcCaption + ']'

	*-- runda4c (dedup aplicat in sparge_document): mesajul "cantitati/preturi" trebuie sa apara
	*-- O SINGURA DATA in cmesajsync, nu o data per document (Problema 2, fix aplicat de Marius).
	IF !EMPTY(lcMesajSync)
		lnAparitiiMesaj = 0
		LOCAL lnPosCaut, lcTextCautat
		lcTextCautat = 'nu au inca cantitati'
		lnPosCaut = 1
		DO WHILE .T.
			lnPosCaut = AT(lcTextCautat, lcMesajSync, lnAparitiiMesaj + 1)
			IF lnPosCaut = 0
				EXIT
			ENDIF
			lnAparitiiMesaj = lnAparitiiMesaj + 1
		ENDDO
		IF lnAparitiiMesaj <= 1
			DO ArLog WITH 'PASS verificare2post (dupa completare cant/pret): mesajul "cantitati/preturi" apare de ' + TRANSFORM(lnAparitiiMesaj) + ' ori (0 = disparut legitim, niciodata >1)'
		ELSE
			DO ArLog WITH 'FAIL verificare2post (dupa completare cant/pret): mesajul "cantitati/preturi" apare de ' + TRANSFORM(lnAparitiiMesaj) + ' ori (astept <=1)'
		ENDIF
		IF lnLenCaption <= 254 AND LEFT(lcCaption, 15) == 'NESINCRONIZAT: '
			DO ArLog WITH 'PASS verificare2b: Caption trunchiat corect la ' + TRANSFORM(lnLenCaption) + ' caractere (<=254), prefix NESINCRONIZAT: OK'
		ELSE
			DO ArLog WITH 'FAIL verificare2b: Caption lungime=' + TRANSFORM(lnLenCaption) + ' (astept <=254) prefix=[' + LEFT(lcCaption,15) + ']'
		ENDIF
	ELSE
		DO ArLog WITH 'INFO verificare2: cmesajsync gol -> nota SINCRONIZATA'
	ENDIF

	*-- lSpargeSecundare implicit .T. si documentele 102/103 sunt participante (lIntern=.F.,
	*-- id_jtva_coloana nenul): dupa sincronizeaza() se sparg TOATE documentele B/S in randuri S
	*-- pe conturile articolelor (aici doar 212), nu doar documentul principal (100/RAJA).
	LOCAL llDoc100Spart, llDoc102Spart, llDoc103Spart
	SELECT introdc
	LOCATE FOR nract = 100 AND tip_rand == 'S'
	llDoc100Spart = FOUND()
	LOCATE FOR nract = 102 AND tip_rand == 'S'
	llDoc102Spart = FOUND()
	LOCATE FOR nract = 103 AND tip_rand == 'S'
	llDoc103Spart = FOUND()
	IF llDoc100Spart AND llDoc102Spart AND llDoc103Spart
		DO ArLog WITH 'PASS verificare3 (lSpargeSecundare implicit): doc 100/102/103 toate sparte (rand S gasit pe fiecare)'
	ELSE
		DO ArLog WITH 'FAIL verificare3 (lSpargeSecundare implicit): doc100Spart=' + TRANSFORM(llDoc100Spart) + ' (astept .T.) doc102Spart=' + TRANSFORM(llDoc102Spart) + ' (astept .T.) doc103Spart=' + TRANSFORM(llDoc103Spart) + ' (astept .T.)'
	ENDIF

	*========================= PAS 2: dupa sincronizeaza() =========================
	DO HarnessStep WITH 2, 'dupa sincronizeaza(): ' + IIF(EMPTY(lcMesajSync),'SINCRONIZAT','NESINCRONIZAT (' + TRANSFORM(LEN(lcMesajSync)) + ' car., mesaj repetat de ' + TRANSFORM(lnAparitiiMesaj) + ' ori)') + ' introdc=' + TRANSFORM(RECCOUNT_ACTIVE('introdc')) + ' randuri'

	*========================= do_executa() din nou + totaluri finale =========================
	LOCAL llEroareExec
	llEroareExec = .F.
	TRY
		goObj.do_executa()
	CATCH TO loExcExec
		llEroareExec = .T.
		DO ArLog WITH 'EROARE la do_executa() ' + TRANSFORM(loExcExec.ErrorNo) + ' [' + loExcExec.Message + ;
			'] in ' + loExcExec.Procedure + ':' + TRANSFORM(loExcExec.LineNo)
	ENDTRY
	goObj.recalculeaza()
	goObj.actualizeaza_banda()
	DOEVENTS FORCE

	DO ArLog WITH 'DIAG dump introdc FINAL (dupa sincronizeaza+do_executa):'
	DO DumpIntrodc
	DO ArLog WITH 'DIAG dump rul_temp FINAL:'
	SELECT rul_temp
	SCAN
		DO ArLog WITH '  denumire=' + ALLTRIM(denumire) + ' cont=' + ALLTRIM(NVL(cont,'')) + ' cant=' + TRANSFORM(cant) + ;
			' pret=' + TRANSFORM(pret) + ' pret_val=' + TRANSFORM(pret_val) + ' cota_tva=' + TRANSFORM(cota_tva)
	ENDSCAN

	DO ArLog WITH 'TOTALURI FINALE: nbaza_lei=' + TRANSFORM(goObj.nbaza_lei) + ' ntva_lei=' + TRANSFORM(goObj.ntva_lei) + ;
		' nbaza_val=' + TRANSFORM(goObj.nbaza_val) + ' ntva_val=' + TRANSFORM(goObj.ntva_val) + ;
		' ndif_lei=' + TRANSFORM(goObj.ndif_lei) + ' ndif_val=' + TRANSFORM(goObj.ndif_val)
	LOCAL lnSumaBazaDoc, lnSumaTvaDoc
	SELECT SUM(IIF(tip_rand=='B' OR tip_rand=='S',suma,0)) AS s FROM introdc INTO CURSOR crsBazaTot NOFILTER
	lnSumaBazaDoc = NVL(crsBazaTot.s,0)
	USE IN crsBazaTot
	SELECT SUM(IIF(tip_rand=='T',suma,0)) AS s FROM introdc INTO CURSOR crsTvaTot NOFILTER
	lnSumaTvaDoc = NVL(crsTvaTot.s,0)
	USE IN crsTvaTot
	DO ArLog WITH 'TOTALURI PE RANDURI introdc: suma(B+S)=' + TRANSFORM(lnSumaBazaDoc) + ' (asteptare bruta B: 5230+1000+500=6730) suma(T)=' + TRANSFORM(lnSumaTvaDoc)

	DO ArLog WITH 'GATA - scenariu executat integral'
	DO HarnessStep WITH 3, 'final: introdc=' + TRANSFORM(RECCOUNT_ACTIVE('introdc')) + ' randuri, articol_adaugat=' + TRANSFORM(llArticolAdaugat) + ' eroare_sync=' + TRANSFORM(llEroareSync) + ' eroare_exec=' + TRANSFORM(llEroareExec)
	DO HarnessDone WITH 'done'

CATCH TO loExc
	DO ArLog 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); valorile respecta exact
	*-- dialog.construieste_todlg.
	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): toate coloanele VRUL (Oracle
	*-- dev, MARIUSM_AUTO, 17.07.2026) + pret_lei/pret_val/cota_tva (aliasuri din select-ul care
	*-- creeaza rul_temp gol). Folosita atat la pregatirea INAINTE de instantiere, cat si de
	*-- dummyexecutor.oExecute (cand import_nota.Init recreeaza rul_temp gol din Oracle).
	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 VerificaPereche
	*-- verifica perechea B+T adaugata (dupa nract): sume lei/valuta, valuta pe baza
	LPARAMETERS tnNract, tnBazaLei, tnBazaVal, tcVal, tlTDvi
	LOCAL lnSel, lcMsg, llB, llT, lnBS, lnBSV, lcNV
	lnSel = SELECT()
	SELECT introdc
	LOCATE FOR nract = tnNract AND tip_rand == 'B'
	llB = FOUND()
	IF llB
		lnBS = suma
		lnBSV = suma_val
		lcNV = ALLTRIM(NVL(nume_val,''))
	ENDIF
	LOCATE FOR nract = tnNract AND tip_rand == 'T'
	*-- cu DVI, randul T e legitim re-cheiat pe documentul vamal (verificat de VerificaDvi)
	llT = FOUND() OR tlTDvi
	IF llB AND llT AND ABS(lnBS - tnBazaLei) < 0.01 AND ABS(lnBSV - tnBazaVal) < 0.01 AND ALLTRIM(NVL(lcNV,'')) == tcVal
		lcMsg = 'PASS pereche nract=' + TRANSFORM(tnNract) + ': B(suma=' + TRANSFORM(lnBS) + ' val=' + TRANSFORM(lnBSV) + ' ' + lcNV + ') + randT gasit'
	ELSE
		lcMsg = 'FAIL pereche nract=' + TRANSFORM(tnNract) + ': B?=' + TRANSFORM(llB) + ' T?=' + TRANSFORM(llT) + ' bazaLei=' + TRANSFORM(IIF(llB,lnBS,-1)) + '(astept ' + TRANSFORM(tnBazaLei) + ') bazaVal=' + TRANSFORM(IIF(llB,lnBSV,-1)) + '(astept ' + TRANSFORM(tnBazaVal) + ') val=[' + IIF(llB,lcNV,'?') + '](astept [' + tcVal + '])'
	ENDIF
	DO ArLog WITH lcMsg
	SELECT (lnSel)
ENDPROC

PROCEDURE VerificaDvi
	*-- randul T e re-cheiat pe documentul vamal (Go lnRecT - fix), nu mai ramane sub nract-ul facturii.
	LPARAMETERS tnNract, tnNractDvi, tnBazaLei, tnTvaLei
	LOCAL lnSel, lcMsg, llB, llT, lnBS, lnTS, lcTPartc, llVama, llRamasitaT
	lnSel = SELECT()
	SELECT introdc
	LOCATE FOR nract = tnNract AND tip_rand == 'B'
	llB = FOUND()
	IF llB
		lnBS = suma
	ENDIF
	LOCATE FOR nract = tnNractDvi AND tip_rand == 'T'
	llT = FOUND()
	IF llT
		lnTS = suma
		lcTPartc = ALLTRIM(NVL(partc,''))
		llVama = ('VAMAL' $ UPPER(lcTPartc))
	ENDIF
	LOCATE FOR nract = tnNract AND tip_rand == 'T'
	llRamasitaT = FOUND()
	IF llB AND llT AND !llRamasitaT AND ABS(lnBS - tnBazaLei) < 0.01 AND ABS(lnTS - tnTvaLei) < 0.01 AND llVama
		lcMsg = 'PASS DVI nract=' + TRANSFORM(tnNract) + ': B(suma=' + TRANSFORM(lnBS) + ') + T_DVI(nract=' + TRANSFORM(tnNractDvi) + ' partc=' + lcTPartc + ' suma=' + TRANSFORM(lnTS) + ')'
	ELSE
		lcMsg = 'FAIL DVI nract=' + TRANSFORM(tnNract) + ': B(suma=' + TRANSFORM(IIF(llB,lnBS,-1)) + ', astept ' + TRANSFORM(tnBazaLei) + ') T_DVI(nract=' + TRANSFORM(tnNractDvi) + ' suma=' + TRANSFORM(IIF(llT,lnTS,-1)) + ', astept ' + TRANSFORM(tnTvaLei) + ' partc=' + IIF(llT,lcTPartc,'?') + ') ramasitaT=' + TRANSFORM(llRamasitaT) + ' (astept .F.)'
	ENDIF
	DO ArLog WITH lcMsg
	SELECT (lnSel)
ENDPROC

PROCEDURE VerificaSumaGrupVama
	LPARAMETERS tnNractVama, tnSumaAsteptata, tnRanduriAsteptate
	LOCAL lnSel, lcMsg, lnSumaT, lnCount
	lnSel = SELECT()
	SELECT COUNT(*) AS n, SUM(suma) AS s FROM introdc WHERE nract = tnNractVama AND tip_rand == 'T' INTO CURSOR crsGrupVama NOFILTER
	lnCount = crsGrupVama.n
	lnSumaT = NVL(crsGrupVama.s, 0)
	USE IN crsGrupVama
	IF lnCount = tnRanduriAsteptate AND ABS(lnSumaT - tnSumaAsteptata) < 0.01
		lcMsg = 'PASS grup vama nract=' + TRANSFORM(tnNractVama) + ': ' + TRANSFORM(lnCount) + ' randuri T, suma totala=' + TRANSFORM(lnSumaT)
	ELSE
		lcMsg = 'FAIL grup vama nract=' + TRANSFORM(tnNractVama) + ': randuri=' + TRANSFORM(lnCount) + ' (astept ' + TRANSFORM(tnRanduriAsteptate) + ') suma=' + TRANSFORM(lnSumaT) + ' (astept ' + TRANSFORM(tnSumaAsteptata) + ')'
	ENDIF
	DO ArLog WITH lcMsg
	SELECT (lnSel)
ENDPROC

PROCEDURE DumpIntrodc
	LOCAL lnSel
	lnSel = SELECT()
	SELECT introdc
	SCAN
		DO ArLog WITH '  recno=' + TRANSFORM(RECNO()) + ' nract=' + TRANSFORM(nract) + ' tip_rand=' + tip_rand + ;
			' scd=' + ALLTRIM(scd) + ' suma=' + TRANSFORM(suma) + ' suma_val=' + TRANSFORM(suma_val) + ;
			' partc=[' + ALLTRIM(NVL(partc,'')) + '] doc_key=[' + ALLTRIM(doc_key) + ']'
	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 ArErr
	LPARAMETERS tnErr, tcMsg, tcProg, tnLine
	DO ArLog WITH 'ON ERROR ' + TRANSFORM(tnErr) + ' [' + tcProg + ':' + TRANSFORM(tnLine) + '] ' + tcMsg
ENDPROC

PROCEDURE ArLog
	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
		*-- update_jtva_coloane (SELECT * FROM vjtva_coloane ...): mock cu date reale.
		IF 'VJTVA_COLOANE' $ UPPER(m.tcSql)
			DO CreeazaJtvaColoaneView WITH m.tcCursor
			RETURN 1
		ENDIF
		*-- rul_temp (recreat de import_nota.Init din vrul cand e gol): structura FIDELA (vezi
		*-- CreeazaRulTemp) - altfel grila isi pierde coloanele (eroare 1925) sau campuri reale
		*-- (proc_tva etc.) lipsesc din cursor si GATHER/REPLACE pe ele arunca eroare 12.
		IF UPPER(ALLTRIM(m.tcCursor)) == 'RUL_TEMP'
			DO CreeazaRulTemp
		ELSE
			*-- ramura generica: acopera si config_analitice (assign_analitic) - cursor gol,
			*-- lnSucces>0 dar RECCOUNT=0 => lnSucces=-1 in assign_analitic => lcAnalitic='' (fara
			*-- analitic pe cont 212, comportament acceptat de team-lead pentru acest test).
			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
		*-- GetCodFiscalPartenerById: cod fiscal gol => VERIFICA_RTVAI sare peste 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
	LPARAMETERS tcCursor
	IF USED(m.tcCursor)
		USE IN (m.tcCursor)
	ENDIF
	CREATE CURSOR (m.tcCursor) (id_jtva_coloana N(14), denumire C(100), jc N(1), jv N(1), ;
		coloana_jc C(20), coloana_jv C(20), cota_tva N(6,2), id_tva N(14) NULL, afisat N(1), ;
		id_jtva_ex N(14) NULL, id_jtva_neex N(14) NULL, ordine N(8), ;
		taxcode N(6) NULL, taxcode_neexigibil N(6) NULL, taxcode_n50 N(6) NULL, ;
		taxcode_n50_tvai N(6) NULL, taxcode_n100 N(6) NULL, taxcode_regularizare N(6) NULL, taxcode_tvai N(6) NULL)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, afisat, ordine, cota_tva) VALUES (0, '', 0, 0, 1, 0, 0)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, coloana_jc, cota_tva, id_tva, afisat, ordine, taxcode) ;
		VALUES (208, 'ACH. INT. 21%', 1, 0, 'RO21B', 21, 209, 1, 1, 301104)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, coloana_jc, cota_tva, afisat, ordine, taxcode) ;
		VALUES (209, 'TVA ACH. INT. 21%', 1, 0, 'RO21T', 21, 1, 2, 301104)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, coloana_jc, cota_tva, id_tva, afisat, ordine, taxcode) ;
		VALUES (220, 'ACH. IMP. 21%', 1, 0, 'FO21B', 21, 221, 1, 3, 301204)
	INSERT INTO (m.tcCursor) (id_jtva_coloana, denumire, jc, jv, coloana_jc, cota_tva, afisat, ordine, taxcode) ;
		VALUES (221, 'TVA ACH. IMP. 21%', 1, 0, 'FO21T', 21, 1, 4, 301204)
ENDPROC
