PARAMETERS cBdd, cIdView

SET TALK off
SET STATUS BAR OFF
SET SYSMENU off
SET DELETED on
SET CONFIRM OFF
SET NOTIFY off
SET SAFETY off
SET CENTURY ON
SET DATE TO DMY
SET FIXED OFF
SET DECIMALS TO 6
_SCREEN.Visible=.F.


IF EMPTY(cBdd)
	cBdd='fss1'
ENDIF


*** Clases para el manejo de Web
SET PATH TO ".\classes" ADDITIVE

SET PROCEDURE TO wwHTTP ADDITIVE   
SET PROCEDURE TO wwAPI ADDITIVE
SET PROCEDURE TO wwFTP ADDITIVE
SET PROCEDURE TO wwUtils ADDITIVE

SET PROCEDURE TO json/qdfoxJSON ADDITIVE
JSONStart()


DO source\app.prg WITH cBdd
DO source\invoicer.prg


SET PATH TO ".\json\boleteria" ADDITIVE
SET PATH TO ".\json\" ADDITIVE

DO json\json_cliente.prg

*oJson.initMon()


PUBLIC oJs

oJs = CREATEOBJECT ('ClassJsonServer', cIdView)
oJs.run()



DEFINE CLASS ClassJsonServer AS CUSTOM 

cMsgResult = ''
cIdView = ''
cIdCmd=''
cParam=''
xmlfe=0


FUNCTION init (cIdView) 
	
	IF !EMPTY(cIdView)
		this.cIdView = cIdView
	ENDIF

ENDFUNC	

FUNCTION run()

	this.procesar ()

	oApp.setView (this.cIdView,2)
	oApp.setSetup('Ghost_lJsonBuzy',.F.,'L')

ENDFUNC

FUNCTION procesar ()
LOCAL cAccion, cWebResult, iCount, cCmd, cParam
	oApp.runLink('json/procesar.php', @cWebResult)

	*WAIT WINDOW '' TIMEOUT 1
		
	*MESSAGEBOX(cWebResult)
	IF cWebResult!='@@'
	
		o = JSON.Parse(cWebResult)
		
		oApp.runLink('json/set_activity.php?activity=1', @cWebResult)

		FOR iCount=1 TO o.Count
			oApp.msg_add(this.cIdView,'Evento '+o.Item[iCount].id)
			cAccion = o.Item[iCount].cmd
			this.cIdCmd = o.Item[iCount].id
			this.cParam = ALLTRIM(o.Item[iCount].param)
			cAccion = ALLTRIM(cAccion) + '.prg'
			
			*messagebox(cAccion)			
			DO &cAccion 
		ENDFOR
		
		
		
	ENDIF

ENDFUNC


FUNCTION jset (cLlave, cValor, lFree)
LOCAL cRet, cV
	
	cV = ALLTRIM(TRANSFORM(cValor))
	IF VARTYPE(cValor)='N'
		cRet = cLlave+':'+cV+IIF(lFree,'',',')
	ELSE
		cRet = cLlave+':\"'+cV+'\"'+IIF(lFree,'',',')
	ENDIF
	
	RETURN cRet
ENDFUNC

FUNCTION sset (cLlave, cValor, lFree)
LOCAL cRet, cV
	
	cV = ALLTRIM(TRANSFORM(cValor))
	IF VARTYPE(cValor)='N'
		cRet = cLlave+':'+cV+IIF(lFree,'',',')
	ELSE
		cRet = cLlave+':"'+cV+'"'+IIF(lFree,'',',')
	ENDIF
	
	RETURN cRet
ENDFUNC


FUNCTION cll (cValor, lFree)

	IF ISNULL(cValor)
		cValor = ''
	ENDIF
	

	IF VARTYPE(cValor)='N'
		cValor = TRANSFORM(cValor,'9999999.9999')
	ENDIF
	
	cValor = ALLTRIM(cValor)
	RETURN '\"'+cValor+'\"'+IIF(lFree,'',',')
ENDFUNC



FUNCTION fixStr(sVal)
	IF ISNULL(sval)
		RETURN ""
	ENDIF

	IF vartype(sval)='N'
		sval = transform(sVal)
	ENDIF
	
	IF vartype(sval)='D'
		sval = DTOC(sVal,1)
	ENDIF

	sval = STRTRAN(sval ,'"','\"')
	RETURN ALLTRIM(sVal)


FUNCTION android_cd (curCab, curDet)

LOCAL nLen, ii, cRet

*idcustomer,date,total

	cRet = this.android (curCab, 1) + '~' + this.android (curDet, 1)
	
	SELECT &curCab
	SCAN all
		cRet = cRet + '{'
		cRet = cRet + this.android_row(curCab)
				
		cId = &curCab..id
		
		SELECT * FROM &curDet WHERE idCab = cId INTO CURSOR curDet0
		IF _tally>0
			cRet = cRet + '~'
			cRet = cRet + this.android('curDet0',2,'_')
		ENDIF
		
		
	ENDSCAN

	*STRTOFILE(cret,'\eddy.txt')
	RETURN cRet
ENDFUNC


FUNCTION android_up (cTabla, cTablaDet)

	IF EMPTY(cTablaDet)
		this.ftpsend(this.android (cTabla))
	ELSE
		this.ftpsend(this.android_cd(cTabla, cTablaDet))
	ENDIF
	
	
ENDFUNC


FUNCTION android (cTabla, nOp, cSepRow, nLimit)
LOCAL nLen, ii, iCampo,cCmd_

	DIMENSION aCampos[1]
	nLen = AFIELDS(aCampos,cTabla)
	
	IF EMPTY(cSepRow)
		cSepRow = '~'
	ENDIF
	
	IF EMPTY(nLimit)
		nLimit = 0
	ELSE
		nLen = nLimit
	ENDIF
	
	
	IF EMPTY(nOp)
		nOp = 0  && cabecera y valores
	ENDIF

	cCmd_ = ''
	
	IF nOp < 2  && 2 = para solo valores 
		FOR ii = 1 TO nLen
			iCampo = aCampos[ii,1]
			cCmd_ = cCmd_ + iCampo + IIF(ii<nLen,'|','')
		ENDFOR
	ENDIF
	
	IF nOp = 1  && solo nombres de campos
		RETURN cCmd_
	ENDIF
	
	
	IF cSepRow!='_'

		cCmd_ = cCmd_ + cSepRow
	
	ENDIF
	

	iCount = 0

	SELECT &cTabla
	GO top

	SCAN all
		IF iCount>0
			cCmd_ = cCmd_ + cSepRow
		ENDIF
		
		cCmd_ = cCmd_ + this.android_row(cTabla)	

		SELECT &cTabla
		iCount = iCount + 1
	ENDSCAN
	
	*MESSAGEBOX(cCmd_)
	
	RETURN cCmd_
ENDFUNC

FUNCTION android_row (cTabla)
	LOCAL nLen, ii, _cCmd_
	
	DIMENSION aCampos[1]
	nLen = AFIELDS(aCampos,cTabla)
	_cCmd_ = ''
	FOR ii = 1 TO nLen
		iCampo = aCampos[ii,1]
		_cCmd_ = _cCmd_ + this.fixstr(&iCampo)+IIF(ii<nLen,'|','')
	ENDFOR
	return _cCmd_
ENDFUNC




FUNCTION ftpsend(cCmd)
	
	cZipFileName = SYS(2015)+'.zip'
	cTxtFileName = SYS(2015)+'.txt'

	STRTOFILE(cCmd,cTxtFileName)

	cmdZip =  '7z a -tzip -mx=9 '+cZipFileName+' '+cTxtFileName
	RUN &cmdZip

	lRetVal = .T.

	IF oIp.FTPSendFile (oApp.FtpHost,;
	            cZipFileName,;
	            cZipFileName,;
	            oApp.FtpUser,;
	            oApp.FtpPswd)=0

	    cCmd = "json/registrar.php?path="+cZipFileName+'&id='+oJs.cIdCmd
	    
	    *STRTOFILE(cCmd, '\eddy.txt')
	    
	    oApp.runLink(cCmd)
	ELSE
		messagebox('Error ftp: '+oIp.cErrorMsg)
		lRetVal = .T.
	ENDIF
	DELETE FILE (cZipFileName)
	DELETE FILE(cTxtFileName)

	RETURN lRetVal 

ENDFUNC


ENDDEFINE