**************************************************************************************************************
**	Clase:
**		oGeneratorNumere
**	Proceduri:
**		viz_config_serii_complet
**		creeaza_cursor_serii
**	Functii:
**		caut_serii_fact
**		caut_tipentitate_permis
**************************************************************************************************************
*********************************** Obiect oNumereDocumente *********************************
Define Class oGeneratorNumere As Custom
	cAliasVechi = Null
	lInitializare = .F.		&&	modificare 14.07.2010
	nTipuriDocument = 30
	Declare paPlaje (30,8)
**	1	-	numar
**	2	-	nume cursor
**	3	-	cu_serie
**	4	-	cu_plaja
**	5	-	id_serie
** 	6	-	cu buffer			.T.	daca se genereaza mai multe numere in cadrul aceleiasi operatii
**	7	-	tip buffer			1	-	in caz de renuntare, se dezaloca toate numerele din buffer ( implicit )
**								2	-	in caz de renuntare, se dezaloca toate numerele din buffer, cu exceptia primului, care este repus in array
**	8	-	nume cursor buffer
******************************
	Procedure Init
*!*	modificare 14.07.2010
*!*		This.ResetAll()
		This.lInitializare = .T.
		This.ResetAll()
		This.lInitializare = .F.
*!*	modificare 14.07.2010	^
	Endproc
******************************
	Procedure Reset
	Lparameters tnIdTipDoc
	With This
		.paPlaje(tnIdTipDoc,1) = 0
		.paPlaje(tnIdTipDoc,2) = Null
		.paPlaje(tnIdTipDoc,3) = .F.
		.paPlaje(tnIdTipDoc,4) = .F.
		.paPlaje(tnIdTipDoc,5) = Null
		.dezactiveazaBuffer(tnIdTipDoc)
	Endwith
	Endproc
******************************
	Procedure ResetAll
	With This
		For i = 1 To This.nTipuriDocument
			.Reset(i)
		Endfor
	Endwith
	Endproc
******************************
	Procedure ResetNumar
	Lparameters tnIdTipDoc
	With This
		.paPlaje(tnIdTipDoc,1) = 0
		.paPlaje(tnIdTipDoc,5) = Null
	Endwith
	Endproc
******************************
	Procedure ResetNumere
	With This
		For i = 1 To This.nTipuriDocument
			.ResetNumar(i)
		Endfor
	Endwith
	Endproc
******************************
	Procedure activeazaBuffer
	Lparameters tnIdTipDoc,tnTipBuffer
	Local lnTipBuffer
	lcSql = [begin pack_serii_numere.seteazaNumereMultiple(1); end;]
	If goExecutor.oExecuta(lcSql)
		If Empty(tnTipBuffer) Or !Between(tnTipBuffer,1,2)
			lnTipBuffer = 1
		Else
			lnTipBuffer = tnTipBuffer
		Endif
		lcCursor = [crsbufsertemp]+Alltrim(Str(tnIdTipDoc))
		Create Cursor (lcCursor) (numar N(14))
*!*			If !Empty(This.paPlaje(tnIdTipDoc,1))
*!*				Select (lcCursor)
*!*				Append Blank
*!*				Replace numar With This.paPlaje(tnIdTipDoc,1)
*!*				This.ResetNumar(tnIdTipDoc)
*!*			Endif
		This.paPlaje(tnIdTipDoc,6) = .T.
		This.paPlaje(tnIdTipDoc,7) = lnTipBuffer
		This.paPlaje(tnIdTipDoc,8) = lcCursor
	Endif
	Endproc
******************************
	Procedure dezactiveazaBuffer
	Lparameters tnIdTipDoc
	lcSql = [begin pack_serii_numere.seteazaNumereMultiple(0); end;]
*!*	modificare 14.07.2010 : imi apeleaza de 20 de ori procedura la fiecare initializare a obiectului 
*!*		lnSucces = goExecutor.oExecute(lcSql)
	If (This.lInitializare And tnIdTipDoc = 1) Or !This.lInitializare
		llSucces = goExecutor.oExecuta(lcSql)
	Else
		llSucces = .T.
	Endif
*!*	modificare 14.07.2010	^
	If llSucces
		lcCursor = This.paPlaje(tnIdTipDoc,8)
		If !Empty(Nvl(lcCursor,[]))
			Use In (lcCursor)
		Endif
		This.paPlaje(tnIdTipDoc,6) = .F.
		This.paPlaje(tnIdTipDoc,7) = 1
		This.paPlaje(tnIdTipDoc,8) = Null
	Endif
	Endproc
******************************
	Procedure salveazaAliasVechi
	This.cAliasVechi	= Alias()
	Endproc
******************************
	Procedure selecteazaAliasVechi
	If !Empty(Nvl(This.cAliasVechi,[]))
		Select (This.cAliasVechi)
	Endif
	Endproc
******************************
	Function creeaza_cursor_serii
	Lparameters tnIdTipDoc
	Local lcSql,lcCursor,llSucces
	Private pnIdTipDoc,pnRezultat
	pnIdTipDoc = tnIdTipDoc
	pnRezultat = 0
	This.salveazaAliasVechi()
	lcCursor = [crsseriitemp]+Alltrim(Str(tnIdTipDoc))
	If Used(lcCursor)
		Use In (lcCursor)
	Endif
	lcSql = [{call ] + gcS + [.pack_serii_numere.verifica_cursor_serii(?pnIdTipDoc,?gnIdUtil,?gnIdSucursala,?@pnRezultat)}]
	llSucces = goExecutor.oExecuta(lcSql,lcCursor)
	With This
*!*	modificare	04.09.2012
		If !llSucces OR pnRezultat = -1
*!*			If !llSucces
*!*	modificare	04.09.2012	^
			*!*	modificare	ROAGEST v 2.0.138
			lcSql = [{call ] + gcs + [.pack_serii_numere.verifica_tipdoc(?pnIdTipDoc,?@pnRezultat)}]
			IF !goExecutor.oExecuta(lcSql,lcCursor)
				*!*	modificare	ROAGEST v 2.0.138	^
				pnRezultat = 1
				.paPlaje(tnIdTipDoc,2) = Null
				.paPlaje(tnIdTipDoc,3) = .F.
				.paPlaje(tnIdTipDoc,4) = .F.
				*!*	modificare	ROAGEST v 2.0.138
			Else
				.paPlaje(tnIdTipDoc,2) = Null
				.paPlaje(tnIdTipDoc,3) = Iif(Mod(pnRezultat,2)=1,.T.,.F.)
				.paPlaje(tnIdTipDoc,4) = .F.
			Endif
			*!*	modificare	ROAGEST v 2.0.138	^
		Else
			If Reccount(lcCursor) > 0
				.paPlaje(tnIdTipDoc,2) = lcCursor
				.paPlaje(tnIdTipDoc,3) = Iif(Mod(pnRezultat,2)=1,.T.,.F.)
				.paPlaje(tnIdTipDoc,4) = Iif(pnRezultat>=2,.T.,.F.)
			Else
				pnRezultat = 1
				.paPlaje(tnIdTipDoc,2) = Null
				.paPlaje(tnIdTipDoc,3) = .F.
				.paPlaje(tnIdTipDoc,4) = .F.
			Endif
		Endif
	Endwith
	This.selecteazaAliasVechi()
	Release pnIdTipDoc
	Return pnRezultat
	Endfunc	&&	creeaza_cursor_serii
******************************
	Function aloca_numar
	Lparameters tnIdTipDoc,tnIdGestiune
	Private pnIdTipDoc,pnIdGestiune,pnNumar,pnIdSerie
	Do Case
	Case Isnull(This.paPlaje(tnIdTipDoc,2))
		pnNumar = 0
*!*	modificare ROAGEST v 2.0.105
*!*		Case This.paPlaje(tnIdTipDoc,1) <> 0
*!*	modificare ROAGEST v 2.0.105	^
	Case This.paPlaje(tnIdTipDoc,1) <> 0 And !This.paPlaje(tnIdTipDoc,6)
		pnNumar = This.paPlaje(tnIdTipDoc,1)
	Otherwise
		pnNumar = -1
		pnIdTipDoc = tnIdTipDoc
		pnIdGestiune = Iif(Isnull(tnIdGestiune) Or Pcount()=1,Null,tnIdGestiune)
		This.salveazaAliasVechi()
		lcCursor = This.paPlaje(tnIdTipDoc,2)
		If Used(lcCursor)
			Select (lcCursor)
			pnIdSerie = id_serie
		Else
			pnIdSerie = Null
		Endif
		lcSql=[begin ] + gcS + [.pack_serii_numere.aloca_numar(?pnIdTipDoc,?pnIdSerie,?pnIdGestiune,?gnIdUtil,?gnIdSucursala,?@pnNumar); end;]
		IF !goExecutor.oExecuta(lcSql)
			pnNumar = 0
		Endif
		Release pnIdTipDoc,pnIdGestiune,pnIdEntitate
		With This
*!*	modificare ROAGEST v 2.0.105 : daca e activat buffer-ul, atunci pun fiecare numar generat in cursor,nu in array ( in afara de primul, care ramane in array )
			If This.paPlaje(tnIdTipDoc,6)
				lcCursor = This.paPlaje(tnIdTipDoc,8)
				Select (lcCursor)
				Append Blank
				Replace numar With pnNumar
			Else
*!*	modificare ROAGEST v 2.0.105^
				.paPlaje(tnIdTipDoc,1) = pnNumar
*!*	modificare ROAGEST v 2.0.105
			Endif
*!*	modificare ROAGEST v 2.0.105	^
			.paPlaje(tnIdTipDoc,5) = pnIdSerie
		Endwith
		This.selecteazaAliasVechi()
	Endcase
	Return pnNumar
	Endfunc	&&	aloca_numar
******************************
	Procedure verifica_numar
	Lparameters tnIdTipDoc,tnNumar
	With This
		If tnNumar <> .paPlaje(tnIdTipDoc,1) And .paPlaje(tnIdTipDoc,1) <> 0
			.dezaloca_numar(tnIdTipDoc)
		Endif
	Endwith
	Endproc
******************************
	Function verifica_serie
	Lparameters tnIdTipDoc
	Local lcCursor,llReturn
	llReturn = .F.
	With This
		If !Isnull(This.paPlaje(tnIdTipDoc,2))
			.salveazaAliasVechi()
			lcCursor = Alltrim(.paPlaje(tnIdTipDoc,2))
			If Used(lcCursor)
				Select (lcCursor)
				If Nvl(.paPlaje(tnIdTipDoc,5),-99)<>Nvl(id_serie,-99)
					.dezaloca_numar(tnIdTipDoc)
					llReturn = .T.
				Endif
			Endif
			.selecteazaAliasVechi()
		Else
			llReturn = .T.
		Endif
	Endwith
	Return llReturn
	Endfunc
******************************
	Procedure dezaloca_numar
	Lparameters tnIdTipDoc
	Private pnIdTipDoc
	pnIdTipDoc = tnIdTipDoc
	With This
		If !Isnull(This.paPlaje(tnIdTipDoc,2))
			.salveazaAliasVechi()
			If .paPlaje(tnIdTipDoc,1) <> 0
				* Cateodata dadea eroare la executia in Oracle wrong type, desi pnIdTipDoc era numeric
*!*					lcSql=[begin ] + gcS + [.pack_serii_numere.dezaloca_numar(?pnIdTipDoc); end;]
				lcSql=[begin pack_serii_numere.dezaloca_numar(] + ALLTRIM(TRANSFORM(m.pnIdTipDoc)) + [); end;]
				If goExecutor.oExecuta(lcSql)
					.paPlaje(tnIdTipDoc,1) = 0
				Endif
			Endif
			.selecteazaAliasVechi()
		Endif
	Endwith
	Release pnIdTipDoc
	Endproc	&&	dezaloca_numar
******************************
	Procedure dezaloca_buffer
	Lparameters tnIdTipDoc,tlDezactiveazaBuffer
	Private pnIdTipDoc
	pnIdTipDoc = tnIdTipDoc
	With This
		pnTip = .paPlaje(tnIdTipDoc,7)
		.salveazaAliasVechi()
		lcSql=[begin ] + gcS + [.pack_serii_numere.dezaloca_numere(?pnIdTipDoc,?pnTip); end;]
		If goExecutor.oExecuta(lcSql)
			If pnTip = 1
				.paPlaje(tnIdTipDoc,1) = 0
			Endif
			If tlDezactiveazaBuffer
				.dezactiveazaBuffer(tnIdTipDoc)
			Endif
		Endif
		.selecteazaAliasVechi()
	Endwith
	Release pnIdTipDoc,pnTip
	Endproc	&&	dezaloca_numere
*******************************
	Procedure dezaloca_numere
	With This
		For i = 1 To This.nTipuriDocument
			If .paPlaje(i,1)<>0
				.dezaloca_numar(i)
			Endif
		Endfor
	Endwith
	Endproc	&&	dezaloca_numere
******************************
	Function getNumeCursor
	Lparameters tnIdTipDoc
	Return This.paPlaje(tnIdTipDoc,2)
	Endfunc	&&	getNumeCursor
******************************
	Function getSerieCursor
	Lparameters tnIdTipDoc
	Local lcSerie
	With This
		If Reccount(.paPlaje(tnIdTipDoc,2))>0
			.salveazaAliasVechi()
			Select (.paPlaje(tnIdTipDoc,2))
			lcSerie = Alltrim(serie)
			.selecteazaAliasVechi()
		Else
			lcSerie = []
		Endif
	Endwith
	Return lcSerie
	Endfunc	&&	getNumeCursor
******************************
	Function getLungimeSerie
	Lparameters tnIdTipDoc
	Local lnLungime
	With This
		If Reccount(.paPlaje(tnIdTipDoc,2))>0
			.salveazaAliasVechi()
			Select (.paPlaje(tnIdTipDoc,2))
			lnLungime = lungime
			.selecteazaAliasVechi()
		Else
			lnLungime = Null
		Endif
	Endwith
	Return lnLungime
	Endfunc	&&	getLungimeSerie
******************************
	Function getIdSerie
	Lparameters tnIdTipDoc
	Return This.paPlaje(tnIdTipDoc,5)
	Endfunc	&&	getIdSerie
******************************
Enddefine
************************************ INCEPUT : viz_config_serii_complet **********************************
Procedure viz_config_serii_complet
Lparameters tnIdTipDoc
Private pcSchema,pcSelect,pcFiltru,pcOrder,poDocumente,poPlaje,poSerii
Local llAfiseaza
Store [] To pcSchema,pcSelect,pcFiltru,pcOrder,poDocumente,poPlaje,poSerii
Store .F. To llAfiseaza
pcSchema = []
pcSelect = [select id_tipdoc, id_tipentitate, tipdoc, descriere, maxlen, cu_serie, cu_plaje, tipentitate, plajepeentitate, an, luna, an2caractere from vserii_tipdoc where]
If Empty(tnIdTipDoc)
	pcFiltru = [2=2]
Else
	pcFiltru = [id_tipdoc=] + Alltrim(Str(tnIdTipDoc))
Endif
pcOrder = [tipdoc]
llAfiseaza = .F.
gencursor('poDocumente','crsTipdoc',pcSelect,pcFiltru,pcSchema,pcOrder,llAfiseaza)
poDocumente.ca_baza1.afisare()

pcSchema = [id_serie N(10),serie C(10),an N(1),luna N(1),lungime N(10),inactiv N(1),resetare C(50),exemplu C(50), isautofactura N(1), isbeneficiari N(1), isterti N(1), isfurnizori N(1), prefix N(5), an2caractere N(1)]
pcSelect = [select id_serie, serie, an, luna, lungime, inactiv, resetare, exemplu, isautofactura, isbeneficiari, isterti, isfurnizori, prefix, an2caractere from vserii where]
pcFiltru = [2=2]
pcOrder = [serie]
llAfiseaza = .F.
gencursor('poSerii','crsSerii',pcSelect,pcFiltru,pcSchema,pcOrder,llAfiseaza)
poSerii.ca_baza1.afisare()

pcSchema = [id_plaja N(10),id_tipdoc N(10),id_tipentitate N(10),id_entitate N(10),pl_inf N(18),pl_sup N(18),] + ;
	[inactiv N(1),id_serie N(10),datai d,datas d,tipentitate C(100),entitate C(100),serie C(10),urmval C(100),valabil N(1)]
pcSelect = [select id_plaja, id_tipdoc, id_tipentitate, id_entitate, pl_inf, pl_sup, inactiv, id_serie,] + ;
	[datai, datas, tipentitate, entitate, serie, urmval, valabil from vplaje_numere where]
pcFiltru = [1=2]
pcOrder = [id_tipdoc]
llAfiseaza = .F.
lcFiltruOriginal = [NVL(id_sucursala,-99) = NVL(?gnIdSucursala,-99)]
gencursor('poPlaje','crsPlaje',pcSelect,pcFiltru,pcSchema,pcOrder,llAfiseaza,[],.F.,lcFiltruOriginal)

ofrmseriicomplet = Createobject('frm_plaje_numere')
ofrmseriicomplet.Show(1)

Use In (Select('crsTipdoc'))
Use In (Select('crsSerii'))
Use In (Select('crsPlaje'))
Release ofrmseriicomplet, poDocumente, poSerii, poPlaje
Endproc	&&	viz_config_serii_complet
************************************ SFARSIT : viz_config_serii_complet **********************************
*********************************** INCEPUT : caut_tipentitate_permis ********************************
Function caut_tipentitate_permis
Parameters tnTipDoc,tnPornire, tlDesktop
Private pnTipDoc
pnTipDoc = tnTipDoc
Local  lcCont,loCauta, lnPornire, llDesktop
lnPornire = tnPornire && 1-incepe cu...6-toate
llDesktop = tlDesktop
Store "" To loCauta

lcSelect = [select id_tipentitate, descriere from ] + gcS + [.vserii_tipentpermis]
lcFiltru = [1=2]
lcSchema = []
lcOrder = [descriere]
lccoloane = [descriere]
lcTitlu = [Alegeti tipul entitatii]
lcTitluColoane = [Tip entitate]
lcFiltruOriginal = [id_tipdoc = ?pnTipDoc]
lcNumeProc = []
llToateIreg = .F.
loCauta = cauta_alfa(lcSelect,lcFiltru,lcSchema,lcOrder,lccoloane,lcTitlu,lcTitluColoane, lcNumeProc, llToateIreg, lcFiltruOriginal,,lnPornire,,,llDesktop)		&& 11.07.2007

Return loCauta
Endfunc && caut_tipentitate_permis
********************************** SFARSIT : caut_tipentitate_permis ********************************
************************************** INCEPUT : caut_serii_fact ************************************
Function caut_serii_fact
Parameters tnPornire, tlDesktop

Local  lcCont,loCauta, lnPornire, llDesktop
lnPornire = tnPornire && 1-incepe cu...6-toate
llDesktop = tlDesktop
Store "" To loCauta

lcSelect = [select id_serie,serie,resetare,lungime,exemplu,an,luna from ] + gcS + [.vserii]
lcFiltru = [1=2]
lcSchema = [id_serie N(10),serie C(50),resetare C(50),lungime N(10),exemplu C(50),an N(1),luna N(1)]
lcOrder = [serie,luna,an]
lccoloane = [serie,resetare,lungime,exemplu]
lcTitlu = [Alegeti seria]
lcTitluColoane = [Serie,Resetare,Lungime,Exemplu]
lcFiltruOriginal = [inactiv = 0]
lcNumeProc = []
llToateIreg = .F.
loCauta = cauta_alfa(lcSelect,lcFiltru,lcSchema,lcOrder,lccoloane,lcTitlu,lcTitluColoane, lcNumeProc, llToateIreg, lcFiltruOriginal,,lnPornire,,,llDesktop)		&& 11.07.2007

Return loCauta
Endfunc && caut_serii_fact
************************************** SFARSIT : caut_serii_fact ************************************