*!* oproceduri_comune_imob
*!* 12.07.2006
*!* marius.mutu

*!* 27.03.2013
*!* marius.mutu
*!* GET_CURSOR_INCHIDERE_AMORTIZARI
*!* DEFALCARE SUME CHELTUIELI IN DEDUCTIBILE, NEDEDUCTIBILE IN FUNCTIE DE AMORT_MAX_DEDUCTIBL

*!* 09.04.2013
*!* marius.mutu
*!*	GET_CURSOR_INCHIDERE_AMORTIZARI
*!*	s-au tratat imobilizarile fara cont

*!* 24.06.2020
*!* Inchiderea de amortizari se face folosind modul experimental, din view
*!* In ROACONT nu am mod experimental, asa ca implicit este .T.

*!*	23.01.2024
*!*	GET_CURSOR_INCHIDERE_AMORTIZARI : defalcare pe lucrari

*!* 18.05.2026
*!* GET_CURSOR_INCHIDERE_AMORTIZARI - amort_max_deductibila = NULL,0,>0 pentru diferentierea cazurilor in care toata amort_max_deductibila=0 => amortizarea lunara este total nedeductibila

*!* 25.05.2026
*!* GET_CURSOR_INCHIDERE_AMORTIZARI = corectat suma nedeductibila pentru cazul in care rata < amort_max_deductibila. punea nedeductibil = rata

*** tcCursor = GET_CURSOR_AMORTIZARI (tnFiscala, tnAn, tnLuna)
*** tcCursor = GET_CURSOR_INCHIDERE_AMORTIZARI (tnTip, tnSectie, tnAn, tnLuna, tcCursor)

*----------------------------------------------------
Procedure GET_CURSOR_AMORTIZARI
	Lparameters tnFiscala, tnAn, tnLuna, tnIdTipImobilizare, tnIdMf

	Local lnSucces, lcSel, lcCursor, llExperimental
	Local lcFiltru, llSucces
	Private pdDataI, pdDataF, pcCond, pnFiscala
	llExperimental = .T.
	IF TYPE('goApp.nExperimental') = 'N'
		llExperimental = .T. && 25.11.2021 llExperimental = (goApp.nExperimental = 1)
	ENDIF 	

	pdDataI = Date(tnAn, tnLuna, 1)
	pdDataF = pdDataI
	*pcCond = [id_tip_imobilizare = ] + ALLTRIM(STR(tnTip)) && merge fffff greu

	pcCond = [2=2]
	lcFiltru = IIF(!EMPTY(m.tnIdTipImobilizare), [id_tip_imobilizare = ] + Alltrim(Str(m.tnIdTipImobilizare)), [1=1]) + m.gcCondSucursala + IIF(!EMPTY(m.tnIdMf), [ and id_mf = ] + ALLTRIM(STR(m.tnIdMf)), []) && merge normal in mod experimental cu selectie din view imob_vsituatie_lunara
	If Empty(tnFiscala)
		pnFiscala = 0
	Else
		pnFiscala = tnFiscala
	Endif
	lcCursor = 'crsAmortizareTemp'
	WAIT WINDOW  'Selectie amortizari '  + PADL(INT(m.tnLuna), 2, '0') + '/' + ALLTRIM(STR(INT(m.tnAn))) + ' ...' NOWAIT 
	If m.llExperimental
		* EXPERIMENTAL SELECTIE DIN IMOB_VSITUATIE_LUNARA IN LOC DE PACK_IMOB.CALCUL_SITUATIE_LUNARA()
		* Setez luna pentru view

		llSucces = goExecutor.oExecuta('begin pack_imob.setlunacurenta(?pdDataI); end;')
		If m.llSucces
			lcSel = [SELECT * FROM ] + Iif(m.pnFiscala = 0, [imob_vsituatie_lunara], [imobf_vsituatie_lunara]) + [ WHERE ] + m.lcFiltru
			llSucces = goExecutor.oExecuta(m.lcSel, m.lcCursor)
		Endif
	Else
		lcSel = [{call pack_imob.CALCUL_SITUATIE_LUNARA(?pdDataI,?pdDataF,?pcCond,?pnFiscala,?gnIdSucursala)}]
		llSucces = goExecutor.oExecuta(lcSel, lcCursor)
	Endif

	If !m.llSucces
		lcCursor = ''
	Endif
	Return m.lcCursor

Endproc && GET_CURSOR_AMORTIZARI

*----------------------------------------------------
Procedure GET_CURSOR_INCHIDERE_AMORTIZARI
	Lparameters tnTip, tnSectie, tnLucrare, tnAn, tnLuna, tcCursor

	Local lnFiscal, lcCursor, lcSqlSectie, lnAnLuna, lnSucces
	Local lcCursorPlcont, lcCursorTemp, lcSql
	lnFiscal = 0 && amortizare contabila
	lcCursor = ""

	lcCursor = GET_CURSOR_AMORTIZARI(m.lnFiscal, m.tnAn, m.tnLuna, m.tnTip)
	If Empty(lcCursor)
		Return ''
	Endif

	&& CORESPONDENTE CONTURI INCHIDERE
	&& DACA NU SUNT COMPLETATE - SE FOLOSESC CONTURI DEFAULT
	*** ADAUG LINIILE PENTRU SUME DEDUCTIBILE (TIP = 1)
	Create Cursor crsConturi (TIP N(1), Id N(10), id_sectie I Null, Cont c(4), acont c(4) Null, scd c(4)  Null, ascd c(4)  Null, scc c(4)  Null, ascc c(4)  Null)

	lcSql = "select 1 as tip, id, id_sectie, cont, acont, scd, ascd, scc, ascc from imob_inchidere"
	llSucces = goExecutor.oExecuta(lcSql, "crsConturiTemp")
	If m.llSucces 
		Insert Into crsConturi (tip, id, id_sectie, cont, acont, scd, ascd, scc, ascc) ;
			select tip, Id, id_sectie, cont, acont, scd, ascd, scc, ascc From crsConturiTemp
	Endif

*!*		*** COMPLETEZ CONTURILE CARE NU SUNT CONFIGURATE IN IMOB_INCHIDERE
*!*		Insert Into crsConturi (TIP, Cont, acont) ;
*!*			select Distinct 1 As TIP, Cont, acont ;
*!*			from (m.lcCursor) Where !Empty(Nvl(Cont, '')) And Padr(Nvl(Cont, ''), 4, ' ') + Padr(Nvl(acont, ''), 4, ' ') Not In ;
*!*			(Select Padr(Nvl(Cont, ''), 4, ' ') + Padr(Nvl(acont, ''), 4, ' ') From crsConturi)

*!*		&& COMPLETEZ SCD, SCC DEFAULT
*!*		Update crsConturi Set scd = '6811' Where Empty(scd)
*!*		Update crsConturi Set scc = Iif(tnTip = 1, '281', '280') + Substr(Cont, 3, 1) Where Empty(scc)

	*** ADAUG LINIILE PENTRU SUME NEDEDUCTIBILE (TIP = 2)
	Insert Into crsConturi (tip, id_sectie, Cont, acont, scd, ascd, scc, ascc) ;
		select 2 As tip, id_sectie, Cont, acont, scd, ascd, scc, ascc ;
		from crsConturi

	lnAnLuna = tnAn * 12 + tnLuna;

	lcSql = [select cont, acont, explicatie from  plcont where inactiv = 0 and an = ] + Transform(tnAn)
	lcCursorPlcont = [crsPlcontTemp]
	lnSucces = goExecutor.oExecute(lcSql, lcCursorPlcont)
	If lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare, 0 + 16, 'Eroare')
	Endif
	If !Used("crsPlContTemp")
		Create Cursor crsPlContTemp (Cont c(4), acont c(4), explicatie c (30))
	Endif

	*** CUMULARE PE SECTII sau pe lucrari
	m.lcCursorTemp = 'crsAmortizariTemp'
	
	* amort_max_deductibil poate sa fie NULL, 0, > 0. daca = NULL, atunci toata suma este deductibila, daca = 0, atunci toata suma este nedeductibila       
	Select Sum(Cast(Iif(c.TIP = 1, ;
	          Iif(ISNULL(a.amort_max_deductibil), a.rata, ;
	              IIF(a.amort_max_deductibil > 0, ;
	                  IIF(a.rata < a.amort_max_deductibil, a.rata, a.amort_max_deductibil), ;
	                  0)), ;
	          iif(ISNULL(a.amort_max_deductibil), 0, ;
	              IIF(a.amort_max_deductibil = 0, a.rata, ;
	                  IIF(a.rata > a.amort_max_deductibil, a.rata - a.amort_max_deductibil, 0)))) ;
	        As N(20, 4))) As suma, ;
		a.Cont, a.acont, Nvl(a.id_sectie, 00000) As id_sectie, Nvl(a.sectie, Space(50)) As sectie, ;
		CAST(Nvl(a.id_lucrare, 0) as I) As id_lucrare, CAST(Nvl(a.nrord, '') as C(100)) As nrord, ;
		IIF(!Empty(Nvl(c.Cont, "")), c.scd, Space(4)) As scd, ;
		IIF(!Empty(Nvl(c.Cont, "")), c.ascd, Space(4)) As ascd, ;
		IIF(!Empty(Nvl(c.Cont, "")), c.scc, Space(4)) As scc, ;
		IIF(!Empty(Nvl(c.Cont, "")), c.ascc, Space(4)) As ascc, ;
		padr(Alltrim(Nvl(a.Cont, '')) + Iif(!Empty(Nvl(a.acont, '')), '.' + Nvl(a.acont, ''), '') + ' ' + Alltrim(Nvl(p.explicatie, '')) + Iif(c.TIP = 1, ' - DEDUCTIBIL', ' - NEDEDUCTIBIL'), 200, ' ') As explicatie ;
		from (m.lcCursor) a ;
		left Join crsPlContTemp p On a.Cont = p.Cont And Nvl(a.acont, 'xxxx') = Nvl(p.acont, 'xxxx') ;
		left Join crsConturi c On a.Cont = c.Cont And Nvl(a.acont, 'xxxx') = Nvl(c.acont, 'xxxx') AND NVL(a.id_sectie, 0) = NVL(c.id_sectie,0) ;
		where id_tip_imobilizare = tnTip ;
		group By a.Cont, a.acont, a.id_sectie, a.sectie, a.id_lucrare, a.nrord, p.explicatie, c.Cont, c.scd, c.ascd, c.scc, c.ascc, c.TIP ;
		into Cursor crsAmortizariTemp Readwrite

	DO CASE 
		*** CUMULARE SUME PE SECTII SI LUCRARI
		CASE tnSectie = 1 AND tnLucrare = 1
			Select Cont, acont, scd, ascd, scc, ascc, explicatie, id_sectie, sectie, id_lucrare, nrord, Sum(suma) As suma ;
				from (m.lcCursorTemp) ;
				group By Cont, acont, scd, ascd, scc, ascc, explicatie, id_sectie, sectie, id_lucrare, nrord ;
				into Cursor (m.tcCursor) Readwrite
				
		*** CUMULARE SUME FARA SECTII, PE LUCRARI
		CASE tnSectie <> 1 AND tnLucrare = 1 && nu pe sectii
			Select Cont, acont, scd, ascd, scc, ascc, explicatie, 0 As id_sectie, '' As sectie, id_lucrare, nrord, Sum(suma) As suma ;
				from (m.lcCursorTemp) ;
				group By Cont, acont, scd, ascd, scc, ascc, explicatie, id_lucrare, nrord ;
				into Cursor (m.tcCursor) Readwrite

		*** CUMULARE SUME PE SECTI, FARA LUCRARI
		CASE tnSectie = 1 AND tnLucrare <> 1 && nu pe sectii
			Select Cont, acont, scd, ascd, scc, ascc, explicatie, id_sectie, sectie, 0 as id_lucrare, '' as nrord, Sum(suma) As suma ;
				from (m.lcCursorTemp) ;
				group By Cont, acont, scd, ascd, scc, ascc, explicatie, id_sectie, sectie ;
				into Cursor (m.tcCursor) Readwrite

		*** CUMULARE SUME FARA LUCRARI, FARA SECTI
		CASE tnSectie <> 1 AND tnLucrare <> 1 && nu pe sectii
			Select Cont, acont, scd, ascd, scc, ascc, explicatie, 0 AS id_sectie, '' AS sectie, 0 as id_lucrare, '' as nrord, Sum(suma) As suma ;
				from (m.lcCursorTemp) ;
				group By Cont, acont, scd, ascd, scc, ascc, explicatie ;
				into Cursor (m.tcCursor) Readwrite
				
	ENDCASE 			
	Use In (Select(m.lcCursorTemp))


	&& sterg sumele 0
	Delete From (tcCursor) Where suma = 0

	Use In (Select(m.lcCursor))
	Use In (Select(m.lcCursorPlcont))

	If Used(m.tcCursor)
		Return m.tcCursor
	Else
		Return ''
	Endif

Endproc && GET_CURSOR_INCHIDERE_AMORTIZARI
