**************************************
* Intoarce .T. daca luna are situatii contabile invalidate
**************************************
PROCEDURE VerificaInvalidat
	LPARAMETERS tnAn, tnLuna
	LOCAL llInvalidat, lnRec, llInvalidat, lcSql
	PRIVATE pnRec
	llInvalidat = .F.
	
	lcSql = [select count(*) as nr from tabele_validare where dinvalidat is not null]
	IF !(EMPTY(m.tnAn) OR EMPTY(m.tnLuna))
		lcSql = lcSql + [ and an = ] + ALLTRIM(STR(m.tnAn)) + [ and luna = ] + ALLTRIM(STR(m.tnLuna))
	ENDIF 	
	pnRec = 0
	llSucces = goExecutor.oSelecteaza2Value(m.lcSql, @pnRec)
	IF m.llSucces
		llInvalidat = (m.pnRec > 0)
	ENDIF 
	RETURN m.llInvalidat 
ENDPROC 


*!* Arata situatii contabile invalidate in luna curenta
Procedure SituatiiInvalidate

	Private poSituatii
	Local loSituatiiInvalide As 'frm_situatii_invalidate'
	Local lcFiltru, lcFiltruOriginal, lcGroup, lcOrder, lcSelect, llAfiseaza, llModParam
	poSituatii = null
	
	Text To lcSelect Noshow
   select tabel,
   	  situatie,
      an,
      luna,
      cont,
      id_gestiune,
      cgest,
      nume_gestiune,
      dultima_operatie,
      dultima_refacere,
      dinvalidat,
      id_util_invalidare,
      util_invalidare,
      explicatie,
      tip_validare,
      ctip_validare,
      blocat,
      id_util_validare,
      util_validare,
      dataora_validare
 from vtabele_validare
	Endtext
	lcSchema = []
	lcOrder = [tabel, cont, cgest, an, luna]
	lcGroup = []
	lcFiltru = [1=2]
	lcFiltruOriginal = []
	llModParam = .T.
	llAfiseaza = .F.

	gencursor('poSituatii', 'cSituatiiInvalidate', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza, lcGroup, llModParam, lcFiltruOriginal)
	poSituatii.ca_baza1.afisare()

	If Used('cSituatiiInvalidate')
		loSituatiiInvalide = Createobject('frm_situatii_invalidate')
		loSituatiiInvalide.Show(1)
	Endif

Endproc && SituatiiInvalide

Procedure verificareGlobala
	Parameters tlArat, tlFaraPeriada
	Private plArat, pcondper, plPerioada, plFaraPeriada

	Local lnPoz
	Store .F. To plPerioada

	If Empty(tlArat)
		Store .F. To plArat
	Else
		plArat = tlArat
	Endif

	If Empty(tlFaraPeriada)
		Store .F. To plFaraPeriada
	Else
		plFaraPeriada = tlFaraPeriada
	Endif

	pcondper = ""
	If !plFaraPeriada
		ofrmperioada = Createobject('frm_perioada_luni')
		ofrmperioada.Show(1)

		If gnButon = 2
			Return
		Endif

		lnPoz = At('_', pcondper)
		If lnPoz > 0
			plPerioada = .T.
		Endif
	Endif

	Private pcmesaj_refacere
	Store "" To pcmesaj_refacere
	If Used('crsverificari')
		Use In crsverificari
	Endif
	Create Cursor crsverificari (sursa c(20), Cont c(20), tip c(1), mesaj c(200), selectat N(1), explicatia c(50), luna N(2), an N(4))

	If !plArat
		If plPerioada
			lnInit = Val(Substr(pcondper, 1, 2)) + Val(Substr(pcondper, 3, 4)) * 12
			lnFinal = Val(Substr(pcondper, 8, 2)) + Val(Substr(pcondper, 10, 4)) * 12
			For k = lnInit To lnFinal
				lnLuna = Mod(k, 12)
				lnAn = Int(k / 12)
				If lnLuna = 0
					lnLuna = 12
					lnAn = lnAn - 1
				Endif
				Do VerificareLunaDeschisa With lnLuna, lnAn, .F.
			Endfor
			Select crsverificari
			Set Filter To !Empty(mesaj)
			Go Top
			obj = Createobject('frm_verificare_globala_per')
			obj.lb_titlu_alb_b121.Caption = "VERIFICARE GLOBALA PENTRU PERIOADA " + Substr(pcondper, 1, 2) + "/" + Substr(pcondper, 3, 4) + " - " + Substr(pcondper, 8, 2) + "/" + Substr(pcondper, 10, 4)
			obj.ninit = lnInit
			obj.nfinal = lnFinal
			obj.Show(1)
		Else
			Do VerificareLunaDeschisa
			If Empty(pcmesaj_refacere)
				obj = Createobject('frm_verificare_globala')
				obj.Show(1)
			Endif
		Endif
	Else
		Do VerificareLunaDeschisa
		Select crsverificari
		Replace All selectat With 1
		If Used('crsrefaceri')
			Use In crsrefaceri
		Endif
		Select sursa, Cont, tip From crsverificari Where selectat = 1 Into Cursor crsrefaceri
		If Reccount('crsrefaceri') > 0
			Local lcsursa, lccont, lctip
			Select crsrefaceri
			Scan
				lcsursa = Upper(Alltrim(sursa))
				lccont = Alltrim(Cont)
				*		lctip=tip
				lctip = '1'
				Do refacere With lcsursa, lctip, lccont In orefaceri.prg
				Select crsrefaceri
			Endscan

			If Used('crsverificari')
				Use In crsverificari
			Endif
			Create Cursor crsverificari (sursa c(20), Cont c(20), tip c(1), mesaj c(200), selectat N(1), explicatia c(50), luna N(2), an N(4))
			Do VerificareLunaDeschisa
			Select crsverificari
			Go Top
			*!*			MESSAGEBOX("Refacerea s-a incheiat!",0+64,"Info refacere")
		Endif

		Use In crsrefaceri
	Endif

	Use In crsverificari
Endproc
**********************************************************************************************************************************

**********************************************************************************************************************************
Procedure VerificareLunaDeschisa
	Lparameters tnLuna, tnAn, tlRefaceri, tlFaraTest, tlExtraCont
	&& tlRefaceri = .T. cu refaceri ; .F. fara refaceri
	&& tlTest = .T. face inainte_de
	&& llExtraCont = .T. : + STOC, STOC_OBINV

	Private pcmesaj, pctip
	Local lnselectat, lcsursa, lccont, lnNrInreg, loTherm, lcTask, lnPercent, lcexplicatia, lnInregAct, lnLuna, lnAn, llRefaceri
	Store "" To pcmesaj, pctip, lcsursa, lccont, loTherm, lcTask, lcexplicatia
	Store 0 To lnselectat, lnNrInreg, lnPercent, lnInregAct
	lnNrInreg = 6

	If !Used('crsverificari')
		Create Cursor crsverificari (sursa c(20), Cont c(20), tip c(1), mesaj c(200), selectat N(1), explicatia c(50), luna N(2), an N(4))
	Endif


	If Empty(tnLuna)
		lnLuna = gnLuna
	Else
		lnLuna = tnLuna
	Endif

	If Empty(tnAn)
		lnAn = gnAn
	Else
		lnAn = tnAn
	Endif

	If Pcount() = 3
		llRefaceri =	tlRefaceri
	Else
		llRefaceri = .T.
	Endif
	llCuTest = !tlFaraTest

	lcsursa = "BALANTA"
	If llCuTest
		Do inainte With lcsursa, '', "pcmesaj", "pctip", '', '', lnLuna, lnAn In oinainte_de.prg
	Else
		pcmesaj = ""
	Endif
	If !Empty(pcmesaj) And llRefaceri
		pcmesaj_refacere = pcmesaj + Chr(10) + Chr(13) + Chr(10) + Chr(13) + "Doriti sa refaceti balanta?"
		If Empty(plArat) Or !plArat
			If aMessagebox(pcmesaj_refacere, 4 + 32, "Diferente la balanta") == 6
				aMessagebox("Se apeleaza refacerea", 64, "Info refacere")
				lccont = ""
				Do refacere With lcsursa, pctip, lccont, lnLuna, lnAn In orefaceri.prg
				pcmesaj_refacere = ""
				Do inainte With lcsursa, '', "pcmesaj", "pctip", '', '', lnLuna, lnAn In oinainte_de.prg
			Else
				*Return
				pcmesaj_refacere = ""
			Endif
		Else
			Do refacere With lcsursa, pctip, lnLuna, lnAn In orefaceri.prg
			pcmesaj_refacere = ""
			Do inainte With lcsursa, '', "pcmesaj", "pctip", '', '', lnLuna, lnAn In oinainte_de.prg
		Endif

	Endif

	lnselectat = Iif(Empty(pcmesaj), 0, 1)
	Insert Into crsverificari (sursa, tip, mesaj, selectat, explicatia, luna, an) Values (lcsursa, pctip, pcmesaj, lnselectat, 'Balanta', lnLuna, lnAn)

	lcSql = [select distinct 1 as nrcrt,cont from ] + Alltrim(gcS) + [.config_cont_ireg ]
	lcSql = lcSql + [ union ]
	lcSql = lcSql + [select distinct 2 as nrcrt,cont from ] + Alltrim(gcS) + [.config_Cont_ireg ] + ;
		[where cu_Inregistrari = 1]
	lcSql = lcSql + [ order by cont]
	lcCursor = [crsConturi]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)
	If lnSucces < 0
		aMessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
	Endif

	If Used('crsconturi')
		lnNrInreg = lnNrInreg + Reccount('crsconturi')
	Endif

	loTherm = Newobject("_thermometer", "_therm", "", "Verificare globala " + Alltrim(Str(lnLuna)) + "/" + Alltrim(Str(lnAn)) + "...")
	With loTherm
		.Show()
		lnPercent = 0

		*!*	modificare 2007
		If gnAn < 2007
			lcexplicatia = "Registrul de vanzari"
			.Update(lnPercent, 'Verificare ' + lcexplicatia)

			lcCoresp = ['4111']
			lcExceptii = ['667,622']
			lcsursa = "VANZ"
			If llCuTest
				Do inainte With lcsursa, , "pcmesaj", "pctip", lcExceptii, lcCoresp, lnLuna, lnAn In oinainte_de.prg
			Endif
			lnselectat = Iif(Empty(pcmesaj), 0, 1)
			Insert Into crsverificari (sursa, Cont, tip, mesaj, selectat, explicatia, luna, an) Values ('TVA', '4427', pctip, pcmesaj, lnselectat, lcexplicatia, lnLuna, lnAn)


			lnPercent = (1 * 100) / lnNrInreg
			lcexplicatia = 'Registrul de cumparari'
			.Update(lnPercent, 'Verificare ' + lcexplicatia)
			lcsursa = "CUMP"
			If llCuTest
				Do inainte With lcsursa, '', "pcmesaj", "pctip", '', '', lnLuna, lnAn In oinainte_de.prg
			Endif
			lnselectat = Iif(Empty(pcmesaj), 0, 1)
			Insert Into crsverificari (sursa, Cont, tip, mesaj, selectat, explicatia, luna, an) Values ('TVA', '4426', pctip, pcmesaj, lnselectat, lcexplicatia, lnLuna, lnAn)
		Else

			lcexplicatia = "Registrul de vanzari"
			.Update(lnPercent, 'Verificare ' + lcexplicatia)

			lcCoresp = []
			lcExceptii = []
			lcsursa = "TVA_2007"
			pcmesaj = ""
			If llCuTest
				Do inainte With "VANZ2007", , "pcmesaj", "pctip", lcExceptii, lcCoresp, lnLuna, lnAn In oinainte_de.prg
			Endif
			lnselectat = Iif(Empty(pcmesaj), 0, 1)
			Insert Into crsverificari (sursa, Cont, tip, mesaj, selectat, explicatia, luna, an) Values (lcsursa, 'JV', pctip, pcmesaj, lnselectat, lcexplicatia, lnLuna, lnAn)


			lnPercent = (1 * 100) / lnNrInreg
			lcexplicatia = 'Registrul de cumparari'
			pcmesaj = ""
			.Update(lnPercent, 'Verificare ' + lcexplicatia)
			lcsursa = "TVA_2007"
			If llCuTest
				Do inainte With "CUMP2007", '', "pcmesaj", "pctip", '', '', lnLuna, lnAn In oinainte_de.prg
			Endif
			lnselectat = Iif(Empty(pcmesaj), 0, 1)
			Insert Into crsverificari (sursa, Cont, tip, mesaj, selectat, explicatia, luna, an) Values (lcsursa, 'JC', pctip, pcmesaj, lnselectat, lcexplicatia, lnLuna, lnAn)

		Endif


		If Used('crsconturi') And Reccount('crsconturi') > 0
			lnInreg = 6
			Select crsconturi
			Scan
				lnnrcrt = nrcrt
				lccont = Cont
				Do Case
					Case lnnrcrt = 1
						lnPercent = (lnInreg * 100) / lnNrInreg
						lcexplicatia = 'Balanta analitica - cont ' + lccont
						.Update(lnPercent, 'Verificare ' + lcexplicatia)
						lcsursa = "BALANTA_PARTENERI"
						pcmesaj = ""
						If llCuTest
							Do inainte With lcsursa, lccont, "pcmesaj", "pctip", '', '', lnLuna, lnAn In oinainte_de.prg
						Endif
						lnselectat = Iif(Empty(pcmesaj), 0, 1)
						Insert Into crsverificari (sursa, Cont, tip, mesaj, selectat, explicatia, luna, an) Values (lcsursa, lccont, pctip, pcmesaj, lnselectat, lcexplicatia, lnLuna, lnAn)
						lnInreg = lnInreg + 1

					Case lnnrcrt = 2
						lnPercent = (lnInreg * 100) / lnNrInreg
						lcexplicatia = 'Inregistrari - cont ' + lccont
						.Update(lnPercent, 'Verificare ' + lcexplicatia)
						lcsursa = "IREG_PARTENERI"
						pcmesaj = ""
						If llCuTest
							Do inainte With lcsursa, lccont, "pcmesaj", "pctip", '', '', lnLuna, lnAn In oinainte_de.prg
						Endif
						lnselectat = Iif(Empty(pcmesaj), 0, 1)
						Insert Into crsverificari (sursa, Cont, tip, mesaj, selectat, explicatia, luna, an) Values (lcsursa, lccont, pctip, pcmesaj, lnselectat, lcexplicatia, lnLuna, lnAn)
						lnInreg = lnInreg + 1

				Endcase
			Endscan
			If tlExtraCont
				Insert Into crsverificari (sursa, Cont, tip, mesaj, selectat, explicatia, luna, an) Values ('STOC', '', '', '', 0, 'Stocuri', lnLuna, lnAn)
				Insert Into crsverificari (sursa, Cont, tip, mesaj, selectat, explicatia, luna, an) Values ('STOC_OBINV', '', '', '', 0, 'Stocuri obiecte de inventar', lnLuna, lnAn)
			Endif
		Endif
		.Complete()
	Endwith
	Release loTherm

Endproc
