*!* 10.12.2015
*!* marius.mutu
*!* listare explicatie din registrul jurnal pe chitanta
*!* #4246

*!* 30.06.2017
*!* marius.mutu
*!* listare_chitanta, relistare_chitanta - suma in vorbe tine cont de valuta

*******************************************************************************************
DEFINE CLASS oDateChitanta AS CUSTOM
	id_client		=	NULL
	nume_client		=	[]
	nr_incasare		=	0
	serie_chit		=	[]
	dataact			=	{}
	incasat			=	0
	sumavorbe		=	[]
	explicatie_chit	=	[]
	nume_valuta		= 	[]
	lSilent			= 	.F.
	
	* lSilent (default .F.) listare silentioasa, fara previzualizare
	
	PROCEDURE INIT
		LPARAMETERS tnIdPartener, tcSirNrFact
		WITH THIS
			.id_client = tnIdPartener
			*!*			lcSirFact = Substr(tcSirNrFact,1,LEN(tcSirNrFact)-2)	&&	are [, ] la sfarsit
			lcSirFact = IIF(RIGHT(ALLTRIM(tcSirNrFact), 1) = ',', LEFT(ALLTRIM(tcSirNrFact), LEN(ALLTRIM(tcSirNrFact)) - 1), ALLTRIM(tcSirNrFact))
			IF [, ] $ lcSirFact
				.explicatie_chit = [facturilor ] + lcSirFact
			ELSE
				.explicatie_chit = [facturii ] + lcSirFact
			ENDIF
		ENDWITH
	ENDPROC

ENDDEFINE
*******************************************************************************************
PROCEDURE clienti_chitanta
	LPARAMETERS tnIdSet
	LOCAL lnIdSet
	lnIdSet = IIF(!EMPTY(m.tnIdSet), m.tnIdSet, 10329) && casa > incasare clienti 4111

	PRIVATE glListareChitanta
	PRIVATE pcSirFacturi
	glListareChitanta = .T.
	STORE [] TO pcSirFacturi
	lans(m.lnIdSet)
	glListareChitanta = .F.
ENDPROC	&&	clienti_chitanta
*******************************************************************************************
PROCEDURE listeaza_chitanta
	LPARAMETERS tcSirNrFact
	LOCAL lnIdClient, loDate, lcNumeClient
	LOCAL laProperties[1], lcProperty, lnProperties, lnProperty

	SELECT actactan
	LOCATE FOR id_partc <> 0
	lnIdClient = id_partc
	tcSirNrFact = IIF(RIGHT(ALLTRIM(tcSirNrFact), 1) = ',', LEFT(ALLTRIM(tcSirNrFact), LEN(ALLTRIM(tcSirNrFact)) - 1), ALLTRIM(tcSirNrFact)) + IIF(!EMPTY(NVL(explicatia, '')), ' ' + ALLTRIM(explicatia), '')

	lcNumeClient = IIF(TYPE('poAct.v4111') = 'C', poAct.v4111, IIF(TYPE('poAct.v461') = 'C', poAct.v461, ''))
	IF EMPTY(m.lcNumeClient)
		lnProperties = AMEMBERS(laProperties, poAct, 0)
		FOR lnProperty = 1 TO m.lnProperties
			lcProperty = LOWER(laProperties(m.lnProperty))
			IF LEFT(m.lcProperty, 2) = 'v4'
				lcNumeClient = EVALUATE('poAct.' + _m.lcProperty)
				EXIT
			ENDIF
		ENDFOR
	ENDIF

	loDate = CREATEOBJECT('oDateChitanta', lnIdClient, tcSirNrFact)
	WITH loDate
		.nume_client	=	m.lcNumeClient
		.serie_chit		=	poAct.serie_act
		.nr_incasare	=	poAct.nract
		.dataact		=	poAct.dataact
		.incasat		=	poAct.suma
		.nume_valuta	=	IIF(EMPTY(ALLTRIM(NVL(poAct.nume_val, ''))), 'LEI', STRTRAN(ALLTRIM(NVL(poAct.nume_val, '')), 'RON', 'LEI', 1, 1, 1))
		.sumavorbe		=	SUMA_IN_VORBE(.incasat, NVL(poAct.nume_val, ''))
	ENDWITH
	listare_chitanta(@loDate, [actactan])
	RELEASE lnIdClient, loDate
ENDPROC
*******************************************************************************************
PROCEDURE relisteaza_chitanta
	LPARAMETERS tnCod, tlSilent
	* tnCod act.cod pentru chitanta
	* tlSilent (default .F.) se listeaza silentios, fara previzualizare
	
	LOCAL loDate, lcSirFact, loDetalii
	*!*	am pus conditia scd = '5311' ca sa nu-mi ia si notele de exigibilizare a TVA-ului
	*!*	am pus conditia id_set = 10329 ca sa nu poata lista decat ce a introdus pe la incasare clienti cu/fara listare chitanta
	*!*	id_set = 10329 ( incasare clienti cu/fara listare chitanta )
	*!*	id_set = 10576 ( incasare clienti cu bon fiscal )
	lcSql = [select a.id_partc,c.denumire,a.serie_act,a.nract,a.dataact, d.nume_val, sum(a.suma) as suma,] + ;
		[stringagg(' '||nvl2(b.serie_act, b.serie_act || ' ', '') || ] + ;
		[trim(replace(replace(to_char(b.nract, '999999999999999999'), '.' , ' '), ',' , ' ')) ] + ;
		[ || ' din '|| to_char(b.dataact,'DD/MM/YYYY')) as sirfact, max(explicatia) as explicatia ] + ;
		[from (select id_partc, serie_act, nract, dataact, suma, id_factc, explicatia, id_valuta from act ] + ;
		[where cod = ] + ALLTRIM(STR(tnCod)) + [ and sters = 0 and scd = '5311') a ] + ;
		[left join documente b on a.id_factc = b.id_doc ] + ;
		[left join nom_parteneri c on a.id_partc = c.id_part ] + ;
		[left join vnom_valute d on a.id_valuta = d.id_valuta ] + ;
		[group by a.id_partc, c.denumire, a.serie_act, a.nract, a.dataact, d.nume_val]
	*!*		lcSql = [select a.id_partc,c.denumire,a.serie_act,a.nract,a.dataact,a.suma,] + ;
	*!*			[nvl2(b.serie_act, b.serie_act || ' ', '') || ] + ;
	*!*			[trim(replace(replace(to_char(b.nract, '999G999G999G999G999G999'), '.' , ' '), ',' , ' ')) ] + ;
	*!*			[ || ' din '|| to_char(b.dataact,'DD/MM/YYYY') as sirfact ] + ;
	*!*			[from (select id_partc, serie_act, nract, dataact, suma, id_factc from act ] + ;
	*!*			[where cod = ] + Alltrim(Str(tnCod)) + [ and sters = 0 and scd = '5311' and id_set = 10329) a ] + ;
	*!*			[left join documente b on a.id_factc = b.id_doc ] + ;
	*!*			[left join nom_parteneri c on a.id_partc = c.id_part ]
	lcCursor = [crsdatechitanta]
	IF goExecutor.oExecuta(lcSql, lcCursor)
		IF RECCOUNT(lcCursor) > 0
			*!*				lcSirFact = []
			*!*				Select (lcCursor)
			*!*				Scan
			*!*					If Recno() = 1
			*!*						Scatter Name loDetalii
			*!*					Else
			*!*						loDetalii.suma = loDetalii.suma + suma
			*!*					Endif
			*!*					lcSirFact = lcSirFact + ALLTRIM(sirfact) + [, ]
			*!*				Endscan
			*!*				loDate = Createobject('oDateChitanta',loDetalii.id_partc,SUBSTR(lcSirFact,3))
			*!*				With loDate
			*!*					.nume_client	=	Alltrim(loDetalii.denumire)
			*!*					.serie_chit		=	Alltrim(loDetalii.serie_act)
			*!*					.nr_incasare	=	loDetalii.nract
			*!*					.dataact		=	loDetalii.dataact
			*!*					.incasat		=	loDetalii.suma
			*!*					.sumavorbe		=	SUMA_IN_VORBE(.incasat)
			*!*				Endwith
			lcSirNrFact = IIF(RIGHT(ALLTRIM(sirfact), 1) = ',', LEFT(ALLTRIM(sirfact), LEN(ALLTRIM(sirfact)) - 1), ALLTRIM(sirfact)) + IIF(!EMPTY(NVL(explicatia, '')), ' ' + ALLTRIM(explicatia), '')

			SELECT (lcCursor)
			loDate = CREATEOBJECT('oDateChitanta', id_partc, lcSirNrFact)	&&	[, ] este eliminat in oDateChitanta
			WITH loDate
				.nume_client	=	ALLTRIM(denumire)
				.serie_chit		=	ALLTRIM(serie_act)
				.nr_incasare	=	nract
				.dataact		=	dataact
				.incasat		=	suma
				.nume_valuta	=	IIF(EMPTY(ALLTRIM(NVL(nume_val, ''))), 'LEI', STRTRAN(ALLTRIM(NVL(nume_val, '')), 'RON', 'LEI', 1, 1, 1))
				.sumavorbe		=	SUMA_IN_VORBE(.incasat, NVL(nume_val, ''))
				.lSilent		= 	m.tlSilent
			ENDWITH
			listare_chitanta(@loDate, lcCursor)
			RELEASE lnIdClient, loDate, loDateChit
		ELSE
			amessagebox("Nu exista date pentru listarea chitantei!", 48, "Atentie")
		ENDIF
	ENDIF
	USE IN (SELECT(lcCursor))
ENDPROC
*******************************************************************************************
PROCEDURE listare_chitanta
	LPARAMETERS toDate, tcCursor
	PRIVATE poClient, poFirma, poDate
	
	Local lcCursor, lcRaport, lcSql, lnRaspuns
	Local loPreviewerConfig As "PreviewerConfig"
	poDate = toDate

	lcSql = [select 1 as tip,(case when tip_persoana = 1 then prefix||' '||denumire||' '||sufix else denumire end) as denumire,] + ;
		[cod_fiscal,adresa,telefon,'' as fax,reg_comert,banca,cont_banca,sysdate as dataora,tip_persoana,0 as capital_soc_var,] + ;
		[0 as capital_soc_sub from vnom_parteneri where id_part = ?poDate.id_client ] + ;
		[union all ] + ;
		[select 3 as tip,antet as denumire,cod_fiscal,adresa,telefon,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]
	IF goExecutor.oExecuta(lcSql, lcCursor)
		SELECT crsdatefacturare
		SCAN
			DO CASE
				CASE tip = 1
					SCATTER NAME poClient MEMO
					*!*			ptDataOra = poClient.dataora
				OTHERWISE
					SCATTER NAME poFirma MEMO
			ENDCASE
		ENDSCAN
		USE IN crsdatefacturare

		* Daca nu am gasit clientul (nu exista clientul) (ex: 5311 = 581) - da eroare, creez o structura poClient gol
		IF TYPE('poClient.denumire') <> 'C'
			poClient = CREATEOBJECT("custom")
			poClient.ADDPROPERTY("tip", 1)
			poClient.ADDPROPERTY("denumire", "")
			poClient.ADDPROPERTY("cod_fiscal", "")
			poClient.ADDPROPERTY("adresa", "")
			poClient.ADDPROPERTY("telefon", "")
			poClient.ADDPROPERTY("fax", "")
			poClient.ADDPROPERTY("reg_comert", "")
			poClient.ADDPROPERTY("banca", "")
			poClient.ADDPROPERTY("cont_banca", "")
			poClient.ADDPROPERTY("dataora", {//::})
			poClient.ADDPROPERTY("tip_persoana", 0)
			poClient.ADDPROPERTY("capital_soc_var", 0)
			poClient.ADDPROPERTY("capital_soc_sub", 0)

		ENDIF
		*!*	modificare v 2.0.100
		*!*	poClient.denumire = Nvl(poClient.denumire,Padl([_],100,[_]))
		*!*	poClient.adresa = Nvl(poClient.adresa,Padl([_],150,[_]))
		*!*	poDate.explicatie = Nvl(poDate.explicatie,Padl([_],100,[_]))
		poClient.denumire = NVL(poClient.denumire, REPLICATE([. ], 50))
		poClient.adresa = NVL(poClient.adresa, REPLICATE([. ], 75))
		poDate.explicatie_chit = NVL(poDate.explicatie_chit, REPLICATE([. ], 50))
		*!*	modificare v 2.0.100	^


		lcRaport = IIF(TYPE('gnRC_RAPORT_CHITANTA') = 'N', IIF(m.gnRC_RAPORT_CHITANTA = 0, [chitanta], [chitanta_landscape]), [chitanta])
		loPreviewerConfig = Createobject("PreviewerConfig") && rapoarte.prg

		If poDate.lSilent
			m.loPreviewerConfig.SetValue("lDirectPrint", .T.)
			goExport.export2frx(m.tcCursor,  m.lcRaport, .F., , , , , .T., , loPreviewerConfig)
		ELSE 	
*!*				lnRaspuns = 6
*!*				DO WHILE lnRaspuns = 6
				goExport.export2frx(m.tcCursor,  m.lcRaport, .F., , , , , .T., , loPreviewerConfig)
*!*					lnRaspuns = amessagebox("Doriti sa mai listati chitanta?", 4 + 32 + 256, "Confirmare repetare listare")
*!*				ENDDO
		ENDIF && lSilent
	ENDIF && goExecutor
	RELEASE poClient, poFirma, poDate
ENDPROC	&&	listeaza_chitanta
*******************************************************************************************




************************************
*** note incasare facturi 5121 = 4111 prin selectarea facturilor incasate direct din facturi cu sold
*** nu trateaza cazul firmelor cu TVA Incasare
************************************
PROCEDURE incasare_facturi_sold
	PARAMETERS tcXMLFacturi

	LOCAL loForm AS [frm_modific2007]

	lcSCD = '5121'
	lcSCC = '4111'

	lnRecords = XMLTOCURSOR(m.tcXMLFacturi, "crsFacturiIncasareTemp")
	IF lnRecords = 0
		USE IN (SELECT("crsFacturiIncasareTemp"))
		RETURN
	ENDIF
	lcMesaj = ''
	lnIdSet = 99999 && nota fara predefinire
	lnBut = lans(m.lnIdSet)

	IF m.lnBut = 1
		SELECT actactan
		SCATTER NAME loAct
		ZAP

		INSERT INTO actactan(nr_nota, luna, an, scd, scc, ascc, suma, id_set, id_factc, perechec, id_partc, partc, explicatia) ;
			SELECT RECCOUNT('actactan') + RECNO() AS nr_nota, m.gnLuna AS luna, m.gnAn AS an, m.lcSCD AS scd, CONT AS scc, acont AS ascc, ;
			(precdeb + debit) - (preccred + credit) AS suma, m.lnIdSet AS id_set, id_fact AS id_factc, nract AS perechec, id_part AS id_partc, nume AS partc, ;
			'Incasare prin banca' AS explicatia ;
			FROM crsFacturiIncasareTemp


		SELECT actactan
		SCAN
			GATHER NAME loAct FIELDS EXCEPT nr_nota, luna, an, scd, scc, suma, id_set, id_factc, perechec, id_partc, partc
			REPLACE id_sucursala WITH m.gnIdSucursala
		ENDSCAN


		update_jtva_coloane("JV", 'crsExplicatiiTVATemp', 1)

		SELECT .F. AS ales, 0 AS Cu_tva, CAST(0 AS N(2)) AS tipnota, a.*, b.denumire AS explicatie_tva_2 ;
			FROM actactan a LEFT JOIN crsExplicatiiTVATemp b ON a.id_jtva_coloana = b.id_jtva_coloana ;
			INTO CURSOR tact READWRITE

		Update tact Set explicatie_tva = explicatie_tva_2

		USE IN (SELECT('crsExplicatiiTVATemp'))


		SELECT tact
		GO TOP
		llNotaNoua = .T.
		llMaximize = .T.
		loForm = CREATEOBJECT([frm_modific2007], m.lnIdSet, m.llNotaNoua)
		loForm.lb_titlu_alb_b121.CAPTION = 'Incasare facturi'
		SELECT tact
		loForm.SHOW(1, m.llMaximize)

		IF buton = 1
			USE IN (SELECT('actactan'))

			SELECT tact
			REPLACE id_jtva_coloana WITH NULL, PROC_TVA WITH 0 FOR Cu_tva = 0
			SELECT * FROM tact INTO CURSOR actactan READWRITE

			SELECT actactan
			REPLACE ALL id_util WITH gnIdUtil, id_set WITH m.lnIdSet
			lnSucces  = OSCRIE_IN_FISIERE(0)
		ENDIF && buton = 1
	ENDIF &&  m.lnBut = 1


	USE IN (SELECT('actactan'))
	USE IN (SELECT('tact'))

ENDPROC && incasare_facturi_sold  