#INCLUDE COMUN.H
#INCLUDE MVC.H

Define Class LoginController As Custom
	oDBController = Null
	oControllerSetari = Null
	oUtilizator = Null
*!*		oFirma = Null
	oView = Null
	nTipLogin = Null
	cFormLogin = Null
	cProcLogin = Null
	nTipVerificare	=	0	
*!*	0	=	verificare utilizator si parola
*!*	1	=	verificare utilizator si parola cu id-ul dat ca parametru
*!*	2	=	verificare utilizator si parola cu nivelul dat ca parametru
	nIdUtil = Null
	nNivelAcces = Null
	cExplicatie = "Autentificare"
	cRestaurant = Null
	***********************************************************************
	Procedure Init
		Lparameters toDbController,toControllerSetari,tnTipVerificare,tnParametru,tcDenumire
		This.oDBController = toDbController
		This.oControllerSetari = toControllerSetari
		This.oUtilizator = Createobject("UtilizatorModel")
		*!*			This.oFirma = CREATEOBJECT("FirmaModel")
		This.nTipLogin = 2
		This.cFormLogin = [Login] + Iif(This.nTipLogin=2,[CodBare],[])
		This.cProcLogin = [Login] + Iif(This.nTipLogin=2,[CodBare],[])
		This.nTipVerificare = IIF(TYPE('tnTipVerificare')<>'N',0,tnTipVerificare)
		Do Case
			Case This.nTipVerificare = 1
				This.nIdUtil = tnParametru
				This.nNivelAcces = Null
				This.cExplicatie = This.cExplicatie + [ ] + ALLTRIM(tcDenumire)
			Case This.nTipVerificare = 2
				This.nNivelAcces = tnParametru
				This.nIdUtil = Null
				This.cExplicatie = This.cExplicatie + [ nivel minim : ] + ALLTRIM(tcDenumire)
			Otherwise
				This.oDBController.setLoginController(This)
				This.nIdUtil = Null
				This.nNivelAcces = Null
		Endcase
	Endfunc && INIT
	***********************************************************************
	Procedure conecteaza
		Local lcParolaSchema,lcServer,lcSchema,lcParola
		lcServer = This.oControllerSetari.getServerSetari()
		This.oDBController.setServer(lcServer)
		This.oDBController.setSchemaParola(This.oControllerSetari.getSchemaSecurity(lcServer),This.oControllerSetari.getParolaSecurity(lcServer))
		lnIdFirmaSetari = This.oControllerSetari.getIdFirmaSetari()
		If !Empty(lnIdFirmaSetari)
			lcParolaSchema = This.oDBController.getParolaSchema(lnIdFirmaSetari)
			IF !EMPTY(lcParolaSchema)
				lnPoz = AT('$',lcParolaSchema)
				lcSchema = SUBSTR(lcParolaSchema,1,lnPoz-1)
				lcParola = SUBSTR(lcParolaSchema,lnPoz+1)
				This.oDBController.setSchemaParola(lcSchema,lcParola)
				If This.nTipVerificare = 0
					This.oControllerSetari.verificaVersiuni()
				Endif
			ENDIF
		Endif
	Endproc
	***********************************************************************
	PROCEDURE deconecteaza
		This.oDBController.deconecteaza()
		This.oUtilizator.reseteazaDate()
	ENDPROC
	***********************************************************************
	Function getIdUtil
		Return This.oUtilizator.getIdUtil()
	Endfunc
	***********************************************************************
	Function getUtilizator
		Return This.oUtilizator.getUtilizator()
	Endfunc
	***********************************************************************
	Function getSex
		Return This.oUtilizator.getSex()
	Endfunc
	***********************************************************************
	Procedure setInTura
		LPARAMETERS tlInTura
		This.oUtilizator.setInTura(tlInTura)
	Endproc
	***********************************************************************
	Function getInTura
		Return This.oUtilizator.getInTura()
	Endfunc
	***********************************************************************
	Function getNivelAcces
		Return This.oUtilizator.getNivelAcces()
	Endfunc
	***********************************************************************
	Function getIdRestaurant
		Return This.oControllerSetari.getIdRestaurant()
	Endfunc
	***********************************************************************
	FUNCTION setRestaurant
		LPARAMETERS tcRestaurant
		This.cRestaurant = tcRestaurant
		_Screen.Caption = gcExplicatieProgram + [ * ] + tcRestaurant
	ENDFUNC
	***********************************************************************	
	FUNCTION getRestaurant
		RETURN This.cRestaurant
	ENDFUNC
	***********************************************************************	
	Function getIdSucursala
		Return This.oControllerSetari.getIdSucursala()
	Endfunc
	*********************************************************************
	Function getIdMama
		Return This.oControllerSetari.getIdMama()
	Endfunc
	*********************************************************************
	Function getEMama
		Return This.oControllerSetari.getEMama()
	Endfunc
	*********************************************************************	
	FUNCTION getFirma
		LOCAL loFirma
		loFirma = This.oControllerSetari.getoFirma()
		RETURN NVL(loFirma.firma,[ ])
	endfunc
	***********************************************************************	
	Procedure lanseazaLogin
		If This.conecteaza()
			Do Form (This.cFormLogin) With This,This.cExplicatie Name This.oView
		Else
			amessagebox("Nu s-a putut stabili conexiunea cu serverul!",16,"Eroare")
		Endif
	Endproc && List
	***********************************************************************
	Procedure verificaUtilizator
		Parameters tcParola,tcUtilizator
		Local lcCod,lcUtilizator
		This.oDBController.setAfiseazaEroare(.F.)
		lcCod = This.oDBController.verifica_drepturi_utilizator(tcParola,;
			This.oControllerSetari.getIdFirmaSetari(),This.oControllerSetari.getNumeProgram(),;
			This.nTipVerificare,IIF(This.nTipVerificare=2,This.nNivelAcces,This.nIdUtil),tcUtilizator)

		If !ISNULL(lcCod)
			This.oUtilizator.actualizeazaDate(tcParola,lcCod,tcUtilizator)
			IF this.nTipVerificare = 0
				This.oControllerSetari.citesteSetariSchema(This.oUtilizator.getIdUtil())			
				This.setRestaurant(This.oDBController.initializeaza_restaurant(This.oControllerSetari.getIdRestaurant()))
			ENDIF
		Else
			This.oUtilizator.reseteazaDate()
		Endif

		IF This.verificaSucces()
			This.oView.Release()
		Else
			If This.nTipLogin = 1
*!*					lcErrorMessage = "Combinatie utilizator/parola invalida!"
				This.oView.txtUtilizator.SetFocus()
			Else
*!*					lcErrorMessage = "Cod invalid!"
				This.oView.txtParola.Value = []
				This.oView.txtParola.SetFocus()
			Endif
*!*				This.oView.lblMessage.Caption = lcErrorMessage
			This.oView.lblMessage.Caption = This.oDBController.getEroare()
		Endif
		This.oDBController.setAfiseazaEroare(.T.)
	Endproc
	***********************************************************************
	FUNCTION verificaSucces
		LOCAL llReturn
		IF This.esteAutentificat()
*!*	 AND ((This.nTipVerificare = 0) OR ;
*!*			 		(This.nTipVerificare = 1 AND This.oUtilizator.getIdUtil() = This.nIdUtil) OR ;
*!*			 		(This.nTipVerificare = 2 AND This.oUtilizator.getNivelAcces() >= This.nNivelAcces))
		 	llReturn = .T.
		ELSE
		 	llReturn = .F.
		ENDIF
		RETURN llReturn
	ENDFUNc
	***********************************************************************	
	Function esteAutentificat
		Return This.oUtilizator.esteAutentificat()
	Endfunc
	***********************************************************************
	Procedure SetEventsHandlers
		Bindevent(This.oView.cmdLogin, "CLICK", This, This.cProcLogin )
		Bindevent(This.oView.cmdCancel, "CLICK", This, This.cProcLogin )
	Endproc && SetEventsHandler
	***********************************************************************
	Procedure Login
		Local lnRows, lcCommand, lcErrorMessage, lcUtilizator, lcParola, loUtilizator

		Declare laEvents[3]
		lcErrorMessage = ""
		lnRows = Aevents(laEvents,0)

		If lnRows = 3
			lcCommand = Upper(laEvents(1).Name)
		Else
			lcCommand = ""
		Endif
		If  lcCommand = 'CMDLOGIN'
			lcUtilizator = Alltrim(This.oView.cboUtilizator.Value)
			lcParola = Alltrim(This.oView.txtParola.Value)

			This.verificaUtilizator(lcParola,lcUtilizator)
		Else
			This.oView.Release()
		Endif

	Endproc && Login
	***********************************************************************
	Procedure LoginCodBare
		Local lnRows, lcCommand, lcErrorMessage, lcUtilizator, lcParola, loUtilizator
		Declare laEvents[3]
		lcErrorMessage = ""
		lnRows = Aevents(laEvents,0)
		If lnRows = 3
			lcCommand = Upper(laEvents(1).Name)
		Else
			lcCommand = ""
		Endif

		If  Inlist(lcCommand, 'CMDLOGIN')
			lcCodBare = Alltrim(This.oView.txtParola.Value)
*!*				lcUtilizator = Left(lcCodBare,At("$", lcCodBare)-1)
*!*				lcParola = Substr(lcCodBare,At("$", lcCodBare)+1)
			lcParola = lcCodBare
			This.verificaUtilizator(lcParola)
		Else
			This.oView.Release()
		Endif

	Endproc && LoginCodBare
	***********************************************************************
	PROCEDURE reinitializeazaDate
		This.oControllerSetari.citesteSetariSchema(This.oUtilizator.getIdUtil())
	ENDPROC
	***********************************************************************	
	PROCEDURE release
		This.oUtilizator.Release()
	ENDPROC
	***********************************************************************	
Enddefine