*!* 09.07.2020
*!* marius.mutu
*!* start_firma - initializare pack_sesiune.set_an, set_luna, pack_sesiune.set_sucursala, pack_sesiune.set_id_util

* PROCEDURE Start_Firma(  )
* Date : 06/10/2004, 12:27:00
* author : marius.mutu
* description:
* creeaza cursorul v_firme;
* alege firma, anul, luna;
* initializeaza <gcS> = schema firmei
* creeaza cursorul <calendar> si verifica luna aleasa
* apeleaza <oinit_optiuni>
* apeleaza <update_nomenclator>

****** PARAMETER BLOCK **************
* Parameters : 0
*
******************************************* INCEPUT:Start_Firma *******************************************
Procedure Start_Firma


	*** selectez firmele
	Do update_firme_util_prog With gnIdProgram,gnIdUtil In updateserver.prg

	Select v_Firme
	If Reccount() = 0
		aMessagebox('Nu aveti drepturi pe nici o firma!',0+16,'Atentie')
		Quit
	Endif
	Go Top
	If Type('goFirma') = 'O' And Type('goFirma.id_firma') = 'N'
		If !Empty(goFirma.id_firma) And !Isnull(goFirma.id_firma)
			Locate For id_firma = goFirma.id_firma
			If !Found()
				Go Top
			Endif
		Endif
	Endif

	Do While .T.


		*---------------------
		Private polunilean

		lcSelect = [select * from syn_lunilean ]
		lcCursor = [v_lunilean]
		lnSucces = goExecutor.oExecute(lcSelect,lcCursor)
		If lnSucces < 0
			aMessagebox('Eroare la selectie ' + goExecutor.cEroare)
			Return
		Endif



		*---------------------
		If Type('loStart_Firma')!='O' Or Isnull(loStart_Firma)
			*!*				DO FORM frm_start_firma NAME loStart_Firma LINKED NOSHOW
			Select v_Firme
			Do Form frm_start_firma_nou Name loStart_Firma Linked Noshow
		Endif

		Select v_Firme
		If gnLuna*gnAn <> 0
			Select v_lunilean
			Locate For nrluna = gnLuna

			loStart_Firma.cLuna = Alltrim(v_lunilean.numeluna)
			loStart_Firma.cAn = Alltrim(Str(gnAn))
		Endif
		loStart_Firma.cFirma = Alltrim(v_Firme.firma)
		*****************************Inceput Lansare toolbar*****************************
		If !(gnLuna*gnAn <> 0 And primadata=.T.)
			loStart_Firma.Show(1)
		Else
			*!* 05.03.2007
			*!* la lansarea din toolbar formularul este invizibil si gnbuton = 2 -> (ca si cum as da renunt - iese din program)
			gnbuton = 1
			*!* 05.03.2007 ^

			Select v_Firme
*!* 11.02.2008
*!*			Locate For Upper(Alltrim(schema))== gcs
			Locate For id_firma = gnIdFirma
*!* 11.02.2008 ^			
			lcschemaParola=Alltrim(v_Firme.parola)
			loStart_Firma.nnrluna = gnLuna
			loStart_Firma.cAn = Alltrim(Str(gnAn))
			lnschema_noua = schema_noua
			lnIdFirma = v_firme.id_firma

			lnSucces = schimba_firma(gnHandle,gcs,lnIdFirma,lcschemaParola,lnschema_noua)
			If lnSucces<0
				aMessagebox(goExecutor.cEroare, 0+16, 'Eroare')
				Return
			Endif
			*---	&&lansare
		Endif

		*****************************Sfarsit Lansare toolbar*****************************
		*!*	modificare 01.03.2007
		*!*	am pus conditia TYPE("loStart_Firma") # "O"	 pentru cazul in care utilizatorul apasa "X"
		*!*	atunci cand este vizibila fereastra de alegere a firmei/lunii/anului >> apare "Doriti sa iesiti din program"
		*!*	si daca apasa da,atunci gnButon = 1 din fereastra de confirmare si nu de la fereastra de alegere a firmei
		If Type("loStart_Firma.nnrluna") = "U"	And gnbuton = 1
			Return To Master
		Endif
		*!*	modificare 01.03.2007	^
		If gnbuton <> 1
			*			QUIT
			Cancel
			*RETRY
			*RETURN
			If Buton = 2
				Do Start_Firma
			Endif
		Endif

		Select v_Firme
		*!*	modificare	17.12.2008
		Locate for id_firma = gnIdFirma
		*!*	modificare	17.12.2008	^		
		Scatter Name goFirma && variabila globala
		
		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', ALLTRIM(Strtran(Alltrim(gofirma.cod_fiscal), 'RO','')))
		Endif

		lnId_Firma = v_Firme.id_firma
		*lnLuna = ROUND(VAL(loStart_Firma.cLuna),0)
		lnLuna = loStart_Firma.nnrluna
		lnAn = Round(Val(loStart_Firma.cAn),0)
		lcFirma = Upper(Alltrim(v_Firme.firma))
		lcSchema = Upper(Alltrim(v_Firme.schema))
		lcschemaParola = Upper(Alltrim(v_Firme.parola))
		lnschema_noua = v_Firme.schema_noua

		*!*			IF USED('v_ancalendar')
		*!*				USE IN v_ancalendar
		*!*			ENDIF
		*!*			IF USED('crs_ani')
		*!*				USE IN crs_ani
		*!*			ENDIF



		If Empty(lcSchema) Or Isnull(lcSchema)
			*!*				DO mesaj WITH "Firma "+ lcFirma + " nu are definita schema in <NOM_FIRME>",""
			lcMesaj="Firma "+ lcFirma + " nu are definita schema in <NOM_FIRME>!"
			aMessagebox(lcMesaj,0+48,"Atentie")
			Loop
		Endif

*!* 11.02.2008
*!*			If gcs <> lcSchema
		IF lnId_Firma <> goFirma.id_firma
			LLSCHIMB = .T.
		Else
			LLSCHIMB = .F.
		Endif
*!* 11.02.2008 ^

		gcs = lcSchema
		lnSucces =  1
		If LLSCHIMB
			lnSucces = schimba_firma(gnHandle,gcS,lnId_Firma,lcSchema,lnschema_noua)
			*DO schimba_firma WITH gnHandle,GCS,lcschemaParola
			If lnSucces < 0
				aMessagebox('Firma nu s-a putut schimba!'+Chr(13)+Chr(13)+'ostartfirma.prg',0+16)
				Return
			Endif

		Endif

		If lnSucces > 0
			lnSucces = update_calendar()
		Endif



		llLunaBuna = .F.
		If !Used('calendar')
			*!*				DO mesaj WITH 'Nu s-a putut deschide calendarul firmei ' + lcFirma,''
			lcMesaj="Nu s-a putut deschide calendarul firmei "+ lcFirma + "!"
			aMessagebox(lcMesaj,0+48,"Atentie")
			Loop
		Endif
		*** verific daca luna aleasa e deschisa in calendar
		Select Min(Val(an)*12 + Val(nl)) As minluna,Max(Val(an)*12 + Val(nl)) As maxluna From calendar Into Cursor crsCalendar
		Select crsCalendar
		lnMaxLuna = maxluna
		lnMinLuna = minluna
		lnAnLuna = lnAn * 12+ lnLuna
		Use In crsCalendar

		lnLunaMax = Mod(lnMaxLuna,12)
		lnAnMax = Int(lnMaxLuna/12)
		If lnLunaMax = 0 And lnAnMax <> 0
			lnLunaMax = 12
			lnAnMax = lnAnMax - 1
		Endif

		lnLunaMin = Mod(lnMinLuna,12)
		lnAnMin = Int(lnMinLuna/12)
		If lnLunaMin = 0 And lnAnMin <> 0
			lnLunaMin = 12
			lnAnMin = lnAnMin - 1
		Endif

		Select calendar
		Locate For Val(nl) = lnLuna And Val(an) = lnAn
		If Found()
			llLunaBuna = .T.
			Scatter Name goCalendar

			If lnMaxLuna = lnAnLuna
				glUltimaLuna = .T.
			Endif
			If lnMinLuna = lnAnLuna
				glPrimaLuna = .T.
			Endif

			glLunaBuna = llLunaBuna


			lnSucces = OINIT_OPTIUNI()
			If lnSucces > 0
				Do update_nomenclator.prg
				Do ovariabile_globale.prg
				lcSql = [begin pack_contafin.set_idutil(?gnIdUtil); pack_contafin.set_id_sucursala(?gnIdSucursala); pack_sesiune.setan(?gnAn); pack_sesiune.setluna(?gnLuna);end;]
				lnSucces = goExecutor.oExecute(m.lcSql)  
			Endif

			Return lnSucces

		Else


			Do Case
				Case lnAnLuna > lnMaxLuna And lnMaxLuna <> 0
					lcMesaj = [Ultima luna deschisa este ] + Alltrim(Str(lnLunaMax)) + [/] + Alltrim(Str(lnAnMax))
					gnLuna = lnLunaMax
					gnAn = lnAnMax
					pcNl = Padl(Alltrim(Str(gnLuna)),2,'0')
					pcAn = Alltrim(Str(gnAn))
					m.nl = m.pcnl
					m.an = m.pcAn
				Case lnAnLuna < lnMinLuna And lnMinLuna <>0
					lcMesaj = [Prima luna deschisa este ] + Alltrim(Str(lnLunaMin)) + [/] + Alltrim(Str(lnAnMin))
					gnLuna = lnLunaMin
					gnAn = lnAnMin
					pcNl = Padl(Alltrim(Str(gnLuna)),2,'0')
					pcAn = Alltrim(Str(gnAn))
					m.nl = m.pcnl
					m.an = m.pcAn					
				Otherwise
					lcMesaj = [Luna ] + Alltrim(Str(lnLuna)) + [ / ] + Alltrim(Str(lnAn)) + [ nu este deschisa in calendar]
			Endcase
			*!*				DO mesaj WITH lcMesaj,''
			aMessagebox(lcMesaj,0+48,"Atentie")
			Loop
		Endif
	Enddo
Endproc
******************************************* SFARSIT: Start_Firma *******************************************


* PROCEDURE login(  )
* Date : 18/10/2004, 13:14:01
* author : marius.mutu
* description:

****** PARAMETER BLOCK **************
* Parameters : 0
*
******************************************* INCEPUT:login *******************************************
Procedure login(  )
	*LPARAMETERS tcHost,tcUserName,tcPassword,tcUserNameApp,tcPasswordApp,tnIdUtil,tnIdProgram

	Do Form frm_login

Endproc
******************************************* SFARSIT: login *******************************************


******************************************* INCEPUT:schimba_firma *************************************
Procedure  schimba_firma
	Lparameters tnHandle,tcSchema, tnIdFirma,tcschemaParola,tnschema_noua
	Local llDeconectez,lcuser, lnSucces, lnIdFirma
	LOCAL lnSucces
	lnSucces = 1

	IF TYPE('goFirma') = 'O'
		lnIdFirma = goFirma.id_firma
	ELSE 
		lnIdFirma = -1
	ENDIF 	
*!* 16.02.2009
*!* aveam probleme :)
*!* ma deconectez/conectez la fiecare apel
*!*		IF tnIdFirma <> lnIdFirma
*!*			llDeconectez = .T.
*!*		ENDIF
	llDeconectez = .T.
*!* 16.02.2009 ^	

	If llDeconectez
		goConn.Disconnect(tnHandle)

		If tnschema_noua = 0
			lcHost = gcHost
			lcuser = gcUserName
			lcPassword = gcPassword
		Else
			lcHost = gcHost
			lcuser = tcSchema
			lcPassword = tcschemaParola
		Endif

		lnHandle = goConn.Connect(lcHost, lcuser, lcPassword)
		goLog.Log('CONECTARE ' + lcHost + ' ' + lcUser + ' ID_FIRMA ' + STR(lnIdFirma) + ' HANDLE = ' + TRANSFORM(lnHandle), PROGRAM())

		If lnHandle < 0
			lnSucces = -1
		Else
			lnSucces = 1
		Endif
		gcs = tcSchema

	Endif && llDeconectez


	Return lnSucces

Endproc
******************************************* SFARSIT:schimba_firma *************************************
