*!* 20.02.2015
*!* marius.mutu
*!* + caut_facturi_multiple_client_articol: alegere factura dupa client si articol in scop de retur articol pe o factura de vanzare normala

*!*	16.06.2015
*!*	marius.mutu
*!*	vizualizare_facturi + coloane:id_facturare, listare_detaliata

*!*	11.05.2017
*!*	marius.mutu
*!*	caut_contract_facturare: se poate adauga un contract din cautarea de contracte - parametru contracte_nou with tnIdPart

*!* 16.12.2019
*!* marius.mutu
*!* vizualizare_facturi, vizualizare_facturi2 crsDetalii.id_jtva_coloana_ex, jtva_coloana_ex

*!* 29.12.2019
*!* marius.mutu
*!* vizualizare_facturi, vizualizare_facturi2 crsFcturi.email_facturare, adresa_client, email_client, adresa_beneficiar, email_beneficiar pentru trimiterea facturii pe email la clienti

*!* 20.01.2020
*!* marius.mutu
*!* vizualizare_facturi, vizualizare_facturi2 crsFacturi.codmatc - codmat client

*!* 30.01.2020
*!* marius.mutu
*!*	vizualizare_facturi, vizualizare_facturi2 + id_util pentru listarea facturii cu responsabilul care a introdus factura

*!* 25.03.2020
*!* marius.mutu
*!*	vizualizare_facturi, vizualizare_facturi2 + tlShowRecords - arata facturile din luna la intrarea in formular

***************************************************************************************************************
****	Proceduri:
****		vizualizare_optiuni_facturare
****		configurare_serii_facturare
****		facturare_lista_de_preturi
****		facturare_contracte
****		facturare_comenzi
****		facturare_avize
****		facturare_articole_compuse
****		emitere_aviz_clienti
****		emitere_aviz_clienti_debitori
****		emitere_aviz_clienti_custodie
****		emitere_aviz_transfer
****		emitere_dispozitii_livrare
****		vizualizare_facturi
****		vizualizare_proforme
****		verifica_setari
****		vanzare_materii_prime
****		vanzare_produse
****		vanzare_marfa_pret_achi
****		vanzare_marfa_pret_vanz
****		vanzare_marfa_pret_achi_vanz
****		selecteaza_gestiuni
****		creeaza_cursor_seturi
****		creeaza_cursor_seturi_proforme
****		recreeaza_cursoare_retur
****		calculeaza_totaluri
****	Functii	:
****		actualizeaza_optiuni_program	-	inlocuita de actualizeaza_optiuni_program din oinit_optiuni.prg
****		caut_comanda_client
****		caut_comanda_gestiune
****		caut_contract_facturare
****		caut_avize
****		caut_facturi_client
****		caut_facturi_multiple_client
****		caut_gestiune_sursa
****		caut_utilizator_facturare
****		caut_gestiune_dest				-	am mutat-o in ocautare.prg
****		caut_politici_curente_util		-	veche 26.01
***************************************************************************************************************
************************************ INCEPUT: vizualizare_optiuni_facturare ***********************************
Procedure vizualizare_optiuni_facturare
	Lparameters tnTip
	&&	1	=	optiuni de pe server
	&&	2	=	optiuni din registrul Windows
	Local lnTipOptiuni
	If Used('crsoptiuni')
		Use In crsoptiuni
	Endif
	If gcNumeProgram = 'ROACONTRACTE'
		lnTipOptiuni = 2
	Else
		lnTipOptiuni = 1
	Endif
	Do Case
		Case tnTip = 1
			If actualizeaza_optiuni_program()
				ofrmoptiune = Createobject('frm_optiuni_facturare', lnTipOptiuni)
				ofrmoptiune.Show(1)
				Release ofrmoptiune
			Endif
		Case tnTip = 2
			ofrmoptiune = Createobject('frm_optiuni_facturare_local', lnTipOptiuni)
			ofrmoptiune.Show(1)
			Release ofrmoptiune
		Otherwise
			*!*				amessagebox("Optiune invalida!",16,"Eroare")
			lnTipOptiuni = 0
	Endcase
	If Used('crsoptiuni')
		Use In crsoptiuni
	Endif
	Release lnTipOptiuni
Endproc	&&	vizualizare_optiuni_facturare
************************************ SFARSIT: vizualizare_optiuni_facturare ***********************************
*!*	************************************ INCEPUT: facturare_din_stoc *******************************************
*!*	Procedure facturare_din_stoc
*!*	factureaza(7)
*!*	Endproc	&&	facturare_din_stoc
*!*	************************************ SFARSIT: facturare_din_stoc *******************************************
************************************ INCEPUT: facturare_lista_de_preturi ***********************************
Procedure facturare_lista_de_preturi
	Do politica.mpr
Endproc	&&	facturare_lista_de_preturi
************************************ SFARSIT: facturare_lista_de_preturi ***********************************
************************************ INCEPUT: facturare_contracte ******************************************
Procedure facturare_contracte
	LPARAMETERS tcTip

	LOCAL lcTip
	lcTip = UPPER(ALLTRIM(TRANSFORM(m.tcTip)))

	IF EMPTY(m.lcTip) OR !INLIST(m.lcTip, 'FACTURA LEI', 'INVOICE', 'FACTURA VALUTA')
		lcTip = 'FACTURA LEI'
	ENDIF 
	DO CASE 
		CASE m.lcTip = 'FACTURA LEI'
			factureaza(2)
		CASE m.lcTip = 'INVOICE'
			factureaza(6)
		CASE m.lcTip = 'FACTURA VALUTA'
			factureaza(52)
	ENDCASE 					
Endproc	&&	facturare_contracte
************************************ SFARSIT: facturare_contracte ******************************************
************************************ INCEPUT: facturare_comenzi ********************************************
Procedure facturare_comenzi
	factureaza(3)
Endproc	&&	facturare_comenzi
************************************ SFARSIT: facturare_comenzi ********************************************
************************************ INCEPUT: facturare_avize ***********************************************
Procedure facturare_avize
	factureaza(4)
Endproc	&&	facturare_avize
************************************ SFARSIT: facturare_avize ***********************************************

************************************ INCEPUT: copiere_factura ***********************************************
PROCEDURE copiere_factura
	LPARAMETERS toFactura
	factureaza(toFactura.Tip, toFactura)
ENDPROC && copiere_factura
************************************ SFARSIT: copiere_factura ***********************************************

********************************** INCEPUT: facturare_articole_compuse ***************************************
Function facturare_articole_compuse
	Private ofrmfactseturi
	Local lnNrInregRate
	*!*	modificare	v 2.0.56
	*!*	Dimension laStructura(1,18)
	*!*	Afields(laStructura,[crsfactura])
	*!*	If Used('crsfacturaset')
	*!*		Use In crsfacturaset
	*!*	Endif
	*!*	Create Cursor crsfacturaset From Array laStructura
	*!*	modificare	v 2.0.67
	*!*	copiaza_structura_cursor([crsfactura],[crsfacturaset])
	*!*	modificare	v 2.0.67	^
	*!*	modificare	v 2.0.56	^
	gnButon = 1
	Select crsfactura
	lnRecno = Recno()
	Replace id_set_fact With 0 All
	*!*	modificare	v 2.0.67
	Calculate Cnt() For Inlist(opt_facturare, 1, 2) To lnNrInregRate
	Set Filter To !Inlist(opt_facturare, 1, 2)
	If lnNrInregRate <> Reccount('crsfactura')
		copiaza_structura_cursor([crsfactura], [crsfacturaset])
		Select crsfactura
		*!*	modificare	v 2.0.67	^
		Go Top
		ofrmfactseturi = Createobject('frm_articole_compuse')
		ofrmfactseturi.Show(1)
		Release ofrmfactseturi
		*!*	modificare	v 2.0.67
	Endif
	Release lnNrInregRate
	*!*	modificare	v 2.0.67	^
	Select crsfactura
	*!*	modificare	v 2.0.67
	Set Filter To
	*!*	modificare	v 2.0.67	^
	Go Min(lnRecno, Reccount('crsfactura'))
	Return Iif(gnButon = 1, .T., .F.)
Endfunc
********************************** SFARSIT: facturare_articole_compuse ***************************************
************************************ INCEPUT: emitere_aviz_clienti *****************************************
Procedure emitere_aviz_clienti
	Lparameters tnTip
	Do Case
		Case tnTip = 1
			factureaza(21)		&&	pe baza de comenzi
		Case tnTip = 2
			factureaza(22)		&&	pe baza de lista de preturi
		Case tnTip = 3
			factureaza(26)		&&	pe baza de lista de contract

		Case tnTip = 4			&&	Retur aviz \<marfa (valoric) - gestiune la pret de achizitie *
			gnTipGest = 7
			initializeaza_vanzare_din_stoc(260)

		Case tnTip = 5			&&	Retur \<aviz marfa (valoric) - gestiune la pret de vanzare
			gnTipGest = 6
			initializeaza_vanzare_din_stoc(238)

		Case tnTip = 6 && protocol marfa la pret de vanzare
			gnTipGest = 6
			initializeaza_vanzare_din_stoc(270)

		Case tnTip = 7
			factureaza(24)		&&	Retur aviz

		Otherwise
			amessagebox("Nu exista configurarea pentru aceasta optiune!", 48, "Atentie")
	Endcase

Endproc	&&	emitere_aviz_clienti
************************************ SFARSIT: emitere_aviz_clienti *****************************************
******************************** INCEPUT: emitere_aviz_clienti_debitori ************************************
Procedure emitere_aviz_clienti_debitori
	Lparameters tnTip
	Do Case
		Case tnTip = 1
			factureaza(28)		&&	pe baza de comenzi
		Case tnTip = 2
			factureaza(29)		&&	pe baza de lista de preturi
		Otherwise
			amessagebox("Nu exista configurarea pentru aceasta optiune!", 48, "Atentie")
	Endcase

Endproc	&&	emitere_aviz_clienti
******************************** SFARSIT: emitere_aviz_clienti_debitori ************************************
******************************** INCEPUT: emitere_aviz_clienti_custodie ************************************
Procedure emitere_aviz_clienti_custodie
	Lparameters tnTip
	Do Case
		Case tnTip = 1
			factureaza(42)		&&	pe baza de comenzi
		Case tnTip = 2
			factureaza(47)		&&	pe baza de comenzi
		CASE tnTip = 3
			gnTipGest = 6
			initializeaza_vanzare_din_stoc(267)		&&	retur
		Otherwise
			amessagebox("Nu exista configurarea pentru aceasta optiune!", 48, "Atentie")
	Endcase

Endproc	&&	emitere_aviz_clienti_custodie
******************************** SFARSIT: emitere_aviz_clienti_custodie ************************************
************************************ INCEPUT: emitere_aviz_transfer ****************************************
Procedure emitere_aviz_transfer
	Lparameters tnTip
	Do Case
		Case tnTip = 1
			factureaza(25)		&&	pe baza de comenzi
		Case tnTip = 2
			factureaza(23)		&&	pe baza de lista de preturi
		Case tnTip = 3
			factureaza(27)		&&	pe baza de lucrare
		Case tnTip = 4
			factureaza(30)		&&	pe baza de NIR
			*!* v 2.0.16
		Case tnTip = 5
			factureaza(41)		&&	retur pe baza de lista de preturi
			*!* v 2.0.16 ^
		Otherwise
			amessagebox("Nu exista configurarea pentru aceasta optiune!", 48, "Atentie")
	Endcase
Endproc	&&	emitere_aviz_transfer
************************************ SFARSIT: emitere_aviz_transfer ****************************************
********************************** INCEPUT: emitere_dispozitii_livrare *************************************
Procedure emitere_dispozitii_livrare
	Private poFacturi
	Local llAfiseaza, loFrmFacturi
	Store .F. To llAfiseaza
	Store '' To poFacturi
	If Used('crsfacturi')
		Use In crsfacturi
	Endif

	pcschema1 = ['selectat n(1),id_vanzare n(20),dataora t,dataora_exp t,data_act d,numar_act n(14),serie_act c(10),'+] + ;
		['tip n(5),id_part n(10),discount n(17,4),discount_evidentiat n(1),text_aditional c(250),facturat n(1),data_facturat t,'+] + ;
		['explicatie c(240),altele c(200),client c(100),cod n(20),utilizator c(30),utilfact c(30),delegat c(70),'+] + ;
		['bidelegat c(30),nume_agent c(70),nrinmat c(10),total_fara_tva n(20,4),total_tva n(20,4),total_cu_tva n(20,4),id_gestiune n(10),tip_incasare n(10)']
	pcselect1 = ['select 0 as selectat,id_vanzare,dataora,dataora_exp,data_act,numar_act,serie_act,'+] + ;
		['tip,id_part,discount,discount_evidentiat,text_aditional,facturat,data_facturat,'+] + ;
		['explicatie,altele,client,cod,utilizator,utilfact,delegat,bidelegat,'+] + ;
		['nume_agent,nrinmat,total_fara_tva,total_tva,total_cu_tva,id_gestiune,tip_incasare from ] + gcS + [.fact_vfacturi ] + ;
		[where ']
	pcorder1 = [data_act]
	pcfiltru1 = [1=2]
	llAfiseaza = .F.
	gencursor('poFacturi', 'crsfacturi', pcselect1, pcfiltru1, pcschema1, pcorder1, llAfiseaza)
	poFacturi.ca_baza1.afisare()

	loFrmFacturi = Createobject("frm_emitere_dispozitii")
	loFrmFacturi.Show(1)

	Release loFrmFacturi, poFacturi, poDetalii
Endproc	&&	emitere_dispozitii_livrare
********************************** SFARSIT: emitere_dispozitii_livrare *************************************
************************************ INCEPUT: vizualizare_facturi ******************************************
Procedure vizualizare_facturi
	LPARAMETERS tlShowRecords
	* tlShowRecords: .T. arata facturile din luna curenta la intrarea in formular, default (.F.)

	Private poFacturi, poDetaliiFactura, pdData1, pdData2, pnRecords
	Local llAfiseaza, loFrmFacturi, lcData, lcSql
	llAfiseaza = .F.
	poFacturi = Null
	poDetalii = Null
	Use In (SELECT('crsfacturi'))
	Use In (SELECT('crsDetalii'))

	* Afisez automat facturile din luna daca tlShowRecords=.T. sau daca numarul de vanzari.count(*) <= optiunea gnVanzariRecords sau 500 de inregistrari
	IF !m.tlShowRecords
		lnVanzariRecords = IIF(TYPE('gnVanzariRecords') = 'N', m.gnVanzariRecords, 500)
		pdData1 = DATE(m.gnAn, m.gnLuna, 1)
		pdData2 = GOMONTH(m.pdData1, 1) - 1
		pnRecords = 0
		lcSql = [SELECT COUNT(*) as nr FROM vanzari WHERE data_act between ?pdData1 AND ?pdData2 AND STERS = 0]
		llSucces = goExecutor.oSelecteaza2Value(m.lcSql, @pnRecords)
		IF m.pnRecords < m.lnVanzariRecords	
			tlShowRecords = .T.
		ENDIF 
	ENDIF 

	lcSchema = ['ales n(1),id_vanzare n(20),id_comanda n(20),dataora t,dataora_exp t,data_act d,data_scad d,sters n(1),dataoras t,numar_act n(14),serie_act c(10),'+] + ;
		['tip n(5),id_part n(10),discount n(17,4),disc_fara_tva n(17,4),discount_evidentiat n(1),text_aditional M,facturat n(1),data_facturat t,'+] + ;
		['explicatie c(240),altele c(200),client c(100),cod n(20),id_util I,utilizator c(30),utils c(30),utilfact c(30),delegat c(70),'+] + ;
		['bidelegat c(30),cnpdelegat c(15),nume_agent c(70),nrinmat c(10),total_fara_tva n(20,4),total_tva n(20,4),total_cu_tva n(20,4),id_gestiune n(10),'+] + ;
		['serie_chit c(20),nr_incasare n(10),incasat n(17,4),in_valuta n(1),sucursala c(100),id_ruta n(10),ruta c(100),curs n(20,4),multiplicator n(10),nume_val c(50),'+] + ;
		['valuta c(50), id_valuta n(5), valval n(20,4), tvaval n(20,4), totval n(20,4),'+] + ;
		['id_delegat n(10),id_agent n(10),id_masina n(10),valoarea N(20,4),nume_gestiune C(50),id_sectie n(5),sectie c(50),id_lucrare n(20), lucrare c(200),'+] + ;
		['tip_incasare n(10),afisare_scadenta n(1),id_ordl n(10),tva_incasare n(1),listare_detaliata n(1),'+] + ;
		['id_facturare n(10),adresa_facturare c(250),email_facturare c(100),adresa_client c(250),email_client c(100),adresa_beneficiar c(250),email_beneficiar c(100),'+] + ;
		['id_beneficiar n(20),beneficiar C(100),tip_factura N(5),ctip_factura C(100), id_ctr N(10), contract C(100),eproforma N(1),tip_saft N(3) null,tip_saftname V(250) null,'+]+;
		['id_livrare N(10),adresa_livrare V(250), denumire_adresa_livrare V(100), email_livrare V(100),institutie_publica N(1) null,efactura N(1) null,cod_fiscal C(20),id_fact N(20)']

	lcSelect = ['select 0 as ales,id_vanzare,id_comanda,dataora,dataora_exp,data_act,data_scad,sters,dataoras,numar_act,serie_act,'+] + ;
		['tip,id_part,discount,disc_fara_tva,discount_evidentiat,text_aditional,facturat,data_facturat,'+] + ;
		['explicatie,altele,client,cod,id_util,utilizator,utils,utilfact,delegat,bidelegat,cnpdelegat,'+] + ;
		['nume_agent,nrinmat,total_fara_tva,total_tva,total_cu_tva,id_gestiune,serie_chit,nr_incasare,incasat,'+] + ;
		['in_valuta,sucursala,id_ruta,ruta,curs,multiplicator,nume_val,'+] + ;
		['valuta,id_valuta,valval,tvaval,totval,'+] + ;
		['id_delegat,id_agent,id_masina,valoarea,'+] + ;
		['nume_gestiune,id_sectie,sectie,id_lucrare,lucrare,tip_incasare,afisare_scadenta,id_ordl,tva_incasare,listare_detaliata,'+] + ;
		['id_facturare,adresa_facturare,email_facturare,adresa_client,email_client,adresa_beneficiar,email_beneficiar,'+] + ;
		['id_beneficiar, beneficiar, tip_factura, ctip_factura, id_ctr, contract, eproforma,tip_saft,tip_saftname, '+]+;
		['id_livrare,adresa_livrare,denumire_adresa_livrare,email_livrare,institutie_publica,efactura,cod_fiscal,id_fact from fact_vfacturi where ']
		
	lcorder = [data_act,numar_act]
	lcData = Alltrim(Str(gnAn)) + Padl(Alltrim(Str(gnLuna)), 2, [0]) + [01]
	lcFiltru = IIF(!m.tlShowRecords, [1=2], [data_act between to_date('] + lcData + [','YYYYMMDD') and last_day(to_date('] + lcData + [','YYYYMMDD')) AND STERS = 0])
	llAfiseaza = .F.
	lcGroup = []
	llModParam = .F.
	lcFiltruOriginal = []
	gencursor('poFacturi', 'crsfacturi', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza, lcGroup, llModParam, lcFiltruOriginal)

	poFacturi.ca_baza1.afisare()

*!*	TEXT TO lcSchema NOSHOW 
*!*	id_vanzare n(20),id_vanzare_det n(10),serie c(100), pret n(20,4), pret_val n(20,4),sters n(1),dataoras d, 
*!*	proc_tvav n(10,4), cantitate n(10,4), discount_unitar n(20,4), discount_unitar_val n(20,4), cont c(4), 
*!*	id_valuta n(10), id_gestiune n(20), pret_achizitie n(20,4), pret_cu_tva n(1), denumire c(250), explicatie c(240), 
*!*	um c(10), nume_lista_preturi c(100), nume_gestiune c(50), utils c(30), curs n(20,4), multiplicator n(10), 
*!*	nume_val c(10), tip_valuta n(1), id_vanzare_set n(10), codmatf c(50), codmat c(50), codbare c(50), 
*!*	id_subgrupa n(5), subgrupa c(100), client c(70), id_jtva_coloana n(10), id_ctr n(10), numar_contract c(50)
*!*	ENDTEXT 
lcSchema = ['id_vanzare n(20),id_vanzare_det n(10),lot C(20), serie c(100), pret n(20,6), pret_val n(20,6),sters n(1),dataoras d, '+] + ;
			['proc_tvav n(10,4), cantitate n(10,4), discount_unitar n(20,6), discount_unitar_val n(20,6), cont c(4), '+] + ;
			['id_valuta n(10), id_gestiune n(20), pret_achizitie n(20,6), pret_cu_tva n(1), denumire c(250), explicatie M, '+] + ;
			['um c(10), nume_lista_preturi c(100), nume_gestiune c(50), utils c(30), curs n(20,4), multiplicator n(10), '+] + ;
			['nume_val c(10), tip_valuta n(1), id_vanzare_set n(10), codmatf c(50), codmat c(50), codmatc c(50), codbare c(50), '+] + ;
			['id_subgrupa n(5), subgrupa c(100), client c(70), id_jtva_coloana n(10), jtva_coloana C(100), id_jtva_coloana_ex n(10), jtva_coloana_ex C(100), id_ctr n(10), numar_contract c(50), taxcode N(6), taxname C(250)']

*!*	TEXT TO lcSelect NOSHOW 
*!*	select id_vanzare, id_vanzare_det, serie, pret, pret_val, sters, dataoras, 
*!*	proc_tvav, cantitate, discount_unitar, discount_unitar_val, cont, 
*!*	id_valuta, id_gestiune, pret_achizitie, pret_cu_tva, denumire, explicatie, 
*!*	um, nume_lista_preturi, nume_gestiune, utils, curs, multiplicator, 
*!*	nume_val, tip_valuta, id_vanzare_set, codmatf, codmat, codbare, 
*!*	id_subgrupa, subgrupa, client, id_jtva_coloana, id_ctr, numar_contract 
*!*	from fact_vfacturi_detalii
*!*	ENDTEXT 
lcSelect = ['select id_vanzare, id_vanzare_det, lot, serie, pret, pret_val, sters, dataoras, '+] + ;
			['proc_tvav, cantitate, discount_unitar, discount_unitar_val, cont, '+] + ;
			['id_valuta, id_gestiune, pret_achizitie, pret_cu_tva, denumire, explicatie, '+] + ;
			['um, nume_lista_preturi, nume_gestiune, utils, curs, multiplicator, '+] + ;
			['nume_val, tip_valuta, id_vanzare_set, codmatf, codmat, codmatc, codbare, '+] + ;
			['id_subgrupa, subgrupa, client, id_jtva_coloana, jtva_coloana, id_jtva_coloana_ex, jtva_coloana_ex, id_ctr, numar_contract, taxcode, taxname '+] + ;
			['from fact_vfacturi_detalii where 2=2']

	lcOrder = [id_vanzare_det]
	lcfiltru = [1 = 2]
	llAfiseaza = .F.
	lcGroup = []
	llModParam = .F.
	lcFiltruOriginal = []
	gencursor('poDetalii', 'crsDetalii', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza, lcGroup, llModParam, lcFiltruOriginal)
	poDetalii.ca_baza1.afisare()

	SELECT crsfacturi
	loFrmFacturi = Createobject("frm_facturi")
	loFrmFacturi.Show(1)
	
	USE IN (SELECT('crsFacturi'))
	USE IN (SELECT('crsDetalii'))
	Release loFrmFacturi, poFacturi, poDetalii

Endproc
************************************ SFARSIT: vizualizare_facturi ****************************************

************************************ INCEPUT: vizualizare_facturi2 ******************************************
*** acelasi formular ca vizualizare_facturi
*** diferenta la fact_vfacturi.totalurile in lei si in valuta, valuta sunt calculate in vanzari, in loc sa fie luate din vanzari_detalii, pentru rapiditate
*** totalurile sunt completate in vanzari la insert into vanzari
************************************ INCEPUT: vizualizare_facturi2 ******************************************
Procedure vizualizare_facturi2
	LPARAMETERS tlShowRecords
	* tlShowRecords: .T. arata facturile din luna curenta la intrarea in formular, default (.F.)

	Private poFacturi, poDetaliiFactura, pdData1, pdData2, pnRecords
	Local llAfiseaza, loFrmFacturi, lcData, lcSql
	llAfiseaza = .F.
	poFacturi = Null
	poDetalii = Null
	Use In (SELECT('crsfacturi'))
	Use In (SELECT('crsDetalii'))

	* Afisez automat facturile din luna daca tlShowRecords=.T. sau daca numarul de vanzari.count(*) <= optiunea gnVanzariRecords sau 500 de inregistrari
	IF !m.tlShowRecords
		lnVanzariRecords = IIF(TYPE('gnVanzariRecords') = 'N', m.gnVanzariRecords, 500)
		pdData1 = DATE(m.gnAn, m.gnLuna, 1)
		pdData2 = GOMONTH(m.pdData1, 1) - 1
		pnRecords = 0
		lcSql = [SELECT COUNT(*) as nr FROM vanzari WHERE data_act between ?pdData1 AND ?pdData2 AND STERS = 0]
		llSucces = goExecutor.oSelecteaza2Value(lcSql, @pnRecords)
		IF m.pnRecords < m.lnVanzariRecords	
			tlShowRecords = .T.
		ENDIF 
	ENDIF 	
	
	TEXT TO lcSchema NOSHOW
ales n(1),id_vanzare n(20),id_comanda n(20),dataora t,dataora_exp t,data_act d,data_scad d,sters n(1),dataoras t,numar_act n(14),serie_act c(10),tip n(5),id_part n(10),discount n(17,4),disc_fara_tva n(17,4),discount_evidentiat n(1),text_aditional M,facturat n(1),data_facturat t,explicatie c(240),altele c(200),client c(100),cod n(20),id_util I, utilizator c(30),utils c(30),utilfact c(30),delegat c(70),bidelegat c(30),cnpdelegat c(15),nume_agent c(70),nrinmat c(10),total_fara_tva n(20,4),total_tva n(20,4),total_cu_tva n(20,4),id_gestiune n(10),serie_chit c(20),nr_incasare n(10),incasat n(17,4),in_valuta n(1),sucursala c(100),id_ruta n(10),ruta c(100),curs n(20,4),multiplicator n(10),nume_val c(50),valuta c(50), id_valuta N(5), valval n(20,4), tvaval n(20,4), totval n(20,4),id_delegat n(10),id_agent n(10),id_masina n(10),valoarea N(20,4),nume_gestiune C(50),id_sectie n(5),sectie c(50),id_lucrare n(20), lucrare c(200),tip_incasare n(10),afisare_scadenta n(1),id_ordl n(10),tva_incasare n(1),listare_detaliata n(1),id_facturare N(10), adresa_facturare c(250), email_facturare c(100), adresa_client c(250), email_client c(100), adresa_beneficiar c(250), email_beneficiar c(100),id_beneficiar n(20), beneficiar C(100), tip_factura N(5), ctip_factura C(100), id_ctr N(10), contract C(100),eproforma N(1),tip_saft N(3) null,tip_saftname V(250) null,id_livrare N(10),adresa_livrare V(250), denumire_adresa_livrare V(100), email_livrare V(100),institutie_publica N(1) null,efactura N(1) null, cod_fiscal C(20), id_fact n(20)
	ENDTEXT 


	TEXT TO lcSelect NOSHOW
select 0 as ales,id_vanzare,id_comanda,dataora,dataora_exp,data_act,data_scad,sters,dataoras,numar_act,serie_act,tip,id_part,discount,disc_fara_tva,discount_evidentiat,text_aditional,facturat,data_facturat,
explicatie,altele,client,cod,id_util,utilizator,utils,utilfact,delegat,bidelegat,cnpdelegat,nume_agent,nrinmat,total_fara_tva,total_tva,total_cu_tva,id_gestiune,serie_chit,nr_incasare,incasat,
in_valuta,sucursala,id_ruta,ruta,curs,multiplicator,nume_val,valuta,id_valuta,valval,tvaval,totval,id_delegat,id_agent,id_masina,valoarea,
nume_gestiune,id_sectie,sectie,id_lucrare,lucrare,tip_incasare,afisare_scadenta,id_ordl,tva_incasare,listare_detaliata,
id_facturare,adresa_facturare,email_facturare,adresa_client,email_client,adresa_beneficiar,email_beneficiar,id_beneficiar, beneficiar, tip_factura, ctip_factura, id_ctr, contract,eproforma,
tip_saft,tip_saftname,id_livrare,adresa_livrare,denumire_adresa_livrare,email_livrare,institutie_publica,efactura,cod_fiscal,id_fact
from fact_vfacturi2
	ENDTEXT
			
	lcorder = [data_act,numar_act]
	lcData = Alltrim(Str(gnAn)) + Padl(Alltrim(Str(gnLuna)), 2, [0]) + [01]
	lcFiltru = IIF(!m.tlShowRecords, [1=2], [data_act between to_date('] + lcData + [','YYYYMMDD') and last_day(to_date('] + lcData + [','YYYYMMDD')) AND STERS = 0])
	llAfiseaza = .F.
	lcGroup = []
	llModParam = .T.
	lcFiltruOriginal = []
	gencursor('poFacturi', 'crsfacturi', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza, lcGroup, llModParam, lcFiltruOriginal)

	poFacturi.ca_baza1.afisare()


TEXT TO lcSchema NOSHOW
	id_vanzare n(20),id_vanzare_det n(10),lot c(20), serie c(100), pret n(20,6), pret_val n(20,6),sters n(1),dataoras d, proc_tvav n(10,4), cantitate n(10,4), discount_unitar n(20,4), discount_unitar_val n(20,4), cont c(4), id_valuta n(10), id_gestiune n(20), pret_achizitie n(20,6), pret_cu_tva n(1), denumire c(250), explicatie M, um c(10), nume_lista_preturi c(100), nume_gestiune c(50), utils c(30), curs n(20,4), multiplicator n(10), nume_val c(10), tip_valuta n(1), id_vanzare_set n(10), codmatf c(50), codmat c(50), codmatc c(50), codbare c(50), id_subgrupa n(5), subgrupa c(100), client c(70), id_jtva_coloana n(10), jtva_coloana C(100), id_jtva_coloana_ex n(10), jtva_coloana_ex C(100), id_ctr n(10), numar_contract c(50),taxcode N(6),taxname C(250)
ENDTEXT 

	TEXT TO lcSelect NOSHOW
select id_vanzare, id_vanzare_det, lot, serie, pret, pret_val, sters, dataoras, 
proc_tvav, cantitate, discount_unitar, discount_unitar_val, cont, 
id_valuta, id_gestiune, pret_achizitie, pret_cu_tva, denumire, explicatie, 
um, nume_lista_preturi, nume_gestiune, utils, curs, multiplicator, 
nume_val, tip_valuta, id_vanzare_set, codmatf, codmat, codmatc, codbare, id_subgrupa, subgrupa, client, id_jtva_coloana, jtva_coloana, id_jtva_coloana_ex, jtva_coloana_ex, id_ctr, numar_contract,taxcode,taxname
from fact_vfacturi_detalii
	ENDTEXT 
	lcOrder=[id_vanzare_det]
	lcfiltru = [1 = 2]
	llAfiseaza = .F.
	lcGroup = []
	llModParam = .T.
	lcFiltruOriginal = []
	gencursor('poDetalii','crsDetalii', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza, lcGroup, llModParam, lcFiltruOriginal)
	poDetalii.ca_baza1.afisare()

	SELECT crsfacturi
	loFrmFacturi = Createobject("frm_facturi")
	loFrmFacturi.Show(1)

	USE IN (SELECT('crsFacturi'))
	USE IN (SELECT('crsDetalii'))
	Release loFrmFacturi, poFacturi, poDetalii

ENDPROC && vizualizare_facturi2
************************************ SFARSIT: vizualizare_facturi2 ****************************************

*********************************** INCEPUT: vizualizare_proforme ****************************************
Procedure vizualizare_proforme
	Private poFacturi, poDetaliiFactura
	Local llAfiseaza,loFrmFacturi
	Store .F. To llAfiseaza
	Store '' To poFacturi, poDetalii
	If Used('crsfacturi')
		Use In crsfacturi
	Endif
	If Used('crsDetalii')
		Use In crsDetalii
	Endif

	pcschema1=['id_proforma n(20),id_comanda n(20),dataora t,data_act d,sters n(1),dataoras t,numar_act n(14),'+]+;
		['tip n(5),id_part n(10),discount n(17,4),discount_evidentiat n(1),text_aditional m,'+]+;
		['explicatie c(240),altele c(200),client c(100), utilizator c(30),utils c(30),'+]+;
		['total_fara_tva n(20,4),total_tva n(20,4),total_cu_tva n(20,4),in_valuta n(1),sucursala c(100),nume_val c(50),curs n(20,gnPCurs),multiplicator n(10),'+] +;
		['id_facturare n(10), adresa_facturare c(250), listare_detaliata n(1)']
		
	pcselect1=['select id_proforma, id_comanda, dataora, data_act, sters, dataoras, numar_act, tip, id_part, discount,' + ] + ;
		['discount_evidentiat, text_aditional, explicatie, altele, client, utilizator, utils, total_fara_tva, total_tva,' + ] + ;
		['total_cu_tva,in_valuta,sucursala,nume_val,curs,multiplicator, CAST(null as Number(10)) as id_facturare, CAST(null as varchar2(100)) as adresa_facturare, 0 as listare_detaliata from fact_vproforme where ']
	pcorder1=[data_act]
	pcfiltru1 = [1=2]
	llAfiseaza = .F.
	gencursor('poFacturi','crsfacturi',pcselect1,pcfiltru1,pcschema1,pcorder1,llAfiseaza)
	poFacturi.ca_baza1.afisare()

	pcschema1=['']
	pcselect1=['select a.id_proforma_det as id_vanzare_det,a.id_proforma_set as id_vanzare_set,a.* from ] + gcS + [.fact_vproforme_detalii a where 2=2']
	pcorder1=[a.id_proforma_det]
	pcfiltru1 = [1 = 2]
	llAfiseaza = .F.
	gencursor('poDetalii','crsDetalii',pcselect1,pcfiltru1,pcschema1,pcorder1,llAfiseaza)
	poDetalii.ca_baza1.afisare()

	loFrmFacturi = Createobject("frm_proforme")
	loFrmFacturi.Show(1)

	Release loFrmFacturi, poFacturi, poDetalii

Endproc
*********************************** SFARSIT: vizualizare_proforme ****************************************


************************************ INCEPUT: vizualizare_borderou_facturi ******************************************
*** vizualizare borderou facturi de trimis la clienti pe email
Procedure vizualizare_borderou_facturi
	
	Private poBorderou, poFacturi
	Local llAfiseaza,loFrmBorderou, lcData
	Store .F. To llAfiseaza
	Store '' To poBorderou, poFacturi
	Use In (SELECT('crsBorderou'))
	USE IN (SELECT('crsFacturiBorderou'))

	TEXT TO lcSchema NOSHOW 
ales n(1), id_borderou I, data_borderou T, id_client N(10), client C(200), email_client c(100), trimis N(1), data_trimis T, confirmat N(1), data_confirmat T
	ENDTEXT 

	TEXT TO lcSelect NOSHOW
select 0 as ales,id_borderou,data_borderou,id_client,client,email_client,trimis,data_trimis,confirmat,data_confirmat FROM fact_vborderou
	ENDTEXT 	
	lcorder = [data_borderou desc,id_borderou desc]
	lcData = Alltrim(Str(gnAn)) + Padl(Alltrim(Str(gnLuna)), 2, [0]) + [01]
	lcFiltru = [TRUNC(data_borderou) between to_date('] + lcData + [','YYYYMMDD') and SYSDATE]
	llAfiseaza = .F.
	lcGroup = []
	llModParam = .T.
	lcFiltruOriginal = []

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


	TEXT TO lcSchema NOSHOW
id_borderou n(20), data_borderou T, id_client N(10), client C(200), id_vanzare N(20), data_act D, numar_act n(14),serie_act c(10),ctip_factura C(100), total_fara_tva N(20,4), total_cu_tva N(20,4), valval N(20,4), totval N(20,4), valuta C(10), in_valuta N(1), trimis N(1), data_trimis T, confirmat N(1), data_confirmat T
	ENDTEXT 

	TEXT TO lcSelect NOSHOW
select id_borderou, data_borderou, id_client, client, id_vanzare, data_act, numar_act, serie_act, ctip_factura, total_fara_tva, total_cu_tva, valval, totval, valuta, in_valuta, trimis,data_trimis,confirmat,data_confirmat FROM fact_vborderou_detalii
	ENDTEXT 	
	lcorder = [data_borderou desc, id_borderou desc, data_act, serie_act, numar_act]
	lcFiltru = [TRUNC(data_borderou) between to_date('] + lcData + [','YYYYMMDD') and SYSDATE]
	llAfiseaza = .F.
	lcGroup = []
	llModParam = .T.
	lcFiltruOriginal = []
	gencursor('poFacturi', 'crsFacturiBorderou', lcSelect, lcFiltru, lcSchema, lcOrder, llAfiseaza, lcGroup, llModParam, lcFiltruOriginal)
	poFacturi.ca_baza1.afisare()

	SELECT crsBorderou
	DO FORM frm_borderou_facturi
*!*		loFrmBorderou = Createobject("frm_borderou_facturi")
*!*		loFrmBorderou.Show(1)

	Release loFrmBorderou, poBorderou, poFacturi

ENDPROC && vizualizare_borderou_facturi
************************************ SFARSIT: vizualizare_borderou_facturi ****************************************

*---------------------------------------------------------------------------
* Genereaza borderouri clienti pentru facturile selectate
* Se apeleaza din frm_facturi (ofacturare.vcx)
* Intoarce lista cu id-uri de borderouri generate, pentru listare
Procedure generare_borderou_facturi
	Local lcClient, lcIds, lcRecc, lcRecno, lcSelect, lcSqlB, lcSqlBd, llSucces, llSucces2, lnRecSel
	PRIVATE pnIdBorderou, pnIdPart
	pnIdBorderou = 0
	pnIdPart = 0
	lcSelect = Select()
	lcIds = ''

	lcSqlB = [begin pack_fact_borderou.BorderouNou(?pnIdPart, ?gnIdUtil, ?@pnIdBorderou); end;]
	*lcSqlBd = [begin pack_fact_borderou.BorderouAdaugaFactura(?pnIdBorderou,?pnIdVanzare); end;]

	Select crsfacturi
	lnRecSel = Recno()
	Locate For ales = 1
	If !Found()
		amessagebox('Nu ati selectat nici un document pentru borderou!', 0 + 48, 'Atentie')
	Else
		Private pcDataora
		pcDataora = get_ora(2)

		* Facturile selectate
		Select client, id_part, id_vanzare ;
			FROM crsfacturi ;
			WHERE ales = 1 ;
			ORDER By 1, 2, 3 ;
			INTO Cursor crsdocgrup

		* Creez borderouri pentru fiecare client
		* adaug in borderou inregistrari distincte pentru fiecare client si in borderou detalii facturile aferente
		llSucces = .T.
		Select Distinct id_part, client From crsDocGrup Group By id_part, client ORDER BY client Into Cursor crsDocClienti
	
		llSucces = goConn.BeginManualTransaction()
		IF m.llSucces
			Select crsDocClienti
			lcRecc = ALLTRIM(STR(RECCOUNT()))
			SCAN
				lcRecno = ALLTRIM(STR(RECNO()))
				pnIdPart = id_part
				pnIdBorderou = 0
				lcClient = ALLTRIM(client)
				WAIT WINDOW 'Generare borderou ' + m.lcClient + ' ' + lcRecno + '/' + m.lcRecc NOWAIT 
				llSucces = goExecutor.oExecuta(m.lcSqlB)
				If m.llSucces
					* Adaugare facturi pe borderou
					lcSqlBd = [begin ]
					Select crsDocGrup
					Scan For id_part = m.pnIdPart
						lcSqlBd = lcSqlBd + [pack_fact_borderou.BorderouAdaugaFactura(] + ALLTRIM(STR(m.pnIdBorderou)) + [,] + ALLTRIM(STR(crsDocGrup.id_vanzare)) + [);]	
					Endscan && crsDocGrup
					lcSqlBd = lcSqlBd + [ end;]
					llSucces = goExecutor.oExecuta(m.lcSqlBd)					
					If !m.llSucces
						Exit
					ELSE 
						* Lista id-uri borderouri pentru listare si salvare pdf borderouri
						lcIds = m.lcIds + ',' + ALLTRIM(STR(m.pnIdBorderou))
					ENDIF 	
				Else
					Exit && crsDocClienti
				Endif
			Endscan && crsDocClienti
			lcIds = IIF(!EMPTY(m.lcIds), SUBSTR(m.lcIds,2), '')

			llSucces2 = goConn.EndManualTransaction(IIF(m.llSucces, 'COMMIT', 'ROLLBACK'))
			llSucces = m.llSucces and m.llSucces2
		ENDIF && llSucces
	Endif && Found
	Use In (Select('crsDocGrup'))
	Use In (Select('crsDocClienti'))

	USE IN (SELECT('crsBorderouTemp'))

	Select (m.lcSelect)
	IF !m.llSucces
		lcIds = ''
	ENDIF 	
		
	Return m.lcIds
Endproc && generare_borderou_facturi

*---------------------------------------------------------------------------
* Trimite emailuri pe baza de borderou - la fiecare client ataseaza facturile si documentele asociate intr-un singur email
PROCEDURE email_borderou_facturi
	LPARAMETERS tcIds, tcCursorFacturi
	* tcIds: lista id_borderou
	* tcCursorFacturi: optional, pentru citirea ctip_factura suprascris de utilizator (ACN!)
	
	Local laFiles[1], lcBody, lcClient, lcDir, lcEmail, lcEmailClient, lcErrorMessage, lcFacturaFile
	Local lcFile, lcFiles, lcRaportEmail, lcScheletonFile, lcSelect, lcSubject, lcTo, lcWhere
	Local ldDataBoderou, ldDataFactura, llClearErrors, llSucces, lnFile, lnFiles, lnIdBorderou
	Local lnIdVanzare, lnNumarFactura, lnRecno, loDate, loMail, lcSubjectTemplate, lcBodyTemplate

	lcSelect = Select()
	lcRaportEmail = Addbs(gcTempPath) + "email_" + Ttoc(Datetime(), 1) + '.xls'

	* Selectez borderourile direct de pe server, nu din cursor borderouri
	* Sa pot apela procedura si programatic daca este cazul, fara sa existe cursorul de borderouri
	TEXT TO lcSql TEXTMERGE NOSHOW
Select id_borderou, id_vanzare, data_act, numar_act, id_borderou, data_borderou, ctip_factura, id_client, client, email_client, adresa_client
    FROM fact_vborderou_detalii
	WHERE id_borderou in (<<m.tcIds>>)
	ENDTEXT
	lcCursor = SYS(2015)
	llSucces = goExecutor.oExecuta(m.lcSql, m.lcCursor)

	***************
	* Suprascriere ctip_factura special pentru ACN, pentru ca voiau ENERGIE ELECTRICA in loc de CHIRII SI UTILITATI pe borderou
	* si nu am adaugat un tip nou de prestatii ENERGIE ELECTRICA
	***************
	IF !EMPTY(m.tcCursorFacturi) AND USED(m.tcCursorFacturi)
		SELECT (m.tcCursorFacturi)
		SCAN
			lnIdVanzare = id_vanzare
			lcTipFactura = ALLTRIM(NVL(ctip_factura,''))
			UPDATE (m.lcCursor) SET ctip_factura = m.lcTipFactura WHERE id_vanzare = m.lnIdVanzare
		ENDSCAN 
	ENDIF 
			
	Select distinct id_borderou, data_borderou, id_client, client, email_client, .F. As email_succes, Space(250) As email_mesaj ;
		From (m.lcCursor) ;
		ORDER BY id_borderou ;
		Into Cursor cBorderouEmail READWRITE 

	Select id_borderou, id_vanzare, data_act, numar_act, ctip_factura ;
		From (m.lcCursor) ;
		ORDER BY data_act, numar_act ;
		Into Cursor cFacturiEmail Readwrite

	USE IN (SELECT(m.lcCursor))
	
	loMail = getoEmail() && email.prg

	lcSubjectTemplate = loMail.cBorderouSubject 
	lcBodyTemplate = loMail.cBorderouBody 
	llCancel = .F.
	Sele cBorderouEmail
	Scan
		lnIdBorderou = id_borderou
		ldDataBorderou = Ttod(data_borderou)
		lcClient = Upper(Alltrim(client))
		lnIdClient = id_client
		lcEmailClient = Alltrim(NVL(email_client,''))
		loMail.cTo = m.lcEmailClient
		llSucces = .T.							
		
		Wait Window 'Email ' + Alltrim(Transform(Recno('cBorderouEmail'))) + '/' + Alltrim(Transform(Reccount('cBorderouEmail'))) + ' Client: ' + m.lcClient Nowait
		
		lcSubject = m.lcSubjectTemplate && Borderou facturi nr. <nr> din <data> - <client>'
		lcSubject = Strtran(m.lcSubject, '<nr>', Alltrim(Str(m.lnIdBorderou)), 1, 1, 1)
		lcSubject = Strtran(m.lcSubject, '<data>', Alltrim(Dtoc(m.ldDataBorderou)), 1, 1, 1)
		lcSubject = Strtran(m.lcSubject, '<client>', m.lcClient, 1, 1, 1)
		loMail.cSubject = m.lcSubject

		lcBody = m.lcBodyTemplate && Aveti atasat borderoul de facturi nr. <nr> din <data> si documentele asociate pentru serviciile prestate de <<ALLTRIM(goFirma.firma)>>.
		lcBody = Strtran(m.lcBody, '<nr>', Alltrim(Str(m.lnIdBorderou)), 1, 10, 1)
		lcBody = Strtran(m.lcBody, '<data>', Alltrim(Dtoc(m.ldDataBorderou)), 1, 10, 1)
		lcBody = Strtran(m.lcBody, '<client>', m.lcClient, 1, 10, 1)
		loMail.cHTMLBody = m.lcBody

		* Atasez borderoul si toate fisierele pdf care au aceeasi denumire ca aaaallzz_nrfact_[factura].pdf
		lcFiles = ""
		lcFile = GetPDFBorderouFile(m.ldDataBorderou, m.lnIdBorderou) && d:\roa\pdf\20200401_29_borderou.pdf
		* Generez borderoul in caz ca nu era listat si salvat pdf
		IF !FILE(m.lcFile)
			llListare  = .F.
			llPdf = .T.
			rap_borderou_clienti(m.lnIdBorderou, m.llPdf, m.llListare) && oproceduri_rapoarte_fact.prg		
		ENDIF 
		IF !FILE(m.lcFile)
			lnRaspuns = MESSAGEBOX('Email ' + Alltrim(Transform(Recno('cBorderouEmail'))) + '/' + Alltrim(Transform(Reccount('cBorderouEmail'))) + ' Client: ' + m.lcClient + CHR(13) + CHR(10) + ;
				'Nu exista borderoul ' + m.lcFile + '!' + CHR(13) + CHR(10) + ;
				'Continuati fara borderou?', 3+32, _Screen.Caption)			
			DO CASE 
				CASE m.lnRaspuns = 6 && YES
					*
				CASE m.lnRaspuns = 7 && NO
					llSucces = .F.
				CASE m.lnRaspuns = 2 && CANCEL
					llSucces = .F.
					llCancel = .T.
					EXIT 									
			ENDCASE 				
		ENDIF 
		IF m.llCancel 
			EXIT 
		ENDIF 
		
		loMail.EmailSetAttachment(m.lcFile)
		lcFiles = m.lcFiles + Iif(File(m.lcFile), m.lcFile, "")

		Select cFacturiEmail
		Scan For id_borderou = m.lnIdBorderou
			lnIdVanzare = id_vanzare
			ldDataFactura = data_act
			lnNumarFactura = numar_act
			loDate = Null
			lcFacturaFile = GetPDFFacturaFile(m.ldDataFactura, m.lnNumarFactura) && d:\roa\pdf\20200402_12346_factura.pdf
			IF !FILE(m.lcFacturaFile)
				lnRaspuns = MESSAGEBOX('Email ' + Alltrim(Transform(Recno('cBorderouEmail'))) + '/' + Alltrim(Transform(Reccount('cBorderouEmail'))) + ' Client: ' + m.lcClient + CHR(13) + CHR(10) + ;
					'Nu exista factura ' + m.lcFacturaFile + '!' + CHR(13) + CHR(10) + ;
					'Continuati fara factura?', 3+32, _Screen.Caption)
				DO CASE 
					CASE m.lnRaspuns = 6 && YES
						*
					CASE m.lnRaspuns = 7 && NO
						llSucces = .F.
						EXIT 			
					CASE m.lnRaspuns = 2 && CANCEL
						llSucces = .F.
						llCancel = .T.
						EXIT 									
				ENDCASE 
			ENDIF 	
			lcScheletonFile = Strtran(lcFacturaFile, 'factura', '*', 1, 10, 1) && d:\roa\pdf\20200402_12346_*.pdf (_recapitulatia.pdf, _situatie_stationare.pdf, _declaratie_comandant.pdf)
			lcDir = Addbs(Justpath(m.lcFacturaFile))
			lnFiles = Adir(laFiles, m.lcScheletonFile)
			For lnFile = 1 To m.lnFiles
				lcFile = m.lcDir + laFiles[m.lnFile, 1]
				loMail.EmailSetAttachment(m.lcFile)
				lcFiles = m.lcFiles + Iif(File(m.lcFile), m.lcFile, "")
			Endfor
		Endscan && cFacturiEmail

		IF m.llCancel
			EXIT
		ENDIF 
		
		* Email
		IF m.llSucces
			lcErrorMessage = ''
			If loMail.Send() > 0
				llClearErrors = .T.
				lcErrorMessage = loMail.GetErrorMessage(m.llClearErrors)
				llSucces = .F.
			Endif

			Replace email_succes With m.llSucces, email_mesaj With Left(m.lcErrorMessage, 250) In cBorderouEmail
		ENDIF && llSucces	

		If Inkey(0.5) = 27 && 'ESC'
			If AMESSAGEBOX('Doriti sa intrerupeti operatia?', 4 + 32, _Screen.Caption) = 6
				Exit
			Endif
		Endif
	Endscan

	Select cBorderouEmail
	Copy To (m.lcRaportEmail) Type Xl5
	OPEN_DEFAULT_APP(m.lcRaportEmail)

	Use In (Select('cBorderouEmail'))
	Use In (Select('cFacturiEmail'))

	Select (m.lcSelect)
ENDPROC && email_borderou_facturi

* ------------------------------------
* Apeleaza email_facturi() cu parametri ListareFactura, ListareRecapitulatie, ListareAlteDocumente
PROCEDURE wrap_email_facturi
	LPARAMETERS toFacturiForm, tcCursorFacturi, tnFactura, tnRecapitulatia, tnAlteDocumente, tnIdVanzare
	* toFacturiForm (optional): referinta la formularul frm_facturi, pentru generare pdf factura, recapitulatie, in caz ca nu exista deja
	* tcCursorFacturi: cursorul cu lista de facturi (id_vanzare, data_act, numar_act, serie_act, total_fara_tva, total_tva, total_cu_tva, client, email_facturare)
	* tnFactura: 0 = nu se ataseaza factura, 1 = se ataseaza factura, 2 = se genereaza factura daca nu exista si se ataseaza
	* tnRecapitulatia: 0 = nu se ataseaza recapitulatia, 1 = se ataseaza recapitulatia, 2 = se genereaza recapitulatia daca nu exista si se ataseaza
	* tnAlteDocumente: 0 = nu se ataseaza alte documente, 1 = se ataseaza alte documente cu aceeasi structura denumire ca factura
	* tnIdVanzare: (optional) vanzari.id_vanzare, daca se apeleaza din afara formularului de vizualizare facturi
	
	LOCAL loDate	
	loDate = CREATEOBJECT("empty")
	ADDPROPERTY(loDate, 'cCursorFacturi', IIF(TYPE('tcCursorFacturi') = 'C' and !EMPTY(m.tcCursorFacturi) and USED(m.tcCursorFacturi), m.tcCursorFacturi, ''))
	ADDPROPERTY(loDate, 'nListareFactura', m.tnFactura)
	ADDPROPERTY(loDate, 'nListareRecapitulatia', m.tnRecapitulatia)
	ADDPROPERTY(loDate, 'nListareAlteDocumente', m.tnAlteDocumente)	
	ADDPROPERTY(loDate, 'nIdVanzare', IIF(TYPE('tnIdVanzare') = 'N', m.tnIdVanzare, 0))
	
	email_facturi(toFacturiForm, loDate)
ENDPROC && wrap_email_facturi

* ------------------------------------
* trimite email factura, recapitulatie si alte documente pdf cu aceeasi structura ca factura.pdf
* In formularul Vizualizare facturi se apasa butonul listare
PROCEDURE email_facturi
	Lparameters toFacturiForm, toDate
	* toFacturiForm (optional): referinta la formularul frm_facturi, pentru generare pdf factura, recapitulatie, in caz ca nu exista deja

	
	*** Trimit email la facturile alese, sau la factura de pe inregistrarea curenta

	Local laFiles[1], lcBody, lcClient, lcDir, lcEmail, lcErrorMessage, lcFacturaFile, lcFile, lcSelect
	Local lcFileName, lcFiles, lcNume, lcRaportEmail, lcRecapitulatiaFile, lcScheletonFile
	Local lcSerieFactura, lcSubject, lcTipDocument, lcTo, lcWhere, ldDataFactura, llClearErrors
	Local llJustPDF, llListareFactura, llListareRecapitulatia, llListareAlteDocumente, llSilent, llSucces, lnFile, lnFiles
	Local lnIdVanzare, lnNumarFactura, lnRecno, loDate, loMail, lcSubjectTemplate, lcBodyTemplate, lnIdVanzare, lcCursorFacturi, llUsed
	lcSelect = SELECT()
	loMail = getoEmail() && email.prg

	lcWhere = ""
	llListareFactura = .T.
	llGenerareFactura = .F.
	llListareRecapitulatia = .F.
	llGenerareRecapitulatia = .F.
	llListareAlteDocumente = .F.
	lnIdVanzare = 0
	lcCursorFacturi = ''
	If Type('toDate') = 'O'
		lcCursorFacturi = IIF(TYPE('toDate.cCursorFacturi') = 'C', toDate.cCursorFacturi, '')
		llListareFactura = IIF(Type('toDate.nListareFactura') = 'N', INLIST(toDate.nListareFactura, 1, 2), .T.)
		llGenerareFactura = IIF(Type('toDate.nListareFactura') = 'N', toDate.nListareFactura = 2, .F.)
		
		llListareRecapitulatia = IIF(Type('toDate.nListareRecapitulatia') = 'N', INLIST(toDate.nListareRecapitulatia, 1, 2), .F.)
		llGenerareRecapitulatia = IIF(Type('toDate.nListareRecapitulatia') = 'N', toDate.nListareRecapitulatia = 2, .F.)
		
		llListareAlteDocumente = IIF(Type('toDate.nListareAlteDocumente') = 'L', toDate.nListareAlteDocumente = 1, .F.)	
		lnIdVanzare = IIF(Type('toDate.nIdVanzare') = 'N', toDate.nIdVanzare, 0)			
	Endif

	lcRaportEmail = Addbs(gcTempPath) + "email_" + Ttoc(Datetime(), 1) + '.xls'

	CREATE CURSOR cEmail (id_vanzare I, data_act D null, numar_act N(14) null, serie_act C(20) null, client C(200) null, ;
		total_fara_tva N(20,4) null, total_tva N(20,4) null, total_cu_tva N(20,4) null, ;
		email_facturare C(200) null, email_succes L, email_mesaj C(250))		

	llUsed = .T.
	IF !USED(m.lcCursorFacturi) AND !EMPTY(m.lnIdVanzare)
		lcCursorFacturi = 'cEmailTemp'	
		lcSql = [SELECT id_vanzare, data_act, numar_act, serie_act, total_fara_tva, total_tva, total_cu_tva, client, email_facturare FROM fact_vfacturi2 WHERE id_vanzare = ] + ALLTRIM(STR(m.lnIdVanzare))
		llSucces = goExecutor.oExecuta(m.lcSql, m.lcCursorFacturi)
		llUsed = .F.
	ENDIF 	
	IF USED(m.lcCursorFacturi)
		SELECT cEmail
		APPEND FROM DBF(m.lcCursorFacturi)
	ENDIF 
	IF !m.llUsed
		USE IN (SELECT(m.lcCursorFacturi))
	ENDIF 

	lcSubjectTemplate = loMail.cSubject
	lcBodyTemplate = loMail.cHtmlBody

	Sele cEmail
	Scan
		lcFiles = ""
		ldDataFactura = data_act
		lcSerieFactura = Alltrim(serie_act)
		lnNumarFactura = numar_act
		lcNume = Upper(Alltrim(NVL(client,'')))
		lcClient = m.lcNume
		lnIdVanzare = id_vanzare
		loDate = Null

		lcSubject = m.lcSubjectTemplate
		lcSubject = Strtran(m.lcSubject, '<nrfact>', Alltrim(Str(m.lnNumarFactura)), 1, 1, 1)
		lcSubject = Strtran(m.lcSubject, '<datafact>', Alltrim(Dtoc(m.ldDataFactura)), 1, 1, 1)
		lcSubject = Strtran(m.lcSubject, '<client>', m.lcClient, 1, 1, 1)
		loMail.cSubject = m.lcSubject

		lcBody = m.lcBodyTemplate
		lcBody = Strtran(m.lcBody, '<nrfact>', Alltrim(Str(m.lnNumarFactura)), 1, 10, 1)
		lcBody = Strtran(m.lcBody, '<datafact>', Alltrim(Dtoc(m.ldDataFactura)), 1, 10, 1)
		lcBody = Strtran(m.lcBody, '<client>', m.lcClient, 1, 10, 1)
		loMail.cHtmlBody = m.lcBody

		* Generare PDF factura
		lcFacturaFile = GetPDFFacturaFile(m.ldDataFactura, m.lnNumarFactura)
		If m.llListareFactura
			If !File(m.lcFacturaFile) AND m.llGenerareFactura AND TYPE('toFacturiForm') = 'O'
				llSilent = .F.
				llJustPDF = .T.
				lcTipDocument = 'FACTURA'
				loDate = toFacturiForm.do_listare_document(m.lnIdVanzare, m.llSilent, m.llJustPDF, m.lcTipDocument) && .ListareDocument(m.llSilent, m.llJustPDF, "cEmail")
			ENDIF
			IF FILE(m.lcFacturaFile)
				loMail.EmailSetAttachment(m.lcFacturaFile)
				lcFiles = m.lcFiles + Iif(File(m.lcFacturaFile), m.lcFacturaFile, "")
			ENDIF 	
		ENDIF && llListareFactura

		* Generare PDF recapitulatie
		lcRecapitulatiaFile = GetPDFRecapitulatiaFile(ldDataFactura, m.lnNumarFactura)
		If m.llListareRecapitulatia
			If !File(m.lcRecapitulatiaFile) AND m.llGenerareRecapitulatia AND TYPE('toFacturiForm') = 'O'
				llSilent = .F.
				llJustPDF = .T.
				lcTipDocument = 'RECAPITULATIE'
				loDate = toFacturiForm.do_listare_document(m.lnIdVanzare, m.llSilent, m.llJustPDF, m.lcTipDocument) && .ListareDocument(m.llSilent, m.llJustPDF, "cEmail")
			ENDIF
			IF FILE(m.lcRecapitulatiaFile)
				loMail.EmailSetAttachment(m.lcRecapitulatiaFile)
				lcFiles = m.lcFiles + Iif(File(m.lcRecapitulatiaFile), "," + m.lcRecapitulatiaFile, "")
			ENDIF 	
		ENDIF && llListareRecapitulatia

		* Atasez alte documente cu aceeasi structura ca factura.pdf
		IF m.llListareAlteDocumente 
			lcScheletonFile = Strtran(lcFacturaFile, 'factura', '*', 1, 10, 1) && d:\roa\pdf\20200402_12346_*.pdf (_recapitulatia.pdf, _situatie_stationare.pdf, _declaratie_comandant.pdf)
			lcDir = Addbs(Justpath(m.lcFacturaFile))
			lnFiles = Adir(laFiles, m.lcScheletonFile)
			For lnFile = 1 To m.lnFiles
				lcFileName = laFiles[m.lnFile, 1]
				* Nu mai atasez factura.pdf si recapitulatie.pdf
				If Inlist(Lower(m.lcFileName), Lower(JUSTFNAME(m.lcFacturaFile)), Lower(JUSTFNAME(m.lcRecapitulatiaFile)))
					Loop
				Endif
				lcFile = m.lcDir + m.lcFileName
				loMail.EmailSetAttachment(m.lcFile)
				lcFiles = m.lcFiles + Iif(File(m.lcFile), m.lcFile, "")
			ENDFOR
		ENDIF && llListareAlteDocumente 	

		* Email
		Wait Window 'Email ' + Alltrim(Transform(Recno('cEmail'))) + '/' + Alltrim(Transform(Reccount('cEmail'))) + ' Client: ' + m.lcClient + ' Factura: ' + Alltrim(Str(m.lnNumarFactura)) Nowait
		lcEmail = Alltrim(Nvl(cEmail.email_facturare, '')) && am adaugat direct in crsFacturi.email_facturare, ca sa nu mai folosesc loDate.oClient.email_facturare, daca fisierele pdf sunt deja listate
		lcTo = Iif(!Empty(Nvl(m.lcEmail, '')), m.lcEmail, '')
		loMail.cTo = m.lcTo

		llSucces = .T.
		lcErrorMessage = ''
		If loMail.Send() > 0
			llClearErrors = .T.
			lcErrorMessage = loMail.GetErrorMessage(m.llClearErrors)
			llSucces = .F.
		Endif
		Replace email_succes With m.llSucces, email_mesaj With Left(m.lcErrorMessage, 250) In cEmail

		If Inkey(0.5) = 27 && 'ESC'
			If AMESSAGEBOX('Doriti sa intrerupeti operatia?', 4 + 32, _Screen.Caption) = 6
				Exit
			Endif
		Endif
	Endscan

	lcMesaj = ''
	SELECT cEmail
	SCAN
		lcMesaj = lcMesaj + 'Factura ' + TRANSFORM(numar_act) + ' din ' + TRANSFORM(data_act) + ' ' + ALLTRIM(NVL(client,'')) + ' : EMAIL ' + ;
			IIF(email_succes, 'SUCCES', 'EROARE: ' + ' ' + ALLTRIM(NVL(email_mesaj,''))) + CHR(13) + CHR(10)
	ENDSCAN 	
	AMESSAGEBOX(m.lcMesaj,0+64,_screen.Caption)
*!*		
*!*		Copy To (m.lcRaportEmail) Type Xl5
*!*		OPEN_DEFAULT_APP(m.lcRaportEmail)

	Use In (Select('cEmail'))

	SELECT(m.lcSelect)

ENDPROC && email_facturi

***********************************************
* Listeaza factura, aviz, proforma, recapitulatie
* Se apeleaza dupa scrierea in baza de date pe baza id_vanzare
* Exista si listarea dupa emiterea facturii, din cursoarele locale
* intoarce poDate pentru folosirea informatiilor despre factura (ex: poDate.email_factura in pluginul de trimitere email cu atasament factura)
PROCEDURE listeaza_formular
	Lparameters lnIdVanzare, tlSilent, tlJustPDF, tcTipDocument, tlEFactura
	* tlSilent: listare fara previzualizare
	* tlJustPDF: listare doar pdf, fara previzualizare/imprimanta - pentru atasament la email
	* tcTipDocument: FACTURA/RECAPITULATIE/EFACTURA
	* tlEfactura: export xml eFactura


	Local lcNumeCursor, lnIdVanzare, lcTipDocument, lnIdFact, lnIdPart, loSoldClient
	Local lcCursor, lcProcedura, lcRaport, lcSql, lnSucces
	LOCAL loEx as Exception 
	*:Global ocomanda
	*lcNumeCursor = tcNumeCursor
	Local lnTip
	Private poDate, poDateGestiuneDest, pnCod, pnid_comanda_aviz, pnIdGestiuneRetur
	Local lnDiscount, lnDiscountVal, lnProcTvav
	lcTipDocument = Iif(Type('tcTipDocument') = 'C' And !Empty(m.tcTipDocument), Upper(Alltrim(m.tcTipDocument)), 'FACTURA') && FACTURA/RECAPITULATIE/EFACTURA


**********************************************************
* Creez cursoarele crsFacturaListare, crsDetaliiListare
**********************************************************
Create Cursor crsFacturaListare (ales N(1) null, id_vanzare N(20) null, id_fact N(20), id_comanda N(20) null, dataora T(8) null, dataora_exp T(8) null, ;
	data_act D(8) null, sters N(1) null, dataoras T(8) null, numar_act N(14) null, serie_act C(10) null, tip N(5) null, ;
	id_part N(10) null, discount N(17,4) null, disc_fara_tva N(17,4) null, discount_evidentiat N(1) null, text_aditional M(4) null, ;
	facturat N(1) null, data_facturat T(8) null, explicatie C(240) null, altele C(200) null, client C(100) null, cod N(20) null, ;
	id_util I(4) null, utilizator C(30) null, utils C(30) null, utilfact C(30) null, delegat C(70) null, bidelegat C(30) null, ;
	cnpdelegat C(15) null, nume_agent C(70) null, nrinmat C(10) null, total_fara_tva N(20,4) null, total_tva N(20,4) null, total_cu_tva N(20,4) null, ;
	id_gestiune N(10) null, serie_chit C(20) null, nr_incasare N(10) null, incasat N(17,4) null, in_valuta N(1) null, sucursala C(100) null, ;
	id_ruta N(10) null, ruta C(100) null, curs N(20,4) null, multiplicator N(10) null, nume_val C(50) null, valuta C(50) null, id_valuta N(5) null, ;
	valval N(20,4) null, tvaval N(20,4) null, totval N(20,4) null, id_delegat N(10) null, id_agent N(10) null, id_masina N(10) null, valoarea N(20,4) null, ;
	nume_gestiune C(50) null, id_sectie N(5) null, sectie C(50) null, id_lucrare N(20) null, lucrare C(200) null, tip_incasare N(10) null, ;
	afisare_scadenta N(1) null, id_ordl N(10) null, tva_incasare N(1) null, listare_detaliata N(1) null, id_facturare N(10) null, adresa_facturare C(250) null, ;
	email_facturare C(100) null, adresa_client C(250) null, email_client C(100) null, adresa_beneficiar C(250) null, email_beneficiar C(100) null, ;
	id_beneficiar N(20) null, beneficiar C(100) null, tip_factura N(5) null, ctip_factura C(100) null, id_ctr N(10) null, contract C(100) null, eproforma N(1) null,tip_saft N(3) null,institutie_publica N(1) null)

Create Cursor crsDetaliiListare (id_vanzare N(20) null, id_vanzare_det N(10) null, id_articol N(10) null, lot c(100) null, serie C(100) null, pret N(20,6) null, pret_val N(20,6) null, sters N(1) null, dataoras D(8) null, ;
proc_tvav N(10,4) null, cantitate N(10,4) null, discount_unitar N(20,6) null, discount_unitar_val N(20,6) null, cont C(4) null, id_valuta N(10) null, id_gestiune N(20) null, ;
pret_achizitie N(20,6) null, pret_cu_tva N(1) null, denumire C(250) null, explicatie M(4) null, um C(10) null, cod_um_iso C(5) null, nume_lista_preturi C(100) null, nume_gestiune C(50) null, ;
utils C(30) null, curs N(20,4) null, multiplicator N(10) null, nume_val C(10) null, tip_valuta N(1) null, id_vanzare_set N(10) null, codmatf C(50) null, codmat C(50) null, ;
codmatc C(50) null, codbare C(50) null, id_subgrupa N(5) null, subgrupa C(100) null, client C(70) null, id_jtva_coloana N(10) null, jtva_coloana C(100) null, ;
id_jtva_coloana_ex N(10) null, jtva_coloana_ex C(100) null, id_ctr N(10) null, numar_contract C(50) null, taxcode N(6) null)


	TEXT TO lcSqlFactura TEXTMERGE NOSHOW
select 0 as ales,id_vanzare,id_fact,id_comanda,dataora,dataora_exp,data_act,sters,dataoras,numar_act,serie_act,tip,id_part,discount,disc_fara_tva,discount_evidentiat,text_aditional,facturat,data_facturat,
	explicatie,altele,client,cod,id_util,utilizator,utils,utilfact,delegat,bidelegat,cnpdelegat,nume_agent,nrinmat,total_fara_tva,total_tva,total_cu_tva,id_gestiune,serie_chit,nr_incasare,incasat,
	in_valuta,sucursala,id_ruta,ruta,curs,multiplicator,nume_val,valuta,id_valuta,valval,tvaval,totval,id_delegat,id_agent,id_masina,valoarea,
	nume_gestiune,id_sectie,sectie,id_lucrare,lucrare,tip_incasare,afisare_scadenta,id_ordl,tva_incasare,listare_detaliata,
	id_facturare,adresa_facturare,email_facturare,adresa_client,email_client,adresa_beneficiar,email_beneficiar,id_beneficiar, beneficiar, tip_factura, ctip_factura, id_ctr, contract,eproforma,tip_saft,institutie_publica
	from fact_vfacturi2 
	where id_vanzare = <<m.lnIdVanzare>>
	ENDTEXT 
		
	TEXT TO lcSqlDetalii TEXTMERGE NOSHOW
select id_vanzare, id_vanzare_det, id_articol, lot, serie, pret, pret_val, sters, dataoras, 
	proc_tvav, cantitate, discount_unitar, discount_unitar_val, cont, 
	id_valuta, id_gestiune, pret_achizitie, pret_cu_tva, denumire, explicatie, 
	um, cod_um_iso, nume_lista_preturi, nume_gestiune, utils, curs, multiplicator, 
	nume_val, tip_valuta, id_vanzare_set, codmatf, codmat, codmatc, codbare, id_subgrupa, subgrupa, client, id_jtva_coloana, jtva_coloana, id_jtva_coloana_ex, jtva_coloana_ex, id_ctr, numar_contract, taxcode 
	from fact_vfacturi_detalii 
	where id_vanzare = <<m.lnIdVanzare>>
	order by id_vanzare_det
	ENDTEXT 
			
	llSucces = goExecutor.oExecuta(m.lcSqlFactura, 'cFacturaTemp')
	IF m.llSucces
		SELECT crsFacturaListare
		APPEND FROM DBF('cFacturaTemp')
		USE IN (SELECT('cFacturaTemp'))
		
		llSucces = goExecutor.oExecuta(m.lcSqlDetalii, 'cDetaliiTemp')
		IF m.llSucces
			SELECT crsDetaliiListare
			APPEND FROM DBF('cDetaliiTemp')
			USE IN (SELECT('cDetaliiTemp'))		
		ENDIF && llSucces	
	ENDIF && llSucces


*******************************************

	Select crsFacturaListare
	lnTip = tip
	pnCod = cod
	pnid_comanda_aviz = 0

	poDate = Createobject("oDateFactura", 0, 0)
	
	Select crsFacturaListare
	lnIdFact = id_fact
	lnIdPart = id_part
	poDate.nid_vanzare = id_vanzare
	poDate.nRelistare = 1
	poDate.tip = lnTip
	poDate.nract = numar_act
	poDate.serie_act = Alltrim(serie_act)
	poDate.dataact = Iif(Type('data_act') = 'T', Ttod(data_act), data_act)
	poDate.discount_evidentiat = discount_evidentiat
	poDate.text_aditional = Nvl(text_aditional, [])
	poDate.text_aditional = Strtran(poDate.text_aditional, CHR(170),Chr(13)+Chr(10),1,100,1)
	poDate.nrinmat = nrinmat
	poDate.nume_delegat = delegat
	poDate.bidelegat = bidelegat
	poDate.cnpdelegat = cnpdelegat
	poDate.dataora_exp = dataora_exp
	*!*	poDate.listaid = Iif(poDate.tip = 2,Alltrim(Str(id_ctr)),Iif(Inlist(poDate.tip, 3, 21, 28, 42),Alltrim(Str(id_comanda)), [0]))
	poDate.listaid = Iif(Inlist(poDate.tip, 3, 21, 28, 42, 47), Alltrim(Str(id_comanda)), []) && id_comanda
	poDate.ntip_incasare = tip_incasare			&&	modificare v 2.0.84
	poDate.nr_incasare = nr_incasare
	poDate.serie_chit = Nvl(serie_chit, [])		&&	modificare v 2.0.80
	poDate.incasat = incasat
	poDate.in_valuta = in_valuta
	poDate.afisare_scadenta = afisare_scadenta	&&	modificare	ROACONTRACTE v 2.0.38
	poDate.descriere = altele			&&	modificare	v 2.0.44 nr comanda/aviz
	poDate.id_ordl = id_ordl			&&	modificare	v 2.0.98
	poDate.tva_incasare = tva_incasare	&&	modificare	v 2.2.0
	poDate.id_facturare = id_facturare
	poDate.nListareDetaliata = listare_detaliata

	poDate.nListareSilentioasa = Iif(m.tlSilent, 1, 0)
	poDate.nListareJustPDF = Iif(m.tlJustPDF, 1, 0)
	
	poDate.nIdBeneficiar = id_beneficiar
	poDate.nTipFactura = tip_factura
	poDate.id_util = id_util
	poDate.eProforma = eproforma
	poDate.nEFactura = IIF(m.tlEFactura, 1, 0) && xml EFactura
	poDate.id_ctr = id_ctr
	poDate.contract = ALLTRIM(NVL(contract,''))

	
	If poDate.in_valuta = 1
		*!*	modificare	v 2.0.56
		poDate.nume_valuta = nume_val
		poDate.Curs = Curs
		poDate.multiplicator = multiplicator
		poDate.totftvaconv		=	total_fara_tva
		poDate.tottvaconv		=	total_tva
		poDate.totctvaconv		=	poDate.totftvaconv + poDate.tottvaconv
		lnDiscountVal = discount
		lnDiscount = Round(lnDiscountVal * poDate.Curs / poDate.multiplicator, gnPc)
	Else
		lnDiscount = discount
		lnDiscountVal = 0
	ENDIF
	poDate.nValVal = valval
	poDate.nTvaVal = tvaval
	poDate.nTotVal = totval
	poDate.cValuta = ALLTRIM(nume_val)
	poDate.Curs = Curs
	poDate.multiplicator = multiplicator	
	poDate.tip_saft = NVL(tip_saft, 380) && factura initiala
	poDate.institutie_publica = NVL(institutie_publica, 0)

	Do Case
		Case m.lcTipDocument = 'FACTURA'
			If !Pemstatus(poDate, "id_comanda_aviz", 5)
				AddProperty(poDate, "id_comanda_aviz", 0)
			Endif
			If Inlist(poDate.tip, 3, 21, 28, 42, 47) && factura / aviz din comanda	&&	modificare v 2.0.93 : am adaugat 47
				Select crsFacturaListare
				poDate.id_comanda_aviz = id_comanda
				If Inlist(poDate.tip, 21, 28)
					lcSql = [select nr_fact from fact_vdetalii_fact where id_vanzare_aviz = ] + Alltrim(Str(poDate.nid_vanzare))
					lnSucces = goExecutor.oExecute(lcSql, [crsnrfact])
					If lnSucces < 0
						amessagebox(goExecutor.oPrelucrareEroare(), 16, "Eroare")
					Else
						If Reccount('crsnrfact') > 0
							Select crsnrfact
							AddProperty(poDate, [descriere2], Alltrim(nr_fact))
						Endif
						Use In crsnrfact
					Endif
				Endif
				Select crsFacturaListare
			Else
				If poDate.tip = 4 && factura din aviz
					lcSql = [select max(v.id_comanda) as id_comanda  from vanzari_coresp vc ] + ;
						[join vanzari v on vc.id_vanzare_aviz = v.id_vanzare where vc.sters = 0 and v.sters = 0 and vc.id_vanzare_fact =] + Alltrim(Str(poDate.nid_vanzare))

					lnSucces = goExecutor.oSelect2Value(lcSql, @pnid_comanda_aviz)
					If lnSucces < 0
						amessagebox(goExecutor.oPrelucrareEroare(), 16, "Eroare")
						poDate.id_comanda_aviz = 0
					Else
						poDate.id_comanda_aviz = Nvl(pnid_comanda_aviz, 0)
					Endif
					Select crsFacturaListare
					poDate.descriere = Alltrim(altele)
				Endif
			Endif


			If Inlist(lnTip, 23, 25, 27, 30, 41, -6) && AVIZE SUBUNITATI 23,25,27,30; AVIZE SUBUNITATI RETUR 41, AVIZ RETUR VALORIC -6

				poDateGestiuneDest = Createobject("oDateGestiune")
				poDateGestiuneDest.id_gestiune = id_gestiune

				If Inlist(m.lnTip, 41, -6) && AVIZE SUBUNITATI RETUR (GESTIUNEA DESTINATIE ESTE DEPOZIT, TREBUIE GESTIUNEA SURSA DIN VANZARI_DETALII)
					pnIdGestiuneRetur = 0
					lcSql = [select min(id_gestiune) from vanzari_detalii where id_vanzare = ] + + Alltrim(Str(poDate.nid_vanzare))
					lnSucces = goExecutor.oSelect2Value(lcSql, @pnIdGestiuneRetur)
					If lnSucces < 0
						Messagebox(goExecutor.cEroare, 0 + 16, _Screen.Caption)
					Else
						poDateGestiuneDest.id_gestiune	= m.pnIdGestiuneRetur
						poDate.id_client = m.pnIdGestiuneRetur
					Endif
				Endif

				Select crsDetaliiListare
				poDate.id_client = id_gestiune
				poDate.nume_client = nume_gestiune

				If !Isnull(pnCod)
					lcSql = [select r.id_responsabil, p.denumire as nresp from rul r left join nom_parteneri p on r.id_responsabil = p.id_part where r.cod = ] + Alltrim(Str(m.pnCod)) + [ and rownum = 1]
					*		lcSql = [select /*+ index(rul IDX_RUL_005) */ r.id_responsabil, p.denumire as nresp from rul r left join nom_parteneri p on r.id_responsabil = p.id_part where r.cod = ?pnCod and rownum = 1]
					If Used('crsresp')
						Use In crsresp
					Endif
					lcCursor = [crsresp]
					lnSucces = goExecutor.oExecute(lcSql, lcCursor)
					If lnSucces < 0
						amessagebox(goExecutor.oPrelucrareEroare(), 16, "Eroare")
						Return
					Else
						Select (lcCursor)
						poDate.id_responsabil = id_responsabil
						poDate.responsabil = nresp
						Use In (lcCursor)
					Endif

					If Inlist(lnTip, 23, 25)
						lcSql = [select id_partd from act where cod = ] + Alltrim(Str(m.pnCod)) + [ and scd = '357' ] + ;
							[and id_gestin = ] + Alltrim(Str(poDateGestiuneDest.id_gestiune)) + [ and rownum = 1]
						lnSucces = goExecutor.oExecute(lcSql, lcCursor)
						If lnSucces < 0
							amessagebox(goExecutor.oPrelucrareEroare(), 16, "Eroare")
							Return
						Else
							If Reccount(lcCursor) > 0
								Select (lcCursor)
								poDate.id_part_rez = id_partd
							Endif
							Use In (lcCursor)
						Endif
					Endif
				Endif
			Else
				poDate.id_client = id_part
				poDate.nume_client = client
			Endif
			If !Isnull(pnCod)
				If poDate.tip = -12 And !Isnull(poDate.id_ordl)
					lcSql = [select a.datascad,b.nr_dosar,c1.denumire as asigurator,d.inspector from ] + ;
						[(select MAX(id_lucrare) as id_lucrare,MAX(datascad) as datascad from act where cod = ] + Alltrim(Str(pnCod)) + [) a ] + ;
						[left join dev_ordl b on a.id_lucrare = b.id_lucrare and b.sters = 0 ] + ;
						[left join dev_nom_asiguratori c on b.id_asigurator = c.id_asigurator ] + ;
						[left join nom_parteneri c1 on c.id_part = c1.id_part ] + ;
						[left join dev_nom_inspectori d on b.id_inspector = d.id_inspector]
					lcCursor = [crsdatascad]
					If Used(lcCursor)
						Use In (lcCursor)
					Endif
					lnSucces = goExecutor.oExecute(lcSql, lcCursor)
					If lnSucces < 0
						amessagebox(goExecutor.oPrelucrareEroare(), 16, "Eroare")
						Return
					Else
						Select (lcCursor)
						Scatter Name ocomanda Memo
						poDate.datascad = datascad
						poDate.tip_doc_394 = 'F' && F = factura, M = factura simplificata
						Use In (lcCursor)
					Endif
				Else
					lcSql = [select MAX(a.datascad) as datascad, MAX(f.tip_doc) as tip_doc from act a left join nom_fdoc f on a.id_fdoc = f.id_fdoc where cod = ] + Alltrim(Str(m.pnCod))
					lcCursor = [crsdatascad]
					Use In (SELECT(m.lcCursor))
					llSucces = goExecutor.oExecuta(lcSql, lcCursor)
					If !m.llSucces 
						Return
					Else
						Select (m.lcCursor)
						poDate.datascad = datascad
						poDate.tip_doc_394 = NVL(tip_doc, 'F') && F = factura, M = factura simplificata
						Use In (SELECT(m.lcCursor))
					ENDIF				
				Endif
			Endif

			Use In (Select('crsfactura'))

			* Text TVA de afisat in partea de sus a facturii, daca este o singura explicatie de TVA pe toate liniile din crsDetaliiListare
			ADDPROPERTY(poDate, 'cTextTVA', '')
			SELECT distinct jtva_coloana_ex, jtva_coloana FROM crsDetaliiListare INTO CURSOR cTextTVATemp
			IF 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'))


			* Soldul pana la factura inclusiv
			loSoldClient = GetSoldClient(m.lnIdPart, m.lnIdFact)
			poDate.sold_lei = loSoldClient.sold_lei
			poDate.sold_valuta = loSoldClient.sold_valuta
			
			Select crsDetaliiListare
			Calculate Max(proc_tvav) To lnProcTvav

			creeaza_facturacrs([crsfactura])

			prelucreaza_facturacrs([crsDetaliiListare], [crsfactura], lnProcTvav, lnDiscount, lnDiscountVal)

			creeaza_cursor_seturi(pnCod)

			listeaza_ofacturare() && ofacturare.prg

			Use In (Select('crsfactura'))

			Use In (Select('crsfacturaset'))
		Case m.lcTipDocument = 'RECAPITULATIE'
				Select crsFacturaListare
				poDate.id_client = id_part
				poDate.nume_client = client

				Do Case
					Case poDate.tip = 51 && FACTURA ACN
	*!*						TRY 
							Do Case
								Case poDate.nTipFactura = 0 && TRANZIT
									do listeaza_recapitulatie_tranzit in proceduri_acnpro.prg
								Case poDate.nTipFactura = 1 && CHEIAJ
									do listeaza_recapitulatie_cheiaj in proceduri_acnpro.prg
								Case poDate.nTipFactura = 6 && PENALITATI
									do listeaza_recapitulatie_penalitati in proceduri_acnpro.prg
								Otherwise && CHIRII / APA / ALTELE
									do listeaza_recapitulatie_alte in proceduri_acnpro.prg
							ENDCASE
	*!*						CATCH TO loEx
	*!*							AMESSAGEBOX('Listati recapitulatia din programul "ROAACNPRO"!' + CHR(13) + CHR(10) + loEx.Message,0+48,_SCREEN.Caption)
	*!*						ENDTRY 	
					OTHERWISE 
						 DO listeaza_recapitulatie WITH 'crsDetaliiListare', 'recapitulatie.frx' in ofacturare.prg
						* am facu si o recapitulatie generica, pentru ca am pus recapitulatia in meniul de listare pentru ACN
				ENDCASE 

	Endcase

	Release pnCod, lnTip
	USE IN (SELECT('crsFacturaListare'))
	USE IN (SELECT('crsDetaliiListare'))

	Return poDate && intorc poDate pentru folosirea informatiilor despre factura (ex: poDate.email_factura in pluginul de trimitere email cu atasament factura)

ENDPROC && listeaza_formular

* Intoarce vanzari.id_fact pe baza vanzari.id_vanzare
* Folosesc la GetSoldClient
FUNCTION IdVanzare2IdFact
	LPARAMETERS tnIdVanzare
	PRIVATE pnIdVanzare
	LOCAL lnIdFact
	pnIdVanzare = m.tnIdVanzare
	lnIdFact = 0

	llSucces = goExecutor.oSelecteaza2Value("Select MIN(id_fact) from vanzari where id_vanzare = ?pnIdVanzare", @lnIdFact)
	
	RETURN m.lnIdFact
ENDFUNC 
***********************************
* Obtine sold client 4111 + 461 din balanta_parteneri sau din ireg_parteneri pana la numarul facturii 
* Pentru afisarea in raportul factura
***********************************
FUNCTION GetSoldClient
	LPARAMETERS tnIdPart, tnIdFact
	* tnIdPart: Id partener client
	* tnIdFact : (optional) Id factura 

	PRIVATE pnIdPart, pnIdFact
	Local loSold As "empty"
	Local lcSelect, lcSql, llSucces

	pnIdPart = tnIdPart
	pnIdFact = tnIdFact
	lcSelect = SELECT()
	
	loSold = CREATEOBJECT("empty")
	ADDPROPERTY(loSold, 'sold_lei', 0)
	ADDPROPERTY(loSold, 'sold_valuta', 0)	
	
	lcSql = [{call pack_facturare.getSoldClient(?gnAn, ?gnLuna, ?pnIdPart, ?pnIdFact)}]
	
	If goExecutor.oExecuta(m.lcSql, 'cSoldClientTemp')
		SELECT cSoldClientTemp
		GO TOP 
		loSold.sold_lei = NVL(sold_lei, 0)
		loSold.sold_valuta = NVL(sold_valuta, 0)
	ENDIF 	
	USE IN (SELECT('cSoldClientTemp'))
	
	SELECT (m.lcSelect)
	RETURN loSold
ENDFUNC 

******************************** INCEPUT: verifica_setari *************************************
Procedure verifica_setari
	LPARAMETERS tlSilent
	* tlSilent: .T. nu afiseaza mesajul, .F. (default) afiseaza mesajul
	
	Private pcMesaj
	Store "" To pcMesaj
	lcSql = [begin ] + gcS + [.pack_facturare.verificare_setari_program(?gnIdUtil,?@pcMesaj);] + ;
		[end;]
	lnSucces = goExecutor.oexecute(lcSql)
	If lnSucces < 0
		amessagebox(goExecutor.oPrelucrareEroare(), 16, "Eroare")
	Else
		If !m.tlSilent AND !Isnull(m.pcMesaj) And !Empty(m.pcMesaj)
			amessagebox(pcMesaj, 48, "Atentie")
		Endif
	ENDIF
	RETURN m.pcMesaj
Endproc
******************************** SFARSIT: verifica_setari *************************************
************************************ INCEPUT: vanzare_materii_prime ****************************************
Procedure vanzare_materii_prime
	Local lnIdSet
	gnTipGest = 2
	Do vanzare1.mpr
Endproc
************************************ SFARSIT: vanzare_materii_prime ****************************************
*************************************** INCEPUT: vanzare_produse *******************************************
Procedure vanzare_produse
	gnTipGest = 4
	Do vanzare2.mpr
Endproc
*************************************** SFARSIT: vanzare_produse *******************************************
*********************************** INCEPUT: vanzare_marfa_pret_achi ***************************************
Procedure vanzare_marfa_pret_achi
	gnTipGest = 5
	Do vanzare3.mpr
Endproc
*********************************** SFARSIT: vanzare_marfa_pret_achi ***************************************
*********************************** INCEPUT: vanzare_marfa_pret_vanz ***************************************
Procedure vanzare_marfa_pret_vanz
	gnTipGest = 6
	Do vanzare4.mpr
Endproc
*********************************** SFARSIT: vanzare_marfa_pret_vanz ***************************************
*********************************** INCEPUT: vanzare_marfa_pret_achi_vanz ***************************************
Procedure vanzare_marfa_pret_achi_vanz
	gnTipGest = 7
	Do vanzare5.mpr
Endproc
*********************************** SFARSIT: vanzare_marfa_pret_achi_vanz ***************************************
************************************* INCEPUT: selecteaza_gestiuni *****************************************
Procedure selecteaza_gestiuni
	If Used('nom_gestiuni')
		Use In nom_gestiuni
	Endif
	Do Case
		Case "-1" $ gcGestPermis
			lcSql = [select * from ] + gcS + [.vnom_GESTIUNI where inactiv = 0 ] + ;
				[and nr_pag = ?gnTipGest order by nume_gestiune]
			lcCursor = [nom_gestiuni]
			lnSucces = goExecutor.oexecute(lcSql, lcCursor)
			If lnSucces < 0
				amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
				Quit
			Endif
			goExecutor.oReset()
		Otherwise
			lcSql = [select * from ] + gcS + [.vnom_GESTIUNI where inactiv = 0 and nr_pag = ?gnTipGest ] + ;
				[ and id_gestiune in ] + ;
				[(select g.id_gestiune from ] + gcS + [.vgest_coresp_grupe_gestiuni g join ] + ;
				gcS + [.vgest_coresp_util_grupe u on g.id_grupe = u.id_grupe and u.id_util = ?gnIdUtil) order by nume_gestiune]

			lcCursor = [nom_gestiuni]
			lnSucces = goExecutor.oexecute(lcSql, lcCursor)
			If lnSucces < 0
				amessagebox(goExecutor.cEroare, 0 + 16, 'Eroare')
				Quit
			Endif
			goExecutor.oReset()
	Endcase
Endproc	&&	selecteaza_gestiuni
************************************* SFARSIT: selecteaza_gestiuni *****************************************
************************************* INCEPUT: creeaza_cursor_seturi *****************************************
Procedure creeaza_cursor_seturi
	Lparameters tnCod
	*!*	Dimension laStructura(1,18)
	lcCursorTemp = [crsfacturasettemp]
	lcCursor = [crsfacturaset]
	If Used(lcCursorTemp)
		Use In (lcCursorTemp)
	Endif
	If Used(lcCursor)
		Use In (lcCursor)
	Endif
	*!*	modificare	v 2.0.84
	*!*	lcSql = [select c.id_vanzare_set as id_set_fact,Min(b.id_vanzare_det) as id_temp,c.denumire,c.explicatie,c.cantitate,c.um,c.serie,c.pret,Nvl(c.discount_unitar,0) as discount_unitar,c.pret_cu_tva ] + ;
	*!*		[from ] + gcS + [.vanzari a ] + ;
	*!*		[left join ] + gcS + [.vanzari_detalii b on a.id_vanzare = b.id_vanzare and b.sters = 0 ] + ;
	*!*		[left join ] + gcS + [.vanzari_seturi c on b.id_vanzare_set = c.id_vanzare_set ] + ;
	*!*		[where a.cod = ] + Alltrim(Str(tnCod)) + [ and b.id_vanzare_set is not null ] + ;
	*!*		[group by c.id_vanzare_set,c.denumire,c.explicatie,c.cantitate,c.um,c.serie,c.pret,Nvl(c.discount_unitar,0),c.pret_cu_tva]
	lcSql = [{call pack_facturare.citeste_vanzari_seturi(] + Alltrim(Str(tnCod)) + [)}]
	*!*	modificare	v 2.0.84	^
	lnSucces = goExecutor.oexecute(lcSql, lcCursorTemp)
	Do Case
		Case lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
			Return
		Case Reccount(lcCursorTemp) = 0
			Use In (lcCursorTemp)
		Otherwise
			*!*	modificare	v 2.0.56
			*!*		Afields(laStructura,[crsfactura])
			*!*		Create Cursor (lcCursor) From Array laStructura
			copiaza_structura_cursor([crsfactura], lcCursor)
			*!*	modificare	v 2.0.56	^
			*!*	*!*		Insert Into (lcCursor) ;
			*!*	*!*			Select a.id_set_fact,a.denumire,a.explicatie,a.cantitate,a.um,a.serie,;
			*!*	*!*			Max(b.proc_tvav) As proc_tvav,;
			*!*	*!*			Sum(b.valftva) As valftva,Sum(b.valtva) As valtva,;
			*!*	*!*			Sum(b.valdiscountftva) As valdiscountftva,Sum(b.valdiscounttva) As valdiscounttva,;
			*!*	*!*			Sum(b.valdiminuatftva) As valdiminuatftva,;
			*!*	*!*			Sum(b.valdiminuattva) As valdiminuattva,Sum(b.valdiminuatctva) As valdiminuatctva,;
			*!*	*!*			Sum(b.vvalftva) As vvalftva,Sum(b.vvaltva) As vvaltva,Sum(b.vvalctva) As vvalctva,;
			*!*	*!*			Sum(b.vdiscountftva) As vdiscountftva,Sum(b.vdiscountctva) As vdiscountctva,;
			*!*	*!*			Sum(b.vvaldiscountftva) As vvaldiscountftva,Sum(b.vvaldiscounttva) As vvaldiscounttva,;
			*!*	*!*			Sum(b.vvaldiscountctva) As vvaldiscountctva,Sum(b.vvaldiminuatftva) As vvaldiminuatftva,;
			*!*	*!*			Sum(b.vvaldiminuattva) As vvaldiminuattva,Sum(b.vvaldiminuatctva) As vvaldiminuatctva,;
			*!*	*!*			Round(Sum(b.valftva)/a.cantitate,gnPc) As pretftva,Round(Sum(b.valtva)/a.cantitate,gnPc) As tva,;
			*!*	*!*			Round(Sum(b.valdiscountftva)/a.cantitate,gnPc) As discountftva,;
			*!*	*!*			Round(Sum(b.valdiscounttva)/a.cantitate,gnPc) As discounttva,;
			*!*	*!*			Round(Sum(b.vvalftva)/a.cantitate,gnPc) As vpretftva,;
			*!*	*!*			ROUND(Sum(b.vvaltva)/a.cantitate,gnPc) As vtva,;
			*!*	*!*			Round(Sum(b.vvalctva) / a.cantitate,gnPc) As vpretctva,;
			*!*	*!*			ROUND(Sum(b.vvaldiscountftva) / a.cantitate,gnPc) As vdiscountftva,;
			*!*	*!*			Round(Sum(b.vvaldiscountctva) / a.cantitate,gnPc) As vdiscountctva,;
			*!*	*!*			ROUND(Sum(b.vvaldiscountftva) / a.cantitate,gnPc) - Round(Sum(b.vvaldiscountctva) / a.cantitate,gnPc) As vdiscounttva ;
			*!*	*!*			FROM (lcCursorTemp) a Left Join crsfactura b On a.id_set_fact = b.id_set_fact ;
			*!*	*!*			Group By a.id_set_fact,a.denumire,a.explicatie,a.cantitate,a.um,a.serie

			*!*			Insert Into (lcCursor) (id_set_fact,denumire,explicatie,cantitate,um,serie,;
			*!*				proc_tvav,pretftva,valftva,valtva,discountftva,valdiscountftva,;
			*!*				valdiscounttva,valdiminuatftva,valdiminuattva,vpretftva,;
			*!*				vvalftva,vvaltva,vdiscountftva,vvaldiscountftva,vvaldiscounttva,;
			*!*				vvaldiminuatftva,vvaldiminuattva) ;
			*!*				Select a.id_set_fact,a.denumire,a.explicatie,a.cantitate,a.um,a.serie,;
			*!*				b.proc_tvav,;
			*!*				Round(b.valftva/a.cantitate,gnPc) As pretftva,;
			*!*				b.valftva,b.valtva,;
			*!*				Round(b.valdiscountftva/a.cantitate,gnPc) As discountftva,;
			*!*				b.valdiscountftva,b.valdiscounttva,b.valdiminuatftva,b.valdiminuattva,;
			*!*				Round(b.vvalftva/a.cantitate,gnPc) As vpretftva,b.vvalftva,b.vvaltva,;
			*!*				b.vdiscountftva,b.vvaldiscountftva,b.vvaldiscounttva,b.vvaldiminuatftva,;
			*!*				b.vvaldiminuattva ;
			*!*				From (lcCursorTemp) a ;
			*!*				Left Join (Select Max(proc_tvav) As proc_tvav,;
			*!*				Sum(valftva) As valftva,Sum(valtva) As valtva,;
			*!*				Sum(valdiscountftva) As valdiscountftva,Sum(valdiscounttva) As valdiscounttva,;
			*!*				Sum(valdiminuatftva) As valdiminuatftva,;
			*!*				Sum(valdiminuattva) As valdiminuattva,;
			*!*				Sum(vvalftva) As vvalftva,Sum(vvaltva) As vvaltva,;
			*!*				Sum(vdiscountftva) As vdiscountftva,;
			*!*				Sum(vvaldiscountftva) As vvaldiscountftva,Sum(vvaldiscounttva) As vvaldiscounttva,;
			*!*				Sum(vvaldiminuatftva) As vvaldiminuatftva,;
			*!*				Sum(vvaldiminuattva) As vvaldiminuattva,id_set_fact From crsfactura Where id_set_fact <> 0 ;
			*!*				GROUP By id_set_fact) b On a.id_set_fact = b.id_set_fact
			*!*	modificare	v 2.0.56 : am discount_unitar pe articolul din set
			*!*			Insert Into (lcCursor) (id_set_fact,id_temp,denumire,explicatie,cantitate,um,serie,;
			*!*				proc_tvav,id_jtva_coloana,gestionabil,pretftva,valftva,valtva,discountftva,valdiscountftva,;
			*!*				valdiscounttva,valdiminuatftva,valdiminuattva,vpretftva,;
			*!*				vvalftva,vvaltva,vdiscountftva,vvaldiscountftva,vvaldiscounttva,;
			*!*				vvaldiminuatftva,vvaldiminuattva) ;
			*!*				Select a.id_set_fact,a.id_temp,a.denumire,a.explicatie,a.cantitate,a.um,a.serie,;
			*!*				b.proc_tvav,b.id_jtva_coloana,b.gestionabil,IIF(poDate.in_valuta=0,a.pret,ROUND(a.pret*b.curs,gnPc)) As pretftva,;
			*!*				Round(Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))*a.cantitate,gnPc) As valftva,;
			*!*				Round(Round(Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) As valtva,;
			*!*				Round(b.valdiscountftva/a.cantitate,gnPc) As discountftva,;
			*!*				b.valdiscountftva,b.valdiscounttva,;
			*!*				ROUND((Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))-Round(b.valdiscountftva/a.cantitate,gnPc))*a.cantitate,gnPc) as valdiminuatftva,;
			*!*				ROUND(ROUND((Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))-Round(b.valdiscountftva/a.cantitate,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) as valdiminuattva,;
			*!*				IIF(poDate.in_valuta=0,0,a.pret) as vpretftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(a.pret*a.cantitate,gnPc)) as vvalftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(ROUND(a.pret*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc)) as vvaltva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(b.vvaldiscountftva/a.cantitate,gnPc)) as vdiscountftva,;
			*!*				b.vvaldiscountftva,b.vvaldiscounttva,;
			*!*				ROUND(Iif(poDate.in_valuta=0,0,a.Pret-Round(b.vvaldiscountftva/a.cantitate,gnPc))*a.cantitate,gnPc) as vvaldiminuatftva,;
			*!*				ROUND(ROUND(Iif(poDate.in_valuta=0,0,a.Pret-Round(b.vvaldiscountftva/a.cantitate,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) as vvaldiminuattva ;
			*!*				From (lcCursorTemp) a ;
			*!*				Left Join (Select id_set_fact,IIF(poDate.in_valuta=0,1,poDate.curs) as curs,;
			*!*				Max(proc_tvav) As proc_tvav,Min(id_jtva_coloana) as id_jtva_coloana,Max(gestionabil) as gestionabil,;
			*!*				Sum(valdiscountftva) As valdiscountftva,Sum(valdiscounttva) As valdiscounttva,;
			*!*				Sum(vvaldiscountftva) As vvaldiscountftva,Sum(vvaldiscounttva) As vvaldiscounttva ;
			*!*				From crsfactura Where id_set_fact <> 0 ;
			*!*				GROUP By 1,2) b On a.id_set_fact = b.id_set_fact
			*!*	modificare	v 2.0.67	:	am adaugat id_ctr, numar_contract
			*!*	modificare	v 2.0.84
			*!*			Insert Into (lcCursor) (id_set_fact,id_temp,denumire,explicatie,cantitate,um,serie,;
			*!*				proc_tvav,id_jtva_coloana,gestionabil,pretftva,valftva,valtva,discountftva,valdiscountftva,;
			*!*				valdiscounttva,valdiminuatftva,valdiminuattva,vpretftva,;
			*!*				vvalftva,vvaltva,vdiscountftva,vvaldiscountftva,vvaldiscounttva,;
			*!*				vvaldiminuatftva,vvaldiminuattva,id_ctr,numar_contract) ;
			*!*				Select a.id_set_fact,a.id_temp,a.denumire,a.explicatie,a.cantitate,a.um,a.serie,;
			*!*				b.proc_tvav,b.id_jtva_coloana,b.gestionabil,;
			*!*				IIF(poDate.in_valuta=0,a.pret,ROUND(a.pret*b.curs,gnPc)) As pretftva,;
			*!*				Round(Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))*a.cantitate,gnPc) As valftva,;
			*!*				Round(Round(Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) As valtva,;
			*!*				IIF(poDate.in_valuta=0,a.discount_unitar,ROUND(a.discount_unitar*b.curs,gnPc)) As discountftva,;
			*!*				Round(Iif(poDate.in_valuta=0,a.discount_unitar,Round(a.discount_unitar*b.Curs,gnPc))*a.cantitate,gnPc) As valdiscountftva,;
			*!*				Round(Round(Iif(poDate.in_valuta=0,a.discount_unitar,Round(a.discount_unitar*b.Curs,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) As valdiscounttva,;
			*!*				ROUND(Iif(poDate.in_valuta=0,a.Pret-a.discount_unitar,Round((a.Pret-a.discount_unitar)*b.Curs,gnPc))*a.cantitate,gnPc) as valdiminuatftva,;
			*!*				ROUND(ROUND(Iif(poDate.in_valuta=0,a.Pret-a.discount_unitar,Round((a.Pret-a.discount_unitar)*b.Curs,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) as valdiminuattva,;
			*!*				IIF(poDate.in_valuta=0,0,a.pret) as vpretftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(a.pret*a.cantitate,gnPc)) as vvalftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(ROUND(a.pret*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc)) as vvaltva,;
			*!*				IIF(poDate.in_valuta=0,0,a.discount_unitar) as vdiscountftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(a.discount_unitar*a.cantitate,gnPc)) as vvaldiscountftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(ROUND(a.discount_unitar*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc)) as vvaldiscounttva,;
			*!*				ROUND(Iif(poDate.in_valuta=0,0,a.Pret-a.discount_unitar)*a.cantitate,gnPc) as vvaldiminuatftva,;
			*!*				ROUND(ROUND(Iif(poDate.in_valuta=0,0,a.Pret-a.discount_unitar)*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) as vvaldiminuattva, ;
			*!*				b.id_ctr,b.numar_contract ;
			*!*				From (lcCursorTemp) a ;
			*!*				Left Join (Select id_set_fact,IIF(poDate.in_valuta=0,1,poDate.curs) as curs,id_ctr,numar_contract,;
			*!*				Max(proc_tvav) As proc_tvav,Min(id_jtva_coloana) as id_jtva_coloana,Max(gestionabil) as gestionabil ;
			*!*				From crsfactura Where id_set_fact <> 0 ;
			*!*				GROUP By 1,2,3,4) b On a.id_set_fact = b.id_set_fact
			prelucreaza_facturacrs(lcCursorTemp, lcCursor, 0, 0, 0)
			*!*	modificare	v 2.0.84	^
			*!*	modificare	v 2.0.56	^
	Endcase
	If Used(lcCursorTemp)
		Use In (lcCursorTemp)
	Endif
Endproc	&&	creeaza_cursor_seturi
************************************* SFARSIT: creeaza_cursor_seturi *****************************************
********************************* INCEPUT: creeaza_cursor_seturi_proforme *************************************
Procedure creeaza_cursor_seturi_proforme
	Lparameters tnIdProforma
	*!*	Dimension laStructura(1,18)
	lcCursorTemp = [crsfacturasettemp]
	lcCursor = [crsfacturaset]
	If Used(lcCursorTemp)
		Use In (lcCursorTemp)
	Endif
	If Used(lcCursor)
		Use In (lcCursor)
	Endif
	*!*	modificare	v 2.0.84
	*!*	lcSql = [select Min(a.id_proforma_det) as id_temp,b.id_proforma_set as id_set_fact,b.denumire,b.explicatie,b.cantitate,b.um,b.serie,b.pret,Nvl(b.discount_unitar,0) as discount_unitar ] + ;
	*!*		[from ] + gcS + [.proforme_detalii a ] + ;
	*!*		[left join ] + gcS + [.proforme_seturi b on a.id_proforma_set = b.id_proforma_set ] + ;
	*!*		[where a.id_proforma = ] + Alltrim(Str(tnIdProforma)) + [ and a.id_proforma_set is not null ] + ;
	*!*		[and a.sters = 0 ] + ;
	*!*		[group by b.id_proforma_set,b.denumire,b.explicatie,b.cantitate,b.um,b.serie,b.pret,Nvl(b.discount_unitar,0)]
	lcSql = [{call pack_facturare.citeste_proforme_seturi(] + Alltrim(Str(tnIdProforma)) + [)}]
	*!*	modificare	v 2.0.84	^
	lnSucces = goExecutor.oexecute(lcSql, lcCursorTemp)
	Do Case
		Case lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
			Return
		Case Reccount(lcCursorTemp) = 0
			Use In (lcCursorTemp)
		Otherwise
			*!*	modificare	v 2.0.56
			*!*		Afields(laStructura,[crsfactura])
			*!*		Create Cursor (lcCursor) From Array laStructura
			copiaza_structura_cursor([crsfactura], lcCursor)
			*!*	modificare	v 2.0.56	^
			*!*			Insert Into (lcCursor) (id_set_fact,denumire,explicatie,cantitate,um,serie,;
			*!*				proc_tvav,pretftva,valftva,valtva,discountftva,valdiscountftva,;
			*!*				valdiscounttva,valdiminuatftva,valdiminuattva,vpretftva,;
			*!*				vvalftva,vvaltva,vdiscountftva,vvaldiscountftva,vvaldiscounttva,;
			*!*				vvaldiminuatftva,vvaldiminuattva) ;
			*!*				Select a.id_set_fact,a.denumire,a.explicatie,a.cantitate,a.um,a.serie,;
			*!*				b.proc_tvav,;
			*!*				Round(b.valftva/a.cantitate,gnPc) As pretftva,;
			*!*				b.valftva,b.valtva,;
			*!*				Round(b.valdiscountftva/a.cantitate,gnPc) As discountftva,;
			*!*				b.valdiscountftva,b.valdiscounttva,b.valdiminuatftva,b.valdiminuattva,;
			*!*				Round(b.vvalftva/a.cantitate,gnPc) As vpretftva,b.vvalftva,b.vvaltva,;
			*!*				b.vdiscountftva,b.vvaldiscountftva,b.vvaldiscounttva,b.vvaldiminuatftva,;
			*!*				b.vvaldiminuattva ;
			*!*				FROM (lcCursorTemp) a ;
			*!*				Left Join (Select Max(proc_tvav) As proc_tvav,;
			*!*				Sum(valftva) As valftva,Sum(valtva) As valtva,;
			*!*				Sum(valdiscountftva) As valdiscountftva,Sum(valdiscounttva) As valdiscounttva,;
			*!*				Sum(valdiminuatftva) As valdiminuatftva,;
			*!*				Sum(valdiminuattva) As valdiminuattva,;
			*!*				Sum(vvalftva) As vvalftva,Sum(vvaltva) As vvaltva,;
			*!*				Sum(vdiscountftva) As vdiscountftva,;
			*!*				Sum(vvaldiscountftva) As vvaldiscountftva,Sum(vvaldiscounttva) As vvaldiscounttva,;
			*!*				Sum(vvaldiminuatftva) As vvaldiminuatftva,;
			*!*				Sum(vvaldiminuattva) As vvaldiminuattva,id_set_fact From crsfactura Where id_set_fact <> 0 ;
			*!*				GROUP By id_set_fact) b On a.id_set_fact = b.id_set_fact
			*!*	modificare	v 2.0.56 : am discount_unitar pe articolul din set
			*!*			Insert Into (lcCursor) (id_temp,id_set_fact,denumire,explicatie,cantitate,um,serie,;
			*!*				proc_tvav,pretftva,valftva,valtva,discountftva,valdiscountftva,;
			*!*				valdiscounttva,valdiminuatftva,valdiminuattva,vpretftva,;
			*!*				vvalftva,vvaltva,vdiscountftva,vvaldiscountftva,vvaldiscounttva,;
			*!*				vvaldiminuatftva,vvaldiminuattva) ;
			*!*				Select a.id_temp,a.id_set_fact,a.denumire,a.explicatie,a.cantitate,a.um,a.serie,;
			*!*				b.proc_tvav,IIF(poDate.in_valuta=0,a.pret,ROUND(a.pret*b.curs,gnPc)) As pretftva,;
			*!*				Round(Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))*a.cantitate,gnPc) As valftva,;
			*!*				Round(Round(Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) As valtva,;
			*!*				Round(b.valdiscountftva/a.cantitate,gnPc) As discountftva,;
			*!*				b.valdiscountftva,b.valdiscounttva,;
			*!*				ROUND((Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))-Round(b.valdiscountftva/a.cantitate,gnPc))*a.cantitate,gnPc) as valdiminuatftva,;
			*!*				ROUND(ROUND((Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))-Round(b.valdiscountftva/a.cantitate,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) as valdiminuattva,;
			*!*				IIF(poDate.in_valuta=0,0,a.pret) as vpretftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(a.pret*a.cantitate,gnPc)) as vvalftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(ROUND(a.pret*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc)) as vvaltva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(b.vvaldiscountftva/a.cantitate,gnPc)) as vdiscountftva,;
			*!*				b.vvaldiscountftva,b.vvaldiscounttva,;
			*!*				ROUND(Iif(poDate.in_valuta=0,0,a.Pret-Round(b.vvaldiscountftva/a.cantitate,gnPc))*a.cantitate,gnPc) as vvaldiminuatftva,;
			*!*				ROUND(ROUND(Iif(poDate.in_valuta=0,0,a.Pret-Round(b.vvaldiscountftva/a.cantitate,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) as vvaldiminuattva ;
			*!*				From (lcCursorTemp) a ;
			*!*				Left Join (Select id_set_fact,IIF(poDate.in_valuta=0,1,poDate.curs) as curs,;
			*!*				Max(proc_tvav) As proc_tvav,;
			*!*				Sum(valdiscountftva) As valdiscountftva,Sum(valdiscounttva) As valdiscounttva,;
			*!*				Sum(vvaldiscountftva) As vvaldiscountftva,Sum(vvaldiscounttva) As vvaldiscounttva ;
			*!*				From crsfactura Where id_set_fact <> 0 ;
			*!*				GROUP By 1,2) b On a.id_set_fact = b.id_set_fact
			*!*	modificare	v 2.0.84
			*!*	modificare	v 2.0.84
			*!*			Insert Into (lcCursor) (id_temp,id_set_fact,denumire,explicatie,cantitate,um,serie,;
			*!*				proc_tvav,pretftva,valftva,valtva,discountftva,valdiscountftva,;
			*!*				valdiscounttva,valdiminuatftva,valdiminuattva,vpretftva,;
			*!*				vvalftva,vvaltva,vdiscountftva,vvaldiscountftva,vvaldiscounttva,;
			*!*				vvaldiminuatftva,vvaldiminuattva) ;
			*!*				Select a.id_temp,a.id_set_fact,a.denumire,a.explicatie,a.cantitate,a.um,a.serie,;
			*!*				b.proc_tvav,;
			*!*				IIF(poDate.in_valuta=0,a.pret,ROUND(a.pret*b.curs,gnPc)) As pretftva,;
			*!*				Round(Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))*a.cantitate,gnPc) As valftva,;
			*!*				Round(Round(Iif(poDate.in_valuta=0,a.Pret,Round(a.Pret*b.Curs,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) As valtva,;
			*!*				IIF(poDate.in_valuta=0,a.discount_unitar,ROUND(a.discount_unitar*b.curs,gnPc)) As discountftva,;
			*!*				Round(Iif(poDate.in_valuta=0,a.discount_unitar,Round(a.discount_unitar*b.Curs,gnPc))*a.cantitate,gnPc) As valdiscountftva,;
			*!*				Round(Round(Iif(poDate.in_valuta=0,a.discount_unitar,Round(a.discount_unitar*b.Curs,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) As valdiscounttva,;
			*!*				ROUND(Iif(poDate.in_valuta=0,a.Pret-a.discount_unitar,Round((a.Pret-a.discount_unitar)*b.Curs,gnPc))*a.cantitate,gnPc) as valdiminuatftva,;
			*!*				ROUND(ROUND(Iif(poDate.in_valuta=0,a.Pret-a.discount_unitar,Round((a.Pret-a.discount_unitar)*b.Curs,gnPc))*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) as valdiminuattva,;
			*!*				IIF(poDate.in_valuta=0,0,a.pret) as vpretftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(a.pret*a.cantitate,gnPc)) as vvalftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(ROUND(a.pret*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc)) as vvaltva,;
			*!*				IIF(poDate.in_valuta=0,0,a.discount_unitar) as vdiscountftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(a.discount_unitar*a.cantitate,gnPc)) as vvaldiscountftva,;
			*!*				IIF(poDate.in_valuta=0,0,ROUND(ROUND(a.discount_unitar*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc)) as vvaldiscounttva,;
			*!*				ROUND(Iif(poDate.in_valuta=0,0,a.Pret-a.discount_unitar)*a.cantitate,gnPc) as vvaldiminuatftva,;
			*!*				ROUND(ROUND(Iif(poDate.in_valuta=0,0,a.Pret-a.discount_unitar)*a.cantitate,gnPc)*(b.proc_tvav-1),gnPc) as vvaldiminuattva ;
			*!*				From (lcCursorTemp) a ;
			*!*				Left Join (Select id_set_fact,IIF(poDate.in_valuta=0,1,poDate.curs) as curs,;
			*!*				Max(proc_tvav) As proc_tvav ;
			*!*				From crsfactura Where id_set_fact <> 0 ;
			*!*				GROUP By 1,2) b On a.id_set_fact = b.id_set_fact
			prelucreaza_facturacrs(lcCursorTemp, lcCursor, 0, 0, 0)
			*!*	modificare	v 2.0.84	^
			*!*	modificare	v 2.0.56	^
	Endcase
	If Used(lcCursorTemp)
		Use In (lcCursorTemp)
	Endif
Endproc	&&	creeaza_cursor_seturi_proforme
********************************* SFARSIT: creeaza_cursor_seturi_proforme **************************************
*!*	******************************** INCEPUT: actualizeaza_optiuni_program *************************************
*!*	Function actualizeaza_optiuni_program
*!*	Lparameters tnTip
*!*	&&	0	-	toate
*!*	&&	1	-	fara id-uri
*!*	Local llReturn
*!*	Store .F. To llReturn
*!*	If Empty(tnTip)
*!*		lcFiltru = []
*!*	Else
*!*		lcFiltru = [ and varname not like 'ID%']
*!*	Endif
*!*	lcSql=[SELECT VARTYPE,VARNAME,VARVALUE FROM ] + gcS + [.OPTIUNI WHERE PROGRAM='] + Upper(Alltrim(gcNumeProgram)) + ['] + ;
*!*		lcFiltru
*!*	lcCursor=[crsoptiuni]
*!*	lnSucces=goExecutor.oexecute(lcSql,lcCursor)
*!*	If lnSucces<0
*!*		amessagebox(goExecutor.cEroare,0+48,"Eroare")
*!*	Else
*!*		Select crsoptiuni
*!*		Scan
*!*			lcvartype=Alltrim(Upper(Vartype))
*!*			lcvarname=Alltrim(Upper(varname))
*!*			Do Case
*!*			Case lcvartype = "CHARACTER"
*!*				luvarvalue = Alltrim(varvalue)
*!*				gc&lcvarname. = luvarvalue
*!*			Case lcvartype = "NUMERIC"
*!*				luvarvalue = Val(varvalue)
*!*				gn&lcvarname. = luvarvalue
*!*			Endcase
*!*		Endscan
*!*		Use In crsoptiuni
*!*		llReturn = .T.
*!*	Endif
*!*	Return llReturn
*!*	Endfunc	&&	actualizeaza_optiuni_program
*!*	******************************** SFARSIT: actualizeaza_optiuni_program *************************************
******************************************* INCEPUT:caut_locatie_restaurant *******************************************
Function caut_locatie_restaurant
	Local loCauta
	Store "" To loCauta
	pcselect = [select id_rest_locatie,locatie FROM ] + gcS + [.rest_vcoresp_util_locatii]
	pcfiltru = [1=2]
	pcschema = ['']
	pcorder = [locatie]
	pccoloane = [locatie]
	pcTitlu = [Alegeti locatia]
	pcTitluColoane = [Locatie]
	pcFiltruOriginal = [id_util_rest = ] + Alltrim(Str(gnIdUtil))
	lcNumeProc = []
	llToateIreg = .F.
	loCauta = cauta_alfa(pcselect, pcfiltru, pcschema, pcorder, pccoloane, pcTitlu, pcTitluColoane, lcNumeProc, llToateIreg, pcFiltruOriginal)
	Return loCauta
Endfunc
******************************************* SFARSIT: caut_locatie_restaurant *******************************************
******************************************* INCEPUT:caut_comanda_client *******************************************
Function caut_comanda_client
	Lparameters tnIdPart
	Local loCauta
	Store "" To loCauta
	If !Empty(tnIdPart)
		lcFiltruClient = [ and id_part = ] + Alltrim(Str(tnIdPart))
	Else
		lcFiltruClient = []
	Endif
	pcselect = [select id_comanda, data_comanda, nr_comanda FROM vcomenzi ]
	pcfiltru = [1=2]
	pcschema = ['']
	pcorder = [data_comanda,nr_comanda]
	pccoloane = [nr_comanda,data_comanda]
	pcTitlu = [Alegeti comanda]
	pcTitluColoane = [Nr. comanda,Data comanda]
	pcFiltruOriginal = [facturat = 0 and interna in (2,5) ] + lcFiltruClient + gcCondSucursala
	lcNumeProc = []
	llToateIreg = .F.
	loCauta = cauta_alfa(pcselect, pcfiltru, pcschema, pcorder, pccoloane, pcTitlu, pcTitluColoane, lcNumeProc, llToateIreg, pcFiltruOriginal)

	Return loCauta
Endfunc
******************************************* SFARSIT: caut_comanda_client *******************************************
******************************************* INCEPUT:caut_comanda_clienti *******************************************
* Afisez toate comenzile, indiferent de client
* Este posibil sa fac comanda pe un client si sa fac facturi pe persoana fizica si pe persoana juridica pentru acel client
* Arat comenzile ordonate dupa client si numar comanda
* Este folosita in ROAFACTURARE > frm_date_factura.do_cauta_comanda
Function caut_comanda_clienti
	Lparameters tnIdPart

	PRIVATE pnIdPart
	pnIdPart = IIF(!EMPTY(NVL(m.tnIdPart,0)), m.tnIdPart, 0)

	Local loCauta
	Store "" To loCauta
	If !Empty(tnIdPart)
		lcFiltruClient = [ and id_part = ] + Alltrim(Str(tnIdPart))
	Else
		lcFiltruClient = []
	Endif
	pcselect = [select decode(id_part, ?pnIdpart, 1, 2) as ord, id_part, nume, id_comanda, data_comanda, nr_comanda FROM vcomenzi]
	pcfiltru = [1=2]
	pcschema = ['']
	pcorder = [ord, nume,data_comanda,nr_comanda]
	pccoloane = [nr_comanda,data_comanda,nume]
	pcTitlu = [Alegeti comanda]
	pcTitluColoane = [Nr. comanda,Data comanda,Client]
	pcFiltruOriginal = [facturat = 0 and interna in (2,5) ] + gcCondSucursala
	lcNumeProc = []
	llToateIreg = .F.
	loCauta = cauta_alfa(pcselect, pcfiltru, pcschema, pcorder, pccoloane, pcTitlu, pcTitluColoane, lcNumeProc, llToateIreg, pcFiltruOriginal)

	Return loCauta
Endfunc
******************************************* SFARSIT: caut_comanda_clienti *******************************************
******************************************* INCEPUT:caut_comanda_gestiune *******************************************
Function caut_comanda_gestiune
	Lparameters tnIdGestiune
	Local loCauta
	Store "" To loCauta
	If !Empty(tnIdGestiune)
		lcFiltruGestiune = [ and id_gestiune = ] + Alltrim(Str(tnIdGestiune))
	Else
		lcFiltruGestiune = []
	Endif
	pcselect = [select id_comanda, data_comanda, nr_comanda FROM ] + gcS + [.vcomenzi ]
	pcfiltru = [1=2]
	pcschema = ['']
	pcorder = [data_comanda,nr_comanda]
	pccoloane = [nr_comanda,data_comanda]
	pcTitlu = [Alegeti comanda]
	pcTitluColoane = [Nr. comanda,Data comanda]
	pcFiltruOriginal = [facturat = 0 and interna = 3 ] + lcFiltruGestiune + gcCondSucursala
	lcNumeProc = []
	llToateIreg = .F.
	loCauta = cauta_alfa(pcselect, pcfiltru, pcschema, pcorder, pccoloane, pcTitlu, pcTitluColoane, lcNumeProc, llToateIreg, pcFiltruOriginal)

	Return loCauta
Endfunc
******************************************* SFARSIT: caut_comanda_gestiune *******************************************
***************************************** INCEPUT:caut_contract_facturare *******************************************
Function caut_contract_facturare
	Lparameters tnIdPart, tcSirTipFacturare
	Local loCauta, lcSelect, lcFiltru, lcSchema, lcColoane, lcTitlu, lcTitluColoane, lcFiltruOriginal, lcNumeProg, llToateInreg, lcOrder, lcFiltruSuplimentar, lcPrimaColoana, lnPornire, lnTipReturn, lcIdColumn
	Store "" To loCauta
	*!*	modificare v 2.0.67 : am adaugat tcSirTipFacturare si lcFiltruSuplimentar
	If !Empty(tcSirTipFacturare)
		lcFiltruSuplimentar = [ and opt_facturare in (] + Alltrim(tcSirTipFacturare) + [)]
	Else
		lcFiltruSuplimentar = []
	Endif
	If !Empty(tnIdPart)
		lcFiltruSuplimentar = lcFiltruSuplimentar + [ and id_part = ] + Alltrim(Str(tnIdPart))
	Else
		lcFiltruSuplimentar = []
	Endif
	lcSelect = [select id_ctr, contract, numar, data, denumire, scadenta_incasare, opt_facturare, text_standard, afisare_scadenta FROM fact_vcontracte ]
	lcFiltru = [1=2]
	lcSchema = ['']
	lcOrder = [data,numar]
	lcColoane = [numar,data,denumire]
	lcTitlu = [Alegeti contractul (mouse-click pe numar sau apasati SPACE)]
	lcTitluColoane = [Numar,Data,Denumire]
	lcFiltruOriginal = [2=2] + gcCondSucursala + lcFiltruSuplimentar
	lcNumeProc = [contracte] +  ';' + IIF(!EMPTY(m.tnIdPart), ALLTRIM(STR(m.tnIdPart)), '0') + ',-1'
	llToateInreg = .F.
	*!*	modificare v 2.0.67
	*!*	loCauta = cauta_alfa(pcselect,pcfiltru,pcschema,pcorder,pccoloane,pcTitlu,pcTitluColoane, lcNumeProc, llToateIreg, pcFiltruOriginal)
	lcPrimaColoana = [numar]
	lnPornire = 1
	lnTipReturn = 1		&&	1 = Cursor XML
	lcIdColumn = [id_ctr]
	loCauta = cauta_alfa(lcSelect, lcFiltru, lcSchema, lcOrder, lcColoane, lcTitlu, lcTitluColoane, lcNumeProc, llToateInreg, lcFiltruOriginal, lcPrimaColoana, lnPornire, lnTipReturn, lcIdColumn)
	*!*	modificare v 2.0.67	^
	Release lcSelect, lcFiltru, lcSchema, lcColoane, lcTitlu, lcTitluColoane, lcFiltruOriginal, lcNumeProg, llToateInreg, lcOrder, lcFiltruSuplimentar, lcPrimaColoana, lnPornire, lnTipReturn, lcIdColumn
	Return loCauta
Endfunc
***************************************** SFARSIT: caut_contract_facturare *******************************************
******************************************* INCEPUT: caut_avize *******************************************
Function caut_avize
	Lparameters tnIdPart
	Store "" To loCauta

	lcFiltruPart = Iif(Empty(tnIdPart), [], [ and a.id_part = ] + Alltrim(Str(tnIdPart)))
	lcFiltru = [1=2]
	lcSchema = [serie_act C(50),numar_act N(20),data_act d,dataora t,id_vanzare N(20), id_comanda n(20)]
	lcOrder = [dataora]
	lcColoane = [serie_act,numar_act,data_act,dataora]
	lcTitlu = [Alegeti avizele (mouse-click pe numar sau apasati SPACE)]
	lcTitluColoane = [Serie act,Numar act,Data,Data inreg.]
	llMultipleArticles = .T.
	lcSelect = [select a.serie_act,a.numar_act,a.data_act,a.dataora,a.id_vanzare,] + ;
		[(case when a.tip in (21,42) then a.id_comanda else 0 end) as id_comanda from ] + gcS + [.vanzari a ]
	lcFiltruOriginal =  [a.sters=0 and a.tip in (21,22,26,42) and a.facturat = 0] + Strtran(gcCondSucursala, [id_sucursala], [a.id_sucursala]) + ;
		lcFiltruPart
	**	21	-	catre clienti din comanda
	**	22	-	catre clienti din lista
	**	23	-	transfer catre subunitati din lista
	**	24  -	aviz de retur
	**	25	-	transfer catre subunitati din comanda
	**	26	-	catre clienti din contract
	**	27	-	transfer catre subunitati pe baza de lucrare
	**	28	-	catre clienti debitori din comanda
	**	29	-	catre clienti debitori din lista
	**	30	-	transfer catre subunitati pe baza de NIR
	**	41	-	retur transfer catre subunitati lista pret
	**	42	-	catre clienti custodie pe baza de comanda
	**	fara 47, deoarece acele avize le descarc prin alta optiune
	lcNumeProc = []
	llToateIreg = .F.
	lcPrimaColoana = [numar_act]
	lnPornire = 1
	lnTipReturn = Iif(llMultipleArticles, 1, 0)
	lcIdColumn = "id_vanzare"
	loCauta = cauta_alfa(lcSelect, lcFiltru, lcSchema, lcOrder, lcColoane, lcTitlu, lcTitluColoane, lcNumeProc, llToateIreg, lcFiltruOriginal, lcPrimaColoana, lnPornire, lnTipReturn, lcIdColumn)

	Return loCauta
Endfunc && caut_avize
******************************************* SFARSIT: caut_avize *******************************************
******************************************* INCEPUT: caut_facturi_client *******************************************
Function caut_facturi_client
	Lparameters tnIdPart
	Local loCauta
	Store "" To loCauta
	If !Empty(tnIdPart)
		lcFiltruClient = [ and id_part = ] + Alltrim(Str(tnIdPart))
	Else
		lcFiltruClient = []
	Endif
	pcselect = [select id_fact,dataact,serie_act,nract from ] + gcS + [.ireg_parteneri ]
	pcfiltru = [1=2]
	pcschema = ['']
	pcorder = [dataact,serie_act,nract]
	pccoloane = [dataact,serie_act,nract]
	pcTitlu = [Alegeti factura]
	pcTitluColoane = [Data factura,Serie act,Nr. factura]
	pcFiltruOriginal = [an = ?gnAn and luna = ?gnLuna and cont = ?gcCont411 ] + ;
		[and valdebit + precvaldeb <> precvalcred + valcredit and id_valuta <> 0 ] + lcFiltruClient + ;
		+ gcCondSucursala
	lcNumeProc = []
	llToateIreg = .F.
	loCauta = cauta_alfa(pcselect, pcfiltru, pcschema, pcorder, pccoloane, pcTitlu, pcTitluColoane, lcNumeProc, llToateIreg, pcFiltruOriginal)

	Return loCauta
	******************************************* SFARSIT: caut_facturi_client *******************************************
	*************************************** INCEPUT: caut_facturi_multiple_client ***************************************
Function caut_facturi_multiple_client
	Lparameters tnIdPart, tnInValuta, tnIdValuta, tlFacturiMultiple
	Local loCauta
	Store "" To loCauta
	lcFiltruPart = Iif(Empty(tnIdPart), [], [ and id_part = ] + Alltrim(Str(tnIdPart)))
	lcFiltruValuta = Iif(Empty(tnInValuta), [ and in_valuta = 0], [ and in_valuta = 1 and id_valuta = ] + Alltrim(Str(tnIdValuta)))
	lcFiltru = [1=2]
	lcSchema = [serie_act C(50),numar_act N(20),data_act d,dataora t,id_vanzare N(20)]
	lcOrder = [dataora]
	lcColoane = [serie_act,numar_act,data_act,dataora]
	If Empty(tlFacturiMultiple)
		lcTitlu = [Alegeti factura]
	Else
		lcTitlu = [Alegeti facturile (mouse-click pe numar sau apasati SPACE)]
	Endif

	lcTitluColoane = [Serie act,Numar act,Data,Data inreg.]
	lcSelect = [select serie_act,numar_act,data_act,dataora,id_vanzare ] + ;
		[from ] + gcS + [.fact_vfacturi ]
	lcFiltruOriginal =  [sters=0 and tip in (1,2,3,4,5,6,10,-1,-2,-3,-4,-11) ] + gcCondSucursala + ;
		lcFiltruPart + lcFiltruValuta
	lcNumeProc = []
	llToateIreg = .F.
	lcPrimaColoana = [numar_act]
	lnPornire = 1
	lnTipReturn = Iif(tlFacturiMultiple, 1, 0)
	lcIdColumn = "id_vanzare"
	loCauta = cauta_alfa(lcSelect, lcFiltru, lcSchema, lcOrder, lcColoane, lcTitlu, lcTitluColoane, lcNumeProc, llToateIreg, lcFiltruOriginal, lcPrimaColoana, lnPornire, lnTipReturn, lcIdColumn)

	Return loCauta
	*************************************** SFARSIT: caut_facturi_multiple_client ***************************************

	*************************************** INCEPUT: caut_facturi_multiple_client_articol ***************************************
Function caut_facturi_multiple_client_articol
	Lparameters tnIdPart, tnInValuta, tnIdValuta, tlFacturiMultiple, tnIdArticol
	Local loCauta
	loCauta = ""
	PRIVATE pnIdArticol, pnIdPart, pnIdValuta
	pnIdArticol = tnIdArticol
	pnIdPart = tnIdPart
	pnIdValuta = tnIdValuta

	lcFiltruPart = Iif(Empty(tnIdPart), [], [ and a.id_part = ?pnIdPart])
	lcFiltruValuta = Iif(Empty(tnInValuta), [ and a.in_valuta = 0], [ and a.in_valuta = 1 and b.id_valuta = ?pnIdValuta])
	lcFiltru = [1=2]
	lcSchema = [serie_act C(50),numar_act N(20),data_act d,dataora t,id_vanzare N(20)]
	lcOrder = [a.data_act desc, a.numar_act desc, a.dataora desc]
	lcColoane = [serie_act,numar_act,data_act,dataora]
	If Empty(tlFacturiMultiple)
		lcTitlu = [Alegeti factura]
	Else
		lcTitlu = [Alegeti facturile (mouse-click pe numar sau apasati SPACE)]
	Endif

	lcTitluColoane = [Serie act,Numar act,Data,Data inreg.]
	lcSelect = [select a.serie_act,a.numar_act,a.data_act,a.dataora,a.id_vanzare ] + ;
		[from vanzari a join vanzari_detalii b on a.id_vanzare = b.id_vanzare and b.id_articol = ?pnIdArticol ]

	lcFiltruOriginal =  [a.sters=0 and a.tip in (1,2,3,4,5,6,10,-1,-2,-3,-4,-11) ] + gcCondSucursala + ;
		lcFiltruPart + lcFiltruValuta
	lcNumeProc = []
	llToateIreg = .F.
	lcPrimaColoana = [numar_act]
	lnPornire = 1
	lnTipReturn = Iif(tlFacturiMultiple, 1, 0)
	lcIdColumn = "id_vanzare"
	loCauta = cauta_alfa(lcSelect, lcFiltru, lcSchema, lcOrder, lcColoane, lcTitlu, lcTitluColoane, lcNumeProc, llToateIreg, lcFiltruOriginal, lcPrimaColoana, lnPornire, lnTipReturn, lcIdColumn)

	Return loCauta
	*************************************** SFARSIT: caut_facturi_multiple_client ***************************************


	******************************************* INCEPUT: caut_gestiune_sursa *******************************************
Function caut_gestiune_sursa
	Lparameters tcListaTipGest
	Store "" To loCauta
	pcFiltruOriginal = [a.id_util = ?gnIdUtil and a.sters = 0 and b.sters = 0 and c.sters = 0]
	If !Empty(tcListaTipGest)
		pcFiltruOriginal = pcFiltruOriginal + [ and c.nr_pag in (] + Alltrim(tcListaTipGest) + [)]
	Endif
	pcselect = [select distinct c.nume_gestiune, b.id_gestiune, c.cont, c.acont ] + ;
		[from ] + gcS + [.gest_coresp_util_grupe a ] + ;
		[left join ] + gcS + [.gest_coresp_grupe_gestiuni b on a.id_grupe = b.id_grupe ] + ;
		[left join ] + gcS + [.nom_gestiuni c on b.id_gestiune = c.id_gestiune ]
	pcfiltru = [1=2]
	pcschema = []
	pcorder = [nume_gestiune,cont,acont]
	pccoloane = [nume_gestiune,cont,acont]
	pcTitlu = [Alegeti gestiunea]
	pcTitluColoane = [Gestiune,Cont,Acont]
	lcNumeProc = []
	llToateIreg = .F.
	loCauta = cauta_alfa(pcselect, pcfiltru, pcschema, pcorder, pccoloane, pcTitlu, pcTitluColoane, lcNumeProc, llToateIreg, pcFiltruOriginal)

	Return loCauta
Endfunc
******************************************* SFARSIT: caut_gestiune_sursa *******************************************
**************************************** INCEPUT: caut_utilizator_facturare ****************************************
Function caut_utilizator_facturare
	Store "" To loCauta
	pcFiltruOriginal = [a.id_util in (select distinct id_util from ] + gcS + [.vanzari where sters = 0] + ;
		gcCondSucursala + [)]
	pcselect = [select a.id_util,a.utilizator from syn_vutilizatori a ]
	pcfiltru = [1=2]
	pcschema = []
	pcorder = [utilizator]
	pccoloane = [utilizator]
	pcTitlu = [Alegeti utilizatorul]
	pcTitluColoane = [Utilizator]
	lcNumeProc = []
	llToateIreg = .F.
	loCauta = cauta_alfa(pcselect, pcfiltru, pcschema, pcorder, pccoloane, pcTitlu, pcTitluColoane, lcNumeProc, llToateIreg, pcFiltruOriginal)

	Return loCauta
Endfunc
**************************************** SFARSIT: caut_utilizator_facturare ****************************************
*!*	******************************************* INCEPUT: caut_gestiune_dest *******************************************
*!*	Function caut_gestiune_dest
*!*	Lparameters tnIdGestiune
*!*	Store "" To loCauta
*!*	pcFiltruOriginal = [a.id_util=?gnIdUtil and a.sters=0 and b.sters=0 and c.sters=0 and c.nr_pag in (4,5,6)]
*!*	If !Empty(tnIdGestiune)
*!*		pcFiltruOriginal = pcFiltruOriginal + [ and b.id_gestiune!=] + Alltrim(Str(tnIdGestiune))
*!*	Endif

*!*	pcselect = [select distinct c.nume_gestiune,b.id_gestiune,d.id_tipgest,] + ;
*!*		[(case when p.explicatie is null then s.explicatie else p.explicatie end) as explicatie,] + ;
*!*		[d.cont,p.acont ] + ;
*!*		[from ] + gcS + [.gest_coresp_util_grupe a ] + ;
*!*		[left join ] + gcS + [.gest_coresp_grupe_gestiuni b on a.id_grupe=b.id_grupe ] + ;
*!*		[left join ] + gcS + [.nom_gestiuni c on b.id_gestiune=c.id_gestiune ] + ;
*!*		[left join ] + gcS + [.coresp_cont_tipgest d on c.nr_pag=d.id_tipgest ] + ;
*!*		[left join ] + gcS + [.vplcont_analitic p on d.cont=p.cont and p.an=?gnAn ] + ;
*!*		[left join ] + gcS + [.vplcont_sintetic s on d.cont=s.cont and s.an=?gnAn ]
*!*	pcfiltru = [1=2]
*!*	pcschema = []
*!*	pcorder = [nume_gestiune,cont,acont]
*!*	pccoloane = [nume_gestiune,cont,acont,explicatie]
*!*	pcTitlu = [Alegeti gestiunea si contul de transfer]
*!*	pcTitluColoane = [Gestiune,Cont,Analitic,Explicatie]
*!*	lcNumeProc = []
*!*	llToateIreg = .F.
*!*	loCauta = cauta_alfa(pcselect,pcfiltru,pcschema,pcorder,pccoloane,pcTitlu,pcTitluColoane, lcNumeProc, llToateIreg, pcFiltruOriginal)

*!*	Return loCauta
*!*	Endfunc
*!*	******************************************* SFARSIT: caut_gestiune_dest *******************************************
*!*	******************************************* INCEPUT: caut_politici_curente_util *******************************************
*!*	Procedure caut_politici_curente_util
*!*	Local loCauta,llReturn
*!*	Store "" To loCauta
*!*	Store .F. To llReturn
*!*	lcSelect = ["select id_pol,nume,datai,datas from ] + gcS + [.fact_vpolitici_pret_curente"]
*!*	lcFiltru = [1=2]
*!*	lcSchema = ['']
*!*	lcorder = [2]
*!*	lccoloane = [nume,datai,datas]
*!*	lcTitlu = [Alegeti politica de preturi]
*!*	lcTitluColoane = [Nume,Data inceput,Data sfarsit]
*!*	lcNumeProc = []
*!*	llToateIreg = .F.
*!*	lcFiltruOriginal = [id_util = ] + Alltrim(Str(gnIdUtil))
*!*	loCauta = cauta_alfa(lcSelect, lcFiltru, lcSchema, lcorder, lccoloane, lcTitlu, lcTitluColoane, lcNumeProc, llToateIreg, lcFiltruOriginal)
*!*	Return loCauta
*!*	Endproc	&&	caut_politici_curente_util
*!*	******************************************* SFARSIT: caut_politici_curente_util *******************************************
******************************************* INCEPUT: calculeaza_totaluri *******************************************
Function calculeaza_totaluri
	Lparameters toArticol
	*!* v 2.0.15 calcul valoare tva din valoare cu tva daca preturi_cu_tva = 1
	Local lnProcTvav_19_119
	lnProcTvav_19_119 = (toArticol.proc_tvav - 1) / toArticol.proc_tvav
	*!* v 2.0.15 ^
	If Type('toArticol.cu_tva')	<> 'U'
		AddProperty(toArticol, 'preturi_cu_tva', toArticol.cu_tva)
		AddProperty(toArticol, 'discount_unitar', toArticol.discountftva)
		AddProperty(toArticol, 'discount_unitar_ctva', toArticol.discountctva)
		AddProperty(toArticol, 'pretftva_val', toArticol.vpretftva)
		AddProperty(toArticol, 'pretctva_val', toArticol.vpretctva)
		AddProperty(toArticol, 'discount_unitar_val', toArticol.vdiscountftva)
		AddProperty(toArticol, 'discount_unitar_ctva_val', toArticol.vdiscountctva)
	Endif

	If toArticol.preturi_cu_tva = 0
		toArticol.vvalftva = Round(toArticol.Pretftva_val * toArticol.cantitate, gnPVal)
		toArticol.vvaltva = Round(Round(toArticol.Pretftva_val * toArticol.cantitate, gnPVal) * (toArticol.proc_tvav - 1), gnPVal)
		toArticol.vvalctva = toArticol.vvalftva + toArticol.vvaltva

		toArticol.vvaldiminuatftva = Round((toArticol.Pretftva_val - toArticol.discount_unitar_val) * toArticol.cantitate, gnPVal)
		toArticol.vvaldiminuattva = Round(Round((toArticol.Pretftva_val - toArticol.discount_unitar_val) * toArticol.cantitate, gnPVal) * (toArticol.proc_tvav - 1), gnPVal)
		toArticol.vvaldiminuatctva = toArticol.vvaldiminuatftva + toArticol.vvaldiminuattva

		toArticol.vvaldiscountftva = Round(toArticol.discount_unitar_val * toArticol.cantitate, gnPVal)
		toArticol.vvaldiscounttva = Round(Round(toArticol.discount_unitar_val * toArticol.cantitate, gnPVal) * (toArticol.proc_tvav - 1), gnPVal)
		toArticol.vvaldiscountctva = toArticol.vvaldiscountftva + toArticol.vvaldiscounttva

		If poDate.in_valuta = 0
			toArticol.valftva = Round(toArticol.Pretftva * toArticol.cantitate, gnPc)
			toArticol.valtva = Round(Round(toArticol.Pretftva * toArticol.cantitate, gnPc) * (toArticol.proc_tvav - 1), gnPc)
			toArticol.valctva = toArticol.valftva + toArticol.valtva

			toArticol.valdiscountftva = Round(toArticol.discount_unitar * toArticol.cantitate, gnPc)
			toArticol.valdiscounttva = Round(Round(toArticol.discount_unitar * toArticol.cantitate, gnPc) * (toArticol.proc_tvav - 1), gnPc)
			toArticol.valdiscountctva = toArticol.valdiscountftva + toArticol.valdiscounttva

			toArticol.valdiminuatftva = Round((toArticol.Pretftva - toArticol.discount_unitar) * toArticol.cantitate, gnPc)
			toArticol.valdiminuattva = Round(Round((toArticol.Pretftva - toArticol.discount_unitar) * toArticol.cantitate, gnPc) * (toArticol.proc_tvav - 1), gnPc)
			toArticol.valdiminuatctva = toArticol.valdiminuatftva + toArticol.valdiminuattva

		Else
			toArticol.valftva = Round(Round(toArticol.Pretftva_val * toArticol.Curs / toArticol.multiplicator, gnPPretV) * toArticol.cantitate, gnPc)
			toArticol.valtva = Round(Round(Round(toArticol.Pretftva_val * toArticol.Curs / toArticol.multiplicator, gnPPretV) * toArticol.cantitate, gnPc) * (toArticol.proc_tvav - 1), gnPc)
			toArticol.valctva = toArticol.valftva + toArticol.valtva

			toArticol.valdiscountftva = Round(Round(toArticol.discount_unitar_val * toArticol.Curs / toArticol.multiplicator, gnPPretV) * toArticol.cantitate, gnPc)
			toArticol.valdiscounttva = Round(Round(Round(toArticol.discount_unitar_val * toArticol.Curs / toArticol.multiplicator, gnPPretV) * toArticol.cantitate, gnPc) * (toArticol.proc_tvav - 1), gnPc)
			toArticol.valdiscountctva = toArticol.valdiscountftva + toArticol.valdiscounttva

			toArticol.valdiminuatftva = Round((Round(toArticol.Pretftva_val * toArticol.Curs / toArticol.multiplicator, gnPPretV) - Round(toArticol.discount_unitar_val * toArticol.Curs / toArticol.multiplicator, gnPPretV)) * toArticol.cantitate, gnPc)
			toArticol.valdiminuattva = Round(Round((Round(toArticol.Pretftva_val * toArticol.Curs / toArticol.multiplicator, gnPPretV) - Round(toArticol.discount_unitar_val * toArticol.Curs / toArticol.multiplicator, gnPPretV)) * toArticol.cantitate, gnPc) * (toArticol.proc_tvav - 1), gnPc)
			toArticol.valdiminuatctva = toArticol.valdiminuatftva + toArticol.valdiminuattva
		Endif

	Else
		*!* v 2.0.15 calcul valoare tva din valoare cu tva daca preturi_cu_tva = 1
		toArticol.vvalctva = Round(toArticol.Pretctva_val * toArticol.cantitate, gnPVal)
		*!*		toArticol.vvalftva = Round(toArticol.Pretftva_val * toArticol.cantitate, gnPVal)
		*!*		toArticol.vvaltva = toArticol.vvalctva - toArticol.vvalftva
		toArticol.vvaltva = Round(toArticol.vvalctva * m.lnProcTvav_19_119, gnPVal)
		toArticol.vvalftva = toArticol.vvalctva - toArticol.vvaltva

		toArticol.vvaldiscountctva = Round(toArticol.discount_unitar_ctva_val * toArticol.cantitate, gnPVal)
		*!*		toArticol.vvaldiscountftva = Round(toArticol.discount_unitar_val * toArticol.cantitate, gnPVal)
		*!*		toArticol.vvaldiscounttva = toArticol.vvaldiscountctva - toArticol.vvaldiscountftva
		toArticol.vvaldiscounttva =  Round(toArticol.vvaldiscountctva * m.lnProcTvav_19_119, gnPVal)
		toArticol.vvaldiscountftva = toArticol.vvaldiscountctva - toArticol.vvaldiscounttva


		toArticol.vvaldiminuatctva = Round((toArticol.Pretctva_val - toArticol.discount_unitar_ctva_val) * toArticol.cantitate, gnPVal)
		*!*		toArticol.vvaldiminuatftva = Round(Round((toArticol.Pretctva_val - toArticol.discount_unitar_ctva) / toArticol.proc_tvav, gnPVal) * toArticol.cantitate, gnPVal)
		*!*		toArticol.vvaldiminuattva = toArticol.vvaldiminuatctva - toArticol.vvaldiminuatftva
		toArticol.vvaldiminuattva = Round(toArticol.vvaldiminuatctva  * m.lnProcTvav_19_119, gnPVal)
		toArticol.vvaldiminuatftva = toArticol.vvaldiminuatctva - toArticol.vvaldiminuattva

		If poDate.in_valuta = 0
			toArticol.valctva = Round(toArticol.Pretctva * toArticol.cantitate, gnPc)
			*!*			toArticol.valftva = Round(toArticol.Pretftva * toArticol.cantitate,gnPc)
			*!*			toArticol.valtva = toArticol.valctva - toArticol.valftva
			toArticol.valtva = Round(toArticol.valctva * m.lnProcTvav_19_119, gnPc)
			toArticol.valftva = toArticol.valctva - toArticol.valtva

			toArticol.valdiscountctva = Round(toArticol.discount_unitar_ctva * toArticol.cantitate, gnPc)
			*!*			toArticol.valdiscountftva = Round(toArticol.discount_unitar * toArticol.cantitate,gnPc)
			*!*			toArticol.valdiscounttva = toArticol.valdiscountctva - toArticol.valdiscountftva
			toArticol.valdiscounttva = Round(toArticol.valdiscountctva * m.lnProcTvav_19_119, gnPc)
			toArticol.valdiscountftva = toArticol.valdiscountctva - toArticol.valdiscounttva

			toArticol.valdiminuatctva = Round((toArticol.Pretctva - toArticol.discount_unitar_ctva) * toArticol.cantitate, gnPc)
			*!*			toArticol.valdiminuatftva = Round(Round((toArticol.Pretctva - toArticol.discount_unitar_ctva) / toArticol.proc_tvav,gnPc) * toArticol.cantitate, gnPc)
			*!*			toArticol.valdiminuattva = toArticol.valdiminuatctva - toArticol.valdiminuatftva
			toArticol.valdiminuattva = Round(toArticol.valdiminuatctva * m.lnProcTvav_19_119, gnPc)
			toArticol.valdiminuatftva = toArticol.valdiminuatctva - toArticol.valdiminuattva
		Else
			toArticol.valctva = Round(Round(toArticol.Pretctva_val * toArticol.Curs / toArticol.multiplicator, gnPPretV) * toArticol.cantitate, gnPc)
			*!*			toArticol.valftva = Round(Round(toArticol.Pretftva_val * toArticol.Curs,gnPc) * toArticol.cantitate,gnPc)
			*!*			toArticol.valtva = toArticol.valctva - toArticol.valftva
			toArticol.valtva = Round(toArticol.valctva * m.lnProcTvav_19_119, gnPc)
			toArticol.valftva = toArticol.valctva - toArticol.valtva

			toArticol.valdiscountctva = Round(Round(toArticol.discount_unitar_ctva_val * toArticol.Curs / toArticol.multiplicator, gnPPretV) * toArticol.cantitate, gnPc)
			*!*			toArticol.valdiscountftva = Round(Round(toArticol.discount_unitar_val * toArticol.Curs,gnPc) * toArticol.cantitate,gnPc)
			*!*			toArticol.valdiscounttva = toArticol.valdiscountctva - toArticol.valdiscountftva
			toArticol.valdiscounttva = Round(toArticol.valdiscountctva * m.lnProcTvav_19_119, gnPc)
			toArticol.valdiscountftva = toArticol.valdiscountctva - toArticol.valdiscounttva

			toArticol.valdiminuatctva = Round((Round(toArticol.Pretctva_val * toArticol.Curs / toArticol.multiplicator, gnPPretV) - Round(toArticol.discount_unitar_ctva_val * toArticol.Curs / toArticol.multiplicator, gnPPretV)) * toArticol.cantitate, gnPc)
			*!*			toArticol.valdiminuatftva = Round(Round((Round(toArticol.Pretctva_val*toArticol.Curs,gnPc) - ROUND(toArticol.discount_unitar_ctva_val*toArticol.Curs,gnPc)) / toArticol.proc_tvav,gnPc) * toArticol.cantitate, gnPc)
			*!*			toArticol.valdiminuattva = toArticol.valdiminuatctva - toArticol.valdiminuatftva
			toArticol.valdiminuattva = Round(toArticol.valdiminuatctva * m.lnProcTvav_19_119, gnPc)
			toArticol.valdiminuatftva = toArticol.valdiminuatctva - toArticol.valdiminuattva
		Endif
		*!* v 2.0.15 ^
	Endif

	If Type('toArticol.cu_tva')	<> 'U'
		toArticol.cu_tva = toArticol.preturi_cu_tva
		Removeproperty(toArticol, 'preturi_cu_tva')
		toArticol.discountftva = toArticol.discount_unitar
		Removeproperty(toArticol, 'discount_unitar')
		toArticol.discountctva = toArticol.discount_unitar_ctva
		Removeproperty(toArticol, 'discount_unitar_ctva')
		toArticol.vpretftva	= toArticol.Pretftva_val
		Removeproperty(toArticol, 'pretftva_val')
		toArticol.vpretctva = toArticol.Pretctva_val
		Removeproperty(toArticol, 'pretctva_val')
		toArticol.vdiscountftva = toArticol.discount_unitar_val
		Removeproperty(toArticol, 'discount_unitar_val')
		toArticol.vdiscountctva = toArticol.discount_unitar_ctva_val
		Removeproperty(toArticol, 'discount_unitar_ctva_val')
	Endif
	Release lnProcTvav_19_119
	Return toArticol
Endfunc
******************************************* SFARSIT: calculeaza_totaluri ******************************************* 