*tnIdClient,tcCursor,tcSirNrOrd,tcNrInmat,tnTipfact,tnProcDiscM,tnProcDiscN,tnDiscountM,tnDiscountN,tnDiscountTvaM,tnDiscountTvaN,tcdenop
SET DELETED ON
SET CENTURY ON
SET ANSI ON
SET EXACT ON
SET DATE DMY
SET SAFETY OFF

*CLOSE DATABASES ALL

lcDir = "C:\TEST_LISTARE_FACTURA\"
*lcDir = "d:\roa_rb\roaauto\test\test_listare_factura\"
CD (lcDir)
messagebox(lcDir)
lcHost = "roa_arrow"
lcUserName = "auto_arrow"
lcPassword = "123"
PRIVATE gnHandle
gnHandle = 0
gnTIP_LFACTURA = 1
plEmiteBon = .F.
PRIVATE gcS
gcS = lcUserName
PRIVATE gnIdFirma, gnIdUtil
gnIdFirma = 1
gnIdUtil = 1
PRIVATE GNCGARANTIE
GNCGARANTIE = 0
PRIVATE GNPA, gnLuna, gnAn, gnAnRon, gnLunaRon, gnPc
gnLuna = 7
gnAn = 2007
gnAnRon = 2005
gnLunaRon = 7
gnPc = 2
GNPA = 2
pntotftva = 100
pntottva = 19
pntotctva = 119
pcNumeresp = ""
pcbidresp = ""
pccnpresp = ""

SET DEFAULT TO (lcDir)
SET PATH TO (lcDir)
SET PROCEDURE TO oproceduri_comune.prg ADDITIVE
SET PROCEDURE TO proceduri_rapoarte.prg ADDITIVE
SET PROCEDURE TO test_listare_factura.prg ADDITIVE

PRIVATE goExecutor
goExecutor = CREATEOBJECT("oExecutor")

PRIVATE goConn
goConn = CREATEOBJECT("oConn")
gnHandle = goConn.CONNECT(lcHost, lcUserName, lcPassword)


IF !USED('actactan')
	USE actactan IN 0 SHARED
ENDIF 	


DO listare_factura WITH 34,"actactan","59/CT-02-ZIP","CT-02-ZIP",1,0,0,0,0,0,0,"MANOPERA"

goConn.Disconnect()

PROCEDURE listare_factura
	PARAMETERS tnIdClient,tcCursor,tcSirNrOrd,tcNrInmat,tnTipfact,tnProcDiscM,tnProcDiscN,tnDiscountM,tnDiscountN,tnDiscountTvaM,tnDiscountTvaN,tcdenop

	IF EMPTY(tcdenop)
		STORE 'MANOPERA' TO tcdenop
	ENDIF

	IF EMPTY(tnProcDiscN)
		STORE 0 TO tnProcDiscN,tnDiscountN,tnDiscountTvaN
	ENDIF

	IF EMPTY(tnProcDiscM)
		STORE 0 TO tnProcDiscM,tnDiscountM,tnDiscountTvaM
	ENDIF

	tnDiscountN = ABS(tnDiscountN)
	tnDiscountM = ABS(tnDiscountM)

	PRIVATE poDate,poFirma,poClient,ptDataOra
	poDate = CREATEOBJECT('oDateFacturaDevize',tnIdClient,tcSirNrOrd,tcNrInmat,IIF(tnTipfact=4,1,0))
	lcSql = [select 1 as tip,(case when tip_persoana = 1 then prefix||' '||denumire||' '||sufix else denumire end) as denumire,]+;
		[cod_fiscal,adresa,telefon,'' as telefon2,'' as telefon3,'' as fax,reg_comert,banca,cont_banca,sysdate as dataora,tip_persoana,0 as capital_soc_var,] + ;
		[0 as capital_soc_sub from ] + gcS + [.vnom_parteneri where id_part = ?poDate.id_client ] + ;
		[union all ] + ;
		[select 2 as tip,denumire,cod_fiscal,'' as adresa,'' as telefon,'' as telefon2,'' as telefon3,'' as fax,reg_comert,'' as banca,'' as cont_banca,] + ;
		[null as dataora,tip_persoana,0 as capital_soc_var,0 as capital_soc_sub from ] + gcS + [.nom_parteneri ] + ;
		[where sters = 0 and inactiv = 0 and id_part in ] + ;
		[(select id_partener from ] + gcS + [.utilizatori_rol_intern where sters = 0 and id_util = ?gnIdUtil)] + ;
		[union all ] + ;
		[select 3 as tip,antet as denumire,cod_fiscal,adresa,telefon,telefon2,telefon3,fax,reg_comert,banca1 as banca,] + ;
		[cont_banca1 as cont_banca,null as dataora,0 as tip_persoana,capital_soc_var,capital_soc_sub ] + ;
		[from syn_nom_firme where id_firma = ?gnIdFirma ]
	IF USED('crsdatefacturare')
		USE IN crsdatefacturare
	ENDIF
	lcCursor = [crsdatefacturare]
	lnSucces = goExecutor.oExecute(lcSql,lcCursor)
	IF lnSucces<0
		amessagebox(goExecutor.oPrelucrareEroare(),16,"Eroare")
		RETURN
	ENDIF
	SELECT crsdatefacturare
	SCAN
		DO CASE
			CASE tip = 1
				SCATTER NAME poClient
				ptDataOra = poClient.dataora
			CASE tip = 2
				WITH poDate
					.nume_responsabil	=	denumire
					.BIresp				=	reg_comert
					.CNPresp			=	cod_fiscal
				ENDWITH
			OTHERWISE
				SCATTER NAME poFirma
		ENDCASE
	ENDSCAN
	USE IN crsdatefacturare

	*!*	ofrmdelegat=Createobject("frm_delegat_factura")
	*!*	ofrmdelegat.nidpartener = poDate.id_client
	*!*	ofrmdelegat.cclient = poDate.nume_client
	*!*	ofrmdelegat.cnrinmat_exp = poDate.nrinmat_del
	*!*	ofrmdelegat.Show(1)
	*!*	Release ofrmdelegat

	PRIVATE pnSumaMat,pnSumaMan,pnTvaMat,pnTvaMan,pnDiscMan,pnDiscMat,pnTvaDiscMan,pnTvaDiscMat,pnAvans,pnTvaAvans
	STORE 0 TO pnSumaMat,pnSumaMan,pnTvaMat,pnTvaMan,pnDiscMan,pnDiscMat,pnTvaDiscMan,pnTvaDiscMat,pnAvans,pnTvaAvans

	SELECT &tcCursor
	SUM suma FOR ALLTRIM(scd) = '4111' AND ALLTRIM(scc) = '707' AND LEFT(ALLTRIM(explicatia),3) = "MAT" TO pnSumaMat
	SUM suma FOR ALLTRIM(scd) = '4111' AND ALLTRIM(scc) = '4427' AND LEFT(ALLTRIM(explicatia),7) = "TVA MAT" TO pnTvaMat
	*!*	Sum suma For Alltrim(scd) = '411' And Alltrim(scc) = '707' And Left(Alltrim(explicatia),12) = "DISCOUNT MAT" To pnDiscMat
	SUM suma FOR ALLTRIM(scd) = '4111' AND ALLTRIM(scc) = '4427' AND LEFT(ALLTRIM(explicatia),12) = "DISCOUNT MAT" TO pnTvaDiscMat

	SUM suma FOR ALLTRIM(scd) = '4111' AND ALLTRIM(scc) = '704' AND LEFT(ALLTRIM(explicatia),3) = "MAN" TO pnSumaMan
	SUM suma FOR ALLTRIM(scd) = '4111' AND ALLTRIM(scc) = '4427' AND LEFT(ALLTRIM(explicatia),7) = "TVA MAN" TO pnTvaMan
	*!*	Sum suma For Alltrim(scd) = '411' And Alltrim(scc) = '704' And Left(Alltrim(explicatia),12) = "DISCOUNT MAN" To pnDiscMan
	SUM suma FOR ALLTRIM(scd) = '4111' AND ALLTRIM(scc) = '4427' AND LEFT(ALLTRIM(explicatia),12) = "DISCOUNT MAN" TO pnTvaDiscMan

	SELECT &tcCursor
	*!*	Locate For Alltrim(explicatia ) = 'STORNARE AVANS'
	*!*	If Found()
	*!*		pnAvans = (-1) * Abs(&tcCursor..suma)
	*!*	Endif
	CALCULATE SUM(suma) FOR ALLTRIM(explicatia)='STORNARE AVANS' TO pnAvans
	CALCULATE SUM(suma) FOR ALLTRIM(explicatia)='STORNARE AVANS' AND ALLTRIM(scc)<>'4427' TO pnAvansFTva
	CALCULATE SUM(suma) FOR ALLTRIM(explicatia)='STORNARE AVANS' AND ALLTRIM(scc)='4427' TO pnAvansTva
	pnAvans=(-1) * ABS(ROUND(pnAvans,GNPA))
	pnAvansTva=(-1) * ABS(ROUND(pnAvansTva,GNPA))
	pnAvansFTva=(-1) * ABS(ROUND(pnAvansFTva,GNPA))

	PRIVATE plAchitat,plEmiteBon
	STORE .T. TO plAchitat,plEmiteBon

	SELECT &tcCursor
	LOCATE FOR ALLTRIM(scd) = '4111'
	IF FOUND()
		poDate.nract = &tcCursor..nract
		poDate.dataact = &tcCursor..dataact
		poDate.proc_tva = &tcCursor..proc_tva*100-100
		poDate.datascad = &tcCursor..datascad
	ENDIF

	SELECT &tcCursor
	LOCATE FOR ALLTRIM(scd) = '5311'
	DO CASE
		CASE FOUND() AND pnTipIncasare=2
			STORE .F. TO plAchitat
			STORE .T. TO plEmiteBon
			poDate.nrchit = &tcCursor..nract
			poDate.sumaachit = &tcCursor..suma
		CASE FOUND() AND pnTipIncasare=3
			STORE .F. TO plEmiteBon
			poDate.nrchit = &tcCursor..nract
			poDate.datachit = &tcCursor..dataact
			poDate.sumaachit = &tcCursor..suma
			poDate.sumavorbe = SUMA_IN_VORBE(ROUND(poDate.sumaachit,gnPc))
		OTHERWISE
			STORE .F. TO plEmiteBon
			STORE .F. TO plAchitat
	ENDCASE

	*-------------------
	*!* modificare 07.04
	*!*	pnDiscMan = tnDiscountM
	*!*	pnDiscMat = tnDiscountN
	pnDiscMan = SIGN(pnSumaMan) * tnDiscountM
	tnDiscountM = SIGN(pnSumaMan) * tnDiscountM
	pnTvaDiscMan = (-1) * pnTvaDiscMan

	pnDiscMat = SIGN(pnSumaMat) * tnDiscountN
	tnDiscountN = SIGN(pnSumaMat) * tnDiscountN
	pnTvaDiscMat = (-1) * pnTvaDiscMat
	*!*	modificare ^


	REPORT FORM usr_factav_cuchit TO PRINTER PROMPT PREVIEW


	RELEASE poDate
ENDPROC	&&	listare_factura
*********************************************************************************************************

DEFINE CLASS oDateFacturaDevize AS CUSTOM
	devize_multiple	=	0
	listanrord		=	[]
	dataact			=	{}
	datascad		=	{}
	nract			=	0
	id_client		=	NULL
	nume_client		=	[]
	nume_responsabil=	[]		&&	alte date pentru facturare
	BIresp			=	[]
	CNPresp			=	[]
	id_delegat		=	NULL
	nume_delegat	=	[]
	BIdelegat		=	[]
	elibdelegat		=	[]
	CNPdelegat		=	[]
	eliberatde		=	[]
	nrinmat_del		=	[]
	proc_tva		=	0
	nrchit			=	0
	datachit		=	{}
	sumaachit		=	0
	sumavorbe		=	[]
	text_aditional	=	[]

	PROCEDURE INIT
		LPARAMETERS tnIdPartener,tcSirNrOrd,tcNrInmat,tnDevize
		WITH THIS
			.id_client = tnIdPartener
			.nrinmat_del = tcNrInmat
			.devize_multiple = tnDevize
			.listanrord = tcSirNrOrd
		ENDWITH
	ENDPROC

ENDDEFINE
