***************************************************************************************************************
****	Proceduri:
****		vizualizare_lucrari
****		vizualizare_comenzi
****		vizualizare_optiuni
****		extrage_optiuni			-	mutata in proceduri_comune.prg	( v 2.0.11 )
****		recompune_optiuni		-	mutata in proceduri_comune.prg	( v 2.0.11 )
****		make_sectii_utilizator	-	mutata in proceduri_comune.prg	( v 2.0.11 )
****		verifica_lucrare
***************************************************************************************************************
******************************************* INCEPUT: vizualizare_lucrari *******************************************
Procedure vizualizare_lucrari
gcAcces=[1;2;3;4;]
Private polucrari,pocomenzi,polucrarielemente
Local lcSchema,lcSelect,lcOrder,lcFiltru,lcFiltruOriginal,llAfiseaza
Store '' To polucrari,pocomenzi,polucrarielemente
lcSchema=[id_lucrare n(20),nrord c(30),termen_executie t,id_lucrari_detalii n(20),explicatie c(100),data_lucrare t,]+;
	[dataora t,utilizator c(30),inchisa n(1)]
lcSelect=[select id_lucrare,nrord,termen_executie,id_lucrari_detalii,explicatie,data_lucrare,]+;
	[dataora,utilizator,inchisa from vlucrari_detalii]
lcOrder=[data_lucrare]
lcFiltru = [1=2]
lcFiltruOriginal = [id_sectie=]+ALLTRIM(STR(gnId_sectie))
llAfiseaza = .F.
lcgroup = []
llModParam = .T.
llAfiseaza = .F.
*gencursor('polucrari','crslucrari',lcSelect,lcFiltru,lcSchema,lcOrder,llAfiseaza)
gencursor('polucrari','crslucrari', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza,  lcgroup, llModParam, lcFiltruOriginal)
polucrari.ca_baza1.afisare()

lcSchema=[id_comanda n(20),COMANDA_EXTERNA c(20),id_codclient c(20),id_lucrare n(20),id_part n(20),id_gestiune n(20),]+;
	[nr_comanda c(100),data_comanda t,nume c(70),data_livrare t,interna n(1),id_sectie n(5),id_sectie2 n(5),id_facturare n(5),id_livrare n(5),adresa_facturare c(50),adresa_livrare c(50)]
lcSelect=[select id_comanda,COMANDA_EXTERNA,id_codclient,id_lucrare,id_part,id_gestiune]+;
	[nr_comanda,data_comanda,nume,data_livrare,interna,id_sectie,id_sectie2,id_facturare,id_livrare,b.denumire_adresa as adresa_facturare,c.denumire_adresa as adresa_livrare]+;
	[from vcomenzi a left join adrese_parteneri b on b.id_adresa=a.id_facturare left join adrese_parteneri c on c.id_adresa=a.id_livrare]
lcOrder=[data_comanda]
lcFiltru = [1=2]
lcFiltruOriginal = [id_sectie=]+ALLTRIM(STR(gnId_sectie))
llAfiseaza = .F.
lcgroup = []
llModParam = .T.
llAfiseaza = .F.
*gencursor('pocomenzi','crscomenzi',lcSelect,lcFiltru,lcSchema,lcOrder,llAfiseaza)
gencursor('pocomenzi','crscomenzi', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza,  lcgroup, llModParam, lcFiltruOriginal)
pocomenzi.ca_baza1.afisare()

lcSchema=[id_lucrare n(20),denumire c(100),cantitate n(16,3),um c(10),cantitate2 n(16,3),um2 c(10)]
lcSelect=[select id_lucrare,denumire,cantitate,um,cantitate2,um2 from vlucrari_elemente]
lcOrder=[denumire]
lcFiltru = [1=2]
lcFiltruOriginal = [] && [id_sectie=]+ALLTRIM(STR(gnId_sectie))
llAfiseaza = .F.
lcgroup = []
llModParam = .T.
llAfiseaza = .F.
*gencursor('polucrarielemente','crslucrarielemente',lcSelect,lcFiltru,lcSchema,lcOrder,llAfiseaza)
gencursor('polucrarielemente','crslucrarielemente', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza,  lcgroup, llModParam, lcFiltruOriginal)
polucrarielemente.ca_baza1.afisare()

ofrmlucrari=Createobject('frm_lucrari_detalii')
ofrmlucrari.Show(1)
Release polucrari,pocomenzi,polucrarielemente,ofrmlucrari

Endproc	&&	vizualizare_lucrari
******************************************* SFARSIT: vizualizare_lucrari *******************************************
******************************************* INCEPUT: vizualizare_comenzi *******************************************
Procedure vizualizare_comenzi
gcAcces=[1;2;3;4;]
Private pocomenzi,pocomenzielemente
Local lcSchema,lcSelect,lcOrder,lcFiltru,llAfiseaza
Store '' To pocomenzi,pocomenzielemente
lcSchema=[selectat n(1),id_comanda n(20),COMANDA_EXTERNA c(20),id_codclient c(20),id_lucrare n(20),id_part n(20),id_agent n(20),id_delegat n(20),id_masina n(20),]+;
	[nr_comanda c(100),data_comanda t,nume c(70),tip_comanda c(20),data_livrare t,nume_agent c(70),nume_delegat c(70),]+;
	[nrinmat c(10),nrord c(30),facturat n(1),dataora t,utilizator c(40),data_livrat t,interna n(1),]+;
	[nr_livrare c(50),inchisa n(1), id_sectie n(5),id_sectie2 n(5),id_facturare n(5),id_livrare n(5),adresa_facturare c(50),adresa_livrare c(50), cod_client c(50), id_ctr n(10),numar_contract c(100)]
lcSelect=[select 0 as selectat,a.id_comanda,a.COMANDA_EXTERNA,a.id_codclient,a.id_lucrare,a.id_part,a.id_agent,a.id_delegat,a.id_masina,]+;
	[a.nr_comanda,a.data_comanda,a.nume,a.tip_comanda,a.data_livrare,a.nume_agent,a.nume_delegat,]+;
	[a.nrinmat,a.nrord,a.facturat,a.dataora,a.utilizator,a.data_livrat,a.interna,a.nr_livrare,a.inchisa,a.id_sectie,a.id_sectie2,a.id_facturare,a.id_livrare,] + ;
	[b.denumire_adresa as adresa_facturare,c.denumire_adresa as adresa_livrare, d.COD as cod_client,d.id_ctr,d.numar_contract ]+;
	[from vcomenzi a left join adrese_parteneri b on b.id_adresa=a.id_facturare left join adrese_parteneri c on c.id_adresa=a.id_livrare left join parteneri_coduri d on d.id=a.id_codclient]
lcOrder=[data_comanda]
lcFiltru = [1=2]
lcFiltruOriginal = [] && [id_sectie=]+ALLTRIM(STR(gnId_sectie))
llAfiseaza = .F.
*gencursor('pocomenzi','crscomenzi',lcSelect,lcFiltru,lcSchema,lcOrder,llAfiseaza)
gencursor('pocomenzi','crscomenzi', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza,  lcgroup, llModParam, lcFiltruOriginal)
pocomenzi.ca_baza1.afisare()

lcSchema=[id_comanda n(20),id_articol n(20),id_pol n(20),denumire c(100),pret n(16,] + alltrim(str(gnPPretV)) + [),cantitate n(16,] + alltrim(str(gnPCant)) + [),um c(10)]
lcSelect=[select id_comanda,id_articol,id_pol,denumire,pret,cantitate,um from vcomenzi_elemente]
lcOrder=[denumire]
lcFiltru = [1=2]
lcFiltruOriginal = [] && [id_sectie=]+ALLTRIM(STR(gnId_sectie))
llAfiseaza = .F.
*gencursor('pocomenzielemente','crscomenzielemente',lcSelect,lcFiltru,lcSchema,lcOrder,llAfiseaza)
gencursor('pocomenzielemente','crscomenzielemente', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza,  lcgroup, llModParam, lcFiltruOriginal)
pocomenzielemente.ca_baza1.afisare()

ofrmcomenzi=Createobject('frm_comenzi')
ofrmcomenzi.Show(1)
Release pocomenzi,pocomenzielemente,ofrmcomenzi

Endproc	&&	vizualizare_comenzi
******************************************* SFARSIT: vizualizare_comenzi *******************************************
******************************************* INCEPUT: vizualizare_optiuni *******************************************
Procedure vizualizare_optiuni
If Used('crsoptiuni')
	Use In crsoptiuni
Endif
lcSql=[SELECT VARTYPE,VARNAME,VARVALUE FROM ] + gcS + [.OPTIUNI WHERE PROGRAM='] + Upper(Alltrim(gcNumeProgram)) + [']
lcCursor=[crsoptiuni]
lnSucces=goExecutor.oExecute(lcSql,lcCursor)
If lnSucces<0
	amessagebox(goExecutor.cEroare,0+48,"Eroare")
Else
	Select crsoptiuni
	Scan
		lcvartype=Alltrim(Upper(Vartype))
		lcvarname=Alltrim(Upper(varname))
		Do Case
		Case lcvartype = "CHARACTER"
			luvarvalue = Alltrim(varvalue)
			gc&lcvarname. = luvarvalue
		Case lcvartype = "NUMERIC"
			luvarvalue = Val(varvalue)
			gn&lcvarname. = luvarvalue
		Endcase
	Endscan
	Use In crsoptiuni
	ofrmoptiune=Createobject('frm_optiuni_comenzi')
	ofrmoptiune.Show(1)
	Release ofrmoptiune
Endif
Endproc	&&	vizualizare_optiuni
******************************************* SFARSIT: vizualizare_optiuni *******************************************
********************************************************************************************************************
*!*	modificare v 2.0.11
*!*	Procedure extrage_optiuni
*!*	Parameters tcLista, tnId

*!*	Local lcLista, lcReturn
*!*	Store '' To lcReturn
*!*	lcLista = Nvl(Alltrim(tcLista),'')
*!*	lnNrOptiuni = Getwordcount(lcLista,";")
*!*	For i=1 To lnNrOptiuni
*!*		lcExtragOptId = Getwordnum(lcLista,i,";")
*!*		lcId_extras = Getwordnum(lcExtragOptId,1,"::")
*!*		If lcId_extras = Alltrim(Str(tnId))
*!*			lcReturn = Getwordnum(lcExtragOptId,2,"::")
*!*		Endif
*!*	Endfor

*!*	Return lcReturn

*!*	Endproc && extrage_optiune
********************************************************************************************************************
********************************************************************************************************************
*!*	*!*	lcLista = '1001::3;2::777;21001::5;'
*!*	*!*	lcId = 2
*!*	*!*	lcValoareNoua = '9'
*!*	*!*	lcListaNoua = recompune_optiuni(lcLista,lcId,lcValoareNoua)

*!*	Procedure recompune_optiuni
*!*	Parameters tcLista, tnId, tcValoareNoua

*!*	Local lcLista, lcListaNoua, lcId, lcValoareNoua, lnGrupuri, lcGrup, lcIdCautat, lnPoz, lnPozGrup, lcValoare
*!*	lcLista = Nvl(Alltrim(tcLista),'')
*!*	lcId = Alltrim(Str(tnId))
*!*	lcValoareNoua = Alltrim(tcValoareNoua)

*!*	llGasit = .F.
*!*	lcListaNoua = ''
*!*	lnGrupuri = Getwordcount(lcLista,';')
*!*	For i = 1 To lnGrupuri
*!*		lcGrup = Getwordnum(lcLista,i,";")
*!*		If i=1
*!*			lcIdCautat = lcId + '::'
*!*		Else
*!*			lcIdCautat = ';'+ lcId + '::'
*!*			lcGrup = ';' + lcGrup				&&	';id_sectie::optiune_veche'
*!*		Endif
*!*		lnPozGrup = At(lcIdCautat,lcGrup)
*!*		If lnPozGrup > 0
*!*			llGasit = .T.
*!*			lnPoz = At('::', lcGrup)
*!*			If lnPoz > 0
*!*				lcValoare = Substr(lcGrup,lnPoz)	&&	'::optiune_veche'
*!*				lcGrup= Strtran(lcGrup,lcValoare,'::'+lcValoareNoua)
*!*			Endif
*!*		Endif
*!*		lcListaNoua = lcListaNoua + lcGrup
*!*	Endfor

*!*	If lnGrupuri = 0
*!*		lcListaNoua = lcId + '::' + lcValoareNoua
*!*	Else
*!*		If !llGasit
*!*			lcListaNoua = lcListaNoua + ';' + lcId + '::' + lcValoareNoua
*!*		Endif
*!*	Endif

*!*	Return lcListaNoua

*!*	Endproc && recompune_optiuni
********************************************************************************************************************
*!*	Procedure make_sectii_utilizator
*!*	lcSel = [{call PACK_COMENZI.sectii_utilizator(?gnIdUtil,?gnIdSucursala)}]
*!*	lcCursor = 'crsSectii'
*!*	lnSucces = goExecutor.oExecute(lcSel,lcCursor)
*!*	If lnSucces < 0
*!*		amessagebox(goExecutor.cEroare,0+16,"Eroare")
*!*		Return
*!*	Endif
*!*	Select crsSectii
*!*	Go Top
*!*	Endproc && make_sectii_utilizator
*!*	modificare v 2.0.11	^
********************************************************************************************************************
Function verifica_lucrare
Lparameters tnIdLucrare,tcNumeCursorVFP
Local llReturn,lcSql,lcNumeCursorOra,lcCursorV,lcMesaj
&&&	(loCauta.id_lucrare,[crstempcomlucrare])
llReturn = .T.
lcNumeCursorTempV = [crstempvluc]
lcCursorVerificare = [crsverificareluc]
lcMesaj = []
If Used(lcCursorVerificare)
	Use In (lcCursorVerificare)
Endif
If Used(lcNumeCursorTempV)
	Use In (lcNumeCursorTempV)
Endif
Create Cursor (lcCursorVerificare) (Id N(14),nume c(100),nr_comanda M)
Create Cursor (lcNumeCursorTempV) (interna N(1),Id N(14),nume c(200),nr_comanda M,nr N(14))
If !Empty(Nvl(tnIdLucrare,0))
	lcNumeCursorOra = [crstemporalucrare]
	If Used(lcNumeCursorOra)
		Use In (lcNumeCursorOra)
	Endif
	lcSql = [select a.interna,a.id,(case when a.interna in (2,5) then b.denumire ] + ;
		[when interna = 3 then c.nume_gestiune ] + ;
		[when interna = 4 then d.sectie ] + ;
		[else null end) as nume,] + ;
		[stringagg(a.nr_comanda) as nr_comanda,a.nr from ] + ;
		[(select id_lucrare, interna,] + ;
		[(case when interna in (2,5) then id_part ] + ;
		[when interna = 3 then id_gestiune ] + ;
		[when interna = 4 then id_sectie2 ] + ;
		[else null end) as id,] + ;
		[nr_comanda,] + ;
		[count(*) over(partition by id_lucrare, interna,(case when interna = 2 then id_part when interna = 3 then id_gestiune ] + ;
		[when interna = 4 then id_sectie2 else null end)) as nr ] + ;
		[from ] + gcS + [.comenzi where sters = 0 and id_sectie = ?gnId_sectie and interna <> 1 and id_lucrare = ] + Alltrim(Str(tnIdLucrare)) + [) a ] + ;
		[left join ] + gcS + [.nom_parteneri b on a.id = b.id_part and a.interna = 2 ] + ;
		[left join ] + gcS + [.nom_gestiuni c on a.id = c.id_gestiune and a.interna = 3 ] + ;
		[left join ] + gcS + [.nom_sectii d on a.id = d.id_sectie and a.interna = 4 ] + ;
		[group by a.interna,a.id,(case when a.interna in (2,5) then b.denumire when a.interna = 3 then c.nume_gestiune when a.interna = 4 then d.sectie else null end),a.nr ] + ;
		[order by 3,2]
	lnSucces = goExecutor.oExecute(lcSql,lcNumeCursorOra)
	If lnSucces < 0
		amessagebox(goExecutor.cEroare,16,"Eroare")
		Return .F.
	Else
		Insert Into (lcNumeCursorTempV) Select interna,id,nume,Chr(44)+Alltrim(nr_comanda)+Chr(44) as nr_comanda,nr From (lcNumeCursorOra)
		Use In (lcNumeCursorOra)
	Endif
Endif

If !Empty(tcNumeCursorVFP)
	Local loDublura
	Select (tcNumeCursorVFP)
	Scan For interna <> 1
		Scatter Name loDublura
		Select (lcNumeCursorTempV)
		Locate For interna = loDublura.interna And Id = Iif(INLIST(loDublura.interna,2,5),loDublura.id_part,Iif(loDublura.interna=3,loDublura.id_gestiune,loDublura.id_sectie2))	 &&	modificare	v 2.0.20
		If Found()
			If At([,] + Alltrim(loDublura.nr_comanda) + [,],Alltrim(nr_comanda)) = 0
&&	s-ar putea sa am comanda deja pe lucrare si sa o am si selectata in cursorul din VFP
				Replace nr_comanda With Alltrim(nr_comanda) + Alltrim(loDublura.nr_comanda) + [,],nr With nr + 1
			Endif
		Else
			Append Blank
			Replace interna With loDublura.interna,Id With Iif(INLIST(loDublura.interna,2,5),loDublura.id_part,Iif(loDublura.interna=3,loDublura.id_gestiune,loDublura.id_sectie2)),;	&& modificare v 2.0.20
				nume With loDublura.nume,nr_comanda With [,] + Alltrim(loDublura.nr_comanda) + [,],nr With 1
		Endif
		Select (tcNumeCursorVFP)
	Endscan
	Release loDublura
Endif

Insert Into (lcCursorVerificare) Select Id,nume,nr_comanda From (lcNumeCursorTempV) Where nr > 1 Order By nume,Id
If Reccount(lcCursorVerificare)>0
	lcMesaj = [Urmatorii beneficiari vor avea mai mult de o comanda pe lucrare : ] + Chr(13) + Chr(10)
	Select (lcCursorVerificare)
	Scan
		lcMesaj = lcMesaj + Padr(Alltrim(nume),50,[ ]) + [ - ] + Substr(Alltrim(nr_comanda),2,Len(Alltrim(nr_comanda))-2) + Chr(13) + Chr(10)
	Endscan
	lcMesaj = lcMesaj + Chr(13) + Chr(10) + [Doriti sa continuati?]
	If amessagebox(lcMesaj,4+32,"Comenzi multiple") = 7
		llReturn = .F.
	Endif
Endif

If Used(lcCursorVerificare)
	Use In (lcCursorVerificare)
Endif
Return llReturn
Endfunc	&&	verifica_lucrare
********************************************************************************************************************