* PLUGIN email factura, recapitulatie si alte documente pdf cu aceeasi structura ca factura.pdf
* MARIUS MUTU
* V 23.02.2016
* INSTALARE: Se copie fisierul prg in directorul ROA\[APLICATIE]\PLUGIN\
* FOLOSIRE: In formularul Vizualizare facturi se apasa butonul listare
* copiat din oproceduri_facturare.prg:email_facturi 

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

Local laFiles[1], lcBody, lcClient, lcDir, lcEmail, lcErrorMessage, lcFacturaFile, lcFile
Local lcFileName, lcFiles, lcNume, lcRaportEmail, lcRecapitulatiaFile, lcScheletonFile
Local lcSerieFactura, lcSubject, lcTipDocument, lcTo, lcWhere, ldDataFactura, llClearErrors
Local llJustPDF, llListareFactura, llListareRecapitulatia, llListareAlteDocumente, llSilent, llSucces, lnFile, lnFiles
Local lnIdVanzare, lnNumarFactura, lnRecno, loDate, loMail

loMail = getoEmail() && email.prg

lcWhere = ""
llListareFactura = .T.
llGenerareFactura = .T.
llListareRecapitulatia = .T.
llGenerareRecapitulatia = .T.
llListareAlteDocumente = .T.
If Type('toDate') = 'O'
	llListareFactura = Iif(Type('toDate.nListareFactura') = 'N', Inlist(toDate.nListareFactura, 1, 2), .T.)
	llGenerareFactura = Iif(Type('toDate.nListareFactura') = 'N', toDate.nListareFactura = 2, .F.)

	llListareRecapitulatia = Iif(Type('toDate.nListareRecapitulatia') = 'N', Inlist(toDate.nListareRecapitulatia, 1, 2), .F.)
	llGenerareRecapitulatia = Iif(Type('toDate.nListareRecapitulatia') = 'N', toDate.nListareRecapitulatia = 2, .F.)

	llListareAlteDocumente = Iif(Type('toDate.nListareAlteDocumente') = 'L', toDate.nListareAlteDocumente = 1, .F.)
Endif

Select crsFacturi
lnRecno = Recno()
Locate For ales = 1
If !Found()
	Goto m.lnRecno
	lcWhere = m.lcWhere + 'recno() = ' + Alltrim(Str(m.lnRecno))
Else
	lcWhere = m.lcWhere + 'ales = 1'
Endif

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

Select F.id_vanzare, F.data_act, F.numar_act, F.serie_act, F.total_fara_tva, F.total_tva, F.total_cu_tva, F.client, ;
	email_facturare, Cast(Null As L) As email_succes, Cast(Null As c(250)) As email_mesaj ;
	From crsFacturi F ;
	Where &lcWhere ;
	Into Cursor cEmail Readwrite

lcSubjectTemplate = loMail.cSubject
lcBodyTemplate = loMail.cHtmlBody
Sele cEmail
Scan
	lcFiles = ""
	ldDataFactura = data_act
	lcSerieFactura = Alltrim(serie_act)
	lnNumarFactura = numar_act
	lcNume = Upper(Alltrim(client))
	lcClient = m.lcNume
	lnIdVanzare = id_vanzare
	loDate = Null

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

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

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

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

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

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

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

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

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

Use In (Select('cEmail'))

Select crsFacturi
Go lnRecno
