***************************************************************************************************************
****	Clasa:
****		oDateListare
****	Proceduri:
****		rap_avize
****		rap_facturi
****		rap_incasari
****		rap_articole
****		rap_articole_tr
****		rap_avize_transfer
****		rap_centralizator
****		rap_centr_subgrupe
***************************************************************************************************************
Define Class oDateListare As Custom
	datai			=	{}
	dataf	 		=	{}
	data_referinta	=	{}
	csql			=	[]
	id_client		=	Null
	nume_client		=	[]
	id_agent		=	Null
	nume_agent		=	[]
	id_delegat		=	Null
	nume_delegat	=	[]
	id_masina		=	Null
	nrinmat			=	[]
	categorie		=	[]
	id_categorie_entitate	=	Null
	denumire		=	[]
	id_articol		=	Null
	grupa			=	[]
	id_grupa		=	Null
	subgrupa		=	[]
	id_subgrupa		=	Null
	id_gestiune_sursa	= Null
	gestiune_sursa		= []
	id_gestiune_dest	= Null
	gestiune_dest		= []
	id_util				= Null
	utilizator			= []
	id_gestiune	= Null
	nume_gestiune		= []
	subgr_art			= []
	id_furnart 			= Null
	furnizor_art		= []
	*!*		tip_raport		=	1
	*!*		Dimension acoloane(30,4)	&&	1	-	nume_camp
	*!*									&&	2	-	ordine la grupare
	*!*									&&	3	-	ordine la order by
	*!*									&&	4	-	sens la order by	(	1	-	ASC,	2	-	DESC	)
	Procedure Init
		With This
			ldData			=	Ttod(get_ora())
			If Year(ldData) * 12 + Month(ldData) <> gnAn * 12 + gnLuna
				.datai 		= Date(gnAn, gnLuna, 1)
				.dataf		= Gomonth(Date(gnAn, gnLuna, 1), 1) - 1
				.data_referinta = .dataf
			Else
				.dataf			=	ldData
				.data_referinta	=	ldData
				.datai			=	Date(Year(ldData), Month(ldData), 1)
			Endif
		Endwith
	Endproc

Enddefine
***************************************************************************************************************
********************************************** INCEPUT: rap_avize *********************************************
Procedure rap_avize
	Private poListare
	poListare = Createobject("oDateListare")
	ofrmdatelistare = Createobject("frm_date_rapoarte_avize")
	ofrmdatelistare.Show(1)
	Release poListare, ofrmdatelistare
Endproc &&	rap_avize
********************************************** SFARSIT: rap_avize *********************************************
********************************************* INCEPUT: rap_facturi ********************************************
Procedure rap_facturi
	Private poListare
	poListare = Createobject("oDateListare")
	ofrmdatelistare = Createobject("frm_date_rapoarte_facturi")
	ofrmdatelistare.Show(1)
	Release poListare, ofrmdatelistare
Endproc &&	rap_facturi
********************************************** SFARSIT: rap_facturi *******************************************
******************************************** INCEPUT: rap_incasari ********************************************
Procedure rap_incasari
	Private poListare
	poListare = Createobject("oDateListare")
	ofrmdatelistare = Createobject("frm_date_rapoarte_incasari")
	ofrmdatelistare.Show(1)
	Release poListare, ofrmdatelistare
Endproc	&&	rap_incasari
******************************************** SFARSIT: rap_incasari ********************************************
******************************************** INCEPUT: rap_articole ********************************************
Procedure rap_articole
	Private poListare
	poListare = Createobject("oDateListare")
	ofrmdatelistare = Createobject("frm_date_rapoarte_articole")
	ofrmdatelistare.Show(1)
	Release poListare, ofrmdatelistare
Endproc	&&	rap_articole
******************************************** SFARSIT: rap_articole ********************************************
******************************************** INCEPUT: rap_articole_tr ********************************************
Procedure rap_articole_tr
	Private poListare
	poListare = Createobject("oDateListare")
	ofrmdatelistare = Createobject("frm_date_rapoarte_articole_tr")
	ofrmdatelistare.Show(1)
	Release poListare, ofrmdatelistare
Endproc	&&	rap_articole_tr
******************************************** SFARSIT: rap_articole_tr ********************************************
******************************************** INCEPUT: rap_articole_v ********************************************
Procedure rap_articole_v
	Private poListare
	poListare = Createobject("oDateListare")
	ofrmdatelistare = Createobject("frm_date_rapoarte_articole_v")
	ofrmdatelistare.Show(1)
	Release poListare, ofrmdatelistare
Endproc	&&	rap_articole_tr
******************************************** SFARSIT: rap_articole_v ********************************************
***************************************** INCEPUT: rap_avize_transfer *****************************************
Procedure rap_avize_transfer
	Private poListare
	poListare = Createobject("oDateListare")
	ofrmdatelistare = Createobject("frm_date_rapoarte_avize_tr")
	ofrmdatelistare.Show(1)
	Release poListare, ofrmdatelistare
Endproc	&&	rap_avize_transfer
***************************************** SFARSIT: rap_avize_transfer *****************************************
***************************************** INCEPUT: rap_centralizator ******************************************
Procedure rap_centralizator
	Private pcDataora, pcPerioada, pdDataI, pdDataF, pnTotalFTva1, pnTotalTva1, pnTotalFTva2, pnTotalTva2
	Store {} To pdDataI, pdDataF
	Store 0 To pnTotalFaraTva1, pnTotalTva1, pnTotalFaraTva2, pnTotalTva2
	Local lcOrder, lcSql, lcFiltru, lnSucces, lnNefacturatFTva, lnNefacturatTva

	ofrmperioada = Createobject('frm_perioada_zzllaaaa')
	ofrmperioada.Show(1)
	If gnButon = 2
		Return
	Endif

	pcPerioada = [Perioada ] + Dtoc(pdDataI) + [ - ] + Dtoc(pdDataF)

	Create Cursor crsCentralizator(tip N(2), categorie c(100), valftva N(20, Max(gnPC, 4)), valtva N(20, Max(gnPC, 4)))
	*!*	tip = 1	>>	nu afiseaza sumele
	pcDataora = get_ora(2)

	If Used('crsCentrTot')
		Use In crsCentrTot
	Endif
	lcSql1 = [select 1 as crs,tip,SUM(suma_fara_tva) as valftva,SUM(suma_tva) as valtva from ] + ;
		gcS + [.fact_vrap_centralizator_tipuri where data_act between ?pdDataI and ?pdDataF] + gcCondSucursala + ;
		[ group by tip]
	lcSql2 = [select 2 as crs,null as tip,SUM(suma_fara_tva) as valftva,SUM(suma_tva) as valtva from ] + ;
		gcS + [.fact_vrap_centralizator_art where data_act between ?pdDataI and ?pdDataF] + gcCondSucursala
	lcSql3 = [select 3 as crs,null as tip,SUM(suma_fara_tva) as valftva,SUM(suma_tva) as valtva from ] + ;
		gcS + [.fact_vrap_centralizator_rate where data_act between ?pdDataI and ?pdDataF] + gcCondSucursala
	lcSql4 = [select 4 as crs,null as tip,SUM(disc_fara_tva) as valftva,SUM(disc_tva) as valtva from ] + ;
		gcS + [.fact_vrap_centralizator_fact where data_act between ?pdDataI and ?pdDataF] + gcCondSucursala
	lcSql = lcSql1 + [ union all ] + lcSql2 + [ union all ] + lcSql3 + [ union all ] + lcSql4
	lcCursor = [crsCentrTot]
	lnSucces = goExecutor.oexecute(lcSql, lcCursor)
	If lnSucces < 0
		amessagebox(goExecutor.oPrelucrareEroare(), 16, "Eroare")
		Use In crsCentrTot
		Return
	Endif

	***	prelucrare raport
	*!*	Facturi
	Select crsCentralizator
	Append Blank
	Replace categorie With "1. FACTURI"
	Select Sum(valftva) As valftva, Sum(valtva) As valtva From crsCentrTot Where tip < 20 And crs = 1 Into Cursor crsTemp
	Select crsTemp
	Scatter Name poCentr
	Select crsCentralizator
	Replace valftva With Nvl(poCentr.valftva, 0), valtva With Nvl(poCentr.valtva, 0)
	pnTotalFTva1 = Nvl(poCentr.valftva, 0)
	pnTotalTva1 = Nvl(poCentr.valtva, 0)

	*!*	Avize catre clienti
	Select crsCentralizator
	Append Blank
	Replace categorie With "2. AVIZE CATRE CLIENTI", tip With 1

	*!*	Valoare totala
	Append Blank
	Replace categorie With Space(5) + "a) Valoare totala"
	Select Sum(valftva) As valftva, Sum(valtva) As valtva From crsCentrTot Where Inlist(tip, 21, 22) And crs = 1 Into Cursor crsTemp
	Select crsTemp
	Scatter Name poCentr
	Select crsCentralizator
	Replace valftva With Nvl(poCentr.valftva, 0), valtva With Nvl(poCentr.valtva, 0)
	lnNefacturatFTva = Nvl(poCentr.valftva, 0)
	lnNefacturatTva = Nvl(poCentr.valtva, 0)

	*!*	Valoare facturata
	Append Blank
	Replace categorie With Space(5) + "b) Valoare facturata"
	Select Sum(valftva) As valftva, Sum(valtva) As valtva From crsCentrTot Where tip = 4 And crs = 1 Into Cursor crsTemp
	Select crsTemp
	Scatter Name poCentr
	Select crsCentralizator
	Replace valftva With Nvl(poCentr.valftva, 0), valtva With Nvl(poCentr.valtva, 0)
	lnNefacturatFTva = lnNefacturatFTva - Nvl(poCentr.valftva, 0)
	lnNefacturatTva = lnNefacturatTva - Nvl(poCentr.valtva, 0)

	*!*	Valoare retururi
	Append Blank
	Replace categorie With Space(5) + "c) Valoare retururi"
	Select Sum(valftva) As valftva, Sum(valtva) As valtva From crsCentrTot Where tip = 24 And crs = 1 Into Cursor crsTemp
	Select crsTemp
	Scatter Name poCentr
	Select crsCentralizator
	Replace valftva With Nvl(poCentr.valftva, 0), valtva With Nvl(poCentr.valtva, 0)
	lnNefacturatFTva = lnNefacturatFTva + Nvl(poCentr.valftva, 0)
	lnNefacturatTva = lnNefacturatTva + Nvl(poCentr.valtva, 0)

	*!*	Valoare nefacturata
	Append Blank
	Replace categorie With Space(5) + "d) Valoare nefacturata (a-b+c)"
	Replace valftva With lnNefacturatFTva, valtva With lnNefacturatTva
	pnTotalFTva1 = pnTotalFTva1 + lnNefacturatFTva
	pnTotalTva1 = pnTotalTva1 + lnNefacturatTva

	*!*	Transferuri intre subunitati
	Select crsCentralizator
	Append Blank
	Replace categorie With "3. TRANSFERURI INTRE SUBUNITATI"
	Select Sum(valftva) As valftva, Sum(valtva) As valtva From crsCentrTot Where tip = 23 And crs = 1 Into Cursor crsTemp
	Select crsTemp
	Scatter Name poCentr
	Select crsCentralizator
	Replace valftva With Nvl(poCentr.valftva, 0), valtva With Nvl(poCentr.valtva, 0)
	pnTotalFTva1 = pnTotalFTva1 + Nvl(poCentr.valftva, 0)
	pnTotalTva1 = pnTotalTva1 + Nvl(poCentr.valtva, 0)

	*!*	Articole
	Select crsCentralizator
	Append Blank
	Replace categorie With "4. TOTAL ARTICOLE VANDUTE"
	Select crsCentrTot
	Locate For crs = 2
	Scatter Name poCentr
	Select crsCentralizator
	Replace valftva With Nvl(poCentr.valftva, 0), valtva With Nvl(poCentr.valtva, 0)
	pnTotalFTva2 = Nvl(poCentr.valftva, 0)
	pnTotalTva2 = Nvl(poCentr.valtva, 0)

	*!*	Articole
	Select crsCentralizator
	Append Blank
	Replace categorie With "5. TOTAL RATE CONTRACTE"
	Select crsCentrTot
	Locate For crs = 3
	Scatter Name poCentr
	Select crsCentralizator
	Replace valftva With Nvl(poCentr.valftva, 0), valtva With Nvl(poCentr.valtva, 0)
	pnTotalFTva2 = pnTotalFTva2 + Nvl(poCentr.valftva, 0)
	pnTotalTva2 = pnTotalTva2 + Nvl(poCentr.valtva, 0)

	*!*	Discount-uri globale
	Select crsCentralizator
	Append Blank
	Replace categorie With "6. TOTAL DISCOUNT GLOBAL"
	Select crsCentrTot
	Locate For crs = 4
	Scatter Name poCentr
	Select crsCentralizator
	Replace valftva With Nvl(poCentr.valftva, 0), valtva With Nvl(poCentr.valtva, 0)
	pnTotalFTva2 = pnTotalFTva2 - Nvl(poCentr.valftva, 0)
	pnTotalTva2 = pnTotalTva2 - Nvl(poCentr.valtva, 0)

	Release poCentr
	Use In crsTemp

	If Used('crsCentrTot')
		Use In crsCentrTot
	Endif

	If Reccount('crsCentralizator') > 0
		Keyboard "{ctrl+f10}"
		Select crsCentralizator
		Report Form raport_centralizator To Printer Prompt Preview
	Else
		amessagebox("Nu exista inregistrari pentru listare!", 0 + 48, "Atentie")
	Endif

	If Used('crsCentralizator')
		Use In crsCentralizator
	Endif

	Release pcDataora, pcPerioada, pdDataI, pdDataF, pnTotalFTva1, pnTotalTva1, pnTotalFTva2, pnTotalTva2
Endproc	&&	rap_centralizator
***************************************** SFARSIT: rap_centralizator ******************************************
***************************************** INCEPUT: rap_centr_subgrupe *****************************************
Procedure rap_centr_subgrupe
	Local lcSql, lnSucces, lcCursor
	Private pcDataora, pcPerioada, pdDataI, pdDataF, pcTitlu
	Store {} To pdDataI, pdDataF
	lcCursor = [crscentrsubgr]

	ofrmperioada = Createobject('frm_perioada_zzllaaaa')
	ofrmperioada.Show(1)
	If gnButon = 1
		pcPerioada = [Perioada ] + Dtoc(pdDataI) + [ - ] + Dtoc(pdDataF)
	Else
		Return
	Endif

	lcXMLGestiune = caut_gestiune_xml(2)
	If !Empty(lcXMLGestiune) And gnButon = 1
		Xmltocursor(lcXMLGestiune, "crsGestTemp")
		If Reccount('crsGestTemp') = 0
			amessagebox('Nu ati selectat nici o gestiune!', 0 + 48, 'Inventar')
			Return
		Endif
		pcTitlu = [CENTRALIZATOR VALORI PE SUBGRUPE DE ARTICOLE]
		lcListaIdGestiuni = cursor2lista("crsGestTemp", "id_gestiune", ",")
		Use In crsGestTemp
	Else
		Return
	Endif

	If Used(lcCursor)
		Use In (lcCursor)
	Endif
	lcSql = [select subgrupa,nume_gestiune,id_gestiune,Sum(val_ach) as val_ach,Sum(val_vanz) as val_vanz from ] + gcS + [.fact_vcentr_val_subgrupe ] + ;
		[where data_act between ?pdDataI and ?pdDataF and id_gestiune in (] + lcListaIdGestiuni + [) ] + ;
		[group by id_gestiune,nume_gestiune,subgrupa ] + ;
		[order by nume_gestiune,subgrupa]
	lnSucces = goExecutor.oexecute(lcSql, lcCursor)
	If lnSucces < 0
		amessagebox(goExecutor.cEroare, 16, "Eroare")
	Else
		goExport.export2frx(lcCursor, [rap_centr_val_subgrupe])	&&	, , , , , , .T.)
	Endif
	If Used(lcCursor)
		Use In (lcCursor)
	Endif

	Release lcSql, lnSucces, pcDataora, pcPerioada, pdDataI, pdDataF, pcTitlu
Endproc	&&	rap_centr_subgrupe
***************************************** SFARSIT: rap_centr_subgrupe *****************************************
Procedure centr_avize_ruta


	lcFiltru = [2=2]
	lcSchema = [id_lucrare n(10),nrord c(100)]
	lcOrder = [nrord]
	lccoloane = [nrord]
	lcTitlu = [ALEGETI LUCRAREA]
	lcTitluColoane = [Lucrare]
	*!*	llMaiMulteMasini = tlMaiMulteMasini
	lcSelect = [select id_lucrare,nrord  from vnom_lucrari]
	lcFiltruOriginal = [inactiv = 0]
	*!*	lcNumeProc = [nom_nomrute]
	llToateIreg = .F.
	lcPrimaColoana = [nrord]
	lnPornire = 1
	*!*	lnTipReturn = Iif(tlMaiMulteMasini, 1, 0)
	lcIdColumn = "id_lucrare"
	loCauta = cauta_alfa(lcSelect, lcFiltru, lcSchema, lcOrder, lccoloane, lcTitlu, lcTitluColoane, , llToateIreg, lcFiltruOriginal)&&, lcPrimaColoana, lnPornire, , lcIdColumn)
	*!*	loCauta = cauta_alfa(lcSelect, lcFiltru, lcSchema, lcOrder, lccoloane, lcTitlu, lcTitluColoane, lcNumeProc, tlToateIreg, lcFiltruOriginal)		&& 11.07.2007


	If Empty(loCauta.id_lucrare) Or Isnull(loCauta.id_lucrare)
		Return
	Endif



	lcSql = [ select * from fact_vrap_avize_ruta where id_lucrare = ] + Alltrim(Str(loCauta.id_lucrare)) + [ order by ruta,data_act,numar_act]
	lcCursor = [crsavizeruta]
	lnSucces = goExecutor.oexecute(lcSql, lcCursor)
	If lnSucces < 0
		amessagebox(goExecutor.oPrelucrareEroare(), 16, "Eroare")
		Use In crsavizeruta
		Return
	Endif
	Select crsavizeruta
	If Reccount() < 1
		amessagebox('Nu exista date pentru comanda selectata!', 0 + 64, 'Atentie')
		Use In crsavizeruta

		Return
	Endif
	Private pcDataora
	pcDataora = get_ora(2)
	Select crsavizeruta
	Report Form  rap_avizerute To Printer Prompt Preview

	Use In crsavizeruta
Endproc


*-------------------------------------------------------------------------------

Procedure centr_art_ruta


	lcFiltru = [2=2]
	lcSchema = [id_lucrare n(10),nrord c(100)]
	lcOrder = [nrord]
	lccoloane = [nrord]
	lcTitlu = [ALEGETI LUCRAREA]
	lcTitluColoane = [Lucrare]
	*!*	llMaiMulteMasini = tlMaiMulteMasini
	lcSelect = [select id_lucrare,nrord  from vnom_lucrari]
	lcFiltruOriginal = [inactiv = 0]
	*!*	lcNumeProc = [nom_nomrute]
	llToateIreg = .F.
	lcPrimaColoana = [nrord]
	lnPornire = 1
	*!*	lnTipReturn = Iif(tlMaiMulteMasini, 1, 0)
	lcIdColumn = "id_lucrare"
	loCauta = cauta_alfa(lcSelect, lcFiltru, lcSchema, lcOrder, lccoloane, lcTitlu, lcTitluColoane, , llToateIreg, lcFiltruOriginal)&&, lcPrimaColoana, lnPornire, , lcIdColumn)
	*!*	loCauta = cauta_alfa(lcSelect, lcFiltru, lcSchema, lcOrder, lccoloane, lcTitlu, lcTitluColoane, lcNumeProc, tlToateIreg, lcFiltruOriginal)		&& 11.07.2007


	If Empty(loCauta.id_lucrare) Or Isnull(loCauta.id_lucrare)
		Return
	Endif



	lcSql = [ select * from fact_vrap_art_ruta where id_lucrare = ] + Alltrim(Str(loCauta.id_lucrare)) + [ order by ruta,subgrupa,denumire]
	lcCursor = [crsartruta]
	lnSucces = goExecutor.oexecute(lcSql, lcCursor)
	If lnSucces < 0
		amessagebox(goExecutor.oPrelucrareEroare(), 16, "Eroare")
		Use In crsartruta
		Return
	Endif
	Select crsartruta
	If Reccount() < 1
		amessagebox('Nu exista date pentru comanda selectata!', 0 + 64, 'Atentie')
		Use In crsartruta

		Return
	Endif
	Private pcDataora
	pcDataora = get_ora(2)
	Select crsartruta
	Report Form  rap_ARTrute To Printer Prompt Preview

	Use In crsartruta
Endproc

*---------------------------------------------------------------------------
Procedure rap_factav_doc && lansat din rap_listfactav_centr, lansat la randul lui din frm_facturi (ofacturare.vcx)
	Local lnNrIreg, lcFiltruInit, lnRecSel
	Store 0 To lnNrIreg, lnRecSel
	Store '' To lcFiltruInit

	Select crsfacturi
	lcFiltruInit = Set("Filter")
	lnRecSel = Recno()
	Set Filter To ales = 1

	Count For ales = 1 To lnNrIreg
	*!*	modificare	v 2.0.40
	*!*	If Reccount() <1
	If lnNrIreg < 1
		*!*	modificare	v 2.0.40	^
		amessagebox('Nu ati selectat vreun document!', 0 + 48, 'Atentie')
		*!*	modificare	v 2.0.40
		*!*		Return
		*!*	EndIf
	Else
		*!*	modificare	v 2.0.40	^
		Private pcDataora
		pcDataora = get_ora(2)
		Select crsfacturi
		Report Form rap_avizedoc To Printer Prompt Preview
		*!*	modificare	v 2.0.40
	Endif
	*!*	modificare	v 2.0.40	^
	Select crsfacturi
	Set Filter To &lcFiltruInit
	If lnRecSel > 0
		Goto lnRecSel
	Endif
Endproc
*---------------------------------------------------------------------------
Procedure rap_factav_grupate_doc && lansat din rap_listfactav_centr, lansat la randul lui din frm_facturi (ofacturare.vcx)
	Local lnNrIreg, lcFiltruInit, lnRecSel
	Store 0 To lnNrIreg, lnRecSel
	Store '' To lcFiltruInit

	Select crsfacturi
	lcFiltruInit = Set("Filter")
	lnRecSel = Recno()
	Set Filter To ales = 1

	Count For ales = 1 To lnNrIreg
	*!*	modificare	v 2.0.40
	*!*	If Reccount() <1
	If lnNrIreg < 1
		*!*	modificare	v 2.0.40	^
		amessagebox('Nu ati selectat vreun document!', 0 + 48, 'Atentie')
		*!*	modificare	v 2.0.40
		*!*		Return
		*!*	EndIf
	Else
		*!*	modificare	v 2.0.40	^
		Private pcDataora
		pcDataora = get_ora(2)
		Select crsfacturi
		*!* modificare	v 2.0.63
		If Used('crsdocgrup')
			Use In crsdocgrup
		Endif
		Select  client, numar_act, data_act, valoarea, total_cu_tva, ;
			PADR(Iif(Inlist(tip, 1, 2, 3, 4, 5, 6, 8, 9, 10, -1, -2, -3, -4, -8, -11), 'FACTURI', ;
				IIF(Inlist(tip, 21, 22, 24, 26, -7, -13), 'AVIZE CATRE CLIENTI', ;
				  IIF(Inlist(tip, 23, 25, 27, 30, 41, -6), 'TRANSFERURI', ;
					IIF(Inlist(tip, 28, 29), 'AVIZE CATRE CLIENTI DEBITORI', ;
					  IIF(Inlist(tip, -9), 'AVIZE DE PROTOCOL', ;
						IIF(Inlist(tip, -10), 'AVIZE DE SPONSORIZARI', ;
						  IIF(Inlist(tip, 7), 'CREDIT NOTE', ;
							IIF(Inlist(tip, 42, 47), 'CLIENTI IN CUSTODIE - COMANDA', ;
							  IIF(Inlist(tip, 43), 'BONURI FISCALE MAGAZINE', '<FARA TIP>'))))))))), 70, ' ') As explicatie ;
			FROM crsfacturi ;
			ORDER By 6, 1, 3 ;
			INTO Cursor crsdocgrup

		Report Form rap_avizedoc_grup To Printer Prompt Preview
		Use In crsdocgrup
		*!* modificare v 2.0.63 ^

		*!*	modificare	v 2.0.40
	Endif
	*!*	modificare	v 2.0.40	^
	Select crsfacturi
	Set Filter To &lcFiltruInit
	If lnRecSel > 0
		Goto lnRecSel
	Endif
Endproc
*---------------------------------------------------------------------------
Procedure rap_factav_art	&& lansat din rap_listfactav_centr, lansat la randul lui din frm_facturi (ofacturare.vcx)
	Local lcidvanzari, lcSql, lnSucces
	Store '' To lcidvanzari
	Local lnNrIreg, lcFiltruInit, lnRecSel
	Store 0 To lnNrIreg, lnRecSel
	Store '' To lcFiltruInit

	Select crsfacturi
	lcFiltruInit = Set("Filter")
	lnRecSel = Recno()
	Set Filter To ales = 1

	Count For ales = 1 To lnNrIreg
	*!*	modificare	v 2.0.40
	*!*	If Reccount() <1
	If lnNrIreg < 1
		*!*	modificare	v 2.0.40	^
		amessagebox('Nu ati selectat vreun document!', 0 + 48, 'Atentie')
		*!*	modificare	v 2.0.40
		*!*		Return
		*!*	EndIf
	Else
		*!*	modificare	v 2.0.40
		Select crsfacturi
		Scan
			lcidvanzari = lcidvanzari + [, ] + Alltrim(Str(crsfacturi .id_vanzare))
			Select crsfacturi
		Endscan
		lcidvanzari = Substr(lcidvanzari, 2)
		*!*	modificare	v 2.0.83
		*!*			lcSql = [select a.codmat,a.denumire,a.pret_achizitie,a.pret,a.subgrupa,sum(cantitate) as cantitate from fact_vfacturi_detalii a ]+;
		*!*				[ where a.id_vanzare in (]+Alltrim(lcidvanzari)+[)]+;
		*!*				[ group by a.codmat,a.denumire,a.pret_achizitie,a.pret,a.subgrupa]+;
		*!*				[ order by a.codmat, a.denumire ]
		lcSql = [select d.codmat,NVL(d.denumire, a.explicatie) as denumire,a.pret_achizitie,] + ;
			[sum(round(a.cantitate*a.pret_achizitie,?gnPc)) as valoare_achizitie,] + ;
			[pack_sesiune.calculeaza_pret_cu_tva(a.pret,nvl(c.curs,1)/NVL(c.multiplicator,1),a.id_valuta,a.proc_tvav,a.pret_cu_tva,2) as pretvtva,] + ;
			[sum(pack_sesiune.calculeaza_total_cu_tva(a.pret,a.diferenta,nvl(c.curs,1)/NVL(c.multiplicator,1),a.discount_unitar,b.discount_evidentiat,a.cantitate,a.pret_cu_tva,a.proc_tvav,?gnPPretV,?gnPC)) as valoarevtva,] + ;
			[e.subgrupa,sum(a.cantitate) as cantitate ] + ;
			[from vanzari_detalii a ] + ;
			[left join vanzari b on a.id_vanzare = b.id_vanzare ] + ;
			[left join vanzari_cursuri c on a.id_vanzare = c.id_vanzare and a.id_valuta = c.id_valuta ] + ;
			[left join nom_articole d on a.id_articol = d.id_articol ] + ;
			[left join gest_art_sbgr e on d.id_subgrupa = e.id_subgrupa ] + ;
			[where a.sters = 0 and a.id_vanzare in (] + Alltrim(lcidvanzari) + [) ] + ;
			[group by NVL(d.denumire, a.explicatie),d.codmat,a.pret_achizitie,e.subgrupa,a.pret,nvl(c.curs,1),NVL(c.multiplicator,1),a.id_valuta,a.proc_tvav,a.pret_cu_Tva ] + ;
			[order by e.subgrupa,NVL(d.denumire, a.explicatie) ]
		*!*	modificare	v 2.0.83	^
		lnSucces = goExecutor.oexecute(lcSql, 'crsfactdetaliiartrap')
		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
			*!*	modificare	v 2.0.40
			*!*			Return
			*!*		Endif
		Else
			*!*	modificare	v 2.0.40	^
			Private pcDataora
			pcDataora = get_ora(2)
			Select crsfactdetaliiartrap
			Report Form rap_artdoc To Printer Prompt Preview
			*!*	modificare	v 2.0.40
		Endif
	Endif
	*!*	modificare	v 2.0.40	^
	Select crsfacturi
	Set Filter To &lcFiltruInit
	If lnRecSel > 0
		Goto lnRecSel
	Endif
Endproc
*---------------------------------------------------------------------------
Procedure rap_borderou_ar
	Local lcCursor, lcCursorAdrese
	lcCursor = [crsARClienti]
	lcCursorAdrese = [crsARAdresePart]
	lcCursorFirma = [crsARDateFirma]
	Select a.client, a.id_part, Space(255) As loc_judet From crsfacturi a With (Buffering = .T.) ;
		Where ales = 1 Order By 1 Into Cursor (lcCursor) NOFILTER Readwrite
	If Reccount(lcCursor) < 1
		amessagebox('Nu ati selectat nici un document!', 0 + 48, 'Atentie')
	Else
		lcSql = [select antet as denumire,adresa,telefon from syn_nom_firme where id_firma = ?gnIdFirma]
		If goExecutor.oExecuta(lcSql, lcCursorFirma)
			Private poFirma
			Select (lcCursorFirma)
			Scatter Name poFirma

			Select (lcCursor)
			lcListaId = []
			Scan For !Isnull(id_part)
				lcListaId = lcListaId + [,] + Alltrim(Str(id_part))
			Endscan

			lcSql = [select distinct id_part,localitate||nvl2(judet,' ('||judet||')','') as loc_judet from vadrese_parteneri ] + ;
				[where id_part in (] + Substr(lcListaId, 2) + [) and principala = 1]
			If goExecutor.oExecuta(lcSql, lcCursorAdrese)
				Select (lcCursorAdrese)
				Scan For !Isnull(loc_judet)
					lcLocJudet = loc_judet
					lnIdPart = id_part
					Update (lcCursor) Set loc_judet = lcLocJudet Where id_part = lnIdPart
				Endscan
				goExport.export2frx(lcCursor, [borderou_ar], , , , , , .T.)
			Endif
			Use In (Select(lcCursorAdrese))
		Endif
		Use In (Select(lcCursorFirma))
	Endif
	Use In (Select(lcCursor))
Endproc
*---------------------------------------------------------------------------
Procedure rap_factav_docart && lansat din rap_listfactav_centr, lansat la randul lui din frm_facturi (ofacturare.vcx)

	Select a.*, 1 As g1 From crsfacturi a With (Buffering = .T.) Where ales = 1 Into Cursor crsDocArt NOFILTER Readwrite
	Select crsDocArt
	If Reccount() < 1
		amessagebox('Nu ati selectat nici un document!', 0 + 48, 'Atentie')
		*!*	modificare	v 2.0.40
		*!*		Return
		*!*	EndIf
	Else
		*!*	modificare	v 2.0.40	^

		Local lcidvanzari, lcSql, lnSucces
		Store '' To lcidvanzari

		Select crsDocArt
		Scan
			lcidvanzari = lcidvanzari + [, ] + Alltrim(Str(crsDocArt.id_vanzare))
			Select crsDocArt
		Endscan
		lcidvanzari = Substr(lcidvanzari, 2)

		lcSql = [select a.codmat,a.denumire,a.pret_achizitie,a.pret,a.subgrupa,sum(cantitate) as cantitate from fact_vfacturi_detalii a ] + ;
			[ where a.id_vanzare in (] + Alltrim(lcidvanzari) + [)] + ;
			[ group by a.codmat,a.denumire,a.pret_achizitie,a.pret,a.subgrupa] + ;
			[ order by a.codmat, a.denumire ]

		lnSucces = goExecutor.oexecute(lcSql, 'crsfactdetaliiartrap')
		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
			*!*	modificare	v 2.0.40
			*!*		Return
			*!*	EndIf
		Else
			*!*	modificare	v 2.0.40	^
			Private pcDataora
			pcDataora = get_ora(2)
			Select crsfactdetaliiartrap
			Report Form rap_artdoc To Printer Prompt Preview
			*!*	modificare	v 2.0.40
		Endif
	Endif
	*!*	modificare	v 2.0.40	^
	Select crsfacturi
	Set Filter To &lcFiltruInit
	If lnRecSel > 0
		Goto lnRecSel
	Endif
Endproc
*----------------------------------------------------------------------------------------------
Procedure rap_transf_art
	Local lcidvanzari, lcSql, lnSucces
	Store '' To lcidvanzari

	Select * From crsfacturi With (Buffering = .T.) Where ales = 1 Into Cursor crsTmp
	If Reccount() < 1
		amessagebox('Nu ati selectat vreun document!', 0 + 48, 'Atentie')
		*!*	modificare	v 2.0.40
		*!*		Return
		*!*	EndIf
	Else
		*!*	modificare	v 2.0.40	^
		Select crsTmp
		Scan
			lcidvanzari = lcidvanzari + [, ] + Alltrim(Str(crsTmp.id_vanzare))
			Select crsTmp
		Endscan
		Use In crsTmp
		lcidvanzari = Substr(lcidvanzari, 2)
		lcSql = [select a.codmat,a.denumire,a.pret_achizitie,a.pret,a.subgrupa,sum(cantitate) as cantitate,client from fact_vfacturi_detalii a ] + ;
			[ where a.id_vanzare in (] + Alltrim(lcidvanzari) + [)] + ;
			[ group by a.denumire,a.codmat,a.pret_achizitie,a.pret,a.subgrupa,client] + ;
			[ order by a.subgrupa,a.denumire ]
		lnSucces = goExecutor.oexecute(lcSql, 'crssitconsum')
		If lnSucces < 0
			amessagebox(goExecutor.cEroare, 0 + 16, "Eroare")
			*!*	modificare	v 2.0.40
			*!*			Return
			*!*		Endif
		Else
			*!*	modificare	v 2.0.40	^
			Private pcDataora
			pcDataora = get_ora(2)
			Select crssitconsum
			Report Form rap_transferart To Printer Prompt Preview
			*!*	modificare	v 2.0.40
		Endif
	Endif
	*!*	modificare	v 2.0.40	^
	Select crsfacturi
Endproc &&rap_transf_art


*---------------------------------------------------------------------------
* Borderou documente de trimis la clienti grupat pe clienti
* Salveaza borderouri individiuale pdf si listeaza la imprimanta un raport cu toate borderourile [borderou1-borderou2]
* Un borderou pentru toti clientii sau un borderou pentru un client, daca se transmite tnIdPart
Procedure rap_borderou_clienti && lansat din frm_facturi (ofacturare.vcx)
	Lparameters tcIds, tlPDF, tlListare, tcCursorFacturi
	* tlSilent, tlPDF: se apeleaza cu parametri din generare_borderou_facturi si email_borderou_facturi pentru generare pdf silentios pentru fiecare id_borderou
	* tlListare: se apeleaza din frm_facturi.do_listare
	* tcIds : lista id-uri borderou C; id_borderou N
	* tcCursorFacturi: optional, pentru citirea ctip_factura suprascris de utilizator (ACN!)
	
	Private pcDataora
	Local loPreviewerConfig As "PreviewerConfig"
	Local lcDestinationFile, lcSelect, lcSetare, lcSetareVizualizare, lcSql, ldDataBorderou, llSucces
	Local lnIdBorderou, lcIds, lcCursor, lcCursor2
	pcDataora = get_ora(2)

	lcSelect = Select()
	lcIds = IIF(TYPE('tcIds') = 'C', m.tcIds, IIF(TYPE('tcIds') = 'N', ALLTRIM(STR(m.tcIds)), ''))
	llSucces = !EMPTY(m.lcIds)
	lcCursor = SYS(2015)
	SET STEP ON 
	IF m.llSucces
		TEXT TO lcSql TEXTMERGE NOSHOW
	Select client, data_act, numar_act, total_fara_tva, total_tva, total_cu_tva, valval, tvaval, totval, ctip_factura, in_valuta, valuta, id_vanzare, id_client, id_borderou, data_borderou, email_client, adresa_client
	    FROM fact_vborderou_detalii
		WHERE id_borderou in (<<m.lcIds>>)
		ORDER By 1, 2, 3 
		ENDTEXT

		llSucces = goExecutor.oExecuta(m.lcSql, m.lcCursor)
		
		***************
		* Suprascriere ctip_factura special pentru ACN, pentru ca voiau ENERGIE ELECTRICA in loc de CHIRII SI UTILITATI pe borderou
		* si nu am adaugat un tip nou de prestatii ENERGIE ELECTRICA
		***************
		IF !EMPTY(m.tcCursorFacturi) AND USED(m.tcCursorFacturi)
			SELECT (m.tcCursorFacturi)
			SCAN
				lnIdVanzare = id_vanzare
				lcTipFactura = ALLTRIM(NVL(ctip_factura,''))
				UPDATE (m.lcCursor) SET ctip_factura = m.lcTipFactura WHERE id_vanzare = m.lnIdVanzare
			ENDSCAN 
		ENDIF 
	ENDIF 
		
	If m.llSucces
		***********************
		lcSetare = [BORDEROU]
		lcSetareVizualizare = [PREVIZBORDEROU]
		loPreviewerConfig = Createobject("PreviewerConfig") && rapoarte.prg

		* Listez toate borderourile intr-un singur raport
		If m.tlListare
			* m.loPreviewerConfig.SetValue("lDirectPrint", m.tlSilent)
			goExport.export2frx(m.lcCursor, [rap_borderou_clienti], .F., m.lcSetareVizualizare, m.lcSetare, , , .T., , loPreviewerConfig)
		Endif && tlListare

		* salvez pdf-uri cu borderouri individuale cu denumirea borderou_[aaaallzz]_[tnId].pdf
		If m.tlPDF
			SELECT distinct id_borderou FROM (m.lcCursor) INTO CURSOR cIdsBorderouListareTemp
			SELECT cIdsBorderouListareTemp
			lcRecc = ALLTRIM(STR(RECCOUNT()))
			SCAN 
				lnIdBorderou = id_borderou
				lcRecno = ALLTRIM(STR(RECNO()))
				WAIT WINDOW 'Generare pdf borderou ' + m.lcRecno + '/' + m.lcRecc NOWAIT 
				lcCursor2 = SYS(2015)
				SELECT * FROM (m.lcCursor) WHERE id_borderou = m.lnIdBorderou INTO CURSOR (m.lcCursor2)
				SELECT (m.lcCursor2)
				GO TOP 
				ldDataBorderou = data_borderou
				lcDestinationFile = GetPDFBorderouFile(m.ldDataBorderou, m.lnIdBorderou)
				If !File(m.lcDestinationFile) Or (File(m.lcDestinationFile) And amessagebox('Doriti sa suprascrieti ' + m.lcDestinationFile + '?', 4 + 32, _Screen.Caption) = 6)
					m.loPreviewerConfig.SetValue("cDestFile", m.lcDestinationFile)
					m.loPreviewerConfig.SetValue("lOpenDestFile", .F.)
					m.loPreviewerConfig.SetValue("lDirectPrint", .T.)
					m.loPreviewerConfig.SetValue("lOpenViewer", .F.)
					*!*					lcPdfHasImage = goApp.ReadIni([email_factura], "borderoupdfhasimage")
					*!*					llPdfHasImage = (m.lcPdfHasImage = "1")
					*!*					If Empty(m.lcPdfHasImage)
					*!*						goApp.WriteIni([email_factura], "borderoupdfhasimage", "0")
					*!*						llPdfHasImage = .F.
					*!*					Endif
					*!*					m.loPreviewerConfig.SetValue("lPDFasImage", m.llPdfHasImage)

					* tcAlias, tcRaport, tlCereTitlu, tcSetareVizualizare, tcSetareImprimanta, tcParametriListare, toListener, tlMultiPreview, tcReportPreviewer, toPreviewerConfig
					goExport.export2frx(m.lcCursor2, [rap_borderou_clienti], .F., m.lcSetareVizualizare, m.lcSetare, , , .T., , loPreviewerConfig)
					USE IN (SELECT(m.lcCursor2))
				Endif

			ENDSCAN && cIdsBorderouListareTemp
			USE IN (SELECT('cIdsBorderouListareTemp'))
		Endif && m.tlPDF
		***********************
		*!*			goExport.export2frx('crsBorderouFacturiTemp', [rap_borderou_clienti], , , , , , .T.)
		Use In (Select(m.lcCursor))
	Endif && llSuccess

	Select (m.lcSelect)
	Return m.llSucces
Endproc && rap_borderou_clienti