*!* 14.03.2013
*!* marius.mutu
*!* corectare mai multe facturi cu acelasi cod (nota fara predefinire) - nu aparea nume, nract, dataact in listare

*!* 23.09.2013
*!* marius.mutu
*!* Se includ si facturile fara TVA INCASARE cu valoare 0

*!* 17.10.2013
*!* marius.mutu
*!* la facturile fara TVA Incasare cu valoare 0, aparea o linie goala

*!* 23.05.2014
*!* marius.mutu
*!* am defalcat achizitiile din CE in achizitii bunuri CE si achizitii servicii CE (la cererea Silvia Carapetru)
*!* in vizualizare sunt in continuare comasate (CEB = CEBB + CESB, CET = CEBT + CEST)

*!* 23.05.2016
*!* marius.mutu
*** selectez facturile tva la incasare doar pe cele din filtrul curent (interne, import, CE, taxare inversa, total)

*!* 16.01.2019
*!* marius.mutu
*!* corectat cod tva TI19BFB in TI19B pentru facturile cu taxare inversa de pe facturile TVA_INCASARE 

*!* 09.04.2019
*!* marius.mutu
*!* corectat CEBFSC la recapitulatie

*!* 26.05.2022
*!* marius.mutu
*!* tratare bug se bloca la listare doar pe calculatorul Silviei la selectie din (cListRC, jtva_coloane)

Private pcTitlu, pcSubtitlu, pcPerioada, pcSubtitlu1, pcdataora
Local lcNumeAct, lcRaport, lcSelect, lcSql, lcWhere, lctext, lddata1, lddata2, llSucces, lnCod, lcSerieAct
Local lnDiferenta, lnIdFact, lnIncasat3, lnMeniu, lnNrCrt, lnNrCrtF, lnNrc, lnPerioada, lnSucces
Local lnTip, lnTipJurnal, lnOptiuneBug
*:Global pnAn, pnLuna

pcTitlu = "Registru cumparari"
pcSubtitlu = ""

lnTipJurnal = Thisform.nlistitemid - 1
If This.filtru_traducere() # ""
	lctext = " si " + This.filtru_traducere()
Else
	lctext = ""
Endif

pcSubtitlu1 = This.filtru_pretty_ro + m.lctext
pcPerioada = Padl(m.gnLuna,2,'0') + '/' + Alltrim(Str(m.gnAn))
****************************************************************
lnMeniu = xmenu("Registru Cumparari lunar;Registru Cumparari - recapitulatie 3 luni;Registru Cumparari - recapitulatie an curent")
If !Inlist(m.lnMeniu, 1, 2, 3)
	Return
Endif

pcdataora = get_ora(2)

&& REGISTRU TVA LUNA CURENTA
Select 0 As tip, * ;
	FROM actcv ;
	INTO Cursor cListRC ;
	ORDER By dataact, nract ;
	READWRITE

If m.lnMeniu = 1 And _Tally = 0
	amessagebox("Nu exista inregistrari pentru listare!", 48, "Atentie")
Endif

*** FILTRU PENTRU RECAPITULATIE
lcWhere = pocump.ca_baza1.cfiltru

*** STERG LUNA=XX AND AN=XXXX DIN FILTRU (in cazul in care vreau sa aduc si lunile din recapitulatie)
If Left(Upper(lcWhere),4) = 'LUNA'
	lnPos = Atc('AN=', lcWhere,1)
	If m.lnPos > 0
		lcWhere = Alltrim(Substr(m.lcWhere, m.lnPos + 11))
	Endif
Endif

Do Case
	Case m.lnTipJurnal = 1
		pcSubtitlu = "Achizitii de bunuri si prestari de sevicii taxabile din tara"
		lcRaport   = [registru_cumparari_jtva2010_ro]

		*!* facturile RO <> 0 SAU facturile cu toate coloanele <> 0
		lcWhere = m.lcWhere + [ and ((RO24B<>0 OR RO24T<>0 OR RO20B<>0 OR RO20T<>0 OR RO19B<>0 OR RO19T<>0 OR RO09B<>0 OR RO09T<>0 ] + ;
			[OR RO05B<>0 OR RO05T<>0 OR ROSCN<>0 OR ROTN<>0 OR ROTN50<>0 OR ROTN100<>0 ] + ;
			[ OR ro24nb<>0 OR ro24nt<>0 OR ro20nb<>0 OR ro20nt<>0 OR ro19nb<>0 OR ro19nt<>0 ro09nb<>0 OR ro09nt<>0 OR ro05nb<>0 OR ro05nt<>0) OR ] + ;
			[(TOTCTVA = 0  and RO24B = 0 and RO24T = 0 and RO20B = 0 and RO20T = 0 and RO19B = 0 and RO19T = 0 and RO09B = 0 and RO09T = 0 ] + ;
			[ and RO05B = 0 and RO05T = 0 and ROSCN = 0 and ROTN = 0 and ROTN50 = 0 and ROTN100 = 0 ] + ;
			[ and FO24B = 0 and FO24T = 0 and FO20B = 0 and FO20T = 0 and FO19B = 0 and FO19T = 0 and FO09B = 0 and FO09T = 0 and FOSCN = 0 ] + ;
			[ and CEB = 0 and CET = 0 and CESC = 0 and CEN = 0 ] + ;
			[ and XX19TIB = 0 AND XX19TIT = 0 ] + ;
			[ and TI24B = 0 and TI24T = 0 and TI20B = 0 and TI20T = 0 and TI19B = 0 and TI19T = 0 and TI09B = 0 and TI09T = 0 ] + ;
			[ and ro24nb = 0 and ro24nt = 0 and ro20nb = 0 and ro20nt = 0 and ro19nb = 0 and ro19nt = 0 and ro09nb = 0 and ro09nt = 0 and ro05nb = 0 and ro05nt = 0))]

	Case  m.lnTipJurnal = 2
		pcSubtitlu = "Achizitii de bunuri si prestari de sevicii taxabile din import"
		lcRaport   = [registru_cumparari_jtva2010_fo]

		lcWhere = m.lcWhere + [ and (FO24B<>0 OR FO24T<>0 OR FO20B<>0 OR FO20T<>0 OR FO19B<>0 OR FO19T<>0 OR FO09B<>0 OR FO09T<>0 OR FOSCN<>0)]

	Case m.lnTipJurnal = 3
		pcSubtitlu = "Achizitii intracomunitare de bunuri"
		lcRaport   = [registru_cumparari_jtva2010_ce]

		lcWhere = m.lcWhere + [ and (CEB<>0 OR CET<>0 OR CESC<>0 OR CEN<>0)]

	Case  m.lnTipJurnal = 4
		pcSubtitlu = "Bunuri si Sevicii pentru care cumparatorul este obligat la plata taxei (din Tara, Import) - Taxare Inversa"
		lcRaport   = [registru_cumparari_jtva2010_ti]

		lcWhere = m.lcWhere + [ and (TI24B<>0 OR TI24T<>0 OR TI20B<>0 OR TI20T<>0 OR TI19B<>0 OR TI19T<>0 OR TI09B<>0 OR TI09T<>0 OR XX19TIB <> 0 OR XX19TIT <> 0)]

Endcase

&& REGISTRE TVA RECAPITULATIE
If INLIST(m.lnMeniu, 2, 3) && recapitulatie 3 luni  / an curent
	Private m.pnAn, m.pnLuna
	IF m.lnMeniu = 2
		* recapitulatie 3 luni
		lnPerioada = Ceil(m.gnLuna / 3)		
		pnLuna	   = (m.lnPerioada - 1) * 3 + 1
	ELSE
		pnLuna = 1
	ENDIF  	
	pnAn	   = m.gnAn

	lddata1	= Date(m.pnAn, m.pnLuna, 1)
	lddata2	= Date(m.gnAn, m.gnLuna, 1)

	pcPerioada = m.pcPerioada + " * Recap. " + Padl(m.pnLuna, 2, '0') + '/' + Alltrim(Str(m.pnAn)) + ' - ' + Padl(m.gnLuna, 2, '0') + '/' + Alltrim(Str(m.gnAn))
	TEXT to lcSelect textmerge noshow
	select 1 as tip, v.an, v.luna, cast(0 as Number(20)) as cod, to_date('','ddmmyyyy') as dataact, 000000000 as nract,
       (select la.numeluna || ' ' || to_char(v.an) from lunilean la where la.nrluna = v.luna) as denumire,
        SUM(v.TOTCTVA) AS TOTCTVA,
		SUM(v.CEB) AS CEB, SUM(v.CET) AS CET, 
		SUM(v.CEBB) AS CEBB, SUM(v.CEBT) AS CEBT,
		SUM(v.CESB) AS CESB, SUM(v.CEST) AS CEST,		
		SUM(v.CEN) AS CEN,
		SUM(v.CESC) AS CESC,
		SUM(v.FOSCN) AS FOSCN, SUM(v.FO09B) AS FO09B, SUM(v.FO09T) AS FO09T,
		SUM(v.FO24B) AS FO24B, SUM(v.FO24T) AS FO24T, SUM(v.FO20B) AS FO20B, SUM(v.FO20T) AS FO20T, SUM(v.FO19B) AS FO19B, SUM(v.FO19T) AS FO19T,
		SUM(v.XX19TIB) AS XX19TIB, SUM(v.XX19TIT) AS XX19TIT,
		SUM(v.ROSCN) AS ROSCN, SUM(v.RO09B) AS RO09B, SUM(v.RO09T) AS RO09T, SUM(v.RO05B) AS RO05B, SUM(v.RO05T) AS RO05T,
		SUM(v.RO24B) AS RO24B, SUM(v.RO24T) AS RO24T, SUM(v.RO20B) AS RO20B, SUM(v.RO20T) AS RO20T, SUM(v.RO19B) AS RO19B, SUM(v.RO19T) AS RO19T, Sum(v.ROTN) AS ROTN, Sum(v.ROTN50) AS ROTN50, Sum(v.ROTN100) AS ROTN100,
		SUM(v.TI09B) AS TI09B, SUM(v.TI09T) AS TI09T, SUM(v.TI19B) AS TI19B, SUM(v.TI19T) AS TI19T, SUM(v.TI24B) AS TI24B, SUM(v.TI24T) AS TI24T, SUM(v.TI20B) AS TI20B, SUM(v.TI20T) AS TI20T
		FROM (select <<m.pnAn>> as an, <<m.pnLuna>> + rownum - 1 as luna from dual connect by level <= <<(m.gnAn*12 + m.gnLuna) - (m.pnAn*12 + m.pnLuna ) + 1>>) l
		left JOIN VJC2010 V on v.an = l.an and v.luna = l.luna
		WHERE v.an*12+v.luna between ?pnAn*12 + ?pnLuna and ?gnAn*12 + ?gnLuna <<Iif(!Empty(m.lcWhere), " and " + m.lcWhere, "")>>
		GROUP BY v.an, v.luna
		ORDER BY v.an, v.luna
	ENDTEXT

	lnSucces = goExecutor.oExecute(m.lcSelect, "crsTotal")
	If m.lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
	Else
		Replace All dataact With Gomonth(Date(an, luna, 1), 120) In crsTotal
		Replace All cod With Int(Val(Dtos(dataact))) In crsTotal

		Select cListRC
		Append From Dbf('crsTotal')
		Use In crsTotal
	Endif
Endif && m.lnMeniu  = 2

*** FORMAT TVA INCASARE 2013
Select cListRC
Index On denumire + Str(nract, 14, 0) Tag numeact
Index On cod Tag cod

update_jtva_coloane('JC')

*** SELECTEZ FACTURILE CU TVA_INCASARE CU TOTAL BAZA, TVA, BAZAN, TVAN
*** doar pe cele din filtrul curent (interne, import, CE, taxare inversa, total)
Select r.cod, r.id_fact, r.tip, r.denumire, r.nract, r.SERIE_ACT, r.dataact, r.COD_FISCAL, Space(10) As COD_TVA, Space(100) As EXPLICATIE_TVA, r.TOTCTVA, r.TOTFTVATAX, r.TOTTVATAX, TVA_INCASARE, ;
	(RO24B + RO20B + RO19B + RO09B + RO05B + ROSCN + TI09B + TI19B + TI24B + TI20B + CEB + CEN + CESC + FOSCN + FO09B + FO19B + XX19TIB + FO24B + FO20B) As BAZA, ;
	(RO24T + RO20T + RO19T + RO09T + RO05T + TI09T + TI19T + TI24T + TI20T + CET + FO09T + FO19T + XX19TIT + FO24T + FO20T) As TVA, ;
	(RO24NB + RO20NB + RO19NB + RO09NB + RO05NB) As BAZAN, ;
	(RO24NT + RO20NT + RO19NT + RO09NT + RO05NT) As TVAN, ;
	cast(0 As N(14)) As NRACTI, Cast({} As D) As DATAACTI, Space(20) As FDOCI, Cast(0 As N(16, 4)) As INCASAT ;
	FROM cListRC r ;
	where tip = 0 And TVA_INCASARE = 1 ;
	INTO Cursor cListRCTemp Readwrite

Select cListRCTemp
Index On id_fact Tag id_fact
Set Order To


*** SELECTEZ FACTURILE FARA TVA_INCASARE CU TOTAL, BAZA, TVA
* tratare bug se bloca la listare doar pe calculatorul Silviei la selectie din (cListRC, jtva_coloane)
* Pe un calculator statea 10 minute pana facea selectia 
* Am refacut join-ul ca doua scan-uri
lnOptiuneBug = INT(VAL(NVL(citeste_optiune_utilizator('RC_REGCUMP_LISTARE_BUG'), '0')))

IF m.lnOptiuneBug = 1
	SELECT r.cod, r.id_fact, r.tip, r.denumire, r.nract, r.SERIE_ACT, r.dataact, r.COD_FISCAL, SPACE(20) as COD_TVA, SPACE(250) as EXPLICATIE_TVA, r.TOTCTVA, r.TOTFTVATAX, r.TOTTVATAX, TVA_INCASARE, ;
		CAST(0 As N(16, 4)) As BAZA, ;
		CAST(0 As N(16, 4)) As TVA ;
		FROM cListRC r ;
		WHERE .F. ;
		INTO Cursor cListRCTemp1 Readwrite

	SELECT cListRC
	lcRecc = ALLTRIM(STR(RECCOUNT()))
	SCAN FOR tip = 0 AND tva_incasare = 0
		SCATTER NAME loRC
		WAIT WINDOW 'Facturi fara TVA Incasare ' + ALLTRIM(STR(RECNO())) + '/'+ m.lcRecc NOWAIT 
		ADDPROPERTY(loRC,'cod_tva', '')
		ADDPROPERTY(loRC,'explicatie_tva', '')
		ADDPROPERTY(loRC,'baza',0.00)
		ADDPROPERTY(loRC,'tva', 0.00)
			
		SELECT JTVA_COLOANE 
		SCAN FOR jc = 1 And INLIST(ALLTRIM(coloana_jc), 'RO24B','RO20B', 'RO19BVB', 'RO09BVB', 'RO05B', 'ROSCN', 'TI09BFB', 'TI19BFB', 'TI24B', 'TI20B', ;
			 'CEB', 'CE24SB', 'CEBFN', 'CEBFSC','FO09BVB', 'FO19BVB', 'XX19TIB', 'FO24B', 'FO20B','ROTN','ROTN50','ROTN100')   
			loRC.cod_tva = ALLTRIM(coloana_jc)
			loRC.explicatie_tva = ALLTRIM(denumire)
			
			loRC.baza = Iif(loRC.cod_tva = 'RO24B', loRC.RO24B, ;
			Iif(loRC.cod_tva = 'RO20B', loRC.RO20B, ;	
			IIF(loRC.cod_tva = 'RO19BVB', loRC.RO19B, ;
			IIF(loRC.cod_tva = 'RO09BVB', loRC.RO09B, ;
			IIF(loRC.cod_tva = 'RO05B', loRC.RO05B, ;
			IIF(loRC.cod_tva = 'ROSCN', loRC.ROSCN, ;
			IIF(loRC.cod_tva = 'TI09BFB', loRC.TI09B, ;
			IIF(loRC.cod_tva = 'TI19BFB', loRC.TI19B, ;
			IIF(loRC.cod_tva = 'TI24B', loRC.TI24B, ;
			IIF(loRC.cod_tva = 'TI20B', loRC.TI20B, ;	
			IIF(loRC.cod_tva = 'CEB', loRC.CEBB, ;
			IIF(loRC.cod_tva = 'CE24SB', loRC.CESB, ;	
			IIF(loRC.cod_tva = 'CEBFN', loRC.CEN, ;
			IIF(loRC.cod_tva = 'CEBFSC', loRC.CESC, ;
			IIF(loRC.cod_tva = 'FOSCN', loRC.FOSCN, ;
			IIF(loRC.cod_tva = 'FO09BVB', loRC.FO09B, ;
			IIF(loRC.cod_tva = 'FO19BVB', loRC.FO19B, ;
			IIF(loRC.cod_tva = 'XX19TIB', loRC.XX19TIB, ;	
			IIF(loRC.cod_tva = 'FO24B', loRC.FO24B, ;
			IIF(loRC.cod_tva = 'FO20B', loRC.FO20B, ;	
			0))))))))))))))))))))
			
			loRC.tva = Iif(loRC.cod_tva = 'RO24B', loRC.RO24T, ;
			Iif(loRC.cod_tva = 'RO20B', loRC.RO20T, ;	
			IIF(loRC.cod_tva = 'RO19BVB', loRC.RO19T, ;
			IIF(loRC.cod_tva = 'RO09BVB', loRC.RO09T, ;
			IIF(loRC.cod_tva = 'RO05B', loRC.RO05T, ;
			IIF(loRC.cod_tva = 'TI09BFB', loRC.TI09T, ;
			IIF(loRC.cod_tva = 'TI19BFB', loRC.TI19T, ;
			IIF(loRC.cod_tva = 'TI24B', loRC.TI24T, ;
			IIF(loRC.cod_tva = 'TI20B', loRC.TI20T, ;	
			IIF(loRC.cod_tva = 'CEB', loRC.CEBT, ;
			IIF(loRC.cod_tva = 'CE24SB', loRC.CEST, ;	
			IIF(loRC.cod_tva = 'FO09BVB', loRC.FO09T, ;
			IIF(loRC.cod_tva = 'FO19BVB', loRC.FO19T, ;
			IIF(loRC.cod_tva = 'XX19TIB', loRC.XX19TIT, ;	
			IIF(loRC.cod_tva = 'FO24B', loRC.FO24T, ;
			IIF(loRC.cod_tva = 'FO20B', loRC.FO20T, ;	
			IIF(loRC.cod_tva = 'ROTN', loRC.ROTN, ;
			IIF(loRC.cod_tva = 'ROTN50', loRC.ROTN50, ;
			IIF(loRC.cod_tva = 'ROTN100', loRC.ROTN100, ;		
			0)))))))))))))))))))

			IF !(loRC.baza = 0 AND loRC.tva = 0)
				SELECT cListRCTemp1 
				APPEND BLANK 
				GATHER NAME loRC
			ENDIF 
			
		ENDSCAN && JTVA_COLOANE  JC = 1 And !Empty(Nvl(J.COLOANA_JC, ''))
	ENDSCAN && cListRC tip = 0 AND tva_incasare = 0
ELSE 
	Select r.cod, r.id_fact, r.tip, r.denumire, r.nract, r.SERIE_ACT, r.dataact, r.COD_FISCAL, J.COLOANA_JC As COD_TVA, J.denumire As EXPLICATIE_TVA, r.TOTCTVA, r.TOTFTVATAX, r.TOTTVATAX, TVA_INCASARE, ;
		CAST(Iif(J.COLOANA_JC = 'RO24B', r.RO24B, ;
		Iif(J.COLOANA_JC = 'RO20B', r.RO20B, ;	
		IIF(J.COLOANA_JC = 'RO19BVB', r.RO19B, ;
		IIF(J.COLOANA_JC = 'RO09BVB', r.RO09B, ;
		IIF(J.COLOANA_JC = 'RO05B', r.RO05B, ;
		IIF(J.COLOANA_JC = 'ROSCN', r.ROSCN, ;
		IIF(J.COLOANA_JC = 'TI09BFB', r.TI09B, ;
		IIF(J.COLOANA_JC = 'TI19BFB', r.TI19B, ;
		IIF(J.COLOANA_JC = 'TI24B', r.TI24B, ;
		IIF(J.COLOANA_JC = 'TI20B', r.TI20B, ;	
		IIF(J.COLOANA_JC = 'CEB', r.CEBB, ;
		IIF(J.COLOANA_JC = 'CE24SB', r.CESB, ;	
		IIF(J.COLOANA_JC = 'CEBFN', r.CEN, ;
		IIF(J.COLOANA_JC = 'CEBFSC', r.CESC, ;
		IIF(J.COLOANA_JC = 'FOSCN', r.FOSCN, ;
		IIF(J.COLOANA_JC = 'FO09BVB', r.FO09B, ;
		IIF(J.COLOANA_JC = 'FO19BVB', r.FO19B, ;
		IIF(J.COLOANA_JC = 'XX19TIB', r.XX19TIB, ;	
		IIF(J.COLOANA_JC = 'FO24B', r.FO24B, ;
		IIF(J.COLOANA_JC = 'FO20B', r.FO20B, ;	
		0)))))))))))))))))))) As N(16, 4)) As BAZA, ;
		CAST(Iif(J.COLOANA_JC = 'RO24B', r.RO24T, ;
		Iif(J.COLOANA_JC = 'RO20B', r.RO20T, ;	
		IIF(J.COLOANA_JC = 'RO19BVB', r.RO19T, ;
		IIF(J.COLOANA_JC = 'RO09BVB', r.RO09T, ;
		IIF(J.COLOANA_JC = 'RO05B', r.RO05T, ;
		IIF(J.COLOANA_JC = 'TI09BFB', r.TI09T, ;
		IIF(J.COLOANA_JC = 'TI19BFB', r.TI19T, ;
		IIF(J.COLOANA_JC = 'TI24B', r.TI24T, ;
		IIF(J.COLOANA_JC = 'TI20B', r.TI20T, ;	
		IIF(J.COLOANA_JC = 'CEB', r.CEBT, ;
		IIF(J.COLOANA_JC = 'CE24SB', r.CEST, ;	
		IIF(J.COLOANA_JC = 'FO09BVB', r.FO09T, ;
		IIF(J.COLOANA_JC = 'FO19BVB', r.FO19T, ;
		IIF(J.COLOANA_JC = 'XX19TIB', r.XX19TIT, ;	
		IIF(J.COLOANA_JC = 'FO24B', r.FO24T, ;
		IIF(J.COLOANA_JC = 'FO20B', r.FO20T, ;	
		IIF(J.COLOANA_JC = 'ROTN', r.ROTN, ;
		IIF(J.COLOANA_JC = 'ROTN50', r.ROTN50, ;
		IIF(J.COLOANA_JC = 'ROTN100', r.ROTN100, ;		
		0))))))))))))))))))) As N(16, 4)) As TVA ;
		FROM cListRC r, JTVA_COLOANE J ;
		where tip = 0 And r.TVA_INCASARE = 0 And J.JC = 1 And !Empty(Nvl(J.COLOANA_JC, '')) ;
		INTO Cursor cListRCTemp1 Readwrite
ENDIF && gnRC_REGCUMP_LISTARE_BUG

*** SELECTEZ FACTURILE CU VALOARE 0 pe toate randurile
WAIT WINDOW 'Facturi fara TVA Incasare cu valoare 0' NOWAIT
Select distinct cod, id_fact, tip, denumire, nract, SERIE_ACT, dataact, COD_FISCAL, '' as COD_TVA, '' as EXPLICATIE_TVA, TOTCTVA, TOTFTVATAX, TOTTVATAX, TVA_INCASARE, ;
	baza, tva ;
	FROM cListRCTemp1 ;
	WHERE BAZA = 0 And TVA = 0 ;
	AND id_fact NOT in (SELECT id_fact FROM cListRCTemp1 Where !(BAZA = 0 And TVA = 0)) ;	
	INTO CURSOR cListRCTemp2 NOFILTER 

Delete From cListRCTemp1 Where BAZA = 0 And TVA = 0
Update cListRCTemp1 Set COD_TVA = 'RO19B', EXPLICATIE_TVA = 'ACH. INT. 19%' Where Left(COD_TVA,5) = 'RO19B'
Update cListRCTemp1 Set COD_TVA = 'RO09B', EXPLICATIE_TVA = 'ACH. INT. 9%' Where Left(COD_TVA,5) = 'RO09B'

Update cListRCTemp1 Set COD_TVA = 'FO19B', EXPLICATIE_TVA = 'ACH. IMP. 19%' Where Left(COD_TVA,5) = 'FO19B'
Update cListRCTemp1 Set COD_TVA = 'FO09B', EXPLICATIE_TVA = 'ACH. IMP. 9%' Where Left(COD_TVA,5) = 'FO09B'

Update cListRCTemp1 Set COD_TVA = 'TI19B', EXPLICATIE_TVA = 'ACH. TX. INV. 19%' Where Left(COD_TVA,5) = 'TI19B'
Update cListRCTemp1 Set COD_TVA = 'TI09B', EXPLICATIE_TVA = 'ACH. TX INV. 9%' Where Left(COD_TVA,5) = 'TI09B'

* TOATE ACHIZITIILE CE, FARA NEIMPOZABILE (..N) SI SCUTITE (..SC)
Update cListRCTemp1 Set EXPLICATIE_TVA = 'ACH. BUNURI CE' Where Left(COD_TVA,3) = 'CEB' AND !INLIST(RIGHT(ALLTRIM(COD_TVA), 1), 'C', 'N')
Update cListRCTemp1 Set EXPLICATIE_TVA = 'ACH. BUNURI CE' Where Left(COD_TVA,5) = 'CE20B'
Update cListRCTemp1 Set COD_TVA = 'CES', EXPLICATIE_TVA = 'ACH. SERVICII CE' Where Left(COD_TVA,6) = 'CE24SB'
Update cListRCTemp1 Set COD_TVA = 'CES', EXPLICATIE_TVA = 'ACH. SERVICII CE' Where Left(COD_TVA,6) = 'CE20SB'
	
Select cListRCTemp
Append From Dbf('cListRCTemp1')
Append From Dbf('cListRCTemp2')
Use In (Select('cListRCTemp1'))
Use In (Select('cListRCTemp2'))

*** SELECTEZ EXPLICATIILE DE TVA 0% EXIGIBILE IN PERIOADA PENTRU FACTURILE CU TVA_INCASARE
*** PE FACTURILE TVA_INCASARE POATE APAREA SI TAXARE INVERSA
WAIT WINDOW 'Facturi TVA 0% exigibile in perioada pentru facturile cu TVA Incasare' NOWAIT
Select r.cod, r.id_fact, r.tip, r.denumire, r.nract, r.SERIE_ACT, r.dataact, r.COD_FISCAL, J.COLOANA_JC As COD_TVA, J.denumire As EXPLICATIE_TVA, ;
	CAST(Iif(J.COLOANA_JC = 'ROSCN', r.ROSCN, ;
	IIF(J.COLOANA_JC = 'CEBFSC', r.CESC, ;
	IIF(J.COLOANA_JC = 'FOSCN', r.FOSCN, ;
	IIF(J.COLOANA_JC = 'TI09BFB', r.TI09B, ;	
	IIF(J.COLOANA_JC = 'TI19BFB', r.TI19B, ;	
	IIF(J.COLOANA_JC = 'TI24B', r.TI24B, ;	
	IIF(J.COLOANA_JC = 'TI20B', r.TI20B, ;
	0))))))) As N(16, 4)) As BAZA, ;
	CAST(Iif(J.COLOANA_JC = 'ROTN', r.ROTN, ;
	Iif(J.COLOANA_JC = 'ROTN50', r.ROTN50, ;
	Iif(J.COLOANA_JC = 'ROTN100', r.ROTN100, ;	
	IIF(J.COLOANA_JC = 'TI09BFB', r.TI09T, ;	
	IIF(J.COLOANA_JC = 'TI19BFB', r.TI19T, ;	
	IIF(J.COLOANA_JC = 'TI24B', r.TI24T, ;	
	IIF(J.COLOANA_JC = 'TI20B', r.TI20T, ;	
	0))))))) As N(16, 4)) As TVA ;
	FROM cListRC r, JTVA_COLOANE J ;
	WHERE tip = 0 And r.TVA_INCASARE = 1 And J.JC = 1 And !Empty(Nvl(J.COLOANA_JC, '')) And (Nvl(J.COTA_TVA, 0) = 0 OR LEFT(J.COLOANA_JC,2) = 'TI');
	INTO Cursor cListRCTemp1 Readwrite

Delete From cListRCTemp1 Where BAZA = 0 And TVA = 0

Update cListRCTemp1 Set COD_TVA = 'TI19B', EXPLICATIE_TVA = 'ACH. TX. INV. 19%' Where Left(COD_TVA,5) = 'TI19B'
Update cListRCTemp1 Set COD_TVA = 'TI09B', EXPLICATIE_TVA = 'ACH. TX INV. 9%' Where Left(COD_TVA,5) = 'TI09B'

Select cListRCTemp
Append From Dbf('cListRCTemp1')
Use In (Select('cListRCTemp1'))

*!*	*** Daca am pus filtru in RC, sterg facturile care nu se potrivesc filtrului
*!*	If !Empty(m.lcWhere)
*!*		Delete From cListRCTemp Where Padl(nract,14,'0') + '|' + Dtos(dataact) + '|' + Padr(denumire,100,' ') Not In ;
*!*			(Select Padl(nract,14,'0') + '|' + Dtos(dataact) + '|' + Padr(denumire,100,' ') From cListRC WHERE !EMPTY(NVL(denumire,'')) )
*!*	Endif

WAIT WINDOW 'TVA exigibilizat din plati facturi cu TVA Incasare' NOWAIT
*** ADAUG TVA INCASARE (DIN INCASARI/PLATI, TVA 4427 LA 90 ZILE)
TEXT to m.lcSql textmerge noshow
	select cast(0 as Number(10)) as nrc, a.cod, 2 As tip, jc.id_fact, jc.nract As nract, jc.serie_act, jc.dataact, a.nract As NRACTI, a.dataact As DATAACTI,  fdoc.fel_document as FDOCI, p.denumire As denumire, p.COD_FISCAL,
	J.COTA_TVA, J2.COLOANA_JC As COD_TVA, J2.denumire As EXPLICATIE_TVA,
	round(a.suma * 100 / J.COTA_TVA, 2) As BAZA, a.suma As TVA,
	round(a.suma * 100 / J.COTA_TVA, 2) + a.suma As INCASAT,
	(select sum(suma) from act a1 where a1.an = ?m.gnAn and a1.luna = ?m.gnLuna and a1.sters = 0 and a1.id_factd = a.id_fact and a1.cod = a.cod) as INCASAT2
	from act a left join jc2007 jc on a.id_fact = jc.id_fact
    left join nom_parteneri p on jc.id_part = p.id_part
	Left Join JTVA_COLOANE J On a.id_jtva_coloana = J.id_jtva_coloana
	LEFT Join JTVA_COLOANE J2 On J.id_jtva_coloana = J2.id_tva
	left join nom_fdoc fdoc on a.id_fdoc = fdoc.id_fdoc
	where a.an = ?m.gnAn and a.luna = ?m.gnLuna and a.sters = 0 and a.scd = '4426' And a.scc = '4428' And Nvl(a.id_jtva_coloana, 0) <> 0
	and jc.an = ?m.gnAn and jc.luna = ?m.gnLuna and (jc.ro24nt <> 0 or jc.ro20nt <> 0 or jc.ro19nt <> 0 or jc.ro9nt <> 0 or jc.ro5nt <> 0)
	ORDER BY a.cod, J2.COLOANA_JC
ENDTEXT

*!*	round(a.suma * 100 / J.COTA_TVA, 2) As BAZA
*!*	Round(a.suma * (100 + J.COTA_TVA) / J.COTA_TVA, 2) As INCASAT

llSucces = goExecutor.oExecuta(m.lcSql, "cListRcTemp1")

If m.llSucces
	Update cListRCTemp1 Set COD_TVA = 'RO19B', EXPLICATIE_TVA = 'ACH. INT. 19%' Where Left(COD_TVA,5) = 'RO19B'
	Update cListRCTemp1 Set COD_TVA = 'RO09B', EXPLICATIE_TVA = 'ACH. INT. 9%' Where Left(COD_TVA,5) = 'RO09B'


	*** daca am pus filtru in Registru pe Nume sau altceva
	*** sterg incasarile/platile care nu apartin de facturile filtrate
	*** ca sa am doar incasarile/platile care tin de filtru
	If !Empty(m.lcWhere)
		Delete From cListRCTemp1 Where Padl(nract,14,'0') + '|' + Dtos(dataact) + '|' + Padr(denumire,100,' ') Not In ;
			(Select Padl(nract,14,'0') + '|' + Dtos(dataact) + '|' + Padr(denumire,100,' ') From cListRCTemp WHERE !EMPTY(NVL(denumire,'')) )
	Endif

	***  ROTUNJIRE BAZA EXIGIBILA = BAZA INCASAT - TVA EXIGIBIL PENTRU FIECARE INCASARE
	*** INCASAT ESTE TOATA SUMA INCASATA, TREBUIE SA CALCULEZ BAZELE AFERENTE TVA-URILOR DIN SUMA INCASATA
	Select cListRCTemp1
	Go Top
	lnCod = cod
	lnNrAct = nract
	lcSerieAct = NVL(serie_act, '')
	ldDataAct = dataact
	lcDenumire = denumire
	lnNrc = 0
	Scan
		lnNrc = m.lnNrc + 1
		If m.lnCod <> cod Or m.lnNrAct <> nract OR m.lcSerieAct <> NVL(serie_act, '') Or m.ldDataAct <> dataact Or m.lcDenumire <> denumire
			lnNrc = 1
		Endif
		lnCod = cod
		Replace nrc With m.lnNrc
	Endscan

	*** adun diferenta la prima baza
	Select cod, nract, serie_act, dataact, denumire, INCASAT2, Sum(INCASAT) As INCASAT ;
		from cListRCTemp1 ;
		group By cod, nract, serie_act, dataact, denumire, INCASAT2 ;
		into Cursor cIncasariTVA

	Select cIncasariTVA
	Scan
		lnCod = cod
		lnNrAct = nract
		lcSerieAct = NVL(serie_act, '')
		ldDataAct = dataact
		lcDenumire = denumire
		lnIncasat = Nvl(INCASAT, 0)
		lnIncasat2 = Nvl(INCASAT2,0)
		lnIncasat3 = 0 && suma baza EXPLICATII DE TVA 0% EXIGIBILE

		*!*			select cListRcTemp1
		*!*			locate for cod = m.lnCod and nrc = 1
		*!*			if found()
		*!*				lnIdFact = id_fact
		*!*				select cListRcTemp
		*!*				calculate sum(baza) to lnIncasat3 for id_Fact = m.lnIdFact and tip = 0 and tva = 0 in cListRcTemp && baza TVA 0%
		*!*			endif
		Calculate Sum(BAZA) To lnIncasat3 For denumire = m.lcDenumire And nract = m.lnNrAct AND NVL(serie_act, '') = m.lcSerieAct And dataact = m.ldDataAct And tip = 0 And TVA = 0 In cListRCTemp && baza TVA 0%
		lnDiferenta = Iif(m.lnIncasat2 <> 0, m.lnIncasat2 - m.lnIncasat - m.lnIncasat3, 0)

		*** exista posibilitatea ca tva-ul din suma achitata sa nu fie corespunzator intregii sume (daca am si limitare deductibilitate tva)
		*** daca este prea mare diferenta dintre suma incasata si baza calculata, nu mai adun diferenta
		If Abs(m.lnDiferenta) > 1
			m.lnDiferenta = 0
		Endif
		If m.lnDiferenta <> 0
			Select cListRCTemp1
			Locate For cod = m.lnCod And denumire = m.lcDenumire And nract = m.lnNrAct AND NVL(serie_act, '') = m.lcSerieAct And dataact = m.ldDataAct And nrc = 1
			If Found()
				Replace INCASAT With INCASAT + m.lnDiferenta,  BAZA With BAZA + m.lnDiferenta
			Endif
		Endif
	Endscan
	Use In (Select('cIncasariTVA'))

	Update cListRCTemp1 Set nract = 0 Where Isnull(nract)
	Update cListRCTemp1 Set denumire = '' Where Isnull(denumire)
	Update cListRCTemp1 Set COD_FISCAL = '' Where Isnull(COD_FISCAL)
	Update cListRCTemp1 Set FDOCI = '' Where Isnull(FDOCI)

	Select cListRCTemp
	Append From Dbf('cListRcTemp1')

	*** ROTUNJIRE BAZA FACTURA - BAZE EXIGIBILE
	*** bazele exigibile de pe facturile cu TVA INCASARE
	Select nract, serie_act, dataact, denumire, Sum(BAZA) As BAZA ;
		from cListRCTemp ;
		where TVA_INCASARE = 0 And tip = 0 And TOTCTVA <> 0 ;
		group By nract, serie_act, dataact, denumire ;
		into Cursor cFacturiTVAIncasare

	Select cFacturiTVAIncasare
	Scan
		lnNrAct = nract
		lcSerieAct = NVL(serie_act, '')
		ldDataAct = dataact
		lcDenumire = denumire
		lnBazaFact = BAZA
		*** baza exigibila din tip = 0 (tva 0%) si tip = 2 (incasari/plati)
		Calculate Sum(BAZA) To lnBazaExigibila In cListRCTemp For TVA_INCASARE = 0 And Inlist(tip, 0, 2) And TOTCTVA = 0 And denumire = m.lcDenumire And ;
			nract = m.lnNrAct AND NVL(serie_act, '') = m.lcSerieAct And dataact = m.ldDataAct

		lnDiferenta = Iif(m.lnBazaFact <> 0, m.lnBazaFact - m.lnBazaExigibila, 0)
		If m.lnDiferenta <> 0
			Select cListRCTemp
			Locate For BAZA <> 0 And TVA_INCASARE = 0 And tip = 2 And TOTCTVA = 0 And denumire = m.lcDenumire And nract = m.lnNrAct ;
				AND NVL(serie_act, '') = m.lcSerieAct And dataact = m.ldDataAct
			If Found()
				Replace INCASAT With BAZA + m.lnDiferenta + TVA, BAZA With BAZA + m.lnDiferenta
			Endif
		Endif
	Endscan
	Use In (Select('cFacturiTVAIncasare'))

Endif
Use In (Select('cListRcTemp1'))

WAIT WINDOW 'Recapitulatie' NOWAIT
*** SELECTEZ INREGISTRARILE TIP = 1 RECAPITULATIE
IF m.lnOptiuneBug = 1
	SELECT r.cod, r.id_fact, r.tip, r.denumire, r.nract, r.SERIE_ACT, r.dataact, r.COD_FISCAL, SPACE(20) as COD_TVA, SPACE(250) as EXPLICATIE_TVA, r.TOTCTVA, r.TOTFTVATAX, r.TOTTVATAX, TVA_INCASARE, ;
		CAST(0 As N(16, 4)) As BAZA, ;
		CAST(0 As N(16, 4)) As TVA ;
		FROM cListRC r ;
		WHERE .F. ;
		INTO Cursor cListRCTemp1 Readwrite

	SELECT cListRC
	lcRecc = ALLTRIM(STR(RECCOUNT()))
	SCAN FOR tip = 1
		SCATTER NAME loRC
		WAIT WINDOW 'Recapitulatie ' + ALLTRIM(STR(RECNO())) + '/'+ m.lcRecc NOWAIT 
		ADDPROPERTY(loRC,'cod_tva', '')
		ADDPROPERTY(loRC,'explicatie_tva', '')
		ADDPROPERTY(loRC,'baza',0.00)
		ADDPROPERTY(loRC,'tva', 0.00)
			
		SELECT JTVA_COLOANE 
		SCAN FOR jc = 1 And INLIST(ALLTRIM(coloana_jc), 'RO24B','RO20B', 'RO19BVB', 'RO09BVB', 'RO05B', 'ROSCN', 'TI09BFB', 'TI19BFB', 'TI24B', 'TI20B', ;
			 'CEB', 'CE24SB', 'CEBFN', 'CEBFSC','FO09BVB', 'FO19BVB', 'XX19TIB', 'FO24B', 'FO20B','ROTN','ROTN50','ROTN100')   
			loRC.cod_tva = ALLTRIM(coloana_jc)
			loRC.explicatie_tva = ALLTRIM(denumire)
			
			loRC.baza = Iif(loRC.cod_tva = 'RO24B', loRC.RO24B, ;
			Iif(loRC.cod_tva = 'RO20B', loRC.RO20B, ;	
			IIF(loRC.cod_tva = 'RO19BVB', loRC.RO19B, ;
			IIF(loRC.cod_tva = 'RO09BVB', loRC.RO09B, ;
			IIF(loRC.cod_tva = 'RO05B', loRC.RO05B, ;
			IIF(loRC.cod_tva = 'ROSCN', loRC.ROSCN, ;
			IIF(loRC.cod_tva = 'TI09BFB', loRC.TI09B, ;
			IIF(loRC.cod_tva = 'TI19BFB', loRC.TI19B, ;
			IIF(loRC.cod_tva = 'TI24B', loRC.TI24B, ;
			IIF(loRC.cod_tva = 'TI20B', loRC.TI20B, ;	
			IIF(loRC.cod_tva = 'CEB', loRC.CEBB, ;
			IIF(loRC.cod_tva = 'CE24SB', loRC.CESB, ;	
			IIF(loRC.cod_tva = 'CEBFN', loRC.CEN, ;
			IIF(loRC.cod_tva = 'CEBFSC', loRC.CESC, ;
			IIF(loRC.cod_tva = 'FOSCN', loRC.FOSCN, ;
			IIF(loRC.cod_tva = 'FO09BVB', loRC.FO09B, ;
			IIF(loRC.cod_tva = 'FO19BVB', loRC.FO19B, ;
			IIF(loRC.cod_tva = 'XX19TIB', loRC.XX19TIB, ;	
			IIF(loRC.cod_tva = 'FO24B', loRC.FO24B, ;
			IIF(loRC.cod_tva = 'FO20B', loRC.FO20B, ;	
			0))))))))))))))))))))
			
			loRC.tva = Iif(loRC.cod_tva = 'RO24B', loRC.RO24T, ;
			Iif(loRC.cod_tva = 'RO20B', loRC.RO20T, ;	
			IIF(loRC.cod_tva = 'RO19BVB', loRC.RO19T, ;
			IIF(loRC.cod_tva = 'RO09BVB', loRC.RO09T, ;
			IIF(loRC.cod_tva = 'RO05B', loRC.RO05T, ;
			IIF(loRC.cod_tva = 'TI09BFB', loRC.TI09T, ;
			IIF(loRC.cod_tva = 'TI19BFB', loRC.TI19T, ;
			IIF(loRC.cod_tva = 'TI24B', loRC.TI24T, ;
			IIF(loRC.cod_tva = 'TI20B', loRC.TI20T, ;	
			IIF(loRC.cod_tva = 'CEB', loRC.CEBT, ;
			IIF(loRC.cod_tva = 'CE24SB', loRC.CEST, ;	
			IIF(loRC.cod_tva = 'FO09BVB', loRC.FO09T, ;
			IIF(loRC.cod_tva = 'FO19BVB', loRC.FO19T, ;
			IIF(loRC.cod_tva = 'XX19TIB', loRC.XX19TIT, ;	
			IIF(loRC.cod_tva = 'FO24B', loRC.FO24T, ;
			IIF(loRC.cod_tva = 'FO20B', loRC.FO20T, ;	
			IIF(loRC.cod_tva = 'ROTN', loRC.ROTN, ;
			IIF(loRC.cod_tva = 'ROTN50', loRC.ROTN50, ;
			IIF(loRC.cod_tva = 'ROTN100', loRC.ROTN100, ;		
			0)))))))))))))))))))

			IF !(loRC.baza = 0 AND loRC.tva = 0)
				SELECT cListRCTemp1 
				APPEND BLANK 
				GATHER NAME loRC
			ENDIF 
			
		ENDSCAN && JTVA_COLOANE  JC = 1 And !Empty(Nvl(J.COLOANA_JC, ''))
	ENDSCAN && cListRC tip = 0 AND tva_incasare = 0
ELSE
	Select r.cod, r.tip, r.denumire, r.nract, r.SERIE_ACT, r.dataact, r.COD_FISCAL, J.COLOANA_JC As COD_TVA, J.denumire As EXPLICATIE_TVA, r.TOTCTVA, r.TOTFTVATAX, r.TOTTVATAX, TVA_INCASARE, ;
		CAST(Iif(J.COLOANA_JC = 'RO24B', r.RO24B, ;
		Iif(J.COLOANA_JC = 'RO20B', r.RO20B, ;
		IIF(J.COLOANA_JC = 'RO19BFB', r.RO19B, ;
		IIF(J.COLOANA_JC = 'RO09BFB', r.RO09B, ;
		IIF(J.COLOANA_JC = 'RO05B', r.RO05B, ;
		IIF(J.COLOANA_JC = 'ROSCN', r.ROSCN, ;
		IIF(J.COLOANA_JC = 'TI09BFB', r.TI09B, ;
		IIF(J.COLOANA_JC = 'TI19BFB', r.TI19B, ;
		IIF(J.COLOANA_JC = 'TI24B', r.TI24B, ;
		IIF(J.COLOANA_JC = 'TI20B', r.TI20B, ;	
		IIF(J.COLOANA_JC = 'CEB', r.CEBB, ;
		IIF(J.COLOANA_JC = 'CE24SB', r.CESB, ;
		IIF(J.COLOANA_JC = 'CEBFN', r.CEN, ;
		IIF(J.COLOANA_JC = 'CEBFSC', r.CESC, ;
		IIF(J.COLOANA_JC = 'FOSCN', r.FOSCN, ;
		IIF(J.COLOANA_JC = 'FO09B', r.FO09B, ;
		IIF(J.COLOANA_JC = 'FO19B', r.FO19B, ;
		IIF(J.COLOANA_JC = 'XX19TIB', r.XX19TIB, ;	
		IIF(J.COLOANA_JC = 'FO24B', r.FO24B, ;
		IIF(J.COLOANA_JC = 'FO20B', r.FO20B, ;	
		0)))))))))))))))))))) As N(16, 4)) As BAZA, ;
		CAST(Iif(J.COLOANA_JC = 'RO24B', r.RO24T, ;
		Iif(J.COLOANA_JC = 'RO20B', r.RO20T, ;
		IIF(J.COLOANA_JC = 'RO19BFB', r.RO19T, ;
		IIF(J.COLOANA_JC = 'RO09BFB', r.RO09T, ;
		IIF(J.COLOANA_JC = 'RO05B', r.RO05T, ;
		IIF(J.COLOANA_JC = 'TI09BFB ', r.TI09T, ;
		IIF(J.COLOANA_JC = 'TI19BFB ', r.TI19T, ;
		IIF(J.COLOANA_JC = 'TI24B ', r.TI24T, ;
		IIF(J.COLOANA_JC = 'TI20B ', r.TI20T, ;	
		IIF(J.COLOANA_JC = 'CEB', r.CEBT, ;
		IIF(J.COLOANA_JC = 'CE24SB', r.CEST, ;
		IIF(J.COLOANA_JC = 'FO09B ', r.FO09T, ;
		IIF(J.COLOANA_JC = 'FO19B ', r.FO19T, ;
		IIF(J.COLOANA_JC = 'XX19TIB ', r.XX19TIT, ;	
		IIF(J.COLOANA_JC = 'FO24B', r.FO24T, ;
		IIF(J.COLOANA_JC = 'FO20B', r.FO20T, ;	
		IIF(J.COLOANA_JC = 'ROTN', r.ROTN, ;
		IIF(J.COLOANA_JC = 'ROTN50', r.ROTN50, ;
		IIF(J.COLOANA_JC = 'ROTN100', r.ROTN100, ;
		0))))))))))))))))))) As N(16, 4)) As TVA ;
		FROM cListRC r FULL JOIN JTVA_COLOANE J ON 1=1 ;
		where tip = 1 And J.JC = 1 And !Empty(Nvl(J.COLOANA_JC, '')) ;
		INTO Cursor cListRCTemp1 Readwrite
ENDIF 	

WAIT WINDOW 'Completare recapitulatie cu facturi neexigibile' NOWAIT
*** adaug si lunile care nu au nimic exigibil
Select Distinct r.cod, r.tip, r.denumire, r.nract, r.SERIE_ACT, r.dataact, r.COD_FISCAL ;
	from cListRCTemp1 r ;
	where cod Not In (Select cod From cListRCTemp1  Where !(BAZA = 0 And TVA = 0)) ;
	into Cursor cListRCTemp2

Delete From cListRCTemp1 Where BAZA = 0 And TVA = 0
Update cListRCTemp1 Set COD_TVA = 'RO19B', EXPLICATIE_TVA = 'ACH. INT. 19%' Where Left(COD_TVA,5) = 'RO19B'
Update cListRCTemp1 Set COD_TVA = 'RO09B', EXPLICATIE_TVA = 'ACH. INT. 9%' Where Left(COD_TVA,5) = 'RO09B'

Update cListRCTemp1 Set COD_TVA = 'TI19B', EXPLICATIE_TVA = 'ACH. TX. INV. 19%' Where Left(COD_TVA,5) = 'TI19B'
Update cListRCTemp1 Set COD_TVA = 'TI09B', EXPLICATIE_TVA = 'ACH. TX INV. 9%' Where Left(COD_TVA,5) = 'TI09B'

Update cListRCTemp1 Set EXPLICATIE_TVA = 'ACH. BUNURI CE' Where Left(COD_TVA,3) = 'CEB' AND !INLIST(RIGHT(ALLTRIM(COD_TVA), 1), 'C', 'N')
Update cListRCTemp1 Set EXPLICATIE_TVA = 'ACH. BUNURI CE' Where Left(COD_TVA,5) = 'CE20B'
Update cListRCTemp1 Set COD_TVA = 'CES', EXPLICATIE_TVA = 'ACH. SERVICII CE' Where Left(COD_TVA,6) = 'CE24SB'
Update cListRCTemp1 Set COD_TVA = 'CES', EXPLICATIE_TVA = 'ACH. SERVICII CE' Where Left(COD_TVA,6) = 'CE20SB'

Select cListRCTemp
Append From Dbf('cListRCTemp1')
Append From Dbf('cListRCTemp2')
Use In (Select('cListRCTemp1'))
Use In (Select('cListRCTemp2'))

*** completez nume, nract, dataact pentru inregistrarile TVA care nu le au completate
WAIT WINDOW 'Completare infomatii inregistrari TVA' NOWAIT
Select cListRCTemp
Scan For tip = 2
	lcNumeAct = denumire + Str(nract, 14, 0)
	lnCod	  = cod
	If Seek(m.lnCod, 'cListRC', 'cod')
		Replace denumire With cListRC.denumire, nract With cListRC.nract, dataact With cListRC.dataact In cListRCTemp
	Else
		If Seek(m.lcNumeAct, 'cListRC', 'numeact')
			Replace cod With cListRC.cod, dataact With cListRC.dataact In cListRCTemp
		Endif && seek
	Endif && seek
Endscan

Use In (Select('cListRC'))

Select Cast(0 As I) As nrcrt, Cast(0 As I) As nrcrtf, * From cListRCTemp Into Cursor cListRC Order By dataact, nract, denumire, tip Readwrite

Update cListRC Set tip = 0 Where tip  = 2 && incasarile pe acelasi nivel cu facturile
Use In (Select('cListRCTemp'))

*** INSUMEZ FACTURILE DUPA NRACT, DATAACT, DENUMIRE
*!*	SELECT Cast(0 As I) As nrcrt, Cast(0 As I) As nrcrtf, min(COD) as COD, min(ID_FACT) as ID_FACT, TIP, DENUMIRE, NRACT, SERIE_ACT, DATAACT, COD_FISCAL, COD_TVA, EXPLICATIE_TVA, TVA_INCASARE, NRACTI, DATAACTI, FDOCI, ;
*!*		sum(INCASAT) AS INCASAT, sum(TOTCTVA) AS TOTCTVA, sum(TOTFTVATAX) AS TOTFTVATAX, sum(TOTTVATAX) AS TOTTVATAX, sum(BAZA) AS BAZA, sum(TVA) AS TVA, sum(BAZAN) AS BAZAN, sum(TVAN) AS TVAN ;
*!*		from cListRCTemp ;
*!*		where tip = 0 ;
*!*		group by TIP, DENUMIRE, NRACT, SERIE_ACT, DATAACT, COD_FISCAL, COD_TVA, EXPLICATIE_TVA, TVA_INCASARE, NRACTI, DATAACTI, FDOCI ;
*!*	union all ;
*!*	SELECT Cast(0 As I) As nrcrt, Cast(0 As I) As nrcrtf, COD, ID_FACT, TIP, DENUMIRE, NRACT, SERIE_ACT, DATAACT, COD_FISCAL, COD_TVA, EXPLICATIE_TVA, TVA_INCASARE, NRACTI, DATAACTI, FDOCI, ;
*!*		INCASAT, TOTCTVA, TOTFTVATAX, TOTTVATAX, BAZA, TVA, BAZAN, TVAN ;
*!*		from cListRCTemp ;
*!*		where tip <> 0 ;
*!*	Into Cursor cListRC ;
*!*	Order By 9, 7, 6, 5 Readwrite


*** STERG NUME, NRACT... DE PE INREGISTRARILE 2..N (EXPLICATII_TVA) PENTRU ACEEASI FACTURA
Select cListRC
Go Top
lnCod	 = 0
lcSerieAct = ""
lnNrAct = 0
ldDataAct = {}
lcDenumire = ""
lnNrCrt	 = 0
lnNrCrtF = 0
Scan
	*!*		If m.lnCod <> cod && prima linie din fiecare factura
	If m.lcSerieAct <> serie_act OR m.lnNrAct <> nract Or m.ldDataAct <> dataact Or m.lcDenumire <> denumire && prima linie din fiecare factura
		lnCod	 = cod
		lcSerieAct = serie_act
		lnNrAct = nract
		ldDataAct = dataact
		lcDenumire = denumire
		lnNrCrt	 = m.lnNrCrt + 1
		lnNrCrtF = 1 && numar curent in cadrul fiecarei facturi
		Replace nrcrt With m.lnNrCrt, nrcrtf With m.lnNrCrtF
	Else && liniile 2..n din facturi
		lnNrCrtF = m.lnNrCrtF + 1
		Replace nrcrt With m.lnNrCrt, nrcrtf With m.lnNrCrtF, denumire With '', nract With 0, SERIE_ACT With '', dataact With {}, COD_FISCAL With "", TOTCTVA With 0, TOTFTVATAX With 0, TOTTVATAX With 0
	Endif
Endscan

*** TOTALURI EXIGIBIL PE COTE TVA
*** ADUN FIE INREGISTRARILE DIN LUNA CURENTA TIP = 0, FIE INREGISTRARILE TIP RECAPITULATIE TIP = 1
WAIT WINDOW 'Total exigibil pe cote TVA' NOWAIT
lnTip = Iif(m.lnMeniu  = 1, 0, 1)
Insert Into cListRC (tip, COD_TVA, EXPLICATIE_TVA, INCASAT, BAZA, TVA) ;
	Select 2 As tip, COD_TVA, EXPLICATIE_TVA, Sum(INCASAT) As INCASAT, Sum(BAZA) As BAZA, Sum(TVA) As TVA ;
	from cListRC ;
	where tip = m.lnTip And !Empty(Nvl(COD_TVA, '')) ;
	ORDER By COD_TVA ;
	group By COD_TVA, EXPLICATIE_TVA
Select cListRC

Use In (Select('JTVA_COLOANE'))
*** FORMAT TVA INCASARE 2013 ^

If Reccount('cListRC') > 0
	Thisform.AlwaysOnTop = .F.

	IF AMESSAGEBOX('Doriti listare clasica?',4+32,_screen.Caption) = 6
		goExport.export2frx([cListRc], [registru_jtva2013])
	ELSE		
		goExport.export2frx([cListRc], [registru_jtva2013], , , , , , .T.)
	ENDIF 	
	
	Thisform.AlwaysOnTop = .T.
Endif

Use In (Select('cListRC'))



