#Define crlf Chr(13) + Chr(10)

Local lcSql, lcfile, cVersiune, lcClipText, lcSir, lcFisier, lcDir, lcFisVersDif, ;
	lcfileVersiuniMax, lnsucces, lnCustomerId

lnCustomerId = crs2xml.id


lcClipText = []
lcSir = []
lcFisier = []
lcDir = []
lcFisVersDif = []
x = SQLConnect("JCSSERVER","SOFT_SERII","123")
If x < 0
	Return
Endif

lcSql = [select DISTINCT customer_id, NUME, ID_PROGRAM, (CASE WHEN Upper(PROGRAM) ] + ;
	[LIKE 'INDEX%' THEN 'MANUAL' ELSE PROGRAM END) AS PROGRAM from vgen_programe ] + ;
	[where customer_id = ] + Alltrim(Str(crs2xml.id))
lnsucces = SQLExec(x, lcSql, "crsProgsTemp1")
If lnsucces < 0
	Return
Endif
SQLDisconnect(x)
*!* selectie combobox - un client, programele cu licenta pentru client
executaSql([select 0 as ales, v.customer, v.program, v.versiune, v.versiune_maxima, ]+ ;
	[v.id, v.customer_id from vsc_versiune_max_inst_dv v ] + ;
	[where v.customer_id = ] + Alltrim(Str(crs2xml.id)), "crsprogstemp2", .T.)
*!* selectie combobox - un client, programele cu licenta pentru client ^
executasql([select * from programs], "crsPrograms", .t.)
executasql([select * from sc_versiune_programe], "crsVersProg", .t.)

Select a.nume As customer, a.Program, b.versiune, b.versiune_maxima, a.customer_id, a.id_program ;
	From crsprogstemp2 b ;
	INNER Join crsProgsTemp1 a On a.customer_id = b.customer_id And b.Id = a.id_program ;
	ORDER By 2 ;
	INTO Cursor crsXmlOut

*SELECT c.customer, c.program, c.versiune, c.customer_id FROM crsprogstemp2 c ;
	JOIN crsprograms p ON p.id = c.id ORDER BY 2 ;
	WHERE !EMPTY(NVL(c.versiune,[])) ;
	INTO CURSOR crsProgLic

lcClipText = lcClipText + Alltrim(customer) + crlf + "VERSIUNI EXISTENTE: "
lcVersiune = ""
Set Textmerge On To Memvar lcVersiune Noshow
Set Textmerge Delimiters To '{{', '}}'
		\<?xml version='1.0'?>

Select crsXmlOut
*SELECT crsProgLic
Scan For customer_id = lnCustomerId
		\<{{Alltrim(PROGRAM)}}>
		\<codeupdate>
		\<version>{{Alltrim(VERSIUNE)}}</version>
		\<fileURL>{{'\_ARHIVE\' + Alltrim(PROGRAM) + '\' + Alltrim(PROGRAM) + '-' + Alltrim(VERSIUNE) + '.EXE'}}</fileURL>
		\<Usermsg>S-a gasit {{Alltrim(PROGRAM)}} versiunea {{Alltrim(VERSIUNE)}}. Programul va incepe procedurile de actualizare !</Usermsg>
		\</codeupdate>
		\</{{Alltrim(PROGRAM)}}>

	lcClipText = lcClipText + Alltrim(Program) + '-' + Alltrim(versiune) + ', '
Endscan
Set Textmerge To
lcfile = Putfile("Alegeti calea",Upper(Alltrim(crs2xml.Name)) + '.xml',"xml")
Strtofile(lcVersiune,lcfile)
If Messagebox("Doriti sa generati XML Aplicatii diferenta",4) = 6

	Select Distinct c.Program, c.versiune, c.versiune_maxima, c.id_program, c.customer_id , ;
		'\_ARHIVE\' + Alltrim(c.Program) + '\' + Alltrim(c.Program) + '-' + ;
		Alltrim(c.versiune_maxima) + '.EXE' As fisier ;
		FROM crsXmlOut c INNER Join porec p On p.customer_id = c.customer_id ;
		WHERE NVL(c.versiune,[]) <> NVL(c.versiune_maxima,[]) ;
		INTO Cursor crsVersiuniDiferiteTemp
	SELECT program , versiune_maxima as versiune, id_program, customer_id, fisier ;
		FROM crsVersiuniDiferiteTemp INTO CURSOR crsVersiuniDiferite

	*!*		 Cursor cu ultimele Versiuni

	lcFisier = Addbs(Upper(Alltrim(crs2xml.Name)))
	lcDir = Addbs(Justpath(lcfile)) + "ACTUALIZARIAPLICATII\" + lcFisier
	If Not Directory(lcDir)
		Md(lcDir)
	Endif
	lcFisVersDif = lcDir + "Aplicatii_" + Alltrim(crs2xml.Name) + ".XML"
	Cursortoxml("crsVersiunidiferite",lcFisVersDif,1,512,0,"1")

	*!*		Cursor cu Versiuni diferite ^
	lcClipText = lcClipText + crlf + 'VERSIUNI DIFERITE: '

	Select crsVersiuniDiferite
	Scan
		lcClipText = lcClipText + Alltrim(Program) + '-' + Alltrim(versiune) + ', '
	Endscan


	*!*		versiunea maxima a programelor instalate la client
	executaSql([select * from sc_versiune_programe], [crsVmax],.T.)
	Select c1.Program, c1.nume, cv.versiune_curenta As versiune From crsProgsTemp1 c1 ;
		JOIN crsvmax cv On c1.id_program = cv.id_program ;
		WHERE c1.customer_id = crs2xml.id ; &&porec.cust_id
		order By 1;
		into Cursor crsVersiuniMax
	Set Textmerge On To Memvar lcVersiune Noshow
	Select crsVersiuniMax
	Scan
		\<{{Alltrim(PROGRAM)}}>
		\<codeupdate>
		\<version>{{Alltrim(VERSIUNE)}}</version>
		\<fileURL>{{'\_ARHIVE\' + Alltrim(PROGRAM) + '\' + Alltrim(PROGRAM) + '-' + Alltrim(VERSIUNE) + '.EXE'}}</fileURL>
		\<Usermsg>S-a gasit {{Alltrim(PROGRAM)}} versiunea {{Alltrim(VERSIUNE)}}. Programul va incepe procedurile de actualizare !</Usermsg>
		\</codeupdate>
		\</{{Alltrim(PROGRAM)}}>
	Endscan

	Set Textmerge To
	lcfileVersiuniMax = Addbs(Justpath(lcFisVersDif)) +  Upper(Alltrim(crs2xml.Name)) + '.xml'
	Strtofile(lcVersiune,lcfileVersiuniMax )
	* Cursor cu ultimele Versiuni ^
	
	Use In (Select("crsProgLic"))
	Use In (Select("crsprograms"))		
	Use In (Select("crsVersiuniDiferite"))
	Use In (Select("crsVersiuniDiferiteTemp"))
	Use In (Select("crsProgsTemp1"))
	Use In (Select("crsProgsTemp2"))
	Use In (Select("crsVmax"))
	Use In (Select("crsVersiuniMax"))
	Use In (Select("crsxmlout"))
	Use In (Select("crsVersProg"))	
	
	_Cliptext = Nvl(lcClipText,[])

Endif

thisform.release()