*!*	13.11.2014
*!*	marius.mutu
*!*	listeaza_ofacturare - listare ROAAUTO - facturi tipizate - se listeaza A4 sau A5 dupa optiunea de tip formular netipizate

*!* 19.01.2015
*!* marius.mutu
*!* factureaza gnRF_ULTIMELE_DATE_COMPLETATE .completeaza_setari_document() completeaza datele de la un document anterior

*!*	04.02.2015
*!*	marius.mutu
*!*	factureaza - listare aviz de expeditie pentru bonul fiscal

*!*	05.05.2015
*!*	marius.mutu
*!*	listeaza_bon_fiscal - s-a corectat o problema la listare bon fiscal detaliat

*!*	23.09.2015
*!*	marius.mutu
*!*	listeaza_ofacturare - listare proforma (din comanda) - dadea eroare pentru ca nu existau toate coloanele necesare

*!*	03.02.2016
*!*	marius.mutu
*!*	listeaza_ofacturare: tip 41 (aviz retur subunitati) - se listeaza partenerul asociat gestiunii din care se face retur

*!*	09.02.2016
*!*	marius.mutu
*!*	listeaza_ofacturare: adaugat proprietate poDate.oClient pentru accesarea poDate.oClient.email_facturare la trimiterea emailurilor
*!* + poDate.nListareSilentioasa, poDate.nListareJustPDF

*!*	14.07.2016
*!*	marius.mutu
*!*	listeaza_ofacturare: dadea eroare la listare proforma - nu exista poDate.id_comanda_aviz si nici nu trebuia (nu exista proforme bazate pe aviz din comanda)

*!* 21.11.2019
*!* marius.mutu
*!* +listeaza_recapitulatie

*!* 22.01.2020
*!* marius.mutu
*!* listeaza_recapitulatie +poFirma.adresa

*!*	28.01.2020
*!*	factureaza, factureaza2 - completare crsFactura.codmatc (cod material client)

*!*	30.01.2020
*!*	marius.mutu
*!*	listeaza_ofacturare - poDate.id_util in loc de gnIdUtil pentru numele celui care a introdus factura, in loc de cel care o (re)listeaza

*!* 12.03.2021
*!* marius.mutu
*!* factureaza
*!* daca este proforma, pun toate articolele negestionabile, ca sa nu mai arate stocul

*!* 16.09.2021
*!* marius.mutu
*!* listeaza_bon_fiscal: Daca bonul nu se listeaza detaliat, listez cumulat pe cote TVA
*!* lista toata suma cu cota TVA = 0 eronat!!!

*!* factureaza: dezaloca numar chitanta, numar bon fiscal la renuntare

*!*	17.11.2023
*!*	marius.mutu
*!*	adaugare tip 52 factura fiscala in valuta contract

*!*	15.01.2024
*!*	marius.mutu
*!*	listeaza_bon_fiscal - se transmite la casa de marcat (pretctva-discountctva), in loc de pretctva

*!*	23.01.2024
*!*	listeaza_ofacturare
*!*	efactura. se listeaza valori in valuta pentru factura in valuta

***************************************************************************************************************
****	Proceduri:
****		factureaza
****		completeaza_date_aviz_retur
****		listeaza_ofacturare
****		listeaza_oconformitate
***************************************************************************************************************
******************************************** INCEPUT: factureaza **********************************************
Procedure factureaza
	Lparameters tnTip, toFactura

	* toFactura - daca se doreste copierea unei facturi anterioare

	*** 08.06.2017
	*** Daca exista optiunea gnFacturareNou = 1 atunci lansez formularul frm_facturare_articole2 in loc de frm_facturare_articole
	If Type('gnFacturareNou') = 'N' And m.gnFacturareNou = 1
		If AMESSAGEBOX('Facturare noua (DA) sau standard (NU)?', 4 + 32, _Screen.Caption) = 6
			factureaza2(m.tnTip)
			Return
		Endif
	Endif

	Private pnButon, poDate, poDateGestiuneDest, pnTotalDiscountAvize, plVizibil, poGeneratorNumere

	*!*	modificare	v 2.0.67
	*!*	PRIVATE pnTipFacturare
	*!*	pnTipFacturare = 0
	*!*	modificare	v 2.0.67	^
	Local lcCursor, lcObiect, lcObject, lcSqlCursor, lnSucces, lnTipTemp, loArticole, ltDataOra
	Local llCopiere
	*:Global ofrmceredate, ofrmdetaliifactura
	If glLunaInchisa
		AMESSAGEBOX("Nu puteti emite documente, deoarece aceasta luna este închisa!", 0 + 48, "Luna închisa")
		Return
	Endif

	Local lnIdSet, lnRaspuns, loDateAnterior, loSoldClient
	loDateAnterior = Null
	llCopiere = (Type('toFactura') = 'O')
		
	pnTotalDiscountAvize = 0

	****************************************************
	**	Valori pentru pnTipFacturare
	**	0	-	facturare pe baza de lista de preturi,pe baza de comanda
	**	1	-	facturare contracte in rate
	**	2	-	facturare contracte in rate
	**	3	-	facturare contracte (articole din contract)
	**	4	-	facturare contracte (articole din lista de preturi)
	****************************************************
	lnIdSet = 25000 + tnTip - 1 + gnScadereStoc * 10
	****************************************************
	**	Valori pentru (lnIdSet,tnTip)
	**	Facturare
	**		(25000,1)	-	pe baza de lista de preturi	( 25010 - daca e cu scadere din gestiune )
	**		(25001,2)	-	pe baza de contracte		( 25011 - daca e cu scadere din gestiune )
	**		(25002,3)	-	pe baza de comenzi			( 25012 - daca e cu scadere din gestiune )
	**		(25003,4)	-	pe baza de avize			( 25013 - daca e cu scadere din gestiune )
	**		(25004,5)	-	pe baza de lista de preturi in valuta	( 25014 - daca e cu scadere din gestiune )
	**		(25005,6)	-	pe baza de contract in valuta			( 25015 - daca e cu scadere din gestiune )
	**		(25006,7)	-	credit note
	**		(25007,8)	-	retur factura in lei		( 25017 )
	**		(25008,9)	-	retur factura in valuta		( 25018 )
	**		(25009,10)	-	factura fiscala in valuta	( 25019 )
	**		(25043,44)	-	factura hotel
	**		(25044,45)	-	factura restaurant			( 25054 )
	**		(25045,46)	-	nota de plata restaurant	( 25055 )
	**		(25047,48)	-	factura marfa custodie cu desc. k	( 25057 )
	**		(25048,49)	-	factura marfa custodie fara desc. k	( 25058 )
	**		(25051,52)	-	pe baza de contract factura fiscala in valuta			( 25061 - daca e cu scadere din gestiune )	
	
	**  Avize de expeditie
	**		(25020,21)	-	catre clienti din comanda					( 25030 - daca e cu scadere din gestiune )
	**		(25021,22)	-	catre clienti din lista						( 25031 - daca e cu scadere din gestiune )
	**		(25022,23)	-	transfer catre subunitati din lista			( 25032 - daca e cu scadere din gestiune )
	**		(25023,24)  -	aviz de retur								( 25033 - daca e cu scadere din gestiune )
	**		(25024,25)	-	transfer catre subunitati din comanda		( 25034 - daca e cu scadere din gestiune )
	**		(25025,26)	-	catre clienti din contract					( 25035 - daca e cu scadere din gestiune )
	**		(25026,27)	-	transfer catre subunitati pe baza de lucrare( 25036 - daca e cu scadere din gestiune )
	**		(25027,28)	-	catre clienti debitori din comanda			( 25037 - daca e cu scadere din gestiune )
	**		(25028,29)	-	catre clienti debitori din lista			( 25038 - daca e cu scadere din gestiune )
	**		(25029,30)	-	transfer catre subunitati pe baza de NIR	( 25039 - daca e cu scadere din gestiune )
	**		(25040,41)	-	retur transfer catre subunitati lista pret  ( 25050 - daca e cu scadere din gestiune )
	**		(25041,42)	-	catre clienti custodie pe baza de comanda	( 25051 - daca e cu scadere din gestiune )
	**		(25046,47)	-	catre clienti custodie pe baza de comanda ptr. descarcare cu K	( 25056 - daca e cu scadere din gestiune )
	**	    (25049,50)	-	in lucru	retur de la clienti custodie 				( 25059 - daca e cu scadere din gestiune )
	**
	**		ROAGEST
	**		(236/240/241/242,-6)	-	retur de la gestiune valorica
	**
	**		ROARETAIL
	**		(25042,43)	-	bonuri fiscale colectate de la magazine 	( 25052 - daca e cu scadere din gestiune )
	**		ROAACNPRO
	**		(50100,51)	-	factura ROAACNPRO 	( 25060 - daca e cu scadere din gestiune )
	****************************************************
	*!*	modificare	v 2.2.0
	*!*	update_jtva_coloane([JV])
	update_jtva_coloane([JV], [jtva_coloane_temp])
	update_saft_taxtable()

	*!*	modificare	v 2.2.0 	^
	lnRaspuns = 6
	Do While lnRaspuns = 6
		plVizibil = .T.

		If !actualizeaza_optiuni_program()
			Release poDate, poGeneratorNumere
			Return
		Endif

		If Type('poDate') <> 'O'
			poDate = Createobject("oDateFactura", lnIdSet, tnTip)
			poGeneratorNumere = Createobject("oGeneratorNumere")
		Endif
		Do Case
			Case tnTip = 27
				poDate.nIdTipDoc = 6 && AVIZ
			Case tnTip = 30
				poDate.nIdTipDoc = 6 && AVIZ
			Case tnTip < 21 Or Inlist(tnTip, 45, 48, 49, 51, 52)		&&	modificare : 45 (v2.0.87), 48 (v2.0.93), 49 (v2.0.95)
				poDate.nIdTipDoc = 5 && FACTURA
			Otherwise
				poDate.nIdTipDoc = 6 && AVIZ
		Endcase


		* Completare date
		If Type('loDateAnterior') = 'O'
			poDate.completeaza_setari_document(m.loDateAnterior)
		Else
			If m.llCopiere
				poDate.completeaza_setari_document(m.toFactura, .T.)
			Endif
		Endif

		poGeneratorNumere.ResetNumere()


		poDate.rezultat_serii = poGeneratorNumere.creeaza_cursor_serii(poDate.nIdTipDoc)

		If Type('poDateGestiuneDest') <> 'O'
			poDateGestiuneDest = Createobject("oDateGestiune")
		Endif

		Do Case
			Case tnTip = 27
				lcObiect = [frm_date_aviz_lucrare]
			Case tnTip = 30
				lcObiect = [frm_date_aviz_lucrare]
			Case tnTip < 21 Or Inlist(tnTip, 45, 48, 49, 51, 52)		&&	modificare : 45 (v2.0.87), 48 (v2.0.93), 49 (v2.0.95)
				lcObiect = [frm_date_factura]
			Otherwise
				lcObiect = [frm_date_aviz]
		Endcase

		pnButon = 1
		If Inlist(lcObiect, [frm_date_factura], [frm_date_aviz])
			ofrmceredate = Createobject(m.lcObiect, toFactura)
		Else
			ofrmceredate = Createobject(m.lcObiect)
		Endif
		ofrmceredate.ntip = tnTip
		ofrmceredate.Show()
		*!*	modificare	v 2.2.0
		Use In (Select('jtva_coloane'))

		* sterg regularizare taxa colectata pentru ca are procent TVA 
		* la adaugarea automata a tuturor articolelor factura/aviz din comanda arata formularul de adaugare articol pentru ca sunt mai mult explicatii TVA

		If poDate.tva_incasare = 0
			Select * From jtva_coloane_temp Where Isnull(id_jtva_ex) AND !'RORTC'$UPPER(coloana_jv) Into Cursor jtva_coloane
		Else
			Select * From jtva_coloane_temp Where Isnull(id_jtva_neex) AND !'RORTC'$UPPER(coloana_jv) Into Cursor jtva_coloane
		Endif
		*!*	modificare	v 2.2.0 	^
		Release ofrmceredate
		If pnButon = 2
			poGeneratorNumere.dezaloca_numar(poDate.nIdTipDoc)
			
			*!*		modificare	 v 2.0.67
			If Used('crsCtrTemp')
				Use In ('crsCtrTemp')
			Endif
			*!*		modificare	 v 2.0.67	^
			Release poDate, poGeneratorNumere
			Return
		Else
			poGeneratorNumere.verifica_numar(poDate.nIdTipDoc, poDate.nract)
		Endif

		*!*	modificare	v 2.0.67
		*!*		pnTipFacturare = 0
		*!*	modificare	v 2.0.67	^
		Do Case
			Case m.llCopiere
				lcSqlCursor = [{call pack_facturare.cursor_retur_document(?poDate.in_valuta,?poDate.listaid,1,?poDate.eProforma,?gnIdUtil)}]

				*!*	modificare v 2.0.93 ; modificare v 2.0.95 : am adaugat 49
			Case Inlist(tnTip, 48, 49)
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_articole_k(?poDate.zi_curs,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
				*!*	modificare v 2.0.93	^
				*!*	modificare v 2.0.87
			Case tnTip = 45
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_preturi(?poDate.zi_curs,?poDate.tip,?poDate.id_valuta,] + ;
					[?poDate.listaid,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
				*!*	modificare v 2.0.87	^
			Case Inlist(tnTip, 1, 22, 5, 29, 7, 10, 23)	&&	lista de preturi
				&&	modificare v 2.0.56 : am adaugat 10
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_preturi(?poDate.zi_curs,?poDate.tip,?poDate.id_valuta,] + ;
					[?poDate.id_gestiune_init,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
			Case Inlist(tnTip, 2, 26, 6, 52)				&&	contract
				poDate.id_agent = 0
				*!*	modificare v 2.0.67
				*!*			lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_contract(?poDate.zi_curs,?poDate.tip,?poDate.id_valuta,] + ;
				*!*				[?poDate.listaid,?poDate.id_gestiune_init,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala,?@pnTipFacturare,] + ;
				*!*				[?@poDate.id_agent,?@poDate.nume_agent)}]
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_contract(?poDate.zi_curs,?poDate.tip,?poDate.id_valuta,] + ;
					[?poDate.listaid,?poDate.id_gestiune_init,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala,?@poDate.id_agent,?@poDate.nume_agent)}]
				*!*	modificare v 2.0.67	^
			Case Inlist(tnTip, 3, 21, 25, 28, 42, 47)		&&	comenzi
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_comanda(?poDate.zi_curs,?poDate.tip,?poDate.listaid,?gnIdUtil)}]
			Case tnTip = 4							&&	din avize
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_avize(?poDate.listaid,?gnIdUtil,?@pnTotalDiscountAvize)}]
			Case Inlist(tnTip, 41)				&&	transfer intre subunitati pe baza de lista de preturi + retur transfer
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_gestiune(?poDate.zi_curs,?poDate.id_pol,] + ;
					[?poDate.id_client,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
			Case tnTip = 30							&&	transfer intre subunitati pe baza de NIR
				lcSqlCursor = [{call pack_facturare.cursor_aviz_nir(?poDate.id_client,?poDate.id_furnizor,?poDate.nrfactachi,?gnIdSucursala)}]
				*!*	modificare v 2.0.90	^
			Case tnTip = 27
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_lucrare(?poDate.zi_curs,?poDate.id_client,] + ;
					[?poDate.id_lucrare,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
				*!*	modificare v 2.0.56
			Case Inlist(tnTip, 8, 9, 24)				&&	8,9 = facturi de retur, 24 = aviz retur
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_retur(?poDate.in_valuta,?poDate.listaid,?gnIdUtil)}]
		Endcase

		lcCursor = [crsarticole]
		lnSucces = goExecutor.oExecute(lcSqlCursor, lcCursor)

		If lnSucces < 0
			AMESSAGEBOX(goExecutor.oPrelucrareEroare(), 16, "Eroare")
			If goExecutor.nEroare = 20005
				vizualizeaza_curs(poDate.zi_curs)
			ENDIF 
			*!*		nu inchid crsCtrTemp
			*!*	modificare	v 2.0.56
			*!*			poGeneratorNumere.dezaloca_numar(pnIdTipDoc)
			*!*	modificare	v 2.0.56	^
			poGeneratorNumere.dezaloca_numar(poDate.nIdTipDoc) && 29.03.2021 deblochez numarul ca sa il pot reda pe acelasi cand se reincarca formularul
		Else
			If Reccount(lcCursor) = 0 And (!Used(lcCursor + '1') Or (Used(lcCursor + '1') And Reccount(lcCursor + '1') = 0))
				AMESSAGEBOX("Nu exista articole pentru optiunile alese!", 48, "Atentie")
				poGeneratorNumere.dezaloca_numar(poDate.nIdTipDoc)

				Use In (SELECT('crsCtrTemp'))
			ELSE

				* Daca este o proforma, consider toate articolele negestionabile, pentru a nu mai alege din stoc
				* 12.03.2021
				IF poDate.eProforma = 1
					UPDATE (m.lcCursor) SET gestionabil  = 0
					GO TOP IN (m.lcCursor)
				ENDIF 
				
				creeaza_facturacrs([crsfactura])

				If tnTip = 30 && Aviz din NIR
					* populez factura cu articolele din NIR
					Local lcIdJtva
					Select (lcCursor)
					Scan
						Scatter Name loArticole Memo
						Select crsfactura
						update_jtva_coloane([JV], 'CrsCotaTva', 0)
						Select CrsCotaTva
						Locate For cota_tva = (loArticole.proc_tvav * 100 - 100)
						If Found()
							lcIdJtva = id_jtva_coloana
						Else
							lcIdJtva = Null
						Endif
						Use In CrsCotaTva

						Insert Into crsfactura ;
							(id_c, id_temp, id_articol, id_gestiune, Cont, um, ;
							  id_valuta, gestionabil, tip_valuta, Curs, multiplicator, id_jtva_coloana, codmat, ;
							  pret_achizitie, pretd, id_valuta_d, denumire, pretftva, pretctva, valftva, valtva, valctva, ;
							  cantitate, discountftva, discountctva, valdiscountftva, valdiscounttva, ;
							  valdiscountctva, valdiminuatftva, valdiminuattva,	valdiminuatctva, proc_tvav, cu_tva, serie, ;
							  id_set_fact, explicatie, id_pol, id_ctr ) ;
							Values (;
							  loArticole.id_c, loArticole.id_c, loArticole.id_articol, loArticole.id_gestiune, loArticole.Cont, loArticole.um, ;
							  loArticole.id_valuta, loArticole.gestionabil, loArticole.tip_valuta, loArticole.Curs, ;
							  loArticole.multiplicator, lcIdJtva, Nvl(loArticole.codmat, ' '), ;
							  loArticole.preta, loArticole.pretd, loArticole.id_valuta_d, loArticole.denumire, ;
							  loArticole.pretftva, loArticole.pretctva, ;
							  Round(loArticole.cantitate * loArticole.pretftva, gnpc), ;
							  Round(loArticole.cantitate * loArticole.tvav, gnpc), ;
							  Round(loArticole.cantitate * loArticole.pretctva, gnpc), ;
							  loArticole.cantitate, 0, 0, 0, 0, ;
							  0, Round(loArticole.cantitate * loArticole.pretftva, gnpc), Round(loArticole.cantitate * loArticole.tvav, gnpc), ;
							  Round(loArticole.cantitate * loArticole.pretctva, gnpc), loArticole.proc_tvav, loArticole.preturi_cu_tva, '', ;
							  Null, '', Null, Null)

						Select (lcCursor)
					Endscan
					Release lcIdJtva
					Select crsfactura
					Release loArticole
					
				ENDIF && tnTip = 30
											
				If tnTip = 27
					lcObject = [frm_avizare_lucrare]
					Create Cursor crsgestiunidest(id_gestiune N(20), nume_gestiune c(100))
					Select crsgestiunidest
					Append Blank
					Replace id_gestiune With - 9999, nume_gestiune With '<TOATE>'
					Insert Into crsgestiunidest(id_gestiune, nume_gestiune) ;
						Select Distinct id_gestiune, nume_gestiune From (lcCursor) Order By nume_gestiune
				Else
					lcObject = [frm_facturare_articole]
				Endif
				*!*	modificare	v 2.0.67
				*!*				Endif
				*!*	modificare	v 2.0.67	^
				*!*	modificare v 2.0.56
				*!*				If Type('crsarticole.curs')<>'U'
				*!*					Select Distinct Curs,nume_val From crsArticole With (Buffering = .T.) Where tip_valuta = 1 Into Cursor crscursuri
				*!*					If poDate.in_valuta = 1
				*!*						Select crscursuri
				*!*						poDate.Curs = Curs
				*!*					Endif
				If poDate.in_valuta = 1
					Select Distinct Curs, nume_val, id_valuta, multiplicator From (lcCursor) With (Buffering = .T.) Where tip_valuta = 1 Order By nume_val Into Cursor crscursuri
					Select crscursuri
					poDate.Curs = Curs
					poDate.multiplicator = multiplicator
				Else
					citeste_cursuri_zi(poDate.zi_curs)
					If Reccount('crscursuri') = 0
						Use In crscursuri
					Endif
					*!*	modificare v 2.0.56	^
				Endif

				If Type(lcCursor + '.nume_lista_preturi') <> 'U'
					Create Cursor crspolitici (id_pol N(20), nume_lista_preturi c(100))
					Select crspolitici
					*!*	modificare	v 2.0.77
					*!*					Append Blank
					*!*					Replace id_pol With -9999,nume_lista_preturi With '<TOATE>'
					Insert Into crspolitici(id_pol, nume_lista_preturi) Values (-9999, '<TOATE>')
					*!*	modificare	v 2.0.77	^
					Insert Into crspolitici(id_pol, nume_lista_preturi) ;
						Select Distinct id_pol, nume_lista_preturi From (lcCursor) Order By nume_lista_preturi
				Endif

				*!*	modificare	v 2.0.67
				If Inlist(tnTip, 2, 6, 26)
					Create Cursor crscontracte(id_ctr N(20), numar_contract c(100), Data d Null, numar c(50) Null)
					Select crscontracte
					Append Blank
					Replace id_ctr With - 9999, numar_contract With '<TOATE>'
					Insert Into crscontracte(id_ctr, numar_contract, Data, numar) ;
						Select Distinct id_ctr, numar_contract, Data, numar From (lcCursor + [1]) Order By Data, numar
				Endif
				*!*	modificare	v 2.0.67	^
				ofrmdetaliifactura = Createobject(lcObject)

				IF tnTip = 30 && AVIZ DIN NIR
					* CALCULEZ TOTALURILE SI FINALIZEZ FORMULARUL FARA SA IL ARAT
					Private plreturn
					Store .T. To plreturn
					ofrmdetaliifactura.do_calculeaza_totaluri()
					ofrmdetaliifactura.but_termin1.Click()
					plVizibil = .F.
					If !plreturn
						Return
					ENDIF
				ELSE 
					IF m.llCopiere
						* Modificare sau copiere factura
						* Nu adaug automat articolele din factura originala, ca sa dau posibilitate de modificare a cantitatilor, pretului, explicatiei
						* ofrmdetaliifactura.do_adauga_tot()

						* Sterg din cursorul cu articole inregistrarile adaugate in factura
						*DELETE FROM crsArticole
						
						* Adaug in lista articolelor cursorul cu lista de preturi ca sa pot adauga si alte articole in factura copiata/modificata
						lcSqlCursor = [{call pack_facturare.cursor_preturi(?poDate.zi_curs,?poDate.tip,?poDate.id_valuta,] + ;
						[?poDate.id_gestiune_init,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
						
						lcCursorTemp = [crsArticoleTemp]
						llSucces = goExecutor.oExecuta(lcSqlCursor, lcCursorTemp)
						IF m.llSucces
							SELECT crsArticole
							APPEND FROM DBF(m.lcCursorTemp)
						ENDIF 
						USE IN (SELECT(m.lcCursorTemp))
					ENDIF && m.llCopiere
						
					If plVizibil			
						ofrmdetaliifactura.Show()
					Else
						ofrmdetaliifactura.Release()
						pnButon = 2
					Endif
				ENDIF && tnTip = 30

				If Used('crspolitici')
					Use In crspolitici
				Endif

				If Used('crscursuri')
					Use In crscursuri
				Endif

				*!*	modificare v 2.0.67
				If Used('crscontracte')
					Use In crscontracte
				Endif
				If Used('crsCtrTemp')
					Use In crsCtrTemp
				Endif
				*!*	modificare v 2.0.67	^

				*!*	modificare v 2.0.80
				If poDate.incasat <> 0
					poGeneratorNumere.verifica_numar(16, poDate.nr_incasare)
				Endif
				*!*	modificare v 2.0.80

				If pnButon = 1
					* Completez podate.institutie_publica
					poDate.institutie_publica = GetInstitutiePublica(poDate.id_client)
					
					* Completare codmat client, codnc8, codcpv
					Select crsfactura
					Scan
						Replace codmatc With GetCodmatC(poDate.id_client, crsfactura.id_articol) In crsfactura

						loCod = GetCodNC8CPV(crsfactura.id_articol)
						Replace codnc8 WITH loCod.codnc8, codcpv WITH loCod.codcpv In crsfactura
					Endscan
					If poDate.tip <> 27
						If poDate.ntip_incasare = 2 && bon fiscal
							If Type('gnListareAvizBonFiscal') = 'N' And m.gnListareAvizBonFiscal = 1
								lnTipTemp =  poDate.tip
								poDate.tip = 22 && La bonul fiscal listez AVIZ DE EXPEDITIE
								listeaza_ofacturare()
								poDate.tip = m.lnTipTemp
							Endif
							ltDataOra =  Get_Ora()
							listeaza_bon_fiscal(ltDataOra, poDate.nr_incasare, poDate.incasat, poDate.incasatCARD, poDate.proc_tva, poDate.nract, poDate.nBonDetaliat, "crsFactura")
						ELSE
							* Soldul pana la factura inclusiv
							loSoldClient = GetSoldClient(poDate.id_client, IdVanzare2IdFact(podate.nid_vanzare))
							poDate.sold_lei = loSoldClient.sold_lei
							poDate.sold_valuta = loSoldClient.sold_valuta

							listeaza_ofacturare()
						ENDIF
						
					ENDIF && tip
				Else
					poGeneratorNumere.dezaloca_numar(poDate.nIdTipDoc)
					
					poGeneratorNumere.dezaloca_numar(16) && dezaloca numar chitanta
					poGeneratorNumere.dezaloca_numar(3) && dezaloca numar bon fiscal
					poGeneratorNumere.dezaloca_numar(26) && dezaloca numar POS/Card
					
					poDate.nr_incasare = 0
					poDate.incasat = 0	
				ENDIF && pnButon

				Use In (SELECT('crsfactura'))
				Use In (SELECT('crsfacturaset'))
				Use In (SELECT('crsfacturafinala'))
			Endif

			*!*				lnRaspuns = amessagebox("Doriti sa continuati cu operatii de acest fel?", 4 + 32, "Confirmare")
			lnRaspuns = AMESSAGEBOX([Doriti sa continuati cu operatii de acest fel?] + Chr(13) + Chr(10) + [DA=copiere date / NU=fara copiere date / Abandon], 3 + 32, "Confirmare")
			If lnRaspuns = 6 && DA
				loDateAnterior = CloneObj(m.poDate) && se retin informatiile pentru completarea pe factura urmatoare
			Else
				loDateAnterior = Null
				If m.lnRaspuns = 7 && NU inseamna DA fara copierea datelor
					lnRaspuns = 6 && DA
				Endif
			Endif

			poDate.Reset(7 - lnRaspuns)
			poDateGestiuneDest.Reset()
		Endif
		*!*			Endif
		*!*		Endif
	Enddo
	*!*	modificare	v 2.2.0
	Use In (Select('jtva_coloane_temp'))
	Use In (Select('jtva_coloane'))
	*!*	modificare	v 2.2.0	^
	Release poDate, poGeneratorNumere
Endproc
******************************************** SFARSIT: factureaza **********************************************


***
******************************************** INCEPUT: factureaza2 **********************************************
Procedure factureaza2
	Lparameters tnTip
	Private pnButon, poDate, poDateGestiuneDest, pnTotalDiscountAvize, plVizibil, poGeneratorNumere

	*!*	modificare	v 2.0.67
	*!*	PRIVATE pnTipFacturare
	*!*	pnTipFacturare = 0
	*!*	modificare	v 2.0.67	^
	Local lcCursor, lcObiect, lcObject, lcSqlCursor, lnSucces, lnTipTemp, loArticole, ltDataOra
	*:Global ofrmceredate, ofrmdetaliifactura
	If glLunaInchisa
		AMESSAGEBOX("Nu pute_i emite documente, deoarece aceasta luna este închisa!", 0 + 48, "Luna închisa")
		Return
	Endif

	Local lnIdSet, lnRaspuns, loDateAnterior
	loDateAnterior = Null

	pnTotalDiscountAvize = 0

	****************************************************
	**	Valori pentru pnTipFacturare
	**	0	-	facturare pe baza de lista de preturi,pe baza de comanda
	**	1	-	facturare contracte in rate
	**	2	-	facturare contracte in rate
	**	3	-	facturare contracte (articole din contract)
	**	4	-	facturare contracte (articole din lista de preturi)
	****************************************************
	lnIdSet = 25000 + tnTip - 1 + gnScadereStoc * 10
	****************************************************
	**	Valori pentru (lnIdSet,tnTip)
	**	Facturare
	**		(25000,1)	-	pe baza de lista de preturi	( 25010 - daca e cu scadere din gestiune )
	**		(25001,2)	-	pe baza de contracte		( 25011 - daca e cu scadere din gestiune )
	**		(25002,3)	-	pe baza de comenzi			( 25012 - daca e cu scadere din gestiune )
	**		(25003,4)	-	pe baza de avize			( 25013 - daca e cu scadere din gestiune )
	**		(25004,5)	-	pe baza de lista de preturi in valuta	( 25014 - daca e cu scadere din gestiune )
	**		(25005,6)	-	pe baza de contract in valuta			( 25015 - daca e cu scadere din gestiune )
	**		(25006,7)	-	credit note
	**		(25007,8)	-	retur factura in lei		( 25017 )
	**		(25008,9)	-	retur factura in valuta		( 25018 )
	**		(25009,10)	-	factura fiscala in valuta	( 25019 )
	**		(25043,44)	-	factura hotel
	**		(25044,45)	-	factura restaurant			( 25054 )
	**		(25045,46)	-	nota de plata restaurant	( 25055 )
	**		(25047,48)	-	factura marfa custodie cu desc. k	( 25057 )
	**		(25048,49)	-	factura marfa custodie fara desc. k	( 25058 )
	**  Avize de expeditie
	**		(25020,21)	-	catre clienti din comanda					( 25030 - daca e cu scadere din gestiune )
	**		(25021,22)	-	catre clienti din lista						( 25031 - daca e cu scadere din gestiune )
	**		(25022,23)	-	transfer catre subunitati din lista			( 25032 - daca e cu scadere din gestiune )
	**		(25023,24)  -	aviz de retur								( 25033 - daca e cu scadere din gestiune )
	**		(25024,25)	-	transfer catre subunitati din comanda		( 25034 - daca e cu scadere din gestiune )
	**		(25025,26)	-	catre clienti din contract					( 25035 - daca e cu scadere din gestiune )
	**		(25026,27)	-	transfer catre subunitati pe baza de lucrare( 25036 - daca e cu scadere din gestiune )
	**		(25027,28)	-	catre clienti debitori din comanda			( 25037 - daca e cu scadere din gestiune )
	**		(25028,29)	-	catre clienti debitori din lista			( 25038 - daca e cu scadere din gestiune )
	**		(25029,30)	-	transfer catre subunitati pe baza de NIR	( 25039 - daca e cu scadere din gestiune )
	**		(25040,41)	-	retur transfer catre subunitati lista pret  ( 25050 - daca e cu scadere din gestiune )
	**		(25041,42)	-	catre clienti custodie pe baza de comanda	( 25051 - daca e cu scadere din gestiune )
	**		(25046,47)	-	catre clienti custodie pe baza de comanda ptr. descarcare cu K	( 25056 - daca e cu scadere din gestiune )
	**	in lucru	(25049,50)	-	retur de la clienti custodie 				( 25059 - daca e cu scadere din gestiune )
	**
	**		ROAGEST
	**		(236/240/241/242,-6)	-	retur de la gestiune valorica
	**
	**		ROARETAIL
	**		(25042,43)	-	bonuri fiscale colectate de la magazine 	( 25052 - daca e cu scadere din gestiune )
	****************************************************
	*!*	modificare	v 2.2.0
	*!*	update_jtva_coloane([JV])
	update_jtva_coloane([JV], [jtva_coloane_temp])
	*!*	modificare	v 2.2.0 	^
	lnRaspuns = 6
	Do While lnRaspuns = 6
		plVizibil = .T.

		If !actualizeaza_optiuni_program()
			Release poDate, poGeneratorNumere
			Return
		Endif

		If Type('poDate') <> 'O'
			poDate = Createobject("oDateFactura", lnIdSet, tnTip)
			poGeneratorNumere = Createobject("oGeneratorNumere")
		Endif
		Do Case
			Case tnTip = 27
				poDate.nIdTipDoc = 6 && AVIZ
			Case tnTip = 30
				poDate.nIdTipDoc = 6 && AVIZ
			Case tnTip < 21 Or Inlist(tnTip, 45, 48, 49)		&&	modificare : 45 (v2.0.87), 48 (v2.0.93), 49 (v2.0.95)
				poDate.nIdTipDoc = 5 && FACTURA
			Otherwise
				poDate.nIdTipDoc = 6 && AVIZ
		Endcase


		* Completare date
		If Type('loDateAnterior') = 'O'
			poDate.completeaza_setari_document(m.loDateAnterior)
		Endif

		poGeneratorNumere.ResetNumere()


		poDate.rezultat_serii = poGeneratorNumere.creeaza_cursor_serii(poDate.nIdTipDoc)

		If Type('poDateGestiuneDest') <> 'O'
			poDateGestiuneDest = Createobject("oDateGestiune")
		Endif

		Do Case
			Case tnTip = 27
				lcObiect = [frm_date_aviz_lucrare]
			Case tnTip = 30
				lcObiect = [frm_date_aviz_lucrare]
			Case tnTip < 21 Or Inlist(tnTip, 45, 48, 49)		&&	modificare : 45 (v2.0.87), 48 (v2.0.93), 49 (v2.0.95)
				lcObiect = [frm_date_factura]
			Otherwise
				lcObiect = [frm_date_aviz]
		Endcase

		pnButon = 1
		If .F.
			ofrmceredate = Createobject(m.lcObiect)
			ofrmceredate.ntip = tnTip
			ofrmceredate.Show()
		Endif && .F.
		*!*	modificare	v 2.2.0
		Use In (Select('jtva_coloane'))
		If poDate.tva_incasare = 0
			Select * From jtva_coloane_temp Where Isnull(id_jtva_ex) Into Cursor jtva_coloane
		Else
			Select * From jtva_coloane_temp Where Isnull(id_jtva_neex) Into Cursor jtva_coloane
		Endif
		*!*	modificare	v 2.2.0 	^
		Release ofrmceredate
		If pnButon = 2
			poGeneratorNumere.dezaloca_numar(poDate.nIdTipDoc)
			
			poGeneratorNumere.dezaloca_numar(16) && dezaloca numar chitanta
			poGeneratorNumere.dezaloca_numar(3) && dezaloca numar bon fiscal
			
			IF TYPE('poDate.nr_incasare') = 'N'
				poDate.nr_incasare = 0
			ENDIF 	
			IF TYPE('poDate.incasat') = 'N'
				poDate.incasat = 0	
			ENDIF 
						
			*!*		modificare	 v 2.0.67
			If Used('crsCtrTemp')
				Use In ('crsCtrTemp')
			Endif
			*!*		modificare	 v 2.0.67	^
			Release poDate, poGeneratorNumere
			Return
		Else
			poGeneratorNumere.verifica_numar(poDate.nIdTipDoc, poDate.nract)
		Endif

		*!*	modificare	v 2.0.67
		*!*		pnTipFacturare = 0
		*!*	modificare	v 2.0.67	^
		Do Case
				*!*	modificare v 2.0.93 ; modificare v 2.0.95 : am adaugat 49
			Case Inlist(tnTip, 48, 49)
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_articole_k(?poDate.zi_curs,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
				*!*	modificare v 2.0.93	^
				*!*	modificare v 2.0.87
			Case tnTip = 45
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_preturi(?poDate.zi_curs,?poDate.tip,?poDate.id_valuta,] + ;
					[?poDate.listaid,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
				*!*	modificare v 2.0.87	^
			Case Inlist(tnTip, 1, 22, 5, 29, 7, 10)	&&	lista de preturi
				&&	modificare v 2.0.56 : am adaugat 10
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_preturi(?poDate.zi_curs,?poDate.tip,?poDate.id_valuta,] + ;
					[?poDate.id_gestiune_init,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
			Case Inlist(tnTip, 2, 26, 6)				&&	contract
				poDate.id_agent = 0
				*!*	modificare v 2.0.67
				*!*			lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_contract(?poDate.zi_curs,?poDate.tip,?poDate.id_valuta,] + ;
				*!*				[?poDate.listaid,?poDate.id_gestiune_init,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala,?@pnTipFacturare,] + ;
				*!*				[?@poDate.id_agent,?@poDate.nume_agent)}]
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_contract(?poDate.zi_curs,?poDate.tip,?poDate.id_valuta,] + ;
					[?poDate.listaid,?poDate.id_gestiune_init,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala,?@poDate.id_agent,?@poDate.nume_agent)}]
				*!*	modificare v 2.0.67	^
			Case Inlist(tnTip, 3, 21, 25, 28, 42, 47)		&&	comenzi
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_comanda(?poDate.zi_curs,?poDate.tip,?poDate.listaid,] + ;
					[?gnIdUtil)}]
			Case tnTip = 4							&&	din avize
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_avize(?poDate.listaid,?gnIdUtil,?@pnTotalDiscountAvize)}]
			Case Inlist(tnTip, 23, 41)				&&	transfer intre subunitati pe baza de lista de preturi + retur transfer
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_gestiune(?poDate.zi_curs,?poDate.id_pol,] + ;
					[?poDate.id_client,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
			Case tnTip = 30							&&	transfer intre subunitati pe baza de NIR
				*!*	modificare	v 2.0.61
				*!*	modificare v 2.0.56
				*!*			lcSqlCursor = [select rownum as id_c, a.ID_ARTICOL,a.SERIE,a.ID_GESTIUNE,a.ID_VALUTA,]+;
				*!*				[ 0 as discount_unitar,a.CODMAT,a.DENUMIRE, Nvl(a.UM,Chr(32)) as um, 1 as gestionabil,]+;
				*!*				[ a.CANT as cantitate, a.PROC_TVAV,0 as preturi_cu_tva, a.CURS, a.PRET as preta, a.PRETv as pret,a.tvav as tvav, 0 as pret_val,a.nume_val,]+;
				*!*				[ 0 as discount_unitar_val, 0 as tip_valuta,cont ]+;
				*!*				[ from vrul A where a.cod in (select max(b.cod) ]+;
				*!*				[ from act b where b.id_partc = ?poDate.id_furnizor and b.scc = '401' and b.nract = ?poDate.nrfactachi)]
				*!*			lcSqlCursor = [select rownum as id_c,rownum as id_temp, a.ID_ARTICOL,a.SERIE,a.ID_GESTIUNE,b.ID_VALUTA,]+;
				*!*				[ 0 as discount_unitar,a.CODMAT,a.DENUMIRE, Nvl(a.UM,Chr(32)) as um, 1 as gestionabil,]+;
				*!*				[ a.CANT as cantitate, a.PROC_TVAV,0 as preturi_cu_tva, 1 as CURS, a.PRET as preta, a.PRETv as pret,a.tvav as tvav, 0 as pret_val,b.nume_val,]+;
				*!*				[ 0 as discount_unitar_val, 0 as tip_valuta,a.cont,a.pretd,a.id_valuta as id_valuta_d ]+;
				*!*				[ from vrul A left join ] + ;
				*!*				[ (select id_valuta, nume_val from vnom_valute where moneda_nationala = 1 and rownum = 1) b on 1 = 1 ] + ;
				*!*				[ where a.cod in (select max(b.cod) ]+;
				*!*				[ from act a1 where a1.sters = 0 and a1.id_partc = ?poDate.id_furnizor and a1.scc = '401' and a1.nract = ?poDate.nrfactachi)]
				*!*		*!*	modificare v 2.0.56	^
				*!*	modificare	v 2.0.90
				*!*	modificare v 2.0.71 : 0 as curs; conditii gcCondSucursala si a.id_gestiune = ?poDate.id_client
				*!*			lcSqlCursor = [select rownum as id_c,rownum as id_temp, a.ID_ARTICOL,a.SERIE,a.ID_GESTIUNE,b.ID_VALUTA,]+;
				*!*				[ 0 as discount_unitar,c.CODMAT,c.DENUMIRE, Nvl(c.UM,Chr(32)) as um, 1 as gestionabil,]+;
				*!*				[ a.CANT as cantitate, a.PROC_TVAV,decode(d.nr_pag,6,1,7,1,0) as preturi_cu_tva, 0 as CURS,1 as multiplicator,a.PRET as preta,] + ;
				*!*				[a.pretv+a.tvav as pretctva,a.pretv as pretftva,] + ;
				*!*				[a.tvav,0 as pret_val,b.nume_val,]+;
				*!*				[ 0 as discount_unitar_val, 0 as tip_valuta,a.cont,a.pretd,a.id_valuta as id_valuta_d ]+;
				*!*				[ from rul A left join ] + ;
				*!*				[ (select id_valuta, nume_val from vnom_valute where moneda_nationala = 1 and rownum = 1) b on 1 = 1 ] + ;
				*!*				[ left join nom_articole c on a.id_articol = c.id_articol ] + ;
				*!*				[ left join nom_gestiuni d on a.id_gestiune = d.id_gestiune ] + ;
				*!*				[ where a.sters = 0 and a.id_gestiune = ?poDate.id_client and a.cod in (select max(a1.cod) ]+;
				*!*				[ from act a1 where a1.sters = 0 and a1.id_partc = ?poDate.id_furnizor and a1.scc = '401' and a1.nract = ?poDate.nrfactachi] + ;
				*!*				STRTRAN(gcCondSucursala,[id_sucursala],[a1.id_sucursala]) + [)]
				*!*	modificare	v 2.0.61	^
				lcSqlCursor = [{call pack_facturare.cursor_aviz_nir(?poDate.id_client,?poDate.id_furnizor,?poDate.nrfactachi,?gnIdSucursala)}]
				*!*	modificare v 2.0.90	^
			Case tnTip = 27
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_lucrare(?poDate.zi_curs,?poDate.id_client,] + ;
					[?poDate.id_lucrare,?gnLuna,?gnAn,?gnIdUtil,?gnIdSucursala)}]
				*!*	modificare v 2.0.56
			Case Inlist(tnTip, 8, 9)				&&	facturi de retur
				lcSqlCursor = [{call ] + gcS + [.pack_facturare.cursor_retur(?poDate.in_valuta,?poDate.listaid,?gnIdUtil)}]
				*!*	modificare v 2.0.56	^
		Endcase
		If .F.
			lcCursor = [crsarticole]
			lnSucces = goExecutor.oExecute(lcSqlCursor, lcCursor)
		Endif
		lnSucces = 1
		If lnSucces < 0
			AMESSAGEBOX(goExecutor.oPrelucrareEroare(), 16, "Eroare")
			If goExecutor.nEroare = 20005
				vizualizeaza_curs(poDate.zi_curs)
			Endif
			*!*		nu inchid crsCtrTemp
			*!*	modificare	v 2.0.56
			*!*			poGeneratorNumere.dezaloca_numar(pnIdTipDoc)
			*!*	modificare	v 2.0.56	^
		Else
			If .F. And Reccount(lcCursor) = 0 And (!Used(lcCursor + '1') Or (Used(lcCursor + '1') And Reccount(lcCursor + '1') = 0))
				AMESSAGEBOX("Nu exista articole pentru optiunile alese!", 48, "Atentie")
				poGeneratorNumere.dezaloca_numar(poDate.nIdTipDoc)
				*!*		modificare	 v 2.0.67
				If Used('crsCtrTemp')
					Use In ('crsCtrTemp')
				Endif
				*!*		modificare	 v 2.0.67	^
			Else
				*!*	modificare	v 2.0.67
				*!*				If Inlist(pnTipFacturare,1,2)
				*!*					*!*	modificare	v 2.0.56 : am mutat codul in procedura
				*!*					creeaza_crsfactura_rate()
				*!*					*!*	modificare	v 2.0.56	^
				*!*					lcObject = [frm_facturare_rate]
				*!*				Else
				*!*	modificare	v 2.0.67	^
				*!*	modificare	v 2.0.56 : am mutat codul in procedura
				creeaza_facturacrs([crsfactura])
				*!*	modificare	v 2.0.56	^
				If tnTip = 30
					Local lcIdJtva
					Select (lcCursor)
					Scan
						Scatter Name loArticole Memo
						Select crsfactura
						update_jtva_coloane([JV], 'CrsCotaTva', 0)
						Select CrsCotaTva
						Locate For cota_tva = (loArticole.proc_tvav * 100 - 100)
						If Found()
							lcIdJtva = id_jtva_coloana
						Else
							lcIdJtva = Null
						Endif
						Use In CrsCotaTva
						*!*	modificare	v 2.0.61
						*!*	modificare v 2.0.56 : am adaugat pretd,id_valuta_d
						*!*								Insert Into crsfactura ;
						*!*								(id_c,id_temp,id_articol,id_gestiune,Cont,um,;
						*!*								id_valuta,gestionabil ,tip_valuta ,Curs ,id_jtva_coloana ,codmat ,;
						*!*								pret_achizitie,pretd,id_valuta_d,denumire ,pretftva ,pretctva ,valftva , valtva ,valctva ,;
						*!*								cantitate ,discountftva , discountctva , valdiscountftva ,valdiscounttva ,;
						*!*								valdiscountctva ,valdiminuatftva ,valdiminuattva ,	valdiminuatctva ,proc_tvav ,cu_tva ,serie ,;
						*!*								id_set_fact ,explicatie,id_pol ) ;
						*!*								values (;
						*!*								loArticole.id_c,loArticole.id_c,loArticole.id_articol,loArticole.id_gestiune,loArticole.Cont,loArticole.um,;
						*!*								loArticole.id_valuta,loArticole.gestionabil,loArticole.tip_valuta,loArticole.Curs,lcIdJtva ,Nvl(loArticole.codmat,' '),;
						*!*								loArticole.preta,loArticole.pretd,loArticole.id_valuta_d,loArticole.denumire,loArticole.Pret,loArticole.Pret+loArticole.tvav,Round(loArticole.cantitate*loArticole.Pret,gnpa),Round(loArticole.cantitate*loArticole.tvav,gnpa),Round((loArticole.cantitate*loArticole.Pret)+(loArticole.cantitate*loArticole.tvav),gnpa),;
						*!*								loArticole.cantitate,0,0,0,0,;
						*!*								0,Round(loArticole.cantitate*loArticole.Pret,gnpa),Round(loArticole.cantitate*loArticole.tvav,gnpa),Round((loArticole.cantitate*loArticole.Pret)+(loArticole.cantitate*loArticole.tvav),gnpa),loArticole.proc_tvav,loArticole.preturi_cu_tva,'',;
						*!*								null,'',Null)
						*!*	modificare v 2.0.71 : am adaugat id_ctr = Null
						*!*	modificare v 2.0.90 : am adaugat multiplicator
						Insert Into crsfactura ;
							(id_c, id_temp, id_articol, id_gestiune, Cont, um, ;
							  id_valuta, gestionabil, tip_valuta, Curs, multiplicator, id_jtva_coloana, codmat, ;
							  pret_achizitie, pretd, id_valuta_d, denumire, pretftva, pretctva, valftva, valtva, valctva, ;
							  cantitate, discountftva, discountctva, valdiscountftva, valdiscounttva, ;
							  valdiscountctva, valdiminuatftva, valdiminuattva,	valdiminuatctva, proc_tvav, cu_tva, serie, ;
							  id_set_fact, explicatie, id_pol, id_ctr ) ;
							Values (;
							  loArticole.id_c, loArticole.id_c, loArticole.id_articol, loArticole.id_gestiune, loArticole.Cont, loArticole.um, ;
							  loArticole.id_valuta, loArticole.gestionabil, loArticole.tip_valuta, loArticole.Curs, ;
							  loArticole.multiplicator, lcIdJtva, Nvl(loArticole.codmat, ' '), ;
							  loArticole.preta, loArticole.pretd, loArticole.id_valuta_d, loArticole.denumire, ;
							  loArticole.pretftva, loArticole.pretctva, ;
							  Round(loArticole.cantitate * loArticole.pretftva, gnpc), ;
							  Round(loArticole.cantitate * loArticole.tvav, gnpc), ;
							  Round(loArticole.cantitate * loArticole.pretctva, gnpc), ;
							  loArticole.cantitate, 0, 0, 0, 0, ;
							  0, Round(loArticole.cantitate * loArticole.pretftva, gnpc), Round(loArticole.cantitate * loArticole.tvav, gnpc), ;
							  Round(loArticole.cantitate * loArticole.pretctva, gnpc), loArticole.proc_tvav, loArticole.preturi_cu_tva, '', ;
							  Null, '', Null, Null)
						*!*	modificare	v 2.0.61	^
						&&25039
						Select (lcCursor)
					Endscan
					Release lcIdJtva
					Select crsfactura
					Release loArticole
				Endif

				If tnTip = 27
					lcObject = [frm_avizare_lucrare]
					Create Cursor crsgestiunidest(id_gestiune N(20), nume_gestiune c(100))
					Select crsgestiunidest
					Append Blank
					Replace id_gestiune With - 9999, nume_gestiune With '<TOATE>'
					Insert Into crsgestiunidest(id_gestiune, nume_gestiune) ;
						Select Distinct id_gestiune, nume_gestiune From (lcCursor) Order By nume_gestiune
				Else
					lcObject = [frm_facturare_articole2]
				Endif
				*!*	modificare	v 2.0.67
				*!*				Endif
				*!*	modificare	v 2.0.67	^
				*!*	modificare v 2.0.56
				*!*				If Type('crsarticole.curs')<>'U'
				*!*					Select Distinct Curs,nume_val From crsArticole With (Buffering = .T.) Where tip_valuta = 1 Into Cursor crscursuri
				*!*					If poDate.in_valuta = 1
				*!*						Select crscursuri
				*!*						poDate.Curs = Curs
				*!*					Endif
				If poDate.in_valuta = 1
					Select Distinct Curs, nume_val, id_valuta, multiplicator From (lcCursor) With (Buffering = .T.) Where tip_valuta = 1 Order By nume_val Into Cursor crscursuri
					Select crscursuri
					poDate.Curs = Curs
					poDate.multiplicator = multiplicator
				Else
					Do citeste_cursuri_zi With poDate.zi_curs In oproceduri_curs.prg
					If Used('crscursuri')
						If Reccount('crscursuri') = 0
							Use In (Select('crscursuri'))
						Endif
					Endif
					*!*	modificare v 2.0.56	^
				Endif

				If .F.
					If Type(lcCursor + '.nume_lista_preturi') <> 'U'
						Create Cursor crspolitici (id_pol N(20), nume_lista_preturi c(100))
						Select crspolitici
						*!*	modificare	v 2.0.77
						*!*					Append Blank
						*!*					Replace id_pol With -9999,nume_lista_preturi With '<TOATE>'
						Insert Into crspolitici(id_pol, nume_lista_preturi) Values (-9999, '<TOATE>')
						*!*	modificare	v 2.0.77	^
						Insert Into crspolitici(id_pol, nume_lista_preturi) ;
							Select Distinct id_pol, nume_lista_preturi From (lcCursor) Order By nume_lista_preturi
					Endif
				Endif && .F.
				If .F.
					*!*	modificare	v 2.0.67
					If Inlist(tnTip, 2, 6, 26)
						Create Cursor crscontracte(id_ctr N(20), numar_contract c(100), Data d Null, numar c(50) Null)
						Select crscontracte
						Append Blank
						Replace id_ctr With - 9999, numar_contract With '<TOATE>'
						Insert Into crscontracte(id_ctr, numar_contract, Data, numar) ;
							Select Distinct id_ctr, numar_contract, Data, numar From (lcCursor + [1]) Order By Data, numar
					Endif
				Endif && .F.
				*!*	modificare	v 2.0.67	^
				ofrmdetaliifactura = Createobject(lcObject)
				If tnTip = 30
					Private plreturn
					Store .T. To plreturn
					ofrmdetaliifactura.do_calculeaza_totaluri()
					ofrmdetaliifactura.but_termin1.Click()
					plVizibil = .F.
					If !plreturn
						Return
					Endif
				Else
					If plVizibil
						ofrmdetaliifactura.Show()
					Else
						ofrmdetaliifactura.Release()
						pnButon = 2
					Endif
				Endif

				If Used('crspolitici')
					Use In crspolitici
				Endif

				If Used('crscursuri')
					Use In crscursuri
				Endif

				*!*	modificare v 2.0.67
				If Used('crscontracte')
					Use In crscontracte
				Endif
				If Used('crsCtrTemp')
					Use In crsCtrTemp
				Endif
				*!*	modificare v 2.0.67	^

				*!*	modificare v 2.0.80
				If poDate.incasat <> 0
					poGeneratorNumere.verifica_numar(16, poDate.nr_incasare)
				Endif
				*!*	modificare v 2.0.80

				If pnButon = 1
					* Completare codmat client
					Select crsfactura
					Scan
						Replace codmatc With GetCodmatC(poDate.id_client, crsfactura.id_articol) In crsfactura
					Endscan

					If poDate.tip <> 27
						If poDate.ntip_incasare = 2 && bon fiscal
							If Type('gnListareAvizBonFiscal') = 'N' And m.gnListareAvizBonFiscal = 1
								lnTipTemp =  poDate.tip
								poDate.tip = 22 && La bonul fiscal listez AVIZ DE EXPEDITIE
								listeaza_ofacturare()
								poDate.tip = m.lnTipTemp
							Endif
							ltDataOra =  Get_Ora()
							listeaza_bon_fiscal(ltDataOra, poDate.nr_incasare, poDate.incasat, poDate.incasatCARD, poDate.proc_tva, poDate.nract, poDate.nBonDetaliat, "crsFactura")
						Else
							listeaza_ofacturare()
						Endif
					Endif
				Else
					poGeneratorNumere.dezaloca_numar(poDate.nIdTipDoc)
				Endif

				If Used('crsfactura')
					Use In crsfactura
				Endif
				If Used('crsfacturaset')
					Use In crsfacturaset
				Endif
				If Used('crsfacturafinala')
					Use In crsfacturafinala
				Endif
			Endif

			*!*				lnRaspuns = amessagebox("Doriti sa continuati cu operatii de acest fel?", 4 + 32, "Confirmare")
			lnRaspuns = AMESSAGEBOX([Doriti sa continuati cu operatii de acest fel?] + Chr(13) + Chr(10) + [DA=copiere date / NU=fara copiere date / Abandon], 3 + 32, "Confirmare")
			If lnRaspuns = 6 && DA
				loDateAnterior = CloneObj(m.poDate) && se retin informatiile pentru completarea pe factura urmatoare
			Else
				loDateAnterior = Null
				If m.lnRaspuns = 7 && NU inseamna DA fara copierea datelor
					lnRaspuns = 6 && DA
				Endif
			Endif

			poDate.Reset(7 - lnRaspuns)
			poDateGestiuneDest.Reset()
		Endif
		*!*			Endif
		*!*		Endif
	Enddo
	*!*	modificare	v 2.2.0
	Use In (Select('jtva_coloane_temp'))
	Use In (Select('jtva_coloane'))
	*!*	modificare	v 2.2.0	^
	Release poDate, poGeneratorNumere
Endproc && factureaza2
******************************************** SFARSIT: factureaza2 **********************************************

***

************************************ INCEPUT: completeaza_date_aviz_retur *************************************
Function completeaza_date_aviz_retur
	Local lnRezultatSerii
	lnRezultatSerii = poGeneratorNumere.creeaza_cursor_serii(6)
	ofrmdateavizretur = Createobject('frm_date_aviz_retur', lnRezultatSerii)
	ofrmdateavizretur.Show(1)
	Release ofrmdateavizretur, lnRezultatSerii
	Return Iif(pnButon = 1, 1, -1)
Endfunc	&&	completeaza_date_aviz_retur
************************************ SFARSIT: completeaza_date_aviz_retur *************************************
******************************************** INCEPUT: listeaza_ofacturare **********************************************
Procedure listeaza_ofacturare
	Local loArticol, lcRaportVal, lnPretListAviz, lcRaport, llSilentios
	Local loPreviewerConfig As "PreviewerConfig"
	Local lcCursor, lcDestinationFile, lcDocument, lcExplicatieRaport, lcPdfHasImage, lcSetare
	Local lcSetareVizualizare, lcSql, llPdfHasImage, lcSqlTVA
	Local lcCursorFacturaTemp
	*:Global poFirma
	Private poClient, poBeneficiar, ptDataOra, pcExplicatieContract, pnTotalPozitii, pnIdUtil
	poClient = Null
	poBeneficiar = Null
	ptDataOra = {//::}
	pcExplicatieContract = ''
	pnTotalPozitii = 0

	lcRaportVal = []
	lnPretListAviz = 2		&&	fara TVA
	pnIdUtil = Iif(!Empty(Nvl(poDate.id_util, 0)), poDate.id_util, m.gnIdUtil)

	************************************************
	**	Date client, responsabil facturare si firma
	************************************************
	Do Case
		Case poDate.eProforma = 1
			&& Proforma
			TEXT TO lcSql NOSHOW
select 1 as tip,(case when p.tip_persoana = 1 then prefix||' '|| p.denumire||' '|| p.sufix else p.denumire end) as denumire,
	p.cod_fiscal, p.cod_fiscal2, p.adresa,p.telefon,'' as fax,p.reg_comert,p.banca,p.cont_banca,
	'' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,
	sysdate as dataora,p.tip_persoana,0 as capital_soc_var,
	0 as capital_soc_sub, b.comanda_externa,c.cod,d.adresa as adresa_facturare,NVL(d.email, p.email) as email_facturare,e.adresa as adresa_livrare,
	d.denumire_adresa as denadrf,e.denumire_adresa as denadrl, 
	d.strada as stradaf, d.judet as judetf, d.indicativ as indicativ_judetf, d.localitate as localitatef, d.tara as taraf, d.prescurtare as cod_taraf, 
	e.strada as stradal, e.judet as judetl, e.indicativ as indicativ_judetl, e.localitate as localitatel, e.tara as taral, e.prescurtare as cod_taral, 
	f.numar as nr_ctr,f.data as data_ctr,
	p.cod_tara, p.judet, p.indicativ_judet, p.cod_judet, p.localitate, p.id_cetatenie, p.strada, p.numar as strada_nr
	from vnom_parteneri p
	left join comenzi b on b.id_part=p.id_part and b.id_comanda=?poDate.listaid
	left join parteneri_coduri c on c.id=b.id_codclient
	left join vadrese_parteneri d on d.id_adresa=b.id_facturare
	left join vadrese_parteneri e on e.id_adresa=b.id_livrare
	left join contracte f on b.id_ctr = f.id_ctr
	where p.id_part = ?poDate.id_client
union all
select 2 as tip,denumire,cod_fiscal,cod_fiscal2,'' as adresa,'' as telefon,'' as fax,reg_comert,'' as banca,'' as cont_banca,
	'' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,
	null as dataora,tip_persoana,0 as capital_soc_var,0 as capital_soc_sub  ,'' as comanda_externa,'' as cod,
	'' as adresa_facturare,''as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,
	'' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, 
	'' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, 
	'' as nr_ctr,null as data_ctr,
	'' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr
	from nom_parteneri where sters = 0 and inactiv = 0 and id_part in
	(select id_partener from utilizatori_rol_intern where sters = 0 and id_util = ?pnIdUtil)
union all
select 3 as tip,antet as denumire,cod_fiscal,'' as cod_fiscal2,adresa,telefon,fax,reg_comert,banca1 as banca,
	cont_banca1 as cont_banca,banca2,cont_banca2,banca3,cont_banca3,
	null as dataora,0 as tip_persoana,capital_soc_var,capital_soc_sub,
	'' as comanda_externa,'' as cod,'' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,
	'' as denadrf,'' as denadrl,
	'' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, 
	'' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, 	
	'' as nr_ctr,null as data_ctr,
	'' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr
	from syn_nom_firme where id_firma = ?gnIdFirma
union all
select 4 as tip,(case when p.tip_persoana = 1 then prefix||' '|| p.denumire||' '|| p.sufix else p.denumire end) as denumire,
	p.cod_fiscal,p.cod_fiscal2,p.adresa,p.telefon,'' as fax,p.reg_comert,p.banca,p.cont_banca,
	'' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,
	sysdate as dataora,p.tip_persoana,0 as capital_soc_var,
	0 as capital_soc_sub, b.comanda_externa,c.cod,d.adresa as adresa_facturare,NVL(d.email, p.email) as email_facturare,e.adresa as adresa_livrare,
	d.denumire_adresa as denadrf,e.denumire_adresa as denadrl,
	d.strada as stradaf, d.judet as judetf, d.indicativ as indicativ_judetf, d.localitate as localitatef, d.tara as taraf, d.prescurtare as cod_taraf, 
	e.strada as stradal, e.judet as judetl, e.indicativ as indicativ_judetl, e.localitate as localitatel, e.tara as taral, e.prescurtare as cod_taral, 	
	f.numar as nr_ctr,f.data as data_ctr,
	p.cod_tara, p.judet, p.indicativ_judet, p.cod_judet, p.localitate, p.id_cetatenie, p.strada, p.numar as strada_nr
	from vnom_parteneri p
	left join comenzi b on b.id_part=p.id_part and b.id_comanda=?poDate.listaid
	left join parteneri_coduri c on c.id=b.id_codclient
	left join vadrese_parteneri d on d.id_adresa=b.id_facturare
	left join vadrese_parteneri e on e.id_adresa=b.id_livrare
	left join contracte f on b.id_ctr = f.id_ctr
	where p.id_part = ?poDate.nIdBeneficiar
			ENDTEXT
		Case Inlist(poDate.tip, 23, 25, 27, 30) And Isnull(poDate.id_part_rez) && AVIZE SUBUNITATI PE BAZA DE LISTA DE PRETURI, COMANDA, LUCRARE, NIR
			*!*			14.12.2015 - sesizare Printconcept 4171 - nu apareau datele clientului
			lcSql = [select 1 as tip,Nvl(c.denumire,a.nume_gestiune) as denumire,nvl(c.cod_fiscal,a.cgest) as cod_fiscal,c.cod_fiscal2,] + ;
				[Nvl(d.adresa,'') as adresa,NVL(d.email,'') as email,Nvl(d.telefon1,'') as telefon,Nvl(d.fax,'') as fax,] + ;
				[Nvl(c.reg_comert,'') as reg_comert,Nvl(c.banca,'') as banca,Nvl(c.cont_banca,'') as cont_banca,] + ;
				['' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,] + ;
				[sysdate as dataora,1 as tip_persoana,0 as capital_soc_var,] + ;
				[0 as capital_soc_sub, ] + ;
				['' as comanda_externa,'' as cod,d.adresa as adresa_facturare,d.email as email_facturare, '' as adresa_livrare,] + ;
				[d.denumire_adresa as denadrf,'' as denadrl,] + ;
				[d.strada as stradaf, d.judet as judetf, d.indicativ as indicativ_judetf, d.localitate as localitatef, d.tara as taraf, d.prescurtare as cod_taraf,] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral,] + ;
				['' as nr_ctr, '' as data_ctr,] + ;
				[c.cod_tara, c.judet, c.indicativ_judet, c.cod_judet, c.localitate, c.id_cetatenie, c.strada, c.numar as strada_nr ] + ;
				[from nom_gestiuni a ] + ;
				[left join asociere_parteneri_gestiuni b on a.id_gestiune = b.id_gestiune ] + ;
				[left join vnom_parteneri c on b.id_part = c.id_part ] + ;
				[left join vadrese_parteneri d on c.id_part = d.id_part and d.principala = 1 ] + ;
				[where a.id_gestiune = ?poDateGestiuneDest.id_gestiune ] + ;
				[union all ] + ;
				[select 2 as tip,denumire,cod_fiscal,'' as cod_fiscal2,'' as adresa,'' as email,'' as telefon,'' as fax,'' as reg_comert,'' as banca,] + ;
				['' as cont_banca,] + ;
				['' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,] + ;
				[null as dataora,tip_persoana,0 as capital_soc_var,0 as capital_soc_sub, ] + ;
				['' as comanda_externa,'' as cod,] + ;
				['' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,] + ;
				['' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, ] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, ] + ;	
				['' as nr_ctr,null as data_ctr,] + ;
				['' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr ] + ;				
				[from nom_parteneri where sters = 0 and inactiv = 0 and id_part in ] + ;
				[(select id_partener from utilizatori_rol_intern where sters = 0 and id_util = ?pnIdUtil)] + ;
				[union all ] + ;
				[select 3 as tip,antet as denumire,cod_fiscal,'' as cod_fiscal2,adresa,email,telefon,fax,reg_comert,] + ;
				[banca1 as banca,cont_banca1 as cont_banca,] + ;
				[banca2,cont_banca2,banca3,cont_banca3,] + ;
				[null as dataora,0 as tip_persoana,capital_soc_var,capital_soc_sub, ] + ;
				['' as comanda_externa,'' as cod,] + ;
				['' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,] + ;
				['' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, ] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, ] + ;	
				['' as nr_ctr,null as data_ctr,] + ;
				['' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr ] + ;				
				[from syn_nom_firme where id_firma = ?gnIdFirma]
			*!*			14.12.2015 ^
		Case Inlist(poDate.tip, 41, -6) && RETUR AVIZ SUBUNITATI(ROAFACTURARE), RETUR AVIZ DIN GESTIUNE VALORICA (ROAGEST)
			lcSql = [select 1 as tip,Nvl(c.denumire,a.nume_gestiune) as denumire,nvl(c.cod_fiscal,a.cgest) as cod_fiscal,c.cod_fiscal2,] + ;
				[Nvl(d.adresa,'') as adresa,NVL(d.email,'') as email,Nvl(d.telefon1,'') as telefon,Nvl(d.fax,'') as fax,] + ;
				[Nvl(c.reg_comert,'') as reg_comert,Nvl(c.banca,'') as banca,Nvl(c.cont_banca,'') as cont_banca,] + ;
				['' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,] + ;
				[sysdate as dataora,1 as tip_persoana,0 as capital_soc_var,0 as capital_soc_sub, ] + ;
				['' as comanda_externa,'' as cod,d.adresa as adresa_facturare,d.email as email_facturare, '' as adresa_livrare,] + ;
				[d.denumire_adresa as denadrf,'' as denadrl,] + ;
				[d.strada as stradaf, d.judet as judetf, d.indicativ as indicativ_judetf, d.localitate as localitatef, d.tara as taraf, d.prescurtare as cod_taraf,] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral,] + ;
				['' as nr_ctr, '' as data_ctr,] + ;
				[c.cod_tara, c.judet, c.indicativ_judet, c.cod_judet, c.localitate, c.id_cetatenie, c.strada, c.numar as strada_nr ] + ;
				[from nom_gestiuni a ] + ;
				[left join asociere_parteneri_gestiuni b on a.id_gestiune = b.id_gestiune ] + ;
				[left join vnom_parteneri c on b.id_part = c.id_part ] + ;
				[left join vadrese_parteneri d on c.id_part = d.id_part and d.principala = 1 ] + ;
				[where a.id_gestiune = ?poDate.id_client ] + ;
				[union all ] + ;
				[select 2 as tip,denumire,cod_fiscal,cod_fiscal2,'' as adresa,'' as email,'' as telefon,'' as fax,'' as reg_comert,'' as banca,] + ;
				['' as cont_banca,] + ;
				['' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,] + ;
				[null as dataora,tip_persoana,0 as capital_soc_var,0 as capital_soc_sub, ] + ;
				['' as comanda_externa,'' as cod,] + ;
				['' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,] + ;
				['' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, ] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, ] + ;	
				['' as nr_ctr,null as data_ctr,] + ;
				['' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr ] + ;				
				[from nom_parteneri where sters = 0 and inactiv = 0 and id_part in ] + ;
				[(select id_partener from utilizatori_rol_intern where sters = 0 and id_util = ?pnIdUtil)] + ;
				[union all ] + ;
				[select 3 as tip,antet as denumire,cod_fiscal,'' as cod_fiscal2,adresa,email,telefon,fax,reg_comert,] + ;
				[banca1 as banca,cont_banca1 as cont_banca,] + ;
				[banca2,cont_banca2,banca3,cont_banca3,] + ;
				[null as dataora,0 as tip_persoana,capital_soc_var,capital_soc_sub, ] + ;
				['' as comanda_externa,'' as cod,] + ;
				['' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,] + ; 
				['' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, ] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, ] + ;	
				['' as nr_ctr,null as data_ctr,] + ;
				['' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr ] + ;				
				[from syn_nom_firme where id_firma = ?gnIdFirma]
			*!*	modificare	v 2.0.16	^
		Case Inlist(poDate.tip, 3, 21, 28, 42, 47) Or (poDate.tip = 4 And Not Empty(poDate.id_comanda_aviz))
			&&Factura pe baza de comanda/aviz din comanda sau factura din aviz si avizul este din comanda
			*!*	modificare	v 2.0.99 : am adaugat data, nr_ctr
			lcSql = [select 1 as tip,(case when p.tip_persoana = 1 then p.prefix||' '||p.denumire||' '||p.sufix else p.denumire end) as denumire,] + ;
				[p.cod_fiscal,p.cod_fiscal2,p.adresa,p.telefon,'' as fax,p.reg_comert,p.banca,p.cont_banca,] + ;
				['' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,] + ;
				[sysdate as dataora,p.tip_persoana,0 as capital_soc_var,0 as capital_soc_sub,] + ;
				[b.comanda_externa,c.cod,] + ;
				[d.adresa as adresa_facturare,d.email as email_facturare,e.adresa as adresa_livrare,] + ;
				[d.denumire_adresa as denadrf,e.denumire_adresa as denadrl,] + ;
				[d.strada as stradaf, d.judet as judetf, d.indicativ as indicativ_judetf, d.localitate as localitatef, d.tara as taraf, d.prescurtare as cod_taraf, ] + ;
				[e.strada as stradal, e.judet as judetl, e.indicativ as indicativ_judetl, e.localitate as localitatel, e.tara as taral, e.prescurtare as cod_taral, ] + ;
				[f.numar as nr_ctr,f.data as data_ctr, ] + ;
				[p.cod_tara, p.judet, p.indicativ_judet, p.cod_judet, p.localitate, p.id_cetatenie, p.strada, p.numar as strada_nr ] + ;
				[ from vnom_parteneri p ] + ;
				[left join comenzi b on b.id_part=p.id_part and b.id_comanda=] + Iif(Inlist(poDate.tip, 3, 21, 28, 42, 47), [?poDate.listaid ], [?poDate.id_comanda_aviz ]) + ;
				[left join parteneri_coduri c on c.id=b.id_codclient ] + ;
				[left join vadrese_parteneri d on d.id_adresa=b.id_facturare ] + ;
				[left join vadrese_parteneri e on e.id_adresa=b.id_livrare ] + ;
				[left join contracte f on b.id_ctr = f.id_ctr ] + ;
				[where p.id_part = ?poDate.id_client ] + ;
				[union all ] + ;
				[select 2 as tip,denumire,cod_fiscal,cod_fiscal2,'' as adresa,'' as telefon,'' as fax,reg_comert,'' as banca,'' as cont_banca,] + ;
				['' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,] + ;
				[null as dataora,tip_persoana,0 as capital_soc_var,0 as capital_soc_sub ,] + ;
				['' as comanda_externa,'' as cod, ] + ;
				['' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,] + ;
				['' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, ] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, ] + ;	
				['' as nr_ctr,null as data_ctr,] + ;
				['' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr ] + ;
				[from nom_parteneri ] + ;
				[where sters = 0 and inactiv = 0 and id_part in ] + ;
				[(select id_partener from utilizatori_rol_intern where sters = 0 and id_util = ?pnIdUtil)] + ;
				[union all ] + ;
				[select 3 as tip,antet as denumire,cod_fiscal,'' as cod_fiscal2,adresa,telefon,fax,reg_comert,banca1 as banca,] + ;
				[cont_banca1 as cont_banca,] + ;
				[banca2,cont_banca2,banca3,cont_banca3,] + ;
				[null as dataora,0 as tip_persoana,capital_soc_var,capital_soc_sub,] + ;
				['' as comanda_externa,'' as cod,] + ;
				['' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,] + ;
				['' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, ] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, ] + ;	
				['' as nr_ctr,null as data_ctr,] + ;
				['' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr ] + ;
				[from syn_nom_firme where id_firma = ?gnIdFirma]
			*!*	modificare	ROAAUTO v 2.0.33
		Case poDate.tip = -12 And !Empty(Nvl(poDate.id_ordl, 0))
			lcSql = [select 1 as tip,(case when tip_persoana = 1 then prefix||' '||denumire||' '||sufix else denumire end) as denumire,] + ;
				[cod_fiscal,cod_fiscal2,adresa,email,telefon,'' as fax,reg_comert,banca,cont_banca,] + ;
				['' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,] + ;
				[sysdate as dataora,tip_persoana,0 as capital_soc_var, 0 as capital_soc_sub, ]+ ;
				['' as comanda_externa,'' as cod,] + ;
				['' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,] + ;
				['' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, ] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, ] + ;	
				['' as nr_ctr,null as data_ctr,] + ;
				['' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, strada, numar as strada_nr ] + ;
				[from vnom_parteneri where id_part in ] + ;
				[(select id_partener from dev_masiniclienti where id_masiniclient in ] + ;
				[(select id_masiniclient from dev_ordl where id_ordl = ?poDate.id_ordl)) ] + ;
				[union all ] + ;
				[select 2 as tip,denumire,cod_fiscal,cod_fiscal2,'' as adresa,'' as email,'' as telefon,'' as fax,reg_comert,'' as banca,'' as cont_banca,] + ;
				['' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,] + ;
				[null as dataora,tip_persoana,0 as capital_soc_var,0 as capital_soc_sub, ] + ;
				['' as comanda_externa,'' as cod,] + ;
				['' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,] + ;
				['' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, ] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, ] + ;	
				['' as nr_ctr,null as data_ctr,] + ;
				['' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr ] + ;				
				[from nom_parteneri ] + ;
				[where sters = 0 and inactiv = 0 and id_part in ] + ;
				[(select id_partener from utilizatori_rol_intern where sters = 0 and id_util = ?pnIdUtil)] + ;
				[union all ] + ;
				[select 3 as tip,antet as denumire,cod_fiscal,'' as cod_fiscal2,adresa,'' as email,telefon,fax,reg_comert,banca1 as banca,] + ;
				[cont_banca1 as cont_banca,] + ;
				[banca2,cont_banca2,banca3,cont_banca3,] + ;
				[null as dataora,0 as tip_persoana,capital_soc_var,capital_soc_sub, ] + ;
				['' as comanda_externa,'' as cod,] + ;
				['' as adresa_facturare,'' as email_facturare,'' as adresa_livrare,'' as denadrf,'' as denadrl,] + ;
				['' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf, ] + ;
				['' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, ] + ;	
				['' as nr_ctr,null as data_ctr,] + ;
				['' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr ] + ;				
				[from syn_nom_firme where id_firma = ?gnIdFirma]
			*!*	modificare	ROAAUTO v 2.0.33	^
		Otherwise
			&&Factura
			*!*	modificare	v 2.0.21	: Nvl(?poDate.id_part_rez,?poDate.id_client) in loc de ?poDate.id_client
			TEXT TO lcSql NOSHOW
select 1 as tip,(case when tip_persoana = 1 then p.prefix||' '|| p.denumire||' '|| p.sufix else denumire end) as denumire,
	p.cod_fiscal,p.cod_fiscal2,p.adresa,p.telefon,'' as fax,p.reg_comert,p.banca,p.cont_banca,
	'' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,
	'' as banca4,'' as cont_banca4,'' as banca5,'' as cont_banca5,'' as banca6,'' as cont_banca6,'' as banca7,'' as cont_banca7,
	'' as banca8,'' as cont_banca8,'' as banca9,'' as cont_banca9,'' as banca10,'' as cont_banca10,	
	sysdate as dataora,p.tip_persoana,0 as capital_soc_var,0 as capital_soc_sub,
	'' as comanda_externa,'' as cod,
	d.adresa as adresa_facturare,d.email as email_facturare,'' as adresa_livrare,d.denumire_adresa as denadrf,'' as denadrl,
	d.strada as stradaf, d.judet as judetf, d.indicativ as indicativ_judetf, d.localitate as localitatef, d.tara as taraf, d.prescurtare as cod_taraf,
	'' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral,
	p.cod_tara, p.judet, p.indicativ_judet, p.cod_judet, p.localitate, p.id_cetatenie, p.strada, p.numar as strada_nr
	from vnom_parteneri p left join vanzari b on b.id_part=p.id_part and b.id_vanzare = ?poDate.nid_vanzare
	left join vadrese_parteneri d on d.id_adresa=b.id_facturare
	where p.id_part = Nvl(?poDate.id_part_rez,?poDate.id_client)
union all
select 2 as tip,denumire,cod_fiscal,cod_fiscal2,'' as adresa,'' as telefon,'' as fax,reg_comert,'' as banca,'' as cont_banca,
	'' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,
	'' as banca4,'' as cont_banca4,'' as banca5,'' as cont_banca5,'' as banca6,'' as cont_banca6,'' as banca7,'' as cont_banca7,
	'' as banca8,'' as cont_banca8,'' as banca9,'' as cont_banca9,'' as banca10,'' as cont_banca10,	
	null as dataora,tip_persoana,0 as capital_soc_var,0 as capital_soc_sub, 
	'' as comanda_externa,'' as cod,
	'' as adresa_facturare, '' as email_facturare,'' as adresa_livrare, '' as denadrf,'' as denadrl,
	'' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf,
	'' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, 	
	'' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr
	from nom_parteneri
	where sters = 0 and inactiv = 0 and id_part in
	(select id_partener from utilizatori_rol_intern where sters = 0 and id_util = ?pnIdUtil)
union all
	select 3 as tip,antet as denumire,cod_fiscal,'' as cod_fiscal2,adresa,telefon,fax,reg_comert,banca1 as banca,
	cont_banca1 as cont_banca,banca2,cont_banca2,banca3,cont_banca3,
	banca4,cont_banca4,banca5,cont_banca5,banca6,cont_banca6,banca7,cont_banca7,
	banca8,cont_banca8,banca9,cont_banca9,banca10,cont_banca10,
	null as dataora,0 as tip_persoana,capital_soc_var,capital_soc_sub, 
	'' as comanda_externa,'' as cod,
	'' as adresa_facturare,''as email_facturare,'' as adresa_livrare, '' as denadrf,'' as denadrl,
	'' as stradaf, '' as judetf, '' as indicativ_judetf, '' as localitatef, '' as taraf, '' as cod_taraf,
	'' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral, 	
	'' as cod_tara, '' as judet, '' as indicativ_judet, '' as cod_judet, '' as localitate, 0 as id_cetatenie, '' as strada, '' as strada_nr
	from syn_nom_firme where id_firma = ?gnIdFirma
union all
select 4 as tip,(case when tip_persoana = 1 then p.prefix||' '|| p.denumire||' '|| p.sufix else denumire end) as denumire,
	p.cod_fiscal,p.cod_fiscal2,p.adresa,p.telefon,'' as fax,p.reg_comert,p.banca,p.cont_banca,
	'' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,
	'' as banca4,'' as cont_banca4,'' as banca5,'' as cont_banca5,'' as banca6,'' as cont_banca6,'' as banca7,'' as cont_banca7,
	'' as banca8,'' as cont_banca8,'' as banca9,'' as cont_banca9,'' as banca10,'' as cont_banca10,	
	sysdate as dataora,p.tip_persoana,0 as capital_soc_var,0 as capital_soc_sub,
	'' as comanda_externa,'' as cod,	
	d.adresa as adresa_facturare,d.email as email_facturare,'' as adresa_livrare,d.denumire_adresa as denadrf,'' as denadrl,
	d.strada as stradaf, d.judet as judetf, d.indicativ as indicativ_judetf, d.localitate as localitatef, d.tara as taraf, d.prescurtare as cod_taraf,
	'' as stradal, '' as judetl, '' as indicativ_judetl, '' as localitatel, '' as taral, '' as cod_taral,
	p.cod_tara, p.judet, p.indicativ_judet, p.cod_judet, p.localitate, p.id_cetatenie, p.strada, p.numar as strada_nr	
	from vnom_parteneri p left join vanzari b on b.id_part=p.id_part and b.id_vanzare = ?poDate.nid_vanzare
	left join vadrese_parteneri d on d.id_adresa=b.id_facturare
	where p.id_part = ?poDate.nIdBeneficiar
			ENDTEXT
	Endcase

	If Used('crsdatefacturare')
		Use In crsdatefacturare
	Endif
	lcCursor = [crsdatefacturare]
	If !goExecutor.oExecuta(lcSql, lcCursor)
		Return
	Endif

	Select crsdatefacturare
	Scan
		Do Case
			Case tip = 1
				Scatter Name poClient Memo
				ptDataOra = poClient.dataora
			Case tip = 2
				With poDate
					.nume_responsabil	=	denumire
					.BIresp				=	reg_comert
					.CNPresp			=	cod_fiscal
				Endwith
			Case tip = 4
				Scatter Name poBeneficiar Memo
			Otherwise
				Scatter Name poFirma Memo
		Endcase
	Endscan
	If Type('poBeneficiar') <> 'O'
		Select crsdatefacturare
		Scatter Name poBeneficiar Memo Blank
	Endif
	Use In crsdatefacturare
	************************************************
	************************************************
	AddProperty(poClient, 'denumireClient', [])
	AddProperty(poFirma, 'b_swift', [])
	AddProperty(poFirma, 'b_adresa', [])
	AddProperty(poFirma, 'b_swift2', [])
	AddProperty(poFirma, 'b_adresa2', [])
	AddProperty(poFirma, 'b_swift3', [])
	AddProperty(poFirma, 'b_adresa3', [])
	************************************************
	************************************************
	**	Date suplimentare contract
	************************************************
	poClient.denumireClient = poClient.denumire && salvez denumirea originala in denumireClient
	Do Case
			*!*	modificare	v 2.0.67
			*!*	Case poDate.tip = 2
			*!*	&&	facturare pe baza de contract
			*!*		Private pnIdContract
			*!*		pnIdContract=Iif(Type('poDate.listaid')='C',Val(poDate.listaid),poDate.listaid)
			*!*		lcSql = [select contract from ] + gcS + [.vcontracte where id_ctr = ?pnIdContract and text_standard = 1]
			*!*		If Used('crsdatesuplimentare')
			*!*			Use In crsdatesuplimentare
			*!*		Endif
			*!*		lcCursor = [crsdatesuplimentare]
			*!*		lnSucces = goExecutor.oExecute(lcSql,lcCursor)
			*!*		If lnSucces<0
			*!*			amessagebox(goExecutor.oPrelucrareEroare(),16,"Eroare")
			*!*			Return
			*!*		Endif
			*!*		If Reccount('crsdatesuplimentare')>0
			*!*			Select crsdatesuplimentare
			*!*			lcNumarCtr = Nvl(contract,[.........])
			*!*			pcExplicatieContract = [Conform contractului nr. ] + lcNumarCtr
			*!*		Else
			*!*			pcExplicatieContract = []
			*!*		Endif
			*!*		Use In crsdatesuplimentare
			*!*		Release pnIdContract
			*!*	modificare	v 2.0.67	^
		Case  Inlist(poDate.tip, 3, 21, 28, 42, 47) Or (poDate.tip = 4 And Not Empty(poDate.id_comanda_aviz))
			&&	factura/aviz pe baza de comanda
			*!*	modificare	v 2.0.99 : am adaugat nr_ctr si data_ctr
			pcExplicatieContract = Iif(!Empty(Nvl(poClient.comanda_externa, [])), [Conform comanda nr. ] + poClient.comanda_externa, []) + ;
				Iif(!Empty(Nvl(poClient.data_ctr, {})), Iif(!Empty(Nvl(poClient.comanda_externa, [])), CRLF, []) + ;
				  [Conform contract nr.] + Alltrim(poClient.nr_ctr) + [ din data de ] + ;
				  Padl(Alltrim(Str(Day(poClient.data_ctr))), 2, [0]) + [.] + ;
				  Padl(Alltrim(Str(Month(poClient.data_ctr))), 2, [0]) + [.] + ;
				  Alltrim(Str(Year(poClient.data_ctr))), [])
			*!*	modificare	v 2.0.46
			*!*		If poDate.tip = 3 Or (poDate.tip=4 And Not Empty(poDate.id_comanda_aviz))
			*!*			poClient.adresa=Iif(Empty(poClient.adresa_facturare),poClient.adresa,poClient.adresa_facturare)
			*!*		Else
			*!*			poClient.adresa=Iif(Empty(poClient.adresa_livrare),poClient.adresa,poClient.adresa_livrare)
			*!*		Endif

			If poDate.tip = 3 Or (poDate.tip = 4 And Not Empty(poDate.id_comanda_aviz))
				poClient.adresa = Alltrim(Iif(Empty(Nvl(poClient.adresa_facturare, [])), poClient.adresa, poClient.adresa_facturare))
				poClient.denumire = poClient.denumire + Nvl(poClient.denadrf, [])
			Else
				poClient.adresa = Alltrim(Iif(Empty(Nvl(poClient.adresa_livrare, [])), poClient.adresa, poClient.adresa_livrare))
				poClient.denumire = poClient.denumire + Nvl(poClient.denadrl, [])
			Endif
			*!*	modificare	v 2.0.46	^
		Case poDate.tip = 4
			&&	facturare conform aviz
			pcExplicatieContract = [Conform aviz : ] + poDate.descriere
			*!*	modificare 11.02.2008
			*!*		poClient.adresa=Iif(Empty(poClient.adresa_facturare),poClient.adresa,poClient.adresa_facturare)
			*!*	modificare 11.02.2008	^
		Otherwise
			pcExplicatieContract = []

			If Type('poClient.adresa_facturare') = 'C'
				poClient.adresa = Alltrim(Iif(Empty(Nvl(poClient.adresa_facturare, [])), poClient.adresa, poClient.adresa_facturare))
				poClient.denumire = poClient.denumire + Nvl(poClient.denadrf, [])
			Endif

	Endcase
	************************************************
	************************************************

	* Salvez obiectul poClient in poDate sa pot accesa poDate.oClient.email_facturare la trimitere emailuri cu listare factura pdf
	poDate.oClient = poClient

	*!*	modificare	v 2.0.58
	If poDate.nRelistare = 0
		poDate.nSalveazaAtasamente = Nvl(citeste_optiune_firma([ATASAMENTEFACT]), 0)
	Endif
	*!*	modificare	v 2.0.58	^

	*!*	modificare	v 2.0.46
	alege_optiuni_listare_facturare()
	If m.glAlegeBanca
		alege_banci_facturare()
	Endif
	*!*	modificare	v 2.0.46	^
	Select crsfactura
	Calculate Max(Round(Nvl(proc_tvav, 1), 2)) To poDate.proc_tva
	poDate.proc_tva = poDate.proc_tva * 100 - 100

	poDate.text_aditional = STRTRAN(poDate.text_aditional, CHR(170), CHR(13) + CHR(10), 1, 100, 1)
	
	poDate.text_aditional_en = poDate.text_aditional	&&	modificare v 2.0.80

	* Text TVA de afisat in partea de sus a facturii, daca este o singura explicatie de TVA pe toate liniile din crsDetalii
	IF TYPE('poDate.cTextTVA') = 'U'
		ADDPROPERTY(poDate, 'cTextTVA', '')
		IF TYPE('gnTextTVAFactura') = 'N' AND m.gnTextTVAFactura = 1 AND !EMPTY(NVL(poDate.nid_vanzare,0))
			TEXT TO lcSqlTVA TEXTMERGE noshow		
	select distinct jt.denumire    as jtva_coloana,
            jte.explicatie as jtva_coloana_ex
  from vanzari_detalii a
  left join jtva_coloane_explicatii jte
on jte.id = a.id_jtva_coloana_ex
  left join jtva_coloane jt
on jt.id_jtva_coloana = a.id_jtva_coloana
 where a.id_vanzare = <<INT(poDate.nid_vanzare)>>
	 		ENDTEXT 
	 		llSucces = goExecutor.oExecuta(m.lcSqlTVA, 'cTextTVATemp')
			IF m.llSucces AND RECCOUNT('cTextTVATemp') = 1
				SELECT cTextTVATemp
				GO TOP 
				poDate.cTextTVA = IIF(!EMPTY(NVL(jtva_coloana_ex, '')), ALLTRIM(jtva_coloana_ex), ALLTRIM(NVL(jtva_coloana,'')))
			ENDIF 
			USE IN (SELECT('cTextTVATemp'))
		ENDIF 		
	ENDIF && poDate.cTextTVA
		
	Do Case
		CASE poDate.nEFactura = 1
			* do nothing

		Case INLIST(poDate.tip, 10, 52)  And poDate.eProforma = 0 &&	factura fiscala in valuta lista de preturi, factura fiscala in valuta pe baza de contract
			*!*	modificare  v 2.0.56
			lcSetare = [FACTURA]
			lcExplicatieRaport = [factura]
			lcSetareVizualizare = [PREVIZFACT]
			Do Case
				Case gnFormatFactura = 1	&&  format A4
					lcRaport = [FACTURA]
					lcRaportVal = [FACTURA_VAL]
				Case gnFormatFactura = 2	&&	format A5
					lcRaport = [FACTURA_A5]
					lcRaportVal = [FACTURA_VAL_A5]
				Case gnFormatFactura = 3
					If optiuni_format_listare(0) = 2
						lcRaport = [FACTURA_A5]
						lcRaportVal = [FACTURA_VAL_A5]
					Else
						lcRaport = [FACTURA]
						lcRaportVal = [FACTURA_VAL]
					Endif
			Endcase
			poDate.text_aditional = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
				[Curs valutar = ] + formateaza(poDate.Curs, 14, gnPCurs) + [ RON / ] + ;
				Iif(poDate.multiplicator = 1, [], Alltrim(Str(poDate.multiplicator)) + [ ]) + Alltrim(poDate.nume_valuta)
			poDate.text_aditional_en = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
				[Exchange rate = ] + Alltrim(Str(poDate.Curs, 14, gnPCurs)) + [ RON / ] + ;
				Iif(poDate.multiplicator = 1, [], Alltrim(Str(poDate.multiplicator)) + [ ]) + Alltrim(poDate.nume_valuta)
			If poDate.incasat <> 0
				poDate.sumavorbe = suma_in_vorbe(poDate.incasat)
				poDate.explicatie_chit = [facturii ]  + Allt(Allt(Nvl(poDate.serie_act, [])) + [ ] + Alltrim(Transf(poDate.nract, get_mask(20, 0)))) + [ din ] + Dtoc(poDate.dataact)
				*!*	modificare ROAFACTURARE v 2.0.84 : lcDocument
				lcDocument =  Iif(poDate.ntip_incasare = 11, [chitanta], Iif(poDate.ntip_incasare = 2, [bon fiscal], Iif(poDate.ntip_incasare = 3, [POS/Card], [bon fiscal/chitanta])))
				*!*	modificare ROAAUTO v 2.0.41 ( 06.03.2011 ) : daca e bon fiscal, atunci nu-i trec seria
				poDate.text_aditional = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
					[ Achitat cu ] + lcDocument + [ nr.] + Iif(poDate.ntip_incasare = 2, [], Alltrim(Nvl(poDate.serie_chit, []))) + [ ] + Alltrim(Str(poDate.nr_incasare)) + [ din ] + Dtoc(poDate.dataact) + [ .]
				*!*	modificare ROAAUTO v 2.0.41 ( 06.03.2011 ) : daca e bon fiscal, atunci nu-i trec seria
				poDate.text_aditional_en = Iif(!Empty(Nvl(poDate.text_aditional_en, [])), poDate.text_aditional_en + Chr(13) + Chr(10), []) + ;
					[ Paid with cash ( receipt no. ] + Iif(poDate.ntip_incasare = 2, [], Alltrim(Nvl(poDate.serie_chit, []))) + [ ] + Alltrim(Str(poDate.nr_incasare)) + [ from ] + Dtoc(poDate.dataact) + [ ).]
			Endif
			*!*	modificare  v 2.0.56	^
			*!*	modificare	v 2.0.77
			*!*	*!*	modificare	v 2.0.4
			*!*	Case Inlist(poDate.tip,23,25,27,28,29,30,41,-6)
			*!*		If Nvl(gnPretListSubunitati,2)=2
			*!*			lcRaport = [AVIZ_TRANSFER]
			*!*		Else
			*!*			lcRaport = [AVIZ_TRANSFER_CTVA]
			*!*		Endif
			*!*		lcSetare = [AVIZ]
			*!*		lcExplicatieRaport = [avizul de transfer]
			*!*	*!*	modificare	v 2.0.46
			*!*	*!*		lnVizualizare = Iif(Type('gnVizualAviz')='N',gnVizualAviz,1)
			*!*		lcSetareVizualizare = [PREVIZAVIZ]
			*!*	*!*	modificare	v 2.0.46	^
			*!*	*!*	modificare	v 2.0.4	^
			*!*	modificare	v 2.0.77	^
			*!*	modificare	v 2.0.93
		Case (Between(poDate.tip, 1, 20) Or Inlist(poDate.tip, -1, -2, -3, -4, -8, -11, 44, 45, 48, 49, 50, 51, 52)) And poDate.eProforma = 1
			*!*	Case poDate.tip < 20 And poDate.eProforma = 1			&&	proforme
			*!*	modificare	v 2.0.93	^
			lcRaport = [PROFORMA]
			lcRaportVal = [PROFORMA_VAL]
			lcSetare = [PROFORMA]
			lcExplicatieRaport = [proforma]
			*!*	modificare	v 2.0.46
			*!*		lnVizualizare = Iif(Type('gnVizualProf')='N',gnVizualProf,1)
			lcSetareVizualizare = [PREVIZPROF]
			*!*	modificare	v 2.0.46	^
			*!*	modificare	v 2.0.70
			If Nvl(gnListCursVal, 1) = 1 And poDate.in_valuta = 0
				Local lcCurs, lcCursEn
				lcCurs = []
				lcCursEn = []
				Create Cursor crsTempVal(nume_val c(100), Curs N(14, gnPCurs), multiplicator N(10))
				Insert Into crsTempVal(nume_val, Curs, multiplicator) ;
					Select Distinct nume_val, Curs, multiplicator From crsfactura Where tip_valuta = 1
				Select crsTempVal
				Scan
					lcCurs = lcCurs + [;] + formateaza(Curs, 14, gnPCurs) + [ RON / ] + ;
						Iif(multiplicator = 1, [], Alltrim(Str(multiplicator)) + [ ]) + Alltrim(nume_val) + [ ]
					lcCursEn = lcCursEn + [;] + Alltrim(Str(Curs, 14, gnPCurs)) + [ RON / ] + ;
						Iif(multiplicator = 1, [], Alltrim(Str(multiplicator)) + [ ]) + Alltrim(nume_val) + [ ]
				Endscan
				If !Empty(lcCurs)
					poDate.text_aditional = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
						[Curs valutar = ] + Substr(lcCurs, 2)
					poDate.text_aditional_en = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
						[Exchange rate = ] + Substr(lcCursEn, 2)
				Endif
				If Used('crsTempVal')
					Use In crsTempVal
				Endif
				Release lcCurs, lcCursEn
			Endif
			*!*	modificare	v 2.0.70	^
			*!*	modificare v 2.0.41
			*!*		Case poDate.tip < 20
			*!*	modificare	v 2.0.76 : am adaugat 44, 45
			*!*	modificare	v 2.0.93 : am adaugat 48
		Case Between(poDate.tip, 1, 20) Or Inlist(poDate.tip, -1, -2, -3, -4, -8, -11, 44, 45, 48, 49, 50, 51)		&&	facturi lei + invoice
			*!*	modificare v 2.0.41	^
			lcSetare = [FACTURA]
			lcExplicatieRaport = [factura]
			*!*	modificare	v 2.0.46
			*!*		lnVizualizare = Iif(Type('gnVizualFact')='N',gnVizualFact,1)
			lcSetareVizualizare = [PREVIZFACT]
			*!*	modificare	v 2.0.46	^
			Do Case
				Case poDate.tip = 51 && ACN
					lcRaport = [FACTURA_ACN]
					lcRaportVal = [FACTURA_ACN]

				Case gnFormatFactura = 1	&&  format A4
					lcRaport = [FACTURA]
					lcRaportVal = [INVOICE]
				Case gnFormatFactura = 2	&&	format A5
					lcRaport = [FACTURA_A5]
					lcRaportVal = [INVOICE_A5]
				Case gnFormatFactura = 3
					*!*	modificare	v 2.0.46
					*!*			If optiuni_format_factura() = 2
					If optiuni_format_listare(0) = 2
						*!*	modificare	v 2.0.46
						lcRaport = [FACTURA_A5]
						lcRaportVal = [INVOICE_A5]
					Else
						lcRaport = [FACTURA]
						lcRaportVal = [INVOICE]
					Endif
			Endcase
			lcRaportVal = Iif(poDate.tip = 7, [CREDIT_NOTE], lcRaportVal)
			If poDate.in_valuta = 1
				*!*	modificare	v 2.0.46
				poDate.text_aditional_invoice = poDate.text_aditional
				*!*	modificare	v 2.0.46	^
				poDate.text_aditional = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
					[Curs valutar = ] + formateaza(poDate.Curs, 14, gnPCurs) + [ RON / ] + ;
					Iif(poDate.multiplicator = 1, [], Alltrim(Str(poDate.multiplicator)) + [ ]) + Alltrim(poDate.nume_valuta)
				poDate.text_aditional_en = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
					[Exchange rate = ] + Alltrim(Str(poDate.Curs, 14, gnPCurs)) + [ RON / ] + ;
					Iif(poDate.multiplicator = 1, [], Alltrim(Str(poDate.multiplicator)) + [ ]) + Alltrim(poDate.nume_valuta)
				*!*	modificare	v 2.0.67
			Else
				If Nvl(gnListCursVal, 1) = 1
					Local lcCurs, lcCursEn
					lcCurs = []
					lcCursEn = []
					Create Cursor crsTempVal(nume_val c(100), Curs N(14, gnPCurs), multiplicator N(10))
					Insert Into crsTempVal(nume_val, Curs, multiplicator) ;
						Select Distinct nume_val, Curs, multiplicator From crsfactura Where tip_valuta = 1
					Select crsTempVal
					Scan
						lcCurs = lcCurs + [;] + formateaza(Curs, 14, gnPCurs) + [ RON / ] + ;
							Iif(multiplicator = 1, [], Alltrim(Str(multiplicator)) + [ ]) + Alltrim(nume_val) + [ ]
						lcCursEn = lcCursEn + [;] + Alltrim(Str(Curs, 14, gnPCurs)) + [ RON / ] + ;
							Iif(multiplicator = 1, [], Alltrim(Str(multiplicator)) + [ ]) + Alltrim(nume_val) + [ ]
					Endscan
					If !Empty(lcCurs)
						poDate.text_aditional = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
							[Curs valutar = ] + Substr(lcCurs, 2)
						poDate.text_aditional_en = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
							[Exchange rate = ] + Substr(lcCursEn, 2)
					Endif
					If Used('crsTempVal')
						Use In crsTempVal
					Endif
					Release lcCurs
				Endif
				*!*	modificare	v 2.0.67	^
			Endif

			If poDate.incasat <> 0
				poDate.sumavorbe = suma_in_vorbe(poDate.incasat)
				poDate.explicatie_chit = [facturii ]  + Allt(Allt(Nvl(poDate.serie_act, [])) + [ ] + Alltrim(Transf(poDate.nract, get_mask(20, 0)))) + [ din ] + Dtoc(poDate.dataact)
				*!*	modificare ROAFACTURARE v 2.0.84 : lcDocument
				lcDocument =  Iif(poDate.ntip_incasare = 11, [chitanta], Iif(poDate.ntip_incasare = 2, [bon fiscal], Iif(poDate.ntip_incasare = 3, [POS/Card], [bon fiscal/chitanta])))
				*!*	modificare ROAAUTO v 2.0.41 ( 06.03.2011 ) : daca e bon fiscal, atunci nu-i trec seria
				poDate.text_aditional = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
					[ Achitat cu ] + lcDocument + [ nr.] + Iif(poDate.ntip_incasare = 2, [], Alltrim(Nvl(poDate.serie_chit, []))) + [ ] + Alltrim(Str(poDate.nr_incasare)) + [ din ] + Dtoc(poDate.dataact)
				*!*	modificare ROAAUTO v 2.0.41 ( 06.03.2011 ) : daca e bon fiscal, atunci nu-i trec seria
				poDate.text_aditional_en = Iif(!Empty(Nvl(poDate.text_aditional_en, [])), poDate.text_aditional_en + Chr(13) + Chr(10), []) + ;
					[ Paid with cash ( receipt no. ] + Iif(poDate.ntip_incasare = 2, [], Alltrim(Nvl(poDate.serie_chit, []))) + [ ] + Alltrim(Str(poDate.nr_incasare)) + [ from ] + Dtoc(poDate.dataact) + [ ).]
			Endif
			*!*	modificare	v 2.0.189
			*!*	modificare	ROAAUTO v 2.0.28
		Case Inlist(poDate.tip, -12)
			lcSetare = [FACTURA]
			lcExplicatieRaport = [factura]
			lcSetareVizualizare = [PREVIZFACT]
			Do Case
				Case gnTIP_LFACTURA_LINII = 1 And (gnTIP_LFACTURA = 1 Or (gnTIP_LFACTURA = 3 And poDate.ntip_incasare = 11))
					lcRaport = [FACTURATIP_CUCHIT]
					lcRaportVal = [INVOICE]
				Case gnTIP_LFACTURA_LINII = 1 And Inlist(gnTIP_LFACTURA, 2, 3)
					lcRaport = [FACTURATIP] && A5
					lcRaportVal = [INVOICE]

					If gnFormatFactura = 3 && A4 SAU A5
						If optiuni_format_listare(0) = 2
							lcRaport = [FACTURATIP_A5]
							lcRaportVal = [INVOICE_A5]
						Else
							lcRaport = [FACTURATIP]
							lcRaportVal = [INVOICE]
						Endif
					Endif

				Case gnTIP_LFACTURA_LINII = 2 And gnFormatFactura = 1	&&  format A4
					lcRaport = [FACTURA]
					lcRaportVal = [INVOICE]
				Case gnTIP_LFACTURA_LINII = 2 And gnFormatFactura = 2	&&	format A5
					lcRaport = [FACTURA_A5]
					lcRaportVal = [INVOICE_A5]
				Case gnTIP_LFACTURA_LINII = 2 And gnFormatFactura = 3
					If optiuni_format_listare(0) = 2
						lcRaport = [FACTURA_A5]
						lcRaportVal = [INVOICE_A5]
					Else
						lcRaport = [FACTURA]
						lcRaportVal = [INVOICE]
					Endif
			Endcase
			lcRaportVal = Iif(poDate.tip = 7, [CREDIT_NOTE], lcRaportVal)

			If poDate.incasat <> 0
				poDate.sumavorbe = suma_in_vorbe(poDate.incasat)
				poDate.explicatie_chit = [facturii ]  + Allt(Allt(Nvl(poDate.serie_act, [])) + [ ] + Alltrim(Transf(poDate.nract, get_mask(20, 0)))) + [ din ] + Dtoc(poDate.dataact)
				lcDocument =  Iif(poDate.ntip_incasare = 11, [chitanta], Iif(poDate.ntip_incasare = 2, [bon fiscal], Iif(poDate.ntip_incasare = 3, [POS/Card], [bon fiscal/chitanta])))
				*!*	modificare ROAAUTO v 2.0.41 ( 06.03.2011 ) : daca e bon fiscal, atunci nu-i trec seria
				poDate.text_aditional = Iif(!Empty(Nvl(poDate.text_aditional, [])), poDate.text_aditional + Chr(13) + Chr(10), []) + ;
					[ Achitat cu ] + lcDocument + [ nr.] + Iif(poDate.ntip_incasare = 2, [], Alltrim(Nvl(poDate.serie_chit, []))) + [ ] + Alltrim(Str(poDate.nr_incasare)) + [ din ] + Dtoc(poDate.dataact) + [ .]
			Endif
			*!*	modificare	ROAAUTO v 2.0.28	^
			*!*	modificare	v 2.0.189
		Case Inlist(poDate.tip, -9, -10)
			lcSetare = [AVIZ]
			lcExplicatieRaport = [avizul]
			lcSetareVizualizare = [PREVIZAVIZ]
			lcRaport = [PROTOCOL2]
			*!*	modificare	v 2.0.189	^
		Otherwise											&&	avize
			lcSetare = [AVIZ]
			lcExplicatieRaport = [avizul]
			*!*	modificare	v 2.0.46
			*!*		lnVizualizare = Iif(Type('gnVizualAviz')='N',gnVizualAviz,1)
			lcSetareVizualizare = [PREVIZAVIZ]
			*!*		lcRaport = [AVIZ]
			*!*	modificare	v 2.0.77
			*!*		If Nvl(gnPretListSubunitati,2)=2
			*!*			Do Case
			*!*			Case gnFormatAviz = 1	&&  format A4
			*!*				lcRaport = [AVIZ]
			*!*			Case gnFormatAviz = 2	&&	format A5
			*!*				lcRaport = [AVIZ_A5]
			*!*			Case gnFormatAviz = 3
			*!*				If optiuni_format_listare(1) = 2
			*!*					lcRaport = [AVIZ_A5]
			*!*				Else
			*!*					lcRaport = [AVIZ]
			*!*				Endif
			*!*			Endcase
			*!*		Else
			*!*			Do Case
			*!*			Case gnFormatAviz = 1	&&  format A4
			*!*				lcRaport = [AVIZ_TRANSFER_CTVA]
			*!*			Case gnFormatAviz = 2	&&	format A5
			*!*				lcRaport = [AVIZ_TRANSFER_CTVA_A5]
			*!*			Case gnFormatAviz = 3
			*!*				If optiuni_format_listare(1) = 2
			*!*					lcRaport = [AVIZ_TRANSFER_CTVA_A5]
			*!*				Else
			*!*					lcRaport = [AVIZ_TRANSFER_CTVA]
			*!*				Endif
			*!*			Endcase
			*!*		Endif
			lcRaport = [AVIZ]
			&&	transferuri intre subunitati : 23,25,27,28,29,30,41,-6
			If Nvl(gnPretListAviz, 2) = 1
				lcRaport = lcRaport + [_CTVA]
				lnPretListAviz = 1
			Endif
			If gnFormatAviz = 2	&&	format A5
				lcRaport = lcRaport + [_A5]
			Else
				If gnFormatAviz = 3
					If optiuni_format_listare(1) = 2
						lcRaport = lcRaport + [_A5]
					Endif
				Endif
			Endif
			*!*	modificare	v 2.0.77	^
			*!*	modificare	v 2.0.46	^
	Endcase

	* DECLARATIE CONFORMITATE
	FOR lnLinie = 1 TO 8
		lcLinie = 'gcTEXTCONFORMFACT' + ALLTRIM(STR(m.lnLinie))
		&lcLinie = STRTRAN(&lcLinie, '<FIRMA>', ALLTRIM(UPPER(NVL(poFirma.denumire,''))),1,10,1)
		&lcLinie = STRTRAN(&lcLinie, '<ADRESA>', ALLTRIM(NVL(poFirma.adresa,'')),1,10,1)
		&lcLinie = STRTRAN(&lcLinie, '<REG_COMERT>', ALLTRIM(NVL(poFirma.reg_comert,'')),1,10,1)
		&lcLinie = STRTRAN(&lcLinie, '<COD_FISCAL>', ALLTRIM(NVL(poFirma.cod_fiscal,'')),1,10,1)						
		&lcLinie = STRTRAN(&lcLinie, '<FACTURA_NR>', ALLTRIM(STR(poDate.nract)),1,10,1)						
		&lcLinie = STRTRAN(&lcLinie, '<FACTURA_DATA>', ALLTRIM(TRANSFORM(poDate.dataact)),1,10,1)
	ENDFOR 
	
	&&	prelucrare factura si aviz de retur
	prelucreaza_factura([crsfactura], [crsfacturaset], [crsfacturafinala], poDate.discount_evidentiat, poDate.in_valuta, lnPretListAviz, poDate.nListareDetaliata)
	If Used('crsavizretur') And poDate.nract_retur <> 0
		prelucreaza_aviz_retur(poDate.discount_evidentiat)				&&	ofacturare_comun.prg
	Endif

	lcCursorFacturaTemp = 'crsFacturaFinala'
	If (poDate.tip = 51 And poDate.in_valuta = 1)
		lcCursorFacturaTemp = 'crsfacturafinalaval' && la factura ACN este un singur formular pentru intern si extern. nu mai listez un formular suplimentar pentru extern
	Endif

	Select (m.lcCursorFacturaTemp)
	Calculate Cnt() To pnTotalPozitii For cantitate <> 0
	*!*	modificare v 2.0.80
	If poDate.incasat <> 0 And poDate.eProforma = 0 AND poDate.nEFactura = 0 And poDate.in_valuta = 0 And pnTotalPozitii <= 10 And lcRaport = [FACTURA] ;
			And Iif(Type('glFacturaCuChitanta') <> 'L', .F., glFacturaCuChitanta) ;
			And poDate.ntip_incasare = 11	&&	modificare v 2.0.84
		lcRaport = [FACTURA_CHIT]
	Endif
	*!*	modificare v 2.0.80	^

	Go Top
	If poDate.listare_componenta_set = 0
		Select(m.lcCursorFacturaTemp)
		Set Filter To cantitate <> 0
	Endif

	loPreviewerConfig = Createobject("PreviewerConfig") && rapoarte.prg
	
	* daca listez factura
	If poDate.nListareJustPDF = 0 AND poDate.nEFactura = 0 
		If poDate.nListareSilentioasa = 1
			m.loPreviewerConfig.SetValue("lDirectPrint", .T.)
		Endif
		&&	listare factura / aviz / proforma

		goExport.export2frx(m.lcCursorFacturaTemp, lcRaport, .F., lcSetareVizualizare, lcSetare, , , .T., , loPreviewerConfig)	&&	modificare v 2.2.15
	ENDIF 

	* Optiune daca se trimite factura pe email dupa generarea facturii
	lnFacturaEmail = Nvl(citeste_optiune_firma([FACTURAEMAIL]), 0) && 0 = nu trimite, 1 = trimite fara confirmare, 2 = trimite cu confirmare
	lnFacturaEmail = IIF(m.lnFacturaEmail >0 and poDate.nRelistare = 0, m.lnFacturaEmail, 0) 

	llGenerarePDFDisk = .F.
	* daca generez doar pdf pe disk sau vreau sa trimit factura pe email (trebuie sa generez pdf)
	IF poDate.nListareJustPDF = 1 OR ;
		(m.lnFacturaEmail = 1) OR ;
		(m.lnFacturaEmail = 2 AND AMESSAGEBOX('Doriti sa trimiteti factura pe email (' + ALLTRIM(NVL(poDate.oClient.email_facturare,'')) + ')?',4+32,_screen.Caption) = 6)
		lcDestinationFile = GetPDFFacturaFile(poDate.dataact, poDate.nract)
		llGenerarePDFDisk = .T.
		If !File(m.lcDestinationFile) Or (File(m.lcDestinationFile) And AMESSAGEBOX('Doriti sa suprascrieti ' + m.lcDestinationFile + '?', 4 + 32, _Screen.Caption) = 6)
			m.loPreviewerConfig.SetValue("cDestFile", m.lcDestinationFile)
			m.loPreviewerConfig.SetValue("lOpenDestFile", .F.)
			m.loPreviewerConfig.SetValue("lDirectPrint", .T.)
			m.loPreviewerConfig.SetValue("lOpenViewer", .F.)

			lcPdfHasImage = goApp.ReadIni([email_factura], "pdfhasimage")
			llPdfHasImage = (m.lcPdfHasImage = "1")
			If Empty(m.lcPdfHasImage)
				goApp.WriteIni([email_factura], "pdfhasimage", "0")
				llPdfHasImage = .F.
			Endif
			m.loPreviewerConfig.SetValue("lPDFasImage", m.llPdfHasImage)

			* tcAlias, tcRaport, tlCereTitlu, tcSetareVizualizare, tcSetareImprimanta, tcParametriListare, toListener, tlMultiPreview, tcReportPreviewer, toPreviewerConfig
			goExport.export2frx(m.lcCursorFacturaTemp, lcRaport, .F., lcSetareVizualizare, lcSetare, , , .T., , loPreviewerConfig)
		ENDIF
		
		If m.lnFacturaEmail > 0 AND File(m.lcDestinationFile)
			wrap_email_facturi(null, "", 1, 1, 1, poDate.nid_vanzare)
		ENDIF 
	ENDIF
	
	&&	salvare factura / aviz
	If	poDate.nRelistare =	0 And poDate.eProforma = 0 AND poDate.nEFactura = 0 And poDate.nSalveazaAtasamente = 1
		goExport.export2pdf(m.lcCursorFacturaTemp, lcRaport, .F., poDate.cDocAtasate)
	Endif
	If poDate.listare_componenta_set = 0
		Select (m.lcCursorFacturaTemp)
		Set Filter To
	Endif

	*!*	modificare	ROAAUTO v 2.0.28
	&&	certificat garantie ROAAUTO
	If poDate.tip = -12 AND poDate.nEFactura = 0 
		If gnTIP_LFACTURA_LINII = 2 And gnCGarantie = 1
			AMESSAGEBOX("Introduceti facturile pentru a se imprima certificatul de garantie pe verso!", 48, "Atentie")
			If lcRaport = [FACTURA_A5]
				goExport.export2frx('crsfacturafinala', [certificatg_a5], .F., , , , , .T.)	&&	modificare ROAAUTO v 2.1.9
			Else
				goExport.export2frx('crsfacturafinala', [certificatg_a4], .F., , , , , .T.)	&& 	modificare ROAAUTO v 2.1.9
			Endif
		Endif
	Endif
	*!*	modificare	ROAAUTO v 2.0.28	^

	&&	listare si salvare aviz retur
	If Used('crsfacturafinala2') AND poDate.nEFactura = 0 
		Select crsfacturafinala2
		poDate.text_aditional = poDate.text_aditional_retur
		poDate.nract = poDate.nract_retur
		poDate.serie_act = poDate.serie_act_retur

		Calculate Cnt() To pnTotalPozitii For cantitate <> 0
		Go Top

		lcRaport = [AVIZ]
		lcSetare = [AVIZ]
		lcSetareVizualizare = [PREVIZAVIZ]
		goExport.export2frx('crsfacturafinala2', lcRaport, .F., lcSetareVizualizare, lcSetare, , , .T.)			&& modificare v 2.2.15
		If	poDate.nRelistare =	0 And poDate.eProforma = 0 And poDate.nSalveazaAtasamente = 1
			goExport.export2pdf('crsfacturafinala2', lcRaport, .F., poDate.cDocAtasate)
		Endif
		Use In crsfacturafinala2
	Endif

	&&	listare factura in valuta
	If poDate.in_valuta = 1 And poDate.tip <> 51 AND poDate.nEFactura = 0 && la factura ACN este un singur formular pentru intern si extern. nu mai listez un formular suplimentar pentru extern

		Select crsfacturafinalaval
		Calculate Cnt() To pnTotalPozitii For cantitate <> 0
		Go Top
		If poDate.listare_componenta_set = 0
			Select crsfacturafinalaval
			Set Filter To cantitate <> 0
		ENDIF
		
		* daca listez factura
		If poDate.nListareJustPDF = 0
			If poDate.nListareSilentioasa = 1
				m.loPreviewerConfig.SetValue("lDirectPrint", .T.)
			Endif
			&&	listare factura / aviz / proforma

			goExport.export2frx('crsfacturafinalaval', lcRaportVal, .F., lcSetareVizualizare, lcSetare, , , .T., , loPreviewerConfig)	&&	modificare v 2.2.15
		ENDIF 
		
		* llGenerarePDFDisk
		* Am verificat deja la listarea facturii in lei daca se doreste salvarea pdf pe disk pentru email
		IF m.llGenerarePDFDisk
			lcDestinationFile = GetPDFFacturaValutaFile(poDate.dataact, poDate.nract)
			If !File(m.lcDestinationFile) Or (File(m.lcDestinationFile) And AMESSAGEBOX('Doriti sa suprascrieti ' + m.lcDestinationFile + '?', 4 + 32, _Screen.Caption) = 6)
				m.loPreviewerConfig.SetValue("cDestFile", m.lcDestinationFile)
				m.loPreviewerConfig.SetValue("lOpenDestFile", .F.)
				m.loPreviewerConfig.SetValue("lDirectPrint", .T.)
				m.loPreviewerConfig.SetValue("lOpenViewer", .F.)

				lcPdfHasImage = goApp.ReadIni([email_factura], "pdfhasimage")
				llPdfHasImage = (m.lcPdfHasImage = "1")
				If Empty(m.lcPdfHasImage)
					goApp.WriteIni([email_factura], "pdfhasimage", "0")
					llPdfHasImage = .F.
				Endif
				m.loPreviewerConfig.SetValue("lPDFasImage", m.llPdfHasImage)

				* tcAlias, tcRaport, tlCereTitlu, tcSetareVizualizare, tcSetareImprimanta, tcParametriListare, toListener, tlMultiPreview, tcReportPreviewer, toPreviewerConfig
				goExport.export2frx('crsfacturafinalaval', lcRaportVal, .F., lcSetareVizualizare, lcSetare, , , .T., , loPreviewerConfig)
			ENDIF
			
			If m.lnFacturaEmail > 0 AND File(m.lcDestinationFile)
				wrap_email_facturi(null, "", 1, 1, 1, poDate.nid_vanzare)
			ENDIF 
		ENDIF && llGenerarePDFDisk	
		
		
*!*			goExport.export2frx('crsfacturafinalaval', lcRaportVal, .F., lcSetareVizualizare, lcSetare, , , .T.)	&& modificare v 2.2.15
		If	poDate.nRelistare =	0 And poDate.eProforma = 0 And poDate.nSalveazaAtasamente = 1
			goExport.export2pdf('crsfacturafinalaval', lcRaportVal, .F., poDate.cDocAtasate)
		Endif
		Use In crsfacturafinalaval
		*!*	modificare v 2.0.80
		*!*		poDate.text_aditional = Strtran(poDate.text_aditional,[Exchange rate =],[Curs valutar =])
		poDate.text_aditional = poDate.text_aditional_en
		*!*	modificare v 2.0.80	^
	Endif

	&&	salveaza recapitulatie
	If	poDate.nRelistare =	0 And poDate.eProforma = 0 AND poDate.nEFactura = 0 And Used('crsfacturaset') And poDate.nSalveazaAtasamente = 1
		prelucreaza_recapitulatie(poDate.discount_evidentiat)
		Select crsrecapitulatie
		Calculate Cnt() To pnTotalPozitii For cantitate <> 0
		Go Top
		goExport.export2pdf('crsrecapitulatie', 'recapitulatie', .F., poDate.cDocAtasate)
		Select crsrecapitulatie
		Go Top
	Endif

	&&	listare recapitulatie
	If poDate.listare_recapitulatie = 1 AND poDate.nEFactura = 0 
		If poDate.nRelistare = 1 ;
				Or (poDate.nRelistare =	0 And poDate.eProforma = 0 And Used('crsfacturaset') And poDate.nSalveazaAtasamente = 0)	&&	modificare v 2.0.67
			prelucreaza_recapitulatie(poDate.discount_evidentiat)
			Select crsrecapitulatie
			Calculate Cnt() To pnTotalPozitii For cantitate <> 0
			Go Top
		Endif
		goExport.export2frx('crsrecapitulatie', 'recapitulatie', , , , , , .T.)	&& modificare v 2.2.15
	Endif
	If Used('crsrecapitulatie')
		Use In crsrecapitulatie
	Endif

	&&	listare lista livrare
	If poDate.listare_lista_livrare = 1 AND poDate.nEFactura = 0 	&&	pentru proforme e intotdeauna 0 ( vezi alege_optiuni_listare_facturare )
		prelucreaza_lista_livrare()
		goExport.export2frx('crslistalivrare', 'lista_livrare', , , , , , .T.)	&& modificare v 2.2.15
		Use In crslistalivrare
	Endif

	*!*	modificare v 2.0.80
	If poDate.incasat <> 0 And poDate.eProforma = 0 AND poDate.nEFactura = 0 And poDate.in_valuta = 0 And !Inlist(lcRaport, [FACTURA_CHIT], [FACTURATIP_CUCHIT]) ;
			And poDate.ntip_incasare = 11	&& modificare ROAFACTURARE v 2.0.84
		goExport.export2frx('crsfacturafinala', 'chitanta', .F., lcSetareVizualizare, lcSetare, , , .T.)	&& modificare v 2.2.15
	Endif
	*!*	modificare v 2.0.80	^

	&&	salvare atasamente
	If poDate.nRelistare = 0 And poDate.eProforma = 0 AND poDate.nEFactura = 0 And poDate.nSalveazaAtasamente = 1
		poDate.scrieAtasamente()
	Endif

	* Listare eFactura
	IF poDate.nEFactura = 1
		&&	listare factura in valuta
		If poDate.in_valuta = 1 And poDate.tip <> 51 && la factura ACN este un singur formular pentru intern si extern. nu mai listez un formular suplimentar pentru extern
			lcCursoreFactura = 'crsfacturafinalaval'
		ELSE 
			lcCursoreFactura = m.lcCursorFacturaTemp
		ENDIF 	

		SELECT (m.lcCursoreFactura)
		Calculate Cnt() To pnTotalPozitii For cantitate <> 0
		Go Top
		If poDate.listare_componenta_set = 0
			SELECT (m.lcCursoreFactura)
			Set Filter To cantitate <> 0
		ENDIF
		
			
		llSilentios = (poDate.nListareSilentioasa = 1)
		poDate.oInfoEFactura = goExport.export2xml_efactura(poDate, m.lcCursoreFactura, m.llSilentios)	&&	modificare v 2.2.15
	ENDIF 

	Use In (SELECT('crsfacturafinala'))
Endproc
******************************************** SFARSIT: listeaza_ofacturare **********************************************
Procedure listeaza_oconformitate
	Local loArticol
	Private poClient, poFirma&&,pcstring1,pcstring2,pcstring3

	If Used('crsFirma')
		Use In crsFirma
	Endif

	lcSql = [select antet as denumire,cod_fiscal,localitate,reg_comert from syn_nom_firme ] + ;
		[where id_firma = ?gnIdFirma]
	lcCursor = "crsFirma"
	If !goExecutor.oExecuta(lcSql, lcCursor)
		Return
	Endif

	Select crsFirma
	Scatter Name poFirma

	*!*	modificare	v 2.0.46
	*!*	If Used('crsConformitate')
	*!*		Use In crsConformitate
	*!*	Endif
	lcCursorTemp = "crsConformitateT"
	lcCursor = "crsConformitate"
	If Used(lcCursorTemp)
		Use In (lcCursorTemp)
	Endif
	*!*	modificare	v 2.0.46	^

	*!*	modificare	v 2.2.18 : am adaugat distinct si r.sters = 0 and r.id_tip_rulaj = 0
	lcSql = [select distinct r.id_articol,a.codmat,a.denumire,a.codmatf,a.conditii_pastrare,r.lot,r.adata_expirare,r.dataact ] + ;
		[from rul r left join nom_articole a on r.id_articol = a.id_articol where r.cod = ?poDate.Cod and r.sters = 0 and r.id_tip_rulaj = 0]
	*!*	modificare	v 2.0.46
	*!*	lcCursor="crsConformitate"
	*!*	lnSucces = goExecutor.oExecute(lcSql,lcCursor)
	If !goExecutor.oExecuta(lcSql, lcCursorTemp)
		*!*	modificare	v 2.0.46	^
		Return
	Endif
	lcRaport = "conformitate"
	*	loExport = Createobject("oExportConfig")

	*!*	pcstring1="Noi " + NVL(ALLTRIM(poFirma.denumire),"") + "," + NVL(ALLTRIM(poFirma.Localitate),"") + "cu nr. de inregistrare la Registrul Comertului nr. " + NVL(ALLTRIM(poFirma.reg_comert),"")+ " Cod Fiscal "+NVL(ALLTRIM(poFirma.cod_fiscal),"")
	*!*	pcstring2=", declaram si garantam pe proprie raspundere ca produsele la care se refera aceasta declaratie nu pun in pericol"
	*!*	pcstring3=	" viata sau sanatatea si sunt in conformitate cu legislatia sanitara, sanitar-veterinara si pentru siguranta alimentelor, in vigoare :"
	*!*		MESSAGEBOX(pcstring1 + pcstring2 + pcstring3)

	*!*	listareUserReport('crsConformitate','FRX',lcRaport)
	*!*	Use In crsFirma
	Select codmat, codmatf, denumire, lot, adata_expirare, dataact, conditii_pastrare, id_articol From (lcCursorTemp) ;
		Into Cursor (lcCursor) Readwrite
	Select (lcCursor)
	Browse
	goExport.export2frx(lcCursor, lcRaport, , , , , , .T.)	&& modificare v 2.2.15
	Use In crsFirma
	Use In (lcCursorTemp)
	Use In (lcCursor)
	*!*	modificare	v 2.0.46	^
Endproc


*******************************************************************************************************************
Procedure listeaza_bon_fiscal
	Lparameters ttDataOra, tnNrBon, tnSumaAchitNumerar, tnSumaAchitCARD, tnProcentTva, tnNrFact, tnBonDetaliat, tcCursorFactura
	* ttDataOra: dataora listare
	* tnNrBon: numar bon fiscal
	* tnSumaAchitNumerar: Suma achitata numerar
	* tnSumaAchitCARD: Suma achitata cu card la POS
	* tnProcentTVA
	* tnNrFact: Numar factura, daca NU se listeaza bonul detaliat pe articole
	* tnBonDetaliat: 1 = se listeaza articolele individual; 0 = "contravaloarea factura nr..."
	* tcCursorFactura: numele cursorului cu articole din factura pentru listarea detaliata a bonului fiscal

	Local lcCursor, lcCursorArticole, lcSelect, lnBonDetaliat, loHash, lnValoare, lnDiferenta
	lcSelect = Select()
	lnBonDetaliat = Iif(Type('tnBonDetaliat') = 'N', m.tnBonDetaliat, 0)
	lcCursorFactura = Iif(Type('tcCursorFactura') = 'C' And !Empty(m.tcCursorFactura), m.tcCursorFactura, '')

	lcCursor = [crsDateBon]
	Create Cursor (lcCursor) (denumire c(100), um c(10), CANT N(10, 4), Pret N(18, 4), DEPARTAMENT N(2), proc_tvav N(5, 2), PROCENT N(5, 2), DISCOUNT N(2) Default 0)


	DO CASE 
		CASE  m.lnBonDetaliat = 1 And !Empty(m.lcCursorFactura) And Used(m.lcCursorFactura)
			Insert Into (lcCursor) (denumire, um, CANT, Pret, DEPARTAMENT, proc_tvav) ;
				Select denumire, Nvl(um, 'BUC') As um, cantitate, (pretctva-discountctva), 1, proc_tvav From (m.lcCursorFactura)

		CASE  m.lnBonDetaliat = 0 And !Empty(m.lcCursorFactura) And Used(m.lcCursorFactura)
			* Daca bonul nu se listeaza detaliat, cumulez pe cote TVA
			SELECT (lcCursorFactura)
			SELECT proc_tvav, ROUND(SUM(cantitate*(pretctva-discountctva)), m.gnPC) as valoare FROM (lcCursorFactura) GROUP BY proc_tvav ORDER BY proc_tvav DESC INTO CURSOR cCoefTVATemp READWRITE 

			* Pun diferenta dintre totalul incasat si totalul sumelor defalcate pe cote TVA pe prima cota TVA
			CALCULATE SUM(valoare) TO lnValoare IN cCoefTVATemp
			lnDiferenta = m.tnSumaAchitNumerar + m.tnSumaAchitCARD - m.lnValoare
			SELECT cCoefTVATemp
			LOCATE FOR !EMPTY(valoare)
			IF !FOUND()
				GO TOP 
			ENDIF 
			REPLACE valoare WITH valoare + m.lnDiferenta

			Insert Into (lcCursor) (denumire, um, CANT, Pret, DEPARTAMENT, proc_tvav) ;
				SELECT [Fact.] + Alltrim(Str(tnNrFact)) as denumire, "" as um, 1 as cant, valoare as pret, 1 as departament, proc_tvav FROM cCoefTVATemp

			USE IN (SELECT('cCoefTVATemp'))
			
		OTHERWISE 
			Insert Into (lcCursor) (denumire, um, CANT, Pret, DEPARTAMENT, proc_tvav) ;
				Values ([Fact.] + Alltrim(Str(tnNrFact)), "", 1, m.tnSumaAchitNumerar + m.tnSumaAchitCARD, 1, 1 + (tnProcentTva / 100))
	ENDCASE 

	loHash = GetHash()
	loHash.SetValue("cTip", "1;")
	loHash.SetValue("nNumarBon", tnNrBon)
	loHash.SetValue("nValoareNumerar", m.tnSumaAchitNumerar)
	loHash.SetValue("nValoareCARD", m.tnSumaAchitCARD)
	loHash.SetValue("nValoareTotal", m.tnSumaAchitNumerar + m.tnSumaAchitCARD)
	loHash.SetValue("cCursor", lcCursor)
	loHash.SetValue("tDataOra", ttDataOra)
	goControllerEcr.imprima(loHash)

	Use In (lcCursor)
	Release loHash

	Select (m.lcSelect)
Endproc	&&	listeaza_bon_fiscal


*** Copiata din oproceduri_listari.prg ca sa nu se includa tot fisierul
Procedure listare_protocol
	Lparameters tnNract, tnTip

	*!* tnTip: 1 (default) = protocol (pret cu tva); 2 = sponsorizare (pret fara tva)
	Private pnNrAct, pnCod
	Local lcCursor, lcCursor2, lnTip
	lcCursor = [crsdateact]
	lcCursor2 = [crsprotocol]

	If Type('tnTip') <> 'N'
		lnTip = 1
	Else
		lnTip = tnTip
	Endif
	pnNrAct = tnNract
	*!* v 2.0.18 roagest (denumire partener, id_set 232, 233, 235)
	lcSql = [Select distinct a.cod, a.dataact, p.id_part as id_client, p.denumire, b.nume_gestiune as gestiune from act a ] + ;
		[ left join nom_gestiuni b on a.id_gestout = b.id_gestiune ] + ;
		[ left join nom_parteneri p on a.id_partc = p.id_part ] + ;
		[where a.an = ?gnAn and a.luna = ?gnLuna and a.nract = ?pnNrAct and a.scc = '401' and a.sters = 0 and a.id_set in (230,232,233,235,261) ] + ;
		Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala])
	lnSucces = goExecutor.oExecute(lcSql, lcCursor)
	If lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
		Return
	Endif

	If Reccount(lcCursor) > 0
		Select (lcCursor)
		Scatter Name poClient
		*!* v 2.0.18 roagest (iau partenerul din jurnal)
		*!*		AddProperty(poClient,[id_client],[0])
		*!*		AddProperty(poClient,[denumire],[])
		pnCod = cod
		poDate = Createobject("oDateFactura", 0, 0)
		poDate.nract = pnNrAct
		poDate.dataact = Iif(Type('dataact') = 'T', Ttod(dataact), dataact)
		poDate.dataora_exp = Get_Ora(2)			&&	modificare ROAGEST v 2.0.150

		*!*		loCauta = caut_partener()
		*!*		If gnButon <> 2
		*!*			poClient.id_client = loCauta.id_part
		*!*			poClient.denumire = loCauta.denumire
		*!*		Else
		poDate.tip = 23		&&	artificiu ca sa nu mai caute doar in delegatii pentru partenerul respectiv
		*!*		EndIf
		*!* v 2.0.18 roagest ^
		Use In (lcCursor)

		Private ofrmdatesupl
		ofrmdatesupl = Createobject("frm_alte_date")
		ofrmdatesupl.Show(1)
		Release ofrmdatesupl

		*!*	modificare ROAGEST v 2.0.150
		If gnButon = 1
			*!*	modificare ROAGEST v 2.0.150	^
			lcSql = [select denumire as nume_responsabil,cod_fiscal as CNPresp,reg_comert as BIresp from nom_parteneri ] + ;
				[where sters = 0 and inactiv = 0 and id_part in ] + ;
				[(select id_partener from utilizatori_rol_intern where sters = 0 and id_util = ?gnIdUtil)]
			lnSucces = goExecutor.oExecute(lcSql, lcCursor2)
			If lnSucces < 0
				AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
				Return
			Endif
			Select (lcCursor2)
			poDate.nume_responsabil	= nume_responsabil
			poDate.BIresp =	BIresp
			poDate.CNPresp = CNPresp
			Use In (lcCursor2)

			lcSql = [select a.cante as cantitate,] + ;
				Iif(lnTip = 1, [a.pretv+a.tvav], [a.pretv]) + [ as pretctva, ] + ;
				Iif(lnTip = 1, [Round(a.cante*(a.pretv+a.tvav),?gnPc)], [Round(a.cante*a.pretv,?gnPc)]) + [ as valctva,] + ;
				[a.serie,b.codmat,b.denumire,b.um ] + ;
				[from rul a left join nom_articole b on a.id_articol = b.id_articol ] + ;
				[where a.sters = 0 and a.cod = ?pnCod]
			lnSucces = goExecutor.oExecute(lcSql, lcCursor2)
			If lnSucces < 0
				AMESSAGEBOX(goExecutor.cEroare, 0 + 16, "Eroare")
				Return
			Endif
			goExport.export2frx(lcCursor2, [protocol])
			If Used(lcCursor2)
				Use In (lcCursor2)
			Endif
			*!*	modificare ROAGEST v 2.0.150
		Endif
		*!*	modificare ROAGEST v 2.0.150	^
	Else
		AMESSAGEBOX("Nu exista avizul cu acest numar in luna curenta!", 48, "Atentie")
	Endif
	Release lcCursor, lcCursor2, pnNrAct, pnCod
Endproc	&&	listare_protocol

********************************************************
* apelez listarea Recapitulatiei cu numele unui cursor si numele unui raport
********************************************************
Procedure listeaza_recapitulatie
	Lparameters tcNumeCursor, tcRaport

	Local lcSql, lcCursorDetalii, lcSetareVizualizare, lcSetare, lcRaport, lcSelect, lnListareJustPDF, lnListareSilentioasa
	Private poFirma, poClient, pnIdVanzare, pnIdClient
	poFirma = Null
	poClient = Null
	lcSelect = Select()

	lnListareJustPDF = 0
	lnListareSilentioasa = 0
	pnIdVanzare = 0
	pnIdClient = 0

	If Type('poDate') = 'O'
		pnIdVanzare = Iif(Type('poDate.nid_vanzare') = 'N', poDate.nid_vanzare, 0)
		pnIdClient = Iif(Type('poDate.id_client') = 'N', poDate.id_client, 0)
		lnListareJustPDF = Iif(Type('poDate.nListareJustPDF') = 'N', poDate.nListareJustPDF, 0)
		lnListareSilentioasa = Iif(Type('poDate.nListareSilentioasa') = 'N', poDate.nListareSilentioasa, 0)
	Endif

	If Empty(m.tcNumeCursor)
		Select (m.lcSelect)
		Return
	Endif

	TEXT TO lcSql NOSHOW
select 3 as tip,antet as denumire,cod_fiscal,'' as cod_fiscal2,adresa,telefon,fax,reg_comert,banca1 as banca,
	cont_banca1 as cont_banca,
	banca2,cont_banca2,banca3,cont_banca3,
	null as dataora,0 as tip_persoana,capital_soc_var,capital_soc_sub, '' as adresa_facturare,''as email_facturare, '' as denadrf
	from syn_nom_firme where id_firma = ?gnIdFirma
	ENDTEXT

	If !goExecutor.oExecuta(lcSql, 'crsFirma')
		Select (m.lcSelect)
		Return
	Endif
	Select crsFirma
	Go Top
	Scatter Name poFirma Memo
	Use In (Select('crsFirma'))

	TEXT TO lcSql NOSHOW
select 1 as tip,(case when tip_persoana = 1 then p.prefix||' '|| p.denumire||' '|| p.sufix else denumire end) as denumire,
	p.cod_fiscal,p.cod_fiscal2,p.adresa,p.telefon,'' as fax,p.reg_comert,p.banca,p.cont_banca,
	'' as banca2,'' as cont_banca2,'' as banca3,'' as cont_banca3,
	sysdate as dataora,p.tip_persoana,0 as capital_soc_var,
	0 as capital_soc_sub,d.adresa as adresa_facturare,d.email as email_facturare,d.denumire_adresa as denadrf
	from vnom_parteneri p left join vanzari b on b.id_part=p.id_part and b.id_vanzare = ?pnIdVanzare
	left join vadrese_parteneri d on d.id_adresa=b.id_facturare
	where p.id_part = ?pnIdClient
	ENDTEXT
	If !goExecutor.oExecuta(lcSql, 'crsClient')
		Select (m.lcSelect)
		Return
	Endif
	Select crsClient
	Go Top
	Scatter Name poClient Memo
	Use In (Select('crsClient'))

	lcRaport = Iif(Type('tcRaport') = 'C' And !Empty(m.tcRaport), m.tcRaport, 'recapitulatie.frx')

	lcSetare = [RECAPITULATIE]
	lcSetareVizualizare = [PREVIZRECAP]
	loPreviewerConfig = Createobject("PreviewerConfig") && rapoarte.prg
	If m.lnListareJustPDF = 0
		If m.lnListareSilentioasa = 1
			m.loPreviewerConfig.SetValue("lDirectPrint", .T.)
		Endif
		&&	listare factura / aviz / proforma
		goExport.export2frx(m.tcNumeCursor, m.lcRaport, .F., m.lcSetareVizualizare, m.lcSetare, , , .T., , loPreviewerConfig)	&&	modificare v 2.2.15
	Else
		lcDestinationFile = GetPDFRecapitulatiaFile(poDate.dataact, poDate.nract)
		If !File(m.lcDestinationFile) Or (File(m.lcDestinationFile) And AMESSAGEBOX('Doriti sa suprascrieti ' + m.lcDestinationFile + '?', 4 + 32, _Screen.Caption) = 6)
			m.loPreviewerConfig.SetValue("cDestFile", m.lcDestinationFile)
			m.loPreviewerConfig.SetValue("lOpenDestFile", .F.)
			m.loPreviewerConfig.SetValue("lDirectPrint", .T.)
			m.loPreviewerConfig.SetValue("lOpenViewer", .F.)
			lcPdfHasImage = goApp.ReadIni([email_factura], "pdfhasimage")
			llPdfHasImage = (m.lcPdfHasImage = "1")
			If Empty(m.lcPdfHasImage)
				goApp.WriteIni([email_factura], "pdfhasimage", "0")
				llPdfHasImage = .F.
			Endif
			m.loPreviewerConfig.SetValue("lPDFasImage", m.llPdfHasImage)

			* tcAlias, tcRaport, tlCereTitlu, tcSetareVizualizare, tcSetareImprimanta, tcParametriListare, toListener, tlMultiPreview, tcReportPreviewer, toPreviewerConfig
			goExport.export2frx(m.tcNumeCursor, m.lcRaport, .F., m.lcSetareVizualizare, m.lcSetare, , , .T., , loPreviewerConfig)
		Endif
	Endif
Endproc && listeaza_recapitulatie

*********************************************************************************
*** intoarce calea catre fisierul pdf FACTURA
Function GetPDFFacturaFile
	Lparameters tdDataFactura, tnNumarFactura

	Local lcDestinationFile, lcTip, lcFileName
	Local lcDefaultPrefix, lcDefaultSufix
	lcTip = 'factura'
	lcFileName = Dtos(m.tdDataFactura) + "_" + Alltrim(Str(m.tnNumarFactura))
	lcDefaultPrefix = ''
	lcDefaultSufix = m.lcTip
	lcDestinationFile = GetPDFFile(m.lcTip, m.lcFileName, m.lcDefaultPrefix, m.lcDefaultSufix)

	Return m.lcDestinationFile
Endfunc && GetPDFFacturaFile

*********************************************************************************
*** intoarce calea catre fisierul pdf FACTURA FISCALA VALUTA
Function GetPDFFacturaValutaFile
	Lparameters tdDataFactura, tnNumarFactura

	Local lcDestinationFile, lcTip, lcFileName
	Local lcDefaultPrefix, lcDefaultSufix
	lcTip = 'factura_valuta'
	lcFileName = Dtos(m.tdDataFactura) + "_" + Alltrim(Str(m.tnNumarFactura))
	lcDefaultPrefix = ''
	lcDefaultSufix = m.lcTip
	lcDestinationFile = GetPDFFile(m.lcTip, m.lcFileName, m.lcDefaultPrefix, m.lcDefaultSufix)

	Return m.lcDestinationFile
Endfunc && GetPDFFacturaValutaFile

*********************************************************************************
*** intoarce calea catre fisierul pdf INVOICE
Function GetPDFInvoiceFile
	Lparameters tdDataFactura, tnNumarFactura

	Local lcDestinationFile, lcTip, lcFileName
	Local lcDefaultPrefix, lcDefaultSufix
	lcTip = 'invoice'
	lcFileName = Dtos(m.tdDataFactura) + "_" + Alltrim(Str(m.tnNumarFactura))
	lcDefaultPrefix = ''
	lcDefaultSufix = m.lcTip
	lcDestinationFile = GetPDFFile(m.lcTip, m.lcFileName, m.lcDefaultPrefix, m.lcDefaultSufix)

	Return m.lcDestinationFile
Endfunc && GetPDFInvoiceFile

*********************************************************************************
*** intoarce calea catre fisierul pdf RECAPITULATIA
Function GetPDFRecapitulatiaFile
	Lparameters tdDataFactura, tnNumarFactura

	Local lcDestinationFile, lcTip, lcFileName
	Local lcDefaultPrefix, lcDefaultSufix
	lcTip = 'recapitulatie'
	lcFileName = Dtos(m.tdDataFactura) + "_" + Alltrim(Str(m.tnNumarFactura))
	lcDefaultPrefix = ''
	lcDefaultSufix = m.lcTip
	lcDestinationFile = GetPDFFile(m.lcTip, m.lcFileName, m.lcDefaultPrefix, m.lcDefaultSufix)

	Return m.lcDestinationFile
Endfunc && GetPDFRecapitulatiaFile

*********************************************************************************
*** intoarce calea catre fisierul pdf BORDEROU: borderou_[aaaallzz]_[tnId].pdf
Function GetPDFBorderouFile
	Lparameters tdDataBorderou, tnIdBorderou

	Local lcDestinationFile, lcTip, lcFileName
	Local lcDefaultPrefix, lcDefaultSufix
	lcTip = 'borderou'
	lcFileName = Dtos(m.tdDataBorderou) + "_" + Alltrim(Str(m.tnIdBorderou))
	lcDefaultPrefix = ''
	lcDefaultSufix = m.lcTip
	lcDestinationFile = GetPDFFile(m.lcTip, m.lcFileName, m.lcDefaultPrefix, m.lcDefaultSufix)

	Return m.lcDestinationFile
Endfunc && GetPDFBorderouFile


*************************************************
*** Intoarce optiunea [pdf_factura_acn/recapitulatia][salvareautomata]
*** se foloseste dupa listare factura sau recapitulatie, sa se salveze automat pdf
*************************************************
Function GetPDFSalvareAutomata
	Lparameters tcTip
	Local lcTip, lcSection, lcPDFSalvareAutomata

	lcTip = Iif(!Empty(m.tcTip) And Type('tcTip') = 'C', Alltrim(Lower(m.tcTip)), 'factura')
	lcSection = 'pdf_' + m.lcTip + '_' + m.gcS && [pdf_factura_acn] / [pdf_recapitulatia_acn]
	lcPDFSalvareAutomata = goApp.ReadIni(m.lcSection, "salvareautomata")
	If Empty(m.lcPDFSalvareAutomata)
		lcPDFSalvareAutomata = "0"
		goApp.WriteIni(m.lcSection, "salvareautomata", m.lcPDFSalvareAutomata)
	Endif

	Return (lcPDFSalvareAutomata = "1")
Endfunc

*------------------------------------------
* Intoarce directorul catre un tip de document
* Ex: GetPdfPath(TIP_DOCUMENT) 
* intoarce variabila "pdfpath" din sectiunea [pdf_TIP_DOCUMENT_SCHEMA] din settings.ini 
* daca nu este completata variabila pdfpath, ofera completarea
* Ex: lcFacturaPath = GetDocumentPath('factura')
* Ex: lcRecapitulatiaPath = GetDocumentPath('recapitulatie')
Function GetDocumentPath
	Lparameters tcTip
	* tcTip (sectiunea de unde se citesc directorul pdf): factura/contract => email_factura/email_contract

	Local lcPdfPath, lcSection, lcTip
	lcPdfPath = ''
	lcTip = IIF(!EMPTY(m.tcTip), m.tcTip, 'factura')
	lcSection = 'pdf_' + m.lcTip + '_' + m.gcS && [pdf_factura_acn] / [pdf_recapitulatie_acn]
	lcPdfPath = goApp.ReadIni(m.lcSection, "pdfpath")
	If Empty(m.lcPdfPath)
		lcPdfPath = Getdir("", "Alege director pentru pdf-uri " + m.lcTip, "Director pdf " + m.lcTip, 64)
		If !Empty(m.lcPdfPath)
			goApp.WriteIni(m.lcSection, "pdfpath", m.lcPdfPath)
		Endif
	Endif
	Return m.lcPdfPath
Endfunc

*------------------------------------------
*** intoarce calea catre fisierul pdf
Function GetPDFFile
	Lparameters tcTip, tcFileName, tcDefaultPrefix, tcDefaultSufix
	* tcTip (sectiunea de unde se citesc directorul pdf): factura/contract => email_factura/email_contract
	* tcFileName (numele fisierului): 20160210_123456
	* tcDefaultPrefix: daca se ataseaza un prefix la tcFileName. Se suprascrie cu cel din din setting.ini
	* tcDefaultSufix: daca se ataseaza un sufix la tcFileName. Se suprascrie cu cel din din setting.ini

	Local lcDestinationFile, lcFacturiPdfPath, lcSection, lcTip, lcFileName
	Local lcPdfPath, lcPdfPrefix, lcPdfSufix

	lcTip = Iif(!Empty(m.tcTip) And Type('tcTip') = 'C', Alltrim(Lower(m.tcTip)), 'factura')
	lcFileName = Iif(!Empty(m.tcFileName), Lower(Transform(m.tcFileName)), Sys(2015))
	lcSection = 'pdf_' + m.lcTip + '_' + m.gcS && [pdf_factura_acn] / [pdf_recapitulatia_acn]
	lcPdfPath = goApp.ReadIni(m.lcSection, "pdfpath")
	lcPdfPrefix = goApp.ReadIni(m.lcSection, "pdfprefix")
	lcPdfSufix = goApp.ReadIni(m.lcSection, "pdfsufix")
	If Empty(m.lcPdfPrefix) And !Empty(m.tcDefaultPrefix)
		lcPdfPrefix = m.tcDefaultPrefix
		goApp.WriteIni(m.lcSection, "pdfprefix", m.lcPdfPrefix)
	Endif
	If Empty(m.lcPdfSufix) And !Empty(m.tcDefaultSufix)
		m.lcPdfSufix = m.tcDefaultSufix
		goApp.WriteIni(m.lcSection, "pdfsufix", m.lcPdfSufix)
	Endif
	If Empty(m.lcPdfPrefix) And Empty(m.lcPdfSufix)
		m.lcPdfSufix = m.lcTip
		goApp.WriteIni(m.lcSection, "pdfsufix", m.lcPdfSufix)
	Endif

	If Empty(m.lcPdfPath)
		lcPdfPath = Getdir("", "Alege director pentru pdf-uri", "Director pdf", 64)
		If Empty(m.lcPdfPath)
			m.lcPdfPath = m.gcTempPath
		Endif
		goApp.WriteIni(m.lcSection, "pdfpath", m.lcPdfPath)
	Endif

	lcDestinationFile = Addbs(m.lcPdfPath) + Iif(!Empty(m.lcPdfPrefix), m.lcPdfPrefix + '_', '') + m.lcFileName + Iif(!Empty(m.lcPdfSufix), "_" + Lower(Transform(m.lcPdfSufix)), '') + ".pdf"
	Return m.lcDestinationFile
Endfunc && GetPDFFile

* Intoarce codmat client din PARTENERI_ARTICOLE_CODURI pentru un (client, articol)
Function GetCodmatC
	Parameters pnIdClient, pnIdArticol
	Private pcCod
	pcCod = ''

	llSucces = goExecutor.oSelecteaza2Value([select cod from parteneri_articole_coduri where id_part = ?pnIdClient and id_articol = ?pnIdArticol and sters = 0], @pcCod)
	Return Nvl(m.pcCod, '')
Endfunc && GetCodmatC

* Intoarce CODNC8, CODCPV din NOM_ARTICOLE pentru un articol
Function GetCodNC8CPV
	Parameters pnIdArticol
	LOCAL lcCursor, loRet, lcSelect
	lcSelect = SELECT()
	loRet = CREATEOBJECT("empty")
	ADDPROPERTY(loRet, 'codnc8', '')
	ADDPROPERTY(loRet, 'codcpv', '')

	lcCursor = SYS(2015)
	llSucces = goExecutor.oExecuta([select codnc8, codcpv from nom_articole where id_articol = ?pnIdArticol and sters = 0], m.lcCursor)
	IF m.llSucces
		SELECT (m.lcCursor)
		loRet.codnc8 = ALLTRIM(NVL(codnc8,''))
		loRet.codcpv = ALLTRIM(NVL(codcpv,''))		
	ENDIF 
	USE IN (SELECT(m.lcCursor))	
	SELECT (m.lcSelect)
	Return loRet
Endfunc && GetCodmatC

* intoarce nom_parteneri.institutie_publica pentru un id_part
function GetInstitutiePublica
	LPARAMETERS tnIdClient
	PRIVATE pnIdClient
	LOCAL lnInstitutiePublica
	pnIdClient = tnIdClient	
	lnInstitutiePublica = 0
	
	llSucces = goExecutor.oSelecteaza2Value([select MAX(NVL(institutie_publica,0)) as institutie_publica from nom_parteneri where id_part = ?pnIdClient and sters = 0], @lnInstitutiePublica)
	Return m.lnInstitutiePublica
ENDFUNC && GetInstitutiePublica