*!* 19.12.2019
*!* marius.mutu
*!* Adauga_modifica_inregistrare, sterge_inregistrare: Am adaugat variabila pnIdAMS pentru a fi completata cu id-ul generat la salvarea unei inregistrari noi in baza de date

&& ------------------------------INCEPUT: Adauga_Modifica_Inregistrare------------------------------
*!* Procedura:  Adauga_Modifica_Inregistrare
*!* Parametri: tcForm, toRec, tnId
*!* Data/Ora generarii: 26/04/2004 11:21
*!* Autor: MARIUS.MUTU
Procedure  Adauga_Modifica_Inregistrare
	Lparameters tcAlias, toRec, tnId, tcActiune,tlNoShow,tcObjName

	* parametrul @tnId se transmite optional prin referinta la INSERT pentru a fi completat cu Id-ul generat la adaugarea in baza de date
	* trebuie modificate cus_odata_xxx.make_sql pentru a completa variabila pnIdAMS
	Private pnIdAMS
	pnIdAMS = m.tnId

	Local lcAlias,llNoShow,lcObjName
	lcAlias = Upper(Alltrim(tcAlias))

	lcForm = 'frm_' + lcAlias + '_nou'
	llNoShow = tlNoShow
	Private poRec
	poRec = toRec
	If Empty(tcObjName)
		lcObjName = 'loAdMod' + lcAlias
	Else
		lcObjName = Alltrim(tcObjName)
	Endif
	*DO FORM &lcForm NAME loAdMod WITH toRec,tnId,tcActiune NOSHOW 

	&lcObjName = Createobject(lcForm,poRec,tnId,tcActiune)
	If !tlNoShow
		&lcObjName..Show(1)
	Endif

	tnId = m.pnIdAMS

	Return gnButon
Endproc
&& ------------------------------SFARSIT: Adauga_Modifica_Inregistrare------------------------------

&& ------------------------------INCEPUT: Sterge_Inregistrare ------------------------------
*!* Procedura:  Sterge_Inregistrare
*!* Parametri: tcAlias, tnId
*!* Data/Ora generarii: 26/04/2004 11:28
*!* Autor: MARIUS.MUTU
Procedure  Sterge_Inregistrare
	Lparameters tcAlias, tnId, tlNoSave,tcObjName

	Private pnIdAMS
	pnIdAMS = m.tnId

	lcAlias = Upper(Alltrim(tcAlias))
	lnId = tnId

	llNoSave = tlNoSave
	If Empty(tcObjName)
		lcObjName = 'loData'
	Else
		lcObjName = Alltrim(tcObjName)
	Endif


	lcData = 'cus_odata_' + lcAlias
	&lcObjName = Createobject(lcData)
	&lcObjName..cActiune = "DELETE"

	If !llNoSave
		Return &lcObjName..salvare(,lnId)
	Else
		Return .T.
	Endif

Endproc
&& ------------------------------SFARSIT: Sterge_Inregistrare ------------------------------

Define Class BizDataObject As Custom
	oObject = Null
	cObjectName = ''
	cSelect = ''
	cSchema = ''
	nId = NULL
	
	Procedure Init
		Lparameters toHash
		*nId: id inregistrare, doar daca se transmite si cObjectName
		*cObjectName: identificator select, schema dintr-o colectie (optional)
		*cSelect: instructiune [select a,b,c from tabel where id = ?pnId], in cazul in care nu exista cObjectName (optional)
		*cSchema: schema pentru cSelect (optional)
		* loBizDataObject = CreateObject("BizDataObject", GetHash([cSelect=>select a,b,c from tabel where id = ?pnId]))
		* loBizDataObject.GetDataObject()
		This.nId = 	toHash.GetValue("nId")
		This.cObjectName = 	toHash.GetValue("cObjectName")
		This.cSelect = toHash.GetValue("cSelect")
		This.cSchema = 	toHash.GetValue("cSchema")
	Endproc && Init

	***************************************************************
	*** Intoarce un obiect selectat din baza de date pe baza tnId
	***************************************************************
	Procedure GetDataObject
		LOCAL lcSelect, lcSelectV, llSucces
		PRIVATE pnObjectId, poObject
		lcSelectV = SELECT()
		llSucces = .F.
		poObject = NULL
		pnObjectId = this.nId
		
		lcSelect = this.cSelect
		lcSchema = this.cSchema && id_articol N(20), codmat V(50), denumire V(100), descriere V(200), um V(10), id_grupa N(5), grupa V(50), id_subgrupa N(5), subgrupa V(50), tip N(3)
		IF !(Empty(This.cObjectName) OR EMPTY(This.nId))
			loDefObject = This.GetDefObject()
			lcSelect = loDefObject.GetValue("cSelect")
			lcSchema = loDefObject.GetValue("cSchema")
			lcIdColumn = loDefObject.GetValue("cIdColoana")
			lcSelect = lcSelect + " where " + m.lcIdColumn + " = ?pnObjectId"
			This.cSelect = m.lcSelect 
		ENDIF  

		IF !EMPTY(m.lcSelect)
			IF EMPTY(m.lcSchema)
				llSucces = goExecutor.oSelecteaza2Object(m.lcSelect, @poObject)
			ELSE
				poCursor = Null
				gencursor_hash(gethash([cnume=>poCursor??cAlias=>crsDataObject??cselect=>] + m.lcSelect + [??cschema=>] + m.lcSchema + [??lModParam=>.T.??cwhere=>] + m.lcIdColumn + [=pnObjectId]))
				IF USED('crsDataObject')
					llSucces = .T.
					SELECT crsDataObject
					SCATTER NAME poObject MEMO 
					USE IN (SELECT('crsDataObject'))
				ENDIF 
			ENDIF 
		ENDIF 	
		IF !m.llSucces
			poObject = NULL
		ENDIF 
		SELECT (m.lcSelectV)
		RETURN poObject	
	ENDPROC && GetDataObject

	*************************************************
	Function GetDefObject
		lcObjectName = Lower(This.cObjectName)
		loHash = GetHash()
		Do Case
			CASE EMPTY(m.lcObjectName)
				*
			Case lcObjectName = "nom_parteneri"
				With loHash
					.SetValue("cSelect", "select id_part, denumire, cod_fiscal from nom_parteneri")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "denumire")
					.SetValue("cColoana", "denumire,cod_fiscal,id_part")
					.SetValue("cTitlu", "Alegeti partenerul")
					.SetValue("cTitluColoana", "Nume,Cod fiscal,ID")
					.SetValue("cFiltruOriginal", [STERS = 0 AND INACTIV = 0])
					.SetValue("cresultcoloana","denumire")
					.SetValue("cidcoloana","id_Part")
				Endwith

			Case lcObjectName = "nom_parteneri4111"
				With loHash
					.SetValue("cSelect", "SELECT B.ID_PART, B.DENUMIRE, B.COD_FISCAL " + ;
						" FROM CORESP_TIP_PART A " + ;
						" LEFT JOIN NOM_PARTENERI B ON A.ID_PART = B.ID_PART")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "b.denumire")
					.SetValue("cColoana", "b.denumire,b.cod_fiscal,b.id_part")
					.SetValue("cTitlu", "Alegeti partenerul")
					.SetValue("cTitluColoana", "Nume,Cod fiscal,ID")
					.SetValue("cFiltruOriginal", [B.STERS = 0 AND B.INACTIV = 0 AND A.ID_TIP_PART IN (SELECT id_tip_part FROM coresp_tip_cont WHERE cont = '4111')])
					.SetValue("cresultcoloana","denumire")
					.SetValue("cidcoloana","id_Part")
				Endwith

			Case lcObjectName = "nom_responsabil"
				With loHash
					.SetValue("cSelect", "select id_responsabil, nume, marca, bi, cnp from vnom_responsabili")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "nume")
					.SetValue("cColoana", "nume, marca, bi, cnp, id_responsabil")
					.SetValue("cTitlu", "Alegeti responsabilul")
					.SetValue("cTitluColoana", "Nume, Marca, BI, CNP, Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","nume")
					.SetValue("cidcoloana","id_responsabil")
				Endwith


			Case lcObjectName = "nom_feldoc"
				With loHash
					.SetValue("cSelect", "select id_fdoc, fel_document FROM vnom_fdoc")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "fel_document")
					.SetValue("cColoana", "fel_document,id_fdoc")
					.SetValue("cTitlu", "Alegeti felul documentului")
					.SetValue("cTitluColoana", "Document,ID")
					.SetValue("cFiltruOriginal", [inactiv = 0])
					.SetValue("cresultcoloana","fel_document")
					.SetValue("cidcoloana","id_fdoc")
				Endwith


			Case lcObjectName = "nom_gestiune"
				With loHash
					.SetValue("cSelect", "select nume_gestiune,cgest,id_gestiune,nr_pag,cont from vnom_gestiuni ")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "nume_gestiune")
					.SetValue("cColoana", "nume_gestiune,cgest")
					.SetValue("cTitlu", "Alegeti Gestiunea")
					.SetValue("cTitluColoana", "Gestiune,Cgest")
					.SetValue("cFiltruOriginal", [inactiv = 0])
					.SetValue("cresultcoloana","nume_gestiune")
					.SetValue("cidcoloana","id_gestiune")
				Endwith

			Case lcObjectName = "nom_lucrare"
				With loHash
					.SetValue("cSelect", "select nrord, id_lucrare from vnom_lucrari")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "nrord")
					.SetValue("cColoana", "nrord,id_lucrare")
					.SetValue("cTitlu", "Alegeti lucrarea")
					.SetValue("cTitluColoana", "Lucrare,ID")
					.SetValue("cFiltruOriginal", [inactiv = 0])
					.SetValue("cresultcoloana","nrord")
					.SetValue("cidcoloana","id_lucrare ")
				Endwith

			Case lcObjectName = "nom_sectie"
				With loHash
					.SetValue("cSelect", "select id_sectie, sectie, csectie FROM vnom_sectii")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "sectie")
					.SetValue("cColoana", "sectie, csectie")
					.SetValue("cTitlu", "Alegeti sectia")
					.SetValue("cTitluColoana", "Sectie, Indicativ")
					.SetValue("cFiltruOriginal", [inactiv = 0])
					.SetValue("cresultcoloana","sectie")
					.SetValue("cidcoloana","id_sectie")
				Endwith

			Case lcObjectName = "nom_valuta"
				With loHash
					.SetValue("cSelect", "select id_valuta, nume_val FROM vnom_valute")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "nume_val")
					.SetValue("cColoana", "nume_val")
					.SetValue("cTitlu", "Alegeti valuta")
					.SetValue("cTitluColoana", "Valuta")
					.SetValue("cFiltruOriginal", [inactiv = 0 and moneda_nationala = 0])
					.SetValue("cresultcoloana","nume_val")
					.SetValue("cidcoloana","id_valuta")
				Endwith

			Case lcObjectName = "nom_sucursala"
				With loHash
					.SetValue("cSelect", "select id_sucursala, sucursala, inactiv from vnom_sucursale")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "2=2")
					.SetValue("cOrder", "sucursala")
					.SetValue("cColoana", "sucursala,id_sucursala")
					.SetValue("cTitlu", "Alegeti sucursala")
					.SetValue("cTitluColoana", "Sucursala, Id")
					If glEMama && DACA SUNT PE FIRMA MAMA ARAT DOAR SUCURSALELE EI
						.SetValue("cFiltruOriginal", [inactiv = 0 AND id_mama = ?gnIdFirma])
					Else && DACA SUNT PE O SUCURSALA SAU PE FIRMA INDEPENDENTA NU ARAT NIMIC
						.SetValue("cFiltruOriginal", [1=2])
					Endif
					.SetValue("cresultcoloana","sucursala")
					.SetValue("cidcoloana","id_sucursala")
				Endwith

			Case lcObjectName = "nom_utilizator"
				With loHash
					.SetValue("cSelect", "select id_util,utilizator from syn_vutilizatori")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "utilizator")
					.SetValue("cColoana", "utilizator")
					.SetValue("cTitlu", "Alegeti utilizatorul")
					.SetValue("cTitluColoana", "Utilizator")
					.SetValue("cFiltruOriginal", [inactiv = 0])
					.SetValue("cresultcoloana","utilizator")
					.SetValue("cidcoloana","id_util")
				Endwith

			Case lcObjectName = "nom_venchel"
				With loHash
					.SetValue("cSelect", "select id_venchelt, explicatie, (case tip_venchelt when 1 then 'Cheltuiala' else 'Venit' end) as tip_venchelt FROM vnom_venchel")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "explicatie")
					.SetValue("cColoana", "explicatie,tip_venchelt")
					.SetValue("cTitlu", "Alegeti venitul/cheltuiala")
					.SetValue("cTitluColoana", "Explicatia, Tip")
					.SetValue("cFiltruOriginal", [inactiv = 0])
					.SetValue("cresultcoloana","explicatie")
					.SetValue("cidcoloana","id_venchelt")
				Endwith

			Case Inlist(lcObjectName, "nom_articol", "nom_articole")
				With loHash
					.SetValue("cSelect", "select denumire, codmat, um, grupa, subgrupa, id_grupa, id_subgrupa, descriere, dnf, cont, acont, inactiv, id_articol from vnom_articole")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "denumire")
					.SetValue("cColoana", "denumire,codmat,um,grupa,subgrupa,cont,acont,dnf,id_articol")
					.SetValue("cTitlu", "Alegeti articolul")
					.SetValue("cTitluColoana", "Denumire,Cod Material,UM,Grupa,Subgrupa,Cont,Analitic,DNF,Id")
					.SetValue("cFiltruOriginal", [inactiv = 0])
					.SetValue("cresultcoloana","denumire")
					.SetValue("cidcoloana","id_articol")
				Endwith

			Case lcObjectName = "nom_grupa_articol"
				With loHash
					.SetValue("cSelect", "select grupa, id_grupa from vgest_art_gr")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "grupa")
					.SetValue("cColoana", "grupa,id_grupa")
					.SetValue("cTitlu", "Alegeti grupa de articole")
					.SetValue("cTitluColoana", "Grupa,Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","grupa")
					.SetValue("cidcoloana","id_grupa")
				Endwith

			Case lcObjectName = "nom_subgrupa_articol"
				With loHash
					.SetValue("cSelect", "select subgrupa, grupa, id_subgrupa from vgest_art_sbgr")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "subgrupa")
					.SetValue("cColoana", "subgrupa,grupa,id_subgrupa")
					.SetValue("cTitlu", "Alegeti subgrupa de articole")
					.SetValue("cTitluColoana", "Subgrupa,Grupa,Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","subgrupa")
					.SetValue("cidcoloana","id_subgrupa")
				Endwith

			Case lcObjectName = "nom_grupa_gestiune"
				With loHash
					.SetValue("cSelect", "select id_grupe, nume_grupa, nume_parinte, parent_id FROM vgest_nom_grupe")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "nume_grupa")
					.SetValue("cColoana", "nume_grupa,nume_parinte,id_grupe,parent_id")
					.SetValue("cTitlu", "Alegeti grupa de gestiuni")
					.SetValue("cTitluColoana", "Grupa,Grupa parinte,Id,Id parinte")
					.SetValue("cFiltruOriginal", [inactiv = 0])
					.SetValue("cresultcoloana","nume_grupa")
					.SetValue("cidcoloana","id_grupe")
				Endwith

			Case lcObjectName = "nom_meserie"
				With loHash
					.SetValue("cSelect", "select id_meseria, meserie from SAL_VNOM_MES")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "meserie")
					.SetValue("cColoana", "id_meseria, meserie")
					.SetValue("cTitlu", "Alegeti meseria")
					.SetValue("cTitluColoana", "Meseria, Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","meserie")
					.SetValue("cidcoloana","meseria")
				Endwith

			Case lcObjectName = "nom_formatie"
				With loHash
					.SetValue("cSelect", "select id_formatia, denumire from sal_vnom_formatii")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "denumire")
					.SetValue("cColoana", "denumire, id_formatia")
					.SetValue("cTitlu", "Alegeti formatia")
					.SetValue("cTitluColoana", "Formatia, Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","denumire")
					.SetValue("cidcoloana","id_formatia")
				Endwith

			Case lcObjectName = "nom_tip_rulaj"
				With loHash
					.SetValue("cSelect", "select id_tip_rulaj,descriere from nom_tip_rulaj")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "2=2")
					.SetValue("cOrder", "descriere")
					.SetValue("cColoana", "descriere,id_tip_rulaj")
					.SetValue("cTitlu", "Alegeti tipul de rulaj")
					.SetValue("cTitluColoana", "Descriere,ID")
					.SetValue("cFiltruOriginal", [STERS = 0])
					.SetValue("cresultcoloana","descriere")
					.SetValue("cidcoloana","id_tip_rulaj")
				Endwith

			Case lcObjectName = "nom_contracte"
				With loHash
					.SetValue("cSelect", "select id_ctr, id_part, denumire, contract, numar, data from vcontracte")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "data desc, numar")
					.SetValue("cColoana", "contract,denumire,id_ctr")
					.SetValue("cTitlu", "Alegeti contractul")
					.SetValue("cTitluColoana", "Contract,Partener,Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","contract")
					.SetValue("cidcoloana","id_ctr")
				Endwith

			Case lcObjectName = "tipuri_contracte"
				With loHash
					.SetValue("cSelect", "select id_tip_ctr,denumire from tipuri_contracte")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "2=2")
					.SetValue("cOrder", "denumire")
					.SetValue("cColoana", "denumire,id_tip_ctr")
					.SetValue("cTitlu", "Alegeti tipul de contract")
					.SetValue("cTitluColoana", "Denumire,ID")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","denumire")
					.SetValue("cidcoloana","id_tip_ctr")
				Endwith

			Case lcObjectName = "nom_transe"
				With loHash
					.SetValue("cSelect", "select id_transa, transa from sal_vnom_transe")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "transa")
					.SetValue("cColoana", "transa, id_transa")
					.SetValue("cTitlu", "Alegeti transa")
					.SetValue("cTitluColoana", "Transa, Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","transa")
					.SetValue("cidcoloana","id_transa")
				Endwith

			Case lcObjectName = "nom_tichete"
				With loHash
					.SetValue("cSelect", "select id, denumire,valoare from sal_vnom_tichete")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "denumire")
					.SetValue("cColoana", "denumire, valoare, id")
					.SetValue("cTitlu", "Alegeti tipul tichetului")
					.SetValue("cTitluColoana", "Tip tichet,Valoare, Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","denumire")
					.SetValue("cidcoloana","id")
				Endwith

			Case lcObjectName = "nom_locatii"
				With loHash
					.SetValue("cSelect", "select id, denumire, sters, inactiv, id_util, dataora, id_utils, dataoras from vnom_locatii")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "denumire")
					.SetValue("cColoana", "denumire,id")
					.SetValue("cTitlu", "Alegeti locatia")
					.SetValue("cTitluColoana", "Denumire,Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","denumire")
					.SetValue("cidcoloana","id")
				Endwith

			Case lcObjectName = "nom_tipfactura"
				With loHash
					.SetValue("cSelect", "select id, denumire from fact_tipuri")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "denumire")
					.SetValue("cColoana", "denumire,id")
					.SetValue("cTitlu", "Alegeti tipul facturii")
					.SetValue("cTitluColoana", "Denumire,Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","denumire")
					.SetValue("cidcoloana","id")
				Endwith

			Case lcObjectName = "nom_ruta"
				With loHash
					.SetValue("cSelect", "select id_ruta, ruta from fact_nom_rute")
					.SetValue("cSchema", "")
					.SetValue("cFiltru", "1=2")
					.SetValue("cOrder", "ruta")
					.SetValue("cColoana", "denumire,id")
					.SetValue("cTitlu", "Alegeti ruta")
					.SetValue("cTitluColoana", "Ruta,Id")
					.SetValue("cFiltruOriginal", [])
					.SetValue("cresultcoloana","ruta")
					.SetValue("cidcoloana","id_ruta")
				Endwith
		ENDCASE
		
		RETURN loHash
	ENDFUNC && GetDefObjectByName

Enddefine && BizDataObject
