*!* 04.11.2010
*!* marius.mutu
*!* fisa_cont_noua 
*!* fisa de cont in valuta nedefalcata pe conturi corespondente
*!* am pus suma pe fiecare document pe debit sau pe credit, in functie de tipul contului
*!* doar la documentele care au si debit si credit
*!* #2756 Conpress

*!* 08.12.2010
*!*	*!* marius.mutu
*!*	list_inventar_p
*!*	listare sold 0 (fara documente) + export XLS (goExport.export2frx)
*!*	# 2804 Conpress

*!* 08.12.2010
*!* marius.mutu
*!*	fisa_cont_noua
*!* soldul precedent se reseteaza la schimbarea lunii = precdeb-preccred, in loc de soldul cumulat de la operatia precedenta
*!* in fisegrup3.frx - totalurile lunare se calculeaza din sumele afisate in tabel, in loc de totdeb si totcred 
*!* daca totdeb si totcred nu sunt corect initializate in balanta parteneri,  nu mai dau matematic cu sumele din tabel

*!* 24.10.2011
*!* marius.mutu
*!*	raport_regvanz2007_restrans		
*!* am corectat selectia

*!* 18.11.2011
*!* marius.mutu
*!*	fisa_cont_noua
*!* fisa de cont se listeaza cu foxypreviewer

*!* 08.02.2013
*!* marius.mutu
*!* raport_regvanz2007_restrans
*!* TVA INCASARE

*!*	25.06.2014
*!*	marius.mutu
*!*	fisa_cont_noua: 
*!* se tine cont si de analiticul ales
*!*	optiune daca se calculeaza sau nu sold documente

*!*	25.01.2017
*!*	marius.mutu
*!*	raport_regvanz2007_restrans
*!*	+ RO19NB, RO19NT		


*!* 02.01.2018
*!* marius.mutu
*!* + TVADefalcatIncasariBanca
*!* Raport facturi incasate in luna curenta prin banca (conturi curente, conturi TVA) + baza/tva din registrul de vanzari
*!* contine atentionari la facturile care nu sunt incasate corect baza/tva in contul curent/TVA

*!* 15.01.2018
*!* marius.mutu
*!* TVADefalcatIncasariBanca
*!* inainte sa defalc suma incasata curent pe cote tva, diminuez sumele pe cote TVA cu suma incasata anterior

*!* 05.03.2018
*!* marius.mutu
*!* + TVADefalcatFacturiFurnizori tva defalcat pentru soldul facturilor de funizori
*!* TVADefalcatIncasariBanca + stare tva, splittva, tva incasare, inactiv

*!*	07.03.2018
*!*	marius.mutu
*!*	TVADefalcatIncasariBanca - 5121/5311 = 473 operare monetare Conpress

*!*	13.03.2018
*!*	marius.mutu
*!*	TVADefalcatIncasariPlati 
*!*	s-au inclus si incasari de tipul 5311/473 = 4111... pentru bonurile fiscale Conpress Group
*!*	se poate genera si raportul pentru plati din casa 401 ... = 5311

*!*	TVADefalcatIncasariPlati
*!*	473 doar daca este raportul de incasari casa lei 5311, nu si pentru banca lei 5121
*!* + se includ si incasari/plati la 4511
*!*	TVADefalcatFacturiFurnizori
*!*	se poate defalca o valoare diferita de sold, pentru o singura factura

*!* 22.03.2018
*!* marius.mutu
*!* TVADefalcatFacturi, TVADefalcatIncasariPlati - facturi, incasari doar de pe sucursala curenta; corectat sold baza si sold tva cu suma incasata anterior
*!*	TVADefalcatFacturi - si facturi clienti, nu doar furnizori, doar facturi cu sold

*!*	23.03.2018
*!*	marius.mutu
*!*	TVADefalcatIncasariPlati
*!*	se iau in considerare si platile din 542 la platile din casa

*!*	28.03.2018
*!*	marius.mutu
*!*	TVADefalcatIncasariPlati
*!*	se tine cont de sucursala si analiticul contului 4111.. la join incasari cu facturi

*!*	29.03.2018
*!*	marius.mutu
*!*	TVADefalcatIncasariPlati
*!*	se tine cont de contul 5126 la incasarile prin banca 5121 - nu apareau incasarile prin 5126

*!*	30.03.2018
*!*	marius.mutu
*!*	TVADefalcatIncasariPlati
*!*	corectat eroare iif

*!*	03.04.2018
*!*	marius.mutu
*!*	TVADefalcatIncasariPlati
*!*	corectat defalcare incasari/plati cu semn diferit fata de semnul facturii

*!* 11.07.2018
*!* marius.mutu
*!* fisa_cont_noua - denumirea contului

*!* 10.06.2019
*!* marius.mutu
*!* fisa_cont_noua - lucrare este acum camp distinct pentru exportul xls

*!*	24.11.2025
*!*	fisa_cont_noua - MAX(LEFT(explicatia,255)) rezolvat eroare la group by mai mare de 255 caractere

#Define ListenerPreview   1
************************************
**** procedura viz_form_rapbal *****
************************************
Procedure viz_form_rapbal
	Parameters tcRaport

	*!*	PRIVATE T, lccont1, lccont2
	*!*	STORE '' TO lccont1, lccont2
	*!*	T='.t.'
	lnLuna = gnLuna
	lnAn = gnAn
	Do make_tbalanta With .F., lnLuna, lnAn, lnLuna, lnAn
	If glEMama
		Select * From tbalanta Where Inlist(BNIVEL, 6, 7) Into Cursor tbalanta Readwrite
	Endif
	orap_bal = Createobject("frm_rap_balana")
	orap_bal.raport = tcRaport
	orap_bal.Show(1)
	Use In tbalanta

Endproc
******* sfartsit viz_form_rapbal


************************************
**** procedura viz_form_rapjurnal *****
************************************
Procedure viz_form_rapjurnal
	Parameters tcRaport

	*!*	PRIVATE poregjur, pcschema, pclcampuri
	*!*	STORE '' TO poregjur

	*!*	pcschema=['LUNA N(2),AN N(4),NRACT N(14),DATAACT D,EXPLICATIA C(150),'+]+;
	*!*		['SCD C(4),ASCD C(4),SCC C(4), ASCC C(4),SUMA N(19,gnPa),PERECHED N(14),PERECHEC N(14),'+]+;
	*!*		['NRORD C(30),PARTC C(50),PARTD C(50),FDOC C(30),'+]+;
	*!*		['EXPLICATIA4 C(100),EXPLICATIA5 C(100)',id_valuta n(5),suma_val n(16,gnPval),curs n(6,gnPcurs),nume_val c(10)]
	*!*	pclcampuri = [LUNA, AN, NRACT, DATAACT, NVL(EXPLICATIA,' '), SCD, ASCD, SCC, ASCC, SUMA, ] + ;
	*!*	    [PERECHED, PERECHEC, NVL(NRORD,' '), NVL(PARTC,' '), NVL(PARTD,' '), NVL(FDOC,' '), ] +;
	*!*		[NVL(EXPLICATIA4,' '), NVL(EXPLICATIA5,' '),NVL(id_valuta,0), suma_val, curs, nume_val ]

	*!*	pcselect=["select ] + pclcampuri + [ from ] + gcS + [.vact where 1=2"]
	*!*	*pcfiltru=[luna=]+ALLTRIM(STR(gnluna))+[ and an=]+ALLTRIM(STR(gnan))
	*!*	pcFiltru = [1=1]

	*!*	IF AT([6;],gcAcces)>0
	*!*		pcfiltru = pcfiltru + [ AND ID_UTIL=]+ALLTRIM(STR(gnidutil))+[ ]
	*!*	ENDIF

	*!*	pcorder=[dataact]
	*!*	llAfisare=.F.
	*!*	gencursor('poregjur','regjurnal',pcselect,pcfiltru,pcschema,pcorder,llAfisare)
	*!*	*!*	poregjur.ca_baza1.afisare()
	*!*	SELECT * FROM regjurnal1 INTO CURSOR regjurnal READWRITE
	*!*	SELECT regjurnal

	orap_jurnal = Createobject("frm_rap_jurnal")
	orap_jurnal.raport = tcRaport
	*!*	orap_jurnal.oObj = [poregjur]
	*!*	orap_jurnal.ctabel = [regjurnal]
	*!*	orap_jurnal.cFiltru = pcFiltru
	orap_jurnal.Show(1)
	*USE IN regjurnal

Endproc
******* sfartsit viz_form_rapjurnal


******************************************* INCEPUT:fisa_cont_noua ******************
Procedure fisa_cont_noua
	*!*	modificare v 2.0.58
	Lparameters tlInValuta
	*!*	modificare v 2.0.58	^

	*!*	modificare	v 2.0.64 - 12.09.2008
	* Fisa contului pt. toate conturile unui partener
	* adaugat in selectia lcSqlT coloana cont
	* ramuri pt. empty(cont)
	Local llConturiCorespondente, llDocumenteSold, llLucrari
	llConturiCorespondente = .T.
	llDocumenteSold = .F.
	llLucrari = .F. && defalcare dupa lucrari
	gnbuton = 2
	Private xrec, pnOptiune, plExcel
	Store 1 To pnOptiune	&&	modificare	v 2.0.125
	Store .F. To plExcel
	xrec = Createobject('custom')
	xrec.AddProperty('id_part', '0')
	xrec.AddProperty('cont', '')
	xrec.AddProperty('acont', '')
	xrec.AddProperty('dencont', '')
	xrec.AddProperty('lunai', Padl(Alltrim(Str(gnLuna)), 2, '0'))
	xrec.AddProperty('lunaf', Padl(Alltrim(Str(gnLuna)), 2, '0'))
	xrec.AddProperty('activ', '')
	xrec.AddProperty('ani', Alltrim(Str(gnAn)))
	xrec.AddProperty('anf', Alltrim(Str(gnAn)))
	xrec.AddProperty('numepart', '')
	*!*	modificare v 2.0.58
	xrec.AddProperty('in_valuta', Iif(tlInValuta, 1, 0))
	xrec.AddProperty('id_valuta', 0)
	xrec.AddProperty('nume_val', [LEI])
	xrec.AddProperty('conturicorespondente', m.llConturiCorespondente)
	xrec.AddProperty('documentesold', m.llDocumenteSold)	&& daca se calculeaza sold pe documente (valabil doar pentru conturicorespondente = .F.)
	xrec.AddProperty('lucrari', m.llLucrari)

	If xrec.in_valuta = 1
		Local loCauta
		loCauta = caut_valuta(.F., .F., .T.)
		If gnbuton = 1
			xrec.id_valuta = loCauta.id_valuta
			xrec.nume_val = loCauta.nume_val
			gnbuton = 2
			Release loCauta
		Else
			Release loCauta
			Return
		Endif
	Endif
	*!*	modificare v 2.0.58	^

	Do Form fisa_cont_noua Noshow Name loFisaCont Linked
	loFisaCont.clb_luna_intre1.tx_n1.Value = xrec.lunai + xrec.ani
	loFisaCont.clb_luna_intre1.tx_n2.Value = xrec.lunaf + xrec.anf
	loFisaCont.Show(1)

	If Used('v_nom_cont')
		Use In v_nom_cont
	Endif
	If Used('v_acont_tot')
		Use In v_acont_tot
	Endif
	If Used('v_nom_cont')
		Use In v_nom_cont
	Endif
	If gnbuton = 2
		Return
	Endif

	Local loTherm, lcTask, lnPercent, lnSeconds, x, i
	i = 0
	loTherm = Newobject("_thermometer", "_therm", "", 'FISA CONT ' + xrec.Cont)
	lcTask = "Progres"
	loTherm.Show()

	If Empty(xrec.Cont) And xrec.id_part = "0" && <toate conturile> si <toti partenerii>
		AMESSAGEBOX('Alegeti partenerul!', 0 + 48, 'Atentie')
		Return
	Endif

	llConturiCorespondente = xrec.conturicorespondente
	llDocumenteSold = xrec.documentesold
	llLucrari = xrec.lucrari

	xrec.Cont = Alltrim(Nvl(xrec.Cont, ''))
	xrec.acont = Alltrim(Nvl(xrec.acont, ''))
	lcBal = IIF(LEFT(xrec.cont,1) = '8', 'vbal_c8', IIF(LEFT(xrec.cont,1) = '9', 'vbal_c9', 'vbal'))
	lcBalana = IIF(LEFT(xrec.cont,1) = '8', 'vbalana_c8', IIF(LEFT(xrec.cont,1) = '9', 'vbalana_c9', 'vbalana'))	
	xrec.id_part = Alltrim(xrec.id_part)
	lcNrLuni1 = Transform(Val(xrec.ani) * 12 + Val(xrec.lunai))
	lcNrLuni2 = Transform(Val(xrec.anf) * 12 + Val(xrec.lunaf))

	&& explicatia = partener ( partd) ,partener coresp ,explicatia,explicatie4 si explicatie 5
	*!*	modificare	v 2.0.58
	*!*	lcPrecDeb = [pack_sesiune.suma_ron(a.precdeb,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)]
	*!*	lcPrecCred = [pack_sesiune.suma_ron(a.preccred,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)]
	*!*	lcRulDeb = [pack_sesiune.suma_ron(a.ruldeb,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)]
	*!*	lcRulCred = [pack_sesiune.suma_ron(a.rulcred,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)]
	*!*	lcTotDeb = [pack_sesiune.suma_ron(a.totdeb,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)]
	*!*	lcTotCred = [pack_sesiune.suma_ron(a.totcred,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)]
	*!*	lcSumaAct = [pack_sesiune.suma_ron(a.suma,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)]
	*!*	lcBal = [vbal]
	*!*	in valuta :
	*!*	lcPrecDeb = [a.precvaldeb]
	*!*	lcPrecCred = [a.precvalcred]
	*!*	lcRulDeb = [a.valdebit]
	*!*	lcRulCred = [a.valcredit]
	*!*	lcTotDeb = [a.totvaldeb]
	*!*	lcTotCred = [a.totvalcred]
	*!*	lcSumaAct = [a.suma_val]
	*!*	lcBal = [vbalanta_parteneri]

	*!*	modificare	v 2.0.
	*!*		am adaugat in lcSqlT  "||' ; '|| TRIM(a.explicatia4)" pe coloana "explicatia"
	If xrec.in_valuta = 1
		lcSql = [select a.cont,] + ;
			[SUM(a.precvaldeb) as precdeb,] + ;
			[SUM(a.precvalcred) as preccred,] + ;
			[SUM(a.valdebit) as ruldeb,] + ;
			[SUM(a.valcredit) as rulcred,] + ;
			[SUM(a.totvaldeb) as totdeb,] + ;
			[SUM(a.totvalcred) as totcred,] + ;
			[a.luna,a.an from ]
		If xrec.id_part <> "0"
			* Cu partener
			If Empty(xrec.Cont) && "<TOATE INREGISTRARILE>"   *!*	modificare	v 2.0.64
				* Fara Cont
				lcSqlT = [select a.an,a.luna,a.nract,a.serie_act,a.dataact,a.dataireg,a.fdoc, ] + ;
					[decode(a.id_partd,] + xrec.id_part + [,a.suma_val,0) as debit,] + ;
					[decode(a.id_partc,] + xrec.id_part + [,a.suma_val,0) as credit,] + ;
					[decode(a.id_partd,] + xrec.id_part + [,a.scd,a.scc) as cont,] + ;
					[decode(a.id_partd,] + xrec.id_part + [,a.ascd,a.ascc) as ana_cont,] + ;
					[decode(a.id_partd,] + xrec.id_part + [,a.scc,a.scd) as cont_cor,] + ;
					[decode(a.id_partd,] + xrec.id_part + [,a.ascc,a.ascd) as ana_coresp,] + ;
					[NVL(partd,'') || (case when partc is not null then ', ' || NVL(partc,' ') else '' end) || ] + ;
					[(case when a.nresp is not null then '; ' || TRIM(a.nresp) else '' end) || ] + ;
					[(case when a.nrord is not null then '; ' || TRIM(a.nrord) else '' end) || ] + ;
					[(case when a.explicatia is not null then '; ' || TRIM(a.explicatia) else '' end) || ] + ;
					[(case when a.explicatia4 is not null then '; '|| TRIM(a.explicatia4) else '' end) as explicatia, ] + ;
					iif(m.llLucrari, 'a.nrord' , ['']) + [ as lucrare] + ;
					[ from ]
			Else
				* Cu Cont
				lcSqlT = [select a.an,a.luna,a.nract,a.serie_act,a.dataact,a.dataireg,a.fdoc, ] + ;
					[ (case when a.id_partd = ] + xrec.id_part + [ and a.scd= '] + xrec.Cont + ['] + Iif(!Empty(xrec.acont), [ and a.ascd = '] + xrec.acont + ['], []) + [ then a.suma_val else 0 end) as debit, ] + ;
					[ (case when a.id_partc = ] + xrec.id_part + [ and a.scc= '] + xrec.Cont + ['] + Iif(!Empty(xrec.acont), [ and a.ascc = '] + xrec.acont + ['], []) + [ then a.suma_val else 0 end) as credit, ] + ;
					[decode(a.scd,'] + xrec.Cont + [',a.scd,a.scc) as cont,] + ;
					[decode(a.scd,'] + xrec.Cont + [',a.ascd,a.ascc) as ana_cont,] + ;
					[decode(a.scd,'] + xrec.Cont + [',a.scc,a.scd) as cont_cor,] + ;
					[decode(a.scd,'] + xrec.Cont + [',a.ascc,a.ascd) as ana_coresp,] + ;
					[NVL(partd,'') || (case when partc is not null then ', ' || NVL(partc,' ') else '' end) || ] + ;
					[(case when a.nresp is not null then '; ' || TRIM(a.nresp) else '' end) || ] + ;
					[(case when a.nrord is not null then '; ' || TRIM(a.nrord) else '' end) || ] + ;
					[(case when a.explicatia is not null then '; ' || TRIM(a.explicatia) else '' end) || ] + ;
					[(case when a.explicatia4 is not null then '; '|| TRIM(a.explicatia4) else '' end) as explicatia, ] + ;
					iif(m.llLucrari, 'a.nrord' , ['']) + [ as lucrare] + ;
					[ from ]
			Endif && Empty(xrec.Cont)
		Else
			* Fara partener
			lcSqlT = [select a.an,a.luna,a.nract,a.serie_act,a.dataact,a.dataireg,a.fdoc, ] + ;
				[case when a.scd= '] + xrec.Cont + ['] + Iif(!Empty(xrec.acont), [ and a.ascd = '] + xrec.acont + ['], []) + [ then a.suma_val else 0 end as debit,] + ;
				[case when a.scc= '] + xrec.Cont + ['] + Iif(!Empty(xrec.acont), [ and a.ascc = '] + xrec.acont + ['], []) + [ then a.suma_val else 0 end as credit,] + ;
				[decode(a.scd,'] + xrec.Cont + [',a.scd,a.scc) as cont,] + ;
				[decode(a.scd,'] + xrec.Cont + [',a.ascd,a.ascc) as ana_cont,] + ;
				[decode(a.scd,'] + xrec.Cont + [',a.scc,a.scd) as cont_cor,] + ;
				[decode(a.scd,'] + xrec.Cont + [',a.ascc,a.ascd) as ana_coresp,] + ;
				[NVL(partd,'') || (case when partc is not null then ', ' || NVL(partc,' ') else '' end) || ] + ;
				[(case when a.nresp is not null then '; ' || TRIM(a.nresp) else '' end) || ] + ;
				[(case when a.nrord is not null then '; ' || TRIM(a.nrord) else '' end) || ] + ;
				[(case when a.explicatia is not null then '; ' || TRIM(a.explicatia) else '' end) || ] + ;
				[(case when a.explicatia4 is not null then '; '|| TRIM(a.explicatia4) else '' end) as explicatia, ] + ;
				iif(m.llLucrari, 'a.nrord' , ['']) + [ as lucrare] + ;
				[ from ]

		Endif && xrec.id_part <> "0"

		*** Sold initial
		Do Case
			Case xrec.id_part = '0'
				If Empty(Nvl(xrec.acont, '')) Or Nvl(xrec.acont, '') = '-1'
					lcSql = lcSql + ;
						[vbalanta_parteneri a where a.cont = '] + xrec.Cont + [' and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
						[ group by a.cont,a.an,a.luna]

					lcSqlT = lcSqlT + ;
						[vact a where (a.scd = '] + xrec.Cont + [' or a.scc = '] + xrec.Cont + [') and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
				Else
					lcSql = lcSql + ;
						[vbalanta_parteneri a where a.cont = '] + xrec.Cont + [' and a.acont = '] + xrec.acont + ;
						[' and a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [ and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
						[ group by a.cont,a.acont,a.luna,a.an]

					lcSqlT = lcSqlT + ;
						[vact a where ((a.scd = '] + xrec.Cont + [' and a.ascd = '] + xrec.acont + [') or (a.scc = '] + ;
						xrec.Cont + [' and a.ascc = '] + xrec.acont + [')) and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
				Endif
			Case Empty(Nvl(xrec.acont, '')) Or Nvl(xrec.acont, '') = '-1'
				If Empty(xrec.Cont) && "<TOATE INREGISTRARILE>"   *!*	modificare	v 2.0.64
					lcSql = [select a.cont,] + ;
						[SUM(a.precvaldeb) as precdeb,] + ;
						[SUM(a.precvalcred) as preccred,] + ;
						[SUM(a.valdebit) as ruldeb,] + ;
						[SUM(a.valcredit) as rulcred,] + ;
						[SUM(a.precvaldeb + a.valdebit) as totdeb,] + ;
						[SUM(a.precvalcred + a.valcredit) as totcred,] + ;
						[a.luna,a.an from ] + ;
						[vbalanta_parteneri a where a.id_part = ] + xrec.id_part + [ and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
						[ group by a.cont,a.id_part,a.an,a.luna]

					lcSqlT = lcSqlT + ;
						[vact a where (a.id_partd = ] + xrec.id_part + [ or a.id_partc = ] + xrec.id_part + [) ] + ;
						[and (a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						[and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])

				Else
					lcSql = [select a.cont,] + ;
						[SUM(a.precvaldeb) as precdeb,] + ;
						[SUM(a.precvalcred) as preccred,] + ;
						[SUM(a.valdebit) as ruldeb,] + ;
						[SUM(a.valcredit) as rulcred,] + ;
						[SUM(a.precvaldeb + a.valdebit) as totdeb,] + ;
						[SUM(a.precvalcred + a.valcredit) as totcred,] + ;
						[a.luna,a.an from ] + ;
						[vbalanta_parteneri a where a.cont = '] + xrec.Cont + [' and a.id_part = ] + xrec.id_part + [ and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
						[ group by a.cont,a.id_part,a.an,a.luna]

					lcSqlT = lcSqlT + ;
						[vact a where ((a.id_partd = ] + xrec.id_part + [ AND a.scd = '] + xrec.Cont + [') or (a.id_partc = ] + xrec.id_part + [ AND a.scc = '] + xrec.Cont + [')) ] + ;
						[and (a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						[and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
				Endif
			Otherwise
				lcSql = [select a.cont,] + ;
					[SUM(a.precvaldeb) as precdeb,] + ;
					[SUM(a.precvalcred) as preccred,] + ;
					[SUM(a.valdebit) as ruldeb,] + ;
					[SUM(a.valcredit) as rulcred,] + ;
					[SUM(a.precvaldeb + a.valdebit) as totdeb,] + ;
					[SUM(a.precvalcred + a.valcredit) as totcred,] + ;
					[a.luna,a.an from ] + ;
					[vbalanta_parteneri a where a.cont = '] + xrec.Cont + [' and a.acont = '] + xrec.acont + ;
					[' and a.id_part = ] + xrec.id_part + [ and ] + ;
					[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
					Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
					[ group by a.cont,a.id_part,a.acont,a.an,a.luna]

				lcSqlT = lcSqlT + ;
					[vact a where ((a.id_partd=] + xrec.id_part + [ AND a.scd = '] + xrec.Cont + [' and a.ascd = '] + xrec.acont + [') or ] + ;
					[(a.id_partc = ] + xrec.id_part + [ AND a.scc = '] + xrec.Cont + [' and a.ascc = '] + xrec.acont + [')) AND ] + ;
					[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) and a.id_valuta = ] + Alltrim(Str(xrec.id_valuta)) + ;
					Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
		Endcase
	Else
		*** Suma in lei
		*!*	modificare	v 2.0.58	^
		lcSql = [select a.cont,] + ;
			[SUM(pack_sesiune.suma_ron(a.precdeb,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as precdeb,] + ;
			[SUM(pack_sesiune.suma_ron(a.preccred,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as preccred,] + ;
			[SUM(pack_sesiune.suma_ron(a.ruldeb,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as ruldeb,] + ;
			[SUM(pack_sesiune.suma_ron(a.rulcred,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as rulcred,] + ;
			[SUM(pack_sesiune.suma_ron(a.totdeb,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as totdeb,] + ;
			[SUM(pack_sesiune.suma_ron(a.totcred,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as totcred,] + ;
			[a.luna,a.an from ]

		If xrec.id_part <> "0"
			*!*	modificare	v 2.0.58 aveam coloana credit inca o data :
			*!*			[ decode(a.scc,'] + xrec.Cont + [',pack_sesiune.suma_ron(a.suma,a.an,a.luna,] + Alltrim(Str(gnpa)) + [),0) as credit,]
			*!*	modificare	v 2.0.58	^
			* Cu partener
			If Empty(xrec.Cont) && "<TOATE INREGISTRARILE>"   *!*	modificare	v 2.0.64
				* Fara cont
				lcSqlT = [select a.an,a.luna,a.nract,a.serie_act,a.dataact,a.dataireg,a.fdoc, ] + ;
					[ (case when a.id_partd =] + xrec.id_part + [ then ] + ;
					[ pack_sesiune.suma_ron(a.suma,a.an,a.luna,] + Alltrim(Str(gnpa)) + [) else 0 end) as debit, ] + ;
					[ (case when a.id_partc =] + xrec.id_part + [ then ] + ;
					[ pack_sesiune.suma_ron(a.suma,a.an,a.luna,] + Alltrim(Str(gnpa)) + [) else 0 end) as credit, ] + ;
					[decode(a.id_partd,'] + xrec.id_part + [',a.scd,a.scc) as cont,] + ;
					[decode(a.id_partd,'] + xrec.id_part + [',a.ascd,a.ascc) as ana_cont,] + ;
					[decode(a.id_partd,'] + xrec.id_part + [',a.scc,a.scd) as cont_cor,] + ;
					[decode(a.id_partd,'] + xrec.id_part + [',a.ascc,a.ascd) as ana_coresp,] + ;
					[NVL(partd,'') || (case when partc is not null then ', ' || NVL(partc,' ') else '' end) || ] + ;
					[(case when a.nresp is not null then '; ' || TRIM(a.nresp) else '' end) || ] + ;
					[(case when a.nrord is not null then '; ' || TRIM(a.nrord) else '' end) || ] + ;
					[(case when a.explicatia is not null then '; ' || TRIM(a.explicatia) else '' end) || ] + ;
					[(case when a.explicatia4 is not null then '; '|| TRIM(a.explicatia4) else '' end) as explicatia, ] + ;
					iif(m.llLucrari, 'a.nrord' , ['']) + [ as lucrare] + ;
					[ from ]
			Else
				* Cu cont
				lcSqlT = [select a.an,a.luna,a.nract,a.serie_act,a.dataact,a.dataireg,a.fdoc, ] + ;
					[ (case when a.id_partd =] + xrec.id_part + [ and a.scd= '] + xrec.Cont + ['] + Iif(!Empty(xrec.acont), [ and a.ascd = '] + xrec.acont + ['], []) + [ then ] + ;
					[ pack_sesiune.suma_ron(a.suma,a.an,a.luna,] + Alltrim(Str(gnpa)) + [) else 0 end) as debit, ] + ;
					[ (case when a.id_partc =] + xrec.id_part + [ and a.scc= '] + xrec.Cont + ['] + Iif(!Empty(xrec.acont), [ and a.ascc = '] + xrec.acont + ['], []) + [ then ] + ;
					[ pack_sesiune.suma_ron(a.suma,a.an,a.luna,] + Alltrim(Str(gnpa)) + [) else 0 end) as credit, ] + ;
					[decode(a.scd,'] + xrec.Cont + [',a.scd,a.scc) as cont,] + ;
					[decode(a.scd,'] + xrec.Cont + [',a.ascd,a.ascc) as ana_cont,] + ;
					[decode(a.scd,'] + xrec.Cont + [',a.scc,a.scd) as cont_cor,] + ;
					[decode(a.scd,'] + xrec.Cont + [',a.ascc,a.ascd) as ana_coresp,] + ;
					[NVL(partd,'') || (case when partc is not null then ', ' || NVL(partc,' ') else '' end) || ] + ;
					[(case when a.nresp is not null then '; ' || TRIM(a.nresp) else '' end) || ] + ;
					[(case when a.nrord is not null then '; ' || TRIM(a.nrord) else '' end) || ] + ;
					[(case when a.explicatia is not null then '; ' || TRIM(a.explicatia) else '' end) || ] + ;
					[(case when a.explicatia4 is not null then '; '|| TRIM(a.explicatia4) else '' end) as explicatia, ] + ;
					iif(m.llLucrari, 'a.nrord' , ['']) + [ as lucrare] + ;
					[ from ]
			Endif && Empty(xrec.Cont)
		Else
			* Fara partener
			lcSqlT = [select a.an,a.luna,a.nract,a.serie_act,a.dataact,a.dataireg,a.fdoc,] + ;
				[case when a.scd= '] + xrec.Cont + ['] + Iif(!Empty(xrec.acont), [ and a.ascd = '] + xrec.acont + ['], []) + [ then pack_sesiune.suma_ron(a.suma,a.an,a.luna,] + Alltrim(Str(gnpa)) + [) else 0 end as debit,] + ;
				[case when a.scc= '] + xrec.Cont + ['] + Iif(!Empty(xrec.acont), [ and a.ascc = '] + xrec.acont + ['], []) + [ then pack_sesiune.suma_ron(a.suma,a.an,a.luna,] + Alltrim(Str(gnpa)) + [) else 0 end as credit,] + ;
				[decode(a.scd,'] + xrec.Cont + [',a.scd,a.scc) as cont,] + ;
				[decode(a.scd,'] + xrec.Cont + [',a.ascd,a.ascc) as ana_cont,] + ;
				[decode(a.scd,'] + xrec.Cont + [',a.scc,a.scd) as cont_cor,] + ;
				[decode(a.scd,'] + xrec.Cont + [',a.ascc,a.ascd) as ana_coresp,] + ;
				[NVL(partd,'') || (case when partc is not null then ', ' || NVL(partc,' ') else '' end) || ] + ;
				[(case when a.nresp is not null then '; ' || TRIM(a.nresp) else '' end) || ] + ;
				[(case when a.nrord is not null then '; ' || TRIM(a.nrord) else '' end) || ] + ;
				[(case when a.explicatia is not null then '; ' || TRIM(a.explicatia) else '' end) || ] + ;
				[(case when a.explicatia4 is not null then '; '|| TRIM(a.explicatia4) else '' end) as explicatia, ] + ;
				iif(m.llLucrari, 'a.nrord' , ['']) + [ as lucrare] + ;
				[ from ]
		Endif && xrec.id_part <> "0"


		Do Case
			Case xrec.id_part = '0'
				If Empty(Nvl(xrec.acont, '')) Or Nvl(xrec.acont, '') = '-1'
					lcSql = lcSql + ;
						m.lcBal + [ a where a.cont = '] + xrec.Cont + [' and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
						[ group by a.cont,a.an,a.luna]

					lcSqlT = lcSqlT + ;
						[vact a where (a.scd = '] + xrec.Cont + [' or a.scc = '] + xrec.Cont + [') and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
				Else
					lcSql = lcSql + ;
						m.lcBalana + [ a where a.cont = '] + xrec.Cont + [' and a.acont = '] + xrec.acont + ;
						[' and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
						[ group by a.cont,a.acont,a.luna,a.an]

					lcSqlT = lcSqlT + ;
						[vact a where ((a.scd = '] + xrec.Cont + [' and a.ascd = '] + xrec.acont + [') or (a.scc = '] + ;
						xrec.Cont + [' and a.ascc = '] + xrec.acont + [')) and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
				Endif
			Case Empty(Nvl(xrec.acont, '')) Or Nvl(xrec.acont, '') = '-1'

				If Empty(xrec.Cont) && "<TOATE INREGISTRARILE>"
					lcSql = [select a.cont,] + ;
						[SUM(pack_sesiune.suma_ron(a.precdeb,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as precdeb,] + ;
						[SUM(pack_sesiune.suma_ron(a.preccred,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as preccred,] + ;
						[SUM(pack_sesiune.suma_ron(a.debit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as ruldeb,] + ;
						[SUM(pack_sesiune.suma_ron(a.credit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as rulcred,] + ;
						[SUM(pack_sesiune.suma_ron(a.precdeb + a.debit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as totdeb,] + ;
						[SUM(pack_sesiune.suma_ron(a.preccred + a.credit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as totcred,] + ;
						[a.luna,a.an from ] + ;
						[vbalanta_parteneri a where id_part = ] + xrec.id_part + [ and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
						[ group by a.cont,a.id_part,a.an,a.luna]

					lcSqlT = lcSqlT + ;
						[vact a where (a.id_partd = ] + xrec.id_part + ;
						[ or a.id_partc = ] + xrec.id_part + [) and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
				Else
					lcSql = [select a.cont,] + ;
						[SUM(pack_sesiune.suma_ron(a.precdeb,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as precdeb,] + ;
						[SUM(pack_sesiune.suma_ron(a.preccred,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as preccred,] + ;
						[SUM(pack_sesiune.suma_ron(a.debit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as ruldeb,] + ;
						[SUM(pack_sesiune.suma_ron(a.credit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as rulcred,] + ;
						[SUM(pack_sesiune.suma_ron(a.precdeb + a.debit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as totdeb,] + ;
						[SUM(pack_sesiune.suma_ron(a.preccred + a.credit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as totcred,] + ;
						[a.luna,a.an from ] + ;
						[vbalanta_parteneri a where a.cont = '] + xrec.Cont + [' and id_part = ] + xrec.id_part + [ and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
						[ group by a.cont,a.id_part,a.an,a.luna]

					lcSqlT = lcSqlT + ;
						[vact a where ((a.id_partd = ] + xrec.id_part + [ AND a.scd = '] + xrec.Cont + [')] + ;
						[ OR (a.id_partc = ] + xrec.id_part + [ and a.scc = '] + xrec.Cont + ['))] + ;&&&&&&&& 25.02.2009
						[ and ] + ;
						[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
						Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
				Endif
			Otherwise
				lcSql = [select a.cont,] + ;
					[SUM(pack_sesiune.suma_ron(a.precdeb,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as precdeb,] + ;
					[SUM(pack_sesiune.suma_ron(a.preccred,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as preccred,] + ;
					[SUM(pack_sesiune.suma_ron(a.debit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as ruldeb,] + ;
					[SUM(pack_sesiune.suma_ron(a.credit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as rulcred,] + ;
					[SUM(pack_sesiune.suma_ron(a.precdeb + a.debit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as totdeb,] + ;
					[SUM(pack_sesiune.suma_ron(a.preccred + a.credit,a.an,a.luna,] + Alltrim(Str(gnpa)) + [)) as totcred,] + ;
					[a.luna,a.an from ] + ;
					[vbalanta_parteneri a where a.cont = '] + xrec.Cont + [' and a.acont = '] + xrec.acont + ;
					[' and id_part = ] + xrec.id_part + [ and ] + ;
					[(a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
					Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
					[ group by a.cont,a.id_part,a.acont,a.an,a.luna]

				lcSqlT = lcSqlT + ;  &&&&&&&&& 25.02.09
					[vact a where ((a.id_partd=] + xrec.id_part + [ AND a.scd = '] + xrec.Cont + [' and a.ascd = '] + xrec.acont + [')] + ;
					[ or (a.id_partc = ] + xrec.id_part + [ and a.scc = '] + xrec.Cont + [' and a.ascc = '] + xrec.acont + [')) ] + ;
					[ and (a.an * 12 + a.luna between ] + lcNrLuni1 + [ and ] + lcNrLuni2 + [) ] + ;
					Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
		Endcase
		*!*	modificare	v 2.0.58
	Endif
	*!*	modificare	v 2.0.58	^

	lcCursor = [v_fisa_cont]
	lnSucces = goExecutor.oExecute(lcSqlT, lcCursor)

	If lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	lcCursor = [v_sold_fisa]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	If !Empty(xrec.Cont)
		lcCursorCont = update_plcont(xrec.Cont, gnAn)
	Else
		lcCursorCont = update_plcont('', gnAn)
	Endif


	*!* 09.08.2010
	*!*	modificare	v 2.0.61	:	am pus MAX(LEFT(explicatia,500)) in loc de 250
	If !m.llConturiCorespondente
		Select an, luna, nract, serie_act, dataact, dataireg, fdoc, Cont, ana_cont, lucrare, Max(Left(explicatia, 255)) As  explicatia, '' As cont_cor, '' As ana_coresp, ;
			Sum(debit) As debit, Sum(credit) As credit  ;
			From v_fisa_cont ;
			Group By 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
			Into Cursor v_fisa_contTemp
		Use In (Select('v_fisa_cont'))

		Select * From v_fisa_contTemp Into Cursor v_fisa_cont Readwrite
		Use In (Select('v_fisa_contTemp'))
	Endif
	*!* 09.08.2010 ^

	Select Nvl(F.Cont, v.Cont) As Cont, ;
		Iif(Empty(xrec.Cont), p.tip_sold, xrec.Activ) As tip_sold, ;
		CAST(IIF(NVL(f.debit,0)<>0, ALLTRIM(NVL(f.cont,'')) + IIF(!EMPTY(NVL(f.ana_cont,'')), '.' + ALLTRIM(f.ana_cont), ''), ALLTRIM(NVL(f.cont_cor, '')) + IIF(!EMPTY(NVL(f.ana_coresp,'')), '.' + ALLTRIM(f.ana_coresp), '')) as C(10)) as scd, ;
		CAST(IIF(NVL(f.credit,0)<>0, ALLTRIM(NVL(f.cont,'')) + IIF(!EMPTY(NVL(f.ana_cont,'')), '.' + ALLTRIM(f.ana_cont), ''), ALLTRIM(NVL(f.cont_cor, '')) + IIF(!EMPTY(NVL(f.ana_coresp,'')), '.' + ALLTRIM(f.ana_coresp), '')) as C(10)) as scc, ;
		Nvl(F.an, v.an) As an, ;
		Nvl(F.luna, v.luna) As luna, ;
		Nvl(F.nract, 00000000000000) As nract, ;
		Nvl(Ttod(F.dataact), {}) As dataact, ;
		Nvl(F.serie_act, []) As serie_act, ;
		Nvl(Ttod(F.dataireg), {}) As dataireg, ;
		Nvl(F.fdoc, Space(20)) As fdoc, ;
		Nvl(F.debit, 000000000000.0000) As debit, Nvl(F.credit, 000000000000.0000) As credit, ;
		F.ana_cont, F.cont_cor, F.ana_coresp, F.explicatia, F.lucrare, ;
		v.precdeb, v.preccred, v.ruldeb, v.rulcred, v.totdeb, v.totcred, 00000000000000.0000 As sold_op, ;
		00000000000000.0000 As sold_prec ;		&&	modificare	v 2.0.125
		From v_fisa_cont F ;
		Right Join v_sold_fisa v On F.luna = v.luna And F.an = v.an And F.Cont = v.Cont ;
		Join (lcCursorCont) p On p.Cont = v.Cont ;
		Into Cursor fisa_final Readwrite ;
		Order By v.Cont, v.an, v.luna, F.dataact, F.nract
	*!*		SELECT * FROM v_fisa_cont INTO TABLE C:\fisacont.dbf
	*!*		SELECT * FROM v_sold_fisa INTO TABLE C:\soldfisa.dbf
	*!*		SELECT * FROM fisa_final INTO TABLE C:\fisafinal.dbf

	*!* 04.11.2010
	If m.llDocumenteSold && !llConturiCorespondente
		Update fisa_final Set debit = debit - credit, credit = 0 Where tip_sold = 'A' And debit <> 0 And credit <> 0
		Update fisa_final Set credit = credit - debit, debit = 0 Where tip_sold = 'P' And debit <> 0 And credit <> 0
	Endif
	*!* 04.11.2010 ^

	If Used(lcCursorCont)
		Use In (lcCursorCont)
	Endif

	Local lnsold, lcCont, lcContPrec, lnSoldPrec	&&	modificare	v 2.0.125 : lnSoldPrec
	Store '' To lcCont, lcContPrec

	Select fisa_final
	x = Reccount()
	lnNrLuna = 0
	Scan
		lcCont = Cont
		If lcCont <> lcContPrec
			lcContPrec = lcCont
			lnsold = 0
			lnSoldPrec = 0		&&	modificare	v 2.0.125
		Endif

		If Empty(xrec.Cont)
			If an * 12 + luna = lnNrLuna
				lnsold = lnsold + Iif(xrec.Activ = 'A', 1, -1) * (debit - credit)
			Else
				lnsold = Iif(xrec.Activ = 'A', 1, -1) * ((precdeb - preccred) + (debit - credit))
				*!* 18.05.2011
				lnSoldPrec = Iif(xrec.Activ = 'A', 1, -1) * (precdeb - preccred)
				*!* 18.05.2011 ^
				lnNrLuna = an * 12 + luna
			Endif
		Else
			If an * 12 + luna = lnNrLuna
				lnsold = lnsold + Iif(tip_sold = 'P', -1, 1) * (debit - credit)
			Else
				lnsold = Iif(tip_sold = 'P', -1, 1) * ((precdeb - preccred) + (debit - credit))
				*!* 18.05.2011
				lnSoldPrec = Iif(tip_sold = 'P', -1, 1) * (precdeb - preccred)
				*!* 18.05.2011 ^
				lnNrLuna = an * 12 + luna
			Endif
		Endif
		*!*	modificare	v 2.0.125
		*!*		Replace sold_op With lnsold
		Replace sold_op With lnsold, sold_prec With lnSoldPrec
		lnSoldPrec = lnsold
		*!*	modificare	v 2.0.125	^
		i = i + 1
		lnPercent = i / x * 100
		loTherm.Update(lnPercent, lcTask)
		Select fisa_final
	Endscan

	*!*	modificare v 2.0.125
	If pnOptiune = 2
		Local lnPrecDeb, lnPrecCred, lcCont
		If Used('crstmpsold')
			Use In crstmpsold
		Endif
		Select Distinct precdeb, preccred, Cont From fisa_final Where an = Val(xrec.ani) And luna = Val(xrec.lunai) ;
			Into Cursor crstmpsold
		Select crstmpsold
		*!*			SELECT * FROM crstmpsold INTO TABLE C:\crstmpsold.dbf
		Scan
			lnPrecDeb = precdeb
			lnPrecCred = preccred
			lcCont = Cont
			Select fisa_final
			Replace sold_prec With Iif(an = Val(xrec.ani) And luna = Val(xrec.lunai), (-1) * Iif(tip_sold = 'P', lnPrecDeb - lnPrecCred, lnPrecCred - lnPrecDeb), sold_prec), ;
				precdeb With precdeb - lnPrecDeb, totdeb With totdeb - lnPrecDeb, ;
				preccred With preccred - lnPrecCred, totcred With totcred - lnPrecCred ;
				For Cont = lcCont
			Select crstmpsold
		Endscan
		Use In crstmpsold
		Release lnPrecDeb, lnPrecCred, lcCont
	Else
		Select fisa_final
		Replace sold_prec With Iif(tip_sold = 'P', - (precdeb - preccred), - (preccred - precdeb)) For an = Val(xrec.ani) And luna = Val(xrec.lunai)
	Endif
	*!*	modificare v 2.0.125	^

	*!*	Select * From fisa_final Into Table c:\fisa_final.Dbf

	loTherm.Complete()
	Release loTherm

	If Used('v_sold_fisa')
		Use In v_sold_fisa
	Endif

	If Used('v_fisa_cont')
		Use In v_fisa_cont
	Endif

	*!*	modificare	v 2.0.125
	If !m.plExcel
		*!*	modificare	v 2.0.125	^
		*!*	modificare	v 2.0.58
		*!*		pctitlu = "FISA CONTULUI " + xrec.Cont + Iif(!Empty(xrec.acont) And xrec.acont#'-1'," . "  + xrec.acont,'')
		pctitlu = "FISA CONTULUI " + ALLTRIM(xrec.Cont) + Iif(!Empty(Nvl(xrec.acont, '')) And Nvl(xrec.acont, '') # '-1', " . "  + ALLTRIM(xrec.acont), '') + [ - ] + ALLTRIM(NVL(xrec.dencont,'')) +  [ - ] + xrec.nume_val
		*!*	modificare	v 2.0.58	^
		*!* 18.11.2011
		goExport.export2frx([fisa_final], [fisegrup3], , , , , , .T.)
		*!* 18.11.2011 ^		
		plExcel = (AMESSAGEBOX('Doriti export in format xls?',4+32,_screen.Caption) = 6)
	ENDIF 
		
	If m.plExcel
		Create Cursor inregistrari_fisa(fdoc c(60) Null, dataact d Null, dataireg d Null, serie_act c(20) Null, nract N(20) Null, explicatia c(200) Null, debit N(14, gnpa) Null, credit N(14, gnpa) Null, sold_op N(14, gnpa) Null, sold_prec N(14, gnpa) Null, cont_cor c(4) Null, ana_coresp c(4) Null, lucrare C(100) null, scd c(10) NULL, scc c(10) NULL)
		Select fisa_final
		Go Top
		Scatter Name lorec Memo
		lorec.explicatia = 'SUME PRECEDENTE LUNA ' + Alltr(Str(luna)) + ' / ' + Alltr(Str(an)) + ' ' + xrec.numepart
		lorec.credit = lorec.preccred
		lorec.debit = lorec.precdeb
		*lorec.sold_op=Iif(xrec.Activ='A',lorec.precdeb-lorec.preccred,lorec.preccred-lorec.precdeb)
		lorec.sold_op = Iif(tip_sold = 'P', - (lorec.precdeb - lorec.preccred), - (lorec.preccred - lorec.precdeb))
		lorec.dataact = Null
		lorec.dataireg = Null
		lorec.fdoc = ""
		lorec.serie_act = ""
		lorec.nract = Null
		lorec.cont_cor = ""
		lorec.ana_coresp = ""
		Select inregistrari_fisa
		Insert Into inregistrari_fisa From Name lorec
		Append From Dbf('fisa_final')
		*		Insert into fisa_final(Debit,Credit,Sold_Op,Explicatia) values(Precdeb,Preccred,IIF(xrec.activ='A',precdeb-preccred,preccred-precdeb),'ABCD')
		export_xls('inregistrari_fisa')
	Endif
	If Used('fisa_final')
		Use In fisa_final
	Endif
	Release xrec
Endproc	&&	fisa_cont_noua
******************************************* SFARSIT: fisa_cont_noua *****************
******************************************* INCEPUT : notificari *****************
Procedure meniu_notificari
	Local lcSql, lcCursor, lcCursorParteneri, lcMeniu, lnOptiune, loFrm
	lcSql = [select * from notificari where sters = 0 order by ordine]
	lcCursor = [crsnotificari]
	lcCursorParteneri = [crsparteneri]
	lcCursorParteneriNot = [crspartnot]
	lcMeniu = []
	If goExecutor.oExecuta(lcSql, lcCursor)
		If Reccount(lcCursor) > 0
			Select (lcCursor)
			Scan
				lcMeniu = lcMeniu + [;] + Alltrim(titlu)
			Endscan
			lnOptiune = xmenu(Substr(lcMeniu, 2))
			If Between(lnOptiune, 1, Reccount(lcCursor))
				Select (lcCursor)
				Go (lnOptiune)
				Scatter Name loDateNotificare Memo
				lcSql = [{call parteneri_notificari(] + Alltrim(Str(loDateNotificare.id_notificare)) + [,?gnAn,?gnIdSucursala)}]
				If goExecutor.oExecuta(lcSql, lcCursorParteneri)
					Select 0 As ales, a.* From (lcCursorParteneri) a ;
						Into Cursor (lcCursorParteneriNot) Readwrite
					loFrm = Createobject("frm_notificari", loDateNotificare)

					lnCol = Fcount(lcCursorParteneriNot)
					loFrm.grid_parteneri.RecordSource = lcCursorParteneriNot
					loFrm.grid_parteneri.ColumnCount = lnCol - 4
					lnDiferenta = 0
					For i = 1 To lnCol
						Select (lcCursorParteneriNot)
						lcField = Upper(Alltrim(Field(i)))
						lcI = Alltrim(Str(i - lnDiferenta))
						Do Case
							Case lcField = [ALES]
								loFrm.grid_parteneri.Column&lcI..ControlSource = lcField
							Case Inlist(lcField, [ADRESA], [COD_FISCAL], [REG_COMERT])
								lnDiferenta = lnDiferenta + 1
							Case lcField <> [ID_PART]
								lcHeader = lcField
								With loFrm.grid_parteneri
									.Column&lcI..ControlSource = lcField
									If Val(Substr(lcField, 2)) <> 0 && sct "_" de la _401, _411, etc...
										.Column&lcI..ControlSource = [IIF(!empty(] + lcField + [),"X"," ")]
										lcHeader = Substr(lcField, 2)
									Endif
									.Column&lcI..header1.Caption = lcHeader
									.Column&lcI..header1.FontName = 'Arial Narrow'
								Endwith
							Otherwise
								lnDiferenta = lnDiferenta + 1
						Endcase
					Endfor
					loFrm.grid_parteneri.AutoFit()
					loFrm.Show(1)
					If Used(lcCursorParteneri)
						Use In (lcCursorParteneri)
					Endif
				Endif
			Endif
		Else
			AMESSAGEBOX("Nu au fost definite modele de notificari!", 48, "Atentie")
		Endif
		If Used(lcCursor)
			Use In (lcCursor)
		Endif
	Endif
	Release lcSql, lcCursor, lcMeniu, lnOptiune, loFrm
Endproc
******************************************* SFARSIT: notificari *****************
Procedure inventar_patrimoniu

	Local lcSelCont, lcSelFact, lcSelX, lnSucces
	lnSucces  = 1
	lcSelX    = [{call pack_situatii_financiare.inventar_patrimoniu(?gnAn,?gnLuna,'1=1',?gnIdSucursala)}]
	lcSelCont = [select cont, explicatie, 0 as ales from ] + gcS + [.config_cont_ireg  order by cont]
	*where cu_inregistrari = 1
	*!*		lcSelFact = [select i.fel_cont, p.cont, p.acont ,p.id_part,e.NUME,e.cod_fiscal,e.reg_comert,e.adresa,e.localitate,p.dataact,p.serie_act,p.nract,p.datascad, ] + ;
	*!*			[SUM(case i.fel_cont when 0 then p.precdeb+p.debit else p.preccred+p.credit end) as totctva, ] +;
	*!*			[SUM(case i.fel_cont when 0 then p.preccred+p.credit else p.precdeb+p.debit end) as achitat, ] +;
	*!*			[SUM((case i.fel_cont when 0 then 1 else -1 end)*(p.precdeb+p.debit-p.preccred-p.credit)) as sold ] +;
	*!*			[from ] + gcS + [.config_cont_ireg i ] +;
	*!*			[left join ] + gcS + [.ireg_parteneri p on i.cont = p.cont ] +;
	*!*			[left join ] + gcS + [.vnom_parteneri e on p.id_part = e.id_part ]


	If lnSucces > 0
		lcCursor = 'crs_parteneri'
		lnSucces = goExecutor.oExecute(lcSelX, lcCursor)
		If lnSucces < 0
			AMESSAGEBOX('Call inventar_patrimoniu' + Chr(13) + goExecutor.cEroare, 0 + 16, "Eroare")
		Endif
	Endif
	If lnSucces > 0
		lcCursor = 'crs_conturi'
		lnSucces = goExecutor.oExecute(lcSelCont, lcCursor)
		If lnSucces < 0
			AMESSAGEBOX('Select conturi cu inregistrari' + Chr(13) + goExecutor.cEroare, 0 + 16, "Eroare")
		Endif
	Endif
	If lnSucces > 0
		oinvp = Createobject('frm_part_cont')		 && in orapoarte.vcx
		Select crs_parteneri
		*REPLACE nume WITH '<TOTI PARTENERII>' FOR id_part = -1

		lnCol = Fcount()

		oinvp.grid_parteneri.RecordSource = [crs_parteneri]
		oinvp.grid_parteneri.ColumnCount = lnCol
		For i = 1 To lnCol
			lcI = Alltrim(Str(i))
			lcField = Upper(Alltrim(Field(i)))
			lcHeader = lcField
			With oinvp.grid_parteneri
				.Column&lcI..ControlSource = lcField
				If Val(Substr(lcField, 2)) <> 0 && sct "_" de la _401, _411, etc...
					.Column&lcI..Width = 25
					.Column&lcI..ControlSource = [IIF(!empty(] + lcField + [),"X"," ")]
					lcHeader = Substr(lcField, 2)
				Endif
				.Column&lcI..header1.Caption = lcHeader
				.Column&lcI..header1.FontName = 'Arial'
			Endwith
			If lcField = [ID_PART]
				oinvp.grid_parteneri.Column&lcI..Visible = .F.
			Endif
		Endfor

		With oinvp.ct_grid_search1
			*!*			.cselect = lcSelFact
			*!*			.cschema = [fel_cont n(1),cont c(4),acont c(4),id_part n(10),nume c(50),e.cod_fiscal c(20), reg_comert c(20), adresa c(250), localitate c(100), dataact d,nract n(9),datascad d,totctva n(14,2),achitat n(14,2),sold n(14,2)]
			.cTitlu_coloane = [Nume,Data act,Serie act,Nr.act,Data scad.,Valoare Document,Achitat/Facturat,Sold,Valoare Document Valuta,Achitat/Facturat Valuta,Sold Valuta,Nume Valuta,Cont,Analitic,Explicatia]
			.cNume_coloane  = [nume;dataact;serie_act;nract;datascad;totctva;achitat;sold;valtotctva;achitatval;soldval;nume_val;cont;acont;explicatia]
			.cMask          = "[1]=;[2]=;[3]=;[4]=replicate('9',12);[5]=;[6]=get_mask(12,gnPc);[7]=get_mask(12,gnPc);[8]=get_mask(12,gnPc);[9]=get_mask(12,gnPc);[10]=get_mask(12,gnPc);[11]=get_mask(12,gnPc);[12]=;[13]=;[14]=;[15]="
			*!*			.cMask          = "[1]=;[2]=;[3]=;[4]=replicate('9',12);[5]=;[6]=get_mask(12,gnPc);[7]=get_mask(12,gnPc);[8]=get_mask(12,gnPc);[9]=;[10]="
			*!*			.cFiltruOriginal = [i.cu_inregistrari = 1 and (p.precdeb+p.debit-p.preccred-p.credit) <> 0 and p.an = ?gnAn and p.luna = ?gnLuna ]
			.cFiltru = []
			*!*			.cOrder = [e.nume,p.cont,p.acont]
			*!*			.cGroup = [i.fel_cont, p.id_part,e.NUME,e.cod_fiscal,e.reg_comert,e.adresa,e.localitate,p.dataact,p.nract,p.datascad,p.cont,p.acont]
			.cTitlu = [INREGISTRARI]
			.cnumeCursor = [crs_facturi]
			.lmodparam = .T.
			*!*			.cSelect = [select pack_situatii_financiare.inventar_patrimoniu_ireg(?pcSchema, ?pnAn, ?pnLuna, ?pnId_Part, ?pnCont, ?pnAcont) from dual]
			.cSelect = [{call pack_situatii_financiare.inventar_patrimoniu_ireg(?pnAn, ?pnLuna, ?pnId_Part, ?pnCont, ?pnAcont, ?gnIdSucursala)}]
		Endwith
		oinvp.Show(1)
	Endif

	If Used('crs_parteneri')
		Use In crs_parteneri
	Endif
	If Used('crs_conturi')
		Use In crs_conturi
	Endif
	If Used('crs_facturi')
		Use In crs_facturi
	Endif
Endproc &&inventar_patrimoniu
***--------------------------------------------------------------------------------------------------------

Procedure list_inventar_p
	Parameters tcCursor, tnGroup, tlValuta, tnTipRaport
	* tnGroup = 1 : grupare dupa cont
	* tnGroup = 2 : grupare dupa analitic
	* tnGroup = 3 : grupare dupa cont, analitic
	* tnTipRaport: 1=STANDARD (text + lista facturi), 2= INAINTARE (doar text si total sold, fara facturi) + ANEXA (lista facturi pe conturi)
	
	Private pcNumeProc, pcMesaj1, pcMesaj2, pcmesaj3, pcdataora, pcGrupCont, pcSumainCont, pdData, plValuta
	Local lcVal, lnTipRaport, lcRaport, lcSelect, lcSql 

	Store '' To pcMesaj1, pcMesaj2, pcmesaj3
	plValuta = m.tlValuta
	pcSumainCont = 'suma de .................................. in contul ....... '
	pdData = {}	
	lnTipRaport = m.tnTipRaport
	
	lcSelect = SELECT()
	
	If Empty(tcCursor) Or Type('tcCursor') # 'C'
		tcCursor = 'crs_facturi'
	ENDIF

	If Empty(tnGroup) Or Type('tnGroup') # 'N'
		tnGroup = 3
	ENDIF
	Do Case
		Case tnGroup = 1
			pcGrupCont = [NVL(cont,'')]
		Case tnGroup = 2
			pcGrupCont = [NVL(acont,'')]
		Case tnGroup = 3
			pcGrupCont = [LEFT(NVL(cont,''),4)+'.'+LEFT(NVL(acont,''),4)]
		Otherwise
			pcGroupCont = []
	Endcase	
	

	lcRaport = [rap_inventar_patrimoniu]
	
	Use In (SELECT('crs_texte'))

	pcNumeProc = IIF(m.lnTipRaport = 1, [INVENTAR_PATRIMONIU], [INVENTAR_PATRIMONIU_INAINTARE])
	
	lcSql = [select id_mesaj,mesaj,numar from mesaje where procedura = ?pcNumeProc]
	lcCursor = 'crs_texte'
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)
	If lnSucces < 0
		AMESSAGEBOX('Select texte' + Chr(13) + goExecutor.cEroare, 0 + 16, "Eroare")
		Return
	Endif

	pdData = Gomonth(Date(gnAn, gnLuna, 1), 1) - 1 && ultima zi din luna

	Select crs_texte
	Locate For numar = 1
	If Found()
		pcMesaj1 = Alltrim(mesaj)
		lnidmesaj1 = id_mesaj
	Endif
	Locate For numar = 2
	If Found()
		pcMesaj2 = Alltrim(mesaj)
		lnidmesaj2 = id_mesaj
	Endif
	Locate For numar = 3
	If Found()
		pcMesaj3 = Alltrim(mesaj)
		lnidmesaj3 = id_mesaj
	Endif
	omsg = Createobject('frm_text_inventar')
*!*		omsg.Ed_tx_simplu1._edbase1.Value = pcMesaj1
	omsg.Ed_tx_simplu1.Tag = Transform(lnidmesaj1)
		
*!*		omsg.Ed_tx_simplu2._edbase1.Value = pcMesaj2
	omsg.Ed_tx_simplu2.Tag = Transform(lnidmesaj2)
	
*!*		omsg.Ed_tx_simplu3._edbase1.Value = pcmesaj3
	omsg.Ed_tx_simplu3.Tag = Transform(lnidmesaj3)
	omsg.Show()


	pcMesaj1 = Strtran(pcMesaj1, 'DD/MM/YYYY', Alltrim(Dtoc(m.pdData)),1,10,1)
	pcMesaj1 = Strtran(pcMesaj1, '<<ultima_zi_din_luna>>', Alltrim(Dtoc(m.pdData)),1,10,1)		
*!*		pcMesaj3 = Strtran(m.pcMesaj3, '<<suma_in_contul>>', m.pcSumainCont,1,10,1)		

	If gnbuton = 1
		DO CASE 
			CASE m.lnTipRaport = 1
				lcRaport = [rap_inventar_patrimoniu]
				goExport.export2frx(m.tcCursor, m.lcRaport, , , , , , .T.)		
			CASE m.lnTipRaport = 2
					 
				* Daca nu am aceeasi explicatie la toate inregistrarile, nu o arat
				* Vreau sa arat explicatia doar pentru conturile care nu au inregistrari, si explicatia este aceeasi din planul de conturi

				SELECT id_part, nume, cod_fiscal, reg_comert, email, LEFT(adresa,250) as adresa, cont, acont, nume_val, ;
					 CAST(MIN(IIF(EMPTY(NVL(nract,0)) AND EMPTY(NVL(dataact,{})),NVL(explicatia,''), '')) as C(100)) as explicatia, ;
					 SUM(totctva) as totctva, SUM(achitat) as achitat, SUM(valtotctva) as valtotctva, SUM(achitatval) as achitatval ;
					 FROM (m.tcCursor) ;
					 GROUP BY 1,2,3,4,5,6,7,8, 9 ;
					 ORDER BY nume, cod_fiscal ;
					 INTO CURSOR cInventarTemp readwrite
						
				lcRaport = [rap_inventar_patrimoniu_inaintare]
				goExport.export2frx([cInventarTemp], m.lcRaport, , , , , , .T.)			
				USE IN (SELECT('cInventarTemp'))
				
				IF (AMESSAGEBOX('Doriti sa listati anexa?',4+32,_Screen.Caption) = 6)
					lcRaport = [rap_inventar_patrimoniu_anexa]
					goExport.export2frx(m.tcCursor, m.lcRaport, , , , , , .T.)				
				ENDIF 	
		ENDCASE 		
	ENDIF && gnbuton 
	
	*!* 08.12.2010 ^	
	Release pcGrupCont
	Use In (SELECT('crs_texte'))
	SELECT (m.lcSelect)
Endproc &&list_inventar_p
***--------------------------------------------------------------------------------------------------------

************************************
* Intoarce text pentru raportul rap_inventar_patrimoniu, rap_inventar_patrimoniu_inaintare
************************************
FUNCTION GetSumainCont
	LPARAMETERS tnIdPart
	LOCAL lcSumainCont, lcSelect

	lcSelect = SELECT()

	SELECT distinct cont, nume_val  ;
		FROM crs_list ;
		WHERE id_part = m.tnIdPart ;
		ORDER BY cont ;
		INTO CURSOR cGrupContTotalTemp

	lcSumainCont = ''			
	SELECT cGrupContTotalTemp
	SCAN
		lcSumainCont = m.lcSumainCont + 'suma de ..................' + ALLTRIM(nume_val) + ' in contul ' + ALLTRIM(cont)
			
		lcSumainCont = m.lcSumainCont + IIF(RECNO()<>RECCOUNT(), ', ', '')	
	ENDSCAN 
	
	IF EMPTY(m.lcSumainCont)
		lcSumainCont = 'suma de .................................. in contul ....... '
	ENDIF 
	
	SELECT (m.lcSelect)
	RETURN m.lcSumainCont
ENDFUNC 	
************************************
	
Procedure datorii_buget
	PcTitluCol = []
	pcNumeCol = []
	lcSel = [{call pack_situatii_financiare.datorii_buget(?gnAn,?gnLuna,?gnIdSucursala)}]
	lcSchema = []
	lcCursor = 'crs_Datorii'
	lnSucces = goExecutor.oExecute(lcSel, lcCursor)

	If lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
		Return
	Endif

	Select crs_datorii
	odat = Crea('frm_datorii_buget')
	odat.Show(1)

Endproc
***--------------------------------------------------------------

** ===============================================================
** listeaza cartea mare pentru o lista de conturi
** do cartea_mare with 4111,401 in orapoarte.prg
** tcCorespondente debitoare/creditoare:D/C
** am luat initial din oracle corespondentele debitoare si creditoare - dar se pare ca trebuie doar creditoare
** nu am mai modificat selectia din oracle - am filtrat doar rezultatele in foxpro
** ===============================================================
Procedure cartea_mare
	Lparameters tcListaConturi, tcCorespondente

	If Pcount() = 0 Or Type('tcListaConturi') <> 'C' Or Empty(tcListaConturi)
		Return
	Endif


	Local i, j, lcSql, lcCursor, lnSucces, lcListaColoane, lorec, loRecTemp, nConturi, lnConturi, loHash
	Local lcField, lcColumn, lcCreateCursor, lcSumColumns, lnColumnCount, lnGrupe12, lnRest12, llUltimaPagina
	llUltimaPagina = .F.

	Private pcCont, pnAn, pnLuna, pcConturiCorespondente, pnSold, pcdataora, pcCorespondente
	Private pnTotalPagini, pnPaginaCurenta
	pnTotalPagini = 0
	pnPaginaCurenta = 0
	If Empty(tcCorespondente) Or Pcount() < 2 Or Type('tcCorespondente') <> 'C'
		pcCorespondente = 'C'
	Else
		pcCorespondente = Iif(tcCorespondente $ 'D/C', tcCorespondente, 'C')
	Endif
	Private Array c, d
	Dimension c[100], d[100]
	Store '' To c, d


	Local ox As ReportListener

	*-- Create a ReportListener
	ox = Createobject('ReportListener')
	ox.DynamicLineHeight = .F.
	ox.ListenerType = 1 && Preview

	loHash = GetHash()
	loHash.SetValue("oListener", ox)

	pcConturiCorespondente = ""
	pnSold = 0
	lnConturi = Getwordcount(tcListaConturi, ",")
	For nConturi = 1 To lnConturi

		llUltimulCont = nConturi = lnConturi
		If !llUltimulCont
			LCNOPAGEEJECT = 'NOPAGEEJECT'
		Else
			LCNOPAGEEJECT = 'TO PRINTER PROMPT'
		Endif


		pcCont = Getwordnum(tcListaConturi, nConturi, ",")
		pnAn = gnAn
		pnLuna = gnLuna

		Wait Window 'CONT: ' + pcCont Nowait

		&& sold initial la 1 ianuarie
		lcSql = [select ABS(precdeb - preccred) as sold from vbal where an = ?pnAn and luna = 1 and cont = ?pcCont] + ;
			gcCondSucursala
		lcCursor = 'cSold'
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
			Return
		Endif

		pnSold = cSold.sold
		Use In cSold

		&& cursorul crostabulat cu coloane de tipul LUNA, NUMELUNA, D701, D702, C4427
		lcSql = [{call pack_contab_rapoarte.cartea_mare(?pcCont, ?pcCorespondente, ?pnAn, ?pnLuna, ?gnIdSucursala)}]
		lcCursor = 'cCarteaMare1'
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
			Return
		Endif

		lcListaColoane = ""

		Select cCarteaMare1
		For i = 3 To Fcount()
			lcField = Upper(Field(i))
			If Inlist(lcField, 'TOTDEB', 'TOTCRED', 'SOLDDEB', 'SOLDCRED')
				Loop
			Endif
			lcListaColoane = lcListaColoane + "," + lcField
		Endfor
		lcListaColoane = "LUNA, NUMELUNA " + Iif(!Empty(lcListaColoane), lcListaColoane, "") + ", TOTDEB, TOTCRED, SOLDDEB, SOLDCRED"

		** CREEZ CARTEA MARE PENTRU CONTURILE DEBITOARE SI CARTEA MARE PENTRU CONTURILE CREDITOARE
		lnColumnCount = 0

		* listez doar corespondentele DEBITOARE sau CREDITOARE

		If Empty(lcListaColoane)
			Loop
		Endif

		lcSelect = "select " + lcListaColoane + " from cCarteaMare1 into cursor cCarteaMare nofilter"
		&lcSelect

		pcConturiCorespondente = Iif(tcCorespondente = 'D', "CREDITOARE", "DEBITOARE")

		** CREEZ UN CURSOR CU 99 DE COLOANE C01, C02, ..C30
		** COMPLETEZ ARRAY-UL D[1], C[lnFcount] cu numele coloanelor D701, D702, D703, D707

		lcCreateCursor = "CREATE CURSOR cCarteaMareTemp(numeluna c(30), luna n(2), trimestru n(1), totdeb n(20,4), totcred n(20,4), solddeb n(20,4), soldcred n(20,4)"
		lnFcount = Fcount('cCarteaMare')
		&&	cCarteaMare(luna, numeluna, D701, D702, ETC)
		lcSumColumns = ""
		Store "" To c, d

		For i = 3 To 101
			lcColumn = "c" + Padl(i - 2, 2, "0")
			If i <= lnFcount - 4 && FARA  TOTDEB, TOTCRED, SOLDDEB, SOLDCRED
				lcField = Field(i, "cCarteaMare")
				If Inlist(Left(lcField, 1), 'D', 'C')
					d[i - 2] = Substr(lcField, 2)
					lcSumColumns = lcSumColumns + ", sum(" + lcColumn + ") as " + lcColumn
					lnColumnCount = lnColumnCount + 1 && numarul de coloane cu sume
				Endif
			Endif

			lcCreateCursor = lcCreateCursor + ", " + lcColumn + " N(20,4)"
		Endfor && i = 3 TO 102

		lcSumColumns = Iif(!Empty(Alltrim(lcSumColumns)), Substr(Alltrim(lcSumColumns), 2) + ", ", "") + " SUM(totdeb) as totdeb, SUM(totcred) as totcred"

		lcCreateCursor = lcCreateCursor + ")"
		&lcCreateCursor

		Select cCarteaMare
		Scan
			Scatter Name lorec
			Select cCarteaMareTemp
			Scatter Name loRecTemp Blank

			&& cCarteaMareTemp.C01 = cCarteaMare.D701, cCarteaMareTemp.C02 = cCarteaMare.D702 ETC
			For i = 3 To lnFcount - 4
				lcField = "lorec." + Field(i, "cCarteaMare")
				If Inlist(Left(Field(i, "cCarteaMare"), 1), 'D', 'C')
					lnValue = Nvl(Evaluate(lcField), 0.00)
					lcFieldTemp = "loRecTemp.c" + Padl(i - 2, 2, "0")
					&lcFieldTemp = lnValue
				Endif
			Endfor
			loRecTemp.totdeb = Nvl(lorec.totdeb, 0)
			loRecTemp.totcred = Nvl(lorec.totcred, 0)
			loRecTemp.solddeb = Nvl(lorec.solddeb, 0)
			loRecTemp.soldcred = Nvl(lorec.soldcred, 0)
			loRecTemp.luna = lorec.luna
			loRecTemp.numeluna = lorec.numeluna
			Do Case
				Case Between(lorec.luna, 1, 3)
					loRecTemp.trimestru = 1
				Case Between(lorec.luna, 4, 6)
					loRecTemp.trimestru = 2
				Case Between(lorec.luna, 7, 9)
					loRecTemp.trimestru = 3
				Otherwise
					loRecTemp.trimestru = 4
			Endcase

			Insert Into cCarteaMareTemp From Name loRecTemp

			&& TOTALURI
			If Inlist(lorec.luna, 3, 6, 9, 12)
				lcSelectTotals = "SELECT " + lcSumColumns + " from cCarteaMareTemp where LEFT(NUMELUNA,2) # 'TR' INTO CURSOR cTotaluriTemp"
				&lcSelectTotals
				Select cTotaluriTemp
				Scatter Name loTotal


				&&			numeluna c(20), luna n(2), trimestru n(1), totdeb n(20,4), totcred, n(20,4), solddeb n(20,4), soldcred n(20,4)
				AddProperty(loTotal, "numeluna", "TR " + Iif(lorec.luna = 3, "I", Iif(lorec.luna = 6, "I+II", Iif(lorec.luna = 9, "I+II+III", "I+II+III+IV"))) )
				AddProperty(loTotal, "luna", lorec.luna)
				AddProperty(loTotal, "trimestru", loRecTemp.trimestru)
				AddProperty(loTotal, "solddeb", loRecTemp.trimestru)
				AddProperty(loTotal, "soldcred", loRecTemp.trimestru)
				Select cCarteaMareTemp
				loTotal.solddeb = Iif(pnSold + loTotal.totdeb - loTotal.totcred >= 0, Abs(pnSold + loTotal.totdeb - loTotal.totcred), 0)
				loTotal.soldcred = Iif(pnSold + loTotal.totdeb - loTotal.totcred < 0, Abs(pnSold + loTotal.totdeb - loTotal.totcred), 0)

				Insert Into cCarteaMareTemp From Name loTotal
			Endif && TOTALURI

		Endscan && cCarteaMare
		pcdataora = get_ora(2)



		lnGrupe12 = Int(lnColumnCount / 12)
		lnRest12 = Mod(lnColumnCount, 12)
		If lnRest12 > 8
			lnGrupe12 = lnGrupe12 + 1
			lnRest12 = 0
		Endif
		pnTotalPagini = lnGrupe12 + Iif(lnRest12 > 0, 1, 0)
		pnTotalPagini = Iif(pnTotalPagini > 0, pnTotalPagini, 1)

		pnPaginaCurenta = 0
		llUltimaPagina = .F.
		For i = 1 To 5
			Store '' To c
			If llUltimaPagina
				Exit
			Endif
			lnOffset = (i - 1) * 12
			lcSelect1 = ""
			lcSelect2 = ""
			For j = 1 To 12
				If i <= lnGrupe12 && AND lnOffset + j <= lnColumnCount
					If lnOffset + j <= lnColumnCount
						lcSelect1 = lcSelect1 + ", C" + Padl(lnOffset + j, 2, '0') + " as C" + Padl(j, 2, '0')
					Else
						lcSelect1 = lcSelect1 + ", 0 as C" + Padl(j, 2, '0')
					Endif
				Else
					If j <= 8 && AND lnOffset + j <= lnColumnCount
						If lnOffset + j <= lnColumnCount
							lcSelect2 = lcSelect2 + ", C" + Padl(lnOffset + j, 2, '0') + " as C" + Padl(j, 2, '0')
						Else
							lcSelect2 = lcSelect2 + ", 0 as C" + Padl(j, 2, '0')
						Endif
					Endif
				Endif
				c[j] = d[(i - 1) * 12 + j]
			Endfor


			If !Empty(lcSelect1)
				lcSelect1 = "select luna, numeluna " + lcSelect1 + " from cCarteaMareTemp into cursor cCarteaMareTempCurent"
				&lcSelect1
				Select cCarteaMareTempCurent
				With loHash
					.SetValue("cRaport", "rap_carteamare1")
					.SetValue("cCommandClauses", "NOPAGEEJECT")
				Endwith
				pnPaginaCurenta = pnPaginaCurenta + 1
				LISTAREUSERREPORT_HASH(loHash) && oproceduri_rapoarte
				* goExport.export2frx('cCarteaMareTempCurent', 'rap_carteamare1', , , , 'NOPAGEEJECT', , .T.)

				Use In (Select ('cCarteaMareTempCurent'))
			Else
				llUltimaPagina = .T.
				lcSelect2 = "select luna, numeluna " + lcSelect2 + ", totdeb,totcred,solddeb,soldcred from cCarteaMareTemp into cursor cCarteaMareTempCurent"
				&lcSelect2
				Select cCarteaMareTempCurent
				With loHash
					.SetValue("cRaport", "rap_carteamare")
					.SetValue("cCommandClauses", LCNOPAGEEJECT)
				Endwith
				pnPaginaCurenta = pnPaginaCurenta + 1
				Keyboard '{CTRL+F10}'
				LISTAREUSERREPORT_HASH(loHash) && proceduri_rapoarte
				*goExport.export2frx('cCarteaMareTempCurent', 'rap_carteamare', , , , m.LCNOPAGEEJECT, , .T.)
				Use In (Select ('cCarteaMareTempCurent'))
			Endif

		Endfor

		Use In (Select('cCarteaMareTemp'))

		Use In (Select('cCarteaMareTemp1'))
		Use In (Select('cTotaluriTemp'))
	Endfor && nConturi = 1 TO lnConturi
	*!*		SET REPORTBEHAVIOR &lcReportBehaviour
Endproc && cartea mare
***--------------------------------------------------------------
***************************************************************************
Function raport_regvanz2007_restrans
	Lparameters tnTip, tcCursor
	Private pnTipZile
	Store 0 To pnTipZile

	Select Distinct 0 As ales, fel_document, id_fdoc From (tcCursor) ;
		Order By fel_document Into Cursor crsdocumente Readwrite

	Select Distinct 0 As ales, id_part, denumire, cod_fiscal From (tcCursor) ;
		Order By denumire, cod_fiscal Into Cursor crsparteneri Readwrite

	ofrmcriterii = Createobject('frm_regvanz2007_restrans')
	ofrmcriterii.Show(1)
	If gnbuton = 1
		&&	pnTipZile
		*!*			select id_jv,an,luna,cod,dataact,cod_fiscal,nract,serie_act,id_part,id_fdoc,totctva,ro19b,ro19t,ro9b,
		*!*				ro9t,roti,cescdd1,cescdd2,wrscdd,fodd,fofdd,wrscfdd,wrn,fel_document,denumire from (tcCursor)
		Do Case
			Case gnAn * 12 + gnLuna < 2010 * 12 + 7
				If pnTipZile = 1
					*!*	cumulat pe perioada
					Select 0 As tip, Min(dataact) As dataact, cod_fiscal, Min(nract) As nract, serie_act, id_part, id_fdoc, Sum(totctva) As totctva, ;
						Sum(ro19b) As ro19b, Sum(ro19t) As ro19t, Sum(ro9b) As ro9b, Sum(ro9t) As ro9t, ;
						Sum(ro5b) As ro5b, Sum(ro5t) As ro5t, Sum(roti) As roti, ;
						Sum(cescdd1 + ceoptr) As cescdd1, Sum(cescdd2) As cescdd2, Sum(wrscdd) As wrscdd, ;
						Sum(fodd + cesvdd) As fodd, Sum(fofdd + cesvfdd) As fofdd, Sum(cesvfs) As cesvfs, ;
						Sum(wrscfdd) As wrscfdd, Sum(wrn) As wrn, fel_document, denumire From (tcCursor) ;
						Where id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						id_part In (Select id_part From crsparteneri Where ales = 1) ;
						Group By serie_act, cod_fiscal, id_part, id_fdoc, fel_document, denumire ;
						Union All ;
						Select 0 As tip, a.dataact, a.cod_fiscal, a.nract, a.serie_act, a.id_part, a.id_fdoc, a.totctva, a.ro19b, a.ro19t, ;
						a.ro9b, a.ro9t, a.ro5b, a.ro5t, ;
						a.roti, a.cescdd1 + a.ceoptr As cescdd1, a.cescdd2, a.wrscdd, ;
						a.fodd + a.cesvdd As fodd, a.fofdd + a.cesvfdd As fofdd, a.cesvfs, ;
						a.wrscfdd, a.wrn, a.fel_document, a.denumire ;
						From (tcCursor) a ;
						Where a.id_fdoc Not In (Select id_fdoc From crsdocumente Where ales = 1) ;
						Or (a.id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						    a.id_part Not In (Select id_part From crsparteneri Where ales = 1)) ;
						Order By a.dataact, a.nract Into Cursor cListRC
				Else
					*!*	cumulat in fiecare zi a perioadei
					Select 0 As tip, dataact, cod_fiscal, Min(nract) As nract, serie_act, id_part, id_fdoc, Sum(totctva) As totctva, Sum(ro19b) As ro19b, ;
						Sum(ro19t) As ro19t, Sum(ro9b) As ro9b, Sum(ro9t) As ro9t, Sum(ro5b) As ro5b, Sum(ro5t) As ro5t, ;
						Sum(roti) As roti, Sum(cescdd1 + ceoptr) As cescdd1, ;
						Sum(cescdd2) As cescdd2, Sum(wrscdd) As wrscdd, Sum(fodd + cesvdd) As fodd, Sum(fofdd + cesvfdd) As fofdd, ;
						Sum(cesvfs) As cesvfs, Sum(wrscfdd) As wrscfdd, ;
						Sum(wrn) As wrn, fel_document, denumire From (tcCursor) ;
						Where id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						id_part In (Select id_part From crsparteneri Where ales = 1) ;
						Group By serie_act, dataact, cod_fiscal, id_part, id_fdoc, fel_document, denumire ;
						Union All ;
						Select 0 As tip, a.dataact, a.cod_fiscal, a.nract, a.serie_act, a.id_part, a.id_fdoc, a.totctva, a.ro19b, a.ro19t, ;
						a.ro9b, a.ro9t, a.ro5b, a.ro5t, ;
						a.roti, a.cescdd1 + a.ceoptr As cescdd1, a.cescdd2, a.wrscdd, ;
						a.fodd + a.cesvdd As fodd, a.fofdd + a.cesvfdd As fofdd, a.cesvfs, ;
						a.wrscfdd, a.wrn, a.fel_document, a.denumire ;
						From (tcCursor) a ;
						Where a.id_fdoc Not In (Select id_fdoc From crsdocumente Where ales = 1) ;
						Or (a.id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						    a.id_part Not In (Select id_part From crsparteneri Where ales = 1)) ;
						Order By a.dataact, a.nract Into Cursor cListRC
				Endif
			Case gnAn * 12 + gnLuna < 2012 * 12 + 12
				*!* 24.10.2011
				If pnTipZile = 1
					*!*	cumulat pe perioada		
					Select 0 As tip, Min(dataact) As dataact, cod_fiscal, Min(nract) As nract, serie_act, id_part, id_fdoc, Sum(totctva) As totctva, ;
						Sum(ro24b) As ro24b, Sum(ro24t) As ro24t, Sum(ro19b) As ro19b, Sum(ro19t) As ro19t, Sum(ro9b) As ro9b, Sum(ro9t) As ro9t, ;
						Sum(ro5b) As ro5b, Sum(ro5t) As ro5t, Sum(roti) As roti, ;
						Sum(cescdd1) As cescdd1, Sum(cescdd2) As cescdd2, Sum(ceoptr) As ceoptr, Sum(wrscdd) As wrscdd, ;
						Sum(fodd) As fodd, Sum(cesvdd) As cesvdd, Sum(fofdd) As fofdd, Sum(cesvfdd) As cesvfdd, Sum(cesvfs) As cesvfs, ;
						Sum(wrscfdd) As wrscfdd, Sum(wrn) As wrn, fel_document, denumire From (tcCursor) ;
						Where id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						id_part In (Select id_part From crsparteneri Where ales = 1) ;
						Group By serie_act, cod_fiscal, id_part, id_fdoc, fel_document, denumire ;
						Union All ;
						Select 0 As tip, a.dataact, a.cod_fiscal, a.nract, a.serie_act, a.id_part, a.id_fdoc, a.totctva, a.ro24b, a.ro24t, a.ro19b, a.ro19t, ;
						a.ro9b, a.ro9t, a.ro5b, a.ro5t, ;
						a.roti, a.cescdd1, a.ceoptr, a.cescdd2, a.wrscdd, ;
						a.fodd, a.cesvdd, a.fofdd, a.cesvfdd, a.cesvfs, ;
						a.wrscfdd, a.wrn, a.fel_document, a.denumire ;
						From (tcCursor) a ;
						Where a.id_fdoc Not In (Select id_fdoc From crsdocumente Where ales = 1) ;
						Or (a.id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						    a.id_part Not In (Select id_part From crsparteneri Where ales = 1)) ;
						Order By a.dataact, a.nract Into Cursor cListRC
				Else
					*!*	cumulat in fiecare zi a perioadei
					Select 0 As tip, dataact, cod_fiscal, Min(nract) As nract, serie_act, id_part, id_fdoc, Sum(totctva) As totctva, ;
						Sum(ro24b) As ro24b, Sum(ro24t) As ro24t, Sum(ro19b) As ro19b, Sum(ro19t) As ro19t, Sum(ro9b) As ro9b, Sum(ro9t) As ro9t, ;
						Sum(ro5b) As ro5b, Sum(ro5t) As ro5t, Sum(roti) As roti, ;
						Sum(cescdd1) As cescdd1, Sum(cescdd2) As cescdd2, Sum(ceoptr) As ceoptr, Sum(wrscdd) As wrscdd, ;
						Sum(fodd) As fodd, Sum(cesvdd) As cesvdd, Sum(fofdd) As fofdd, Sum(cesvfdd) As cesvfdd, Sum(cesvfs) As cesvfs, ;
						Sum(wrscfdd) As wrscfdd, Sum(wrn) As wrn, fel_document, denumire From (tcCursor) ;
						Where id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						id_part In (Select id_part From crsparteneri Where ales = 1) ;
						Group By serie_act, dataact, cod_fiscal, id_part, id_fdoc, fel_document, denumire ;
						Union All ;
						Select 0 As tip, a.dataact, a.cod_fiscal, a.nract, a.serie_act, a.id_part, a.id_fdoc, a.totctva, a.ro24b, a.ro24t, a.ro19b, a.ro19t, ;
						a.ro9b, a.ro9t, a.ro5b, a.ro5t, ;
						a.roti, a.cescdd1, a.ceoptr, a.cescdd2, a.wrscdd, ;
						a.fodd, a.cesvdd, a.fofdd, a.cesvfdd, a.cesvfs, ;
						a.wrscfdd, a.wrn, a.fel_document, a.denumire ;
						From (tcCursor) a ;
						Where a.id_fdoc Not In (Select id_fdoc From crsdocumente Where ales = 1) ;
						Or (a.id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						    a.id_part Not In (Select id_part From crsparteneri Where ales = 1)) ;
						Order By a.dataact, a.nract Into Cursor cListRC
				Endif
			Otherwise && 01/2013 && TVA INCASARE
				If pnTipZile = 1
					*!*	cumulat pe perioada		
					Select 0 As tip, Max(COD) As COD, Max(id_fact) As id_fact, Min(dataact) As dataact, cod_fiscal, Min(nract) As nract, serie_act, id_part, id_fdoc, Sum(totctva) As totctva, ;
						tva_incasare, Sum(totftvatax) As totftvatax, Sum(tottvatax) As tottvatax, ;
						Sum(ro24b) As ro24b, Sum(ro24t) As ro24t, Sum(ro20b) As ro20b, Sum(ro20t) As ro20t, Sum(ro19b) As ro19b, Sum(ro19t) As ro19t, Sum(ro9b) As ro9b, Sum(ro9t) As ro9t, ;
						Sum(ro5b) As ro5b, Sum(ro5t) As ro5t, Sum(roti) As roti, ;
						Sum(ro24nb) As ro24nb, Sum(ro24nt) As ro24nt, Sum(ro20nb) As ro20nb, Sum(ro20nt) As ro20nt, Sum(ro19nb) As ro19nb, Sum(ro19nt) As ro19nt, Sum(ro9nb) As ro9nb, Sum(ro9nt) As ro9nt, Sum(ro5nb) As ro5nb, Sum(ro5nt) As ro5nt, ;
						Sum(cescdd1) As cescdd1, Sum(cescdd2) As cescdd2, Sum(ceoptr) As ceoptr, Sum(wrscdd) As wrscdd, ;
						Sum(fodd) As fodd, Sum(cesvdd) As cesvdd, Sum(fofdd) As fofdd, Sum(cesvfdd) As cesvfdd, Sum(cesvfs) As cesvfs, ;
						Sum(wrscfdd) As wrscfdd, Sum(wrn) As wrn, fdoc, denumire From (tcCursor) ;
						Where id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						id_part In (Select id_part From crsparteneri Where ales = 1) ;
						Group By serie_act, cod_fiscal, id_part, id_fdoc, fdoc, denumire, tva_incasare ;
						Union All ;
						Select 0 As tip, a.COD, a.id_fact, a.dataact, a.cod_fiscal, a.nract, a.serie_act, a.id_part, a.id_fdoc, a.totctva, ;
						a.tva_incasare, a.totftvatax, a.tottvatax, ;
						a.ro24b, a.ro24t, a.ro20b, a.ro20t, a.ro19b, a.ro19t, ;
						a.ro9b, a.ro9t, a.ro5b, a.ro5t, ;
						a.ro24nb, a.ro24nt, a.ro20nb, a.ro20nt, a.ro19nb, a.ro19nt, a.ro9nb, a.ro9nt, a.ro5nb, a.ro5nt, ;
						a.roti, a.cescdd1, a.ceoptr, a.cescdd2, a.wrscdd, ;
						a.fodd, a.cesvdd, a.fofdd, a.cesvfdd, a.cesvfs, ;
						a.wrscfdd, a.wrn, a.fdoc, a.denumire ;
						From (tcCursor) a ;
						Where a.id_fdoc Not In (Select id_fdoc From crsdocumente Where ales = 1) ;
						Or (a.id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						    a.id_part Not In (Select id_part From crsparteneri Where ales = 1)) ;
						Order By a.dataact, a.nract Into Cursor cListRC
				Else
					*!*	cumulat in fiecare zi a perioadei
					Select 0 As tip, Max(COD) As COD, Max(id_fact) As id_fact, dataact, cod_fiscal, Min(nract) As nract, serie_act, id_part, id_fdoc, Sum(totctva) As totctva, ;
						tva_incasare, Sum(totftvatax) As totftvatax, Sum(tottvatax) As tottvatax, ;
						Sum(ro24b) As ro24b, Sum(ro24t) As ro24t, Sum(ro20b) As ro20b, Sum(ro20t) As ro20t, Sum(ro19b) As ro19b, Sum(ro19t) As ro19t, Sum(ro9b) As ro9b, Sum(ro9t) As ro9t, ;
						Sum(ro5b) As ro5b, Sum(ro5t) As ro5t, Sum(roti) As roti, ;
						Sum(ro24nb) As ro24nb, Sum(ro24nt) As ro24nt, Sum(ro20nb) As ro20nb, Sum(ro20nt) As ro20nt, Sum(ro19nb) As ro19nb, Sum(ro19nt) As ro19nt, Sum(ro9nb) As ro9nb, Sum(ro9nt) As ro9nt, Sum(ro5nb) As ro5nb, Sum(ro5nt) As ro5nt, ;
						Sum(cescdd1) As cescdd1, Sum(cescdd2) As cescdd2, Sum(ceoptr) As ceoptr, Sum(wrscdd) As wrscdd, ;
						Sum(fodd) As fodd, Sum(cesvdd) As cesvdd, Sum(fofdd) As fofdd, Sum(cesvfdd) As cesvfdd, Sum(cesvfs) As cesvfs, ;
						Sum(wrscfdd) As wrscfdd, Sum(wrn) As wrn, fdoc, denumire From (tcCursor) ;
						Where id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						id_part In (Select id_part From crsparteneri Where ales = 1) ;
						Group By serie_act, dataact, cod_fiscal, id_part, id_fdoc, fdoc, denumire, tva_incasare ;
						Union All ;
						Select 0 As tip, a.COD, a.id_fact, a.dataact, a.cod_fiscal, a.nract, a.serie_act, a.id_part, a.id_fdoc, a.totctva, ;
						a.tva_incasare, a.totftvatax, a.tottvatax, ;
						a.ro24b, a.ro24t, a.ro20b, a.ro20t, a.ro19b, a.ro19t, a.ro9b, a.ro9t, a.ro5b, a.ro5t, ;
						a.ro24nb, a.ro24nt, a.ro20nb, a.ro20nt, a.ro19nb, a.ro19nt, a.ro9nb, a.ro9nt, a.ro5nb, a.ro5nt, ;
						a.roti, a.cescdd1, a.ceoptr, a.cescdd2, a.wrscdd, ;
						a.fodd, a.cesvdd, a.fofdd, a.cesvfdd, a.cesvfs, ;
						a.wrscfdd, a.wrn, a.fdoc, a.denumire ;
						From (tcCursor) a ;
						Where a.id_fdoc Not In (Select id_fdoc From crsdocumente Where ales = 1) ;
						Or (a.id_fdoc In (Select id_fdoc From crsdocumente Where ales = 1) And ;
						    a.id_part Not In (Select id_part From crsparteneri Where ales = 1)) ;
						Order By a.dataact, a.nract Into Cursor cListRC
				Endif

		Endcase
		lnTip = tnTip
	Else
		lnTip = 0
	Endif

	If Used('crsdocumente')
		Use In crsdocumente
	Endif

	If Used('crsparteneri')
		Use In crsparteneri
	Endif

	Release ofrmcriterii, pnTipZile
	Return lnTip
Endfunc	&&	raport_regvanz2007_restrans
***************************************************************************


Procedure registrul_inventar
	Local lnAn, lnLuna, lcSelect
	lcSelect = SELECT()
	lnLuna = m.gnLuna
	lnAn = m.gnAn
	Do make_tbalanta With .F., m.lnLuna, m.lnAn, m.lnLuna, m.lnAn
	If m.glEMama
		Select * From tbalanta Where Inlist(BNIVEL, 6, 7) Into Cursor tbalanta Readwrite
	Endif

	SELECT tbalanta
	SET FILTER TO !EMPTY(NVL(cont, '')) AND EMPTY(NVL(acont, ''))

	goExport.export2frx("tbalanta", "registru_inventar.frx", , , , , , .T.)

	Use In (Select('tbalanta'))
	SELECT (m.lcSelect)
Endproc && registrul_inventar

*****************
*** Raport facturi incasate in luna curenta prin banca (conturi curente, conturi TVA) + baza/tva din registrul de vanzari
*** contine atentionari la facturile care nu sunt incasate corect baza/tva in contul curent/TVA
*****************
Procedure TVADefalcatIncasariPlati
	LPARAMETERS tcCont, tcTip
	* tcCont: cont banca sau casa 5121/5311
	* tcTip: INCASARI/PLATI ex: TVADefalcatIncasariPlati('5311', 'PLATI') = plati prin casa

	Private  pdDataI, pdDataF
	Local lofrm As "frm_perioada_zzllaaaa"
	Local laCoteTVA[1], lcField, lcIndex, lcItem, lcSelect, lcSelectAct, lcSelectBanca, lcSelectBanca2
	Local lcSir, lcSirActcv, lcSirBaza, lcSirBazaTVA, lcSirNeimpozabil, lcSirTVA, lcSirTVAAltele
	Local lcWhereA, lcXlsFile, lcnume, ldDataCurenta, llSucces, lnBaza, lnBazaIncasat, lnCotaTVA
	Local lnField, lnIncasat, lnItem, lnItems, lnTVA, lnTVAIncasat, loFact, loFactTVACE, loFactTVAFO
	Local loFactTVARO, loFactTVAWR, lcTipJurnal
	LOCAL lcCont, llFound, lnIdFact, llIncasari, lcTip, lnSign

	lcCont = IIF(TYPE('tcCont') = 'C' and !EMPTY(m.tcCont), m.tcCont, '5121') && incasari banca default
	lcTip = IIF(TYPE('tcTip') = 'C' and !EMPTY(m.tcTip), UPPER(ALLTRIM(m.tcTip)), 'INCASARI')
	lcTip = IIF(INLIST(m.lcTip, "INCASARI", "PLATI"), m.lcTip, "INCASARI")
	llIncasari = (m.lcTip = "INCASARI")
	lcTipJurnal = IIF(m.llIncasari, "JV", "JC")

	ldDataCurenta = Date()
	If m.gnAn * 12 + m.gnLuna = Year(m.ldDataCurenta) * 12 + Month(m.ldDataCurenta)
		pdDataI = m.ldDataCurenta
	Else
		pdDataI = DATE(m.gnAn, m.gnLuna, 1)
	Endif
	pdDataF = m.pdDataI

	***	alegere perioada
	lofrm = Createobject("frm_perioada_zzllaaaa")
	lofrm.Show(1)
	If m.gnButon = 2 Or EMPTY(NVL(m.pdDataI, {})) Or EMPTY(NVL(m.pdDataF, {}))
		Return
	Endif


IF m.llIncasari
	TEXT TO lcWhereA NOSHOW TEXTMERGE
a.an = <<m.gnAn>> and a.luna = <<m.gnLuna>> and a.sters = 0 and a.dataact between to_date('<<DTOS(m.pdDataI)>>', 'yyyymmdd') and to_date('<<DTOS(m.pdDataF)>>', 'yyyymmdd') And 
a.scd <<IIF(m.lcCont = '5311', [in ('5311','473')], IIF(m.lcCont = '5121', [in ('5121','5126')], IIF(m.lcCont = '5124', [in ('5124','5127')], [= '] + m.lcCont + ['])))>> And a.scc in ('411', '4111', '418', '461', '4118', '4511') And NVL(pd.denumire, 'X') not like '%COMPENS%' <<Strtran(m.gcCondSucursala, [id_sucursala], [a.id_sucursala])>>
	ENDTEXT 
ELSE
	TEXT TO lcWhereA NOSHOW TEXTMERGE
a.an = <<m.gnAn>> and a.luna = <<m.gnLuna>> and a.sters = 0 and a.dataact between to_date('<<DTOS(m.pdDataI)>>', 'yyyymmdd') and to_date('<<DTOS(m.pdDataF)>>', 'yyyymmdd') And 
a.scc <<IIF(m.lcCont = '5311', [in ('5311','542')], IIF(m.lcCont = '5121', [in ('5121','5126')], IIF(m.lcCont = '5124', [in ('5124','5127')], [= '] + m.lcCont + ['])))>> And a.scd in ('401', '404', '408', '462', '473', '4511') And NVL(pc.denumire, 'X') not like '%COMPENS%' <<Strtran(m.gcCondSucursala, [id_sucursala], [a.id_sucursala])>>
	ENDTEXT 
ENDIF 	

	*** Selectez incasarile/platile prin/din banca/casa din Registrul Jurnal
IF m.llIncasari
	TEXT TO lcSelectAct NOSHOW TEXTMERGE
Select a.an, a.luna, a.dataact, a.id_partc as id_part, pc.denumire As partener, pc.cod_fiscal, id_factc as id_fact, perechec As nrfact, a.id_partd as id_banca, pd.denumire As banca, stringagg(a.scd) as contc, a.scc As cont, a.id_sucursala, SUM(a.suma) as suma 
	From act a left join nom_parteneri pd on a.id_partd = pd.id_part
	left join nom_parteneri pc on a.id_partc = pc.id_part
	Where <<m.lcWhereA>>
	group by a.an, a.luna, a.dataact, a.id_partc, pc.denumire, pc.cod_fiscal, id_factc, perechec, a.id_partd, pd.denumire, a.scc, a.id_sucursala
	ENDTEXT
ELSE
	TEXT TO lcSelectAct NOSHOW TEXTMERGE
Select a.an, a.luna, a.dataact, a.id_partd as id_part, pd.denumire As partener, pd.cod_fiscal, id_factd as id_fact, pereched As nrfact, a.id_partc as id_banca, pc.denumire As banca, stringagg(a.scc) as contc, a.scd As cont, a.id_sucursala, SUM(a.suma) as suma
	From act a left join nom_parteneri pc on a.id_partc = pc.id_part
	left join nom_parteneri pd on a.id_partd = pd.id_part
	Where <<m.lcWhereA>>
	group by a.an, a.luna, a.dataact, a.id_partd, pd.denumire, pd.cod_fiscal, id_factd, pereched, a.id_partc, pc.denumire, a.scd, a.id_sucursala
	ENDTEXT
ENDIF 	
	llSucces = goExecutor.oExecuta(m.lcSelectAct, "cIncasariBanca")

	IF !m.llSucces
		RETURN
	ENDIF

	If RECCOUNT('cIncasariBanca') = 0
		AMESSAGEBOX('Nu exista inregistrari pentru perioada aleasa!', 0 + 48, _Screen.Caption)
		Use In (Select('cIncasariBanca'))
		Return
	ENDIF

	Select Distinct BANCA, BANCA As cbanca, Iif('TVA' $ BANCA, 2, 1) As ordine From cIncasariBanca Order By 3, 2 Into Cursor cbanca Readwrite
	Update cbanca Set cbanca = Strtran(Strtran(Alltrim(cbanca), ' ', '_'), '-', '_')
	Update cbanca Set cbanca = 'contx_' + Strtran(Alltrim(cbanca), '__', '_')

	lcSelectBanca = ''
	lcSelectBanca2 = ''
	Select cbanca
	Scan
		lcSelectBanca = lcSelectBanca + [, SUM(CASE banca WHEN '] + Alltrim(BANCA) + [' THEN suma ELSE 0.00 END) as "] + LEFT(Alltrim(cbanca),30) + ["]
		lcSelectBanca2 = lcSelectBanca2 + [, "] + LEFT(Alltrim(cbanca),30) + ["]
	Endscan

	*** Selectez datele facturilor din Ireg_parteneri (nract, dataact, serie_act, total_fact, total_achitat)
	TEXT TO lcSelect NOSHOW textmerge
WITH actsel as (SELECT an, luna, dataact, id_part, partener, cod_fiscal, id_fact, nrfact, SUBSTR(stringagg(contc),1, 100) as contc, cont, id_sucursala,
    SUM(suma) as total_cont_incasat, 
    SUM(CASE when banca like '%TVA%' then 0.00 else suma end) as cont_curent, 
    SUM(CASE when banca like '%TVA%' then suma else 0.00 end) as cont_tva
    <<lcSelectBanca>>
from (<<m.lcSelectAct>>)
group by an, luna, dataact,id_part,partener,cod_fiscal,id_fact,nrfact, cont, id_sucursala)
select b.dataact, b.id_part, b.partener, b.cod_fiscal, b.id_fact, b.nrfact, b.contc, b.cont, ip.dataact as datafact, ip.serie_act as seriefact, b.id_sucursala,
	<<IIF(m.llIncasari, '(ip.precdeb+ip.debit)', '(ip.preccred+ip.credit)')>> as totalfact, 
	<<IIF(m.llIncasari, '(ip.preccred+ip.credit)', '(ip.precdeb+ip.debit)')>> as totalachitat, 
	<<IIF(m.llIncasari, '(ip.precdeb+ip.debit-ip.preccred-ip.credit)','(ip.preccred+ip.credit-ip.precdeb-ip.debit)')>> as totalsold,
	CAST(0 as Number(20,4)) as total_baza_si_tva, CAST(0 as Number(20,4)) as baza, CAST(0 as Number(20,4)) as tva, 
	CAST(0 as Number(20,4)) as neimpozabil, CAST(0 as Number(20,4)) as alte_tva,
	CAST(0 as Number(20,4)) as baza_incasat, CAST(0 as Number(20,4)) as tva_incasat, 
	CAST(0 as Number(20,4)) as difbaza, CAST(0 as Number(20,4)) as diftva,
	b.total_cont_incasat, b.cont_curent, b.cont_tva
	<<m.lcSelectBanca2>>
from actsel b 
left join (select ip1.dataact, ip1.serie_act, ip1.cont, ip1.id_fact, ip1.an, ip1.luna, ip1.id_sucursala, 
       sum(ip1.precdeb) as precdeb, sum(ip1.preccred) as preccred,  sum(ip1.debit) as debit,  sum(ip1.credit) as credit 
       from ireg_parteneri ip1 join actsel b1
           on b1.cont = ip1.cont
   			and b1.id_fact = ip1.id_fact
   			and b1.an = ip1.an
   			and b1.luna = ip1.luna
   			and NVL(b1.id_sucursala, 0) = NVL(ip1.id_sucursala, 0)
   group by ip1.dataact, ip1.serie_act, ip1.cont, ip1.id_fact, ip1.an, ip1.luna, ip1.id_sucursala) ip
on b.cont = ip.cont and b.id_fact = ip.id_fact and b.an = ip.an and b.luna = ip.luna and NVL(b.id_sucursala, 0) = NVL(ip.id_sucursala, 0)
order by b.dataact, b.partener, b.nrfact, b.cont   	
	ENDTEXT 

	llSucces = goExecutor.oExecuta(m.lcSelect, "cIncasariBanca2")

	Use In (Select('cIncasariBanca'))

	IF !m.llSucces
		RETURN
	ENDIF

	*** Selectez prima aparitie a facturilor din Jurnalul de Vanzari, pentru a lua sume pe cote TVA
*!*		SELECT distinct a.an,
*!*	                                       a.luna,
*!*	                                       a.dataact,
*!*	                                       a.id_partc as id_part,
*!*	                                       id_factc   as id_fact,
*!*	                                       perechec   As nrfact,
*!*	                                       scc        As cont
*!*	                         from act a left join nom_parteneri pd on a.id_partd  = pd.id_part
*!*	                        where <<m.lcWhereA>>

TEXT TO lcSelect TEXTMERGE NOSHOW
select *
  from (select j.*,
               rank() over(partition by j.nract, j.serie_act, j.dataact, j.id_part order by j.an, j.luna) as rn
          from <<IIF(m.llIncasari, 'jv2007', 'jc2007')>> j
          join (select distinct ip.id_part,
                      ip.id_fact,
                      ip.nract,
                      ip.cont,
                      ip.dataact,
                      ip.serie_act,
                      ip.id_sucursala
                 from (<<m.lcSelectAct>>) b
                 join ireg_parteneri ip
                   on b.cont = ip.cont
                  and b.id_fact = ip.id_fact
                  and b.an = ip.an
                  and b.luna = ip.luna
                  and NVL(b.id_sucursala,0) = NVL(ip.id_sucursala,0)) f
           on j.id_part = f.id_part
           and j.dataact = f.dataact
           and nvl(j.serie_act, 'xxx') = nvl(f.serie_act, 'xxx')
           and j.nract = f.nract
           and nvl(j.id_fact,f.id_fact) = f.id_fact
           and NVL(j.id_sucursala,0) = NVL(f.id_sucursala,0))
 where rn = 1
 ENDTEXT 

	llSucces = goExecutor.oExecuta(m.lcSelect, "cJurnalTVATemp")

	IF !m.llSucces
		USE IN (SELECT('cIncasariBanca2'))
		RETURN
	ENDIF

	REPLACE ALL serie_act WITH '' FOR ISNULL(serie_act) IN cJurnalTVATemp
	REPLACE ALL seriefact WITH '' FOR ISNULL(seriefact) IN cIncasariBanca2
	
	Select cJurnalTVATemp
	Index On Padr(id_part, 20, ' ') + Dtos(dataact) + Padr(serie_act, 10, ' ') + Padl(nract, 14, '0') + PADL(NVL(id_sucursala,0), 10, '0') Tag ordine
	INDEX on id_fact TAG id_fact
	

	lcSir = ''
	lcSirBaza = '0'
	lcSirTVA = '0'
	lcSirNeimpozabil = '0'
	lcSirTVAAltele = '0'
	lcSirBazaTVA = '0'
	For lnField = 1 To Fcount()
		lcField = UPPER(Field(m.lnField))
		If Inlist(Left(lcField, 2), 'RO', 'CE', 'WR', 'FO')
			lcSir = lcSir + ',j.' + m.lcField

			Do Case
				Case Left(m.lcField, 2) = 'RO' And Right(m.lcField, 1) = 'B'
					lcSirBaza =  m.lcField + '+' + m.lcSirBaza
				Case Left(m.lcField, 2) = 'RO' And Right(m.lcField, 1) = 'T'
					lcSirTVA = m.lcField + '+' + m.lcSirTVA
				Case Left(m.lcField, 2) = 'RO'
					lcSirNeimpozabil = m.lcField + '+' + m.lcSirNeimpozabil
				Otherwise
					lcSirTVAAltele = m.lcField + '+' + m.lcSirTVAAltele
			Endcase
			lcSirBazaTVA = m.lcField + '+' + m.lcSirBazaTVA
		Endif && Inlist(Left(lcField, 2), 'RO', 'CE', 'WR', 'FO')
	Endfor && lnField

	If Used('cJurnalTVATemp')
		Select cIncasariBanca2
		lcSirActcv = ''
		For lnField = 1 To Fcount()
			lcField = Field(m.lnField)
			lcSirActcv = lcSirActcv + ',a.' + m.lcField
		Endfor
		lcSirActcv = Substr(m.lcSirActcv, 2)

		lcSelect = [SELECT ] + m.lcSirActcv + ;
			[, SPACE(2) as platitortva, SPACE(2) as splittva, SPACE(2) as tvaincasare, SPACE(2) as inactiv ] + ;
			lcSir + ;
			[ FROM cIncasariBanca2 a, cJurnalTVATemp j where .F. INTO CURSOR cFacturiTVA READWRITE]
		&lcSelect

		Select cIncasariBanca2
		Scan
			Scatter Name loFact
			lcIndex = Padr(id_part, 20, ' ') + Dtos(datafact) + Padr(seriefact, 10, ' ') + Padl(nrfact, 14, '0') + PADL(NVL(id_sucursala,0), 10, '0')
			lnIdFact = id_fact
			Select cFacturiTVA
			Append Blank
			Gather Name loFact

			llFound = .F.
			Select cJurnalTVATemp
			If !Seek(m.lnIdFact,"cJurnalTVATemp","id_fact")
				If Seek(m.lcIndex,"cJurnalTVATemp","ordine")
					llFound = .T.
				ENDIF 
			ELSE
				llFound = .T.	
			ENDIF 	
			IF m.llFound
				Scatter Name loFactTVARO Fields Like 'RO*'
				Scatter Name loFactTVACE Fields Like 'CE*'
				Scatter Name loFactTVAWR Fields Like 'WR*'
				Scatter Name loFactTVAFO Fields Like 'FO*'

				Select cFacturiTVA
				Gather Name loFactTVARO
				Gather Name loFactTVACE
				Gather Name loFactTVAWR
				Gather Name loFactTVAFO

				Replace baza With Evaluate(m.lcSirBaza), TVA With Evaluate(m.lcSirTVA), ;
					neimpozabil With Evaluate(m.lcSirNeimpozabil), alte_tva With Evaluate(m.lcSirTVAAltele), ;
					total_baza_si_tva With Evaluate(m.lcSirBazaTVA)

				*** Defalcare suma incasata pe cote TVA
				lnIncasat = total_cont_incasat
			    lnIncasatAnterior = loFact.totalachitat - m.lnIncasat
			    lnSign = SIGN(loFact.totalfact)
			
				Create Cursor cCoteTVAFactura(cota N(5, 2), baza N(20, 4), TVA N(20, 4))
				lnItems = Amembers(laCoteTVA, loFactTVARO)
				For lnItem = 1 To m.lnItems
					lcItem = Upper(laCoteTVA[m.lnItem]) && RO19B, RO19T, RO19NB, RO19NT
					lnBaza = 0
					lnTVA = 0

					If Right(m.lcItem, 1) = 'T'
						lnTVA = EVALUATE('loFactTVARO.' + m.lcItem)
					ELSE
						lnBaza = EVALUATE('loFactTVARO.' + m.lcItem)
					Endif

					lnCotaTVA = Val(Strtran(m.lcItem, 'RO', ''))
					Select cCoteTVAFactura
					Locate For cota = m.lnCotaTVA
					If Found()
						REPLACE baza WITH baza + m.lnBaza, TVA WITH TVA + m.lnTVA
					Else
						Insert Into cCoteTVAFactura(cota, baza, TVA) Values (m.lnCotaTVA, m.lnBaza, m.lnTVA)
					Endif
				Endfor

				Select cota, Sum(baza) As baza, Sum(TVA) As TVA, Cast(0 As N(20, 4)) As bazaincasat, Cast(0 As N(20, 4)) As tvaincasat ;
					From cCoteTVAFactura ;
					Group By cota ;
					Order By cota Desc ;
					Into Cursor cCoteTVAFactura2 Readwrite

				Use In (Select('cCoteTVAFactura'))

				*** Actualizez suma pe cote TVA conform sumei incasate anterior
				IF m.lnIncasatAnterior <> 0
					Select cCoteTVAFactura2
					Scan			
						If (baza <> 0 OR tva <> 0) AND (ABS(m.lnIncasatAnterior) < ABS(baza + TVA) OR SIGN(m.lnIncasatAnterior) <> SIGN(baza+tva))
							lnTVA = ROUND(cota / (100 + cota) * m.lnIncasatAnterior, 2)
							lnBaza = (m.lnIncasatAnterior - m.lnTVA)
						Else
							lnTVA = tva
							lnBaza = baza
						Endif
						Replace tva With tva - m.lnTVA, baza With baza - m.lnBaza
						lnIncasatAnterior = m.lnIncasatAnterior - (m.lnBaza + m.lnTVA)
						
						If m.lnIncasatAnterior = 0 && OR SIGN(m.lnIncasatAnterior) <> m.lnSign
							Exit
						Endif
					ENDSCAN
				ENDIF && m.lnIncasatAnterior <> 0
				
				*** Scad suma achitata curent conform cote TVA, din sold
				Select cCoteTVAFactura2
				Scan
					If (baza <> 0 OR tva <> 0) AND (ABS(m.lnIncasat) < ABS(baza + TVA) OR SIGN(m.lnIncasat) <> SIGN(baza+tva))
						Replace tvaincasat With ROUND(cota / (100 + cota) * m.lnIncasat, 2)
						Replace bazaincasat With m.lnIncasat - tvaincasat
					Else
						Replace tvaincasat With TVA, bazaincasat With baza
					Endif
					lnIncasat = m.lnIncasat - (bazaincasat + tvaincasat)
					If m.lnIncasat = 0
						Exit
					Endif
				Endscan

				Calculate Sum(bazaincasat), Sum(tvaincasat) To lnBazaIncasat, lnTVAIncasat In cCoteTVAFactura2
				Use In (Select('cCoteTVAFactura2'))

				* Daca a mai ramas un rest de incasat fara cota de tva, il pun pe baza incasat
				SELECT cFacturiTVA
				Replace baza_incasat With m.lnBazaIncasat + (total_cont_incasat - m.lnBazaIncasat - m.lnTVAIncasat), tva_incasat With m.lnTVAIncasat
				REPLACE difbaza WITH cont_curent - baza_incasat, diftva WITH cont_tva - tva_incasat
			Endif && SEEK(m.lcIndex)
		Endscan && ales
	Endif && USED('cJurnalTVATemp')

	Use In (Select('cIncasariBanca2'))

	*** Verific pe ANAF starea split tva la data curenta
	SELECT distinct cod_fiscal FROM cFacturiTVA INTO CURSOR cCodFiscalTemp
	loVerificareANAF = CREATEOBJECT("VerificareANAF")
	loVerificareANAF.VerificaListaCIF("cCodFiscalTemp", m.pdDataI, "crsANAF")
	USE IN (SELECT('cCodFiscalTemp'))
	IF USED("crsAnaf")
*!*			crsANAF (cui N(13), data D, denumire V(100), firma V(100), adresa C(244), scpTVA L, data_inceput_ScpTVA D, data_sfarsit_ScpTVA D, data_anul_imp_ScpTVA D, mesaj_ScpTVA C(244), ;
*!*					dataInceputTvaInc D, dataSfarsitTvaInc D, dataActualizareTvaInc D, dataPublicareTvaInc D, tipActTvaInc C(100), statusTvaIncasare L, dataInactivare D, dataReactivare D, dataPublicare D, dataRadiere D, statusInactivi L, ;
*!*					dataInceputSplitTVA D, dataAnulareSplitTVA D, statusSplitTVA L, ;
*!*					tva L, tvaincasare L, splittva L, inactiv L, ;
*!*					mesaj V(100), judet V(100), strada V(100), nr V(20), localitate V(100), Bloc V(20), scara V(20), etaj V(20), apart V(20))
				
		SELECT crsANAF
		SCAN
			SCATTER NAME loANAF
			UPDATE cFacturiTVA SET platitortva = IIF(loANAF.TVA, "DA", "NU"), ;
				splittva = IIF(loANAF.splittva, "DA", "NU"), ;
				tvaincasare = IIF(loANAF.tvaincasare, "DA", "NU"), ;
				inactiv = IIF(loANAF.inactiv, "DA", "NU") ;
				WHERE ALLTRIM(STR(loANAF.cui))$cod_fiscal 	
		ENDSCAN 
	ENDIF 
	
	lcnume = ''
	Select cFacturiTVA
	If Reccount() > 0
		Go Top
		lcnume = 'splittva'


		lcXlsFile = Putfile('Facturi TVA defalcat', 'facturitva_' + LOWER(m.lcTip) + '_' + m.lcCont + '_' + Alltrim(Str(gnAn)) + '_' + Padl(m.gnLuna, 2, '0') + '_' + DTOS(m.pdDataI) + '_' + DTOS(m.pdDataF) + '_' + m.lcnume, 'xls')
		If !Empty(m.lcXlsFile)
			Select cFacturiTVA
			Copy To (m.lcXlsFile) Type Xl5
			OPEN_DEFAULT_APP(m.lcXlsFile)
		Endif
	Endif


	Use In (Select('cFacturiTVA'))
	Use In (Select('cJurnalTVATemp'))
Endproc && TVADefalcatIncasariPlati


*****************
*** Raport facturi funizori/clienti pentru un parteneri + baza/tva din registrul de vanzari
*** pentru a sti ce suma TVA trebuie achitata in contul de TVA al furnizorului
*** Optional tnIdFact, tnValoare - doar pentru o factura
*****************
Procedure TVADefalcatFacturi
	LPARAMETERS tcCont, tlActiv, tnIdPart, tnIdFact, tnValoare
	* tcCont: (obligatoriu) contul 401/4111...
	* tlActiv: (obligatoriu) contul este de activ sau pasiv - pentru JV sau JC
	* tnIdPart: (obligatoriu) id_partener pentru care se face raportul
	* tnIdFact: (optional) id-ul facturii pentru care se face raportul
	* tnValoare: (optional) valoarea achitata pentru care se calculeaza raportul, pentru o singura factura
	
	Private  pdDataI, pdDataF
	Local lofrm As "frm_perioada_zzllaaaa"
	Local laCoteTVA[1], lcField, lcIndex, lcItem, lcSelect, lcSelectAct, lcSelectBanca, lcSelectBanca2
	Local lcSir, lcSirActcv, lcSirBaza, lcSirBazaTVA, lcSirNeimpozabil, lcSirTVA, lcSirTVAAltele
	Local lcWhereA, lcXlsFile, lcnume, ldDataCurenta, llSucces, lnBaza, lnBazaIncasat, lnCotaTVA
	Local lnField, lnIncasat, lnItem, lnItems, lnTVA, lnTVAIncasat, loFact, loFactTVACE, loFactTVAFO
	Local loFactTVARO, loFactTVAWR, lcTipJurnal
	LOCAL lcCont, llFound, lnIdFact, llActiv
	lcCont = IIF(TYPE('tcCont') = 'C' and !EMPTY(m.tcCont), m.tcCont, '401') && 401 furnizori default
	llActiv = m.tlActiv
	
	lcTipJurnal = IIF(m.llActiv, "JV", "JC")
	
	lcWhereIreg = [i.an = ] + ALLTRIM(STR(m.gnAn)) + [ and i.luna = ] + ALLTRIM(STR(m.gnLuna)) + [ and i.cont = '] + ALLTRIM(m.lcCont) + ;
		[' and i.id_part = ] + ALLTRIM(STR(m.tnIdPart)) + IIF(!EMPTY(m.tnIdFact), [ and i.id_fact = ] + ALLTRIM(STR(m.tnIdFact)), []) + ;
		[ and i.precdeb + i.debit - i.preccred - i.credit <> 0] + ;
		Strtran(m.gcCondSucursala, [id_sucursala], [i.id_sucursala])
		
	*** Selectez datele facturilor din Ireg_parteneri (nract, dataact, serie_act, total_fact, total_achitat)
	TEXT TO lcSelect NOSHOW textmerge
SELECT i.dataact,
       i.id_part,
       p.denumire as partener,
       p.cod_fiscal,
       i.id_fact,
       i.nract as nrfact,
       i.cont,
       CAST(stringagg(i.acont) as varchar2(100)) as acont,
       i.dataact as datafact,
       i.serie_act as seriefact,
       sum(<<IIF(m.llActiv, 'i.precdeb + i.debit', 'i.preccred + i.credit')>>) as totalfact,
       sum(<<IIF(m.llActiv, 'i.preccred + i.credit', 'i.precdeb + i.debit')>>) as totalachitat,
       SUM(<<IIF(m.llActiv, 'i.precdeb + i.debit - i.preccred - i.credit', 'i.preccred + i.credit - i.precdeb - i.debit')>>) as sold,       
       CAST(0 as Number(20, 4)) as total_baza_si_tva,
       CAST(0 as Number(20, 4)) as baza,
       CAST(0 as Number(20, 4)) as tva,
       CAST(0 as Number(20, 4)) as neimpozabil,
       CAST(0 as Number(20, 4)) as alte_tva,
       CAST(0 as Number(20, 4)) as total_achitat,
       CAST(0 as Number(20, 4)) as baza_achitat,
       CAST(0 as Number(20, 4)) as tva_achitat
  from ireg_parteneri i
  LEFT JOIN NOM_PARTENERI P
    ON I.ID_PART = P.ID_PART
 where <<m.lcWhereIreg>>
 group by i.dataact,
          i.id_part,
          p.denumire,
          p.cod_fiscal,
          i.id_fact,
          i.nract,
          i.cont,
          i.dataact,
          i.serie_act
 order by i.dataact, p.denumire, i.nract, i.cont	  	
	ENDTEXT 

	llSucces = goExecutor.oExecuta(m.lcSelect, "cIncasariBanca2")

	Use In (Select('cIncasariBanca'))

	IF !m.llSucces
		RETURN
	ENDIF

	*** Selectez prima aparitie a facturilor din Jurnalul de Vanzari, pentru a lua sume pe cote TVA
TEXT TO lcSelect TEXTMERGE NOSHOW
select *
  from (select j.*,
               rank() over(partition by j.nract, j.serie_act, j.dataact, j.id_part order by j.an, j.luna) as rn
          from <<IIF(m.llActiv, 'jv2007', 'jc2007')>> j
          join (select distinct id_part,
                      id_fact,
                      nract,
                      dataact,
                      serie_act
                 from ireg_parteneri i
   				 where <<m.lcWhereIreg>>) f
           on j.id_part = f.id_part
           and j.dataact = f.dataact
           and nvl(j.serie_act, 'xxx') = nvl(f.serie_act, 'xxx')
           and j.nract = f.nract
           and nvl(j.id_fact,f.id_fact) = f.id_fact)
 where rn = 1
 ENDTEXT 

	llSucces = goExecutor.oExecuta(m.lcSelect, "cJurnalTVATemp")

	IF !m.llSucces
		USE IN (SELECT('cIncasariBanca2'))
		RETURN
	ENDIF

	REPLACE ALL serie_act WITH '' FOR ISNULL(serie_act) IN cJurnalTVATemp
	REPLACE ALL seriefact WITH '' FOR ISNULL(seriefact) IN cIncasariBanca2
	
	Select cJurnalTVATemp
	Index On Padr(id_part, 20, ' ') + Dtos(dataact) + Padr(serie_act, 10, ' ') + Padl(nract, 14, '0') Tag ordine
	INDEX on id_fact TAG id_fact
	

	lcSir = ''
	lcSirBaza = '0'
	lcSirTVA = '0'
	lcSirNeimpozabil = '0'
	lcSirTVAAltele = '0'
	lcSirBazaTVA = '0'
	For lnField = 1 To Fcount()
		lcField = UPPER(Field(m.lnField))
		If Inlist(Left(lcField, 2), 'RO', 'CE', 'WR', 'FO')
			lcSir = lcSir + ',j.' + m.lcField

			Do Case
				Case Left(m.lcField, 2) = 'RO' And Right(m.lcField, 1) = 'B'
					lcSirBaza =  m.lcField + '+' + m.lcSirBaza
				Case Left(m.lcField, 2) = 'RO' And Right(m.lcField, 1) = 'T'
					lcSirTVA = m.lcField + '+' + m.lcSirTVA
				Case Left(m.lcField, 2) = 'RO'
					lcSirNeimpozabil = m.lcField + '+' + m.lcSirNeimpozabil
				Otherwise
					lcSirTVAAltele = m.lcField + '+' + m.lcSirTVAAltele
			Endcase
			lcSirBazaTVA = m.lcField + '+' + m.lcSirBazaTVA
		Endif && Inlist(Left(lcField, 2), 'RO', 'CE', 'WR', 'FO')
	Endfor && lnField

	If Used('cJurnalTVATemp')
		Select cIncasariBanca2
		lcSirActcv = ''
		For lnField = 1 To Fcount()
			lcField = Field(m.lnField)
			lcSirActcv = lcSirActcv + ',a.' + m.lcField
		Endfor
		lcSirActcv = Substr(m.lcSirActcv, 2)

		lcSelect = [SELECT ] + m.lcSirActcv + ;
			[, SPACE(2) as platitortva, SPACE(2) as splittva, SPACE(2) as tvaincasare, SPACE(2) as inactiv ] + ;
			lcSir + ;
			[ FROM cIncasariBanca2 a, cJurnalTVATemp j where .F. INTO CURSOR cFacturiTVA READWRITE]
		&lcSelect

		Select cIncasariBanca2
		Scan
			Scatter Name loFact
			lcIndex = Padr(id_part, 20, ' ') + Dtos(datafact) + Padr(seriefact, 10, ' ') + Padl(nrfact, 14, '0')
			lnIdFact = id_fact
			Select cFacturiTVA
			Append Blank
			Gather Name loFact

			llFound = .F.
			Select cJurnalTVATemp
			If !Seek(m.lnIdFact,"cJurnalTVATemp","id_fact")
				If Seek(m.lcIndex,"cJurnalTVATemp","ordine")
					llFound = .T.
				ENDIF 
			ELSE
				llFound = .T.	
			ENDIF 	
			IF m.llFound
				Scatter Name loFactTVARO Fields Like 'RO*'
				Scatter Name loFactTVACE Fields Like 'CE*'
				Scatter Name loFactTVAWR Fields Like 'WR*'
				Scatter Name loFactTVAFO Fields Like 'FO*'

				Select cFacturiTVA
				Gather Name loFactTVARO
				Gather Name loFactTVACE
				Gather Name loFactTVAWR
				Gather Name loFactTVAFO

				Replace baza With Evaluate(m.lcSirBaza), TVA With Evaluate(m.lcSirTVA), ;
					neimpozabil With Evaluate(m.lcSirNeimpozabil), alte_tva With Evaluate(m.lcSirTVAAltele), ;
					total_baza_si_tva With Evaluate(m.lcSirBazaTVA)

				*** Defalcare suma incasata pe cote TVA
				lnIncasat = IIF(EMPTY(m.tnIdFact), loFact.sold, m.tnValoare) && defalc soldul sau valoarea trimisa ca parametru pentru o singura factura
			    lnIncasatAnterior = loFact.totalachitat
				lnSign = SIGN(loFact.totalfact)
				REPLACE total_achitat WITH m.lnIncasat
				
				Create Cursor cCoteTVAFactura(cota N(5, 2), baza N(20, 4), TVA N(20, 4))
				lnItems = Amembers(laCoteTVA, loFactTVARO)
				For lnItem = 1 To m.lnItems
					lcItem = Upper(laCoteTVA[m.lnItem]) && RO19B, RO19T, RO19NB, RO19NT
					lnBaza = 0
					lnTVA = 0

					If Right(m.lcItem, 1) = 'T'
						lnTVA = EVALUATE('loFactTVARO.' + m.lcItem)
					ELSE
						lnBaza = EVALUATE('loFactTVARO.' + m.lcItem)
					Endif

					lnCotaTVA = Val(Strtran(m.lcItem, 'RO', ''))
					Select cCoteTVAFactura
					Locate For cota = m.lnCotaTVA
					If Found()
						REPLACE baza WITH baza + m.lnBaza, TVA WITH TVA + m.lnTVA
					Else
						Insert Into cCoteTVAFactura(cota, baza, TVA) Values (m.lnCotaTVA, m.lnBaza, m.lnTVA)
					Endif
				Endfor

				Select cota, Sum(baza) As baza, Sum(TVA) As TVA, Cast(0 As N(20, 4)) As bazaincasat, Cast(0 As N(20, 4)) As tvaincasat ;
					From cCoteTVAFactura ;
					Group By cota ;
					Order By cota Desc ;
					Into Cursor cCoteTVAFactura2 Readwrite

				Use In (Select('cCoteTVAFactura'))

				*** Diminuez baza si TVA pe cote TVA cu suma incasate anterior
				IF m.lnIncasatAnterior <> 0
					Select cCoteTVAFactura2
					SCAN
						If ABS(m.lnIncasatAnterior) < ABS(baza + TVA)
							lnTVA = ROUND(cota / (100 + cota) * m.lnIncasatAnterior, 2)
							lnBaza = (m.lnIncasatAnterior - m.lnTVA)
						Else
							lnTVA = tva
							lnBaza = baza
						Endif
						Replace tva With tva - m.lnTVA, baza With baza - m.lnBaza
						lnIncasatAnterior = m.lnIncasatAnterior - (m.lnBaza + m.lnTVA)
						If m.lnIncasatAnterior = 0 OR SIGN(m.lnIncasatAnterior) <> m.lnSign
							Exit
						Endif
					ENDSCAN
				ENDIF && m.lnIncasatAnterior <> 0
				
				*** Scad suma achitata curent conform cote TVA, din sold
				Select cCoteTVAFactura2
				Scan
					If ABS(m.lnIncasat) < ABS(baza + TVA)
						Replace tvaincasat With ROUND(cota / (100 + cota) * m.lnIncasat, 2)
						Replace bazaincasat With m.lnIncasat - tvaincasat
					Else
						Replace tvaincasat With TVA, bazaincasat With baza
					Endif
					lnIncasat = lnIncasat - (bazaincasat + tvaincasat)
					If m.lnIncasat = 0 OR SIGN(m.lnIncasat) <> m.lnSign
						Exit
					Endif
				Endscan

				Calculate Sum(bazaincasat), Sum(tvaincasat) To lnBazaIncasat, lnTVAIncasat In cCoteTVAFactura2
				Use In (Select('cCoteTVAFactura2'))

				* Daca a mai ramas un rest de incasat fara cota de tva, il pun pe baza incasat
				* Daca am transmis o valoare de defalcat, nu mai adaug nimic ??? nu imi da corect 
				SELECT cFacturiTVA
					Replace baza_achitat With m.lnBazaIncasat + IIF(EMPTY(m.tnIdFact),(sold - m.lnBazaIncasat - m.lnTVAIncasat), 0), tva_achitat With m.lnTVAIncasat
*!*					REPLACE difbaza WITH cont_curent - baza_sold, diftva WITH cont_tva - tva_sold
			Endif && SEEK(m.lcIndex)
		Endscan && ales
	Endif && USED('cJurnalTVATemp')

	Use In (Select('cIncasariBanca2'))

	*** Verific pe ANAF starea split tva la data curenta
	SELECT distinct cod_fiscal FROM cFacturiTVA INTO CURSOR cCodFiscalTemp
	loVerificareANAF = CREATEOBJECT("VerificareANAF")
	loVerificareANAF.VerificaListaCIF("cCodFiscalTemp", Date(), "crsANAF")
	USE IN (SELECT('cCodFiscalTemp'))
	IF USED("crsAnaf")
*!*			crsANAF (cui N(13), data D, denumire V(100), firma V(100), adresa C(244), scpTVA L, data_inceput_ScpTVA D, data_sfarsit_ScpTVA D, data_anul_imp_ScpTVA D, mesaj_ScpTVA C(244), ;
*!*					dataInceputTvaInc D, dataSfarsitTvaInc D, dataActualizareTvaInc D, dataPublicareTvaInc D, tipActTvaInc C(100), statusTvaIncasare L, dataInactivare D, dataReactivare D, dataPublicare D, dataRadiere D, statusInactivi L, ;
*!*					dataInceputSplitTVA D, dataAnulareSplitTVA D, statusSplitTVA L, ;
*!*					tva L, tvaincasare L, splittva L, inactiv L, ;
*!*					mesaj V(100), judet V(100), strada V(100), nr V(20), localitate V(100), Bloc V(20), scara V(20), etaj V(20), apart V(20))
				
		SELECT crsANAF
		SCAN
			SCATTER NAME loANAF
			UPDATE cFacturiTVA SET platitortva = IIF(loANAF.TVA, "DA", "NU"), ;
				splittva = IIF(loANAF.splittva, "DA", "NU"), ;
				tvaincasare = IIF(loANAF.tvaincasare, "DA", "NU"), ;
				inactiv = IIF(loANAF.inactiv, "DA", "NU") ;
				WHERE ALLTRIM(STR(loANAF.cui))$cod_fiscal 	
		ENDSCAN 
	ENDIF 

	lcnume = ''
	Select cFacturiTVA
	If Reccount() > 0
		Go Top
		lcNume = 'splittva_' + ALLTRIM(cFacturiTVA.cod_fiscal)

		lcXlsFile = Putfile('Facturi TVA defalcat', 'facturitva_achitat_' + m.lcCont + '_' + lcTipJurnal + '_' + Alltrim(Str(gnAn)) + Padl(m.gnLuna, 2, '0') + '_' + m.lcnume, 'xls')
		If !Empty(m.lcXlsFile)
			Select cFacturiTVA
			Copy To (m.lcXlsFile) Type Xl5
			OPEN_DEFAULT_APP(m.lcXlsFile)
		Endif
	Endif


	Use In (Select('cFacturiTVA'))
	Use In (Select('cJurnalTVATemp'))
Endproc && TVADefalcatFacturi