*!* 23.07.2010
*!* marius.mutu
*!* declaratia394 - este optional daca se aduna si achizitiile din tara scutite sau neimpozabile (ROSCN)

*!* 09.08.2010
*!* marius.mutu
*!* viz_decontTVA_0710 && 07/2010 TVA 24%

*!* 12.04.2011
*!* marius.mutu
*!* viz_decl390 - tratare coduri fiscale nule

*!* 08.02.2012
*!* marius.mutu
*!* viz_decontTVA_0112 && 01/2012 CORELARE D394, CEREALE
*!* RANDURI NOI 17, 17.1., 27.1

*!* 09.02.2012
*!* D394
*!* nu se lua in considerare TAXARE INVERSA IN cursorul final D394

*!* 10.02.2012
*!* D390
*!* nu trebuie incluse in declaratie livrari scutite cu drept de deducere cf. art. 143 b,c (coduri nevalide)

*!* 21.02.2012
*!* D394 - nu se mai sterg fisierele PDF, XDP daca se apeleaza din D300 silentios
*!* daca se genera D394 si apoi D300, se stergeau fisierele D394 fara sa stie utilizatorul
*!* XDP D394,D300 - nu se mai pune tag-ul universalcode in XDP - suprascria versiunea PDF-ului

*!* 23.02.2012
*!* marius.mutu
*!* D394.do_executa - cursorul cu codurifiscale pentru verificare are si coloana "atribut_fiscal"
*!* necesara pentru formularul de verificare coduri fiscale

*!* 07.03.2012
*!* marius.mutu
*!* calculeaza_decl101
*!*	rd 28 6582 (in cod)
*!*	rd 40.1 = 16% rd. 39 (in formula total tabel)
*!*	rd 43 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod si in formula total tabel) (rd. 1 din Contul de Profit si Pierdere) (in cod
*!* rd 27 protocol peste limita legala 2% (rd. 10 + rd. 24 + 623.protocol)

*!* 26.03.2012
*!* marius.mutu
*!* D394.do_executa
*!* TVA LIVRARI TAXARE INVERSA = 0 IN LOC DE 0.24 * BAZA

*!* 18.05.2012
*!* marius.mutu
*!* D390.do_executa
*!* nu se includeau achizitiile de bunuri (era de 2 ori cesb <> 0 in loc de cebb <> 0)

*!* 20.07.2012
*!* marius.mutu
*!* D300 viz_decontTVA0112
*!* randul 20 si randul 7 nu se mai aduna TAXARE INVERSA DIN TARA
*!* la livrari, taxarea inversa era si pe randul 7 si pe randul 12, care faceau parte din randul 17 total si se aduna de doua ori
*!* in plus, la livrari, rd 1 - 17 sunt doar COMERT INTRACOMUNITAR SI IN AFARA UE; TX INV este interna si nu avea ce cauta in sectiunea aceasta.

*!* D100
*!* corectat randul 02 - impozit pe profit persoane juridice
*!* nu se mai completeaza randul 07 cu impozitul din salarii

*!* 09.11.2012
*!* marius.mutu
*!* D300 - nu mai completez id_jtva 1001 si 1002 pe randurile 30 si 16 (regularizare taxa dedusa/colectata)
*** id_jtva = 1001 s-a redenumit in limitare deducere TVA 4426 ROTN si se scade din rd 28 = rd 27 - ROTN

*!* 16.11.2012
*!* marius.mutu
*!* s-a introdus ID_JTVA 1003 regularizare taxa dedusa, ID_JTVA 1001 s-a schimbat in limitare deducere TVA 4426
*!* D300 - completez din nou ID_JTVA 1002 (regularizare taxa colectata) pe rd. 30 si ID_JTVA 1003 (regularizare taxa dedusa) pe rd. 16

*!* 25.01.2013
*!* marius.mutu
*!* D394 - se afiseaza tipurile de documente si pentru partenerii fara RO in cod fiscal

*!* 22.03.2013
*!* marius.mutu
*!* viz_regvanz, viz_regcump - adaugare coloana diferenta_tva_incasare (plata - (baza+tva exigibil)) la cautare

*!* 16.04.2013
*!* marius.mutu
*!* viz_decontTVA_0113 - sold neexigibil din ultima luna, in loc de toate lunile din perioada

*!* 18.07.2013
*!* marius.mutu
*!* viz_decontTVA_0113 randul 35 ia platile 4423  = X, din toata perioada

*!* 08.01.2014
*!* marius.mutu
*!* Declaratia 394 valabila din 12/2013
*!* D394.do_executa, + do_genereaza_xdp_201312

*!* 15.01.2014
*!* marius.mutu
*!* D394.do_executa - defalcare nrfact, nrfact taxare inversa (nu calcula corect pentru un partener care avea si facturi normale si facturi taxare inversa, punea toate facturile pe ambele randuri)

*!* 23.01.2014
*!* marius.mutu
*!* D394
*!* do_genereaza_xdp_201312 se iau in considerare clientii / furnizorii cu nrfact <> 0, in loc de baza <> 0 (este posibil sa am un furnizori cu factura + si factura -, pe total 0, dar are 2 facturi)

*!* 04.03.2014
*!* marius.mutu
*!* D101 - recalculare sold 711 la fel ca la SITFIN.F20 (venituri exploatare)

*!* 12.03.2014
*!* marius.mutu
*!* D101 - se poate calcula pe perioade 02-12,04-12,07-12,10-13

*!* 13.03.2014
*!* marius.mutu
*!* D101
*!* la randul 02 ch. din exploatare se aduna si ch. impozit profit/venit 691/698 (care apoi se scade ca cheltuiala nedeductibila)
*!* la randul 24 ch. cu impozitul pe profit se include si impozitul pe venit
*!* Se completeaza randul 49 Impozit pe venit declarat in D100 la 30.09

*!* 13.03.2014
*!* marius.mutu
*!* D097.do_executa - reparat eroare instantiere clasa frm_aleg_tipdoc

*!* 27.01.2015
*!* marius.mutu
*!* viz_decontTVA_0113 - Rd. 35 doar 4423 = 5311/5121 in locd e 4423 = x
*!* D101 valabil 2014
*!* viz_decl100_102: impozit pe profit se ia rulaj debitor 691 in loc de rulaj creditor 441/4411

*!* 20.03.2015
*!* marius.mutu
*!*	viz_decl101. Rd. 16 Rezerva legala deductibila = 0 daca profitul brut < 0

*!*	10.12.2015
*!*	marius.mutu
*!*	D392 - cifra de afaceri este suma livrarilor - 7583 (vanzari de active) CF art 152 a 2
*!*	#4252
*!* D097 - se calculeaza cifra de afaceri pentru perioda ianuarie - (luna curenta-1) in loc de un an in urma
*!*	#4247

*!* 02.02.2016
*!* marius.mutu
*!* D392, D101 - declaratii pdf 2015

*!* 02.02.2016
*!* marius.mutu
*!*	d394.do_executa - cota TVA 20% 2016

*!* 24.08.2016
*!* marius.mutu
*!* d394_2016.do_genereaza_xdp - nu se declara decat tipul document F si M. Nu mai declar si achizitii bonurile fiscale cu CUI (ex: ROMPETROL)
*!* NU STIU SIGUR!!! in instructiuni se spune facturi si/sau alte documente. Ce sunt alte documente?

*!*	26.09.2016
*!*	marius.mutu
*!*	D394_2016.LIST crsFacturiEmiseTip seria NULL - dadea eroare la insert de facturi stornate fara serie

*!*	26.09.2016
*!*	marius.mutu
*!*	d394_2016.configureaza_date_jtva, do_genereaza_xdp: se rapoarteaza facturile tip LS, AS (livrari, achizitii regim special - servicii turism, second hand fara TVA pe factura (tva la marja de profit)
*!* do_genereaza_xdp - cota 0 pentru LS, AS, N, V

*!* 14.10.2016
*!* d394_2016.pregateste_sectiunea_1_2
*!*	IdTipDoc = 24 sau 5 plaja de facturi special creata pentru declararea in D394. Nu se foloseste la generarea de numere de facturi

*!*	26.10.2016
*!*	marius.mutu
*!*	d394_2016.pregateste_sectiunea_facturi_pers_fizice_10000 - se iau doar tip_persoana = 2 (pers fizica)
*!*	d394_2016 * emisa_in_perioada = dataireg in perioada, in loc de dataact in perioada
*!*	d394_2016 * se ia si tip_persoana fizica/juridica din vjv2013, vjc2013
*!*	d394_2016.citeste_date_jtva - se iau dor facturile din jurnalele de tva care au tva <> 0 sau TOTAL = 0, nu mai iau si pe cele cu tva = 0 (ex: neimpozabile)
*!*	d394_2016.pregateste_sectiunea_1_2 cursorul cu serii/plaje facturi emise nu lua prima factura daca erau doar o singura factura

*!*	09.11.2016
*!*	marius.mutu
*!*	d394_2016.do_genereaza_xdp - corectie <tip1>, facturi pers fizice valoare absoluta > 10000
*!*	d394_2016.pregateste_sectiunea_facturi_pers_fizice_10000 facturi pers fizice valoare absoluta > 10000, doar documentele tip factura F,M,U,H

*!*	21.11.2016
*!*	marius.mutu
*!*	citeste_date_jtva - corectie numar bonuri fiscale B,C, exceptate de la AMEF E

*!* 25.11.2016
*!* d394_2016.genereaza_xdp - se sterg facturile catre pers fizice < 10.000 lei. apareau daca totalul facturilor pe o persoana fizica > 10.000 lei
*!* s-au inclus si achizitiile ROSCN (pers neinregistrate - pers juridice fara RO sau pers fizice). nu mai erau incluse pentru ca pusesem conditie TVA <> 0, la fel ca la livrari

*!* citeste_date_jtva sterg facturile de achizitie de la platitorii de TVA (RO) care au doar baza00 <> 0 (ROSCN). nu trebuie sa apara in declaratie. doar la tipul 2 (neinregistrati in scopuri TVA)

*!* 19.12.2016
*!* d394_2016.citeste_date_jtva - nu se sterg facturile de achizitie de la platitorii de TVA (RO), cu TVA 0, de tip AS (turism, second hand)

*!* 19.01.2017
*!* marius.mutu
*!* D097.do_executa - includere RO20B in calculul cifrei de afaceri

*!* 11.02.2017
*!* marius.mutu
*!*	D300 viz_decontTVA_0117

*!* 15.02.2017
*!* marius.mtuut
*!* D394_2016
*!* + TVA 19% neexigibil, facturile persoane fizice <10000 doar in perioada 07/2016-12/2016

*!*	25.04.2017
*!*	marius.mutu
*!*	D394_2016.citeste_date_jtva - facturile anulate din act chiar si fara serie (exproton nu are serii)
*!* viz_decontTVA_0117 - se calculeaza Regularizare taxa colectata (rd 16) si regularizare taxa dedusa (rd 32) din facturile cu data act din alta luna decat data inregistrarii, in perioada declaratiei

*!* 19.06.2017
*!* marius.mutu
*!* viz_decontTVA_0117. nu se pune pe Rd. 26 regularizari taxa dedusa taxare inversa 9% si 19%

*!* 26.07.2017
*!* marius.mutu
*!* d394_2016.do_genereaza_xdp - tratare caz fara facturi

*!* 19.12.2017
*!* marius.mutu
*!* viz_decontTVA_0117 - corectare suma rand 3 din decontul de TVA D300

*!*	27.03.2018
*!*	D394_2016 
*!*	corectie autofacturi

*!* 27.09.2018
*!* d394.2016.do_genereaza_xdp - eliminare xmlcharacters si diacritice din strada, bloc, numar

*!* 24.01.2019
*!* d394_2016.do_genereaza_xdp - tratare tip_document = 4 ('R' - contract)

*!* 11.03.2019
*!* d101.calculare_2018 se foloseste setul cont profit si pierdere sem. II 2018 in loc de II 2015. au fost adaugate conturi

*!* 19.08.2019
*!* viz_decontTVA_0719

*!* 23.08.2019
*!* d394_2016.do_genereaza_xdp - se exclud facturile simplificate (M) din sectiunea de facturi. Se declara in sectiunea I1.3

*!*	27.01.2020
*!* viz_decl392 12/2019

*!*	25.02.2020
*!*	d394_2016.pregateste_sectiunea_1_2: plaje numere tin cont de prefix si an2caractere

*!* 24.03.2020
*!* Actualizare D390 02/2020

*!* 23.04.2020
*!* Actualizare D100 03/2020

*!* 22.09.2020
*!* D394_2016 + bifa persoane afiliate

*!*	09.12.2020
*!*	D097 - nu se afiseaza tipuri documente cu id_fdoc NULL

*!* 26.01.2021
*!* d394_2016.pregateste_sectiunea_1_2 
*!* plaje numere alocate facturi nu tinea cont de facturi alocate d394 sterse

*!*	25.01.2022
*!*	viz_decontTVA_0719, viz_regcump + XX19TIB, XX19TIT, ROTN50, ROTN100

*!* 25.03.2022
*!* D101 12/2021
*!* D394 tippart=4 dupa codtara <> RO sau UE

*!* 03.08.2022
*!* D394_2016. tippart=2 persoane neinregistrate si din afara RO (fara atribut fiscal sau persoane fizice)

*!*	23.02.2023
*!*	D394_2016 - facturi anulate doar din documentele anulate cu tip_doc = M,F

*!*	21.06.2024
*!*	D394_2016 - se completeaza xml sectiunile 2.3, 2.4 facturi emise de beneficiari, terti in numele persoanei impozabile 

*!*	17.10.2024
*!*	D394 - CORECTARE calcul numar bonuri fiscale daca sunt introduse cu acelasi cod

*!*	20.02.2025
*!*	ACTUALIZARE D300, D394 - CAEN REV3
*!*	ACTUALIZAER D205 12/2024

*!*	12.05.2025
*!*	viz_decontTVA_0719, d394 + ACH. TAXARE INVERSA NON-CE XX9TIB, XX19TIT


*!* 21.08.2025
*!*	viz_regcump 07/2025 corectie afisare limitare deducere 50%


*!*	08.01.2026
*!*	d394_2025.citeste_date_jtva - am inclus si XX21TI, XX11TI - ACH. TX. INV. NON-CE 21%/11%
*!*	 am corectat tipul 4 de persoane - in afara RO, NON-CE, pers. juridice, fara cod fiscal
*!* do_genereaza_xdp - corectat crsamef 21%


*!*	04.02.2026
*!*	calculeaza_2024
*!*	modificat din scrierea pe randul 50, in loc randul 49
*!*	pn49 a devenit pn50

*!*	25.02.2026
*!*	D394 bonuri fiscale 11% le pune pe 19% 
*!*	do_genereaza_xdp - corectat crsamef 11%

#Include COMUN\Include\COMUN.H

*===========================================
Procedure viz_decontTVA
	Parameters tlTrim
	LOCAL lcPerioadaTVA 

	IF PCOUNT() = 0
		tlTrim = 1
		lcPerioadaTVA = UPPER(ALLTRIM(NVL(m.gcPERIOADA_TVA, 'LUNAR')))
		DO CASE 
			CASE m.lcPerioadaTVA = 'LUNAR'
				tlTrim = 1
			CASE m.lcPerioadaTVA = 'TRIMESTRIAL'
				tlTrim = 3
			CASE m.lcPerioadaTVA = 'SEMESTRIAL'
				tlTrim = 6
			CASE m.lcPerioadaTVA = 'ANUAL'
				tlTrim = 12
		ENDCASE 				
	ENDIF 
	
	Private pnNr_facturi, pnBaza, pnTVA, pnNr_facturi2, pnBaza2, pnTVA2, pnNr_facturi3, pnBaza3, pnTVA3, pnBaza41, pnBaza42
	Store 0 To pnNr_facturi, pnBaza, pnTVA, pnNr_facturi2, pnBaza2, pnTVA2, pnNr_facturi3, pnBaza3, pnTVA3, pnBaza41, pnBaza42

	Do Case
		Case gnAn * 12 + gnLuna >= 2026 * 12 + 1
			Do viz_decontTVA_2601 With tlTrim && s-au scos cotele 19,9,5
		Case gnAn * 12 + gnLuna >= 2025 * 12 + 8
			Do viz_decontTVA_2508 With tlTrim
		Case gnAn * 12 + gnLuna <= 2007 * 12 + 4
			Do viz_decontTVA_0107
		Case Between(gnAn * 12 + gnLuna, 2007 * 12 + 5, 2008 * 12 + 11) && 05/2007 - 11/2008 ORDIN 273 din 28/05/2007 (MO 392 11/06/2007)
			Do viz_decontTVA_0507 With tlTrim
		Case Between(gnAn * 12 + gnLuna, 2008 * 12 + 12, 2010 * 12 + 6) && 12/2008 - 06/2010
			Do viz_decontTVA_0110 With tlTrim
		Case Between(gnAn * 12 + gnLuna, 2010 * 12 + 7, 2011 * 12 + 12) && 07/2010 - 12/2011 TVA 24%
			Do viz_decontTVA_0710 With tlTrim
		Case Between(gnAn * 12 + gnLuna, 2012 * 12 + 1, 2012 * 12 + 12) && 01/2012 - 12/2012
			Do viz_decontTVA_0112 With tlTrim
		Case Between(gnAn * 12 + gnLuna, 2013 * 12 + 1, 2015 * 12 + 12) && 01/2013 - 12/2015
			Do viz_decontTVA_0113 With tlTrim
		Case gnAn * 12 + gnLuna = 2016 * 12 + 1 && 01/2016
			Do viz_decontTVA_0116 With tlTrim
		Case gnAn * 12 + gnLuna <= 2018 * 12 + 12 && 01/2017 - 12/2018
			Do viz_decontTVA_0117 With tlTrim
		Otherwise
			Do viz_decontTVA_0719 With tlTrim && 01/2019 -
	Endcase

Endproc && viz_decontTVA

*===========================================
Procedure viz_decontTVA_2601
	Lparameters tnNumarLuni
	*Parameters tlTrim

	*** tnNumarLuni: 1 = o luna, 3 = 3 luni trimestrial, 6 = 6 luni semestrial, 12 = 12 luni anual

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec

	Local lodt As 'frm_decontTVA'
	Local lcCursor, lcSql, ldDataCurenta, ldDataPrecedenta, lnAnCurent, lnAnPrecedent, lnLunaCurenta
	Local lnLunaMin, lnLunaPrecedenta, lnPerioada, lnPlati, lnSucces, lnTP, lnTR, loEx As Exception, lnROTN
	Local lcRand, llSucces, lnSuma
	PRIVATE pn10B, pn10T, pn11B, pn11T, pn121B, pn121T, pn122B, pn122T, pn12B, pn12T, pn13B, pn14B
	PRIVATE pn15B, pn1B, pn201B, pn201T, pn20B, pn20T, pn21B, pn21T, pn221B, pn221T, pn22B, pn22T
	PRIVATE pn23B, pn23T, pn24B, pn24T, pn25B, pn25T, pn261B, pn261T, pn262B, pn262T, pn26B, pn26T
	PRIVATE pn291B, pn29B, pn31B, pn33B, pn33T, pn3B, pn51B, pn51T, pn5B, pn5T, pn6B, pn6T, pn71B
	PRIVATE pn71T, pn7B, pn7T, pn8B, pn8T, pn9B, pn9T, pnRegularizareB, pnRegularizareT

	lnAnCurent = m.gnAn
	lnLunaCurenta = m.gnLuna
	ldDataCurenta = Date(m.lnAnCurent, m.lnLunaCurenta, 1)
	lnROTN = 0

	* Luna de inceput a perioadei
	* 1 = gnLuna
	* 3 = 1,4,7,10
	* 6 = 1,7
	* 12 = 1
	lnPerioada = Ceil(m.lnLunaCurenta / tnNumarLuni)
	lnLunaMin = (lnPerioada - 1) * tnNumarLuni + 1

	*** luna pentru balanta precedenta
	ldDataPrecedenta = Gomonth(Date(m.lnAnCurent, m.lnLunaMin, 1), -1)
	lnLunaPrecedenta = Month(m.ldDataPrecedenta)
	lnAnPrecedent = Year(m.ldDataPrecedenta)
	&& ---
	pnLunaI = lnLunaMin
	pnLunaF = m.lnLunaCurenta

	pnLunaPrec = m.lnLunaPrecedenta
	pnAnPrec = m.lnAnPrecedent

	Private poDecont
	Store '' To poDecont

	Private pnProRata, pnAchizitieBazaN, pnAchizitieTVAN, pnLivrareBazaN, pnLivrareTVAN, pnAchizitieBaza6N, pnAchizitieTVA6N, pnLivrareBaza6N, pnLivrareTVA6N
	pnProRata = 100
	pnAchizitieBazaN = 0
	pnAchizitieTVAN = 0
	pnLivrareBazaN = 0
	pnLivrareTVAN = 0
	pnAchizitieBaza6N = 0
	pnAchizitieTVA6N = 0
	pnLivrareBaza6N = 0
	pnLivrareTVA6N = 0

	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	&& schimbare cota TVA standard de la 19% la 21% + s-au scos cotele 19,9,5. a ramas o situatie doar cu 9%
	lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
		[where valabil = '2601' order by id_decont]						

	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Text To lcSql Noshow Textmerge
	SELECT 
	ROUND(SUM(RO21B),0) AS RO21B, 
		 ROUND(SUM(RO21T),0) AS RO21T, 
		 ROUND(SUM(RO11B),0) AS RO11B, 
		 ROUND(SUM(RO11T),0) AS RO11T,
		 ROUND(SUM(RO19B),0) AS RO19B, 
		 ROUND(SUM(RO19T),0) AS RO19T, 
		 ROUND(SUM(RO9B),0) AS RO9B, 
		 ROUND(SUM(RO9T),0) AS RO9T,
		 ROUND(SUM(RO5B),0) AS RO5B, 
		 ROUND(SUM(RO5T),0) AS RO5T, 
		 ROUND(SUM(RO20B),0) AS RO20B, 
		 ROUND(SUM(RO20T),0) AS RO20T,
		 ROUND(SUM(RO24B),0) AS RO24B, 
		 ROUND(SUM(RO24T),0) AS RO24T,
		 ROUND(SUM(ROTI),0) AS ROTI, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO21B ELSE 0 END),0) AS RO21Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO21T ELSE 0 END),0) AS RO21Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO11B ELSE 0 END),0) AS RO11Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO11T ELSE 0 END),0) AS RO11Tr,
		 ROUND(SUM(CESCDD1),0) AS CESCDD1,
		 ROUND(SUM(CESCDD2),0) AS CESCDD2,
		 ROUND(SUM(CEOPTR),0) AS CEOPTR,
		 ROUND(SUM(CESVDD),0) AS CESVDD,
		 ROUND(SUM(CESVFDD),0) AS CESVFDD,
		 ROUND(SUM(CESVFS),0) AS CESVFS,
		 ROUND(SUM(WRSCDD),0) AS WRSCDD, 
		 ROUND(SUM(WRSCDDAB),0) AS WRSCDDAB, 
		 ROUND(SUM(WRSCDDCD),0) AS WRSCDDCD, 		 		 
		 ROUND(SUM(FODD),0) AS FODD, 
		 ROUND(SUM(FOFDD),0) AS FOFDD, 
		 ROUND(SUM(CEOPTR),0) AS CEOPTR, 
		 ROUND(SUM(CESVDD),0) AS CESVDD, 
		 ROUND(SUM(CESVFDD),0) AS CESVFDD, 
		 ROUND(SUM(CESVFS),0) AS CESVFS, 
		 ROUND(SUM(WRSCFDD),0) AS WRSCFDD,
		 ROUND(SUM(WRN),0) AS WRN,
         ROUND(SUM(rortc21),0) AS rortc21,
         ROUND(SUM(rortc11),0) AS rortc11,
		 ROUND(SUM(rortc24),0) AS rortc24,
         ROUND(SUM(rortc20),0) AS rortc20,
         ROUND(SUM(rortc19),0) AS rortc19,
         ROUND(SUM(rortc9),0) AS rortc9,
         ROUND(SUM(rortc5),0) AS rortc5
		  FROM JV2007 
		 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF <<m.gcCondSucursala>>
	Endtext
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala


	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	Go Top
	pn1B = Nvl(cescdd1, 0) + Nvl(ceoptr, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0) + Nvl(cesvdd, 0) + Nvl(cesvfdd, 0) + Nvl(cesvfs, 0)
	pn31B = Nvl(cesvfs, 0)

	&& scad tva exigibil cu data act in afara perioadei de declarare. il pun pe regularizare rd. 16 
	pn9B = Nvl(ro21b, 0) - Nvl(ro21br, 0)
	pn9T = Nvl(ro21t, 0) - Nvl(ro21tr, 0)

	pn10B = Nvl(ro11b, 0) - Nvl(ro11br, 0)
	pn10T = Nvl(ro11t, 0) - Nvl(ro11tr, 0)
	
	pn11B = 0 && 9% achizitii apartamente cu cota 9%
	pn11T = 0
		
	pnRegularizareB = Nvl(ro20b, 0) + Nvl(ro24b, 0) + Nvl(ro21br, 0) + Nvl(ro11br, 0)+ Nvl(ro19b, 0) + Nvl(ro9b, 0) + Nvl(ro5b, 0)
	pnRegularizareT = Nvl(ro20t, 0) + Nvl(ro24t, 0) + Nvl(ro21tr, 0) + Nvl(ro11tr, 0)+ Nvl(ro19t, 0) + Nvl(ro9t, 0) + Nvl(ro5t, 0)

	pn13B = Nvl(roti, 0)
	pn14B = Nvl(wrscdd, 0) + Nvl(wrscddab, 0) + Nvl(wrscddcd, 0)
*!*		pn141B = Nvl(wrscddab, 0)
*!*		pn142B = Nvl(wrscddcd, 0)		

	pn15B = Nvl(wrscfdd, 0)

	*!*		*** LIVRARI D394
	*!*		pn171B = Nvl(loD394.livb + loD394.livtib,0)
	*!*		pn171T = Nvl(loD394.livt + loD394.livtit,0)

	Select cDecont
	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '3.1'
	If Found()
		Replace valoare With pn31B
	Endif

	Locate For Alltrim(Rand) = '9'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '10'
	If Found()
		Replace valoare With pn10B, TVA With pn10T
	Endif

	Locate For Alltrim(Rand) = '11'
	If Found()
		Replace valoare With pn11B, TVA With pn11T
	Endif

	Locate For Alltrim(Rand) = '13'
	If Found()
		Replace valoare With pn13B
	Endif

	Locate For Alltrim(Rand) = '14'
	If Found()
		Replace valoare With pn14B
	Endif


	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pn15B
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '16'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif


	Text To lcSql Noshow Textmerge
	SELECT Round(Sum(RO21B),0) As RO21B, 
		 Round(Sum(RO21T),0) As RO21T, 
		 ROUND(SUM(RO11B),0) AS RO11B, 
		 ROUND(SUM(RO11T),0) AS RO11T, 
		 Round(Sum(RO24B),0) As RO24B, 
		 Round(Sum(RO24T),0) As RO24T, 
		 ROUND(SUM(RO20B),0) AS RO20B, 
		 ROUND(SUM(RO20T),0) AS RO20T, 
		 ROUND(SUM(RO19BCB),0) AS RO19BCB, 
		 ROUND(SUM(RO19BCT),0) AS RO19BCT, 
		 ROUND(SUM(RO19BVB),0) AS RO19BVB, 
		 ROUND(SUM(RO19BVT),0) AS RO19BVT, 
		 ROUND(SUM(RO19BFB),0) AS RO19BFB, 
		 ROUND(SUM(RO19BFT),0) AS RO19BFT, 
		 ROUND(SUM(RO09BCB),0) AS RO09BCB, 
		 ROUND(SUM(RO09BCT),0) AS RO09BCT, 
		 ROUND(SUM(RO09BVB),0) AS RO09BVB, 
		 ROUND(SUM(RO09BVT),0) AS RO09BVT, 
		 ROUND(SUM(RO09BFB),0) AS RO09BFB, 
		 ROUND(SUM(RO09BFT),0) AS RO09BFT, 
		 ROUND(SUM(RO05B),0) AS RO05B, 
		 ROUND(SUM(RO05T),0) AS RO05T, 
		 ROUND(SUM(ROSCN),0) AS ROSCN, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO21B ELSE 0 END),0) AS RO21Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO21T ELSE 0 END),0) AS RO21Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO11B ELSE 0 END),0) AS RO11Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO11T ELSE 0 END),0) AS RO11Tr, 
		 Round(Sum(FO21B),0) As FO21B, 
		 Round(Sum(FO21T),0) As FO21T, 
		 Round(Sum(FO11B),0) As FO11B, 
		 Round(Sum(FO11T),0) As FO11T, 
		 Round(Sum(FO24B),0) As FO24B, 
		 Round(Sum(FO24T),0) As FO24T, 
		 Round(Sum(FO20B),0) As FO20B, 
		 Round(Sum(FO20T),0) As FO20T, 
		 ROUND(SUM(FO19BCB),0) AS FO19BCB, 
		 ROUND(SUM(FO19BCT),0) AS FO19BCT, 
		 ROUND(SUM(FO19BVB),0) AS FO19BVB, 
		 ROUND(SUM(FO19BVT),0) AS FO19BVT, 
		 ROUND(SUM(FO19BFB),0) AS FO19BFB, 
		 ROUND(SUM(FO19BFT),0) AS FO19BFT, 
		 ROUND(SUM(FO09BCB),0) AS FO09BCB, 
		 ROUND(SUM(FO09BCT),0) AS FO09BCT, 
		 ROUND(SUM(FO09BVB),0) AS FO09BVB, 
		 ROUND(SUM(FO09BVT),0) AS FO09BVT, 
		 ROUND(SUM(FO09BFB),0) AS FO09BFB, 
		 ROUND(SUM(FO09BFT),0) AS FO09BFT, 
		 ROUND(SUM(FOSCN),0) AS FOSCN, 
		 ROUND(SUM(XX21TIB),0) AS XX21TIB, 
		 ROUND(SUM(XX21TIT),0) AS XX21TIT, 		 
		 ROUND(SUM(XX11TIB),0) AS XX11TIB, 
		 ROUND(SUM(XX11TIT),0) AS XX11TIT,
		 ROUND(SUM(XX19TIB),0) AS XX19TIB, 
		 ROUND(SUM(XX19TIT),0) AS XX19TIT, 		 
		 ROUND(SUM(XX9TIB),0) AS XX9TIB, 
		 ROUND(SUM(XX9TIT),0) AS XX9TIT, 		 		 
		 Round(Sum(CE24B),0) As CE24B, 
		 Round(Sum(CE24T),0) As CE24T, 
		 Round(Sum(CE20B),0) As CE20B, 
		 Round(Sum(CE20T),0) As CE20T, 
         ROUND(SUM(ce21ctb),0) AS ce21ctb,
         ROUND(SUM(ce21ctt),0) AS ce21ctt,
         ROUND(SUM(ce11ctb),0) AS ce11ctb,
         ROUND(SUM(ce11ctt),0) AS ce11ctt,
         ROUND(SUM(ce19ctb),0) AS ce19ctb,
         ROUND(SUM(ce19ctt),0) AS ce19ctt,
         ROUND(SUM(ce9ctb),0) AS ce9ctb,
         ROUND(SUM(ce9ctt),0) AS ce9ctt,
         ROUND(SUM(ce5ctb),0) AS ce5ctb,
         ROUND(SUM(ce5ctt),0) AS ce5ctt,
         ROUND(SUM(ce21ftb),0) AS ce21ftb,
         ROUND(SUM(ce21ftt),0) AS ce21ftt,
         ROUND(SUM(ce11ftb),0) AS ce11ftb,
         ROUND(SUM(ce11ftt),0) AS ce11ftt,
         ROUND(SUM(ce19ftb),0) AS ce19ftb,
         ROUND(SUM(ce19ftt),0) AS ce19ftt,
         ROUND(SUM(ce9ftb),0) AS ce9ftb,
         ROUND(SUM(ce9ftt),0) AS ce9ftt,
         ROUND(SUM(ce5ftb),0) AS ce5ftb,
         ROUND(SUM(ce5ftt),0) AS ce5ftt,		 
		 Round(Sum(CE21SB),0) As CE21SB, 
		 Round(Sum(CE21ST),0) As CE21ST, 
		 Round(Sum(CE11SB),0) As CE11SB, 
		 Round(Sum(CE11ST),0) As CE11ST, 
		 Round(Sum(CE24SB),0) As CE24SB, 
		 Round(Sum(CE24ST),0) As CE24ST, 
		 Round(Sum(CE20SB),0) As CE20SB, 
		 Round(Sum(CE20ST),0) As CE20ST, 
		 Round(Sum(CE5SB),0) As CE5SB, 
		 Round(Sum(CE5ST),0) As CE5ST, 
		 Round(Sum(CE9SB),0) As CE9SB, 
		 Round(Sum(CE9ST),0) As CE9ST, 
		 Round(Sum(CE19SB),0) As CE19SB, 		
		 Round(Sum(CE19ST),0) As CE19ST, 		 
		 ROUND(SUM(CEBCB),0) AS CEBCB, 
		 ROUND(SUM(CEBCT),0) AS CEBCT, 
		 ROUND(SUM(CEBVB),0) AS CEBVB, 
		 ROUND(SUM(CEBVT),0) AS CEBVT, 
		 ROUND(SUM(CEBVSC),0) AS CEBVSC, 
		 ROUND(SUM(CEBVN),0) AS CEBVN, 
		 ROUND(SUM(CEBFB),0) AS CEBFB, 
		 ROUND(SUM(CEBFT),0) AS CEBFT, 
		 ROUND(SUM(CEBFSC),0) AS CEBFSC, 
		 ROUND(SUM(CEBFN),0) AS CEBFN, 
		 Round(Sum(TI21B), 0) As TI21B, 
		 Round(Sum(TI21T), 0) As TI21T, 
		 Round(Sum(TI11B), 0) As TI11B, 
		 Round(Sum(TI11T), 0) As TI11T, 
		 Round(Sum(TI24B), 0) As TI24B, 
		 Round(Sum(TI24T), 0) As TI24T, 
		 Round(Sum(TI20B), 0) As TI20B, 
		 Round(Sum(TI20T), 0) As TI20T, 
		 ROUND(SUM(TI19BCB),0) AS TI19BCBRO, 
		 ROUND(SUM(TI19BCT),0) AS TI19BCTRO, 
		 ROUND(SUM(TI19BVB),0) AS TI19BVBRO, 
		 ROUND(SUM(TI19BVT),0) AS TI19BVTRO, 
		 ROUND(SUM(TI19BFB),0) AS TI19BFBRO, 
		 ROUND(SUM(TI19BFT),0) AS TI19BFTRO, 
		 ROUND(SUM(TI09BVB),0) AS TI09BVBRO, 
		 ROUND(SUM(TI09BVT),0) AS TI09BVTRO, 
		 ROUND(SUM(TI09BFB),0) AS TI09BFBRO, 
		 ROUND(SUM(TI09BFT),0) AS TI09BFTRO,
		 ROUND(SUM(ROTN+ROTN50+ROTN100),0) AS ROTN,
         ROUND(SUM(rortd21),0) AS rortd21,
         ROUND(SUM(rortd11),0) AS rortd11,
         ROUND(SUM(rortd24),0) AS rortd24,
         ROUND(SUM(rortd20),0) AS rortd20,
         ROUND(SUM(rortd19),0) AS rortd19,
         ROUND(SUM(rortd9),0) AS rortd9,
         ROUND(SUM(rortd5),0) AS rortd5 		 		  
		 FROM JC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF <<m.gcCondSucursala>>
	Endtext
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC
	Go Top
	pn20B = NVL(CE21CTB,0) + NVL(CE11CTB,0) + NVL(CE21FTB,0) + NVL(CE11FTB,0) && ach. intracomunitare pre SAFT + ach. intracomunitare furnizori inregistrati/neinregistrati TVA
	pn20T = NVL(CE21CTT,0) + NVL(CE11CTT,0) + NVL(CE21FTT,0) + NVL(CE11FTT,0)
	pn201B = NVL(CE21CTB,0) + NVL(CE11CTB,0) && ach. intracomunitare pre SAFT + ach. intracomunitare furnizori inregistrati TVA
	pn201T = NVL(CE21CTT,0) + NVL(CE11CTT,0)
	pn5B = pn20B
	pn5T = pn20T
	pn51B = pn201B
	pn51T = pn201T

	*** rd.21 = rd.6
	*** Regularizari privind achizitiile intracomunitare de bunuri pentru care cumparatorul este obligat la plata TVA (taxare inversa) (rd.19=rd.6)
	pn21B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0) + NVL(CE19CTB,0) + NVL(CE9CTB,0) + NVL(CE5CTB,0) + NVL(CE19FTB,0) + NVL(CE9FTB,0) + NVL(CE5FTB,0) + Nvl(ce20b, 0) + Nvl(ce24b, 0)
	pn21T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0) + NVL(CE19CTT,0) + NVL(CE9CTT,0) + NVL(CE5CTT,0) + NVL(CE19FTT,0) + NVL(CE9FTT,0) + NVL(CE5FTT,0) + Nvl(ce20t, 0) + Nvl(ce24t, 0)
	pn6B = pn21B
	pn6T = pn21T

	*** Achizitii de bunuri, altele decat cele de la rd. 18 si 19, si achizitii de servicii pentru care beneficiarul din Romania este obligat la plata TVA (taxare
	*** inversa) (rd.22=rd.7), din care
	pn22B = Nvl(CE21SB, 0) + Nvl(XX21TIB, 0) + Nvl(XX11TIB, 0)
	pn22T = Nvl(CE21ST, 0) + Nvl(XX21TIT, 0) + Nvl(XX11TIT, 0)
	pn7B = pn22B
	pn7T = pn22T

	*** Achizitii de servicii intracomunitare pentru care beneficiarul este obligat la plata TVA (taxare inversa) (rd.22.1=rd.7.1)
	pn221B = Nvl(CE21SB, 0)
	pn221T = Nvl(CE21ST, 0)
	pn71B = pn221B
	pn71T = pn221T

	*** Regularizari privind achizitii de servicii intracomunitare pentru care beneficiarul din Romania este obligat la plata TVA (taxare inversa)(rd.23=rd.8)
	pn23B = Nvl(CE19SB, 0) + Nvl(XX19TIB, 0) + Nvl(XX9TIB, 0) + Nvl(CE24SB, 0) + Nvl(CE20SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn23T = Nvl(CE19ST, 0) + Nvl(XX19TIT, 0) + Nvl(XX9TIT, 0) + Nvl(CE24ST, 0) + Nvl(CE20ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn8B = pn23B
	pn8T = pn23T

	*** Achizitii de bunuri si servicii taxabile cu cota de 21%, altele decat cele de la rd.27	
	&& scad tva exigibil din operatiile cu data act in afara perioadei de declarare. le pun pe regularizari, rd. 34
	pn24B = Nvl(ro21b, 0) + Nvl(fo21b, 0) - Nvl(ro21br, 0)
	pn24T = Nvl(ro21t, 0) + Nvl(fo21t, 0) - Nvl(ro21tr, 0)

	*** Achizitii de bunuri si servicii taxabile cu cota de 11%, altele decat cele de la rd.27	
	pn25B = Nvl(ro11b, 0) + Nvl(fo11b, 0) - Nvl(ro11br, 0)
	pn25T = Nvl(ro11t, 0) + Nvl(fo11t, 0) - Nvl(ro11tr, 0)

	*** Achizitii de bunuri si servicii supuse masurilor de simplificare pentru care beneficiarul este obligat la plata TVA (taxare inversa),din care (rd.26=rd.12)
	pn26B = Nvl(ti21b, 0) + Nvl(ti11b, 0) && ACH. TI
	pn26T = Nvl(ti21t, 0) + Nvl(ti11t, 0)
	pn12B = pn26B
	pn12T = pn26T

	pn261B = Nvl(ti21b, 0) && ACH. TI 21%
	pn261T = Nvl(ti21t, 0)
	pn121B = pn261B
	pn121T = pn261T

	pn262B = Nvl(ti11b, 0) && && ACH. TI 11%
	pn262T = Nvl(ti11t, 0)
	pn122B = pn262B
	pn122T = pn262T

	*** Achizitii de bunuri si servicii scutite de taxa sau neimpozabile, din care
	pn29B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0) && Achizi_ii de bunuri ši servicii scutite de taxa sau neimpozabile
	pn291B = Nvl(cebvsc, 0) + Nvl(cebfsc, 0) && Achizitii de servicii intracomunitare scutite de taxa

	*** Regularizari taxa dedusa
	pn33B = Nvl(ro21br, 0) + Nvl(ro11br, 0) + ;
		Nvl(ro20b, 0) + Nvl(ro24b, 0) + Nvl(fo20b, 0) + Nvl(fo24b, 0) + Nvl(ti24b, 0) + Nvl(ti20b, 0) + ;
		Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0) + ;
		Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0) + ;
		Nvl(ro05b, 0) + ;
		Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0)		
		
	pn33T = Nvl(ro21tr, 0) + Nvl(ro11tr, 0) + ;
		Nvl(ro20t, 0) + Nvl(ro24t, 0) + Nvl(fo20t, 0) + Nvl(fo24t, 0) + Nvl(ti24b, 0) + Nvl(ti20b, 0) + ;
		Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0) + ;
		Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0) + ;
		Nvl(ro05t, 0) + ;
		Nvl(ti24t, 0) + Nvl(ti20t, 0) + Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)


	lnROTN = Int(Round(Nvl(ROTN, 0), 0)) &&  && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001

	Select cDecont
	Locate For Alltrim(Rand) = '20'
	If Found()
		Replace valoare With pn20B, TVA With pn20T
	Endif

	Locate For Alltrim(Rand) = '20.1'
	If Found()
		Replace valoare With pn201B, TVA With pn201T
	Endif

	Locate For Alltrim(Rand) = '5'
	If Found()
		Replace valoare With pn5B, TVA With pn5T
	Endif

	Locate For Alltrim(Rand) = '5.1'
	If Found()
		Replace valoare With pn51B, TVA With pn51T
	Endif

	Locate For Alltrim(Rand) = '21'
	If Found()
		Replace valoare With pn21B, TVA With pn21T
	Endif

	Locate For Alltrim(Rand) = '6'
	If Found()
		Replace valoare With pn6B, TVA With pn6T
	Endif

	Locate For Alltrim(Rand) = '22'
	If Found()
		Replace valoare With pn22B, TVA With pn22T
	Endif

	Locate For Alltrim(Rand) = '22.1'
	If Found()
		Replace valoare With pn221B, TVA With pn221T
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '7.1'
	If Found()
		Replace valoare With pn71B, TVA With pn71T
	Endif

	Locate For Alltrim(Rand) = '23'
	If Found()
		Replace valoare With pn23B, TVA With pn23T
	Endif

	Locate For Alltrim(Rand) = '8'
	If Found()
		Replace valoare With pn8B, TVA With pn8T
	Endif

	Locate For Alltrim(Rand) = '24'
	If Found()
		Replace valoare With pn24B, TVA With pn24T
	Endif

	Locate For Alltrim(Rand) = '25'
	If Found()
		Replace valoare With pn25B, TVA With pn25T
	Endif

	Locate For Alltrim(Rand) = '26'
	If Found()
		Replace valoare With pn26B, TVA With pn26T
	Endif

	Locate For Alltrim(Rand) = '26.1'
	If Found()
		Replace valoare With pn261B, TVA With pn261T
	Endif

	Locate For Alltrim(Rand) = '26.2'
	If Found()
		Replace valoare With pn262B, TVA With pn262T
	Endif
		
	Locate For Alltrim(Rand) = '12'
	If Found()
		Replace valoare With pn12B, TVA With pn12T
	Endif

	Locate For Alltrim(Rand) = '12.1'
	If Found()
		Replace valoare With pn121B, TVA With pn121T
	Endif

	Locate For Alltrim(Rand) = '12.2'
	If Found()
		Replace valoare With pn122B, TVA With pn122T
	Endif
	
	Locate For Alltrim(Rand) = '29'
	If Found()
		Replace valoare With pn29B
	Endif

	Locate For Alltrim(Rand) = '29.1'
	If Found()
		Replace valoare With pn291B
	Endif

	*!* REGULARIZARE
	Locate For Alltrim(Rand) = '33'
	If Found()
		Replace valoare With pn33B, TVA With pn33T
	Endif

	*** Rd. 41 Soldul sumei negative a TVA reportate din perioada precedenta pentru care nu s-a solicitat rambursare (rd.44 din decontul perioadei fiscale precedente)
	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala

	lnTR = 0
	llSucces = goExecutor.oSelecteaza2Value(m.lcSql, @lnTR)
	IF m.llSucces
		lnTR = Int(Round(lnTR, 0))
	ENDIF 	

	Select cDecont
	Locate For Alltrim(Rand) = '41'
	If Found()
		Replace TVA With lnTR
	Endif


	*** Rd. 38 Soldul TVA de plata din decontul perioadei fiscale precedente (rd.45 din decontul perioadei fiscale precedente) neachitate pana la data depunerii decontului de TVA
	Text To lcSql Textmerge Noshow
	SELECT NVL(soldcred,0) as  soldcred
		from vbal
		where cont = '4423' and AN = ?pnAnPrec AND LUNA = ?pnLunaPrec
		<<m.gcCondSucursala>>
	Endtext

	lnTP = 0
	llSucces = goExecutor.oSelecteaza2Value(m.lcSql, @lnTP)
	IF m.llSucces
		lnTP = Int(Round(lnTP, 0))
	ENDIF 	

	* cat TVA am achitat in perioada curenta pentru TVA de plata 4423
	Text To lcSql Textmerge Noshow
	select  nvl(sum(suma),0) as suma
		from act
		where an = ?gnAn and luna between ?pnLunaI and ?pnLunaF
		and sters = 0
		and scd = '4423' and scc In ('5121', '5311')
		<<m.gcCondSucursala>>
	Endtext

	lnPlati = 0
	llSucces = goExecutor.oSelecteaza2Value(m.lcSql, @lnPlati)
	IF m.llSucces
		lnPlati = Int(Round(lnPlati, 0))
	ENDIF 	
	
	lnTP = Max(m.lnTP - m.lnPlati, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '38'
	If Found()
		Replace TVA With Nvl(m.lnTP, 0)
	Endif

	*** Regularizari
	lcSql = [select sum(suma) as suma, id_jtva_coloana from act ] + ;
		[where sters = 0 and id_jtva_coloana in (1002,1003,1006,1007,1008,1009,1010,1011,1012,1013,1014,1015,1016,1017,1018,1019) and ] + ;
		[an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + m.gcCondSucursala + ;
		[ group by id_jtva_coloana order by id_jtva_coloana]
	lcCursor = [crsRegTaxe]
	If goExecutor.oExecuta(lcSql, lcCursor)
		Select (lcCursor)
		Scan
			lnSuma = Int(Round(suma, 0))
			IF INLIST(id_jtva_coloana, 1003,1011,1012,1013,1014,1015,1018,1019) && regularizare taxa dedusa
				lcRand = '33' && regularizare taxa dedusa
			ELSE 
				lcRand = '16' && regularizare taxa colectata
			ENDIF 	
			Select cDecont
			Replace TVA With TVA - lnSuma For Rand = lcRand
			Select (lcCursor)
		Endscan
	Endif
	Use In (Select(lcCursor))

	*** SOLD NEEXIGIBIL
	Text To lcSql Noshow Textmerge
	select sum(RO21NB+RO11NB+RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO21NT+RO11NT+RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBazaN = Int(Round(bazan, 0))
		pnAchizitieTVAN = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO21NB+RO11NB+RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO21NT+RO11NT+RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBaza6N = Int(Round(bazan, 0))
		pnAchizitieTVA6N = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))


	Text To lcSql Noshow Textmerge
	select sum(RO21NB+RO11NB+RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO21NT+RO11NT+RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBazaN = Int(Round(bazan, 0))
		pnLivrareTVAN = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO21NB+RO11NB+RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO21NT+RO11NT+RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBaza6N = Int(Round(bazan, 0))
		pnLivrareTVA6N = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))

	Select cDecont
	lodt = Createobject('frm_decontTVA_202601', m.tnNumarLuni)

	lodt.nROTN = Nvl(m.lnROTN, 0) && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001
	lodt.lblRandAtentie.Caption = lodt.lblRandAtentie.Caption + ' (' + Alltrim(Str(m.lnROTN)) + ' lei)'
	lodt.Show(1)
Endproc && viz_decontTVA_2601
*===========================================
Procedure viz_decontTVA_2508
	Lparameters tnNumarLuni
	*Parameters tlTrim

	*** tnNumarLuni: 1 = o luna, 3 = 3 luni trimestrial, 6 = 6 luni semestrial, 12 = 12 luni anual

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec

	Local lodt As 'frm_decontTVA'
	Local lcCursor, lcSql, ldDataCurenta, ldDataPrecedenta, lnAnCurent, lnAnPrecedent, lnLunaCurenta
	Local lnLunaMin, lnLunaPrecedenta, lnPerioada, lnPlati, lnSucces, lnTP, lnTR, loEx As Exception, lnROTN
	*:Global pn10B, pn10T, pn11B, pn11T, pn13B, pn14B, pn15B, pn171B, pn171T, pn17B, pn17T, pn1911B
	*:Global pn1911T, pn191B, pn191T, pn19B, pn19T, pn1B, pn21B, pn21T, pn22B, pn22T, pn23B, pn23T
	*:Global pn241B, pn24B, pn31B, pn3B, pn51B, pn51T, pn5B, pn5T, pn711B, pn711T, pn71B, pn71T, pn7B
	*:Global pn7T, pn9B, pn9T, pnRegularizareB, pnRegularizareT

	lnAnCurent = m.gnAn
	lnLunaCurenta = m.gnLuna
	ldDataCurenta = Date(m.lnAnCurent, m.lnLunaCurenta, 1)
	lnROTN = 0

	* Luna de inceput a perioadei
	* 1 = gnLuna
	* 3 = 1,4,7,10
	* 6 = 1,7
	* 12 = 1
	lnPerioada = Ceil(m.lnLunaCurenta / tnNumarLuni)
	lnLunaMin = (lnPerioada - 1) * tnNumarLuni + 1

	*** luna pentru balanta precedenta
	ldDataPrecedenta = Gomonth(Date(m.lnAnCurent, m.lnLunaMin, 1), -1)
	lnLunaPrecedenta = Month(m.ldDataPrecedenta)
	lnAnPrecedent = Year(m.ldDataPrecedenta)
	&& ---
	pnLunaI = lnLunaMin
	pnLunaF = m.lnLunaCurenta

	pnLunaPrec = m.lnLunaPrecedenta
	pnAnPrec = m.lnAnPrecedent

	Private poDecont
	Store '' To poDecont

	Private pnProRata, pnAchizitieBazaN, pnAchizitieTVAN, pnLivrareBazaN, pnLivrareTVAN, pnAchizitieBaza6N, pnAchizitieTVA6N, pnLivrareBaza6N, pnLivrareTVA6N
	pnProRata = 100
	pnAchizitieBazaN = 0
	pnAchizitieTVAN = 0
	pnLivrareBazaN = 0
	pnLivrareTVAN = 0
	pnAchizitieBaza6N = 0
	pnAchizitieTVA6N = 0
	pnLivrareBaza6N = 0
	pnLivrareTVA6N = 0

	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	&& schimbare cota TVA standard de la 19% la 21%
	Do Case
		CASE m.gnAn*12+m.gnLuna >= 2025*12+8
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '2508' order by id_decont]						
	Endcase
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Text To lcSql Noshow Textmerge
	SELECT 
	ROUND(SUM(RO21B),0) AS RO21B, 
		 ROUND(SUM(RO21T),0) AS RO21T, 
		 ROUND(SUM(RO11B),0) AS RO11B, 
		 ROUND(SUM(RO11T),0) AS RO11T,
		 ROUND(SUM(RO19B),0) AS RO19B, 
		 ROUND(SUM(RO19T),0) AS RO19T, 
		 ROUND(SUM(RO9B),0) AS RO9B, 
		 ROUND(SUM(RO9T),0) AS RO9T,
		 ROUND(SUM(RO5B),0) AS RO5B, 
		 ROUND(SUM(RO5T),0) AS RO5T, 
		 ROUND(SUM(RO20B),0) AS RO20B, 
		 ROUND(SUM(RO20T),0) AS RO20T,
		 ROUND(SUM(RO24B),0) AS RO24B, 
		 ROUND(SUM(RO24T),0) AS RO24T,
		 ROUND(SUM(ROTI),0) AS ROTI, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO21B ELSE 0 END),0) AS RO21Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO21T ELSE 0 END),0) AS RO21Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO11B ELSE 0 END),0) AS RO11Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO11T ELSE 0 END),0) AS RO11Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19B ELSE 0 END),0) AS RO19Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19T ELSE 0 END),0) AS RO19Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO9B ELSE 0 END),0) AS RO9Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO9T ELSE 0 END),0) AS RO9Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO5B ELSE 0 END),0) AS RO5Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO5T ELSE 0 END),0) AS RO5Tr, 
		 ROUND(SUM(CESCDD1),0) AS CESCDD1,
		 ROUND(SUM(CESCDD2),0) AS CESCDD2,
		 ROUND(SUM(CEOPTR),0) AS CEOPTR,
		 ROUND(SUM(CESVDD),0) AS CESVDD,
		 ROUND(SUM(CESVFDD),0) AS CESVFDD,
		 ROUND(SUM(CESVFS),0) AS CESVFS,
		 ROUND(SUM(WRSCDD),0) AS WRSCDD, 
		 ROUND(SUM(WRSCDDAB),0) AS WRSCDDAB, 
		 ROUND(SUM(WRSCDDCD),0) AS WRSCDDCD, 		 		 
		 ROUND(SUM(FODD),0) AS FODD, 
		 ROUND(SUM(FOFDD),0) AS FOFDD, 
		 ROUND(SUM(CEOPTR),0) AS CEOPTR, 
		 ROUND(SUM(CESVDD),0) AS CESVDD, 
		 ROUND(SUM(CESVFDD),0) AS CESVFDD, 
		 ROUND(SUM(CESVFS),0) AS CESVFS, 
		 ROUND(SUM(WRSCFDD),0) AS WRSCFDD,
		 ROUND(SUM(WRN),0) AS WRN,
         ROUND(SUM(rortc21),0) AS rortc21,
         ROUND(SUM(rortc11),0) AS rortc11,
		 ROUND(SUM(rortc24),0) AS rortc24,
         ROUND(SUM(rortc20),0) AS rortc20,
         ROUND(SUM(rortc19),0) AS rortc19,
         ROUND(SUM(rortc9),0) AS rortc9,
         ROUND(SUM(rortc5),0) AS rortc5
		  FROM JV2007 
		 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF <<m.gcCondSucursala>>
	Endtext
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala


	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	Go Top
	pn1B = Nvl(cescdd1, 0) + Nvl(ceoptr, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0) + Nvl(cesvdd, 0) + Nvl(cesvfdd, 0) + Nvl(cesvfs, 0)
	pn31B = Nvl(cesvfs, 0)

	&& scad tva exigibil cu data act in afara perioadei de declarare. il pun pe regularizare rd. 16 
	pn9B = Nvl(ro21b, 0) - Nvl(ro21br, 0)
	pn9T = Nvl(ro21t, 0) - Nvl(ro21tr, 0)
	pn9_1B = Nvl(ro19b, 0) - Nvl(ro19br, 0)
	pn9_1T = Nvl(ro19t, 0) - Nvl(ro19tr, 0)

	pn10B = Nvl(ro11b, 0) - Nvl(ro11br, 0)
	pn10T = Nvl(ro11t, 0) - Nvl(ro11tr, 0)
	pn10_1B = Nvl(ro9b, 0) - Nvl(ro9br, 0)
	pn10_1T = Nvl(ro9t, 0) - Nvl(ro9tr, 0)
	
	pn11B = Nvl(ro5b, 0) - Nvl(ro5br, 0)
	pn11T = Nvl(ro5t, 0) - Nvl(ro5tr, 0)
	pnRegularizareB = Nvl(ro20b, 0) + Nvl(ro24b, 0) + Nvl(ro21br, 0) + Nvl(ro11br, 0)+ Nvl(ro19br, 0) + Nvl(ro9br, 0) + Nvl(ro5br, 0)
	pnRegularizareT = Nvl(ro20t, 0) + Nvl(ro24t, 0) + Nvl(ro21tr, 0) + Nvl(ro11tr, 0)+ Nvl(ro19tr, 0) + Nvl(ro9tr, 0) + Nvl(ro5tr, 0)

	pn13B = Nvl(roti, 0)
	pn14B = Nvl(wrscdd, 0) + Nvl(wrscddab, 0) + Nvl(wrscddcd, 0)
*!*		pn141B = Nvl(wrscddab, 0)
*!*		pn142B = Nvl(wrscddcd, 0)		

	pn15B = Nvl(wrscfdd, 0)

	*!*		*** LIVRARI D394
	*!*		pn171B = Nvl(loD394.livb + loD394.livtib,0)
	*!*		pn171T = Nvl(loD394.livt + loD394.livtit,0)

	Select cDecont
	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '3.1'
	If Found()
		Replace valoare With pn31B
	Endif

	Locate For Alltrim(Rand) = '9'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '9.1'
	If Found()
		Replace valoare With pn9_1B, TVA With pn9_1T
	ENDIF
	
	Locate For Alltrim(Rand) = '10'
	If Found()
		Replace valoare With pn10B, TVA With pn10T
	Endif

	Locate For Alltrim(Rand) = '10.1'
	If Found()
		Replace valoare With pn10_1B, TVA With pn10_1T
	ENDIF
	
	Locate For Alltrim(Rand) = '11'
	If Found()
		Replace valoare With pn11B, TVA With pn11T
	Endif

	Locate For Alltrim(Rand) = '13'
	If Found()
		Replace valoare With pn13B
	Endif

	Locate For Alltrim(Rand) = '14'
	If Found()
		Replace valoare With pn14B
	Endif

*!*		Locate For Alltrim(Rand) = '14.1'
*!*		If Found()
*!*			Replace valoare With pn141B
*!*		Endif

*!*		Locate For Alltrim(Rand) = '14.2'
*!*		If Found()
*!*			Replace valoare With pn142B
*!*		Endif

	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pn15B
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '16'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	*!*		*!* LIVRARI INTERNE CATRE CLIENTI PLATITORI TVA IN ROMANIA (LIVRARI D394)
	*!*		Locate For Alltrim(Rand) = '17.1'
	*!*		If Found()
	*!*			Replace valoare With pn171B, TVA With pn171T
	*!*		Endif

	Text To lcSql Noshow Textmerge
	SELECT Round(Sum(RO21B),0) As RO21B, 
		 Round(Sum(RO21T),0) As RO21T, 
		 ROUND(SUM(RO11B),0) AS RO11B, 
		 ROUND(SUM(RO11T),0) AS RO11T, 
		 Round(Sum(RO24B),0) As RO24B, 
		 Round(Sum(RO24T),0) As RO24T, 
		 ROUND(SUM(RO20B),0) AS RO20B, 
		 ROUND(SUM(RO20T),0) AS RO20T, 
		 ROUND(SUM(RO19BCB),0) AS RO19BCB, 
		 ROUND(SUM(RO19BCT),0) AS RO19BCT, 
		 ROUND(SUM(RO19BVB),0) AS RO19BVB, 
		 ROUND(SUM(RO19BVT),0) AS RO19BVT, 
		 ROUND(SUM(RO19BFB),0) AS RO19BFB, 
		 ROUND(SUM(RO19BFT),0) AS RO19BFT, 
		 ROUND(SUM(RO09BCB),0) AS RO09BCB, 
		 ROUND(SUM(RO09BCT),0) AS RO09BCT, 
		 ROUND(SUM(RO09BVB),0) AS RO09BVB, 
		 ROUND(SUM(RO09BVT),0) AS RO09BVT, 
		 ROUND(SUM(RO09BFB),0) AS RO09BFB, 
		 ROUND(SUM(RO09BFT),0) AS RO09BFT, 
		 ROUND(SUM(RO05B),0) AS RO05B, 
		 ROUND(SUM(RO05T),0) AS RO05T, 
		 ROUND(SUM(ROSCN),0) AS ROSCN, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO21B ELSE 0 END),0) AS RO21Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO21T ELSE 0 END),0) AS RO21Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO11B ELSE 0 END),0) AS RO11Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO11T ELSE 0 END),0) AS RO11Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19BCB + RO19BVB + RO19BFB ELSE 0 END),0) AS RO19Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19BCT + RO19BVT + RO19BFT ELSE 0 END),0) AS RO19Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO09BCB + RO09BVB + RO09BFB ELSE 0 END),0) AS RO09Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO09BCT + RO09BVT + RO09BFT ELSE 0 END),0) AS RO09Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO05B ELSE 0 END),0) AS RO05Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO05T ELSE 0 END),0) AS RO05Tr, 		
		 Round(Sum(FO21B),0) As FO21B, 
		 Round(Sum(FO21T),0) As FO21T, 
		 Round(Sum(FO11B),0) As FO11B, 
		 Round(Sum(FO11T),0) As FO11T, 
		 Round(Sum(FO24B),0) As FO24B, 
		 Round(Sum(FO24T),0) As FO24T, 
		 Round(Sum(FO20B),0) As FO20B, 
		 Round(Sum(FO20T),0) As FO20T, 
		 ROUND(SUM(FO19BCB),0) AS FO19BCB, 
		 ROUND(SUM(FO19BCT),0) AS FO19BCT, 
		 ROUND(SUM(FO19BVB),0) AS FO19BVB, 
		 ROUND(SUM(FO19BVT),0) AS FO19BVT, 
		 ROUND(SUM(FO19BFB),0) AS FO19BFB, 
		 ROUND(SUM(FO19BFT),0) AS FO19BFT, 
		 ROUND(SUM(FO09BCB),0) AS FO09BCB, 
		 ROUND(SUM(FO09BCT),0) AS FO09BCT, 
		 ROUND(SUM(FO09BVB),0) AS FO09BVB, 
		 ROUND(SUM(FO09BVT),0) AS FO09BVT, 
		 ROUND(SUM(FO09BFB),0) AS FO09BFB, 
		 ROUND(SUM(FO09BFT),0) AS FO09BFT, 
		 ROUND(SUM(FOSCN),0) AS FOSCN, 
		 ROUND(SUM(XX21TIB),0) AS XX21TIB, 
		 ROUND(SUM(XX21TIT),0) AS XX21TIT, 		 
		 ROUND(SUM(XX11TIB),0) AS XX11TIB, 
		 ROUND(SUM(XX11TIT),0) AS XX11TIT,
		 ROUND(SUM(XX19TIB),0) AS XX19TIB, 
		 ROUND(SUM(XX19TIT),0) AS XX19TIT, 		 
		 ROUND(SUM(XX9TIB),0) AS XX9TIB, 
		 ROUND(SUM(XX9TIT),0) AS XX9TIT, 		 		 
		 Round(Sum(CE24B),0) As CE24B, 
		 Round(Sum(CE24T),0) As CE24T, 
		 Round(Sum(CE20B),0) As CE20B, 
		 Round(Sum(CE20T),0) As CE20T, 
         ROUND(SUM(ce21ctb),0) AS ce21ctb,
         ROUND(SUM(ce21ctt),0) AS ce21ctt,
         ROUND(SUM(ce11ctb),0) AS ce11ctb,
         ROUND(SUM(ce11ctt),0) AS ce11ctt,
         ROUND(SUM(ce19ctb),0) AS ce19ctb,
         ROUND(SUM(ce19ctt),0) AS ce19ctt,
         ROUND(SUM(ce9ctb),0) AS ce9ctb,
         ROUND(SUM(ce9ctt),0) AS ce9ctt,
         ROUND(SUM(ce5ctb),0) AS ce5ctb,
         ROUND(SUM(ce5ctt),0) AS ce5ctt,
         ROUND(SUM(ce21ftb),0) AS ce21ftb,
         ROUND(SUM(ce21ftt),0) AS ce21ftt,
         ROUND(SUM(ce11ftb),0) AS ce11ftb,
         ROUND(SUM(ce11ftt),0) AS ce11ftt,
         ROUND(SUM(ce19ftb),0) AS ce19ftb,
         ROUND(SUM(ce19ftt),0) AS ce19ftt,
         ROUND(SUM(ce9ftb),0) AS ce9ftb,
         ROUND(SUM(ce9ftt),0) AS ce9ftt,
         ROUND(SUM(ce5ftb),0) AS ce5ftb,
         ROUND(SUM(ce5ftt),0) AS ce5ftt,		 
		 Round(Sum(CE21SB),0) As CE21SB, 
		 Round(Sum(CE21ST),0) As CE21ST, 
		 Round(Sum(CE11SB),0) As CE11SB, 
		 Round(Sum(CE11ST),0) As CE11ST, 
		 Round(Sum(CE24SB),0) As CE24SB, 
		 Round(Sum(CE24ST),0) As CE24ST, 
		 Round(Sum(CE20SB),0) As CE20SB, 
		 Round(Sum(CE20ST),0) As CE20ST, 
		 Round(Sum(CE5SB),0) As CE5SB, 
		 Round(Sum(CE5ST),0) As CE5ST, 
		 Round(Sum(CE9SB),0) As CE9SB, 
		 Round(Sum(CE9ST),0) As CE9ST, 
		 Round(Sum(CE19SB),0) As CE19SB, 		
		 Round(Sum(CE19ST),0) As CE19ST, 		 
		 ROUND(SUM(CEBCB),0) AS CEBCB, 
		 ROUND(SUM(CEBCT),0) AS CEBCT, 
		 ROUND(SUM(CEBVB),0) AS CEBVB, 
		 ROUND(SUM(CEBVT),0) AS CEBVT, 
		 ROUND(SUM(CEBVSC),0) AS CEBVSC, 
		 ROUND(SUM(CEBVN),0) AS CEBVN, 
		 ROUND(SUM(CEBFB),0) AS CEBFB, 
		 ROUND(SUM(CEBFT),0) AS CEBFT, 
		 ROUND(SUM(CEBFSC),0) AS CEBFSC, 
		 ROUND(SUM(CEBFN),0) AS CEBFN, 
		 Round(Sum(TI21B), 0) As TI21B, 
		 Round(Sum(TI21T), 0) As TI21T, 
		 Round(Sum(TI11B), 0) As TI11B, 
		 Round(Sum(TI11T), 0) As TI11T, 
		 Round(Sum(TI24B), 0) As TI24B, 
		 Round(Sum(TI24T), 0) As TI24T, 
		 Round(Sum(TI20B), 0) As TI20B, 
		 Round(Sum(TI20T), 0) As TI20T, 
		 ROUND(SUM(TI19BCB),0) AS TI19BCBRO, 
		 ROUND(SUM(TI19BCT),0) AS TI19BCTRO, 
		 ROUND(SUM(TI19BVB),0) AS TI19BVBRO, 
		 ROUND(SUM(TI19BVT),0) AS TI19BVTRO, 
		 ROUND(SUM(TI19BFB),0) AS TI19BFBRO, 
		 ROUND(SUM(TI19BFT),0) AS TI19BFTRO, 
		 ROUND(SUM(TI09BVB),0) AS TI09BVBRO, 
		 ROUND(SUM(TI09BVT),0) AS TI09BVTRO, 
		 ROUND(SUM(TI09BFB),0) AS TI09BFBRO, 
		 ROUND(SUM(TI09BFT),0) AS TI09BFTRO,
		 ROUND(SUM(ROTN+ROTN50+ROTN100),0) AS ROTN,
         ROUND(SUM(rortd21),0) AS rortd21,
         ROUND(SUM(rortd11),0) AS rortd11,
         ROUND(SUM(rortd24),0) AS rortd24,
         ROUND(SUM(rortd20),0) AS rortd20,
         ROUND(SUM(rortd19),0) AS rortd19,
         ROUND(SUM(rortd9),0) AS rortd9,
         ROUND(SUM(rortd5),0) AS rortd5 		 		  
		 FROM JC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF <<m.gcCondSucursala>>
	Endtext
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC
	Go Top
	pn20B = NVL(CE21CTB,0) + NVL(CE11CTB,0) + NVL(CE21FTB,0) + NVL(CE11FTB,0) && ach. intracomunitare pre SAFT + ach. intracomunitare furnizori inregistrati/neinregistrati TVA
	pn20T = NVL(CE21CTT,0) + NVL(CE11CTT,0) + NVL(CE21FTT,0) + NVL(CE11FTT,0)
	pn201B = NVL(CE21CTB,0) + NVL(CE11CTB,0) && ach. intracomunitare pre SAFT + ach. intracomunitare furnizori inregistrati TVA
	pn201T = NVL(CE21CTT,0) + NVL(CE11CTT,0)
	pn5B = pn20B
	pn5T = pn20T
	pn51B = pn201B
	pn51T = pn201T

	*** rd.21 = rd.6
	*** Regularizari privind achizi_iile intracomunitare de bunuri pentru care cumparatorul este obligat la plata TVA (taxare inversa) (rd.19=rd.6)
	pn21B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0) + NVL(CE19CTB,0) + NVL(CE9CTB,0) + NVL(CE5CTB,0) + NVL(CE19FTB,0) + NVL(CE9FTB,0) + NVL(CE5FTB,0) + Nvl(ce20b, 0) + Nvl(ce24b, 0)
	pn21T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0) + NVL(CE19CTT,0) + NVL(CE9CTT,0) + NVL(CE5CTT,0) + NVL(CE19FTT,0) + NVL(CE9FTT,0) + NVL(CE5FTT,0) + Nvl(ce20t, 0) + Nvl(ce24t, 0)
	pn6B = pn21B
	pn6T = pn21T

	*** Achizi_ii de bunuri, altele decat cele de la rd. 18 ši 19, si achizitii de servicii pentru care beneficiarul din Romania este obligat la plata TVA (taxare
	*** inversa) (rd.22=rd.7), din care
	pn22B = Nvl(CE21SB, 0) + Nvl(XX21TIB, 0) + Nvl(XX11TIB, 0)
	pn22T = Nvl(CE21ST, 0) + Nvl(XX21TIT, 0) + Nvl(XX11TIT, 0)
	pn7B = pn22B
	pn7T = pn22T

	*** Achizitii de servicii intracomunitare pentru care beneficiarul este obligat la plata TVA (taxare inversa) (rd.22.1=rd.7.1)
	pn221B = Nvl(CE21SB, 0)
	pn221T = Nvl(CE21ST, 0)
	pn71B = pn221B
	pn71T = pn221T

	*** Regularizari privind achizitii de servicii intracomunitare pentru care beneficiarul din Romania este obligat la plata TVA (taxare inversa)(rd.23=rd.8)
	pn23B = Nvl(CE19SB, 0) + Nvl(XX19TIB, 0) + Nvl(XX9TIB, 0) + Nvl(CE24SB, 0) + Nvl(CE20SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn23T = Nvl(CE19ST, 0) + Nvl(XX19TIT, 0) + Nvl(XX9TIT, 0) + Nvl(CE24ST, 0) + Nvl(CE20ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn8B = pn23B
	pn8T = pn23T

	*** Achizitii de bunuri si servicii taxabile cu cota de 21%, altele decat cele de la rd.27	
	&& scad tva exigibil din operatiile cu data act in afara perioadei de declarare. le pun pe regularizari, rd. 34
	pn24B = Nvl(ro21b, 0) + Nvl(fo21b, 0) - Nvl(ro21br, 0)
	pn24T = Nvl(ro21t, 0) + Nvl(fo21t, 0) - Nvl(ro21tr, 0)

	*** Achizitii de bunuri si servicii taxabile cu cota de 19%, altele decat cele de la rd.27	
	pn241B = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0) - Nvl(ro19br, 0)
	pn241T = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0) - Nvl(ro19tr, 0)

	*** Achizitii de bunuri si servicii taxabile cu cota de 11%, altele decat cele de la rd.27	
	pn25B = Nvl(ro11b, 0) + Nvl(fo11b, 0) - Nvl(ro11br, 0)
	pn25T = Nvl(ro11t, 0) + Nvl(fo11t, 0) - Nvl(ro11tr, 0)

	*** Achizitii de bunuri si servicii taxabile cu cota de 9%
	pn251B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0) - Nvl(ro09br, 0)
	pn251T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0) - Nvl(ro09tr, 0)

	*** Achizitii de bunuri taxabile cu cota de 5%
	pn26B = Nvl(ro05b, 0) - Nvl(ro05br, 0)
	pn26T = Nvl(ro05t, 0) - Nvl(ro05tr, 0)

	*** Achizitii de bunuri si servicii supuse masurilor de simplificare pentru care beneficiarul este obligat la plata TVA (taxare inversa),din care (rd.27=rd.12)
	pn27B = Nvl(ti21b, 0) + Nvl(ti11b, 0) + Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) && ACH. TI
	pn27T = Nvl(ti21t, 0) + Nvl(ti11t, 0) + Nvl(ti24t, 0) + Nvl(ti20t, 0) + Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn12B = pn27B
	pn12T = pn27T

	pn271B = Nvl(ti21b, 0) && ACH. TI 21%
	pn271T = Nvl(ti21t, 0)
	pn121B = pn271B
	pn121T = pn271T

	pn272B = Nvl(ti11b, 0) && && ACH. TI 11%
	pn272T = Nvl(ti11t, 0)
	pn122B = pn272B
	pn122T = pn272T

	pn273B = Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) && ACH. TI 19%
	pn273T = Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0)
	pn123B = pn273B
	pn123T = pn273T

	pn274B = Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) && && ACH. TI 9%
	pn274T = Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn124B = pn274B
	pn124T = pn274T

	pn275B = 0 && ACH. TI 5% - NU AM EXPLICATIE TVA PENTRU TI 5%. AR TREBUI ADAUGATA
	pn275T = 0
	pn125B = pn275B
	pn125T = pn275T

	*** Achizitii de bunuri si servicii scutite de taxa sau neimpozabile, din care
	pn30B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0) && Achizi_ii de bunuri ši servicii scutite de taxa sau neimpozabile
	pn301B = Nvl(cebvsc, 0) + Nvl(cebfsc, 0) && Achizitii de servicii intracomunitare scutite de taxa

	*** Regularizari taxa dedusa
	pn34B = Nvl(ro20b, 0) + Nvl(ro24b, 0) + Nvl(fo20b, 0) + Nvl(fo24b, 0) + Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ro21br, 0) + Nvl(ro11br, 0)+ Nvl(ro19br, 0) + Nvl(ro09br, 0) + Nvl(ro05br, 0)
	pn34T = Nvl(ro20t, 0) + Nvl(ro24t, 0) + Nvl(fo20t, 0) + Nvl(fo24t, 0) + Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ro21tr, 0) + Nvl(ro11tr, 0)+ Nvl(ro19tr, 0) + Nvl(ro09tr, 0) + Nvl(ro05tr, 0)

	lnROTN = Int(Round(Nvl(ROTN, 0), 0)) &&  && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001

	Select cDecont
	Locate For Alltrim(Rand) = '20'
	If Found()
		Replace valoare With pn20B, TVA With pn20T
	Endif

	Locate For Alltrim(Rand) = '20.1'
	If Found()
		Replace valoare With pn201B, TVA With pn201T
	Endif

	Locate For Alltrim(Rand) = '5'
	If Found()
		Replace valoare With pn5B, TVA With pn5T
	Endif

	Locate For Alltrim(Rand) = '5.1'
	If Found()
		Replace valoare With pn51B, TVA With pn51T
	Endif

	Locate For Alltrim(Rand) = '21'
	If Found()
		Replace valoare With pn21B, TVA With pn21T
	Endif

	Locate For Alltrim(Rand) = '6'
	If Found()
		Replace valoare With pn6B, TVA With pn6T
	Endif

	Locate For Alltrim(Rand) = '22'
	If Found()
		Replace valoare With pn22B, TVA With pn22T
	Endif

	Locate For Alltrim(Rand) = '22.1'
	If Found()
		Replace valoare With pn221B, TVA With pn221T
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '7.1'
	If Found()
		Replace valoare With pn71B, TVA With pn71T
	Endif

	Locate For Alltrim(Rand) = '23'
	If Found()
		Replace valoare With pn23B, TVA With pn23T
	Endif

	Locate For Alltrim(Rand) = '8'
	If Found()
		Replace valoare With pn8B, TVA With pn8T
	Endif

	Locate For Alltrim(Rand) = '24'
	If Found()
		Replace valoare With pn24B, TVA With pn24T
	Endif

	Locate For Alltrim(Rand) = '24.1'
	If Found()
		Replace valoare With pn241B, TVA With pn241T
	Endif

	Locate For Alltrim(Rand) = '25'
	If Found()
		Replace valoare With pn25B, TVA With pn25T
	Endif

	Locate For Alltrim(Rand) = '25.1'
	If Found()
		Replace valoare With pn251B, TVA With pn251T
	Endif

	Locate For Alltrim(Rand) = '26'
	If Found()
		Replace valoare With pn26B, TVA With pn26T
	Endif

	Locate For Alltrim(Rand) = '27'
	If Found()
		Replace valoare With pn27B, TVA With pn27T
	Endif

	Locate For Alltrim(Rand) = '27.1'
	If Found()
		Replace valoare With pn271B, TVA With pn271T
	Endif

	Locate For Alltrim(Rand) = '27.2'
	If Found()
		Replace valoare With pn272B, TVA With pn272T
	Endif

	Locate For Alltrim(Rand) = '27.3'
	If Found()
		Replace valoare With pn273B, TVA With pn273T
	Endif

	Locate For Alltrim(Rand) = '27.4'
	If Found()
		Replace valoare With pn274B, TVA With pn274T
	ENDIF
	
	Locate For Alltrim(Rand) = '27.5'
	If Found()
		Replace valoare With pn275B, TVA With pn275T
	ENDIF
		
	Locate For Alltrim(Rand) = '12'
	If Found()
		Replace valoare With pn12B, TVA With pn12T
	Endif

	Locate For Alltrim(Rand) = '12.1'
	If Found()
		Replace valoare With pn121B, TVA With pn121T
	Endif

	Locate For Alltrim(Rand) = '12.2'
	If Found()
		Replace valoare With pn122B, TVA With pn122T
	Endif

	Locate For Alltrim(Rand) = '12.3'
	If Found()
		Replace valoare With pn123B, TVA With pn123T
	Endif

	Locate For Alltrim(Rand) = '12.4'
	If Found()
		Replace valoare With pn124B, TVA With pn124T
	ENDIF
	
	Locate For Alltrim(Rand) = '12.5'
	If Found()
		Replace valoare With pn125B, TVA With pn125T
	ENDIF
		
	Locate For Alltrim(Rand) = '30'
	If Found()
		Replace valoare With pn30B
	Endif

	Locate For Alltrim(Rand) = '30.1'
	If Found()
		Replace valoare With pn301B
	Endif

	*!* REGULARIZARE
	Locate For Alltrim(Rand) = '34'
	If Found()
		Replace valoare With pn34B, TVA With pn34T
	Endif

	*** Rd. 42 Soldul sumei negative a TVA reportate din perioada precedenta pentru care nu s-a solicitat rambursare (rd.44 din decontul perioadei fiscale precedente)
	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala

	lnTR = 0
	llSucces = goExecutor.oSelecteaza2Value(m.lcSql, @lnTR)
	IF m.llSucces
		lnTR = Int(Round(lnTR, 0))
	ENDIF 	

	Select cDecont
	Locate For Alltrim(Rand) = '42'
	If Found()
		Replace TVA With lnTR
	Endif


	*** Rd. 37 Soldul TVA de plata din decontul perioadei fiscale precedente (rd.43 din decontul perioadei fiscale precedente) neachitate pana la data depunerii decontului de TVA
	Text To lcSql Textmerge Noshow
	SELECT NVL(soldcred,0) as  soldcred
		from vbal
		where cont = '4423' and AN = ?pnAnPrec AND LUNA = ?pnLunaPrec
		<<m.gcCondSucursala>>
	Endtext

	lnTP = 0
	llSucces = goExecutor.oSelecteaza2Value(m.lcSql, @lnTP)
	IF m.llSucces
		lnTP = Int(Round(lnTP, 0))
	ENDIF 	

	* cat TVA am achitat in perioada curenta pentru TVA de plata 4423
	Text To lcSql Textmerge Noshow
	select  nvl(sum(suma),0) as suma
		from act
		where an = ?gnAn and luna between ?pnLunaI and ?pnLunaF
		and sters = 0
		and scd = '4423' and scc In ('5121', '5311')
		<<m.gcCondSucursala>>
	Endtext

	lnPlati = 0
	llSucces = goExecutor.oSelecteaza2Value(m.lcSql, @lnPlati)
	IF m.llSucces
		lnPlati = Int(Round(lnPlati, 0))
	ENDIF 	
	
	lnTP = Max(m.lnTP - m.lnPlati, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '39'
	If Found()
		Replace TVA With Nvl(m.lnTP, 0)
	Endif

	*** Regularizari
	lcSql = [select sum(suma) as suma, id_jtva_coloana from act ] + ;
		[where sters = 0 and id_jtva_coloana in (1002, 1003) and ] + ;
		[an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + m.gcCondSucursala + ;
		[ group by id_jtva_coloana order by id_jtva_coloana]
	lcCursor = [crsRegTaxe]
	If goExecutor.oExecuta(lcSql, lcCursor)
		Select (lcCursor)
		Scan
			lnSuma = Int(Round(suma, 0))
			lcRand = Iif(id_jtva_coloana = 1003, '34', '16')
			Select cDecont
			Replace TVA With TVA - lnSuma For Rand = lcRand
			Select (lcCursor)
		Endscan
	Endif
	Use In (Select(lcCursor))

	*** SOLD NEEXIGIBIL
	Text To lcSql Noshow Textmerge
	select sum(RO21NB+RO11NB+RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO21NT+RO11NT+RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBazaN = Int(Round(bazan, 0))
		pnAchizitieTVAN = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO21NB+RO11NB+RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO21NT+RO11NT+RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBaza6N = Int(Round(bazan, 0))
		pnAchizitieTVA6N = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))


	Text To lcSql Noshow Textmerge
	select sum(RO21NB+RO11NB+RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO21NT+RO11NT+RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBazaN = Int(Round(bazan, 0))
		pnLivrareTVAN = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO21NB+RO11NB+RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO21NT+RO11NT+RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBaza6N = Int(Round(bazan, 0))
		pnLivrareTVA6N = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))

	Select cDecont
	lodt = Createobject('frm_decontTVA_201701', m.tnNumarLuni)

	lodt.nROTN = Nvl(m.lnROTN, 0) && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001
	lodt.lblRandAtentie.Caption = lodt.lblRandAtentie.Caption + ' (' + Alltrim(Str(m.lnROTN)) + ' lei)'
	lodt.Show(1)
Endproc && viz_decontTVA_2508

*===========================================
Procedure viz_decontTVA_0719
	Lparameters tnNumarLuni
	*Parameters tlTrim

	*** tnNumarLuni: 1 = o luna, 3 = 3 luni trimestrial, 6 = 6 luni semestrial, 12 = 12 luni anual

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec

	Local lodt As 'frm_decontTVA'
	Local lcCursor, lcSql, ldDataCurenta, ldDataPrecedenta, lnAnCurent, lnAnPrecedent, lnLunaCurenta
	Local lnLunaMin, lnLunaPrecedenta, lnPerioada, lnPlati, lnSucces, lnTP, lnTR, loEx As Exception, lnROTN
	*:Global pn10B, pn10T, pn11B, pn11T, pn13B, pn14B, pn15B, pn171B, pn171T, pn17B, pn17T, pn1911B
	*:Global pn1911T, pn191B, pn191T, pn19B, pn19T, pn1B, pn21B, pn21T, pn22B, pn22T, pn23B, pn23T
	*:Global pn241B, pn24B, pn31B, pn3B, pn51B, pn51T, pn5B, pn5T, pn711B, pn711T, pn71B, pn71T, pn7B
	*:Global pn7T, pn9B, pn9T, pnRegularizareB, pnRegularizareT

	lnAnCurent = m.gnAn
	lnLunaCurenta = m.gnLuna
	ldDataCurenta = Date(m.lnAnCurent, m.lnLunaCurenta, 1)
	lnROTN = 0

	* Luna de inceput a perioadei
	* 1 = gnLuna
	* 3 = 1,4,7,10
	* 6 = 1,7
	* 12 = 1
	lnPerioada = Ceil(m.lnLunaCurenta / tnNumarLuni)
	lnLunaMin = (lnPerioada - 1) * tnNumarLuni + 1

	*** luna pentru balanta precedenta
	ldDataPrecedenta = Gomonth(Date(m.lnAnCurent, m.lnLunaMin, 1), -1)
	lnLunaPrecedenta = Month(m.ldDataPrecedenta)
	lnAnPrecedent = Year(m.ldDataPrecedenta)
	&& ---
	pnLunaI = lnLunaMin
	pnLunaF = m.lnLunaCurenta

	pnLunaPrec = m.lnLunaPrecedenta
	pnAnPrec = m.lnAnPrecedent

	Private poDecont
	Store '' To poDecont

	Private pnProRata, pnAchizitieBazaN, pnAchizitieTVAN, pnLivrareBazaN, pnLivrareTVAN, pnAchizitieBaza6N, pnAchizitieTVA6N, pnLivrareBaza6N, pnLivrareTVA6N
	pnProRata = 100
	pnAchizitieBazaN = 0
	pnAchizitieTVAN = 0
	pnLivrareBazaN = 0
	pnLivrareTVAN = 0
	pnAchizitieBaza6N = 0
	pnAchizitieTVA6N = 0
	pnLivrareBaza6N = 0
	pnLivrareTVA6N = 0

	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	&& schimbare cota TVA standard de la 24% la 20%
	Do Case
		CASE m.gnAn*12+m.gnLuna >= 2024*12+5
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '0524' order by id_decont]						
		CASE m.gnAn*12+m.gnLuna >= 2023*12+8
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '0823' order by id_decont]						
		Case m.gnAn * 12 + m.gnLuna >= 2019*12+7
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '0719' order by id_decont]
		Case m.gnAn * 12 + m.gnLuna >= 2017*12+1
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '0117' order by id_decont]
		Case m.gnAn * 12 + m.gnLuna >= 2013*12+1
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '0113' order by id_decont]
		OTHERWISE && Case m.gnAn * 12 + m.gnLuna >= 2016*12+1
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '0116' order by id_decont]
	Endcase
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Text To lcSql Noshow Textmerge
	SELECT ROUND(SUM(RO19B),0) AS RO19B, 
		 ROUND(SUM(RO19T),0) AS RO19T, 
		 ROUND(SUM(RO9B),0) AS RO9B, 
		 ROUND(SUM(RO9T),0) AS RO9T,
		 ROUND(SUM(RO5B),0) AS RO5B, 
		 ROUND(SUM(RO5T),0) AS RO5T, 
		 ROUND(SUM(RO20B),0) AS RO20B, 
		 ROUND(SUM(RO20T),0) AS RO20T,
		 ROUND(SUM(RO24B),0) AS RO24B, 
		 ROUND(SUM(RO24T),0) AS RO24T,
		 ROUND(SUM(ROTI),0) AS ROTI, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19B ELSE 0 END),0) AS RO19Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19T ELSE 0 END),0) AS RO19Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO9B ELSE 0 END),0) AS RO9Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO9T ELSE 0 END),0) AS RO9Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO5B ELSE 0 END),0) AS RO5Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO5T ELSE 0 END),0) AS RO5Tr, 
		 ROUND(SUM(CESCDD1),0) AS CESCDD1,
		 ROUND(SUM(CESCDD2),0) AS CESCDD2,
		 ROUND(SUM(CEOPTR),0) AS CEOPTR,
		 ROUND(SUM(CESVDD),0) AS CESVDD,
		 ROUND(SUM(CESVFDD),0) AS CESVFDD,
		 ROUND(SUM(CESVFS),0) AS CESVFS,
		 ROUND(SUM(WRSCDD),0) AS WRSCDD, 
		 ROUND(SUM(WRSCDDAB),0) AS WRSCDDAB, 
		 ROUND(SUM(WRSCDDCD),0) AS WRSCDDCD, 		 		 
		 ROUND(SUM(FODD),0) AS FODD, 
		 ROUND(SUM(FOFDD),0) AS FOFDD, 
		 ROUND(SUM(CEOPTR),0) AS CEOPTR, 
		 ROUND(SUM(CESVDD),0) AS CESVDD, 
		 ROUND(SUM(CESVFDD),0) AS CESVFDD, 
		 ROUND(SUM(CESVFS),0) AS CESVFS, 
		 ROUND(SUM(WRSCFDD),0) AS WRSCFDD,
		 ROUND(SUM(WRN),0) AS WRN,
		 ROUND(SUM(rortc24),0) AS rortc24,
         ROUND(SUM(rortc20),0) AS rortc20,
         ROUND(SUM(rortc19),0) AS rortc19,
         ROUND(SUM(rortc9),0) AS rortc9,
         ROUND(SUM(rortc5),0) AS rortc5
		  FROM JV2007 
		 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF <<m.gcCondSucursala>>
	Endtext
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala


	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	Go Top
	pn1B = Nvl(cescdd1, 0) + Nvl(ceoptr, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0) + Nvl(cesvdd, 0) + Nvl(cesvfdd, 0) + Nvl(cesvfs, 0)
	pn31B = Nvl(cesvfs, 0)

	&& scad tva exigibil cu data act in afara perioadei de declarare. il pun pe regularizare rd. 16 
	pn9B = Nvl(ro19b, 0) - Nvl(ro19br, 0)
	pn9T = Nvl(ro19t, 0) - Nvl(ro19tr, 0)
	pn10B = Nvl(ro9b, 0) - Nvl(ro9br, 0)
	pn10T = Nvl(ro9t, 0) - Nvl(ro9tr, 0)
	pn11B = Nvl(ro5b, 0) - Nvl(ro5br, 0)
	pn11T = Nvl(ro5t, 0) - Nvl(ro5tr, 0)
	pnRegularizareB = Nvl(ro20b, 0) + Nvl(ro24b, 0) + Nvl(ro19br, 0) + Nvl(ro9br, 0) + Nvl(ro5br, 0)
	pnRegularizareT = Nvl(ro20t, 0) + Nvl(ro24t, 0) + Nvl(ro19tr, 0) + Nvl(ro9tr, 0) + Nvl(ro5tr, 0)

	pn13B = Nvl(roti, 0)
	pn14B = Nvl(wrscdd, 0) + Nvl(wrscddab, 0) + Nvl(wrscddcd, 0)
	pn141B = Nvl(wrscddab, 0)
	pn142B = Nvl(wrscddcd, 0)		

	pn15B = Nvl(wrscfdd, 0)

	*!*		*** LIVRARI D394
	*!*		pn171B = Nvl(loD394.livb + loD394.livtib,0)
	*!*		pn171T = Nvl(loD394.livt + loD394.livtit,0)

	Select cDecont
	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '3.1'
	If Found()
		Replace valoare With pn31B
	Endif

	Locate For Alltrim(Rand) = '9'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '10'
	If Found()
		Replace valoare With pn10B, TVA With pn10T
	Endif

	Locate For Alltrim(Rand) = '11'
	If Found()
		Replace valoare With pn11B, TVA With pn11T
	Endif

	Locate For Alltrim(Rand) = '13'
	If Found()
		Replace valoare With pn13B
	Endif

	Locate For Alltrim(Rand) = '14'
	If Found()
		Replace valoare With pn14B
	Endif

	Locate For Alltrim(Rand) = '14.1'
	If Found()
		Replace valoare With pn141B
	Endif

	Locate For Alltrim(Rand) = '14.2'
	If Found()
		Replace valoare With pn142B
	Endif

	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pn15B
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '16'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	*!*		*!* LIVRARI INTERNE CATRE CLIENTI PLATITORI TVA IN ROMANIA (LIVRARI D394)
	*!*		Locate For Alltrim(Rand) = '17.1'
	*!*		If Found()
	*!*			Replace valoare With pn171B, TVA With pn171T
	*!*		Endif

	Text To lcSql Noshow Textmerge
	SELECT Round(Sum(RO24B),0) As RO24B, 
		 Round(Sum(RO24T),0) As RO24T, 
		 ROUND(SUM(RO20B),0) AS RO20B, 
		 ROUND(SUM(RO20T),0) AS RO20T, 
		 ROUND(SUM(RO19BCB),0) AS RO19BCB, 
		 ROUND(SUM(RO19BCT),0) AS RO19BCT, 
		 ROUND(SUM(RO19BVB),0) AS RO19BVB, 
		 ROUND(SUM(RO19BVT),0) AS RO19BVT, 
		 ROUND(SUM(RO19BFB),0) AS RO19BFB, 
		 ROUND(SUM(RO19BFT),0) AS RO19BFT, 
		 ROUND(SUM(RO09BCB),0) AS RO09BCB, 
		 ROUND(SUM(RO09BCT),0) AS RO09BCT, 
		 ROUND(SUM(RO09BVB),0) AS RO09BVB, 
		 ROUND(SUM(RO09BVT),0) AS RO09BVT, 
		 ROUND(SUM(RO09BFB),0) AS RO09BFB, 
		 ROUND(SUM(RO09BFT),0) AS RO09BFT, 
		 ROUND(SUM(RO05B),0) AS RO05B, 
		 ROUND(SUM(RO05T),0) AS RO05T, 
		 ROUND(SUM(ROSCN),0) AS ROSCN, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19BCB + RO19BVB + RO19BFB ELSE 0 END),0) AS RO19Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19BCT + RO19BVT + RO19BFT ELSE 0 END),0) AS RO19Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO09BCB + RO09BVB + RO09BFB ELSE 0 END),0) AS RO09Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO09BCT + RO09BVT + RO09BFT ELSE 0 END),0) AS RO09Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO05B ELSE 0 END),0) AS RO05Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO05T ELSE 0 END),0) AS RO05Tr, 		
		 Round(Sum(FO24B),0) As FO24B, 
		 Round(Sum(FO24T),0) As FO24T, 
		 Round(Sum(FO20B),0) As FO20B, 
		 Round(Sum(FO20T),0) As FO20T, 
		 ROUND(SUM(FO19BCB),0) AS FO19BCB, 
		 ROUND(SUM(FO19BCT),0) AS FO19BCT, 
		 ROUND(SUM(FO19BVB),0) AS FO19BVB, 
		 ROUND(SUM(FO19BVT),0) AS FO19BVT, 
		 ROUND(SUM(FO19BFB),0) AS FO19BFB, 
		 ROUND(SUM(FO19BFT),0) AS FO19BFT, 
		 ROUND(SUM(XX19TIB),0) AS XX19TIB, 
		 ROUND(SUM(XX19TIT),0) AS XX19TIT, 		 
		 ROUND(SUM(XX9TIB),0) AS XX9TIB, 
		 ROUND(SUM(XX9TIT),0) AS XX9TIT, 		 
		 ROUND(SUM(FO09BCB),0) AS FO09BCB, 
		 ROUND(SUM(FO09BCT),0) AS FO09BCT, 
		 ROUND(SUM(FO09BVB),0) AS FO09BVB, 
		 ROUND(SUM(FO09BVT),0) AS FO09BVT, 
		 ROUND(SUM(FO09BFB),0) AS FO09BFB, 
		 ROUND(SUM(FO09BFT),0) AS FO09BFT, 
		 ROUND(SUM(FOSCN),0) AS FOSCN, 
		 Round(Sum(CE24B),0) As CE24B, 
		 Round(Sum(CE24T),0) As CE24T, 
		 Round(Sum(CE20B),0) As CE20B, 
		 Round(Sum(CE20T),0) As CE20T, 
		 Round(Sum(CE24SB),0) As CE24SB, 
		 Round(Sum(CE24ST),0) As CE24ST, 
		 Round(Sum(CE20SB),0) As CE20SB, 
		 Round(Sum(CE20ST),0) As CE20ST, 
		 Round(Sum(CE5SB),0) As CE5SB, 
		 Round(Sum(CE5ST),0) As CE5ST, 
		 Round(Sum(CE9SB),0) As CE9SB, 
		 Round(Sum(CE9ST),0) As CE9ST, 
		 Round(Sum(CE19SB),0) As CE19SB, 		
		 Round(Sum(CE19ST),0) As CE19ST, 
		 ROUND(SUM(CEBCB),0) AS CEBCB, 
		 ROUND(SUM(CEBCT),0) AS CEBCT, 
		 ROUND(SUM(CEBVB),0) AS CEBVB, 
		 ROUND(SUM(CEBVT),0) AS CEBVT, 
		 ROUND(SUM(CEBVSC),0) AS CEBVSC, 
		 ROUND(SUM(CEBVN),0) AS CEBVN, 
		 ROUND(SUM(CEBFB),0) AS CEBFB, 
		 ROUND(SUM(CEBFT),0) AS CEBFT, 
		 ROUND(SUM(CEBFSC),0) AS CEBFSC, 
		 ROUND(SUM(CEBFN),0) AS CEBFN, 
         ROUND(SUM(ce19ctb),0) AS ce19ctb,
         ROUND(SUM(ce19ctt),0) AS ce19ctt,
         ROUND(SUM(ce9ctb),0) AS ce9ctb,
         ROUND(SUM(ce9ctt),0) AS ce9ctt,
         ROUND(SUM(ce5ctb),0) AS ce5ctb,
         ROUND(SUM(ce5ctt),0) AS ce5ctt,
         ROUND(SUM(ce19ftb),0) AS ce19ftb,
         ROUND(SUM(ce19ftt),0) AS ce19ftt,
         ROUND(SUM(ce9ftb),0) AS ce9ftb,
         ROUND(SUM(ce9ftt),0) AS ce9ftt,
         ROUND(SUM(ce5ftb),0) AS ce5ftb,
         ROUND(SUM(ce5ftt),0) AS ce5ftt,
		 Round(Sum(TI24B), 0) As TI24B, 
		 Round(Sum(TI24T), 0) As TI24T, 
		 Round(Sum(TI20B), 0) As TI20B, 
		 Round(Sum(TI20T), 0) As TI20T, 
		 ROUND(SUM(TI19BCB),0) AS TI19BCBWR, 
		 ROUND(SUM(TI19BCT),0) AS TI19BCTWR, 
		 ROUND(SUM(TI19BVB),0) AS TI19BVBWR, 
		 ROUND(SUM(TI19BVT),0) AS TI19BVTWR, 
		 ROUND(SUM(TI19BFB),0) AS TI19BFBWR, 
		 ROUND(SUM(TI19BFT),0) AS TI19BFTWR, 
		 ROUND(SUM(TI09BVB),0) AS TI09BVBWR, 
		 ROUND(SUM(TI09BVT),0) AS TI09BVTWR, 
		 ROUND(SUM(TI09BFB),0) AS TI09BFBWR, 
		 ROUND(SUM(TI09BFT),0) AS TI09BFTWR, 
		 ROUND(SUM(TI19BCB),0) AS TI19BCBRO, 
		 ROUND(SUM(TI19BCT),0) AS TI19BCTRO, 
		 ROUND(SUM(TI19BVB),0) AS TI19BVBRO, 
		 ROUND(SUM(TI19BVT),0) AS TI19BVTRO, 
		 ROUND(SUM(TI19BFB),0) AS TI19BFBRO, 
		 ROUND(SUM(TI19BFT),0) AS TI19BFTRO, 
		 ROUND(SUM(TI09BVB),0) AS TI09BVBRO, 
		 ROUND(SUM(TI09BVT),0) AS TI09BVTRO, 
		 ROUND(SUM(TI09BFB),0) AS TI09BFBRO, 
		 ROUND(SUM(TI09BFT),0) AS TI09BFTRO,
		 ROUND(SUM(ROTN+ROTN50+ROTN100),0) AS ROTN,
         ROUND(SUM(rortd24),0) AS rortd24,
         ROUND(SUM(rortd20),0) AS rortd20,
         ROUND(SUM(rortd19),0) AS rortd19,
         ROUND(SUM(rortd9),0) AS rortd9,
         ROUND(SUM(rortd5),0) AS rortd5 		 		  
		 FROM JC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF <<m.gcCondSucursala>>
	Endtext
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC
	Go Top
	pn20B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0) + NVL(CE19CTB,0) + NVL(CE9CTB,0) + NVL(CE5CTB,0) + NVL(CE19FTB,0) + NVL(CE9FTB,0) + NVL(CE5FTB,0) && ach. intracomunitare pre SAFT + ach. intracomunitare furnizori inregistrati/neinregistrati TVA
	pn20T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0) + NVL(CE19CTT,0) + NVL(CE9CTT,0) + NVL(CE5CTT,0) + NVL(CE19FTT,0) + NVL(CE9FTT,0) + NVL(CE5FTT,0)
	pn201B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0) + NVL(CE19CTB,0) + NVL(CE9CTB,0) + NVL(CE5CTB,0) && ach. intracomunitare pre SAFT + ach. intracomunitare furnizori inregistrati TVA
	pn201T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0) + NVL(CE19CTT,0) + NVL(CE9CTT,0) + NVL(CE5CTT,0)
	pn5B = pn20B
	pn5T = pn20T
	pn51B = pn201B
	pn51T = pn201T

	*** rd.21 = rd.6
	*** Regularizari privind achizi_iile intracomunitare de bunuri pentru care cumparatorul este obligat la plata TVA (taxare inversa) (rd.19=rd.6)
	pn21B = Nvl(ce20b, 0) + Nvl(ce24b, 0)
	pn21T = Nvl(ce20t, 0) + Nvl(ce24t, 0)
	pn6B = pn21B
	pn6T = pn21T

	*** Achizi_ii de bunuri, altele decat cele de la rd. 18 ši 19, si achizitii de servicii pentru care beneficiarul din Romania este obligat la plata TVA (taxare
	*** inversa) (rd.22=rd.7), din care
	pn22B = Nvl(CE19SB, 0) + Nvl(XX19TIB, 0) + Nvl(XX9TIB, 0)
	pn22T = Nvl(CE19ST, 0) + Nvl(XX19TIT, 0) + Nvl(XX9TIT, 0)
	pn7B = pn22B
	pn7T = pn22T

	*** Achizitii de servicii intracomunitare pentru care beneficiarul este obligat la plata TVA (taxare inversa) (rd.22.1=rd.7.1)
	pn221B = Nvl(CE19SB, 0)
	pn221T = Nvl(CE19ST, 0)
	pn71B = pn221B
	pn71T = pn221T

	*** Regularizari privind achizitii de servicii intracomunitare pentru care beneficiarul din Romania este obligat la plata TVA (taxare inversa)(rd.23=rd.8)
	pn23B = Nvl(CE24SB, 0) + Nvl(CE20SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn23T = Nvl(CE24ST, 0) + Nvl(CE20ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn8B = pn23B
	pn8T = pn23T

	*** Achizi_ii de bunuri ši servicii taxabile cu cota de 19%, altele decat cele de la rd.27	
	&& scad tva exigibil din operatiile cu data act in afara perioadei de declarare. le pun pe regularizari, rd. 34
	pn24B = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0) - Nvl(ro19br, 0)
	pn24T = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0) - Nvl(ro19tr, 0)

	*** Achizi_ii de bunuri ši servicii taxabile cu cota de 9%
	pn25B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0) - Nvl(ro09br, 0)
	pn25T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0) - Nvl(ro09tr, 0)

	*** Achizi_ii de bunuri taxabile cu cota de 5%
	pn26B = Nvl(ro05b, 0) - Nvl(ro05br, 0)
	pn26T = Nvl(ro05t, 0) - Nvl(ro05tr, 0)

	*** Achizi_ii de bunuri ši servicii supuse masurilor de simplificare pentru care beneficiarul este obligat la plata TVA (taxare inversa),din care (rd.27=rd.12)
	pn27B = Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) && ACH. TI
	pn27T = Nvl(ti24t, 0) + Nvl(ti20t, 0) + Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn12B = pn27B
	pn12T = pn27T

	pn271B = Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) && ACH. TI 19%
	pn271T = Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0)
	pn121B = pn271B
	pn121T = pn271T

	pn272B = Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) && && ACH. TI 9%
	pn272T = Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn122B = pn272B
	pn122T = pn272T

	pn273B = 0 && ACH. TI 5% - NU AM EXPLICATIE TVA PENTRU TI 5%. AR TREBUI ADAUGATA
	pn273T = 0
	pn123B = pn273B
	pn123T = pn273T

	*** Achizi_ii de bunuri ši servicii scutite de taxa sau neimpozabile, din care
	pn30B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0) && Achizi_ii de bunuri ši servicii scutite de taxa sau neimpozabile
	pn301B = Nvl(cebvsc, 0) + Nvl(cebfsc, 0) && Achizitii de servicii intracomunitare scutite de taxa

	*** Regularizari taxa dedusa
	pn34B = Nvl(ro20b, 0) + Nvl(ro24b, 0) + Nvl(fo20b, 0) + Nvl(fo24b, 0) + Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ro19br, 0) + Nvl(ro09br, 0) + Nvl(ro05br, 0)
	pn34T = Nvl(ro20t, 0) + Nvl(ro24t, 0) + Nvl(fo20t, 0) + Nvl(fo24t, 0) + Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ro19tr, 0) + Nvl(ro09tr, 0) + Nvl(ro05tr, 0)

	lnROTN = Int(Round(Nvl(ROTN, 0), 0)) &&  && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001

	Select cDecont
	Locate For Alltrim(Rand) = '20'
	If Found()
		Replace valoare With pn20B, TVA With pn20T
	Endif

	Locate For Alltrim(Rand) = '20.1'
	If Found()
		Replace valoare With pn201B, TVA With pn201T
	Endif

	Locate For Alltrim(Rand) = '5'
	If Found()
		Replace valoare With pn5B, TVA With pn5T
	Endif

	Locate For Alltrim(Rand) = '5.1'
	If Found()
		Replace valoare With pn51B, TVA With pn51T
	Endif

	Locate For Alltrim(Rand) = '21'
	If Found()
		Replace valoare With pn21B, TVA With pn21T
	Endif

	Locate For Alltrim(Rand) = '6'
	If Found()
		Replace valoare With pn6B, TVA With pn6T
	Endif

	Locate For Alltrim(Rand) = '22'
	If Found()
		Replace valoare With pn22B, TVA With pn22T
	Endif

	Locate For Alltrim(Rand) = '22.1'
	If Found()
		Replace valoare With pn221B, TVA With pn221T
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '7.1'
	If Found()
		Replace valoare With pn71B, TVA With pn71T
	Endif

	Locate For Alltrim(Rand) = '23'
	If Found()
		Replace valoare With pn23B, TVA With pn23T
	Endif

	Locate For Alltrim(Rand) = '8'
	If Found()
		Replace valoare With pn8B, TVA With pn8T
	Endif

	Locate For Alltrim(Rand) = '24'
	If Found()
		Replace valoare With pn24B, TVA With pn24T
	Endif

	Locate For Alltrim(Rand) = '25'
	If Found()
		Replace valoare With pn25B, TVA With pn25T
	Endif

	Locate For Alltrim(Rand) = '26'
	If Found()
		Replace valoare With pn26B, TVA With pn26T
	Endif

	Locate For Alltrim(Rand) = '27'
	If Found()
		Replace valoare With pn27B, TVA With pn27T
	Endif

	Locate For Alltrim(Rand) = '27.1'
	If Found()
		Replace valoare With pn271B, TVA With pn271T
	Endif

	Locate For Alltrim(Rand) = '27.2'
	If Found()
		Replace valoare With pn272B, TVA With pn272T
	Endif

	Locate For Alltrim(Rand) = '27.3'
	If Found()
		Replace valoare With pn273B, TVA With pn273T
	Endif

	Locate For Alltrim(Rand) = '12'
	If Found()
		Replace valoare With pn12B, TVA With pn12T
	Endif

	Locate For Alltrim(Rand) = '12.1'
	If Found()
		Replace valoare With pn121B, TVA With pn121T
	Endif

	Locate For Alltrim(Rand) = '12.2'
	If Found()
		Replace valoare With pn122B, TVA With pn122T
	Endif

	Locate For Alltrim(Rand) = '12.3'
	If Found()
		Replace valoare With pn123B, TVA With pn123T
	Endif

	Locate For Alltrim(Rand) = '30'
	If Found()
		Replace valoare With pn30B
	Endif

	Locate For Alltrim(Rand) = '30.1'
	If Found()
		Replace valoare With pn301B
	Endif

	*!* REGULARIZARE
	Locate For Alltrim(Rand) = '34'
	If Found()
		Replace valoare With pn34B, TVA With pn34T
	Endif

	*** Rd. 42 Soldul sumei negative a TVA reportate din perioada precedenta pentru care nu s-a solicitat rambursare (rd.44 din decontul perioadei fiscale precedente)
	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala


	lcCursor = [crsTR] && TVA de recuperat
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTR
	lnTR = Int(Round(solddeb, 0))

	Select cDecont
	Locate For Alltrim(Rand) = '42'
	If Found()
		Replace TVA With lnTR
	Endif

	If Used('crsTR')
		Use In crsTR
	Endif

	*** Rd. 37 Soldul TVA de plata din decontul perioadei fiscale precedente (rd.43 din decontul perioadei fiscale precedente) neachitate pana la data depunerii decontului de TVA
	Text To lcSql Textmerge Noshow
	SELECT NVL(soldcred,0) as  soldcred
		from vbal
		where cont = '4423' and AN = ?pnAnPrec AND LUNA = ?pnLunaPrec
		<<m.gcCondSucursala>>
	Endtext

	lcCursor = [crsTP] && nu si-au platit tva din luna precedenta
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTP
	lnTP = Int(Round(soldcred, 0))

	Use In (Select('crsTP'))

	Text To lcSql Textmerge Noshow
	select  nvl(sum(suma),0) as suma
		from act
		where an = ?gnAn and luna between ?pnLunaI and ?pnLunaF
		and sters = 0
		and scd = '4423' and scc In ('5121', '5311')
		<<m.gcCondSucursala>>
	Endtext

	lcCursor = [crsPlati]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsPlati
	lnPlati = Int(Round(suma, 0))

	Use In (Select('crsPlati'))

	lnTP = Max(m.lnTP - m.lnPlati, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '39'
	If Found()
		Replace TVA With Nvl(m.lnTP, 0)
	Endif

	*** Regularizari
	lcSql = [select sum(suma) as suma, id_jtva_coloana from act ] + ;
		[where sters = 0 and id_jtva_coloana in (1002, 1003) and ] + ;
		[an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + m.gcCondSucursala + ;
		[ group by id_jtva_coloana order by id_jtva_coloana]
	lcCursor = [crsRegTaxe]
	If goExecutor.oExecuta(lcSql, lcCursor)
		Select (lcCursor)
		Scan
			lnSuma = Int(Round(suma, 0))
			lcRand = Iif(id_jtva_coloana = 1003, '34', '16')
			Select cDecont
			Replace TVA With TVA - lnSuma For Rand = lcRand
			Select (lcCursor)
		Endscan
	Endif
	Use In (Select(lcCursor))

	*** SOLD NEEXIGIBIL
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBazaN = Int(Round(bazan, 0))
		pnAchizitieTVAN = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBaza6N = Int(Round(bazan, 0))
		pnAchizitieTVA6N = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))


	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBazaN = Int(Round(bazan, 0))
		pnLivrareTVAN = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBaza6N = Int(Round(bazan, 0))
		pnLivrareTVA6N = Int(Round(tvan, 0))
	Endif
	Use In (Select('cNeexTemp'))

	Select cDecont
	Do Case
		CASE m.gnAn >= 2017
			lodt = Createobject('frm_decontTVA_201701', m.tnNumarLuni)	&& la fel ca 2016, lblRandAtentie
		Case m.gnAn = 2016
			lodt = Createobject('frm_decontTVA_201601', m.tnNumarLuni)	&& s-au mai adaugat 2 campuri la sfarsit - sold livrari/ach. neex pt livrari in ultimele 6 luni
		OTHERWISE 
			lodt = Createobject('frm_decontTVA', m.tnNumarLuni)
	Endcase
	lodt.nROTN = Nvl(m.lnROTN, 0) && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001
	lodt.lblRandAtentie.Caption = lodt.lblRandAtentie.Caption + ' (' + Alltrim(Str(m.lnROTN)) + ' lei)'
	lodt.Show(1)
Endproc && viz_decontTVA_0719

*===========================================
*!* 01.01.2017 TVA 19%
Procedure viz_decontTVA_0117
	Lparameters tnNumarLuni
	*Parameters tlTrim

	*** tnNumarLuni: 1 = o luna, 3 = 3 luni trimestrial, 6 = 6 luni semestrial, 12 = 12 luni anual

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec

	Local lodt As 'frm_decontTVA'
	Local lcCursor, lcSql, ldDataCurenta, ldDataPrecedenta, lnAnCurent, lnAnPrecedent, lnLunaCurenta
	Local lnLunaMin, lnLunaPrecedenta, lnPerioada, lnPlati, lnSucces, lnTP, lnTR, loEx As Exception, lnROTN
	*:Global pn10B, pn10T, pn11B, pn11T, pn13B, pn14B, pn15B, pn171B, pn171T, pn17B, pn17T, pn1911B
	*:Global pn1911T, pn191B, pn191T, pn19B, pn19T, pn1B, pn21B, pn21T, pn22B, pn22T, pn23B, pn23T
	*:Global pn241B, pn24B, pn31B, pn3B, pn51B, pn51T, pn5B, pn5T, pn711B, pn711T, pn71B, pn71T, pn7B
	*:Global pn7T, pn9B, pn9T, pnRegularizareB, pnRegularizareT

	lnAnCurent = m.gnAn
	lnLunaCurenta = m.gnLuna
	ldDataCurenta = Date(m.lnAnCurent, m.lnLunaCurenta, 1)
	lnROTN = 0

	* Luna de inceput a perioadei
	* 1 = gnLuna
	* 3 = 1,4,7,10
	* 6 = 1,7
	* 12 = 1
	lnPerioada = Ceil(m.lnLunaCurenta / tnNumarLuni)
	lnLunaMin = (lnPerioada - 1) * tnNumarLuni + 1

	*** luna pentru balanta precedenta
	ldDataPrecedenta = Gomonth(Date(m.lnAnCurent, m.lnLunaMin, 1), -1)
	lnLunaPrecedenta = Month(m.ldDataPrecedenta)
	lnAnPrecedent = Year(m.ldDataPrecedenta)
	&& ----
	pnLunaI = lnLunaMin
	pnLunaF = m.lnLunaCurenta

	pnLunaPrec = m.lnLunaPrecedenta
	pnAnPrec = m.lnAnPrecedent

	Private poDecont
	Store '' To poDecont

	Private pnProRata, pnAchizitieBazaN, pnAchizitieTVAN, pnLivrareBazaN, pnLivrareTVAN, pnAchizitieBaza6N, pnAchizitieTVA6N, pnLivrareBaza6N, pnLivrareTVA6N
	pnProRata = 100
	pnAchizitieBazaN = 0
	pnAchizitieTVAN = 0
	pnLivrareBazaN = 0
	pnLivrareTVAN = 0
	pnAchizitieBaza6N = 0
	pnAchizitieTVA6N = 0
	pnLivrareBaza6N = 0
	pnLivrareTVA6N = 0

	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	&& schimbare cota TVA standard de la 24% la 20%
	Do Case
		Case m.gnAn < 2016
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '0113' order by id_decont]
		Case m.gnAn = 2016
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '0116' order by id_decont]
		Otherwise
			lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
				[where valabil = '0117' order by id_decont]
	Endcase
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Text To lcSql Noshow Textmerge
	SELECT ROUND(SUM(RO19B),0) AS RO19B, 
		 ROUND(SUM(RO19T),0) AS RO19T, 
		 ROUND(SUM(RO9B),0) AS RO9B, 
		 ROUND(SUM(RO9T),0) AS RO9T,
		 ROUND(SUM(RO5B),0) AS RO5B, 
		 ROUND(SUM(RO5T),0) AS RO5T, 
		 ROUND(SUM(RO20B),0) AS RO20B, 
		 ROUND(SUM(RO20T),0) AS RO20T,
		 ROUND(SUM(RO24B),0) AS RO24B, 
		 ROUND(SUM(RO24T),0) AS RO24T,
		 ROUND(SUM(ROTI),0) AS ROTI, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19B ELSE 0 END),0) AS RO19Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19T ELSE 0 END),0) AS RO19Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO9B ELSE 0 END),0) AS RO9Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO9T ELSE 0 END),0) AS RO9Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO5B ELSE 0 END),0) AS RO5Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO5T ELSE 0 END),0) AS RO5Tr, 
		 ROUND(SUM(CESCDD1),0) AS CESCDD1,
		 ROUND(SUM(CESCDD2),0) AS CESCDD2,
		 ROUND(SUM(CEOPTR),0) AS CEOPTR,
		 ROUND(SUM(CESVDD),0) AS CESVDD,
		 ROUND(SUM(CESVFDD),0) AS CESVFDD,
		 ROUND(SUM(CESVFS),0) AS CESVFS,
		 ROUND(SUM(WRSCDD),0) AS WRSCDD, 
		 ROUND(SUM(FODD),0) AS FODD, 
		 ROUND(SUM(FOFDD),0) AS FOFDD, 
		 ROUND(SUM(CEOPTR),0) AS CEOPTR, 
		 ROUND(SUM(CESVDD),0) AS CESVDD, 
		 ROUND(SUM(CESVFDD),0) AS CESVFDD, 
		 ROUND(SUM(CESVFS),0) AS CESVFS, 
		 ROUND(SUM(WRSCFDD),0) AS WRSCFDD,
		 ROUND(SUM(WRN),0) AS WRN FROM JV2007 
		 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF <<m.gcCondSucursala>>
	Endtext
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala


	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	Go Top
	pn1B = Nvl(cescdd1, 0) + Nvl(ceoptr, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0) + Nvl(cesvdd, 0) + Nvl(cesvfdd, 0) + Nvl(cesvfs, 0)
	pn31B = Nvl(cesvfs, 0)

	&& scad tva exigibil cu data act in afara perioadei de declarare. il pun pe regularizare rd. 16 
	pn9B = Nvl(ro19b, 0) - Nvl(ro19br, 0)
	pn9T = Nvl(ro19t, 0) - Nvl(ro19tr, 0)
	pn10B = Nvl(ro9b, 0) - Nvl(ro9br, 0)
	pn10T = Nvl(ro9t, 0) - Nvl(ro9tr, 0)
	pn11B = Nvl(ro5b, 0) - Nvl(ro5br, 0)
	pn11T = Nvl(ro5t, 0) - Nvl(ro5tr, 0)
	pnRegularizareB = Nvl(ro20b, 0) + Nvl(ro24b, 0) + Nvl(ro19br, 0) + Nvl(ro9br, 0) + Nvl(ro5br, 0)
	pnRegularizareT = Nvl(ro20t, 0) + Nvl(ro24t, 0) + Nvl(ro19tr, 0) + Nvl(ro9tr, 0) + Nvl(ro5tr, 0)

	pn13B = Nvl(roti, 0)
	pn14B = Nvl(wrscdd, 0)

	pn15B = Nvl(wrscfdd, 0)

	*!*		*** LIVRARI D394
	*!*		pn171B = Nvl(loD394.livb + loD394.livtib,0)
	*!*		pn171T = Nvl(loD394.livt + loD394.livtit,0)

	Select cDecont
	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '3.1'
	If Found()
		Replace valoare With pn31B
	Endif

	Locate For Alltrim(Rand) = '9'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '10'
	If Found()
		Replace valoare With pn10B, TVA With pn10T
	Endif

	Locate For Alltrim(Rand) = '11'
	If Found()
		Replace valoare With pn11B, TVA With pn11T
	Endif

	Locate For Alltrim(Rand) = '13'
	If Found()
		Replace valoare With pn13B
	Endif

	Locate For Alltrim(Rand) = '14'
	If Found()
		Replace valoare With pn14B
	Endif

	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pn15B
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '16'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	*!*		*!* LIVRARI INTERNE CATRE CLIENTI PLATITORI TVA IN ROMANIA (LIVRARI D394)
	*!*		Locate For Alltrim(Rand) = '17.1'
	*!*		If Found()
	*!*			Replace valoare With pn171B, TVA With pn171T
	*!*		Endif

	Text To lcSql Noshow Textmerge
	SELECT Round(Sum(RO24B),0) As RO24B, 
		 Round(Sum(RO24T),0) As RO24T, 
		 ROUND(SUM(RO20B),0) AS RO20B, 
		 ROUND(SUM(RO20T),0) AS RO20T, 
		 ROUND(SUM(RO19BCB),0) AS RO19BCB, 
		 ROUND(SUM(RO19BCT),0) AS RO19BCT, 
		 ROUND(SUM(RO19BVB),0) AS RO19BVB, 
		 ROUND(SUM(RO19BVT),0) AS RO19BVT, 
		 ROUND(SUM(RO19BFB),0) AS RO19BFB, 
		 ROUND(SUM(RO19BFT),0) AS RO19BFT, 
		 ROUND(SUM(RO09BCB),0) AS RO09BCB, 
		 ROUND(SUM(RO09BCT),0) AS RO09BCT, 
		 ROUND(SUM(RO09BVB),0) AS RO09BVB, 
		 ROUND(SUM(RO09BVT),0) AS RO09BVT, 
		 ROUND(SUM(RO09BFB),0) AS RO09BFB, 
		 ROUND(SUM(RO09BFT),0) AS RO09BFT, 
		 ROUND(SUM(RO05B),0) AS RO05B, 
		 ROUND(SUM(RO05T),0) AS RO05T, 
		 ROUND(SUM(ROSCN),0) AS ROSCN, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19BCB + RO19BVB + RO19BFB ELSE 0 END),0) AS RO19Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO19BCT + RO19BVT + RO19BFT ELSE 0 END),0) AS RO19Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO09BCB + RO09BVB + RO09BFB ELSE 0 END),0) AS RO09Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO09BCT + RO09BVT + RO09BFT ELSE 0 END),0) AS RO09Tr, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO05B ELSE 0 END),0) AS RO05Br, 
		 ROUND(SUM(CASE WHEN EXTRACT(YEAR FROM DATAIREG)*12+EXTRACT(MONTH FROM DATAIREG) between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF AND EXTRACT(YEAR FROM DATAACT)*12+EXTRACT(MONTH FROM DATAACT) not between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF THEN RO05T ELSE 0 END),0) AS RO05Tr, 		
		 Round(Sum(FO24B),0) As FO24B, 
		 Round(Sum(FO24T),0) As FO24T, 
		 Round(Sum(FO20B),0) As FO20B, 
		 Round(Sum(FO20T),0) As FO20T, 
		 ROUND(SUM(FO19BCB),0) AS FO19BCB, 
		 ROUND(SUM(FO19BCT),0) AS FO19BCT, 
		 ROUND(SUM(FO19BVB),0) AS FO19BVB, 
		 ROUND(SUM(FO19BVT),0) AS FO19BVT, 
		 ROUND(SUM(FO19BFB),0) AS FO19BFB, 
		 ROUND(SUM(FO19BFT),0) AS FO19BFT, 
		 ROUND(SUM(FO09BCB),0) AS FO09BCB, 
		 ROUND(SUM(FO09BCT),0) AS FO09BCT, 
		 ROUND(SUM(FO09BVB),0) AS FO09BVB, 
		 ROUND(SUM(FO09BVT),0) AS FO09BVT, 
		 ROUND(SUM(FO09BFB),0) AS FO09BFB, 
		 ROUND(SUM(FO09BFT),0) AS FO09BFT, 
		 ROUND(SUM(FOSCN),0) AS FOSCN, 
		 Round(Sum(CE24B),0) As CE24B, 
		 Round(Sum(CE24T),0) As CE24T, 
		 Round(Sum(CE20B),0) As CE20B, 
		 Round(Sum(CE20T),0) As CE20T, 
		 Round(Sum(CE24SB),0) As CE24SB, 
		 Round(Sum(CE24ST),0) As CE24ST, 
		 Round(Sum(CE20SB),0) As CE20SB, 
		 Round(Sum(CE20ST),0) As CE20ST, 
		 Round(Sum(CE5SB),0) As CE5SB, 
		 Round(Sum(CE5ST),0) As CE5ST, 
		 Round(Sum(CE9SB),0) As CE9SB, 
		 Round(Sum(CE9ST),0) As CE9ST, 
		 Round(Sum(CE19SB),0) As CE19SB, 		
		 Round(Sum(CE19ST),0) As CE19ST, 
		 ROUND(SUM(CEBCB),0) AS CEBCB, 
		 ROUND(SUM(CEBCT),0) AS CEBCT, 
		 ROUND(SUM(CEBVB),0) AS CEBVB, 
		 ROUND(SUM(CEBVT),0) AS CEBVT, 
		 ROUND(SUM(CEBVSC),0) AS CEBVSC, 
		 ROUND(SUM(CEBVN),0) AS CEBVN, 
		 ROUND(SUM(CEBFB),0) AS CEBFB, 
		 ROUND(SUM(CEBFT),0) AS CEBFT, 
		 ROUND(SUM(CEBFSC),0) AS CEBFSC, 
		 ROUND(SUM(CEBFN),0) AS CEBFN, 
		 Round(Sum(TI24B), 0) As TI24B, 
		 Round(Sum(TI24T), 0) As TI24T, 
		 Round(Sum(TI20B), 0) As TI20B, 
		 Round(Sum(TI20T), 0) As TI20T, 
		 ROUND(SUM(TI19BCB),0) AS TI19BCBWR, 
		 ROUND(SUM(TI19BCT),0) AS TI19BCTWR, 
		 ROUND(SUM(TI19BVB),0) AS TI19BVBWR, 
		 ROUND(SUM(TI19BVT),0) AS TI19BVTWR, 
		 ROUND(SUM(TI19BFB),0) AS TI19BFBWR, 
		 ROUND(SUM(TI19BFT),0) AS TI19BFTWR, 
		 ROUND(SUM(TI09BVB),0) AS TI09BVBWR, 
		 ROUND(SUM(TI09BVT),0) AS TI09BVTWR, 
		 ROUND(SUM(TI09BFB),0) AS TI09BFBWR, 
		 ROUND(SUM(TI09BFT),0) AS TI09BFTWR, 
		 ROUND(SUM(TI19BCB),0) AS TI19BCBRO, 
		 ROUND(SUM(TI19BCT),0) AS TI19BCTRO, 
		 ROUND(SUM(TI19BVB),0) AS TI19BVBRO, 
		 ROUND(SUM(TI19BVT),0) AS TI19BVTRO, 
		 ROUND(SUM(TI19BFB),0) AS TI19BFBRO, 
		 ROUND(SUM(TI19BFT),0) AS TI19BFTRO, 
		 ROUND(SUM(TI09BVB),0) AS TI09BVBRO, 
		 ROUND(SUM(TI09BVT),0) AS TI09BVTRO, 
		 ROUND(SUM(TI09BFB),0) AS TI09BFBRO, 
		 ROUND(SUM(TI09BFT),0) AS TI09BFTRO, 
		 ROUND(SUM(ROTN),0) AS ROTN 
		 FROM JC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF <<m.gcCondSucursala>>
	Endtext
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC
	Go Top
	pn18B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0) && 19%
	pn18T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0)
	pn181B = pn18B
	pn181T = pn18T
	pn5B = pn18B
	pn5T = pn18T
	pn51B = pn181B
	pn51T = pn181T

	*** rd.19 = rd.6
	*** Regularizari privind achizi_iile intracomunitare de bunuri pentru care cumparatorul este obligat la plata TVA (taxare inversa) (rd.19=rd.6)
	pn19B = Nvl(ce20b, 0) + Nvl(ce24b, 0)
	pn19T = Nvl(ce20t, 0) + Nvl(ce24t, 0)
	pn6B = pn19B
	pn6T = pn19T

	*** Achizi_ii de bunuri, altele decat cele de la rd. 18 ši 19, si achizitii de servicii pentru care beneficiarul din Romania este obligat la plata TVA (taxare
	*** inversa) (rd.20=rd.7), din care
	pn20B = Nvl(CE19SB, 0)
	pn20T = Nvl(CE19ST, 0)
	pn7B = pn20B
	pn7T = pn20T

	*** Achizitii de servicii intracomunitare pentru care beneficiarul este obligat la plata TVA (taxare inversa) (rd.20.1=rd.7.1)
	pn201B = Nvl(CE19SB, 0)
	pn201T = Nvl(CE19ST, 0)
	pn71B = pn201B
	pn71T = pn201T

	*** Regularizari privind achizitii de servicii intracomunitare pentru care beneficiarul din Romania este obligat la plata TVA (taxare inversa)(rd.21=rd.8)
	pn21B = Nvl(CE24SB, 0) + Nvl(CE20SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn21T = Nvl(CE24ST, 0) + Nvl(CE20ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn8B = pn21B
	pn8T = pn21T

	*** Achizi_ii de bunuri ši servicii taxabile cu cota de 19%, altele decat cele de la rd.25	
	&& scad tva exigibil din operatiile cu data act in afara perioadei de declarare. le pun pe regularizari, rd. 32
	pn22B = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0) - Nvl(ro19br, 0)
	pn22T = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0) - Nvl(ro19tr, 0)

	*** Achizi_ii de bunuri ši servicii taxabile cu cota de 9%
	pn23B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0) - Nvl(ro09br, 0)
	pn23T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0) - Nvl(ro09tr, 0)

	*** Achizi_ii de bunuri taxabile cu cota de 5%
	pn24B = Nvl(ro05b, 0) - Nvl(ro05br, 0)
	pn24T = Nvl(ro05t, 0) - Nvl(ro05tr, 0)

	*** Achizi_ii de bunuri ši servicii supuse masurilor de simplificare pentru care beneficiarul este obligat la plata TVA (taxare inversa),din care (rd.25=rd.12)
	pn25B = Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) && ACH. TI
	pn25T = Nvl(ti24t, 0) + Nvl(ti20t, 0) + Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn12B = pn25B
	pn12T = pn25T

	pn251B = Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) && ACH. TI 19%
	pn251T = Nvl(ti19bctro, 0) + Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0)
	pn121B = pn251B
	pn121T = pn251T

	pn252B = Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) && && ACH. TI 9%
	pn252T = Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn122B = pn252B
	pn122T = pn252T

	pn253B = 0 && ACH. TI 5% - NU AM EXPLICATIE TVA PENTRU TI 5%. AR TREBUI ADAUGATA
	pn253T = 0
	pn123B = pn253B
	pn123T = pn253T

	*** Achizi_ii de bunuri ši servicii scutite de taxa sau neimpozabile, din care
	pn28B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0) && Achizi_ii de bunuri ši servicii scutite de taxa sau neimpozabile
	pn281B = Nvl(cebvsc, 0) + Nvl(cebfsc, 0) && Achizitii de servicii intracomunitare scutite de taxa

	*** Regularizari taxa dedusa
	pn32B = Nvl(ro20b, 0) + Nvl(ro24b, 0) + Nvl(fo20b, 0) + Nvl(fo24b, 0) + Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ro19br, 0) + Nvl(ro09br, 0) + Nvl(ro05br, 0)
	pn32T = Nvl(ro20t, 0) + Nvl(ro24t, 0) + Nvl(fo20t, 0) + Nvl(fo24t, 0) + Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ro19tr, 0) + Nvl(ro09tr, 0) + Nvl(ro05tr, 0)

	lnROTN = Nvl(ROTN, 0) &&  && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001

	Select cDecont
	Locate For Alltrim(Rand) = '18'
	If Found()
		Replace valoare With pn18B, TVA With pn18T
	Endif

	Locate For Alltrim(Rand) = '18.1'
	If Found()
		Replace valoare With pn181B, TVA With pn181T
	Endif

	Locate For Alltrim(Rand) = '5'
	If Found()
		Replace valoare With pn5B, TVA With pn5T
	Endif

	Locate For Alltrim(Rand) = '5.1'
	If Found()
		Replace valoare With pn51B, TVA With pn51T
	Endif

	Locate For Alltrim(Rand) = '19'
	If Found()
		Replace valoare With pn19B, TVA With pn19T
	Endif

	Locate For Alltrim(Rand) = '6'
	If Found()
		Replace valoare With pn6B, TVA With pn6T
	Endif

	Locate For Alltrim(Rand) = '20'
	If Found()
		Replace valoare With pn20B, TVA With pn20T
	Endif

	Locate For Alltrim(Rand) = '20.1'
	If Found()
		Replace valoare With pn201B, TVA With pn201T
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '7.1'
	If Found()
		Replace valoare With pn71B, TVA With pn71T
	Endif

	Locate For Alltrim(Rand) = '21'
	If Found()
		Replace valoare With pn21B, TVA With pn21T
	Endif

	Locate For Alltrim(Rand) = '8'
	If Found()
		Replace valoare With pn8B, TVA With pn8T
	Endif

	Locate For Alltrim(Rand) = '22'
	If Found()
		Replace valoare With pn22B, TVA With pn22T
	Endif

	Locate For Alltrim(Rand) = '23'
	If Found()
		Replace valoare With pn23B, TVA With pn23T
	Endif

	Locate For Alltrim(Rand) = '24'
	If Found()
		Replace valoare With pn24B, TVA With pn24T
	Endif

	Locate For Alltrim(Rand) = '25'
	If Found()
		Replace valoare With pn25B, TVA With pn25T
	Endif

	Locate For Alltrim(Rand) = '25.1'
	If Found()
		Replace valoare With pn251B, TVA With pn251T
	Endif

	Locate For Alltrim(Rand) = '25.2'
	If Found()
		Replace valoare With pn252B, TVA With pn252T
	Endif

	Locate For Alltrim(Rand) = '25.3'
	If Found()
		Replace valoare With pn253B, TVA With pn253T
	Endif

	Locate For Alltrim(Rand) = '12'
	If Found()
		Replace valoare With pn12B, TVA With pn12T
	Endif

	Locate For Alltrim(Rand) = '12.1'
	If Found()
		Replace valoare With pn121B, TVA With pn121T
	Endif

	Locate For Alltrim(Rand) = '12.2'
	If Found()
		Replace valoare With pn122B, TVA With pn122T
	Endif

	Locate For Alltrim(Rand) = '12.3'
	If Found()
		Replace valoare With pn123B, TVA With pn123T
	Endif

	Locate For Alltrim(Rand) = '28'
	If Found()
		Replace valoare With pn28B
	Endif

	Locate For Alltrim(Rand) = '28.1'
	If Found()
		Replace valoare With pn281B
	Endif

	*!* REGULARIZARE
	Locate For Alltrim(Rand) = '32'
	If Found()
		Replace valoare With pn32B, TVA With pn32T
	Endif

	*** Rd. 40 Soldul sumei negative a TVA reportate din perioada precedenta pentru care nu s-a solicitat rambursare (rd.44 din decontul perioadei fiscale precedente)
	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala


	lcCursor = [crsTR] && TVA de recuperat
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTR
	lnTR = solddeb

	Select cDecont
	Locate For Alltrim(Rand) = '40'
	If Found()
		Replace TVA With lnTR
	Endif

	If Used('crsTR')
		Use In crsTR
	Endif

	*** Rd. 37 Soldul TVA de plata din decontul perioadei fiscale precedente (rd.43 din decontul perioadei fiscale precedente) neachitate pana la data depunerii decontului de TVA
	Text To lcSql Textmerge Noshow
	SELECT NVL(soldcred,0) as  soldcred
		from vbal
		where cont = '4423' and AN = ?pnAnPrec AND LUNA = ?pnLunaPrec
		<<m.gcCondSucursala>>
	Endtext

	lcCursor = [crsTP] && nu si-au platit tva din luna precedenta
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTP
	lnTP = soldcred

	Use In (Select('crsTP'))

	Text To lcSql Textmerge Noshow
	select  nvl(sum(suma),0) as suma
		from act
		where an = ?gnAn and luna between ?pnLunaI and ?pnLunaF
		and sters = 0
		and scd = '4423' and scc In ('5121', '5311')
		<<m.gcCondSucursala>>
	Endtext

	lcCursor = [crsPlati]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsPlati
	lnPlati = suma

	Use In (Select('crsPlati'))

	lnTP = Max(m.lnTP - m.lnPlati, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '37'
	If Found()
		Replace TVA With Nvl(m.lnTP, 0)
	Endif

	*** Regularizari
	lcSql = [select sum(suma) as suma, id_jtva_coloana from act ] + ;
		[where sters = 0 and id_jtva_coloana in (1002, 1003) and ] + ;
		[an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + m.gcCondSucursala + ;
		[ group by id_jtva_coloana order by id_jtva_coloana]
	lcCursor = [crsRegTaxe]
	If goExecutor.oExecuta(lcSql, lcCursor)
		Select (lcCursor)
		Scan
			lnSuma = suma
			lcRand = Iif(id_jtva_coloana = 1003, '32', '16')
			Select cDecont
			Replace TVA With TVA - lnSuma For Rand = lcRand
			Select (lcCursor)
		Endscan
	Endif
	Use In (Select(lcCursor))

	*** SOLD NEEXIGIBIL
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBazaN = Round(bazan, 0)
		pnAchizitieTVAN = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBaza6N = Round(bazan, 0)
		pnAchizitieTVA6N = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))


	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBazaN = Round(bazan, 0)
		pnLivrareTVAN = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO19NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO19NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBaza6N = Round(bazan, 0)
		pnLivrareTVA6N = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))

	Select cDecont
	Do Case
		Case m.gnAn < 2016
			lodt = Createobject('frm_decontTVA', m.tnNumarLuni)
		Case m.gnAn = 2016
			lodt = Createobject('frm_decontTVA_201601', m.tnNumarLuni)	&& s-au mai adaugat 2 campuri la sfarsit - sold livrari/ach. neex pt livrari in ultimele 6 luni
		Otherwise && 2017
			lodt = Createobject('frm_decontTVA_201701', m.tnNumarLuni)	&& la fel ca 2016, lblRandAtentie
	Endcase
	lodt.nROTN = m.lnROTN && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001
	lodt.lblRandAtentie.Caption = lodt.lblRandAtentie.Caption + ' (' + Alltrim(Str(m.lnROTN)) + ' lei)'
	lodt.Show(1)
Endproc && viz_decontTVA_0117

*===========================================
*!* 01.01.2016 TVA 20%
Procedure viz_decontTVA_0116
	Lparameters tnNumarLuni
	*Parameters tlTrim

	*** tnNumarLuni: 1 = o luna, 3 = 3 luni trimestrial, 6 = 6 luni semestrial, 12 = 12 luni anual

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec

	Local lodt As 'frm_decontTVA'
	Local lcCursor, lcSql, ldDataCurenta, ldDataPrecedenta, lnAnCurent, lnAnPrecedent, lnLunaCurenta
	Local lnLunaMin, lnLunaPrecedenta, lnPerioada, lnPlati, lnSucces, lnTP, lnTR, loEx As Exception, lnROTN
	*:Global pn10B, pn10T, pn11B, pn11T, pn13B, pn14B, pn15B, pn171B, pn171T, pn17B, pn17T, pn1911B
	*:Global pn1911T, pn191B, pn191T, pn19B, pn19T, pn1B, pn21B, pn21T, pn22B, pn22T, pn23B, pn23T
	*:Global pn241B, pn24B, pn31B, pn3B, pn51B, pn51T, pn5B, pn5T, pn711B, pn711T, pn71B, pn71T, pn7B
	*:Global pn7T, pn9B, pn9T, pnRegularizareB, pnRegularizareT

	lnAnCurent = m.gnAn
	lnLunaCurenta = m.gnLuna
	ldDataCurenta = Date(m.lnAnCurent, m.lnLunaCurenta, 1)
	lnROTN = 0

	* Luna de inceput a perioadei
	* 1 = gnLuna
	* 3 = 1,4,7,10
	* 6 = 1,7
	* 12 = 1
	lnPerioada = Ceil(m.lnLunaCurenta / tnNumarLuni)
	lnLunaMin = (lnPerioada - 1) * tnNumarLuni + 1

	*** luna pentru balanta precedenta
	ldDataPrecedenta = Gomonth(Date(m.lnAnCurent, m.lnLunaMin, 1), -1)
	lnLunaPrecedenta = Month(m.ldDataPrecedenta)
	lnAnPrecedent = Year(m.ldDataPrecedenta)
	&& ----
	pnLunaI = lnLunaMin
	pnLunaF = m.lnLunaCurenta

	pnLunaPrec = m.lnLunaPrecedenta
	pnAnPrec = m.lnAnPrecedent

	Private poDecont
	Store '' To poDecont

	Private pnProRata, pnAchizitieBazaN, pnAchizitieTVAN, pnLivrareBazaN, pnLivrareTVAN, pnAchizitieBaza6N, pnAchizitieTVA6N, pnLivrareBaza6N, pnLivrareTVA6N
	pnProRata = 100
	pnAchizitieBazaN = 0
	pnAchizitieTVAN = 0
	pnLivrareBazaN = 0
	pnLivrareTVAN = 0
	pnAchizitieBaza6N = 0
	pnAchizitieTVA6N = 0
	pnLivrareBaza6N = 0
	pnLivrareTVA6N = 0

	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	&& schimbare cota TVA standard de la 24% la 20%
	If gnAn < 2016
		lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
			[where valabil = '0113' order by id_decont]
	Else
		lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
			[where valabil = '0116' order by id_decont]
	Endif
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif


	lcSql = [SELECT ROUND(SUM(RO19B),0) AS RO19B, ] + ;
		[ ROUND(SUM(RO19T),0) AS RO19T, ] + ;
		[ ROUND(SUM(RO9B),0) AS RO9B, ] + ;
		[ ROUND(SUM(RO9T),0) AS RO9T,] + ;
		[ ROUND(SUM(RO5B),0) AS RO5B, ] + ;
		[ ROUND(SUM(RO5T),0) AS RO5T, ] + ;
		[ ROUND(SUM(RO20B),0) AS RO20B, ] + ;
		[ ROUND(SUM(RO20T),0) AS RO20T,] + ;
		[ ROUND(SUM(RO24B),0) AS RO24B, ] + ;
		[ ROUND(SUM(RO24T),0) AS RO24T,] + ;
		[ ROUND(SUM(ROTI),0) AS ROTI, ] + ;
		[ ROUND(SUM(CESCDD1),0) AS CESCDD1,] + ;
		[ ROUND(SUM(CESCDD2),0) AS CESCDD2,] + ;
		[ ROUND(SUM(CEOPTR),0) AS CEOPTR,] + ;
		[ ROUND(SUM(CESVDD),0) AS CESVDD,] + ;
		[ ROUND(SUM(CESVFDD),0) AS CESVFDD,] + ;
		[ ROUND(SUM(CESVFS),0) AS CESVFS,] + ;
		[ ROUND(SUM(WRSCDD),0) AS WRSCDD, ] + ;
		[ ROUND(SUM(FODD),0) AS FODD, ] + ;
		[ ROUND(SUM(FOFDD),0) AS FOFDD, ] + ;
		[ ROUND(SUM(CEOPTR),0) AS CEOPTR, ] + ;
		[ ROUND(SUM(CESVDD),0) AS CESVDD, ] + ;
		[ ROUND(SUM(CESVFDD),0) AS CESVFDD, ] + ;
		[ ROUND(SUM(CESVFS),0) AS CESVFS, ] + ;
		[ ROUND(SUM(WRSCFDD),0) AS WRSCFDD,] + ;
		[ ROUND(SUM(WRN),0) AS WRN FROM JV2007 ] + ;
		[ WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala


	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	Go Top
	pn1B = Nvl(cescdd1, 0) + Nvl(ceoptr, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0) + Nvl(cesvdd, 0) + Nvl(cesvfdd, 0) + Nvl(cesvfs, 0) + Nvl(cesvfs, 0)
	pn31B = Nvl(cesvfs, 0)

	pn9B = Nvl(ro20b, 0)
	pn9T = Nvl(ro20t, 0)
	pn10B = Nvl(ro9b, 0)
	pn10T = Nvl(ro9t, 0)
	pn11B = Nvl(ro5b, 0)
	pn11T = Nvl(ro5t, 0)
	pnRegularizareB = Nvl(ro19b, 0) + Nvl(ro24b, 0)
	pnRegularizareT = Nvl(ro19t, 0) + Nvl(ro24t, 0)

	pn13B = Nvl(roti, 0)
	pn14B = Nvl(wrscdd, 0)

	pn15B = Nvl(wrscfdd, 0)

	*!*		*** LIVRARI D394
	*!*		pn171B = Nvl(loD394.livb + loD394.livtib,0)
	*!*		pn171T = Nvl(loD394.livt + loD394.livtit,0)

	Select cDecont
	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '3.1'
	If Found()
		Replace valoare With pn31B
	Endif

	Locate For Alltrim(Rand) = '9'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '10'
	If Found()
		Replace valoare With pn10B, TVA With pn10T
	Endif

	Locate For Alltrim(Rand) = '11'
	If Found()
		Replace valoare With pn11B, TVA With pn11T
	Endif

	Locate For Alltrim(Rand) = '13'
	If Found()
		Replace valoare With pn13B
	Endif

	Locate For Alltrim(Rand) = '14'
	If Found()
		Replace valoare With pn14B
	Endif

	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pn15B
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '16'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	*!*		*!* LIVRARI INTERNE CATRE CLIENTI PLATITORI TVA IN ROMANIA (LIVRARI D394)
	*!*		Locate For Alltrim(Rand) = '17.1'
	*!*		If Found()
	*!*			Replace valoare With pn171B, TVA With pn171T
	*!*		Endif


	lcSql = [SELECT Round(Sum(RO24B),0) As RO24B, ] + ;
		[ Round(Sum(RO24T),0) As RO24T, ] + ;
		[ ROUND(SUM(RO20B),0) AS RO20B, ] + ;
		[ ROUND(SUM(RO20T),0) AS RO20T, ] + ;
		[ ROUND(SUM(RO19BCB),0) AS RO19BCB, ] + ;
		[ ROUND(SUM(RO19BCT),0) AS RO19BCT, ] + ;
		[ ROUND(SUM(RO19BVB),0) AS RO19BVB, ] + ;
		[ ROUND(SUM(RO19BVT),0) AS RO19BVT, ] + ;
		[ ROUND(SUM(RO19BFB),0) AS RO19BFB, ] + ;
		[ ROUND(SUM(RO19BFT),0) AS RO19BFT, ] + ;
		[ ROUND(SUM(RO09BCB),0) AS RO09BCB, ] + ;
		[ ROUND(SUM(RO09BCT),0) AS RO09BCT, ] + ;
		[ ROUND(SUM(RO09BVB),0) AS RO09BVB, ] + ;
		[ ROUND(SUM(RO09BVT),0) AS RO09BVT, ] + ;
		[ ROUND(SUM(RO09BFB),0) AS RO09BFB, ] + ;
		[ ROUND(SUM(RO09BFT),0) AS RO09BFT, ] + ;
		[ ROUND(SUM(RO05B),0) AS RO05B, ] + ;
		[ ROUND(SUM(RO05T),0) AS RO05T, ] + ;
		[ ROUND(SUM(ROSCN),0) AS ROSCN, ] + ;
		[ Round(Sum(FO24B),0) As FO24B, ] + ;
		[ Round(Sum(FO24T),0) As FO24T, ] + ;
		[ Round(Sum(FO20B),0) As FO20B, ] + ;
		[ Round(Sum(FO20T),0) As FO20T, ] + ;
		[ ROUND(SUM(FO19BCB),0) AS FO19BCB, ] + ;
		[ ROUND(SUM(FO19BCT),0) AS FO19BCT, ] + ;
		[ ROUND(SUM(FO19BVB),0) AS FO19BVB, ] + ;
		[ ROUND(SUM(FO19BVT),0) AS FO19BVT, ] + ;
		[ ROUND(SUM(FO19BFB),0) AS FO19BFB, ] + ;
		[ ROUND(SUM(FO19BFT),0) AS FO19BFT, ] + ;
		[ ROUND(SUM(FO09BCB),0) AS FO09BCB, ] + ;
		[ ROUND(SUM(FO09BCT),0) AS FO09BCT, ] + ;
		[ ROUND(SUM(FO09BVB),0) AS FO09BVB, ] + ;
		[ ROUND(SUM(FO09BVT),0) AS FO09BVT, ] + ;
		[ ROUND(SUM(FO09BFB),0) AS FO09BFB, ] + ;
		[ ROUND(SUM(FO09BFT),0) AS FO09BFT, ] + ;
		[ ROUND(SUM(FOSCN),0) AS FOSCN, ] + ;
		[ Round(Sum(CE24B),0) As CE24B, ] + ;
		[ Round(Sum(CE24T),0) As CE24T, ] + ;
		[ Round(Sum(CE20B),0) As CE20B, ] + ;
		[ Round(Sum(CE20T),0) As CE20T, ] + ;
		[ Round(Sum(CE24SB),0) As CE24SB, ] + ;
		[ Round(Sum(CE24ST),0) As CE24ST, ] + ;
		[ Round(Sum(CE20SB),0) As CE20SB, ] + ;
		[ Round(Sum(CE20ST),0) As CE20ST, ] + ;
		[ Round(Sum(CE5SB),0) As CE5SB, ] + ;
		[ Round(Sum(CE5ST),0) As CE5ST, ] + ;
		[ Round(Sum(CE9SB),0) As CE9SB, ] + ;
		[ Round(Sum(CE9ST),0) As CE9ST, ] + ;
		[ ROUND(SUM(CEBCB),0) AS CEBCB, ] + ;
		[ ROUND(SUM(CEBCT),0) AS CEBCT, ] + ;
		[ ROUND(SUM(CEBVB),0) AS CEBVB, ] + ;
		[ ROUND(SUM(CEBVT),0) AS CEBVT, ] + ;
		[ ROUND(SUM(CEBVSC),0) AS CEBVSC, ] + ;
		[ ROUND(SUM(CEBVN),0) AS CEBVN, ] + ;
		[ ROUND(SUM(CEBFB),0) AS CEBFB, ] + ;
		[ ROUND(SUM(CEBFT),0) AS CEBFT, ] + ;
		[ ROUND(SUM(CEBFSC),0) AS CEBFSC, ] + ;
		[ ROUND(SUM(CEBFN),0) AS CEBFN, ] + ;
		[ Round(Sum(TI24B), 0) As TI24B, ] + ;
		[ Round(Sum(TI24T), 0) As TI24T, ] + ;
		[ Round(Sum(TI20B), 0) As TI20B, ] + ;
		[ Round(Sum(TI20T), 0) As TI20T, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBWR, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTWR, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBWR, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTWR, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBWR, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTWR, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBWR, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTWR, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBWR, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTWR, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBRO, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTRO, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBRO, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTRO, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBRO, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTRO, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBRO, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTRO, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBRO, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTRO, ] + ;
		[ ROUND(SUM(ROTN),0) AS ROTN ] + ;
		[ FROM JC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC
	Go Top
	pn18B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0) + Nvl(ce20b, 0) + Nvl(ce24b, 0)
	pn18T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0) + Nvl(ce20t, 0) + Nvl(ce24t, 0)
	pn181B = pn18B
	pn181T = pn18T
	pn5B = pn18B
	pn5T = pn18T
	pn51B = pn181B
	pn51T = pn181T

	pn22B = Nvl(ro20b, 0) + Nvl(fo20b, 0) && Nvl(ro24b, 0) + Nvl(fo24b, 0)
	pn22T = Nvl(ro20t, 0) + Nvl(fo20t, 0) && Nvl(ro24t, 0) + Nvl(fo24t, 0)

	pn23B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0)
	pn23T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0)

	pn24B = Nvl(ro05b, 0)
	pn24T = Nvl(ro05t, 0)

	pn25B = Nvl(ti24b, 0) + Nvl(ti20b, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) && ACH. TI
	pn25T = Nvl(ti24t, 0) + Nvl(ti20t, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn12B = pn25B
	pn12T = pn25T

	pn251B = Nvl(ti20b, 0) && ACH. TI 20%
	pn251T = Nvl(ti20t, 0)
	pn121B = pn251B
	pn121T = pn251T

	pn252B = Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) && && ACH. TI 9%
	pn252T = Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn122B = pn252B
	pn122T = pn252T

	pn253B = 0 && ACH. TI 5% - NU AM EXPLICATIE TVA PENTRU TI 5%. AR TREBUI ADAUGATA
	pn253T = 0
	pn123B = pn253B
	pn123T = pn253T

	pn26B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0)
	pn261B = Nvl(cebvsc, 0) + Nvl(cebfsc, 0)

	*** rd.19=rd.6 ???
	pnRegularizareB = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(ro24b, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0) + Nvl(fo24b, 0)
	pnRegularizareT = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(ro24t, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0) + Nvl(fo24t, 0)


	pn20B = Nvl(CE20SB, 0) + Nvl(CE24SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn20T = Nvl(CE20ST, 0) + Nvl(CE24ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn201B = Nvl(CE20SB, 0) + Nvl(CE24SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn201T = Nvl(CE20ST, 0) + Nvl(CE24ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn7B = pn20B
	pn7T = pn20T
	pn71B = pn201B
	pn71T = pn201T

	*!*		*** ACHIZITII D394
	*!*		pn271B = Nvl(loD394.achb + loD394.achtib,0)
	*!*		pn271T = Nvl(loD394.acht + loD394.achtit,0)

	lnROTN = Nvl(ROTN, 0) &&  && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001

	pnRegularizareB = pnRegularizareB + Nvl(ti19bcbwr, 0) + Nvl(ti19bvbwr, 0) + Nvl(ti19bfbwr, 0) + Nvl(ti09bvbwr, 0) + Nvl(ti09bfbwr, 0)
	pnRegularizareT = pnRegularizareT + Nvl(ti19bctwr, 0) + Nvl(ti19bvtwr, 0) + Nvl(ti19bftwr, 0) + Nvl(ti09bvtwr, 0) + Nvl(ti09bftwr, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '18'
	If Found()
		Replace valoare With pn18B, TVA With pn18T
	Endif

	Locate For Alltrim(Rand) = '18.1'
	If Found()
		Replace valoare With pn181B, TVA With pn181T
	Endif

	Locate For Alltrim(Rand) = '5'
	If Found()
		Replace valoare With pn5B, TVA With pn5T
	Endif

	Locate For Alltrim(Rand) = '5.1'
	If Found()
		Replace valoare With pn51B, TVA With pn51T
	Endif

	Locate For Alltrim(Rand) = '20'
	If Found()
		Replace valoare With pn20B, TVA With pn20T
	Endif

	Locate For Alltrim(Rand) = '20.1'
	If Found()
		Replace valoare With pn201B, TVA With pn201T
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '7.1'
	If Found()
		Replace valoare With pn71B, TVA With pn71T
	Endif

	Locate For Alltrim(Rand) = '22'
	If Found()
		Replace valoare With pn22B, TVA With pn22T
	Endif

	Locate For Alltrim(Rand) = '23'
	If Found()
		Replace valoare With pn23B, TVA With pn23T
	Endif

	Locate For Alltrim(Rand) = '24'
	If Found()
		Replace valoare With pn24B, TVA With pn24T
	Endif

	Locate For Alltrim(Rand) = '25'
	If Found()
		Replace valoare With pn25B, TVA With pn25T
	Endif

	Locate For Alltrim(Rand) = '25.1'
	If Found()
		Replace valoare With pn251B, TVA With pn251T
	Endif

	Locate For Alltrim(Rand) = '25.2'
	If Found()
		Replace valoare With pn252B, TVA With pn252T
	Endif

	Locate For Alltrim(Rand) = '25.3'
	If Found()
		Replace valoare With pn253B, TVA With pn253T
	Endif

	Locate For Alltrim(Rand) = '12'
	If Found()
		Replace valoare With pn12B, TVA With pn12T
	Endif

	Locate For Alltrim(Rand) = '12.1'
	If Found()
		Replace valoare With pn121B, TVA With pn121T
	Endif

	Locate For Alltrim(Rand) = '12.2'
	If Found()
		Replace valoare With pn122B, TVA With pn122T
	Endif

	Locate For Alltrim(Rand) = '12.3'
	If Found()
		Replace valoare With pn123B, TVA With pn123T
	Endif

	Locate For Alltrim(Rand) = '26'
	If Found()
		Replace valoare With pn26B
	Endif

	Locate For Alltrim(Rand) = '26.1'
	If Found()
		Replace valoare With pn261B
	Endif

	*!*		Locate For Alltrim(Rand) = '27.1'
	*!*		If Found()
	*!*			Replace valoare With pn271B, TVA WITH pn271T
	*!*		Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '30'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala


	lcCursor = [crsTR] && TVA de recuperat
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTR
	lnTR = solddeb

	Select cDecont
	Locate For Alltrim(Rand) = '38'
	If Found()
		Replace TVA With lnTR
	Endif

	If Used('crsTR')
		Use In crsTR
	Endif


	***----
	Text To lcSql Textmerge Noshow
	SELECT NVL(soldcred,0) as  soldcred
		from vbal
		where cont = '4423' and
		AN = ?pnAnPrec AND LUNA = ?pnLunaPrec
		<<m.gcCondSucursala>>
	Endtext

	lcCursor = [crsTP] && nu si-au platit tva din luna precedenta
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTP
	lnTP = soldcred

	Use In (Select('crsTP'))

	Text To lcSql Textmerge Noshow
	select  nvl(sum(suma),0) as suma
		from act
		where an = ?gnAn
		and luna  between ?pnLunaI and ?pnLunaF
		and sters = 0
		and scd = '4423' and scc In ('5121', '5311')
		<<m.gcCondSucursala>>
	Endtext

	lcCursor = [crsPlati]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsPlati
	lnPlati = suma

	Use In (Select('crsPlati'))

	lnTP = Max(m.lnTP - m.lnPlati, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '35'
	If Found()
		Replace TVA With Nvl(m.lnTP, 0)
	Endif

	lcSql = [select sum(suma) as suma, id_jtva_coloana from act ] + ;
		[where sters = 0 and id_jtva_coloana in (1002, 1003) and ] + ;
		[an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala + ;
		[ group by id_jtva_coloana order by id_jtva_coloana]
	lcCursor = [crsRegTaxe]
	If goExecutor.oExecuta(lcSql, lcCursor)
		Select (lcCursor)
		Scan
			lnSuma = suma
			lcRand = Iif(id_jtva_coloana = 1003, '30', '16')
			Select cDecont
			Replace TVA With TVA - lnSuma For Rand = lcRand
			Select (lcCursor)
		Endscan
	Endif
	Use In (Select(lcCursor))

	*** SOLD NEEXIGIBIL
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBazaN = Round(bazan, 0)
		pnAchizitieTVAN = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBaza6N = Round(bazan, 0)
		pnAchizitieTVA6N = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))


	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBazaN = Round(bazan, 0)
		pnLivrareTVAN = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))

	* sold neexigibil doar de la facturile din ultimele 6 luni, inclusiv luna de raportare
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO20NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO20NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF and (extract(year from dataact) * 12 + extract(month from dataact) > ?gnan*12+?pnLunaF-6) <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBaza6N = Round(bazan, 0)
		pnLivrareTVA6N = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))

	Select cDecont
	If gnAn < 2016
		lodt = Createobject('frm_decontTVA', m.tnNumarLuni)
	Else
		lodt = Createobject('frm_decontTVA_201601', m.tnNumarLuni)	&& s-au mai adaugat 2 campuri la sfarsit - sold livrari/ach. neex pt livrari in ultimele 6 luni
	Endif
	lodt.nROTN = m.lnROTN && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001
	lodt.lblRand28.Caption = lodt.lblRand28.Caption + ' (' + Alltrim(Str(m.lnROTN)) + ' lei)'
	lodt.Show(1)
Endproc && viz_decontTVA_0116


*===========================================
*!* 19.02.2013
*!* TVA INCASARE
Procedure viz_decontTVA_0113
	Lparameters tnNumarLuni
	*Parameters tlTrim

	*** tnNumarLuni: 1 = o luna, 3 = 3 luni trimestrial, 6 = 6 luni semestrial, 12 = 12 luni anual

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec

	Local lodt As 'frm_decontTVA'
	Local lcCursor, lcSql, ldDataCurenta, ldDataPrecedenta, lnAnCurent, lnAnPrecedent, lnLunaCurenta
	Local lnLunaMin, lnLunaPrecedenta, lnPerioada, lnPlati, lnSucces, lnTP, lnTR, loEx As Exception, lnROTN
	*:Global pn10B, pn10T, pn11B, pn11T, pn13B, pn14B, pn15B, pn171B, pn171T, pn17B, pn17T, pn1911B
	*:Global pn1911T, pn191B, pn191T, pn19B, pn19T, pn1B, pn21B, pn21T, pn22B, pn22T, pn23B, pn23T
	*:Global pn241B, pn24B, pn31B, pn3B, pn51B, pn51T, pn5B, pn5T, pn711B, pn711T, pn71B, pn71T, pn7B
	*:Global pn7T, pn9B, pn9T, pnRegularizareB, pnRegularizareT

	lnAnCurent = m.gnAn
	lnLunaCurenta = m.gnLuna
	ldDataCurenta = Date(m.lnAnCurent, m.lnLunaCurenta, 1)
	lnROTN = 0

	* Luna de inceput a perioadei
	* 1 = gnLuna
	* 3 = 1,4,7,10
	* 6 = 1,7
	* 12 = 1
	lnPerioada = Ceil(m.lnLunaCurenta / tnNumarLuni)
	lnLunaMin = (lnPerioada - 1) * tnNumarLuni + 1

	*** luna pentru balanta precedenta
	ldDataPrecedenta = Gomonth(Date(m.lnAnCurent, m.lnLunaMin, 1), -1)
	lnLunaPrecedenta = Month(m.ldDataPrecedenta)
	lnAnPrecedent = Year(m.ldDataPrecedenta)
	&& ----
	pnLunaI = lnLunaMin
	pnLunaF = m.lnLunaCurenta

	pnLunaPrec = m.lnLunaPrecedenta
	pnAnPrec = m.lnAnPrecedent

	Private poDecont
	Store '' To poDecont

	Private pnProRata, 	pnAchizitieBazaN, pnAchizitieTVAN, pnLivrareBazaN, pnLivrareTVAN
	pnProRata = 100
	pnAchizitieBazaN = 0
	pnAchizitieTVAN = 0
	pnLivrareBazaN = 0
	pnLivrareTVAN = 0

	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
		[where valabil = '0113' order by id_decont]
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif


	lcSql = [SELECT ROUND(SUM(RO19B),0) AS RO19B, ] + ;
		[ ROUND(SUM(RO19T),0) AS RO19T, ] + ;
		[ ROUND(SUM(RO9B),0) AS RO9B, ] + ;
		[ ROUND(SUM(RO9T),0) AS RO9T,] + ;
		[ ROUND(SUM(RO5B),0) AS RO5B, ] + ;
		[ ROUND(SUM(RO5T),0) AS RO5T, ] + ;
		[ ROUND(SUM(RO24B),0) AS RO24B, ] + ;
		[ ROUND(SUM(RO24T),0) AS RO24T,] + ;
		[ ROUND(SUM(ROTI),0) AS ROTI, ] + ;
		[ ROUND(SUM(CESCDD1),0) AS CESCDD1,] + ;
		[ ROUND(SUM(CESCDD2),0) AS CESCDD2,] + ;
		[ ROUND(SUM(CEOPTR),0) AS CEOPTR,] + ;
		[ ROUND(SUM(CESVDD),0) AS CESVDD,] + ;
		[ ROUND(SUM(CESVFDD),0) AS CESVFDD,] + ;
		[ ROUND(SUM(CESVFS),0) AS CESVFS,] + ;
		[ ROUND(SUM(WRSCDD),0) AS WRSCDD, ] + ;
		[ ROUND(SUM(FODD),0) AS FODD, ] + ;
		[ ROUND(SUM(FOFDD),0) AS FOFDD, ] + ;
		[ ROUND(SUM(CEOPTR),0) AS CEOPTR, ] + ;
		[ ROUND(SUM(CESVDD),0) AS CESVDD, ] + ;
		[ ROUND(SUM(CESVFDD),0) AS CESVFDD, ] + ;
		[ ROUND(SUM(CESVFS),0) AS CESVFS, ] + ;
		[ ROUND(SUM(WRSCFDD),0) AS WRSCFDD,] + ;
		[ ROUND(SUM(WRN),0) AS WRN FROM JV2007 ] + ;
		[ WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala


	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	Go Top
	pn1B = Nvl(cescdd1, 0) + Nvl(ceoptr, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0) + Nvl(cesvdd, 0) + Nvl(cesvfdd, 0) + Nvl(cesvfs, 0) + Nvl(cesvfs, 0)
	pn31B = Nvl(cesvfs, 0)

	pn9B = Nvl(ro24b, 0)
	pn9T = Nvl(ro24t, 0)
	pn10B = Nvl(ro9b, 0)
	pn10T = Nvl(ro9t, 0)
	pn11B = Nvl(ro5b, 0)
	pn11T = Nvl(ro5t, 0)
	pnRegularizareB = Nvl(ro19b, 0)
	pnRegularizareT = Nvl(ro19t, 0)

	pn13B = Nvl(roti, 0)
	pn14B = Nvl(wrscdd, 0)

	pn15B = Nvl(wrscfdd, 0)

	*!*		*** LIVRARI D394
	*!*		pn171B = Nvl(loD394.livb + loD394.livtib,0)
	*!*		pn171T = Nvl(loD394.livt + loD394.livtit,0)

	Select cDecont
	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '3.1'
	If Found()
		Replace valoare With pn31B
	Endif

	Locate For Alltrim(Rand) = '9'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '10'
	If Found()
		Replace valoare With pn10B, TVA With pn10T
	Endif

	Locate For Alltrim(Rand) = '11'
	If Found()
		Replace valoare With pn11B, TVA With pn11T
	Endif

	Locate For Alltrim(Rand) = '13'
	If Found()
		Replace valoare With pn13B
	Endif

	Locate For Alltrim(Rand) = '14'
	If Found()
		Replace valoare With pn14B
	Endif

	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pn15B
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '16'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	*!*		*!* LIVRARI INTERNE CATRE CLIENTI PLATITORI TVA IN ROMANIA (LIVRARI D394)
	*!*		Locate For Alltrim(Rand) = '17.1'
	*!*		If Found()
	*!*			Replace valoare With pn171B, TVA With pn171T
	*!*		Endif


	lcSql = [SELECT Round(Sum(RO24B),0) As RO24B, ] + ;
		[ Round(Sum(RO24T),0) As RO24T, ] + ;
		[ ROUND(SUM(RO19BCB),0) AS RO19BCB, ] + ;
		[ ROUND(SUM(RO19BCT),0) AS RO19BCT, ] + ;
		[ ROUND(SUM(RO19BVB),0) AS RO19BVB, ] + ;
		[ ROUND(SUM(RO19BVT),0) AS RO19BVT, ] + ;
		[ ROUND(SUM(RO19BFB),0) AS RO19BFB, ] + ;
		[ ROUND(SUM(RO19BFT),0) AS RO19BFT, ] + ;
		[ ROUND(SUM(RO09BCB),0) AS RO09BCB, ] + ;
		[ ROUND(SUM(RO09BCT),0) AS RO09BCT, ] + ;
		[ ROUND(SUM(RO09BVB),0) AS RO09BVB, ] + ;
		[ ROUND(SUM(RO09BVT),0) AS RO09BVT, ] + ;
		[ ROUND(SUM(RO09BFB),0) AS RO09BFB, ] + ;
		[ ROUND(SUM(RO09BFT),0) AS RO09BFT, ] + ;
		[ ROUND(SUM(RO05B),0) AS RO05B, ] + ;
		[ ROUND(SUM(RO05T),0) AS RO05T, ] + ;
		[ ROUND(SUM(ROSCN),0) AS ROSCN, ] + ;
		[ Round(Sum(FO24B),0) As FO24B, ] + ;
		[ Round(Sum(FO24T),0) As FO24T, ] + ;
		[ ROUND(SUM(FO19BCB),0) AS FO19BCB, ] + ;
		[ ROUND(SUM(FO19BCT),0) AS FO19BCT, ] + ;
		[ ROUND(SUM(FO19BVB),0) AS FO19BVB, ] + ;
		[ ROUND(SUM(FO19BVT),0) AS FO19BVT, ] + ;
		[ ROUND(SUM(FO19BFB),0) AS FO19BFB, ] + ;
		[ ROUND(SUM(FO19BFT),0) AS FO19BFT, ] + ;
		[ ROUND(SUM(FO09BCB),0) AS FO09BCB, ] + ;
		[ ROUND(SUM(FO09BCT),0) AS FO09BCT, ] + ;
		[ ROUND(SUM(FO09BVB),0) AS FO09BVB, ] + ;
		[ ROUND(SUM(FO09BVT),0) AS FO09BVT, ] + ;
		[ ROUND(SUM(FO09BFB),0) AS FO09BFB, ] + ;
		[ ROUND(SUM(FO09BFT),0) AS FO09BFT, ] + ;
		[ ROUND(SUM(FOSCN),0) AS FOSCN, ] + ;
		[ Round(Sum(CE24B),0) As CE24B, ] + ;
		[ Round(Sum(CE24T),0) As CE24T, ] + ;
		[ Round(Sum(CE24SB),0) As CE24SB, ] + ;
		[ Round(Sum(CE24ST),0) As CE24ST, ] + ;
		[ Round(Sum(CE5SB),0) As CE5SB, ] + ;
		[ Round(Sum(CE5ST),0) As CE5ST, ] + ;
		[ Round(Sum(CE9SB),0) As CE9SB, ] + ;
		[ Round(Sum(CE9ST),0) As CE9ST, ] + ;
		[ ROUND(SUM(CEBCB),0) AS CEBCB, ] + ;
		[ ROUND(SUM(CEBCT),0) AS CEBCT, ] + ;
		[ ROUND(SUM(CEBVB),0) AS CEBVB, ] + ;
		[ ROUND(SUM(CEBVT),0) AS CEBVT, ] + ;
		[ ROUND(SUM(CEBVSC),0) AS CEBVSC, ] + ;
		[ ROUND(SUM(CEBVN),0) AS CEBVN, ] + ;
		[ ROUND(SUM(CEBFB),0) AS CEBFB, ] + ;
		[ ROUND(SUM(CEBFT),0) AS CEBFT, ] + ;
		[ ROUND(SUM(CEBFSC),0) AS CEBFSC, ] + ;
		[ ROUND(SUM(CEBFN),0) AS CEBFN, ] + ;
		[ Round(Sum(TI24B), 0) As TI24B, ] + ;
		[ Round(Sum(TI24T), 0) As TI24T, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBWR, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTWR, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBWR, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTWR, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBWR, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTWR, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBWR, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTWR, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBWR, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTWR, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBRO, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTRO, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBRO, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTRO, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBRO, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTRO, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBRO, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTRO, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBRO, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTRO, ] + ;
		[ ROUND(SUM(ROTN),0) AS ROTN ] + ;
		[ FROM JC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC
	Go Top
	pn18B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0) + Nvl(ce24b, 0)
	pn18T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0) + Nvl(ce24t, 0)
	pn181B = pn18B
	pn181T = pn18T
	pn5B = pn18B
	pn5T = pn18T
	pn51B = pn181B
	pn51T = pn181T

	pn22B = Nvl(ro24b, 0) + Nvl(fo24b, 0)
	pn22T = Nvl(ro24t, 0) + Nvl(fo24t, 0)

	pn23B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0)
	pn23T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0)

	pn24B = Nvl(ro05b, 0)
	pn24T = Nvl(ro05t, 0)

	pn25B = Nvl(ti24b, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0)
	pn25T = Nvl(ti24t, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn12B = pn25B
	pn12T = pn25T
	pn26B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0)
	pn261B = Nvl(cebvsc, 0) + Nvl(cebfsc, 0)

	*** rd.19=rd.6 ???
	pnRegularizareB = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0)
	pnRegularizareT = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0)


	pn20B = Nvl(CE24SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn20T = Nvl(CE24ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn201B = Nvl(CE24SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn201T = Nvl(CE24ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn7B = pn20B
	pn7T = pn20T
	pn71B = pn201B
	pn71T = pn201T

	*!*		*** ACHIZITII D394
	*!*		pn271B = Nvl(loD394.achb + loD394.achtib,0)
	*!*		pn271T = Nvl(loD394.acht + loD394.achtit,0)

	lnROTN = Nvl(ROTN, 0) &&  && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001

	pnRegularizareB = pnRegularizareB + Nvl(ti19bcbwr, 0) + Nvl(ti19bvbwr, 0) + Nvl(ti19bfbwr, 0) + Nvl(ti09bvbwr, 0) + Nvl(ti09bfbwr, 0)
	pnRegularizareT = pnRegularizareT + Nvl(ti19bctwr, 0) + Nvl(ti19bvtwr, 0) + Nvl(ti19bftwr, 0) + Nvl(ti09bvtwr, 0) + Nvl(ti09bftwr, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '18'
	If Found()
		Replace valoare With pn18B, TVA With pn18T
	Endif

	Locate For Alltrim(Rand) = '18.1'
	If Found()
		Replace valoare With pn181B, TVA With pn181T
	Endif

	Locate For Alltrim(Rand) = '5'
	If Found()
		Replace valoare With pn5B, TVA With pn5T
	Endif

	Locate For Alltrim(Rand) = '5.1'
	If Found()
		Replace valoare With pn51B, TVA With pn51T
	Endif

	Locate For Alltrim(Rand) = '20'
	If Found()
		Replace valoare With pn20B, TVA With pn20T
	Endif

	Locate For Alltrim(Rand) = '20.1'
	If Found()
		Replace valoare With pn201B, TVA With pn201T
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '7.1'
	If Found()
		Replace valoare With pn71B, TVA With pn71T
	Endif

	Locate For Alltrim(Rand) = '22'
	If Found()
		Replace valoare With pn22B, TVA With pn22T
	Endif

	Locate For Alltrim(Rand) = '23'
	If Found()
		Replace valoare With pn23B, TVA With pn23T
	Endif

	Locate For Alltrim(Rand) = '24'
	If Found()
		Replace valoare With pn24B, TVA With pn24T
	Endif

	Locate For Alltrim(Rand) = '25'
	If Found()
		Replace valoare With pn25B, TVA With pn25T
	Endif

	Locate For Alltrim(Rand) = '12'
	If Found()
		Replace valoare With pn12B, TVA With pn12T
	Endif

	Locate For Alltrim(Rand) = '26'
	If Found()
		Replace valoare With pn26B
	Endif

	Locate For Alltrim(Rand) = '26.1'
	If Found()
		Replace valoare With pn261B
	Endif

	*!*		Locate For Alltrim(Rand) = '27.1'
	*!*		If Found()
	*!*			Replace valoare With pn271B, TVA WITH pn271T
	*!*		Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '30'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala


	lcCursor = [crsTR] && TVA de recuperat
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTR
	lnTR = solddeb

	Select cDecont
	Locate For Alltrim(Rand) = '38'
	If Found()
		Replace TVA With lnTR
	Endif

	If Used('crsTR')
		Use In crsTR
	Endif


	***----
	Text To lcSql Textmerge Noshow
	SELECT NVL(soldcred,0) as  soldcred
		from vbal
		where cont = '4423' and
		AN = ?pnAnPrec AND LUNA = ?pnLunaPrec
		<<m.gcCondSucursala>>
	Endtext

	lcCursor = [crsTP] && nu si-au platit tva din luna precedenta
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTP
	lnTP = soldcred

	Use In (Select('crsTP'))

	Text To lcSql Textmerge Noshow
	select  nvl(sum(suma),0) as suma
		from act
		where an = ?gnAn
		and luna  between ?pnLunaI and ?pnLunaF
		and sters = 0
		and scd = '4423' and scc In ('5121', '5311')
		<<m.gcCondSucursala>>
	Endtext

	lcCursor = [crsPlati]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsPlati
	lnPlati = suma

	Use In (Select('crsPlati'))

	lnTP = Max(m.lnTP - m.lnPlati, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '35'
	If Found()
		Replace TVA With Nvl(m.lnTP, 0)
	Endif

	lcSql = [select sum(suma) as suma, id_jtva_coloana from act ] + ;
		[where sters = 0 and id_jtva_coloana in (1002, 1003) and ] + ;
		[an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala + ;
		[ group by id_jtva_coloana order by id_jtva_coloana]
	lcCursor = [crsRegTaxe]
	If goExecutor.oExecuta(lcSql, lcCursor)
		Select (lcCursor)
		Scan
			lnSuma = suma
			lcRand = Iif(id_jtva_coloana = 1003, '30', '16')
			Select cDecont
			Replace TVA With TVA - lnSuma For Rand = lcRand
			Select (lcCursor)
		Endscan
	Endif
	Use In (Select(lcCursor))

	*** SOLD NEEXIGIBIL
	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO9NT+RO5NT) as tvan
		from vjc2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnAchizitieBazaN = Round(bazan, 0)
		pnAchizitieTVAN = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))

	Text To lcSql Noshow Textmerge
	select sum(RO24NB+RO9NB+RO5NB) as bazan, sum(RO24NT+RO9NT+RO5NT) as tvan
		from vjv2010
		where an = ?gnAn and luna = ?pnLunaF <<m.gcCondSucursala>>
	Endtext
	llSucces = goExecutor.oExecuta(m.lcSql, "cNeexTemp")
	If m.llSucces
		pnLivrareBazaN = Round(bazan, 0)
		pnLivrareTVAN = Round(tvan, 0)
	Endif
	Use In (Select('cNeexTemp'))


	Select cDecont
	lodt = Createobject('frm_decontTVA', m.tnNumarLuni)
	lodt.nROTN = m.lnROTN && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001
	lodt.lblRand28.Caption = lodt.lblRand28.Caption + ' (' + Alltrim(Str(m.lnROTN)) + ' lei)'
	lodt.Show(1)
Endproc && viz_decontTVA_0113


*===========================================
*!* 08.02.2012
*!* CORELARE D394, CEREALE
Procedure viz_decontTVA_0112
	Lparameters tnNumarLuni
	*Parameters tlTrim

	*** tnNumarLuni: 1 = o luna, 3 = 3 luni trimestrial, 6 = 6 luni semestrial, 12 = 12 luni anual

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec

	Local lodt As 'frm_decontTVA'
	Local lcCursor, lcSql, ldDataCurenta, ldDataPrecedenta, lnAnCurent, lnAnPrecedent, lnLunaCurenta
	Local lnLunaMin, lnLunaPrecedenta, lnPerioada, lnPlati, lnSucces, lnTP, lnTR, loEx As Exception, lnROTN
	*:Global pn10B, pn10T, pn11B, pn11T, pn13B, pn14B, pn15B, pn171B, pn171T, pn17B, pn17T, pn1911B
	*:Global pn1911T, pn191B, pn191T, pn19B, pn19T, pn1B, pn21B, pn21T, pn22B, pn22T, pn23B, pn23T
	*:Global pn241B, pn24B, pn31B, pn3B, pn51B, pn51T, pn5B, pn5T, pn711B, pn711T, pn71B, pn71T, pn7B
	*:Global pn7T, pn9B, pn9T, pnRegularizareB, pnRegularizareT

	lnAnCurent = m.gnAn
	lnLunaCurenta = m.gnLuna
	ldDataCurenta = Date(m.lnAnCurent, m.lnLunaCurenta, 1)
	lnROTN = 0

	* Luna de inceput a perioadei
	* 1 = gnLuna
	* 3 = 1,4,7,10
	* 6 = 1,7
	* 12 = 1
	lnPerioada = Ceil(m.lnLunaCurenta / tnNumarLuni)
	lnLunaMin = (lnPerioada - 1) * tnNumarLuni + 1

	*** luna pentru balanta precedenta
	ldDataPrecedenta = Gomonth(Date(m.lnAnCurent, m.lnLunaMin, 1), -1)
	lnLunaPrecedenta = Month(m.ldDataPrecedenta)
	lnAnPrecedent = Year(m.ldDataPrecedenta)
	&& ----
	pnLunaI = lnLunaMin
	pnLunaF = m.lnLunaCurenta

	pnLunaPrec = m.lnLunaPrecedenta
	pnAnPrec = m.lnAnPrecedent

	Private poDecont
	Store '' To poDecont

	Private pnProRata
	pnProRata = 100
	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
		[where valabil = '0112' order by id_decont]
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif
	*** D394
	*** randurile 17.1 si 27.1 (facturi livrari si achizitii din RO catre persoane juridice platitoare de TVA)
	Local loD394 As "D394"
	Try
		loD394 = Createobject("D394") && ovanzcump.vcx
		loD394.do_executa(.T., m.tnNumarLuni)
	Catch To loEx
		loD394 = Createobject("custom")
		loD394.AddProperty("livb", 0)
		loD394.AddProperty("livt", 0)
		loD394.AddProperty("livtib", 0)
		loD394.AddProperty("livtit", 0)
		loD394.AddProperty("achb", 0)
		loD394.AddProperty("acht", 0)
		loD394.AddProperty("achtib", 0)
		loD394.AddProperty("achtit", 0)
		loD394.AddProperty("Total", 0)
		loD394.AddProperty("rectificativa", 0)
		loD394.AddProperty("TIP", 'L')
		loD394.AddProperty("Perioada", 1)
		loD394.AddProperty("NumarCoduriFiscale", 0)
		amessagebox('Eroare calcul D394 pentru randurile 17.1 si 27.1' + CRLF + loEx.Message + ' ' + Transform(loEx.ErrorNo), 0 + 48, _Screen.Caption)
	Endtry


	lcSql = [SELECT ROUND(SUM(RO19B),0) AS RO19B, ] + ;
		[ ROUND(SUM(RO19T),0) AS RO19T, ] + ;
		[ ROUND(SUM(RO9B),0) AS RO9B, ] + ;
		[ ROUND(SUM(RO9T),0) AS RO9T,] + ;
		[ ROUND(SUM(RO5B),0) AS RO5B, ] + ;
		[ ROUND(SUM(RO5T),0) AS RO5T, ] + ;
		[ ROUND(SUM(RO24B),0) AS RO24B, ] + ;
		[ ROUND(SUM(RO24T),0) AS RO24T,] + ;
		[ ROUND(SUM(ROTI),0) AS ROTI, ] + ;
		[ ROUND(SUM(CESCDD1),0) AS CESCDD1,] + ;
		[ ROUND(SUM(CESCDD2),0) AS CESCDD2,] + ;
		[ ROUND(SUM(CEOPTR),0) AS CEOPTR,] + ;
		[ ROUND(SUM(CESVDD),0) AS CESVDD,] + ;
		[ ROUND(SUM(CESVFDD),0) AS CESVFDD,] + ;
		[ ROUND(SUM(CESVFS),0) AS CESVFS,] + ;
		[ ROUND(SUM(WRSCDD),0) AS WRSCDD, ] + ;
		[ ROUND(SUM(FODD),0) AS FODD, ] + ;
		[ ROUND(SUM(FOFDD),0) AS FOFDD, ] + ;
		[ ROUND(SUM(CEOPTR),0) AS CEOPTR, ] + ;
		[ ROUND(SUM(CESVDD),0) AS CESVDD, ] + ;
		[ ROUND(SUM(CESVFDD),0) AS CESVFDD, ] + ;
		[ ROUND(SUM(CESVFS),0) AS CESVFS, ] + ;
		[ ROUND(SUM(WRSCFDD),0) AS WRSCFDD,] + ;
		[ ROUND(SUM(WRN),0) AS WRN FROM JV2007 ] + ;
		[ WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala


	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	Go Top
	pn1B = Nvl(cescdd1, 0) + Nvl(ceoptr, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0) + Nvl(cesvdd, 0) + Nvl(cesvfdd, 0) + Nvl(cesvfs, 0) + Nvl(cesvfs, 0)
	pn31B = Nvl(cesvfs, 0)

	pn9B = Nvl(ro24b, 0)
	pn9T = Nvl(ro24t, 0)
	pn10B = Nvl(ro9b, 0)
	pn10T = Nvl(ro9t, 0)
	pn11B = Nvl(ro5b, 0)
	pn11T = Nvl(ro5t, 0)
	pnRegularizareB = Nvl(ro19b, 0)
	pnRegularizareT = Nvl(ro19t, 0)

	pn13B = Nvl(roti, 0)
	pn14B = Nvl(wrscdd, 0)

	pn15B = Nvl(wrscfdd, 0)

	*** LIVRARI D394
	pn171B = Nvl(loD394.livb + loD394.livtib, 0)
	pn171T = Nvl(loD394.livt + loD394.livtit, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '3.1'
	If Found()
		Replace valoare With pn31B
	Endif

	Locate For Alltrim(Rand) = '9'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '10'
	If Found()
		Replace valoare With pn10B, TVA With pn10T
	Endif

	Locate For Alltrim(Rand) = '11'
	If Found()
		Replace valoare With pn11B, TVA With pn11T
	Endif

	Locate For Alltrim(Rand) = '13'
	If Found()
		Replace valoare With pn13B
	Endif

	Locate For Alltrim(Rand) = '14'
	If Found()
		Replace valoare With pn14B
	Endif

	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pn15B
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '16'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	*!* LIVRARI INTERNE CATRE CLIENTI PLATITORI TVA IN ROMANIA (LIVRARI D394)
	Locate For Alltrim(Rand) = '17.1'
	If Found()
		Replace valoare With pn171B, TVA With pn171T
	Endif


	lcSql = [SELECT Round(Sum(RO24B),0) As RO24B, ] + ;
		[ Round(Sum(RO24T),0) As RO24T, ] + ;
		[ ROUND(SUM(RO19BCB),0) AS RO19BCB, ] + ;
		[ ROUND(SUM(RO19BCT),0) AS RO19BCT, ] + ;
		[ ROUND(SUM(RO19BVB),0) AS RO19BVB, ] + ;
		[ ROUND(SUM(RO19BVT),0) AS RO19BVT, ] + ;
		[ ROUND(SUM(RO19BFB),0) AS RO19BFB, ] + ;
		[ ROUND(SUM(RO19BFT),0) AS RO19BFT, ] + ;
		[ ROUND(SUM(RO09BCB),0) AS RO09BCB, ] + ;
		[ ROUND(SUM(RO09BCT),0) AS RO09BCT, ] + ;
		[ ROUND(SUM(RO09BVB),0) AS RO09BVB, ] + ;
		[ ROUND(SUM(RO09BVT),0) AS RO09BVT, ] + ;
		[ ROUND(SUM(RO09BFB),0) AS RO09BFB, ] + ;
		[ ROUND(SUM(RO09BFT),0) AS RO09BFT, ] + ;
		[ ROUND(SUM(RO05B),0) AS RO05B, ] + ;
		[ ROUND(SUM(RO05T),0) AS RO05T, ] + ;
		[ ROUND(SUM(ROSCN),0) AS ROSCN, ] + ;
		[ Round(Sum(FO24B),0) As FO24B, ] + ;
		[ Round(Sum(FO24T),0) As FO24T, ] + ;
		[ ROUND(SUM(FO19BCB),0) AS FO19BCB, ] + ;
		[ ROUND(SUM(FO19BCT),0) AS FO19BCT, ] + ;
		[ ROUND(SUM(FO19BVB),0) AS FO19BVB, ] + ;
		[ ROUND(SUM(FO19BVT),0) AS FO19BVT, ] + ;
		[ ROUND(SUM(FO19BFB),0) AS FO19BFB, ] + ;
		[ ROUND(SUM(FO19BFT),0) AS FO19BFT, ] + ;
		[ ROUND(SUM(FO09BCB),0) AS FO09BCB, ] + ;
		[ ROUND(SUM(FO09BCT),0) AS FO09BCT, ] + ;
		[ ROUND(SUM(FO09BVB),0) AS FO09BVB, ] + ;
		[ ROUND(SUM(FO09BVT),0) AS FO09BVT, ] + ;
		[ ROUND(SUM(FO09BFB),0) AS FO09BFB, ] + ;
		[ ROUND(SUM(FO09BFT),0) AS FO09BFT, ] + ;
		[ ROUND(SUM(FOSCN),0) AS FOSCN, ] + ;
		[ Round(Sum(CE24B),0) As CE24B, ] + ;
		[ Round(Sum(CE24T),0) As CE24T, ] + ;
		[ Round(Sum(CE24SB),0) As CE24SB, ] + ;
		[ Round(Sum(CE24ST),0) As CE24ST, ] + ;
		[ Round(Sum(CE5SB),0) As CE5SB, ] + ;
		[ Round(Sum(CE5ST),0) As CE5ST, ] + ;
		[ Round(Sum(CE9SB),0) As CE9SB, ] + ;
		[ Round(Sum(CE9ST),0) As CE9ST, ] + ;
		[ ROUND(SUM(CEBCB),0) AS CEBCB, ] + ;
		[ ROUND(SUM(CEBCT),0) AS CEBCT, ] + ;
		[ ROUND(SUM(CEBVB),0) AS CEBVB, ] + ;
		[ ROUND(SUM(CEBVT),0) AS CEBVT, ] + ;
		[ ROUND(SUM(CEBVSC),0) AS CEBVSC, ] + ;
		[ ROUND(SUM(CEBVN),0) AS CEBVN, ] + ;
		[ ROUND(SUM(CEBFB),0) AS CEBFB, ] + ;
		[ ROUND(SUM(CEBFT),0) AS CEBFT, ] + ;
		[ ROUND(SUM(CEBFSC),0) AS CEBFSC, ] + ;
		[ ROUND(SUM(CEBFN),0) AS CEBFN, ] + ;
		[ Round(Sum(TI24B), 0) As TI24B, ] + ;
		[ Round(Sum(TI24T), 0) As TI24T, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBWR, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTWR, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBWR, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTWR, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBWR, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTWR, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBWR, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTWR, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBWR, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTWR, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBRO, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTRO, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBRO, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTRO, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBRO, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTRO, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBRO, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTRO, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBRO, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTRO, ] + ;
		[ ROUND(SUM(ROTN),0) AS ROTN ] + ;
		[ FROM JC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC
	Go Top
	pn18B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0) + Nvl(ce24b, 0)
	pn18T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0) + Nvl(ce24t, 0)
	pn181B = pn18B
	pn181T = pn18T
	pn5B = pn18B
	pn5T = pn18T
	pn51B = pn181B
	pn51T = pn181T

	pn22B = Nvl(ro24b, 0) + Nvl(fo24b, 0)
	pn22T = Nvl(ro24t, 0) + Nvl(fo24t, 0)

	pn23B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0)
	pn23T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0)

	pn24B = Nvl(ro05b, 0)
	pn24T = Nvl(ro05t, 0)

	pn25B = Nvl(ti24b, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0)
	pn25T = Nvl(ti24t, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)
	pn12B = pn25B
	pn12T = pn25T
	pn26B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0)
	pn261B = Nvl(cebvsc, 0) + Nvl(cebfsc, 0)

	*** rd.19=rd.6 ???
	pnRegularizareB = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0)
	pnRegularizareT = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0)


	pn20B = Nvl(CE24SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn20T = Nvl(CE24ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn201B = Nvl(CE24SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn201T = Nvl(CE24ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)
	pn7B = pn20B
	pn7T = pn20T
	pn71B = pn201B
	pn71T = pn201T

	*** ACHIZITII D394
	pn271B = Nvl(loD394.achb + loD394.achtib, 0)
	pn271T = Nvl(loD394.acht + loD394.achtit, 0)

	lnROTN = Nvl(ROTN, 0) &&  && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001

	pnRegularizareB = pnRegularizareB + Nvl(ti19bcbwr, 0) + Nvl(ti19bvbwr, 0) + Nvl(ti19bfbwr, 0) + Nvl(ti09bvbwr, 0) + Nvl(ti09bfbwr, 0)
	pnRegularizareT = pnRegularizareT + Nvl(ti19bctwr, 0) + Nvl(ti19bvtwr, 0) + Nvl(ti19bftwr, 0) + Nvl(ti09bvtwr, 0) + Nvl(ti09bftwr, 0)

	Select cDecont
	Locate For Alltrim(Rand) = '18'
	If Found()
		Replace valoare With pn18B, TVA With pn18T
	Endif

	Locate For Alltrim(Rand) = '18.1'
	If Found()
		Replace valoare With pn181B, TVA With pn181T
	Endif

	Locate For Alltrim(Rand) = '5'
	If Found()
		Replace valoare With pn5B, TVA With pn5T
	Endif

	Locate For Alltrim(Rand) = '5.1'
	If Found()
		Replace valoare With pn51B, TVA With pn51T
	Endif

	Locate For Alltrim(Rand) = '20'
	If Found()
		Replace valoare With pn20B, TVA With pn20T
	Endif

	Locate For Alltrim(Rand) = '20.1'
	If Found()
		Replace valoare With pn201B, TVA With pn201T
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '7.1'
	If Found()
		Replace valoare With pn71B, TVA With pn71T
	Endif

	Locate For Alltrim(Rand) = '22'
	If Found()
		Replace valoare With pn22B, TVA With pn22T
	Endif

	Locate For Alltrim(Rand) = '23'
	If Found()
		Replace valoare With pn23B, TVA With pn23T
	Endif

	Locate For Alltrim(Rand) = '24'
	If Found()
		Replace valoare With pn24B, TVA With pn24T
	Endif

	Locate For Alltrim(Rand) = '25'
	If Found()
		Replace valoare With pn25B, TVA With pn25T
	Endif

	Locate For Alltrim(Rand) = '12'
	If Found()
		Replace valoare With pn12B, TVA With pn12T
	Endif

	Locate For Alltrim(Rand) = '26'
	If Found()
		Replace valoare With pn26B
	Endif

	Locate For Alltrim(Rand) = '26.1'
	If Found()
		Replace valoare With pn261B
	Endif

	Locate For Alltrim(Rand) = '27.1'
	If Found()
		Replace valoare With pn271B, TVA With pn271T
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '30'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala


	lcCursor = [crsTR] && TVA de recuperat
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTR
	lnTR = solddeb

	Select cDecont
	Locate For Alltrim(Rand) = '38'
	If Found()
		Replace TVA With lnTR
	Endif

	If Used('crsTR')
		Use In crsTR
	Endif


	***----
	lcSql = [SELECT NVL(soldcred,0) as  soldcred ] + ;
		[ from vbal ] + ;
		[ where cont = '4423' ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec ] + gcCondSucursala

	lcCursor = [crsTP] && nu si-au platit tva din luna precedenta
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTP
	lnTP = soldcred

	If Used('crsTP')
		Use In crsTP
	Endif

	lcSql = [ select  nvl(sum(suma),0) as suma ] + ;
		[from act ] + ;
		[where an=?gnAn ] + ;
		[and luna=?gnLuna ] + ;
		[and sters=0 ] + ;
		[and scd='4423' and scc in  ('5121','5311')] + gcCondSucursala

	lcCursor = [crsPlati]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsPlati
	lnPlati = suma

	If Used('crsPlati')
		Use In crsPlati
	Endif

	lnTP = lnTP - lnPlati

	Select cDecont
	Locate For Alltrim(Rand) = '35'
	If Found()
		Replace TVA With Nvl(lnTP, 0)
	Endif

	lcSql = [select sum(suma) as suma, id_jtva_coloana from act ] + ;
		[where sters = 0 and id_jtva_coloana in (1002, 1003) and ] + ;
		[an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala + ;
		[ group by id_jtva_coloana order by id_jtva_coloana]
	lcCursor = [crsRegTaxe]
	If goExecutor.oExecuta(lcSql, lcCursor)
		Select (lcCursor)
		Scan
			lnSuma = suma
			lcRand = Iif(id_jtva_coloana = 1003, '30', '16')
			Select cDecont
			Replace TVA With TVA - lnSuma For Rand = lcRand
			Select (lcCursor)
		Endscan
	Endif
	Use In (Select(lcCursor))

	***----

	Select cDecont
	lodt = Createobject('frm_decontTVA', m.tnNumarLuni)
	lodt.nROTN = m.lnROTN && LIMITARE DEDUCERE TVA 4426 ID_JTVA = 1001
	lodt.lblRand28.Caption = lodt.lblRand28.Caption + ' (' + Alltrim(Str(m.lnROTN)) + ' lei)'
	lodt.Show(1)
Endproc && viz_decontTVA_0112


*===========================================
*!* 09.08.2010
*!* TVA 24%
Procedure viz_decontTVA_0710
	Lparameters tnNumarLuni
	*Parameters tlTrim

	*** tnNumarLuni: 1 = o luna, 3 = 3 luni trimestrial, 6 = 6 luni semestrial, 12 = 12 luni anual

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec


	*** JC, JV lunile precedente
	lnAnCurent = m.gnAn
	lnLunaCurenta = m.gnLuna
	ldDataCurenta = Date(m.lnAnCurent, m.lnLunaCurenta, 1)

	* Luna de inceput a perioadei
	* 1 = gnLuna
	* 3 = 1,4,7,10
	* 6 = 1,7
	* 12 = 1
	lnPerioada = Ceil(m.lnLunaCurenta / tnNumarLuni)
	lnLunaMin = (lnPerioada - 1) * tnNumarLuni + 1

	*** luna pentru balanta precedenta
	ldDataPrecedenta = Gomonth(Date(m.lnAnCurent, m.lnLunaMin, 1), -1)
	lnLunaPrecedenta = Month(m.ldDataPrecedenta)
	lnAnPrecedent = Year(m.ldDataPrecedenta)
	&& ----
	pnLunaI = lnLunaMin
	pnLunaF = m.lnLunaCurenta

	pnLunaPrec = m.lnLunaPrecedenta
	pnAnPrec = m.lnAnPrecedent

	Private poDecont
	Store '' To poDecont

	Private pnProRata
	pnProRata = 100
	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
		[where valabil = '0111' order by id_decont]
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	lcSql = [SELECT ROUND(SUM(RO19B),0) AS RO19B, ] + ;
		[ ROUND(SUM(RO19T),0) AS RO19T, ] + ;
		[ ROUND(SUM(RO9B),0) AS RO9B, ] + ;
		[ ROUND(SUM(RO9T),0) AS RO9T,] + ;
		[ ROUND(SUM(RO5B),0) AS RO5B, ] + ;
		[ ROUND(SUM(RO5T),0) AS RO5T, ] + ;
		[ ROUND(SUM(RO24B),0) AS RO24B, ] + ;
		[ ROUND(SUM(RO24T),0) AS RO24T,] + ;
		[ ROUND(SUM(ROTI),0) AS ROTI, ] + ;
		[ ROUND(SUM(CESCDD1),0) AS CESCDD1,] + ;
		[ ROUND(SUM(CESCDD2),0) AS CESCDD2,] + ;
		[ ROUND(SUM(CEOPTR),0) AS CEOPTR,] + ;
		[ ROUND(SUM(CESVDD),0) AS CESVDD,] + ;
		[ ROUND(SUM(CESVFDD),0) AS CESVFDD,] + ;
		[ ROUND(SUM(CESVFS),0) AS CESVFS,] + ;
		[ ROUND(SUM(WRSCDD),0) AS WRSCDD, ] + ;
		[ ROUND(SUM(FODD),0) AS FODD, ] + ;
		[ ROUND(SUM(FOFDD),0) AS FOFDD, ] + ;
		[ ROUND(SUM(CEOPTR),0) AS CEOPTR, ] + ;
		[ ROUND(SUM(CESVDD),0) AS CESVDD, ] + ;
		[ ROUND(SUM(CESVFDD),0) AS CESVFDD, ] + ;
		[ ROUND(SUM(CESVFS),0) AS CESVFS, ] + ;
		[ ROUND(SUM(WRSCFDD),0) AS WRSCFDD,] + ;
		[ ROUND(SUM(WRN),0) AS WRN FROM JV2007 ] + ;
		[ WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala


	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	*!*	modificare	v 2.0.126
	*!*		pn1B = Nvl(cescdd1,0)
	*!*		pn3B = Nvl(cescdd2,0) + Nvl(fodd,0) + Nvl(fofdd,0)
	pn1B = Nvl(cescdd1, 0) + Nvl(ceoptr, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0) + Nvl(cesvdd, 0) + Nvl(cesvfdd, 0) + Nvl(cesvfs, 0) + Nvl(cesvfs, 0)
	pn31B = Nvl(cesvfs, 0)
	*!*	modificare	v 2.0.126	^
	pn9B = Nvl(ro24b, 0)
	pn9T = Nvl(ro24t, 0)
	pn10B = Nvl(ro9b, 0)
	pn10T = Nvl(ro9t, 0)
	pn11B = Nvl(ro5b, 0)
	pn11T = Nvl(ro5t, 0)
	pnRegularizareB = Nvl(ro19b, 0)
	pnRegularizareT = Nvl(ro19t, 0)

	pn13B = Nvl(roti, 0) + Nvl(wrscdd, 0)
	pn14B = Nvl(wrscfdd, 0) 	&&+ NVL(wrscfdd,0)

	Select cDecont

	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	*!*	modificare	v 2.0.126
	Locate For Alltrim(Rand) = '3.1'
	If Found()
		Replace valoare With pn31B
	Endif
	*!*	modificare	v 2.0.126	^
	Locate For Alltrim(Rand) = '9' && '7'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '10' && '8'
	If Found()
		Replace valoare With pn10B, TVA With pn10T
	Endif

	Locate For Alltrim(Rand) = '11' && '9'
	If Found()
		Replace valoare With pn11B, TVA With pn11T
	Endif

	Locate For Alltrim(Rand) = '13' && '11'
	If Found()
		Replace valoare With pn13B
	Endif

	Locate For Alltrim(Rand) = '14' && '12'
	If Found()
		Replace valoare With pn14B
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	lcSql = [SELECT Round(Sum(RO24B),0) As RO24B, ] + ;
		[ Round(Sum(RO24T),0) As RO24T, ] + ;
		[ ROUND(SUM(RO19BCB),0) AS RO19BCB, ] + ;
		[ ROUND(SUM(RO19BCT),0) AS RO19BCT, ] + ;
		[ ROUND(SUM(RO19BVB),0) AS RO19BVB, ] + ;
		[ ROUND(SUM(RO19BVT),0) AS RO19BVT, ] + ;
		[ ROUND(SUM(RO19BFB),0) AS RO19BFB, ] + ;
		[ ROUND(SUM(RO19BFT),0) AS RO19BFT, ] + ;
		[ ROUND(SUM(RO09BCB),0) AS RO09BCB, ] + ;
		[ ROUND(SUM(RO09BCT),0) AS RO09BCT, ] + ;
		[ ROUND(SUM(RO09BVB),0) AS RO09BVB, ] + ;
		[ ROUND(SUM(RO09BVT),0) AS RO09BVT, ] + ;
		[ ROUND(SUM(RO09BFB),0) AS RO09BFB, ] + ;
		[ ROUND(SUM(RO09BFT),0) AS RO09BFT, ] + ;
		[ ROUND(SUM(RO05B),0) AS RO05B, ] + ;
		[ ROUND(SUM(RO05T),0) AS RO05T, ] + ;
		[ ROUND(SUM(ROSCN),0) AS ROSCN, ] + ;
		[ Round(Sum(FO24B),0) As FO24B, ] + ;
		[ Round(Sum(FO24T),0) As FO24T, ] + ;
		[ ROUND(SUM(FO19BCB),0) AS FO19BCB, ] + ;
		[ ROUND(SUM(FO19BCT),0) AS FO19BCT, ] + ;
		[ ROUND(SUM(FO19BVB),0) AS FO19BVB, ] + ;
		[ ROUND(SUM(FO19BVT),0) AS FO19BVT, ] + ;
		[ ROUND(SUM(FO19BFB),0) AS FO19BFB, ] + ;
		[ ROUND(SUM(FO19BFT),0) AS FO19BFT, ] + ;
		[ ROUND(SUM(FO09BCB),0) AS FO09BCB, ] + ;
		[ ROUND(SUM(FO09BCT),0) AS FO09BCT, ] + ;
		[ ROUND(SUM(FO09BVB),0) AS FO09BVB, ] + ;
		[ ROUND(SUM(FO09BVT),0) AS FO09BVT, ] + ;
		[ ROUND(SUM(FO09BFB),0) AS FO09BFB, ] + ;
		[ ROUND(SUM(FO09BFT),0) AS FO09BFT, ] + ;
		[ ROUND(SUM(FOSCN),0) AS FOSCN, ] + ;
		[ Round(Sum(CE24B),0) As CE24B, ] + ;
		[ Round(Sum(CE24T),0) As CE24T, ] + ;
		[ Round(Sum(CE24SB),0) As CE24SB, ] + ;
		[ Round(Sum(CE24ST),0) As CE24ST, ] + ;
		[ Round(Sum(CE5SB),0) As CE5SB, ] + ;
		[ Round(Sum(CE5ST),0) As CE5ST, ] + ;
		[ Round(Sum(CE9SB),0) As CE9SB, ] + ;
		[ Round(Sum(CE9ST),0) As CE9ST, ] + ;
		[ ROUND(SUM(CEBCB),0) AS CEBCB, ] + ;
		[ ROUND(SUM(CEBCT),0) AS CEBCT, ] + ;
		[ ROUND(SUM(CEBVB),0) AS CEBVB, ] + ;
		[ ROUND(SUM(CEBVT),0) AS CEBVT, ] + ;
		[ ROUND(SUM(CEBVSC),0) AS CEBVSC, ] + ;
		[ ROUND(SUM(CEBVN),0) AS CEBVN, ] + ;
		[ ROUND(SUM(CEBFB),0) AS CEBFB, ] + ;
		[ ROUND(SUM(CEBFT),0) AS CEBFT, ] + ;
		[ ROUND(SUM(CEBFSC),0) AS CEBFSC, ] + ;
		[ ROUND(SUM(CEBFN),0) AS CEBFN, ] + ;
		[ Round(Sum(TI24B), 0) As TI24B, ] + ;
		[ Round(Sum(TI24T), 0) As TI24T, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBWR, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTWR, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBWR, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTWR, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBWR, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTWR, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBWR, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTWR, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBWR, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTWR, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBRO, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTRO, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBRO, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTRO, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBRO, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTRO, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBRO, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTRO, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBRO, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTRO ] + ;
		[ FROM JC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC

	pn21B = Nvl(ro24b, 0) + Nvl(fo24b, 0)
	pn21T = Nvl(ro24t, 0) + Nvl(fo24t, 0)

	pn22B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0)
	pn22T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0)

	pn23B = Nvl(ro05b, 0)
	pn23T = Nvl(ro05t, 0)
	pn24B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0)
	pn241B = Nvl(cebvsc, 0) + Nvl(cebfsc, 0)

	pnRegularizareB = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0)
	pnRegularizareT = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0)


	pn17B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0) + Nvl(ce24b, 0)
	pn17T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0) + Nvl(ce24t, 0)

	pn171B = pn17B
	pn171T = pn17T

	pn19B = Nvl(ti24b, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) + Nvl(CE24SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn19T = Nvl(ti24t, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0) + Nvl(CE24ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)

	pn191B = Nvl(CE24SB, 0) + Nvl(CE9SB, 0) + Nvl(CE5SB, 0)
	pn191T = Nvl(CE24ST, 0) + Nvl(CE9ST, 0) + Nvl(CE5ST, 0)

	pn1911B = pn191B
	pn1911T = pn191T

	pn5B = pn17B
	pn5T = pn17T
	pn51B = pn171B
	pn51T = pn171T
	pn7B = pn19B
	pn7T = pn19T
	pn71B = pn191B
	pn71T = pn191T
	pn711B = pn1911B
	pn711T = pn1911T

	pnRegularizareB = pnRegularizareB + Nvl(ti19bcbwr, 0) + Nvl(ti19bvbwr, 0) + Nvl(ti19bfbwr, 0) + Nvl(ti09bvbwr, 0) + Nvl(ti09bfbwr, 0)
	pnRegularizareT = pnRegularizareT + Nvl(ti19bctwr, 0) + Nvl(ti19bvtwr, 0) + Nvl(ti19bftwr, 0) + Nvl(ti09bvtwr, 0) + Nvl(ti09bftwr, 0)

	Select cDecont

	Locate For Alltrim(Rand) = '21'
	If Found()
		Replace valoare With pn21B, TVA With pn21T
	Endif

	Locate For Alltrim(Rand) = '22'
	If Found()
		Replace valoare With pn22B, TVA With pn22T
	Endif

	Locate For Alltrim(Rand) = '23'
	If Found()
		Replace valoare With pn23B, TVA With pn23T
	Endif

	Locate For Alltrim(Rand) = '24'
	If Found()
		Replace valoare With pn24B
	Endif


	Locate For Alltrim(Rand) = '24.1'
	If Found()
		Replace valoare With pn241B
	Endif

	Locate For Alltrim(Rand) = '17'
	If Found()
		Replace valoare With pn17B, TVA With pn17T
	Endif

	Locate For Alltrim(Rand) = '17.1'
	If Found()
		Replace valoare With pn171B, TVA With pn171T
	Endif

	Locate For Alltrim(Rand) = '5'
	If Found()
		Replace valoare With pn5B, TVA With pn5T
	Endif

	Locate For Alltrim(Rand) = '5.1'
	If Found()
		Replace valoare With pn51B, TVA With pn51T
	Endif

	Locate For Alltrim(Rand) = '19'
	If Found()
		Replace valoare With pn19B, TVA With pn19T
	Endif

	Locate For Alltrim(Rand) = '19.1'
	If Found()
		Replace valoare With pn191B, TVA With pn191T
	Endif

	Locate For Alltrim(Rand) = '19.1.1'
	If Found()
		Replace valoare With pn1911B, TVA With pn1911T
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '7.1'
	If Found()
		Replace valoare With pn71B, TVA With pn71T
	Endif

	Locate For Alltrim(Rand) = '7.1.1'
	If Found()
		Replace valoare With pn711B, TVA With pn711T
	Endif

	*!* REGULARIZARE 19%
	Locate For Alltrim(Rand) = '28'
	If Found()
		Replace valoare With pnRegularizareB, TVA With pnRegularizareT
	Endif

	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala


	lcCursor = [crsTR] && TVA de recuperat
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTR
	lnTR = solddeb

	Select cDecont
	Locate For Alltrim(Rand) = '35'
	If Found()
		Replace TVA With lnTR
	Endif

	If Used('crsTR')
		Use In crsTR
	Endif


	***----
	lcSql = [SELECT NVL(soldcred,0) as  soldcred ] + ;
		[ from vbal ] + ;
		[ where cont = '4423' ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec ] + gcCondSucursala

	lcCursor = [crsTP] && nu si-au platit tva din luna precedenta
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTP
	lnTP = soldcred

	If Used('crsTP')
		Use In crsTP
	Endif

	lcSql = [ select  nvl(sum(suma),0) as suma ] + ;
		[from act ] + ;
		[where an=?gnAn ] + ;
		[and luna=?gnLuna ] + ;
		[and sters=0 ] + ;
		[and scd='4423' and scc in  ('5121','5311')] + gcCondSucursala

	lcCursor = [crsPlati]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsPlati
	lnPlati = suma

	If Used('crsPlati')
		Use In crsPlati
	Endif

	lnTP = lnTP - lnPlati

	Select cDecont
	Locate For Alltrim(Rand) = '33'
	If Found()
		Replace TVA With Nvl(lnTP, 0)
	Endif


	***----


	Select cDecont
	lodt = Createobject('frm_decontTVA', m.tnNumarLuni)
	lodt.Show(1)
Endproc && viz_decontTVA_0710




*===========================================
Procedure viz_decontTVA_0110
	Parameters tlTrim

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec
	Local llTrim

	llTrim = tlTrim

	If llTrim
		Do Case
			Case Inlist(gnLuna, 1, 2, 3)
				pnLunaI = 1
				pnLunaF = 3
				pnLunaPrec = 12
				pnAnPrec = gnAn - 1
			Case Inlist(gnLuna, 4, 5, 6)
				pnLunaI = 4
				pnLunaF = 6
				pnLunaPrec = 3
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 7, 8, 9)
				pnLunaI = 7
				pnLunaF = 9
				pnLunaPrec = 6
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 10, 11, 12)
				pnLunaI = 10
				pnLunaF = 12
				pnLunaPrec = 9
				pnAnPrec = gnAn
		Endcase
	Else
		pnLunaI = gnLuna
		pnLunaF = gnLuna
		If gnLuna = 1
			pnLunaPrec = 12
			pnAnPrec = gnAn - 1
		Else
			pnLunaPrec = gnLuna - 1
			pnAnPrec = gnAn
		Endif
	Endif


	Private poDecont
	Store '' To poDecont

	Private pnProRata
	pnProRata = 100
	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
		[where valabil = '0110' order by id_decont]
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif


	lcSql = [SELECT ROUND(SUM(RO19B),0) AS RO19B, ] + ;
		[ ROUND(SUM(RO19T),0) AS RO19T, ] + ;
		[ ROUND(SUM(RO9B),0) AS RO9B, ] + ;
		[ ROUND(SUM(RO9T),0) AS RO9T,] + ;
		[ ROUND(SUM(RO5B),0) AS RO5B, ] + ;
		[ ROUND(SUM(RO5T),0) AS RO5T, ] + ;
		[ ROUND(SUM(ROTI),0) AS ROTI, ] + ;
		[ ROUND(SUM(CESCDD1),0) AS CESCDD1,] + ;
		[ ROUND(SUM(CESCDD2),0) AS CESCDD2,] + ;
		[ ROUND(SUM(WRSCDD),0) AS WRSCDD, ] + ;
		[ ROUND(SUM(FODD),0) AS FODD, ] + ;
		[ ROUND(SUM(FOFDD),0) AS FOFDD, ] + ;
		[ ROUND(SUM(WRSCFDD),0) AS WRSCFDD,] + ;
		[ ROUND(SUM(WRN),0) AS WRN FROM JV2007 ] + ;
		[ WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	pn1B = Nvl(cescdd1, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0)
	pn7B = Nvl(ro19b, 0)
	pn7T = Nvl(ro19t, 0)
	pn8B = Nvl(ro9b, 0)
	pn8T = Nvl(ro9t, 0)
	pn9B = Nvl(ro5b, 0)
	pn9T = Nvl(ro5t, 0)

	pn11B = Nvl(roti, 0) + Nvl(wrscdd, 0)
	pn12B = Nvl(wrscfdd, 0) &&+ NVL(wrscfdd,0)

	Select cDecont

	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '9' && '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '10' && '8'
	If Found()
		Replace valoare With pn8B, TVA With pn8T
	Endif

	Locate For Alltrim(Rand) = '11' && '9'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '13' && '11'
	If Found()
		Replace valoare With pn11B
	Endif

	Locate For Alltrim(Rand) = '14' && '12'
	If Found()
		Replace valoare With pn12B
	Endif


	lcSql = [SELECT ROUND(SUM(RO19BCB),0) AS RO19BCB, ] + ;
		[ ROUND(SUM(RO19BCT),0) AS RO19BCT, ] + ;
		[ ROUND(SUM(RO19BVB),0) AS RO19BVB, ] + ;
		[ ROUND(SUM(RO19BVT),0) AS RO19BVT, ] + ;
		[ ROUND(SUM(RO19BFB),0) AS RO19BFB, ] + ;
		[ ROUND(SUM(RO19BFT),0) AS RO19BFT, ] + ;
		[ ROUND(SUM(RO09BCB),0) AS RO09BCB, ] + ;
		[ ROUND(SUM(RO09BCT),0) AS RO09BCT, ] + ;
		[ ROUND(SUM(RO09BVB),0) AS RO09BVB, ] + ;
		[ ROUND(SUM(RO09BVT),0) AS RO09BVT, ] + ;
		[ ROUND(SUM(RO09BFB),0) AS RO09BFB, ] + ;
		[ ROUND(SUM(RO09BFT),0) AS RO09BFT, ] + ;
		[ ROUND(SUM(ROSCN),0) AS ROSCN, ] + ;
		[ ROUND(SUM(FO19BCB),0) AS FO19BCB, ] + ;
		[ ROUND(SUM(FO19BCT),0) AS FO19BCT, ] + ;
		[ ROUND(SUM(FO19BVB),0) AS FO19BVB, ] + ;
		[ ROUND(SUM(FO19BVT),0) AS FO19BVT, ] + ;
		[ ROUND(SUM(FO19BFB),0) AS FO19BFB, ] + ;
		[ ROUND(SUM(FO19BFT),0) AS FO19BFT, ] + ;
		[ ROUND(SUM(FO09BCB),0) AS FO09BCB, ] + ;
		[ ROUND(SUM(FO09BCT),0) AS FO09BCT, ] + ;
		[ ROUND(SUM(FO09BVB),0) AS FO09BVB, ] + ;
		[ ROUND(SUM(FO09BVT),0) AS FO09BVT, ] + ;
		[ ROUND(SUM(FO09BFB),0) AS FO09BFB, ] + ;
		[ ROUND(SUM(FO09BFT),0) AS FO09BFT, ] + ;
		[ ROUND(SUM(RO05B),0) AS RO05B, ] + ;
		[ ROUND(SUM(RO05T),0) AS RO05T, ] + ;
		[ ROUND(SUM(FOSCN),0) AS FOSCN, ] + ;
		[ ROUND(SUM(CEBCB),0) AS CEBCB, ] + ;
		[ ROUND(SUM(CEBCT),0) AS CEBCT, ] + ;
		[ ROUND(SUM(CEBVB),0) AS CEBVB, ] + ;
		[ ROUND(SUM(CEBVT),0) AS CEBVT, ] + ;
		[ ROUND(SUM(CEBVSC),0) AS CEBVSC, ] + ;
		[ ROUND(SUM(CEBVN),0) AS CEBVN, ] + ;
		[ ROUND(SUM(CEBFB),0) AS CEBFB, ] + ;
		[ ROUND(SUM(CEBFT),0) AS CEBFT, ] + ;
		[ ROUND(SUM(CEBFSC),0) AS CEBFSC, ] + ;
		[ ROUND(SUM(CEBFN),0) AS CEBFN, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBWR, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTWR, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBWR, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTWR, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBWR, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTWR, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBWR, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTWR, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBWR, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTWR, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBRO, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTRO, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBRO, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTRO, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBRO, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTRO, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBRO, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTRO, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBRO, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTRO ] + ;
		[ FROM VJC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC

	pn18B = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0)

	pn18T = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0)


	pn19B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0) + ;
		Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0)

	pn19T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0) + ;
		Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)

	pn20B = Nvl(ro05b, 0)
	pn20T = Nvl(ro05t, 0)
	pn21B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0)



	pn15B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0)
	pn15T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0)
	pn4B = pn15B
	pn4T = pn15T
	pn10B = Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) &&pn17B + pn18B
	pn10T = Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0) && pn17T + pn18T

	pn17B = Nvl(ti19bcbwr, 0) + Nvl(ti19bvbwr, 0) + Nvl(ti19bfbwr, 0) + Nvl(ti09bvbwr, 0) + Nvl(ti09bfbwr, 0)
	pn17T = Nvl(ti19bctwr, 0) + Nvl(ti19bvtwr, 0) + Nvl(ti19bftwr, 0) + Nvl(ti09bvtwr, 0) + Nvl(ti09bftwr, 0)
	pn6B = pn17B
	pn6T = pn17T


	Select cDecont

	Locate For Alltrim(Rand) = '21' && '18'
	If Found()
		Replace valoare With pn18B, TVA With pn18T
	Endif

	Locate For Alltrim(Rand) = '22' && '19'
	If Found()
		Replace valoare With pn19B, TVA With pn19T
	Endif

	Locate For Alltrim(Rand) = '23' && '20'
	If Found()
		Replace valoare With pn20B, TVA With pn20T
	Endif


	Locate For Alltrim(Rand) = '24' && '21'
	If Found()
		Replace valoare With pn21B
	Endif

	Locate For Alltrim(Rand) = '17' && '15'
	If Found()
		Replace valoare With pn15B, TVA With pn15T
	Endif

	Locate For Alltrim(Rand) = '17.1' && '15.1'
	If Found()
		Replace valoare With pn15B, TVA With pn15T
	Endif

	Locate For Alltrim(Rand) = '5' && '4'
	If Found()
		Replace valoare With pn4B, TVA With pn4T
	Endif

	Locate For Alltrim(Rand) = '5.1' && '4.1'
	If Found()
		Replace valoare With pn4B, TVA With pn4T
	Endif

	*!*		Locate For Alltrim(Rand) = '9'
	*!*		If Found()
	*!*			Replace valoare With pn9B, tva With pn9T
	*!*		Endif

	Locate For Alltrim(Rand) = '19' && '17'
	If Found()
		Replace valoare With pn17B, TVA With pn17T
	Endif

	Locate For Alltrim(Rand) = '7' && '6'
	If Found()
		Replace valoare With pn6B, TVA With pn6T
	Endif

	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala


	lcCursor = [crsTR] && TVA de recuperat
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTR
	lnTR = solddeb

	Select cDecont
	Locate For Alltrim(Rand) = '35' && '32'
	If Found()
		Replace TVA With lnTR
	Endif

	If Used('crsTR')
		Use In crsTR
	Endif


	***----
	lcSql = [SELECT NVL(soldcred,0) as  soldcred ] + ;
		[ from vbal ] + ;
		[ where cont = '4423' ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec ] + gcCondSucursala

	lcCursor = [crsTP] && nu si-au platit tva din luna precedenta
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTP
	lnTP = soldcred

	If Used('crsTP')
		Use In crsTP
	Endif

	lcSql = [ select  nvl(sum(suma),0) as suma ] + ;
		[from act ] + ;
		[where an=?gnAn ] + ;
		[and luna=?gnLuna ] + ;
		[and sters=0 ] + ;
		[and scd='4423' and scc in  ('5121','5311')] + gcCondSucursala

	lcCursor = [crsPlati]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsPlati
	lnPlati = suma

	If Used('crsPlati')
		Use In crsPlati
	Endif

	lnTP = lnTP - lnPlati

	Select cDecont			&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	Locate For Alltrim(Rand) = '33' && '30'
	If Found()
		Replace TVA With Nvl(lnTP, 0)
	Endif


	***----


	Select cDecont
	lodt = Createobject('frm_decontTVA')
	lodt.Show(1)
Endproc && viz_decontTVA_0110


******************************************	sfarsit viz_decontTVA_0110 *******************************************
*===========================================
Procedure viz_decontTVA_1208
	Parameters tlTrim

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec
	Local llTrim

	llTrim = tlTrim

	If llTrim
		Do Case
			Case Inlist(gnLuna, 1, 2, 3)
				pnLunaI = 1
				pnLunaF = 3
				pnLunaPrec = 12
				pnAnPrec = gnAn - 1
			Case Inlist(gnLuna, 4, 5, 6)
				pnLunaI = 4
				pnLunaF = 6
				pnLunaPrec = 3
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 7, 8, 9)
				pnLunaI = 7
				pnLunaF = 9
				pnLunaPrec = 6
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 10, 11, 12)
				pnLunaI = 10
				pnLunaF = 12
				pnLunaPrec = 9
				pnAnPrec = gnAn
		Endcase
	Else
		pnLunaI = gnLuna
		pnLunaF = gnLuna
		If gnLuna = 1
			pnLunaPrec = 12
			pnAnPrec = gnAn - 1
		Else
			pnLunaPrec = gnLuna - 1
			pnAnPrec = gnAn
		Endif
	Endif


	Private poDecont
	Store '' To poDecont

	Private pnProRata
	pnProRata = 100
	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d ] + ;
		[where valabil = '1208' order by id_decont]
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif


	lcSql = [SELECT ROUND(SUM(RO19B),0) AS RO19B, ] + ;
		[ ROUND(SUM(RO19T),0) AS RO19T, ] + ;
		[ ROUND(SUM(RO9B),0) AS RO9B, ] + ;
		[ ROUND(SUM(RO9T),0) AS RO9T,] + ;
		[ ROUND(SUM(RO5B),0) AS RO5B, ] + ;
		[ ROUND(SUM(RO5T),0) AS RO5T, ] + ;
		[ ROUND(SUM(ROTI),0) AS ROTI, ] + ;
		[ ROUND(SUM(CESCDD1),0) AS CESCDD1,] + ;
		[ ROUND(SUM(CESCDD2),0) AS CESCDD2,] + ;
		[ ROUND(SUM(WRSCDD),0) AS WRSCDD, ] + ;
		[ ROUND(SUM(FODD),0) AS FODD, ] + ;
		[ ROUND(SUM(FOFDD),0) AS FOFDD, ] + ;
		[ ROUND(SUM(WRSCFDD),0) AS WRSCFDD,] + ;
		[ ROUND(SUM(WRN),0) AS WRN FROM JV2007 ] + ;
		[ WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	pn1B = Nvl(cescdd1, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0)
	pn7B = Nvl(ro19b, 0)
	pn7T = Nvl(ro19t, 0)
	pn8B = Nvl(ro9b, 0)
	pn8T = Nvl(ro9t, 0)
	pn9B = Nvl(ro5b, 0)
	pn9T = Nvl(ro5t, 0)

	pn11B = Nvl(roti, 0) + Nvl(wrscdd, 0)
	pn12B = Nvl(wrscfdd, 0) &&+ NVL(wrscfdd,0)

	Select cDecont

	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '8'
	If Found()
		Replace valoare With pn8B, TVA With pn8T
	Endif

	Locate For Alltrim(Rand) = '9'
	If Found()
		Replace valoare With pn9B, TVA With pn9T
	Endif

	Locate For Alltrim(Rand) = '11'
	If Found()
		Replace valoare With pn11B
	Endif

	Locate For Alltrim(Rand) = '12'
	If Found()
		Replace valoare With pn12B
	Endif


	lcSql = [SELECT ROUND(SUM(RO19BCB),0) AS RO19BCB, ] + ;
		[ ROUND(SUM(RO19BCT),0) AS RO19BCT, ] + ;
		[ ROUND(SUM(RO19BVB),0) AS RO19BVB, ] + ;
		[ ROUND(SUM(RO19BVT),0) AS RO19BVT, ] + ;
		[ ROUND(SUM(RO19BFB),0) AS RO19BFB, ] + ;
		[ ROUND(SUM(RO19BFT),0) AS RO19BFT, ] + ;
		[ ROUND(SUM(RO09BCB),0) AS RO09BCB, ] + ;
		[ ROUND(SUM(RO09BCT),0) AS RO09BCT, ] + ;
		[ ROUND(SUM(RO09BVB),0) AS RO09BVB, ] + ;
		[ ROUND(SUM(RO09BVT),0) AS RO09BVT, ] + ;
		[ ROUND(SUM(RO09BFB),0) AS RO09BFB, ] + ;
		[ ROUND(SUM(RO09BFT),0) AS RO09BFT, ] + ;
		[ ROUND(SUM(ROSCN),0) AS ROSCN, ] + ;
		[ ROUND(SUM(FO19BCB),0) AS FO19BCB, ] + ;
		[ ROUND(SUM(FO19BCT),0) AS FO19BCT, ] + ;
		[ ROUND(SUM(FO19BVB),0) AS FO19BVB, ] + ;
		[ ROUND(SUM(FO19BVT),0) AS FO19BVT, ] + ;
		[ ROUND(SUM(FO19BFB),0) AS FO19BFB, ] + ;
		[ ROUND(SUM(FO19BFT),0) AS FO19BFT, ] + ;
		[ ROUND(SUM(FO09BCB),0) AS FO09BCB, ] + ;
		[ ROUND(SUM(FO09BCT),0) AS FO09BCT, ] + ;
		[ ROUND(SUM(FO09BVB),0) AS FO09BVB, ] + ;
		[ ROUND(SUM(FO09BVT),0) AS FO09BVT, ] + ;
		[ ROUND(SUM(FO09BFB),0) AS FO09BFB, ] + ;
		[ ROUND(SUM(FO09BFT),0) AS FO09BFT, ] + ;
		[ ROUND(SUM(RO05B),0) AS RO05B, ] + ;
		[ ROUND(SUM(RO05T),0) AS RO05T, ] + ;
		[ ROUND(SUM(FOSCN),0) AS FOSCN, ] + ;
		[ ROUND(SUM(CEBCB),0) AS CEBCB, ] + ;
		[ ROUND(SUM(CEBCT),0) AS CEBCT, ] + ;
		[ ROUND(SUM(CEBVB),0) AS CEBVB, ] + ;
		[ ROUND(SUM(CEBVT),0) AS CEBVT, ] + ;
		[ ROUND(SUM(CEBVSC),0) AS CEBVSC, ] + ;
		[ ROUND(SUM(CEBVN),0) AS CEBVN, ] + ;
		[ ROUND(SUM(CEBFB),0) AS CEBFB, ] + ;
		[ ROUND(SUM(CEBFT),0) AS CEBFT, ] + ;
		[ ROUND(SUM(CEBFSC),0) AS CEBFSC, ] + ;
		[ ROUND(SUM(CEBFN),0) AS CEBFN, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBWR, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTWR, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBWR, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTWR, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBWR, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTWR, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBWR, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTWR, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBWR, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTWR, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBRO, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTRO, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBRO, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTRO, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBRO, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTRO, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBRO, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTRO, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBRO, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTRO ] + ;
		[ FROM VJC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC

	pn18B = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0)

	pn18T = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0)


	pn19B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0) + ;
		Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0)

	pn19T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0) + ;
		Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)

	pn20B = Nvl(ro05b, 0)
	pn20T = Nvl(ro05t, 0)
	pn21B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0)



	pn15B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0)
	pn15T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0)
	pn4B = pn15B
	pn4T = pn15T
	pn10B = Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) &&pn17B + pn18B
	pn10T = Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0) && pn17T + pn18T

	pn17B = Nvl(ti19bcbwr, 0) + Nvl(ti19bvbwr, 0) + Nvl(ti19bfbwr, 0) + Nvl(ti09bvbwr, 0) + Nvl(ti09bfbwr, 0)
	pn17T = Nvl(ti19bctwr, 0) + Nvl(ti19bvtwr, 0) + Nvl(ti19bftwr, 0) + Nvl(ti09bvtwr, 0) + Nvl(ti09bftwr, 0)
	pn6B = pn17B
	pn6T = pn17T


	Select cDecont

	Locate For Alltrim(Rand) = '18'
	If Found()
		Replace valoare With pn18B, TVA With pn18T
	Endif

	Locate For Alltrim(Rand) = '19'
	If Found()
		Replace valoare With pn19B, TVA With pn19T
	Endif

	Locate For Alltrim(Rand) = '20'
	If Found()
		Replace valoare With pn20B, TVA With pn20T
	Endif


	Locate For Alltrim(Rand) = '21'
	If Found()
		Replace valoare With pn21B
	Endif

	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pn15B, TVA With pn15T
	Endif

	Locate For Alltrim(Rand) = '15.1'
	If Found()
		Replace valoare With pn15B, TVA With pn15T
	Endif

	Locate For Alltrim(Rand) = '4'
	If Found()
		Replace valoare With pn4B, TVA With pn4T
	Endif

	Locate For Alltrim(Rand) = '4.1'
	If Found()
		Replace valoare With pn4B, TVA With pn4T
	Endif

	*!*		Locate For Alltrim(Rand) = '9'
	*!*		If Found()
	*!*			Replace valoare With pn9B, tva With pn9T
	*!*		Endif

	Locate For Alltrim(Rand) = '17'
	If Found()
		Replace valoare With pn17B, TVA With pn17T
	Endif

	Locate For Alltrim(Rand) = '6'
	If Found()
		Replace valoare With pn6B, TVA With pn6T
	Endif

	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala


	lcCursor = [crsTR] && TVA de recuperat
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTR
	lnTR = solddeb

	Select cDecont
	Locate For Alltrim(Rand) = '32'
	If Found()
		Replace TVA With lnTR
	Endif

	If Used('crsTR')
		Use In crsTR
	Endif


	***----
	lcSql = [SELECT NVL(soldcred,0) as  soldcred ] + ;
		[ from vbal ] + ;
		[ where cont = '4423' ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec ] + gcCondSucursala

	lcCursor = [crsTP] && nu si-au platit tva din luna precedenta
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTP
	lnTP = soldcred

	If Used('crsTP')
		Use In crsTP
	Endif

	lcSql = [ select  nvl(sum(suma),0) as suma ] + ;
		[from act ] + ;
		[where an=?gnAn ] + ;
		[and luna=?gnLuna ] + ;
		[and sters=0 ] + ;
		[and scd='4423' and scc in  ('5121','5311')] + gcCondSucursala

	lcCursor = [crsPlati]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsPlati
	lnPlati = suma

	If Used('crsPlati')
		Use In crsPlati
	Endif

	lnTP = lnTP - lnPlati

	Select cDecont			&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	Locate For Alltrim(Rand) = '30'
	If Found()
		Replace TVA With Nvl(lnTP, 0)
	Endif


	***----


	Select cDecont
	lodt = Createobject('frm_decontTVA')
	lodt.Show(1)
Endproc && viz_decontTVA_1208


******************************************	inceput viz_decontTVA_0507 *******************************************
Procedure viz_decontTVA_0507
	Parameters tlTrim

	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec
	Local llTrim

	*!*		If Mod(gnLuna,3)=0
	*!*			llTrim = .T.
	*!*		Else
	*!*			llTrim=.F.
	*!*		ENDIF

	llTrim = tlTrim

	If llTrim
		Do Case
			Case Inlist(gnLuna, 1, 2, 3)
				pnLunaI = 1
				pnLunaF = 3
				pnLunaPrec = 12
				pnAnPrec = gnAn - 1
			Case Inlist(gnLuna, 4, 5, 6)
				pnLunaI = 4
				pnLunaF = 6
				pnLunaPrec = 3
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 7, 8, 9)
				pnLunaI = 7
				pnLunaF = 9
				pnLunaPrec = 6
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 10, 11, 12)
				pnLunaI = 10
				pnLunaF = 12
				pnLunaPrec = 9
				pnAnPrec = gnAn
		Endcase
	Else
		pnLunaI = gnLuna
		pnLunaF = gnLuna
		If gnLuna = 1
			pnLunaPrec = 12
			pnAnPrec = gnAn - 1
		Else
			pnLunaPrec = gnLuna - 1
			pnAnPrec = gnAn
		Endif
	Endif


	Private poDecont
	Store '' To poDecont

	Private pnProRata
	pnProRata = 100
	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d where valabil = '0507' order by id_decont]
	lcCursor = [cDecont]

	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif


	lcSql = [SELECT ROUND(SUM(RO19B),0) AS RO19B, ] + ;
		[ ROUND(SUM(RO19T),0) AS RO19T, ] + ;
		[ ROUND(SUM(RO9B),0) AS RO9B, ] + ;
		[ ROUND(SUM(RO9T),0) AS RO9T,] + ;
		[ ROUND(SUM(ROTI),0) AS ROTI, ] + ;
		[ ROUND(SUM(CESCDD1),0) AS CESCDD1,] + ;
		[ ROUND(SUM(CESCDD2),0) AS CESCDD2,] + ;
		[ ROUND(SUM(WRSCDD),0) AS WRSCDD, ] + ;
		[ ROUND(SUM(FODD),0) AS FODD, ] + ;
		[ ROUND(SUM(FOFDD),0) AS FOFDD, ] + ;
		[ ROUND(SUM(WRSCFDD),0) AS WRSCFDD,] + ;
		[ ROUND(SUM(WRN),0) AS WRN FROM JV2007 ] + ;
		[ WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	&& [ WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	pn1B = Nvl(cescdd1, 0)
	pn3B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0)
	pn7B = Nvl(ro19b, 0)
	pn7T = Nvl(ro19t, 0)
	pn8B = Nvl(ro9b, 0)
	pn8T = Nvl(ro9t, 0)
	pn10B = Nvl(roti, 0) + Nvl(wrscdd, 0)
	pn11B = Nvl(wrscfdd, 0) &&+ NVL(wrscfdd,0)

	Select cDecont

	Locate For Alltrim(Rand) = '1'
	If Found()
		Replace valoare With pn1B
	Endif

	Locate For Alltrim(Rand) = '3'
	If Found()
		Replace valoare With pn3B
	Endif

	Locate For Alltrim(Rand) = '7'
	If Found()
		Replace valoare With pn7B, TVA With pn7T
	Endif

	Locate For Alltrim(Rand) = '8'
	If Found()
		Replace valoare With pn8B, TVA With pn8T
	Endif

	Locate For Alltrim(Rand) = '10'
	If Found()
		Replace valoare With pn10B
	Endif

	Locate For Alltrim(Rand) = '11'
	If Found()
		Replace valoare With pn11B
	Endif

	*!*		lcSql = [SELECT ROUND(SUM(RO19BCB),0) AS RO19BCB, ]+;
	*!*			[ ROUND(SUM(RO19BCT),0) AS RO19BCT, ]+;
	*!*			[ ROUND(SUM(RO19BVB),0) AS RO19BVB, ]+;
	*!*			[ ROUND(SUM(RO19BVT),0) AS RO19BVT, ]+;
	*!*			[ ROUND(SUM(RO19BFB),0) AS RO19BFB, ]+;
	*!*			[ ROUND(SUM(RO19BFT),0) AS RO19BFT, ]+;
	*!*			[ ROUND(SUM(RO09BCB),0) AS RO09BCB, ]+;
	*!*			[ ROUND(SUM(RO09BCT),0) AS RO09BCT, ]+;
	*!*			[ ROUND(SUM(RO09BVB),0) AS RO09BVB, ]+;
	*!*			[ ROUND(SUM(RO09BVT),0) AS RO09BVT, ]+;
	*!*			[ ROUND(SUM(RO09BFB),0) AS RO09BFB, ]+;
	*!*			[ ROUND(SUM(RO09BFT),0) AS RO09BFT, ]+;
	*!*			[ ROUND(SUM(ROSCN),0) AS ROSCN, ]+;
	*!*			[ ROUND(SUM(FO19BCB),0) AS FO19BCB, ]+;
	*!*			[ ROUND(SUM(FO19BCT),0) AS FO19BCT, ]+;
	*!*			[ ROUND(SUM(FO19BVB),0) AS FO19BVB, ]+;
	*!*			[ ROUND(SUM(FO19BVT),0) AS FO19BVT, ]+;
	*!*			[ ROUND(SUM(FO19BFB),0) AS FO19BFB, ]+;
	*!*			[ ROUND(SUM(FO19BFT),0) AS FO19BFT, ]+;
	*!*			[ ROUND(SUM(FO09BCB),0) AS FO09BCB, ]+;
	*!*			[ ROUND(SUM(FO09BCT),0) AS FO09BCT, ]+;
	*!*			[ ROUND(SUM(FO09BVB),0) AS FO09BVB, ]+;
	*!*			[ ROUND(SUM(FO09BVT),0) AS FO09BVT, ]+;
	*!*			[ ROUND(SUM(FO09BFB),0) AS FO09BFB, ]+;
	*!*			[ ROUND(SUM(FO09BFT),0) AS FO09BFT, ]+;
	*!*			[ ROUND(SUM(FOSCN),0) AS FOSCN, ]+;
	*!*			[ ROUND(SUM(CEBCB),0) AS CEBCB, ]+;
	*!*			[ ROUND(SUM(CEBCT),0) AS CEBCT, ]+;
	*!*			[ ROUND(SUM(CEBVB),0) AS CEBVB, ]+;
	*!*			[ ROUND(SUM(CEBVT),0) AS CEBVT, ]+;
	*!*			[ ROUND(SUM(CEBVSC),0) AS CEBVSC, ]+;
	*!*			[ ROUND(SUM(CEBVN),0) AS CEBVN, ]+;
	*!*			[ ROUND(SUM(CEBFB),0) AS CEBFB, ]+;
	*!*			[ ROUND(SUM(CEBFT),0) AS CEBFT, ]+;
	*!*			[ ROUND(SUM(CEBFSC),0) AS CEBFSC, ]+;
	*!*			[ ROUND(SUM(CEBFN),0) AS CEBFN, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI19BCB ELSE 0 END),0) AS TI19BCBWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI19BCT ELSE 0 END),0) AS TI19BCTWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI19BVB ELSE 0 END),0) AS TI19BVBWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI19BVT ELSE 0 END),0) AS TI19BVTWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI19BFB ELSE 0 END),0) AS TI19BFBWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI19BFT ELSE 0 END),0) AS TI19BFTWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI09BVB ELSE 0 END),0) AS TI09BVBWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI09BVT ELSE 0 END),0) AS TI09BVTWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI09BFB ELSE 0 END),0) AS TI09BFBWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) <> 'RO' THEN TI09BFT ELSE 0 END),0) AS TI09BFTWR, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI19BCB ELSE 0 END),0) AS TI19BCBRO, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI19BCT ELSE 0 END),0) AS TI19BCTRO, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI19BVB ELSE 0 END),0) AS TI19BVBRO, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI19BVT ELSE 0 END),0) AS TI19BVTRO, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI19BFB ELSE 0 END),0) AS TI19BFBRO, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI19BFT ELSE 0 END),0) AS TI19BFTRO, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI09BVB ELSE 0 END),0) AS TI09BVBRO, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI09BVT ELSE 0 END),0) AS TI09BVTRO, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI09BFB ELSE 0 END),0) AS TI09BFBRO, ]+;
	*!*			[ ROUND(SUM(CASE WHEN SUBSTR(UPPER(TRIM(COD_FISCAL)),1,2) = 'RO' THEN TI09BFT ELSE 0 END),0) AS TI09BFTRO ]+;
	*!*			[ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala


	lcSql = [SELECT ROUND(SUM(RO19BCB),0) AS RO19BCB, ] + ;
		[ ROUND(SUM(RO19BCT),0) AS RO19BCT, ] + ;
		[ ROUND(SUM(RO19BVB),0) AS RO19BVB, ] + ;
		[ ROUND(SUM(RO19BVT),0) AS RO19BVT, ] + ;
		[ ROUND(SUM(RO19BFB),0) AS RO19BFB, ] + ;
		[ ROUND(SUM(RO19BFT),0) AS RO19BFT, ] + ;
		[ ROUND(SUM(RO09BCB),0) AS RO09BCB, ] + ;
		[ ROUND(SUM(RO09BCT),0) AS RO09BCT, ] + ;
		[ ROUND(SUM(RO09BVB),0) AS RO09BVB, ] + ;
		[ ROUND(SUM(RO09BVT),0) AS RO09BVT, ] + ;
		[ ROUND(SUM(RO09BFB),0) AS RO09BFB, ] + ;
		[ ROUND(SUM(RO09BFT),0) AS RO09BFT, ] + ;
		[ ROUND(SUM(ROSCN),0) AS ROSCN, ] + ;
		[ ROUND(SUM(FO19BCB),0) AS FO19BCB, ] + ;
		[ ROUND(SUM(FO19BCT),0) AS FO19BCT, ] + ;
		[ ROUND(SUM(FO19BVB),0) AS FO19BVB, ] + ;
		[ ROUND(SUM(FO19BVT),0) AS FO19BVT, ] + ;
		[ ROUND(SUM(FO19BFB),0) AS FO19BFB, ] + ;
		[ ROUND(SUM(FO19BFT),0) AS FO19BFT, ] + ;
		[ ROUND(SUM(FO09BCB),0) AS FO09BCB, ] + ;
		[ ROUND(SUM(FO09BCT),0) AS FO09BCT, ] + ;
		[ ROUND(SUM(FO09BVB),0) AS FO09BVB, ] + ;
		[ ROUND(SUM(FO09BVT),0) AS FO09BVT, ] + ;
		[ ROUND(SUM(FO09BFB),0) AS FO09BFB, ] + ;
		[ ROUND(SUM(FO09BFT),0) AS FO09BFT, ] + ;
		[ ROUND(SUM(FOSCN),0) AS FOSCN, ] + ;
		[ ROUND(SUM(CEBCB),0) AS CEBCB, ] + ;
		[ ROUND(SUM(CEBCT),0) AS CEBCT, ] + ;
		[ ROUND(SUM(CEBVB),0) AS CEBVB, ] + ;
		[ ROUND(SUM(CEBVT),0) AS CEBVT, ] + ;
		[ ROUND(SUM(CEBVSC),0) AS CEBVSC, ] + ;
		[ ROUND(SUM(CEBVN),0) AS CEBVN, ] + ;
		[ ROUND(SUM(CEBFB),0) AS CEBFB, ] + ;
		[ ROUND(SUM(CEBFT),0) AS CEBFT, ] + ;
		[ ROUND(SUM(CEBFSC),0) AS CEBFSC, ] + ;
		[ ROUND(SUM(CEBFN),0) AS CEBFN, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBWR, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTWR, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBWR, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTWR, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBWR, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTWR, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBWR, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTWR, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBWR, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTWR, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCBRO, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCTRO, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVBRO, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVTRO, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFBRO, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFTRO, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVBRO, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVTRO, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFBRO, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFTRO ] + ;
		[ FROM VJC2007 WHERE an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
	** [ FROM VJC2007  WHERE AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC

	*!*		pn17B = Nvl(ro19bcb,0) + Nvl(ro19bvb,0) + Nvl(ro19bfb,0) + Nvl(fo19bcb,0) + Nvl(fo19bvb,0) + Nvl(fo19bfb,0) + ;
	*!*			NVL(ti19bcbro,0) + Nvl(ti19bvbro,0) + Nvl(ti19bfbro,0)

	*!*		pn17T = Nvl(ro19bct,0) + Nvl(ro19bvt,0) + Nvl(ro19bft,0) + Nvl(fo19bct,0) + Nvl(fo19bvt,0) + Nvl(fo19bft,0) + ;
	*!*			NVL(ti19bctro,0) + Nvl(ti19bvtro,0) + Nvl(ti19bftro,0)


	pn17B = Nvl(ro19bcb, 0) + Nvl(ro19bvb, 0) + Nvl(ro19bfb, 0) + Nvl(fo19bcb, 0) + Nvl(fo19bvb, 0) + Nvl(fo19bfb, 0)

	pn17T = Nvl(ro19bct, 0) + Nvl(ro19bvt, 0) + Nvl(ro19bft, 0) + Nvl(fo19bct, 0) + Nvl(fo19bvt, 0) + Nvl(fo19bft, 0)


	pn18B = Nvl(ro09bcb, 0) + Nvl(ro09bvb, 0) + Nvl(ro09bfb, 0) + Nvl(fo09bcb, 0) + Nvl(fo09bvb, 0) + Nvl(fo09bfb, 0) + ;
		Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0)

	pn18T = Nvl(ro09bct, 0) + Nvl(ro09bvt, 0) + Nvl(ro09bft, 0) + Nvl(fo09bct, 0) + Nvl(fo09bvt, 0) + Nvl(fo09bft, 0) + ;
		Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0)

	pn19B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0)



	pn14B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0)
	pn14T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0)
	pn4B = pn14B
	pn4T = pn14T
	pn9B = Nvl(ti19bcbro, 0) + Nvl(ti19bvbro, 0) + Nvl(ti19bfbro, 0) + Nvl(ti09bvbro, 0) + Nvl(ti09bfbro, 0) &&pn17B + pn18B
	pn9T = Nvl(ti19bctro, 0) + Nvl(ti19bvtro, 0) + Nvl(ti19bftro, 0) + Nvl(ti09bvtro, 0) + Nvl(ti09bftro, 0) && pn17T + pn18T

	pn16B = Nvl(ti19bcbwr, 0) + Nvl(ti19bvbwr, 0) + Nvl(ti19bfbwr, 0) + Nvl(ti09bvbwr, 0) + Nvl(ti09bfbwr, 0)
	pn16T = Nvl(ti19bctwr, 0) + Nvl(ti19bvtwr, 0) + Nvl(ti19bftwr, 0) + Nvl(ti09bvtwr, 0) + Nvl(ti09bftwr, 0)
	pn6B = pn16B
	pn6T = pn16T

	Select cDecont

	Locate For Alltrim(Rand) = '17'
	If Found()
		Replace valoare With pn17B, TVA With pn17T
	Endif

	Locate For Alltrim(Rand) = '18'
	If Found()
		Replace valoare With pn18B, TVA With pn18T
	Endif

	Locate For Alltrim(Rand) = '19'
	If Found()
		Replace valoare With pn19B
	Endif

	Locate For Alltrim(Rand) = '14'
	If Found()
		Replace valoare With pn14B, TVA With pn14T
	Endif

	Locate For Alltrim(Rand) = '14.1'
	If Found()
		Replace valoare With pn14B, TVA With pn14T
	Endif

	Locate For Alltrim(Rand) = '4'
	If Found()
		Replace valoare With pn4B, TVA With pn4T
	Endif

	Locate For Alltrim(Rand) = '4.1'
	If Found()
		Replace valoare With pn4B, TVA With pn4T
	Endif

	*!*		Locate For Alltrim(Rand) = '9'
	*!*		If Found()
	*!*			Replace valoare With pn9B, tva With pn9T
	*!*		Endif

	Locate For Alltrim(Rand) = '16'
	If Found()
		Replace valoare With pn16B, TVA With pn16T
	Endif

	Locate For Alltrim(Rand) = '6'
	If Found()
		Replace valoare With pn6B, TVA With pn6T
	Endif

	lcSql = [SELECT solddeb ] + ;
		[ from vbal ] + ;
		[ where cont = 4424 ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala


	lcCursor = [crsTR] && TVA de recuperat
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTR
	lnTR = solddeb

	Select cDecont
	Locate For Alltrim(Rand) = '30'
	If Found()
		Replace TVA With lnTR
	Endif

	If Used('crsTR')
		Use In crsTR
	Endif


	***----
	lcSql = [SELECT NVL(soldcred,0) as  soldcred ] + ;
		[ from vbal ] + ;
		[ where cont = '4423' ] + ;
		[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec ] + gcCondSucursala

	lcCursor = [crsTP] && nu si-au platit tva din luna precedenta
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsTP
	lnTP = soldcred

	If Used('crsTP')
		Use In crsTP
	Endif

	lcSql = [ select  nvl(sum(suma),0) as suma ] + ;
		[from act ] + ;
		[where an=?gnAn ] + ;
		[and luna=?gnLuna ] + ;
		[and sters=0 ] + ;
		[and scd='4423' and scc in  ('5121','5311')] + gcCondSucursala

	lcCursor = [crsPlati]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsPlati
	lnPlati = suma

	If Used('crsPlati')
		Use In crsPlati
	Endif

	lnTP = lnTP - lnPlati

	Select cDecont
	Locate For Alltrim(Rand) = '28'
	If Found()
		Replace TVA With Nvl(lnTP, 0)
	Endif


	***----


	Select cDecont
	lodt = Createobject('frm_decontTVA')
	lodt.Show(1)
Endproc && viz_decontTVA_0507

*===========================================
Procedure viz_decontTVA_0107
	Private poDecont
	Store '' To poDecont

	Private pnProRata
	pnProRata = 100

	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	lcSql = [select d.*, 00000000000 as valoare, 00000000000 as tva from decontTVA d where valabil = '0107' order by id_decont]
	lcCursor = "cDecont"
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	lcSql = [SELECT ROUND(SUM(RO19B),0) AS RO19B, ] + ;
		[ ROUND(SUM(RO19T),0) AS RO19T, ] + ;
		[ ROUND(SUM(RO9B),0) AS RO9B, ] + ;
		[ ROUND(SUM(RO9T),0) AS RO9T,] + ;
		[ ROUND(SUM(ROTI),0) AS ROTI, ] + ;
		[ ROUND(SUM(CESCDD1),0) AS CESCDD1,] + ;
		[ ROUND(SUM(CESCDD2),0) AS CESCDD2,] + ;
		[ ROUND(SUM(WRSCDD),0) AS WRSCDD, ] + ;
		[ ROUND(SUM(FODD),0) AS FODD, ] + ;
		[ ROUND(SUM(FOFDD),0) AS FOFDD, ] + ;
		[ ROUND(SUM(WRSCFDD),0) AS WRSCFDD,] + ;
		[ ROUND(SUM(WRN),0) AS WRN FROM JV2007 WHERE AN=?gnAn AND LUNA=?gnLuna] + gcCondSucursala

	lcCursor = [crsJV]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJV
	pn06B = Nvl(ro19b, 0)
	pn06T = Nvl(ro19t, 0)
	pn07B = Nvl(ro9b, 0)
	pn07T = Nvl(ro9t, 0)
	pn08B = Nvl(roti, 0) + Nvl(wrscdd, 0)
	pn09B = Nvl(wrscfdd, 0) &&+ NVL(wrscfdd,0)

	pn01B = Nvl(cescdd1, 0)
	pn02B = Nvl(cescdd2, 0) + Nvl(fodd, 0) + Nvl(fofdd, 0)

	Select cDecont

	Locate For Alltrim(Rand) = '01'
	If Found()
		Replace valoare With pn01B
	Endif

	Locate For Alltrim(Rand) = '02'
	If Found()
		Replace valoare With pn02B
	Endif

	Locate For Alltrim(Rand) = '06'
	If Found()
		Replace valoare With pn06B, TVA With pn06T
	Endif

	Locate For Alltrim(Rand) = '07'
	If Found()
		Replace valoare With pn07B, TVA With pn07T
	Endif

	Locate For Alltrim(Rand) = '08'
	If Found()
		Replace valoare With pn08B
	Endif

	Locate For Alltrim(Rand) = '09'
	If Found()
		Replace valoare With pn09B
	Endif

	lcSql = [SELECT ROUND(SUM(RO19BCB),0) AS RO19BCB, ] + ;
		[ ROUND(SUM(RO19BCT),0) AS RO19BCT, ] + ;
		[ ROUND(SUM(RO19BVB),0) AS RO19BVB, ] + ;
		[ ROUND(SUM(RO19BVT),0) AS RO19BVT, ] + ;
		[ ROUND(SUM(RO19BFB),0) AS RO19BFB, ] + ;
		[ ROUND(SUM(RO19BFT),0) AS RO19BFT, ] + ;
		[ ROUND(SUM(RO09BCB),0) AS RO09BCB, ] + ;
		[ ROUND(SUM(RO09BCT),0) AS RO09BCT, ] + ;
		[ ROUND(SUM(RO09BVB),0) AS RO09BVB, ] + ;
		[ ROUND(SUM(RO09BVT),0) AS RO09BVT, ] + ;
		[ ROUND(SUM(RO09BFB),0) AS RO09BFB, ] + ;
		[ ROUND(SUM(RO09BFT),0) AS RO09BFT, ] + ;
		[ ROUND(SUM(ROSCN),0) AS ROSCN, ] + ;
		[ ROUND(SUM(FO19BCB),0) AS FO19BCB, ] + ;
		[ ROUND(SUM(FO19BCT),0) AS FO19BCT, ] + ;
		[ ROUND(SUM(FO19BVB),0) AS FO19BVB, ] + ;
		[ ROUND(SUM(FO19BVT),0) AS FO19BVT, ] + ;
		[ ROUND(SUM(FO19BFB),0) AS FO19BFB, ] + ;
		[ ROUND(SUM(FO19BFT),0) AS FO19BFT, ] + ;
		[ ROUND(SUM(FO09BCB),0) AS FO09BCB, ] + ;
		[ ROUND(SUM(FO09BCT),0) AS FO09BCT, ] + ;
		[ ROUND(SUM(FO09BVB),0) AS FO09BVB, ] + ;
		[ ROUND(SUM(FO09BVT),0) AS FO09BVT, ] + ;
		[ ROUND(SUM(FO09BFB),0) AS FO09BFB, ] + ;
		[ ROUND(SUM(FO09BFT),0) AS FO09BFT, ] + ;
		[ ROUND(SUM(FOSCN),0) AS FOSCN, ] + ;
		[ ROUND(SUM(CEBCB),0) AS CEBCB, ] + ;
		[ ROUND(SUM(CEBCT),0) AS CEBCT, ] + ;
		[ ROUND(SUM(CEBVB),0) AS CEBVB, ] + ;
		[ ROUND(SUM(CEBVT),0) AS CEBVT, ] + ;
		[ ROUND(SUM(CEBVSC),0) AS CEBVSC, ] + ;
		[ ROUND(SUM(CEBVN),0) AS CEBVN, ] + ;
		[ ROUND(SUM(CEBFB),0) AS CEBFB, ] + ;
		[ ROUND(SUM(CEBFT),0) AS CEBFT, ] + ;
		[ ROUND(SUM(CEBFSC),0) AS CEBFSC, ] + ;
		[ ROUND(SUM(CEBFN),0) AS CEBFN, ] + ;
		[ ROUND(SUM(TI19BCB),0) AS TI19BCB, ] + ;
		[ ROUND(SUM(TI19BCT),0) AS TI19BCT, ] + ;
		[ ROUND(SUM(TI19BVB),0) AS TI19BVB, ] + ;
		[ ROUND(SUM(TI19BVT),0) AS TI19BVT, ] + ;
		[ ROUND(SUM(TI19BFB),0) AS TI19BFB, ] + ;
		[ ROUND(SUM(TI19BFT),0) AS TI19BFT, ] + ;
		[ ROUND(SUM(TI09BVB),0) AS TI09BVB, ] + ;
		[ ROUND(SUM(TI09BVT),0) AS TI09BVT, ] + ;
		[ ROUND(SUM(TI09BFB),0) AS TI09BFB, ] + ;
		[ ROUND(SUM(TI09BFT),0) AS TI09BFT ] + ;
		[ FROM JC2007  WHERE AN=?gnAn AND LUNA=?gnLuna] + gcCondSucursala

	lcCursor = [crsJC]
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
		Return
	Endif

	Select crsJC
	pn03B = Nvl(cebcb, 0) + Nvl(cebvb, 0) + Nvl(cebfb, 0)
	pn03T = Nvl(cebct, 0) + Nvl(cebvt, 0) + Nvl(cebft, 0)

	pn04B = Nvl(ti19bcb, 0) + Nvl(ti19bvb, 0) + Nvl(ti19bfb, 0) + Nvl(ti09bvb, 0) + Nvl(ti09bfb, 0)
	pn04T = Nvl(ti19bct, 0) + Nvl(ti19bvt, 0) + Nvl(ti19bft, 0) + Nvl(ti09bvt, 0) + Nvl(ti09bft, 0)

	pn13B = Nvl(cebvb, 0)
	pn13T = Nvl(cebvt, 0)

	pn14B = Nvl(cebcb, 0) + Nvl(cebfb, 0)
	pn14T = Nvl(cebct, 0) + Nvl(cebft, 0)

	pn15B = Nvl(ti19bcb, 0) + Nvl(ti19bvb, 0) + Nvl(ti19bfb, 0) + Nvl(ti09bvb, 0) + Nvl(ti09bfb, 0)
	pn15T = Nvl(ti19bct, 0) + Nvl(ti19bvt, 0) + Nvl(ti19bft, 0) + Nvl(ti09bvt, 0) + Nvl(ti09bft, 0)

	pn18B = Nvl(ro19bvb, 0) + Nvl(fo19bvb, 0)
	pn18T = Nvl(ro19bvt, 0) + Nvl(fo19bvt, 0)

	pn19B = Nvl(ro09bvb, 0) + Nvl(fo09bvb, 0)
	pn19T = Nvl(ro09bvt, 0) + Nvl(fo09bvt, 0)

	pn20B = Nvl(roscn, 0) + Nvl(cebvsc, 0) + Nvl(cebvn, 0) + Nvl(cebfsc, 0) + Nvl(cebfn, 0) + Nvl(foscn, 0)

	pn21B = Nvl(ro19bcb, 0) + Nvl(fo19bcb, 0)
	pn21T = Nvl(ro19bct, 0) + Nvl(fo19bct, 0)

	pn22B = Nvl(ro19bfb, 0) + Nvl(fo19bfb, 0)
	pn22T = Nvl(ro19bft, 0) + Nvl(fo19bft, 0)

	pn23B = Nvl(ro09bfb, 0) + Nvl(fo09bfb, 0)
	pn23T = Nvl(ro09bft, 0) + Nvl(fo09bft, 0)

	Select cDecont

	Locate For Alltrim(Rand) = '03'
	If Found()
		Replace valoare With pn03B, TVA With pn03T
	Endif

	Locate For Alltrim(Rand) = '04'
	If Found()
		Replace valoare With pn04B, TVA With pn04T
	Endif

	Locate For Alltrim(Rand) = '13'
	If Found()
		Replace valoare With pn13B, TVA With pn13T
	Endif

	Locate For Alltrim(Rand) = '14'
	If Found()
		Replace valoare With pn14B, TVA With pn14T
	Endif

	Locate For Alltrim(Rand) = '15'
	If Found()
		Replace valoare With pn15B, TVA With pn15T
	Endif

	Locate For Alltrim(Rand) = '18'
	If Found()
		Replace valoare With pn18B, TVA With pn18T
	Endif

	Locate For Alltrim(Rand) = '19'
	If Found()
		Replace valoare With pn19B, TVA With pn19T
	Endif

	Locate For Alltrim(Rand) = '20'
	If Found()
		Replace valoare With pn20B
	Endif

	Locate For Alltrim(Rand) = '21'
	If Found()
		Replace valoare With pn21B, TVA With pn21T
	Endif

	Locate For Alltrim(Rand) = '22'
	If Found()
		Replace valoare With pn22B, TVA With pn22T
	Endif

	Locate For Alltrim(Rand) = '23'
	If Found()
		Replace valoare With pn23B, TVA With pn23T
	Endif

	Select cDecont
	lodt = Createobject('frm_decontTVA')
	lodt.Show(1)

Endproc && viz_decontTVA_0107_0407
***--------------------------------------	inceput date_identif ------------------------
Procedure date_identif
	Lparameters tcCale

	Local lcSelect, lcFiltru, lcSchema, lcOrder, llAfisare, lcGroup, llModParam, lcFiltruOriginal

	If !Empty(tcCale)
		gcDecont_caleExport = tcCale
	Else
		gcDecont_caleExport = "C:\Program Files\Declaratii fiscale " + Alltrim(Str(gnAn))
	Endif

	If !Directory(gcDecont_caleExport)
		gcDecont_caleExport = Getdir("C:", "Directorul Declaratii fiscale", "Declaratii fiscale")
	Endif
	If Empty(gcDecont_caleExport)
		Return
	Endif


	lcDir = Addbs(Alltrim(gcDecont_caleExport)) + Addbs(Alltrim(gofirma.firma))
	If !Directory(lcDir)
		Md (lcDir)
	Endif

	lcFisUnitate = lcDir + "unitate.dbf"
	If !File(lcFisUnitate)
		Create Table (lcFisUnitate) (caen N(4), codi N(10), deni c(50), adr c(150), telf c(10), banca c(100), Cont c(100), intocmitf c(30), calIntoc c(30))
	Else
		Use (lcFisUnitate) In 0 Alias unitate
	Endif

	Select unitate
	If Eof()
		Append Blank
	Else
		Go Top
	Endif

	Select unitate
	Scatter Name pounitate

	If Empty(codi)
		lccf = Nvl(Alltrim(gofirma.cod_fiscal), '')
		lccf_tot = ''
		For i = 1 To Len(lccf)
			If Type(Substr(lccf, i, 1)) = "N"
				lcnc = Substr(lccf, i, 1)
				lccf_tot = lccf_tot + lcnc
			Endif
		Endfor
		pounitate.codi = Val(lccf_tot)
	Endif

	If Empty(deni)
		pounitate.deni = Alltrim(gofirma.firma)
	Endif
	***

	lcCaen = ""
	lcCalIntoc = ""

	*!*	** caen
	*!*		Private poCaen
	*!*		Store '' To poCaen
	*!*		lcSchema1 = [ID N(4), CODCAEN C(100), COD N(4)]
	*!*		lcSelect1 = ['select id, codcaen, cod from sitfin_codCaen where 1=2']
	*!*		lcOrder1 = []
	*!*		lcFiltru1 = [2=2]
	*!*		llAfiseaza = .F.
	*!*		gencursor('poCaen','crsCodCaen', lcSelect1, lcFiltru1, lcSchema1, lcOrder1, llAfiseaza)
	*!*		poCaen.ca_baza1.afisare()


	** caen
	Private poCaen
	Store '' To poCaen

	lcSchema = []
	lcSelect = [select clasa as cod, trim(clasa)||'--'||substr(trim(caen),1,100) As codcaen from caen]
	lcOrder = [clasa]
	lcFiltru = [clasa is not null]

	llAfisare = .F.
	llModParam = .T.
	lcFiltruOriginal = []

	gencursor('poCaen', 'crsCodCaen', lcSelect, lcFiltru, lcSchema, lcOrder, llAfisare, lcGroup, llModParam, lcFiltruOriginal )
	poCaen.ca_baza1.afisare()

	If Empty(pounitate.caen)
		Select crsCodCaen
		Locate For Alltrim(cod) = Alltrim(gofirma.caen)
		If Found()
			pounitate.caen = cod
			lcCaen = codCaen
		Endif
	Else
		Select crsCodCaen
		Locate For Alltrim(cod) = Alltrim(Str(pounitate.caen))
		If Found()
			lcCaen = codCaen
		Endif
	Endif

	Select unitate
	If Empty(adr)
		pounitate.adr = Nvl(Alltrim(gofirma.adresa), '')
	Endif

	If Empty(telf)
		pounitate.telf = Nvl(Alltrim(gofirma.telefon), '') + ' ' + Nvl(Alltrim(gofirma.fax), '') + ' ' + Nvl(Alltrim(gofirma.email), '')
	Endif

	If Empty(banca)
		pounitate.banca = Nvl(Alltrim(gofirma.banca1), '')
	Endif

	If Empty(Cont)
		pounitate.Cont = Nvl(Alltrim(gofirma.cont_banca1), '')
	Endif

	If Empty(intocmitf)
		pounitate.intocmitf = Nvl(Alltrim(gofirma.declarant), '')
	Endif

	If Empty(calIntoc)
		pounitate.calIntoc = Nvl(Alltrim(gofirma.functie), '')
	Endif

	Select unitate

	lodi = Createobject("frm_dateIdentificare")
	lodi.cCaen = lcCaen
	lodi.Show(1)

	Release poCaen

	If buton = 2
		Use In unitate
		Return
	Endif

	Select unitate
	Gather Name pounitate

	Use In unitate
Endproc && date_identif

***-------------------------------------------------------------------------------------------------------------------------
Procedure export_decont
	Parameters tcAlias

	Local lcSir, lcDirTabele, lcFisUnitate, lcCodFiscal, lcDirDecont, lcFile, lcFisAdresa, lcfisDec300T, lcfisDec300T9
	lcSir = ''
	lcFisAdresa = []
	lcfisDec300T = []
	lcfisDec300T9 = []

	If gnAn > 2007
		gcDecont_caleExport = "C:\Program Files\Declaratii fiscale " + Alltrim(Str(gnAn))
	Else
		gcDecont_caleExport = "C:\Program Files\Declaratii fiscale 2007"
	Endif
	If !Directory(gcDecont_caleExport)
		gcDecont_caleExport = Getdir("C:", "Directorul Declaratii fiscale", "Declaratii fiscale")
	Endif
	If Empty(gcDecont_caleExport)
		Return
	Endif

	lcDirTabele = Addbs(Alltrim(gcDecont_caleExport)) + Alltrim(gofirma.firma) + "\"
	lcFisUnitate = lcDirTabele + "Unitate.dbf"
	If !File(lcFisUnitate)
		amessage("Nu ati salvat Date de identificare unitate!", 0 + 48, _Screen.Caption)
		Return
	Endif

	Use (lcFisUnitate) In 0 Alias unitate
	Select unitate
	lcCodFiscal = Alltrim(Str(codi))
	Use In unitate

	lcDirDecont = Addbs(Alltrim(gcDecont_caleExport)) + lcCodFiscal

	*** creez unitate.txt
	Set Textmerge To
	lcFile = lcDirTabele + "adresa.txt"
	Set Textmerge On To (lcFile) Noshow

	Use (lcFisUnitate) In 0 Alias unitate
	Select unitate
	Go Top
	lcSir = "##,#" + Alltrim(Str(codi)) + "#,#" + ;
		Alltrim(deni) + "#,#" + ;
		Alltrim(deni) + "#,#" + ;
		Alltrim(adr) + "#,#" + ;
		Alltrim(telf) + "#,#" + ;
		Alltrim(banca) + "#,#" + ;
		Alltrim(Cont) + "#,#" + ;
		"#,##,##,#" + ;
		Alltrim(intocmitf) + "#,#" + ;
		Alltrim(calIntoc) + "#," + ;
		Alltrim(Str(caen))

	*USE IN unitate

\\<<lcSir>>
	Set Textmerge To
	Wait Window "S-a exportat adresa.txt..." Timeout 1

	****
	***
	*** creez Dec300T.txt
	Set Textmerge To
	lcFile = lcDirTabele + "\Dec300T.txt"
	Set Textmerge On To (lcFile) Noshow

	lcSir = ''

	lcTest = ''
	*lcSir = lcSir + '300,,' + PADL(gnLuna,2,'0') + ',' +  ALLTRIM(STR(gnAn,4,0)) + ',,,'
	lcSir = lcCodFiscal + ',' + Padl(gnLuna, 2, '0') + ',' +  Alltrim(Str(gnAn, 4, 0)) + ',,' + Alltrim(Transform(pnProRata)) + ','
	Select (tcAlias)
	Scan For !Empty(Nvl(Rand, 0))
		lcSir = lcSir + ;
			Iif(Nvl(inactivB, 0) <> 1, Iif(valoare <> 0, Alltrim(Str(valoare, 15, 0)), []) + [,], []) + ;
			Iif(Nvl(inactivT, 0) <> 1, Iif(TVA <> 0, Alltrim(Str(TVA, 15, 0)), []) + [,], [])
	Endscan

	lcSir = lcSir + Alltrim(Str(pnNr_facturi, 15, 0)) + ',' + Alltrim(Str(pnBaza, 15, 0)) + ',' + Alltrim(Str(pnTVA, 15, 0)) + ','

	Select unitate
	* lcSir = lcSir + ALLTRIM(STR(caen))+',#N#,#'+ALLTRIM(banca)+'#,#'+ALLTRIM(cont) + '#'
	lcSir = lcSir + Alltrim(Str(caen)) + ',#N#'

	Use In unitate


	*!*		IF !EMPTY(lcSir)
	*!*			lcSir = LEFT(lcSir,LEN(lcSir)-1)
	*!*		ENDIF
	lcSir = lcSir + Chr(13) + Chr(10)


\\<<lcSir>>
	Set Textmerge To
	Wait Window "S-a exportat Dec300T.txt..." Timeout 1

	lcA = lcDirTabele + "\" + "adresa.txt"
	lcD = lcDirTabele + "\" + "dec300T.txt"

	If !Directory(lcDirDecont)
		Md (lcDirDecont)
	Endif
	lcFisAdresa = lcDirDecont + "\adresa.txt"
	lcfisDec300T = lcDirDecont + "\dec300T.txt"
	lcfisDec300T9 = lcDirDecont + "\dec300T9.txt"

	Copy File (lcA) To (lcFisAdresa)
	Copy File (lcD) To (lcfisDec300T)
	Copy File (lcD) To (lcfisDec300T9)

	amessagebox([Fisierele au fost exportate in directorul ] + Alltrim(Addbs(Justpath(lcfisDec300T))) + Chr(13) + Chr(10) + ;
		  [Intrati in programul de Declaratii Fiscale si selectati Declaratia 300], 0 + 64, 'Decont TVA')
Endproc && export_decont
***--------------------------------------------------------------
*********************************************************
*** DECLARATIA 392
*********************************************************
Procedure viz_decl392
	Lparameters tcTip
	Local lcTip
	lcTip = Iif(Type('tcTip') = 'C', m.tcTip, Iif(Substr(Alltrim(gofirma.cod_fiscal), 1, 2) = 'RO', 'A', 'B'))

	If m.gnLuna = 12
		Local loD392 As "D392"
		loD392 = Createobject("D392", m.lcTip) && ovanzcump.vcx
		loD392.do_executa()
	Else
		amessagebox("Pentru a putea genera declaratia 392 " + Alltrim(tcTip) + " trebuie sa intrati in luna decembrie!", 48, "Atentie")
	Endif
Endproc

Define Class D392 As Custom

	cTip = []	&&	A sau B
	nAn	= 0
	nLuna = 0
	cCodFiscal = 0
	cCodCaen = []
	lPlatitorTVA = .F.
	cDenumireFirma = []
	cAdresa = []
	cTelefon = []
	cFax = []
	cEmail = []
	cDeclarant = []
	cNumeDeclarant = []
	cPrenumeDeclarant = []
	cFunctieDeclarant = []

	nCifraDeAfaceri = 0
	nBazaLivrPlTVA = 0
	nTVALivrPlTVA = 0
	nBazaLivrNeplTVA = 0
	nTVALivrNeplTVA = 0
	nBazaAchPlTVA = 0
	nTVAAchPlTVA = 0
	nBazaAchNeplTVA = 0

	nProcTVATI = 0.24

	Procedure Init
		Lparameters tcTip
		With This
			.cTip = tcTip
			.nAn = gnAn
			.nLuna = m.gnLuna
			.cCodFiscal = Strtran(Alltrim(Upper(Nvl(gofirma.cod_fiscal, ''))), 'RO', '')
			.cCodCaen = Alltrim(gofirma.caen)
			.lPlatitorTVA = (Substr(Alltrim(Upper(Nvl(gofirma.cod_fiscal, ''))), 1, 2) == 'RO')
			.cDenumireFirma = Nvl(Alltrim(gofirma.firma), '')
			.cAdresa = Nvl(Alltrim(gofirma.adresa), '')
			.cTelefon = Nvl(Alltrim(gofirma.telefon), '')
			.cFax = Nvl(Alltrim(gofirma.fax), '')
			.cEmail = Nvl(Alltrim(gofirma.email), '')
			.cDeclarant = Alltrim(gofirma.declarant)
			.cNumeDeclarant = Alltrim(gofirma.nume_declarant)
			.cPrenumeDeclarant = Alltrim(gofirma.prenume_declarant)
			.cFunctieDeclarant = Alltrim(gofirma.functie)

			.nCifraDeAfaceri = 0
			.nBazaLivrPlTVA = 0
			.nTVALivrPlTVA = 0
			.nBazaLivrNeplTVA = 0
			.nTVALivrNeplTVA = 0
			.nBazaAchPlTVA = 0
			.nTVAAchPlTVA = 0
			.nBazaAchNeplTVA = 0
		Endwith
	Endproc

	Procedure do_executa
		Local loFrmDoc As "frm_decl_tipdoc"
		Local lcAppPath, lcCaleFisier, lcCaleFisierPDF, lcCaleTemp, lcComunPath, lcConditieAct, lcCursor
		Local lcFileName, lcFileNamePDF, lcFirma, lcFisierPDFNou, lcFisierPDFOriginal, lcFisierTXT
		Local lcFisierXDP, lcFisierXLS, lcMesaj, lcNumeFisier, lcRun, lcSelect, lcSirId, lcSql, llSucces
		Local lnPos, loEx
		lcSelect = Select()
		*** Initializare cai export
		lcAppPath = Addbs(gcAppPath)
		lcComunPath = lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If lnPos > 0
			lcComunPath = Left(m.lcAppPath, lnPos)
		Endif
		lcComunPath = lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		*** Creez structura director export : caletemp\D392\aaaa\firma\
		llSucces = .F.
		Try
			lcCaleTemp = m.gcBasePath
			lcCaleFisier = m.lcCaleTemp + "D392\"
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcFirma = Iif(gcAppName = "CONCONT", Alltrim(gofirma.fscurt), m.gcFirma)
			lcCaleFisier = m.lcCaleFisier + Strtran(lcFirma, [ ], [_]) + "\"

			lcCaleFisierPDF = m.lcCaleFisier  && c:\temp\D392\2014\firma\

			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcFirma = Iif(gcAppName = "CONCONT", Alltrim(gofirma.fscurt), m.gcFirma)
			lcCaleFisier = m.lcCaleFisier + Strtran(lcFirma, [ ], [_]) + "\"
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			llSucces = .T.
		Catch To loEx
			lcMesaj = "Creare director " + lcCaleFisier + " " + loEx.Message

			amessagebox(lcMesaj, 0 + 48)
		Endtry
		If !llSucces
			Return
		Endif

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', Strtran(Alltrim(gofirma.cod_fiscal), 'RO', ''))
		Endif

		lcNumeFisier = "d392_" + gofirma.codfiscalfro + "_" + Alltrim(Str(gnAn))
		Do Case
			Case gnAn >= 2019
				lcFisierPDFOriginal = lcComunPath + "d392_2019.pdf" && d:\contafin\comuncontafin\d392.pdf

			Case gnAn >= 2015
				lcFisierPDFOriginal = lcComunPath + "d392_2015.pdf" && d:\contafin\comuncontafin\d392.pdf
			Otherwise
				lcFisierPDFOriginal = lcComunPath + "d392_2014.pdf" && d:\contafin\comuncontafin\d392.pdf
		Endcase

		This.UpdatePDF(m.lcFisierPDFOriginal) && actualizez pdf de la romfast

		lcFileName = m.lcCaleFisier + lcNumeFisier

		lcFileNamePDF = m.lcCaleFisierPDF + lcNumeFisier
		lcFisierPDFNou = m.lcFileNamePDF + [.pdf]
		lcFisierXDP = m.lcFileNamePDF + [.xdp]

		lcFisierTXT = m.lcCaleFisier + lcNumeFisier + [.txt]
		lcFisierXLS = m.lcCaleFisier + lcNumeFisier + [.xls]

		If File(lcFisierTXT)
			Delete File (lcFisierTXT)
		Endif

		If File(lcFisierXLS)
			Delete File (lcFisierXLS)
		Endif
		If File(lcFisierPDFNou)
			Delete File (lcFisierPDFNou)
		Endif
		If File(lcFisierXDP)
			Delete File (lcFisierXDP)
		Endif


		*** ALEGERE DOCUMENTE FACTURI
		lcConditieAct = [ and (extract(year from dataact)*12+extract(month from dataact) = an*12+luna)]

		Text To lcSql Textmerge Noshow
			SELECT 0 AS ALES, FEL_DOCUMENT AS FDOC, ID_FDOC
			FROM vjv2010
			WHERE an = <<ALLTRIM(STR(This.nAn))>>
			<<lcConditieAct>>
			<<gcCondSucursala>>
			UNION
			SELECT 0 AS ALES, FEL_DOCUMENT AS FDOC, ID_FDOC
			FROM vjc2010
			WHERE an = <<ALLTRIM(STR(This.nAn))>>
			<<lcConditieAct>>
			<<gcCondSucursala>>
		Endtext
		goLog.Log(lcSql)

		If !goExecutor.oExecuta(lcSql, "crsFdoc")
			Return
		Endif

		Update crsFDoc Set ales = 1 Where Inlist(Alltrim(fdoc), 'FACTURA', 'BON FISCAL')
		Go Top In crsFDoc

		loFrmDoc = Createobject("frm_decl_tipdoc", "392")
		loFrmDoc.label1.Caption = 'Alegeti tipurile de documente care vor intra'
		loFrmDoc.label2.Caption = 'in declaratie'
		loFrmDoc.Show(1)

		*!*	sir tipuri document alese
		lcSirId = ""
		Select id_fdoc From crsFDoc With (Buffering = .T.) Where ales = 1 Into Cursor crsFdocTemp
		Use In (Select("crsFDoc"))
		If _Tally = 0
			amessagebox('Trebuie sa alegeti un tip de document!', 0 + 48, 'Atentie')
			Use In (Select("crsFdocTemp"))
			Return
		Endif

		Select crsFdocTemp
		Scan
			lcSirId = 	lcSirId + [, ] + Alltrim(Str(id_fdoc))
		Endscan
		If !Empty(lcSirId)
			lcSirId = Substr(lcSirId, 2)
		Endif
		Use In (Select("crsFdocTemp"))

		*** citesc JV
		Text To lcSql Textmerge Noshow
			SELECT ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', '') AS COD_FISCALFRO, SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) AS ATRIBUT_FISCAL, DENUMIRE,
			 ROUND(SUM(RO24B + RO19B + RO9B + RO5B + RO24NB + RO9NB + RO5NB + WRN + WRSCDD + WRSCFDD), 0) AS BAZA,
			 ROUND(SUM(RO24T + RO19T + RO9T + Ro5T + RO24NT + RO9NT + RO5NT), 0) AS TVA,
			 ROUND(SUM(ROTI), 0) AS TIBAZA,
			 0 AS TITVA,
	         SUM(case when ROTI = 0 then 1 else 0 end ) as NRFACT,
	         SUM(case when ROTI <> 0 then 1 else 0 end ) as NRFACTTI
			 FROM VJV2013
			 WHERE AN = <<ALLTRIM(STR(This.nAn))>>
			<<lcConditieAct>>
			AND ID_FDOC IN (<<m.lcSirId>>)
		    <<gcCondSucursala>>
			GROUP BY ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', ''), SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2), DENUMIRE
		Endtext
		goLog.Log(lcSql)

		lcCursor = [crsJV]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Update crsJV Set atribut_fiscal = '' Where Isdigit(atribut_fiscal)

		*!*	citesc JC
		Text To lcSql Textmerge Noshow
			SELECT ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', '') AS COD_FISCALFRO, SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) AS ATRIBUT_FISCAL, DENUMIRE,
			 ROUND(SUM(RO24B + RO19B + RO09B + RO05B + RO24NB + RO9NB + RO5NB + ROSCN), 0) AS BAZA,
			 ROUND(SUM(RO24T + RO19T + RO09T + RO05T + RO24NT + RO9NT + RO5NT), 0) AS TVA,
			 ROUND(SUM(TI19B + TI09B + TI24B), 0) as TIBAZA,
			 ROUND(SUM(TI19T + TI09T + TI24T), 0) as TITVA,
		     SUM(case when TI19B = 0 and TI09B = 0 and TI24B = 0 then 1 else 0 end ) as NRFACT,
	         SUM(case when TI19B <> 0 or TI09B <> 0 or TI24B <> 0 then 1 else 0 end ) as NRFACTTI
			 FROM VJC2013
			 WHERE AN = <<ALLTRIM(STR(This.nAn))>>
			<<lcConditieAct>>
			AND ID_FDOC IN (<<m.lcSirId>>)
		    <<gcCondSucursala>>
			GROUP BY ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', ''), SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2), DENUMIRE
		Endtext
		goLog.Log(lcSql)

		lcCursor = [crsJC]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Use In (Select("crsJV"))
			Return
		Endif
		Update crsJC Set atribut_fiscal = '' Where Isdigit(atribut_fiscal)

		*!*	D392
		Create Cursor crsTipA (TIP c(1))
		Insert Into crsTipA (TIP) Values ('A')
		Insert Into crsTipA (TIP) Values ('C') && ACHIZITII TAXARE INVERSA

		Create Cursor crsTipL (TIP c(1))
		Insert Into crsTipL (TIP) Values ('L')
		Insert Into crsTipL (TIP) Values ('V') && LIVRARI TAXARE INVERSA

		Select b.TIP, Iif(Substr(Alltrim(cod_fiscal), 1, 2) = [RO], 1, 0) As plTVA, ;
			Left(cod_fiscal, 30) As cod_fiscal, Left(cod_fiscalfro, 30) As cod_fiscalfro, Padr(Nvl(atribut_fiscal, ''), 2, ' ') As atribut_fiscal, denumire, ;
			Cast(Iif(b.TIP = 'A', baza, tibaza) As N(20)) As baza, ;
			Cast(Iif(b.TIP = 'A', TVA, titva) As N(20)) As TVA, ;
			Cast(Iif(b.TIP = 'A', NRFACT, NRFACTTI) As N(20)) As NRFACT ;
			From crsJC a, crsTipA b ;
			Where a.baza <> 0 Or a.TVA <> 0 Or a.tibaza <> 0 Or a.titva <> 0 ;		&&	modificare	v 2.5.26
			Union ;
			Select b.TIP, Iif(Substr(Alltrim(cod_fiscal), 1, 2) = [RO], 1, 0) As plTVA, ;
			Left(cod_fiscal, 30) As cod_fiscal, Left(cod_fiscalfro, 30) As cod_fiscalfro, Padr(Nvl(atribut_fiscal, ''), 2, ' ') As atribut_fiscal, denumire, ;
			Cast(Iif(b.TIP = 'L', baza, tibaza) As N(20)) As baza, ;
			Cast(Iif(b.TIP = 'L', TVA, titva) As N(20)) As TVA, ;
			Cast(Iif(b.TIP = 'L', NRFACT, NRFACTTI) As N(20)) As NRFACT ;
			From crsJV a, crsTipL b ;
			Where a.baza <> 0 Or a.TVA <> 0 Or a.tibaza <> 0 Or a.titva <> 0 ;		&&	modificare	v 2.5.26 : HAVING baza <> 0 Or tva <> 0 Or tibaza <> 0 Or titva <> 0
			Order By 1, 3 ;
			Into Cursor crsD392 Readwrite

		Use In (Select('crsTipA'))
		Use In (Select('crsTipL'))
		Use In (Select('crsJV'))
		Use In (Select('crsJC'))

		*!*	cifra de afaceri
		*!*			Text To lcSql Textmerge Noshow
		*!*				SELECT SUM(totcred) as totalcredit FROM vbal
		*!*					WHERE (SUBSTR(cont,1,2) = '70' OR cont = '7583') AND AN = <<ALLTRIM(STR(This.nAn))>> AND LUNA = 12
		*!*			ENDTEXT

		Text To lcSql Textmerge Noshow
			SELECT SUM(totcred) as totalcredit FROM vbal
				WHERE (cont = '7583') AND AN = <<ALLTRIM(STR(This.nAn))>> AND LUNA = 12
		Endtext		goLog.Log(lcSql)

		lcCursor = [crsTotalCifraAfaceri]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Use In (Select('crsD392'))
			Return
		Endif

		llSucces = .T.
		If m.llSucces
			Select crsTotalCifraAfaceri
			This.nCifraDeAfaceri = - Nvl(totalcredit, 0) && -7583 (vanzarile de active nu se iau in considerare) && CF art 152 alin 2


			Calculate Sum(Iif(TIP = 'L' And plTVA = 1, baza, 0)), Sum(Iif(TIP = 'L' And plTVA = 1, TVA, 0)), ;
				Sum(Iif(TIP = 'L' And plTVA = 0, baza, 0)), Sum(Iif(TIP = 'L' And plTVA = 0, TVA, 0)), ;
				Sum(Iif(TIP = 'A' And plTVA = 1, baza, 0)), Sum(Iif(TIP = 'A' And plTVA = 1, TVA, 0)), ;
				Sum(Iif(TIP = 'A' And plTVA = 0, baza, 0)) ;
				To This.nBazaLivrPlTVA, This.nTVALivrPlTVA, ;
				This.nBazaLivrNeplTVA, This.nTVALivrNeplTVA, ;
				This.nBazaAchPlTVA, This.nTVAAchPlTVA, ;
				This.nBazaAchNeplTVA In crsD392

			This.nCifraDeAfaceri = This.nCifraDeAfaceri + This.nBazaLivrPlTVA + This.nBazaLivrNeplTVA && cifra de afaceri sunt livrarile fara 7583. S-ar putea sa intre si livrarile intracomunitare???

			*!* am nume diferite cu acelasi cod fiscal in JV, JC sau in amandoua
			*!* exista nume fara cod fiscal??? (nu stiu de ce am pus conditia asta - le-am luat pe cele cu RO - au cod fiscal)
			This.do_genereaza_xls(m.lcFisierXLS)
			Do Case
				Case This.nAn >= 2019
					This.do_genereaza_xdp_2014(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

					If File(m.lcFisierXDP)
						open_default_app(m.lcFisierXDP)
					Endif

				Case This.nAn >= 2015
					This.do_genereaza_xdp_2014(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

					If File(m.lcFisierXDP)
						open_default_app(m.lcFisierXDP)
					Endif

				Case This.nAn >= 2014
					This.do_genereaza_xdp_2014(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

					If File(m.lcFisierXDP)
						open_default_app(m.lcFisierXDP)
					Endif

				Otherwise
					This.do_genereaza_txt(lcFisierTXT)
			Endcase

			*** deschid explorer in directorul de export
			lcRun = [RUN /N7 explorer.exe "] + m.lcCaleFisier + ["]
			&lcRun
		Endif && llSucces

		Use In (Select('crsTotalCifraAfaceri'))
		Use In (Select('crsD392'))

		Select (m.lcSelect)

	Endproc && do_executa
	*===========================================
	Procedure do_genereaza_xls
		Lparameters tcFisierXLS

		Local lcSelect
		lcSelect = Select()

		If Type('crsD392.nrfact') <> 'U'
			Select TIP, Left(Alltrim(cod_fiscal), 10) As cod_fiscal, Left(denumire, 100) As denumire, baza + TVA As Total, baza, TVA, NRFACT ;
				From crsD392 ;
				Where NRFACT <> 0 ;
				Order By 1 Desc, 3 ;
				Into Cursor cExportXLS
		Else
			Select TIP, Left(Alltrim(cod_fiscal), 10) As cod_fiscal, Left(denumire, 100) As denumire, baza + TVA As Total, baza, TVA ;
				From crsD392 ;
				Where baza <> 0 Or TVA <> 0;
				Order By 1 Desc, 3 ;
				Into Cursor cExportXLS
		Endif

		Select cExportXLS
		Copy To (m.tcFisierXLS) Type Xl5
		*OPEN_DEFAULT_APP(m.tcFisierXLS)

		Use In (Select('cExportXLS'))

		Select (m.lcSelect)

	Endproc && do_genereaza_xls

	*===========================================
	Procedure do_genereaza_txt
		Lparameters tcFisierTXT
		*!*	Cale fisier :
		*!*		Windows 7	:	C:\Users\<user>\AppData\Local\VirtualStore\Program Files\D392 (an = 2013)\
		*!*		Windows XP	:	C:\Program Files\D392 (an = 2013)\
		*!*	Nume fisier :
		*!*			D392.txt
		*!*	Structura fisier :	( 2 linii )
		*!*		linia 1 :	392,<an>,<cod fiscal(fara RO)>,<cod caen>,#<X daca este A>#,#<X daca este B>#,<cifra de afaceri>,
		*!*	<baza livrari catre plat.TVA>,<TVA livrari catre plat.TVA>,<baza livrari catre neplat.TVA>,<TVA livrari catre neplat.TVA>,
		*!*	<baza achizitii de la plat.TVA>,<TVA achizitii de la plat.TVA>,<baza achizitii de la neplat.TVA>
		*!*		linia 2 :	#<RO daca este plat.TVA>#,#<denumire firma>#,#<adresa>#,#<telefon>#,#<fax>#,#<email>#,
		*!*	#<nume si prenume declarant>#,#<functia>#

		Local lcOS, lcCaleFisierD392
		lcOS = Os(1)
		lcCaleFisierD392 = [\Program Files\D392 (an = 2013)\]
		Do Case
			Case ("6.02" $ lcOS And Os(11) = "1") ;		&&	WIN 8
				Or ("6.01" $ lcOS And Os(11) = "1") ;	&&	WIN 7
				Or ("6.01" $ lcOS) ;					&&	WIN 2008 R2
				Or ("6.00" $ lcOS And Os(11) = "1") ;	&&	VISTA
				Or ("6.00" $ lcOS)						&&	WIN 2008
				lcCaleFisierD392 = Alltrim(Getenv("LOCALAPPDATA")) + [\VirtualStore] + lcCaleFisierD392
			Case ("5.02" $ lcOS) ;						&&	WIN 2003
				Or ("5.01" $ lcOS) ;					&&	WIN XP
				Or ("5.0" $ lcOS) ;						&&	WIN 2000
				Or ("NT" $ lcOS)						&&	WIN NT
				lcCaleFisierD392 = Alltrim(Getenv("SystemDrive")) + lcCaleFisierD392
			Otherwise
				&&	WIN 98, WIN 95, WIN ME, WIN 31, necunoscut
				amessagebox("Nu se poate copia automat fisierul pentru D392!", 16, "Eroare")
				Return
		Endcase

		If !Directory(lcCaleFisierD392)
			lcCaleFisierD392 = Getdir("c:\", "D392", "Alegeti directorul D392\")
			If Empty(lcCaleFisierD392)
				amessagebox("Nu ati ales directorul!", 16, "Eroare")
				Return
			Endif
		Endif

		Local lcDecl392, lcPoint

		lcPoint = Set("Point")
		Set Point To '.'
		Set Textmerge On To Memv lcDecl392 Noshow
		*!*		linia 1 :	392,<an>,<cod fiscal(fara RO)>,<cod caen>,#<X daca este A>#,#<X daca este B>#,<cifra de afaceri>,
		*!*	<baza livrari catre plat.TVA>,<TVA livrari catre plat.TVA>,<baza livrari catre neplat.TVA>,<TVA livrari catre neplat.TVA>,
		*!*	<baza achizitii de la plat.TVA>,<TVA achizitii de la plat.TVA>,<baza achizitii de la neplat.TVA>
		*!*		linia 2 :	#<RO daca este plat.TVA>#,#<denumire firma>#,#<adresa>#,#<telefon>#,#<fax>#,#<email>#,
		*!*	#<nume si prenume declarant>#,#<functia>#
		With This
			\392,<<Alltrim(Str(.nAn))>>,<<Alltrim(.cCodFiscal)>>,<<Alltrim(.cCodCaen)>>,#<<Iif(.cTip=[A],[X],[])>>#,#<<Iif(.cTip=[B],[X],[])>>#,<<Alltrim(Str(.nCifraDeAfaceri))>>,
			\\<<Alltrim(Str(.nBazaLivrPlTVA))>>,<<Iif(.cTip=[B],[],Alltrim(Str(.nTVALivrPlTVA)))>>,
			\\<<Alltrim(Str(.nBazaLivrNeplTVA))>>,<<Iif(.cTip=[B],[],Alltrim(Str(.nTVALivrNeplTVA)))>>,
			\\<<Iif(.cTip=[A],[],Alltrim(Str(.nBazaAchPlTVA)))>>,<<Iif(.cTip=[A],[],Alltrim(Str(.nTVAAchPlTVA)))>>,
			\\<<Iif(.cTip=[A],[],Alltrim(Str(.nBazaAchNeplTVA)))>>
			\#<<Iif(.lPlatitorTVA,[RO],[])>>#,#<<Alltrim(.cDenumireFirma)>>#,#<<Alltrim(.cAdresa)>>#,#<<Alltrim(.cTelefon)>>#,
			\\#<<Alltrim(.cFax)>>#,#<<Alltrim(.cEmail)>>#,#<<Alltrim(.cDeclarant)>>#,#<<Alltrim(.cFunctieDeclarant)>>#
		Endwith
		Set Textmerge To
		Set Point To &lcPoint

		Strtofile(lcDecl392, lcCaleFisierD392 + [D392.txt])
		Strtofile(lcDecl392, tcFisierTXT)

		amessagebox([Fisierul a fost exportat!] + Chr(13) + Chr(10) + [Intrati in aplicatia D392 si apasati pe butonul ] + ;
			  ["START"], 0 + 64, 'Declaratia 392')
	Endproc && do_genereaza_txt

	*==========================================
	Procedure do_genereaza_xdp_2014
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou

		Local lcSelect, lcXDP
		lcSelect = Select()


		lcXDP = ""
		With This
			Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0"?>
<?xfa generator="XFA2_4" APIVersion="3.3.10270.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>

<form1>
<titlu>
	<tip392><<IIF(this.cTip = 'A', '1' , '2')>></tip392><an_r><<Alltrim(Str(.nAn))>></an_r><luna_r><<Alltrim(Str(.nLuna))>></luna_r><d_rec>0</d_rec><totalPlata_A><<Alltrim(Str(.nCifraDeAfaceri))>></totalPlata_A>
</titlu>
<cap1>
	<nume1><<Alltrim(.cDenumireFirma)>></nume1><adresa1><<Alltrim(.cAdresa)>></adresa1>
	<adresa><tel1><<Alltrim(.cTelefon)>></tel1><fax1><<Alltrim(.cFax)>></fax1><email1><<Alltrim(.cEmail)>></email1></adresa>
	<cif><<Alltrim(.cCodFiscal)>></cif><CAEN><<Alltrim(.cCodCaen)>></CAEN>
</cap1>
<cap2>
	<nume2></nume2><adresa2></adresa2>
	<adresa><tel2></tel2><fax2></fax2><email2></email2></adresa>
	<nr_act></nr_act><data_act></data_act><cif2></cif2>
</cap2>
<cap3><cifra_afaceri><<Alltrim(Str(.nCifraDeAfaceri))>></cifra_afaceri></cap3>
<D392A>
	<val1_L><<Alltrim(Str(.nBazaLivrPlTVA))>></val1_L><val2_L><<Alltrim(Str(.nBazaLivrNeplTVA))>></val2_L>
	<coloana2>
		<tva1_L><<Iif(.cTip=[B],[],Alltrim(Str(.nTVALivrPlTVA)))>></tva1_L>
		<tva2_L><<Iif(.cTip=[B],[],Alltrim(Str(.nTVALivrNeplTVA)))>></tva2_L>
	</coloana2>
</D392A>
<D392B>
	<val1_A><<Iif(.cTip=[A],[],Alltrim(Str(.nBazaAchPlTVA)))>></val1_A>
	<val2_A><<Iif(.cTip=[A],[],Alltrim(Str(.nBazaAchNeplTVA)))>></val2_A>
	<tva1_A><<Iif(.cTip=[A],[],Alltrim(Str(.nTVAAchPlTVA)))>></tva1_A>
</D392B>
<final>
	<nume_declarant><<Alltrim(.cNumeDeclarant)>></nume_declarant><prenume_declarant><<Alltrim(.cPrenumeDeclarant)>></prenume_declarant><functia_declarant><<Alltrim(.cFunctieDeclarant)>></functia_declarant>
</final>
</form1>
</xfa:data>
</xfa:datasets>
<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
</xdp:xdp>
			Endtext
		Endwith
		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				amessagebox('Nu exista fisierul ' + m.tcFisierPDFOriginal, 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)
	Endproc && do_genereaza_xdp_2014

	Procedure UpdatePDF
		Lparameters tcPDFFile
		*** tcPDFFile: calea completa a fisierului pdf D:\CONTAFIN\COMUNCONTAFIN\d392.pdf

		Local lcFile, lcPath, llOpen
		lcFile = Justfname(m.tcPDFFile)
		lcPath = Addbs(Justpath(m.tcPDFFile))
		llOpen = .F.

		Return DownloadSupportFile(m.lcFile, m.lcPath, m.llOpen) && proceduri_comune.prg

	Endproc && UpdatePDF

Enddefine && D392
*=======================================================================


*********************************************************
*** DECLARATIA 205 IMPOZIT DIVIDENDE
*********************************************************
Procedure viz_decl205

	If m.gnLuna = 12
		Local loD205 As "D205"
		loD205 = Createobject("D205") && ovanzcump.vcx
		loD205.do_executa()
	Else
		amessagebox("Pentru a putea genera declaratia 205 trebuie sa intrati in luna decembrie!", 48, "Atentie")
	Endif
Endproc

Define Class D205 As Custom

	nAn	= 0
	nLuna = 0
	cCodFiscal = 0
	cCodCaen = []
	lPlatitorTVA = .F.
	cDenumireFirma = []
	cAdresa = []
	cTelefon = []
	cFax = []
	cEmail = []
	cDeclarant = []
	cNumeDeclarant = []
	cPrenumeDeclarant = []
	cFunctieDeclarant = []
	
	nBeneficiari = 0
	nDividende = 0

	Procedure Init

		With This
			.nAn = gnAn
			.nLuna = m.gnLuna
			.cCodFiscal = Strtran(Alltrim(Upper(Nvl(gofirma.cod_fiscal, ''))), 'RO', '')
			.cCodCaen = Alltrim(gofirma.caen)
			.lPlatitorTVA = (Substr(Alltrim(Upper(Nvl(gofirma.cod_fiscal, ''))), 1, 2) == 'RO')
			.cDenumireFirma = Nvl(Alltrim(gofirma.firma), '')
			.cAdresa = Nvl(Alltrim(gofirma.adresa), '')
			.cTelefon = Nvl(Alltrim(gofirma.telefon), '')
			.cFax = Nvl(Alltrim(gofirma.fax), '')
			.cEmail = Nvl(Alltrim(gofirma.email), '')
			.cDeclarant = Alltrim(gofirma.declarant)
			.cNumeDeclarant = Alltrim(gofirma.nume_declarant)
			.cPrenumeDeclarant = Alltrim(gofirma.prenume_declarant)
			.cFunctieDeclarant = Alltrim(gofirma.functie)
		Endwith
	Endproc

	Procedure do_executa
		Local loFrmDoc As "frm_decl_tipdoc"
		Local lcAppPath, lcCaleFisier, lcCaleFisierPDF, lcCaleTemp, lcComunPath, lcConditieAct, lcCursor
		Local lcFileName, lcFileNamePDF, lcFirma, lcFisierPDFNou, lcFisierPDFOriginal, lcFisierTXT
		Local lcFisierXDP, lcFisierXLS, lcMesaj, lcNumeFisier, lcRun, lcSelect, lcSirId, lcSql, llSucces
		Local lnPos, loEx, lndividende, lnImpozit
		lcSelect = Select()
		*** Initializare cai export
		lcAppPath = Addbs(gcAppPath)
		lcComunPath = lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If lnPos > 0
			lcComunPath = Left(m.lcAppPath, lnPos)
		Endif
		lcComunPath = lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		*** Creez structura director export (caletemp\D394\aaaa_ll\firma\
		llSucces = .F.
		Try
			lcTempPath = m.gcBasePath
			lcCaleFisier = m.lcTempPath + "D205\" && d:\roa\D205\

			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Alltrim(Str(gnAn)) + "_" + Padl(Alltrim(Str(gnLuna)), 2, "0") + "\"&& d:\roa\D205\2023_12\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Strtran(gcFirma, [ ], [_]) + "\" && d:\roa\D205\2023_12\firma\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisierPDF = m.lcCaleFisier  && d:\roa\D205\2023_12\firma\
			If !Directory(m.lcCaleFisierPDF)
				Md (m.lcCaleFisierPDF)
			Endif

			llSucces = .T.
		Catch To loEx
			lcMesaj = "Creare director " + lcCaleFisier + " " + loEx.Message

			amessagebox(lcMesaj, 0 + 48)
		Endtry
		If !llSucces
			Return
		Endif

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', ALLTRIM(Strtran(Alltrim(gofirma.cod_fiscal), 'RO', '')))
		Endif

		lcNumeFisier = "d205_" + gofirma.codfiscalfro + "_" + Alltrim(Str(gnAn))
		Do Case
			Case gnAn >= 2024
				lcFisierPDFOriginal = lcComunPath + "d205_2024.pdf"

			Case gnAn >= 2023
				lcFisierPDFOriginal = lcComunPath + "d205_2023.pdf"

			Case gnAn >= 2022
				lcFisierPDFOriginal = lcComunPath + "d205_2022.pdf"

			Otherwise
				lcFisierPDFOriginal = lcComunPath + "d205_" + ALLTRIM(STR(INT(m.gnAn))) + ".pdf" 
		Endcase

		This.UpdatePDF(m.lcFisierPDFOriginal) && actualizez pdf de la romfast

		lcFileName = m.lcCaleFisier + lcNumeFisier

		lcFileNamePDF = m.lcCaleFisierPDF + lcNumeFisier
		lcFisierPDFNou = m.lcFileNamePDF + [.pdf]
		lcFisierXDP = m.lcFileNamePDF + [.xdp]

		lcFisierTXT = m.lcCaleFisier + lcNumeFisier + [.txt]
		lcFisierXLS = m.lcCaleFisier + lcNumeFisier + [.xls]

		If File(lcFisierTXT)
			Delete File (lcFisierTXT)
		Endif

		If File(lcFisierXLS)
			Delete File (lcFisierXLS)
		Endif
		If File(lcFisierPDFNou)
			Delete File (lcFisierPDFNou)
		Endif
		If File(lcFisierXDP)
			Delete File (lcFisierXDP)
		Endif

		*** citesc notele x = 457 din Reg. Jurnal (inregistrare dividende)
		Text To lcSql Textmerge Noshow
			SELECT UPPER(PARTC) AS BENEFICIAR, COD_FISCALC AS CNP, ROUND(SUM(SUMA),0) AS DIVIDENDE, CAST(0 as Number(20)) as DIVIDENDEP, CAST(0 as Number(20)) as IMPOZIT
			 FROM VACT
			 WHERE AN = <<ALLTRIM(STR(This.nAn))>> AND SCC LIKE '457%'
			GROUP BY PARTC, COD_FISCALC
		Endtext

		lcCursor = [crs205]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		IF m.llSucces
			*** citesc notele 457 = 5xx din Reg. Jurnal (plata dividende)
			*** citesc notele 457 = 446 din Reg. Jurnal (impozit retinut)
			
			Text To lcSql Textmerge Noshow
				SELECT UPPER(PARTD) AS BENEFICIAR, COD_FISCALD AS CNP, 
					ROUND(SUM(CASE WHEN SCC LIKE '5%' THEN SUMA ELSE 0 END),0) AS DIVIDENDE,
					ROUND(SUM(CASE WHEN SCC LIKE '446%' THEN SUMA ELSE 0 END),0) AS IMPOZIT					
				 FROM VACT
				 WHERE AN = <<ALLTRIM(STR(This.nAn))>> AND SCD LIKE '457%' and (SCC LIKE '5%' or SCC LIKE '446%')
				GROUP BY PARTD, COD_FISCALD
			Endtext

			lcCursor = [crs205p]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)
			IF m.llSucces
				SELECT crs205p
				SCAN 
					lcBeneficiar = ALLTRIM(NVL(beneficiar, 'x'))
					lcCNP = ALLTRIM(NVL(cnp, 'x'))
					lnDividende = NVL(dividende, 0)
					lnImpozit = NVL(impozit, 0)
					
					SELECT crs205
					LOCATE FOR NVL(beneficiar, 'x') = m.lcBeneficiar AND NVL(cnp, 'x') = m.lcCNP
					IF FOUND()
						REPLACE dividendep WITH m.lnDividende, impozit WITH m.lnImpozit
					ENDIF 
				ENDSCAN 
			ENDIF 
			USE IN (SELECT('crs205p'))
		ENDIF 
				
		If m.llSucces
			Do Case
				Case This.nAn >= 2024
					This.do_genereaza_xdp_2024(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

					If File(lcFisierXDP) And File(m.lcFisierPDFNou)
						open_default_app(lcFisierXDP)
					ENDIF

				Case This.nAn >= 2023
					This.do_genereaza_xdp_2023(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

					If File(lcFisierXDP) And File(m.lcFisierPDFNou)
						open_default_app(lcFisierXDP)
					ENDIF

				Case This.nAn >= 2022
					This.do_genereaza_xdp_2022(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

					If File(m.lcFisierXDP)
						open_default_app(m.lcFisierXDP)
					Endif

			Endcase

		
			*** deschid explorer in directorul de export
			lcRun = [RUN /N7 explorer.exe "] + m.lcCaleFisier + ["]
			&lcRun
		Endif && llSucces

		Use In (Select('crs205'))

		Select (m.lcSelect)

	Endproc && do_executa

	*==========================================
	Procedure do_genereaza_xdp_2024
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou

		Local lcSelect, lcXDP
		lcSelect = Select()

		This.nBeneficiari = RECCOUNT('crs205')
		CALCULATE SUM(dividende) TO lnDividende IN crs205
		This.nDividende = INT(m.lnDividende)
		lnTotalPlata = this.nBeneficiari + this.nDividende
		
		lcXDP = ""
		With This
			Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0"?>
<?xfa generator="XFA2_4" APIVersion="3.3.10270.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>

<form1>
   <ID0>
      <d_rec>0</d_rec>
      <an_r><<Alltrim(Str(.nAn))>></an_r>
      <luna_r><<Alltrim(Str(.nLuna))>></luna_r>
      <cifS />
      <d_succ>0</d_succ>
   </ID0>
   <ID1>
      <cif><<Alltrim(.cCodFiscal)>></cif>
      <RO>RO</RO>
      <den><<Alltrim(.cDenumireFirma)>></den>
      <adresa><<Alltrim(.cAdresa)>></adresa>
      <telefon><<Alltrim(.cTelefon)>></telefon>
      <fax><<Alltrim(.cFax)>></fax>
      <mail><<Alltrim(.cEmail)>></mail>
   </ID1>
   <ID4>
      <FUNCTIA><<Alltrim(.cFunctieDeclarant)>></FUNCTIA>
      <PRENUME><<Alltrim(.cPrenumeDeclarant)>></PRENUME>
      <NUME><<Alltrim(.cNumeDeclarant)>></NUME>
      <totalPlata_A><<m.lnTotalPlata>></totalPlata_A>
   </ID4>
   <T2>
      <calc_aut>1</calc_aut>
   </T2>
   <T3>
      <repet>
         <CATEG>08. Venituri din dividende       (col.7,8,9,10)</CATEG>
      </repet>
   ENDTEXT 
   
   SELECT crs205
   SCAN 
	   Text To lcXDP Additive Textmerge Noshow Pretext 1
      <repet1>
         <Tara_V />
         <seq1><<RECNO()>></seq1>
         <NUME_BENEF><<ALLTRIM(beneficiar)>></NUME_BENEF>
         <Rezid>1</Rezid>
         <Stat_R />
         <CNP_BENEF><<ALLTRIM(cnp)>></CNP_BENEF>
         <CNP_BENEF_S />
         <CASTIG><<INT(dividende)>></CASTIG>
         <PIERDERE><<INT(dividendep)>></PIERDERE>
         <BAZA><<INT(dividende)>></BAZA>
         <IMPOZIT><<INT(impozit)>></IMPOZIT>
         <NATURA>2</NATURA>
      </repet1>
	   ENDTEXT
   ENDSCAN 
   
	Text To lcXDP Additive Textmerge Noshow Pretext 1      
   </T3>

</form1>

</xfa:data>
</xfa:datasets>
<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
</xdp:xdp>
	Endtext
	Endwith
	
		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				amessagebox('Nu exista fisierul ' + m.tcFisierPDFOriginal, 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)
	Endproc && do_genereaza_xdp_2024
	
	*==========================================
	Procedure do_genereaza_xdp_2023
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou

		Local lcSelect, lcXDP
		lcSelect = Select()

		This.nBeneficiari = RECCOUNT('crs205')
		CALCULATE SUM(dividende) TO lnDividende IN crs205
		This.nDividende = INT(m.lnDividende)
		lnTotalPlata = this.nBeneficiari + this.nDividende
		
		lcXDP = ""
		With This
			Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0"?>
<?xfa generator="XFA2_4" APIVersion="3.3.10270.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>

<form1>
   <ID0>
      <d_rec>0</d_rec>
      <an_r><<Alltrim(Str(.nAn))>></an_r>
      <luna_r><<Alltrim(Str(.nLuna))>></luna_r>
      <cifS />
      <d_succ>0</d_succ>
   </ID0>
   <ID1>
      <cif><<Alltrim(.cCodFiscal)>></cif>
      <RO>RO</RO>
      <den><<Alltrim(.cDenumireFirma)>></den>
      <adresa><<Alltrim(.cAdresa)>></adresa>
      <telefon><<Alltrim(.cTelefon)>></telefon>
      <fax><<Alltrim(.cFax)>></fax>
      <mail><<Alltrim(.cEmail)>></mail>
   </ID1>
   <ID4>
      <FUNCTIA><<Alltrim(.cFunctieDeclarant)>></FUNCTIA>
      <PRENUME><<Alltrim(.cPrenumeDeclarant)>></PRENUME>
      <NUME><<Alltrim(.cNumeDeclarant)>></NUME>
      <totalPlata_A><<m.lnTotalPlata>></totalPlata_A>
   </ID4>
   <CATEGORII>
      <V04>0</V04>
      <V13>0</V13>
      <V01>0</V01>
      <V24>0</V24>
      <V08>1</V08>
      <V09>0</V09>
      <V11>0</V11>
      <V12>0</V12>
      <V18>0</V18>
      <V15>0</V15>
      <V16>0</V16>
      <V22>0</V22>
      <V02>0</V02>
      <V03>0</V03>
      <V05>0</V05>
      <V06>0</V06>
      <V07>0</V07>
      <V10>0</V10>
      <V14>0</V14>
      <V17>0</V17>
      <V19>0</V19>
      <V20>0</V20>
      <V21>0</V21>
      <V23>0</V23>
   </CATEGORII>
   <T2>
      <calc_aut>1</calc_aut>
   </T2>
   <T3>
      <repet>
         <CATEG>08. Venituri din dividende       (col.7,8)</CATEG>
      </repet>
   ENDTEXT 
   
   SELECT crs205
   SCAN 
	   Text To lcXDP Additive Textmerge Noshow Pretext 1
      <repet1>
         <Tara_V />
         <seq1><<RECNO()>></seq1>
         <NUME_BENEF><<ALLTRIM(beneficiar)>></NUME_BENEF>
         <Rezid>1</Rezid>
         <Stat_R />
         <CNP_BENEF><<ALLTRIM(cnp)>></CNP_BENEF>
         <CNP_BENEF_S />
         <CASTIG><<INT(dividende)>></CASTIG>
         <PIERDERE />
         <BAZA>0</BAZA>
         <IMPOZIT>0</IMPOZIT>
         <NATURA>2</NATURA>
      </repet1>
	   ENDTEXT
   ENDSCAN 
   
	Text To lcXDP Additive Textmerge Noshow Pretext 1      
   </T3>

</form1>

</xfa:data>
</xfa:datasets>
<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
</xdp:xdp>
	Endtext
	Endwith
	
		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				amessagebox('Nu exista fisierul ' + m.tcFisierPDFOriginal, 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)
	Endproc && do_genereaza_xdp_2023
	
	*==========================================
	Procedure do_genereaza_xdp_2022
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou

		Local lcSelect, lcXDP
		lcSelect = Select()

		This.nBeneficiari = RECCOUNT('crs205')
		CALCULATE SUM(dividende) TO lnDividende IN crs205
		This.nDividende = INT(m.lnDividende)
		lnTotalPlata = this.nBeneficiari + this.nDividende
		
		lcXDP = ""
		With This
			Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0"?>
<?xfa generator="XFA2_4" APIVersion="3.3.10270.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>

<form1>
   <ID0>
      <d_rec>0</d_rec>
      <an_r><<Alltrim(Str(.nAn))>></an_r>
      <luna_r><<Alltrim(Str(.nLuna))>></luna_r>
      <cifS />
      <d_succ>0</d_succ>
   </ID0>
   <ID1>
      <cif><<Alltrim(.cCodFiscal)>></cif>
      <RO>RO</RO>
      <den><<Alltrim(.cDenumireFirma)>></den>
      <adresa><<Alltrim(.cAdresa)>></adresa>
      <telefon><<Alltrim(.cTelefon)>></telefon>
      <fax><<Alltrim(.cFax)>></fax>
      <mail><<Alltrim(.cEmail)>></mail>
   </ID1>
   <ID4>
      <FUNCTIA><<Alltrim(.cFunctieDeclarant)>></FUNCTIA>
      <PRENUME><<Alltrim(.cPrenumeDeclarant)>></PRENUME>
      <NUME><<Alltrim(.cNumeDeclarant)>></NUME>
      <totalPlata_A><<m.lnTotalPlata>></totalPlata_A>
   </ID4>
   <CATEGORII>
      <V04>0</V04>
      <V13>0</V13>
      <V01>0</V01>
      <V24>0</V24>
      <V08>1</V08>
      <V09>0</V09>
      <V11>0</V11>
      <V12>0</V12>
      <V18>0</V18>
      <V15>0</V15>
      <V16>0</V16>
      <V22>0</V22>
      <V02>0</V02>
      <V03>0</V03>
      <V05>0</V05>
      <V06>0</V06>
      <V07>0</V07>
      <V10>0</V10>
      <V14>0</V14>
      <V17>0</V17>
      <V19>0</V19>
      <V20>0</V20>
      <V21>0</V21>
      <V23>0</V23>
   </CATEGORII>
   <T2>
      <calc_aut>1</calc_aut>
   </T2>
   <T3>
      <repet>
         <CATEG>08. Venituri din dividende</CATEG>
      </repet>
   ENDTEXT 
   
   SELECT crs205
   SCAN 
	   Text To lcXDP Additive Textmerge Noshow Pretext 1
      <repet1>
         <Tara_V />
         <seq1><<RECNO()>></seq1>
         <NUME_BENEF><<ALLTRIM(beneficiar)>></NUME_BENEF>
         <Rezid>1</Rezid>
         <Stat_R />
         <CNP_BENEF><<ALLTRIM(cnp)>></CNP_BENEF>
         <CNP_BENEF_S />
         <CASTIG><<INT(dividende)>></CASTIG>
         <PIERDERE />
         <BAZA>0</BAZA>
         <IMPOZIT>0</IMPOZIT>
         <NATURA>2</NATURA>
      </repet1>
	   ENDTEXT
   ENDSCAN 
   
	Text To lcXDP Additive Textmerge Noshow Pretext 1      
   </T3>

</form1>

</xfa:data>
</xfa:datasets>
<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
</xdp:xdp>
	Endtext
	Endwith
	
		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				amessagebox('Nu exista fisierul ' + m.tcFisierPDFOriginal, 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)
	Endproc && do_genereaza_xdp_2022

	Procedure UpdatePDF
		Lparameters tcPDFFile
		*** tcPDFFile: calea completa a fisierului pdf D:\CONTAFIN\COMUNCONTAFIN\d392.pdf

		Local lcFile, lcPath, llOpen
		lcFile = Justfname(m.tcPDFFile)
		lcPath = Addbs(Justpath(m.tcPDFFile))
		llOpen = .F.

		Return DownloadSupportFile(m.lcFile, m.lcPath, m.llOpen) && proceduri_comune.prg

	Endproc && UpdatePDF

Enddefine && D205
*=======================================================================

*********************************************************
*** DECLARATIA 394 ACHIZITII/LIVRARI INTERNE
*********************************************************
Procedure viz_decl394
	Local loD394 As "D394"

	Do Case
		Case m.gnAn * 12 + m.gnLuna >= 2025 * 12 + 8 && TVA 21%, 11% de la 01.08.2025
			* 08/2025
			loD394 = Createobject("D394_2025") && ovanzcump.vcx
			loD394.List()
		Case m.gnAn * 12 + m.gnLuna >= 2016 * 12 + 7 && 'AMDX4'$UPPER(SYS(0)) AND MESSAGEBOX('D394 2016?',4+32,_screen.Caption) = 6
			* 07/2016
			loD394 = Createobject("D394_2016") && ovanzcump.vcx
			loD394.List()
		Otherwise
			loD394 = Createobject("D394") && ovanzcump.vcx
			loD394.do_executa()
	Endcase

Endproc

Define Class D394_2025 As Custom

	Total = 0
	rectificativa = 0
	TIP = 'L'
	Perioada = 1
	NumarCoduriFiscale = 0

	oForm = Null && formular D384
	oUnitate = Null

	CaleFisierPDF = '' && director fisier pdf
	cFisierPDF = '' && fisier pdf

	* Afiseaza formularul 394 fara facturi, pentru alegerea tipurilor de documente, optiuni etc.
	* Din formular se vor selecta facturile in functie de optiuni
	Procedure List

		Local loDecl As "frm_decl394_2025"
		Local lcSelect, lcTip, llSilent, llSucces, lnNumarLuni, lcValoare
		llSilent = .F. && m.tlSilent

		lcTip = Upper(Nvl(citeste_optiune('D394_TIP'), 'LUNAR'))
		lcTip = Iif(!Inlist(m.lcTip, 'LUNAR', 'TRIMESTRIAL', 'SEMESTRIAL', 'ANUAL'), 'LUNAR', m.lcTip)

		lcSelect = Select()

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', Strtran(Alltrim(gofirma.cod_fiscal), 'RO', ''))
		Endif

		Private pounitate
		pounitate = Createobject("empty")
		AddProperty(pounitate, "tva_incasare", goCalendar.tva_incasare)

		AddProperty(pounitate, "an", m.gnAn)
		AddProperty(pounitate, "luna1", m.gnLuna)
		AddProperty(pounitate, "luna2", m.gnLuna)

		AddProperty(pounitate, "tip", m.lcTip)
		AddProperty(pounitate, "luniextra", 0)
		AddProperty(pounitate, "tvanedeductibil", 0)
		AddProperty(pounitate, "codi", Strtran(Strtran(Alltrim(Upper(Nvl(gofirma.cod_fiscal, ''))), 'RO', ''), 'R', ''))
		AddProperty(pounitate, "deni", Nvl(Alltrim(gofirma.firma), ''))
		AddProperty(pounitate, "adr", Nvl(Alltrim(gofirma.adresa), ''))
		poUnitate.adr = PADR(poUnitate.adr, 150, ' ')			
		AddProperty(pounitate, "tel", Nvl(Alltrim(gofirma.telefon), ''))
		AddProperty(pounitate, "fax", Nvl(Alltrim(gofirma.fax), ''))
		AddProperty(pounitate, "email", Nvl(Alltrim(gofirma.email), ''))

		AddProperty(pounitate, "taraR", "")
		AddProperty(pounitate, "codiR", "")
		AddProperty(pounitate, "deniR", "")
		AddProperty(pounitate, "adrR", "")
		AddProperty(pounitate, "telR", "")
		AddProperty(pounitate, "faxR", "")
		AddProperty(pounitate, "emailR", "")

		AddProperty(pounitate, "numeD", "")
		AddProperty(pounitate, "functieD", "")

		AddProperty(pounitate, "operatiuni", 1)
		AddProperty(pounitate, "tvaincasare", goCalendar.tva_incasare)
		AddProperty(pounitate, "nr_facturi_emise", 0)
		AddProperty(pounitate, "nr_facturi_beneficiari", 0)
		AddProperty(pounitate, "nr_facturi_terti", 0)				

		AddProperty(pounitate, "lPerioadaDoarRO", Between(m.gnAn * 12 + m.gnLuna, 2016 * 12 + 7, 2016 * 12 + 9)) && intre 07/2016 si 09/2016 se declara doar partenerii cu RO
		AddProperty(pounitate, "ExportSectiuneaI", 1) && intre 07/2016 si 09/2016 nu se completeaza sectiunea I
		AddProperty(pounitate, "PersoaneAfiliate", 0) && 0 = fara persoane afiliate, 1 = cu persoane afiliate

		AddProperty(pounitate, "DenRepr", Alltrim(citeste_optiune('D394_DEN_REPREZ')))
		AddProperty(pounitate, "AdrRepr", Alltrim(citeste_optiune('D394_ADR_REPREZ')))
		poUnitate.AdrRepr = PADR(poUnitate.AdrRepr, 150, ' ')		
		AddProperty(pounitate, "CuiRepr", Alltrim(citeste_optiune('D394_CUI_REPREZ')))
		AddProperty(pounitate, "TipRepr", Alltrim(citeste_optiune('D394_TIP_REPREZ')))
		AddProperty(pounitate, "TelRepr", Alltrim(citeste_optiune('D394_TEL_REPREZ')))
		AddProperty(pounitate, "FaxRepr", Alltrim(citeste_optiune('D394_FAX_REPREZ')))
		AddProperty(pounitate, "EmailRepr", Alltrim(citeste_optiune('D394_EMAIL_REPREZ')))

		AddProperty(pounitate, "DenIntocmit", Alltrim(citeste_optiune('D394_DEN_INTOCMIT')))
		AddProperty(pounitate, "CuiIntocmit", Alltrim(citeste_optiune('D394_CUI_INTOCMIT')))
		AddProperty(pounitate, "PJIntocmit", Int(Val(Nvl(citeste_optiune('D394_PJ_INTOCMIT'), '1'))) )
		AddProperty(pounitate, "CalitateIntocmit", Alltrim(citeste_optiune('D394_CALITATE_INTOCMIT')))
		poUnitate.CalitateIntocmit = PADR(poUnitate.CalitateIntocmit, 100, ' ')
		AddProperty(pounitate, "Calitate2Intocmit", Alltrim(citeste_optiune('D394_CALITATE2_INTOCMIT')))
		poUnitate.Calitate2Intocmit = PADR(poUnitate.Calitate2Intocmit, 100, ' ')		
		AddProperty(pounitate, "FunctieIntocmit", Alltrim(citeste_optiune('D394_FUNCTIE_INTOCMIT')))
		poUnitate.FunctieIntocmit= PADR(poUnitate.FunctieIntocmit, 100, ' ')		

		AddProperty(pounitate, "NrAMEF", Int(Val(Nvl(citeste_optiune('D394_NR_AMEF'), '1'))))
		AddProperty(pounitate, "OptiuneANAF", Int(Val(Nvl(citeste_optiune('D394_OPTIUNE_ANAF'), '0'))))	 && 1 = Se da acordul pentru consultarea D394 de catre furnizori/clienti

		AddProperty(pounitate, "cTextFacturiEmiseAlocate", "") && facturi emise care nu sunt in plaja de facturi alocate

		This.oUnitate = pounitate


		Create Cursor crsFdocTip (id_fdoc N(10) null, fel_document c(100) null, tip_doc c(10) Null, bifa N(1))

		*!*			llSucces = update_fdoc([NVL(tip_doc,'!') <> '!'], 'crsFdocTipTemp') && doar fel document cu tip
		*!*			AddProperty(pounitate, "tipdoc_factura", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'F'])) && lista tip documente = factura
		*!*			AddProperty(pounitate, "tipdoc_bonfiscal_cu_cui",  cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'C'])) && lista tip documente = bon fiscal cu factura
		*!*			AddProperty(pounitate, "tipdoc_bonfiscal_fara_cui", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'B'])) && lista tip documente = bon fiscal fara factura
		*!*			AddProperty(pounitate, "tipdoc_factura_simplificata", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'M'])) && lista tip documente = factura simplificata
		*!*			AddProperty(pounitate, "tipdoc_exceptate_amef", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'E'])) && lista tip documente = exceptate de la AMEF (bilete)
		*!*			AddProperty(pounitate, "tipdoc_borderou_achizitii", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'O'])) && lista tip documente = borderou achizitii
		*!*			AddProperty(pounitate, "tipdoc_carnet_comercializare", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'Z'])) && lista tip documente = carnet comercialziare
		*!*			AddProperty(pounitate, "tipdoc_achizitii_second_hand", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'H'])) && lista tip documente = achizitii second hand
		*!*			AddProperty(pounitate, "tipdoc_servicii_turism", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'U'])) && lista tip documente = servicii turism


		llSucces = update_serii(.F., [isautofactura = 1 or isbeneficiari = 1 or isterti = 1 or isfurnizori = 1]) && crsSerii - seriile marcate autofactura/emise de beneficiari/emise de terti/emise in numele furnizorilor!
		If !m.llSucces
			Create Cursor crsSerii (seria c(20), isautofactura N(1), isbeneficiari N(1), isterti N(1), isfurnizori N(1))
		Endif

		Create Cursor crsSectiuneaI (punct c(4) Null, baza N(20) Null, TVA N(20) Null, proctva N(2) Null)
		Create Cursor crsSectiuneaI4 (punct c(4) Null, TVA21 N(20) Null, TVA11 N(20) Null, TVA19 N(20) Null, TVA09 N(20) Null, TVA05 N(20) Null, TVA24 N(20) Null, TVA20 N(20) Null)
		Create Cursor crsSectiuneaI6 (punct c(4) Null, incasari N(20) Null, costuri N(20) Null, marja N(20) Null, TVA N(20) Null) && agentii turism si second hand
		Create Cursor crsSectiuneaI7 (caen c(4) Null, TIP c(10), baza N(20), TVA21 N(20) Null, TVA11 N(20) Null, TVA19 N(20) Null, TVA09 N(20) Null, TVA05 N(20) Null, TVA24 N(20) Null, TVA20 N(20) Null)

		Create Cursor crsFacturiAlocate (seria c(20) Null, nr1 N(20), nr2 N(20))
		Create Cursor crsFacturiEmise (seria c(20) Null, nr1 N(20), nr2 N(20))
		Create Cursor crsFacturiEmiseTip (TIP c(20), ntip N(1), seria c(20) Null, nr N(20))
		Create Cursor crsAutoFacturi (seria c(20) Null, nr N(20), baza21 N(16, 4) Null, baza11 N(16, 4) Null, baza19 N(16, 4) Null, baza09 N(16, 4) Null, baza05 N(16, 4) Null, baza24 N(16, 4) Null, baza20 N(16, 4) Null, ;
			  TVA21 N(16, 4) Null, TVA11 N(16, 4) Null, TVA19 N(16, 4) Null, TVA09 N(16, 4) Null, TVA05 N(16, 4) Null, TVA24 N(16, 4) Null, TVA20 N(16, 4) Null)

		Create Cursor crsFacturiTerti (punct c(4) Null, seria c(20) Null, nr1 N(20), nr2 N(20), denumire c(100) Null, cod_fiscal c(20) Null)
		Create Cursor crsSeriiFacturi (TIP c(20), seria c(20), descriere c(100))
		Insert Into crsSeriiFacturi (TIP, seria, descriere) ;
			Select 'AUTOFACTURA' As TIP, serie As seria, 'serie autofacturi' As descriere ;
			From crsSerii Where isautofactura = 1 ;
			Union ;
			Select 'BENEFICIARI' As TIP, serie As seria, 'serie facturi emise de beneficiari in numele persoanei impozabile' As descriere ;
			From crsSerii Where isbeneficiari = 1 ;
			Union ;
			Select 'TERTI' As TIP, serie As seria, 'serie facturi emise de terti in numele persoanei impozabile' As descriere ;
			From crsSerii Where isterti = 1 ;
			Union ;
			Select 'FURNIZORI' As TIP, serie As seria, 'serie facturi emise de pers impozabila in calitate de beneficiar, in numele furnizorilor' As descriere ;
			From crsSerii Where isfurnizori = 1

		Go Top In crsSeriiFacturi
		AddProperty(pounitate, "serie_autofacturare", cursor2lista("crsSerii", "serie", ",", [isautofactura=1]))
		AddProperty(pounitate, "serie_facturi_beneficiari", cursor2lista("crsSerii", "serie", ",", [isbeneficiari=1]))
		AddProperty(pounitate, "serie_facturi_terti", cursor2lista("crsSerii", "serie", ",", [isterti=1]))
		AddProperty(pounitate, "serie_facturi_furnizori", cursor2lista("crsSerii", "serie", ",", [isfurnizori=1]))

		close_serii()

		Create Cursor crsAMEF (TIP N(1), luna N(2), nramef N(10), nrbonuri N(10), incasari N(16, 4), ;
			  baza21 N(16, 4) Null, baza11 N(16, 4) Null, baza19 N(16, 4) Null, baza09 N(16, 4) Null, baza05 N(16, 4) Null, baza20 N(16, 4) Null, ;
			  TVA21 N(16, 4) Null, TVA11 N(16, 4) Null, TVA19 N(16, 4) Null, TVA09 N(16, 4) Null, TVA05 N(16, 4) Null, TVA20 N(16, 4) Null)

		Create Cursor crsJTVA(seq N(20), tipj c(2), tippart N(1), tipdoc c(2) Null, TIP c(2), dataact D Null, dataireg D Null, serie_act c(10) Null,  nract N(20) Null, id_fdoc N(12) Null, fel_document c(30) Null, id_sucursala N(7) Null, sucursala c(100) Null, ;
			  iscif N(1) Null, cifvalid N(1) Null, id_part N(20) Null, cod_fiscal c(30) Null, cod_fiscalfro c(30) Null, atribut_fiscal c(2) Null, denumire c(100) Null, TIP_PERSOANA N(1) Null, AFILIAT N(5) Null, CODTARA V(5) NULL, ;
			  TARA c(100) Null, COD_TARA c(10) Null, JUDET c(100) Null, COD_JUDET c(10) Null, LOCALITATE c(100) Null, STRADA c(100) Null, NUMAR c(20) Null, Bloc c(20) Null, SCARA c(50) Null, APART c(100) Null, ;
			  baza N(16, 4) Null, TVA N(16, 4) Null, tibaza N(16, 4) Null, titva N(16, 4) Null, ;
			  baza21 N(16, 4) Null, baza11 N(16, 4) Null, baza19 N(16, 4) Null, baza09 N(16, 4) Null, baza05 N(16, 4) Null, baza24 N(16, 4) Null, baza20 N(16, 4) Null, baza00 N(16, 4) Null, ;
			  TVA21 N(16, 4) Null, TVA11 N(16, 4) Null, TVA19 N(16, 4) Null, TVA09 N(16, 4) Null, TVA05 N(16, 4) Null, TVA24 N(16, 4) Null, TVA20 N(16, 4) Null, ;
			  tibaza21 N(16, 4) Null, tibaza11 N(16, 4) Null, tibaza19 N(16, 4) Null, tibaza09 N(16, 4) Null, tibaza05 N(16, 4) Null, tibaza24 N(16, 4) Null, tibaza20 N(16, 4) Null, ;
			  titva21 N(16, 4) Null, titva11 N(16, 4) Null, titva19 N(16, 4) Null, titva09 N(16, 4) Null, titva05 N(16, 4) Null, titva24 N(16, 4) Null, titva20 N(16, 4) Null, ;
			  tva_incasare N(1), FACT_TVA_INCASARE N(1), NRFACT N(1), EMISA_PERIOADA N(1), ;
			  ro21t N(16, 4) Null, ro11t N(16, 4) Null, ro19t N(16, 4) Null, RO09T N(16, 4) Null, ro05t N(16, 4) Null, ro24t N(16, 4) Null, ro20t N(16, 4) Null)

		loDecl = Createobject("frm_decl394_2025")
		*DO FORM frm_decl394_2016 NAME loDecl NOSHOW
		loDecl.oParent = This
		This.oForm = loDecl
		loDecl.Show(1)

		scrie_optiune('D394_DEN_REPREZ', pounitate.DenRepr, 'D394 denumire reprezentant')
		scrie_optiune('D394_ADR_REPREZ', pounitate.AdrRepr, 'D394 adresa reprezentant')
		scrie_optiune('D394_CUI_REPREZ', pounitate.CUIRepr, 'D394 CUI reprezentant')
		scrie_optiune('D394_TIP_REPREZ', pounitate.TipRepr, 'D394 tip reprezentant')
		scrie_optiune('D394_TEL_REPREZ', pounitate.TelRepr, 'D394 telefon reprezentant')
		scrie_optiune('D394_FAX_REPREZ', pounitate.FaxRepr, 'D394 fax reprezentant')
		scrie_optiune('D394_EMAIL_REPREZ', pounitate.EmailRepr, 'D394 email reprezentant')
		scrie_optiune('D394_DEN_INTOCMIT', pounitate.DenIntocmit, 'D394 denumire intocmit')
		scrie_optiune('D394_CUI_INTOCMIT', pounitate.CUIIntocmit, 'D394 CUI intocmit')
		scrie_optiune('D394_PJ_INTOCMIT', Alltrim(Str(pounitate.PJIntocmit)), 'D394 persoana juridica intocmit')
		scrie_optiune('D394_CALITATE_INTOCMIT', pounitate.CalitateIntocmit, 'D394 calitate intocmit')
		scrie_optiune('D394_CALITATE2_INTOCMIT', pounitate.Calitate2Intocmit, 'D394 alta calitate intocmit')
		scrie_optiune('D394_FUNCTIE_INTOCMIT', pounitate.FunctieIntocmit, 'D394 functie intocmit')
		scrie_optiune('D394_TIP', pounitate.TIP, 'Tip declaratie 394: LUNAR/TRIMESTRIAL/SEMESTRIAL/ANUAL')
		scrie_optiune('D394_NR_AMEF', Alltrim(Str(pounitate.nramef)), 'D394 nr AMEF')
		scrie_optiune('D394_OPTIUNE_ANAF', Alltrim(Str(pounitate.OptiuneANAF)), 'D394 optiunea ANAF; 1=DA/0=NU')

		close_fdoc()
	Endproc && List

	***************************************************
	* Citeste JV/JC
	* Configureaza tipurile de documente (facturi, facturi simplificate, bonuri fiscale etc.)
	***************************************************
	Procedure do_citeste_date
		Local llSucces, lcSelect, loUnitate
		Local lcTip, lnAn, lnLuna1, lnLuna2, lnNumarLuni, lnPerioada
		lcSelect = Select()
		loUnitate = This.oUnitate

		* Luna de inceput a perioadei
		* 1 = gnLuna
		* 3 = 1,4,7,10
		* 6 = 1,7
		* 12 = 1
		lnAn = loUnitate.an
		lcTip = loUnitate.TIP
		This.TIP = Left(m.lcTip, 1)
		lnNumarLuni = Iif(m.lcTip = "TRIMESTRIAL", 3, Iif(m.lcTip = "SEMESTRIAL", 6, Iif(m.lcTip = "ANUAL", 12, 1)))

		lnPerioada = Ceil(m.gnLuna / m.lnNumarLuni)
		lnLuna1 = (lnPerioada - 1) * lnNumarLuni + 1
		lnLuna2 = lnPerioada * lnNumarLuni

		loUnitate.luna1 = m.lnLuna1
		loUnitate.luna2 = m.lnLuna2

		llPerioadaDoarRO = Between(m.gnAn * 12 + m.gnLuna, 2016 * 12 + 7, 2016 * 12 + 9) && intre 07/2016 si 09/2016 se iau doar partenerii cu RO

		* Citeste JV/JC
		Wait Window 'Se citesc date din Registrele TVA...' Nowait
		llSucces = This.citeste_date_jtva()

		If m.llSucces And Used('crsJtva')
			Wait Window 'Se proceseaza datele din Registrele TVA...' Nowait
			llSucces = This.configureaza_date_jtva()
		Else
			llSucces = .F.
		Endif

		If m.llSucces
			Wait Window 'Se pregatesc sectiunile 1 si 2...' Nowait
			llSucces =	This.pregateste_sectiunea_1_2()
		Endif

		If m.llSucces
			Wait Window 'Se pregatesc sectiunile 4 si 5...' Nowait
			llSucces =	This.pregateste_sectiunea_4_5()
		Endif

		If m.llSucces
			Wait Window 'Se pregateste sectiunea 6...' Nowait
			llSucces =	This.pregateste_sectiunea_6()
		Endif

		If m.llSucces
			Wait Window 'Se pregateste sectiunea 7...' Nowait
			llSucces =	This.pregateste_sectiunea_7()
		Endif

		If m.llSucces
			Wait Window 'Se pregateste sectiunea AMEF...' Nowait
			llSucces =	This.pregateste_sectiunea_amef()
			If loUnitate.lPerioadaDoarRO
				Delete From crsAMEF && nu se declara bonurile fiscale in perioada 07-09/2016
			Endif
		Endif

*!*			If m.llSucces
*!*				Wait Window 'Se pregateste sectiunea persoane fizice...' Nowait
*!*				llSucces =	This.pregateste_sectiunea_facturi_pers_fizice_10000()
*!*				If loUnitate.lPerioadaDoarRO
*!*					Delete From crsPersFizice && nu se declara bonurile fiscale in perioada 07-09/2016
*!*				Endif
*!*			Endif

		Go Top In crsJTVA

		Select (m.lcSelect)
		Return m.llSucces
	Endproc

	* Citeste JV/JC
	Procedure citeste_date_jtva

		Private pnIdSetAnulat && set facturi anulate
		pnIdSetAnulat = Iif(Type('gnIdSetAnulat') = 'N', m.gnIdSetAnulat, 99996)

		Local lcConditieAct, lcConditieLuna, lcCursor, lcSql, lnAn, lnLuna1, lnLuna2, lnSucces, loUnitate, llPerioadaDoarRO
		loUnitate = This.oUnitate
		lnAn = loUnitate.an
		lnLuna1 = loUnitate.luna1
		lnLuna2 = loUnitate.luna2
		llPerioadaDoarRO = loUnitate.lPerioadaDoarRO

		lcConditieLuna = [ (AN  = ] + Alltrim(Str(m.lnAn)) + [ and (luna BETWEEN ] + Alltrim(Str(m.lnLuna1)) + [ and ] + Alltrim(Str(m.lnLuna2)) + [ ))]

		*** iau doar facturile din luna emiterii
		lcConditieAct = [ and (extract(year from dataact)*12+extract(month from dataact) = an*12+luna)]

		* Iau toate inregistrarile din Registrele de TVA, INCLUSIV FATURILE EMISE ANTERIOR PERIOADEI DE DECLARARE, pentru calcul sectiune 4 si 5 (TVA EXIGIBIL)
		Text To lcSql Textmerge Noshow
SELECT 'JV' AS TIPJ, 0 AS TIPPART, TIP_DOC as tipdoc, 'L ' as tip, dataact, dataireg, serie_act, nract, id_fdoc, fel_document, id_sucursala, sucursala,
   	 0 as ISCIF, 0 AS CIFVALID,
	 J.ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', '') AS COD_FISCALFRO, SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) AS ATRIBUT_FISCAL, DENUMIRE, TIP_PERSOANA, AFILIAT, CODTARA,
	 A.TARA, A.COD_TARA, A.JUDET, A.COD_JUDET, A.LOCALITATE, A.STRADA, A.BLOC, A.SCARA, A.NUMAR, A.APART,
	 RO21B + RO11B + RO19B + RO9B + RO5B + RO21NB + RO11NB + RO19NB + RO9NB + RO5NB AS BAZA,
	 RO21T + RO11T + RO19T + RO9T + RO5T + RO21NT + RO11NT + RO19NT + RO9NT + RO5NT AS TVA,
	 ROTI AS TIBAZA,
	 0 AS TITVA,
	 RO21B + RO21NB as BAZA21, RO11B + RO11NB AS BAZA11, RO19B + RO19NB AS BAZA19, RO9B + RO9NB AS BAZA09, RO5B + RO5NB AS BAZA05, RO24B + RO24NB as BAZA24, RO20B + RO20NB AS BAZA20, 0.00 AS BAZA00,
	 RO21T + RO21NT as TVA21, RO11T + RO11NT AS TVA11, RO19T + RO19NT AS TVA19, RO9T + RO9NT AS TVA09, RO5T + RO5NT AS TVA05, RO24T + RO24NT AS TVA24, RO20T + RO20NT AS TVA20,
	 ROTI AS TIBAZA21, 0.00 AS TIBAZA11, 0.00 AS TIBAZA19, 0.00 AS TIBAZA09, 0.00 AS TIBAZA05, 0.00 AS TIBAZA24, 0 AS TIBAZA20, 
	 0.00 as TITVA21, 0.00 AS TITVA11, 0.00 AS TITVA19, 0.00 AS TITVA09, 0.00 AS TITVA05, 0.00 as TITVA24, 0.00 AS TITVA20, 
	 TVA_INCASARE, 0 as FACT_TVA_INCASARE, 1 as NRFACT, decode(extract(year from dataireg)*12+extract(month from dataireg), an*12+luna, 1, 0) as EMISA_PERIOADA,
	 RO21T, RO11T, RO19T, RO9T AS RO09T, RO5T AS RO05T, RO24T, RO20T
	 FROM VJV2025 J
	 LEFT JOIN (SELECT ID_PART,TARA,PRESCURTARE AS COD_TARA,JUDET,COD_JUDET,LOCALITATE,STRADA,NUMAR,APART,SCARA,BLOC FROM VADRESE_PARTENERI WHERE PRINCIPALA = 1) A ON J.ID_PART = A.ID_PART 
	 WHERE (TOTCTVA = 0 OR RO21T <> 0 OR RO11T <> 0 OR RO19T <> 0 OR RO9T <> 0 OR RO5T <> 0 OR RO24T <> 0 OR RO20T <> 0 OR RO21NT <> 0 OR RO11NT <> 0 OR RO19NT <> 0 OR RO9NT <> 0 OR RO5NT <> 0 OR RO24NT <> 0 OR RO20NT <> 0 OR ROTI <> 0) AND 
	<<lcConditieLuna>>
UNION
SELECT 'JC' AS TIPJ, 0 AS TIPPART, TIP_DOC as tipdoc, 'A ' as tip, dataact, dataireg, serie_act, nract, id_fdoc, fel_document, id_sucursala, sucursala,
	 0 as ISCIF, 0 AS CIFVALID,
	 J.ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', '') AS COD_FISCALFRO, SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) AS ATRIBUT_FISCAL, DENUMIRE, TIP_PERSOANA, AFILIAT, CODTARA,
	 A.TARA, A.COD_TARA, A.JUDET, A.COD_JUDET, A.LOCALITATE, A.STRADA, A.BLOC, A.SCARA, A.NUMAR, A.APART,
	 RO21B + RO11B + RO19B + RO09B + RO05B + RO24B + RO20B + RO21NB + RO11NB + RO19NB + RO9NB + RO5NB + RO24NB + RO20NB + ROSCN AS BAZA,
	 RO21T + RO11T + RO19T + RO09T + RO05T + RO21NT + RO11NT + RO19NT + RO9NT + RO5NT AS TVA,
	 TI21B + TI11B + TI19B + TI09B + TI24B + TI20B + XX21TIB + XX11TIB + XX19TIB + XX9TIB as TIBAZA,
	 TI21T + TI11T + TI19T + TI09T + TI24T + TI20T + XX21TIT + XX11TIT + XX19TIT + XX9TIT as TITVA,
	 RO21B + RO21NB AS BAZA21, RO11B + RO11NB AS BAZA11, RO19B + RO19NB AS BAZA19, RO09B + RO9NB AS BAZA09, RO05B + RO5NB AS BAZA05, RO24B + RO24NB AS BAZA24, RO20B + RO20NB AS BAZA20, ROSCN AS BAZA00,
	 RO21T + RO21NT AS TVA21, RO11T + RO11NT  AS TVA11, RO19T + RO19NT AS TVA19, RO09T + RO9NT AS TVA09, RO05T + RO5NT AS TVA05, RO24T + RO24NT AS TVA24, RO20T + RO20NT AS TVA20,
	 TI21B + XX21TIB AS TIBAZA21, TI11B + XX11TIB AS TIBAZA11, TI19B + XX19TIB AS TIBAZA19, TI09B + XX9TIB AS TIBAZA09, 0.00 AS TIBAZA05, TI24B AS TIBAZA24, TI20B AS TIBAZA20, 
	 TI21T + XX21TIT as TITVA21, TI11T + XX11TIT AS TITVA11, TI19T + XX19TIT AS TITVA19, TI09T + XX9TIT AS TITVA09, 0.00 AS TITVA05, TI24T as TITVA24, TI20T AS TITVA20, 
	 TVA_INCASARE, 0 as FACT_TVA_INCASARE, 1 as NRFACT, decode(extract(year from dataireg)*12+extract(month from dataireg), an*12+luna, 1, 0) as EMISA_PERIOADA,
	 RO21T, RO11T, RO19T, RO09T, RO05T, RO24T, RO20T
	 FROM VJC2025 J
	 LEFT JOIN (SELECT ID_PART,TARA,PRESCURTARE AS COD_TARA,JUDET,COD_JUDET,LOCALITATE,STRADA,NUMAR,APART,SCARA,BLOC FROM VADRESE_PARTENERI WHERE PRINCIPALA = 1) A ON J.ID_PART = A.ID_PART	 
	 WHERE (TOTCTVA = 0 OR RO21T <> 0 OR RO11T <> 0 OR RO19T <> 0 OR RO09T <> 0 OR RO05T <> 0 OR RO24T <> 0 OR RO20T <> 0 OR 
	 RO21NT <> 0 OR RO11NT <> 0 OR RO19NT <> 0 OR RO9NT <> 0 OR RO5NT <> 0 OR RO24NT <> 0 OR RO20NT <> 0 OR 
	 TI21T <> 0 OR TI11T <> 0 OR TI19T <> 0 OR TI09T <> 0 OR TI24T <> 0 OR TI24T <> 0 OR 
	 XX21TIB<>0 OR XX21TIT <> 0 OR XX11TIB<>0 OR XX11TIT <> 0 OR XX19TIB<>0 OR XX19TIT <> 0 OR XX9TIB<>0 OR XX9TIT <> 0 OR 
	 ROSCN <> 0) AND 
	<<lcConditieLuna>>
		Endtext

		goLog.Log(lcSql)

		lcCursor = [crsJTVATemp]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		If m.llSucces
			Select crsJTVA
			Append From Dbf('crsJTVATemp')
			Use In (Select('crsJTVATemp'))

			* sterg facturile de achizitie de la platitorii de TVA (RO) care au doar baza00 <> 0 (ROSCN). nu trebuie sa apara in declaratie. doar la tipul 2 (neinregistrati in scopuri TVA)
			* sunt permise facturile cu TVA 0 tip AS - achizitii regim special (servicii turism, second hand, obiecte arta care aplica TVA la marja de profit)
			Delete From crsJTVA Where tipj = 'JC' And atribut_fiscal = 'RO' And TVA21 = 0 And TVA11 = 0 And TVA19 = 0 And TVA09 = 0 And TVA05 = 0 And TVA24 = 0 And TVA20 = 0 And baza00 <> 0 And !Inlist(Alltrim(tipdoc), 'U', 'H')
			Update crsJTVA Set seq = Recno()

			* Bifa Persoane Afiliate
			Select crsJTVA
			Locate For Nvl(AFILIAT, 0) <> 0
			If Found()
				pounitate.PersoaneAfiliate = 1
			Endif
		Endif && llSucces

		* Facturi Anulate din Reg. Jurnal cu gnIdSetAnulate
		If m.llSucces
			Text To lcSql Textmerge Noshow
SELECT distinct a.serie_act as seria, a.nract as nr
  FROM act a left join nom_fdoc f on a.id_fdoc = f.id_fdoc
	WHERE a.sters = 0 and a.id_set = <<m.pnIdSetAnulat>> and f.tip_doc in ('F', 'M') and 
	<<lcConditieLuna>>
	<<lcConditieAct>>
    <<gcCondSucursala>>
			Endtext
 
			lcCursor = [crsFacturiAnulate]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		Endif && llSucces
		* Numar bonuri fiscale cu si fara CUI, B si C (AMEF)
		If m.llSucces

			* 07-09/2016 se declara doar operatiile cu persoane impozabile, deci nu bonuri AMEF
			If m.llPerioadaDoarRO
				Create Cursor crsBonuriAMEF (luna N(2), nr N(10))
				llSucces = .T.
			Else
				*BON=10*
				* se numara toate bonurile fiscale emise, indiferent ca au cod fiscal sau nu. Doar bonurile fiscale pentru care s-au emis facturi nu se iau in considerare!
				* dar acelea trebuie sa fie marcate ca FACTURA
				Text To lcSql Textmerge Noshow
SELECT extract(month FROM dataact) as luna, SUM(case when nr is null then 1 else to_number(nr) end) as nr FROM
(SELECT a.id_fact, a.cod, MAX(a.dataact) as dataact, MAX(replace(replace(upper(regexp_substr(a.explicatia,'\*BON=(\d)*\*',1,1,'i')),'*BON=',''),'*','')) as nr
	FROM act a
	WHERE a.sters = 0 and
	<<lcConditieLuna>>
	<<lcConditieAct>>
    and id_fact in (SELECT j.id_fact
    					from jv2007 j
    					where <<lcConditieLuna>> <<lcConditieAct>> and
    						id_fdoc in (select id_fdoc
                                     from NOM_FDOC t
                                    where sters = 0
                                      and tip_doc in ('B','C')))
   and id_fdoc in (select id_fdoc
                                     from NOM_FDOC t
                                    where sters = 0
                                      and tip_doc in ('B','C'))
    group by a.id_fact, a.cod)
    group by extract(month from dataact)
				Endtext

				Wait Window 'Calculare numar bonuri AMEF...' Nowait
				lcCursor = [crsBonuriAMEF]
				llSucces = goExecutor.oExecuta(lcSql, lcCursor)
			Endif &&  m.llPerioadaDoarRO
		Endif && llSucces

		* Numar bilete E(xceptate) AMEF
		If m.llSucces
			* 07-09/2016 se declara doar operatiile cu persoane impozabile, deci nu bonuri AMEF
			If m.llPerioadaDoarRO
				Create Cursor crsBonuriEAMEF(luna N(2), nr N(10))
				llSucces = .T.
			Else
				Text To lcSql Textmerge Noshow
SELECT extract(month FROM dataact) as luna, SUM(case when nr is null then 1 else to_number(nr) end) as nr FROM
(SELECT a.id_fact, a.cod, MAX(a.dataact) as dataact, MAX(replace(replace(upper(regexp_substr(a.explicatia,'\*BON=(\d)*\*',1,1,'i')),'*BON=',''),'*','')) as nr
	FROM act a
	WHERE a.sters = 0 and
	<<lcConditieLuna>>
	<<lcConditieAct>>
    and id_fact in (SELECT j.id_fact
    					from jv2007 j
    					where <<lcConditieLuna>> <<lcConditieAct>> and
    						id_fdoc in (select id_fdoc
                                     from NOM_FDOC t
                                    where sters = 0
                                      and tip_doc = 'E'))
	and	id_fdoc in (select id_fdoc
                                     from NOM_FDOC t
                                    where sters = 0
                                      and tip_doc = 'E')
    group by a.id_fact, a.cod)
    group by extract(month from dataact)
				Endtext

				Wait Window 'Calculare numar bonuri exceptate AMEF...' Nowait
				lcCursor = [crsBonuriEAMEF]
				llSucces = goExecutor.oExecuta(lcSql, lcCursor)
			Endif &&  m.llPerioadaDoarRO
		Endif && llSucces

		Return m.llSucces
	Endproc && citeste_date_jtva

	* Configureaza tipurile de documente (facturi, facturi simplificate, bonuri fiscale etc.)
	Procedure configureaza_date_jtva

		Local loVerificare As "verificarecod"
		Local loRec

		Update crsJTVA Set atribut_fiscal = '' Where Isdigit(atribut_fiscal) Or Empty(Nvl(cod_fiscal, ''))
		Update crsJTVA Set codtara = 'RO' WHERE EMPTY(NVL(codtara,''))

		* Completez Tip partener: 1/2/3/4
		Update crsJTVA Set tippart = 1 Where tippart = 0 And atribut_fiscal = 'RO' && persoane impozabile inregistrate in scopuri de TVA in Romania
		Update crsJTVA Set tippart = 2 Where tippart = 0 And (codtara = 'RO' AND Empty(atribut_fiscal)) OR  Nvl(TIP_PERSOANA,0) <> 1 && persoane din RO neinregistrate in scopuri de TVA /fara atribut fiscal sau persoane fizice
		Update crsJTVA Set tippart = 3 Where tippart = 0 And ;
			(INLIST(atribut_fiscal,'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES', 'FI', 'FR') OR INLIST(atribut_fiscal, 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'SE', 'SI', 'SK') OR ;
			INLIST(codtara, 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'GR', 'ES', 'FI', 'FR') OR INLIST(codtara, 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'SE', 'SI', 'SK')) && persoane impozabile nestabilite in RO, in UE
		Update crsJTVA Set tippart = 4 Where tippart = 0 And Nvl(TIP_PERSOANA,0) = 1 AND codtara <> 'RO'&& persoane impozabile nestabilite in RO, in afara UE si tip persoana = persoana juridica

		* Completez tip document: FACTURA/BON FISCAL FARA FACTURA/BON FISCAL CU FACTURA/FACTURA SIMPLIFICATA/AUTOFACTURA
		*!*			Update crsJTVA Set tipdoc = ''
		*!*			Update crsJTVA Set tipdoc = 'B' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_bonfiscal_fara_cui + ','
		*!*			Update crsJTVA Set tipdoc = 'C' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_bonfiscal_cu_cui + ','
		*!*			Update crsJTVA Set tipdoc = 'M' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_factura_simplificata + ','
		*!*			Update crsJTVA Set tipdoc = 'E' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_exceptate_amef + ','
		*!*			Update crsJTVA Set tipdoc = 'O' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_borderou_achizitii + ','
		*!*			Update crsJTVA Set tipdoc = 'Z' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_carnet_comercializare + ','
		*!*			Update crsJTVA Set tipdoc = 'H' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_achizitii_second_hand + ','
		*!*			Update crsJTVA Set tipdoc = 'U' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_servicii_turism + ','
		*!*			Update crsJTVA Set tipdoc = 'AF' Where Empty(tipdoc) And ',' + Alltrim(serie_act) + ',' $ ',' + This.oUnitate.serie_autofacturare + ','
		*!*			Update crsJTVA Set tipdoc = 'F' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_factura + ','
		*		Update crsJTVA Set tipdoc = 'F' Where Empty(tipdoc) && completez restul documentelor cu tip = F(actura)

		Insert Into crsFdocTip (id_fdoc, fel_document, tip_doc, bifa) ;
			Select Distinct id_fdoc, fel_document, tipdoc As tip_doc, Iif(Empty(Nvl(tipdoc, '')), 1, 0) As bifa ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1

		Go Top In crsFdocTip

		* Determin CUI/CNP si verific validitate
		Select Distinct cod_fiscal, .F. As iscif, .F. As cifvalid ;
			From crsJTVA ;
			Where !Empty(Nvl(cod_fiscal, '')) ;
			Into Cursor cCUITemp Readwrite

		loVerificare = Createobject("verificarecod") && validare.prg
		Update cCUITemp Set iscif = loVerificare.iscif(cCUITemp.cod_fiscal)
		Update cCUITemp Set cifvalid = loVerificare.Validare(Iif(cCUITemp.iscif, 'CIF', 'CNP'), cCUITemp.cod_fiscal)
		Select cCUITemp
		Scan
			Update crsJTVA Set iscif = Iif(cCUITemp.iscif, 1, 0), cifvalid = Iif(cCUITemp.cifvalid, 1, 0) Where cod_fiscal = cCUITemp.cod_fiscal
		Endscan
		Update crsJTVA Set iscif = 0, cifvalid = 0 Where Empty(Nvl(cod_fiscal, ''))

		Use In (Select('cCUITemp'))

		*!*	L - livrari
		*!*	V - livrari cu taxare inversa
		*!*	LS - livrari regim special

		*!*	A - achizitii
		*!*	C - achizitii cu taxare inversa
		*!*	AS - achizitii regim special
		*!*	N - achizitii de bunuri/servicii pt care au fost primite facturi/borderouri de achizitii/ file din carnete de comercializare de la persoane neinregistrate
		*!*	AI - achizitii de la persoane impozabile care aplica TVA la incasare

		* V/C - taxare inversa
		Select * From crsJTVA Where .F. Into Cursor crsJTVATemp Readwrite

		Select crsJTVA
		Scan For EMISA_PERIOADA = 1 And titva <> 0 Or tibaza <> 0 And Inlist(TIP, 'L', 'A')
			If TVA = 0 And baza = 0
				* daca are doar taxare inversa, marchez factura tip V/C
				Replace TIP With Iif(TIP = 'L', 'V', 'C'), ;
					baza With tibaza, TVA With titva, ;
					baza21 With tibaza21, baza11 With tibaza11, baza19 With tibaza19, baza09 With tibaza09, baza05 With tibaza05, ;
					TVA21 With titva21, TVA11 With titva11, TVA19 With titva19, TVA09 With titva09, TVA05 With titva05 In crsJTVA

				If TIP = 'V' && livrare taxare inversa nu are cota de TVA
					Replace baza00 With 0, titva21 WITH 0, baza11 With 0, tibaza11 With 0 In crsJTVA
				Endif
			Else
				* daca are taxare inversa si tva normal, marchez factura tip V/C si adaug o linie noua cu tva normal
				Select crsJTVA
				Scatter Name loRec
				Replace TIP With Iif(TIP = 'L', 'V', 'C'), ;
					baza With tibaza, TVA With titva, ;
					baza21 With tibaza21, baza11 With tibaza11, baza19 With tibaza19, baza09 With tibaza09, baza05 With tibaza05, baza24 With tibaza24, baza20 With tibaza20, ;
					TVA21 With titva21, TVA11 With titva11, TVA19 With titva19, TVA09 With titva09, TVA05 With titva05, TVA24 With titva24, TVA20 With titva20, ;
					ro21t With 0, ro11t With 0, ro19t With 0, RO09T With 0, ro05t With 0, ro24t With 0, ro20t With 0 In crsJTVA

				With loRec
					.NRFACT = 0 && am deja nrfact = 1 pe linia cu taxare inversa, sa nu duplic numarul de facturi
					.tibaza = 0
					.titva = 0
					.tibaza21 = 0
					.tibaza11 = 0
					.tibaza19 = 0
					.tibaza09 = 0
					.tibaza05 = 0
					.tibaza24 = 0
					.tibaza20 = 0					
					.titva21 = 0
					.titva11 = 0
					.titva19 = 0
					.titva09 = 0
					.titva05 = 0
					.titva24 = 0
					.titva20 = 0					
				Endwith
				Insert Into crsJTVATemp From Name loRec
				Select crsJTVA
			Endif
		Endscan
		Select crsJTVA
		Append From Dbf('crsJTVATemp')
		Use In (Select('crsJTVATemp'))

		* LS - livrari regim special (servicii turism, second hand, obiecte arta care aplica TVA la marja de profit)
		Update crsJTVA Set TIP = 'LS' Where TIP = 'L' And Inlist(Alltrim(tipdoc), 'U', 'H')

		* AS - achizitii regim special (servicii turism, second hand, obiecte arta care aplica TVA la marja de profit)
		Update crsJTVA Set TIP = 'AS' Where TIP = 'A' And Inlist(Alltrim(tipdoc), 'U', 'H')

		* N - achizitii de bunuri/servicii pt care au fost primite facturi/borderouri de achizitii/ file din carnete de comercializare de la persoane neinregistrate
		* Achizitii de la persoane fizice sau persoane juridice neplatitoare de TVA
		Update crsJTVA Set TIP = 'N' Where TIP = 'A' And (iscif = 0 Or (iscif = 1 And !'RO' $ cod_fiscal))

		* Facturi de achizitie provenite de la persoane care aplica sistemul TVA la Incasare, pentru sectiunea I5.2
		Update crsJTVA Set FACT_TVA_INCASARE = 1 Where tipj = 'JC' And iscif = 1 And 'RO' $ (Upper(cod_fiscal)) And VERIFICA_RTVAI_DATA(crsJTVA.cod_fiscal, crsJTVA.dataact)

		* AI - achizitii de la persoane impozabile care aplica TVA la incasare
		Update crsJTVA Set TIP = 'AI' Where TIP = 'A' And FACT_TVA_INCASARE = 1

		* Documentele fara tipdoc asociat nu se iau in considerare la generarea D394
		Update crsJTVA Set TIP = '' Where Empty(Nvl(tipdoc, ''))

		Return .T.

	Endproc && configureaza_date_jtva

	*===========================================
	* Creeaza cursoare pentru sectiunile declaratiei din crsJTVA
	Procedure pregateste_sectiunea_1_2
		Private pnPlaje24
		Local lnAn, lnLuna1, lnLuna2, llSucces
		Local lcSeria, lcSql, lnNr1, lnNr2, loUnitate
		pnPlaje24 = 0

		loUnitate = This.oUnitate
		lnAn = Int(loUnitate.an)
		lnLuna1 = Int(loUnitate.luna1)
		lnLuna2 = Int(loUnitate.luna2)


		* I. ALTE INFORMATII:
		* I 1.1 Livrari de bunuri/prestari de servicii pentru care s-au emis facturi simplificate care au inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.1' As punct, Sum(baza21) As baza, Sum(TVA21) As TVA, 21 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza11) As baza, Sum(TVA11) As TVA, 11 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza20) As baza, Sum(TVA05) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, ''))
			

		* I 1.2 Livrari de bunuri/prestari de servicii pentru care s-au emis facturi simplificate fara a avea inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.2' As punct, Sum(baza21) As baza, Sum(TVA21) As TVA, 21 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza11) As baza, Sum(TVA11) As TVA, 11 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza20) As baza, Sum(TVA20) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, ''))			


		* I 1.3 Achizitii de bunuri si servicii pentru care s-au primit facturi simplificate de la persoane impozabile care aplica sistemul normal de TVA si care au inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.3' As punct, Sum(baza21) As baza, Sum(TVA21) As TVA, 21 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza11) As baza, Sum(TVA11) As TVA, 11 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza20) As baza, Sum(TVA20) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0


		* I 1.4 Achizitii de bunuri si servicii pentru care s-au primit facturi simplificate de la persoane impozabile care aplica sistemul de TVA la incasare si care au inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.4' As punct, Sum(baza21) As baza, Sum(TVA21) As TVA, 21 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza11) As baza, Sum(TVA11) As TVA, 11 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza20) As baza, Sum(TVA20) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1
			
		* I 1.5 Achizitii de bunuri si servicii pentru care s-au primit bonuri fiscale care indeplinesc conditiile unei facturi simplificate si care au inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.5' As punct, Sum(baza21) As baza, Sum(TVA21) As TVA, 21 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza11) As baza, Sum(TVA11) As TVA, 11 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza20) As baza, Sum(TVA20) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' 
			
		Delete From crsSectiuneaI Where Nvl(baza, 0) = 0 And Nvl(TVA, 0) = 0

		* 2.1 Plaja de facturi alocate in perioada de facturare
		pnPlaje24 = 0 && plaja de facturi special creata pentru declararea in D394. Nu se foloseste la generarea de numere de facturi
		lnSucces = goExecutor.oSelect2Value("SELECT COUNT(*) as nr FROM plaje_numere WHERE id_tipdoc = 24 and sters = 0", @pnPlaje24)
		If m.lnSucces = CT_INSUCCES
			amessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
			pnPlaje24 = 0
		Endif
		lnIdTipDocFact = Iif(m.pnPlaje24 > 0, 24, 5)

		Text To lcSql Textmerge Noshow
select s.serie as seria,
	s.prefix || SUBSTR(decode(nvl(s.an, 0), 1,
	              decode(NVL(s.an2caractere,0), 1, 
	              	'<<SUBSTR(ALLTRIM(STR(m.lnAn)),3,2)>>', '<<m.lnAn>>') || decode(s.luna, 1, lpad('<<m.lnLuna1>>', 2, '0'), 
	              	''),
	              '') || lpad(p.pl_inf, nvl(s.lungime, 14), '0'), 1, 30) as nr1,
	s.prefix || SUBSTR(decode(nvl(s.an, 0), 1,
	              decode(NVL(s.an2caractere,0), 1, 	
	              '<<SUBSTR(ALLTRIM(STR(m.lnAn)),3,2)>>', '<<m.lnAn>>') || decode(s.luna, 1, lpad('<<m.lnLuna1>>', 2, '0'), ''),'')
	              || lpad(p.pl_sup, nvl(s.lungime, 14), '0'), 1, 30) as nr2
  from plaje_numere p
  join serii s
    on p.id_serie = s.id_serie
 where p.sters = 0
   and p.inactiv = 0
   and p.id_tipdoc = <<m.lnIdTipDocFact>>
   and ((<<lnAn*12+lnLuna1>> between extract(year from p.datai) * 12 + extract(month from p.datai) and extract(year from p.datas) * 12 + extract(month from p.datas)) or
   (<<lnAn*12+lnLuna2>> between extract(year from p.datai) * 12 + extract(month from p.datai) and extract(year from p.datas) * 12 + extract(month from p.datas)))
		Endtext

		llSucces = goExecutor.oExecuta(m.lcSql, [crsFacturiAlocateTemp])
		If m.llSucces
			Insert Into crsFacturiAlocate (seria, nr1, nr2) ;
				Select seria, Int(Val(nr1)) As nr1, Int(Val(nr2)) As nr2 ;
				From crsFacturiAlocateTemp
		Endif

		* Nu pot sa configurez plaje de numar fara serie. Conventional seria xxx este NULL
		Update crsFacturiAlocate Set seria = '' Where seria = 'XXX'
		
		* In facturi alocate nu intra seriile de facturi emise de beneficiari/terti
		IF !EMPTY(NVL(loUnitate.serie_facturi_beneficiari,''))
			DELETE FROM crsFacturiAlocate WHERE !EMPTY(seria) AND ',' + Alltrim(seria) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_beneficiari) + ','
		ENDIF 
		IF !EMPTY(NVL(loUnitate.serie_facturi_terti,''))
			DELETE FROM crsFacturiAlocate WHERE !EMPTY(seria) AND ',' + Alltrim(seria) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_terti) + ','
		ENDIF 

		Use In (Select('crsFacturiAlocateTemp'))

		* 2.2 Numarul total de facturi emise, in perioada de raportare, din plaja de facturi alocate:
		If m.llSucces
			* selectez seriile si numerele de facturi (sectiunea 2.1), in afara de facturi emise de beneficiari/terti in numele persoanei impozabile (sectiunile 2.3, 2.4)
			Select Distinct serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') ;
				Union ;
				Select Distinct seria, nr ;
				From crsFacturiAnulate ;
				Into Cursor crsNract READWRITE 
			
			IF !EMPTY(NVL(loUnitate.serie_facturi_beneficiari,''))
				DELETE FROM crsNrAct WHERE !EMPTY(seria) AND ',' + Alltrim(seria) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_beneficiari) + ','
			ENDIF 
			IF !EMPTY(NVL(loUnitate.serie_facturi_terti,''))
				DELETE FROM crsNrAct WHERE !EMPTY(seria) AND ',' + Alltrim(seria) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_terti) + ','
			ENDIF 	
			CALCULATE CNT() TO loUnitate.nr_facturi_emise IN crsNrAct
*!*				loUnitate.nr_facturi_emise = _Tally
			Select Distinct seria From crsNract Into Cursor crsSeriiAct

			Select crsSeriiAct
			Scan
				lcSeria = seria
				Select Cast(0 As N(20)) As rec, nr From crsNract Where Nvl(seria, 'XXX') = Nvl(m.lcSeria, 'XXX') Order By nr Into Cursor crsSeriaAct1 Readwrite
				Replace All rec With Recno() In crsSeriaAct1
				Select rec, nr From crsSeriaAct1 Into Cursor crsSeriaAct2

				Select c1.nr As nr1, c2.nr As nr2, c2.nr - Nvl(c1.nr, c2.nr) As Dif ;
					From crsSeriaAct1 c1 Left Join crsSeriaAct2 c2 On c1.rec = c2.rec - 1 ;
					Into Cursor crsSeriaActDif

				Use In (Select('crsSeriaAct1'))
				Use In (Select('crsSeriaAct2'))

				Select crsSeriaActDif
				Go Top
				lnNr1 = nr1
				lnNr2 = nr1
				Scan
					If Dif = 1
						lnNr2 = nr2
					Else
						Insert Into crsFacturiEmise (seria, nr1, nr2) Values (m.lcSeria, m.lnNr1, m.lnNr2)
						lnNr1 = nr2
						lnNr2 = nr2
					Endif
				Endscan
				If m.lnNr1 <> m.lnNr2
					Insert Into crsFacturiEmise (seria, nr1, nr2) Values (m.lcSeria, m.lnNr1, m.lnNr2)
				Endif
				Use In (Select('crsSeriaActDif'))

			Endscan && crsSeriiAct

			* Verificare facturi emise in plajele de facturi alocate!!!
			* Asociez facturile emise cu plajele de facturi alocate
			Select a.seria, a.nr, b.seria As aseria, b.nr1, b.nr2 ;
				From crsNract a Left Join crsFacturiAlocate b On Padr(Nvl(a.seria, ''), 20, ' ') = Padr(Nvl(b.seria, ''), 20, ' ') And Between(a.nr, b.nr1, b.nr2) ;
				Into Cursor cFacturiEmiseAlocateTemp

			* Selectez facturile care nu s-au incadrat in nici o plaja de facturi alocate 	
			Select seria, nr ;
				From cFacturiEmiseAlocateTemp ;
				Where Isnull(nr1) ;
				Order By seria, nr ;
				Into Cursor cFacturiEmiseAlocate

			Use In (Select('cFacturiEmiseAlocateTemp'))

			This.oUnitate.cTextFacturiEmiseAlocate = ""
			If _Tally > 0
				This.oUnitate.cTextFacturiEmiseAlocate = 'Facturi emise care nu sunt in plajele de facturi alocate:' + Chr(13) + Chr(10)
				Select cFacturiEmiseAlocate
				Scan
					This.oUnitate.cTextFacturiEmiseAlocate = This.oUnitate.cTextFacturiEmiseAlocate + Transform(seria) + ' ' + Transform(nr) + Chr(13) + Chr(10)
				Endscan
			Else
				This.oUnitate.cTextFacturiEmiseAlocate = 'Toate facturile emise sunt in plajele de facturi alocate.'
			Endif
			Use In (Select('cFacturiEmiseAlocate'))

			Use In (Select('crsSeriiAct'))
			Use In (Select('crsNrAct'))

			* 2.2 din care (Tip: stornate/anulate/autofacturare/emisa in numele furnizorilor)
			* Facturi stornate
			Insert Into crsFacturiEmiseTip (TIP, ntip, seria, nr) ;
				Select Distinct 'STORNAT' As TIP, 1 As ntip, serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And baza + TVA < 0

			* Facturi anulate
			Insert Into crsFacturiEmiseTip (TIP, ntip, seria, nr) ;
				Select Distinct 'ANULAT' As TIP, 2 As ntip, seria, nr ;
				From crsFacturiAnulate

			* Autofacturi
			Insert Into crsFacturiEmiseTip (TIP, ntip, seria, nr) ;
				Select Distinct 'AUTOFACTURA' As TIP, 3 As ntip, serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_autofacturare) + ','

			* Facturi emise in numele furnizorilor
			Insert Into crsFacturiEmiseTip (TIP, ntip, seria, nr) ;
				Select Distinct 'FURNIZOR' As TIP, 4 As ntip, serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JC' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_furnizori) + ','

			* 2.2 Autofacturare
			Insert Into crsAutoFacturi (seria, nr, baza21, TVA21, baza11, TVA11, baza19, TVA19, baza09, TVA09, baza05, TVA05, baza24, TVA24, baza20, TVA20) ;
				Select serie_act As seria, nract As nr, ;
				Sum(baza21) As baza21, Sum(TVA21) As TVA21, ;
				Sum(baza11) As baza11, Sum(TVA11) As TVA11, ;
				Sum(baza19) As baza19, Sum(TVA19) As TVA19, ;
				Sum(baza09) As baza09, Sum(TVA09) As TVA09, ;
				Sum(baza05) As baza05, Sum(TVA05) As TVA05, ;
				Sum(baza24) As baza24, Sum(TVA24) As TVA24, ;
				Sum(baza20) As baza20, Sum(TVA20) As TVA20 ;				
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_autofacturare) + ',' ;
				Group By serie_act, nract


			* 2.3 Numarul total de facturi emise, in perioada de raportare, de beneficiari in numele persoanei impozabile:
			Insert Into crsFacturiTerti (punct, denumire, cod_fiscal, seria, nr1, nr2) ;
				Select '2.3' As punct, denumire, cod_fiscal, serie_act As seria, Min(nract) As nr1, Max(nract) As nr2 ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_beneficiari) + ',' ;
				Group By denumire, cod_fiscal, serie_act ;
				Union ;
				Select '2.4' As punct, denumire, cod_fiscal, serie_act As seria, Min(nract) As nr1, Max(nract) As nr2 ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_terti) + ',' ;
				Group By denumire, cod_fiscal, serie_act ;
				Order By punct, denumire, cod_fiscal, serie_act
				
			Select Distinct serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_beneficiari) + ',' ;
				Into Cursor crsNract

			loUnitate.nr_facturi_beneficiari = _Tally				
			USE IN (SELECT('crsNract'))

			Select Distinct serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_terti) + ',' ;
				Into Cursor crsNract

			loUnitate.nr_facturi_terti = _Tally				
			USE IN (SELECT('crsNract'))
			
		Endif && m.llSucces


		Return m.llSucces
	Endproc && pregateste_sectiunea_1_2


	* TVA Exigibil
	Procedure pregateste_sectiunea_4_5
		If This.oUnitate.tva_incasare = 0
			* 4. Date aferente operatiunilor desfasurate de catre persoana impozabila care aplica sistemul normal de TVA
			* 4.1 TVA deductibila aferenta facturilor achitate in perioada de raportare indiferent de data in care acestea au fost primite de la
			* persoane impozabile care aplica sistemul de TVA la incasare, defalcata pe fiecare cota de TVA
			Insert Into crsSectiuneaI4 (punct, TVA21, TVA11, TVA19, TVA09, TVA05, TVA24, TVA20) ;
				Select '4.1' As punct, Sum(ro21t) As TVA21, Sum(ro11t) As TVA11, Sum(ro19t) As TVA19, Sum(RO09T) As TVA09, Sum(ro05t) As TVA05, Sum(RO24T) As TVA24, Sum(ro20t) As TVA20 ;
				From crsJTVA Where tipj = 'JC' And atribut_fiscal = 'RO' And tva_incasare = 1 And Inlist(tipdoc, 'F', 'M')
		Else
			* 5. Date aferente operatiunilor desfasurate de catre persoana impozabila care aplica sistemul de TVA la incasare
			* 5.1 TVA colectata aferenta facturilor incasate in perioada de raportare indiferent de data in care acestea au fost emise de catre
			* persoana impozabila care aplica sistemul de TVA la incasare, defalcata pe fiecare cota de TVA
			Insert Into crsSectiuneaI4 (punct, TVA21, TVA11, TVA19, TVA09, TVA05, TVA24, TVA20) ;
				Select '5.1' As punct, Sum(ro21t) As TVA21, Sum(ro11t) As TVA11, Sum(ro19t) As TVA19, Sum(RO09T) As TVA09, Sum(ro05t) As TVA05, Sum(ro24t) As TVA24, Sum(ro20t) As TVA20 ;
				From crsJTVA Where tipj = 'JV' And tva_incasare = 1 And Inlist(tipdoc, 'F', 'M')

			* 5.2 TVA deductibila aferenta facturilor achitate in perioada de  raportare indiferent de data in care acestea au fost primite de la
			* persoane impozabile care aplica sistemul normal de TVA, defalcata pe fiecare cota de TVA
			Insert Into crsSectiuneaI4 (punct, TVA21, TVA11, TVA19, TVA09, TVA05, TVA24, TVA20) ;
				Select '5.2' As punct, Sum(ro21t) As TVA21, Sum(ro11t) As TVA11, Sum(ro19t) As TVA19, Sum(RO09T) As TVA09, Sum(ro05t) As TVA05, Sum(ro24t) As TVA24, Sum(ro20t) As TVA20 ;
				From crsJTVA Where tipj = 'JC' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 And Inlist(tipdoc, 'F', 'M')

			* 5.3 TVA deductibila aferenta facturilor achitate in perioada de raportare indiferent de data in care acestea au fost primite de la
			* persoane impozabile care aplica sistemul de TVA la incasare, defalcata pe fiecare cota de TVA
			Insert Into crsSectiuneaI4 (punct, TVA21, TVA11, TVA19, TVA09, TVA05, TVA24, TVA20) ;
				Select '5.3' As punct, Sum(ro21t) As TVA21, Sum(ro11t) As TVA11, Sum(ro19t) As TVA19, Sum(RO09T) As TVA09, Sum(ro05t) As TVA05, Sum(ro24t) As TVA24, Sum(ro20t) As TVA20 ;
				From crsJTVA Where tipj = 'JC' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 And Inlist(tipdoc, 'F', 'M')
		Endif

	Endproc && pregateste_sectiunea_4_5


	* 6. TVA colectata aferenta marjei de profit din perioada de raportare indiferent de data la care au fost efectuate operatiunile de catre
	* persoanele impozabile care aplica regimul special pt agentiile de turism, pt bunurile second-hand, opere de arta, obiecte de colectie si antichitati
	Procedure pregateste_sectiunea_6
		Return .T.
	Endproc && pregateste_sectiunea_6


	* 7. In situatia in care ati desfasurat, in perioada de raportare, activitati dintre cele inscrise in lista veti selecta activitatea corespunzatoare
	* si veti inscrie valoarea livrarilor/prestarilor, precum si TVA aferenta
	Procedure pregateste_sectiunea_7
		Local lcCodCaen, lcCodCaen1, lcCodCaen2, lcCodCaenTemp, lcListaCAEN, llSucces
		lcCodCaen1 = Alltrim(Nvl(gofirma.caen, ''))
		lcCodCaen2 = Alltrim(Nvl(gofirma.caen394, ''))
		lcCodCaen = Iif(!Empty(m.lcCodCaen2), m.lcCodCaen2, m.lcCodCaen1)

		lcCodCaenTemp = ',' + m.lcCodCaen + ','
		lcListaCAEN = ',1071,4520,4730,47761,47762,4932,55101,55102,56103,5630,812,9313,9602,9603,'
		llSucces = .T.
		If !m.lcCodCaenTemp $ m.lcListaCAEN
			Return m.llSucces
		Endif

		ccsectii() && vnom_sectii
		Select id_sectie, sectie, caen From vnom_sectii Where ',' + Alltrim(Nvl(caen, '')) + ',' $ m.lcListaCAEN Into Cursor cSectii394 Readwrite
		If _Tally = 0
			Select cSectii394
			Append Blank
			Replace caen With m.lcCodCaen
		Endif

		Use In (Select('vnom_sectii'))
		* Insert Into crsSectiuneaI7 (caen, TIP)  Values (m.lcCodCaen, 'LIVRARI')
		* Insert Into crsSectiuneaI7 (caen, TIP)  Values (m.lcCodCaen, 'PRESTARI')
		Use In (Select('cSectii394'))

		Return .T.

	Endproc && pregateste_sectiunea_7

	Procedure pregateste_sectiunea_amef
		Insert Into crsAMEF (TIP, luna, nramef, nrbonuri, incasari, baza05, TVA05, baza09, TVA09, baza19, TVA19, baza11, TVA11, baza21, TVA21, baza20, TVA20) ;
			Select 1 As TIP, Month(dataact) As luna, Cast(pounitate.nramef As N(10)) As nramef, Cast(Nvl(b.nr, 0) As N(10)) As nrbonuri, ;
			Round(Sum(baza + TVA), 0) As incasari, Round(Sum(baza05), 0) As baza05, Round(Sum(TVA05), 0) As TVA05, Round(Sum(baza09), 0) As baza09, Round(Sum(TVA09), 0) As TVA09, ;
			Round(Sum(baza19), 0) As baza19, Round(Sum(TVA19), 0) As TVA19, Round(Sum(baza11), 0) As baza11, Round(Sum(TVA11), 0) As TVA11, Round(Sum(baza21), 0) As baza21, Round(Sum(TVA21), 0) As TVA21, ;
			Round(Sum(baza20), 0) As baza20, Round(Sum(TVA20), 0) As TVA20 ;
			From crsJTVA j Left Join crsBonuriAMEF b On Month(j.dataact) = b.luna ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(Alltrim(tipdoc), 'B', 'C') ;
			Group By 1, 2, 3, 4 ;
			Union ;
			Select 2 As TIP, Month(dataact) As luna, Cast(0 As N(10)) As nramef, Cast(Nvl(b.nr, 0) As N(10)) As nrbonuri, ;
			Round(Sum(baza + TVA), 0) As incasari, Round(Sum(baza05), 0) As baza05, Round(Sum(TVA05), 0) As TVA05, Round(Sum(baza09), 0) As baza09, Round(Sum(TVA09), 0) As TVA09, ;
			Round(Sum(baza19), 0) As baza19, Round(Sum(TVA19), 0) As TVA19, Round(Sum(baza11), 0) As baza11, Round(Sum(TVA11), 0) As TVA11, Round(Sum(baza21), 0) As baza21, Round(Sum(TVA21), 0) As TVA21, ;
			Round(Sum(baza20), 0) As baza20, Round(Sum(TVA20), 0) As TVA20 ;
			From crsJTVA j Left Join crsBonuriEAMEF b On Month(j.dataact) = b.luna ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'E' ;
			Group By 1, 2, 3, 4 ;
			Order By 1, 2

		Return .T.
	Endproc && pregateste_sectiunea_amef

*!*		Procedure pregateste_sectiunea_facturi_pers_fizice_10000
*!*			Local llPerioada122016
*!*			llPerioada122016 = Between(m.gnAn * 12 + m.gnLuna, 2016 * 12 + 7, 2016 * 12 + 12) && intre 07/2016 si 12/2016 nu se declara individual facturile < 10.000 lei catre persoanele fizice

*!*			Insert Into crsPersFizice (TIP, NRFACT, baza, baza05, TVA05, baza09, TVA09, baza19, TVA19, baza11, TVA11, baza21, TVA21) ;
*!*				Select TIP, Sum(NRFACT) As NRFACT, ;
*!*				Sum(baza) As baza, Sum(baza05) As baza05, Sum(TVA05) As TVA05, Sum(baza09) As baza09, Sum(TVA09) As TVA09, ;
*!*				Sum(baza19) As baza19, Sum(TVA19) As TVA19, Sum(baza11) As baza11, Sum(TVA11) As TVA11, Sum(baza21) As baza21, Sum(TVA21) As TVA21 ;
*!*				From crsJTVA j ;
*!*				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(Alltrim(TIP), 'L', 'LS') And Inlist(tipdoc, 'F', 'M', 'U', 'H') And tippart = 2 And Nvl(TIP_PERSOANA, 0) = 2 And (Abs(baza + TVA) <= 10000) And m.llPerioada122016;
*!*				Group By 1 ;
*!*				Order By 1

*!*			Return .T.
*!*		Endproc && pregateste_sectiunea_facturi_pers_fizice_10000

	*===========================================
	Procedure do_genereaza_xdp
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou


		Local lcAdresa, lcAn, lcCodFiscalF, lcDen, lcLuna, lcSelect, lcTag, lcXDP, llPerioadaDoarRO, lnSeq, llExportSectiuneaI, lnCotaTVAStandard
		Local lnTipPart, llTvaNedeductibil
		lcSelect = Select()
		lcCodFiscalF = Alltrim(gofirma.codfiscalfro)
		lcLuna = Padl(Alltrim(Str(gnLuna)), 2, [0])
		lcAn = Alltrim(Str(gnAn))
		lnCotaTVAStandard = Iif(m.gnAn*12+m.gnLuna <= 2025*12+7, 19, 21)
		llTvaNedeductibil = (This.oUnitate.tvanedeductibil = 1)

		llPerioadaDoarRO = Between(m.gnAn * 12 + m.gnLuna, 2016 * 12 + 7, 2016 * 12 + 9) && intre 07/2016 si 09/2016 se iau doar partenerii cu RO

		lcDen = XmlSpecialCharacters(RemoveCharacters(Alltrim(gofirma.antet), .T.))
		lcAdresa = XmlSpecialCharacters(RemoveCharacters(Alltrim(gofirma.adresa), .T.))

		This.TIP = Left(This.oUnitate.TIP, 1)
		llExportSectiuneaI = (This.oUnitate.ExportSectiuneaI = 1)
		lcXDP = ""

		Create Cursor cCote (cota N(3))
		Insert Into cCote (cota) Values (21)
		Insert Into cCote (cota) Values (11)
		Insert Into cCote (cota) Values (19)
		Insert Into cCote (cota) Values (9)
		Insert Into cCote (cota) Values (5)
		Insert Into cCote (cota) Values (24)
		Insert Into cCote (cota) Values (20)		
		Insert Into cCote (cota) Values (0)
		*) Tip Cod de inregistrare fiscala
		*L/A/V/C/AI/N/LS/AS

		* Centralizator achizitii/livrari defalcat pe tip parteneri 1/2/3/4, parteneri, cote, tip livrare/achizitie, tip document
		* Ordonez facturile dupa cota de TVA, sa las nrfact = 1 pe linia cu cota cea mai mare
		* In perioada 07/2016-09/2016 se raporteaza doar facturile cu RO
		If m.llPerioadaDoarRO
			lcWhere = [j.atribut_fiscal = 'RO']
		Else
			lcWhere = [.T.]
		Endif

		Select Cast(0 As N(20)) As rn, j.seq As id_fact, j.tippart, c.cota, j.denumire As denp, j.cod_fiscalfro As cuip, j.TIP, j.TIP_PERSOANA, ;
			j.TARA, j.COD_TARA, j.JUDET, j.COD_JUDET, j.LOCALITATE, j.STRADA, j.Bloc, j.SCARA, j.NUMAR, j.APART,	;
			Iif(j.tipdoc = 'F', 1, Iif(j.tipdoc = 'O', 2, Iif(j.tipdoc = 'Z', 3, Iif(j.tipdoc = 'R', 4, 5)))) As tip_document, ;
			NRFACT, ;
			Iif(c.cota = 21, baza21, Iif(c.cota = 11, baza11, Iif(c.cota = 19, baza19, Iif(c.cota = 9, baza09, Iif(c.cota = 5, baza05, Iif(c.cota = 24, baza24, Iif(c.cota = 20, baza20, ;
  		    Iif(c.cota = 0, Iif(m.llTvaNedeductibil Or Inlist(j.tipdoc, 'U', 'H') Or Inlist(j.TIP, 'V', 'C') Or tippart = 2, baza00, 0.00), baza11)))))))) As baza, ;
			Iif(c.cota = 21, TVA21, Iif(c.cota = 11, TVA11, Iif(c.cota = 19, TVA19, Iif(c.cota = 9, TVA09, Iif(c.cota = 5, TVA05, Iif(c.cota = 24, TVA24, Iif(c.cota = 20, TVA20, Iif(c.cota = 0, 0, TVA11)))))))) As TVA ;
			From crsJTVA j, cCote c ;
			Where j.EMISA_PERIOADA = 1 And Alltrim(j.TIP) In ('L', 'A', 'V', 'C', 'AI', 'N', 'LS', 'AS') And Inlist(tipdoc, 'F', 'U', 'H', 'O', 'Z', 'R') And &lcWhere ;
			Order By 1, 10 Desc ;
			Into Cursor cFacturiTemp Readwrite

		Select cFacturiTemp
		Replace All rn With Recno()

		* Sterg toate liniile cu baza = 0 si tva = 0 in afara de prima linie pentru facturile cu valoare totala = 0
		* Facturile care au valoare 0
		Select id_fact, Min(rn) As rn, Sum(baza) As baza, Sum(TVA) As TVA ;
			From cFacturiTemp ;
			Group By id_fact ;
			Having Sum(baza) = 0 And Sum(TVA) = 0 ;
			Into Cursor cFacturi0

		Select F.*, f0.rn As rn0 ;
			From cFacturiTemp F Left Join cFacturi0 f0 On F.id_fact = f0.id_fact ;
			Into Cursor cFacturitemp2 Readwrite

		Use In (Select('cFacturiTemp'))
		Use In (Select('cFacturi0'))

		Delete From cFacturitemp2 Where baza = 0 And TVA = 0 And (rn <> rn0 Or Isnull(rn0))

		* Pun cota 11 pe facturile cu valoare 0, altfel apar cu 21% sau alta cota
		Update cFacturitemp2 Set cota = m.lnCotaTVAStandard Where baza = 0 And TVA = 0
		Update cFacturitemp2 Set cota = 0 Where Inlist(TIP, 'LS', 'AS', 'ASI', 'N', 'V')
		* DELETE FROM cFacturiTemp WHERE baza = 0 AND tva = 0 AND RECNO() NOT in (Select rn FROM cFacturi0) && EROARE

		* Actualizez nrfact cu 0 in afara de prima aparitie a fiecarei facturi. S-au multiplicat din cauza join-ului cu cotele de TVA
		Select id_fact, Min(rn) As rn ;
			From cFacturitemp2 ;
			Group By id_fact ;
			Into Cursor cFacturi2

		Update cFacturitemp2 Set NRFACT = 0 Where rn Not In (Select rn From cFacturi2)
		Use In (Select('cFacturi2'))

		Update cFacturitemp2 Set LOCALITATE = '' Where LOCALITATE = '<ALEGETI LOCALITATEA>'

		Select tippart, Cast(0 As i) As seq, cota, denp, cuip, TIP, tip_document, TIP_PERSOANA, ;
			TARA, COD_TARA, JUDET, COD_JUDET, LOCALITATE, STRADA, Bloc, SCARA, NUMAR, APART, ;
			Sum(NRFACT) As NRFACT, ;
			Round(Sum(baza), 0) As baza, ;
			Round(Sum(TVA), 0) As TVA ;
			From cFacturitemp2 ;
			Group By 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ;
			Order By tippart, denp, cota, TIP ;
			Into Cursor cFacturi Readwrite

		Use In (Select('cFacturiTemp2'))

		*!*			Select j.tippart, Cast(0 As i) As seq, c.cota, j.denumire As denp, j.cod_fiscalfro As cuip, j.TIP, ;
		*!*				IIF(j.tipdoc = 'F', 1, Iif(j.tipdoc = 'O', 2, Iif(j.tipdoc = 'Z', 3, 1))) As tip_document, ;
		*!*				SUM(NRFACT) As NRFACT, ;
		*!*				SUM(Iif(c.cota = 21, baza21, Iif(c.cota = 20, baza20, Iif(c.cota = 19, baza19, Iif(c.cota = 9, baza09, Iif(c.cota = 5, baza05, Iif(c.cota = 0, baza00, baza20))))))) As baza, ;
		*!*				SUM(Iif(c.cota = 21, TVA21, Iif(c.cota = 20, TVA20, Iif(c.cota = 19, TVA19, Iif(c.cota = 9, TVA09, Iif(c.cota = 5, TVA05, Iif(c.cota = 0, 0, TVA20))))))) As TVA ;
		*!*				FROM crsJTVA j, cCote c ;
		*!*				WHERE Alltrim(j.TIP) In ('L','A','V','C','AI','N','LS','AS') ;
		*!*				GROUP By 1,2,3,4,5,6,7 ;
		*!*				ORDER By j.tippart, j.denumire, c.cota, j.TIP ;
		*!*				INTO Cursor cFacturi Readwrite

		lnSeq = 1
		lnTipPart = 0
		Select cFacturi
		Scan
			If lnTipPart <> tippart
				lnSeq = 1
			Else
				lnSeq = lnSeq + 1
			Endif
			lnTipPart = cFacturi.tippart
			Replace seq With m.lnSeq In cFacturi
		Endscan

		lcCaenRevizie = NVL(goFirma.caen_revizie, '2')
		lnCaenRevizie = INT(VAL(m.lcCaenRevizie))
		lcCaenRev2 = IIF(m.lnCaenRevizie = 2, ALLTRIM(LOWER(goFirma.caen)), '')
		lcCaenRev3 = IIF(m.lnCaenRevizie > 2, ALLTRIM(LOWER(goFirma.caen)), '')		

		Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="XFA2_4" APIVersion="3.0.11263.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>

<!-- textul de sus este textul de inceput iar jos urmeaza fisierul xml data -->
<form1>
<body1>
	<HEADER xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
	<sub_per>
		<an_r><<m.lcAn>></an_r><TIP_D394><<This.tip>></TIP_D394><luna_r><<m.lcLuna>></luna_r><sistem><<INT(this.oUnitate.tvaincasare)>></sistem><operatiuni><<INT(this.oUnitate.operatiuni)>></operatiuni>
		<prsAfiliat><<INT(this.oUnitate.PersoaneAfiliate)>></prsAfiliat>
	</sub_per>
	<sub_dateDeIdentificare>
		<cif><<m.lcCodFiscalF>></cif><den><<m.lcDen>></den><adresa><<m.lcAdresa>></adresa><telefon><<ALLTRIM(goFirma.telefon)>></telefon><fax><<ALLTRIM(goFirma.fax)>></fax>
		<mail><<ALLTRIM(LOWER(goFirma.email))>></mail><d_rec>0</d_rec><caen1><<m.lcCaenRev3>></caen1><caen><<m.lcCaenRev2>></caen>
	</sub_dateDeIdentificare>
</body1>
<body10><totalPlata_A/></body10>
		Endtext

		
		* Sectiunea Facturi
		Set Textmerge On To Memvar lcXDP Additive Noshow
\<!-- Facturi  -->
		lnTipPart = 0
		lnNrFacturi = 0
		Select cFacturi
		Scan
			* Exceptie : Pentru perioada 01.07.2016 - 31.12.2016 in cazul selectarii tip L pentru facturi emise catre persoane fizice,
			* cu valoare individuala/persoana mai mica sau egala cu 10.000 lei
			* Le-am sters la inceput
			*!*				If cFacturi.tippart = 2 AND cFacturi.tip_persoana = 2 and cFacturi.TIP = 'L' And ABS(cFacturi.baza + cFacturi.TVA) <= 10000 AND m.llPerioada122016 && persoane fizice
			*!*					Loop
			*!*				Endif
			lnNrFacturi = lnNrFacturi + 1

			If m.lnTipPart <> cFacturi.tippart
				If m.lnTipPart <> 0
\</tip1>
				Endif

				Do Case
					Case tippart = 1
\<!-- Persoane inregistrate in scopuri de TVA  -->
					Case tippart = 2
\<!-- Persoane neinregistrate in scopuri de TVA  -->
					Case tippart = 3
\<!-- Persoane intracomunitare  -->
					Case tippart = 4
\<!-- Persoane extracomunitare  -->
				Endcase

\<tip1>
\<tip_partener><<cFacturi.tippart>></tip_partener>
			Endif
\       <sub1>
\       	<sub2>
\       		<seq2><<cFacturi.seq>></seq2>
\       		<denP><<XmlSpecialCharacters(RemoveCharacters(Alltrim(cFacturi.denp), .T.))>></denP>
\        		<Tip><<ALLTRIM(cFacturi.tip)>></Tip>
\        		<cota><<cFacturi.cota>></cota>
\        		<cuiP><<ALLTRIM(cFacturi.cuip)>></cuiP>
\        		<nrfact><<cFacturi.nrfact>></nrfact>
\        		<baza><<cFacturi.baza>></baza>
\				<tva><<IIF(INLIST(ALLTRIM(cFacturi.TIP), 'N', 'V', 'AS', 'LS'), '', ALLTRIM(STR(cFacturi.tva)))>></tva>
			If cFacturi.tippart = 2 && persoane neinregistrate in scopuri TVA
				If ALLTRIM(cFacturi.TIP) = 'N' && persoane neinregistrate in scopuri TVA
\		<tip_document><<cFacturi.tip_document>></tip_document>
				ENDIF 
\		<adresa>
\			<tara><<ALLTRIM(NVL(cFacturi.COD_TARA, "RO"))+ '--' + PROPER(ALLTRIM(NVL(cFacturi.TARA, 'ROMANIA')))>></tara>
\			<judet><<ALLTRIM(NVL(cFacturi.cod_judet, ""))>></judet>
\			<localitate><<ALLTRIM(NVL(cFacturi.localitate, ""))>></localitate>
\			<strada><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(NVL(cFacturi.strada, "")), .T.))>></strada>
\			<nr><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(NVL(cFacturi.numar,"")), .T.))>></nr>
\			<bloc><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(NVL(cFacturi.bloc, "")), .T.))>></bloc>
\			<apart><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(NVL(cFacturi.apart,"")), .T.))>></apart>
\		</adresa>
			Endif
\      		</sub2>
			If cFacturi.tippart = 2 OR INLIST(ALLTRIM(cFacturi.tip), 'V', 'C') && persoane neinregistrate in scopuri TVA sau taxare inversa, completez produsele - nu am produse :(, doar nrfacturi, baza si tva
\		<!-- Produse  -->
\		<sub3>
\			<seq3>1</seq3>
\			<codPR></codPR>
\			<nrFacturi><<cFacturi.nrfact>></nrFacturi>
\			<baza><<cFacturi.baza>></baza>
\			<tva><<IIF(INLIST(cFacturi.TIP, 'N', 'V', 'AS', 'LS'), '', ALLTRIM(STR(cFacturi.tva)))>></tva>
\		</sub3>
			ENDIF && tippart = 2 OR INLIST(ALLTRIM(tip), 'V', 'C')
\	    </sub1>

			lnTipPart = cFacturi.tippart
		ENDSCAN && cFacturi
		IF m.lnNrFacturi > 0
\</tip1>
		ENDIF 
		* Sectiunea AMEF
\		<!-- Case marcat  -->
\		<tip2>

		Select crsAMEF
		Scan
\		<amef>
\		<tip><<tip>></tip>
\		<luna><<luna>></luna>
IF tip <> 2
\		<nrAMEF><<nramef>></nrAMEF>
\		<nrbonuri><<nrbonuri>></nrbonuri>
ENDIF 
\		<incasari><<incasari>></incasari>
\		<detaliu_amef><cota>5</cota><baza><<baza05>></baza><tva><<tva05>></tva></detaliu_amef>
\		<detaliu_amef><cota>9</cota><baza><<baza09>></baza><tva><<tva09>></tva></detaliu_amef>
\		<detaliu_amef><cota>11</cota><baza><<baza11>></baza><tva><<tva11>></tva></detaliu_amef>
\		<detaliu_amef><cota>19</cota><baza><<baza19>></baza><tva><<tva19>></tva></detaliu_amef>
\		<detaliu_amef><cota>20</cota><baza><<baza20>></baza><tva><<tva20>></tva></detaliu_amef>
\		<detaliu_amef><cota>21</cota><baza><<baza21>></baza><tva><<tva21>></tva></detaliu_amef>
\		</amef>
		Endscan && crsAMEF

\		</tip2>

IF m.llExportSectiuneaI
		* Sectiunea I1 facturi simplificate si bonuri fiscale cu CUI
\<I1>
\<info1>
\<HeaderRow xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
		Select crsSectiuneaI
		Scan
			lcTag = Iif(punct = '1.1', 'L', Iif(punct = '1.2', 'LI', Iif(punct = '1.3', 'A', Iif(punct = '1.4', 'AI', Iif(punct = '1.5', 'AB', 'L'))))) + Alltrim(Str(proctva)) && L21
\<<'<' + m.lcTag + '>'>><baza><<baza>></baza><tva><<tva>></tva></<<m.lcTag + '>'>>
		Endscan
\</info1>
\</I1>

		* Sectiunea I2 Alte informatii
\		<i2>
\		<!-- 2.1 Plaja de facturi alocate In Perioada de raportare  -->
		Select crsFacturiAlocate
		Scan
\		<plaja1><serie_i><<ALLTRIM(seria)>></serie_i><nr_i><<nr1>></nr_i><serie_f><<ALLTRIM(seria)>></serie_f><nr_f><<nr2>></nr_f></plaja1>
		Endscan
\		<!-- 2.2 Numarul Total de facturi emise, In Perioada de raportare, din Plaja de facturi alocate:  -->
\		<facturi2>

\		<fact_emise><<This.oUnitate.nr_facturi_emise>></fact_emise>
		Select crsFacturiEmise
		Scan
\		<plaja><serie_i><<ALLTRIM(seria)>></serie_i><nr_i><<nr1>></nr_i><serie_f><<ALLTRIM(seria)>></serie_f><nr_f><<nr2>></nr_f></plaja>
		Endscan

\		<!-- 1 facturi stornate  -->
\		<!-- 2 facturi anulate  -->
\		<!-- 3 Facturi autofacturare  -->
\		<!-- 4 Facturi furnizor  -->
		Sele crsFacturiEmiseTip
		Scan
\		<detaliu_fact>
\		<tip_fact><<ntip>></tip_fact>
\		<serie><<ALLTRIM(seria)>></serie>
\		<nr><<nr>></nr>
			If ntip = 3 && autofacturare
				Select crsAutoFacturi
				Locate For Alltrim(crsAutoFacturi.seria) = Alltrim(crsFacturiEmiseTip.seria) And crsAutoFacturi.nr = crsFacturiEmiseTip.nr
				If Found()
\				<autofacturare>
\				<baza21><<baza21>></baza21><tva21><<tva21>></tva21>
\				<baza11><<baza11>></baza11><tva11><<tva11>></tva11>
\				<baza19><<baza19>></baza19><tva19><<tva19>></tva19>
\				<baza9><<baza09>></baza9><tva9><<tva09>></tva9>
\				<baza5><<baza05>></baza5><tva5><<tva05>></tva5>
\				<baza24><<baza24>></baza24><tva24><<tva24>></tva24>
\				<baza20><<baza20>></baza20><tva20><<tva20>></tva20>
\				</autofacturare>
				Endif && FOUND
			Endif
\		</detaliu_fact>
		Endscan && crsFacturiEmiseTip

\		</facturi2>

\		<!-- 2.3 Numarul total de facturi emise, in perioada de raportare, de beneficiari in numele persoanei impozabile:  -->
\		<facturi3>
\		<fact_emise><<This.oUnitate.nr_facturi_beneficiari>></fact_emise>

		Select crsFacturiTerti
		SCAN FOR punct = '2.3'
\		<plaja><serie_i><<ALLTRIM(seria)>></serie_i><nr_i><<nr1>></nr_i><serie_f><<ALLTRIM(seria)>></serie_f><nr_f><<nr2>></nr_f><den><<ALLTRIM(NVL(denumire,''))>></den><cui><<ALLTRIM(NVL(cod_fiscal, ''))>></cui></plaja>
		ENDSCAN
\		</facturi3>

\		<!-- 2.4 Numarul total de facturi emise, in perioada de raportare, de terti in numele persoanei impozabile:  -->
\		<facturi4>
\		<fact_emise><<This.oUnitate.nr_facturi_terti>></fact_emise>
		Select crsFacturiTerti
		SCAN FOR punct = '2.4'
\		<plaja><serie_i><<ALLTRIM(seria)>></serie_i><nr_i><<nr1>></nr_i><serie_f><<ALLTRIM(seria)>></serie_f><nr_f><<nr2>></nr_f><den><<ALLTRIM(NVL(denumire,''))>></den><cui><<ALLTRIM(NVL(cod_fiscal, ''))>></cui></plaja>
		ENDSCAN
\		</facturi4>

\		</i2>

		* Sectiunea 4
		Select crsSectiuneaI4
		Locate For punct = '4.1'
		If Found()
\		<date_deduc><deduc21><<tva21>></deduc21><deduc11><<tva11>></deduc11><deduc19><<tva19>></deduc19><deduc9><<tva09>></deduc9><deduc5><<tva05>></deduc5><deduc24><<tva24>></deduc24><deduc20><<tva20>></deduc20></date_deduc>
		Endif && FOUND

		* Sectiunea 5
\		<date_deduc_TVAi>
		Select crsSectiuneaI4
		Locate For punct = '5.1'
		If Found()
\		<date51><deduc21><<tva21>></deduc21><deduc11><<tva11>></deduc11><deduc19><<tva19>></deduc19><deduc9><<tva09>></deduc9><deduc5><<tva05>></deduc5><deduc24><<tva24>></deduc24><deduc20><<tva20>></deduc20></date51>
		Endif
		Locate For punct = '5.2'
		If Found()
\		<date52><deduc21><<tva21>></deduc21><deduc11><<tva11>></deduc11><deduc19><<tva19>></deduc19><deduc9><<tva09>></deduc9><deduc5><<tva05>></deduc5><deduc24><<tva24>></deduc24><deduc20><<tva20>></deduc20></date52>
		Endif
		Locate For punct = '5.3'
		If Found()
\		<date53><deduc21><<tva21>></deduc21><deduc11><<tva11>></deduc11><deduc19><<tva19>></deduc19><deduc9><<tva09>></deduc9><deduc5><<tva05>></deduc5><deduc24><<tva24>></deduc24><deduc20><<tva20>></deduc20></date53>
		Endif

		* Nu am adunat date despre sectiunea 6 si 7
		If .F.
\		<date_marja><incasari1/><costuri1/><marja1/><tva1/><tva2/><marja2/><costuri2/><incasari2/></date_marja>
\		<!-- 7. In situatia In care ati desfasurat, In Perioada de raportare, activitati dintre cele inscrise In lista veti selecta activitatea corespunzatoare
\		si veti inscrie valoarea livrarilor/prestarilor, precum si TVA aferenta -->
\		<lista>
\		<detaliu_lista>
\		<tip_operatiune>1</tip_operatiune>
\		<activitate>4520</activitate>
\		<livrari>1001</livrari>
\		<deduc5/>
\		<deduc9/>
\		<deduc20>201</deduc20>
\		<deduc21/>
\		</detaliu_lista>
\		<detaliu_lista>
\		<tip_operatiune>2</tip_operatiune>
\		<activitate>4520</activitate>
\		<livrari>1000</livrari>
\		<deduc5/>
\		<deduc9>90</deduc9>
\		<deduc20/>
\		<deduc21/>
\		</detaliu_lista>
\		</lista>
		Endif && .F.

\		</date_deduc_TVAi>

ENDIF && llExportSectiuneaI

		* Reprezentant fiscal
\<body11>
\	<sub_dateDeIdentificare>
\		<cifR><<ALLTRIM(this.oUnitate.CUIRepr)>></cifR>
\		<denR><<ALLTRIM(this.oUnitate.DenRepr)>></denR>
\		<adresaR><<ALLTRIM(this.oUnitate.AdrRepr)>></adresaR>
\		<telefonR><<ALLTRIM(this.oUnitate.TelRepr)>></telefonR>
\		<faxR><<ALLTRIM(this.oUnitate.FaxRepr)>></faxR>
\		<mailR><<ALLTRIM(this.oUnitate.EmailRepr)>></mailR>
\	</sub_dateDeIdentificare>
\</body11>
\<intocmit>
\<tip_reprez><<ALLTRIM(This.oUnitate.TipRepr)>></tip_reprez>
\<reprezentant><<ALLTRIM(this.oUnitate.DenRepr)>></reprezentant>
\<den_intocmit><<ALLTRIM(this.oUnitate.DenIntocmit)>></den_intocmit>
\<functie><<ALLTRIM(this.oUnitate.FunctieIntocmit)>></functie>
\<calitate_alta><<ALLTRIM(this.oUnitate.Calitate2Intocmit)>></calitate_alta>
\<calitate><<ALLTRIM(this.oUnitate.CalitateIntocmit)>></calitate>
\<cui_intocmit><<ALLTRIM(this.oUnitate.CUIIntocmit)>></cui_intocmit>
\<tip_intocmit><<IIF(this.oUnitate.PJIntocmit = 2, 0, 1)>></tip_intocmit>
\<schim_opt>0</schim_opt>
\<optiune><<INT(this.oUnitate.optiuneANAF)>></optiune>
\</intocmit>

\		</form1>

\		<!-- aici se Termina XML-Data iar jos urmeaza textul de sfarsit care face referire la documentul PDF Prin href="D394_XML_2016.PDF"  -->

\</xfa:data>
\</xfa:datasets>
\<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
\</xdp:xdp>
		Set Textmerge To

		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				open_default_app("http://static.anaf.ro/static/10/Anaf/Declaratii_R/394.html")
				amessagebox('Nu exista fisierul ' + m.lcFisierPDFOriginal + CRLF + ;
					  'Copiati pdf-ul inteligent D394 de pe website-ul ANAF in directorul ' + Justpath(m.lcFisierPDFOriginal) + CRLF + ;
					  'cu numele ' + Justfname(m.lcFisierPDFOriginal) + '.', 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)

	Endproc && do_genereaza_xdp

	*==========================================
	Procedure UpdatePDF
		Lparameters tcPDFFile
		*** tcPDFFile: calea completa a fisierului pdf D:\CONTAFIN\COMUNCONTAFIN\d390.pdf

		Local lcFile, lcPath, llOpen
		lcFile = Justfname(m.tcPDFFile)
		lcPath = Addbs(Justpath(m.tcPDFFile))
		llOpen = .F.

		Return DownloadSupportFile(m.lcFile, m.lcPath, m.llOpen) && proceduri_comune.prg

	Endproc && UpdatePDF

	Procedure do_executa

		*** EXPORT D394
		Local lcAppPath, lcCaleFisier, lcCaleFisierPDF, lcComunPath, lcFileName, lcFileNamePDF
		Local lcFisierPDFNou, lcFisierPDFOriginal, lcFisierTXT, lcFisierXDP, lcFisierXLS, lcMesaj
		Local lcNumeFisier, lcRun, lcTempPath, llSucces, lnPos, loEx, lcCaenRevizie, lnCaenRevizie 
		lcAppPath = Addbs(gcAppPath)
		lcComunPath = lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If lnPos > 0
			lcComunPath = Left(m.lcAppPath, lnPos)
		Endif
		lcComunPath = lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		*** Creez structura director export (caletemp\D394\aaaa_ll\firma\
		llSucces = .F.
		Try
			lcTempPath = m.gcBasePath
			lcCaleFisier = m.lcTempPath + "D394\" && c:\temp\D394\

			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Alltrim(Str(gnAn)) + "_" + Padl(Alltrim(Str(gnLuna)), 2, "0") + "\"&& c:\temp\D394\2011_01\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Strtran(gcFirma, [ ], [_]) + "\" && c:\temp\D390\2011_01\firma\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisierPDF = m.lcCaleFisier  && c:\temp\D394\2011_01\firma\
			If !Directory(m.lcCaleFisierPDF)
				Md (m.lcCaleFisierPDF)
			Endif
			This.CaleFisierPDF = m.lcCaleFisierPDF

			llSucces = .T.
		Catch To loEx
			lcMesaj = "Creare director " + lcCaleFisier + " " + loEx.Message

			amessagebox(lcMesaj, 0 + 48)
		Endtry
		If !m.llSucces
			Return
		Endif

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', Strtran(Alltrim(Nvl(gofirma.cod_fiscal, '')), 'RO', ''))
		Endif

		lcCaenRevizie = NVL(goFirma.caen_revizie, '2')
		lnCaenRevizie = INT(VAL(m.lcCaenRevizie))

		lcNumeFisier = "d394_" + gofirma.codfiscalfro + "_" + Alltrim(Str(gnAn)) + "_" + Padl(gnLuna, 2, "0")
		Do Case
			Case m.gnAn * 12 + m.gnLuna >= 2025 * 12 + 8
				lcFisierPDFOriginal = lcComunPath + "d394_202508.pdf"

			Otherwise
				lcFisierPDFOriginal = lcComunPath + "d394_202508.pdf"
		Endcase

		This.UpdatePDF(m.lcFisierPDFOriginal) && actualizez pdf de la romfast

		lcFileName = m.lcCaleFisier + lcNumeFisier

		lcFileNamePDF = m.lcCaleFisierPDF + lcNumeFisier
		lcFisierPDFNou = m.lcFileNamePDF + [.pdf]
		lcFisierXDP = m.lcFileNamePDF + [.xdp]

		lcFisierTXT = m.lcFileName + [.txt]
		lcFisierXLS = m.lcFileName + [.xls]

		If File(lcFisierTXT)
			Delete File (lcFisierTXT)
		Endif
		If File(lcFisierPDFNou)
			Delete File (lcFisierPDFNou)
		Endif
		If File(lcFisierXDP)
			Delete File (lcFisierXDP)
		Endif

		This.do_genereaza_xdp(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)
		This.cFisierPDF = ''
		If File(lcFisierXDP) And File(m.lcFisierPDFNou)
			This.cFisierPDF = m.lcFisierPDFNou
			open_default_app(lcFisierXDP)
		Endif

		*** deschid explorer in directorul de export
		lcRun = [RUN /N7 explorer.exe "] + m.lcCaleFisier + ["]
		&lcRun
	Endproc && do_executa

	Procedure do_verifica_pdf
		Local loProcess As "api_apprun" Of "process.vcx"
		Local lcAppPath, lcValidatorFile, lcComunPath, lcFisierPDF, lnPos
		lcAppPath = Addbs(gcAppPath)
		lcComunPath = m.lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If m.lnPos > 0
			lcComunPath = Left(m.lcAppPath, m.lnPos)
		Endif
		lcComunPath = m.lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		lcValidatorFile = m.lcComunPath + [dist\DUKIntegrator.jar]

		amessagebox('Validati PDF si salvati-l. Apoi inchideti acest mesaj. Dupa ce porneste DukIntegrator, apasati validare in validator pentru a verifica fisierul PDF', 0 + 48, _Screen.Caption)

		lcFisierPDF = This.cFisierPDF
		If !File(m.lcFisierPDF)
			amessagebox('Nu exista fisierul ' + m.lcFisierPDF , 0 + 48, _Screen.Caption)
		Endif

		loProcess = Newobject("api_apprun", "process.vcx")
		loProcess.icLaunchDir = Justpath(m.lcValidatorFile)
		loProcess.iccommandline = [java -jar "DUKIntegrator.jar" "] + m.lcFisierPDF + ["]

		*!*	loProcess.launchappandwait()
		m.loProcess.launchapp()
	Endproc && do_verifica_pdf

Enddefine && D394_2025
*************************************************************************

Define Class D394_2016 As Custom

	Total = 0
	rectificativa = 0
	TIP = 'L'
	Perioada = 1
	NumarCoduriFiscale = 0

	oForm = Null && formular D384
	oUnitate = Null

	CaleFisierPDF = '' && director fisier pdf


	* Afiseaza formularul 394 fara facturi, pentru alegerea tipurilor de documente, optiuni etc.
	* Din formular se vor selecta facturile in functie de optiuni
	Procedure List

		Local loDecl As "frm_decl394_2016"
		Local lcSelect, lcTip, llSilent, llSucces, lnNumarLuni, lcValoare
		llSilent = .F. && m.tlSilent

		lcTip = Upper(Nvl(citeste_optiune('D394_TIP'), 'LUNAR'))
		lcTip = Iif(!Inlist(m.lcTip, 'LUNAR', 'TRIMESTRIAL', 'SEMESTRIAL', 'ANUAL'), 'LUNAR', m.lcTip)

		lcSelect = Select()

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', Strtran(Alltrim(gofirma.cod_fiscal), 'RO', ''))
		Endif

		Private pounitate
		pounitate = Createobject("empty")
		AddProperty(pounitate, "tva_incasare", goCalendar.tva_incasare)

		AddProperty(pounitate, "an", m.gnAn)
		AddProperty(pounitate, "luna1", m.gnLuna)
		AddProperty(pounitate, "luna2", m.gnLuna)

		AddProperty(pounitate, "tip", m.lcTip)
		AddProperty(pounitate, "luniextra", 0)
		AddProperty(pounitate, "tvanedeductibil", 0)
		AddProperty(pounitate, "codi", Strtran(Strtran(Alltrim(Upper(Nvl(gofirma.cod_fiscal, ''))), 'RO', ''), 'R', ''))
		AddProperty(pounitate, "deni", Nvl(Alltrim(gofirma.firma), ''))
		AddProperty(pounitate, "adr", Nvl(Alltrim(gofirma.adresa), ''))
		poUnitate.adr = PADR(poUnitate.adr, 150, ' ')			
		AddProperty(pounitate, "tel", Nvl(Alltrim(gofirma.telefon), ''))
		AddProperty(pounitate, "fax", Nvl(Alltrim(gofirma.fax), ''))
		AddProperty(pounitate, "email", Nvl(Alltrim(gofirma.email), ''))

		AddProperty(pounitate, "taraR", "")
		AddProperty(pounitate, "codiR", "")
		AddProperty(pounitate, "deniR", "")
		AddProperty(pounitate, "adrR", "")
		AddProperty(pounitate, "telR", "")
		AddProperty(pounitate, "faxR", "")
		AddProperty(pounitate, "emailR", "")

		AddProperty(pounitate, "numeD", "")
		AddProperty(pounitate, "functieD", "")

		AddProperty(pounitate, "operatiuni", 1)
		AddProperty(pounitate, "tvaincasare", goCalendar.tva_incasare)
		AddProperty(pounitate, "nr_facturi_emise", 0)
		AddProperty(pounitate, "nr_facturi_beneficiari", 0)
		AddProperty(pounitate, "nr_facturi_terti", 0)				

		AddProperty(pounitate, "lPerioadaDoarRO", Between(m.gnAn * 12 + m.gnLuna, 2016 * 12 + 7, 2016 * 12 + 9)) && intre 07/2016 si 09/2016 se declara doar partenerii cu RO
		AddProperty(pounitate, "ExportSectiuneaI", 1) && intre 07/2016 si 09/2016 nu se completeaza sectiunea I
		AddProperty(pounitate, "PersoaneAfiliate", 0) && 0 = fara persoane afiliate, 1 = cu persoane afiliate

		AddProperty(pounitate, "DenRepr", Alltrim(citeste_optiune('D394_DEN_REPREZ')))
		AddProperty(pounitate, "AdrRepr", Alltrim(citeste_optiune('D394_ADR_REPREZ')))
		poUnitate.AdrRepr = PADR(poUnitate.AdrRepr, 150, ' ')		
		AddProperty(pounitate, "CuiRepr", Alltrim(citeste_optiune('D394_CUI_REPREZ')))
		AddProperty(pounitate, "TipRepr", Alltrim(citeste_optiune('D394_TIP_REPREZ')))
		AddProperty(pounitate, "TelRepr", Alltrim(citeste_optiune('D394_TEL_REPREZ')))
		AddProperty(pounitate, "FaxRepr", Alltrim(citeste_optiune('D394_FAX_REPREZ')))
		AddProperty(pounitate, "EmailRepr", Alltrim(citeste_optiune('D394_EMAIL_REPREZ')))

		AddProperty(pounitate, "DenIntocmit", Alltrim(citeste_optiune('D394_DEN_INTOCMIT')))
		AddProperty(pounitate, "CuiIntocmit", Alltrim(citeste_optiune('D394_CUI_INTOCMIT')))
		AddProperty(pounitate, "PJIntocmit", Int(Val(Nvl(citeste_optiune('D394_PJ_INTOCMIT'), '1'))) )
		AddProperty(pounitate, "CalitateIntocmit", Alltrim(citeste_optiune('D394_CALITATE_INTOCMIT')))
		poUnitate.CalitateIntocmit = PADR(poUnitate.CalitateIntocmit, 100, ' ')
		AddProperty(pounitate, "Calitate2Intocmit", Alltrim(citeste_optiune('D394_CALITATE2_INTOCMIT')))
		poUnitate.Calitate2Intocmit = PADR(poUnitate.Calitate2Intocmit, 100, ' ')		
		AddProperty(pounitate, "FunctieIntocmit", Alltrim(citeste_optiune('D394_FUNCTIE_INTOCMIT')))
		poUnitate.FunctieIntocmit= PADR(poUnitate.FunctieIntocmit, 100, ' ')		

		AddProperty(pounitate, "NrAMEF", Int(Val(Nvl(citeste_optiune('D394_NR_AMEF'), '1'))))
		AddProperty(pounitate, "OptiuneANAF", Int(Val(Nvl(citeste_optiune('D394_OPTIUNE_ANAF'), '0'))))	 && 1 = Se da acordul pentru consultarea D394 de catre furnizori/clienti

		AddProperty(pounitate, "cTextFacturiEmiseAlocate", "") && facturi emise care nu sunt in plaja de facturi alocate

		This.oUnitate = pounitate


		Create Cursor crsFdocTip (id_fdoc N(10) null, fel_document c(100) null, tip_doc c(10) Null, bifa N(1))

		*!*			llSucces = update_fdoc([NVL(tip_doc,'!') <> '!'], 'crsFdocTipTemp') && doar fel document cu tip
		*!*			AddProperty(pounitate, "tipdoc_factura", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'F'])) && lista tip documente = factura
		*!*			AddProperty(pounitate, "tipdoc_bonfiscal_cu_cui",  cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'C'])) && lista tip documente = bon fiscal cu factura
		*!*			AddProperty(pounitate, "tipdoc_bonfiscal_fara_cui", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'B'])) && lista tip documente = bon fiscal fara factura
		*!*			AddProperty(pounitate, "tipdoc_factura_simplificata", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'M'])) && lista tip documente = factura simplificata
		*!*			AddProperty(pounitate, "tipdoc_exceptate_amef", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'E'])) && lista tip documente = exceptate de la AMEF (bilete)
		*!*			AddProperty(pounitate, "tipdoc_borderou_achizitii", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'O'])) && lista tip documente = borderou achizitii
		*!*			AddProperty(pounitate, "tipdoc_carnet_comercializare", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'Z'])) && lista tip documente = carnet comercialziare
		*!*			AddProperty(pounitate, "tipdoc_achizitii_second_hand", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'H'])) && lista tip documente = achizitii second hand
		*!*			AddProperty(pounitate, "tipdoc_servicii_turism", cursor2lista("crsFDocTipTemp", "id_fdoc", ",", [tip_doc = 'U'])) && lista tip documente = servicii turism


		llSucces = update_serii(.F., [isautofactura = 1 or isbeneficiari = 1 or isterti = 1 or isfurnizori = 1]) && crsSerii - seriile marcate autofactura/emise de beneficiari/emise de terti/emise in numele furnizorilor!
		If !m.llSucces
			Create Cursor crsSerii (seria c(20), isautofactura N(1), isbeneficiari N(1), isterti N(1), isfurnizori N(1))
		Endif

		Create Cursor crsSectiuneaI (punct c(4) Null, baza N(20) Null, TVA N(20) Null, proctva N(2) Null)
		Create Cursor crsSectiuneaI4 (punct c(4) Null, TVA24 N(20) Null, TVA20 N(20) Null, TVA19 N(20) Null, TVA09 N(20) Null, TVA05 N(20) Null)
		Create Cursor crsSectiuneaI6 (punct c(4) Null, incasari N(20) Null, costuri N(20) Null, marja N(20) Null, TVA N(20) Null) && agentii turism si second hand
		Create Cursor crsSectiuneaI7 (caen c(4) Null, TIP c(10), baza N(20), TVA24 N(20) Null, TVA20 N(20) Null, TVA19 N(20) Null, TVA09 N(20) Null, TVA05 N(20) Null)

		Create Cursor crsFacturiAlocate (seria c(20) Null, nr1 N(20), nr2 N(20))
		Create Cursor crsFacturiEmise (seria c(20) Null, nr1 N(20), nr2 N(20))
		Create Cursor crsFacturiEmiseTip (TIP c(20), ntip N(1), seria c(20) Null, nr N(20))
		Create Cursor crsAutoFacturi (seria c(20) Null, nr N(20), baza24 N(16, 4) Null, baza20 N(16, 4) Null, baza19 N(16, 4) Null, baza09 N(16, 4) Null, baza05 N(16, 4) Null, ;
			  TVA24 N(16, 4) Null, TVA20 N(16, 4) Null, TVA19 N(16, 4) Null, TVA09 N(16, 4) Null, TVA05 N(16, 4) Null)

		Create Cursor crsFacturiTerti (punct c(4) Null, seria c(20) Null, nr1 N(20), nr2 N(20), denumire c(100) Null, cod_fiscal c(20) Null)
		Create Cursor crsSeriiFacturi (TIP c(20), seria c(20), descriere c(100))
		Insert Into crsSeriiFacturi (TIP, seria, descriere) ;
			Select 'AUTOFACTURA' As TIP, serie As seria, 'serie autofacturi' As descriere ;
			From crsSerii Where isautofactura = 1 ;
			Union ;
			Select 'BENEFICIARI' As TIP, serie As seria, 'serie facturi emise de beneficiari in numele persoanei impozabile' As descriere ;
			From crsSerii Where isbeneficiari = 1 ;
			Union ;
			Select 'TERTI' As TIP, serie As seria, 'serie facturi emise de terti in numele persoanei impozabile' As descriere ;
			From crsSerii Where isterti = 1 ;
			Union ;
			Select 'FURNIZORI' As TIP, serie As seria, 'serie facturi emise de pers impozabila in calitate de beneficiar, in numele furnizorilor' As descriere ;
			From crsSerii Where isfurnizori = 1

		Go Top In crsSeriiFacturi
		AddProperty(pounitate, "serie_autofacturare", cursor2lista("crsSerii", "serie", ",", [isautofactura=1]))
		AddProperty(pounitate, "serie_facturi_beneficiari", cursor2lista("crsSerii", "serie", ",", [isbeneficiari=1]))
		AddProperty(pounitate, "serie_facturi_terti", cursor2lista("crsSerii", "serie", ",", [isterti=1]))
		AddProperty(pounitate, "serie_facturi_furnizori", cursor2lista("crsSerii", "serie", ",", [isfurnizori=1]))

		close_serii()

		Create Cursor crsAMEF (TIP N(1), luna N(2), nramef N(10), nrbonuri N(10), incasari N(16, 4), ;
			  baza24 N(16, 4) Null, baza20 N(16, 4) Null, baza19 N(16, 4) Null, baza09 N(16, 4) Null, baza05 N(16, 4) Null, ;
			  TVA24 N(16, 4) Null, TVA20 N(16, 4) Null, TVA19 N(16, 4) Null, TVA09 N(16, 4) Null, TVA05 N(16, 4) Null)

		Create Cursor crsPersFizice (TIP c(2), NRFACT N(10), baza N(16, 4) Null, ;
			  baza24 N(16, 4) Null, baza20 N(16, 4) Null, baza19 N(16, 4) Null, baza09 N(16, 4) Null, baza05 N(16, 4) Null, ;
			  TVA24 N(16, 4) Null, TVA20 N(16, 4) Null, TVA19 N(16, 4) Null, TVA09 N(16, 4) Null, TVA05 N(16, 4) Null)

		Create Cursor crsJTVA(seq N(20), tipj c(2), tippart N(1), tipdoc c(2) Null, TIP c(2), dataact D Null, dataireg D Null, serie_act c(10) Null,  nract N(20) Null, id_fdoc N(12) Null, fel_document c(30) Null, id_sucursala N(7) Null, sucursala c(100) Null, ;
			  iscif N(1) Null, cifvalid N(1) Null, id_part N(20) Null, cod_fiscal c(30) Null, cod_fiscalfro c(30) Null, atribut_fiscal c(2) Null, denumire c(100) Null, TIP_PERSOANA N(1) Null, AFILIAT N(5) Null, CODTARA V(5) NULL, ;
			  TARA c(100) Null, COD_TARA c(10) Null, JUDET c(100) Null, COD_JUDET c(10) Null, LOCALITATE c(100) Null, STRADA c(100) Null, NUMAR c(20) Null, Bloc c(20) Null, SCARA c(50) Null, APART c(100) Null, ;
			  baza N(16, 4) Null, TVA N(16, 4) Null, tibaza N(16, 4) Null, titva N(16, 4) Null, ;
			  baza24 N(16, 4) Null, baza20 N(16, 4) Null, baza19 N(16, 4) Null, baza09 N(16, 4) Null, baza05 N(16, 4) Null, baza00 N(16, 4) Null, ;
			  TVA24 N(16, 4) Null, TVA20 N(16, 4) Null, TVA19 N(16, 4) Null, TVA09 N(16, 4) Null, TVA05 N(16, 4) Null, ;
			  tibaza24 N(16, 4) Null, tibaza20 N(16, 4) Null, tibaza19 N(16, 4) Null, tibaza09 N(16, 4) Null, tibaza05 N(16, 4) Null, ;
			  titva24 N(16, 4) Null, titva20 N(16, 4) Null, titva19 N(16, 4) Null, titva09 N(16, 4) Null, titva05 N(16, 4) Null, ;
			  tva_incasare N(1), FACT_TVA_INCASARE N(1), NRFACT N(1), EMISA_PERIOADA N(1), ;
			  ro24t N(16, 4) Null, ro20t N(16, 4) Null, ro19t N(16, 4) Null, RO09T N(16, 4) Null, ro05t N(16, 4) Null)

		loDecl = Createobject("frm_decl394_2016")
		*DO FORM frm_decl394_2016 NAME loDecl NOSHOW
		loDecl.oParent = This
		This.oForm = loDecl
		loDecl.Show(1)

		scrie_optiune('D394_DEN_REPREZ', pounitate.DenRepr, 'D394 denumire reprezentant')
		scrie_optiune('D394_ADR_REPREZ', pounitate.AdrRepr, 'D394 adresa reprezentant')
		scrie_optiune('D394_CUI_REPREZ', pounitate.CUIRepr, 'D394 CUI reprezentant')
		scrie_optiune('D394_TIP_REPREZ', pounitate.TipRepr, 'D394 tip reprezentant')
		scrie_optiune('D394_TEL_REPREZ', pounitate.TelRepr, 'D394 telefon reprezentant')
		scrie_optiune('D394_FAX_REPREZ', pounitate.FaxRepr, 'D394 fax reprezentant')
		scrie_optiune('D394_EMAIL_REPREZ', pounitate.EmailRepr, 'D394 email reprezentant')
		scrie_optiune('D394_DEN_INTOCMIT', pounitate.DenIntocmit, 'D394 denumire intocmit')
		scrie_optiune('D394_CUI_INTOCMIT', pounitate.CUIIntocmit, 'D394 CUI intocmit')
		scrie_optiune('D394_PJ_INTOCMIT', Alltrim(Str(pounitate.PJIntocmit)), 'D394 persoana juridica intocmit')
		scrie_optiune('D394_CALITATE_INTOCMIT', pounitate.CalitateIntocmit, 'D394 calitate intocmit')
		scrie_optiune('D394_CALITATE2_INTOCMIT', pounitate.Calitate2Intocmit, 'D394 alta calitate intocmit')
		scrie_optiune('D394_FUNCTIE_INTOCMIT', pounitate.FunctieIntocmit, 'D394 functie intocmit')
		scrie_optiune('D394_TIP', pounitate.TIP, 'Tip declaratie 394: LUNAR/TRIMESTRIAL/SEMESTRIAL/ANUAL')
		scrie_optiune('D394_NR_AMEF', Alltrim(Str(pounitate.nramef)), 'D394 nr AMEF')
		scrie_optiune('D394_OPTIUNE_ANAF', Alltrim(Str(pounitate.OptiuneANAF)), 'D394 optiunea ANAF; 1=DA/0=NU')

		close_fdoc()
	Endproc && List

	***************************************************
	* Citeste JV/JC
	* Configureaza tipurile de documente (facturi, facturi simplificate, bonuri fiscale etc.)
	***************************************************
	Procedure do_citeste_date
		Local llSucces, lcSelect, loUnitate
		Local lcTip, lnAn, lnLuna1, lnLuna2, lnNumarLuni, lnPerioada
		lcSelect = Select()
		loUnitate = This.oUnitate

		* Luna de inceput a perioadei
		* 1 = gnLuna
		* 3 = 1,4,7,10
		* 6 = 1,7
		* 12 = 1
		lnAn = loUnitate.an
		lcTip = loUnitate.TIP
		This.TIP = Left(m.lcTip, 1)
		lnNumarLuni = Iif(m.lcTip = "TRIMESTRIAL", 3, Iif(m.lcTip = "SEMESTRIAL", 6, Iif(m.lcTip = "ANUAL", 12, 1)))

		lnPerioada = Ceil(m.gnLuna / m.lnNumarLuni)
		lnLuna1 = (lnPerioada - 1) * lnNumarLuni + 1
		lnLuna2 = lnPerioada * lnNumarLuni

		loUnitate.luna1 = m.lnLuna1
		loUnitate.luna2 = m.lnLuna2

		llPerioadaDoarRO = Between(m.gnAn * 12 + m.gnLuna, 2016 * 12 + 7, 2016 * 12 + 9) && intre 07/2016 si 09/2016 se iau doar partenerii cu RO

		* Citeste JV/JC
		Wait Window 'Se citesc date din Registrele TVA...' Nowait
		llSucces = This.citeste_date_jtva()

		If m.llSucces And Used('crsJtva')
			Wait Window 'Se proceseaza datele din Registrele TVA...' Nowait
			llSucces = This.configureaza_date_jtva()
		Else
			llSucces = .F.
		Endif

		If m.llSucces
			Wait Window 'Se pregatesc sectiunile 1 si 2...' Nowait
			llSucces =	This.pregateste_sectiunea_1_2()
		Endif

		If m.llSucces
			Wait Window 'Se pregatesc sectiunile 4 si 5...' Nowait
			llSucces =	This.pregateste_sectiunea_4_5()
		Endif

		If m.llSucces
			Wait Window 'Se pregateste sectiunea 6...' Nowait
			llSucces =	This.pregateste_sectiunea_6()
		Endif

		If m.llSucces
			Wait Window 'Se pregateste sectiunea 7...' Nowait
			llSucces =	This.pregateste_sectiunea_7()
		Endif

		If m.llSucces
			Wait Window 'Se pregateste sectiunea AMEF...' Nowait
			llSucces =	This.pregateste_sectiunea_amef()
			If loUnitate.lPerioadaDoarRO
				Delete From crsAMEF && nu se declara bonurile fiscale in perioada 07-09/2016
			Endif
		Endif

		If m.llSucces
			Wait Window 'Se pregateste sectiunea persoane fizice...' Nowait
			llSucces =	This.pregateste_sectiunea_facturi_pers_fizice_10000()
			If loUnitate.lPerioadaDoarRO
				Delete From crsPersFizice && nu se declara bonurile fiscale in perioada 07-09/2016
			Endif
		Endif

		Go Top In crsJTVA

		Select (m.lcSelect)
		Return m.llSucces
	Endproc

	* Citeste JV/JC
	Procedure citeste_date_jtva

		Private pnIdSetAnulat && set facturi anulate
		pnIdSetAnulat = Iif(Type('gnIdSetAnulat') = 'N', m.gnIdSetAnulat, 99996)

		Local lcConditieAct, lcConditieLuna, lcCursor, lcSql, lnAn, lnLuna1, lnLuna2, lnSucces, loUnitate, llPerioadaDoarRO
		loUnitate = This.oUnitate
		lnAn = loUnitate.an
		lnLuna1 = loUnitate.luna1
		lnLuna2 = loUnitate.luna2
		llPerioadaDoarRO = loUnitate.lPerioadaDoarRO

		lcConditieLuna = [ (AN  = ] + Alltrim(Str(m.lnAn)) + [ and (luna BETWEEN ] + Alltrim(Str(m.lnLuna1)) + [ and ] + Alltrim(Str(m.lnLuna2)) + [ ))]

		*** iau doar facturile din luna emiterii
		lcConditieAct = [ and (extract(year from dataact)*12+extract(month from dataact) = an*12+luna)]

		* Iau toate inregistrarile din Registrele de TVA, INCLUSIV FATURILE EMISE ANTERIOR PERIOADEI DE DECLARARE, pentru calcul sectiune 4 si 5 (TVA EXIGIBIL)
		Text To lcSql Textmerge Noshow
SELECT 'JV' AS TIPJ, 0 AS TIPPART, TIP_DOC as tipdoc, 'L ' as tip, dataact, dataireg, serie_act, nract, id_fdoc, fel_document, id_sucursala, sucursala,
   	 0 as ISCIF, 0 AS CIFVALID,
	 J.ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', '') AS COD_FISCALFRO, SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) AS ATRIBUT_FISCAL, DENUMIRE, TIP_PERSOANA, AFILIAT, CODTARA,
	 A.TARA, A.COD_TARA, A.JUDET, A.COD_JUDET, A.LOCALITATE, A.STRADA, A.BLOC, A.SCARA, A.NUMAR, A.APART,
	 RO24B + RO20B + RO19B + RO9B + RO5B + RO24NB + RO20NB + RO19NB + RO9NB + RO5NB AS BAZA,
	 RO24T + RO20T + RO19T + RO9T + RO5T + RO24NT + RO20NT + RO19NT + RO9NT + RO5NT AS TVA,
	 ROTI AS TIBAZA,
	 0 AS TITVA,
	 RO24B + RO24NB as BAZA24, RO20B + RO20NB AS BAZA20, RO19B + RO19NB AS BAZA19, RO9B + RO9NB AS BAZA09, RO5B + RO5NB AS BAZA05, 0.00 AS BAZA00,
	 RO24T + RO24NT as TVA24, RO20T + RO20NT AS TVA20, RO19T + RO19NT AS TVA19, RO9T + RO9NT AS TVA09, RO5T + RO5NT AS TVA05,
	 0.00 AS TIBAZA24, ROTI AS TIBAZA20, 0.00 AS TIBAZA19, 0.00 AS TIBAZA09, 0.00 AS TIBAZA05,
	 0.00 as TITVA24, 0.00 AS TITVA20, 0.00 AS TITVA19, 0.00 AS TITVA09, 0.00 AS TITVA05,
	 TVA_INCASARE, 0 as FACT_TVA_INCASARE, 1 as NRFACT, decode(extract(year from dataireg)*12+extract(month from dataireg), an*12+luna, 1, 0) as EMISA_PERIOADA,
	 RO24T, RO20T, RO19T, RO9T AS RO09T, RO5T AS RO05T
	 FROM VJV2013 J
	 LEFT JOIN (SELECT ID_PART,TARA,PRESCURTARE AS COD_TARA,JUDET,COD_JUDET,LOCALITATE,STRADA,NUMAR,APART,SCARA,BLOC FROM VADRESE_PARTENERI WHERE PRINCIPALA = 1) A ON J.ID_PART = A.ID_PART 
	 WHERE (TOTCTVA = 0 OR RO24T <> 0 OR RO20T <> 0 OR RO19T <> 0 OR RO9T <> 0 OR RO5T <> 0 OR RO24NT <> 0 OR RO20NT <> 0 OR RO19NT <> 0 OR RO9NT <> 0 OR RO5NT <> 0 OR ROTI <> 0) AND 
	<<lcConditieLuna>>
UNION
SELECT 'JC' AS TIPJ, 0 AS TIPPART, TIP_DOC as tipdoc, 'A ' as tip, dataact, dataireg, serie_act, nract, id_fdoc, fel_document, id_sucursala, sucursala,
	 0 as ISCIF, 0 AS CIFVALID,
	 J.ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', '') AS COD_FISCALFRO, SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) AS ATRIBUT_FISCAL, DENUMIRE, TIP_PERSOANA, AFILIAT, CODTARA,
	 A.TARA, A.COD_TARA, A.JUDET, A.COD_JUDET, A.LOCALITATE, A.STRADA, A.BLOC, A.SCARA, A.NUMAR, A.APART,
	 RO24B + RO20B + RO19B + RO09B + RO05B + RO24NB + RO20NB + RO19NB + RO9NB + RO5NB + ROSCN AS BAZA,
	 RO24T + RO20T + RO19T + RO09T + RO05T + RO24NT + RO20NT + RO19NT + RO9NT + RO5NT AS TVA,
	 TI19B + TI09B + TI20B + TI24B + XX19TIB + XX9TIB as TIBAZA,
	 TI19T + TI09T + TI20T + TI24T + XX19TIT + XX9TIT as TITVA,
	 RO24B + RO24NB AS BAZA24, RO20B + RO20NB AS BAZA20, RO19B + RO19NB AS BAZA19, RO09B + RO9NB AS BAZA09, RO05B + RO5NB AS BAZA05, ROSCN AS BAZA00,
	 RO24T + RO24NT AS TVA24, RO20T + RO20NT  AS TVA20, RO19T + RO19NT AS TVA19, RO09T + RO9NT AS TVA09, RO05T + RO5NT AS TVA05,
	 TI24B AS TIBAZA24, TI20B AS TIBAZA20, TI19B + XX19TIB AS TIBAZA19, TI09B + XX9TIB AS TIBAZA09, 0.00 AS TIBAZA05,
	 TI24T as TITVA24, TI20T AS TITVA20, TI19T + XX19TIT AS TITVA19, TI09T + XX9TIT AS TITVA09, 0.00 AS TITVA05,
	 TVA_INCASARE, 0 as FACT_TVA_INCASARE, 1 as NRFACT, decode(extract(year from dataireg)*12+extract(month from dataireg), an*12+luna, 1, 0) as EMISA_PERIOADA,
	 RO24T, RO20T, RO19T, RO09T, RO05T
	 FROM VJC2013 J
	 LEFT JOIN (SELECT ID_PART,TARA,PRESCURTARE AS COD_TARA,JUDET,COD_JUDET,LOCALITATE,STRADA,NUMAR,APART,SCARA,BLOC FROM VADRESE_PARTENERI WHERE PRINCIPALA = 1) A ON J.ID_PART = A.ID_PART	 
	 WHERE (TOTCTVA = 0 OR RO24T <> 0 OR RO20T <> 0 OR RO19T <> 0 OR RO09T <> 0 OR RO05T <> 0 OR RO24NT <> 0 OR RO20NT <> 0 OR RO19NT <> 0 OR RO9NT <> 0 OR RO5NT <> 0 OR TI19T <> 0 OR TI09T <> 0 OR TI20T <> 0 OR TI24T <> 0 OR 
	 XX19TIB<>0 OR XX19TIT <> 0 OR XX9TIB<>0 OR XX9TIT <> 0 OR ROSCN <> 0) AND 
	<<lcConditieLuna>>
		Endtext



		goLog.Log(lcSql)

		lcCursor = [crsJTVATemp]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		If m.llSucces
			Select crsJTVA
			Append From Dbf('crsJTVATemp')
			Use In (Select('crsJTVATemp'))

			* sterg facturile de achizitie de la platitorii de TVA (RO) care au doar baza00 <> 0 (ROSCN). nu trebuie sa apara in declaratie. doar la tipul 2 (neinregistrati in scopuri TVA)
			* sunt permise facturile cu TVA 0 tip AS - achizitii regim special (servicii turism, second hand, obiecte arta care aplica TVA la marja de profit)
			Delete From crsJTVA Where tipj = 'JC' And atribut_fiscal = 'RO' And TVA24 = 0 And TVA20 = 0 And TVA19 = 0 And TVA09 = 0 And TVA05 = 0 And baza00 <> 0 And !Inlist(Alltrim(tipdoc), 'U', 'H')
			Update crsJTVA Set seq = Recno()

			* Bifa Persoane Afiliate
			Select crsJTVA
			Locate For Nvl(AFILIAT, 0) <> 0
			If Found()
				pounitate.PersoaneAfiliate = 1
			Endif
		Endif && llSucces

		* Facturi Anulate din Reg. Jurnal cu gnIdSetAnulate
		If m.llSucces
			Text To lcSql Textmerge Noshow
SELECT distinct a.serie_act as seria, a.nract as nr
  FROM act a left join nom_fdoc f on a.id_fdoc = f.id_fdoc
	WHERE a.sters = 0 and a.id_set = <<m.pnIdSetAnulat>> and f.tip_doc in ('F', 'M') and 
	<<lcConditieLuna>>
	<<lcConditieAct>>
    <<gcCondSucursala>>
			Endtext
 
			lcCursor = [crsFacturiAnulate]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		Endif && llSucces
		* Numar bonuri fiscale cu si fara CUI, B si C (AMEF)
		If m.llSucces

			* 07-09/2016 se declara doar operatiile cu persoane impozabile, deci nu bonuri AMEF
			If m.llPerioadaDoarRO
				Create Cursor crsBonuriAMEF (luna N(2), nr N(10))
				llSucces = .T.
			Else
				*BON=10*
				* se numara toate bonurile fiscale emise, indiferent ca au cod fiscal sau nu. Doar bonurile fiscale pentru care s-au emis facturi nu se iau in considerare!
				* dar acelea trebuie sa fie marcate ca FACTURA
				Text To lcSql Textmerge Noshow
SELECT extract(month FROM dataact) as luna, SUM(case when nr is null then 1 else to_number(nr) end) as nr FROM
(SELECT a.id_fact, a.cod, MAX(a.dataact) as dataact, MAX(replace(replace(upper(regexp_substr(a.explicatia,'\*BON=(\d)*\*',1,1,'i')),'*BON=',''),'*','')) as nr
	FROM act a
	WHERE a.sters = 0 and
	<<lcConditieLuna>>
	<<lcConditieAct>>
    and id_fact in (SELECT j.id_fact
    					from jv2007 j
    					where <<lcConditieLuna>> <<lcConditieAct>> and
    						id_fdoc in (select id_fdoc
                                     from NOM_FDOC t
                                    where sters = 0
                                      and tip_doc in ('B','C')))
   and id_fdoc in (select id_fdoc
                                     from NOM_FDOC t
                                    where sters = 0
                                      and tip_doc in ('B','C'))
    group by a.id_fact, a.cod)
    group by extract(month from dataact)
				Endtext

				Wait Window 'Calculare numar bonuri AMEF...' Nowait
				lcCursor = [crsBonuriAMEF]
				llSucces = goExecutor.oExecuta(lcSql, lcCursor)
			Endif &&  m.llPerioadaDoarRO
		Endif && llSucces

		* Numar bilete E(xceptate) AMEF
		If m.llSucces
			* 07-09/2016 se declara doar operatiile cu persoane impozabile, deci nu bonuri AMEF
			If m.llPerioadaDoarRO
				Create Cursor crsBonuriEAMEF(luna N(2), nr N(10))
				llSucces = .T.
			Else
				Text To lcSql Textmerge Noshow
SELECT extract(month FROM dataact) as luna, SUM(case when nr is null then 1 else to_number(nr) end) as nr FROM
(SELECT a.id_fact, a.cod, MAX(a.dataact) as dataact, MAX(replace(replace(upper(regexp_substr(a.explicatia,'\*BON=(\d)*\*',1,1,'i')),'*BON=',''),'*','')) as nr
	FROM act a
	WHERE a.sters = 0 and
	<<lcConditieLuna>>
	<<lcConditieAct>>
    and id_fact in (SELECT j.id_fact
    					from jv2007 j
    					where <<lcConditieLuna>> <<lcConditieAct>> and
    						id_fdoc in (select id_fdoc
                                     from NOM_FDOC t
                                    where sters = 0
                                      and tip_doc = 'E'))
	and	id_fdoc in (select id_fdoc
                                     from NOM_FDOC t
                                    where sters = 0
                                      and tip_doc = 'E')
    group by a.id_fact, a.cod)
    group by extract(month from dataact)
				Endtext

				Wait Window 'Calculare numar bonuri exceptate AMEF...' Nowait
				lcCursor = [crsBonuriEAMEF]
				llSucces = goExecutor.oExecuta(lcSql, lcCursor)
			Endif &&  m.llPerioadaDoarRO
		Endif && llSucces

		Return m.llSucces
	Endproc && citeste_date_jtva

	* Configureaza tipurile de documente (facturi, facturi simplificate, bonuri fiscale etc.)
	Procedure configureaza_date_jtva

		Local loVerificare As "verificarecod"
		Local loRec


		Update crsJTVA Set atribut_fiscal = '' Where Isdigit(atribut_fiscal) Or Empty(Nvl(cod_fiscal, ''))
		Update crsJTVA Set codtara = 'RO' WHERE EMPTY(NVL(codtara,''))
SET STEP ON 
		* Completez Tip partener: 1/2/3/4
		Update crsJTVA Set tippart = 1 Where tippart = 0 And atribut_fiscal = 'RO' && persoane impozabile inregistrate in scopuri de TVA in Romania
		Update crsJTVA Set tippart = 2 Where tippart = 0 And (Empty(atribut_fiscal) OR  Nvl(TIP_PERSOANA,0) <> 1) && persoane neinregistrate in scopuri de TVA (fara atribut fiscal sau persoane fizice)
		Update crsJTVA Set tippart = 3 Where tippart = 0 And ;
			(INLIST(atribut_fiscal,'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES', 'FI', 'FR') OR INLIST(atribut_fiscal, 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'SE', 'SI', 'SK') OR ;
			INLIST(codtara, 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'GR', 'ES', 'FI', 'FR') OR INLIST(codtara, 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'SE', 'SI', 'SK')) && persoane impozabile nestabilite in RO, in UE
		Update crsJTVA Set tippart = 4 Where tippart = 0 And Nvl(TIP_PERSOANA,0) = 1 AND codtara <> 'RO'&& persoane impozabile nestabilite in RO, in afara UE si tip persoana = persoana juridica

		* Completez tip document: FACTURA/BON FISCAL FARA FACTURA/BON FISCAL CU FACTURA/FACTURA SIMPLIFICATA/AUTOFACTURA
		*!*			Update crsJTVA Set tipdoc = ''
		*!*			Update crsJTVA Set tipdoc = 'B' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_bonfiscal_fara_cui + ','
		*!*			Update crsJTVA Set tipdoc = 'C' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_bonfiscal_cu_cui + ','
		*!*			Update crsJTVA Set tipdoc = 'M' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_factura_simplificata + ','
		*!*			Update crsJTVA Set tipdoc = 'E' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_exceptate_amef + ','
		*!*			Update crsJTVA Set tipdoc = 'O' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_borderou_achizitii + ','
		*!*			Update crsJTVA Set tipdoc = 'Z' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_carnet_comercializare + ','
		*!*			Update crsJTVA Set tipdoc = 'H' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_achizitii_second_hand + ','
		*!*			Update crsJTVA Set tipdoc = 'U' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_servicii_turism + ','
		*!*			Update crsJTVA Set tipdoc = 'AF' Where Empty(tipdoc) And ',' + Alltrim(serie_act) + ',' $ ',' + This.oUnitate.serie_autofacturare + ','
		*!*			Update crsJTVA Set tipdoc = 'F' Where Empty(tipdoc) And ',' + Alltrim(Str(id_fdoc)) + ',' $ ',' + This.oUnitate.tipdoc_factura + ','
		*		Update crsJTVA Set tipdoc = 'F' Where Empty(tipdoc) && completez restul documentelor cu tip = F(actura)

		Insert Into crsFdocTip (id_fdoc, fel_document, tip_doc, bifa) ;
			Select Distinct id_fdoc, fel_document, tipdoc As tip_doc, Iif(Empty(Nvl(tipdoc, '')), 1, 0) As bifa ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1

		Go Top In crsFdocTip

		* Determin CUI/CNP si verific validitate
		Select Distinct cod_fiscal, .F. As iscif, .F. As cifvalid ;
			From crsJTVA ;
			Where !Empty(Nvl(cod_fiscal, '')) ;
			Into Cursor cCUITemp Readwrite

		loVerificare = Createobject("verificarecod") && validare.prg
		Update cCUITemp Set iscif = loVerificare.iscif(cCUITemp.cod_fiscal)
		Update cCUITemp Set cifvalid = loVerificare.Validare(Iif(cCUITemp.iscif, 'CIF', 'CNP'), cCUITemp.cod_fiscal)
		Select cCUITemp
		Scan
			Update crsJTVA Set iscif = Iif(cCUITemp.iscif, 1, 0), cifvalid = Iif(cCUITemp.cifvalid, 1, 0) Where cod_fiscal = cCUITemp.cod_fiscal
		Endscan
		Update crsJTVA Set iscif = 0, cifvalid = 0 Where Empty(Nvl(cod_fiscal, ''))

		Use In (Select('cCUITemp'))

		*!*	L - livrari
		*!*	V - livrari cu taxare inversa
		*!*	LS - livrari regim special

		*!*	A - achizitii
		*!*	C - achizitii cu taxare inversa
		*!*	AS - achizitii regim special
		*!*	N - achizitii de bunuri/servicii pt care au fost primite facturi/borderouri de achizitii/ file din carnete de comercializare de la persoane neinregistrate
		*!*	AI - achizitii de la persoane impozabile care aplica TVA la incasare

		* V/C - taxare inversa
		Select * From crsJTVA Where .F. Into Cursor crsJTVATemp Readwrite

		Select crsJTVA
		Scan For EMISA_PERIOADA = 1 And titva <> 0 Or tibaza <> 0 And Inlist(TIP, 'L', 'A')
			If TVA = 0 And baza = 0
				* daca are doar taxare inversa, marchez factura tip V/C
				Replace TIP With Iif(TIP = 'L', 'V', 'C'), ;
					baza With tibaza, TVA With titva, baza24 With tibaza24, baza20 With tibaza20, baza19 With tibaza19, baza09 With tibaza09, baza05 With tibaza05, ;
					TVA With titva, TVA With titva, TVA24 With titva24, TVA20 With titva20, TVA19 With titva19, TVA09 With titva09, TVA05 With titva05 In crsJTVA

				If TIP = 'V' && livrare taxare inversa nu are cota de TVA
					Replace baza00 With tibaza, baza20 With 0, tibaza20 With 0 In crsJTVA
				Endif
			Else
				* daca are taxare inversa si tva normal, marchez factura tip V/C si adaug o linie noua cu tva normal
				Select crsJTVA
				Scatter Name loRec
				Replace TIP With Iif(TIP = 'L', 'V', 'C'), ;
					baza With tibaza, TVA With titva, baza24 With tibaza24, baza20 With tibaza20, baza19 With tibaza19, baza09 With tibaza09, baza05 With tibaza05, ;
					TVA24 With titva24, TVA20 With titva20, TVA19 With titva19, TVA09 With titva09, TVA05 With titva05, ;
					ro24t With 0, ro20t With 0, ro19t With 0, RO09T With 0, ro05t With 0 In crsJTVA

				With loRec
					.NRFACT = 0 && am deja nrfact = 1 pe linia cu taxare inversa, sa nu duplic numarul de facturi
					.tibaza = 0
					.titva = 0
					.tibaza24 = 0
					.tibaza20 = 0
					.tibaza19 = 0
					.tibaza09 = 0
					.tibaza05 = 0
					.titva24 = 0
					.titva20 = 0
					.titva19 = 0
					.titva09 = 0
					.titva05 = 0
				Endwith
				Insert Into crsJTVATemp From Name loRec
				Select crsJTVA
			Endif
		Endscan
		Select crsJTVA
		Append From Dbf('crsJTVATemp')
		Use In (Select('crsJTVATemp'))

		* LS - livrari regim special (servicii turism, second hand, obiecte arta care aplica TVA la marja de profit)
		Update crsJTVA Set TIP = 'LS' Where TIP = 'L' And Inlist(Alltrim(tipdoc), 'U', 'H')

		* AS - achizitii regim special (servicii turism, second hand, obiecte arta care aplica TVA la marja de profit)
		Update crsJTVA Set TIP = 'AS' Where TIP = 'A' And Inlist(Alltrim(tipdoc), 'U', 'H')

		* N - achizitii de bunuri/servicii pt care au fost primite facturi/borderouri de achizitii/ file din carnete de comercializare de la persoane neinregistrate
		* Achizitii de la persoane fizice sau persoane juridice neplatitoare de TVA
		Update crsJTVA Set TIP = 'N' Where TIP = 'A' And (iscif = 0 Or (iscif = 1 And !'RO' $ cod_fiscal))

		* Facturi de achizitie provenite de la persoane care aplica sistemul TVA la Incasare, pentru sectiunea I5.2
		Update crsJTVA Set FACT_TVA_INCASARE = 1 Where tipj = 'JC' And iscif = 1 And 'RO' $ (Upper(cod_fiscal)) And VERIFICA_RTVAI_DATA(crsJTVA.cod_fiscal, crsJTVA.dataact)

		* AI - achizitii de la persoane impozabile care aplica TVA la incasare
		Update crsJTVA Set TIP = 'AI' Where TIP = 'A' And FACT_TVA_INCASARE = 1

		* Documentele fara tipdoc asociat nu se iau in considerare la generarea D394
		Update crsJTVA Set TIP = '' Where Empty(Nvl(tipdoc, ''))

		Return .T.

	Endproc && configureaza_date_jtva

	*===========================================
	* Creeaza cursoare pentru sectiunile declaratiei din crsJTVA
	Procedure pregateste_sectiunea_1_2
		Private pnPlaje24
		Local lnAn, lnLuna1, lnLuna2, llSucces
		Local lcSeria, lcSql, lnNr1, lnNr2, loUnitate
		pnPlaje24 = 0

		loUnitate = This.oUnitate
		lnAn = Int(loUnitate.an)
		lnLuna1 = Int(loUnitate.luna1)
		lnLuna2 = Int(loUnitate.luna2)


		* I. ALTE INFORMATII:
		* I 1.1 Livrari de bunuri/prestari de servicii pentru care s-au emis facturi simplificate care au inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.1' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza20) As baza, Sum(TVA20) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.1' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And !Empty(Nvl(cod_fiscal, ''))

		* I 1.2 Livrari de bunuri/prestari de servicii pentru care s-au emis facturi simplificate fara a avea inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.2' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza20) As baza, Sum(TVA20) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, '')) ;
			Union ;
			Select '1.2' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'M' And Empty(Nvl(cod_fiscal, ''))


		* I 1.3 Achizitii de bunuri si servicii pentru care s-au primit facturi simplificate de la persoane impozabile care aplica sistemul normal de TVA si care au inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.3' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza20) As baza, Sum(TVA20) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 ;
			Union ;
			Select '1.3' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0

		* I 1.4 Achizitii de bunuri si servicii pentru care s-au primit facturi simplificate de la persoane impozabile care aplica sistemul de TVA la incasare si care au inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.4' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza20) As baza, Sum(TVA20) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 ;
			Union ;
			Select '1.4' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'M' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1

		* I 1.5 Achizitii de bunuri si servicii pentru care s-au primit bonuri fiscale care indeplinesc conditiile unei facturi simplificate si care au inscris codul de inregistrare in scopuri de TVA al beneficiarului
		Insert Into crsSectiuneaI (punct, baza, TVA, proctva) ;
			Select '1.5' As punct, Sum(baza24) As baza, Sum(TVA24) As TVA, 24 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza20) As baza, Sum(TVA20) As TVA, 20 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza19) As baza, Sum(TVA19) As TVA, 19 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza09) As baza, Sum(TVA09) As TVA, 9 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C' ;
			Union ;
			Select '1.5' As punct, Sum(baza05) As baza, Sum(TVA05) As TVA, 5 As proctva ;
			From crsJTVA ;
			Where EMISA_PERIOADA = 1 And tipj = 'JC' And tipdoc = 'C'

		Delete From crsSectiuneaI Where Nvl(baza, 0) = 0 And Nvl(TVA, 0) = 0

		* 2.1 Plaja de facturi alocate in perioada de facturare
		pnPlaje24 = 0 && plaja de facturi special creata pentru declararea in D394. Nu se foloseste la generarea de numere de facturi
		lnSucces = goExecutor.oSelect2Value("SELECT COUNT(*) as nr FROM plaje_numere WHERE id_tipdoc = 24 and sters = 0", @pnPlaje24)
		If m.lnSucces = CT_INSUCCES
			amessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
			pnPlaje24 = 0
		Endif
		lnIdTipDocFact = Iif(m.pnPlaje24 > 0, 24, 5)

		Text To lcSql Textmerge Noshow
select s.serie as seria,
	s.prefix || SUBSTR(decode(nvl(s.an, 0), 1,
	              decode(NVL(s.an2caractere,0), 1, 
	              	'<<SUBSTR(ALLTRIM(STR(m.lnAn)),3,2)>>', '<<m.lnAn>>') || decode(s.luna, 1, lpad('<<m.lnLuna1>>', 2, '0'), 
	              	''),
	              '') || lpad(p.pl_inf, nvl(s.lungime, 14), '0'), 1, 30) as nr1,
	s.prefix || SUBSTR(decode(nvl(s.an, 0), 1,
	              decode(NVL(s.an2caractere,0), 1, 	
	              '<<SUBSTR(ALLTRIM(STR(m.lnAn)),3,2)>>', '<<m.lnAn>>') || decode(s.luna, 1, lpad('<<m.lnLuna1>>', 2, '0'), ''),'')
	              || lpad(p.pl_sup, nvl(s.lungime, 14), '0'), 1, 30) as nr2
  from plaje_numere p
  join serii s
    on p.id_serie = s.id_serie
 where p.sters = 0
   and p.inactiv = 0
   and p.id_tipdoc = <<m.lnIdTipDocFact>>
   and ((<<lnAn*12+lnLuna1>> between extract(year from p.datai) * 12 + extract(month from p.datai) and extract(year from p.datas) * 12 + extract(month from p.datas)) or
   (<<lnAn*12+lnLuna2>> between extract(year from p.datai) * 12 + extract(month from p.datai) and extract(year from p.datas) * 12 + extract(month from p.datas)))
		Endtext

		llSucces = goExecutor.oExecuta(m.lcSql, [crsFacturiAlocateTemp])
		If m.llSucces
			Insert Into crsFacturiAlocate (seria, nr1, nr2) ;
				Select seria, Int(Val(nr1)) As nr1, Int(Val(nr2)) As nr2 ;
				From crsFacturiAlocateTemp
		Endif

		* Nu pot sa configurez plaje de numar fara serie. Conventional seria xxx este NULL
		Update crsFacturiAlocate Set seria = '' Where seria = 'XXX'
		
		* In facturi alocate nu intra seriile de facturi emise de beneficiari/terti
		IF !EMPTY(NVL(loUnitate.serie_facturi_beneficiari,''))
			DELETE FROM crsFacturiAlocate WHERE !EMPTY(seria) AND ',' + Alltrim(seria) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_beneficiari) + ','
		ENDIF 
		IF !EMPTY(NVL(loUnitate.serie_facturi_terti,''))
			DELETE FROM crsFacturiAlocate WHERE !EMPTY(seria) AND ',' + Alltrim(seria) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_terti) + ','
		ENDIF 

		Use In (Select('crsFacturiAlocateTemp'))

		* 2.2 Numarul total de facturi emise, in perioada de raportare, din plaja de facturi alocate:
		If m.llSucces
			* selectez seriile si numerele de facturi (sectiunea 2.1), in afara de facturi emise de beneficiari/terti in numele persoanei impozabile (sectiunile 2.3, 2.4)
			Select Distinct serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') ;
				Union ;
				Select Distinct seria, nr ;
				From crsFacturiAnulate ;
				Into Cursor crsNract READWRITE 
			
			IF !EMPTY(NVL(loUnitate.serie_facturi_beneficiari,''))
				DELETE FROM crsNrAct WHERE !EMPTY(seria) AND ',' + Alltrim(seria) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_beneficiari) + ','
			ENDIF 
			IF !EMPTY(NVL(loUnitate.serie_facturi_terti,''))
				DELETE FROM crsNrAct WHERE !EMPTY(seria) AND ',' + Alltrim(seria) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_terti) + ','
			ENDIF 	
			CALCULATE CNT() TO loUnitate.nr_facturi_emise IN crsNrAct
*!*				loUnitate.nr_facturi_emise = _Tally
			Select Distinct seria From crsNract Into Cursor crsSeriiAct

			Select crsSeriiAct
			Scan
				lcSeria = seria
				Select Cast(0 As N(20)) As rec, nr From crsNract Where Nvl(seria, 'XXX') = Nvl(m.lcSeria, 'XXX') Order By nr Into Cursor crsSeriaAct1 Readwrite
				Replace All rec With Recno() In crsSeriaAct1
				Select rec, nr From crsSeriaAct1 Into Cursor crsSeriaAct2

				Select c1.nr As nr1, c2.nr As nr2, c2.nr - Nvl(c1.nr, c2.nr) As Dif ;
					From crsSeriaAct1 c1 Left Join crsSeriaAct2 c2 On c1.rec = c2.rec - 1 ;
					Into Cursor crsSeriaActDif

				Use In (Select('crsSeriaAct1'))
				Use In (Select('crsSeriaAct2'))

				Select crsSeriaActDif
				Go Top
				lnNr1 = nr1
				lnNr2 = nr1
				Scan
					If Dif = 1
						lnNr2 = nr2
					Else
						Insert Into crsFacturiEmise (seria, nr1, nr2) Values (m.lcSeria, m.lnNr1, m.lnNr2)
						lnNr1 = nr2
						lnNr2 = nr2
					Endif
				Endscan
				If m.lnNr1 <> m.lnNr2
					Insert Into crsFacturiEmise (seria, nr1, nr2) Values (m.lcSeria, m.lnNr1, m.lnNr2)
				Endif
				Use In (Select('crsSeriaActDif'))

			Endscan && crsSeriiAct

			* Verificare facturi emise in plajele de facturi alocate!!!
			* Asociez facturile emise cu plajele de facturi alocate
			Select a.seria, a.nr, b.seria As aseria, b.nr1, b.nr2 ;
				From crsNract a Left Join crsFacturiAlocate b On Padr(Nvl(a.seria, ''), 20, ' ') = Padr(Nvl(b.seria, ''), 20, ' ') And Between(a.nr, b.nr1, b.nr2) ;
				Into Cursor cFacturiEmiseAlocateTemp

			* Selectez facturile care nu s-au incadrat in nici o plaja de facturi alocate 	
			Select seria, nr ;
				From cFacturiEmiseAlocateTemp ;
				Where Isnull(nr1) ;
				Order By seria, nr ;
				Into Cursor cFacturiEmiseAlocate

			Use In (Select('cFacturiEmiseAlocateTemp'))

			This.oUnitate.cTextFacturiEmiseAlocate = ""
			If _Tally > 0
				This.oUnitate.cTextFacturiEmiseAlocate = 'Facturi emise care nu sunt in plajele de facturi alocate:' + Chr(13) + Chr(10)
				Select cFacturiEmiseAlocate
				Scan
					This.oUnitate.cTextFacturiEmiseAlocate = This.oUnitate.cTextFacturiEmiseAlocate + Transform(seria) + ' ' + Transform(nr) + Chr(13) + Chr(10)
				Endscan
			Else
				This.oUnitate.cTextFacturiEmiseAlocate = 'Toate facturile emise sunt in plajele de facturi alocate.'
			Endif
			Use In (Select('cFacturiEmiseAlocate'))

			Use In (Select('crsSeriiAct'))
			Use In (Select('crsNrAct'))

			* 2.2 din care (Tip: stornate/anulate/autofacturare/emisa in numele furnizorilor)
			* Facturi stornate
			Insert Into crsFacturiEmiseTip (TIP, ntip, seria, nr) ;
				Select Distinct 'STORNAT' As TIP, 1 As ntip, serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And baza + TVA < 0

			* Facturi anulate
			Insert Into crsFacturiEmiseTip (TIP, ntip, seria, nr) ;
				Select Distinct 'ANULAT' As TIP, 2 As ntip, seria, nr ;
				From crsFacturiAnulate

			* Autofacturi
			Insert Into crsFacturiEmiseTip (TIP, ntip, seria, nr) ;
				Select Distinct 'AUTOFACTURA' As TIP, 3 As ntip, serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_autofacturare) + ','

			* Facturi emise in numele furnizorilor
			Insert Into crsFacturiEmiseTip (TIP, ntip, seria, nr) ;
				Select Distinct 'FURNIZOR' As TIP, 4 As ntip, serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JC' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_furnizori) + ','

			* 2.2 Autofacturare
			Insert Into crsAutoFacturi (seria, nr, baza24, TVA24, baza20, TVA20, baza19, TVA19, baza09, TVA09, baza05, TVA05) ;
				Select serie_act As seria, nract As nr, ;
				Sum(baza24) As baza24, Sum(TVA24) As TVA24, ;
				Sum(baza20) As baza20, Sum(TVA20) As TVA20, ;
				Sum(baza19) As baza19, Sum(TVA19) As TVA19, ;
				Sum(baza09) As baza09, Sum(TVA09) As TVA09, ;
				Sum(baza05) As baza05, Sum(TVA05) As TVA05 ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_autofacturare) + ',' ;
				Group By serie_act, nract


			* 2.3 Numarul total de facturi emise, in perioada de raportare, de beneficiari in numele persoanei impozabile:
			Insert Into crsFacturiTerti (punct, denumire, cod_fiscal, seria, nr1, nr2) ;
				Select '2.3' As punct, denumire, cod_fiscal, serie_act As seria, Min(nract) As nr1, Max(nract) As nr2 ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_beneficiari) + ',' ;
				Group By denumire, cod_fiscal, serie_act ;
				Union ;
				Select '2.4' As punct, denumire, cod_fiscal, serie_act As seria, Min(nract) As nr1, Max(nract) As nr2 ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_terti) + ',' ;
				Group By denumire, cod_fiscal, serie_act ;
				Order By punct, denumire, cod_fiscal, serie_act
				
			Select Distinct serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_beneficiari) + ',' ;
				Into Cursor crsNract

			loUnitate.nr_facturi_beneficiari = _Tally				
			USE IN (SELECT('crsNract'))

			Select Distinct serie_act As seria, nract As nr ;
				From crsJTVA ;
				Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(tipdoc, 'F', 'M') And ',' + Alltrim(serie_act) + ',' $ ',' + Alltrim(loUnitate.serie_facturi_terti) + ',' ;
				Into Cursor crsNract

			loUnitate.nr_facturi_terti = _Tally				
			USE IN (SELECT('crsNract'))
			
		Endif && m.llSucces


		Return m.llSucces
	Endproc && pregateste_sectiunea_1_2


	* TVA Exigibil
	Procedure pregateste_sectiunea_4_5
		If This.oUnitate.tva_incasare = 0
			* 4. Date aferente operatiunilor desfasurate de catre persoana impozabila care aplica sistemul normal de TVA
			* 4.1 TVA deductibila aferenta facturilor achitate in perioada de raportare indiferent de data in care acestea au fost primite de la
			* persoane impozabile care aplica sistemul de TVA la incasare, defalcata pe fiecare cota de TVA
			Insert Into crsSectiuneaI4 (punct, TVA24, TVA20, TVA19, TVA09, TVA05) ;
				Select '4.1' As punct, Sum(ro24t) As TVA24, Sum(ro20t) As TVA20, Sum(ro19t) As TVA19, Sum(RO09T) As TVA09, Sum(ro05t) As TVA05 ;
				From crsJTVA Where tipj = 'JC' And atribut_fiscal = 'RO' And tva_incasare = 1 And Inlist(tipdoc, 'F', 'M')
		Else
			* 5. Date aferente operatiunilor desfasurate de catre persoana impozabila care aplica sistemul de TVA la incasare
			* 5.1 TVA colectata aferenta facturilor incasate in perioada de raportare indiferent de data in care acestea au fost emise de catre
			* persoana impozabila care aplica sistemul de TVA la incasare, defalcata pe fiecare cota de TVA
			Insert Into crsSectiuneaI4 (punct, TVA24, TVA20, TVA19, TVA09, TVA05) ;
				Select '5.1' As punct, Sum(ro24t) As TVA24, Sum(ro20t) As TVA20, Sum(ro19t) As TVA19, Sum(RO09T) As TVA09, Sum(ro05t) As TVA05 ;
				From crsJTVA Where tipj = 'JV' And tva_incasare = 1 And Inlist(tipdoc, 'F', 'M')

			* 5.2 TVA deductibila aferenta facturilor achitate in perioada de  raportare indiferent de data in care acestea au fost primite de la
			* persoane impozabile care aplica sistemul normal de TVA, defalcata pe fiecare cota de TVA
			Insert Into crsSectiuneaI4 (punct, TVA24, TVA20, TVA19, TVA09, TVA05) ;
				Select '5.2' As punct, Sum(ro24t) As TVA24, Sum(ro20t) As TVA20, Sum(ro19t) As TVA19, Sum(RO09T) As TVA09, Sum(ro05t) As TVA05 ;
				From crsJTVA Where tipj = 'JC' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 0 And Inlist(tipdoc, 'F', 'M')

			* 5.3 TVA deductibila aferenta facturilor achitate in perioada de raportare indiferent de data in care acestea au fost primite de la
			* persoane impozabile care aplica sistemul de TVA la incasare, defalcata pe fiecare cota de TVA
			Insert Into crsSectiuneaI4 (punct, TVA24, TVA20, TVA19, TVA09, TVA05) ;
				Select '5.3' As punct, Sum(ro24t) As TVA24, Sum(ro20t) As TVA20, Sum(ro19t) As TVA19, Sum(RO09T) As TVA09, Sum(ro05t) As TVA05 ;
				From crsJTVA Where tipj = 'JC' And atribut_fiscal = 'RO' And FACT_TVA_INCASARE = 1 And Inlist(tipdoc, 'F', 'M')
		Endif

	Endproc && pregateste_sectiunea_4_5


	* 6. TVA colectata aferenta marjei de profit din perioada de raportare indiferent de data la care au fost efectuate operatiunile de catre
	* persoanele impozabile care aplica regimul special pt agentiile de turism, pt bunurile second-hand, opere de arta, obiecte de colectie si antichitati
	Procedure pregateste_sectiunea_6
		Return .T.
	Endproc && pregateste_sectiunea_6


	* 7. In situatia in care ati desfasurat, in perioada de raportare, activitati dintre cele inscrise in lista veti selecta activitatea corespunzatoare
	* si veti inscrie valoarea livrarilor/prestarilor, precum si TVA aferenta
	Procedure pregateste_sectiunea_7
		Local lcCodCaen, lcCodCaen1, lcCodCaen2, lcCodCaenTemp, lcListaCAEN, llSucces
		lcCodCaen1 = Alltrim(Nvl(gofirma.caen, ''))
		lcCodCaen2 = Alltrim(Nvl(gofirma.caen394, ''))
		lcCodCaen = Iif(!Empty(m.lcCodCaen2), m.lcCodCaen2, m.lcCodCaen1)

		lcCodCaenTemp = ',' + m.lcCodCaen + ','
		lcListaCAEN = ',1071,4520,4730,47761,47762,4932,55101,55102,56103,5630,812,9313,9602,9603,'
		llSucces = .T.
		If !m.lcCodCaenTemp $ m.lcListaCAEN
			Return m.llSucces
		Endif

		ccsectii() && vnom_sectii
		Select id_sectie, sectie, caen From vnom_sectii Where ',' + Alltrim(Nvl(caen, '')) + ',' $ m.lcListaCAEN Into Cursor cSectii394 Readwrite
		If _Tally = 0
			Select cSectii394
			Append Blank
			Replace caen With m.lcCodCaen
		Endif

		Use In (Select('vnom_sectii'))
		* Insert Into crsSectiuneaI7 (caen, TIP)  Values (m.lcCodCaen, 'LIVRARI')
		* Insert Into crsSectiuneaI7 (caen, TIP)  Values (m.lcCodCaen, 'PRESTARI')
		Use In (Select('cSectii394'))

		Return .T.

	Endproc && pregateste_sectiunea_7

	Procedure pregateste_sectiunea_amef
		Insert Into crsAMEF (TIP, luna, nramef, nrbonuri, incasari, baza05, TVA05, baza09, TVA09, baza19, TVA19, baza20, TVA20, baza24, TVA24) ;
			Select 1 As TIP, Month(dataact) As luna, Cast(pounitate.nramef As N(10)) As nramef, Cast(Nvl(b.nr, 0) As N(10)) As nrbonuri, ;
			Round(Sum(baza + TVA), 0) As incasari, Round(Sum(baza05), 0) As baza05, Round(Sum(TVA05), 0) As TVA05, Round(Sum(baza09), 0) As baza09, Round(Sum(TVA09), 0) As TVA09, ;
			Round(Sum(baza19), 0) As baza19, Round(Sum(TVA19), 0) As TVA19, Round(Sum(baza20), 0) As baza20, Round(Sum(TVA20), 0) As TVA20, Round(Sum(baza24), 0) As baza24, Round(Sum(TVA24), 0) As TVA24 ;
			From crsJTVA j Left Join crsBonuriAMEF b On Month(j.dataact) = b.luna ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(Alltrim(tipdoc), 'B', 'C') ;
			Group By 1, 2, 3, 4 ;
			Union ;
			Select 2 As TIP, Month(dataact) As luna, Cast(0 As N(10)) As nramef, Cast(Nvl(b.nr, 0) As N(10)) As nrbonuri, ;
			Round(Sum(baza + TVA), 0) As incasari, Round(Sum(baza05), 0) As baza05, Round(Sum(TVA05), 0) As TVA05, Round(Sum(baza09), 0) As baza09, Round(Sum(TVA09), 0) As TVA09, ;
			Round(Sum(baza19), 0) As baza19, Round(Sum(TVA19), 0) As TVA19, Round(Sum(baza20), 0) As baza20, Round(Sum(TVA20), 0) As TVA20, Round(Sum(baza24), 0) As baza24, Round(Sum(TVA24), 0) As TVA24 ;
			From crsJTVA j Left Join crsBonuriEAMEF b On Month(j.dataact) = b.luna ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And tipdoc = 'E' ;
			Group By 1, 2, 3, 4 ;
			Order By 1, 2

		Return .T.
	Endproc && pregateste_sectiunea_amef

	Procedure pregateste_sectiunea_facturi_pers_fizice_10000
		Local llPerioada122016
		llPerioada122016 = Between(m.gnAn * 12 + m.gnLuna, 2016 * 12 + 7, 2016 * 12 + 12) && intre 07/2016 si 12/2016 nu se declara individual facturile < 10.000 lei catre persoanele fizice

		Insert Into crsPersFizice (TIP, NRFACT, baza, baza05, TVA05, baza09, TVA09, baza19, TVA19, baza20, TVA20, baza24, TVA24) ;
			Select TIP, Sum(NRFACT) As NRFACT, ;
			Sum(baza) As baza, Sum(baza05) As baza05, Sum(TVA05) As TVA05, Sum(baza09) As baza09, Sum(TVA09) As TVA09, ;
			Sum(baza19) As baza19, Sum(TVA19) As TVA19, Sum(baza20) As baza20, Sum(TVA20) As TVA20, Sum(baza24) As baza24, Sum(TVA24) As TVA24 ;
			From crsJTVA j ;
			Where EMISA_PERIOADA = 1 And tipj = 'JV' And Inlist(Alltrim(TIP), 'L', 'LS') And Inlist(tipdoc, 'F', 'M', 'U', 'H') And tippart = 2 And Nvl(TIP_PERSOANA, 0) = 2 And (Abs(baza + TVA) <= 10000) And m.llPerioada122016;
			Group By 1 ;
			Order By 1

		Return .T.
	Endproc && pregateste_sectiunea_facturi_pers_fizice_10000

	*===========================================
	Procedure do_genereaza_xdp
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou


		Local lcAdresa, lcAn, lcCodFiscalF, lcDen, lcLuna, lcSelect, lcTag, lcXDP, llPerioadaDoarRO, lnSeq, llExportSectiuneaI, lnCotaTVAStandard
		Local lnTipPart, llTvaNedeductibil
		lcSelect = Select()
		lcCodFiscalF = Alltrim(gofirma.codfiscalfro)
		lcLuna = Padl(Alltrim(Str(gnLuna)), 2, [0])
		lcAn = Alltrim(Str(gnAn))
		lnCotaTVAStandard = Iif(m.gnAn = 2016, 20, 19)
		llTvaNedeductibil = (This.oUnitate.tvanedeductibil = 1)

		llPerioadaDoarRO = Between(m.gnAn * 12 + m.gnLuna, 2016 * 12 + 7, 2016 * 12 + 9) && intre 07/2016 si 09/2016 se iau doar partenerii cu RO
		llPerioada122016 = Between(m.gnAn * 12 + m.gnLuna, 2016 * 12 + 7, 2016 * 12 + 12) && intre 07/2016 si 12/2016 nu se declara individual facturile < 10.000 lei catre persoanele fizice

		lcDen = XmlSpecialCharacters(RemoveCharacters(Alltrim(gofirma.antet), .T.))
		lcAdresa = XmlSpecialCharacters(RemoveCharacters(Alltrim(gofirma.adresa), .T.))

		This.TIP = Left(This.oUnitate.TIP, 1)
		llExportSectiuneaI = (This.oUnitate.ExportSectiuneaI = 1)
		lcXDP = ""


		Create Cursor cCote (cota N(3))
		Insert Into cCote (cota) Values (24)
		Insert Into cCote (cota) Values (20)
		Insert Into cCote (cota) Values (19)
		Insert Into cCote (cota) Values (9)
		Insert Into cCote (cota) Values (5)
		Insert Into cCote (cota) Values (0)
		*) Tip Cod de inregistrare fiscala
		*L/A/V/C/AI/N/LS/AS

		* Centralizator achizitii/livrari defalcat pe tip parteneri 1/2/3/4, parteneri, cote, tip livrare/achizitie, tip document
		* Ordonez facturile dupa cota de TVA, sa las nrfact = 1 pe linia cu cota cea mai mare
		* In perioada 07/2016-09/2016 se raporteaza doar facturile cu RO
		If m.llPerioadaDoarRO
			lcWhere = [j.atribut_fiscal = 'RO']
		Else
			lcWhere = [.T.]
		Endif

		Select Cast(0 As N(20)) As rn, j.seq As id_fact, j.tippart, c.cota, j.denumire As denp, j.cod_fiscalfro As cuip, j.TIP, j.TIP_PERSOANA, ;
			j.TARA, j.COD_TARA, j.JUDET, j.COD_JUDET, j.LOCALITATE, j.STRADA, j.Bloc, j.SCARA, j.NUMAR, j.APART,	;
			Iif(j.tipdoc = 'F', 1, Iif(j.tipdoc = 'O', 2, Iif(j.tipdoc = 'Z', 3, Iif(j.tipdoc = 'R', 4, 5)))) As tip_document, ;
			NRFACT, ;
			Iif(c.cota = 24, baza24, Iif(c.cota = 20, baza20, Iif(c.cota = 19, baza19, Iif(c.cota = 9, baza09, Iif(c.cota = 5, baza05, ;
					  Iif(c.cota = 0, Iif(m.llTvaNedeductibil Or Inlist(j.tipdoc, 'U', 'H') Or Inlist(j.TIP, 'V', 'C') Or tippart = 2, baza00, 0.00), baza20)))))) As baza, ;
			Iif(c.cota = 24, TVA24, Iif(c.cota = 20, TVA20, Iif(c.cota = 19, TVA19, Iif(c.cota = 9, TVA09, Iif(c.cota = 5, TVA05, Iif(c.cota = 0, 0, TVA20)))))) As TVA ;
			From crsJTVA j, cCote c ;
			Where j.EMISA_PERIOADA = 1 And Alltrim(j.TIP) In ('L', 'A', 'V', 'C', 'AI', 'N', 'LS', 'AS') And Inlist(tipdoc, 'F', 'U', 'H', 'O', 'Z', 'R') And &lcWhere ;
			Order By 1, 10 Desc ;
			Into Cursor cFacturiTemp Readwrite

		* STERG FACTURILE PERSOANE FIZICE < 10.0000 LEI
		If m.llPerioada122016
			Delete From cFacturiTemp Where Inlist(Alltrim(TIP), 'L', 'LS') And tippart = 2 And Nvl(TIP_PERSOANA, 0) = 2 And (Abs(baza + TVA) <= 10000)
		Endif

		Select cFacturiTemp
		Replace All rn With Recno()

		* Sterg toate liniile cu baza = 0 si tva = 0 in afara de prima linie pentru facturile cu valoare totala = 0
		* Facturile care au valoare 0
		Select id_fact, Min(rn) As rn, Sum(baza) As baza, Sum(TVA) As TVA ;
			From cFacturiTemp ;
			Group By id_fact ;
			Having Sum(baza) = 0 And Sum(TVA) = 0 ;
			Into Cursor cFacturi0

		Select F.*, f0.rn As rn0 ;
			From cFacturiTemp F Left Join cFacturi0 f0 On F.id_fact = f0.id_fact ;
			Into Cursor cFacturitemp2 Readwrite

		Use In (Select('cFacturiTemp'))
		Use In (Select('cFacturi0'))

		Delete From cFacturitemp2 Where baza = 0 And TVA = 0 And (rn <> rn0 Or Isnull(rn0))

		* Pun cota 20 pe facturile cu valoare 0, altfel apar cu 24% sau alta cota
		Update cFacturitemp2 Set cota = m.lnCotaTVAStandard Where baza = 0 And TVA = 0
		Update cFacturitemp2 Set cota = 0 Where Inlist(TIP, 'LS', 'AS', 'ASI', 'N', 'V')
		* DELETE FROM cFacturiTemp WHERE baza = 0 AND tva = 0 AND RECNO() NOT in (Select rn FROM cFacturi0) && EROARE

		* Actualizez nrfact cu 0 in afara de prima aparitie a fiecarei facturi. S-au multiplicat din cauza join-ului cu cotele de TVA
		Select id_fact, Min(rn) As rn ;
			From cFacturitemp2 ;
			Group By id_fact ;
			Into Cursor cFacturi2

		Update cFacturitemp2 Set NRFACT = 0 Where rn Not In (Select rn From cFacturi2)
		Use In (Select('cFacturi2'))

		Update cFacturitemp2 Set LOCALITATE = '' Where LOCALITATE = '<ALEGETI LOCALITATEA>'

		Select tippart, Cast(0 As i) As seq, cota, denp, cuip, TIP, tip_document, TIP_PERSOANA, ;
			TARA, COD_TARA, JUDET, COD_JUDET, LOCALITATE, STRADA, Bloc, SCARA, NUMAR, APART, ;
			Sum(NRFACT) As NRFACT, ;
			Round(Sum(baza), 0) As baza, ;
			Round(Sum(TVA), 0) As TVA ;
			From cFacturitemp2 ;
			Group By 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ;
			Order By tippart, denp, cota, TIP ;
			Into Cursor cFacturi Readwrite

		Use In (Select('cFacturiTemp2'))

		*!*			Select j.tippart, Cast(0 As i) As seq, c.cota, j.denumire As denp, j.cod_fiscalfro As cuip, j.TIP, ;
		*!*				IIF(j.tipdoc = 'F', 1, Iif(j.tipdoc = 'O', 2, Iif(j.tipdoc = 'Z', 3, 1))) As tip_document, ;
		*!*				SUM(NRFACT) As NRFACT, ;
		*!*				SUM(Iif(c.cota = 24, baza24, Iif(c.cota = 20, baza20, Iif(c.cota = 19, baza19, Iif(c.cota = 9, baza09, Iif(c.cota = 5, baza05, Iif(c.cota = 0, baza00, baza20))))))) As baza, ;
		*!*				SUM(Iif(c.cota = 24, TVA24, Iif(c.cota = 20, TVA20, Iif(c.cota = 19, TVA19, Iif(c.cota = 9, TVA09, Iif(c.cota = 5, TVA05, Iif(c.cota = 0, 0, TVA20))))))) As TVA ;
		*!*				FROM crsJTVA j, cCote c ;
		*!*				WHERE Alltrim(j.TIP) In ('L','A','V','C','AI','N','LS','AS') ;
		*!*				GROUP By 1,2,3,4,5,6,7 ;
		*!*				ORDER By j.tippart, j.denumire, c.cota, j.TIP ;
		*!*				INTO Cursor cFacturi Readwrite

		lnSeq = 1
		lnTipPart = 0
		Select cFacturi
		Scan
			If lnTipPart <> tippart
				lnSeq = 1
			Else
				lnSeq = lnSeq + 1
			Endif
			lnTipPart = cFacturi.tippart
			Replace seq With m.lnSeq In cFacturi
		Endscan

		Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="XFA2_4" APIVersion="3.0.11263.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>

<!-- textul de sus este textul de inceput iar jos urmeaza fisierul xml data -->
<form1>
<body1>
	<HEADER xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
	<sub_per>
		<an_r><<m.lcAn>></an_r><TIP_D394><<This.tip>></TIP_D394><luna_r><<m.lcLuna>></luna_r><sistem><<INT(this.oUnitate.tvaincasare)>></sistem><operatiuni><<INT(this.oUnitate.operatiuni)>></operatiuni>
		<prsAfiliat><<INT(this.oUnitate.PersoaneAfiliate)>></prsAfiliat>
	</sub_per>
	<sub_dateDeIdentificare>
		<cif><<m.lcCodFiscalF>></cif><den><<m.lcDen>></den><adresa><<m.lcAdresa>></adresa><telefon><<ALLTRIM(goFirma.telefon)>></telefon><fax><<ALLTRIM(goFirma.fax)>></fax>
		<mail><<ALLTRIM(LOWER(goFirma.email))>></mail><d_rec>0</d_rec><caen><<ALLTRIM(LOWER(goFirma.caen))>></caen>
	</sub_dateDeIdentificare>
</body1>
<body10><totalPlata_A/></body10>
		Endtext


		* Sectiunea Facturi
		Set Textmerge On To Memvar lcXDP Additive Noshow
\<!-- Facturi  -->
		lnTipPart = 0
		lnNrFacturi = 0
		Select cFacturi
		Scan
			* Exceptie : Pentru perioada 01.07.2016 - 31.12.2016 in cazul selectarii tip L pentru facturi emise catre persoane fizice,
			* cu valoare individuala/persoana mai mica sau egala cu 10.000 lei
			* Le-am sters la inceput
			*!*				If cFacturi.tippart = 2 AND cFacturi.tip_persoana = 2 and cFacturi.TIP = 'L' And ABS(cFacturi.baza + cFacturi.TVA) <= 10000 AND m.llPerioada122016 && persoane fizice
			*!*					Loop
			*!*				Endif
			lnNrFacturi = lnNrFacturi + 1

			If m.lnTipPart <> cFacturi.tippart
				If m.lnTipPart <> 0
\</tip1>
				Endif

				Do Case
					Case tippart = 1
\<!-- Persoane inregistrate in scopuri de TVA  -->
					Case tippart = 2
\<!-- Persoane neinregistrate in scopuri de TVA  -->
					Case tippart = 3
\<!-- Persoane intracomunitare  -->
					Case tippart = 4
\<!-- Persoane extracomunitare  -->
				Endcase

\<tip1>
\<tip_partener><<cFacturi.tippart>></tip_partener>
			Endif
\       <sub1>
\       	<sub2>
\       		<seq2><<cFacturi.seq>></seq2>
\       		<denP><<XmlSpecialCharacters(RemoveCharacters(Alltrim(cFacturi.denp), .T.))>></denP>
\        		<Tip><<ALLTRIM(cFacturi.tip)>></Tip>
\        		<cota><<cFacturi.cota>></cota>
\        		<cuiP><<ALLTRIM(cFacturi.cuip)>></cuiP>
\        		<nrfact><<cFacturi.nrfact>></nrfact>
\        		<baza><<cFacturi.baza>></baza>
\				<tva><<IIF(INLIST(ALLTRIM(cFacturi.TIP), 'N', 'V', 'AS', 'LS'), '', ALLTRIM(STR(cFacturi.tva)))>></tva>
			If cFacturi.tippart = 2 && persoane neinregistrate in scopuri TVA
				If ALLTRIM(cFacturi.TIP) = 'N' && persoane neinregistrate in scopuri TVA
\		<tip_document><<cFacturi.tip_document>></tip_document>
				ENDIF 
\		<adresa>
\			<tara><<ALLTRIM(NVL(cFacturi.COD_TARA, "RO"))+ '--' + PROPER(ALLTRIM(NVL(cFacturi.TARA, 'ROMANIA')))>></tara>
\			<judet><<ALLTRIM(NVL(cFacturi.cod_judet, ""))>></judet>
\			<localitate><<ALLTRIM(NVL(cFacturi.localitate, ""))>></localitate>
\			<strada><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(NVL(cFacturi.strada, "")), .T.))>></strada>
\			<nr><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(NVL(cFacturi.numar,"")), .T.))>></nr>
\			<bloc><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(NVL(cFacturi.bloc, "")), .T.))>></bloc>
\			<apart><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(NVL(cFacturi.apart,"")), .T.))>></apart>
\		</adresa>
			Endif
\      		</sub2>
			If cFacturi.tippart = 2 OR INLIST(ALLTRIM(cFacturi.tip), 'V', 'C') && persoane neinregistrate in scopuri TVA sau taxare inversa, completez produsele - nu am produse :(, doar nrfacturi, baza si tva
\		<!-- Produse  -->
\		<sub3>
\			<seq3>1</seq3>
\			<codPR></codPR>
\			<nrFacturi><<cFacturi.nrfact>></nrFacturi>
\			<baza><<cFacturi.baza>></baza>
\			<tva><<IIF(INLIST(cFacturi.TIP, 'N', 'V', 'AS', 'LS'), '', ALLTRIM(STR(cFacturi.tva)))>></tva>
\		</sub3>
			ENDIF && tippart = 2 OR INLIST(ALLTRIM(tip), 'V', 'C')
\	    </sub1>

			lnTipPart = cFacturi.tippart
		ENDSCAN && cFacturi
		IF m.lnNrFacturi > 0
\</tip1>
		ENDIF 
		* Sectiunea AMEF
\		<!-- Case marcat  -->
\		<tip2>

		Select crsAMEF
		Scan
\		<amef>
\		<tip><<tip>></tip>
\		<luna><<luna>></luna>
IF tip <> 2
\		<nrAMEF><<nramef>></nrAMEF>
\		<nrbonuri><<nrbonuri>></nrbonuri>
ENDIF 
\		<incasari><<incasari>></incasari>
\		<detaliu_amef><cota>5</cota><baza><<baza05>></baza><tva><<tva05>></tva></detaliu_amef>
\		<detaliu_amef><cota>9</cota><baza><<baza09>></baza><tva><<tva09>></tva></detaliu_amef>
\		<detaliu_amef><cota>19</cota><baza><<baza19>></baza><tva><<tva19>></tva></detaliu_amef>
\		<detaliu_amef><cota>20</cota><baza><<baza20>></baza><tva><<tva20>></tva></detaliu_amef>
\		</amef>
		Endscan && crsAMEF

\		<ianuarie>
		Select crsPersFizice
		Scan
			If Alltrim(TIP) == 'L'
\		<!-- Exceptie: Pentru Perioada 01.07.2016 - 31.12.2016 In cazul selectarii livrari catre PERS. fizice < 10000 lei L  -->
\		<nrfactL><<nrfact>></nrfactL>
\		<bazaL5><<baza05>></bazaL5><tvaL5><<tva05>></tvaL5>
\		<bazaL9><<baza09>></bazaL9><tvaL9><<tva09>></tvaL9>
\		<bazaL19><<baza19>></bazaL19><tvaL19><<tva19>></tvaL19>
\		<bazaL20><<baza20>></bazaL20><tvaL20><<tva20>></tvaL20>
\		<bazaL24><<baza24>></bazaL24><tvaL24><<tva24>></tvaL24>
			Else
\		<!-- Exceptie: Pentru Perioada 01.07.2016 - 31.12.2016 In cazul selectarii livrari catre PERS. fizice < 10000 lei LS  -->
\		<nrLS><<nrfact>></nrLS>
\		<bazaLS><<baza>></bazaLS>
			Endif
		Endscan
\		</ianuarie>
\		</tip2>

IF m.llExportSectiuneaI
		* Sectiunea I1 facturi simplificate si bonuri fiscale cu CUI
\<I1>
\<info1>
\<HeaderRow xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
		Select crsSectiuneaI
		Scan
			lcTag = Iif(punct = '1.1', 'L', Iif(punct = '1.2', 'LI', Iif(punct = '1.3', 'A', Iif(punct = '1.4', 'AI', Iif(punct = '1.5', 'AB', 'L'))))) + Alltrim(Str(proctva)) && L24
\<<'<' + m.lcTag + '>'>><baza><<baza>></baza><tva><<tva>></tva></<<m.lcTag + '>'>>
		Endscan
\</info1>
\</I1>

		* Sectiunea I2 Alte informatii
\		<i2>
\		<!-- 2.1 Plaja de facturi alocate In Perioada de raportare  -->
		Select crsFacturiAlocate
		Scan
\		<plaja1><serie_i><<ALLTRIM(seria)>></serie_i><nr_i><<nr1>></nr_i><serie_f><<ALLTRIM(seria)>></serie_f><nr_f><<nr2>></nr_f></plaja1>
		Endscan
\		<!-- 2.2 Numarul Total de facturi emise, In Perioada de raportare, din Plaja de facturi alocate:  -->
\		<facturi2>

\		<fact_emise><<This.oUnitate.nr_facturi_emise>></fact_emise>
		Select crsFacturiEmise
		Scan
\		<plaja><serie_i><<ALLTRIM(seria)>></serie_i><nr_i><<nr1>></nr_i><serie_f><<ALLTRIM(seria)>></serie_f><nr_f><<nr2>></nr_f></plaja>
		Endscan

\		<!-- 1 facturi stornate  -->
\		<!-- 2 facturi anulate  -->
\		<!-- 3 Facturi autofacturare  -->
\		<!-- 4 Facturi furnizor  -->
		Sele crsFacturiEmiseTip
		Scan
\		<detaliu_fact>
\		<tip_fact><<ntip>></tip_fact>
\		<serie><<ALLTRIM(seria)>></serie>
\		<nr><<nr>></nr>
			If ntip = 3 && autofacturare
				Select crsAutoFacturi
				Locate For Alltrim(crsAutoFacturi.seria) = Alltrim(crsFacturiEmiseTip.seria) And crsAutoFacturi.nr = crsFacturiEmiseTip.nr
				If Found()
\				<autofacturare>
\				<baza24><<baza24>></baza24><tva24><<tva24>></tva24>
\				<baza20><<baza20>></baza20><tva20><<tva20>></tva20>
\				<baza19><<baza19>></baza19><tva19><<tva19>></tva19>
\				<baza9><<baza09>></baza9><tva9><<tva09>></tva9>
\				<baza5><<baza05>></baza5><tva5><<tva05>></tva5>
\				</autofacturare>
				Endif && FOUND
			Endif
\		</detaliu_fact>
		Endscan && crsFacturiEmiseTip

\		</facturi2>

\		<!-- 2.3 Numarul total de facturi emise, in perioada de raportare, de beneficiari in numele persoanei impozabile:  -->
\		<facturi3>
\		<fact_emise><<This.oUnitate.nr_facturi_beneficiari>></fact_emise>

		Select crsFacturiTerti
		SCAN FOR punct = '2.3'
\		<plaja><serie_i><<ALLTRIM(seria)>></serie_i><nr_i><<nr1>></nr_i><serie_f><<ALLTRIM(seria)>></serie_f><nr_f><<nr2>></nr_f><den><<ALLTRIM(NVL(denumire,''))>></den><cui><<ALLTRIM(NVL(cod_fiscal, ''))>></cui></plaja>
		ENDSCAN
\		</facturi3>

\		<!-- 2.4 Numarul total de facturi emise, in perioada de raportare, de terti in numele persoanei impozabile:  -->
\		<facturi4>
\		<fact_emise><<This.oUnitate.nr_facturi_terti>></fact_emise>
		Select crsFacturiTerti
		SCAN FOR punct = '2.4'
\		<plaja><serie_i><<ALLTRIM(seria)>></serie_i><nr_i><<nr1>></nr_i><serie_f><<ALLTRIM(seria)>></serie_f><nr_f><<nr2>></nr_f><den><<ALLTRIM(NVL(denumire,''))>></den><cui><<ALLTRIM(NVL(cod_fiscal, ''))>></cui></plaja>
		ENDSCAN
\		</facturi4>

\		</i2>

		* Sectiunea 4
		Select crsSectiuneaI4
		Locate For punct = '4.1'
		If Found()
\		<date_deduc><deduc24><<tva24>></deduc24><deduc20><<tva20>></deduc20><deduc19><<tva19>></deduc19><deduc9><<tva09>></deduc9><deduc5><<tva05>></deduc5></date_deduc>
		Endif && FOUND

		* Sectiunea 5
\		<date_deduc_TVAi>
		Select crsSectiuneaI4
		Locate For punct = '5.1'
		If Found()
\		<date51><deduc24><<tva24>></deduc24><deduc20><<tva20>></deduc20><deduc19><<tva19>></deduc19><deduc9><<tva09>></deduc9><deduc5><<tva05>></deduc5></date51>
		Endif
		Locate For punct = '5.2'
		If Found()
\		<date52><deduc24><<tva24>></deduc24><deduc20><<tva20>></deduc20><deduc19><<tva19>></deduc19><deduc9><<tva09>></deduc9><deduc5><<tva05>></deduc5></date52>
		Endif
		Locate For punct = '5.3'
		If Found()
\		<date53><deduc24><<tva24>></deduc24><deduc20><<tva20>></deduc20><deduc19><<tva19>></deduc19><deduc9><<tva09>></deduc9><deduc5><<tva05>></deduc5></date53>
		Endif

		* Nu am adunat date despre sectiunea 6 si 7
		If .F.
\		<date_marja><incasari1/><costuri1/><marja1/><tva1/><tva2/><marja2/><costuri2/><incasari2/></date_marja>
\		<!-- 7. In situatia In care ati desfasurat, In Perioada de raportare, activitati dintre cele inscrise In lista veti selecta activitatea corespunzatoare
\		si veti inscrie valoarea livrarilor/prestarilor, precum si TVA aferenta -->
\		<lista>
\		<detaliu_lista>
\		<tip_operatiune>1</tip_operatiune>
\		<activitate>4520</activitate>
\		<livrari>1001</livrari>
\		<deduc5/>
\		<deduc9/>
\		<deduc20>201</deduc20>
\		<deduc24/>
\		</detaliu_lista>
\		<detaliu_lista>
\		<tip_operatiune>2</tip_operatiune>
\		<activitate>4520</activitate>
\		<livrari>1000</livrari>
\		<deduc5/>
\		<deduc9>90</deduc9>
\		<deduc20/>
\		<deduc24/>
\		</detaliu_lista>
\		</lista>
		Endif && .F.

\		</date_deduc_TVAi>

ENDIF && llExportSectiuneaI

		* Reprezentant fiscal
\<body11>
\	<sub_dateDeIdentificare>
\		<cifR><<ALLTRIM(this.oUnitate.CUIRepr)>></cifR>
\		<denR><<ALLTRIM(this.oUnitate.DenRepr)>></denR>
\		<adresaR><<ALLTRIM(this.oUnitate.AdrRepr)>></adresaR>
\		<telefonR><<ALLTRIM(this.oUnitate.TelRepr)>></telefonR>
\		<faxR><<ALLTRIM(this.oUnitate.FaxRepr)>></faxR>
\		<mailR><<ALLTRIM(this.oUnitate.EmailRepr)>></mailR>
\	</sub_dateDeIdentificare>
\</body11>
\<intocmit>
\<tip_reprez><<ALLTRIM(This.oUnitate.TipRepr)>></tip_reprez>
\<reprezentant><<ALLTRIM(this.oUnitate.DenRepr)>></reprezentant>
\<den_intocmit><<ALLTRIM(this.oUnitate.DenIntocmit)>></den_intocmit>
\<functie><<ALLTRIM(this.oUnitate.FunctieIntocmit)>></functie>
\<calitate_alta><<ALLTRIM(this.oUnitate.Calitate2Intocmit)>></calitate_alta>
\<calitate><<ALLTRIM(this.oUnitate.CalitateIntocmit)>></calitate>
\<cui_intocmit><<ALLTRIM(this.oUnitate.CUIIntocmit)>></cui_intocmit>
\<tip_intocmit><<IIF(this.oUnitate.PJIntocmit = 2, 0, 1)>></tip_intocmit>
\<schim_opt>0</schim_opt>
\<optiune><<INT(this.oUnitate.optiuneANAF)>></optiune>
\</intocmit>

\		</form1>

\		<!-- aici se Termina XML-Data iar jos urmeaza textul de sfarsit care face referire la documentul PDF Prin href="D394_XML_2016.PDF"  -->

\</xfa:data>
\</xfa:datasets>
\<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
\</xdp:xdp>
		Set Textmerge To

		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				open_default_app("http://static.anaf.ro/static/10/Anaf/Declaratii_R/394.html")
				amessagebox('Nu exista fisierul ' + m.lcFisierPDFOriginal + CRLF + ;
					  'Copiati pdf-ul inteligent D394 de pe website-ul ANAF in directorul ' + Justpath(m.lcFisierPDFOriginal) + CRLF + ;
					  'cu numele ' + Justfname(m.lcFisierPDFOriginal) + '.', 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)

	Endproc && do_genereaza_xdp

	*==========================================
	Procedure UpdatePDF
		Lparameters tcPDFFile
		*** tcPDFFile: calea completa a fisierului pdf D:\CONTAFIN\COMUNCONTAFIN\d390.pdf

		Local lcFile, lcPath, llOpen
		lcFile = Justfname(m.tcPDFFile)
		lcPath = Addbs(Justpath(m.tcPDFFile))
		llOpen = .F.

		Return DownloadSupportFile(m.lcFile, m.lcPath, m.llOpen) && proceduri_comune.prg

	Endproc && UpdatePDF

	Procedure do_executa

		*** EXPORT D394
		Local lcAppPath, lcCaleFisier, lcCaleFisierPDF, lcComunPath, lcFileName, lcFileNamePDF
		Local lcFisierPDFNou, lcFisierPDFOriginal, lcFisierTXT, lcFisierXDP, lcFisierXLS, lcMesaj
		Local lcNumeFisier, lcRun, lcTempPath, llSucces, lnPos, loEx, lcCaenRevizie, lnCaenRevizie 
		lcAppPath = Addbs(gcAppPath)
		lcComunPath = lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If lnPos > 0
			lcComunPath = Left(m.lcAppPath, lnPos)
		Endif
		lcComunPath = lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		*** Creez structura director export (caletemp\D394\aaaa_ll\firma\
		llSucces = .F.
		Try
			lcTempPath = m.gcBasePath
			lcCaleFisier = m.lcTempPath + "D394\" && c:\temp\D394\

			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Alltrim(Str(gnAn)) + "_" + Padl(Alltrim(Str(gnLuna)), 2, "0") + "\"&& c:\temp\D394\2011_01\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Strtran(gcFirma, [ ], [_]) + "\" && c:\temp\D390\2011_01\firma\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisierPDF = m.lcCaleFisier  && c:\temp\D394\2011_01\firma\
			If !Directory(m.lcCaleFisierPDF)
				Md (m.lcCaleFisierPDF)
			Endif
			This.CaleFisierPDF = m.lcCaleFisierPDF

			llSucces = .T.
		Catch To loEx
			lcMesaj = "Creare director " + lcCaleFisier + " " + loEx.Message

			amessagebox(lcMesaj, 0 + 48)
		Endtry
		If !m.llSucces
			Return
		Endif

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', Strtran(Alltrim(Nvl(gofirma.cod_fiscal, '')), 'RO', ''))
		Endif

		lcCaenRevizie = NVL(goFirma.caen_revizie, '2')
		lnCaenRevizie = INT(VAL(m.lcCaenRevizie))

		lcNumeFisier = "d394_" + gofirma.codfiscalfro + "_" + Alltrim(Str(gnAn)) + "_" + Padl(gnLuna, 2, "0")
		Do Case
			Case m.gnAn * 12 + m.gnLuna >= 2025 * 12 + 1 AND m.lnCaenRevizie >= 3
				* Cod CAEN rev. 3
				lcFisierPDFOriginal = lcComunPath + "d394_202501_v3.pdf"

			Case m.gnAn * 12 + m.gnLuna >= 2022 * 12 + 4
				lcFisierPDFOriginal = lcComunPath + "d394_202204.pdf"
			Case m.gnAn * 12 + m.gnLuna >= 2020 * 12 + 9
				lcFisierPDFOriginal = lcComunPath + "d394_202009.pdf"
			Case m.gnAn * 12 + m.gnLuna >= 2018 * 12 + 1
				lcFisierPDFOriginal = lcComunPath + "d394_201801.pdf"
			Case m.gnAn * 12 + m.gnLuna >= 2017 * 12 + 1
				lcFisierPDFOriginal = lcComunPath + "d394_201701.pdf"
			Case m.gnAn * 12 + m.gnLuna >= 2016 * 12 + 7
				lcFisierPDFOriginal = lcComunPath + "d394_201607.pdf"
			Otherwise
				lcFisierPDFOriginal = lcComunPath + "d394_201607.pdf"
		Endcase

		This.UpdatePDF(m.lcFisierPDFOriginal) && actualizez pdf de la romfast

		lcFileName = m.lcCaleFisier + lcNumeFisier

		lcFileNamePDF = m.lcCaleFisierPDF + lcNumeFisier
		lcFisierPDFNou = m.lcFileNamePDF + [.pdf]
		lcFisierXDP = m.lcFileNamePDF + [.xdp]

		lcFisierTXT = m.lcFileName + [.txt]
		lcFisierXLS = m.lcFileName + [.xls]

		If File(lcFisierTXT)
			Delete File (lcFisierTXT)
		Endif
		If File(lcFisierPDFNou)
			Delete File (lcFisierPDFNou)
		Endif
		If File(lcFisierXDP)
			Delete File (lcFisierXDP)
		Endif

		This.do_genereaza_xdp(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

		If File(lcFisierXDP) And File(m.lcFisierPDFNou)
			open_default_app(lcFisierXDP)
		Endif

		*** deschid explorer in directorul de export
		lcRun = [RUN /N7 explorer.exe "] + m.lcCaleFisier + ["]
		&lcRun
	Endproc && do_executa

	Procedure do_verifica_xml
		Local loProcess As "api_apprun" Of "process.vcx"
		Local lcAppPath, lcBilantValidatorFile, lcComunPath, lcFisierXML, lnPos
		lcAppPath = Addbs(gcAppPath)
		lcComunPath = m.lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If m.lnPos > 0
			lcComunPath = Left(m.lcAppPath, m.lnPos)
		Endif
		lcComunPath = m.lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		lcBilantValidatorFile = m.lcComunPath + [dist\DUKIntegrator.jar]

		amessagebox('Validati fisierul PDF si salvati fisierul D394.xml in directorul ' + This.CaleFisierPDF + Chr(13) + Chr(10) + ;
			  'Dupa ce porneste validatorul ANAF, apasati validare in validator pentru a verifica fisierul XML.', 0 + 48, _Screen.Caption)

		lcFisierXML = Addbs(This.CaleFisierPDF) + 'D394.xml'
		If !File(m.lcFisierXML)
			amessagebox('Nu exista fisierul ' + m.lcFisierXML + Chr(13) + Chr(10) + 'Alegeti fisierul D394.xml!', 0 + 48, _Screen.Caption)
			lcFisierXML = Getfile('xml', 'D394.xml', 'Deschide', 0, 'Alegeti fisierul D394.xml')
		Endif

		loProcess = Newobject("api_apprun", "process.vcx")
		loProcess.icLaunchDir = Justpath(m.lcBilantValidatorFile)
		loProcess.iccommandline = [java -jar "DUKIntegrator.jar" ] + m.lcFisierXML

		*!*	loProcess.launchappandwait()
		m.loProcess.launchapp()
	Endproc && do_verifica_xml

Enddefine && D394_2016
*************************************************************************

Define Class D394 As Custom

	livb = 0 && baza livrari
	livt = 0 && tva livrari
	livnrf = 0 && numar facturi pentru livrari
	livtib = 0 && baza livrari taxare inversa
	livtit = 0 && tva livrari taxare inversa
	livtinrf = 0 && numar facturi pentru livrari taxare inversa
	achb = 0 && baza achizitii
	acht = 0 && tva achizitii
	achnrf = 0 && numar facturi pentru achizitii
	achtib = 0 && baza achizitii taxare inversa
	achtit = 0 && tva achizitii taxare inversa
	achtinrf = 0 && numar facturi pentru achizitii taxare inversa

	Total = 0
	rectificativa = 0
	TIP = 'L'
	Perioada = 1
	NumarCoduriFiscale = 0

	Procedure do_executa
		Lparameters tlSilent, tnNumarLuni
		*** tlSilent: .T. (DEFAULT .F.) Doar calculeaza D394, nu exporta XDP. pentru apelarea din D300 randurile 17.1 si 27.1 (nu vreau sa vad si pdf-ul inteligent D394)
		*** tnNumarLuni: 1,3,6,9,12 - pentru apelare din D300

		Local loDecl As "frm_decl394"
		Local loFrmDoc As "frm_decl_tipdoc"
		Local lcAppPath, lcCaleFisier, lcCaleFisierPDF, lcComunPath, lcConditieAct, lcConditieLuna, lcCursor
		Local lcFileName, lcFileNamePDF, lcFisierPDFNou, lcFisierPDFOriginal, lcFisierTXT, lcFisierXDP
		Local lcFisierXLS, lcMesaj, lcNumeFisier, lcRun, lcSelect, lcSirId, lcSql, lcTempPath, lcTip
		Local llSucces, lnAn, lnLuna1, lnLuna2, lnLunaF, lnLunaI, lnNrLuni1, lnNrLuni2, lnNumarLuni
		Local lnPerioada, lnPos, lnSucces, loEx
		Local llSilent, llSucces

		llSilent = m.tlSilent
		lnNumarLuni = Iif(!Empty(m.tnNumarLuni), m.tnNumarLuni, 1)
		lcTip = Iif(m.lnNumarLuni = 1, "LUNAR", Iif(m.lnNumarLuni = 3, "TRIMESTRIAL", Iif(m.lnNumarLuni = 6, "SEMESTRIAL", "ANUAL")))

		lcSelect = Select()

		*** EXPORT D394
		lcAppPath = Addbs(gcAppPath)
		lcComunPath = lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If lnPos > 0
			lcComunPath = Left(m.lcAppPath, lnPos)
		Endif
		lcComunPath = lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		*** Creez structura director export (caletemp\D394\aaaa_ll\firma\
		llSucces = .F.
		Try
			lcTempPath = m.gcBasePath
			lcCaleFisier = m.lcTempPath + "D394\" && c:\temp\D394\

			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Alltrim(Str(gnAn)) + "_" + Padl(Alltrim(Str(gnLuna)), 2, "0") + "\"&& c:\temp\D394\2011_01\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Strtran(gcFirma, [ ], [_]) + "\" && c:\temp\D390\2011_01\firma\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisierPDF = m.lcCaleFisier  && c:\temp\D394\2011_01\firma\
			If !Directory(m.lcCaleFisierPDF)
				Md (m.lcCaleFisierPDF)
			Endif

			llSucces = .T.
		Catch To loEx
			lcMesaj = "Creare director " + lcCaleFisier + " " + loEx.Message

			amessagebox(lcMesaj, 0 + 48)
		Endtry
		If !llSucces
			Return
		Endif

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', Strtran(Alltrim(gofirma.cod_fiscal), 'RO', ''))
		Endif

		lcNumeFisier = "d394_" + gofirma.codfiscalfro + "_" + Alltrim(Str(gnAn)) + "_" + Padl(gnLuna, 2, "0")
		Do Case
			Case m.gnAn * 12 + m.gnLuna <= 2013 * 12 + 11
				lcFisierPDFOriginal = lcComunPath + "d394.pdf" && d:\contafin\comuncontafin\d394.pdf
			Otherwise
				lcFisierPDFOriginal = lcComunPath + "d394_201312.pdf" && d:\contafin\comuncontafin\d394_201312.pdf
		Endcase
		If !m.llSilent
			This.UpdatePDF(m.lcFisierPDFOriginal) && actualizez pdf de la romfast
		Endif

		lcFileName = m.lcCaleFisier + lcNumeFisier

		lcFileNamePDF = m.lcCaleFisierPDF + lcNumeFisier
		lcFisierPDFNou = m.lcFileNamePDF + [.pdf]
		lcFisierXDP = m.lcFileNamePDF + [.xdp]

		lcFisierTXT = m.lcFileName + [.txt]
		lcFisierXLS = m.lcFileName + [.xls]

		*!* 21.02.2012
		If !m.llSilent
			If File(lcFisierTXT)
				Delete File (lcFisierTXT)
			Endif
			If File(lcFisierPDFNou)
				Delete File (lcFisierPDFNou)
			Endif
			If File(lcFisierXDP)
				Delete File (lcFisierXDP)
			Endif
		Endif
		*!* 21.02.2012 ^


		Private pounitate
		pounitate = Createobject("empty")

		AddProperty(pounitate, "an", gnAn)
		AddProperty(pounitate, "tip", m.lcTip)
		AddProperty(pounitate, "luniextra", 0)
		*!*	23.07.2010
		AddProperty(pounitate, "tvanedeductibil", .F.)
		*!*	23.07.2010 ^
		AddProperty(pounitate, "codi", Strtran(Strtran(Alltrim(Upper(Nvl(gofirma.cod_fiscal, ''))), 'RO', ''), 'R', ''))
		AddProperty(pounitate, "deni", Nvl(Alltrim(gofirma.firma), ''))
		AddProperty(pounitate, "adr", Nvl(Alltrim(gofirma.adresa), ''))
		AddProperty(pounitate, "tel", Nvl(Alltrim(gofirma.telefon), ''))
		AddProperty(pounitate, "fax", Nvl(Alltrim(gofirma.fax), ''))
		AddProperty(pounitate, "email", Nvl(Alltrim(gofirma.email), ''))

		AddProperty(pounitate, "taraR", "")
		AddProperty(pounitate, "codiR", "")
		AddProperty(pounitate, "deniR", "")
		AddProperty(pounitate, "adrR", "")
		AddProperty(pounitate, "telR", "")
		AddProperty(pounitate, "faxR", "")
		AddProperty(pounitate, "emailR", "")

		AddProperty(pounitate, "numeD", "")
		AddProperty(pounitate, "functieD", "")
		AddProperty(pounitate, "rectificativa", 0)

		loDecl = Createobject("frm_decl394")
		loDecl.Show(1)

		If gnButon = 2
			Return
		Endif

		*!* 05.11.2009
		lnAn = pounitate.an
		lcTip = pounitate.TIP
		lnNumarLuni = Iif(m.lcTip = "TRIMESTRIAL", 3, Iif(m.lcTip = "SEMESTRIAL", 6, Iif(m.lcTip = "ANUAL", 12, 1)))
		This.TIP = Left(m.lcTip, 1)
		* Luna de inceput a perioadei
		* 1 = gnLuna
		* 3 = 1,4,7,10
		* 6 = 1,7
		* 12 = 1
		lnPerioada = Ceil(m.gnLuna / m.lnNumarLuni)
		This.Perioada = m.lnPerioada
		lnLuna1 = (lnPerioada - 1) * lnNumarLuni + 1
		lnLuna2 = lnPerioada * lnNumarLuni

		*** dataact
		lnLunaI = lnAn * 12 + lnLuna1
		lnLunaF = lnAn * 12 + lnLuna2
		*** dataact ^

		*** luna/an contabil
		lnNrLuni1 = lnAn * 12 + lnLuna1
		lnNrLuni2 = lnAn * 12 + lnLuna2 + pounitate.luniExtra
		*** luna/an contabil ^

		*!* ID-URI DE TIPURI DOCUMENTE CARE REPREZINTA FACTURI
		If pounitate.luniExtra = 0
			lcConditieLuna = [ (AN  = ] + Alltrim(Str(m.lnAn)) + [ and (luna BETWEEN ] + Alltrim(Str(m.lnLuna1)) + [ and ] + Alltrim(Str(m.lnLuna2)) + [ ))]
		Else
			lcConditieLuna = [ (AN * 12 + LUNA BETWEEN ] + Alltrim(Str(m.lnNrLuni1)) + [ and ] + Alltrim(Str(m.lnNrLuni2)) + [)]
		Endif

		*** iau doar facturile din luna emiterii
		*!*			lcConditieAct = [ and (extract(year from dataact)*12+extract(month from dataact) between ] + Alltrim(Str(m.lnLunaI)) + [ and ] + Alltrim(Str(m.lnLunaF)) + [)]
		lcConditieAct = [ and (extract(year from dataact)*12+extract(month from dataact) = an*12+luna)]

		Text To lcSql Textmerge Noshow
	SELECT 0 AS ALES, FEL_DOCUMENT AS FDOC, ID_FDOC
		FROM vjv2010
		WHERE
		<<lcConditieLuna>>
		<<lcConditieAct>>
		<<gcCondSucursala>>
		UNION
		SELECT 0 AS ALES, FEL_DOCUMENT AS FDOC, ID_FDOC
		FROM vjc2010
		WHERE
		<<lcConditieLuna>>
		<<lcConditieAct>>
		<<gcCondSucursala>>
		Endtext
		goLog.Log(lcSql)

		*!*		    AND SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) = 'RO'

		*!* 05.11.2009 ^
		lnSucces = goExecutor.oExecute(lcSql, "crsFdoc")
		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Update crsFDoc Set ales = 1 Where Alltrim(fdoc) = 'FACTURA'
		Go Top In crsFDoc

		loFrmDoc = Createobject("frm_decl_tipdoc", "394")
		loFrmDoc.Show(1)

		lcSirId = ""
		Select id_fdoc From crsFDoc With (Buffering = .T.) Where ales = 1 Into Cursor crsFdocTemp
		If _Tally = 0
			amessagebox('Trebuie sa alegeti un tip de document!', 0 + 48, 'Atentie')
			Use In crsFDoc
			Use In crsFdocTemp
			Return
		Endif
		Use In crsFDoc
		Select crsFdocTemp
		Scan
			lcSirId = 	lcSirId + [, ] + Alltrim(Str(id_fdoc))
		Endscan
		If !Empty(lcSirId)
			lcSirId = Substr(lcSirId, 2)
		Endif
		Use In crsFdocTemp

		*!* 05.11.2009
		Text To lcSql Textmerge Noshow
		SELECT ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', '') AS COD_FISCALFRO, SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) AS ATRIBUT_FISCAL, DENUMIRE,
		 ROUND(SUM(RO24B + RO20B + RO19B + RO9B + RO5B + RO24NB + RO20NB + RO9NB + RO5NB), 0) AS BAZA,
		 ROUND(SUM(RO24T + RO20T + RO19T + RO9T + Ro5T + RO24NT + RO20NT + RO9NT + RO5NT), 0) AS TVA,
		 ROUND(SUM(ROTI), 0) AS TIBAZA,
		 0 AS TITVA,
         SUM(case when ROTI = 0 then 1 else 0 end ) as NRFACT,
         SUM(case when ROTI <> 0 then 1 else 0 end ) as NRFACTTI
		 FROM VJV2013
		 WHERE
		<<lcConditieLuna>>
		<<lcConditieAct>>
		AND ID_FDOC IN (<<m.lcSirId>>)
	    <<gcCondSucursala>>
		GROUP BY ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', ''), SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2), DENUMIRE
		Endtext

		*!*		    AND SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) = 'RO'

		goLog.Log(lcSql)

		*!* 05.11.2009 ^

		lcCursor = [crsJV]
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Update crsJV Set atribut_fiscal = '' Where Isdigit(atribut_fiscal)

		*!* 23.07.20010 RO24, ROSCN OPTIONAL
		*!* 05.11.2009
		*!*		[ SUM(RO19BCB  + RO19BVB + RO19BFB + RO09BCB + RO09BVB + RO09BFB + RO05B) AS BAZA, ]
		Text To lcSql Textmerge Noshow
	SELECT ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', '') AS COD_FISCALFRO, SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) AS ATRIBUT_FISCAL, DENUMIRE,
		 ROUND(SUM(RO24B + RO20B + RO19B + RO09B + RO05B + RO24NB + RO20NB + RO9NB + RO5NB <<IIF(pounitate.tvanedeductibil, '+ ROSCN', '')>>), 0) AS BAZA,
		 ROUND(SUM(RO24T + RO20T + RO19T + RO09T + RO05T + RO24NT + RO20NT + RO9NT + RO5NT), 0) AS TVA,
		 ROUND(SUM(TI19B + TI09B + TI20B + TI24B), 0) as TIBAZA,
		 ROUND(SUM(TI19T + TI09T + TI20T + TI24T), 0) as TITVA,
	     SUM(case when TI19B = 0 and TI09B = 0 and TI20B = 0 and TI24B = 0 then 1 else 0 end ) as NRFACT,
         SUM(case when TI19B <> 0 or TI09B <> 0 or TI20B <> 0 or TI24B <> 0 then 1 else 0 end ) as NRFACTTI
		 FROM VJC2013
		 WHERE
		<<lcConditieLuna>>
		<<lcConditieAct>>
		AND ID_FDOC IN (<<m.lcSirId>>)
	    <<gcCondSucursala>>
		GROUP BY ID_PART, COD_FISCAL, REGEXP_REPLACE(COD_FISCAL, '[^[:digit:]]', ''), SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2), DENUMIRE
		Endtext

		*!*		    AND SUBSTR(TRIM(UPPER(COD_FISCAL)),1,2) = 'RO'

		goLog.Log(lcSql)
		*!* 05.11.2009 ^
		*!* 23.07.20010 ^

		lcCursor = [crsJC]
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Update crsJC Set atribut_fiscal = '' Where Isdigit(atribut_fiscal)

		Create Cursor crsTipA (TIP c(1))
		Insert Into crsTipA (TIP) Values ('A')
		If m.gnAn > 2011
			Insert Into crsTipA (TIP) Values ('C') && ACHIZITII TAXARE INVERSA
		Endif

		Create Cursor crsTipL (TIP c(1))
		Insert Into crsTipL (TIP) Values ('L')
		If m.gnAn > 2011
			Insert Into crsTipL (TIP) Values ('V') && LIVRARI TAXARE INVERSA
		Endif

		Select b.TIP, id_part, Left(cod_fiscal, 30) As cod_fiscal, Left(cod_fiscalfro, 30) As cod_fiscalfro, Padr(Nvl(atribut_fiscal, ''), 2, ' ') As atribut_fiscal, denumire, ;
			Cast(Iif(b.TIP = 'A', baza, tibaza) As N(20)) As baza, ;
			Cast(Iif(b.TIP = 'A', TVA, titva) As N(20)) As TVA, ;
			Cast(Iif(b.TIP = 'A', NRFACT, NRFACTTI) As N(20)) As NRFACT ;
			From crsJC a, crsTipA b ;
			Where a.baza <> 0 Or a.TVA <> 0 Or NRFACT <> 0 Or a.tibaza <> 0 Or a.titva <> 0 Or NRFACTTI <> 0 ;		&&	modificare	v 2.5.26
			Union ;
			Select b.TIP, id_part, Left(cod_fiscal, 30) As cod_fiscal, Left(cod_fiscalfro, 30) As cod_fiscalfro, Padr(Nvl(atribut_fiscal, ''), 2, ' ') As atribut_fiscal, denumire, ;
			Cast(Iif(b.TIP = 'L', baza, tibaza) As N(20)) As baza, ;
			Cast(Iif(b.TIP = 'L', TVA, titva) As N(20)) As TVA, ;
			Cast(Iif(b.TIP = 'L', NRFACT, NRFACTTI) As N(20)) As NRFACT ;
			From crsJV a, crsTipL b ;
			Where a.baza <> 0 Or a.TVA <> 0 Or NRFACT <> 0 Or a.tibaza <> 0 Or a.titva <> 0 Or NRFACTTI <> 0 ;		&&	modificare	v 2.5.26 : HAVING BAZA <> 0 Or TVA <> 0 OR TIBAZA <> 0 OR TITVA <> 0
			Order By 1 Desc, 4 ;
			Into Cursor crsD394Temp Readwrite

		Use In (Select('crsTipA'))
		Use In (Select('crsTipL'))
		Use In (Select('crsJV'))
		Use In (Select('crsJC'))

		*** verificare coduri fiscale parteneri si alegere care dintre ei sa intre in D394 (coloana ales)
		*!*			Create Cursor crsVerificareParteneri (ales L, denumire c(100) Null, cod_fiscal c(30) Null, Valid L Null, firma c(100) Null, cod c(100) Null, adresa c(250)  Null, ;
		*!*				  judet c(100)  Null, regcom c(30)  Null, codpostal c(20)  Null, telefon c(30)  Null, fax c(30)  Null, ;
		*!*				  datatvamfin c(30) Null, platitortvamfin L Null, VIES L Null, PlatitorTVA L Null, eroare c(250)  Null, iscif L  Null, duplicat L, atribut_fiscal c(2), stare c(100), atentie c(100), ;
		*!*				  cod_fiscalfro c(30) Null, id_part N(16))

		Create Cursor crsVerificareParteneri (ales L, denumire c(100) Null, cod_fiscal c(30) Null, Valid L Null, firma c(100) Null, cod c(100) Null, adresa c(250)  Null, ;
			  JUDET c(100)  Null, regcom c(30)  Null, codpostal c(20)  Null, telefon c(30)  Null, fax c(30)  Null, ;
			  datatvamfin c(30) Null, platitortvamfin L Null, VIES L Null, PlatitorTVA L Null, eroare c(250)  Null, iscif L  Null, duplicat L, atribut_fiscal c(2), stare c(100), atentie c(100), cod_fiscalfro c(30) Null, id_part N(16), ;
			  tvaincasare L, splittva L, inactiv L, Data D)

		Select Distinct id_part, cod_fiscal, cod_fiscalfro, atribut_fiscal, denumire ;
			From crsD394Temp Order By denumire ;
			Into Cursor crsCoduriFiscale

		Select crsVerificareParteneri
		Append From Dbf('crsCoduriFiscale')


		loVerificare = Createobject("verificarecod") && validare.prg

		Select crsVerificareParteneri
		Replace All ales With (atribut_fiscal = 'RO')
		*!*			loVerificare = Createobject("verificarecod") && validare.prg
		*!*			Select crsVerificareParteneri
		*!*			Replace All IsCIF With loVerificare.IsCIF(COD_FISCAL)
		*!*			Replace All Valid With Iif(IsCIF, loVerificare.validare('CIF', COD_FISCAL), loVerificare.validare('CNP', COD_FISCAL))
		*!*			Replace all ales with (IsCIF and valid and atribut_fiscal = 'RO')

		llSucces = .T.
		If !m.llSilent
			Local llValidare, llMFIN, llVIES, llAutoVerificare
			llValidare = .T.
			llMFIN = .T.
			llVIES = .F.
			llAutoVerificare = .F.

			loVerificareParteneri = Createobject("frm_verificare_parteneri", m.llValidare, m.llMFIN, m.llVIES, m.llAutoVerificare, .T., .T.) && parteneri.vcx

			loVerificareParteneri.lb_titlu_alb_b121.Caption = "D394 - Doar partenerii marcati ALES intra in declaratie! Verificati codurile fiscale!!"
			loVerificareParteneri.chkExperimental.Value = .T.
			loVerificareParteneri.do_verifica(.T., .F., .F.)
			loVerificareParteneri.Show(1)
			llSucces = (m.gnButon = 1)
		Endif

		If m.llSucces
			Select D.TIP, D.cod_fiscalfro As cod_fiscal, Max(D.denumire) As denumire, Sum(D.baza) As baza, Sum(D.TVA) As TVA, Sum(D.NRFACT) As NRFACT ;
				From crsD394Temp D Join crsVerificareParteneri V On D.id_part = V.id_part ;
				Where V.ales ;
				Group By D.TIP, D.cod_fiscalfro ;
				Order By 1 Desc, 3 ;
				Into Cursor crsD394

			Use In (Select('crsVerificareParteneri'))

			Select crsD394
			Calculate Sum(Iif(TIP = 'A', baza, 0)), Sum(Iif(TIP = 'A', TVA, 0)), Sum(Iif(TIP = 'A', NRFACT, 0)), ;
				Sum(Iif(TIP = 'C', baza, 0)), Sum(Iif(TIP = 'C', TVA, 0)), Sum(Iif(TIP = 'C', NRFACT, 0)), ;
				Sum(Iif(TIP = 'L', baza, 0)), Sum(Iif(TIP = 'L', TVA, 0)), Sum(Iif(TIP = 'L', NRFACT, 0)), ;
				Sum(Iif(TIP = 'V', baza, 0)), Sum(Iif(TIP = 'V', TVA, 0)), Sum(Iif(TIP = 'V', NRFACT, 0)) ;
				To This.achb, This.acht, This.achnrf, This.achtib, This.achtit, This.achtinrf, This.livb, This.livt, This.livnrf, This.livtib, This.livtit, This.livtinrf In crsD394


			This.Total = This.achb + This.acht + This.achtib + This.achtit + This.livb + This.livt + This.livtib + This.livtit

			If !m.llSilent
				Select Distinct cod_fiscal From crsD394 Into Cursor crsCoduriFiscaleCnt
				This.NumarCoduriFiscale = _Tally
				Use In Select('crsCoduriFiscaleCnt')

				This.NumarCoduriFiscale = _Tally
				Use In Select('crsCoduriFiscale1')

				*!* am nume diferite cu acelasi cod fiscal in JV, JC sau in amandoua
				*!* exista nume fara cod fiscal??? (nu stiu de ce am pus conditia asta - le-am luat pe cele cu RO - au cod fiscal)
				*!*				Select DISTINCT COD_FISCAL, Padr(Alltrim(Upper(denumire)),30,' ') As denumire, "RO" as atribut_fiscal ;
				*!*					FROM crsD394 ;
				*!*					ORDER By 1,2 ;
				*!*					INTO Cursor crsCoduriFiscale

				*** This.do_verifica()


				Do Case
					Case m.gnAn <= 2011
						This.do_genereaza_txt(lcFisierTXT)

					Case m.gnAn * 12 + m.gnLuna <= 2012 * 12 + 11 && 01.2012 - 11.2013
						This.do_genereaza_xdp_201201(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

						If File(lcFisierXDP) And File(m.lcFisierPDFNou)
							open_default_app(lcFisierXDP)
						Endif
					Otherwise && 12.2013 -
						This.do_genereaza_xdp_201312(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

						If File(lcFisierXDP) And File(m.lcFisierPDFNou)
							open_default_app(lcFisierXDP)
						Endif
				Endcase
				This.do_genereaza_xls(m.lcFisierXLS)

				*** deschid explorer in directorul de export
				lcRun = [RUN /N7 explorer.exe "] + m.lcCaleFisier + ["]
				&lcRun
			Endif && m.llSilent

		Endif && llSucces

		Use In (Select('crsVerificareParteneri'))
		Use In Select('crsCoduriFiscale')
		Use In (Select('crsJV'))
		Use In (Select('crsJC'))

		Select (m.lcSelect)

	Endproc && do_executa

	*===========================================
	Procedure do_verifica

		Local lcSelect
		lcSelect = Select()


		If Reccount('crsCoduriFiscale') > 0
			Cursortoxml("crsCoduriFiscale", "lcXMLParteneri", 1, 0, 0, "1")
			Do verificare_parteneri With m.lcXMLParteneri, .T., .T.
		Endif

		Select (m.lcSelect)
	Endproc && do_verifica

	*===========================================
	Procedure do_genereaza_xls
		Lparameters tcFisierXLS

		Local lcSelect
		lcSelect = Select()

		If Type('crsD394.nrfact') <> 'U'
			Select TIP, Left(Alltrim(cod_fiscal), 10) As cod_fiscal, Left(denumire, 100) As denumire, baza + TVA As Total, baza, TVA, NRFACT ;
				From crsD394 ;
				Where NRFACT <> 0 ;
				Order By 1 Desc, 3 ;
				Into Cursor cExportXLS
		Else
			Select TIP, Left(Alltrim(cod_fiscal), 10) As cod_fiscal, Left(denumire, 100) As denumire, baza + TVA As Total, baza, TVA ;
				From crsD394 ;
				Where baza <> 0 Or TVA <> 0;
				Order By 1 Desc, 3 ;
				Into Cursor cExportXLS
		Endif

		Select cExportXLS
		Copy To (m.tcFisierXLS) Type Xl5
		*open_default_app(m.tcFisierXLS)

		Use In (Select('cExportXLS'))

		Select (m.lcSelect)

	Endproc && do_genereaza_xls

	*===========================================
	Procedure do_genereaza_txt
		Lparameters tcFisierTXT

		Local lcDecla, lcDeclb, lcDir, lcDirA, lcDirB, lcFileA, lcFileB, lcFileNameA, lcFileNameB, lcPoint, lnPerioada

		lcPoint = Set("Point")
		Set Point To '.'
		lnPerioada = This.Perioada

		*!* fisierul in format din alte aplicatii
		Set Textmerge On To Memvar lcDecla1 Noshow
		With pounitate
			\394,<<Left(Alltrim(.codi),10)>>,#<<'S' + Left(Alltrim(Str(m.lnPerioada)),1)>>#,<<Left(Alltrim(Str(.an)),4)>>,#<<Left(Alltrim(.deni),100)>>#,
			\\#<<Left(Alltrim(.adr),160)>>#,#<<Left(Alltrim(.tel),30)>>#,#<<Left(Alltrim(.fax),30)>>#,#<<Left(Alltrim(.email),80)>>#,
			\\<<Left(Alltrim(.codiR),10)>>,#<<Left(Alltrim(.deniR),100)>>#,#<<Left(Alltrim(.adrR),160)>>#,#<<Left(Alltrim(.telR),30)>>#,#<<Left(Alltrim(.faxR),30)>>#,#<<Left(Alltrim(.emailR),80)>>#,
			\\<<Alltrim(Str(This.NumarCoduriFiscale,15))>>,<<Alltrim(Str(This.livb,15,2))>>,<<Alltrim(Str(This.livt,15,2))>>,<<Alltrim(Str(This.achb,15,2))>>,<<Alltrim(Str(This.acht,15,2))>>
		Endwith
		Set Textmerge To

		*!* fisierul in format propriu D394
		Set Textmerge On To Memvar lcDeclb1 Noshow
		With pounitate
	\ 2
			\<<Left(Alltrim(Str(.an)),4)>>,#<<'S' + Left(Alltrim(Str(m.lnPerioada)),1)>>#,##,##,<<Left(Alltrim(.codi),10)>>,#<<Left(Alltrim(.deni),100)>>#,
			\\#<<Left(Alltrim(.adr),160)>>#,#<<Left(Alltrim(.tel),30)>>#,#<<Left(Alltrim(.fax),30)>>#,#<<Left(Alltrim(.email),80)>>#,
			\\#<<Alltrim(.numeD)>>#,#<<Alltrim(.functieD)>>#,
			\\#<<Left(Upper(Alltrim(.taraR)),2)>>#,<<Left(Alltrim(.codiR),10)>>,#<<Left(Alltrim(.deniR),100)>>#,#<<Left(Alltrim(.adrR),160)>>#,#<<Left(Alltrim(.telR),30)>>#,#<<Left(Alltrim(.faxR),30)>>#,#<<Left(Alltrim(.emailR),80)>>#
		Endwith
		Set Textmerge To

		*!*	 2
		*!*	2007,#S1#,##,##,1879855,#XACN GREU#,#JUD. CONSTANTA  SECT. 1  LOC. AGIGEA  STR. AGIGEA  NR. 1  SC. B  AP. 21#,#telefon#,#fax#,#email#,#nume declarant#,#functie declarant#,#RO#,1879855,#REPREZENTANT FISCAL#,#DOMICILIU REPREZENTANT FISCAL#,#TELEFON REPR#,#FAX REPR#,#EMAIL REPR#
		*!*	10117033,#MEDIA PRESS#
		*!*	13714478,#S.P.G.C. CERNAVODA#
		*!*	STOP1
		*!*	10117033,,174294.07,15690.45
		*!*	STOP2
		*!*	13714478,,109.34,20.78

		*!* fisierul in format din alte aplicatii
		Set Textmerge On To Memv lcDecla2 Noshow

		Select crsD394
		Scan For baza <> 0 Or TVA <> 0
		\#<<TIP>>#,<<Left(Alltrim(cod_fiscal),10)>>,#<<Left(Alltrim(denumire),100)>>#,<<Alltrim(Str(baza,15,2))>>,<<Alltrim(Str(TVA,15,2))>>
		Endscan
		Set Textmerge To


		*!* fisierul in format propriu D394
		Select Distinct cod_fiscal, denumire From crsD394 Into Cursor crsCoduriFiscaleB
		Set Textmerge On To Memv lcDeclb2 Noshow
		Select crsCoduriFiscaleB
		Scan
			\<<Left(Alltrim(cod_fiscal),10)>>,#<<Alltrim(denumire)>>#
		Endscan
		Use In (Select('crsCoduriFiscaleB'))
		\STOP1

		Select crsD394
		Scan For TIP = 'A' And (baza <> 0 Or TVA <> 0)
		\<<Left(Alltrim(cod_fiscal),10)>>,<<Alltrim(Str(baza+TVA,15,2))>>,<<Alltrim(Str(baza,15,2))>>,<<Alltrim(Str(TVA,15,2))>>
		Endscan
		\STOP2
		Select crsD394
		Scan For TIP = 'L' And (baza <> 0 Or TVA <> 0)
		\<<Left(Alltrim(cod_fiscal),10)>>,<<Alltrim(Str(baza+TVA,15,2))>>,<<Alltrim(Str(baza,15,2))>>,<<Alltrim(Str(TVA,15,2))>>
		Endscan

		Set Textmerge To


		*!* fisierul in format din alte aplicatii
		lcDecla = m.lcDecla1 + Chr(13) + Chr(10) +  m.lcDecla2 + Chr(13) + Chr(10)

		*!* fisierul in format propriu D394
		lcDeclb = m.lcDeclb1 + Chr(13) + Chr(10) +  m.lcDeclb2 + Chr(13) + Chr(10)

		Set Point To &lcPoint


		*!* 394_S107_J1879855.txt
		lcFileNameA = "394_S" + Alltrim(Str(m.lnPerioada)) + Right(Str(pounitate.an, 4), 2) + "_J" + Alltrim(pounitate.codi) + ".txt"

		*!* D394_2007S1_1879855.txt
		lcFileNameB = "D394_" + Alltrim(Str(pounitate.an, 4)) + "S" + Alltrim(Str(m.lnPerioada)) + "_" + Alltrim(pounitate.codi) + ".txt"

		lcDir = "C:\Program Files\D394\"
		If !Directory(lcDir)
			lcDir = Getdir("c:\", "D394", "Alegeti directorul D394\")
		Endif
		If !Empty(lcDir)
			lcDir = Addbs(lcDir)
			lcDirA = lcDir + "394_UF\"
			lcDirB = lcDir + "394\"

			If !Directory(lcDirA)
				Md (lcDirA)
			Endif
			If !Directory(lcDirB)
				Md (lcDirB)
			Endif

			lcFileA = Addbs(lcDirA) + lcFileNameA
			Strtofile(lcDecla, lcFileA)

			lcFileB = Addbs(lcDirB) + lcFileNameB
			Strtofile(lcDeclb, lcFileB)


			amessagebox('Fisierul a fost exportat in ' + lcFileB + Chr(13) + Chr(10) + [Intrati in aplicatia D394 si apasati pe butonul ] + ;
				  ["Restaurare fisier"], 0 + 64, 'Declaratia 394')
		Endif


	Endproc && do_genereaza_txt

	*===========================================
	Procedure do_genereaza_xdp_201312
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou


		lcSelect = Select()
		lcTip = This.TIP && L/T/S/A (Lunara/Trimestriala/Semestriala/Anuala)
		lnRectificativa = This.rectificativa && 0/1

		lnLIVB = This.livb
		lnLIVT = This.livt
		lnLIVNRF = This.livnrf

		lnACHB = This.achb
		lnACHT = This.acht
		lnACHNRF = This.achnrf

		lnLIVTIB = This.livtib
		lnLIVTIT = This.livtit
		lnLIVTINRF = This.livtinrf

		lnACHTIB = This.achtib
		lnACHTIT = This.achtit
		lnACHTINRF = This.achtinrf

		lnTotal = This.Total
		lnNrOperatori = This.NumarCoduriFiscale

		lcCodFiscalF = Alltrim(gofirma.codfiscalfro)
		lcLuna = Padl(Alltrim(Str(gnLuna)), 2, [0])
		lcAn = Alltrim(Str(gnAn))


		lcXDP = ""

		Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="XFA2_4" APIVersion="3.0.11263.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<form1>
<body1>
<HEADER xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<sub_per>
<an_r><<m.lcAn>></an_r>
<luna_r><<m.lcLuna>></luna_r>
<TIP_D394><<m.lcTip>></TIP_D394>
</sub_per>
<sub_dateDeIdentificare>
<cif><<m.lcCodFiscalF>></cif>
<den><<XmlSpecialCharacters(RemoveCharacters(Alltrim(gofirma.antet), .T.))>></den>
<adresa><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(goFirma.adresa), .T.))>></adresa>
<telefon><<ALLTRIM(goFirma.telefon)>></telefon>
<fax><<ALLTRIM(goFirma.fax)>></fax>
<mail><<ALLTRIM(LOWER(goFirma.email))>></mail>
<d_rec><<m.lnRectificativa>></d_rec>
</sub_dateDeIdentificare>
</body1>
<body11>
<sub_dateDeIdentificare>
<cifR/>
<denR/>
<adresaR/>
<telefonR/>
<faxR/>
<mailR/>
</sub_dateDeIdentificare>
</body11>
<body10>
<totalPlata_A></totalPlata_A>
<nrCui><<m.lnNrOperatori>></nrCui>
<bazaL><<m.lnLIVB>></bazaL>
<tvaL><<m.lnLIVT>></tvaL>
<bazaA><<m.lnACHB>></bazaA>
<tvaA><<m.lnACHT>></tvaA>
<bazaV><<m.lnLIVTIB>></bazaV>
<tvaV><<m.lnLIVTIT>></tvaV>
<bazaVc>0</bazaVc>
<tvaVc>0</tvaVc>
<bazaC><<m.lnACHTIB>></bazaC>
<tvaC><<m.lnACHTIT>></tvaC>
<bazaCc>0</bazaCc>
<tvaCc>0</tvaCc>
<NrL><<m.lnLIVNRF>></NrL>
<NrA><<m.lnACHNRF>></NrA>
<NrV><<m.lnLIVTINRF>></NrV>
<NrC><<m.lnACHTINRF>></NrC>
</body10>
<body12>
<sub_semnatura>
<nume_declar><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(goFirma.nume_declarant), .T.))>></nume_declar>
<prenume_declar><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(goFirma.prenume_declarant), .T.))>></prenume_declar>
<functie_declar><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(goFirma.functie), .T.))>></functie_declar>
</sub_semnatura>
<sub_organFiscal>
<numeFisier/>
</sub_organFiscal>
</body12>
<body3>
		Endtext


		lnSecventa = 0
		Select crsD394
		Scan For NRFACT <> 0
			lcCodFiscal = Alltrim(cod_fiscal)
			lcDenumire = Alltrim(Substr(XmlSpecialCharacters(RemoveCharacters(denumire, .T.)), 1, 180))
			lcTip = Alltrim(Upper(TIP))
			lnSecventa = lnSecventa + 1
			lcBaza = Alltrim(Str(baza, 20, 0))
			lcTVA = Alltrim(Str(TVA, 20, 0))
			lcAjutor = Alltrim(Str(baza + TVA, 20, 0))
			lcNrFact = Alltrim(Str(NRFACT, 20, 0))
			Text To lcXDP Additive Textmerge Noshow Pretext 1
<sub1>
<sub2>
<seq2><<m.lnSecventa>></seq2>
<Tip><<m.lcTip>></Tip>
<cuiP><<m.lcCodFiscal>></cuiP>
<denP><<m.lcDenumire>></denP>
<baza><<m.lcBaza>></baza>
<tva><<m.lcTVA>></tva>
<nrfact><<m.lcNrFact>></nrfact>
<ascunde_produs>0</ascunde_produs>
<ajutor><vtva><<m.lcAjutor>></vtva></ajutor>
</sub2>
<sub3>
<seq3>1</seq3>
<codPR/>
<baza/>
<tva/>
</sub3>
</sub1>
			Endtext
			If lnSecventa = 1
				Text To lcXDP Additive Textmerge Noshow Pretext 1
<box_submit>
<verif_unicit_L2/>
<verif_unicit_L3/>
</box_submit>
<box_sortare>
<ddl><<m.lcCodFiscal>>--<<m.lcDenumire>></ddl>
</box_sortare>
				Endtext
			Endif
		Endscan

		Text To lcXDP Additive Textmerge Noshow Pretext 1
</body3>
<butoane xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<calculatedCui><<goFirma.codfiscalfro>></calculatedCui>
<calculatedAnul><<Alltrim(Str(gnAn))>></calculatedAnul>
<calculatedlLuna><<Alltrim(Str(gnLuna)),2,[0])>></calculatedlLuna>
<nr_pagina></nr_pagina>
</form1>
</xfa:data>
</xfa:datasets>
<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
</xdp:xdp>
		Endtext

		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				open_default_app("http://static.anaf.ro/static/10/Anaf/Declaratii_R/394.html")
				amessagebox('Nu exista fisierul ' + m.lcFisierPDFOriginal + CRLF + ;
					  'Copiati pdf-ul inteligent D390 de pe website-ul ANAF in directorul ' + Justpath(m.lcFisierPDFOriginal) + CRLF + ;
					  'cu numele ' + Justfname(m.lcFisierPDFOriginal) + '.', 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)

	Endproc && do_genereaza_xdp_201312

	*===========================================
	Procedure do_genereaza_xdp_201201
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou


		lcSelect = Select()
		lcTip = This.TIP && L/T/S/A (Lunara/Trimestriala/Semestriala/Anuala)
		lnRectificativa = This.rectificativa && 0/1

		lnLIVB = This.livb
		lnLIVT = This.livt
		lnLIVNRF = This.livnrf

		lnACHB = This.achb
		lnACHT = This.acht
		lnACHNRF = This.achnrf

		lnLIVTIB = This.livtib
		lnLIVTIT = This.livtit
		lnLIVTINRF = This.livtinrf

		lnACHTIB = This.achtib
		lnACHTIT = This.achtit
		lnACHTINRF = This.achtinrf

		lnTotal = This.Total
		lnNrOperatori = This.NumarCoduriFiscale

		lcCodFiscalF = Alltrim(gofirma.codfiscalfro)
		lcLuna = Padl(Alltrim(Str(gnLuna)), 2, [0])
		lcAn = Alltrim(Str(gnAn))


		lcXDP = ""
		Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0"?>
<?xfa generator="XFA2_4" APIVersion="3.3.10270.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<form1>
<body1>
<HEADER xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<sub_per>
<an_r><<m.lcAn>></an_r>
<luna_r><<m.lcLuna>></luna_r>
<TIP_D394><<m.lcTip>></TIP_D394>
</sub_per>
<sub_dateDeIdentificare>
<cif><<m.lcCodFiscalF>></cif>
<den><<XmlSpecialCharacters(Alltrim(gofirma.antet))>></den>
<adresa><<ALLTRIM(goFirma.adresa)>></adresa>
<telefon><<ALLTRIM(goFirma.telefon)>></telefon>
<fax><<ALLTRIM(goFirma.fax)>></fax>
<mail><<ALLTRIM(LOWER(goFirma.email))>></mail>
<d_rec><<m.lnRectificativa>></d_rec>
</sub_dateDeIdentificare>
</body1>
<body11>
<sub_dateDeIdentificare>
<cifR/>
<denR/>
<adresaR/>
<telefonR/>
<faxR/>
<mailR/>
</sub_dateDeIdentificare>
</body11>
<body10>
<totalPlata_A></totalPlata_A>
<nrCui><<m.lnNrOperatori>></nrCui>
<bazaL><<m.lnLIVB>></bazaL>
<tvaL><<m.lnLIVT>></tvaL>
<bazaA><<m.lnACHB>></bazaA>
<tvaA><<m.lnACHT>></tvaA>
<bazaV><<m.lnLIVTIB>></bazaV>
<tvaV><<m.lnLIVTIT>></tvaV>
<bazaVc>0</bazaVc>
<tvaVc>0</tvaVc>
<bazaC><<m.lnACHTIB>></bazaC>
<tvaC><<m.lnACHTIT>></tvaC>
<bazaCc>0</bazaCc>
<tvaCc>0</tvaCc>
</body10>
<body12>
<sub_semnatura>
<nume_declar><<ALLTRIM(goFirma.nume_declarant)>></nume_declar>
<prenume_declar><<ALLTRIM(goFirma.prenume_declarant)>></prenume_declar>
<functie_declar><<ALLTRIM(goFirma.functie)>></functie_declar>
</sub_semnatura>
<sub_organFiscal>
<numeFisier/>
</sub_organFiscal>
</body12>
<body3>
		Endtext


		lnSecventa = 0
		Select crsD394
		Scan For baza <> 0 Or TVA <> 0
			lcCodFiscal = Alltrim(cod_fiscal)
			lcDenumire = Alltrim(Substr(XmlSpecialCharacters(denumire), 1, 180))
			lcTip = Alltrim(Upper(TIP))
			lnSecventa = lnSecventa + 1
			lcBaza = Alltrim(Str(baza, 20, 0))
			lcTVA = Alltrim(Str(TVA, 20, 0))
			lcAjutor = Alltrim(Str(baza + TVA, 20, 0))
			Text To lcXDP Additive Textmerge Noshow Pretext 1
<sub1>
<sub2>
<seq2><<m.lnSecventa>></seq2>
<Tip><<m.lcTip>></Tip>
<cuiP><<m.lcCodFiscal>></cuiP>
<denP><<m.lcDenumire>></denP>
<baza><<m.lcBaza>></baza>
<tva><<m.lcTVA>></tva>
<ascunde_produs>0</ascunde_produs>
<ajutor><vtva><<m.lcAjutor>></vtva></ajutor>
</sub2>
<sub3>
<seq3>1</seq3>
<codPR/>
<baza/>
<tva/>
</sub3>
</sub1>
			Endtext
			If lnSecventa = 1
				Text To lcXDP Additive Textmerge Noshow Pretext 1
<box_submit>
<verif_unicit_L2/>
<verif_unicit_L3/>
</box_submit>
<box_sortare>
<ddl><<m.lcCodFiscal>>--<<m.lcDenumire>></ddl>
</box_sortare>
				Endtext
			Endif
		Endscan

		Text To lcXDP Additive Textmerge Noshow Pretext 1
</body3>
<butoane xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<calculatedCui><<goFirma.codfiscalfro>></calculatedCui>
<calculatedAnul><<Alltrim(Str(gnAn))>></calculatedAnul>
<calculatedlLuna><<Alltrim(Str(gnLuna)),2,[0])>></calculatedlLuna>
<nr_pagina></nr_pagina>
</form1>
</xfa:data>
</xfa:datasets>
<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
</xdp:xdp>
		Endtext

		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				open_default_app("http://static.anaf.ro/static/10/Anaf/Declaratii_R/394.html")
				amessagebox('Nu exista fisierul ' + m.lcFisierPDFOriginal + CRLF + ;
					  'Copiati pdf-ul inteligent D390 de pe website-ul ANAF in directorul ' + Justpath(m.lcFisierPDFOriginal) + CRLF + ;
					  'cu numele ' + Justfname(m.lcFisierPDFOriginal) + '.', 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)

	Endproc && do_genereaza_xdp_201201

	*==========================================
	Procedure UpdatePDF
		Lparameters tcPDFFile
		*** tcPDFFile: calea completa a fisierului pdf D:\CONTAFIN\COMUNCONTAFIN\d390.pdf

		Local lcFile, lcPath, llOpen
		lcFile = Justfname(m.tcPDFFile)
		lcPath = Addbs(Justpath(m.tcPDFFile))
		llOpen = .F.

		Return DownloadSupportFile(m.lcFile, m.lcPath, m.llOpen) && proceduri_comune.prg

	Endproc && UpdatePDF

Enddefine && D394
*************************************************************************8

*===========================================
Procedure viz_decl100
	Parameters nDecl, tlTrim && 100/102 sau ambele (0); trimestriala

	*!*	Codurile randurilor cu sume: decl. 100:
	*!*	rd. 2 (106)
	*!*	rd. 6 (121)
	*!*	rd. 7 (602)
	*!*	rd. 12 (604)
	*!*	rd. 22 (612)
	*!*	rd.	24 (810)



	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec
	Local llTrim, lcValabil, loLogCursoare, ldDataScadenta
	loLogCursoare = Createobject("oLogCursoare")
	*!*		If Mod(gnLuna,3)=0
	*!*			llTrim = .T.
	*!*		Else
	*!*			llTrim=.F.
	*!*		Endif

	***---- folosesc nomenclatorul pt semestrul in care am deschis luna contabila, sau daca nu exista, caut ultimul nomenclator valabil
	If gnLuna <= 6
		lcValabil = '01' + Substr(Str(gnAn, 4, 0), 3, 2)
	Else
		lcValabil = '02' + Substr(Str(gnAn, 4, 0), 3, 2)
	Endif

	lcCursor = [crsTest]
	lcSql = [select count(*) as nr_rand from decl_100_102  where valabil = ] + lcValabil
	If !goExecutor.oExecuta(lcSql, lcCursor)
		Return
	Endif
	Select (lcCursor)
	lnNr_rand = nr_rand
	loLogCursoare.inchideCursor(lcCursor)

	If lnNr_rand = 0 && daca nu s-a gasit nomenclatorul pt semestrul in curs, se ia ultimul nomenclator
		lcCursor = [crsMax]
		lcSql = [	select substr(valabil,1,2)+substr(valabil,3,2)*12 as nr_luni, valabil from (select distinct valabil from decl_100_102)]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif
		Select Max(nr_luni) As nrLuni_max From (lcCursor) Into Cursor crsMax2
		Select crsMax2
		lnNrLuni_max = nrLuni_max
		Select valabil From (lcCursor) Where nr_luni = lnNrLuni_max Into Cursor crsValabil
		Select crsValabil
		lcValabil = valabil

		loLogCursoare.inchideCursor(lcCursor)
		loLogCursoare.inchideCursor([crsMax2])
		loLogCursoare.inchideCursor([crsValabil])
	Endif
	***----

	llTrim = tlTrim

	If llTrim
		Do Case
			Case Inlist(gnLuna, 1, 2, 3)
				pnLunaI = 1
				pnLunaF = 3
				pnLunaPrec = 12
				pnAnPrec = gnAn - 1
			Case Inlist(gnLuna, 4, 5, 6)
				pnLunaI = 4
				pnLunaF = 6
				pnLunaPrec = 3
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 7, 8, 9)
				pnLunaI = 7
				pnLunaF = 9
				pnLunaPrec = 6
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 10, 11, 12)
				pnLunaI = 10
				pnLunaF = 12
				pnLunaPrec = 9
				pnAnPrec = gnAn
		Endcase
	Else
		pnLunaI = gnLuna
		pnLunaF = gnLuna
		If gnLuna = 1
			pnLunaPrec = 12
			pnAnPrec = gnAn - 1
		Else
			pnLunaPrec = gnLuna - 1
			pnAnPrec = gnAn
		Endif
	Endif

	ldDataScadenta = Gomonth(Date(m.gnAn, m.pnLunaF, 25), 1)

	Private poDecl
	Store '' To poDecl

	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []


	lcSql = [select d.*, 00000000000 as datorat, 00000000000 as recuperat,  00000000000 as deductibil, 00000000000 as bonificatie, ] + ;
		[00000000000 as sponsorizari, 00000000000 as costamef, 00000000000 as reducere ] + ;
		[ from decl_100_102 d where valabil = '] + lcValabil + [' order by id_decl]

	lcCursor = [cDeclTemp]
	If !goExecutor.oExecuta(lcSql, lcCursor)
		Return
	Endif
	Select *, m.ldDataScadenta As datascad From cDeclTemp Into Cursor cDecl Readwrite
	Update cDecl Set nr_evidenta = ''
	Use In (Select('cDeclTemp'))

	***---------------------------------
	If !m.llTrim
		* Setare analitice rd. 14, 23, 25 - decl. 100

		Create Cursor crs446_447_604(Cont c(20))
		Insert Into crs446_447_604(Cont) Values ("<ALEGETI CONTUL>")
		Insert Into crs446_447_604(Cont) Values ("446")
		Insert Into crs446_447_604(Cont) Values ("447")

		Select crs446_447_604
		Locate For Cont = gcContD100_604

		lcCursorAcont = update_plAcont(m.gcContD100_604, gnAn) && updateserver.prg
		Select * From (lcCursorAcont) Into Cursor crsAcont_604 Readwrite
		Update crsAcont_604 Set denumire = "<ALEGETI ANALITICUL>" Where Alltrim(acont) = "-1"

		Select crsAcont_604 
		Locate For acont = m.gcAcontD100_604
		*-
		Create Cursor crs446_447_612 (Cont c(20))
		Insert Into crs446_447_612(Cont) Values ("<ALEGETI CONTUL>")
		Insert Into crs446_447_612(Cont) Values ("446")
		Insert Into crs446_447_612(Cont) Values ("447")

		Select crs446_447_612
		Locate For Cont = m.gcContD100_612

		lcCursorAcont = update_plAcont(gcContD100_612, gnAn) && updateserver.prg
		Select * From (lcCursorAcont) Into Cursor crsAcont_612 Readwrite
		Update crsAcont_612 Set denumire = "<ALEGETI ANALITICUL>" Where Alltrim(acont) = "-1"

		Select crsAcont_612
		Locate For acont = gcAcontD100_612
		*-
		Create Cursor crs446_447_628 (Cont c(20))
		Insert Into crs446_447_628(Cont) Values ("<ALEGETI CONTUL>")
		Insert Into crs446_447_628(Cont) Values ("446")
		Insert Into crs446_447_628(Cont) Values ("447")

		Select crs446_447_628
		Locate For Cont = gcContD100_628

		lcCursorAcont = update_plAcont(gcContD100_628, gnAn) && updateserver.prg
		Select * From (lcCursorAcont) Into Cursor crsAcont_628 Readwrite
		Update crsAcont_628 Set denumire = "<ALEGETI ANALITICUL>" Where Alltrim(acont) = "-1"

		Select crsAcont_628 
		Locate For acont = gcAcontD100_628

		*-
		Create Cursor crs446_447_810 (Cont c(20))
		Insert Into crs446_447_810(Cont) Values ("<ALEGETI CONTUL>")
		Insert Into crs446_447_810(Cont) Values ("446")
		Insert Into crs446_447_810(Cont) Values ("447")

		Select crs446_447_810
		Locate For Cont = gcContD100_810

		lcCursorAcont = update_plAcont(gcContD100_810, gnAn) && updateserver.prg
		Select * From (lcCursorAcont) Into Cursor crsAcont_810 Readwrite
		Update crsAcont_810 Set denumire = "<ALEGETI ANALITICUL>" Where Alltrim(acont) = "-1"

		Select crsAcont_810 
		Locate For acont = gcAcontD100_810
		
		losc = Createobject("frm_decl100_102_setacont")
		losc.Show(1)

	Endif && !llTrim


	*!*		IF buton = 2
	*!*			RETURN
	*!*		ENDIF
	***---------------------------------

	If !llTrim


		*** Rand 12 (604)Imp.pe ven.-dividende distribuite la PF
		lcSql = [SELECT NVL(rulcred,0) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = ?gcCONTDECL100_RD14 ] + ;
			[ and acont = ?gcACONTDECL100_RD14 ] + ;
			[ and AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala
		lcCursor = [crsDiv]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln12 = rulcred

		Select cDecl
		Locate For Alltrim(cod_imp) = '604' && '14' - (604)Imp.pe ven.-dividende distribuite la PF
		If Found()
			Replace datorat With m.ln12
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------

		*** Rand 20 (612)Imp.pe ven.obt.din Romania de nerezidenti - PF
		lcSql = [SELECT NVL(rulcred,0) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = ?gcCONTDECL100_RD23 ] + ;
			[ and acont = ?gcACONTDECL100_RD23 ] + ;
			[ and AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala
		lcCursor = [crsRed]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln20 = rulcred

		Select cDecl
		Locate For Alltrim(cod_imp) = '612' && '23' - (612)Imp.pe ven.obt.din Romania de nerezidenti - PF
		If Found()
			Replace datorat With m.ln20
		Endif
		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------

		*** Rand 22 (810)Varsam.de la PJ pt.pers.cu handicap neincadrate
		lcSql = [SELECT cont, acont, rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = ?gcCONTDECL100_RD25 ] + ;
			[ and acont = ?gcACONTDECL100_RD25 ] + ;
			[ and AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

		lcCursor = [crsHand]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln22 = rulcred

		Select cDecl
		Locate For Alltrim(cod_imp) = '810' && '25' - (810)Varsam.de la PJ pt.pers.cu handicap neincadrate
		If Found()
			Replace datorat With m.ln22
		Endif

		loLogCursoare.inchideCursor(lcCursor)
	Endif && nDecl = 100
	***-----------------------------
	If m.llTrim

		*** Rand 02 (103)Imp.pe profit datorat de PJ romane
		lcSql = [SELECT sum(ruldeb) as ruldeb ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where cont = '691' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
		lcCursor = [crs691]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln2 = ruldeb
		Use In (Select(lcCursor))

		Select cDecl
		Locate For Alltrim(cod_imp) = '103'
		If Found()
			Replace datorat With ln2
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------

		*!*	modificare	v 2.5.32
		*** Rand 06 (121)Imp.pe ven. microintreprinderilor
		lcSql = [SELECT sum(ruldeb) as ruldeb ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where cont = '698' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
		lcCursor = [crs698]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif
		Select (lcCursor)
		ln2 = ruldeb
		Use In (Select(lcCursor))

		Select cDecl
		Locate For Alltrim(cod_imp) = '121'
		If Found()
			Replace datorat With ln2
		Endif
		loLogCursoare.inchideCursor(lcCursor)
		*!*	modificare	v 2.5.32	^

	Endif && nDecl = 100 trim



	Select cDecl
	Do Case
		Case m.gnAn * 12 + m.gnLuna >= 2024 * 12 + 1
			lodt = Createobject('frm_decl100_2024', nDecl, llTrim)
		Case m.gnAn * 12 + m.gnLuna >= 2020 * 12 + 3
			lodt = Createobject('frm_decl100_2020', nDecl, llTrim)
		Otherwise
			lodt = Createobject('frm_decl100_2016', nDecl, llTrim)
	Endcase
	lodt.Show(1)
Endproc && viz_decl100

*===========================================
Procedure viz_decl100_102
	Parameters nDecl, tlTrim && 100/102 sau ambele (0); trimestriala

	*!*	Codurile randurilor cu sume: decl. 100:
	*!*	rd. 2 (106)
	*!*	rd. 6 (121)
	*!*	rd. 7 (602)
	*!*	rd. 12 (604)
	*!*	rd. 22 (612)
	*!*	rd.	24 (810)

	*!*	Decl. 102: 1 (412), 4(411), 5(416), 6(432), 13(431), 17(439), 18(422), 19(421), 20(423)


	Private pnLunaI, pnLunaF, pnLunaPrec, pnAnPrec
	Local llTrim, lcValabil, loLogCursoare, ldDataScadenta
	loLogCursoare = Createobject("oLogCursoare")
	*!*		If Mod(gnLuna,3)=0
	*!*			llTrim = .T.
	*!*		Else
	*!*			llTrim=.F.
	*!*		Endif

	***---- folosesc nomenclatorul pt semestrul in care am deschis luna contabila, sau daca nu exista, caut ultimul nomenclator valabil
	If gnLuna <= 6
		lcValabil = '01' + Substr(Str(gnAn, 4, 0), 3, 2)
	Else
		lcValabil = '02' + Substr(Str(gnAn, 4, 0), 3, 2)
	Endif

	lcCursor = [crsTest]
	lcSql = [select count(*) as nr_rand from decl_100_102  where valabil = ] + lcValabil
	If !goExecutor.oExecuta(lcSql, lcCursor)
		Return
	Endif
	Select (lcCursor)
	lnNr_rand = nr_rand
	loLogCursoare.inchideCursor(lcCursor)

	If lnNr_rand = 0 && daca nu s-a gasit nomenclatorul pt semestrul in curs, se ia ultimul nomenclator
		lcCursor = [crsMax]
		lcSql = [	select substr(valabil,1,2)+substr(valabil,3,2)*12 as nr_luni, valabil from (select distinct valabil from decl_100_102)]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif
		Select Max(nr_luni) As nrLuni_max From (lcCursor) Into Cursor crsMax2
		Select crsMax2
		lnNrLuni_max = nrLuni_max
		Select valabil From (lcCursor) Where nr_luni = lnNrLuni_max Into Cursor crsValabil
		Select crsValabil
		lcValabil = valabil

		loLogCursoare.inchideCursor(lcCursor)
		loLogCursoare.inchideCursor([crsMax2])
		loLogCursoare.inchideCursor([crsValabil])
	Endif
	***----

	llTrim = tlTrim

	If llTrim
		Do Case
			Case Inlist(gnLuna, 1, 2, 3)
				pnLunaI = 1
				pnLunaF = 3
				pnLunaPrec = 12
				pnAnPrec = gnAn - 1
			Case Inlist(gnLuna, 4, 5, 6)
				pnLunaI = 4
				pnLunaF = 6
				pnLunaPrec = 3
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 7, 8, 9)
				pnLunaI = 7
				pnLunaF = 9
				pnLunaPrec = 6
				pnAnPrec = gnAn
			Case Inlist(gnLuna, 10, 11, 12)
				pnLunaI = 10
				pnLunaF = 12
				pnLunaPrec = 9
				pnAnPrec = gnAn
		Endcase
	Else
		pnLunaI = gnLuna
		pnLunaF = gnLuna
		If gnLuna = 1
			pnLunaPrec = 12
			pnAnPrec = gnAn - 1
		Else
			pnLunaPrec = gnLuna - 1
			pnAnPrec = gnAn
		Endif
	Endif

	ldDataScadenta = Gomonth(Date(m.gnAn, m.pnLunaF, 25), 1)

	Private poDecl
	Store '' To poDecl

	Local lcSchema1, lcSelect1, lcOrder1, lcFiltru1, lcFiltruOriginal, llAfiseaza
	lcSchema1 = []

	Do Case
		Case nDecl = 100
			lcSql = [select d.*, 00000000000 as datorat, 00000000000 as recuperat ] + ;
				[ from decl_100_102 d where id_decl <= 32 and valabil = '] + lcValabil + [' order by id_decl]
		Case nDecl = 102
			lcSql = [select d.*, 00000000000 as datorat, 00000000000 as recuperat ] + ;
				[ from decl_100_102 d where id_decl > 32 and valabil = '] + lcValabil + [' order by id_decl]
		Otherwise
			lcSql = [select d.*, 00000000000 as datorat, 00000000000 as recuperat ] + ;
				[ from decl_100_102 d where valabil = '] + lcValabil + [' order by id_decl]
	Endcase
	lcCursor = [cDeclTemp]
	If !goExecutor.oExecuta(lcSql, lcCursor)
		Return
	Endif
	Select *, m.ldDataScadenta As datascad From cDeclTemp Into Cursor cDecl Readwrite
	Update cDecl Set nr_evidenta = ''
	Use In (Select('cDeclTemp'))

	***---------------------------------
	If (m.nDecl = 100 Or Empty(m.nDecl)) And !m.llTrim
		* Setare analitice rd. 14, 23, 25 - decl. 100

		Create Cursor crs446_447 (Cont c(20))
		Insert Into crs446_447 (Cont) Values ("<ALEGETI CONTUL>")
		Insert Into crs446_447 (Cont) Values ("446")
		Insert Into crs446_447 (Cont) Values ("447")

		Select crs446_447
		Locate For Cont = gcContDecl100_rd14

		lcCursorAcont = update_plAcont(gcContDecl100_rd14, gnAn) && updateserver.prg
		Select * From (lcCursorAcont) Into Cursor crsAcont Readwrite
		Update crsAcont Set denumire = "<ALEGETI ANALITICUL>" Where Alltrim(acont) = "-1"

		Select crsAcont
		Locate For acont = gcAcontDecl100_rd14
		*-
		Create Cursor crs446_447_2 (Cont c(20))
		Insert Into crs446_447_2 (Cont) Values ("<ALEGETI CONTUL>")
		Insert Into crs446_447_2 (Cont) Values ("446")
		Insert Into crs446_447_2 (Cont) Values ("447")

		Select crs446_447_2
		Locate For Cont = gcContDecl100_rd23

		lcCursorAcont = update_plAcont(gcContDecl100_rd23, gnAn) && updateserver.prg
		Select * From (lcCursorAcont) Into Cursor crsAcont_2 Readwrite
		Update crsAcont_2 Set denumire = "<ALEGETI ANALITICUL>" Where Alltrim(acont) = "-1"

		Select crsAcont_2
		Locate For acont = gcAcontDecl100_rd23
		*-
		Create Cursor crs446_447_3 (Cont c(20))
		Insert Into crs446_447_3 (Cont) Values ("<ALEGETI CONTUL>")
		Insert Into crs446_447_3 (Cont) Values ("446")
		Insert Into crs446_447_3 (Cont) Values ("447")

		Select crs446_447_3
		Locate For Cont = gcContDecl100_rd25

		lcCursorAcont = update_plAcont(gcContDecl100_rd25, gnAn) && updateserver.prg
		Select * From (lcCursorAcont) Into Cursor crsAcont_3 Readwrite
		Update crsAcont_3 Set denumire = "<ALEGETI ANALITICUL>" Where Alltrim(acont) = "-1"

		Select crsAcont_3
		Locate For acont = gcAcontDecl100_rd25

		losc = Createobject("frm_decl100_102_setacont")
		losc.Show(1)

	Endif && !llTrim


	*!*		IF buton = 2
	*!*			RETURN
	*!*		ENDIF
	***---------------------------------

	If (nDecl = 100 Or Empty(nDecl)) And !llTrim

		*!*			IF .F.
		*!*				*** ct.444 IMPOZIT PE VENIT DE NATURA SALARIILOR
		*!*				lcSql = [SELECT rulcred ] +;
		*!*					[ from ]+Iif(glEMama,[vbalmama],[vbal]) +;
		*!*					[ where cont = '444' ]+;
		*!*					[ and AN = ?gnAn AND LUNA= ?gnLuna] + gcCondSucursala
		*!*				lcCursor = [crs444] && TVA de recuperat
		*!*				If !goExecutor.oExecuta(lcSql,lcCursor)
		*!*					Return
		*!*				Endif
		*!*				Select (lcCursor)
		*!*				ln9 = rulcred
		*!*				Select cDecl
		*!*				Locate For Alltrim(Rand) = '07' And id_decl <=32 && '09' (602) - Imp.pe ven.-salarii  - rulcred 444
		*!*				If Found()
		*!*					Replace datorat With ln9
		*!*				Endif

		*!*				loLogCursoare.inchideCursor(lcCursor)
		*!*			ENDIF && .F.
		***-----------------------------

		*** Rand 12 (604)Imp.pe ven.-dividende distribuite la PF
		lcSql = [SELECT NVL(rulcred,0) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = ?gcCONTDECL100_RD14 ] + ;
			[ and acont = ?gcACONTDECL100_RD14 ] + ;
			[ and AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala
		lcCursor = [crsDiv]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln12 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '12' And id_decl <= 32 && '14' - (604)Imp.pe ven.-dividende distribuite la PF
		If Found()
			Replace datorat With m.ln12
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------

		*** Rand 20 (612)Imp.pe ven.obt.din Romania de nerezidenti - PF
		lcSql = [SELECT NVL(rulcred,0) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = ?gcCONTDECL100_RD23 ] + ;
			[ and acont = ?gcACONTDECL100_RD23 ] + ;
			[ and AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala
		lcCursor = [crsRed]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln20 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '20' And id_decl <= 32 && '23' - (612)Imp.pe ven.obt.din Romania de nerezidenti - PF
		If Found()
			Replace datorat With m.ln20
		Endif
		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------

		*** Rand 22 (810)Varsam.de la PJ pt.pers.cu handicap neincadrate
		lcSql = [SELECT cont, acont, rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = ?gcCONTDECL100_RD25 ] + ;
			[ and acont = ?gcACONTDECL100_RD25 ] + ;
			[ and AN = ?gnAn AND LUNA= ?gnLuna ] + gcCondSucursala

		lcCursor = [crsHand]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln22 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '22' And id_decl <= 32 && '25' - (810)Varsam.de la PJ pt.pers.cu handicap neincadrate
		If Found()
			Replace datorat With m.ln22
		Endif

		loLogCursoare.inchideCursor(lcCursor)
	Endif && nDecl = 100
	***-----------------------------
	If (nDecl = 100 Or Empty(nDecl)) And llTrim

		*** Rand 02 (103)Imp.pe profit datorat de PJ romane
		lcSql = [SELECT sum(ruldeb) as ruldeb ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where cont = '691' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
		lcCursor = [crs691]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln2 = ruldeb
		Use In (Select(lcCursor))

		Select cDecl
		Locate For Alltrim(Rand) = '02' And id_decl <= 32
		If Found()
			Replace datorat With ln2
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------

		*!*	modificare	v 2.5.32
		*** Rand 06 (121)Imp.pe ven. microintreprinderilor
		lcSql = [SELECT sum(ruldeb) as ruldeb ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where cont = '698' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
		lcCursor = [crs698]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif
		Select (lcCursor)
		ln2 = ruldeb
		Use In (Select(lcCursor))

		Select cDecl
		Locate For Alltrim(Rand) = '06' And id_decl <= 32
		If Found()
			Replace datorat With ln2
		Endif
		loLogCursoare.inchideCursor(lcCursor)
		*!*	modificare	v 2.5.32	^
		***-----------------------------
		*!*			IF .F.
		*!*				*** ct.444 IMPOZIT PE VENIT DE NATURA SALARIILOR
		*!*				lcSql = [SELECT sum(rulcred) as rulcred ] +;
		*!*					[ from ]+Iif(glEMama,[vbalmama],[vbal]) +;
		*!*					[ where cont = '444' ]+;
		*!*					[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
		*!*				lcCursor = [crs444] && TVA de recuperat
		*!*				If !goExecutor.oExecuta(lcSql,lcCursor)
		*!*					Return
		*!*				Endif
		*!*				SELECT (lcCursor)
		*!*				ln9 = rulcred
		*!*				Select cDecl
		*!*				Locate For Alltrim(Rand) = '07' And id_decl <=33 && '09' (602)
		*!*				If Found()
		*!*					Replace datorat With ln9
		*!*				Endif
		*!*				loLogCursoare.inchideCursor(lcCursor)
		*!*			ENDIF && .F.
		***-----------------------------
	Endif && nDecl = 100 trim

	If (nDecl = 102 Or Empty(nDecl))

		&& 01 CAS PERSONAL - rulaj creditor cont 4312  pe perioada celor 3 luni ale trimestrulu la suma datorada
		lcSql = [SELECT sum(rulcred) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where cont = '4312' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala

		&& [ and AN = ?gnAn AND (LUNA=?pnLuna1 or LUNA=?pnLuna2 or LUNA=?pnLuna3)] + gcCondSucursala
		lcCursor = [crs4312]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln1 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '01' And id_decl >= 33 && (412)
		If Found()
			Replace datorat With ln1
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------

		&& 04 - rulaj creditor cont 4311 analitic 1  pe perioada celor 3 luni ale trimestrului la suma datorata

		lcSql = [SELECT sum(rulcred) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = '4311' and acont = '1' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala

		lcCursor = [crs4]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln04 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '04' And id_decl >= 33 &&
		If Found()
			Replace datorat With ln04
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------
		&& 05 - rulaj creditor cont 4311 analitic 2  pe perioada celor 3 luni ale trimestrulu la suma datorata
		&& si la suma deductibila se trece suma inregistrarilor din luna de la inregistrarile 4311.2=423/4312/444/4372/4314
		&& adunata cu soldul debitor al contului 4311.2 al lunii precedente trimestrului.

		lcSql = [SELECT sum(rulcred) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = '4311' and acont = '2' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala
		lcCursor = [crs5]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln05 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '05' And id_decl >= 33
		If Found()
			Replace datorat With ln05
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***---
		lcSql = [ select nvl(sum(suma),0) as suma ] + ;
			[from act ] + ;
			[where an=?gnAn ] + ;
			[and luna=?gnLuna ] + ;
			[and sters=0 ] + ;
			[and scd='4311' and ascd='2' and scc in  ('423','4312','444','4372','4314')] + gcCondSucursala

		lcCursor = [crs5_r]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln05_r = suma
		loLogCursoare.inchideCursor(lcCursor)

		lcSql = [SELECT solddeb ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = '4311' and acont='2' ] + ;
			[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala
		lcCursor = [crsB]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln05_b = solddeb
		ln05_r = ln05_r + ln05_b

		If ln05_r > ln05
			ln05_r = ln05
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '05' And id_decl >= 33
		If Found()
			Replace recuperat With ln05_r
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------

		&& 06 -rulaj creditor cont 4314  pe perioada celor 3 luni ale trimestrului la suma datorada
		lcSql = [SELECT sum(rulcred) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where cont = '4314' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF] + gcCondSucursala

		lcCursor = [crs4314]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln6 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '06' And id_decl >= 33
		If Found()
			Replace datorat With ln6
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------
		&& 13 - rulaj creditor cont 4313 analitic1  pe perioada celor 3 luni ale trimestrului la suma datorada


		lcSql = [SELECT sum(rulcred) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = '4313' and acont = '1' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala

		lcCursor = [crs13]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln13 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '13' And id_decl >= 33
		If Found()
			Replace datorat With ln13
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------
		&& 17 - rulaj creditor cont 4313 analitic 2 pe perioada celor 3 luni ale trimestrului la suma datorata
		&& si la suma deductibila se trece suma inregistrarilor din luna de la inregistrarile 4313.2=423/4312/444/4372/4314
		&& adunata cu soldul debitor al contului 4313.2 al lunii precedente trimestrului



		lcSql = [SELECT sum(rulcred) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = '4313' and acont = '2' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala

		lcCursor = [crs17]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln17 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '17' And id_decl >= 33
		If Found()
			Replace datorat With ln17
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***---
		lcSql = [ select nvl(sum(suma),0) as suma ] + ;
			[from act ] + ;
			[where an=?gnAn ] + ;
			[and luna=?gnLuna ] + ;
			[and sters=0 ] + ;
			[and scd='4313' and ascd='2' and scc in  ('423','4312','444','4372','4314')] + gcCondSucursala

		lcCursor = [crs17_r]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln17_r = suma
		loLogCursoare.inchideCursor(lcCursor)

		lcSql = [SELECT solddeb ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = '4313' and acont='2' ] + ;
			[ and AN = ?pnAnPrec AND LUNA= ?pnLunaPrec] + gcCondSucursala

		lcCursor = [crsB]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln17_b = solddeb
		ln17_r = ln17_r + ln17_b

		If ln17_r > ln17
			ln17_r = ln17
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '17' And id_decl >= 33
		If Found()
			Replace recuperat With ln17_r
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------
		&& 18 - rulaj creditor cont 4372 pe perioada celor 3 luni ale trimestrului la suma datorada

		lcSql = [SELECT sum(rulcred) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where cont = '4372' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF] + gcCondSucursala
		lcCursor = [crs4372]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln18 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '18' And id_decl >= 33
		If Found()
			Replace datorat With ln18
		Endif

		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------
		&& 19 - rulaj creditor cont 4371 analitic 1  pe perioada celor 3 luni ale trimestrului la suma datorada

		lcSql = [SELECT sum(rulcred) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = '4371' and acont = '1' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF ] + gcCondSucursala

		lcCursor = [crs19]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln19 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '19' And id_decl >= 33
		If Found()
			Replace datorat With ln19
		Endif
		loLogCursoare.inchideCursor(lcCursor)
		***-----------------------------
		&& 20 - rulaj creditor cont 4371 analitic 2  pe perioada celor 3 luni ale trimestrului la suma datorada

		lcSql = [SELECT sum(rulcred) as rulcred ] + ;
			[ from ] + Iif(glEMama, [vbalanamama], [vbalana]) + ;
			[ where cont = '4371' and acont = '2' ] + ;
			[ and an*12+luna between ?gnAn*12+?pnLunaI and ?gnAn*12+?pnLunaF] + gcCondSucursala

		lcCursor = [crs20]
		If !goExecutor.oExecuta(lcSql, lcCursor)
			Return
		Endif

		Select (lcCursor)
		ln20 = rulcred

		Select cDecl
		Locate For Alltrim(Rand) = '20' And id_decl >= 33
		If Found()
			Replace datorat With ln20
		Endif
		loLogCursoare.inchideCursor(lcCursor)
	Endif && nDecl = 102 trim



	Select cDecl
	If m.gnAn >= 2016
		lodt = Createobject('frm_decl100_2016', nDecl, llTrim)
	Else
		lodt = Createobject('frm_decl100_102', nDecl, llTrim)
	Endif
	If nDecl = 100
		lodt.lb_titlu_alb_b121.Caption = "100 DECLARATIE PRIVIND OBLIGATIILE DE PLATA LA  BS"
	Endif
	If nDecl = 102
		lodt.lb_titlu_alb_b121.Caption = "102 DECLARATIE PRIVIND OBLIGATIILE DE PLATA LA  BASFS"
	Endif
	lodt.Show(1)
Endproc && viz_decl100_102

*==========================================
Procedure export_decl
	Parameters tcTabel, tnDecl, tlTrim


	Local lcSir, lcDirTabele, lcFisUnitate, lcCodFiscal, lcDirDecl, lcFile, llTrim, llAreAngajati
	lcSir = ''
	llTrim = tlTrim
	llAreAngajati = .F.


	gcDecl_caleExport = "C:\Program Files\Declaratii fiscale " + Alltrim(Str(gnAn))

	If !Directory(gcDecl_caleExport)
		gcDecl_caleExport = Getdir("C:", "Directorul Declaratii fiscale", "Declaratii fiscale")
	Endif
	If Empty(gcDecl_caleExport)
		Return
	Endif

	lcDirTabele = Addbs(Alltrim(gcDecl_caleExport)) + Alltrim(gofirma.firma) + "\"
	lcFisUnitate = lcDirTabele + "Unitate.dbf"
	If !File(lcFisUnitate)
		amessage("Nu ati salvat Date de identificare unitate!", 0 + 48, _Screen.Caption)
		Return
	Endif

	Use (lcFisUnitate) In 0 Alias unitate
	Select unitate
	lcCodFiscal = Alltrim(Str(codi))
	Use In unitate

	lcDirDecl = Addbs(Alltrim(gcDecl_caleExport)) + lcCodFiscal

	***---
	&& Nr sal

	Private pnLunaI, pnNrMedSal
	Store 0 To pnLunaI, pnNrMedSal
	If llTrim
		Do Case
			Case Inlist(gnLuna, 1, 2, 3)
				pnLunaI = 1
			Case Inlist(gnLuna, 4, 5, 6)
				pnLunaI = 4
			Case Inlist(gnLuna, 7, 8, 9)
				pnLunaI = 7
			Case Inlist(gnLuna, 10, 11, 12)
				pnLunaI = 10
		Endcase
		lcSql = [begin PACK_MG_EVOLUTIE.nr_med_salariati_per(?pnLunaI, ?gnAn, ?gnLuna, ?gnAn, ?@pnNrMedSal); end;]
	Else
		lcSql = [begin PACK_MG_EVOLUTIE.nr_med_salariati_per(?gnLuna, ?gnAn, ?gnLuna, ?gnAn, ?@pnNrMedSal); end;]
	Endif

	lnSucces = goExecutor.oExecute(lcSql)

	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
		Return
	Endif

	If pnNrMedSal > 0
		llAreAngajati = .T.
	Endif

	***---

	*** creez unitate.txt
	Set Textmerge To
	lcFile = lcDirTabele + "adresa.txt"
	Set Textmerge On To (lcFile) Noshow

	Use (lcFisUnitate) In 0 Alias unitate
	Select unitate
	Go Top
	lcSir = "##,#" + Alltrim(Str(codi)) + "#,#" + ;
		Alltrim(deni) + "#,#" + ;
		Alltrim(deni) + "#,#" + ;
		Alltrim(adr) + "#,#" + ;
		Alltrim(telf) + "#,#" + ;
		Alltrim(banca) + "#,#" + ;
		Alltrim(Cont) + "#,#" + ;
		"#,##,##,#" + ;
		Alltrim(intocmitf) + "#,#" + ;
		Alltrim(calIntoc) + "#," + ;
		Alltrim(Str(caen))

\\<<lcSir>>
	Set Textmerge To
	Wait Window "S-a exportat adresa.txt..." Timeout 1


	*** creez Dec100T.txt
	Set Textmerge To
	lcFile = lcDirTabele + "\Dec100T.txt"
	Set Textmerge On To (lcFile) Noshow

	lcSir = ''

	* lcSir = lcSir + '300,,' + PADL(gnLuna,2,'0') + ',' +  ALLTRIM(STR(gnAn,4,0)) + ',,,'
	lcSir = lcCodFiscal + ',' + Padl(gnLuna, 2, '0') + ',' +  Alltrim(Str(gnAn, 4, 0)) + ','

	Select (tcTabel)
	Scan For(!Empty(Nvl(datorat, 0)) Or !Empty(Nvl(recuperat, 0))) Or (llAreAngajati And id_decl <= 32 And Alltrim(Rand) = '06')  Or (llAreAngajati And id_decl > 33 And Inlist(Alltrim(Rand), '18', '19', '20'))

		If id_decl <= 32 && decl. 100
			lcSir = lcSir + ;
				Iif(Nvl(inactivD, 0) <> 1, 'BS,' + Substr(denumire, 2, 3) + ',' + Alltrim(Str(datorat, 15, 0)) + [,,,,250908,], [])
		Endif
		If id_decl > 33 && decl. 102
			lcSir = lcSir + ;
				Iif(Nvl(inactivD, 0) <> 1, 'CO,' + Substr(denumire, 2, 3) + ',' + Alltrim(Str(datorat, 15, 0)) + [,] + ;
				  Iif(!Empty(Nvl(recuperat, 0)), Iif(Nvl(inactivR, 0) <> 1, Alltrim(Str(recuperat, 15, 0)), []), []) + ;
				  [,,,250908,], [])

		Endif

	Endscan

	Select unitate
	* lcSir = lcSir + ALLTRIM(STR(caen))+',#N#,#'+ALLTRIM(banca)+'#,#'+ALLTRIM(cont) + '#'
	* lcSir = lcSir + Alltrim(Str(caen))+',#N#'


	Use In unitate


	*!*		IF !EMPTY(lcSir)
	*!*			lcSir = LEFT(lcSir,LEN(lcSir)-1)
	*!*		ENDIF
	lcSir = lcSir + Chr(13) + Chr(10)


\\<<lcSir>>
	Set Textmerge To
	Wait Window "S-a exportat Dec100T.txt..." Timeout 1

	lcA = lcDirTabele + "\" + "adresa.txt"
	lcD = lcDirTabele + "\" + "dec100T.txt"

	If !Directory(lcDirDecl)
		Md (lcDirDecl)
	Endif
	lcFisAdresa = lcDirDecl + "\adresa.txt"
	lcfisDec100T = lcDirDecl + "\dec100T.txt"

	Copy File (lcA) To (lcFisAdresa)
	Copy File (lcD) To (lcfisDec100T)

	amessagebox([Fisierele au fost exportate in directorul ] + Alltrim(Addbs(Justpath(lcfisDec100T))) + Chr(13) + Chr(10) + ;
		  [Intrati in programul de Declaratii Fiscale si selectati Declaratia 100], 0 + 64, 'Decl. 100')
Endproc && export_decl

*********************************************************
*** DECLARATIA 390 RECAPITULATIVA PRIVIND LIVRARILE / ACHIZITIILE / PRESTARILE INTRACOMUNITARE
*********************************************************
Procedure viz_decl390
	loD390 = Createobject("D390")
	loD390.do_executa()
Endproc


Define Class D390 As Custom
	nTotalAB = 0
	nTotalAS = 0
	nTotalLB = 0
	nTotalLS = 0
	nTotalLT = 0
	nTotal = 0
	nNrOperatori = 0
	nNrPagini = 0

	*==========================================
	Procedure do_genereaza_txt_201902
		Lparameters tcFisierTXT

		Local lcAn, lcCodFiscal, lcCodFiscalF, lcDenumire, lcFisier, lcLuna, lcSelect, lcSuma, lcTara, lcTip
		Local lnNrOperatori, lnNrPagini, lnTotal, lnTotalAB, lnTotalAS, lnTotalLB, lnTotalLS, lnTotalLT
		Local lcDenumire, lcAdresa, lcTelefon, lcEmail
		lcSelect = Select()

		lnTotalAB = This.nTotalAB
		lnTotalAS = This.nTotalAS
		lnTotalLB = This.nTotalLB
		lnTotalLS = This.nTotalLS
		lnTotalLT = This.nTotalLT
		lnTotal = This.nTotal
		lnNrPagini = This.nNrPagini
		lnNrOperatori = This.nNrOperatori
		lcCodFiscalF = Alltrim(gofirma.codfiscalfro)
		lcLuna = Padl(Alltrim(Str(gnLuna)), 2, [0])
		lcAn = Alltrim(Str(gnAn))
		lcFisier = ""
		lcDenumire = XmlSpecialCharacters(RemoveCharacters(Alltrim(gofirma.antet), .T.))
		lcAdresa = XmlSpecialCharacters(RemoveCharacters(Alltrim(gofirma.adresa), .T.))
		lcTelefon = Alltrim(gofirma.telefon)
		lcEmail = Alltrim(Lower(gofirma.email))
		Set Textmerge On  To Memvar lcFisier Noshow
		\390,<<m.lcLuna>>,<<m.lcAn>>,<<m.lcCodFiscalF>>,#<<m.lcDenumire>>#,#<<m.lcAdresa>>#,#<<m.lcTelefon>>#,#<<m.lcEmail>>#

		Select crsD390
		Scan
			lcTara = Substr(cod_fiscal, 1, 2)
			If Isalpha(Left(lcTara, 1))
				If !Isalpha(Substr(lcTara, 2, 1))
					lcTara = Left(lcTara, 1)
				Endif
			Else
				lcTara = ""
			Endif
			lcCodFiscal = Alltrim(Substr(cod_fiscal, Len(lcTara) + 1))
			lcDenumire = Alltrim(Substr(denumire, 1, 180))
			lcTip = Alltrim(Upper(TIP))
			lcSuma = Alltrim(Str(suma))

			\#<<m.lcTip>>#,#<<m.lcTara>>#,#<<m.lcCodFiscal>>#,#<<m.lcDenumire>>#,<<m.lcSuma>>
		Endscan
		Set Textmerge To

		Wait Window "Se exporta datele in format TXT" Nowait
		Strtofile(lcFisier, tcFisierTXT)
	Endproc && do_genereaza_txt_201902

	*==========================================
	Procedure do_genereaza_txt
		Lparameters tcFisierTXT

		Local lcAn, lcCodFiscal, lcCodFiscalF, lcDenumire, lcFisier, lcLuna, lcSelect, lcSuma, lcTara, lcTip
		Local lnNrOperatori, lnNrPagini, lnTotal, lnTotalAB, lnTotalAS, lnTotalLB, lnTotalLS, lnTotalLT
		lcSelect = Select()

		lnTotalAB = This.nTotalAB
		lnTotalAS = This.nTotalAS
		lnTotalLB = This.nTotalLB
		lnTotalLS = This.nTotalLS
		lnTotalLT = This.nTotalLT
		lnTotal = This.nTotal
		lnNrPagini = This.nNrPagini
		lnNrOperatori = This.nNrOperatori
		lcCodFiscalF = Alltrim(gofirma.codfiscalfro)
		lcLuna = Padl(Alltrim(Str(gnLuna)), 2, [0])
		lcAn = Alltrim(Str(gnAn))
		lcFisier = ""

		Set Textmerge On  To Memvar lcFisier Noshow
		\390,<<m.lcCodFiscalF>>,<<m.lcLuna>>,<<m.lcAn>>,##,<<lnNrPagini>>,<<lnNrOperatori>>,<<lnTotalLB>>,<<lnTotalAB>>,<<lnTotalLT>>,<<lnTotalLS>>,<<lnTotalAS>>,<<lnTotal>>

		Select crsD390
		Scan
			lcTara = Substr(cod_fiscal, 1, 2)
			If Isalpha(Left(lcTara, 1))
				If !Isalpha(Substr(lcTara, 2, 1))
					lcTara = Left(lcTara, 1)
				Endif
			Else
				lcTara = ""
			Endif
			lcCodFiscal = Alltrim(Substr(cod_fiscal, Len(lcTara) + 1))
			lcDenumire = Alltrim(Substr(denumire, 1, 180))
			lcTip = Alltrim(Upper(TIP))
			lcSuma = Alltrim(Str(suma))

			\#<<m.lcTara>>#,#<<m.lcCodFiscal>>#,#<<m.lcDenumire>>#,#<<m.lcTip>>#,<<m.lcSuma>>
		Endscan
		Set Textmerge To

		Wait Window "Se exporta datele in format TXT" Nowait
		Strtofile(lcFisier, tcFisierTXT)
	Endproc && do_genereaza_txt

	*==========================================
	Procedure do_genereaza_xdp_201902
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou
		Local lcAn, lcCodFiscal, lcCodFiscalF, lcDenumire, lcLuna, lcSelect, lcSuma, lcTara, lcTip, lcXDP
		Local lnNrOperatori, lnNrPagini, lnSecventa, lnTotal, lnTotalAB, lnTotalAS, lnTotalLB, lnTotalLS, lnTotalLT

		lcSelect = Select()

		lnTotalAB = This.nTotalAB
		lnTotalAS = This.nTotalAS
		lnTotalLB = This.nTotalLB
		lnTotalLS = This.nTotalLS
		lnTotalLT = This.nTotalLT
		lnTotal = This.nTotal
		lnNrPagini = This.nNrPagini
		lnNrOperatori = This.nNrOperatori
		lcCodFiscalF = Alltrim(gofirma.codfiscalfro)
		lcLuna = Padl(Alltrim(Str(gnLuna)), 2, [0])
		lcAn = Alltrim(Str(gnAn))


		lcXDP = ""
		Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0"?>
<?xfa generator="XFA2_4" APIVersion="3.3.10270.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<form1>
<codF1>
</codF1>
<body1>
<HEADER xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<dd1>
<d_rec>0</d_rec>
</dd1>
<dd2>
<luna_r><<m.lcLuna>></luna_r>
<an_r><<m.lcAn>></an_r>
</dd2>
<dd3>
<cif><<m.lcCodFiscalF>></cif>
<denumire><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(gofirma.antet), .T.))>></denumire>
<adresa><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(goFirma.adresa), .T.))>></adresa>
<telefon><<ALLTRIM(goFirma.telefon)>></telefon>
<fax><<ALLTRIM(goFirma.fax)>></fax>
<eMail><<ALLTRIM(LOWER(goFirma.email))>></eMail>
</dd3>
<dd4>
<totalPlata_A></totalPlata_A>
<TOTAL_PAGINI><<m.lnNrPagini>></TOTAL_PAGINI>
<TOTAL_OPERATORI><<m.lnNrOperatori>></TOTAL_OPERATORI>
<SUMA_L><<lnTotalLB>></SUMA_L>
<SUMA_T><<lnTotalLT>></SUMA_T>
<SUMA_A><<lnTotalAB>></SUMA_A>
<SUMA_P><<lnTotalLS>></SUMA_P>
<SUMA_S><<lnTotalAS>></SUMA_S>
</dd4>
<dd5>
<numeP><<Alltrim(gofirma.nume_declarant)>></numeP>
<prenP><<Alltrim(gofirma.prenume_declarant)>></prenP>
<functiaP><<ALLTRIM(goFirma.functie)>></functiaP>

</dd5>
<dd6>
<numeFisier></numeFisier>
</dd6>
</body1>
<body2>
<dd1>
<OP1>
		Endtext

		lnSecventa = 0
		Select crsD390
		Scan
			lcTara = Substr(cod_fiscal, 1, 2)
			If Isalpha(Left(lcTara, 1))
				If !Isalpha(Substr(lcTara, 2, 1))
					lcTara = Left(lcTara, 1)
				Endif
			Else
				lcTara = ""
			Endif
			lcCodFiscal = Alltrim(Substr(cod_fiscal, Len(lcTara) + 1))
			lcDenumire = Alltrim(Substr(XmlSpecialCharacters(RemoveCharacters(denumire, .T.)), 1, 180))
			lcTip = Alltrim(Upper(TIP))
			lnSecventa = lnSecventa + 1
			lcSuma = Alltrim(Str(suma))

			Text To lcXDP Additive Textmerge Noshow Pretext 1
<OP2>
<seq><<m.lnSecventa>></seq>
<TIP_OP><<m.lcTip>></TIP_OP>
<TARA><<m.lcTara>></TARA>
<COD_OP><<m.lcCodFiscal>></COD_OP>
<DEN_OP><<m.lcDenumire>></DEN_OP>
<BAZA><<m.lcSuma>></BAZA>
</OP2>
			Endtext

		Endscan

		Text To lcXDP Additive Textmerge Noshow Pretext 1
</OP1>
<TG>
<TOTAL_BAZA><<m.lnTotal>></TOTAL_BAZA>
</TG>
<BUTOANE>
<unicitate>1</unicitate>
</BUTOANE>
</dd1>
</body2>

<body3>
<dd1 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<dd2><seq></seq><TARA1></TARA1><COD_OP1></COD_OP1></dd2>
<dd4 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
</body3>
<body4><dd1 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<dd2><seq></seq><TARA1></TARA1><COD_OP1></COD_OP1><TARA2></TARA2><COD_OP2></COD_OP2><motiv></motiv></dd2>
</body4>

<pp xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<calculatedCui><<goFirma.codfiscalfro>></calculatedCui>
<calculatedAnul><<Alltrim(Str(gnAn))>></calculatedAnul>
<calculatedlLuna><<Alltrim(Str(gnLuna)),2,[0])>></calculatedlLuna>
</form1>
</xfa:data>
</xfa:datasets>
<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
</xdp:xdp>
		Endtext

		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				open_default_app("http://static.anaf.ro/static/10/Anaf/Declaratii_R/390.html")
				amessagebox('Nu exista fisierul ' + m.lcFisierPDFOriginal + CRLF + ;
					  'Copiati pdf-ul inteligent D390 de pe website-ul ANAF in directorul ' + Justpath(m.lcFisierPDFOriginal) + CRLF + ;
					  'cu numele ' + Justfname(m.lcFisierPDFOriginal) + '.', 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)
	Endproc && do_genereaza_xdp_201902


	Procedure do_genereaza_xdp
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou
		Local lcAn, lcCodFiscal, lcCodFiscalF, lcDenumire, lcLuna, lcSelect, lcSuma, lcTara, lcTip, lcXDP
		Local lnNrOperatori, lnNrPagini, lnSecventa, lnTotal, lnTotalAB, lnTotalAS, lnTotalLB, lnTotalLS, lnTotalLT

		lcSelect = Select()

		lnTotalAB = This.nTotalAB
		lnTotalAS = This.nTotalAS
		lnTotalLB = This.nTotalLB
		lnTotalLS = This.nTotalLS
		lnTotalLT = This.nTotalLT
		lnTotal = This.nTotal
		lnNrPagini = This.nNrPagini
		lnNrOperatori = This.nNrOperatori
		lcCodFiscalF = Alltrim(gofirma.codfiscalfro)
		lcLuna = Padl(Alltrim(Str(gnLuna)), 2, [0])
		lcAn = Alltrim(Str(gnAn))


		lcXDP = ""
		Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0"?>
<?xfa generator="XFA2_4" APIVersion="3.3.10270.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<form1>
<codF1>
</codF1>
<body1>
<HEADER xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<dd1>
<d_rec>0</d_rec>
</dd1>
<dd2>
<luna_r><<m.lcLuna>></luna_r>
<an_r><<m.lcAn>></an_r>
</dd2>
<dd3>
<cif><<m.lcCodFiscalF>></cif>
<denumire><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(gofirma.antet), .T.))>></denumire>
<adresa><<XmlSpecialCharacters(RemoveCharacters(ALLTRIM(goFirma.adresa), .T.))>></adresa>
<telefon><<ALLTRIM(goFirma.telefon)>></telefon>
<fax><<ALLTRIM(goFirma.fax)>></fax>
<eMail><<ALLTRIM(LOWER(goFirma.email))>></eMail>
</dd3>
<dd4>
<totalPlata_A></totalPlata_A>
<TOTAL_PAGINI><<m.lnNrPagini>></TOTAL_PAGINI>
<TOTAL_OPERATORI><<m.lnNrOperatori>></TOTAL_OPERATORI>
<SUMA_L><<lnTotalLB>></SUMA_L>
<SUMA_T><<lnTotalLT>></SUMA_T>
<SUMA_A><<lnTotalAB>></SUMA_A>
<SUMA_P><<lnTotalLS>></SUMA_P>
<SUMA_S><<lnTotalAS>></SUMA_S>
</dd4>
<dd5>
<numeP><<Alltrim(gofirma.nume_declarant)>></numeP>
<prenP><<Alltrim(gofirma.prenume_declarant)>></prenP>
<functiaP><<ALLTRIM(goFirma.functie)>></functiaP>

</dd5>
<dd6>
<numeFisier></numeFisier>
</dd6>
</body1>
<body2>
<dd1>
<OP1>
		Endtext

		lnSecventa = 0
		Select crsD390
		Scan
			lcTara = Substr(cod_fiscal, 1, 2)
			If Isalpha(Left(lcTara, 1))
				If !Isalpha(Substr(lcTara, 2, 1))
					lcTara = Left(lcTara, 1)
				Endif
			Else
				lcTara = ""
			Endif
			lcCodFiscal = Alltrim(Substr(cod_fiscal, Len(lcTara) + 1))
			lcDenumire = Alltrim(Substr(XmlSpecialCharacters(RemoveCharacters(denumire, .T.)), 1, 180))
			lcTip = Alltrim(Upper(TIP))
			lnSecventa = lnSecventa + 1
			lcSuma = Alltrim(Str(suma))

			Text To lcXDP Additive Textmerge Noshow Pretext 1
<OP2>
<seq><<m.lnSecventa>></seq>
<TIP_OP><<m.lcTip>></TIP_OP>
<TARA><<m.lcTara>></TARA>
<COD_OP><<m.lcCodFiscal>></COD_OP>
<DEN_OP><<m.lcDenumire>></DEN_OP>
<BAZA><<m.lcSuma>></BAZA>
</OP2>
			Endtext

		Endscan

		Text To lcXDP Additive Textmerge Noshow Pretext 1
</OP1>
<TG>
<TOTAL_BAZA><<m.lnTotal>></TOTAL_BAZA>
</TG>
<BUTOANE>
<unicitate>1</unicitate>
</BUTOANE>
</dd1>
</body2>
<pp xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
<calculatedCui><<goFirma.codfiscalfro>></calculatedCui>
<calculatedAnul><<Alltrim(Str(gnAn))>></calculatedAnul>
<calculatedlLuna><<Alltrim(Str(gnLuna)),2,[0])>></calculatedlLuna>
</form1>
</xfa:data>
</xfa:datasets>
<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
</xdp:xdp>
		Endtext

		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				open_default_app("http://static.anaf.ro/static/10/Anaf/Declaratii_R/390.html")
				amessagebox('Nu exista fisierul ' + m.lcFisierPDFOriginal + CRLF + ;
					  'Copiati pdf-ul inteligent D390 de pe website-ul ANAF in directorul ' + Justpath(m.lcFisierPDFOriginal) + CRLF + ;
					  'cu numele ' + Justfname(m.lcFisierPDFOriginal) + '.', 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)
	Endproc && do_genereaza_xdp


	*==========================================
	Procedure UpdatePDF
		Lparameters tcPDFFile
		*** tcPDFFile: calea completa a fisierului pdf D:\CONTAFIN\COMUNCONTAFIN\d390.pdf

		Local lcFile, lcPath, llOpen
		lcFile = Justfname(m.tcPDFFile)
		lcPath = Addbs(Justpath(m.tcPDFFile))
		llOpen = .F.

		Return DownloadSupportFile(m.lcFile, m.lcPath, m.llOpen) && proceduri_comune.prg

	Endproc && UpdatePDF
	*==========================================
	Procedure do_executa
		Local lcAppPath, lcCaleFisier, lcCaleFisierPDF, lcCodFiscal, lcComunPath, lcCursor, lcCursorA
		Local lcCursorL, lcDenumire, lcFileName, lcFileNamePDF, lcFisier, lcFisierPDFNou
		Local lcFisierPDFOriginal, lcFisierTXT, lcFisierXDP, lcMesaj, lcNumeFisier, lcRun, lcSelect, lcSqlA
		Local lcSqlL, lcSuma, lcTara, lcTempPath, lcTip, lcXDP, llSucces, lnNrOperatori, lnNrPagini
		Local lnPos, lnSecventa, lnSucces, lnTotal, lnTotalAB, lnTotalAS, lnTotalLB, lnTotalLS, lnTotalLT
		Local loEx

		lcSelect = Select()

		*** EXPORT D390
		lcAppPath = Addbs(gcAppPath)
		lcComunPath = lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If lnPos > 0
			lcComunPath = Left(m.lcAppPath, lnPos)
		Endif
		lcComunPath = lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		*** Creez structura director export (caletemp\D390\aaaa_ll\firma\
		llSucces = .F.
		Try
			lcTempPath = Addbs(m.gcBasePath)
			lcCaleFisier = m.lcTempPath + "D390\" && c:\temp\D390\

			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Alltrim(Str(gnAn)) + "_" + Padl(Alltrim(Str(gnLuna)), 2, "0") + "\"&& c:\temp\D390\2011_01\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Strtran(gcFirma, [ ], [_]) + "\" && c:\temp\D390\2011_01\firma\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisierPDF = m.lcCaleFisier  && c:\temp\D390\2011_01\firma\
			If !Directory(m.lcCaleFisierPDF)
				Md (m.lcCaleFisierPDF)
			Endif

			llSucces = .T.
		Catch To loEx
			lcMesaj = "Creare director " + lcCaleFisier + " " + loEx.Message

			amessagebox(lcMesaj, 0 + 48)
		Endtry
		If !llSucces
			Return
		Endif

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', Strtran(Alltrim(gofirma.cod_fiscal), 'RO', ''))
		Endif

		lcNumeFisier = "d390_" + gofirma.codfiscalfro + "_" + Alltrim(Str(gnAn)) + "_" + Padl(gnLuna, 2, "0")
		lcFisierPDFOriginal = lcComunPath + "d390.pdf" && d:\contafin\comuncontafin\d390.pdf

		Do Case
			Case m.gnAn * 12 + m.gnLuna >= 2020 * 12 + 2
				lcFisierPDFOriginal = lcComunPath + "d390_201902.pdf"

			Case m.gnAn * 12 + m.gnLuna >= 2017 * 12 + 1
				lcFisierPDFOriginal = lcComunPath + "d390_2017.pdf" && d:\contafin\comuncontafin\d390_2017.pdf
			Otherwise
				lcFisierPDFOriginal = lcComunPath + "d390.pdf" && d:\contafin\comuncontafin\d390.pdf
		Endcase

		This.UpdatePDF(m.lcFisierPDFOriginal) && actualizez pdf de la romfast

		lcFileName = m.lcCaleFisier + lcNumeFisier

		lcFileNamePDF = m.lcCaleFisierPDF + lcNumeFisier
		lcFisierPDFNou = m.lcFileNamePDF + [.pdf]
		lcFisierXDP = m.lcFileNamePDF + [.xdp]

		lcFisierTXT = m.lcFileName + [.txt]


		If File(lcFisierTXT)
			Delete File (lcFisierTXT)
		Endif
		If File(lcFisierPDFNou)
			Delete File (lcFisierPDFNou)
		Endif
		If File(lcFisierXDP)
			Delete File (lcFisierXDP)
		Endif



		lcCursorA = [crsd390A]
		lcCursorL = [crsd390L]

		lcSqlA = [select id_part, replace(cod_fiscal,CHR(32),'') as cod_fiscal, denumire, ] + ;
			[ ROUND(sum(cebb)) as ceb, ROUND(sum(cesb)) as ces ] + ;
			[ from vjc2010 ] + ;
			[ where an = ?gnAn and luna = ?gnLuna and (cebb <> 0 or cesb <> 0) ] + ;
			[ group by id_part, replace(cod_fiscal,CHR(32),''), denumire, (case when cebb <> 0 then 'A' else 'S' end) ] + ;
			[ order by cod_fiscal]
		llSucces = goExecutor.oExecuta(lcSqlA, lcCursorA)
		If llSucces
			lcSqlL = [select id_part, replace(cod_fiscal,CHR(32),'') as cod_fiscal, denumire, ] + ;
				[ ROUND(sum(ceoptr)) as cet, ] + ;
				[ ROUND(sum(CESCDD1)) as ceb, ROUND(sum(CESVDD+CESVFDD+CESVFS)) as ces from vjv2010 ] + ;
				[where an = ?gnAn and luna = ?gnLuna and (ceoptr <> 0 or CESCDD1 <> 0 or CESVDD <> 0 or CESVFDD <> 0 or CESVFS <> 0) ] + ;
				[group by id_part, cod_fiscal, denumire ] + ;
				[order by cod_fiscal]
			llSucces = goExecutor.oExecuta(lcSqlL, lcCursorL)
		Endif

		If !llSucces
			Return
		Endif

		Create Cursor crsTipA (TIP c(1), ORDINE N(1))
		Insert Into crsTipA (TIP, ORDINE) Values ('A', 1)
		Insert Into crsTipA (TIP, ORDINE) Values ('S', 2)

		Create Cursor crsTipL (TIP c(1), ORDINE N(1))
		Insert Into crsTipL (TIP, ORDINE) Values ('L', 3)
		Insert Into crsTipL (TIP, ORDINE) Values ('P', 4)
		Insert Into crsTipL (TIP, ORDINE) Values ('T', 5)

		Select b.ORDINE, b.TIP, id_part, cod_fiscal, denumire, Cast(Iif(b.TIP = 'A', ceb, ces) As N(20)) As suma ;
			From crsd390A a, crsTipA b ;
			Union ;
			Select b.ORDINE, b.TIP, id_part, cod_fiscal, denumire, Cast(Iif(b.TIP = 'L', ceb, Iif(b.TIP = 'P', ces, cet)) As N(20)) As suma ;
			From crsd390L a, crsTipL b ;
			Order By 1, 2, 5 ;
			Into Cursor crsD390 Readwrite

		Use In (Select('crsTipA'))
		Use In (Select('crsTipL'))
		Use In (Select('crsd390A'))
		Use In (Select('crsd390L'))

		Update crsD390 Set cod_fiscal = Nvl(cod_fiscal, ''), denumire = Nvl(denumire, ''), suma = Nvl(suma, 0)
		Delete From crsD390 Where suma = 0

		*** TOTALURI
		Select Sum(Cast(Iif(TIP = 'A', suma, 0) As N(20))) As TotalAB, ;
			Sum(Cast(Iif(TIP = 'S', suma, 0) As N(20))) As TotalAS, ;
			Sum(Cast(Iif(TIP = 'L', suma, 0) As N(20))) As TotalLB, ;
			Sum(Cast(Iif(TIP = 'P', suma, 0) As N(20))) As TotalLS, ;
			Sum(Cast(Iif(TIP = 'T', suma, 0) As N(20))) As TotalLT ;
			From crsD390 ;
			Into Cursor crsTotal

		Go Top In crsTotal
		This.nTotalAB = crsTotal.TotalAB
		This.nTotalAS = crsTotal.TotalAS
		This.nTotalLB = crsTotal.TotalLB
		This.nTotalLS = crsTotal.TotalLS
		This.nTotalLT = crsTotal.TotalLT
		Use In (Select('crsTotal'))

		This.nTotal = This.nTotalAB + This.nTotalAS + This.nTotalLB + This.nTotalLS + This.nTotalLT

		Select Distinct id_part From crsD390 Into Cursor crsTotalOperatori
		This.nNrOperatori = _Tally
		This.nNrPagini = 1 + Ceiling((This.nNrOperatori - 22) / 26)
		Use In (Select('crsTotalOperatori'))
		*** TOTALURI ^



		Do Case
			Case m.gnAn * 12 + m.gnLuna >= 2020 * 12 + 2
				This.do_genereaza_xdp_201902(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)
				This.do_genereaza_txt_201902(lcFisierTXT)
			Otherwise
				This.do_genereaza_xdp(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)
				This.do_genereaza_txt(lcFisierTXT)
		Endcase



		Use In (Select('crsd390'))

		If File(m.lcFisierXDP) And File(m.lcFisierPDFNou)
			open_default_app(lcFisierXDP)
		Endif
		*** deschid explorer in directorul de export
		lcRun = [RUN /N7 explorer.exe "] + m.lcCaleFisier + ["]
		&lcRun

		Select (m.lcSelect)

	Endproc

Enddefine && D390

*********************************************************
*** DECLARATIA 101 Declaratie privind impozitul pe profit
*********************************************************
Procedure viz_decl101
	Local loD101
	loD101 = Createobject("D101")
	loD101.do_executa()
Endproc && vizualizare


Define Class D101 As Custom

	Procedure do_executa
		Private pnCA, pcPerioada, pnAnul, pnLuna1, pnLuna2
		Local loBackupXML As "backupxml"
		Local loil As "frm_decl101_perioada"
		Local losc As "frm_decl101"
		Local lcCursor, lcPerioade, lcSql, lcXMLFileName, llRestoredBackupSet
		Local lnSucces
		pcPerioada = ""
		pnCA = 0 && (cifra de afaceri folosita in formula 3/1000 CA la rd. 43 Sume reprezentând sponsorizare si/sau mecenat, burse private, în limita prevazuta de lege)
		pnAnul = gnAn
		pnLuna1 = 1
		pnLuna2 = 12

		********************************************************************************
		llRestoredBackupSet = .F.

		lcXMLFileName = "d101_" + Padl(m.gnAn, 4, '0') + "_" + Padl(m.gnLuna, 2, '0')
		loBackupXML = Createobject("backupxml", m.lcXMLFileName) && proceduri_comune.prg
		If loBackupXML.ExistsBackupSet()
			If amessagebox('Exista D101 salvata anterior.' + Chr(13) + Chr(10) + 'Doriti sa o reincarcati?', 4 + 32, _Screen.Caption) = 6
				llRestoredBackupSet = loBackupXML.RestoreBackupSet()
			Endif
		Endif
		********************************************************************************

		If !m.llRestoredBackupSet
			pcPerioada = ''
			lcPerioade = '01-' + Padl(gnLuna, 2, '0') + ',' + ;
				Iif(m.gnLuna = 12, '02-12,04-12,07-12,10-12', '')

			loil = Createobject("frm_decl101_perioada")
			loil.cb_perioada.RowSource = m.lcPerioade
			loil.cb_perioada.Value = Getwordnum(m.lcPerioade, 1, ',') && 01-LL
			loil.Show(1)



			Do Case
				Case gnAn >= 2024
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1224' order by ordine]
					lcProceduraCalculare = 'calculare_2024'
				Case gnAn >= 2021
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1221' order by ordine]
					lcProceduraCalculare = 'calculare_2021'
				Case gnAn >= 2020
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1220' order by ordine]
					lcProceduraCalculare = 'calculare_2020'
				Case gnAn >= 2018
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1218' order by ordine]
					lcProceduraCalculare = 'calculare_2018'
				Case gnAn >= 2017
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1217' order by ordine]
					lcProceduraCalculare = 'calculare_2017'
				Case gnAn >= 2016
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1216' order by ordine]
					lcProceduraCalculare = 'calculare_2016'
				Case gnAn >= 2014
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1214' order by ordine]
					lcProceduraCalculare = 'calculare_2014'
				Case gnAn >= 2012
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1212' order by ordine]
					lcProceduraCalculare = 'calculare_2012'
				Case gnAn >= 2011
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1211' order by ordine]
					lcProceduraCalculare = 'calculare_2012'
				Otherwise
					lcSql = [select d.*, 0000000000.00 as l1, 0000000000.00 as l2 ] + ;
						[ from decl_101 d where valabil = '1210' order by ordine]
					lcProceduraCalculare = 'calculare_2012'
			Endcase

			lcCursor = [cDecl]

			lnSucces = goExecutor.oExecute(lcSql, lcCursor)

			If lnSucces < 0
				amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
				Return
			Endif

			pnAnul = m.gnAn
			pnLuna1 = Int(Val(Left(m.pcPerioada, 2)))
			pnLuna2 = Int(Val(Substr(m.pcPerioada, 4)))


			This.&lcProceduraCalculare(m.pnAnul, m.pnLuna2)
			Select cDecl
			Replace All l2 With suma

			If m.pnLuna1 <> 1 && 10-12
				This.&lcProceduraCalculare(m.pnAnul, m.pnLuna1 - 1)
				Select cDecl
				Replace All l1 With suma

				Replace All suma With l2 - l1
			Endif
		Endif && llRestoredBackupSet

		Select cDecl
		losc = Createobject("frm_decl101")
		If m.pnLuna1 = 1
			losc._grdrow1.cl1.Visible = .F.
			losc._grdrow1.cl2.Visible = .F.
		Endif
		losc.Show(1)

		If m.gnButon = 1
			loBackupXML.SaveBackupSet("cDecl")
			loBackupXML.SavePrivateVariables("pcPerioada,pnCA,pnAnul,pnLuna1,pnLuna2")
		Endif && BUTON = 1

	Endproc && do_executa

	**********************************************
	Procedure calculare_2024
		Parameters tnAnul, tnLuna

		* Generez Contul de Profit si Pierdere pentru venituri, cheltuieli exploatare, financiare, extraordinare

		Private pcId_butonCur, pnCount, pnCumul, pnId_sectie, pnId_setCur, pnId_venchelt

		Local lcCursor, lcField, lcRand, lcSel, lcSql, llSucces, ln1, ln10, ln16, ln2, ln26, ln28, ln29
		Local ln30, ln4, ln5, ln7, ln8, lnBazaProtocol, lnProtocol, lnProtocolLimita, lnRezInPlus
		Local lnRezMaxAn, lnRezMaxTotal, lnSucces
		Private pn1061c, pn1061t, pn50, pn51, pnCA, pnCapSoc, pnProfitBrut

		pnCumul = 1
		pnId_sectie = 0
		pnId_venchelt = 0
		pnCount = 0
		pcId_butonCur = [2024_2_R_20_0] && ultimul formular 20, a fost generat pentru semestrul II 2018, entitati mici/medii/mari
		pnId_setCur = 0 && -218 la 12/2024

		lcSql = [begin pack_sitfin.get_id_set(?pcId_butonCur, ?@pnId_setCur); end;]
		llSucces = goExecutor.oExecuta(lcSql)
		If !m.llSucces
			Return
		Endif

		lcSql	 = [begin PACK_MG_EVOLUTIE.GET_SET(?pnId_setCur, ?@pnCount); end;] && copii din mg_indicatori in temporarul mg_tmp_indicatori
		llSucces = goExecutor.oExecuta(lcSql)

		If !m.llSucces
			Return
		Endif

		If m.pnCount = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine indicatori!', 0 + 48, "Atentie")
			Return
		Endif

		lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(pnCumul)) + [,] + Alltrim(Str(pnId_sectie)) + [,] + Alltrim(Str(pnId_venchelt)) + [)}]
		* lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(?gnLuna, ?gnAn, ?gnLuna,?gnAn, ?pnCumul, ?pnId_sectie, ?pnId_venchelt}] && ODBC error: sintax error or access violation
		lcCursor = 'crsContProfit'

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

		Select crsContProfit
		If Reccount() = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine informatii!', 0 + 48, "Atentie")
			Return
		Endif

		lcField = Field(7, 'crsContProfit')
		Select Rand, nume_ind, Round(&lcField, 0) As valoare ;
			From crsContProfit ;
			Order By Rand ;
			Into Cursor cContProfit Readwrite

		&& denominare
		If 12 * gnAn + gnLuna <= 12 * 2005 + 6
			Select cContProfit
			Replace All valoare With Round(valoare / 1000, 0)
		Endif

		Use In (Select('crsContProfit'))

		Select cContProfit
		Scan
			lcRand = Alltrim(Rand)
			Do Case
				Case m.lcRand = '01' && cifra de afaceri
					* CIFRA DE AFACERI pentru formula rd. 45 (sponsorizari in limita 3/1000 din CA)
					* rd 45 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod) (rd. 1 din Contul de Profit si Pierdere)
					pnCA = valoare
				Case m.lcRand = '16' && venituri din exploatare
					ln1 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '01'
					If Found()
						Replace suma With m.ln1
					Endif
				Case m.lcRand = '42' && cheltuieli din exploatare
					ln2 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '02'
					If Found()
						Replace suma With m.ln2
					Endif
				Case m.lcRand = '52' && venituri financiare
					ln4 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '04'
					If Found()
						Replace suma With m.ln4
					Endif
				Case m.lcRand = '59' && cheltuieli financiare
					ln5 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '05'
					If Found()
						Replace suma With m.ln5
					Endif
					*!*					Case m.lcRand = '57' && venituri extraordinare
					*!*						ln7 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '07'
					*!*						If Found()
					*!*							Replace suma With m.ln7
					*!*						Endif
					*!*					Case m.lcRand = '58' && cheltuieli extraordinare
					*!*						ln8 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '08'
					*!*						If Found()
					*!*							Replace suma With m.ln8
					*!*						Endif
			Endcase
		Endscan && cContProfit
		Use In (Select('cContProfit'))
		&& Veniturile si cheltuielile extraordinare nu mai apar in Contul de Profit si Pierdere 12/2015
		ln7 = 0
		ln8 = 0
		* Profit brut (Rd 16 - cheltuieli cu rezerva deductibila)
		pnProfitBrut = ln1 - ln2 + ln4 - ln5 + ln7 - ln8

		* Capital social (Rd 16 - cheltuieli cu rezerva deductibila)
		Text To lcSql Textmerge Noshow
		select sum(soldcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont = '1012' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pnCapSoc = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCapSoc)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pnCapSoc = Nvl(m.pnCapSoc, 0)

		* Rd 23 Cheltuieli impozit profit 691 (sau 698 daca nu s-a stornat cand a trecut de la micro)
		Text To lcSql Textmerge Noshow
		select sum(totcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont in ('691', '698') and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		lcCursor = [crs691]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln23 = 0
		If m.llSucces
			Select crs691
			ln23 = Round(Nvl(suma, 0), 0)
			Use In crs691
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '23' && Cheltuieli cu impozitul pe profit/venit
		If Found()
			Replace suma With m.ln23
		Endif

		* La randul 2 cheltuieli din exploatare se aduna si Cheltuiala cu impozitul pe profit (apoi se scade din randul 23 ca cheltuiala nedeductibila)
		Select cDecl
		Locate For Alltrim(Rand) = '02'
		If Found()
			Replace suma With suma + m.ln23
		Endif

		* Rd 25 Amenzi, confiscari, dobanzi 6581
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 6581]

		lcCursor = [crs6581]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln25 = 0
		If m.llSucces
			Select crs6581
			ln25 = Nvl(suma, 0)
			Use In crs6581
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '25' && Amenzi, confiscari, dobanzi
		If Found()
			Replace suma With Round(m.ln25, 0)
		Endif
		*---

		* RD 26 Cheltuieli de protocol care depašesc limita prevazuta de lege 27
		*** cheltuieli de protocol sub limita 623.protocol.limita = 2% * (121 + 691 + 623.protocol) = 2% (rd. 10 + rd.24 + 623.protocol )
		*** cheltuieli de protocol care depasesc limita prevazuta de lege = 623.protocol - 623.protocol.limita
		lcSql = [select sum(b.totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalanamama b], [vbalana b]) + ;
			[ join plcont p on b.cont = p.cont and b.acont = p.acont and b.an = p.an ] + ;
			[ where b.an = ] + Alltrim(Str(tnAnul)) + [ and b.luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(b.cont,1,3) = '623' and UPPER(p.explicatie) like '%PROTOCOL%' and p.acont is not null]

		lcCursor = [crs623]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		lnProtocol = 0
		If m.llSucces
			Select crs623
			lnProtocol = Nvl(suma, 0)
			Use In (Select('crs623'))
		Endif

		If m.lnProtocol = 0
			lcSql = [select sum(totcred) as suma ] + ;
				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
				[ and SUBSTR(cont,1,3) = '623']

			lcCursor = [crs623]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)

			If m.llSucces
				Select crs623
				lnProtocol = Nvl(suma, 0)
				Use In (Select('crs623'))
			Endif


		Endif
		ln10 = (m.ln1 - m.ln2) + (m.ln4 - m.ln5) + (m.ln7 - m.ln8)
		If m.ln10 <= 0 && profitul negativ, cheltuielile de protocol depasesc in intregime limita
			ln26 = m.lnProtocol
		Else
			lnBazaProtocol = m.ln10 + m.ln23 + m.lnProtocol
			lnProtocolLimita = m.lnBazaProtocol * 0.02
			ln26 = m.lnProtocol - m.lnProtocolLimita
			ln26 = Max(m.ln26, 0)
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '26' && Cheltuieli de protocol care depasesc limita prevazuta de lege
		If Found()
			Replace suma With Round(m.ln26, 0)
		Endif
		*---
		*!*	Select cDecl
		*!*	Locate For Alltrim(Rand) = '401'
		*!*	Replace suma With ln24 * 0.16

		* Rd 27 Cheltuieli de sponsorizare
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and cont = '6582']

		lcCursor = [crs6582]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln27 = 0
		If m.llSucces
			Select crs6582
			ln27 = Nvl(suma, 0)
			Use In (Select('crs6582'))
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '27' && DONATII SI SUBVENTII ACORDATE
		If Found()
			Replace suma With Round(m.ln27, 0)
		Endif

		* Rd 50 Impozit pe profit declarat pentru anul de raportare prin formularul 100
		* TSD 691 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '691']

		pn50 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn50)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '50'
			If Found()
				Replace suma With Round(Nvl(m.pn50, 0), 0)
			Endif
		Endif

		*!*			* Rd 51 Impozit pe veniturile microintreprinderilor declarat pentru anul de raportare prin formularul 100
		*!*			* TSD 698 30 sept
		*!*			lcSql = [select sum(totdeb) as suma ] + ;
		*!*				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
		*!*				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
		*!*				[ and cont = '698']

		*!*			pn51 = 0
		*!*			lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn51)

		*!*			If m.lnSucces < 0
		*!*				amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		*!*			Else
		*!*				Select cDecl
		*!*				Locate For Alltrim(Rand) = '51'
		*!*				If Found()
		*!*					Replace suma With Round(Nvl(m.pn51, 0), 0)
		*!*				Endif
		*!*			Endif


		* Rd 13 Rezerva legala deductibila
		* <= 5% din profitul contabil inainte de impozitare - venituri neimpozabile + cheltuieli nedeductibile
		* maxim total rezerva <= 20% capital social
		Text To lcSql Noshow Textmerge
		select sum(soldcred) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061t = 0 && sold 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061t)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pn1061t = Nvl(m.pn1061t, 0)

		Text To lcSql Noshow Textmerge
		select sum(totcred-preccred1) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061c = 0 && rulaj anual 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061c)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			ln13 = 0
			If m.pnProfitBrut > 0
				lnRezMaxAn = m.pnProfitBrut * 0.05
				lnRezMaxTotal = m.pnCapSoc * 0.20
				lnRezInPlus = Iif(m.lnRezMaxTotal < m.pn1061t, m.lnRezMaxTotal - m.pn1061t, 0)

				ln13 = Min(m.lnRezMaxAn, m.pn1061c) + m.lnRezInPlus
			Endif

			Select cDecl
			Locate For Alltrim(Rand) = '13'
			If Found()
				Replace suma With Round(Nvl(m.ln13, 0), 0)
			Endif
		Endif



	Endproc && calculare_2024
	
	**********************************************
	Procedure calculare_2021
		Parameters tnAnul, tnLuna

		* Generez Contul de Profit si Pierdere pentru venituri, cheltuieli exploatare, financiare, extraordinare

		Private pcId_butonCur, pnCount, pnCumul, pnId_sectie, pnId_setCur, pnId_venchelt

		Local lcCursor, lcField, lcRand, lcSel, lcSql, llSucces, ln1, ln10, ln16, ln2, ln26, ln28, ln29
		Local ln30, ln4, ln5, ln7, ln8, lnBazaProtocol, lnProtocol, lnProtocolLimita, lnRezInPlus
		Local lnRezMaxAn, lnRezMaxTotal, lnSucces
		Private pn1061c, pn1061t, pn50, pn51, pnCA, pnCapSoc, pnProfitBrut

		pnCumul = 1
		pnId_sectie = 0
		pnId_venchelt = 0
		pnCount = 0
		pcId_butonCur = [2018_2_R_20_0] && ultimul formular 20, a fost generat pentru semestrul II 2018, entitati mici/medii/mari
		pnId_setCur = 0 && -165 la 12/2018

		lcSql = [begin pack_sitfin.get_id_set(?pcId_butonCur, ?@pnId_setCur); end;]
		llSucces = goExecutor.oExecuta(lcSql)
		If !m.llSucces
			Return
		Endif

		lcSql	 = [begin PACK_MG_EVOLUTIE.GET_SET(?pnId_setCur, ?@pnCount); end;] && copii din mg_indicatori in temporarul mg_tmp_indicatori
		llSucces = goExecutor.oExecuta(lcSql)

		If !m.llSucces
			Return
		Endif

		If m.pnCount = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine indicatori!', 0 + 48, "Atentie")
			Return
		Endif

		lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(pnCumul)) + [,] + Alltrim(Str(pnId_sectie)) + [,] + Alltrim(Str(pnId_venchelt)) + [)}]
		* lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(?gnLuna, ?gnAn, ?gnLuna,?gnAn, ?pnCumul, ?pnId_sectie, ?pnId_venchelt}] && ODBC error: sintax error or access violation
		lcCursor = 'crsContProfit'

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

		Select crsContProfit
		If Reccount() = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine informatii!', 0 + 48, "Atentie")
			Return
		Endif

		lcField = Field(7, 'crsContProfit')
		Select Rand, nume_ind, Round(&lcField, 0) As valoare ;
			From crsContProfit ;
			Order By Rand ;
			Into Cursor cContProfit Readwrite

		&& denominare
		If 12 * gnAn + gnLuna <= 12 * 2005 + 6
			Select cContProfit
			Replace All valoare With Round(valoare / 1000, 0)
		Endif

		Use In (Select('crsContProfit'))

		Select cContProfit
		Scan
			lcRand = Alltrim(Rand)
			Do Case
				Case m.lcRand = '01' && cifra de afaceri
					* CIFRA DE AFACERI pentru formula rd. 45 (sponsorizari in limita 3/1000 din CA)
					* rd 45 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod) (rd. 1 din Contul de Profit si Pierdere)
					pnCA = valoare
				Case m.lcRand = '16' && venituri din exploatare
					ln1 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '01'
					If Found()
						Replace suma With m.ln1
					Endif
				Case m.lcRand = '42' && cheltuieli din exploatare
					ln2 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '02'
					If Found()
						Replace suma With m.ln2
					Endif
				Case m.lcRand = '52' && venituri financiare
					ln4 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '04'
					If Found()
						Replace suma With m.ln4
					Endif
				Case m.lcRand = '59' && cheltuieli financiare
					ln5 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '05'
					If Found()
						Replace suma With m.ln5
					Endif
					*!*					Case m.lcRand = '57' && venituri extraordinare
					*!*						ln7 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '07'
					*!*						If Found()
					*!*							Replace suma With m.ln7
					*!*						Endif
					*!*					Case m.lcRand = '58' && cheltuieli extraordinare
					*!*						ln8 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '08'
					*!*						If Found()
					*!*							Replace suma With m.ln8
					*!*						Endif
			Endcase
		Endscan && cContProfit
		Use In (Select('cContProfit'))
		&& Veniturile si cheltuielile extraordinare nu mai apar in Contul de Profit si Pierdere 12/2015
		ln7 = 0
		ln8 = 0
		* Profit brut (Rd 16 - cheltuieli cu rezerva deductibila)
		pnProfitBrut = ln1 - ln2 + ln4 - ln5 + ln7 - ln8

		* Capital social (Rd 16 - cheltuieli cu rezerva deductibila)
		Text To lcSql Textmerge Noshow
		select sum(soldcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont = '1012' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pnCapSoc = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCapSoc)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pnCapSoc = Nvl(m.pnCapSoc, 0)

		* Rd 23 Cheltuieli impozit profit 691 (sau 698 daca nu s-a stornat cand a trecut de la micro)
		Text To lcSql Textmerge Noshow
		select sum(totcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont in ('691', '698') and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		lcCursor = [crs691]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln23 = 0
		If m.llSucces
			Select crs691
			ln23 = Round(Nvl(suma, 0), 0)
			Use In crs691
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '23' && Cheltuieli cu impozitul pe profit/venit
		If Found()
			Replace suma With m.ln23
		Endif

		* La randul 2 cheltuieli din exploatare se aduna si Cheltuiala cu impozitul pe profit (apoi se scade din randul 23 ca cheltuiala nedeductibila)
		Select cDecl
		Locate For Alltrim(Rand) = '02'
		If Found()
			Replace suma With suma + m.ln23
		Endif

		* Rd 25 Amenzi, confiscari, dobanzi 6581
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 6581]

		lcCursor = [crs6581]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln25 = 0
		If m.llSucces
			Select crs6581
			ln25 = Nvl(suma, 0)
			Use In crs6581
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '25' && Amenzi, confiscari, dobanzi
		If Found()
			Replace suma With Round(m.ln25, 0)
		Endif
		*---

		* RD 26 Cheltuieli de protocol care depašesc limita prevazuta de lege 27
		*** cheltuieli de protocol sub limita 623.protocol.limita = 2% * (121 + 691 + 623.protocol) = 2% (rd. 10 + rd.24 + 623.protocol )
		*** cheltuieli de protocol care depasesc limita prevazuta de lege = 623.protocol - 623.protocol.limita
		lcSql = [select sum(b.totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalanamama b], [vbalana b]) + ;
			[ join plcont p on b.cont = p.cont and b.acont = p.acont and b.an = p.an ] + ;
			[ where b.an = ] + Alltrim(Str(tnAnul)) + [ and b.luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(b.cont,1,3) = '623' and UPPER(p.explicatie) like '%PROTOCOL%' and p.acont is not null]

		lcCursor = [crs623]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		lnProtocol = 0
		If m.llSucces
			Select crs623
			lnProtocol = Nvl(suma, 0)
			Use In (Select('crs623'))
		Endif

		If m.lnProtocol = 0
			lcSql = [select sum(totcred) as suma ] + ;
				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
				[ and SUBSTR(cont,1,3) = '623']

			lcCursor = [crs623]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)

			If m.llSucces
				Select crs623
				lnProtocol = Nvl(suma, 0)
				Use In (Select('crs623'))
			Endif


		Endif
		ln10 = (m.ln1 - m.ln2) + (m.ln4 - m.ln5) + (m.ln7 - m.ln8)
		If m.ln10 <= 0 && profitul negativ, cheltuielile de protocol depasesc in intregime limita
			ln26 = m.lnProtocol
		Else
			lnBazaProtocol = m.ln10 + m.ln23 + m.lnProtocol
			lnProtocolLimita = m.lnBazaProtocol * 0.02
			ln26 = m.lnProtocol - m.lnProtocolLimita
			ln26 = Max(m.ln26, 0)
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '26' && Cheltuieli de protocol care depasesc limita prevazuta de lege
		If Found()
			Replace suma With Round(m.ln26, 0)
		Endif
		*---
		*!*	Select cDecl
		*!*	Locate For Alltrim(Rand) = '401'
		*!*	Replace suma With ln24 * 0.16

		* Rd 27 Cheltuieli de sponsorizare
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and cont = '6582']

		lcCursor = [crs6582]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln27 = 0
		If m.llSucces
			Select crs6582
			ln27 = Nvl(suma, 0)
			Use In (Select('crs6582'))
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '27' && DONATII SI SUBVENTII ACORDATE
		If Found()
			Replace suma With Round(m.ln27, 0)
		Endif

		* Rd 49 Impozit pe profit declarat pentru anul de raportare prin formularul 100
		* TSD 691 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '691']

		pn49 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn49)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '49'
			If Found()
				Replace suma With Round(Nvl(m.pn49, 0), 0)
			Endif
		Endif

		*!*			* Rd 51 Impozit pe veniturile microintreprinderilor declarat pentru anul de raportare prin formularul 100
		*!*			* TSD 698 30 sept
		*!*			lcSql = [select sum(totdeb) as suma ] + ;
		*!*				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
		*!*				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
		*!*				[ and cont = '698']

		*!*			pn51 = 0
		*!*			lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn51)

		*!*			If m.lnSucces < 0
		*!*				amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		*!*			Else
		*!*				Select cDecl
		*!*				Locate For Alltrim(Rand) = '51'
		*!*				If Found()
		*!*					Replace suma With Round(Nvl(m.pn51, 0), 0)
		*!*				Endif
		*!*			Endif


		* Rd 13 Rezerva legala deductibila
		* <= 5% din profitul contabil inainte de impozitare - venituri neimpozabile + cheltuieli nedeductibile
		* maxim total rezerva <= 20% capital social
		Text To lcSql Noshow Textmerge
		select sum(soldcred) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061t = 0 && sold 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061t)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pn1061t = Nvl(m.pn1061t, 0)

		Text To lcSql Noshow Textmerge
		select sum(totcred-preccred1) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061c = 0 && rulaj anual 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061c)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			ln13 = 0
			If m.pnProfitBrut > 0
				lnRezMaxAn = m.pnProfitBrut * 0.05
				lnRezMaxTotal = m.pnCapSoc * 0.20
				lnRezInPlus = Iif(m.lnRezMaxTotal < m.pn1061t, m.lnRezMaxTotal - m.pn1061t, 0)

				ln13 = Min(m.lnRezMaxAn, m.pn1061c) + m.lnRezInPlus
			Endif

			Select cDecl
			Locate For Alltrim(Rand) = '13'
			If Found()
				Replace suma With Round(Nvl(m.ln13, 0), 0)
			Endif
		Endif



	Endproc && calculare_2021

	**********************************************
	Procedure calculare_2020
		Parameters tnAnul, tnLuna

		* Generez Contul de Profit si Pierdere pentru venituri, cheltuieli exploatare, financiare, extraordinare

		Private pcId_butonCur, pnCount, pnCumul, pnId_sectie, pnId_setCur, pnId_venchelt

		Local lcCursor, lcField, lcRand, lcSel, lcSql, llSucces, ln1, ln10, ln16, ln2, ln26, ln28, ln29
		Local ln30, ln4, ln5, ln7, ln8, lnBazaProtocol, lnProtocol, lnProtocolLimita, lnRezInPlus
		Local lnRezMaxAn, lnRezMaxTotal, lnSucces
		Private pn1061c, pn1061t, pn50, pn51, pnCA, pnCapSoc, pnProfitBrut

		pnCumul = 1
		pnId_sectie = 0
		pnId_venchelt = 0
		pnCount = 0
		pcId_butonCur = [2018_2_R_20_0] && ultimul formular 20, a fost generat pentru semestrul II 2018, entitati mici/medii/mari
		pnId_setCur = 0 && -165 la 12/2018

		lcSql = [begin pack_sitfin.get_id_set(?pcId_butonCur, ?@pnId_setCur); end;]
		llSucces = goExecutor.oExecuta(lcSql)
		If !m.llSucces
			Return
		Endif

		lcSql	 = [begin PACK_MG_EVOLUTIE.GET_SET(?pnId_setCur, ?@pnCount); end;] && copii din mg_indicatori in temporarul mg_tmp_indicatori
		llSucces = goExecutor.oExecuta(lcSql)

		If !m.llSucces
			Return
		Endif

		If m.pnCount = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine indicatori!', 0 + 48, "Atentie")
			Return
		Endif

		lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(pnCumul)) + [,] + Alltrim(Str(pnId_sectie)) + [,] + Alltrim(Str(pnId_venchelt)) + [)}]
		* lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(?gnLuna, ?gnAn, ?gnLuna,?gnAn, ?pnCumul, ?pnId_sectie, ?pnId_venchelt}] && ODBC error: sintax error or access violation
		lcCursor = 'crsContProfit'

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

		Select crsContProfit
		If Reccount() = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine informatii!', 0 + 48, "Atentie")
			Return
		Endif

		lcField = Field(7, 'crsContProfit')
		Select Rand, nume_ind, Round(&lcField, 0) As valoare ;
			From crsContProfit ;
			Order By Rand ;
			Into Cursor cContProfit Readwrite

		&& denominare
		If 12 * gnAn + gnLuna <= 12 * 2005 + 6
			Select cContProfit
			Replace All valoare With Round(valoare / 1000, 0)
		Endif

		Use In (Select('crsContProfit'))

		Select cContProfit
		Scan
			lcRand = Alltrim(Rand)
			Do Case
				Case m.lcRand = '01' && cifra de afaceri
					* CIFRA DE AFACERI pentru formula rd. 45 (sponsorizari in limita 3/1000 din CA)
					* rd 45 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod) (rd. 1 din Contul de Profit si Pierdere)
					pnCA = valoare
				Case m.lcRand = '16' && venituri din exploatare
					ln1 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '01'
					If Found()
						Replace suma With m.ln1
					Endif
				Case m.lcRand = '42' && cheltuieli din exploatare
					ln2 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '02'
					If Found()
						Replace suma With m.ln2
					Endif
				Case m.lcRand = '52' && venituri financiare
					ln4 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '04'
					If Found()
						Replace suma With m.ln4
					Endif
				Case m.lcRand = '59' && cheltuieli financiare
					ln5 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '05'
					If Found()
						Replace suma With m.ln5
					Endif
					*!*					Case m.lcRand = '57' && venituri extraordinare
					*!*						ln7 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '07'
					*!*						If Found()
					*!*							Replace suma With m.ln7
					*!*						Endif
					*!*					Case m.lcRand = '58' && cheltuieli extraordinare
					*!*						ln8 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '08'
					*!*						If Found()
					*!*							Replace suma With m.ln8
					*!*						Endif
			Endcase
		Endscan && cContProfit
		Use In (Select('cContProfit'))
		&& Veniturile si cheltuielile extraordinare nu mai apar in Contul de Profit si Pierdere 12/2015
		ln7 = 0
		ln8 = 0
		* Profit brut (Rd 16 - cheltuieli cu rezerva deductibila)
		pnProfitBrut = ln1 - ln2 + ln4 - ln5 + ln7 - ln8

		* Capital social (Rd 16 - cheltuieli cu rezerva deductibila)
		Text To lcSql Textmerge Noshow
		select sum(soldcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont = '1012' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pnCapSoc = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCapSoc)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pnCapSoc = Nvl(m.pnCapSoc, 0)

		* Rd 23 Cheltuieli impozit profit 691 (sau 698 daca nu s-a stornat cand a trecut de la micro)
		Text To lcSql Textmerge Noshow
		select sum(totcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont in ('691', '698') and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		lcCursor = [crs691]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln23 = 0
		If m.llSucces
			Select crs691
			ln23 = Round(Nvl(suma, 0), 0)
			Use In crs691
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '23' && Cheltuieli cu impozitul pe profit/venit
		If Found()
			Replace suma With m.ln23
		Endif

		* La randul 2 cheltuieli din exploatare se aduna si Cheltuiala cu impozitul pe profit (apoi se scade din randul 23 ca cheltuiala nedeductibila)
		Select cDecl
		Locate For Alltrim(Rand) = '02'
		If Found()
			Replace suma With suma + m.ln23
		Endif

		* Rd 25 Amenzi, confiscari, dobanzi 6581
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 6581]

		lcCursor = [crs6581]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln25 = 0
		If m.llSucces
			Select crs6581
			ln25 = Nvl(suma, 0)
			Use In crs6581
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '25' && Amenzi, confiscari, dobanzi
		If Found()
			Replace suma With Round(m.ln25, 0)
		Endif
		*---

		* RD 26 Cheltuieli de protocol care depašesc limita prevazuta de lege 27
		*** cheltuieli de protocol sub limita 623.protocol.limita = 2% * (121 + 691 + 623.protocol) = 2% (rd. 10 + rd.24 + 623.protocol )
		*** cheltuieli de protocol care depasesc limita prevazuta de lege = 623.protocol - 623.protocol.limita
		lcSql = [select sum(b.totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalanamama b], [vbalana b]) + ;
			[ join plcont p on b.cont = p.cont and b.acont = p.acont and b.an = p.an ] + ;
			[ where b.an = ] + Alltrim(Str(tnAnul)) + [ and b.luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(b.cont,1,3) = '623' and UPPER(p.explicatie) like '%PROTOCOL%' and p.acont is not null]

		lcCursor = [crs623]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		lnProtocol = 0
		If m.llSucces
			Select crs623
			lnProtocol = Nvl(suma, 0)
			Use In (Select('crs623'))
		Endif

		If m.lnProtocol = 0
			lcSql = [select sum(totcred) as suma ] + ;
				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
				[ and SUBSTR(cont,1,3) = '623']

			lcCursor = [crs623]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)

			If m.llSucces
				Select crs623
				lnProtocol = Nvl(suma, 0)
				Use In (Select('crs623'))
			Endif


		Endif
		ln10 = (m.ln1 - m.ln2) + (m.ln4 - m.ln5) + (m.ln7 - m.ln8)
		If m.ln10 <= 0 && profitul negativ, cheltuielile de protocol depasesc in intregime limita
			ln26 = m.lnProtocol
		Else
			lnBazaProtocol = m.ln10 + m.ln23 + m.lnProtocol
			lnProtocolLimita = m.lnBazaProtocol * 0.02
			ln26 = m.lnProtocol - m.lnProtocolLimita
			ln26 = Max(m.ln26, 0)
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '26' && Cheltuieli de protocol care depasesc limita prevazuta de lege
		If Found()
			Replace suma With Round(m.ln26, 0)
		Endif
		*---
		*!*	Select cDecl
		*!*	Locate For Alltrim(Rand) = '401'
		*!*	Replace suma With ln24 * 0.16

		* Rd 27 Cheltuieli de sponsorizare
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and cont = '6582']

		lcCursor = [crs6582]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln27 = 0
		If m.llSucces
			Select crs6582
			ln27 = Nvl(suma, 0)
			Use In (Select('crs6582'))
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '27' && DONATII SI SUBVENTII ACORDATE
		If Found()
			Replace suma With Round(m.ln27, 0)
		Endif

		* Rd 52 Impozit pe profit declarat pentru anul de raportare prin formularul 100
		* TSD 691 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '691']

		pn52 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn52)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '52'
			If Found()
				Replace suma With Round(Nvl(m.pn52, 0), 0)
			Endif
		Endif

		*!*			* Rd 51 Impozit pe veniturile microintreprinderilor declarat pentru anul de raportare prin formularul 100
		*!*			* TSD 698 30 sept
		*!*			lcSql = [select sum(totdeb) as suma ] + ;
		*!*				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
		*!*				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
		*!*				[ and cont = '698']

		*!*			pn51 = 0
		*!*			lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn51)

		*!*			If m.lnSucces < 0
		*!*				amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		*!*			Else
		*!*				Select cDecl
		*!*				Locate For Alltrim(Rand) = '51'
		*!*				If Found()
		*!*					Replace suma With Round(Nvl(m.pn51, 0), 0)
		*!*				Endif
		*!*			Endif


		* Rd 13 Rezerva legala deductibila
		* <= 5% din profitul contabil inainte de impozitare - venituri neimpozabile + cheltuieli nedeductibile
		* maxim total rezerva <= 20% capital social
		Text To lcSql Noshow Textmerge
		select sum(soldcred) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061t = 0 && sold 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061t)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pn1061t = Nvl(m.pn1061t, 0)

		Text To lcSql Noshow Textmerge
		select sum(totcred-preccred1) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061c = 0 && rulaj anual 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061c)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			ln13 = 0
			If m.pnProfitBrut > 0
				lnRezMaxAn = m.pnProfitBrut * 0.05
				lnRezMaxTotal = m.pnCapSoc * 0.20
				lnRezInPlus = Iif(m.lnRezMaxTotal < m.pn1061t, m.lnRezMaxTotal - m.pn1061t, 0)

				ln13 = Min(m.lnRezMaxAn, m.pn1061c) + m.lnRezInPlus
			Endif

			Select cDecl
			Locate For Alltrim(Rand) = '13'
			If Found()
				Replace suma With Round(Nvl(m.ln13, 0), 0)
			Endif
		Endif



	Endproc && calculare_2020

	**********************************************
	Procedure calculare_2018
		Parameters tnAnul, tnLuna

		* Generez Contul de Profit si Pierdere pentru venituri, cheltuieli exploatare, financiare, extraordinare

		Private pcId_butonCur, pnCount, pnCumul, pnId_sectie, pnId_setCur, pnId_venchelt

		Local lcCursor, lcField, lcRand, lcSel, lcSql, llSucces, ln1, ln10, ln16, ln2, ln26, ln28, ln29
		Local ln30, ln4, ln5, ln7, ln8, lnBazaProtocol, lnProtocol, lnProtocolLimita, lnRezInPlus
		Local lnRezMaxAn, lnRezMaxTotal, lnSucces
		Private pn1061c, pn1061t, pn50, pn51, pnCA, pnCapSoc, pnProfitBrut

		pnCumul = 1
		pnId_sectie = 0
		pnId_venchelt = 0
		pnCount = 0
		pcId_butonCur = [2018_2_R_20_0] && ultimul formular 20, a fost generat pentru semestrul II 2018, entitati mici/medii/mari
		pnId_setCur = 0 && -165 la 12/2018

		lcSql = [begin pack_sitfin.get_id_set(?pcId_butonCur, ?@pnId_setCur); end;]
		llSucces = goExecutor.oExecuta(lcSql)
		If !m.llSucces
			Return
		Endif

		lcSql	 = [begin PACK_MG_EVOLUTIE.GET_SET(?pnId_setCur, ?@pnCount); end;] && copii din mg_indicatori in temporarul mg_tmp_indicatori
		llSucces = goExecutor.oExecuta(lcSql)

		If !m.llSucces
			Return
		Endif

		If m.pnCount = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine indicatori!', 0 + 48, "Atentie")
			Return
		Endif

		lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(pnCumul)) + [,] + Alltrim(Str(pnId_sectie)) + [,] + Alltrim(Str(pnId_venchelt)) + [)}]
		* lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(?gnLuna, ?gnAn, ?gnLuna,?gnAn, ?pnCumul, ?pnId_sectie, ?pnId_venchelt}] && ODBC error: sintax error or access violation
		lcCursor = 'crsContProfit'

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

		Select crsContProfit
		If Reccount() = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine informatii!', 0 + 48, "Atentie")
			Return
		Endif

		lcField = Field(7, 'crsContProfit')
		Select Rand, nume_ind, Round(&lcField, 0) As valoare ;
			From crsContProfit ;
			Order By Rand ;
			Into Cursor cContProfit Readwrite

		&& denominare
		If 12 * gnAn + gnLuna <= 12 * 2005 + 6
			Select cContProfit
			Replace All valoare With Round(valoare / 1000, 0)
		Endif

		Use In (Select('crsContProfit'))

		Select cContProfit
		Scan
			lcRand = Alltrim(Rand)
			Do Case
				Case m.lcRand = '01' && cifra de afaceri
					* CIFRA DE AFACERI pentru formula rd. 45 (sponsorizari in limita 3/1000 din CA)
					* rd 45 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod) (rd. 1 din Contul de Profit si Pierdere)
					pnCA = valoare
				Case m.lcRand = '16' && venituri din exploatare
					ln1 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '01'
					If Found()
						Replace suma With m.ln1
					Endif
				Case m.lcRand = '42' && cheltuieli din exploatare
					ln2 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '02'
					If Found()
						Replace suma With m.ln2
					Endif
				Case m.lcRand = '52' && venituri financiare
					ln4 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '04'
					If Found()
						Replace suma With m.ln4
					Endif
				Case m.lcRand = '59' && cheltuieli financiare
					ln5 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '05'
					If Found()
						Replace suma With m.ln5
					Endif
					*!*					Case m.lcRand = '57' && venituri extraordinare
					*!*						ln7 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '07'
					*!*						If Found()
					*!*							Replace suma With m.ln7
					*!*						Endif
					*!*					Case m.lcRand = '58' && cheltuieli extraordinare
					*!*						ln8 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '08'
					*!*						If Found()
					*!*							Replace suma With m.ln8
					*!*						Endif
			Endcase
		Endscan && cContProfit
		Use In (Select('cContProfit'))
		&& Veniturile si cheltuielile extraordinare nu mai apar in Contul de Profit si Pierdere 12/2015
		ln7 = 0
		ln8 = 0
		* Profit brut (Rd 16 - cheltuieli cu rezerva deductibila)
		pnProfitBrut = ln1 - ln2 + ln4 - ln5 + ln7 - ln8

		* Capital social (Rd 16 - cheltuieli cu rezerva deductibila)
		Text To lcSql Textmerge Noshow
		select sum(soldcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont = '1012' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pnCapSoc = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCapSoc)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pnCapSoc = Nvl(m.pnCapSoc, 0)

		* Rd 23 Cheltuieli impozit profit 691 (sau 698 daca nu s-a stornat cand a trecut de la micro)
		Text To lcSql Textmerge Noshow
		select sum(totcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont in ('691', '698') and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		lcCursor = [crs691]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln23 = 0
		If m.llSucces
			Select crs691
			ln23 = Round(Nvl(suma, 0), 0)
			Use In crs691
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '23' && Cheltuieli cu impozitul pe profit/venit
		If Found()
			Replace suma With m.ln23
		Endif

		* La randul 2 cheltuieli din exploatare se aduna si Cheltuiala cu impozitul pe profit (apoi se scade din randul 23 ca cheltuiala nedeductibila)
		Select cDecl
		Locate For Alltrim(Rand) = '02'
		If Found()
			Replace suma With suma + m.ln23
		Endif

		* Rd 25 Amenzi, confiscari, dobanzi 6581
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 6581]

		lcCursor = [crs6581]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln25 = 0
		If m.llSucces
			Select crs6581
			ln25 = Nvl(suma, 0)
			Use In crs6581
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '25' && Amenzi, confiscari, dobanzi
		If Found()
			Replace suma With Round(m.ln25, 0)
		Endif
		*---

		* RD 26 Cheltuieli de protocol care depašesc limita prevazuta de lege 27
		*** cheltuieli de protocol sub limita 623.protocol.limita = 2% * (121 + 691 + 623.protocol) = 2% (rd. 10 + rd.24 + 623.protocol )
		*** cheltuieli de protocol care depasesc limita prevazuta de lege = 623.protocol - 623.protocol.limita
		lcSql = [select sum(b.totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalanamama b], [vbalana b]) + ;
			[ join plcont p on b.cont = p.cont and b.acont = p.acont and b.an = p.an ] + ;
			[ where b.an = ] + Alltrim(Str(tnAnul)) + [ and b.luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(b.cont,1,3) = '623' and UPPER(p.explicatie) like '%PROTOCOL%' and p.acont is not null]

		lcCursor = [crs623]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		lnProtocol = 0
		If m.llSucces
			Select crs623
			lnProtocol = Nvl(suma, 0)
			Use In (Select('crs623'))
		Endif

		If m.lnProtocol = 0
			lcSql = [select sum(totcred) as suma ] + ;
				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
				[ and SUBSTR(cont,1,3) = '623']

			lcCursor = [crs623]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)

			If m.llSucces
				Select crs623
				lnProtocol = Nvl(suma, 0)
				Use In (Select('crs623'))
			Endif


		Endif
		ln10 = (m.ln1 - m.ln2) + (m.ln4 - m.ln5) + (m.ln7 - m.ln8)
		If m.ln10 <= 0 && profitul negativ, cheltuielile de protocol depasesc in intregime limita
			ln26 = m.lnProtocol
		Else
			lnBazaProtocol = m.ln10 + m.ln23 + m.lnProtocol
			lnProtocolLimita = m.lnBazaProtocol * 0.02
			ln26 = m.lnProtocol - m.lnProtocolLimita
			ln26 = Max(m.ln26, 0)
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '26' && Cheltuieli de protocol care depasesc limita prevazuta de lege
		If Found()
			Replace suma With Round(m.ln26, 0)
		Endif
		*---
		*!*	Select cDecl
		*!*	Locate For Alltrim(Rand) = '401'
		*!*	Replace suma With ln24 * 0.16

		* Rd 27 Cheltuieli de sponsorizare
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and cont = '6582']

		lcCursor = [crs6582]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln27 = 0
		If m.llSucces
			Select crs6582
			ln27 = Nvl(suma, 0)
			Use In (Select('crs6582'))
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '27' && DONATII SI SUBVENTII ACORDATE
		If Found()
			Replace suma With Round(m.ln27, 0)
		Endif

		* Rd 47 Impozit pe profit declarat pentru anul de raportare prin formularul 100
		* TSD 691 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '691']

		pn47 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn47)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '47'
			If Found()
				Replace suma With Round(Nvl(m.pn47, 0), 0)
			Endif
		Endif

		*!*			* Rd 51 Impozit pe veniturile microintreprinderilor declarat pentru anul de raportare prin formularul 100
		*!*			* TSD 698 30 sept
		*!*			lcSql = [select sum(totdeb) as suma ] + ;
		*!*				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
		*!*				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
		*!*				[ and cont = '698']

		*!*			pn51 = 0
		*!*			lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn51)

		*!*			If m.lnSucces < 0
		*!*				amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		*!*			Else
		*!*				Select cDecl
		*!*				Locate For Alltrim(Rand) = '51'
		*!*				If Found()
		*!*					Replace suma With Round(Nvl(m.pn51, 0), 0)
		*!*				Endif
		*!*			Endif


		* Rd 13 Rezerva legala deductibila
		* <= 5% din profitul contabil inainte de impozitare - venituri neimpozabile + cheltuieli nedeductibile
		* maxim total rezerva <= 20% capital social
		Text To lcSql Noshow Textmerge
		select sum(soldcred) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061t = 0 && sold 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061t)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pn1061t = Nvl(m.pn1061t, 0)

		Text To lcSql Noshow Textmerge
		select sum(totcred-preccred1) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061c = 0 && rulaj anual 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061c)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			ln13 = 0
			If m.pnProfitBrut > 0
				lnRezMaxAn = m.pnProfitBrut * 0.05
				lnRezMaxTotal = m.pnCapSoc * 0.20
				lnRezInPlus = Iif(m.lnRezMaxTotal < m.pn1061t, m.lnRezMaxTotal - m.pn1061t, 0)

				ln13 = Min(m.lnRezMaxAn, m.pn1061c) + m.lnRezInPlus
			Endif

			Select cDecl
			Locate For Alltrim(Rand) = '13'
			If Found()
				Replace suma With Round(Nvl(m.ln13, 0), 0)
			Endif
		Endif



	Endproc && calculare_2018

	**********************************************
	Procedure calculare_2017
		Parameters tnAnul, tnLuna

		* Generez Contul de Profit si Pierdere pentru venituri, cheltuieli exploatare, financiare, extraordinare

		Private pcId_butonCur, pnCount, pnCumul, pnId_sectie, pnId_setCur, pnId_venchelt

		Local lcCursor, lcField, lcRand, lcSel, lcSql, llSucces, ln1, ln10, ln16, ln2, ln26, ln28, ln29
		Local ln30, ln4, ln5, ln7, ln8, lnBazaProtocol, lnProtocol, lnProtocolLimita, lnRezInPlus
		Local lnRezMaxAn, lnRezMaxTotal, lnSucces
		Private pn1061c, pn1061t, pn50, pn51, pnCA, pnCapSoc, pnProfitBrut

		pnCumul = 1
		pnId_sectie = 0
		pnId_venchelt = 0
		pnCount = 0
		pcId_butonCur = [2015_2_R_20_0] && ultimul formular 20, a fost generat pentru semestrul II 2015, entitati mici/medii/mari
		pnId_setCur = 0 && -142 la 12/2015

		lcSql = [begin pack_sitfin.get_id_set(?pcId_butonCur, ?@pnId_setCur); end;]
		llSucces = goExecutor.oExecuta(lcSql)
		If !m.llSucces
			Return
		Endif

		lcSql	 = [begin PACK_MG_EVOLUTIE.GET_SET(?pnId_setCur, ?@pnCount); end;] && copii din mg_indicatori in temporarul mg_tmp_indicatori
		llSucces = goExecutor.oExecuta(lcSql)

		If !m.llSucces
			Return
		Endif

		If m.pnCount = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine indicatori!', 0 + 48, "Atentie")
			Return
		Endif

		lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(pnCumul)) + [,] + Alltrim(Str(pnId_sectie)) + [,] + Alltrim(Str(pnId_venchelt)) + [)}]
		* lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(?gnLuna, ?gnAn, ?gnLuna,?gnAn, ?pnCumul, ?pnId_sectie, ?pnId_venchelt}] && ODBC error: sintax error or access violation
		lcCursor = 'crsContProfit'

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

		Select crsContProfit
		If Reccount() = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine informatii!', 0 + 48, "Atentie")
			Return
		Endif

		lcField = Field(7, 'crsContProfit')
		Select Rand, nume_ind, Round(&lcField, 0) As valoare ;
			From crsContProfit ;
			Order By Rand ;
			Into Cursor cContProfit Readwrite

		&& denominare
		If 12 * gnAn + gnLuna <= 12 * 2005 + 6
			Select cContProfit
			Replace All valoare With Round(valoare / 1000, 0)
		Endif

		Use In (Select('crsContProfit'))

		Select cContProfit
		Scan
			lcRand = Alltrim(Rand)
			Do Case
				Case m.lcRand = '01' && cifra de afaceri
					* CIFRA DE AFACERI pentru formula rd. 45 (sponsorizari in limita 3/1000 din CA)
					* rd 45 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod) (rd. 1 din Contul de Profit si Pierdere)
					pnCA = valoare
				Case m.lcRand = '16' && venituri din exploatare
					ln1 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '01'
					If Found()
						Replace suma With m.ln1
					Endif
				Case m.lcRand = '42' && cheltuieli din exploatare
					ln2 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '02'
					If Found()
						Replace suma With m.ln2
					Endif
				Case m.lcRand = '52' && venituri financiare
					ln4 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '04'
					If Found()
						Replace suma With m.ln4
					Endif
				Case m.lcRand = '59' && cheltuieli financiare
					ln5 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '05'
					If Found()
						Replace suma With m.ln5
					Endif
					*!*					Case m.lcRand = '57' && venituri extraordinare
					*!*						ln7 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '07'
					*!*						If Found()
					*!*							Replace suma With m.ln7
					*!*						Endif
					*!*					Case m.lcRand = '58' && cheltuieli extraordinare
					*!*						ln8 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '08'
					*!*						If Found()
					*!*							Replace suma With m.ln8
					*!*						Endif
			Endcase
		Endscan && cContProfit
		Use In (Select('cContProfit'))
		&& Veniturile si cheltuielile extraordinare nu mai apar in Contul de Profit si Pierdere 12/2015
		ln7 = 0
		ln8 = 0
		* Profit brut (Rd 16 - cheltuieli cu rezerva deductibila)
		pnProfitBrut = ln1 - ln2 + ln4 - ln5 + ln7 - ln8

		* Capital social (Rd 16 - cheltuieli cu rezerva deductibila)
		Text To lcSql Textmerge Noshow
		select sum(soldcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont = '1012' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pnCapSoc = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCapSoc)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pnCapSoc = Nvl(m.pnCapSoc, 0)

		* Rd 23 Cheltuieli impozit profit 691 (sau 698 daca nu s-a stornat cand a trecut de la micro)
		Text To lcSql Textmerge Noshow
		select sum(totcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont in ('691', '698') and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		lcCursor = [crs691]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln23 = 0
		If m.llSucces
			Select crs691
			ln23 = Round(Nvl(suma, 0), 0)
			Use In crs691
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '23' && Cheltuieli cu impozitul pe profit/venit
		If Found()
			Replace suma With m.ln23
		Endif

		* La randul 2 cheltuieli din exploatare se aduna si Cheltuiala cu impozitul pe profit (apoi se scade din randul 23 ca cheltuiala nedeductibila)
		Select cDecl
		Locate For Alltrim(Rand) = '02'
		If Found()
			Replace suma With suma + m.ln23
		Endif

		* Rd 25 Amenzi, confiscari, dobanzi 6581
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 6581]

		lcCursor = [crs6581]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln25 = 0
		If m.llSucces
			Select crs6581
			ln25 = Nvl(suma, 0)
			Use In crs6581
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '25' && Amenzi, confiscari, dobanzi
		If Found()
			Replace suma With Round(m.ln25, 0)
		Endif
		*---

		* RD 26 Cheltuieli de protocol care depašesc limita prevazuta de lege 27
		*** cheltuieli de protocol sub limita 623.protocol.limita = 2% * (121 + 691 + 623.protocol) = 2% (rd. 10 + rd.24 + 623.protocol )
		*** cheltuieli de protocol care depasesc limita prevazuta de lege = 623.protocol - 623.protocol.limita
		lcSql = [select sum(b.totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalanamama b], [vbalana b]) + ;
			[ join plcont p on b.cont = p.cont and b.acont = p.acont and b.an = p.an ] + ;
			[ where b.an = ] + Alltrim(Str(tnAnul)) + [ and b.luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(b.cont,1,3) = '623' and UPPER(p.explicatie) like '%PROTOCOL%' and p.acont is not null]

		lcCursor = [crs623]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		lnProtocol = 0
		If m.llSucces
			Select crs623
			lnProtocol = Nvl(suma, 0)
			Use In (Select('crs623'))
		Endif

		If m.lnProtocol = 0
			lcSql = [select sum(totcred) as suma ] + ;
				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
				[ and SUBSTR(cont,1,3) = '623']

			lcCursor = [crs623]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)

			If m.llSucces
				Select crs623
				lnProtocol = Nvl(suma, 0)
				Use In (Select('crs623'))
			Endif


		Endif
		ln10 = (m.ln1 - m.ln2) + (m.ln4 - m.ln5) + (m.ln7 - m.ln8)
		If m.ln10 <= 0 && profitul negativ, cheltuielile de protocol depasesc in intregime limita
			ln26 = m.lnProtocol
		Else
			lnBazaProtocol = m.ln10 + m.ln23 + m.lnProtocol
			lnProtocolLimita = m.lnBazaProtocol * 0.02
			ln26 = m.lnProtocol - m.lnProtocolLimita
			ln26 = Max(m.ln26, 0)
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '26' && Cheltuieli de protocol care depasesc limita prevazuta de lege
		If Found()
			Replace suma With Round(m.ln26, 0)
		Endif
		*---
		*!*	Select cDecl
		*!*	Locate For Alltrim(Rand) = '401'
		*!*	Replace suma With ln24 * 0.16

		* Rd 27 Cheltuieli de sponsorizare
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and cont = '6582']

		lcCursor = [crs6582]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln27 = 0
		If m.llSucces
			Select crs6582
			ln27 = Nvl(suma, 0)
			Use In (Select('crs6582'))
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '27' && DONATII SI SUBVENTII ACORDATE
		If Found()
			Replace suma With Round(m.ln27, 0)
		Endif

		* Rd 47 Impozit pe profit declarat pentru anul de raportare prin formularul 100
		* TSD 691 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '691']

		pn47 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn47)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '47'
			If Found()
				Replace suma With Round(Nvl(m.pn47, 0), 0)
			Endif
		Endif

		*!*			* Rd 51 Impozit pe veniturile microintreprinderilor declarat pentru anul de raportare prin formularul 100
		*!*			* TSD 698 30 sept
		*!*			lcSql = [select sum(totdeb) as suma ] + ;
		*!*				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
		*!*				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
		*!*				[ and cont = '698']

		*!*			pn51 = 0
		*!*			lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn51)

		*!*			If m.lnSucces < 0
		*!*				amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		*!*			Else
		*!*				Select cDecl
		*!*				Locate For Alltrim(Rand) = '51'
		*!*				If Found()
		*!*					Replace suma With Round(Nvl(m.pn51, 0), 0)
		*!*				Endif
		*!*			Endif


		* Rd 13 Rezerva legala deductibila
		* <= 5% din profitul contabil inainte de impozitare - venituri neimpozabile + cheltuieli nedeductibile
		* maxim total rezerva <= 20% capital social
		Text To lcSql Noshow Textmerge
		select sum(soldcred) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061t = 0 && sold 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061t)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pn1061t = Nvl(m.pn1061t, 0)

		Text To lcSql Noshow Textmerge
		select sum(totcred-preccred1) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061c = 0 && rulaj anual 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061c)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			ln13 = 0
			If m.pnProfitBrut > 0
				lnRezMaxAn = m.pnProfitBrut * 0.05
				lnRezMaxTotal = m.pnCapSoc * 0.20
				lnRezInPlus = Iif(m.lnRezMaxTotal < m.pn1061t, m.lnRezMaxTotal - m.pn1061t, 0)

				ln13 = Min(m.lnRezMaxAn, m.pn1061c) + m.lnRezInPlus
			Endif

			Select cDecl
			Locate For Alltrim(Rand) = '13'
			If Found()
				Replace suma With Round(Nvl(m.ln13, 0), 0)
			Endif
		Endif



	Endproc && calculare_2017


	**********************************************
	Procedure calculare_2016
		Parameters tnAnul, tnLuna

		* Generez Contul de Profit si Pierdere pentru venituri, cheltuieli exploatare, financiare, extraordinare

		Private pcId_butonCur, pnCount, pnCumul, pnId_sectie, pnId_setCur, pnId_venchelt

		Local lcCursor, lcField, lcRand, lcSel, lcSql, llSucces, ln1, ln10, ln16, ln2, ln26, ln28, ln29
		Local ln30, ln4, ln5, ln7, ln8, lnBazaProtocol, lnProtocol, lnProtocolLimita, lnRezInPlus
		Local lnRezMaxAn, lnRezMaxTotal, lnSucces
		Private pn1061c, pn1061t, pn50, pn51, pnCA, pnCapSoc, pnProfitBrut

		pnCumul = 1
		pnId_sectie = 0
		pnId_venchelt = 0
		pnCount = 0
		pcId_butonCur = [2015_2_R_20_0] && ultimul formular 20, a fost generat pentru semestrul II 2015, entitati mici/medii/mari
		pnId_setCur = 0 && -142 la 12/2015

		lcSql = [begin pack_sitfin.get_id_set(?pcId_butonCur, ?@pnId_setCur); end;]
		llSucces = goExecutor.oExecuta(lcSql)
		If !m.llSucces
			Return
		Endif

		lcSql	 = [begin PACK_MG_EVOLUTIE.GET_SET(?pnId_setCur, ?@pnCount); end;] && copii din mg_indicatori in temporarul mg_tmp_indicatori
		llSucces = goExecutor.oExecuta(lcSql)

		If !m.llSucces
			Return
		Endif

		If m.pnCount = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine indicatori!', 0 + 48, "Atentie")
			Return
		Endif

		lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(pnCumul)) + [,] + Alltrim(Str(pnId_sectie)) + [,] + Alltrim(Str(pnId_venchelt)) + [)}]
		* lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(?gnLuna, ?gnAn, ?gnLuna,?gnAn, ?pnCumul, ?pnId_sectie, ?pnId_venchelt}] && ODBC error: sintax error or access violation
		lcCursor = 'crsContProfit'

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

		Select crsContProfit
		If Reccount() = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine informatii!', 0 + 48, "Atentie")
			Return
		Endif

		lcField = Field(7, 'crsContProfit')
		Select Rand, nume_ind, Round(&lcField, 0) As valoare ;
			From crsContProfit ;
			Order By Rand ;
			Into Cursor cContProfit Readwrite

		&& denominare
		If 12 * gnAn + gnLuna <= 12 * 2005 + 6
			Select cContProfit
			Replace All valoare With Round(valoare / 1000, 0)
		Endif

		Use In (Select('crsContProfit'))

		Select cContProfit
		Scan
			lcRand = Alltrim(Rand)
			Do Case
				Case m.lcRand = '01' && cifra de afaceri
					* CIFRA DE AFACERI pentru formula rd. 45 (sponsorizari in limita 3/1000 din CA)
					* rd 45 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod) (rd. 1 din Contul de Profit si Pierdere)
					pnCA = valoare
				Case m.lcRand = '16' && venituri din exploatare
					ln1 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '01'
					If Found()
						Replace suma With m.ln1
					Endif
				Case m.lcRand = '42' && cheltuieli din exploatare
					ln2 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '02'
					If Found()
						Replace suma With m.ln2
					Endif
				Case m.lcRand = '52' && venituri financiare
					ln4 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '04'
					If Found()
						Replace suma With m.ln4
					Endif
				Case m.lcRand = '59' && cheltuieli financiare
					ln5 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '05'
					If Found()
						Replace suma With m.ln5
					Endif
					*!*					Case m.lcRand = '57' && venituri extraordinare
					*!*						ln7 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '07'
					*!*						If Found()
					*!*							Replace suma With m.ln7
					*!*						Endif
					*!*					Case m.lcRand = '58' && cheltuieli extraordinare
					*!*						ln8 = valoare

					*!*						Select cDecl
					*!*						Locate For Alltrim(Rand) = '08'
					*!*						If Found()
					*!*							Replace suma With m.ln8
					*!*						Endif
			Endcase
		Endscan && cContProfit
		Use In (Select('cContProfit'))
		&& Veniturile si cheltuielile extraordinare nu mai apar in Contul de Profit si Pierdere 12/2015
		ln7 = 0
		ln8 = 0
		* Profit brut (Rd 16 - cheltuieli cu rezerva deductibila)
		pnProfitBrut = ln1 - ln2 + ln4 - ln5 + ln7 - ln8

		* Capital social (Rd 16 - cheltuieli cu rezerva deductibila)
		Text To lcSql Textmerge Noshow
		select sum(soldcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont = '1012' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pnCapSoc = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCapSoc)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pnCapSoc = Nvl(m.pnCapSoc, 0)

		* Rd 23 Cheltuieli impozit profit 691 (sau 698 daca nu s-a stornat cand a trecut de la micro)
		Text To lcSql Textmerge Noshow
		select sum(totcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont in ('691', '698') and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		lcCursor = [crs691]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln23 = 0
		If m.llSucces
			Select crs691
			ln23 = Round(Nvl(suma, 0), 0)
			Use In crs691
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '23' && Cheltuieli cu impozitul pe profit/venit
		If Found()
			Replace suma With m.ln23
		Endif

		* La randul 2 cheltuieli din exploatare se aduna si Cheltuiala cu impozitul pe profit (apoi se scade din randul 23 ca cheltuiala nedeductibila)
		Select cDecl
		Locate For Alltrim(Rand) = '02'
		If Found()
			Replace suma With suma + m.ln23
		Endif

		* Rd 25 Amenzi, confiscari, dobanzi 6581
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 6581]

		lcCursor = [crs6581]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln25 = 0
		If m.llSucces
			Select crs6581
			ln25 = Nvl(suma, 0)
			Use In crs6581
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '25' && Amenzi, confiscari, dobanzi
		If Found()
			Replace suma With Round(m.ln25, 0)
		Endif
		*---

		* RD 26 Cheltuieli de protocol care depašesc limita prevazuta de lege 27
		*** cheltuieli de protocol sub limita 623.protocol.limita = 2% * (121 + 691 + 623.protocol) = 2% (rd. 10 + rd.24 + 623.protocol )
		*** cheltuieli de protocol care depasesc limita prevazuta de lege = 623.protocol - 623.protocol.limita
		lcSql = [select sum(b.totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalanamama b], [vbalana b]) + ;
			[ join plcont p on b.cont = p.cont and b.acont = p.acont and b.an = p.an ] + ;
			[ where b.an = ] + Alltrim(Str(tnAnul)) + [ and b.luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(b.cont,1,3) = '623' and UPPER(p.explicatie) like '%PROTOCOL%' and p.acont is not null]

		lcCursor = [crs623]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		lnProtocol = 0
		If m.llSucces
			Select crs623
			lnProtocol = Nvl(suma, 0)
			Use In (Select('crs623'))
		Endif

		If m.lnProtocol = 0
			lcSql = [select sum(totcred) as suma ] + ;
				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
				[ and SUBSTR(cont,1,3) = '623']

			lcCursor = [crs623]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)

			If m.llSucces
				Select crs623
				lnProtocol = Nvl(suma, 0)
				Use In (Select('crs623'))
			Endif


		Endif
		ln10 = (m.ln1 - m.ln2) + (m.ln4 - m.ln5) + (m.ln7 - m.ln8)
		If m.ln10 <= 0 && profitul negativ, cheltuielile de protocol depasesc in intregime limita
			ln26 = m.lnProtocol
		Else
			lnBazaProtocol = m.ln10 + m.ln23 + m.lnProtocol
			lnProtocolLimita = m.lnBazaProtocol * 0.02
			ln26 = m.lnProtocol - m.lnProtocolLimita
			ln26 = Max(m.ln26, 0)
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '26' && Cheltuieli de protocol care depasesc limita prevazuta de lege
		If Found()
			Replace suma With Round(m.ln26, 0)
		Endif
		*---
		*!*	Select cDecl
		*!*	Locate For Alltrim(Rand) = '401'
		*!*	Replace suma With ln24 * 0.16

		* Rd 27 Cheltuieli de sponsorizare
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and cont = '6582']

		lcCursor = [crs6582]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln27 = 0
		If m.llSucces
			Select crs6582
			ln27 = Nvl(suma, 0)
			Use In (Select('crs6582'))
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '27' && DONATII SI SUBVENTII ACORDATE
		If Found()
			Replace suma With Round(m.ln27, 0)
		Endif

		* Rd 47 Impozit pe profit declarat pentru anul de raportare prin formularul 100
		* TSD 691 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '691']

		pn47 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn47)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '47'
			If Found()
				Replace suma With Round(Nvl(m.pn47, 0), 0)
			Endif
		Endif

		*!*			* Rd 51 Impozit pe veniturile microintreprinderilor declarat pentru anul de raportare prin formularul 100
		*!*			* TSD 698 30 sept
		*!*			lcSql = [select sum(totdeb) as suma ] + ;
		*!*				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
		*!*				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
		*!*				[ and cont = '698']

		*!*			pn51 = 0
		*!*			lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn51)

		*!*			If m.lnSucces < 0
		*!*				amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		*!*			Else
		*!*				Select cDecl
		*!*				Locate For Alltrim(Rand) = '51'
		*!*				If Found()
		*!*					Replace suma With Round(Nvl(m.pn51, 0), 0)
		*!*				Endif
		*!*			Endif


		* Rd 13 Rezerva legala deductibila
		* <= 5% din profitul contabil inainte de impozitare - venituri neimpozabile + cheltuieli nedeductibile
		* maxim total rezerva <= 20% capital social
		Text To lcSql Noshow Textmerge
		select sum(soldcred) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061t = 0 && sold 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061t)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pn1061t = Nvl(m.pn1061t, 0)

		Text To lcSql Noshow Textmerge
		select sum(totcred-preccred1) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061c = 0 && rulaj anual 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061c)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			ln13 = 0
			If m.pnProfitBrut > 0
				lnRezMaxAn = m.pnProfitBrut * 0.05
				lnRezMaxTotal = m.pnCapSoc * 0.20
				lnRezInPlus = Iif(m.lnRezMaxTotal < m.pn1061t, m.lnRezMaxTotal - m.pn1061t, 0)

				ln13 = Min(m.lnRezMaxAn, m.pn1061c) + m.lnRezInPlus
			Endif

			Select cDecl
			Locate For Alltrim(Rand) = '13'
			If Found()
				Replace suma With Round(Nvl(m.ln13, 0), 0)
			Endif
		Endif



	Endproc && calculare_2016

	**********************************************
	Procedure calculare_2014
		Parameters tnAnul, tnLuna

		* Generez Contul de Profit si Pierdere pentru venituri, cheltuieli exploatare, financiare, extraordinare

		Private pcId_butonCur, pnCount, pnCumul, pnId_sectie, pnId_setCur, pnId_venchelt

		Local lcCursor, lcField, lcRand, lcSel, lcSql, llSucces, ln1, ln10, ln16, ln2, ln26, ln28, ln29
		Local ln30, ln4, ln5, ln7, ln8, lnBazaProtocol, lnProtocol, lnProtocolLimita, lnRezInPlus
		Local lnRezMaxAn, lnRezMaxTotal, lnSucces
		Private pn1061c, pn1061t, pn50, pn51, pnCA, pnCapSoc, pnProfitBrut

		pnCumul = 1
		pnId_sectie = 0
		pnId_venchelt = 0
		pnCount = 0
		pcId_butonCur = [2013_2_0_20_0] && ultimul formular 20, a fost generat pentru semestrul II 2013
		pnId_setCur = 0 && -121 la 12/2013

		lcSql = [begin pack_sitfin.get_id_set(?pcId_butonCur, ?@pnId_setCur); end;]
		llSucces = goExecutor.oExecuta(lcSql)
		If !m.llSucces
			Return
		Endif

		lcSql	 = [begin PACK_MG_EVOLUTIE.GET_SET(?pnId_setCur, ?@pnCount); end;] && copii din mg_indicatori in temporarul mg_tmp_indicatori
		llSucces = goExecutor.oExecuta(lcSql)

		If !m.llSucces
			Return
		Endif

		If m.pnCount = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine indicatori!', 0 + 48, "Atentie")
			Return
		Endif

		lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(pnCumul)) + [,] + Alltrim(Str(pnId_sectie)) + [,] + Alltrim(Str(pnId_venchelt)) + [)}]
		* lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(?gnLuna, ?gnAn, ?gnLuna,?gnAn, ?pnCumul, ?pnId_sectie, ?pnId_venchelt}] && ODBC error: sintax error or access violation
		lcCursor = 'crsContProfit'

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

		Select crsContProfit
		If Reccount() = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine informatii!', 0 + 48, "Atentie")
			Return
		Endif

		lcField = Field(7, 'crsContProfit')
		Select Rand, nume_ind, Round(&lcField, 0) As valoare ;
			From crsContProfit ;
			Order By Rand ;
			Into Cursor cContProfit Readwrite

		&& denominare
		If 12 * gnAn + gnLuna <= 12 * 2005 + 6
			Select cContProfit
			Replace All valoare With Round(valoare / 1000, 0)
		Endif

		Use In (Select('crsContProfit'))

		Select cContProfit
		Scan
			lcRand = Alltrim(Rand)
			Do Case
				Case m.lcRand = '01' && cifra de afaceri
					* CIFRA DE AFACERI pentru formula rd. 45 (sponsorizari in limita 3/1000 din CA)
					* rd 45 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod) (rd. 1 din Contul de Profit si Pierdere)
					pnCA = valoare
				Case m.lcRand = '12' && venituri din exploatare
					ln1 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '01'
					If Found()
						Replace suma With m.ln1
					Endif
				Case m.lcRand = '35' && cheltuieli din exploatare
					ln2 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '02'
					If Found()
						Replace suma With m.ln2
					Endif
				Case m.lcRand = '45' && venituri financiare
					ln4 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '04'
					If Found()
						Replace suma With m.ln4
					Endif
				Case m.lcRand = '52' && cheltuieli financiare
					ln5 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '05'
					If Found()
						Replace suma With m.ln5
					Endif
				Case m.lcRand = '57' && venituri extraordinare
					ln7 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '07'
					If Found()
						Replace suma With m.ln7
					Endif
				Case m.lcRand = '58' && cheltuieli extraordinare
					ln8 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '08'
					If Found()
						Replace suma With m.ln8
					Endif
			Endcase
		Endscan && cContProfit
		Use In (Select('cContProfit'))

		* Profit brut (Rd 16 - cheltuieli cu rezerva deductibila)
		pnProfitBrut = ln1 - ln2 + ln4 - ln5 + ln7 - ln8

		* Capital social (Rd 16 - cheltuieli cu rezerva deductibila)
		Text To lcSql Textmerge Noshow
		select sum(soldcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont = '1012' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pnCapSoc = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCapSoc)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pnCapSoc = Nvl(m.pnCapSoc, 0)

		* Rd 24 Cheltuieli impozit profit 691 (sau 698 daca nu s-a stornat cand a trecut de la micro)
		Text To lcSql Textmerge Noshow
		select sum(totcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont in ('691', '698') and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		lcCursor = [crs691]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln26 = 0
		If m.llSucces
			Select crs691
			ln26 = Round(Nvl(suma, 0), 0)
			Use In crs691
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '26' && Cheltuieli cu impozitul pe profit/venit
		If Found()
			Replace suma With m.ln26
		Endif

		* La randul 2 cheltuieli din exploatare se aduna si Cheltuiala cu impozitul pe profit (apoi se scade din randul 24 ca cheltuiala nedeductibila)
		Select cDecl
		Locate For Alltrim(Rand) = '02'
		If Found()
			Replace suma With suma + m.ln26
		Endif

		* Rd 28 Amenzi, confiscari, dobanzi 6581
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 6581]

		lcCursor = [crs6581]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln28 = 0
		If m.llSucces
			Select crs6581
			ln28 = Nvl(suma, 0)
			Use In crs6581
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '28' && Amenzi, confiscari, dobanzi
		If Found()
			Replace suma With Round(m.ln28, 0)
		Endif
		*---

		* RD 27 Cheltuieli de protocol care depašesc limita prevazuta de lege 27
		*** cheltuieli de protocol sub limita 623.protocol.limita = 2% * (121 + 691 + 623.protocol) = 2% (rd. 10 + rd.24 + 623.protocol )
		*** cheltuieli de protocol care depasesc limita prevazuta de lege = 623.protocol - 623.protocol.limita
		lcSql = [select sum(b.totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalanamama b], [vbalana b]) + ;
			[ join plcont p on b.cont = p.cont and b.acont = p.acont and b.an = p.an ] + ;
			[ where b.an = ] + Alltrim(Str(tnAnul)) + [ and b.luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(b.cont,1,3) = '623' and UPPER(p.explicatie) like '%PROTOCOL%' and p.acont is not null]

		lcCursor = [crs623]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		lnProtocol = 0
		If m.llSucces
			Select crs623
			lnProtocol = Nvl(suma, 0)
			Use In (Select('crs623'))
		Endif

		If m.lnProtocol = 0
			lcSql = [select sum(totcred) as suma ] + ;
				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
				[ and SUBSTR(cont,1,3) = '623']

			lcCursor = [crs623]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)

			If m.llSucces
				Select crs623
				lnProtocol = Nvl(suma, 0)
				Use In (Select('crs623'))
			Endif


		Endif
		ln10 = (m.ln1 - m.ln2) + (m.ln4 - m.ln5) + (m.ln7 - m.ln8)
		If m.ln10 <= 0 && profitul negativ, cheltuielile de protocol depasesc in intregime limita
			ln29 = m.lnProtocol
		Else
			lnBazaProtocol = m.ln10 + m.ln26 + m.lnProtocol
			lnProtocolLimita = m.lnBazaProtocol * 0.02
			ln29 = m.lnProtocol - m.lnProtocolLimita
			ln29 = Max(m.ln28, 0)
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '29' && Cheltuieli de protocol care depasesc limita prevazuta de lege
		If Found()
			Replace suma With Round(m.ln29, 0)
		Endif
		*---
		*!*	Select cDecl
		*!*	Locate For Alltrim(Rand) = '401'
		*!*	Replace suma With ln24 * 0.16

		* Rd 30 Cheltuieli de sponsorizare
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and cont = '6582']

		lcCursor = [crs6582]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln30 = 0
		If m.llSucces
			Select crs6582
			ln30 = Nvl(suma, 0)
			Use In (Select('crs6582'))
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '30' && DONATII SI SUBVENTII ACORDATE
		If Found()
			Replace suma With Round(m.ln30, 0)
		Endif

		* Rd 50 Impozit pe profit declarat pentru anul de raportare prin formularul 100
		* TSD 691 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '691']

		pn50 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn50)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '50'
			If Found()
				Replace suma With Round(Nvl(m.pn50, 0), 0)
			Endif
		Endif

		* Rd 51 Impozit pe veniturile microintreprinderilor declarat pentru anul de raportare prin formularul 100
		* TSD 698 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '698']

		pn51 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn51)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '51'
			If Found()
				Replace suma With Round(Nvl(m.pn51, 0), 0)
			Endif
		Endif


		* Rd 16 Rezerva legala deductibila
		* <= 5% din profitul contabil inainte de impozitare - venituri neimpozabile + cheltuieli nedeductibile
		* maxim total rezerva <= 20% capital social
		Text To lcSql Noshow Textmerge
		select sum(soldcred) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061t = 0 && sold 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061t)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pn1061t = Nvl(m.pn1061t, 0)

		Text To lcSql Noshow Textmerge
		select sum(totcred-preccred1) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061c = 0 && rulaj anual 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061c)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			ln16 = 0
			If m.pnProfitBrut > 0
				lnRezMaxAn = m.pnProfitBrut * 0.05
				lnRezMaxTotal = m.pnCapSoc * 0.20
				lnRezInPlus = Iif(m.lnRezMaxTotal < m.pn1061t, m.lnRezMaxTotal - m.pn1061t, 0)

				ln16 = Min(m.lnRezMaxAn, m.pn1061c) + m.lnRezInPlus
			Endif

			Select cDecl
			Locate For Alltrim(Rand) = '16'
			If Found()
				Replace suma With Round(Nvl(m.ln16, 0), 0)
			Endif
		Endif



	Endproc && calculare_2014

	**********************************************
	Procedure calculare_2012
		Parameters tnAnul, tnLuna

		* Generez Contul de Profit si Pierdere pentru venituri, cheltuieli exploatare, financiare, extraordinare

		Local lcCursor, lcField, lcRand, lcSel, lcSql, llSucces, ln1, ln10, ln2, ln24, ln26, ln27, ln28, ln4
		Local ln5, ln7, ln8, lnBazaProtocol, lnProtocol, lnProtocolLimita
		Private pcId_butonCur, pnCount, pnCumul, pnId_sectie, pnId_setCur, pnId_venchelt

		pnCumul = 1
		pnId_sectie = 0
		pnId_venchelt = 0
		pnCount = 0
		pcId_butonCur = [2013_2_0_20_0] && ultimul formular 20, a fost generat pentru semestrul II 2013
		pnId_setCur = 0 && -121 la 12/2013

		lcSql = [begin pack_sitfin.get_id_set(?pcId_butonCur, ?@pnId_setCur); end;]
		llSucces = goExecutor.oExecuta(lcSql)
		If !m.llSucces
			Return
		Endif

		lcSql	 = [begin PACK_MG_EVOLUTIE.GET_SET(?pnId_setCur, ?@pnCount); end;] && copii din mg_indicatori in temporarul mg_tmp_indicatori
		llSucces = goExecutor.oExecuta(lcSql)

		If !m.llSucces
			Return
		Endif

		If m.pnCount = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine indicatori!', 0 + 48, "Atentie")
			Return
		Endif

		lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(m.tnLuna)) + [,] + Alltrim(Str(m.tnAnul)) + [,] + Alltrim(Str(pnCumul)) + [,] + Alltrim(Str(pnId_sectie)) + [,] + Alltrim(Str(pnId_venchelt)) + [)}]
		* lcSel	 = [{call PACK_MG_EVOLUTIE.GEN_MG_INDICATORI(?gnLuna, ?gnAn, ?gnLuna,?gnAn, ?pnCumul, ?pnId_sectie, ?pnId_venchelt}] && ODBC error: sintax error or access violation
		lcCursor = 'crsContProfit'

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

		Select crsContProfit
		If Reccount() = 0
			amessagebox('Setul Cont Profit si Pierdere ' + pcId_butonCur + ' nu contine informatii!', 0 + 48, "Atentie")
			Return
		Endif

		lcField = Field(7, 'crsContProfit')
		Select Rand, nume_ind, Round(&lcField, 0) As valoare ;
			From crsContProfit ;
			Order By Rand ;
			Into Cursor cContProfit Readwrite

		&& denominare
		If 12 * gnAn + gnLuna <= 12 * 2005 + 6
			Select cContProfit
			Replace All valoare With Round(valoare / 1000, 0)
		Endif

		Use In (Select('crsContProfit'))

		Select cContProfit
		Scan
			lcRand = Alltrim(Rand)
			Do Case
				Case m.lcRand = '01' && cifra de afaceri
					* CIFRA DE AFACERI pentru formula rd. 45 (sponsorizari in limita 3/1000 din CA)
					* rd 45 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod) (rd. 1 din Contul de Profit si Pierdere)
					pnCA = valoare
				Case m.lcRand = '12' && venituri din exploatare
					ln1 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '01'
					If Found()
						Replace suma With m.ln1
					Endif
				Case m.lcRand = '35' && cheltuieli din exploatare
					ln2 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '02'
					If Found()
						Replace suma With m.ln2
					Endif
				Case m.lcRand = '45' && venituri financiare
					ln4 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '04'
					If Found()
						Replace suma With m.ln4
					Endif
				Case m.lcRand = '52' && cheltuieli financiare
					ln5 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '05'
					If Found()
						Replace suma With m.ln5
					Endif
				Case m.lcRand = '57' && venituri extraordinare
					ln7 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '07'
					If Found()
						Replace suma With m.ln7
					Endif
				Case m.lcRand = '58' && cheltuieli extraordinare
					ln8 = valoare

					Select cDecl
					Locate For Alltrim(Rand) = '08'
					If Found()
						Replace suma With m.ln8
					Endif
			Endcase
		Endscan && cContProfit
		Use In (Select('cContProfit'))

		* Profit brut (Rd 16 - cheltuieli cu rezerva deductibila)
		pnProfitBrut = ln1 - ln2 + ln4 - ln5 + ln7 - ln8

		* Capital social (Rd 16 - cheltuieli cu rezerva deductibila)
		Text To lcSql Textmerge Noshow
		select sum(soldcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont = '1012' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pnCapSoc = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCapSoc)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pnCapSoc = Nvl(m.pnCapSoc, 0)

		* Rd 24 Cheltuieli impozit profit 691 (sau 698 daca nu s-a stornat cand a trecut de la micro)
		Text To lcSql Textmerge Noshow
		select sum(totcred) as suma
			from  <<Iif(glEMama, [vbalmama], [vbal])>>
			where cont in ('691', '698') and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		lcCursor = [crs691]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln24 = 0
		If m.llSucces
			Select crs691
			ln24 = Round(Nvl(suma, 0), 0)
			Use In crs691
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '24' && Cheltuieli cu impozitul pe profit/venit
		If Found()
			Replace suma With m.ln24
		Endif

		* La randul 2 cheltuieli din exploatare se aduna si Cheltuiala cu impozitul pe profit (apoi se scade din randul 24 ca cheltuiala nedeductibila)
		Select cDecl
		Locate For Alltrim(Rand) = '02'
		If Found()
			Replace suma With suma + m.ln24
		Endif

		* Rd 26 Amenzi, confiscari, dobanzi 6581
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 6581]

		lcCursor = [crs6581]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln26 = 0
		If m.llSucces
			Select crs6581
			ln26 = Nvl(suma, 0)
			Use In crs6581
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '26' && Amenzi, confiscari, dobanzi
		If Found()
			Replace suma With Round(m.ln26, 0)
		Endif
		*---

		* RD 27 Cheltuieli de protocol care depašesc limita prevazuta de lege 27
		*** cheltuieli de protocol sub limita 623.protocol.limita = 2% * (121 + 691 + 623.protocol) = 2% (rd. 10 + rd.24 + 623.protocol )
		*** cheltuieli de protocol care depasesc limita prevazuta de lege = 623.protocol - 623.protocol.limita
		lcSql = [select sum(b.totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalanamama b], [vbalana b]) + ;
			[ join plcont p on b.cont = p.cont and b.acont = p.acont and b.an = p.an ] + ;
			[ where b.an = ] + Alltrim(Str(tnAnul)) + [ and b.luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(b.cont,1,3) = '623' and UPPER(p.explicatie) like '%PROTOCOL%' and p.acont is not null]

		lcCursor = [crs623]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)
		lnProtocol = 0
		If m.llSucces
			Select crs623
			lnProtocol = Nvl(suma, 0)
			Use In (Select('crs623'))
		Endif

		If m.lnProtocol = 0
			lcSql = [select sum(totcred) as suma ] + ;
				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
				[ and SUBSTR(cont,1,3) = '623']

			lcCursor = [crs623]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)

			If m.llSucces
				Select crs623
				lnProtocol = Nvl(suma, 0)
				Use In (Select('crs623'))
			Endif


		Endif
		ln10 = (m.ln1 - m.ln2) + (m.ln4 - m.ln5) + (m.ln7 - m.ln8)
		If m.ln10 <= 0 && profitul negativ, cheltuielile de protocol depasesc in intregime limita
			ln27 = m.lnProtocol
		Else
			lnBazaProtocol = m.ln10 + m.ln24 + m.lnProtocol
			lnProtocolLimita = m.lnBazaProtocol * 0.02
			ln27 = m.lnProtocol - m.lnProtocolLimita
			ln27 = Max(m.ln27, 0)
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '27' && Cheltuieli de protocol care depasesc limita prevazuta de lege
		If Found()
			Replace suma With Round(m.ln27, 0)
		Endif
		*---
		*!*	Select cDecl
		*!*	Locate For Alltrim(Rand) = '401'
		*!*	Replace suma With ln24 * 0.16

		* Rd 28 Cheltuieli de sponsorizare
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and cont = '6582']

		lcCursor = [crs6582]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		ln28 = 0
		If m.llSucces
			Select crs6582
			ln28 = Nvl(suma, 0)
			Use In (Select('crs6582'))
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '28' && DONATII SI SUBVENTII ACORDATE
		If Found()
			Replace suma With Round(m.ln28, 0)
		Endif

		* Rd 48 Impozit pe profit declarat pentru anul de raportare prin formularul 100
		* TSD 691 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '691']

		pn48 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn48)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '48'
			If Found()
				Replace suma With Round(Nvl(m.pn48, 0), 0)
			Endif
		Endif

		* Rd 49 Impozit pe veniturile microintreprinderilor declarat pentru anul de raportare prin formularul 100
		* TSD 698 30 sept
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = 9 ] + gcCondSucursala + ;
			[ and cont = '698']

		pn49 = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn49)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			Select cDecl
			Locate For Alltrim(Rand) = '49'
			If Found()
				Replace suma With Round(Nvl(m.pn49, 0), 0)
			Endif
		Endif


		* Rd 16 Rezerva legala deductibila
		* <= 5% din profitul contabil inainte de impozitare - venituri neimpozabile + cheltuieli nedeductibile
		* maxim total rezerva <= 20% capital social
		Text To lcSql Noshow Textmerge
		select sum(soldcred) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061t = 0 && sold 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061t)
		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Endif
		pn1061t = Nvl(m.pn1061t, 0)

		Text To lcSql Noshow Textmerge
		select sum(totcred-preccred1) as suma
			from <<Iif(m.glEMama, 'vbalmama', 'vbal')>>
			where cont = '1061' and an = <<m.tnAnul>> and luna = <<m.tnLuna>> <<m.gcCondSucursala>>
		Endtext

		pn1061c = 0 && rulaj anual 1061
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pn1061c)

		If m.lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
		Else
			lnRezMaxAn = m.pnProfitBrut * 0.05
			lnRezMaxTotal = m.pnCapSoc * 0.20
			lnRezInPlus = Iif(m.lnRezMaxTotal < m.pn1061t, m.lnRezMaxTotal - m.pn1061t, 0)

			ln16 = Min(m.lnRezMaxAn, m.pn1061c) + m.lnRezInPlus

			Select cDecl
			Locate For Alltrim(Rand) = '16'
			If Found()
				Replace suma With Round(Nvl(m.ln16, 0), 0)
			Endif
		Endif



	Endproc && calculare_2012

	Procedure calculare_old
		Parameters tnAnul, tnLuna

		*** adun preccred + RULCRED 7xx din bal mai putin 709 (reduceri comerciale acordate) 711
		lcSql = [select sum(preccred+rulcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and (( to_number(SUBSTR(cont,1,3)) between 700 and 759) or to_number(cont) = 781) and to_number(cont) NOT IN(709, 711)]

		lcCursor = [crsVen]
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif


		Select crsVen
		Go Top
		lnVen = Nvl(suma, 0)

		lcSql = [select sum(precdeb+ruldeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 709]

		lcCursor = [crsVen709]
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif
		Select crsVen709
		Go Top
		ln709 = Nvl(suma, 0)

		lcSql = [select sum(preccred+rulcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(cont,1,2) = '70' and to_number(cont) <> 709]

		lcCursor = [crsVen70]
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif
		Select crsVen70
		Go Top
		ln70 = Nvl(suma, 0)

		pnCA = m.ln70 - m.ln709 && CIFRA DE AFACERI pentru formula rd. 43 (sponsorizari in limita 3/1000 din CA)
		*!*	rd 43 6582 maxim 20% rd.40.1 si maxim 3/1000 CA (in cod) (rd. 1 din Contul de Profit si Pierdere) (in cod
		*!*			01. Cifra de afaceri neta (rd. 02+03-04+05+06)
		*!*			02. Produc_ia vânduta (ct.701+702+703+704+705+706+708)
		*!*			03. Venituri din vânzarea marfurilor (ct. 707)
		*!*			04. Reduceri comerciale acordate (ct. 709)
		*!*			05. Venituri din dobânzi înregistrate de entita_ile radiate din Registrul general si care
		*!*			mai au in derulare contracte de leasing (ct.766* )
		*!*			06. Venituri din subven_ii de exploatare aferente cifrei de afaceri nete (ct.7411)

		*** 711
		If .F.
			lcSql = [ select  t.suma from vact t ] + ;
				[where an = ] + Str(tnAnul) + [ and luna = ] + Str(tnLuna) + m.gcCondSucursala + ;
				[ and id_set = 90005 ] + ;
				[and to_number(scd) = 711 and to_number(scc) = 121]

			lcCursor = [crs711]
			lnSucces = goExecutor.oExecute(lcSql, lcCursor)

			If lnSucces < 0
				amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
				Return
			Endif

			Select crs711
			Select Count(suma) As nr_inreg From crs711 Into Cursor crsNrInreg711
			Select crsNrInreg711
			lnInreg = nr_inreg

			Use In crsNrInreg711

			If lnInreg > 0
				Select crs711
				lnSold711 = Nvl(suma, 0)
				Use In crs711
			Else
				Use In crs711
				&& adun soldul creditor 711 sau scad soldul debitor 711
				lcSql = [select sum(soldcred-solddeb) as suma ] + ;
					[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
					[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
					[ and to_number(cont) = 711]

				lcCursor = [crs711] && TVA de recuperat
				lnSucces = goExecutor.oExecute(lcSql, lcCursor)

				If lnSucces < 0
					amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
					Return
				Endif

				Select crs711
				lnSold711 = Nvl(suma, 0)
				Use In crs711
			Endif
		Else
			pnSold711 = 0
			goExecutor.oFunction2Value([pack_mg_evolutie.variatia_stocurilor(?gnLuna, ?gnAn)], @pnSold711)
			lnSold711 = m.pnSold711
		Endif

		ln1 = m.lnVen - m.ln709 + m.lnSold711

		Use In (Select('crsVen'))
		Use In (Select('crsVen709'))
		Use In (Select('crsVen70'))
		Use In (Select('crs711'))

		Select cDecl
		Locate For Alltrim(Rand) = '01' && venituri din exploatare
		If Found()
			Replace suma With Round(m.ln1, 0)
		Endif
		*----
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and ( SUBSTR(cont,1,2)= '76'  or substr(cont,1,3) = '786') ]

		lcCursor = [crs76] && TVA de recuperat
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Select crs76
		ln4 = Nvl(suma, 0)
		Use In crs76

		Select cDecl
		Locate For Alltrim(Rand) = '04' && venituri financiare
		If Found()
			Replace suma With Round(m.ln4, 0)
		Endif
		*---

		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and ( to_number(cont) between 660 and 669 or to_number(cont) = 686) ]

		lcCursor = [crs66] && TVA de recuperat
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Select crs66
		ln5 = Nvl(suma, 0)
		Use In crs66

		Select cDecl
		Locate For Alltrim(Rand) = '05' && cheltuieli financiare
		If Found()
			Replace suma With Round(m.ln5, 0)
		Endif
		*---

		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and ( to_number(cont) between 770 and 779 or to_number(cont) = 787) ]

		lcCursor = [crs77] && TVA de recuperat
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Select crs77
		ln7 = Nvl(suma, 0)
		Use In crs77

		Select cDecl
		Locate For Alltrim(Rand) = '07' && Venituri extraordinare
		If Found()
			Replace suma With Round(m.ln7, 0)
		Endif
		*---
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and ( to_number(cont) between 670 and 679 or to_number(cont) = 687) ]

		lcCursor = [crs67] && TVA de recuperat
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Select crs67
		ln8 = Nvl(suma, 0)
		Use In crs67

		Select cDecl
		Locate For Alltrim(Rand) = '08' && Cheltuieli extraordinare
		If Found()
			Replace suma With Round(m.ln8, 0)
		Endif

		*---
		lcSql = [select sum(totdeb) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and substr(cont,1,1)='6' ]

		lcCursor = [crs60] && TVA de recuperat
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Select crs60
		ln2 = Nvl(suma, 0)
		Use In crs60

		ln2 = m.ln2 - (m.ln5 + m.ln8) && se mai scade de la randul 2 si soldul lui 6811 (randul 14)
		Select cDecl
		Locate For Alltrim(Rand) = '02' && cheltuieli de exploatare
		If Found()
			Replace suma With Round(m.ln2, 0)
		Endif
		*---
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 691 ]

		lcCursor = [crs691]
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Select crs691
		ln24 = Nvl(suma, 0)
		Use In crs691

		Select cDecl
		Locate For Alltrim(Rand) = '24' && Cheltuieli cu impozitul pe profit
		If Found()
			Replace suma With Round(m.ln24, 0)
		Endif
		*---
		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and to_number(cont) = 6581]

		lcCursor = [crs6581]
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Select crs6581
		ln26 = Nvl(suma, 0)
		Use In crs6581

		Select cDecl
		Locate For Alltrim(Rand) = '26' && Amenzi, confiscari, dobanzi
		If Found()
			Replace suma With Round(m.ln26, 0)
		Endif
		*---

		*** cheltuieli de protocol sub limita 623.protocol.limita = 2% * (121 + 691 + 623.protocol) = 2% (rd. 10 + rd.24 + 623.protocol )
		*** cheltuieli de protocol care depasesc limita prevazuta de lege = 623.protocol - 623.protocol.limita
		lcSql = [select sum(b.totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalanamama b], [vbalana b]) + ;
			[ join plcont p on b.cont = p.cont and b.acont = p.acont and b.an = p.an ] + ;
			[ where b.an = ] + Alltrim(Str(tnAnul)) + [ and b.luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and SUBSTR(b.cont,1,3) = '623' and UPPER(p.explicatie) like '%PROTOCOL%' and p.acont is not null]

		lcCursor = [crs623]
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)
		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Select crs623
		lnProtocol = Nvl(suma, 0)
		Use In (Select('crs623'))

		If m.lnProtocol = 0
			lcSql = [select sum(totcred) as suma ] + ;
				[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
				[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
				[ and SUBSTR(cont,1,3) = '623']

			lcCursor = [crs623]
			lnSucces = goExecutor.oExecute(lcSql, lcCursor)

			If lnSucces < 0
				amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
				Return
			Endif

			Select crs623
			lnProtocol = Nvl(suma, 0)
			Use In (Select('crs623'))
		Endif
		ln10 = (m.ln1 - m.ln2) + (m.ln4 - m.ln5) + (m.ln7 - m.ln8)
		If m.ln10 <= 0 && profitul negativ, cheltuielile de protocol depasesc in intregime limita
			ln27 = m.lnProtocol
		Else
			lnBazaProtocol = m.ln10 + m.ln24 + m.lnProtocol
			lnProtocolLimita = m.lnBazaProtocol * 0.02
			ln27 = m.lnProtocol - m.lnProtocolLimita
			ln27 = Max(m.ln27, 0)
		Endif

		Select cDecl
		Locate For Alltrim(Rand) = '27' && Cheltuieli de protocol care depasesc limita prevazuta de lege
		If Found()
			Replace suma With Round(m.ln27, 0)
		Endif
		*---
		*!*	Select cDecl
		*!*	Locate For Alltrim(Rand) = '401'
		*!*	Replace suma With ln24 * 0.16

		lcSql = [select sum(totcred) as suma ] + ;
			[ from ] + Iif(glEMama, [vbalmama], [vbal]) + ;
			[ where an = ] + Alltrim(Str(tnAnul)) + [ and luna = ] + Alltrim(Str(tnLuna)) + gcCondSucursala + ;
			[ and cont = '6582']

		lcCursor = [crs6582]
		lnSucces = goExecutor.oExecute(lcSql, lcCursor)

		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Select crs6582
		ln28 = Nvl(suma, 0)
		Use In (Select('crs6582'))

		Select cDecl
		Locate For Alltrim(Rand) = '28' && DONATII SI SUBVENTII ACORDATE
		If Found()
			Replace suma With Round(m.ln28, 0)
		Endif

	Endproc && calculare_2012

	Procedure do_genereaza_txt
		Parameters tcTabel, tcPerioada


		Local lcSir, lcDirTabele, lcFisUnitate, lcCodFiscal, lcDirDecl

		*!*	gcDecl_caleExport = "C:\Program Files\Declaratii fiscale " + Alltrim(Str(gnAn))
		gcDecl_caleExport = "C:\Program Files\Declaratia 101 impozit pe profit\"
		If !Directory(gcDecl_caleExport)
			gcDecl_caleExport = Getdir("C:", "Directorul Declaratia 101", "Declaratia 101")
		Endif
		If Empty(gcDecl_caleExport)
			Return
		Endif

		lcDirTabele = Addbs(Alltrim(gcDecl_caleExport)) + Alltrim(gofirma.firma) + "\"
		lcFisUnitate = lcDirTabele + "Unitate.dbf"
		If !File(lcFisUnitate)
			amessage("Nu ati salvat Date de identificare unitate!", 0 + 48, _Screen.Caption)
			Return
		Endif

		Use (lcFisUnitate) In 0 Again Alias unitate
		Select unitate
		lcCodFiscal = Alltrim(Str(codi))

		lcDirDecl = Addbs(Alltrim(gcDecl_caleExport)) + lcCodFiscal


		*** creez unitate.txt
		Set Textmerge To
		lcFile = lcDirTabele + "adresa.txt"
		Set Textmerge On To (lcFile) Noshow


		Select unitate
		lcCodFiscal = Alltrim(Str(codi))

		Select unitate
		Go Top
		lcSir = "##,#" + Alltrim(Str(codi)) + "#,#" + ;
			Alltrim(deni) + "#,#" + ;
			Alltrim(deni) + "#,#" + ;
			Alltrim(adr) + "#,#" + ;
			Alltrim(telf) + "#,#" + ;
			Alltrim(banca) + "#,#" + ;
			Alltrim(Cont) + "#,#" + ;
			"#,##,##,#" + ;
			Alltrim(intocmitf) + "#,#" + ;
			Alltrim(calIntoc) + "#," + ;
			Alltrim(Str(caen))

		If Used('unitate')
			Use In unitate
		Endif

	\\<<lcSir>>
		Set Textmerge To
		Wait Window "S-a exportat adresa.txt..." Timeout 1


		If !Directory(lcDirDecl)
			Md (lcDirDecl)
		Endif

		*** creez Dec100T.txt
		*lcFile = lcDirTabele + "\Dec101T.txt"
		lcFile = lcDirDecl + "\Dec101T.txt"

		lcSir = ''
		lcTrim = ''

		Do Case
			Case tcPerioada = '01/10 - 09/10'
				lnLuna = 9
				lnAnul = 2010
				lcTrim = 'T3'
			Case tcPerioada = '10/10 - 12/10'
				lnLuna = 12
				lnAnul = 2010
				lcTrim = 'T4'
			Case tcPerioada = '01/10 - 12/10'
				lnLuna = 12
				lnAnul = 2010
				lcTrim = 'AN'
			Case Lower(Alltrim(tcPerioada)) = 'luna contabila curenta'
				lnLuna = gnLuna
				lnAnul = gnAn
				lcTrim = ''
		Endcase

		lcSir = lcCodFiscal + ',' + Padl(lnLuna, 2, '0') + ',' +  Alltrim(Str(lnAnul, 4, 0)) + ',#' + lcTrim + '#,,#' + gofirma.caen + '#,'


		lnvirgule = 0
		Select (tcTabel)
		Scan For !(Empty(Rand) Or Isnull(Rand))
			If suma = 0 Or Isnull(suma)
				lcSir = lcSir + ','
			Else
				lcSir = lcSir + Alltrim(Str(suma, 15, 0)) + ','
			Endif
		Endscan


		lcSir = lcSir + '##'
		&& lcSir = lcSir + Chr(13) + Chr(10)

		***---


		If File(lcFile)
			lcStr = Filetostr(lcFile)
			Set Textmerge On To (lcFile) Noshow
			Alines(sir, lcStr)

			lcInceput = lcCodFiscal + ',' + Padl(lnLuna, 2, '0') + ',' +  Alltrim(Str(lnAnul, 4, 0)) + ',#' + lcTrim + '#'
			lnInceput = Len(lcInceput)
			llGasit = .F.

			For i = 1 To Alen(sir)
				If Substr(sir[i], 1, lnInceput) = lcInceput
					sir[i] = lcSir
					lcSirul = lcSir
					llGasit = .T.
				Endif
			Endfor

			If !llGasit
				&& appe - scrie la sfarsit
				For i = 1 To Alen(sir)
					lcLinia = sir[i] + Chr(13) + Chr(10)
			\\<<lcLinia>>
				Endfor
	\\<<lcSir>>
			Else
				&& se scriu liniile inapoi in fisier; linia care se modifica este modificata deja in array
				lcLinia = ''
				For i = 1 To Alen(sir)
					lcLinia = sir[i] + Chr(13) + Chr(10)
				\\<<lcLinia>>

				Endfor
			Endif
		Else
			Set Textmerge On To (lcFile) Noshow
	\\<<lcSir>>
		Endif

		Set Textmerge To

		Wait Window "S-a exportat Dec101T.txt..." Timeout 1

		lcA = lcDirTabele + "\" + "adresa.txt"
		lcD = lcDirTabele + "\" + "dec101T.txt"


		lcFisAdresa = lcDirDecl + "\adresa.txt"
		lcfisDec101T = lcDirDecl + "\dec101T.txt"

		Copy File (lcA) To (lcFisAdresa)
		*Copy File (lcD) To (lcfisDec101T)

		amessagebox([Fisierele au fost exportate in directorul ] + Alltrim(Addbs(Justpath(lcfisDec101T))) + Chr(13) + Chr(10) + ;
			  [Intrati in programul Declaratia 101], 0 + 64, 'Decl. 101')
	Endproc && do_genereaza_txt
	*==========================================
Enddefine && D101

*********************************************************
*** Cifra de afaceri pentru TVA trimestrial/lunar
*** DECLARATIA 094
* Cifra de afaceri dpdv perioada fiscala TVA conform art. 322, alin. (2)
* Operatiuni taxabile si/sau scutite cu drept de deducere si/sau neimpozabile în România

* cuprinde: toate operatiunile taxabile, scutite cu drept de deducere, cu taxare inversa si cele neimpozabile
* nu cuprinde: operatiuni scutite fara drept de deducere
*********************************************************
Procedure viz_decl094
	Local loD094 As "D094"
	loD094 = Createobject("D094") && ovanzcump.vcx
	loD094.do_executa()
Endproc

Define Class D094 As Custom

	nAn	= 0
	nLuna = 0
	cCodFiscal = 0
	cCodCaen = []
	lPlatitorTVA = .F.
	cDenumireFirma = []
	cAdresa = []
	cTelefon = []
	cFax = []
	cEmail = []
	cDeclarant = []
	cNumeDeclarant = []
	cPrenumeDeclarant = []
	cFunctieDeclarant = []

	nCifraDeAfaceri = 0

	Procedure Init
		With This
			.nAn = gnAn
			.nLuna = m.gnLuna
			.cCodFiscal = Strtran(Alltrim(Upper(Nvl(gofirma.cod_fiscal, ''))), 'RO', '')
			.cCodCaen = Alltrim(gofirma.caen)
			.lPlatitorTVA = (Substr(Alltrim(Upper(Nvl(gofirma.cod_fiscal, ''))), 1, 2) == 'RO')
			.cDenumireFirma = Nvl(Alltrim(gofirma.firma), '')
			.cAdresa = Nvl(Alltrim(gofirma.adresa), '')
			.cTelefon = Nvl(Alltrim(gofirma.telefon), '')
			.cFax = Nvl(Alltrim(gofirma.fax), '')
			.cEmail = Nvl(Alltrim(gofirma.email), '')
			.cDeclarant = Alltrim(gofirma.declarant)
			.cNumeDeclarant = Alltrim(gofirma.nume_declarant)
			.cPrenumeDeclarant = Alltrim(gofirma.prenume_declarant)
			.cFunctieDeclarant = Alltrim(gofirma.functie)

			.nCifraDeAfaceri = 0
		Endwith
	Endproc

	Procedure do_executa
		Local loFrmDoc As "frm_decl_tipdoc"
		Local lcAppPath, lcCaleFisier, lcCaleFisierPDF, lcCaleTemp, lcComunPath, lcConditieAct, lcCursor
		Local lcFileName, lcFileNamePDF, lcFirma, lcFisierPDFNou, lcFisierPDFOriginal, lcFisierTXT
		Local lcFisierXDP, lcFisierXLS, lcMesaj, lcNumeFisier, lcRun, lcSelect, lcSirId, lcSql, llSucces
		Local lnPos, loEx
		Local lcTextAchIntracomunitar
		lcSelect = Select()
		*** Initializare cai export
		lcAppPath = Addbs(gcAppPath)
		lcComunPath = lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If lnPos > 0
			lcComunPath = Left(m.lcAppPath, lnPos)
		Endif
		lcComunPath = lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		*** Creez structura director export : caletemp\D392\aaaa\firma\
		llSucces = .F.
		Try
			lcCaleTemp = m.gcBasePath
			lcCaleFisier = m.lcCaleTemp + "D094\"
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcFirma = Iif(gcAppName = "CONCONT", Alltrim(gofirma.fscurt), m.gcFirma)
			lcCaleFisier = m.lcCaleFisier + Strtran(lcFirma, [ ], [_]) + "\"

			lcCaleFisierPDF = m.lcCaleFisier  && c:\temp\D392\2014\firma\

			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			llSucces = .T.
		Catch To loEx
			lcMesaj = "Creare director " + lcCaleFisier + " " + loEx.Message

			amessagebox(lcMesaj, 0 + 48)
		Endtry
		If !llSucces
			Return
		Endif

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', Strtran(Alltrim(gofirma.cod_fiscal), 'RO', ''))
		Endif

		lcNumeFisier = "d094_" + gofirma.codfiscalfro + "_" + Alltrim(Str(gnAn))
		Do Case
			Case gnAn >= 2020
				lcFisierPDFOriginal = lcComunPath + "d094_2020.pdf"

			Otherwise
				lcFisierPDFOriginal = lcComunPath + "d094_2020.pdf"
		Endcase

		This.UpdatePDF(m.lcFisierPDFOriginal) && actualizez pdf de la romfast

		lcFileName = m.lcCaleFisier + lcNumeFisier

		lcFileNamePDF = m.lcCaleFisierPDF + lcNumeFisier
		lcFisierPDFNou = m.lcFileNamePDF + [.pdf]
		lcFisierXDP = m.lcFileNamePDF + [.xdp]


		If File(lcFisierPDFNou)
			Delete File (lcFisierPDFNou)
		Endif
		If File(lcFisierXDP)
			Delete File (lcFisierXDP)
		Endif

		*** citesc JV
		Text To lcSql Textmerge Noshow
SELECT ROUND(SUM(RO24B + RO20B + RO19B + RO9B + RO5B + ROTI + WRN + WRSCDD), 0) AS BAZA
	 FROM vjv2013
	 WHERE AN = <<ALLTRIM(STR(This.nAn))>>
    <<gcCondSucursala>>
		Endtext

		lcCursor = [crsJV]
		llSucces = goExecutor.oExecuta(lcSql, lcCursor)

		If m.llSucces

			Text To lcSql Textmerge Noshow
select luna, valoare
  from (select luna,
               sum(cebcb + cebvb + cebvsc + cebvn + cebfb + cebfsc + cebfn +
                   ce24sb + ce20sb + ce19sb + ce9sb + ce5sb + ce24b + ce20b) as valoare
          from jc2007
		 WHERE AN = <<ALLTRIM(STR(This.nAn))>>
         group by luna)
 where valoare <> 0
			Endtext

			lcCursor = [crsJCCE]
			llSucces = goExecutor.oExecuta(lcSql, lcCursor)
			If m.llSucces
				If Reccount('crsJCCE') > 0
					lcTextAchIntracomunitar = 'Atentie! Exista achizitii intracomunitare in lunile urmatoare:' + Chr(13) + Chr(10)
					Select crsJCCE
					Scan
						lcTextAchIntracomunitar = lcTextAchIntracomunitar + 'Luna ' + Padl(luna, 2, '0') + ' : ' + Alltrim(Str(valoare, 20, 2)) + Chr(13) + Chr(10)
					Endscan
					amessagebox(m.lcTextAchIntracomunitar, 0 + 48, _Screen.Caption)
				Endif

			Endif
		Endif && llSucces

		If m.llSucces
			*!*	D094
			Select crsJV
			This.nCifraDeAfaceri = Nvl(crsJV.baza, 0) && cifra de afaceri in scopuri TVA

			Do Case
				Case This.nAn >= 2020
					This.do_genereaza_xdp_2020(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

					If File(m.lcFisierXDP)
						open_default_app(m.lcFisierXDP)
					Endif

				Otherwise
				Case This.nAn >= 2020
					This.do_genereaza_xdp_2020(lcFisierPDFOriginal, lcFisierXDP, lcFisierPDFNou)

					If File(m.lcFisierXDP)
						open_default_app(m.lcFisierXDP)
					Endif
			Endcase

			*** deschid explorer in directorul de export
			lcRun = [RUN /N7 explorer.exe "] + m.lcCaleFisier + ["]
			&lcRun
		Endif && llSucces

		Use In (Select('crsJV'))
		Select (m.lcSelect)

	Endproc && do_executa

	*==========================================
	Procedure do_genereaza_xdp_2020
		Lparameters tcFisierPDFOriginal, tcFisierXDP, tcFisierPDFNou

		Local lcSelect, lcXDP
		lcSelect = Select()


		lcXDP = ""
		With This
			Text To lcXDP Additive Textmerge Noshow Pretext 1
<?xml version="1.0"?>
<?xfa generator="XFA2_4" APIVersion="3.3.10270.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>

<form1>
	<sub1>
		<luna_r><<Alltrim(Str(.nLuna))>></luna_r>
		<an_r><<Alltrim(Str(.nAn))>></an_r>
		<d_rec>0</d_rec>
	</sub1>
	<bord1 xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:dataNode="dataGroup"/>
	<subA>
		<RO1>RO</RO1>
		<cif><<Alltrim(.cCodFiscal)>></cif>
		<DENUMIRE><<Alltrim(.cDenumireFirma)>></DENUMIRE>
		<ADRESA><<Alltrim(.cAdresa)>></ADRESA>
		<text_telefon><<Alltrim(.cTelefon)>></text_telefon>
		<text_email><<Alltrim(.cEmail)>></text_email>
		<denR/>
		<totalPlata_A><<Alltrim(Str(.nCifraDeAfaceri))>></totalPlata_A>
		<cifR/>
		<text_fax><<Alltrim(.cFax)>></text_fax>
	</subA>
	<sub2>
		<Functia><<Alltrim(.cFunctieDeclarant)>></Functia>
		<Nume><<Alltrim(.cDeclarant)>></Nume>
	</sub2>
</form1>
</xfa:data>
</xfa:datasets>
<pdf href="<<JUSTFNAME(tcFisierPDFNou)>>" xmlns="http://ns.adobe.com/xdp/pdf/"/>
</xdp:xdp>
			Endtext
		Endwith
		Wait Window "Se exporta datele in format PDF" Nowait
		Strtofile(lcXDP, tcFisierXDP)
		If File(tcFisierXDP)
			*** copiez fisierul pdf inteligent original in directorul de export
			If File(m.tcFisierPDFOriginal)
				Copy File (tcFisierPDFOriginal) To (tcFisierPDFNou)
			Else
				amessagebox('Nu exista fisierul ' + m.tcFisierPDFOriginal, 0 + 48, _Screen.Caption)
			Endif
		Endif

		Select (lcSelect)
	Endproc && do_genereaza_xdp_2014

	Procedure UpdatePDF
		Lparameters tcPDFFile
		*** tcPDFFile: calea completa a fisierului pdf D:\CONTAFIN\COMUNCONTAFIN\d392.pdf

		Local lcFile, lcPath, llOpen
		lcFile = Justfname(m.tcPDFFile)
		lcPath = Addbs(Justpath(m.tcPDFFile))
		llOpen = .F.

		Return DownloadSupportFile(m.lcFile, m.lcPath, m.llOpen) && proceduri_comune.prg

	Endproc && UpdatePDF

Enddefine && D094
*=======================================================================*


*** notificare aplicare TVA la incasare
Procedure viz_decl097
	Local loD097 As "D097"
	loD097 = Createobject("D097")
	loD097.do_executa()
Endproc && viz_decl097

Define Class D097 As Custom
	nCifraAfaceri = 0 &&& Cifra de afaceri
	nPlafonCifraAfaceri = 2250000 &&& sub acest plafon se aplica TVA la incasare
	dData = {} &&& data de la care se face declaratia
	cPerioada = '' &&& perioada cifrei de afaceri

	Procedure do_executa
		*** EXPORT D097
		Local lcAppPath, lcCaleFisier, lcCaleFisierRTF, lcComunPath, lcFileName, lcFileNameRTF, lcFirma
		Local lcFisierRTFNou, lcFisierRTFOriginal, lcJV, lcLunaBetween, lcMesaj, lcNumeFisier, lcRun
		Local lcTempPath, ldData1, ldData2, llSucces, lnAn, lnAn1, lnAn2, lnLuna, lnLuna1, lnLuna2
		Local lnNrLuni1, lnNrLuni2, lnPos, loEx
		Local lcSelect, lcSql, lnSucces
		Private pnCifraAfaceri
		pnCifraAfaceri = 0

		lcSelect = Select()

		lcAppPath = Addbs(gcAppPath)
		lcComunPath = lcAppPath
		lnPos = Rat("\", Addbs(m.lcAppPath), 2)
		If lnPos > 0
			lcComunPath = Left(m.lcAppPath, lnPos)
		Endif
		lcComunPath = lcComunPath + Iif(gcAppName = "CONCONT", "COMUNCONTAFIN\", "COMUNROA\") && d:\contafin\comuncontafin\

		*** Creez structura director export (caletemp\D394\aaaa_ll\firma\
		llSucces = .F.
		Try
			lcTempPath = m.gcBasePath
			lcCaleFisier = m.lcTempPath + "D097\" && c:\temp\D394\

			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisier = m.lcCaleFisier + Alltrim(Str(gnAn)) + "_" + Padl(Alltrim(Str(gnLuna)), 2, "0") + "\"&& c:\temp\D097\2011_01\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcFirma = Iif(gcAppName = "CONCONT", Alltrim(gofirma.fscurt), m.gcFirma)
			lcCaleFisier = m.lcCaleFisier + Strtran(lcFirma, [ ], [_]) + "\" && c:\temp\D390\2011_01\firma\
			If !Directory(m.lcCaleFisier)
				Md (m.lcCaleFisier)
			Endif

			lcCaleFisierRTF = m.lcCaleFisier  && c:\temp\D097\2011_01\firma\
			If !Directory(m.lcCaleFisierRTF)
				Md (m.lcCaleFisierRTF)
			Endif

			llSucces = .T.
		Catch To loEx
			lcMesaj = "Creare director " + lcCaleFisier + " " + loEx.Message

			amessagebox(lcMesaj, 0 + 48)
		Endtry
		If !llSucces
			Return
		Endif

		If Type('gofirma.codfiscalfro') = 'U'
			AddProperty(gofirma, 'codfiscalfro', Strtran(Alltrim(gofirma.cod_fiscal), 'RO', ''))
		Endif

		lcNumeFisier = "d097_" + gofirma.codfiscalfro + "_" + Alltrim(Str(gnAn)) + "_" + Padl(gnLuna, 2, "0")
		lcFisierRTFOriginal = lcComunPath + "d097.rtf" && d:\contafin\comuncontafin\d097.rtf

		This.UpdateRTF(m.lcFisierRTFOriginal) && actualizez pdf de la romfast

		lcFileName = m.lcCaleFisier + lcNumeFisier

		lcFileNameRTF = m.lcCaleFisierRTF + lcNumeFisier
		lcFisierRTFNou = m.lcFileNameRTF + [.rtf]

		lnAn2 = m.gnAn
		lnLuna2 = m.gnLuna
		ldData2 = Date(m.lnAn2, m.lnLuna2, 1)

		ldData1 = Date(lnAn2, 1, 1) && Gomonth(m.ldData2, -11)
		lnAn1 = Year(m.ldData1)
		lnLuna1 = Month(m.ldData1)

		ldData2 = Gomonth(m.ldData2, 1) - 1

		lnNrLuni1 = lnAn1 * 12 + lnLuna1
		lnNrLuni2 = lnAn2 * 12 + lnLuna2

		This.dData = Iif(m.gnAn = 2012, Date(2013, 1, 1), Gomonth(Date(m.lnAn2, m.lnLuna2, 1), 1))
		This.cPerioada = Dtoc(m.ldData1) + ' - ' + Dtoc(m.ldData2)

		*** TIPURI DOCUMENTE
		Text To lcSql Textmerge Noshow
	SELECT distinct 1 AS ALES, FEL_DOCUMENT AS FDOC, ID_FDOC
		FROM vjv2010
		WHERE an*12+luna between <<m.lnNrLuni1>> and <<m.lnNrLuni2>> and NVL(id_fdoc,0) <> 0
		Endtext

		*!* 05.11.2009 ^
		lnSucces = goExecutor.oExecute(lcSql, "crsFdoc")
		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
			Return
		Endif

		Go Top In crsFDoc

		loFrmDoc = Createobject("frm_decl_tipdoc", "097")
		loFrmDoc.label1.Caption = "Debifati tipurile de documente care nu doriti"
		loFrmDoc.label2.Caption = "sa participe la calculul Cifrei de Afaceri"
		loFrmDoc.Show(1)

		lcSirId = ""
		Select id_fdoc From crsFDoc With (Buffering = .T.) Where ales = 1 Into Cursor crsFdocTemp
		If _Tally = 0
			amessagebox('Trebuie sa alegeti un tip de document!', 0 + 48, 'Atentie')
			Use In crsFDoc
			Use In crsFdocTemp
			Return
		Endif
		Use In crsFDoc
		Select crsFdocTemp
		Scan
			lcSirId = 	lcSirId + [, ] + Alltrim(Str(id_fdoc))
		Endscan
		If !Empty(lcSirId)
			lcSirId = Substr(lcSirId, 2)
		Endif
		Use In crsFdocTemp

		*!*	Mentionam ca potrivit art .134^2 lit. a din Codul fiscal:
		*!*	"Cifra de afaceri pentru calculul plafonului de 2.250.000 lei este constituita din valoarea totala
		*!*	a livrarilor de bunuri si a prestarilor de servicii taxabile si/sau scutite cu drept de deducere,
		*!*	precum si a operatiunilor rezultate din activitati economice pentru care locul livrarii/prestarii
		*!*	se considera ca fiind in strainatate, conform art. 132 si 133, realizate in cursul unui an calendaristic."

		Text To lcSql Textmerge Noshow
		select sum(ro24b + ro24nb + ro20b + ro20nb + ro19b + ro19nb + ro9b + ro9nb + ro5b + ro5nb + roti + cescdd1 + cescdd2 + wrscdd + fodd + fofdd + wrscfdd + wrn + ceoptr + cesvdd + cesvfdd + cesvfs)
			from vjv2010
			where an*12+luna between <<m.lnNrLuni1>> and <<m.lnNrLuni2>>
			AND EXTRACT(YEAR FROM dataireg) = an and EXTRACT(MONTH FROM dataireg) = luna 
			AND ID_FDOC IN (<<m.lcSirId>>)
		Endtext
		pnCifraAfaceri = 0
		lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnCifraAfaceri)
		This.nCifraAfaceri = Int(Round(m.pnCifraAfaceri, 0))

		This.do_genereaza_rtf(m.lcFisierRTFOriginal, m.lcFisierRTFNou)

		If File(m.lcFisierRTFNou)
			open_default_app(m.lcFisierRTFNou)
		Endif

		*** deschid explorer in directorul de export
		lcRun = [RUN /N7 explorer.exe "] + m.lcCaleFisier + ["]
		&lcRun

		Select (m.lcSelect)
	Endproc && do_executa
	*===========================================
	Procedure do_genereaza_rtf
		Lparameters tcFisierRTFOriginal, tcFisierRTFNou

		Local lcCADepasit, lcCANeDepasit, lcDataDepasit, lcDataNeDepasit, lcPerioadaDepasit, lcIntrare, lcIesire
		Local lcPerioadaNeDepasit, lcSelect, lcText, lnCifraAfaceri, lnPlafonCifraAfaceri
		lcSelect = Select()

		lnCifraAfaceri = This.nCifraAfaceri
		lnPlafonCifraAfaceri = This.nPlafonCifraAfaceri
		lcCADepasit = '.......'
		lcCANeDepasit = '.......'
		lcDataDepasit = '.......'
		lcPerioadaDepasit = '.......'
		lcDataNeDepasit = '.......'
		lcPerioadaNeDepasit = '.......'
		lcIntrare = ''
		lcIesire = ''
		If lnCifraAfaceri > m.lnPlafonCifraAfaceri
			lcCADepasit = formateaza(m.lnCifraAfaceri, 16, m.gnPA)
			lcDataDepasit = Dtoc(This.dData)
			lcPerioadaDepasit = This.cPerioada
			lcIesire = 'X'
		Else
			lcCANeDepasit = formateaza(m.lnCifraAfaceri, 16, m.gnPA)
			lcDataNeDepasit = Dtoc(This.dData)
			lcPerioadaNeDepasit = This.cPerioada
			lcIntrare = 'X'
		Endif


		If !File(m.tcFisierRTFOriginal)
			amessagebox('Nu exista fisierul ' + m.tcFisierRTFOriginal, 0 + 48, _Screen.Caption)
			Return
		Endif

		lcText = Filetostr(m.tcFisierRTFOriginal)

		*** completare RTF
		lcText = Strtran(m.lcText, '<I>', m.lcIntrare)
		lcText = Strtran(m.lcText, '<O>', m.lcIesire)
		lcText = Strtran(m.lcText, '<DENUMIRE>', Alltrim(Nvl(gofirma.antet, '')))
		lcText = Strtran(m.lcText, '<CF>', Alltrim(Nvl(gofirma.codfiscalfro, '')))
		lcText = Strtran(m.lcText, '<JUDET>', Alltrim(Nvl(gofirma.JUDET, '')))
		lcText = Strtran(m.lcText, '<SCT>', Alltrim(Nvl(gofirma.sector, '')))
		lcText = Strtran(m.lcText, '<LOCALITATE>', Alltrim(Nvl(gofirma.LOCALITATE, '')))
		lcText = Strtran(m.lcText, '<STRADA>', Alltrim(Nvl(gofirma.STRADA, '')))
		lcText = Strtran(m.lcText, '<NR>', Alltrim(Nvl(gofirma.NUMAR, '')))
		lcText = Strtran(m.lcText, '<BL>', Alltrim(Nvl(gofirma.Bloc, '')))
		lcText = Strtran(m.lcText, '<SC>', Alltrim(Nvl(gofirma.SCARA, '')))
		lcText = Strtran(m.lcText, '<ET>', '')
		lcText = Strtran(m.lcText, '<AP>', Iif(!Empty(Nvl(gofirma.APART, 0)), Alltrim(Str(gofirma.APART)), ''))
		lcText = Strtran(m.lcText, '<CP>', Alltrim(Nvl(gofirma.cod_postal, '')))
		lcText = Strtran(m.lcText, '<TELEFON>', Alltrim(Nvl(gofirma.telefon, '')))
		lcText = Strtran(m.lcText, '<FAX>', Alltrim(Nvl(gofirma.fax, '')))
		lcText = Strtran(m.lcText, '<EMAIL>', Lower(Alltrim(Nvl(gofirma.email, ''))))
		lcText = Strtran(m.lcText, '<CA_DEPASIT>', m.lcCADepasit)
		lcText = Strtran(m.lcText, '<DATA_DEPASIT>', m.lcDataDepasit)
		lcText = Strtran(m.lcText, '<PERIOADA_DEPASIT>', m.lcPerioadaDepasit)
		lcText = Strtran(m.lcText, '<CA_NEDEPASIT>', m.lcCANeDepasit)
		lcText = Strtran(m.lcText, '<DATA_NEDEPASIT>', m.lcDataNeDepasit)
		lcText = Strtran(m.lcText, '<PERIOADA_NEDEPASIT>', m.lcPerioadaNeDepasit)
		lcText = Strtran(m.lcText, '<DECLARANT>', Alltrim(Nvl(gofirma.declarant, '')))
		lcText = Strtran(m.lcText, '<FUNCTIA>', Alltrim(Nvl(gofirma.functie, '')))

		Strtofile(m.lcText, m.tcFisierRTFNou)

		Select (m.lcSelect)

	Endproc && do_genereaza_rtf

	*==========================================
	Procedure UpdateRTF
		Lparameters tcRTFFile
		*** tcPDFFile: calea completa a fisierului pdf D:\CONTAFIN\COMUNCONTAFIN\d097.pdf

		Local lcFile, lcPath, llOpen

		lcFile = Justfname(m.tcRTFFile)
		lcPath = Addbs(Justpath(m.tcRTFFile))
		llOpen = .F.

		Return DownloadSupportFile(m.lcFile, m.lcPath, m.llOpen) && proceduri_comune.prg

	Endproc && UpdateRTF

Enddefine && d097


******************************************* INCEPUT: viz_regvanz *******************************************
Procedure viz_regvanz
	*** verificare INAINTE_DE

	*:Global nuexista, pnRecCnt
	Local oreg As "frm_regvanz2025"
	Local lcCoresp, lcExceptii, lcFiltruOriginal, lcGroup, lcSqlCount, llAfisare, llAfisareTot
	Local llModParam, llSucces, lnInregistrariAfisare, lnProcTvaRed, lnProcTvaSt

	lcCoresp = ['4111']
	lcExceptii = ['667,622']

	Use In (Select('actcv'))

	Private lcschema, lcselect, lcfiltru, lcorder, povanz
	Store "" To povanz

	* Calculez numarul de inregistrari. Daca numarul este mai mic de min(300, gnInregistrariAfisare) afisez toate inregistrarile in loc sa intru cu 1=2
	lnInregistrariAfisare = Iif(Type('gnInregistrariAfisare') = 'N', m.gnInregistrariAfisare, 300)

	lcSqlCount = [SELECT COUNT(*) as nr FROM jv2007 where an=?gnAn and luna=?gnLuna]
	pnRecCnt = 0
	llSucces = goExecutor.oSelecteaza2Value(m.lcSqlCount, @pnRecCnt)
	llAfisareTot = (Nvl(m.pnRecCnt, 0) <= m.lnInregistrariAfisare)

	Do Case
		Case m.gnAn * 12 + m.gnLuna >= 2025 * 12 + 8
			*** 01/08/2025 21%, 11%
			Do inainte With "VANZ2007" In oinainte_de.prg

			TEXT TO lcSchema NOSHOW 
ID_JV I, ID_FACT I, COD I, AN I, LUNA I, DATAACT D, DATAIREG D, SERIE_ACT C(10), NRACT I, ID_PART I, ID_FDOC I, FDOC C(30), FEL_DOCUMENT C(30), TIP_DOC C(10), DENUMIRE C(100), COD_FISCAL C(30), TIP_PERSOANA I, AFILIAT I, CODTARA C(5), ID_SUCURSALA I, SUCURSALA C(250), TOTCTVA N(20,4), RO11B N(20,4), RO11T N(20,4), RO21B N(20,4), RO21T N(20,4), RO19B N(20,4), RO19T N(20,4), RO9B N(20,4), RO9T N(20,4), RO5B N(20,4), RO5T N(20,4), RO24B N(20,4), RO24T N(20,4), RO20B N(20,4), RO20T N(20,4), ROTI N(20,4), CESCDD1 N(20,4), CESCDD2 N(20,4), CEOPTR N(20,4), CESVDD N(20,4), CESVFDD N(20,4), CESVFS N(20,4), WRSCDD N(20,4), FODD N(20,4), FOFDD N(20,4), WRSCFDD N(20,4), WRN N(20,4), RORTC11 N(20,4), RORTC21 N(20,4), RORTC19 N(20,4), RORTC9 N(20,4), RORTC5 N(20,4), RO11NB N(20,4), RO11NT N(20,4), RO21NB N(20,4), RO21NT N(20,4), RO19NB N(20,4), RO19NT N(20,4), RO9NB N(20,4), RO9NT N(20,4), RO5NB N(20,4), RO5NT N(20,4), RO24NB N(20,4), RO24NT N(20,4), RO20NB N(20,4), RO20NT N(20,4), TOTFTVATAX N(20,4), TOTTVATAX N(20,4), TVA_INCASARE I,CONT C(4), ales N(1), diferenta_incasare_tva n(20,4), incasat_exigibil N(20,4), incasat_tot N(20,4), solddif N(20,4), soldf N(20,4) 
		    ENDTEXT 


			TEXT TO lcSelect noshow
select ID_JV, ID_FACT, COD, AN, LUNA, DATAACT, DATAIREG, SERIE_ACT, NRACT, ID_PART, ID_FDOC, FEL_DOCUMENT as FDOC, FEL_DOCUMENT, TIP_DOC, DENUMIRE, COD_FISCAL, 
TIP_PERSOANA, AFILIAT, CODTARA, ID_SUCURSALA, SUCURSALA, 
TOTCTVA, RO11B, RO11T, RO21B, RO21T, RO19B, RO19T, RO9B, RO9T, RO5B, RO5T, RO24B, RO24T, RO20B, RO20T, ROTI, CESCDD1, CESCDD2, CEOPTR, CESVDD, CESVFDD, CESVFS, WRSCDD, FODD, FOFDD, WRSCFDD, WRN, 
RORTC11, RORTC21, RORTC19, RORTC9, RORTC5, RO11NB, RO11NT, RO21NB, RO21NT, RO19NB, RO19NT, RO9NB, RO9NT, RO5NB, RO5NT, RO24NB, RO24NT, RO20NB, RO20NT, TOTFTVATAX, TOTTVATAX, TVA_INCASARE,
'' AS CONT, 0 as ales, cast(0 as NUMBER(20,4)) as diferenta_incasare_tva, 0.0000 as incasat_exigibil, 0.0000 as incasat_tot, 0.0000 as solddif, 0.0000 as soldf 
from vjv2025
			ENDTEXT 

					
			lcfiltru = [1=2]
			lcorder = [dataact,nract]
			llAfisare = .F.
			llAfisare = .F.
			lcGroup = ""
			llModParam = .T.
			lcFiltruOriginal = ""
			gencursor('povanz', 'actcv', lcselect, lcfiltru, lcschema, lcorder, llAfisare, m.lcGroup, m.llModParam, m.lcFiltruOriginal)
			povanz.ca_baza1.afisare()

			nuexista = .F.
			Clear Class "frm_regvanz2025"
			Select actcv
			oreg = Createobject("frm_regvanz2025", m.llAfisareTot)
			oreg.Show(1)

	
		Case m.gnAn * 12 + m.gnLuna >= 2010 * 12 + 7
			*** 01/07/2010 24%
			Do inainte With "VANZ2007" In oinainte_de.prg

			*!*	modificare v 2.0.126 : am adaugat ceoptr,cesvdd,cesvfdd,cesvfs
			lcschema = [id_jv n(20), id_fact n(20), an n(4),luna n(2),cod n(20),dataact d,dataireg d,cod_fiscal c(30),nract N(20),serie_act c(10),] + ;
				[id_part n(20),id_fdoc n(20),totctva n(20,4),ro24b n(20,4),ro24t n(20,4),ro19b n(20,4),ro19t n(20,4),ro9b n(20,4),] + ;
				[ro9t n(20,4),] + ;
				[ro5b n(20,4), ro5t n(20,4),] + ;
				[roti n(20,4),cescdd1 n(20,4),cescdd2 n(20,4),wrscdd n(20,4),fodd n(20,4),] + ;
				[ceoptr n(20,4),cesvdd n(20,4),cesvfdd n(20,4),cesvfs n(20,4),] + ;
				[fofdd n(20,4),wrscfdd n(20,4),wrn n(20,4),fdoc c(30),fel_document c(30),denumire c(70),id_sucursala n(5),sucursala c(100), ] + ;
				[ro24nb n(20,4),ro24nt n(20,4),ro19nb n(20,4),ro19nt n(20,4),ro9nb n(20,4),ro9nt n(20,4),ro5nb n(20,4), ro5nt n(20,4), totftvatax n(20,4), tottvatax n(20,4), tva_incasare n(1), ] + ;
				[diferenta_incasare_tva n(20,4), ales n(1), cont c(4), soldf n(20,4), solddif n(20,4), incasat_tot n(20,4), incasat_exigibil n(20,4), ] + ;
				[ro20b n(20,4),ro20t n(20,4),ro20nb n(20,4),ro20nt n(20,4)]

			lcselect = [select id_jv, id_fact, an,luna,cod,dataact,dataireg,cod_fiscal,nract,serie_act,] + ;
				[id_part,id_fdoc,totctva,ro24b,ro24t,ro19b,ro19t,ro9b,] + ;
				[ro9t,ro5b,ro5t,roti,cescdd1,cescdd2,wrscdd,fodd,] + ;
				[ceoptr,cesvdd,cesvfdd,cesvfs,] + ;
				[fofdd,wrscfdd,wrn,fel_document,fel_document,denumire,id_sucursala,sucursala, ] + ;
				[ro24nb, ro24nt, ro19nb, ro19nt, ro9nb, ro9nt, ro5nb, ro5nt, totftvatax, tottvatax, tva_incasare, ] + ;
				[cast(0 as NUMBER(20,4)) as diferenta_incasare_tva, 0 as ales, '' as cont, 0.0000 as soldf, 0.0000 as solddif, 0.0000 as incasat_tot, 0.0000 as incasat_exigibil, ] + ;
				[ro20b, ro20t, ro20nb, ro20nt ] + ;
				[from ] + Iif(m.gnAn < 2013, [vjv2010], [vjv2013])

			lcfiltru = [1=2]
			lcorder = [dataact,nract]
			llAfisare = .F.
			llAfisare = .F.
			lcGroup = ""
			llModParam = .T.
			lcFiltruOriginal = ""
			gencursor('povanz', 'actcv', lcselect, lcfiltru, lcschema, lcorder, llAfisare, m.lcGroup, m.llModParam, m.lcFiltruOriginal)
			povanz.ca_baza1.afisare()
			nuexista = .F.
			Clear Class "frm_regvanz2010"
			Select actcv
			oreg = Createobject("frm_regvanz2010", m.llAfisareTot)
			oreg.Show(1)

		Case gnAn >= 2007 And m.gnAn * 12 + m.gnLuna < 2010 * 12 + 7
			*** 01/01/2007 19%
			Do inainte With "VANZ2007" In oinainte_de.prg

			lcschema = [id_jv n(4),an n(4),luna n(2),cod n(20),dataact d,cod_fiscal c(30),nract N(20),serie_act c(10),] + ;
				[id_part n(20),id_fdoc n(20),totctva n(20,4),ro19b n(20,4),ro19t n(20,4),ro9b n(20,4),] + ;
				[ro9t n(20,4),] + ;
				[ro5b n(20,4), ro5t n(20,4),] + ;
				[roti n(20,4),cescdd1 n(20,4),cescdd2 n(20,4),wrscdd n(20,4),fodd n(20,4),] + ;
				[fofdd n(20,4),wrscfdd n(20,4),wrn n(20,4),fel_document c(30),denumire c(70),sucursala c(100)]

			lcselect = [select id_jv,an,luna,cod,dataact,cod_fiscal,nract,serie_act,] + ;
				[id_part,id_fdoc,totctva,ro19b,ro19t,ro9b,] + ;
				[ro9t,ro5b,ro5t,roti,cescdd1,cescdd2,wrscdd,fodd,] + ;
				[fofdd,wrscfdd,wrn,fel_document,denumire,sucursala from vjv2010]

			lcfiltru = [1=2]
			lcorder = [dataact,nract]
			llAfisare = .F.
			llAfisare = .F.
			lcGroup = ""
			llModParam = .T.
			lcFiltruOriginal = ""
			gencursor('povanz', 'actcv', lcselect, lcfiltru, lcschema, lcorder, llAfisare, m.lcGroup, m.llModParam, m.lcFiltruOriginal)
			povanz.ca_baza1.afisare()
			nuexista = .F.
			Clear Class "frm_regvanz2007"
			oreg = Createobject("frm_regvanz2007")
			oreg.Show(1)

		Otherwise
			Do inainte With "VANZ", , , , lcExceptii, lcCoresp In oinainte_de.prg
			lcschema = ['id_vanz n(20),an n(5),luna n(2),cod n(20),dataireg d,cod_fiscal c(13),'+] + ;
				['totctva n(19,gnPA),neimpozab n(19,gnPA),totftvai n(19,gnPA),tvai n(19,gnPA),totftvam n(19,gnPA),'+] + ;
				['tvam n(19,gnPA),id_part n(20),nume c(50),id_fdoc n(5),fdoc c(30),dataact d,nract N(20),'+] + ;
				['scd c(4),scutit n(1),ext n(1)']
			lcselect = ['select * from ] + gcs + [.vvanz where 1=2']
			lcfiltru = [1=2]
			lcorder = [dataact,nract]
			llAfisare = .F.
			gencursor('povanz', 'actcv', lcselect, lcfiltru, lcschema, lcorder, llAfisare)
			povanz.ca_baza1.afisare()
			nuexista = .F.
			llSucces = update_cote_TVA()
			lnProcTvaSt = 1.19
			lnProcTvaRed = 1.09

			If llSucces > 0
				Select cote_tva
				Locate For Upper(Alltrim(descriere)) = 'STANDARD'
				If Found()
					lnProcTvaSt = proc_tva
				Endif
				Locate For Upper(Alltrim(descriere)) = 'REDUS'
				If Found()
					lnProcTvaRed = proc_tva
				Endif

				Clear Class "frm_regvanz"
				oreg = Createobject("frm_regvanz")
				oreg.lb_titlu_alb_b121.Caption = "REGISTRUL VANZARI"
				oreg.nProcTvaSt = lnProcTvaSt
				oreg.nProcTvaRed = lnProcTvaRed
				oreg.Show(1)
			Endif
	Endcase

	If Used('ACTCV')
		Use In actcv
	Endif
	Release oreg, povanz
Endproc	&& viz_regvanz
******************************************* SFARSIT: viz_regvanz *******************************************
******************************************* INCEPUT: viz_regcump *******************************************
Procedure viz_regcump

	Local oreg As "frm_regcump2010"
	Local lcFiltruOriginal, lcGroup, lcSqlCount, llAfisare, llAfisareTot, llModParam, llSucces
	Local lnInregistrariAfisare, lnProcTvaRed, lnProcTvaSt
	*:Global pnRecCnt
	If Used('ACTCV')
		Use In actcv
	Endif


	Private lcschema, lcselect, lcfiltru, lcorder, pocump
	Store "" To pocump

	* Calculez numarul de inregistrari. Daca numarul este mai mic de min(300, gnInregistrariAfisare) afisez toate inregistrarile in loc sa intru cu 1=2
	lnInregistrariAfisare = Iif(Type('gnInregistrariAfisare') = 'N', m.gnInregistrariAfisare, 300)

	lcSqlCount = [SELECT COUNT(*) as nr FROM jc2007 where an=?gnAn and luna=?gnLuna]
	pnRecCnt = 0
	llSucces = goExecutor.oSelecteaza2Value(m.lcSqlCount, @pnRecCnt)
	llAfisareTot = (Nvl(m.pnRecCnt, 0) <= m.lnInregistrariAfisare)

	Do Case
		Case m.gnAn * 12 + m.gnLuna >= 2025 * 12 + 8
			*** 01/08/2025 TVA 21% si 11%

			Do inainte With "CUMP2007" In oinainte_de.prg

			TEXT TO lcSchema NOSHOW 
id_fact n(20), id_fdoc n(20), id_jc n(20), id_part n(20), id_sucursala n(5),an n(4), luna n(2), dataact d, dataireg d,cod n(20), cod_fiscal c(30), cont c(4), denumire c(70), fdoc c(30), nract n(20), serie_act c(10), sucursala c(100),ales n(1), diferenta_incasare_tva n(20,4), incasat_exigibil n(20,4), incasat_tot n(20,4), solddif n(20,4), soldf n(20,4), totctva n(20,4), totftvatax n(20,4), tottvatax n(20,4), tva_incasare n(1),ceb n(20,4), cebb n(20,4), cebt n(20,4), cen n(20,4), cesc n(20,4), cesb n(20,4), cest n(20,4), cet n(20,4),fo09b n(20,4), fo09t n(20,4), fo11b n(20,4), fo11t n(20,4), fo19b n(20,4), fo19t n(20,4), fo20b n(20,4), fo20t n(20,4), fo21b n(20,4), fo21t n(20,4), fo24b n(20,4), fo24t n(20,4), foscn n(20,4),ro05b n(20,4), ro05nb n(20,4), ro05nt n(20,4), ro05t n(20,4), ro09b n(20,4), ro09nb n(20,4), ro09nt n(20,4), ro09t n(20,4), ro11b n(20,4), ro11nb n(20,4), ro11nt n(20,4), ro11t n(20,4), ro19b n(20,4), ro19nb n(20,4), ro19nt n(20,4), ro19t n(20,4), ro20b n(20,4), ro20nb n(20,4), ro20nt n(20,4), ro20t n(20,4), ro21b n(20,4), ro21nb n(20,4), ro21nt n(20,4), ro21t n(20,4), ro24b n(20,4), ro24nb n(20,4), ro24nt n(20,4), ro24t n(20,4), roscn n(20,4), rotn n(20,4), rotn50 n(20,4), rotn100 n(20,4), rortd5 n(20,4), rortd9 n(20,4), rortd11 n(20,4), rortd19 n(20,4), rortd20 n(20,4), rortd21 n(20,4), rortd24 n(20,4),ti09b n(20,4), ti09t n(20,4), ti11b n(20,4), ti11t n(20,4), ti19b n(20,4), ti19t n(20,4), ti20b n(20,4), ti20t n(20,4), ti21b n(20,4), ti21t n(20,4), ti24b n(20,4), ti24t n(20,4),xx11tib n(20,4), xx11tit n(20,4), xx19tib n(20,4), xx19tit n(20,4), xx21tib n(20,4), xx21tit n(20,4), xx9tib n(20,4), xx9tit n(20,4)
			ENDTEXT 

			TEXT TO lcSelect NOSHOW 
select id_fact, id_fdoc, id_jc, id_part, id_sucursala,
an, luna, dataact, dataireg,
cod, cod_fiscal, '' as cont, denumire, fel_document, nract, serie_act, sucursala,
0 as ales, cast(0 as NUMBER(20,4)) as diferenta_incasare_tva, 0.0000 as incasat_exigibil, 0.0000 as incasat_tot, 0 as solddif, 0 as soldf, totctva, totftvatax, tottvatax, tva_incasare,
ceb, cebb, cebt, cen, cesc, cesb, cest, cet,
fo09b, fo09t, fo11b, fo11t, fo19b, fo19t, fo20b, fo20t, fo21b, fo21t, fo24b, fo24t, foscn,
ro05b, ro5nb, ro5nt, ro05t, ro09b, ro9nb, ro9nt, ro09t, ro11b, ro11nb, ro11nt, ro11t, ro19b, ro19nb, ro19nt, ro19t, 
ro20b, ro20nb, ro20nt, ro20t, ro21b, ro21nb, ro21nt, ro21t, ro24b, ro24nb, ro24nt, ro24t, roscn, rotn, rotn50, rotn100, 
rortd5, rortd9, rortd11, rortd19, rortd20, rortd21, rortd24,
ti09b, ti09t, ti11b, ti11t, ti19b, ti19t, ti20b, ti20t, ti21b, ti21t, ti24b, ti24t,
xx11tib, xx11tit, xx19tib, xx19tit, xx21tib, xx21tit, xx9tib, xx9tit
from vjc2025
			ENDTEXT			    

			lcfiltru = [1=2]
			lcorder = [dataact,nract]
			llAfisare = .F.
			lcGroup = ""
			llModParam = .T.
			lcFiltruOriginal = ""
			gencursor('pocump', 'actcv', lcselect, lcfiltru, lcschema, lcorder, llAfisare, m.lcGroup, m.llModParam, m.lcFiltruOriginal)
			pocump.ca_baza1.afisare()

			Select actcv
			oreg = Createobject("frm_regcump2025", m.llAfisareTot)
			oreg.lb_titlu_alb_b121.Caption = "REGISTRUL CUMPARARI"
			oreg.Show(1)

		Case m.gnAn * 12 + m.gnLuna >= 2010 * 12 + 7
			*** 01/07/2010

			Do inainte With "CUMP2007" In oinainte_de.prg
			lcschema = [id_fact n(20), id_fdoc n(20), id_jc n(20), id_part n(20), id_sucursala n(5),] + ;
			    [an n(4), dataact d, dataireg d,] + ;
			    [cod n(20), cod_fiscal c(30), cont c(4), denumire c(70), fdoc c(30), luna n(2), nract n(20), serie_act c(10), sucursala c(100),] + ;
			    [ales n(1), diferenta_incasare_tva n(20,4), incasat_exigibil n(20,4), incasat_tot n(20,4), solddif n(20,4), soldf n(20,4), totctva n(20,4), totftvatax n(20,4), tottvatax n(20,4), tva_incasare n(1),] + ;
			    [ceb n(20,4), cebb n(20,4), cebt n(20,4), cen n(20,4), cesc n(20,4), cesb n(20,4), cest n(20,4), cet n(20,4),] + ;
			    [fo09b n(20,4), fo09t n(20,4), fo19b n(20,4), fo19t n(20,4), fo20b n(20,4), fo20t n(20,4), fo24b n(20,4), fo24t n(20,4), foscn n(20,4),] + ;
			    [ro05b n(20,4), ro05nb n(20,4), ro05nt n(20,4), ro05t n(20,4), ro09b n(20,4), ro09nb n(20,4), ro09nt n(20,4), ro09t n(20,4), ro19b n(20,4), ro19nb n(20,4), ro19nt n(20,4), ro19t n(20,4), ] + ;
			    [ro20b n(20,4), ro20nb n(20,4), ro20nt n(20,4), ro20t n(20,4), ro24b n(20,4), ro24nb n(20,4), ro24nt n(20,4), ro24t n(20,4), roscn n(20,4), rotn n(20,4), rotn50 n(20,4), rotn100 n(20,4),] + ;
			    [ti09b n(20,4), ti09t n(20,4), ti19b n(20,4), ti19t n(20,4), ti20b n(20,4), ti20t n(20,4), ti24b n(20,4), ti24t n(20,4),] + ;
			    [xx19tib n(20,4), xx19tit n(20,4), xx9tib n(20,4), xx9tit n(20,4)]

			lcselect = [select id_fact, id_fdoc, id_jc, id_part, id_sucursala,] + ;
			    [an, dataact, dataireg,] + ;
			    [cod, cod_fiscal, '' as cont, denumire, fel_document, luna, nract, serie_act, sucursala,] + ;
			    [0 as ales, cast(0 as NUMBER(20,4)) as diferenta_incasare_tva, 0.0000 as incasat_exigibil, 0.0000 as incasat_tot, 0 as solddif, 0 as soldf, totctva, totftvatax, tottvatax, tva_incasare,] + ;
			    [ceb, cebb, cebt, cen, cesc, cesb, cest, cet,] + ;
			    [fo09b, fo09t, fo19b, fo19t, fo20b, fo20t, fo24b, fo24t, foscn,] + ;
			    [ro05b, ro5nb, ro5nt, ro05t, ro09b, ro9nb, ro9nt, ro09t, ro19b, ro19nb, ro19nt, ro19t, ro20b, ro20nb, ro20nt, ro20t, ro24b, ro24nb, ro24nt, ro24t, roscn, rotn, rotn50, rotn100,] + ;
			    [ti09b, ti09t, ti19b, ti19t, ti20b, ti20t, ti24b, ti24t,] + ;
			    [xx19tib, xx19tit, xx9tib, xx9tit ] + ;
			    [from ] + Iif(m.gnAn < 2013, [vjc2010], [vjc2013])

			lcfiltru = [1=2]
			lcorder = [dataact,nract]
			llAfisare = .F.
			lcGroup = ""
			llModParam = .T.
			lcFiltruOriginal = ""
			gencursor('pocump', 'actcv', lcselect, lcfiltru, lcschema, lcorder, llAfisare, m.lcGroup, m.llModParam, m.lcFiltruOriginal)
			pocump.ca_baza1.afisare()

			Select actcv
			oreg = Createobject("frm_regcump2010", m.llAfisareTot)
			oreg.lb_titlu_alb_b121.Caption = "REGISTRUL CUMPARARI"
			oreg.Show(1)

		Case gnAn >= 2007 And m.gnAn * 12 + m.gnLuna < 2010 * 12 + 7
			*** 01/01/2007
			Do inainte With "CUMP2007" In oinainte_de.prg
			lcschema = ['id_jc n(4),an n(4),luna n(2),totctva n(20,4),id_part n(20),id_fdoc n(20),dataact d,'+] + ;
				['nract N(20),serie_act c(10),ro19bcb n(20,4),ro19bct n(20,4),ro19bvb n(20,4),ro19bvt n(20,4),ro19bfb n(20,4),'+] + ;
				['ro19bft n(20,4),ro09bcb n(20,4),ro09bct n(20,4),ro09bvb n(20,4),ro09bvt n(20,4),ro09bfb n(20,4),'+] + ;
				['ro05b n(20,4), ro05t n(20,4),'+] + ;
				['ro09bft n(20,4),roscn n(20,4),fo19bcb n(20,4),fo19bct n(20,4),fo19bvb n(20,4),fo19bvt n(20,4),'+] + ;
				['fo19bfb n(20,4),fo19bft n(20,4),fo09bcb n(20,4),fo09bct n(20,4),fo09bvb n(20,4),fo09bvt n(20,4),'+] + ;
				['fo09bfb n(20,4),fo09bft n(20,4),foscn n(20,4),cebcb n(20,4),cebct n(20,4),cebvb n(20,4),cebvt n(20,4),'+] + ;
				['cebvsc n(20,4),cebvn n(20,4),cebfb n(20,4),cebft n(20,4),cebfsc n(20,4),cebfn n(20,4),ti19bcb n(20,4),'+] + ;
				['ti19bct n(20,4),ti19bvb n(20,4),ti19bvt n(20,4),ti19bfb n(20,4),ti19bft n(20,4),ti09bvb n(20,4),'+] + ;
				['ti09bvt n(20,4),ti09bfb n(20,4),ti09bft n(20,4),cod n(20),fel_document c(30),denumire c(70),cod_fiscal c(30),sucursala c(100)']
			lcselect = ['select id_jc, an, luna, totctva, id_part, id_fdoc, dataact, nract, serie_act, ro19bcb, ro19bct, ro19bvb,'+] + ;
				['ro19bvt, ro19bfb, ro19bft, ro09bcb, ro09bct, ro09bvb, ro09bvt, ro09bfb,ro05b, ro05t,'+] + ;
				['ro09bft, roscn, fo19bcb, fo19bct,'+] + ;
				['fo19bvb, fo19bvt, fo19bfb, fo19bft, fo09bcb, fo09bct, fo09bvb, fo09bvt, fo09bfb, fo09bft, foscn, cebcb,'+] + ;
				['cebct, cebvb, cebvt, cebvsc, cebvn, cebfb, cebft, cebfsc, cebfn, ti19bcb, ti19bct, ti19bvb, ti19bvt,'+] + ;
				['ti19bfb, ti19bft, ti09bvb, ti09bvt, ti09bfb, ti09bft, cod, fel_document, denumire, cod_fiscal, sucursala '+] + ;
				['from ] + gcs + [.vjc2007 where 1=2']

			lcfiltru = [1=2]
			lcorder = [dataact,nract]
			llAfisare = .F.
			gencursor('pocump', 'actcv', lcselect, lcfiltru, lcschema, lcorder, llAfisare)
			pocump.ca_baza1.afisare()

			Clear Class "frm_regcump2007"
			oreg = Createobject("frm_regcump2007")
			oreg.lb_titlu_alb_b121.Caption = "REGISTRUL CUMPARARI"
			oreg.Show(1)
		Otherwise
			*** INAINTE DE 2007
			Do inainte With "CUMP" In oinainte_de.prg
			lcschema = ['id_cump n(20), an n(5), luna n(2), cod n(20), dataireg d, cod_fiscal c(13),'+] + ;
				['totctva n(19,gnPA), neimpozab n(19,gnPA), totftvai n(19,gnPA), tvai n(19,gnPA), totftvam n(19,gnPA),'+] + ;
				['tvam n(19,gnPA), id_part n(20), nume c(50), id_fdoc n(5), fdoc c(30), dataact d, nract N(20),'+] + ;
				['scd c(4),scutit n(1),ext n(1),scc c(4)']
			lcselect = ['select * from ] + gcs + [.vcump where 1=2']
			lcfiltru = [1=2]
			lcorder = [dataact,nract]
			llAfisare = .F.
			gencursor('pocump', 'actcv', lcselect, lcfiltru, lcschema, lcorder, llAfisare)
			pocump.ca_baza1.afisare()

			llSucces = update_cote_TVA()
			lnProcTvaSt = 1.19
			lnProcTvaRed = 1.09

			If llSucces > 0
				Select cote_tva
				Locate For Upper(Alltrim(descriere)) = 'STANDARD'
				If Found()
					lnProcTvaSt = proc_tva
				Endif
				Locate For Upper(Alltrim(descriere)) = 'REDUS'
				If Found()
					lnProcTvaRed = proc_tva
				Endif
			Endif

			Clear Class "frm_regcump"
			oreg = Createobject("frm_regcump")
			oreg.lb_titlu_alb_b121.Caption = "REGISTRUL CUMPARARI"
			oreg.nProcTvaSt = lnProcTvaSt
			oreg.nProcTvaRed = lnProcTvaRed
			oreg.Show(1)
	Endcase
	If Used('ACTCV')
		Use In actcv
	Endif
	Release oreg, pocump
Endproc && viz_regcump
******************************************* SFARSIT: viz_regcump *******************************************
  