*!* 23.09.2013
*!* marius.mutu
*!* inchidere_tva
*!* fac notele de exigibilizare TVA 90 zile pentru toate firmele, nu numai pentru cele din sistemul TVA Incasare
*!* ex: sunt inregistrate facturi din greseala cu TVA Incasare sau firma a fost cu TVA Incasare

*!* 04.02.2014
*!* marius.mutu
*!* inchidere_tva: nu se mai fac notele de exigibilizare TVA 90 zile de la 01/2014 (COD FISCAL)

*!* 18.05.2014
*!* marius.mutu
*!* inchidere_venituri_cheltuieli mutate in pack_inchideri 609, 709 tratate corect

*!* 25.06.2014
*!* marius.mutu
*!* inchidere_tva_sold - se completeaza si id_sucursala in note, altfel nu se poate modifica nota

*!* 25.08.2014
*!* marius.mutu
*!*	inchidere_tva_sold: apelez defalca_tva_incasare pentru fiecare cota, daca soldul neexigibil total este 0


*!* 21.01.2015
*!* marius.mutu
*!*	inchidere_tva_sold: Daca din diferite motive nu se poate calcula TVA exigibil, pun o linie cu ID_FACT-ul facturii originale, sa se poata completa manual

*!* 16.12.2015
*!* marius.mutu
*!*	inchidere_tva_sold: cota tva 20%

*!* 24.03.2020
*!* marius.mutu: inchidere_venchelt - completare analitic 121 din optiuni

*!*	21.07.2020
*!*	marius.mutu
*!*	inchidere_amortizari_completare_conturi - tratare id_sectie = 0

*!*	23.01.2024
*!*	inchidere_amortizari: defalcare inchidere pe lucrari

*** =====================================================================
Procedure popriri
	If gnNOTASAL = 5

		Local lcCursor, llVerificAnalitic, llCompletareParteneri
		lcSel = [select a.sumapop,a.id_poprire,a.id_stat,] + ;
			[(select id_part from sal_personal where id_salariat in ] + ;
			[ (select id_persoana from sal_stat where id_stat = a.id_stat)) as id_part, ] + ;
			[ c.scd,c.ascd,c.scc,c.ascc,c.nume_popr, ] + ;
			[ (select denumire from nom_parteneri where id_part in ] + ;
			[  (select id_part from sal_personal where id_salariat in (select id_persoana from sal_stat  where id_stat = a.id_stat))) as denumire, ] + ;
			[ e.id_sectie,e.SECTIE,f.sucursala,f.id_sucursala,0000000000 as id_factc,0000000000 as perechec ] + ;
			[ from sal_popriri a ] + ;
			[ left join sal_nom_popriri c on a.id_poprire = c.id_popriri ] + ;
			[ left join sal_stat d on a.id_stat = d.id_stat ] + ;
			[ left join sal_vnom_formatii e on e.id_formatia = d.id_formatie ] + ;
			[ left join vnom_sectii f on e.id_sectie = f.id_sectie ] + ;
			[ where a.sters = 0 and a.id_stat in (select b.id_stat from sal_stat b ] + ;
			[ where b.an = pack_sesiune.getan() and b.luna = pack_sesiune.getluna()) ]
		*!*				 + ;
		*!*				[ and c.scc not in (select cont from config_cont_ireg where cu_inregistrari = 1)]

		If !Isnull(gnIdSucursala)
			lcSel = m.lcSel + [ and f.id_sucursala = ?gnIdSucursala ]
		Endif

		lcCursor = 'crs_popr'
		lnSucces = goExecutor.oExecute(m.lcSel, m.lcCursor)
		If m.lnSucces < 0
			AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
		Endif

		*!*		lcSel = [select * from config_cont_ireg a where a.cont in (select b.scc from sal_nom_popriri b ]+;
		*!*	       [ where b.id_popriri in (select c.id_poprire from sal_popriri c where c.sumapop > 0 and ]+;
		*!*	            [ id_stat in (select id_stat from sal_stat where an = pack_sesiune.getan() and luna = pack_sesiune.getluna()))) and cu_inregistrari = 1 ]
		*!*
		*!*		lcCursor = 'crs_conturi'
		*!*		lnSucces = goExecutor.oExecute(lcSel,lcCursor)
		*!*		IF lnSucces < 0
		*!*			AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
		*!*		EndIf
		*!*
		*!*		Local lcFiltru
		*!*		Store '' to lcFiltru
		*!*		Select crs_conturi
		*!*		If Reccount() > 0
		*!*			Select crs_conturi
		*!*			Scan
		*!*				lcFiltru = lcFiltru +[,] +[']+Alltrim(crs_conturi.cont)+[']
		*!*			EndScan
		*!*			lcFiltru = Substr(lcfiltru,2)
		*!*			SELECT crs_popr
		*!*			lcFiltru  = [Set Filter To InList(Alltrim(scc),]+lcFiltru+[)]
		*!*			&lcFiltru
		*!*			ofrmpereche = CreateObject('frm_alege_pereche')
		*!*			ofrmpereche.show(1)
		*!*			If gnbuton = 2
		*!*				Return
		*!*			EndIf
		*!*		EndIf
		*!*		Use in crs_conturi
		Select crs_popr

		*!*		Browse
		Set Filter To

		Select crs_popr
		Select nume_popr As explicatia, id_part As id_partc, denumire As partc, scd, scc, Nvl(ascd, '') As ascd, Nvl(ascc, '');
			As ascc, sumapop As suma, id_sectie, sectie, id_sucursala, sucursala, id_factc, perechec From crs_popr Where sumapop <> 0 Into Cursor crsNota
		*!*			SELECT id_part as id_partd ,scd,sumapop as suma,id_sectie FROM crs_popr WHERE sumapop <> 0 INTO CURSOR crsNota READWRITE

	Else
		Local lcCursor, llVerificAnalitic, llCompletareParteneri
		lcSel = [select sum(a.suma) as sumapop,a.id_poprire,a.id_sectie,d.id_part,] + ;
			[c.scd,c.ascd,c.scc,c.ascc,c.nume_popr,] + ;
			[ (select denumire from nom_parteneri where id_part =d.id_part) as denumire ] + ;
			[ from (	select sum(a.sumapop) as suma,a.id_poprire,c.id_sectie from sal_vpopriri a ] + ;
			[	 left join sal_stat b on a.id_stat = b.id_stat ] + ;
			[	 left join sal_nom_formatii c on b.id_formatie = c.id_formatia ] + ;
			[	 group by a.id_poprire,c.id_sectie ) a ] + ;
			[      left join sal_nom_popriri c on a.id_poprire = c.id_popriri ] + ;
			[ 	   left join sal_part_popriri_sectii d on d.id_poprire = a.id_poprire and d.id_sectie = a.id_sectie ] + ;
			[  	   where c.scc <> '542'] + ;
			[    group by a.id_poprire,a.id_sectie,d.id_part,c.scd,c.ascd,c.scc,c.ascc,c.nume_popr]

		lcCursor = 'crs_popr'
		lnSucces = goExecutor.oExecute(m.lcSel, m.lcCursor)
		If m.lnSucces < 0
			AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
		Endif

		Select crs_popr


		lcCursor = 'crs_popr'
		lnSucces = goExecutor.oExecute(m.lcSel, m.lcCursor)
		If m.lnSucces < 0
			AMESSAGEBOX('Call pack_salarii.nota_popriri ' + Chr(13) + goExecutor.cEroare, 0 + 16, "Eroare")
		Endif


		Select crs_popr
		Select nume_popr As explicatia, id_part As id_partc, scd, scc, Nvl(ascd, '') As ascd, Nvl(ascc, '') As ascc, sumapop As suma, id_sectie From crs_popr Where sumapop <> 0 Into Cursor crsNota
		*!*			SELECT id_part as id_partd ,scd,sumapop as suma,id_sectie FROM crs_popr WHERE sumapop <> 0 INTO CURSOR crsNota READWRITE
	Endif
	Use In crs_popr
	lcCursor = [crsNota]
	Select &lcCursor

	If !Used(m.lcCursor) Or Empty(m.lcCursor)
		Return
	Endif

	llStergeNotaPrecedenta = .T.
	lnId_Set = 90014
	lnCod = 0
	llVerificAnalitic = .T.
	llCompletareParteneri = .T.

	If m.llStergeNotaPrecedenta
		lnSucces = sterge_nota(m.lnCod, m.lnId_Set)
	Endif


	If m.lnSucces > 0
		lnSucces = scrie_nota_import(m.lnId_Set, m.llVerificAnalitic, m.llCompletareParteneri, m.lcCursor)
	Endif

	If Used(m.lcCursor)
		Use In (m.lcCursor)
	Endif

Endproc && popriri

*** =====================================================================
Procedure Achitare_drepturi_neridicate_Lichidare
	Dimension laValori[2, 3]
	laValori[1, 1] = "poAct.id_vv426"
	laValori[1, 2] = Alltrim(Str(gnID_PARTDRNERIDLIC))
	laValori[1, 3] = !Empty(gnID_PARTDRNERIDLIC)

	lcSel = [select denumire from nom_parteneri where id_part = ] + Alltrim(Str(gnID_PARTDRNERIDLIC))
	lnSucces = goExecutor.oExecute(m.lcSel, [crsnumepart])
	If m.lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
	Endif

	laValori[2, 1] = "poAct.v426"
	laValori[2, 2] = Alltrim(crsnumepart.denumire)
	laValori[2, 3] = !Empty(gnID_PARTDRNERIDLIC)
	Use In crsnumepart
	lans(90011, .F., .T., @laValori)
	*!*	MESSAGEBOX(buton)
	If gnButon = 1

		If Used('crsStatDePastrat')
			Select crsStatDePastrat
			Scan For bifa > 0
				Scatter Name ost
				lcSel = [update sal_stat set ACHITLICNERID = 1 where id_stat = ] + Alltrim(Str(ost.id_stat))
				lnSucces = goExecutor.oExecute(m.lcSel)
				If m.lnSucces < 0
					AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
				Endif
				Release ost
			Endscan
			Use In crsStatDePastrat
		Endif
	Endif
Endproc && Achitare_drepturi_neridicate_Lichidare

*** =====================================================================
Procedure Achitare_drepturi_neridicate_Avans
	Dimension laValori[2, 3]
	laValori[1, 1] = "poAct.id_vv426"
	laValori[1, 2] = Alltrim(Str(gnID_PARTDRNERIDAV))
	laValori[1, 3] = !Empty(gnID_PARTDRNERIDAV)

	lcSel = [select denumire from nom_parteneri where id_part = ] + Alltrim(Str(gnID_PARTDRNERIDAV))
	lnSucces = goExecutor.oExecute(m.lcSel, [crsnumepart])
	If m.lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
	Endif

	laValori[2, 1] = "poAct.v426"
	laValori[2, 2] = Alltrim(crsnumepart.denumire)
	laValori[2, 3] = !Empty(gnID_PARTDRNERIDAV)
	Use In crsnumepart
	lans(90013, .F., .T., @laValori)
	*!*	MESSAGEBOX(buton)
	If gnButon = 1

		If Used('crsStatDePastrat')
			Select crsStatDePastrat
			Scan For bifa > 0
				Scatter Name ost
				lcSel = [update sal_stat set ACHITAVNERID = 1 where id_stat = ] + Alltrim(Str(ost.id_stat))
				lnSucces = goExecutor.oExecute(m.lcSel)
				If m.lnSucces < 0
					AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
				Endif
				Release ost
			Endscan
			Use In crsStatDePastrat
		Endif
	Endif
Endproc && Achitare_drepturi_neridicate_Avans


***-------------------------------------------------------------------------------------------------------------------
Procedure drepturi_nerid_sal



	Private postat
	Store [] To postat

	&&selectie din state si bifarea sumelor pentru care se face trecerea
	pcschema = ['id_stat i,an n(4),luna n(2),NUME C(50),PRENUME C(50),SECTIE C(30),FORMATIE C(64),MARCA n(10),lichidare n(16), bifa n(1),SUMALICNERID n(16)']

	pcselect = [select a.id_stat,a.an,a.luna,a.nume,a.prenume,a.sectie,a.formatia,a.marca,a.lichidare,ROUND(a.BIFALICNERID,0) as bifa,a.SUMALICNERID from sal_vstattot a where 1=2]



	pcfiltru =  [ 1=2]
	pcorder = [a.sectie,a.nume,A.PRENUME]
	llAfiseaza = .F.
	gencursor('postat', 'crsState', pcselect, pcfiltru, pcschema, pcorder, llAfiseaza)
	postat.ca_baza1.afisare()

	Select crsState
	ofrmSelect = Createobject('frm_alege_neridicati')
	ofrmSelect.Show(1)
	If buton = 2
		Return
	Endif

	Select crsState
	Set Filter To
	Locate
	Select  Sum(Nvl(SUMALICNERID, 0)) As suma From crsState Where bifa > 0 Into Cursor crsSuma

	Select crsSuma
	&& / selectie din state si bifarea sumelor pentru care se face trecerea

	&& nota contabila
	Select xsets




	llStergeNotaPrecedenta = .F.
	lnId_Set = 90010
	lnCod = 0
	llVerificAnalitic = .T.
	llCompletareParteneri = .T.

	If m.llStergeNotaPrecedenta
		lnSucces = sterge_nota(m.lnCod, m.lnId_Set)
	Endif


	Select suma From crsSuma Into Cursor crsNota

	Use In crsSuma

	*!*	DEBUG
	*!*	SUSPEND
	lcCursor = [crsNota]
	Select &lcCursor

	If !Used(m.lcCursor) Or Empty(m.lcCursor)
		Return
	Endif
	*!*	scrie_nota_import( tnId_Set, tlVerificAnalitic, tlCompletareParteneri, tcCursorImport,taValori )

	Dimension laValori[2, 3]
	laValori[1, 1] = "poAct.id_vv426"
	laValori[1, 2] = Alltrim(Str(gnID_PARTDRNERIDLIC))
	laValori[1, 3] = !Empty(gnID_PARTDRNERIDLIC)

	lcSel = [select denumire from nom_parteneri where id_part = ] + Alltrim(Str(gnID_PARTDRNERIDLIC))
	lnSucces = goExecutor.oExecute(m.lcSel, [crsnumepart])
	If m.lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
	Endif

	laValori[2, 1] = "poAct.v426"
	laValori[2, 2] = Alltrim(crsnumepart.denumire)
	laValori[2, 3] = !Empty(gnID_PARTDRNERIDLIC)
	Use In crsnumepart
	Select crsNota
	lcCursor = [crsNota]
	lnSucces = scrie_nota_import(m.lnId_Set, m.llVerificAnalitic, m.llCompletareParteneri, m.lcCursor, @laValori)

	If Used(m.lcCursor)
		Use In (m.lcCursor)
	Endif

	&& -- scriu in statele din luna respectiva ca au fost trecute pe drepturi neridicate
	If buton = 2
		Return
	Endif
	If m.lnSucces  > 0
		Select crsState
		Scan For bifa > 0
			Scatter Name ost
			Private pnIdfact
			Store 0 To pnIdfact
			lcSel = [select pack_contafin.GET_IDFACT() as id_fact from dual ]
			lnSucces = goExecutor.oExecute(m.lcSel, [crsIdFact])
			If m.lnSucces < 0
				AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
			Endif
			Select crsIdFact
			Scatter Name oidfact

			lcSel = [update sal_stat set BIFALICNERID = 1, SUMALICNERID = ] + Alltrim(Str(ost.SUMALICNERID)) + [,id_fact_lic = ] + Alltrim(Str(oidfact.id_fact)) + [ where id_stat = ] + Alltrim(Str(ost.id_stat))
			lnSucces = goExecutor.oExecute(m.lcSel)
			If m.lnSucces < 0
				AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
			Endif
			Use In crsIdFact
			Release oidfact
		Endscan
	Endif
	Use In crsState
	Release ofrmSelect
Endproc

***---------------------------------------------------------------------------------------------------------------------

Procedure aleg_salnerid_de_achit
	Parameters tcAlias

	Private pcselect, pcfiltru, pcschema, pcorder, postat
	Local lcTip, llTest, lnSuma
	Store 0 To lnSuma
	Store .F. To llTest
	Store "" To postat
	*Thisform.blocbuton1.terminat1.Enabled=.F.
	*!*		lunanan=gnLuna+gnAn*12
	*!*		Select actcv
	Private oireg
	Select (tcAlias)
	Scatter Name oireg
	Do Case
		Case poAct.id_set = 90011
			&&selectie din state si bifarea sumelor pentru care se face trecerea
			pcschema = ['id_stat i,an n(4),luna n(2),NUME C(50),PRENUME C(50),SECTIE C(30),FORMATIE C(64),MARCA n(10),lichidare n(16), bifa n(1),SUMALICNERID n(16)']

			pcselect = [select a.id_stat,a.an,a.luna,a.nume,a.prenume,a.sectie,a.formatia,a.marca,a.lichidare,ROUND(a.ACHITLICNERID,0) as bifa,a.SUMALICNERID from sal_vstattot a where 1=2]



			pcfiltru =  [ id_fact_lic = ] + Alltrim(Str(oireg.id_fact)) + [ and ACHITLICNERID = 0]

		Case poAct.id_set = 90013
			&&selectie din state si bifarea sumelor pentru care se face trecerea
			pcschema = ['id_stat i,an n(4),luna n(2),NUME C(50),PRENUME C(50),SECTIE C(30),FORMATIE C(64),MARCA n(10),lichidare n(16), bifa n(1),SUMALICNERID n(16)']

			pcselect = [select a.id_stat,a.an,a.luna,a.nume,a.prenume,a.sectie,a.formatia,a.marca,a.avans as lichidare,ROUND(a.ACHITAVNERID,0) as bifa,a.SUMAAVNERID as SUMALICNERID  from sal_vstattot a where 1=2]



			pcfiltru =  [ id_fact_av = ] + Alltrim(Str(oireg.id_fact)) + [ and ACHITAVNERID = 0]
	Endcase
	pcorder = [a.sectie,a.nume,A.PRENUME]
	llAfiseaza = .F.
	gencursor('postat', 'crsState', pcselect, pcfiltru, pcschema, pcorder, llAfiseaza)
	postat.ca_baza1.afisare()

	If !Used('crsStatDePastrat')
		Select * From crsState Where 1 = 2 Into Cursor crsStatDePastrat Readwrite
	Endif

	Select crsState
	ofrmSelect = Createobject('frm_alege_neridicati')
	If poAct.id_set = 90011
		ofrmSelect.noptiune = 2&& pentru achitarea lichidarii neridicate
	Else
		ofrmSelect.noptiune = 5&& pentru achitarea avansului neridicat
	Endif
	ofrmSelect.Show(1)
	If buton = 2
		Return
	Endif

	Select crsStatDePastrat
	Insert Into crsStatDePastrat Select * From crsState Where bifa = 1

	Use In crsState
	Select (tcAlias)
	Replace sumaachi With pnSumaBifata
	Release ofrmSelect
Endproc

***-------------------------------------------------------------------------------------------------------------------
Procedure drepturi_nerid_av



	Private postat
	Store [] To postat

	&&selectie din state si bifarea sumelor pentru care se face trecerea
	pcschema = ['id_stat i,an n(4),luna n(2),NUME C(50),PRENUME C(50),SECTIE C(30),FORMATIE C(64),MARCA n(10),lichidare n(16), bifa n(1),SUMALICNERID n(16)']

	pcselect = [select a.id_stat,a.an,a.luna,a.nume,a.prenume,a.sectie,a.formatia,a.marca,a.avans as lichidare,ROUND(a.BIFAAVNERID,0) as bifa,a.SUMAAVNERID as SUMALICNERID from sal_vstattot a where 1=2]



	pcfiltru =  [ 1=2]
	pcorder = [a.sectie,a.nume,A.PRENUME]
	llAfiseaza = .F.
	gencursor('postat', 'crsState', pcselect, pcfiltru, pcschema, pcorder, llAfiseaza)
	postat.ca_baza1.afisare()

	Select crsState
	ofrmSelect = Createobject('frm_alege_neridicati')
	ofrmSelect.noptiune = 3&& trecerea avansului in drepturi neridicare
	ofrmSelect.Show(1)
	If buton = 2
		Return
	Endif

	Select crsState
	Set Filter To
	Locate
	Select  Sum(Nvl(SUMALICNERID, 0)) As suma From crsState Where bifa > 0 Into Cursor crsSuma

	Select crsSuma
	&& / selectie din state si bifarea sumelor pentru care se face trecerea

	&& nota contabila
	Select xsets




	llStergeNotaPrecedenta = .F.
	lnId_Set = 90012
	lnCod = 0
	llVerificAnalitic = .T.
	llCompletareParteneri = .T.

	If m.llStergeNotaPrecedenta
		lnSucces = sterge_nota(m.lnCod, m.lnId_Set)
	Endif


	Select suma From crsSuma Into Cursor crsNota

	Use In crsSuma

	*!*	DEBUG
	*!*	SUSPEND
	lcCursor = [crsNota]
	Select &lcCursor

	If !Used(m.lcCursor) Or Empty(m.lcCursor)
		Return
	Endif
	*!*	scrie_nota_import( tnId_Set, tlVerificAnalitic, tlCompletareParteneri, tcCursorImport,taValori )

	Dimension laValori[2, 3]
	laValori[1, 1] = "poAct.id_vv426"
	laValori[1, 2] = Alltrim(Str(gnID_PARTDRNERIDAV))
	laValori[1, 3] = !Empty(gnID_PARTDRNERIDAV)

	lcSel = [select denumire from nom_parteneri where id_part = ] + Alltrim(Str(gnID_PARTDRNERIDAV))
	lnSucces = goExecutor.oExecute(m.lcSel, [crsnumepart])
	If m.lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
	Endif

	laValori[2, 1] = "poAct.v426"
	laValori[2, 2] = Alltrim(crsnumepart.denumire)
	laValori[2, 3] = !Empty(gnID_PARTDRNERIDAV)
	Use In crsnumepart
	Select crsNota
	lcCursor = [crsNota]
	lnSucces = scrie_nota_import(m.lnId_Set, m.llVerificAnalitic, m.llCompletareParteneri, m.lcCursor, @laValori)

	If Used(m.lcCursor)
		Use In (m.lcCursor)
	Endif

	&& -- scriu in statele din luna respectiva ca au fost trecute pe drepturi neridicate
	If buton = 2
		Return
	Endif
	If m.lnSucces  > 0
		Select crsState
		Scan For bifa > 0
			Scatter Name ost
			Private pnIdfact
			Store 0 To pnIdfact
			lcSel = [select pack_contafin.GET_IDFACT() as id_fact from dual ]
			lnSucces = goExecutor.oExecute(m.lcSel, [crsIdFact])
			If m.lnSucces < 0
				AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
			Endif
			Select crsIdFact
			Scatter Name oidfact

			lcSel = [update sal_stat set BIFAAVNERID = 1, SUMAAVNERID = ] + Alltrim(Str(ost.SUMALICNERID)) + [,id_fact_av = ] + Alltrim(Str(oidfact.id_fact)) + [ where id_stat = ] + Alltrim(Str(ost.id_stat))
			lnSucces = goExecutor.oExecute(m.lcSel)
			If m.lnSucces < 0
				AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
			Endif
			Use In crsIdFact
			Release oidfact
		Endscan
	Endif
	Use In crsState
	Release ofrmSelect
Endproc

***---------------------------------------------------------------------------------------------------------------------



Procedure inchidere_tva
	Lparameters tnTip
	*** tnTip: 1 = inchidere TVA (inclusiv TVA 4427 exigibil la 90 zile); 2 = TVA 4427 exigibil la 90 zile

	Local overif As 'verificare'
	Local lCompletareParteneri, lVerificAnalitic, lcCursor, lcCursorVerificare, lcSel
	Local lcSql, llSucces, lnBut, lnButon, lnSucces, loAct, lnIdSet
	*:Global gnButon

	Private pcSirDifAcont, pcSirDifPart, pnCod, pcExplicatia, pdDataIreg, pdDataAct, pnIdFdoc, pnNrAct, pnScrieDirect
	lnIdSet = 90004
	pnCod = 0
	llSucces = .T.
	lcCursorVerificare = [crsact]
	
	pnScrieDirect = 0 && nu mai scriu direct notele in ACT, ci las utilizatorul sa le modifice

	*** STERGERE NOTA INCHIDERE TVA 90004
	lcSql = [select * from vact where id_set = 90004 and an = ?gnAn and luna = ?gnLuna] + m.gcCondSucursala
	lcCursor = [actactan]

	lnSucces = goExecutor.oExecute(lcSql, m.lcCursor)
	goExecutor.oReset()
	If m.lnSucces < 0
		AMESSAGEBOX('Cautare Nota Inchidere TVA:' + Chr(13) + goExecutor.cEroare, 0 + 16, "Eroare")
		Return
	Endif

	Select actactan
	If Reccount() > 0
		Clear Class "verificare"
		overif = Createobject('verificare')
		overif.Show(1)
		If buton = 2
			Use In (Select('actactan'))
			Release overif
			Return m.llSucces
		Endif
		lnSucces = OSCRIE_IN_FISIERE(2, .F.)
		llSucces = (m.lnSucces > 0)
	Endif


	If m.llSucces
		lnButon = lans(m.lnIdSet)
		If m.lnButon = 2
			Return m.llSucces
		Endif
	Else
		Return m.llSucces
	Endif

	Select actactan
	Go Top
	Scatter Name loAct
	pnIdFdoc = loAct.id_fdoc
	pnNrAct = loAct.nract
	pdDataAct = loAct.dataact
	pdDataIreg = loAct.dataireg
	pcExplicatia = Alltrim(loAct.explicatia)
	pcSirDifAcont = ""
	pcSirDifPart = ""

	*** SCRIERE TVA EXIGIBIL 90 ZILE
	*** Valabila pana la 12/2013
	If m.llSucces And (m.gnAn * 12 + m.gnLuna <= 2013 * 12 + 12) && And (goCalendar.tva_incasare = 1)

		*** Am pus tranzactie manuala pentru ca dadea o eroare cand se deschideau cursoare bazate pe tabele temporara act_temp
		llSucces = goConn.BeginManualTransaction()
		Do While m.llSucces
			*** da frecvent fatal error cand transmit parametri cu ? in loc de valori - probabil bug in driverul de odbc
			*!*				lcSql = [{call pack_inchideri.note_inchidere_tva_90(?gnAn, ?gnLuna, ?gnIdSucursala, ?pnIdFdoc, ?gnIdUtil, ?pdDataIreg, ?pnNrAct, ?pcExplicatia, ?pcSirDifAcont, ?pcSirDifPart, ?@pnCod)}]
			TEXT To lcSql Noshow Textmerge
				{call pack_inchideri.note_inchidere_tva_90(<<gnAn>>, <<gnLuna>>, <<iif(isnull(gnIdSucursala), 'NULL', transform(gnIdSucursala))>>, <<pnIdFdoc>>, <<gnIdUtil>>, TO_DATE('<<dtos(pdDataIreg)>>', 'YYYYMMDD'), <<pnNrAct>>, '<<pcExplicatia>>', '<<pcSirDifAcont>>', '<<pcSirDifPart>>', ?@pnCod)}
			ENDTEXT

			llSucces = goExecutor.oExecuta(m.lcSql, m.lcCursorVerificare)
			If !m.llSucces && eroare
				Exit
			Endif


			If Reccount(m.lcCursorVerificare) = 0 && nu mai este nimic de completat
				Exit
			Endif

			*** Trebuie completate analitice, parteneri
			pcSirDifAcont = ""
			pcSirDifPart = ""
			llSucces = verificare_completare_act(m.lcCursorVerificare, .T., .T., @pcSirDifAcont, @pcSirDifPart)
			Use In (Select(m.lcCursorVerificare))
		Enddo && .T.
		llSucces = goConn.EndManualTransaction(Iif(m.llSucces, 'COMMIT', 'ROLLBACK'))
	Endif && goCalendar.tva_incasare = 1

	*** SCRIERE INCHIDERE TVA
	If m.llSucces And m.tnTip = 1
		pcSirDifAcont = ""
		pcSirDifPart = ""
		llSucces = goConn.BeginManualTransaction()
		
		Do While m.llSucces
			*** da frecvent fatal error cand transmit parametri cu ? in loc de valori - probabil bug in driverul de odbc
			*!*				lcSql = [{call pack_inchideri.note_inchidere_tva(?gnAn, ?gnLuna, ?gnIdSucursala, ?pnIdFdoc, ?gnIdUtil, ?pdDataIreg, ?pdDataAct, ?pnNrAct, ?pcExplicatia, ?pcSirDifAcont, ?pcSirDifPart, ?@pnCod)}]
			TEXT To lcSql Noshow Textmerge
				{call pack_inchideri.note_inchidere_tva(<<gnAn>>, <<gnLuna>>, <<iif(isnull(gnIdSucursala), 'NULL', transform(gnIdSucursala))>>, <<pnIdFdoc>>, <<gnIdUtil>>, TO_DATE('<<dtos(pdDataIreg)>>', 'YYYYMMDD'), TO_DATE('<<dtos(pdDataAct)>>', 'YYYYMMDD'), <<pnNrAct>>, '<<pcExplicatia>>', '<<pcSirDifAcont>>', '<<pcSirDifPart>>', ?@pnCod, ?pnScrieDirect)}
			ENDTEXT


			llSucces = goExecutor.oExecuta(m.lcSql, m.lcCursorVerificare)
			If !m.llSucces && eroare
				Exit
			Endif

			
			If pnScrieDirect = 1
				If Reccount(m.lcCursorVerificare) = 0 && nu mai este nimic de completat
					Exit
				Endif

				*** Trebuie completate analitice, parteneri
				pcSirDifAcont = ""
				pcSirDifPart = ""
				llSucces = verificare_completare_act(m.lcCursorVerificare, .T., .T., @pcSirDifAcont, @pcSirDifPart)
			Else
				Select actactan
				Go Top
				Scatter Name loAct
				Zap

				Insert Into actactan (scc, ascc, scd, ascd, suma) ;
					Select scc, ascc, scd, ascd, suma From (lcCursorVerificare)
         

				Select actactan
				Scan
					Gather Name loAct Fields Except scc, ascc, scd, ascd, suma
				Endscan

				IF m.gl406
					UPDATE actactan SET taxcode = 301000 WHERE INLIST(scd, '4423', '4424') OR INLIST(scc, '4423', '4424')
					UPDATE actactan SET taxcode = 380200 WHERE !(INLIST(scd, '4423', '4424') OR INLIST(scc, '4423', '4424'))
				ENDIF 

				Select .F. As ales, 0 As Cu_tva, Cast(0 As N(2)) As tipnota, a.*, Space(100) As explicatie_tva ;
					From actactan a ;
					Into Cursor tact Readwrite

				Use In (Select('crsExplicatiiTVATemp'))

				Select tact
				Go Top
				llNotaNoua = .T.
				llMaximize = .T.
				loForm = Createobject([frm_modific2007], m.lnIdSet, m.llNotaNoua)
				loForm.lb_titlu_alb_b121.Caption = 'Nota inchidere TVA'
				Select tact
				loForm.Show(1, m.llMaximize)

				If m.gnButon = 1
					Use In (Select('actactan'))

					Select tact
					Replace id_jtva_coloana With Null, PROC_TVA With 0 For Cu_tva = 0
					Select * From tact Into Cursor actactan Readwrite

					Select actactan
					Replace All id_util With m.gnIdUtil, id_set With m.lnIdSet
					lnSucces  = OSCRIE_IN_FISIERE(0)
					llSucces = (lnSucces > 0)
					IF m.llSucces
						AMESSAGEBOX('S-a inregistrat Nota de inchidere TVA!', 0 + 64, _Screen.Caption)										
					ENDIF 
				Endif && gnButon = 1

				Exit
			Endif && pnScrieDirect
			Use In (Select(m.lcCursorVerificare))
		Enddo && .T.
		llSucces = goConn.EndManualTransaction(Iif(m.llSucces, 'COMMIT', 'ROLLBACK'))

	Endif && llSucces

	Use In (Select('actactan'))

	IF m.pnScrieDirect = 1
		If Nvl(m.pnCod, 0) <> 0
			AMESSAGEBOX('S-a inregistrat Nota de inchidere TVA!', 0 + 64, _Screen.Caption)
		Else
			AMESSAGEBOX('Nu exista inregistrari pentru Nota de inchidere TVA!', 0 + 64, _Screen.Caption)
		ENDIF
	ENDIF 
		
Endproc  && inchidere_tva



************************************
*** note inchidere TVA Incasare pentru facturi cu sold 0 si neexigibilizate la timp
*** se apeleaza din Registrul de Cumparari/Vanzari
************************************
Procedure inchidere_tva_sold
	Parameters tcJurnal, tcXMLFacturi

	Local loForm As [frm_modific2007]
	Local lcCursorDefTVA, lcJurnal, lcSCC, lcSCD, lcSql, llMaximize, llNotaNoua, lnBut, lnIdFact
	Local lnIdSet, lnNrAct, lnProcTVA, lnRecords, lnSucces, lnSuma, loAct
	Local lnCota, lnCotaTVA, lnSuma24, lnSuma5, lnSuma9, lnSumaT
	Local lnSuma24T, lnSuma5T, lnSuma9T, lnSumaNT, lcMesaj
SET STEP ON 
	lcJurnal = Upper(Alltrim(m.tcJurnal)) && JC/JV
	If m.lcJurnal = 'JC' && Jurnal Cumparari
		lcSCD = '4426'
		lcSCC = '4428'
	Else
		lcSCD = '4428'
		lcSCC = '4427'
	Endif
	lnRecords = Xmltocursor(m.tcXMLFacturi, "crsTVAIncasareTemp")
	If lnRecords = 0
		Use In (Select("crsTVAIncasareTemp"))
		Return
	Endif
	lcMesaj = ''
	lnIdSet = 99999 && nota fara predefinire
	lnBut = lans(m.lnIdSet)
	Set Step On
	If m.lnBut = 1
		Select actactan
		Scatter Name loAct
		Zap

		Select crsTVAIncasareTemp
		Scan
			lnNrAct = crsTVAIncasareTemp.nract
			lnIdFact = crsTVAIncasareTemp.id_fact
			lnSumaT = soldn
			lnSuma21 = soldn21
			lnSuma11 = soldn11

			lnSuma24 = soldn24
			lnSuma20 = soldn20
			lnSuma19 = soldn19
			lnSuma9 = soldn9
			lnSuma5 = soldn5

			If m.lcJurnal = 'JC' && Jurnal Cumparari
				lnSuma21T = ro21nt
				lnSuma11T = ro11nt
				lnSuma24T = ro24nt
				lnSuma20T = ro20nt
				lnSuma19T = ro19nt
				lnSuma9T = ro09nt
				lnSuma5T = ro05nt
			ELSE
				lnSuma21T = ro21nt
				lnSuma11T = ro11nt			
				lnSuma24T = ro24nt
				lnSuma20T = ro20nt
				lnSuma19T = ro19nt
				lnSuma9T = ro9nt
				lnSuma5T = ro5nt
			Endif

			For lnCota = 1 To 7
				lnSuma = m.lnSumaT
				lnCotaTVA = 0.00
				If m.lnSumaT = 0
					lnSuma = Iif(m.lnCota = 1, m.lnSuma24, Iif(m.lnCota = 2, m.lnSuma20, Iif(m.lnCota = 3, m.lnSuma19, Iif(m.lnCota = 4, m.lnSuma9, Iif(m.lnCota = 5, m.lnSuma5, Iif(m.lnCota = 21, m.lnSuma21, m.lnSuma11))))))
					lnCotaTVA = Iif(m.lnCota = 1, 1.24, Iif(m.lnCota = 2, 1.20, Iif(m.lnCota = 3, 1.19, Iif(m.lnCota = 4, 1.09, Iif(m.lnCota = 5, 1.05, Iif(m.lnCota = 6, 1.21, 1.11))))))
				Endif

				lnSumaNT = Iif(m.lnCota = 1, m.lnSuma24T, Iif(m.lnCota = 2, m.lnSuma20T, Iif(m.lnCota = 3, m.lnSuma19T, Iif(m.lnCota = 4, m.lnSuma9T, Iif(m.lnCota = 5, m.lnSuma5T, Iif(m.lnCota = 6, m.lnSuma21T, m.lnSuma11T))))))

				lcSql = [{call pack_contab.defalca_tva_incasare(] + Alltrim(Str(m.lnIdFact)) + [,] + ;
					Alltrim(Str(m.lnSuma, 16, m.gnPC)) + [, ] + Alltrim(Str(m.lnCotaTVA, 5, 2)) + [, 1, ?gnLuna, ?gnAn)}]
				lcCursorDefTVA = [crsdeftvainc]
				If goExecutor.oExecuta(m.lcSql, m.lcCursorDefTVA)

					* Daca din diferite motive nu se poate calcula TVA exigibil, pun o linie cu ID_FACT-ul facturii originale, sa se poata completa manual
					If Reccount(lcCursorDefTVA) = 0 And m.lnSumaNT <> 0
						Insert Into (lcCursorDefTVA) (suma, PROC_TVA, id_fact) ;
							Values (m.lnSumaNT, Iif(m.lnCota = 1, 1.24, Iif(m.lnCota = 2, 1.20, Iif(m.lnCota = 3, 1.19,  Iif(m.lnCota = 4, 1.09, Iif(m.lnCota = 5, 1.05, Iif(m.lnCota = 6, 1.21, 1.11)))))), m.lnIdFact)
						lcMesaj = lcMesaj + 'Factura ' + Alltrim(Str(m.lnNrAct)) + Chr(13) + Chr(10)
					Endif

					Insert Into actactan(nract, dataact, dataireg, id_fdoc, fdoc, nr_nota, luna, an, scd, scc, suma, PROC_TVA, id_jtva_coloana, id_set, id_fact, perechec, pereched, explicatia) ;
						Select loAct.nract, loAct.dataact, loAct.dataireg, loAct.id_fdoc, loAct.fdoc, Reccount('actactan') + Recno() As nr_nota, m.gnLuna As luna, m.gnAn As an, m.lcSCD As scd, m.lcSCC As scc, ;
						a.suma, a.PROC_TVA, a.id_jtva_coloana, m.lnIdSet As id_set, m.lnIdFact As id_fact, ;
						Iif(m.lcJurnal = 'JV', m.lnNrAct, 0) As perechec, Iif(m.lcJurnal = 'JC', m.lnNrAct, 0) As pereched, ;
						'Exigibilizare TVA Incasare' As explicatia ;
						From (lcCursorDefTVA) a


					Use In (Select(m.lcCursorDefTVA))
				Endif

				* apelez defalca_tva_incasare pentru fiecare cota, doar daca soldul neexigibil total este 0
				* NU MAI STIU DE CE FAC ASTA???? Am pus lnSumaT > 0 in loc de <> 0, pentru ca aveam un caz special < 0 si nu stiu de ce ieste din bucla
				If m.lnSumaT > 0
					Exit
				Endif

			Endfor 	&& lnCota = 1 TO 3
		Endscan && crsTVAIncasareTemp


		Select actactan
		Scan
			Gather Name loAct Fields Except nr_nota, luna, an, scd, scc, suma, PROC_TVA, id_jtva_coloana, id_set, id_fact, perechec, pereched
			If !Empty(Nvl(loAct.explicatia, ''))
				Replace explicatia With loAct.explicatia
			Endif
			Replace id_sucursala With m.gnIdSucursala
		Endscan


		update_jtva_coloane(m.lcJurnal, 'crsExplicatiiTVATemp', 1)

		Select .F. As ales, 1 As Cu_tva, Cast(0 As N(2)) As tipnota, a.*, b.denumire As explicatie_tva_2 ;
			From actactan a Left Join crsExplicatiiTVATemp b On a.id_jtva_coloana = b.id_jtva_coloana ;
			Into Cursor tact Readwrite

		Update tact Set explicatie_tva = explicatie_tva_2
		
		Use In (Select('crsExplicatiiTVATemp'))

		If !Empty(m.lcMesaj)
			lcMesaj = [ATENTIE! Nu s-au gasit toate informatiile necesare!] + Chr(13) + Chr(10) + ;
				[Completati campul "Explicatia TVA" exigibila pentru inregistrarile:] + Chr(13) + Chr(10) + m.lcMesaj
			AMESSAGEBOX(m.lcMesaj, 0 + 48, _Screen.Caption)
		Endif

		Select tact
		Go Top
		llNotaNoua = .T.
		llMaximize = .T.
		loForm = Createobject([frm_modific2007], m.lnIdSet, m.llNotaNoua)
		loForm.lb_titlu_alb_b121.Caption = 'Exigibilizare TVA Incasare'
		Select tact
		loForm.Show(1, m.llMaximize)

		If buton = 1
			Use In (Select('actactan'))

			Select tact
			Replace id_jtva_coloana With Null, PROC_TVA With 0 For Cu_tva = 0
			Select * From tact Into Cursor actactan Readwrite

			Select actactan
			Replace All id_util With gnIdUtil, id_set With m.lnIdSet
			lnSucces  = OSCRIE_IN_FISIERE(0)
		Endif && buton = 1
	Endif &&  m.lnBut = 1


	Use In (Select('actactan'))
	Use In (Select('tact'))

Endproc && inchidere_tva_sold

*_________________________________
Procedure inchidere_venituri_cheltuieli
	Lparameters tnTip
	* tnTip: 7 = Venituri, 6 = Cheltuieli

	Local overif As 'verificare'
	Local lCompletareParteneri, lVerificAnalitic, lcCursor, lcSel, lcSql, llSucces, lnBut, lnIdSet
	Local lnSucces, lnTip, loAct, lcAcont121, llNotaNoua, llMaximize

	lnTip = Iif(Empty(m.tnTip), 7, m.tnTip)
	lnIdSet = Iif(m.lnTip = 7, 90005, 90006)

	* Stergere inchidere anterioara
	Use In (Select('actactan'))
	lcSql = [select * from vact where id_set = ] + Alltrim(Str(m.lnIdSet)) + [ and an = ?gnAn and luna = ?gnLuna] + m.gcCondSucursala
	lcCursor = [actactan]

	llSucces = goExecutor.oExecuta(lcSql, m.lcCursor)
	If !m.llSucces
		Return
	Endif

	Select actactan
	If Reccount() > 0
		Clear Class "verificare"
		overif = Createobject('verificare')
		overif.Show(1)
		If buton = 2
			Use In (Select('actactan'))
			Release overif
			Return
		Endif
		Do OSCRIE_IN_FISIERE.prg With 2, .F.
	Endif

	* Nota inchidere curenta
	lnBut = lans(m.lnIdSet)
	llSucces = (m.lnBut = 1)

	If m.llSucces
		lcSel = '{call pack_inchideri.inchidere_' + Iif(m.lnTip = 7, 'venituri', 'cheltuieli') + '(?gnAn,?gnLuna,?gnIdSucursala)}'
		lcCursor = 'crs_inchidere'
		llSucces = goExecutor.oExecuta(m.lcSel, m.lcCursor)
	Endif

	If m.llSucces
		Select actactan
		Go Top
		Scatter Name loAct
		Zap

		lcAcont121 = ''
		If m.lnTip = 7 && venituri
			lcAcont121 = Iif(Type('gcRC_ACONT121_INCHIDERE_V') = 'C' And !Empty(Nvl(m.gcRC_ACONT121_INCHIDERE_V, '')), m.gcRC_ACONT121_INCHIDERE_V, Space(4))
			Insert Into actactan (scd, ascd, scc, ascc, suma, id_sectie, sectie) ;
				Select Cont As scd, ACONT As ascd, '121 ' As scc, lcAcont121 As ascc, suma, ;
				Iif(Type('id_sectie') = 'U', loAct.id_sectie, id_sectie) As id_sectie, ;
				Iif(Type('sectie') = 'U', loAct.sectie, sectie) As sectie From crs_inchidere
		Else
			lcAcont121 = Iif(Type('gcRC_ACONT121_INCHIDERE_C') = 'C' And !Empty(Nvl(m.gcRC_ACONT121_INCHIDERE_C, '')), m.gcRC_ACONT121_INCHIDERE_C, Space(4))
			Insert Into actactan (scc, ascc, scd, ascd, suma, id_sectie, sectie) ;
				Select Cont As scc, ACONT As ascc, '121 ' As scd, lcAcont121 As ascd, suma, ;
				Iif(Type('id_sectie') = 'U', loAct.id_sectie, id_sectie) As id_sectie, ;
				Iif(Type('sectie') = 'U', loAct.sectie, sectie) As sectie From crs_inchidere
		Endif

		Select actactan
		Scan
			Gather Name loAct Fields Except scc, ascc, scd, ascd, suma, id_sectie, sectie
		Endscan

		Select .F. As ales, 0 As Cu_tva, Cast(0 As N(2)) As tipnota, a.*, Space(100) As explicatie_tva ;
			From actactan a ;
			Into Cursor tact Readwrite

		Use In (Select('crsExplicatiiTVATemp'))

		Select tact
		Go Top
		llNotaNoua = .T.
		llMaximize = .T.
		loForm = Createobject([frm_modific2007], m.lnIdSet, m.llNotaNoua)
		loForm.lb_titlu_alb_b121.Caption = 'Nota inchidere ' + Iif(m.lnTip = 7, 'venituri', 'cheltuieli')
		Select tact
		loForm.Show(1, m.llMaximize)

		If m.gnButon = 1
			Use In (Select('actactan'))

			Select tact
			Replace id_jtva_coloana With Null, PROC_TVA With 0 For Cu_tva = 0
			Select * From tact Into Cursor actactan Readwrite

			Select actactan
			Replace All id_util With m.gnIdUtil, id_set With m.lnIdSet
			lnSucces  = OSCRIE_IN_FISIERE(0)
		Endif && gnButon = 1
	Endif && llSucces

	Use In (Select('crs_inchidere'))
	Use In (Select('actactan'))
Endproc && inchidere_venituri_cheltuieli


**************************************
*** Genereaza note 461 = 542 pe baza sold inregistrari 542 la 30.06.aa, 31.12.aa
*** folositor pentru aflarea soldul 542 la sfarsitul perioadei in F30 bilant
*** ID_SET: 90021
**************************************
Procedure inchidere_sold_542
	Private pnIdSet, pnCod

	Local overif As 'verificare'
	Local lcCursor, lcCursorVerificare, lcSql, llSucces, lnButon, lnSucces, loAct
	Private pcSirDifAcont, pcSirDifPart, pdDataIreg

	pnIdSet = 90021
	pnCod = 0
	llSucces = .T.
	lcCursorVerificare = [crsact]

	*** STERGERE NOTA INCHIDERE SOLD 542 90021
	lcSql = [select * from vact where id_set = ?pnIdSet and an = ?gnAn and luna = ?gnLuna] + m.gcCondSucursala
	lcCursor = [actactan]

	llSucces = goExecutor.oExecuta(lcSql, m.lcCursor)
	If !m.llSucces
		Return m.llSucces
	Endif

	Select actactan
	If Reccount() > 0
		Clear Class "verificare"
		overif = Createobject('verificare')
		overif.Show(1)
		If buton = 2
			Use In (Select('actactan'))
			Release overif
			Return m.llSucces
		Endif
		lnSucces = OSCRIE_IN_FISIERE(2, .F.)
		llSucces = (m.lnSucces > 0)
	Endif


	If !m.llSucces
		Return m.llSucces
	Endif

	*** Cerere date (nu imi trebuie informatii gen nract, dataact, dar trebuie sa se creeze actactan)
	lnButon = lans(m.pnIdSet)
	If m.lnButon = 2
		llSucces = .F.
		Return m.llSucces
	Endif

	Select actactan
	Go Top
	Scatter Name loAct

	pdDataIreg = loAct.dataireg
	pcSirDifAcont = ""
	pcSirDifPart = ""


	*** SCRIERE NOTA INCHIDERE SOLD 542 90021
	If m.llSucces
		pcSirDifAcont = ""
		pcSirDifPart = ""
		llSucces = goConn.BeginManualTransaction()
		Do While m.llSucces
			*** da frecvent fatal error cand transmit parametri cu ? in loc de valori - probabil bug in driverul de odbc
			*!*				lcSql = [{call pack_inchideri.note_inchidere_tva(?gnAn, ?gnLuna, ?gnIdSucursala, ?pnIdFdoc, ?gnIdUtil, ?pdDataIreg, ?pdDataAct, ?pnNrAct, ?pcExplicatia, ?pcSirDifAcont, ?pcSirDifPart, ?@pnCod)}]
			TEXT To lcSql Noshow Textmerge
				{call pack_inchideri.note_inchidere_542(<<gnAn>>, <<gnLuna>>, <<iif(isnull(gnIdSucursala), 'NULL', transform(gnIdSucursala))>>, <<gnIdUtil>>, TO_DATE('<<dtos(pdDataIreg)>>', 'YYYYMMDD'),  '<<pcSirDifAcont>>', '<<pcSirDifPart>>', ?@pnCod)}
			ENDTEXT


			llSucces = goExecutor.oExecuta(m.lcSql, m.lcCursorVerificare)
			If !m.llSucces && eroare
				Exit
			Endif

			If Reccount(m.lcCursorVerificare) = 0 && nu mai este nimic de completat
				Exit
			Endif

			*** Trebuie completate analitice, parteneri
			pcSirDifAcont = ""
			pcSirDifPart = ""
			llSucces = verificare_completare_act(m.lcCursorVerificare, .T., .T., @pcSirDifAcont, @pcSirDifPart)
			Use In (Select(m.lcCursorVerificare))
		Enddo && .T.
		llSucces = goConn.EndManualTransaction(Iif(m.llSucces, 'COMMIT', 'ROLLBACK'))
	Endif && llSucces

	Use In (Select('actactan'))

	If Nvl(m.pnCod, 0) <> 0
		AMESSAGEBOX('S-a inregistrat Nota de inchidere sold 542!', 0 + 64, _Screen.Caption)
	Else
		AMESSAGEBOX('Nu exista inregistrari pentru Nota de inchidere sold 542!', 0 + 64, _Screen.Caption)
	Endif
Endproc && inchidere_sold_542


**************************************
*** Genereaza note inverse 542 = 461 pe baza notei de inchidere din registrul jurnal din luna anterioara (la 07/aa 01/aa)
*** ID_SET: 90022
**************************************
Procedure repunere_sold_542

	Private pnIdSet, pnCod

	Local overif As 'verificare'
	Local lcCursor, lcCursorVerificare, lcSql, llSucces, lnButon, lnSucces, loAct
	Private pcSirDifAcont, pcSirDifPart, pdDataIreg

	pnIdSet = 90022
	pnCod = 0
	llSucces = .T.
	lcCursorVerificare = [crsact]

	*** STERGERE NOTA REPUNERE SOLD 542 90022
	lcSql = [select * from vact where id_set = ?pnIdSet and an = ?gnAn and luna = ?gnLuna] + m.gcCondSucursala
	lcCursor = [actactan]

	llSucces = goExecutor.oExecuta(lcSql, m.lcCursor)
	If !m.llSucces
		Return m.llSucces
	Endif

	Select actactan
	If Reccount() > 0
		Clear Class "verificare"
		overif = Createobject('verificare')
		overif.Show(1)
		If buton = 2
			Use In (Select('actactan'))
			Release overif
			Return m.llSucces
		Endif
		lnSucces = OSCRIE_IN_FISIERE(2, .F.)
		llSucces = (m.lnSucces > 0)
	Endif


	If !m.llSucces
		Return m.llSucces
	Endif

	*** Cerere date (nu imi trebuie informatii gen nract, dataact, dar trebuie sa se creeze actactan)
	lnButon = lans(m.pnIdSet)
	If m.lnButon = 2
		llSucces = .F.
		Return m.llSucces
	Endif

	Select actactan
	Go Top
	Scatter Name loAct

	pdDataIreg = loAct.dataireg
	pcSirDifAcont = ""
	pcSirDifPart = ""


	*** SCRIERE NOTA REPUNERE SOLD 542 90022
	If m.llSucces
		pcSirDifAcont = ""
		pcSirDifPart = ""
		llSucces = goConn.BeginManualTransaction()
		Do While m.llSucces
			*** da frecvent fatal error cand transmit parametri cu ? in loc de valori - probabil bug in driverul de odbc
			*!*				lcSql = [{call pack_inchideri.note_inchidere_tva(?gnAn, ?gnLuna, ?gnIdSucursala, ?pnIdFdoc, ?gnIdUtil, ?pdDataIreg, ?pdDataAct, ?pnNrAct, ?pcExplicatia, ?pcSirDifAcont, ?pcSirDifPart, ?@pnCod)}]
			TEXT To lcSql Noshow Textmerge
				{call pack_inchideri.note_repunere_542(<<gnAn>>, <<gnLuna>>, <<iif(isnull(gnIdSucursala), 'NULL', transform(gnIdSucursala))>>, <<gnIdUtil>>, TO_DATE('<<dtos(pdDataIreg)>>', 'YYYYMMDD'),  '<<pcSirDifAcont>>', '<<pcSirDifPart>>', ?@pnCod)}
			ENDTEXT


			llSucces = goExecutor.oExecuta(m.lcSql, m.lcCursorVerificare)
			If !m.llSucces && eroare
				Exit
			Endif

			If Reccount(m.lcCursorVerificare) = 0 && nu mai este nimic de completat
				Exit
			Endif

			*** Trebuie completate analitice, parteneri
			pcSirDifAcont = ""
			pcSirDifPart = ""
			llSucces = verificare_completare_act(m.lcCursorVerificare, .T., .T., @pcSirDifAcont, @pcSirDifPart)
			Use In (Select(m.lcCursorVerificare))
		Enddo && .T.
		llSucces = goConn.EndManualTransaction(Iif(m.llSucces, 'COMMIT', 'ROLLBACK'))
	Endif && llSucces

	Use In (Select('actactan'))

	If Nvl(m.pnCod, 0) <> 0
		AMESSAGEBOX('S-a inregistrat Nota de repunere sold 542!', 0 + 64, _Screen.Caption)
	Else
		AMESSAGEBOX('Nu exista inregistrari pentru Nota de repunere sold 542!', 0 + 64, _Screen.Caption)
	Endif
Endproc && repunere_sold_542


*************************************************
*** completare configurare conturi pentru inchiderea de amortizari
*** completeaza conturile de cheltuiala si amortizare pe sectii cu conturile de la inregistrarile fara sectii, daca este cazul
*** id_sectie a fost adaugat in 09.07.2020 si erau configurari doar pe cont, acont, nu si pe sectie
*************************************************
Procedure inchidere_amortizari_completare_conturi
	Local lcLunaAn, lcSelect, lcSql
	lcSelect = Select()

	lcLunaAn = Padl(Int(m.gnLuna), 2, '0') + Alltrim(Str(Int(m.gnAn)))
	TEXT TO lcSql TEXTMERGE NOSHOW
  BEGIN
  	pack_imob.setlunacurenta(to_date('<<m.lcLunaAn>>','mmyyyy'));

  insert into imob_inchidere (id_sectie, cont, acont, scd, ascd, scc, ascc)
  select a.id_sectie,
         a.cont,
         a.acont,
         nvl(a.scd, b.cont_cheltuiala) as scd,
         ascd,
         nvl(a.scc, b.cont_amortizare) as scc,
         a.ascc
    from (Select Distinct DECODE(s.id_sectie, 0, NULL, s.id_sectie) as id_sectie,
                          trim(s.Cont) as cont,
                          trim(s.acont) as acont,
                          nvl(i1.scd, i2.scd) as scd,
                          nvl(i1.ascd, i2.ascd) as ascd,
                          nvl(i1.scc, i2.scc) as scc,
                          nvl(i1.ascc, i2.ascc) as ascc
            FROM imob_vsituatie_lunara s
            left join imob_inchidere i1
              on nvl(s.id_sectie, 0) = nvl(i1.id_sectie, 0)
             and trim(s.cont) = trim(i1.cont)
             and trim(nvl(s.acont, 'x')) = trim(nvl(i1.acont, 'x'))
            left join (select cont, acont, scd, ascd, scc, ascc
                        from imob_inchidere
                       where id_sectie is null) i2
              on trim(s.cont) = trim(i2.cont)
             and trim(nvl(s.acont, 'x')) = trim(nvl(i2.acont, 'x'))
           WHERE s.iesit_din_gest = 0
             and s.id_tip_imobilizare in (1, 2)
             and rpad(nvl(s.id_sectie,0), 10, '0') || rpad(trim(s.cont),4, 'x') || rpad(trim(s.acont), 4, 'x') not in
             (select rpad(nvl(id_sectie,0), 10, '0') || rpad(trim(cont),4, 'x') || rpad(trim(acont), 4, 'x') from imob_inchidere)) a
    left join imob_conturi b
      on trim(a.cont) = trim(b.cont);
   END;
	ENDTEXT
	llSucces = goExecutor.oExecuta(m.lcSql)

	Select (m.lcSelect)
	Return m.llSucces
Endproc && inchidere_amortizari_completare_conturi

Procedure viz_inchidere_amortizari_conturi
	Local loFrm, llBlank, llSucces, llBlank

	inchidere_amortizari_completare_conturi()

	llBlank = .T.
	= ccsectii(m.llBlank) && vnom_sectii
	Select vnom_sectii
	Index On id_sectie Tag id_sectie

	llSucces = goExecutor.oExecuta("select id, id_sectie, sectie, cont, acont, scd, ascd, scc, ascc from vimob_inchidere order by sectie, cont, acont", "imob_conturi")
	If m.llSucces
		Select imob_conturi
		Go Top
		loFrm = Newobject("imob_conturi", "oconfigcont.vcx")
		loFrm.Show(1)
		Use In (Select('imob_conturi'))
	Endif

	= close_ccsectii()
Endproc && viz_inchidere_amortizari_conturi

*________________________________

Procedure inchidere_amortizari
	Parameters tnTip

	Local lcCursorInchidere, lcSql, lcCursor, lnSucces
	Local loForm As [frm_modific2007]
	Local overif As 'verificare'
	Local lcAct, lcDirContafin, llMaximize, llNotaNoua, llSucces, lnBut, lnIdSectie, loAct
	Private pnIdSet, pnSectie, pnTip, pnLucrare

	pnTip = Iif(Empty(tnTip) Or Type('tnTip') # 'N', 1, tnTip)
	If pnTip = 1
		pnIdSet = 90003
	Endif
	If pnTip = 2
		pnIdSet = 90009
	Endif

	* Completare conturi
	inchidere_amortizari_completare_conturi()

	lcCursorInchidere = ""

	lcSql = [select * from vact where id_set = ?pnIdSet and an = ?gnAn and luna = ?gnLuna] + m.gcCondSucursala
	lcCursor = [actactan]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)
	goExecutor.oReset()
	If lnSucces < 0
		AMESSAGEBOX('Cautare Nota Inchidere Amortizari:' + Chr(13) + goExecutor.cEroare, 0 + 16, "Eroare")
		Return
	Endif

	Select actactan
	If Reccount() > 0
		Clear Class "verificare"
		overif = Createobject('verificare')
		overif.Show(1)
		If buton = 2
			If Used('actactan')
				Use In actactan
			Endif
			Release overif
			Return
		Else
			Do OSCRIE_IN_FISIERE.prg With 2, .F.
		Endif
	Endif


	lnBut = lans(pnIdSet)

	If m.lnBut = 1
		If m.gnNotaImob = 3
			* Import nota Contafin
			lcDirContafin = goApp.ReadIni([inchidere], "director_contafin")
			If Empty(m.lcDirContafin) Or !Directory(m.lcDirContafin)
				lcDirContafin = Getdir('', 'Directorul firmei Contafin', 'Directorul firmei Contafin', 64)
				goApp.WriteIni([inchidere], "director_contafin", m.lcDirContafin)
			Endif

			lcAct = Addbs(m.lcDirContafin) + 'AN' + Alltrim(Str(m.gnAn)) + '\DATE' + Padl(Alltrim(Str(m.gnLuna, 2, 0)), 2, '0') + '\ACT.DBF'
			If File(m.lcAct)
				llSucces = goExecutor.oExecuta('SELECT scd, ascd, scc, ascc, suma, explicatia4 as explicatie, id_sectie, sectie FROM vact WHERE 1=2', 'crs_inchidere')

				Use (m.lcAct) In 0 Shared Alias c_act_temp
				Select explicatia, scd, scc, ascd, ascc, sectie, nume_2 As explicatie, suma From c_act_temp Where id_set = m.pnIdSet Into Cursor cActTemp
				Use In (Select('c_act_temp'))
				If Reccount('cActTemp') = 0
					AMESSAGEBOX('Nu exista nici o nota cu tipul de operatie ' + Alltrim(Str(m.pnIdSet)) + ' in Registrul jurnal din Contafin!', 0 + 48, _Screen.Caption)
					Create Cursor crs_inchidere(scd C(4), ascd C(4), scc C(4), ascc C(4), suma N(18, 4), explicatie C(100), id_sectie I, sectie C(100))
				Endif && RECCOUNT

				Select crs_inchidere
				Append From Dbf('cActTemp')
				Use In (Select('cActTemp'))

				Select crs_inchidere
				Scan
					lnIdSectie = GetIdSectie(crs_inchidere.sectie)
					Replace id_sectie With m.lnIdSectie In crs_inchidere
				Endscan
			Else
				AMESSAGEBOX('Nu exista fisierul ' + m.lcAct, 0 + 48, _Screen.Caption)
				Create Cursor crs_inchidere(scd C(4), ascd C(4), scc C(4), ascc C(4), suma N(18, 4), explicatie C(100), id_sectie I, sectie C(100))
			Endif && FILE(m.lcAct)
		Else
			pnSectie = 0
			DO CASE 
				CASE m.gnNotaImob = 2
					pnSectie = 1
				CASE m.gnCampSectie = 1 AND AMESSAGEBOX("Inchiderea se face dupa campul 'Sectie'?", 4 + 32, "Inchidere amortizari") = 6
					pnSectie = 1
			ENDCASE 
			pnLucrare = 0
			IF m.gnCampLucrare = 1 AND AMESSAGEBOX("Inchiderea se face dupa campul 'Lucrare'?", 4 + 32, "Inchidere amortizari") = 6
				pnLucrare = 1
			ENDIF 		

			lcCursorInchidere = GET_CURSOR_INCHIDERE_AMORTIZARI(m.pnTip, m.pnSectie, m.pnLucrare, m.gnAn, m.gnLuna, "crs_inchidere") && oproceduri_comune_imob.prg
			If !Used(lcCursorInchidere)
				lnBut = 2 && RENUNT
			Endif
		Endif && m.gnNotaImob = 3
	Endif && m.lnBut = 1

	If m.lnBut = 1
		Select actactan
		Scatter Name loAct
		Zap
		Insert Into actactan (scd, ascd, scc, ascc, suma, explicatia4, id_sectie, sectie, id_lucrare, nrord) ;
			SELECT scd, ascd, scc, ascc, suma, explicatie, id_sectie, sectie, id_lucrare, nrord From crs_inchidere Where suma <> 0

		Select actactan
		Scan
			Gather Name loAct Fields Except scd, ascd, scc, ascc, suma, explicatia4, id_sectie, sectie, id_lucrare, nrord
		Endscan

		Select .F. As ales, 0 As Cu_tva, Cast(0 As N(2)) As tipnota, a.*, Space(100) As explicatie_tva ;
			From actactan a ;
			Into Cursor tact Readwrite

		Use In (Select('crsExplicatiiTVATemp'))

		Select tact
		Go Top
		llNotaNoua = .T.
		llMaximize = .T.
		loForm = Createobject([frm_modific2007], m.pnIdSet, m.llNotaNoua)
		loForm.lb_titlu_alb_b121.Caption = 'Nota inchidere amortizare ' + Iif(m.pnTip = 1, 'imobilizari corporale', 'imobilizari necorporale')
		Select tact
		loForm.Show(1, m.llMaximize)

		If m.gnButon = 1
			Use In (Select('actactan'))

			Select tact
			Replace id_jtva_coloana With Null, PROC_TVA With 0 For Cu_tva = 0
			Select * From tact Into Cursor actactan Readwrite

			Select actactan
			Replace All id_util With gnIdUtil, id_set With m.pnIdSet
			lnSucces  = OSCRIE_IN_FISIERE(0)
		Endif && gnButon = 1
	Endif && lnBut = 1

	Use In (Select('crs_inchidere'))
	Use In (Select('actactan'))
Endproc && inchidere_amortizari
