Procedure penalitati
	Lparameters tlEvaluare
	*!* 10.03.2010
	*** tlEvaluare : .T. daca se doreste un calcul al penalitatilor in functie de o data de referinta
	*** tlEvaluare : .F. (default) daca se doreste facturare penalitati la momentul incasarii facturilor restante
	*!* 10.03.2010 ^
	Private pdDataReferinta
	pdDataReferinta = Date()

	Private pcNumeFiltru, pcContractFiltru, pcExplicatiaFiltru
	Store "" To pcNumeFiltru, pcContractFiltru, pcExplicatiaFiltru

	*!* selectez incasarile din registrul jurnal care s-au incasat dupa data scadenta a facturilor corespondente
	*!*			left join cCurs ci on c.id_tipv = ci.id_tipv AND i.dataact = ci.datacurs ;
	*!*			LEFT JOIN tipv tv ON  c.id_tipv = tv.id ;

	If !tlEvaluare
		*!* 14.03.2011
		SELECT NUME, NRACT, DATAACT, PERECHE2, SUM(SUMA) as suma, SUM(SUMA_2) as suma_2 ;
			FROM act ;
			WHERE INLIST(SCC,'411','461') AND SCD <> '4118' ;
			GROUP BY NUME, NRACT, DATAACT, PERECHE2 ;
			INTO CURSOR cAct
			
		
		Select F.Id As FACT_ID, Cl.Id As clie_id, F.PL_CLIE_ID As ctr_id, F.NRFACT, F.DATAFACT, F.DATASCAD, F.VALCTVA, F.TOTVAL, F.Curs, F.VALUTA, F.INTERN, ;
			F.EXPLICATIA, C.contract, ;
			I.NUME, I.nract, I.DATAACT, I.SUMA, I.SUMA_2 AS SUMAVAL, I.PERECHE2, C.PPENZI, I.DATAACT - F.DATASCAD As NR_ZILE, ;
			round(Round(PPENZI/100*SUMA,GNZ)*(I.DATAACT - F.DATASCAD), GNZ) As penalitati ;
			from FACTURARE F Join ACNCONTRACTE C On F.PL_CLIE_ID = C.Id ;
			join ACNCLIENTI Cl On C.clie_id = Cl.Id ;
			join cAct I On F.NRFACT = I.PERECHE2 And Padr(Cl.NUME,30,' ') = Padr(I.NUME,30,' ') ;
			where F.TIP <> 6 AND I.DATAACT > F.DATASCAD ;
			order By I.NUME, F.DATAFACT, F.NRFACT, I.DATAACT, I.nract ;
			into Cursor cFiordTemp Readwrite
		*!* 14.03.2011 ^
		Select penalitati
		Scan For Year(DATAACT)*12 + Month(DATAACT) = Val(m.gcAn) * 12 + Val(m.gcNl)
			Scatter Name loRec
			Delete From cFiordTemp Where FACT_ID = loRec.FACT_ID And nract = loRec.nract And DATAACT = loRec.DATAACT
		Endscan

		&& nu facturez facturile/incasarile pe care utilizatorul le-a sters
		Select penalitati_sters
		Scan For Year(DATAACT)*12 + Month(DATAACT) = Val(m.gcAn) * 12 + Val(m.gcNl)
			Scatter Name loRec
			Delete From cFiordTemp Where FACT_ID = loRec.FACT_ID And nract = loRec.nract And DATAACT = loRec.DATAACT And SUMA = loRec.SUMA
		Endscan

		Select * From cFiordTemp Into Cursor CFIORD NOFILTER Readwrite
		Use In (Select('cFiordTemp'))
	Else

		loPenalitatiInfo = Createobject("frm_penalitati_info")
		loPenalitatiInfo.Show(1)
		If gnButon = 2
			Return
		ENDIF
		*!* 14.03.2011
		*** SELECTEZ FACTURILE DIN FACTURARE (EMISE DE COMERCIAL) DAR CU SOLD IN VANZLUN SI DEBLUN IN LUNA CURENTA
		*!* 12.09.2011
		Select i.id_fact, i.id_part, c.id_ctr, i.nract as nrfact, i.dataact as datafact, i.datascad, (i.precdeb+i.debit) as valctva, ;
		(i.precvaldeb+i.valdebit) as totval, i.Curs, i.id_valuta, i.nume_val as valuta, ;
			i.explicatia, c.contract, ;
			i.nume, 0 As nract, pdDataReferinta As DATAACT, ;
			(i.precdeb+i.debit)-(i.preccred+i.credit) As suma, ;
			(i.precvaldeb+i.valdebit)-(i.precvalcred+i.valcredit) AS sumaval, ;
			0 As PERECHE2, c.ppenzi, pdDataReferinta - i.datascad As nr_zile, ;
			round(Round(c.ppenzi/100*((i.precdeb+i.debit)-(i.preccred+i.credit)), gnPA)*(pdDataReferinta - i.datascad), gnPA) As penalitati ;
			from vireg_parteneri i Join contracte C On i.id_part = c.id_part ;
			where i.an = gnAn AND i.luna = gnLuna AND F.TIP <> 6 AND pdDataReferinta > F.DATASCAD And ;
			V.TOTCTVA - V.ACHITAT > 0 ;
			order By Cl.NUME, F.DATAFACT, F.NRFACT ;
			into Cursor CFIORD Readwrite
		*!* 12.09.2011 ^			
		*!* 14.03.2011 ^	
	Endif

	*!* 14.03.2011
	UPDATE cFiord SET valuta = "", totval = 0, sumaval = 0 WHERE intern
	GO TOP IN cFiord
	*!* 14.03.2011 ^
	
	Select NUME Where .F. From CFIORD Into Cursor cClientiTemp Readwrite
	Select Distinct NUME From CFIORD Order By NUME Into Cursor cClientiTemp2
	If _Tally > 1 && daca vreau facturile pentru toti client nu mai adaug <TOTI> pentru filtrarea facturilor
		Insert Into cClientiTemp (NUME) Values ('<TOTI>')
	Endif
	Select cClientiTemp
	Append From Dbf('cClientiTemp2')
	Use In (Select('cClientiTemp2'))

	Select CFIORD
	*!*Do Form frm_penalitati Name loPenalitati Noshow
	loPenalitati = Createobject("frm_penalitati")
	*!* 10.03.2010
	loPenalitati.lEvaluare = .T.
	loPenalitati.dDataReferinta = pdDataReferinta
	If tlEvaluare
		loPenalitati.lb_titlu_alb_b121.Caption = 'Penalitati - evaluare la ' + Dtoc(m.pdDataReferinta)
		loPenalitati.grdPenalitati.cNrDoc.Visible = .F.
		loPenalitati.grdPenalitati.cSuma.Header1.Caption = 'Sold restant'
		loPenalitati.grdPenalitati.cDataDoc.Header1.Caption = 'Data referinta'
		loPenalitati.cmd_Executa1.Visible = .F.
		loPenalitati.cmd_Executa1.Visible = .F.

	Endif
	*!* 10.03.2010 ^
	loPenalitati.Show(1)

	Use In (Select('cFiord'))

Endproc && Penalitati
