*!* 12.06.2014
*!* marius.mutu
*!* adaugat proprietatea oDateFactura.incasatCARD = suma incasata prin CARD POS

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

*!* 19.01.2015
*!* marius.mutu
*!* + proprietati: 	nIdTipDocFactura, nIdTipDocProforma, nIdTipDocBonFiscal, nIdTipDocAvizExpeditie

*!*	06.02.2015
*!*	marius.mutu
*!*	la schimbarea poDate.nIdTipDoc se initializeaza poDate.eBonFiscal, .eProforma in nIdTipDoc_Assign 

*!*	16.06.2015
*!*	marius.mutu
*!*	+ proprietati: 	oDateFactura.id_facturare, nListareDetaliata

*!* 30.03.2017
*!* marius.mutu
*!* prelucreaza_factura
*!* Explicatia articolului a fost marita la 500 de caractere in baza de date, tip Memo si nu poate fi folosita intr-un select group by
*!* Am selectat doar primele 240 caractere apoi completez explicatia din cursorul original

*!*	31.03.2017
*!*	marius.mutu
*!*	prelucreaza_lista_livrare - explicatie M, nu mergea grupata 

*!* 30.01.2019
*!* marius.mutu
*!* prelucreaza_factura, prelucreaza_recapitulatia - s-au tratat grupare dupa campul explicatie Memo - Cast(explicatie as C(254)) pentru facturare seturi

*!* 06.11.2019
*!* marius.mutu
*!* +oDateFactura.nIdBeneficiar, nTipFactura

*!*	20.12.2019
*!*	marius.mutu
*!*	+oDateFactura.nvalval, ntvaval, ntotval, cValuta, tip_factura - pentru listarea valorilor in valuta pe facturile in lei la ROAACNPRO

*!* 22.01.2020
*!* marius.mutu
*!* prelucreaza_factura, prelucreaza_factura_valuta + codmatc (codmat client)

*!* 22.07.2020
*!* marius.mutu
*!* prelucreaza_facturacrs, prelucreaza_factura_valuta: verificare camp codmatc

*!* 12.07.2024
*!*	marius.mutu
*!*	alege_optiuni_listare_facturare - se listeaza serii/lot? 

*!*	14.04.2026
*!*	+ oDateFactura.cod_fiscal

***************************************************************************************************************
****	Clase:
****		oDateFactura
****			init
****			initializeaza_politica_pret
****			initializeaza_setari_document
****			completeaza_setari_document
****			genereaza_numar_act
****			Reset
****			scrieAtasamente
****		oDateGestiune
****			Reset
****	Functii:
****		actualizeaza_document
****		actualizeaza_partener_fact
****		actualizeaza_politica_pret
****		optiuni_format_listare			&&	denumire veche : optiuni_format_factura
****		recalculeaza_pret_vanzare_lei
****		recalculeaza_pret_vanzare_compus
****	Proceduri:
****		modifica_pret_articol_compus
****		prelucreaza_aviz_cu_tva
****		prelucreaza_factura
****		prelucreaza_recapitulatie
****		prelucreaza_factura_valuta
****		prelucreaza_aviz_retur
****		prelucreaza_lista_livrare
****		creeaza_crsfactura
****		creeaza_crsfactura_rate			&&	veche
****		copiaza_structura_cursor
****		creeaza_backup_cursoare_facturare
****		repune_backup_cursoare_facturare
****		sterge_backup_cursoare_facturare
****		alege_optiuni_listare_facturare
****		alege_banci_facturare
****		completeaza_explicatie_tva
****		completeaza_explicatie_contract
***************************************************************************************************************
******************************************** INCEPUT: oDateFactura ********************************************
#define precizie_max_pret_vz	6

Define Class oDateFactura As Custom
	Data			=	{}
	dataireg 		=	{}
	nIdTipDoc		= 	5
	nIdTipDocFactura		=	5
	nIdTipDocProforma		=	23
	nIdTipDocBonFiscal		=	3
	nIdTipDocAvizExpeditie	=	6
	id_fdoc			=	Null
	fdoc			=	[]
	dataact			=	{}
	datascad		=	{}
	nract			=	0
	serie_act		=	[]
	nract_retur		=	0
	serie_act_retur	=	[]
	id_client		=	Null
	nume_client		=	[]
	cod_fiscal		= 	[]
	listaid			=	[]		&&	id_comanda sau lista id_aviz
	descriere		=	[]		&&	numar comanda sau numere avize
	id_venchelt		=	Null	&&	modificare	ROAFACTURARE v 2.0.46
	venchelt		=	[]		&&	modificare	ROAFACTURARE v 2.0.46
	id_responsabil	=	Null
	responsabil		=	[]
	id_set			=	0
	tip				=	0
	id_pol			=	Null
	nume_politica	=	[]
	nume_responsabil=	[]		&&	alte date pentru facturare
	BIresp			=	[]
	CNPresp			=	[]
	id_delegat		=	Null
	nume_delegat	=	[]
	BIdelegat		=	[]
	CNPdelegat		=	[]
	id_masina		=	Null
	nrinmat			=	[]
	dataora_exp		=	{}		&&	modificare ROAFACTURARE v 2.0.80
	id_agent		=	Null
	nume_agent		=	[]
	proc_tva		=	0
	explicatie_tva	=	[]		&&	modificare	ROAFACTURARE v 2.0.46
	zi_curs			=	{}
	discount_evidentiat =	0		&&	daca se evidentiaza discount-ul sau este inclus in pret
	afisare_scadenta	=	1		&&	modificare ROACONTRACTE v 2.0.38
	text_aditional	=	[]
	text_aditional_invoice	=	[]	&&	modificare ROAFACTURARE	v 2.0.46
	text_aditional_retur = []
	text_aditional_en = []		&&	modificare ROAFACTURARE v 2.0.80
	eProforma 		= 	0
	nEFactura		=   0 && xml eFactura
	oInfoEFactura	= 	Null && obiect cu infoe eFactura rezultat in urma listarii (cFile, lOk, cMesaj)
	eBonFiscal		=	0
	serie_chit		=	[]		&&	modificare ROAFACTURARE v 2.0.80
	explicatie_chit = 	[]		&&	modificare ROAFACTURARE v 2.0.80
	nr_incasare		=	0
	id_fdoc_incasare=	0
	ntip_incasare	=	0		&&	modificare ROAFACTURARE v 2.0.84
	id_casa			=	0
	incasat			=	0		&& incasat numerar
	incasatCARD		=	0		&& incasat prin CARD POS
	nIncasatPos		=	0		&& 1 s-a ales bon fiscal prin CARD POS
	nBonDetaliat	=	0		&& 1 se listeaza articole individuale; 0 se listeaza "contravaloare factura nr..."
	incasatTichete	=	0		&& incasat tichete
	totalctva 		=	0
	in_valuta		=	0
	id_valuta		=	0
	nume_valuta		=	[]
	Curs			=	0
	multiplicator	=	1		&&	modificare ROAFACTURARE v 2.0.90
	id_lucrare		=	0
	nrord			=	[]
	id_sectie		=	0
	sectie			=	[]
	totctvaconv		=	0
	totftvaconv		=	0
	tottvaconv		=	0
	nvalval			=	0
	ntvaval			=	0
	ntotval			=	0
	cValuta			=	[]
	rezultat_serii	=	0
	listare_componenta_set		= 1
	listare_componenta_set_ll 	= 1	&&	modificare	ROAFACTURARE v 2.0.56
	listare_serii				= 1		&&	modificare	ROAFACTURARE v 2.0.46
	listare_recapitulatie		= 1		&&	modificare	ROAFACTURARE v 2.0.46
	listare_lista_livrare		= 1		&&	modificare	ROAFACTURARE v 2.0.46
	id_furnizor		= 0     && id_furnizor - imi trebuie la aviz pe baza de NIR
	furnizor 		= []    && nume furnizor - imi trebuie la aviz pe baza de NIR
	nrfactachi		= 0		&& numar factura achizitie - imi trebuie la aviz pe baza de NIR
	id_part_rez		=	Null
	id_gestiune_init = Null	&&	gestiunea aleasa in formularul de introducere a datelor facturii / avizului
	cDocAtasate			=   [crsoDateDocAtasate]&&	modificare	ROAFACTURARE v 2.0.58
	nRelistare			=	0					&&	modificare	ROAFACTURARE v 2.0.58
	nSalveazaAtasamente	=	0					&&	modificare	ROAFACTURARE v 2.0.58
	nid_proforma		=	9999999999			&&	modificare	ROAFACTURARE v 2.0.58
	nid_vanzare			=	9999999999			&&	modificare	ROAFACTURARE v 2.0.58
	nid_vanzare_retur	=	9999999999			&&	modificare	ROAFACTURARE v 2.0.58
	sumavorbe		=	[]	&&	modificare ROAFACTURARE v 2.0.78
	coeficient_k	=	0	&&	modificare ROAFACTURARE v 2.0.93
	explicatia4		=	[]	&&	modificare ROACONTRACTE v 2.0.42
	id_ordl			=	NULL	&&	modificare	ROAAUTO v 2.0.33
	tva_incasare	=	0	&&	modificare ROAGEST v 2.2.0
	id_facturare	= 	NULL && id adresa facturare
	adresa_facturare	= '' && adresa factuare text
	nListareDetaliata = 0	&& articolele se listeaza cumulat dupa cantitate sau detaliat, asa cum au fost adaugate pe factura
	nListareSilentioasa = 0 && listare fara previzualizare
	nListareJustPDF = 0 && listare doar pdf
	oClient = NULL
	lCopiere = .F. && copiere factura/aviz
	nIdBeneficiar	=	Null
	nTipFactura		= 	NULL && tip intern factura (ex: ACN 0=TRANZIT/1=CHEIAJ/3=CHIRII/5=APA/6=PENALITATI)
	id_util			= NULL && id utilizator 
	tip_doc_394     = 'F' && FACTURA pentru xml EFactura
	nRectificativa	= 0 && 0 = factura initiala, 1 = factura rectificativa
	tip_saft		= 380 && factura initiala
	institutie_publica	= 0 && 1 = clientul este institutie publica, pentru folosirea coduri articole CPV sau NC8
	id_ctr = NULL && ID CONTRACT
	contract = NULL && CONTNRACT
	eFactura = 0 && 0/1 = se transmite in borderoul eFactura
	sold_lei = 0 && soldul total pe client la emiterea facturii in lei
	sold_valuta = 0 && soldul total pe client la emiterea facturii in valuta (nediferentiat pe valuta)
	
	Procedure Init
		Lparameters tnIdSet, tnTip
		&&	0,0			-	la listare
		&&	id_set,tip
		
		Local ldData
		If !Empty(m.tnIdSet)
			&&	modificare	ROAFACTURARE v 2.0.58
			Use In (SELECT('crsoDateDocAtasate'))

			Create Cursor crsoDateDocAtasate (nume_frx c(50),fisier w)
			&&	modificare	ROAFACTURARE v 2.0.58	^
			ldData				=	Ttod(get_ora())
			If Month(ldData)<>gnLuna Or Year(ldData)<>gnAn
				ldData=Gomonth(Date(gnAn,gnLuna,1),1)-1
			Endif
			With This
				.Data			=	ldData
				.dataireg 		=	ldData
				.dataact		=	ldData
				.tva_incasare	=	goCalendar.tva_incasare		&&	modificare ROAFACTURARE v 2.2.0
				If (tnTip<21) OR INLIST(tnTip,45,48,49,51,52)		&&	modificare ROAFACTURARE v 2.0.87 ( 45 ), v 2.0.93 ( 48 ), v 2.0.95 ( 49 )
					.datascad		=	IIF(TYPE('gnZileScadentaFact') = 'N', Iif(gnZileScadentaFact=-1,Gomonth(ldData,1),ldData+gnZileScadentaFact), GOMONTH(m.ldData,1))
				Endif
				.zi_curs		=	ldData
				If Inlist(tnIdSet,223,225,226,227,271) OR INLIST(m.tnTip, 5,6,7,9,10,52) && ,25014,25015,25016,25018,25019,25004,25005,25006,25008,25009,25051,25061)
					.in_valuta = 1
				Endif
				.id_set			=	tnIdSet
				.tip			=	tnTip
				.initializeaza_setari_document()
				If Inlist(tnTip,23,30,41)
					.initializeaza_politica_pret()
				Endif
				*!*				.genereaza_numar_act()
				.discount_evidentiat =	IIF(TYPE('gnDiscountEvidentiat') = 'N', m.gnDiscountEvidentiat, 1)
				.nRelistare		=	0		&&	modificare	ROAFACTURARE v 2.0.58

				If INLIST(m.tnTip, 2, 6, 52) And Type('goContract') <> 'U' && FACTURARE DIN ROACONTRACTE
					.id_client = goContract.id_part
					.nume_client = goContract.denumire
					.listaid = goContract.id_ctr
					.descriere = goContract.contract
					.id_sectie = goContract.id_sectie
					.sectie = goContract.sectie
					.id_responsabil = goContract.id_responsabil
					.responsabil = goContract.responsabil
					.id_valuta = goContract.id_valuta
					.nume_valuta = goContract.nume_val

					lcSql = [ select id_ctr, contract, numar, data, denumire, scadenta_incasare, opt_facturare, text_standard, afisare_scadenta  ]+;
						[ FROM fact_vcontracte  ]+;
						[ where id_ctr = ] + Alltrim(Str(goContract.id_ctr))
					lnSucces = goExecutor.oExecute(lcSql,'crsCtrTemp')

					If lnSucces < 0
						AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
						Return
					*!*	modificare ROACONTRACTE	 v 2.0.35
					Else
						lcAlias = SELECT()
						Select crsctrtemp
						Calculate Min(scadenta_incasare),MAX(afisare_scadenta) For Inlist(opt_facturare,3,4,0) To lnScadentaIncasare,lnAfisareScadenta
						If lnScadentaIncasare <> 0
							.datascad = .dataact + lnScadentaIncasare
						Endif
						&&	modificare ROACONTRACTE v 2.0.38
						.afisare_scadenta = lnAfisareScadenta
						&&	modificare ROACONTRACTE	v 2.0.38	^
						Go Top
						Select (lcAlias)
					*!*	modificare ROACONTRACTE	 v 2.0.35	^
					ENDIF && lnSucces < 0
				ENDIF && tnTip = 2 And Type('goContract') <> 'U' && FACTURARE DIN ROACONTRACTE
				
				

				If tnTip = 3 And Type('goComanda') = 'O' && FACTURARE DIN ROACOMENZI
					.id_client = goComanda.id_part
					.nume_client = ALLTRIM(goComanda.nume)
					.cod_fiscal = ALLTRIM(NVL(goComanda.cod_fiscal, ''))
					.listaid = goComanda.id_comanda
					.descriere = goComanda.nr_comanda
					.id_sectie = goComanda.id_sectie
*!*						.sectie = goComanda.sectie
*!*						.id_responsabil = goComanda.id_responsabil
*!*						.responsabil = goComanda.responsabil

					lcSql = [ select sectie from nom_sectii where id_sectie = ] + ALLTRIM(STR(goComanda.id_sectie))
					lnSucces = goExecutor.oExecute(lcSql,'crsComTemp')

					If lnSucces < 0
						AMESSAGEBOX(goExecutor.cEroare,0+16,"Eroare")
						Return
					Else
						lcAlias = SELECT()
						IF RECCOUNT('crsComTemp') > 0
							GO TOP IN crsComTemp
							.sectie = ALLTRIM(crsComTemp.sectie)
						ENDIF 
						USE IN (SELECT('crsComTemp'))
						
						Select (lcAlias)
					ENDIF && lnSucces < 0
				ENDIF && tnTip = 3 And Type('goComanda') = 'O'
								
			Endwith
		Endif
	Endproc

	Procedure initializeaza_politica_pret
		Local lnIdPol,lcNumePol
		lnIdPol = 0
		lcNumePol = []
		actualizeaza_politica_pret(This.tip,@lnIdPol,@lcNumePol)
		With This
			.id_pol = lnIdPol
			.nume_politica = lcNumePol
		Endwith
	Endproc

	Procedure initializeaza_setari_document
		LPARAMETERS tnTip
		Local lnIdFdoc, lcFdoc, lnTip
		lnIdFdoc = 0
		lcFdoc = []

		* vreau sa ReInitializez id_fdoc, daca schimb tipul documentului FACTURA/PROFORMA/BON FISCAL
		* BON FISCAL = -101, PROFORMA = -102
		lnTip = IIF(EMPTY(m.tnTip), This.tip, m.tnTip)	
		actualizeaza_document(m.lnTip,@lnIdFdoc,@lcFdoc)
		With This
			.id_fdoc = lnIdFdoc
			.fdoc = lcFdoc
		Endwith
	Endproc

	*** Se apeleaza in functie de optiunea 
	Procedure completeaza_setari_document
		LPARAMETERS toDateAnterior, tlFactura
		* tlFactura: .T. daca copiez o factura si am nevoie de id-ul facturii anterioare, iar toDateAnterior = toFactura

		IF TYPE('toDateAnterior') = 'O' AND !ISNULL(toDateAnterior) AND PEMSTATUS(toDateAnterior,'id_lucrare', 5)
			With This
			IF m.tlFactura
				.lCopiere = .T.
				*.nIdTipDoc = toDateAnterior.nIdTipDoc
				.id_lucrare = toDateAnterior.id_lucrare
				.nrord = ALLTRIM(NVL(toDateAnterior.lucrare, ''))
				*.id_responsabil = toDateAnterior.id_responsabil 
				*.responsabil = toDateAnterior.responsabil 	
				.id_sectie = toDateAnterior.id_sectie
				.sectie = ALLTRIM(NVL(toDateAnterior.sectie, ''))
				*.id_venchelt = toDateAnterior.id_venchelt
				*.venchelt = toDateAnterior.venchelt 				
				.id_agent = toDateAnterior.id_agent 
				.nume_agent = ALLTRIM(NVL(toDateAnterior.nume_agent , ''))
				.id_delegat = toDateAnterior.id_delegat
				.nume_delegat = ALLTRIM(NVL(toDateAnterior.delegat, ''))
				.BIdelegat = toDateAnterior.BIdelegat
				.CNPdelegat =	toDateAnterior.CNPdelegat
				.nrinmat = ALLTRIM(NVL(toDateAnterior.nrinmat, ''))
				.id_masina = toDateAnterior.id_masina
				.listaid = toDateAnterior.id_vanzare
				.descriere = Alltrim(Alltrim(Nvl(toDateAnterior.serie_act,[]))+[ ]+Alltrim(Str(toDateAnterior.numar_act,14,0)))
				.id_client = toDateAnterior.id_part
				.nume_client = toDateAnterior.client
			ELSE
				.nIdTipDoc = toDateAnterior.nIdTipDoc		
				.id_lucrare = toDateAnterior.id_lucrare
				.nrord = toDateAnterior.nrord
				.id_responsabil = toDateAnterior.id_responsabil 
				.responsabil = toDateAnterior.responsabil 	
				.id_sectie = toDateAnterior.id_sectie
				.sectie = toDateAnterior.sectie							
				.id_venchelt = toDateAnterior.id_venchelt
				.venchelt = toDateAnterior.venchelt 				
				.id_agent = toDateAnterior.id_agent 
				.nume_agent = toDateAnterior.nume_agent 
				.id_delegat = toDateAnterior.id_delegat
				.nume_delegat = toDateAnterior.nume_delegat
				.BIdelegat = toDateAnterior.BIdelegat
				.CNPdelegat =	toDateAnterior.CNPdelegat
				.nrinmat = toDateAnterior.nrinmat
				.id_masina = toDateAnterior.id_masina			
			ENDIF 
			ENDWITH			
		ENDIF 
	ENDPROC
	
	*!*		Procedure genereaza_numar_act
	*!*		Private pnTip,pnNumarAct
	*!*		With This
	*!*			pnTip = .tip
	*!*			pnNumarAct = 0
	*!*			lcSql = [begin ] + gcS + [.pack_facturare.genereaza_numar_act(?pnTip,?gnIdUtil,?@pnNumarAct);] + ;
	*!*				[end;]
	*!*			lnSucces = goExecutor.oExecute(lcSql)
	*!*			If lnSucces<0
	*!*				amessagebox(goExecutor.oPrelucrareEroare(),16,"Eroare")
	*!*			Endif
	*!*			.nract = pnNumarAct
	*!*		Endwith
	*!*		Endproc

	Procedure scrieAtasamente
		Private pnId,pnTip,pcFormat
		If Used('crsoDateDocAtasate') And Nvl(poDate.nid_vanzare,9999999999)<>9999999999
			If Reccount('crsoDateDocAtasate') > 0
				Select crsoDateDocAtasate
				Scan
					*!*					If poDate.tip = 4 And Like([AVIZ*],Upper(Alltrim(crsoDateDocAtasate.nume_frx)))
					*!*						lcSql = [begin pack_facturare.scrie_atasamente_factura(?poDate.nid_vanzare_retur,?crsoDateDocAtasate.nume_frx,?crsoDateDocAtasate.fisier); end;]
					*!*					Else
					*!*						lcSql = [begin pack_facturare.scrie_atasamente_factura(?poDate.nid_vanzare,?crsoDateDocAtasate.nume_frx,?crsoDateDocAtasate.fisier); end;]
					*!*					Endif
					lcNume_Frx = crsoDateDocAtasate.nume_frx
					If poDate.tip = 4 And Like([AVIZ*],Upper(Alltrim(lcNume_Frx)))
						pnId = poDate.nid_vanzare_retur
					Else
						pnId = poDate.nid_vanzare
					Endif

					Do Case
						Case Like([FACTURA_VAL*],Upper(Alltrim(lcNume_Frx)))
							pnTip= 2
						Case Like([FACTURA*],Upper(Alltrim(lcNume_Frx)))
							pnTip = 1
						Case Like([INVOICE*],Upper(Alltrim(lcNume_Frx)))
							pnTip = 3
						Case Like([RECAPITULATIE],Upper(Alltrim(lcNume_Frx)))
							pnTip = 4
						Case Like([AVIZ*],Upper(Alltrim(lcNume_Frx)))
							pnTip = 5
					Endcase

					If Like([*A5],Upper(Alltrim(lcNume_Frx)))
						pcFormat = [A5]
					Else
						pcFormat = [A4]
					Endif

					lcSql = [INSERT INTO ATASAMENTE_VANZARI(ID_VANZARE, TIP, FORMAT, DOCUMENT, ID_UTIL) VALUES (?pnId,?pnTip,?pcFormat,?crsoDateDocAtasate.fisier,?gnIdUtil);]
					lnSucces = goExecutor.oExecute(lcSql)
					If lnSucces < 0
						AMESSAGEBOX(goExecutor.cEroare,16,"Eroare")
					Endif
				Endscan
			Endif
			Use In crsoDateDocAtasate
		Endif
		Release pnId,pnTip,pcFormat
	Endproc

	Procedure Reset
		Lparameters tnTip
		&&	modificare	ROAFACTURARE v 2.0.58
		If Used('crsoDateDocAtasate')
			Use In crsoDateDocAtasate
		Endif
		Create Cursor crsoDateDocAtasate (nume_frx c(50),fisier w)
		&&	modificare	ROAFACTURARE v 2.0.58	^
		With This
			.dataireg 		=	.Data
			.dataact		=	.Data
			If (tnTip<21) OR INLIST(tnTip,45,48,49,51,52)		&&	modificare ROAFACTURARE v 2.0.93 ; v 2.0.95 ( 49 )
*!*	modificare	v 2.2.4
*!*					.datascad		=	Iif(gnScadentaAutomata=0,Gomonth(.Data,1),.Data+gnZileScadentaFact)
				.datascad		=	Iif(gnZileScadentaFact=-1,Gomonth(.Data,1),.Data+gnZileScadentaFact)
*!*	modificare	v 2.2.4	^
			ENDIF
			.nIdTipDoc		= 	5 && FACTURA
			.zi_curs		=	.Data
			.id_client		=	Null
			.nume_client	=	[]
			.listaid		=	[]		&&	id_comanda, id_contract sau lista id_aviz
			.descriere		=	[]		&&	numar comanda, numar contract sau numere avize
			.id_venchelt	=	Null	&&	modificare ROAFACTURARE	v 2.0.46
			.venchelt		=	[]		&&	modificare ROAFACTURARE	v 2.0.46
			.id_responsabil	=	Null
			.responsabil	=	[]
			.id_pol			=	Null
			.nume_politica	=	[]
			.nume_responsabil=	[]		&&	alte date pentru facturare
			.BIresp			=	[]
			.CNPresp		=	[]
			.id_delegat		=	Null
			.nume_delegat	=	[]
			.BIdelegat		=	[]
			.CNPdelegat		=	[]
			.id_masina		=	Null
			.nrinmat		=	[]
			.id_agent		=	Null
			.dataora_exp	=	{}		&&	modificare ROAFACTURARE v 2.0.80
			.nume_agent	=	[]
			.proc_tva		=	0
			.explicatie_tva	=	[]		&&	modificare ROAFACTURARE	v 2.0.46
			.discount_evidentiat =	gnDiscountEvidentiat
			.afisare_scadenta	=	1		&&	modificare ROACONTRACTE v 2.0.38			
			.text_aditional	=	[]
			.text_aditional_invoice	=	[]	&&	modificare ROAFACTURARE	v 2.0.46
			.text_aditional_retur = []
			.text_aditional_en = []		&&	modificare ROAFACTURARE v 2.0.80
			.eProforma 		=	0
			.nEFactura		=	0
			.oInfoEFactura	=	Null
			.eBonFiscal		=	0
			.serie_chit		=	[]		&&	modificare ROAFACTURARE v 2.0.80
			.explicatie_chit = 	[]		&&	modificare ROAFACTURARE v 2.0.80
			.nr_incasare		=	0
			.id_casa			= 	0
			.ntip_incasare		=	0	&&	modificare ROAFACTURARE v 2.0.84				
			.incasat			=	0	&& incasat numerar
			.incasatCARD		=	0	&& incasat prin CARD POS
			.nIncasatPos		=	0	&& 1 s-a ales bon fiscal prin CARD POS				
			.nBonDetaliat		=	0	&& 1 se listeaza articole individuale; 0 se listeaza "contravaloare factura nr..."			
			.incasatTichete		= 	0   && incasat tichete
			.totalctva			=	0
			.nract				=	0
			.serie_act			=	[]
			.nract_retur		=	0
			.serie_act_retur	=	[]
			.id_valuta			=	0
			.nume_valuta		=	[]
			.Curs				=	0
			.multiplicator		=	1
			.id_lucrare			=	0
			.nrord				=	[]
			.id_sectie			=	0
			.sectie				=	[]
			.totctvaconv		=	0
			.totftvaconv		=	0
			.tottvaconv			=	0
			.rezultat_serii		=	0
			.listare_componenta_set	= 1
			.listare_componenta_set_ll	=	1	&&	modificare ROAFACTURARE	v 2.0.56
			.id_part_rez		=	Null
			.id_gestiune_init	=	Null
			.listare_serii			= 1		&&	modificare ROAFACTURARE	v 2.0.46
			.listare_recapitulatie	= 1		&&	modificare ROAFACTURARE	v 2.0.46
			.listare_lista_livrare	= 1		&&	modificare ROAFACTURARE	v 2.0.46
			.nRelistare		=	0			&&	modificare	ROAFACTURARE v 2.0.58
			.nSalveazaAtasamente	=	0	&&	modificare	ROAFACTURARE v 2.0.58
			.nid_proforma			=	9999999999	&&	modificare	ROAFACTURARE v 2.0.58
			.nid_vanzare			=	9999999999	&&	modificare	ROAFACTURARE v 2.0.58
			.nid_vanzare_retur		=	9999999999	&&	modificare	ROAFACTURARE v 2.0.58
			.sumavorbe			=	[]	&&	modificare ROAFACTURARE v 2.0.78
			.coeficient_k		=	0	&&	modificare ROAFACTURARE v 2.0.93
			.explicatia4		=	[]	&&	modificare ROACONTRACTE v 2.0.42
			.id_ordl			=	NULL	&&	modificare	ROAAUTO v 2.0.33
			.id_facturare		=	NULL
			.nListareDetaliata	=	0
			.nListareSilentioasa = 0 && listare fara previzualizare
			.nListareJustPDF = 0 && listare doar pdf
			.oClient = NULL		
			.lCopiere = .F. && copiere factura/aviz
			.nIdBeneficiar	=	Null
			.nTipFactura		= 	NULL && tip intern factura (ex: ACN 0=TRANZIT/1=CHEIAJ/3=CHIRII/5=APA/6=PENALITATI)
			.id_util			= NULL && id utilizator 
			.tip_doc_394     = 'F' && FACTURA pentru xml EFactura
			.nRectificativa	= 0 && 0 = factura initiala, 1 = factura rectificativa				
			.tip_saft		= 380 && factura initiala
			.institutie_publica	= 0 && 1 = institutie publica
			.eFactura = 0 && 1 = se transmite in borderoul eFactura
			.id_ctr = NULL && ID CONTRACT
			.contract = NULL && CONTRACT
		Endwith
	Endproc

	PROCEDURE nIdTipDoc_Assign 
		LPARAMETERS tnIdTipDoc
      
		This.eProforma  = IIF(m.tnIdTipDoc = This.nIdTipDocProforma, 1, 0)
		This.eBonFiscal  = IIF(m.tnIdTipDoc = This.nIdTipDocBonFiscal, 1, 0)
		
		this.nIdTipDoc = m.tnIdTipDoc
   ENDPROC && nIdTipDoc_Assign
   
Enddefine
******************************************** SFARSIT: oDateFactura ********************************************
******************************************** INCEPUT: oDateGestiune ********************************************
Define Class oDateGestiune As Custom
	id_gestiune		=	0
	id_tipgest		=	0
	Cont			=	[]
	acont			=	[]

	Procedure Reset
		With This
			.id_gestiune	=	0
			.id_tipgest		=	0
			.Cont			=	[]
			.acont			=	[]
		Endwith
Enddefine
******************************************** SFARSIT: oDateGestiune ********************************************
************************************** INCEPUT: actualizeaza_document ******************************************
Function actualizeaza_document
	Lparameters tnTip,tnIdFdoc,tcFdoc
	Local llReturn
	Private pnTip,pnIdFdoc,pcFdoc
	Store .T. To llReturn
	pnTip = tnTip
	pnIdFdoc = tnIdFdoc
	pcFdoc = tcFdoc
	lcSql = [begin ] + gcS + [.pack_facturare.citeste_setari_document(?pnTip,?@pnIdFdoc,?@pcFdoc);] + ;
		[end;]
	lnSucces = goExecutor.oExecute(lcSql)
	If lnSucces<0
		AMESSAGEBOX(goExecutor.oPrelucrareEroare(),16,"Eroare")
		pnIdFdoc = Null
		pcFdoc = []
		llReturn = .F.
	Endif
	tnIdFdoc = pnIdFdoc
	tcFdoc = pcFdoc
	Return llReturn
Endfunc
************************************** SFARSIT: actualizeaza_document *****************************************
Function actualizeaza_partener_fact
	Lparameters tnTip,tnIdPart,tcNumePart
	Local llReturn
	Private pnTip,pnIdPart,pcNumePart
	Store .T. To llReturn
	pnTip = tnTip
	pnIdPart = tnIdPart
	pcNumePart = tcNumePart
	lcSql = [begin ] + gcS + [.pack_facturare.citeste_setari_partener(?pnTip,?@pnIdPart,?@pcNumePart);] + ;
		[end;]
	lnSucces = goExecutor.oExecute(lcSql)
	If lnSucces<0
		AMESSAGEBOX(goExecutor.oPrelucrareEroare(),16,"Eroare")
		pnIdPart = Null
		pcNumePart = []
		llReturn = .F.
	Endif
	tnIdPart = pnIdPart
	tcNumePart = pcNumePart
	Return llReturn
Endfunc
************************************ SFARSIT: actualizeaza_partener_fact ***************************************
Function actualizeaza_politica_pret
	Lparameters tnTip,tnIdPol,tcNumePol
	Local llReturn
	Private pnTip,pnIdPol,pcNumePol
	Store .T. To llReturn
	pnTip = tnTip
	pnIdPol = tnIdPol
	pcNumePol = tcNumePol
	lcSql = [begin ] + gcS + [.pack_facturare.citeste_setari_pol_pret(?pnTip,?gnIdUtil,?@pnIdPol,?@pcNumePol);] + ;
		[end;]
	lnSucces = goExecutor.oExecute(lcSql)
	If lnSucces<0
		AMESSAGEBOX(goExecutor.oPrelucrareEroare(),16,"Eroare")
		pnIdPol = Null
		pcNumePol = []
		llReturn = .F.
	Endif
	tnIdPol = pnIdPol
	tcNumePol = pcNumePol
	Return llReturn
Endfunc
************************************ SFARSIT: actualizeaza_politica_pret ***************************************
*********************************************************************************************************
Function recalculeaza_pret_vanzare_lei
	Lparameters tcTabel,tnCurs
	Private pcSirIdTemp,pcSirDiferenta
	Store [] To pcSirIdTemp,pcSirDiferenta
	Local llReturn
	llReturn = .T.
	lcSeparator = [|]

	Select (tcTabel)
	Calculate Sum(Round(vpretftva*cantitate,gnPVal)),Sum(Round(pretftva*cantitate,gnPc)) To lnTotalVal,lnTotalLei
	lnTotalVal = Round(lnTotalVal*tnCurs,gnPc)

	*!*	modificare	09.11.2007
	*!*	If lnTotalVal <> lnTotalLei
	*!*		lnDiferenta = lnTotalVal - lnTotalLei
	*!*		lnSemn = Sign(lnDiferenta)
	*!*		lnDiferenta = Abs(lnDiferenta)
	*!*		lnDiferentaPretU = Exp((-1)*gnPc*Log(10))
	*!*		lnModificari = 1

	*!*		Do While lnModificari <> 0
	*!*			lnModificari = 0
	*!*			Select (tcTabel)
	*!*			Scan
	*!*				lnDiferentaPret = Round(lnDiferentaPretU*cantitate,gnPc)
	*!*				lnSemnPret = Sign(lnDiferentaPret)
	*!*				If Abs(lnDiferentaPret) <= Abs(lnDiferenta)
	*!*					lnDiferenta = lnDiferenta - lnSemn * lnDiferentaPret
	If lnTotalVal <> lnTotalLei
		lnDiferenta = lnTotalVal - lnTotalLei
		lnSemn = Sign(lnDiferenta)
		lnDiferentaMin = Abs(lnDiferenta)
		lnDiferentaPretU = Exp((-1)*gnPPretV*Log(10))
		lnModificari = 1

		Do While lnModificari <> 0
			lnModificari = 0
			Select (tcTabel)
			Scan
				lnDiferentaPret = Round(lnDiferentaPretU*cantitate,gnPC)
				lnSemnPret = Sign(cantitate)
				lnTestDiferenta = lnDiferenta - lnSemn * Abs(lnDiferentaPret)
				*!*				If Abs(lnDiferentaPret) <= Abs(lnDiferenta) And Abs(lnTestDiferenta)<lnDiferentaMin
				If Abs(lnTestDiferenta)<lnDiferentaMin
					lnDiferentaMin = Abs(lnTestDiferenta)
					lnDiferenta = lnTestDiferenta
					*!*	modificare	09.11.2007	^
					lnPretNou = pretftva + lnSemn * lnSemnPret * lnDiferentaPretU
					pcSirIdTemp = pcSirIdTemp + Alltrim(Str(id_temp)) + lcSeparator
					pcSirDiferenta = pcSirDiferenta + Alltrim(Str(lnSemn * lnSemnPret * lnDiferentaPretU,14,gnPPretV)) + lcSeparator
					Replace pretftva With lnPretNou,;
						pretctva With lnPretNou + Round(lnPretNou*(proc_Tvav-1),gnPPretV),;
						valftva With Round(lnPretNou * cantitate,gnPc),;
						valtva With Round(Round(lnPretNou * cantitate,gnPc)*(proc_Tvav-1),gnPc),;
						valctva With valftva + valtva,;
						valdiminuatftva With Round((lnPretNou-discountftva)*cantitate,gnPc),;
						valdiminuattva With Round(Round((lnPretNou-discountftva)*cantitate,gnPc)*(proc_Tvav-1),gnPc),;
						valdiminuatctva With valdiminuatftva + valdiminuattva
					lnModificari = lnModificari + 1
				Endif
			Endscan
		Enddo
	Endif

	If !Empty(pcSirIdTemp)
		lcSql = [begin ] + gcS + [.pack_facturare.adauga_diferente_pret(?pcSirIdTemp,?pcSirDiferenta,?gnIdUtil); end;]
		lnSucces = goExecutor.oExecute(lcSql)
		If lnSucces<0
			AMESSAGEBOX(goExecutor.oPrelucrareEroare(),16,"Eroare")
			llReturn = .F.
		Endif
	Endif

	Return llReturn
Endfunc	&&	recalculeaza_pret_vanzare_lei
*********************************************************************************************************
*********************************************************************************************************
*!*	Function recalculeaza_pret_vanzare_lei_set
*!*	Lparameters tcTabel,tnCurs
*!*	Local llReturn
*!*	llReturn = .T.
*!*	lcSeparator = [|]

*!*	Select (tcTabel)
*!*	Calculate Sum(Round(vpretftva*cantitate,gnPc)),Sum(Round(pretftva*cantitate,gnPc)) To lnTotalVal,lnTotalLei
*!*	lnTotalVal = Round(lnTotalVal*tnCurs,gnPc)

*!*	If lnTotalVal <> lnTotalLei
*!*		lnDiferenta = lnTotalVal - lnTotalLei
*!*		lnSemn = Sign(lnDiferenta)
*!*		lnDiferentaMin = Abs(lnDiferenta)
*!*		lnDiferentaPretU = Exp((-1)*gnPc*Log(10))
*!*		lnModificari = 1

*!*		Do While lnModificari <> 0
*!*			lnModificari = 0
*!*			Select (tcTabel)
*!*			Scan
*!*				lnDiferentaPret = Round(lnDiferentaPretU*cantitate,gnPc)
*!*				lnSemnPret = Sign(cantitate)
*!*				lnTestDiferenta = lnDiferenta - lnSemn * Abs(lnDiferentaPret)
*!*	*!*				If Abs(lnDiferentaPret) <= Abs(lnDiferenta) And Abs(lnTestDiferenta)<lnDiferentaMin
*!*				If Abs(lnTestDiferenta)<lnDiferentaMin
*!*					lnDiferentaMin = Abs(lnTestDiferenta)
*!*					lnDiferenta = lnTestDiferenta
*!*					lnPretNou = pretftva + lnSemn * lnSemnPret * lnDiferentaPretU
*!*					Replace pretftva With lnPretNou,;
*!*						pretctva With lnPretNou + Round(lnPretNou*(proc_Tvav-1),gnPc),;
*!*						valftva With Round(lnPretNou * cantitate,gnPc),;
*!*						valtva With Round(Round(lnPretNou * cantitate,gnPc)*(proc_Tvav-1),gnPc),;
*!*						valctva With valftva + valtva,;
*!*						valdiminuatftva With Round((lnPretNou-discountftva)*cantitate,gnPc),;
*!*						valdiminuattva With Round(Round((lnPretNou-discountftva)*cantitate,gnPc)*(proc_Tvav-1),gnPc),;
*!*						valdiminuatctva With valdiminuatftva + valdiminuattva
*!*					lnModificari = lnModificari + 1
*!*				Endif
*!*			Endscan
*!*		Enddo
*!*	Endif

*!*	Return llReturn
*!*	Endfunc	&&	recalculeaza_pret_vanzare_lei_set
*********************************************************************************************************
Function recalculeaza_pret_vanzare_compus
	Local lnIdSetFact,lnDiferenta,lnSemn,lnDiferentaMin,lnDiferentaPretU,lnModificari,lnCantitateSet,llReturn,lnPreciziePret,lnPrecizieValoare
	Local lnTotalSet,lnTotal
	Store .T. To llReturn
	If Used('crsxtemp')
		Use In crsxtemp
	Endif

	If Used('crsfacturaset') And Reccount('crsfacturaset')>0
		lnPreciziePret = iif(poDate.in_valuta=0,gnPPretV,gnPVal)
		lnPrecizieValoare = iif(poDate.in_valuta=0,gnPc,gnPVal)
		lnDiferentaPretU = Exp((-1)*lnPreciziePret*Log(10))

		Create Cursor crsxtemp(id_temp N(20),difunit N(18,max(lnPreciziePret,4)),tip_valuta N(1),Curs N(10,max(gnPCurs,4)),multiplicator N(10),;
			cantitate N(18,max(gnPCant,4)),pret N(18,max(lnPreciziePret,4)),id_set_fact N(20))

		*!*	modificare ROAFACTURARE	v 2.0.46
		*!*		Insert Into crsxtemp (id_c, difunit, tip_valuta, Curs, cantitate, Pret, id_set_fact) ;
		*!*			Select id_c,Round(Round(lnDiferentaPretU*Iif(poDate.in_valuta=0,Iif(tip_valuta=0,1,Curs),1),gnPPret)*cantitate,gnPc) As difunit,;
		*!*			tip_valuta,Curs,cantitate,Iif(tip_valuta=0,pretftva,vpretftva) As Pret,id_set_fact ;
		*!*			FROM crsfactura Where id_set_fact <> 0 Order By 2 Desc
		Insert Into crsxtemp (id_temp, difunit, tip_valuta, Curs, multiplicator, cantitate, Pret, id_set_fact) ;
			Select id_temp,Round(round(lnDiferentaPretU*Iif(poDate.in_valuta=0,Iif(tip_valuta=0,1,Curs/multiplicator),1),lnPreciziePret)*cantitate,lnPrecizieValoare) As difunit,;
			tip_valuta,Curs,multiplicator,cantitate,Iif(tip_valuta=0,pretftva,vpretftva) As Pret,id_set_fact ;
			FROM crsfactura Where id_set_fact <> 0 Order By 2 Desc
		*!*	modificare ROAFACTURARE	v 2.0.46	^

		Select crsfacturaset
		Scan
			lnIdSetFact = id_set_fact
			lnTotalSet = Iif(poDate.in_valuta=0,Round(pretftva*cantitate,lnPrecizieValoare),Round(vpretftva*cantitate,lnPrecizieValoare))
			lnCantitateSet = cantitate
			lcDenumire = Alltrim(denumire)
			lnPretInitialSet = Iif(poDate.in_valuta=0,pretftva,vpretftva)
			lnPretPropusSet = lnPretInitialSet
			*!*			lnDiferentaMin = Round(lnDiferentaPretU*cantitate,gnPc)

			Select crsfactura
			Calculate Sum(Round(Iif(poDate.in_valuta=0,Iif(tip_valuta=0,pretftva,Round(vpretftva*Curs/multiplicator,gnPc)),vpretftva)*cantitate,lnPrecizieValoare)) ;
				For id_set_fact = lnIdSetFact To lnTotal

			If lnTotalSet <> lnTotal
				lnDiferenta = lnTotalSet - lnTotal
				lnDiferentaInitiala = lnDiferenta
				lnSemn = Sign(lnDiferenta)
				lnDiferentaMin = Abs(lnDiferenta)
				*!*				lnDiferentaPretU = Exp((-1)*gnPc*Log(10))
				lnModificari = 1

				Do While lnModificari <> 0
					lnModificari = 0
					Select crsxtemp
					Scan For id_set_fact = lnIdSetFact
						lnTipValuta = tip_valuta
						lnCurs = Curs / multiplicator
						lnDiferentaPret = difunit
						lnSemnPret = Sign(cantitate)
						lnTestDiferenta = lnDiferenta - lnSemn * Abs(lnDiferentaPret)
						*!*						STRTOFILE(ALLTRIM(STR(lnDiferenta,10,gnPc))+[ >> ]+ALLTRIM(STR(lnDiferentaPret,10,gnPc))+CHR(13)+CHR(10),[c:\testfact.txt],1)
						*!*	modificare	ROAFACTURARE	v 2.0.46
						*!*						If Abs(lnDiferentaPret) <= Abs(lnDiferenta) And Abs(lnTestDiferenta)<lnDiferentaMin
						If Abs(Round(lnDiferentaPret,lnPrecizieValoare)) <= Abs(Round(lnDiferenta,lnPrecizieValoare)) And Abs(Round(lnTestDiferenta,lnPrecizieValoare))<lnDiferentaMin
							*!*	modificare	ROAFACTURARE	v 2.0.46	^
							*!*						If Abs(lnDiferentaPret) <= Abs(lnDiferenta) And Abs(lnTestDiferenta)>lnDiferentaMin
							lnDiferentaMin = Abs(lnTestDiferenta)
							lnDiferenta = lnTestDiferenta
							Replace Pret With Pret + lnSemn * lnSemnPret * lnDiferentaPretU
							*!*	modificare	ROAFACTURARE	v 2.0.46
							*!*							If Abs((lnDiferentaInitiala-lnDiferenta)/lnCantitateSet - Round((lnDiferentaInitiala-lnDiferenta)/lnCantitateSet,gnPc)) <= gnMarjaEroareRepartizare
							If Round(Abs((lnDiferentaInitiala-lnDiferenta)/lnCantitateSet - Round((lnDiferentaInitiala-lnDiferenta)/lnCantitateSet,lnPreciziePret)),lnPreciziePret) <= gnMarjaEroareRepartizare
								*!*	modificare	ROAFACTURARE	v 2.0.46	^
								lnPretPropusSet = lnPretInitialSet - lnSemn * lnSemnPret * ;
									Floor((lnDiferentaInitiala-lnDiferenta)/lnCantitateSet*Exp(lnPreciziePret*Log(10)))*Exp((-1)*lnPreciziePret*Log(10))
							Endif
							If lnDiferenta <> 0
								lnModificari = lnModificari + 1
							Else
								lnModificari = 0
								Exit
							Endif

						Endif
					Endscan
				Enddo

				*!*				amessagebox(Alltrim(Str(lnDiferenta,10,gnPc)))
				*!*	modificare	ROAFACTURARE	v 2.0.46
				*!*				If Abs(lnDiferenta)>gnMarjaEroareRepartizare
				If Abs(Round(lnDiferenta,lnPrecizieValoare))>gnMarjaEroareRepartizare
					*!*	modificare	ROAFACTURARE	v 2.0.46	^
					lnDiferentaUnitara = lnDiferenta/lnCantitateSet
					*!*				amessagebox(ALLTRIM(STR(lnDiferentaUnitara,10,gnPc))+[ ]+ALLTRIM(STR(lnDiferenta,10,gnPc)))
					If Round(lnDiferentaUnitara,lnPreciziePret)=lnDiferentaUnitara
						If AMESSAGEBOX("Nu s-a putut repartiza intreaga diferenta." + Chr(13) + Chr(10) + ;
								"Doriti sa "+Iif(lnDiferentaUnitara>0,[diminuati],[cresteti]) + ;
								" pretul unitar al articolului compus " + lcDenumire + ;
								" cu " + formateaza(Abs(lnDiferentaUnitara),14,lnPreciziePret) + " ?";
								,4+32,"Confirmare")==6
							Select crsfacturaset
							&&	se scade pretul setului cu lnDiferentaUnitara
							modifica_pret_articol_compus(1,lnDiferentaUnitara)
							llReturn = .T.
						Else
							llReturn = .F.
							Exit
						Endif
					Else
						If lnPretPropusSet <> lnPretInitialSet
							If AMESSAGEBOX("Doriti sa modificati pretul pentru " + ;
									lcDenumire + " din " + formateaza(lnPretInitialSet,18,lnPreciziePret) + ;
									" in " + formateaza(lnPretPropusSet,18,lnPreciziePret)+" ?",4+32,"Confirmare")=6
								modifica_pret_articol_compus(2,lnPretPropusSet)
								llReturn = .F.
							Else
								AMESSAGEBOX("Nu se poate repartiza diferenta de pret pentru articolele din componenta " + ;
									lcDenumire + " !",48,"Atentie")
								llReturn = .F.
								Exit
							Endif
						Else
							AMESSAGEBOX("Nu se poate repartiza diferenta de pret pentru articolele din componenta " + ;
								lcDenumire + " !",48,"Atentie")
							llReturn = .F.
							Exit
						Endif
					Endif
				Endif
			Endif
		Endscan
	Endif

	If Used('crsxtemp') And llReturn
		Select crsxtemp
		Scan
			*!*	modificare	ROAFACTURARE	v 2.0.46
			lnIdTemp	=	id_temp
			*!*			lnIdC = id_c
			*!*	modificare	ROAFACTURARE	v 2.0.46	^
			lnPretNou = Pret
			lnCurs = Curs / multiplicator
			Select crsfactura
			*!*	modificare	ROAFACTURARE	v 2.0.46
			Locate For id_temp = lnIdTemp
			*!*			Locate For id_c = lnIdC
			*!*	modificare	ROAFACTURARE	v 2.0.46	^
			If Found()
				lnTipValuta = tip_valuta
				If lnTipValuta = 0
					*!*	modificare	ROAFACTURARE	v 2.0.46
					If pretftva <> lnPretNou
						*!*	modificare	ROAFACTURARE	v 2.0.46	^
						Replace pretftva With lnPretNou,;
							pretctva With lnPretNou + Round(lnPretNou*(proc_Tvav-1),lnPreciziePret),;
							valftva With Round(lnPretNou * cantitate,lnPrecizieValoare),;
							valtva With Round(Round(lnPretNou * cantitate,lnPrecizieValoare)*(proc_Tvav-1),lnPrecizieValoare),;
							valctva With valftva + valtva,;
							valdiminuatftva With Round((lnPretNou-discountftva)*cantitate,lnPrecizieValoare),;
							valdiminuattva With Round(Round((lnPretNou-discountftva)*cantitate,lnPrecizieValoare)*(proc_Tvav-1),lnPrecizieValoare),;
							valdiminuatctva With valdiminuatftva + valdiminuattva
						*!*	modificare	ROAFACTURARE	v 2.0.46
					Endif
					*!*	modificare	ROAFACTURARE	v 2.0.46	^
				Else
					*!*	modificare	ROAFACTURARE	v 2.0.46
					If vpretftva <> lnPretNou
						*!*	modificare	ROAFACTURARE	v 2.0.46	^
						Replace vpretftva With lnPretNou,;
							vpretctva With lnPretNou + Round(lnPretNou*(proc_Tvav-1),lnPreciziePret),;
							vvalftva With Round(lnPretNou * cantitate,lnPrecizieValoare),;
							vvaltva With Round(Round(lnPretNou * cantitate,lnPrecizieValoare)*(proc_Tvav-1),lnPrecizieValoare),;
							vvalctva With vvalftva + vvaltva,;
							vvaldiminuatftva With Round((lnPretNou-vdiscountftva)*cantitate,lnPrecizieValoare),;
							vvaldiminuattva With Round(Round((lnPretNou-vdiscountftva)*cantitate,lnPrecizieValoare)*(proc_Tvav-1),lnPrecizieValoare),;
							vvaldiminuatctva With vvaldiminuatftva + vvaldiminuattva,;
							pretftva With Round(lnPretNou * lnCurs,gnPPretV),;
							pretctva With Round(lnPretNou * lnCurs,gnPPretV) + Round(Round(lnPretNou * lnCurs,gnPPretV)*(proc_Tvav-1),gnPPretV),;
							valftva With Round(Round(lnPretNou * lnCurs,gnPPretV) * cantitate,gnPc),;
							valtva With Round(Round(Round(lnPretNou * lnCurs,gnPPretV) * cantitate,gnPc)*(proc_Tvav-1),gnPc),;
							valctva With valftva + valtva,;
							valdiminuatftva With Round(Round((lnPretNou-vdiscountftva)*lnCurs,gnPPretV)*cantitate,gnPc),;
							valdiminuattva With Round(Round(Round((lnPretNou-vdiscountftva)*lnCurs,gnPPretV)*cantitate,gnPc)*(proc_Tvav-1),gnPc),;
							valdiminuatctva With valdiminuatftva + valdiminuattva
						*!*	modificare	ROAFACTURARE	v 2.0.46
					Endif
					*!*	modificare	ROAFACTURARE	v 2.0.46	^
				Endif
			Endif
			Select crsxtemp
		Endscan

		Use In crsxtemp
	Endif

	Return llReturn
Endfunc	&&	recalculeaza_pret_vanzare_compus
*********************************************************************************************************
Procedure modifica_pret_articol_compus
	Lparameters tnTip,tnValoare
	&&	tnTip	=	1	-	tnValoare este diferenta
	&&			=	2	-	tnValoare este pretul nou
	Local lnPretNou
	Select crsfacturaset
	If poDate.in_valuta = 0
		lnPretNou = Iif(tnTip=1,pretftva + (-1) * tnValoare,tnValoare)
		Replace pretftva With lnPretNou,;
			pretctva With lnPretNou + Round(lnPretNou * (proc_Tvav - 1),gnPPretV),;
			valftva With Round(lnPretNou * cantitate,gnPc),;
			valtva With Round(Round(lnPretNou * cantitate,gnPc)*(proc_Tvav-1),gnPc),;
			valctva With Round(lnPretNou * cantitate,gnPc) + Round(Round(lnPretNou * cantitate,gnPc)*(proc_Tvav-1),gnPc),;
			valdiminuatftva With Round((lnPretNou - valdiscountftva)*cantitate,gnPc),;
			valdiminuattva With Round(Round((lnPretNou - valdiscountftva)*cantitate,gnPc)*(proc_Tvav-1),gnPc),;
			valdiminuatctva With Round((lnPretNou - valdiscountftva)*cantitate,gnPc) + Round(Round((lnPretNou - valdiscountftva)*cantitate,gnPc)*(proc_Tvav-1),gnPc)

	Else
		lnPretNou = Iif(tnTip=1,vpretftva + (-1) * tnValoare,tnValoare)
		Replace vpretftva With lnPretNou,;
			vpretctva With lnPretNou + Round(lnPretNou * (proc_Tvav - 1), gnPVal),;
			vvalftva With Round(lnPretNou * cantitate,gnPVal),;
			vvaltva With Round(Round(lnPretNou * cantitate,gnPVal) * (proc_Tvav - 1),gnPVal),;
			vvalctva With Round(lnPretNou * cantitate,gnPVal) + Round(Round(lnPretNou * cantitate,gnPVal) * (proc_Tvav - 1),gnPVal),;
			vvaldiminuatftva With Round((lnPretNou - vdiscountftva) * cantitate,gnPVal),;
			vvaldiminuattva With Round(Round((lnPretNou - vdiscountftva) * cantitate,gnPVal) * (proc_Tvav - 1),gnPVal),;
			vvaldiminuatctva With Round((lnPretNou - vdiscountftva) * cantitate,gnPVal) + Round(Round((lnPretNou - vdiscountftva) * cantitate,gnPVal) * (proc_Tvav - 1),gnPVal),;
			pretftva With Round(lnPretNou*Curs/multiplicator,gnPPretV),;
			pretctva With Round(lnPretNou*Curs/multiplicator,gnPPretV) + Round(Round(lnPretNou*Curs/multiplicator,gnPPretV) * (proc_Tvav - 1), gnPPretV),;
			valftva With Round(Round(lnPretNou * Curs/multiplicator,gnPPretV) * cantitate,gnPc),;
			valtva With Round(Round(Round(lnPretNou * Curs/multiplicator,gnPPretV) * cantitate,gnPc) * (proc_Tvav - 1),gnPc),;
			valctva With Round(Round(lnPretNou * Curs/multiplicator,gnPPretV) * cantitate,gnPc) + Round(Round(Round(lnPretNou * Curs/multiplicator,gnPPretV) * cantitate,gnPc) * (proc_Tvav - 1),gnPc),;
			valdiminuatftva With Round(Round((lnPretNou - vdiscountftva)* Curs/multiplicator,gnPPretV) * cantitate,gnPc),;
			valdiminuattva With Round(Round(Round((lnPretNou - vdiscountftva)* Curs/multiplicator,gnPPretV) * cantitate,gnPc) * (proc_Tvav - 1),gnPc),;
			valdiminuatctva With Round(Round((lnPretNou - vdiscountftva)* Curs/multiplicator,gnPPretV) * cantitate,gnPc) + Round(Round(Round((lnPretNou - vdiscountftva)* Curs/multiplicator,gnPPretV) * cantitate,gnPc) * (proc_Tvav - 1),gnPc)
	Endif

Endproc	&&	modifica_pret_articol_compus
*********************************************************************************************************
*!*	Procedure prelucreaza_aviz_cu_tva
*!*	Lparameters tnDiscountEvidentiat
*!*	Create Cursor crsfacturafinala(denumire c(100),explicatie c(100),cantitate N(20,4),;
*!*		pretctva N(20,4),valctva N(20,4),proc_tva N(20,4),um c(10),serie c(100))
*!*	Select Recno() As nrcrt,a.denumire,a.explicatie,a.cantitate,a.pretctva,a.valctva,;
*!*		a.proc_tvav as proc_tva,Nvl(a.um,Space(10)) as um,Nvl(a.serie,' ') As serie From crsfactura a;
*!*		into Cursor crsfacturafinala
*!*	Endproc	&&	prelucreaza_aviz_cu_tva
*********************************************************************************************************
Procedure prelucreaza_factura
	*!*	modificare ROAFACTURARE v 2.0.67
	*!*	Lparameters tnDiscountEvidentiat,tnTipFacturare,tnInValuta,tnPretListSubunitati
	*!*	modificare ROAFACTURARE v 2.0.77 : am adaugat tcCursorSursa, tcCursorSursaSet,tcCursorDestinatie
	Lparameters tcCursorSursa,tcCursorSursaSet,tcCursorDestinatie,tnDiscountEvidentiat,tnInValuta,tnPretListAviz, tnListareDetaliata
	*!*	modificare ROAFACTURARE	v 2.0.67	^
	*!*	ii transmit tnPretListSubunitati ca parametru pentru ca sa nu mai testez si daca este vorba de o factura sau nu
	*!* tnListareDetaliata: daca sa se listeze randurile asa cum sunt in loc de grupate dupa articol, pret. sa nu se cumuleze cantitatea pe articole
	LOCAL llListareDetaliata, lnIdArticol
	llListareDetaliata = (IIF(TYPE('tnListareDetaliata') = 'N', tnListareDetaliata, 0) = 1)
	
	Local lcCursor,lnPretListAviz,lnIdTemp
	*!*	modificare	ROAFACTURARE	v 2.0.67	: am adaugat id_ctr, numar_contract
	*!*	modificare v 2.0.77
	creeaza_facturafinala(tcCursorDestinatie)
	*!*		Create Cursor crsfacturafinala(denumire c(100),explicatie c(240),cantitate N(20,4),pretctva N(20,4),valctva N(20,4),;
	*!*			pretftva N(20,4),pretftvai N(20,4),pretctvai N(20,4),proc_disc N(10,4),valftva N(20,4),valtva N(20,4),;
	*!*			valftvai N(20,4),valctvai N(20,4),valdiscount N(20,4),;
	*!*			proc_tva N(20,4),um c(10),serie c(100),codmat c(50),codbare c(50),codmatf c(50),;
	*!*			id_jtva_coloana N(10),expltva c(100),id_ctr N(10) Null,numar_contract c(100) Null)
	*!*	modificare	v 2.0.77	^
	If Used('crsfacttemp')
		Use In crsfacttemp
	Endif
	*!*	modificare v 2.0.4
	If Empty(tnPretListAviz)
		lnPretListAviz = 2
	Else
		lnPretListAviz = Nvl(tnPretListAviz,2)
	ENDIF

	*!*	modificare v 2.0.4	^
	If Empty(tcCursorSursaSet)
		lcCursor = tcCursorSursa
	Else
		If !Used(tcCursorSursaSet)
			lcCursor = tcCursorSursa
		Else
			Select (tcCursorSursaSet)
			lcTab = Chr(9)
			lcCrlf = Chr(13) + Chr(10)
			*!*	modificare	v 2.0.4
			If Used('crsfactstemp')
				Use In crsfactstemp
			Endif
			Select * From (tcCursorSursaSet) Into Cursor crsfactstemp
			Delete From (tcCursorSursaSet)
			Select crsfactstemp
			*!*	modificare	v 2.0.4	^
			Scan
				lnIdSetFact = id_set_fact
				lnCantitate = cantitate
				lnIdTemp = id_temp	&&	modificare ROAFACTURARE v 2.0.67

				Insert Into (tcCursorSursaSet) Select * From crsfactstemp Where id_set_fact = lnIdSetFact
				*!*	modificare  ROAFACTURARE v 2.0.67 : am pus lnIdTemp in loc de MIN(id_temp)
*!*					Insert Into (tcCursorSursaSet) (id_temp,denumire) ;
*!*						Select lnIdTemp As id_temp,lcTab+Alltrim(Alltrim(denumire)+lcTab+Alltrim(codmat)+lcTab+Alltrim(codmatf)+lcCrlf+Nvl(explicatie,[]))+Space(5)+;
*!*						Alltrim(Str(Round(Sum(cantitate)/lnCantitate,gnPCant),9,gnPCant))+[ ]+Alltrim(Nvl(um,[])) As denumire ;
*!*						From (tcCursorSursa) Where id_set_fact = lnIdSetFact Group By denumire,codmat,codmatf,explicatie,um
*!*	Nu merge gruparea dupa campul explicatie Memo. Initial era tip caracter
				select denumire,codmat,codmatc,codmatf,cast(explicatie as c(254)) as explicatie,um, Round(Sum(cantitate)/lnCantitate,gnPCant) as cant ;
					From (tcCursorSursa) ;
					Where id_set_fact = lnIdSetFact ;
					Group By 1,2,3,4,5,6 ;
					into cursor cSeturiTemporar

				Insert Into (tcCursorSursaSet) (id_temp,denumire) ;
						Select lnIdTemp As id_temp,lcTab+Alltrim(Alltrim(denumire)+lcTab+Alltrim(codmat)+lcTab+Alltrim(codmatf)+lcCrlf+Nvl(explicatie,[]))+Space(5)+;
						Alltrim(Str(cant,9,gnPCant))+[ ]+Alltrim(Nvl(um,[])) As denumire ;
						From cSeturiTemporar
				USE IN (SELECT('cSeturiTemporar'))					

				Select crsfactstemp
				*!*		modificare v 2.0.4	^
			Endscan

			Insert Into (tcCursorSursaSet) Select * From (tcCursorSursa) Where Nvl(id_set_fact,0) = 0
			*!*	modificare	v 2.0.4
			If Used('crsfactstemp')
				Use In crsfactstemp
			Endif
			*!*	modificare v 2.0.4	^
			lcCursor = tcCursorSursaSet
		Endif
	Endif

	*!*	modificare	ROAFACTURARE v 2.0.31
	If gnOrdineElemDoc = 2
		Select a.* From (lcCursor) a Into Cursor (lcCursor) Order By a.denumire
		*!*	modificare	ROAFACTURARE v 2.0.56
	Else
		Select a.* From (lcCursor) a Into Cursor (lcCursor) Order By a.id_temp
		*!*	modificare	ROAFACTURARE v 2.0.56	^
	Endif
	*!*	modificare	ROAFACTURARE v 2.0.31	^
	
	Select Recno() As nrcrt,IIF(m.llListareDetaliata, RECNO(), 1000) as grup, a.* From (lcCursor) a Into Cursor (lcCursor) Order By nrcrt

	creeaza_crsfacttemp()

	Do Case
		Case tnDiscountEvidentiat = 1 And lnPretListAviz = 2		&&	pret fara tva
			Insert Into crsfacttemp (nrcrt,denumire,explicatie,codmat,codmatc,codmatf,um,cod_um_iso,cantitate,;
				pretftva,codbare,pretctva,pretftvai,pretctvai,proc_disc,valftvai,valctvai,;
				valftva,valtva,discountftva,valdiscountftva,valdiscounttva, proc_Tvav,;
				serie,id_jtva_coloana,id_jtva_coloana_ex,valctva,discountctva,valdiscountctva,id_ctr,numar_contract,grup, id_articol,lot) ;
				Select Min(nrcrt) As nrcrt,denumire,LEFT(explicatie, 240) as explicatie,Nvl(codmat,Space(50)) As codmat,;
				Nvl(codmatc,Space(50)) As codmatc,Nvl(codmatf,Space(50)) As codmatf,;
				Nvl(um,Space(10)) As um,CAST(cod_um_iso as C(10)) As cod_um_iso,Sum(cantitate) As cantitate,;
				pretftva,Nvl(codbare,Space(50)) As codbare,pretctva,;
				pretftva As pretftvai,pretctva As pretctvai,0 As proc_disc,Sum(valftva) As valftvai,Sum(valftva+valtva) As valctvai,;
				Sum(valftva) As valftva,Sum(valtva) As valtva, ;
				discountftva,Sum(valdiscountftva) As valdiscountftva,Sum(valdiscounttva) As valdiscounttva, proc_Tvav,;
				Min(Nvl(serie,Space(100))) As serie,id_jtva_coloana,id_jtva_coloana_ex, ;
				SUM(valctva) As valctva,discountctva, Sum(discountctva) As valdiscountctva,id_ctr,numar_contract,grup,id_articol,Min(Nvl(lot,Space(20))) As lot ;
				FROM (lcCursor) ;
				group by 2,3,4,5,6,7,8,10,11,12,13,14,15,20,23,25,26,28,30,31,32,33 ;
				ORDER By 1 	&&	Into Cursor crsfacttemp
				
				* Group By denumire,codmat,codmatf,explicatie,um,codbare,pretftva,pretctva,proc_Tvav,id_jtva_coloana,discountftva,discountctva,id_ctr,numar_contract,grup 
				
		Case tnDiscountEvidentiat = 0 And lnPretListAviz = 2		&&	pret fara tva
			Insert Into crsfacttemp (denumire,explicatie,codmat,codmatc,codmatf,cantitate,pretftva,;
				codbare,pretftvai,pretctvai,pretctva,proc_disc,valftva,valtva,;
				valftvai,valctvai,discountftva,valdiscountftva,valdiscounttva,proc_Tvav,um,cod_um_iso,;
				nrcrt,serie,id_jtva_coloana,id_jtva_coloana_ex,valctva,discountctva,valdiscountctva,id_ctr,numar_contract,grup,id_articol,lot) ;
				Select denumire,LEFT(explicatie, 240) as explicatie,Nvl(codmat,Space(50)) As codmat,Nvl(codmatc,Space(50)) As codmatc,Nvl(codmatf,Space(50)) As codmatf,Sum(cantitate) As cantitate,pretftva-discountftva As pretftva,;
				Nvl(codbare,Space(50)) As codbare,pretftva As pretftvai,pretctva As pretctvai,pretctva-discountctva As pretctva,;
				Iif(cu_tva=0,Iif(pretftva<>0,Round(discountftva/pretftva*100,2),0),;
				Iif(pretctva<>0,Round(discountctva/pretctva*100,2),0)) As proc_disc,;
				Sum(valdiminuatftva) As valftva,Sum(valdiminuattva) As valtva,;
				Sum(valftva) As valftvai,Sum(valftva+valtva) As valctvai,;
				0 As discountftva,0 As valdiscountftva,0 As valdiscounttva, proc_Tvav, Nvl(um,Space(10)) As um,CAST(cod_um_iso as C(10)) As cod_um_iso,;
				Min(nrcrt) As nrcrt,Min(Nvl(serie,Space(100))) As serie,id_jtva_coloana,id_jtva_coloana_ex, ;
				SUM(valdiminuatctva) As valctva,0 As discountctva, 0 As valdiscountctva,id_ctr,numar_contract,grup,id_articol,Min(Nvl(lot,Space(20))) As lot ;
				FROM (lcCursor) Group By 1,2,3,4,5,7,8,9,10,11,12,20,21,22,25,26,28,29,30,31,32,33 Order By 23
			*!*				UNION all
			Insert Into crsfacttemp (denumire,explicatie,codmat,codmatc,codmatf,cantitate,pretftva,;
				codbare,pretftvai,pretctvai,pretctva,proc_disc,valftva,valtva,;
				valftvai,valctvai,discountftva,valdiscountftva,valdiscounttva,proc_Tvav,um,cod_um_iso,;
				nrcrt,serie,id_jtva_coloana,id_jtva_coloana_ex,valctva,discountctva,valdiscountctva,grup,id_articol,lot) ;
				Select denumire,explicatie,Nvl(codmat,Space(50)) As codmat,Nvl(codmatc,Space(50)) As codmatc,Nvl(codmatf,Space(50)) As codmatf,1 As cantitate,pretftva-discountftva as pretftva,;
				Space(50) As codbare,pretftva As pretftvai,pretctva As pretctvai,pretctva-discountctva As pretctva,;
				Iif(pretctva<>0,Round(discountctva/pretctva*100,2),0) As proc_disc,;
				0 As valftva,0 As valtva,0 As valftvai,0 As valctvai,discountftva,valdiscountftva,valdiscounttva,;
				proc_Tvav, Nvl(um,Space(10)) As um,CAST(cod_um_iso as C(10)) As cod_um_iso, nrcrt, Nvl(serie,Space(100)) As serie,id_jtva_coloana,id_jtva_coloana_ex, ;
				valdiminuatctva As valctva,0 As discountctva, 0 As valdiscountctva, grup,id_articol, Nvl(lot,Space(20)) As lot ;
				from (lcCursor) Where denumire = Replicate('Z',20) Order By 22 &&	Into Cursor crsfacttemp
		Case tnDiscountEvidentiat = 1 And lnPretListAviz = 1		&&	pret cu tva
			Insert Into crsfacttemp(nrcrt,denumire,explicatie,codmat,codmatc,codmatf,um,cod_um_iso,cantitate,pretctva,;
				valctva,valtva,pretftva,codbare,discountctva,valdiscountctva,valdiscounttva,proc_Tvav,serie,id_jtva_coloana,id_jtva_coloana_ex,;
				pretftvai,pretctvai,proc_disc,valftva,valftvai,valctvai,id_ctr,numar_contract,grup,id_articol,lot) ;
				Select Min(nrcrt) As nrcrt,denumire,LEFT(explicatie, 240) as explicatie,Nvl(codmat,Space(50)) As codmat,Nvl(codmatc,Space(50)) As codmatc,Nvl(codmatf,Space(50)) As codmatf,;
				Nvl(um,Space(10)) As um,CAST(cod_um_iso as C(10)) As cod_um_iso,Sum(cantitate) As cantitate,pretctva,Sum(valctva) As valctva,Sum(valtva) As valtva, ;
				pretftva,Nvl(codbare,Space(50)) As codbare,discountctva,Sum(valdiscountctva) As valdiscountctva,;
				Sum(valdiscounttva) As valdiscounttva, proc_Tvav, Min(Nvl(serie,Space(100))) As serie,id_jtva_coloana,id_jtva_coloana_ex, ;
				pretftva As pretftvai,pretctva As pretctvai,0 As proc_disc,Sum(valftva) As valftva,;
				Sum(valftva) As valftvai,Sum(valctva) As valctvai,id_ctr,numar_contract,grup,id_articol, Min(Nvl(lot,Space(20))) As lot ;
				FROM (lcCursor) ;
				group by 2,3,4,5,6,7,8,10,13,14,15,18,20,21,22,23,24,28,29,30,31 ;
				ORDER By 1 &&	Into Cursor crsfacttemp

				*Group By denumire,codmat,codmatf,explicatie,um,codbare,pretftva, pretctva,proc_Tvav,id_jtva_coloana,discountctva,id_ctr,numar_contract,grup ;
				
		Otherwise														&&	pret cu tva
			Insert Into crsfacttemp(denumire,explicatie,codmat,codmatc,codmatf,cantitate,pretctva,;
				codbare,pretftvai,pretctvai,pretftva,proc_disc,valftva,valctva,valtva,;
				valftvai,valctvai,discountctva,valdiscountctva,valdiscounttva,proc_Tvav,um,cod_um_iso,;
				nrcrt,serie,id_jtva_coloana,id_jtva_coloana_ex,id_ctr,numar_contract,grup,id_articol,lot) ;
				Select denumire,LEFT(explicatie, 240) as explicatie,Nvl(codmat,Space(50)) As codmat,Nvl(codmatc,Space(50)) As codmatc,Nvl(codmatf,Space(50)) As codmatf,;
				Sum(cantitate) As cantitate,pretctva-discountctva As pretctva,;
				Nvl(codbare,Space(50)) As codbare,pretftva As pretftvai,pretctva As pretctvai,pretftva-discountftva As pretftva,;
				Iif(cu_tva=0,Iif(pretftva<>0,Round(discountftva/pretftva*100,2),0),;
				Iif(pretctva<>0,Round(discountctva/pretctva*100,2),0)) As proc_disc,;
				Sum(valdiminuatftva) As valftva,Sum(valdiminuatctva) As valctva,Sum(valdiminuattva) As valtva,;
				Sum(valftva) As valftvai,Sum(valctva) As valctvai,;
				0 As discountctva,0 As valdiscountctva,0 As valdiscounttva, proc_Tvav, Nvl(um,Space(10)) As um,CAST(cod_um_iso as C(10)) As cod_um_iso,;
				Min(nrcrt) As nrcrt, Min(Nvl(serie,Space(100))) As serie,id_jtva_coloana,id_jtva_coloana_ex,id_ctr,numar_contract,grup,id_articol, Min(Nvl(lot,Space(20))) As lot ;
				FROM (lcCursor) Group By 1,2,3,4,5,7,8,9,10,11,12,21,22,23,26,27,28,29,30,31 Order By 24
			*!*				UNION all
			Insert Into crsfacttemp(denumire,explicatie,codmat,codmatc,codmatf,cantitate,pretctva,;
				codbare,pretftvai,pretctvai,pretftva,proc_disc,valftva,valctva,valtva,;
				valftvai,valctvai,discountctva,valdiscountctva,valdiscounttva,proc_Tvav,um,cod_um_iso,;
				nrcrt,serie,id_jtva_coloana,id_jtva_coloana_ex,grup,id_articol,lot) ;
				Select denumire,explicatie,Nvl(codmat,Space(50)) As codmat,Nvl(codmatc,Space(50)) As codmatc,Nvl(codmatf,Space(50)) As codmatf,;
				1 As cantitate,pretctva,;
				Space(50) As codbare,pretftva As pretftvai,pretctva As pretctvai,pretftva,;
				Iif(pretctva<>0,Round(discountctva/pretctva*100,2),0) As proc_disc,;
				0 As valftva,0 As valctva,0 As valtva,0 As valftvai,0 As valctvai,discountctva,;
				valdiscountctva,valdiscounttva, proc_Tvav, Nvl(um,Space(10)) As um,CAST(cod_um_iso as C(10)) As cod_um_iso, nrcrt,;
				Nvl(serie,Space(100)) As serie,id_jtva_coloana,id_jtva_coloana_ex,grup,id_articol,Nvl(lot,Space(20)) As lot ;
				from (lcCursor) Where denumire = Replicate('Z',20) Order By 23 &&	Into Cursor crsfacttemp
	ENDCASE
	
	********************************
	*** Explicatia are 500 de caractere, tip Memo si nu poate fi folosita intr-un select group by
	*** Am selectat doar primele 240 caractere apoi completez explicatia din cursorul original
	********************************
	SELECT (lcCursor)
	SCAN
		SCATTER NAME loRec MEMO
		SELECT crsfacttemp
		LOCATE FOR denumire = loRec.denumire AND Nvl(codmat,Space(50)) = Nvl(loRec.codmat,Space(50)) AND LEFT(explicatie, 240) = LEFT(loRec.explicatie, 240)
		IF FOUND()
			REPLACE explicatie WITH ALLTRIM(loRec.explicatie)
		ENDIF 
	ENDSCAN 

	*!*	modificare ROAFACTURARE	v 2.0.20	^
	*!*		crsfactura(id_temp N(20),id_articol N(20),id_pol N(20),id_gestiune N(20),Cont c(4),um c(10), ;
	*!*			denumire c(100),pretftva N(20,4),pretctva N(20,4),valftva N(20,4), valtva N(20,4), valctva N(20,4),;
	*!*			cantitate N(20,4),discountftva N(20,4), discountctva N(20,4),valdiscountftva N(20,4),valdiscounttva N(20,4),;
	*!*			valdiscountctva N(20,4),valdiminuatftva N(20,4),valdiminuattva N(20,4),;
	*!*			valdiminuatctva N(20,4),proc_tvav N(20,4),cu_tva N(1))
	*!*	modificare ROAFACTURARE	v 2.0.67
	*!*	Endif
	*!*	modificare ROAFACTURARE	v 2.0.67	^
	Select crsfacttemp
	Scan
		Scatter Name loArticol Memo
		Select (tcCursorDestinatie)
		lcSerie = Nvl(loArticol.serie,'')		&&	modificare ROAFACTURARE	v 2.0.46
		lcLot = Nvl(loArticol.lot,'')		&&	modificare ROAFACTURARE	v 2.0.46
		If loArticol.denumire <> Replicate('Z',20)
			Append Blank
			*!*	modificare v 2.0.4
			*!*			If lnPretListSubunitati = 2		&&	pret fara tva
			*!*			*!*	modificare v 2.0.4	^
			*!*				Replace denumire With Alltrim(loArticol.denumire),explicatie With Alltrim(Nvl(loArticol.explicatie,[ ])),;
			*!*					cantitate With loArticol.cantitate,codmat With Alltrim(NVL(loArticol.codmat,Space(50))),codmatf With Alltrim(loArticol.codmatf),;
			*!*					codbare With Alltrim(NVL(loArticol.codbare,Space(50))),;
			*!*					pretftvai with loArticol.pretftvai,pretctvai with loArticol.pretctvai,proc_disc with loArticol.proc_disc,;
			*!*					pretctva with loArticol.pretctva,pretftva With loArticol.pretftva,;
			*!*					valftva With loArticol.valftva,valtva With loArticol.valtva,;
			*!*					valftvai with loArticol.valftvai,valctvai with loArticol.valctvai,;
			*!*					pretctva With loArticol.pretctva,valctva With loArticol.valctva,valtva With loArticol.valtva,;
			*!*					proc_tva With loArticol.proc_Tvav,um With Alltrim(loArticol.um),serie With Alltrim(loArticol.serie),id_jtva_coloana With loArticol.id_jtva_coloana
			*!*			*!*	modificare v 2.0.4
			*!*			Else
			Replace denumire With Alltrim(loArticol.denumire),explicatie With Alltrim(Nvl(loArticol.explicatie,[ ])),;
				cantitate With loArticol.cantitate,codmat With Alltrim(Nvl(loArticol.codmat,Space(50))),codmatc With Alltrim(Nvl(loArticol.codmatc,Space(50))),codmatf With Alltrim(loArticol.codmatf),;
				codbare With Alltrim(Nvl(loArticol.codbare,Space(50))),;
				pretftvai With loArticol.pretftvai,pretctvai With loArticol.pretctvai,proc_disc With loArticol.proc_disc,;
				pretctva With loArticol.pretctva,;
				pretftva With loArticol.pretftva,valftva With loArticol.valftva,;
				valftvai With loArticol.valftvai,valctvai With loArticol.valctvai,;
				pretctva With loArticol.pretctva,valctva With loArticol.valctva,valtva With loArticol.valtva,;
				proc_tva With loArticol.proc_Tvav,um With Alltrim(loArticol.um),cod_um_iso With Alltrim(loArticol.cod_um_iso),;
				lot With Alltrim(loArticol.lot),serie With Alltrim(loArticol.serie),id_jtva_coloana With loArticol.id_jtva_coloana,id_jtva_coloana_ex With loArticol.id_jtva_coloana_ex,;
				id_ctr With loArticol.id_ctr,numar_contract With loArticol.numar_contract, id_articol WITH loArticol.id_articol
			*!*			Endif
			*!*	modificare v 2.0.4	^
			*!*	modificare	v 2.0.46
			lcSerie = []
			lcLot = []
			Select (tcCursorSursa)
			*!*	modificare v 2.0.47
			*!*				Scan For Alltrim(denumire) = Alltrim(loArticol.denumire) ;
			*!*						And Alltrim(Nvl(explicatie,[x_x|])) = Alltrim(Nvl(loArticol.explicatie,[x_x|])) ;
			*!*						and Alltrim(Nvl(codmat,[x_x|])) = Alltrim(Nvl(loArticol.codmat,[x_x|])) ;
			*!*						And Alltrim(Nvl(codmatf,[x_x|])) = Alltrim(Nvl(loArticol.codmatf,[x_x|])) ;
			*!*						and Alltrim(Nvl(codbare,[x_x|])) = Alltrim(Nvl(loArticol.codbare,[x_x|])) ;
			*!*						and pretftva = loArticol.pretftva ;
			*!*						and Alltrim(Nvl(um,[x_x|])) = Alltrim(Nvl(loArticol.um,[x_x|])) ;
			*!*						and discountftva = loArticol.discountftva ;
			*!*						and proc_Tvav = loArticol.proc_Tvav
			*!*					lcSerie = lcSerie + Iif(!Empty(Nvl(serie,'')),[,]+Alltrim(serie),[])
			*!*				Endscan
			If lnPretListAviz = 2
				Scan For Alltrim(denumire) = Alltrim(loArticol.denumire) ;
						And Alltrim(Nvl(explicatie,[x_x|])) = Alltrim(Nvl(loArticol.explicatie,[x_x|])) ;
						and Alltrim(Nvl(codmat,[x_x|])) = Alltrim(Nvl(loArticol.codmat,[x_x|])) ;
						And Alltrim(Nvl(codmatf,[x_x|])) = Alltrim(Nvl(loArticol.codmatf,[x_x|])) ;
						and Alltrim(Nvl(codbare,[x_x|])) = Alltrim(Nvl(loArticol.codbare,[x_x|])) ;
						and pretftva = loArticol.pretftva ;
						and Alltrim(Nvl(um,[x_x|])) = Alltrim(Nvl(loArticol.um,[x_x|])) ;
						and discountftva = loArticol.discountftva ;
						and proc_Tvav = loArticol.proc_Tvav ;
						and Nvl(id_ctr,-1) = Nvl(loArticol.id_ctr,-1) ;
						and Nvl(numar_contract,'') = Nvl(loArticol.numar_contract,'')
					lcSerie = lcSerie + Iif(!Empty(Nvl(serie,'')),[,]+Alltrim(serie),[])
					lcLot = lcLot + Iif(!Empty(Nvl(lot,'')),[,]+Alltrim(lot),[])					
				Endscan
			Else
				Scan For Alltrim(denumire) = Alltrim(loArticol.denumire) ;
						And Alltrim(Nvl(explicatie,[x_x|])) = Alltrim(Nvl(loArticol.explicatie,[x_x|])) ;
						and Alltrim(Nvl(codmat,[x_x|])) = Alltrim(Nvl(loArticol.codmat,[x_x|])) ;
						And Alltrim(Nvl(codmatf,[x_x|])) = Alltrim(Nvl(loArticol.codmatf,[x_x|])) ;
						and Alltrim(Nvl(codbare,[x_x|])) = Alltrim(Nvl(loArticol.codbare,[x_x|])) ;
						and pretctva = loArticol.pretctva ;
						and Alltrim(Nvl(um,[x_x|])) = Alltrim(Nvl(loArticol.um,[x_x|])) ;
						and discountctva = loArticol.discountctva ;
						and proc_Tvav = loArticol.proc_Tvav ;
						and Nvl(id_ctr,-1) = Nvl(loArticol.id_ctr,-1) ;
						and Nvl(numar_contract,'') = Nvl(loArticol.numar_contract,'')
					lcSerie = lcSerie + Iif(!Empty(Nvl(serie,'')),[,]+Alltrim(serie),[])
					lcLot = lcLot + Iif(!Empty(Nvl(lot,'')),[,]+Alltrim(lot),[])					
				Endscan
			Endif
			*!*	modificare v 2.0.47	^
			Select (tcCursorDestinatie)
			lcSerie = Iif(!Empty(lcSerie),Substr(lcSerie,2),Nvl(loArticol.serie,''))
			lcLot = Iif(!Empty(lcLot),Substr(lcLot,2),Nvl(loArticol.lot,''))			
			Replace serie With m.lcSerie, lot WITH m.lcLot
			*!*	modificare	v 2.0.46	^
		Else
			loArticol.denumire = [FACTURA]
		Endif
		*!*	modificare v 2.0.4
		If lnPretListAviz = 2		&&	pret fara tva
			*!*	modificare v 2.0.4	^
			If loArticol.discountftva <> 0
				Append Blank
				lnProcentDiscount = loArticol.discountftva * 100 / loArticol.pretftva
				Replace denumire With [Discount ]+Alltrim(Str(lnProcentDiscount,10,2))+[ % ]+Alltrim(Proper(loArticol.denumire)),;
					lot With Iif(!Empty(Nvl(lcLot,[])),[],Alltrim(loArticol.lot)), serie With Iif(!Empty(Nvl(lcSerie,[])),[],Alltrim(loArticol.serie)),;
					cantitate With loArticol.cantitate,um With Alltrim(loArticol.um),cod_um_iso WITH ALLTRIM(loArticol.cod_um_iso),;
					codbare With Alltrim(Nvl(loArticol.codbare,Space(50))),;
					pretftva With (-1) * loArticol.discountftva,valftva With (-1) * loArticol.valdiscountftva,codmat With Alltrim(Nvl(loArticol.codmat,Space(50))),;
					codmatc With Alltrim(Nvl(loArticol.codmatc,Space(50))),codmatf With Alltrim(loArticol.codmatf),valtva With (-1) * loArticol.valdiscounttva,;
					proc_tva With loArticol.proc_Tvav,id_jtva_coloana With loArticol.id_jtva_coloana,id_jtva_coloana_ex With loArticol.id_jtva_coloana_ex,;
					id_ctr With loArticol.id_ctr,numar_contract With loArticol.numar_contract, id_articol WITH loArticol.id_articol
			Endif
			*!*	modificare v 2.0.4
		Else
			If loArticol.discountctva <> 0
				Append Blank
				lnProcentDiscount = loArticol.discountctva * 100 / loArticol.pretctva
				Replace denumire With [Discount ]+Alltrim(Str(lnProcentDiscount,10,2))+[ % ]+Alltrim(Proper(loArticol.denumire)),;
					lot With Iif(!Empty(lcLot),[],Alltrim(loArticol.lot)), serie With Iif(!Empty(lcSerie),[],Alltrim(loArticol.serie)),;
					cantitate With loArticol.cantitate,um With Alltrim(loArticol.um),cod_um_iso WITH ALLTRIM(loArticol.cod_um_iso),;
					codmatc With Alltrim(Nvl(loArticol.codmatc,Space(50))),codmatf With Alltrim(loArticol.codmatf),codbare With Alltrim(Nvl(loArticol.codbare,Space(50))),;
					pretctva With (-1) * loArticol.discountctva,valctva With (-1) * loArticol.valdiscountctva,codmat With Alltrim(Nvl(loArticol.codmat,Space(50))),;
					valtva With (-1) * loArticol.valdiscounttva,proc_tva With loArticol.proc_Tvav,id_jtva_coloana With loArticol.id_jtva_coloana,id_jtva_coloana_ex With loArticol.id_jtva_coloana_ex,;
					id_ctr With loArticol.id_ctr,numar_contract With loArticol.numar_contract, id_articol WITH loArticol.id_articol
			Endif
		Endif
		*!*	modificare v 2.0.4	^		
		Select crsfacttemp
	Endscan
					
	Use In crsfacttemp
	completeaza_explicatie_tva(tcCursorDestinatie)	&&	modificare ROAFACTURARE	v 2.0.46

	*!*	modificare	ROAFACTURARE	v 2.0.67
	If Inlist(poDate.tip,2,6,26,51,52)
		completeaza_explicatie_contract(tcCursorDestinatie)
	Endif
	*!*	modificare	ROAFACTURARE	v 2.0.67	^

	* Completez CODNC8/CODCPV
	SELECT (m.tcCursorDestinatie)
	SCAN
		lnIdArticol = id_articol
		loCod = GetCodNC8CPV(m.lnIdArticol)
		SELECT (m.tcCursorDestinatie)
		Replace codnc8 WITH loCod.codnc8, codcpv WITH loCod.codcpv
	ENDSCAN 
	
	If tnInValuta = 1
		*!*	nu a fost modificata prelucreaza_factura_valuta pentru ca avizele se fac doar in lei
		*!*	iar optiunea de listare a pretului cu tva este valabila doar pentru RON
		*!*	modificare ROAFACTURARE	v 2.0.67
		*!*		prelucreaza_factura_valuta(tnDiscountEvidentiat,tnTipFacturare)
		prelucreaza_factura_valuta(tcCursorSursa,tcCursorSursaSet,tnDiscountEvidentiat, tnListareDetaliata)
		*!*	modificare ROAFACTURARE	v 2.0.67	^
	Endif

Endproc	&&	prelucreaza_factura
*********************************************************************************************************
Procedure prelucreaza_lista_livrare
	*!*	modificare ROAFACTURARE	v 2.0.67
	*!*	Lparameters tnTipFacturare
	*!*	modificare ROAFACTURARE	v 2.0.67	^
	Local lcCursor
	lcCursor = [crslltemp]
	Create Cursor crslistalivrare(nrcrt N(10),denumire c(100),explicatie M,cantitate N(20,max(gnPCant,4)),um c(10),lot c(20) NULL, serie c(100) null,;
		codmat c(50),codmatf c(50))

	If !Used('crsfacturaset')
		Select a.* From crsfactura a Where a.gestionabil = 1 Into Cursor (lcCursor) Order By 1
	Else
		*!*	modificare ROAFACTURARE v 2.0.56
		*!*			Select a.* From crsfacturaset a Where !Isnull(a.id_set_fact) And Nvl(gestionabil,0) = 1 ;
		*!*				Into Cursor (lcCursor) Order By 1
		If poDate.listare_componenta_set_ll = 1
			Select a.* From crsfacturaset a Where Nvl(a.gestionabil,0) = 1 Or a.cantitate = 0 ;
				Into Cursor (lcCursor) Order By 1
		Else
			Select a.* From crsfacturaset a Where Nvl(a.id_set_fact,0)<>0 Or (Nvl(a.id_set_fact,0)=0 And Nvl(a.gestionabil,0) = 1) ;
				Into Cursor (lcCursor) Order By 1
		Endif
		*!*	modificare ROAFACTURARE v 2.0.56	^
	Endif

	If Reccount(lcCursor) > 0
		Select Min(nrcrt) As nrcrt,denumire,Nvl(codmat,Space(50)) As codmat,Nvl(codmatf,Space(50)) As codmatf,;
			LEFT(explicatie,254) as explicatie,Nvl(um,Space(10)) As um,Sum(cantitate) As cantitate, ;
			Nvl(lot,Space(20)) As lot, Nvl(serie,Space(100)) As serie ;
			FROM (lcCursor) Group By 2,3,4,5,6,8,9 ;
			ORDER By 1 Into Cursor crslistalivrare
			*  denumire,codmat,codmatf,explicatie,um,serie
	Else
		Select * From (lcCursor) Into Cursor crslistalivrare
	Endif

	Use In (lcCursor)

Endproc	&& prelucreaza_lista_livrare
*********************************************************************************************************
Procedure prelucreaza_recapitulatie
	Lparameters tnDiscountEvidentiat
	Local lcCursor
	Create Cursor crsrecapitulatie(denumire c(100),explicatie M,cantitate N(20,max(gnPCant,4)),pretftva N(20,max(gnPPretV,4)),pret_achizitie N(20,max(gnPPret,4)),;
		valftva N(20,max(gnPc,4)),valtva N(20,max(gnPc,4)),um c(10),lot m,serie m,codmat c(50),codmatf c(50),id_jtva_coloana N(10),expltva c(100))

	If Used('crsfacttemp')
		Use In crsfacttemp
	Endif

	*!*	modificare	ROAFACTURARE v 2.0.31
	If gnOrdineElemDoc = 2
		Select a.* From crsfactura a Into Cursor crsfactura Order By a.denumire
		*!*	modificare	ROAFACTURARE v 2.0.56
	Else
		Select a.* From crsfactura a Into Cursor crsfactura Order By a.id_temp
		*!*	modificare	ROAFACTURARE v 2.0.56	^
	Endif
	*!*	modificare	ROAFACTURARE v 2.0.31	^
	Select Recno() As nrcrt,a.* From crsfactura a Into Cursor crsfactura Order By nrcrt

	If tnDiscountEvidentiat = 1
		Select Min(nrcrt) As nrcrt,denumire,Nvl(codmat,Space(50)) As codmat,Nvl(codmatf,Space(50)) As codmatf,;
					CAST(explicatie as C(254)) as explicatie,Nvl(um,Space(10)) As um,Sum(cantitate) As cantitate,pretftva,Sum(valftva) As valftva,;
					Sum(valtva) As valtva, discountftva,pret_achizitie,Sum(valdiscountftva) As valdiscountftva,;
					Sum(valdiscounttva) As valdiscounttva, proc_Tvav, Nvl(serie,Space(100)) As serie,;
					id_jtva_coloana, Nvl(lot,Space(20)) As lot ;
					FROM crsfactura Group By 2,3,4,5,6,8,11,12,15,16,17,18 ;
					ORDER By 1 Into Cursor crsfacttemp			
	Else
		Select denumire,CAST(explicatie as C(254)) as explicatie,Nvl(codmat,Space(50)) As codmat, Sum(cantitate) As cantitate,pretftva-discountftva As pretftva,;
			Sum(valdiminuatftva) As valftva,Sum(valdiminuattva) As valtva,pret_achizitie,;
			0 As discountftva,0 As valdiscountftva,0 As valdiscounttva, proc_Tvav, ;
			Nvl(um,Space(10)) As um,Min(nrcrt) As nrcrt, Nvl(serie,Space(100)) As serie, ;
			Nvl(codmatf,Space(50)) As codmatf,id_jtva_coloana, Nvl(lot,Space(20)) As lot ;
			FROM crsfactura Group By 1,2,3,5,8,12,13,15,16,17,18 ;
			UNION All ;
			Select denumire,explicatie,Nvl(codmat,Space(50)) As codmat,1 As cantitate,pretftva,;
			0 As valftva,0 As valtva,0 As pret_achizitie,discountftva,valdiscountftva,valdiscounttva,;
			proc_Tvav, Nvl(um,Space(10)) As um, nrcrt, Nvl(serie,Space(100)) As serie, ;
			Nvl(codmatf,Space(50)) As codmatf,id_jtva_coloana, Nvl(lot,Space(20)) As lot ;
			from crsfactura Where denumire = Replicate('Z',20) Order By 14 ;
			Into Cursor crsfacttemp
	Endif
	Select crsfacttemp
	Scan
		Scatter Name loArticol Memo
		Select crsrecapitulatie
		If loArticol.denumire <> Replicate('Z',20)
			Append Blank
			Replace denumire With Alltrim(loArticol.denumire),explicatie With Alltrim(Nvl(loArticol.explicatie,[ ])),;
				cantitate With loArticol.cantitate,codmat With Nvl(loArticol.codmat,Space(50)),codmatf With Alltrim(loArticol.codmatf),;
				pretftva With loArticol.pretftva,valftva With loArticol.valftva,valtva With loArticol.valtva,;
				um With Alltrim(loArticol.um),lot With Alltrim(loArticol.lot),serie With Alltrim(loArticol.serie),;
				pret_achizitie With loArticol.pret_achizitie,id_jtva_coloana With loArticol.id_jtva_coloana
		Else
			loArticol.denumire = [FACTURA]
		Endif
		If loArticol.discountftva <> 0
			Append Blank
			lnProcentDiscount = loArticol.discountftva * 100 / loArticol.pretftva
			Replace denumire With [Discount ]+Alltrim(Str(lnProcentDiscount,10,2))+[ % ]+Alltrim(Proper(loArticol.denumire)),;
				lot With Alltrim(loArticol.lot),serie With Alltrim(loArticol.serie),cantitate With loArticol.cantitate,um With Alltrim(loArticol.um),;
				pretftva With (-1) * loArticol.discountftva,valftva With (-1) * loArticol.valdiscountftva,codmat With Nvl(loArticol.codmat,Space(50)),;
				valtva With (-1) * loArticol.valdiscounttva,pret_achizitie With loArticol.pret_achizitie,codmatf With Nvl(loArticol.codmatf,Space(50)),;
				id_jtva_coloana With loArticol.id_jtva_coloana
		Endif
		Select crsfacttemp
	Endscan

	Use In crsfacttemp

Endproc	&&	prelucreaza_recapitulatie
*********************************************************************************************************
Procedure prelucreaza_factura_valuta
	Lparameters tcCursorSursa,tcCursorSursaSet,tnDiscountEvidentiat, tnListareDetaliata
	&&	,tnTipFacturare	*!*	modificare ROAFACTURARE	v 2.0.67
	*!*	modificare	v 2.0.77
	*!* tnListareDetaliata: daca sa se listeze randurile asa cum sunt in loc de grupate dupa articol, pret. sa nu se cumuleze cantitatea pe articole
		
	LOCAL llListareDetaliata, lnIdArticol
	llListareDetaliata = (IIF(TYPE('tnListareDetaliata') = 'N', tnListareDetaliata, 0) = 1)
		
	creeaza_facturafinala([crsfacturafinalaval])
	*!*		Create Cursor crsfacturafinalaval(denumire c(100),explicatie c(240),cantitate N(20,4),pretftva N(20,4),;
	*!*			valftva N(20,4),valtva N(20,4),proc_tva N(20,4),um c(10),serie m,codmat c(50),codmatf c(50),codbare c(50),;
	*!*			id_jtva_coloana N(10),expltva c(100),id_ctr N(10) Null,numar_contract c(100) Null)
	*!*	modificare	v 2.0.77	^
	If Used('crsfacttemp')
		Use In crsfacttemp
	Endif

	If !Used(tcCursorSursaSet)
		lcCursor = tcCursorSursa
	Else
		lcCursor = tcCursorSursaSet
	Endif

	*!*	modificare	ROAFACTURARE v 2.0.56
	If gnOrdineElemDoc = 2
		Select a.* From (lcCursor) a Into Cursor (lcCursor) Order By a.denumire
	Else
		Select a.* From (lcCursor) a Into Cursor (lcCursor) Order By a.id_temp
	ENDIF

	*!*	modificare	ROAFACTURARE v 2.0.56	^

	creeaza_crsfacttemp()
	If tnDiscountEvidentiat = 1
		Insert Into crsfacttemp (nrcrt,denumire,explicatie,um,cod_um_iso,cantitate,pretftva,valftva,valtva, ;
			discountftva,valdiscountftva,valdiscounttva,proc_Tvav,serie,;
			codmat,codmatc,codmatf,id_jtva_coloana,codbare,id_ctr,numar_contract,grup,id_articol,lot) ;
			Select Min(nrcrt) As nrcrt,denumire,LEFT(explicatie, 240) as explicatie,Nvl(um,Space(10)) As um,CAST(cod_um_iso as C(10)) As cod_um_iso,Sum(cantitate) As cantitate,;
			vpretftva As pretftva,Sum(vvalftva) As valftva,Sum(vvaltva) As valtva, ;
			vdiscountftva As discountftva,Sum(vvaldiscountftva) As valdiscountftva,;
			Sum(vvaldiscounttva) As valdiscounttva,proc_Tvav, Min(Nvl(serie,Space(100))) As serie,;
			Nvl(codmat,Space(50)) As codmat,IIF(TYPE(m.lcCursor + '.codmatc') = 'C', Nvl(codmatc,Space(50)), SPACE(50)) As codmatc,Nvl(codmatf,Space(50)) As codmatf,id_jtva_coloana, ;
			Nvl(codbare,Space(50)) As codbare,id_ctr,numar_contract,grup,id_articol, Min(Nvl(lot,Space(20))) As lot ;
			FROM (lcCursor) Group By 2,3,4,5,7,10,13,15,16,17,18,19,20,21,22,23 ;
			ORDER By 1 &&	Into Cursor crsfacttemp
	Else
		Insert Into crsfacttemp(denumire,explicatie,cantitate,pretftva,valftva,valtva,;
			discountftva,valdiscountftva,valdiscounttva,proc_Tvav,um,cod_um_iso,nrcrt,serie,codmat, ;
			codmatc,codmatf,id_jtva_coloana,codbare,id_ctr,numar_contract,grup,id_articol,lot) ;
			Select denumire,LEFT(explicatie, 240) as explicatie,Sum(cantitate) As cantitate,vpretftva-vdiscountftva As pretftva,;
			Sum(vvaldiminuatftva) As valftva,Sum(vvaldiminuattva) As valtva,;
			0 As discountftva,0 As valdiscountftva,0 As valdiscounttva, proc_Tvav,;
			Nvl(um,Space(10)) As um,CAST(cod_um_iso as C(10)) As cod_um_iso,Min(nrcrt) As nrcrt, Min(Nvl(serie,Space(100))) As serie,Nvl(codmat,Space(50)) As codmat, ;
			IIF(TYPE(m.lcCursor + '.codmatc') = 'C', Nvl(codmatc,Space(50)), SPACE(50)),Nvl(codmatf,Space(50)) As codmatf,id_jtva_coloana,Nvl(codbare,Space(50)) As codbare,id_ctr,numar_contract,grup,id_articol, Min(Nvl(lot,Space(20))) As lot ;
			FROM (lcCursor) Group By 1,2,4,10,11,12,15,16,17,18,19,20,21,22,23 ;
			UNION All ;
			Select denumire,LEFT(explicatie, 240) as explicatie,1 As cantitate,vpretftva As pretftva,;
			0 As valftva,0 As valtva,vdiscountftva As discountftva,vvaldiscountftva As valdiscountftva,;
			vvaldiscounttva As valdiscounttva, proc_Tvav,Nvl(um,Space(10)) As um,CAST(cod_um_iso as C(10)) As cod_um_iso, nrcrt,;
			Nvl(serie,Space(100)) As serie,Nvl(codmat,Space(50)) As codmat,IIF(TYPE(m.lcCursor + '.codmatc') = 'C', Nvl(codmatc,Space(50)), SPACE(50)) As codmatc,Nvl(codmatf,Space(50)) As codmatf, ;
			id_jtva_coloana,Nvl(codbare,Space(50)) As codbare,id_ctr,numar_contract,grup,id_articol,Nvl(lot,Space(20)) As lot ;
			from (lcCursor) Where denumire = Replicate('Z',20) Order By 13 &&	Into Cursor crsfacttemp
	Endif
	*!*	modificare ROAFACTURARE	v 2.0.46	^
	*!*	modificare ROAFACTURARE	v 2.0.67
	*!*	Endif
	*!*	modificare ROAFACTURARE	v 2.0.67	^
	
	********************************
	*** Explicatia are 500 de caractere, tip Memo si nu poate fi folosita intr-un select group by
	*** Am selectat doar primele 240 caractere apoi completez explicatia din cursorul original
	********************************
	SELECT (lcCursor)
	SCAN
		SCATTER NAME loRec MEMO
		SELECT crsfacttemp
		LOCATE FOR denumire = loRec.denumire AND Nvl(codmat,Space(50)) = Nvl(loRec.codmat,Space(50)) AND LEFT(explicatie, 240) = LEFT(loRec.explicatie, 240)
		IF FOUND()
			REPLACE explicatie WITH ALLTRIM(loRec.explicatie)
		ENDIF 
	ENDSCAN 
		
	Select crsfacttemp
	Scan
		Scatter Name loArticol Memo
		Select crsfacturafinalaval
		lcSerie = Nvl(loArticol.serie,'')	&&	modificare ROAFACTURARE	v 2.0.46
		lcLot = Nvl(loArticol.lot,'')			
		If loArticol.denumire <> Replicate('Z',20)
			Append Blank
			*!*	modificare ROAFACTURARE	v 2.0.46
			*!*			Replace denumire With Alltrim(loArticol.denumire),cantitate With loArticol.cantitate,;
			*!*				explicatie With Alltrim(Nvl(loArticol.explicatie,[ ])),um With Alltrim(loArticol.um),;
			*!*				pretftva With loArticol.pretftva,valftva With loArticol.valftva,valtva With loArticol.valtva,;
			*!*				proc_tva With loArticol.proc_Tvav,serie With Alltrim(loArticol.serie),codmat With Alltrim(NVL(loArticol.codmat,Space(50)))
			Replace denumire With Alltrim(loArticol.denumire),cantitate With loArticol.cantitate,;
				explicatie With Alltrim(Nvl(loArticol.explicatie,[ ])),um With Alltrim(loArticol.um),cod_um_iso WITH ALLTRIM(loArticol.cod_um_iso),;
				id_articol WITH loArticol.id_articol,;
				pretftva With loArticol.pretftva,valftva With loArticol.valftva,valtva With loArticol.valtva,;
				proc_tva With loArticol.proc_Tvav,codmat With Alltrim(Nvl(loArticol.codmat,Space(50))), ;
				codmatc With Alltrim(Nvl(loArticol.codmatc,Space(50))),;
				codmatf With Alltrim(Nvl(loArticol.codmatf,Space(50))),id_jtva_coloana With loArticol.id_jtva_coloana,;
				id_ctr With loArticol.id_ctr,numar_contract With loArticol.numar_contract
			lcSerie = []
			lcLot = []
			Select (tcCursorSursa)
			Scan For Alltrim(denumire) = Alltrim(loArticol.denumire) ;
					And Alltrim(Nvl(explicatie,[ ])) = Alltrim(Nvl(loArticol.explicatie,[ ])) ;
					and Alltrim(codmat) = Alltrim(loArticol.codmat) ;
					And Alltrim(codmatf) = Alltrim(loArticol.codmatf) ;
					and vpretftva = loArticol.pretftva ;
					and Alltrim(um) = Alltrim(loArticol.um) ;
					and vdiscountftva = loArticol.discountftva ;
					and proc_Tvav = loArticol.proc_Tvav ;
					and Nvl(id_ctr,-1) = Nvl(loArticol.id_ctr,-1) ;
					and Nvl(numar_contract,'') = Nvl(loArticol.numar_contract,'')
				lcSerie = lcSerie + Iif(!Empty(Nvl(serie,'')),[,]+Alltrim(serie),[])
				lcLot = lcLot + Iif(!Empty(Nvl(lot,'')),[,]+Alltrim(lot),[])				
				Select crsfactura
			Endscan
			Select crsfacturafinalaval
			lcSerie = Iif(!Empty(lcSerie),Substr(lcSerie,2),Nvl(loArticol.serie,''))
			lcLot = Iif(!Empty(lcLot),Substr(lcLot,2),Nvl(loArticol.lot,''))			
			Replace serie With m.lcSerie, lot WITH m.lcLot
			*!*	modificare ROAFACTURARE	v 2.0.46	^
		Else
			loArticol.denumire = [INVOICE]
		Endif
		If loArticol.discountftva <> 0
			Append Blank
			lnProcentDiscount = loArticol.discountftva * 100 / loArticol.pretftva
			*!*	modificare ROAFACTURARE	v 2.0.46
			*!*			Replace denumire With Alltrim(Proper(loArticol.denumire))+[ Discount ]+Alltrim(Str(lnProcentDiscount,10,2))+[ %],;
			*!*				cantitate With loArticol.cantitate,um With Alltrim(loArticol.um),serie With Alltrim(loArticol.serie),;
			*!*				pretftva With (-1) * loArticol.discountftva,valftva With (-1) * loArticol.valdiscountftva,;
			*!*				valtva With (-1) * loArticol.valdiscounttva,proc_tva With loArticol.proc_Tvav,;
			*!*				codmat With Alltrim(NVL(loArticol.codmat,Space(50)))
			Replace denumire With Alltrim(Proper(loArticol.denumire))+[ Discount ]+Alltrim(Str(lnProcentDiscount,10,2))+[ %],;
				id_articol WITH loArticol.id_articol,;
				cantitate With loArticol.cantitate,um With Alltrim(loArticol.um),cod_um_iso WITH ALLTRIM(loArticol.cod_um_iso),lot WITH lcLot, serie With lcSerie,;
				pretftva With (-1) * loArticol.discountftva,valftva With (-1) * loArticol.valdiscountftva,;
				valtva With (-1) * loArticol.valdiscounttva,proc_tva With loArticol.proc_Tvav,;
				codmatc With Alltrim(Nvl(loArticol.codmatc,Space(50))), ;
				codmat With Alltrim(Nvl(loArticol.codmat,Space(50))),codmatf With Alltrim(Nvl(loArticol.codmatf,Space(50))),;
				id_jtva_coloana With loArticol.id_jtva_coloana,id_ctr With loArticol.id_ctr,numar_contract With loArticol.numar_contract
			*!*	modificare ROAFACTURARE	v 2.0.46	^
		Endif
		Select crsfacttemp
	Endscan

	* Completez CODNC8/CODCPV
	SELECT crsfacturafinalaval
	SCAN
		lnIdArticol = id_articol
		loCod = GetCodNC8CPV(m.lnIdArticol)
		SELECT crsfacturafinalaval
		Replace codnc8 WITH loCod.codnc8, codcpv WITH loCod.codcpv
	ENDSCAN 
	
	Use In crsfacttemp

Endproc	&&	prelucreaza_factura_valuta
*********************************************************************************************************
Procedure prelucreaza_aviz_retur
	Lparameters tnDiscountEvidentiat
	*!*	modificare	v 2.0.77
	creeaza_facturacrs([crsavizretur2])
	prelucreaza_facturacrs([crsavizretur],[crsavizretur2],0,0,0)
	prelucreaza_factura([crsavizretur2],[],[crsfacturafinala2],tnDiscountEvidentiat,0,2)

	If Used('crsavizretur')
		Use In crsavizretur
	Endif
	If Used('crsavizretur2')
		Use In crsavizretur2
	Endif

Endproc	&&	prelucreaza_aviz_retur
*********************************************************************************************************
*!*	modificare ROAFACTURARE	v 2.0.46
*!*	Function optiuni_format_factura
*!*	Private pnOptiune
*!*	pnOptiune = 1
*!*	pnButon = 1
*!*	lcTitlu = "LISTARE FACTURA"
*!*	lcExplicatie = "Alegeti formatul facturii:"
*!*	lcOptiuni = [A4;A5;]
*!*	lnNrOptiuni = 2
*!*	llFaraRenuntare = .T.
*!*	ofrmopt = Createobject("frm_optiune",lcTitlu,lcExplicatie,lnNrOptiuni,lcOptiuni,llFaraRenuntare)
*!*	ofrmopt.Show(1)
*!*	Release ofrmopt
*!*	Return pnOptiune
*!*	Endfunc &&	optiuni_format_factura
*********************************************************************************************************
Function optiuni_format_listare
	Lparameters tnTip	&&	0	-	factura	;	1	-	aviz
	Private pnOptiune
	pnOptiune = 1
	pnButon = 1
	If Empty(tnTip)
		lcTitlu = "LISTARE FACTURA"
		lcExplicatie = "Alegeti formatul facturii:"
	Else
		lcTitlu = "LISTARE AVIZ"
		lcExplicatie = "Alegeti formatul avizului:"
	Endif
	lcOptiuni = [A4;A5;]
	lnNrOptiuni = 2
	llFaraRenuntare = .T.
	ofrmopt = Createobject("frm_optiune",lcTitlu,lcExplicatie,lnNrOptiuni,lcOptiuni,llFaraRenuntare)
	ofrmopt.Show(1)
	Release ofrmopt
	Return pnOptiune
Endfunc &&	optiuni_format_listare
*!*	modificare ROAFACTURARE	v 2.0.46	^
*********************************************************************************************************
Procedure creeaza_facturafinala
	Lparameters tcNumeCursor
	If Used(tcNumeCursor)
		Use In (tcNumeCursor)
	Endif
	Create Cursor (tcNumeCursor) (denumire c(100),explicatie M,cantitate N(20,max(gnPCant,4)),pretctva N(20,max(gnPPretV,precizie_max_pret_vz)),valctva N(20,max(gnPc,4)),;
		pretftva N(20,max(gnPPretV,precizie_max_pret_vz)),pretftvai N(20,max(gnPPretV,precizie_max_pret_vz)),pretctvai N(20,max(gnPPretV,precizie_max_pret_vz)),proc_disc N(10,4),valftva N(20,max(gnPc,4)),valtva N(20,max(gnPc,4)),;
		valftvai N(20,max(gnPc,4)),valctvai N(20,max(gnPc,4)),valdiscount N(20,max(gnPc,4)),;
		proc_tva N(20,4),um c(10),cod_um_iso C(10) null,lot C(20) null,serie c(100),codmat c(50),codmatc c(50), codbare c(50),codmatf c(50),;
		id_jtva_coloana N(10),expltva c(100),id_jtva_coloana_ex N(10) null,expltva_ex c(100) null,id_ctr N(10) Null,numar_contract c(100) Null, taxcode N(6) null, id_articol N(10) null, codnc8 V(20) null, codcpv V(20) null)
Endproc	&&	creeaza_crsfacturafinala
*********************************************************************************************************
Procedure creeaza_facturacrs
	Lparameters tcNumeCursor
	If Used(tcNumeCursor)
		Use In (tcNumeCursor)
	Endif
	Create Cursor (tcNumeCursor) (id_c N(20),id_temp N(20),id_articol N(20) null,id_pol N(20) Null,id_gestiune N(20),Cont c(4),um c(10) Null, cod_um_iso c(5) null, ;
		id_valuta N(10),gestionabil N(1),tip_valuta N(1),Curs N(20,4),multiplicator N(10),id_jtva_coloana N(20) Null,id_jtva_coloana_ex N(20) Null,codmat c(100),codmatc c(100),codmatf c(100),codbare c(50),;
		pret_achizitie N(20,max(gnPPret,4)),denumire c(100),pretftva N(20,max(gnPPretV,precizie_max_pret_vz)),pretctva N(20,max(gnPPretV,precizie_max_pret_vz)),valftva N(20,max(gnPc,4)), valtva N(20,max(gnPc,4)),valctva N(20,max(gnPc,4)),;
		cantitate N(20,max(gnPCant,4)),discountftva N(20,max(gnPPretV,4)), discountctva N(20,max(gnPPretV,precizie_max_pret_vz)), valdiscountftva N(20,max(gnPc,4)),valdiscounttva N(20,max(gnPc,4)),;
		valdiscountctva N(20,max(gnPc,4)),valdiminuatftva N(20,max(gnPc,4)),valdiminuattva N(20,max(gnPc,4)),valdiminuatctva N(20,max(gnPc,4)),proc_Tvav N(20,4),cu_tva N(1), lot C(20) null, serie c(100) Null,;
		vpretftva N(20,max(gnPVal,precizie_max_pret_vz)),vpretctva N(20,max(gnPVal,precizie_max_pret_vz)),vvalftva N(20,max(gnPVal,4)),vvaltva N(20,max(gnPVal,4)),vvalctva N(20,max(gnPVal,4)),;
		vdiscountftva N(20,max(gnPVal,4)),vdiscountctva N(20,max(gnPVal,4)),vvaldiscountftva N(20,max(gnPVal,4)),vvaldiscounttva N(20,max(gnPVal,4)),;
		vvaldiscountctva N(20,max(gnPVal,4)),vvaldiminuatftva N(20,max(gnPVal,4)),vvaldiminuattva N(20,max(gnPVal,4)),vvaldiminuatctva N(20,max(gnPVal,4)),id_set_fact N(20) Null,explicatie M Null,;
		id_part_rez N(10) Null,id_lucrare_rez N(10) Null,pretv_orig N(20,max(gnPPretV,precizie_max_pret_vz)),pretd N(20,max(gnPPretVal,precizie_max_pret_vz)) Null,id_valuta_d N(15) Null,;
		id_rata N(20) Null,data_rata d,data_scadenta d,nr_rata N(20),id_ctr N(20) Null,opt_facturare N(3),numar_contract c(100) Null,nume_val c(100),id_rul_aux n(10) null, ;
		nume_gestiune c(100) null, cgest c(50) null, taxcode N(6) null, codnc8 V(20) null, codcpv V(20) null)
Endproc	&&	creeaza_facturacrs
*********************************************************************************************************
Procedure creeaza_crsfacttemp
	If Used('creeaza_crsfacttemp')
		Use In creeaza_crsfacttemp
	Endif
	Create Cursor crsfacttemp(nrcrt N(10),id_articol N(20) null,denumire c(100),explicatie M Null,codmat c(50),codmatc c(50),codmatf c(50),codbare c(50),um c(10),cod_um_iso C(10) null, lot C(20) null, serie c(100) Null,;
		id_ctr N(10) Null,numar_contract c(100) Null,cantitate N(20,max(gnPCant,4)),;
		proc_Tvav N(20,4),id_jtva_coloana N(10),id_jtva_coloana_ex N(10) Null,;
		pretftva N(20,max(gnPPretV,precizie_max_pret_vz)),pretftvai N(20,max(gnPPretV,precizie_max_pret_vz)),pretctva N(20,max(gnPPretV,precizie_max_pret_vz)),pretctvai N(20,max(gnPPretV,precizie_max_pret_vz)),;
		valftva N(20,max(gnPc,4)),valftvai N(20,max(gnPc,4)),valtva N(20,max(gnPc,4)),valctva N(20,max(gnPc,4)),valctvai N(20,max(gnPc,4)),;
		proc_disc N(10,4),discountftva N(20,max(gnPPretV,4)),discountctva N(20,max(gnPPretV,precizie_max_pret_vz)),;
		valdiscountftva N(20,max(gnPC,4)),valdiscounttva N(20,max(gnPc,4)),valdiscountctva N(20,max(gnPc,4)), grup N(4), taxcode N(6) null, codnc8 V(20) null, codcpv V(20) null)
Endproc	&&	creeaza_crsfacttemp
*********************************************************************************************************
Procedure prelucreaza_facturacrs
	Lparameters tcCursorSursa,tcCursorDestinatie,tnProcTvav,tnDiscount,tnDiscountVal
	Local lnTotalBaza,lnTotalBazaVal,lnIdTemp
	*!*	modificare	ROAAUTO v 2.0.41 : am adaugat id_articol

	Insert Into (tcCursorDestinatie) (id_articol,id_temp,explicatie,denumire,lot,serie,codmat,codmatc,id_jtva_coloana,id_jtva_coloana_ex,taxcode,codmatf,codbare,pret_achizitie,cantitate,proc_Tvav,um,cod_um_iso,;
		cu_tva,pretctva,discountctva,valctva,valdiscountctva,valdiminuatctva,pretftva,;
		valftva,valtva,discountftva,valdiscountftva,valdiscounttva,valdiminuatftva,;
		valdiminuattva,vpretftva,vvalftva,vvaltva,vdiscountftva,vvaldiscountftva,vvaldiscounttva,;
		vvaldiminuatftva,vvaldiminuattva,id_set_fact,gestionabil,id_ctr,numar_contract,id_valuta,tip_valuta,Curs,multiplicator,nume_val,codnc8,codcpv) ;
		SELECT CAST(IIF(TYPE(tcCursorSursa+".id_articol")='U',0,id_articol) as N(20)) as id_articol,id_vanzare_det As id_temp,;
		explicatie,denumire,lot,serie,Nvl(codmat,Space(50)) As codmat,IIF(TYPE(tcCursorSursa + '.codmatc') = 'C', Nvl(codmatc,Space(50)), SPACE(50)) As codmatc,id_jtva_coloana,id_jtva_coloana_ex,taxcode,;
		NVL(codmatf,Space(50)) As codmatf,;
		Nvl(codbare,Space(50)) As codbare,Nvl(pret_achizitie,0.000) As pret_achizitie,Nvl(cantitate,1) As cantitate,proc_Tvav,Nvl(um,Space(50)) As um, ;
		CAST(IIF(TYPE(tcCursorSursa+".cod_um_iso")='U','',cod_um_iso) as C(5)) as cod_um_iso, ;
		pret_cu_tva As cu_tva,;
		Iif(pret_cu_tva = 1,Round(Pret,gnPPretV),Round(Pret,gnPPretV)+Round(Round(Pret,gnPPretV)*(proc_Tvav-1),gnPPretV)) As pretctva,;
		Iif(pret_cu_tva = 1,Round(discount_unitar,gnPPretV),0.000) As discountctva,;
		Iif(pret_cu_tva = 1,Round(Round(Pret,gnPPretV)*Nvl(cantitate,1),gnPc),Round(Round(Pret,gnPPretV)*Nvl(cantitate,1),gnPc)+;
		Round(Round(Round(Pret,gnPPretV)*Nvl(cantitate,1),gnPc)*(proc_Tvav-1),gnPc)) As valctva,;
		Iif(pret_cu_tva = 1,Round(Round(discount_unitar,gnPPretV)*Nvl(cantitate,1),gnPc),;
		ROUND((Round(discount_unitar,gnPPretV)-Round(Round(discount_unitar,gnPPretV)*(proc_Tvav-1),gnPPretV))*Nvl(cantitate,1),gnPc)) As valdiscountctva,;
		IIF(pret_cu_tva = 1,;
		ROUND((Round(Pret,gnPPretV)-Round(discount_unitar,gnPPretV))*Nvl(cantitate,1),gnPc),;
		ROUND((Round(Pret,gnPPretV)+Round(Round(Pret,gnPPretV)*(proc_Tvav-1),gnPPretV)-Round(discount_unitar,gnPPretV)-Round(Round(discount_unitar,gnPPretV)*(proc_Tvav-1),gnPPretV))*Nvl(cantitate,1),gnPc)) As valdiminuatctva,;
		IIF(pret_cu_tva = 0,;
		Round(Pret,gnPPretV),;
		Round(Round(Pret,gnPPretV)/proc_Tvav,gnPPretV)) As pretftva,;
		IIF(pret_cu_tva = 0,;
		Round(Round(Pret,gnPPretV)*Nvl(cantitate,1),gnPc),;
		ROUND(Round(Pret,gnPPretV)*Nvl(cantitate,1),gnPc) - ;
		Round(Round(Round(Pret,gnPPretV)*Nvl(cantitate,1),gnPc)*(proc_Tvav-1)/proc_Tvav, gnPc)) As valftva,;
		IIF(pret_cu_tva = 0,;
		Round(Round(Round(Pret,gnPPretV)*Nvl(cantitate,1),gnPc)*(proc_Tvav-1),gnPc),;
		Round(Round(Round(Pret,gnPPretV)*Nvl(cantitate,1),gnPc)*(proc_Tvav-1)/proc_Tvav, gnPc)) As valtva,;
		IIF(pret_cu_tva = 0,;
		Round(discount_unitar,gnPPretV),;
		Round(Round(discount_unitar,gnPPretV)/proc_Tvav,gnPPretV)) As discountftva,;
		IIF(pret_cu_tva = 0,;
		Round(Round(discount_unitar,gnPPretV)*Nvl(cantitate,1),gnPc),;
		ROUND(Round(discount_unitar,gnPPretV)*Nvl(cantitate,1),gnPc) - ;
		Round(Round(Round(discount_unitar,gnPPretV)*Nvl(cantitate,1),gnPc)*(proc_Tvav-1)/proc_Tvav, gnPc)) As valdiscountftva,;
		IIF(pret_cu_tva = 0,;
		ROUND(Round(Round(discount_unitar,gnPPretV)*Nvl(cantitate,1),gnPc)*(proc_Tvav-1),gnPc),;
		Round(Round(Round(discount_unitar,gnPPretV)*Nvl(cantitate,1),gnPc)*(proc_Tvav-1)/proc_Tvav, gnPc)) As valdiscounttva,;
		IIF(pret_cu_tva = 0,;
		Round((Round(Pret,gnPPretV)-Round(discount_unitar,gnPPretV))*Nvl(cantitate,1),gnPc),;
		ROUND((Round(Pret,gnPPretV)-Round(discount_unitar,gnPPretV))*Nvl(cantitate,1),gnPc) - ;
		Round(Round((Round(Pret,gnPPretV)-Round(discount_unitar,gnPPretV))*Nvl(cantitate,1),gnPc)*(proc_Tvav-1)/proc_Tvav, gnPc)) As valdiminuatftva,;
		IIF(pret_cu_tva = 0,;
		Round(Round((Round(Pret,gnPPretV)-Round(discount_unitar,gnPPretV))*Nvl(cantitate,1),gnPc)*(proc_Tvav-1),gnPc),;
		Round(Round((Round(Pret,gnPPretV)-Round(discount_unitar,gnPPretV))*Nvl(cantitate,1),gnPc)*(proc_Tvav-1)/proc_Tvav, gnPc)) As valdiminuattva, ;
		IIF(pret_cu_tva = 0,;
		Round(Pret_val,gnPVal),;
		Round(Round(Pret_val,gnPVal)/proc_Tvav,gnPVal)) As vpretftva,;
		IIF(pret_cu_tva = 0,;
		Round(Round(Pret_val,gnPVal)*Nvl(cantitate,1),gnPVal),;
		ROUND(Round(Pret_val,gnPVal)*Nvl(cantitate,1),gnPVal) - ;
		Round(Round(Round(Pret_val,gnPVal)*Nvl(cantitate,1),gnPVal)*(proc_Tvav-1)/proc_Tvav, gnPVal)) As vvalftva,;
		IIF(pret_cu_tva = 0,;
		Round(Round(Round(Pret_val,gnPVal)*Nvl(cantitate,1),gnPVal)*(proc_Tvav-1),gnPVal),;
		Round(Round(Round(Pret_val,gnPVal)*Nvl(cantitate,1),gnPVal)*(proc_Tvav-1)/proc_Tvav, gnPVal)) As vvaltva,;
		IIF(pret_cu_tva = 0,;
		Round(discount_unitar_val,gnPVal),;
		Round(Round(discount_unitar_val,gnPVal)/proc_Tvav,gnPVal)) As vdiscountftva,;
		IIF(pret_cu_tva = 0,;
		Round(Round(discount_unitar_val,gnPVal)*Nvl(cantitate,1),gnPVal),;
		ROUND(Round(discount_unitar_val,gnPVal)*Nvl(cantitate,1),gnPVal) - ;
		Round(Round(Round(discount_unitar_val,gnPVal)*Nvl(cantitate,1),gnPVal)*(proc_Tvav-1)/proc_Tvav, gnPVal)) As vvaldiscountftva,;
		IIF(pret_cu_tva = 0,;
		ROUND(Round(Round(discount_unitar_val,gnPVal)*Nvl(cantitate,1),gnPVal)*(proc_Tvav-1),gnPVal),;
		Round(Round(Round(discount_unitar_val,gnPVal)*Nvl(cantitate,1),gnPVal)*(proc_Tvav-1)/proc_Tvav, gnPVal)) As vvaldiscounttva,;
		IIF(pret_cu_tva = 0,;
		Round((Round(Pret_val,gnPVal)-Round(discount_unitar_val,gnPVal))*Nvl(cantitate,1),gnPVal),;
		ROUND((Round(Pret_val,gnPVal)-Round(discount_unitar_val,gnPVal))*Nvl(cantitate,1),gnPVal) - ;
		Round(Round((Round(Pret_val,gnPVal)-Round(discount_unitar_val,gnPVal))*Nvl(cantitate,1), gnPVal)*(proc_Tvav-1)/proc_Tvav, gnPVal)) As vvaldiminuatftva,;
		IIF(pret_cu_tva = 0,;
		Round(Round((Round(Pret_val,gnPVal)-Round(discount_unitar_val,gnPVal))*Nvl(cantitate,1),gnPVal)*(proc_Tvav-1),gnPVal),;
		Round(Round((Round(Pret_val,gnPVal)-Round(discount_unitar_val,gnPVal))*Nvl(cantitate,1), gnPVal)*(proc_Tvav-1)/proc_Tvav, gnPVal)) As vvaldiminuattva, ;
		Nvl(id_vanzare_set,00000000000000000) As id_set_fact,;
		Iif(Nvl(id_gestiune,0)<>0,1,0) As gestionabil, ;
		id_ctr,numar_contract,id_valuta,tip_valuta,Nvl(Curs,1.000) As Curs,NVL(multiplicator,1) as multiplicator,nume_val,	;
		CAST(IIF(TYPE(tcCursorSursa+".codnc8")='U','',codnc8) as V(20)) as codnc8, ;		
		CAST(IIF(TYPE(tcCursorSursa+".codcpv")='U','',codcpv) as V(20)) as codcpv ;				
		From (tcCursorSursa)
	If tnDiscount<> 0
		Select (tcCursorDestinatie)
		Calculate Sum(valdiminuatftva),Sum(vvaldiminuatftva),Max(id_temp) To lnTotalBaza,lnTotalBazaVal,lnIdTemp
		Select (tcCursorDestinatie)
		Append Blank
		Replace denumire With Replicate('Z',20),cantitate With 1,;
			pretftva With lnTotalBaza,discountftva With tnDiscount,valdiscountftva With tnDiscount,;
			valdiscounttva With Round(tnDiscount * (tnProcTvav - 1),gnPc),;
			vpretftva With lnTotalBazaVal,vdiscountftva With tnDiscountVal,vvaldiscountftva With tnDiscountVal,;
			vvaldiscounttva With Round(tnDiscountVal * (tnProcTvav - 1),gnPVal),;
			proc_Tvav With tnProcTvav,id_temp With lnIdTemp+1
	Endif
Endproc	&&	prelucreaza_facturacrs
*********************************************************************************************************
*!*	modificare v 2.0.67
*!*	Procedure creeaza_crsfactura_rate
*!*	If Used('crsfactura')
*!*		Use in crsfactura
*!*	EndIf
*!*	Create Cursor crsfactura(id_rata N(20),id_temp N(20),den_rata c(100),data_rata d, data_scadenta d,um c(10),denumire c(100),codmat c(100),codmatf c(100),codbare c(50),;
*!*						explicatie c(240),nr_rata N(20), valftva N(20,4), valtva N(20,4), valctva N(20,4),serie c(100),Curs N(20,4),id_jtva_coloana N(20),;
*!*						proc_tvav N(20,4),cu_tva N(1),pretftva N(20,4),pretctva N(20,4),tip_valuta N(1),vpretftva N(20,4),vpretctva N(20,4),discountftva N(20,4))
*!*	Endproc	&&	creeaza_crsfactura_rate
*!*	modificare v 2.0.67	^
*********************************************************************************************************
Procedure creeaza_backup_cursoare_facturare
*!*	modificare	03.06.2013
*!*		*!*	modificare 	v 2.0.56
*!*		*!*	Dimension laStructura(1,18),laStructura2(1,18)
*!*		sterge_backup_cursoare_facturare()
*!*		copiaza_structura_cursor([crsfactura],[bckpfactura])
*!*		*!*	Afields(laStructura,[crsfactura])
*!*		*!*	Create Cursor bckpfactura From Array laStructura
*!*		*!*	modificare 	v 2.0.56	^
*!*		Select * From crsfactura Into Cursor bckpfactura
*!*		*!*	If Used('crsfacturaset')
*!*		*!*		Afields(laStructura2,[crsfacturaset])
*!*		*!*		Create Cursor bckpfacturaset From Array laStructura2
*!*		*!*		Select * From crsfacturaset Into Cursor bckpfacturaset
*!*		*!*	Endif
*!*		Release laStructura,laStructura2
	creeaza_backup_cursoare([crsfactura])	&&	in oproceduri_comune.prg
*!*	modificare	03.06.2013	^
Endproc &&	creeaza_backup_cursoare_facturare
*********************************************************************************************************
Procedure repune_backup_cursoare_facturare
	*!*	modificare ROAFACTURARE	v 2.0.46
	*!*	If Used('bckpfactura')
	*!*		Select * From bckpfactura Into Cursor crsfactura Readwrite
	*!*		Use In bckpfactura
	*!*	Endif
	If Used('crsfacturaset')
		Use In crsfacturaset
	Endif
*!*	modificare	03.06.2013
*!*		If Used('bckpfactura')
*!*			Select crsfactura
*!*			lnRecno = Recno()
*!*			Select * From bckpfactura Into Cursor crsfactura Readwrite
*!*			Use In bckpfactura
*!*			Select crsfactura
*!*			If Reccount('crsfactura') > 0
*!*				Go Min(lnRecno,Reccount('crsfactura'))
*!*			Else
*!*				Go Top
*!*			Endif
*!*		Endif
*!*		*!*	modificare ROAFACTURARE	v 2.0.46	^
*!*		*!*	If Used('bckpfacturaset')
*!*		*!*		Select * From bckpfacturaset Into Cursor crsfacturaset Readwrite
*!*		*!*		Use In bckpfacturaset
*!*		*!*	Endif
	repune_backup_cursoare([crsfactura])	&&	in oproceduri_comune.prg
*!*	modificare	03.06.2013	^
Endproc	&&	repune_backup_cursoare_facturare
*********************************************************************************************************
Procedure sterge_backup_cursoare_facturare
*!*	modificare	03.06.2013
*!*		If Used('bckpfactura')
*!*			Use In bckpfactura
*!*		Endif
*!*		*!*	If Used('bckpfacturaset')
*!*		*!*		Use In bckpfacturaset
*!*		*!*	Endif
	sterge_backup_cursoare([crsfactura])
*!*	modificare	03.06.2013	^
Endproc	&&	sterge_backup_cursoare_facturare
*********************************************************************************************************
*********************************************************************************************************
Procedure alege_banci_facturare
	Local ofrmbanca
	lcSql = [select 0 as ales,Nvl(b.banca,b.denumire) as banca,b.cont_banca as cont,b.reg_comert as swift,] + ;
		[c.adresa from coresp_tip_part a ] + ;
		[join nom_parteneri b on a.id_part = b.id_part and b.sters = 0 and b.inactiv = 0 ] + ;
		[left join vadrese_parteneri c on a.id_part = c.id_part and c.principala = 1 ] + ;
		[where a.id_tip_part = ] + Iif(poDate.in_valuta=0,[24],[25]) + ;
		[ and b.cont_banca is not null order by 2]
	lnSucces = goExecutor.oExecute(lcSql,[crsbanci])
	If lnSucces < 0
		AMESSAGEBOX(goExecutor.cEroare,16,"Eroare")
		Return
	Endif
	gnButon = 1
	If Reccount('crsbanci')>1
		Select crsbanci
		Go Top
		ofrmbanca = Createobject('frm_alege_banca')
		ofrmbanca.Show(1)
		Release ofrmbanca
	Endif
	If gnButon = 1
		Select banca,Cont,swift,adresa From crsbanci Where ales = 1 Into Array laArray
		Dimension laArray(3,4)
		poFirma.banca = Iif(!Empty(laArray(1,1)),laArray(1,1),Null)
		poFirma.cont_banca = Iif(!Empty(laArray(1,2)),laArray(1,2),Null)
		poFirma.b_swift = Iif(!Empty(laArray(1,3)),laArray(1,3),Null)
		poFirma.b_adresa = Iif(!Empty(laArray(1,4)),laArray(1,4),Null)
		poFirma.banca2 = Iif(!Empty(laArray(2,1)),laArray(2,1),Null)
		poFirma.cont_banca2 = Iif(!Empty(laArray(2,2)),laArray(2,2),Null)
		poFirma.b_swift2 = Iif(!Empty(laArray(2,3)),laArray(2,3),Null)
		poFirma.b_adresa2 = Iif(!Empty(laArray(2,4)),laArray(2,4),Null)
		poFirma.banca3 = Iif(!Empty(laArray(3,1)),laArray(3,1),Null)
		poFirma.cont_banca3 = Iif(!Empty(laArray(3,2)),laArray(3,2),Null)
		poFirma.b_swift3 = Iif(!Empty(laArray(3,3)),laArray(3,3),Null)
		poFirma.b_adresa3 = Iif(!Empty(laArray(3,4)),laArray(3,4),Null)
	Endif
	Use In crsbanci
Endproc	&&	alege_banci_facturare
*********************************************************************************************************
Procedure alege_optiuni_listare_facturare
	Local loFrmOptiuniList,lcCursorSerii,lnVizibile

	lnVizibile = 5
	If Used('crsfactura')
		lcCursorSerii = [crsverserartfact]
		Select Recno() From crsfactura Where !Empty(Nvl(serie,'')) OR !EMPTY(NVL(lot, '')) Into Cursor (lcCursorSerii)
		If Used(lcCursorSerii)
			If Reccount(lcCursorSerii)=0
				poDate.listare_serii = 0
				lnVizibile = lnVizibile - 1
			Endif
			Use In (lcCursorSerii)
		Endif
	Endif

	If !Used('crsfacturaset')
		poDate.listare_componenta_set = 0
		poDate.listare_recapitulatie = 0
		lnVizibile = lnVizibile - 2
	Else
		If poDate.eProforma = 1
			poDate.listare_recapitulatie = 0
			lnVizibile = lnVizibile - 1
		Endif
	Endif

	*!*	modificare ROAFACTURARE	v 2.0.67
	*!*	If !(Nvl(gnListaLivrare,0) = 1 And poDate.eProforma = 0 And !Inlist(pnTipFacturare,1,2) And !InList(poDate.tip,4,7))
	If !(Nvl(gnListaLivrare,0) = 1 And poDate.eProforma = 0 And !Inlist(poDate.tip,4,7))
		*!*	modificare ROAFACTURARE	v 2.0.67	^
		poDate.listare_lista_livrare = 0
		poDate.listare_componenta_set_ll	= 0
		lnVizibile = lnVizibile - 2
	Else
		*!*	modificare ROAFACTURARE	v 2.0.61	:	daca nu exista articole gestionabile,nu se mai listeaza
		If Used('crsfactura')
			lcCursorGest = [crsvergestartfact]
			Select Recno() From crsfactura Where gestionabil = 1 Into Cursor (lcCursorGest)
			If Used(lcCursorGest)
				If Reccount(lcCursorGest)=0
					poDate.listare_lista_livrare = 0
					lnVizibile = lnVizibile - 1
				Endif
				Use In (lcCursorGest)
			Endif
		Endif
		*!*		If !Used('crsfacturaset')
		If !Used('crsfacturaset') Or poDate.listare_lista_livrare = 0
			*!*	modificare ROAFACTURARE	v 2.0.61	^
			poDate.listare_componenta_set_ll	= 0
			lnVizibile = lnVizibile - 1
		Endif
	Endif

	If lnVizibile <> 0
		loFrmOptiuniList = Createobject('frm_alege_optlistfact')
		loFrmOptiuniList.Show(1)
	Endif
	Release loFrmOptiuniList,lcCursorSerii,lnVizibile
Endproc	&&	alege_optiuni_listare_facturare
*********************************************************************************************************
Procedure completeaza_explicatie_tva
	Lparameters tcCursor
	Local lnNrCote,lnNrCote0,lcListaId,lcListaIdEx,lcSql,lnSucces

	Use In (SELECT('crsproctvav'))

	IF TYPE(tcCursor + '.id_jtva_coloana_ex') = 'N'
		Select Distinct id_jtva_coloana,id_jtva_coloana_ex, proc_tva From (tcCursor) Into Cursor crsproctvav
	else
		Select Distinct id_jtva_coloana, 0 as id_jtva_coloana_ex, proc_tva From (tcCursor) Into Cursor crsproctvav
	ENDIF 	
	If Reccount('crsproctvav')>0
		lnNrCote = Reccount('crsproctvav')
		lnNrCote0 = 0
		lcListaId = []
		lcListaIdEx = [-9999]		
		Select crsproctvav
		Scan For proc_tva = 1 And Nvl(id_jtva_coloana,-1)<>-1
			lcListaId = lcListaId + [,] + Alltrim(Str(id_jtva_coloana))
			IF !EMPTY(NVL(id_jtva_coloana_ex,0))
				lcListaIdEx = lcListaIdEx + [,] + Alltrim(Str(id_jtva_coloana_ex))
			ENDIF 	
			lnNrCote0 = lnNrCote0 + 1
		ENDSCAN
		lcListaId = IIF(!EMPTY(m.lcListaId), Substr(lcListaId,2), '')
		*lcListaIdEx = IIF(!EMPTY(m.lcListaIdEx), Substr(lcListaIdEx,2), '')		

		If lnNrCote0 > 0
			*!*	modificare ROAFACTURARE 2.0.80 : am adaugat NVL(explicatie,denumire) in loc de denumire
			TEXT TO lcSql TEXTMERGE noshow
				SELECT j.id_jtva_coloana, jex.id_jtva_coloana_ex, NVL(jex.denumire, j.denumire) as denumire from
				(select id_jtva_coloana,NVL(explicatie,denumire) as denumire from jtva_coloane where id_jtva_coloana in (<<m.lcListaId>>)) j left join
				(select id_jtva_coloana, id as id_jtva_coloana_ex, explicatie as denumire from jtva_coloane_explicatii where id in (<<m.lcListaIdEx>>)) jex on jex.id_jtva_coloana = j.id_jtva_coloana		
			ENDTEXT 
			lnSucces = goExecutor.oExecute(lcSql,[crsexpl])
			If lnSucces < 0
				AMESSAGEBOX(goExecutor.cEroare,16,"Eroare")
			Else
				If Reccount('crsexpl')=1 And lnNrCote = lnNrCote0
					Select crsexpl
					poDate.explicatie_tva = Alltrim(denumire)
				ENDIF 
				
				Select crsexpl
				Scan
					lnIdJtva = NVL(id_jtva_coloana,0)
					lnIdJtvaEx = NVL(id_jtva_coloana_ex,0)
					lcDenumire = Alltrim(denumire)
					Select (tcCursor)
					IF TYPE(tcCursor + '.id_jtva_coloana_ex') = 'N' AND !EMPTY(m.lnIdJtvaEx)
						Replace expltva With m.lcDenumire For NVL(id_jtva_coloana_ex,0) = m.lnIdJtvaEx	
					ELSE 
						Replace expltva With m.lcDenumire For NVL(id_jtva_coloana,0) = m.lnIdJtva
					ENDIF 	
					Select crsexpl
				Endscan

				Use In crsexpl
			Endif
		Endif
	Endif

	If Used('crsproctvav')
		Use In crsproctvav
	Endif
Endproc	&&	completeaza_explicatie_tva
*********************************************************************************************************
Procedure completeaza_explicatie_contract
	Lparameters tcCursor
	Local lnPozI,lnPozF,lnIdCtr,lnIdCtrPrec,lnDimArray,lnIndex,lnIndexMax,lcNrContract,lnNrInreg
	lnDimArray = 20
	Dimension laContracte[lnDimArray,3]	&&	1	-	id_ctr;	2	-	numar si data contract;	3	-	pozitiile din factura coresp.ctr.
	lnIndexMax = 1
	lnIdCtrPrec = Null
	lcNrContract = []
	lnNrInreg = 0
	lnIndex = 1
	Select (tcCursor)
	Scan For cantitate <> 0
		lnIdCtr = id_ctr
		lnNrInreg = lnNrInreg+1
		Do Case
			Case Isnull(lnIdCtrPrec) And Isnull(lnIdCtr)
				*!*	nimic
			Case Nvl(lnIdCtr,-1) = Nvl(lnIdCtrPrec,-1)
				lnPozF = lnNrInreg	&&	Recno()
			Otherwise
				If !Isnull(lnIdCtrPrec)
					lnIndex = Ascan(laContracte,lnIdCtrPrec,1,lnIndexMax*3,1,8)
					If lnIndex = 0
						lnIndex = lnIndexMax
						lnIndexMax = lnIndexMax + 1
						laContracte[lnIndex,1] = lnIdCtrPrec
						laContracte[lnIndex,2] = lcNrContract
						laContracte[lnIndex,3] = []
					Endif
					If lnPozI = lnPozF
						laContracte[lnIndex,3] = laContracte[lnIndex,3] + Alltrim(Str(lnPozI)) + [,]
					Else
						laContracte[lnIndex,3] = laContracte[lnIndex,3] + Alltrim(Str(lnPozI)) + [-] + Alltrim(Str(lnPozF)) + [,]
					Endif
				Endif
				If !Isnull(lnIdCtr)
					lnPozI = lnNrInreg	&&	Recno()
					lnPozF = lnNrInreg	&&	Recno()
				Else
					lnPozI = 0
					lnPozF = 0
				Endif
		Endcase
		lnIdCtrPrec = lnIdCtr
		lcNrContract = Alltrim(numar_contract)
	Endscan
	If !Isnull(lnIdCtrPrec)
		lnIndex = Ascan(laContracte,lnIdCtrPrec,1,lnIndexMax*3,1,8)
		If lnIndex = 0
			lnIndex = lnIndexMax
			laContracte[lnIndex,1] = lnIdCtrPrec
			laContracte[lnIndex,2] = lcNrContract
			laContracte[lnIndex,3] = []
		Endif
		If lnPozI = lnPozF
			laContracte[lnIndex,3] = laContracte[lnIndex,3] + Alltrim(Str(lnPozI)) + [,]
		Else
			laContracte[lnIndex,3] = laContracte[lnIndex,3] + Alltrim(Str(lnPozI)) + [-] + Alltrim(Str(lnPozF)) + [,]
		Endif
	Endif

	*!*	modificare	ROAFACTURARE v 2.0.77
	&&	1-3 : conditia !Empty(Nvl(laContracte[lnIndex,2],[])) a fost adaugata pentru ca daca
	&& pe contract nu e stipulat sa apara textul aditional, atunci pe factura nu aparea nr. contractului, ci doar
	&& "Conform contractului nr. "
	pcExplicatieContract = []
	If lnIndexMax = 1
		If !Empty(Nvl(laContracte[lnIndex,2],[]))	&&	1
			pcExplicatieContract = [Conform contractului nr. ] + laContracte[lnIndex,2]
		Endif										&&	2
	Else
		For lnIndex = 1 To lnIndexMax
			If !Empty(laContracte[lnIndex,1]) And !Empty(Nvl(laContracte[lnIndex,2],[]))	&&	3
				pcExplicatieContract = pcExplicatieContract + [;] + Substr(laContracte[lnIndex,3],1,Len(laContracte[lnIndex,3])-1) + [ cf. contractului nr. ] + laContracte[lnIndex,2]
			Endif
		Endfor
		pcExplicatieContract = Substr(pcExplicatieContract,2)
	Endif
	Release lnPozI,lnPozF,lnIdCtr, lnIdCtrPrec, lnDimArray,lnIndex,lcNrContract,lnIndexMax
Endproc	&&	completeaza_explicatie_contract
*********************************************************************************************************
