Lparameters toFacturiForm, toDate
Local lcBody, lcBodyTemplate, lcClickSignPDF, lcClient, lcContract, lcDivizia, lcEmail
Local lcErrorMessage, lcFacturaFile, lcFileSemnat, lcFiles, lcNume, lcRaportEmail, lcRun, lcSelect
Local lcSerieFactura, lcSql, lcSubject, lcSubjectTemplate, lcTipDocument, lcTo, lcValuta, lcWhere
Local lcXML, lcXMLFile, ldDataFactura, llClearErrors, llForceExit, llGenerareFactura, llInValuta
Local llJustPDF, llListareFactura, llSilent, llSucces, lnCurs, lnIdFact, lnIdVanzare, lnNumarFactura
Local lnOptDivizia, lnRecno, lnValoare, loDate, loMail

lcSelect = Select()

lcClickSignPDF = [C:\Program Files (x86)\clickSignPdf\clickSignPdfProfessional.exe]
If !File(m.lcClickSignPDF)
	lcClickSignPDF = [C:\Program Files\clickSignPdf\clickSignPdfProfessional.exe]
Endif
If !File(m.lcClickSignPDF)
	If AMESSAGEBOX('Nu este instalat clickSIGNpdf.exe! Continuati?',4+32,_Screen.Caption) <> 6
		Return
	Endif
Endif

loMail = getoEmail() && email.prg

lcWhere = ""
llListareFactura = .T.
llGenerareFactura = .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, .T.)
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, F.valuta, F.contract, F.Curs, F.in_valuta,;
	f.valval, F.tvaval, F.totval, F.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

If Reccount('cEmail') > 0

	lcFiles = ""
	Sele cEmail
	Scan
		ldDataFactura = data_act
		lcSerieFactura = Alltrim(serie_act)
		lnNumarFactura = numar_act
		lcNume = Upper(Alltrim(client))
		lcClient = m.lcNume
		lnIdVanzare = id_vanzare
		loDate = Null
		lcValuta = Upper(Alltrim(valuta))
		lcContract = Alltrim(Nvl(contract, ''))
		lnCurs = Curs
		llInValuta = (Nvl(in_valuta,0) = 1)

		lcValuta = Upper(Alltrim(Nvl(valuta,'')))
		Do Case
			Case !m.llInValuta
				lcValuta = 'RON'
			Case Left(m.lcValuta,3) = 'EUR' && EURO
				lcValuta = 'EUR'
		Endcase
		lnValoare = Iif(m.llInValuta, totval, total_cu_tva)

		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)
		lcFacturaValutaFile = GetPDFFacturaValutaFile(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
		Endif && llListareFactura

		If File(m.lcFacturaValutaFile) And File(m.lcFacturaFile)
			lnRaspuns = AMESSAGEBOX('Doriti semnarea facturii in valuta (DA) sau a facturii in lei (NU)?',4+32,_Screen.Caption)
			If m.lnRaspuns = 6
				lcFacturaFile = m.lcFacturaValutaFile
			Endif
		Endif
* Semnare PDF
* Lansare utilitar certsign.ro
		OPEN_DEFAULT_APP(Justpath(m.lcFacturaFile))	&& deschid directorul facturii
		If File(m.lcClickSignPDF)
			lcRun = [! /N "] + m.lcClickSignPDF + [" -i "] + m.lcFacturaFile + ["]
			&lcRun
		Endif

		lcFileSemnat = Strtran(m.lcFacturaFile, Juststem(m.lcFacturaFile), Juststem(m.lcFacturaFile) + '_signed')

		AMESSAGEBOX('Se verifica fisierul ' + lcFileSemnat + Chr(13) + Chr(10) + 'Apasati OK cand s-a semnat pdf-ul.',0, _Screen.Caption)
*WAIT WINDOW 'Se asteapta semnarea fisierului ' + m.lcFacturaFile NOWAIT

		llForceExit = .F.
		Do While .T.
			If File(m.lcFileSemnat)
				Exit
			Endif
			Wait Window 'Se verifica ' + m.lcFileSemnat Nowait
			If Inkey(1) = 27 && 'ESC'
				If AMESSAGEBOX('Doriti sa intrerupeti operatia?',4+32, _Screen.Caption) = 6
					llForceExit = .T.
					Exit
				Endif
			Endif
		Enddo

		If m.llForceExit
			Exit
		Endif

		If File(m.lcFileSemnat)
			loMail.EmailSetAttachment(m.lcFileSemnat)
		Endif

* 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)
ENDIF && Reccount('cEmail')

Use In (Select('cEmail'))

Select (m.lcSelect)



