PARAMETERS cBdd, cIdView, cIdServ


IF EMPTY(cIdServ)
	cIdServ = '1'
ENDIF


IF EMPTY(cBdd)
	cBdd='fss1'
ENDIF


PUBLIC oJs

oJs = CREATEOBJECT ('ClassJsonServer', cIdView, cIdServ)
*oJs.ltest = .T.


DEFINE CLASS ClassJsonServer AS CUSTOM 

cMsgResult = ''
cIdView = ''
cIdCmd=''
cParam=''
jParam=''
xmlfe=0
cIdServ = '1'
ltest = .F.
all_cust = .f.
venta_en_linea = .f.
cIdUser = '00000002'
cIdBodega = '00000002'
cIdSalesman = '00000001'
filtrar_item = .f.
cxc_vendedor = .f.

n_copia_cobro 	= 2
n_copia_venta 	= 2
tipo_prn 		= 1
frm_fc 			= ""
frm_nv 			= ""
frm_ne 			= ""
frm_rec 		= ""



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

	IF !EMPTY(cIdView)
		this.cIdView = cIdView
	ENDIF
	
	oApp.cMsgLabel = 'Hilo Nro. '+this.cIdServ

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 cset (cLlave, cValor, lFree)
LOCAL cRet, cV
	
	cV = ALLTRIM(TRANSFORM(cValor))
	
	cRet = '\"'+cLlave+'\":\"'+cV+'\"'+IIF(lFree,'',',')
	
*!*		TEXT TO cRet NOSHOW textmerge
*!*			<<cLlave>>:'<<cV>>\'<<IIF(lFree,'',',')>>
*!*		ENDTEXT
	
	
	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 ,'"','\"')
	sval = STRTRAN(sval ,"'","\'")
	sval = STRTRAN(sval ,CHR(13)," ")
	sval = STRTRAN(sval ,CHR(10),"")
	sval = STRTRAN(sval ,CHR(9)," ")
	
	sval = STRTRAN(sval ,CHR(0),"")
	
	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, cSepRow)

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


FUNCTION json_up (cTabla, cTablaDet)

	IF EMPTY(cTablaDet)

		cJson = this.json_encodeCursor(cTabla)
		*STRTOFILE(cJson,'\eddy.txt')
		this.ftpsend(cJson)
	ELSE
		*this.ftpsend(this.json_cd(cTabla, cTablaDet))
	ENDIF
	
	
ENDFUNC

FUNCTION json_encodeCursor(cTabla)
LOCAL cCampo, sRet, nCount
	
	DIMENSION aFi[1]
	nCount = AFIELDS(aFi)
	sRet = '['
	SELECT &cTabla
	GO top
	SCAN all
		sRet = sRet + '{'
		FOR iCount = 1 TO nCount-1
			cCampo = aFi[iCount,1]
			sRet = sRet + oApp.fixStr(cCampo,2)+':'+oApp.fixStr(&cCampo,2)+','
		ENDFOR
		cCampo = aFi[nCount,1]
		sRet = sRet + oApp.fixStr(cCampo,2)+':'+oApp.fixStr(&cCampo,2)+'},'
	ENDSCAN
	sRet = SUBSTR(sRet,1,LEN(sRet)-1)+']'
	sRet = STRTRAN(sRet,CHR(13),'')
	sRet = STRTRAN(sRet,CHR(10),'')
	sRet = STRTRAN(sRet,CHR(9),'')
	RETURN sRet
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_ + this.fixStr(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, cPipeta)
	LOCAL nLen, ii, _cCmd_
	
	IF EMPTY(cPipeta)
		cPipeta = '|'
	ENDIF
	
	
	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,cPipeta,'')
	ENDFOR
	return _cCmd_
ENDFUNC

FUNCTION rmi_regis


FUNCTION ftpsend(cCmd)

	cIdCmd = ALLTRIM(TRANSFORM(oJs.cIdCmd))

	IF oApp.lGhostLocal
	
	
		TEXT TO sCmd NOSHOW TEXTMERGE 
			UPDATE ghost.commander SET estado=1,respuesta="<<cCmd>>" WHERE id="<<cIdCmd>>"
		ENDTEXT 
		oApp.cWebResult = cCmd
		
		&&MESSAGEBOX(cCmd)
		
		*STRTOFILE(cCmd,'\temp\ftpsendcmd.txt')
		*STRTOFILE(sCmd,'\temp\ftpsend.txt')


		oApp.SQLExec_local(sCmd)
		RETURN .T.
	ENDIF
	
	
	cZipFileName = SYS(2015)+'.zip'
	cTxtFileName = SYS(2015)+'.txt'

	STRTOFILE(cCmd,cTxtFileName)

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

	lRetVal = .T.

	*MESSAGEBOX( oApp.host +CHR(13) +oApp.ftphost+CHR(13)+oApp.ftpuser+CHR(13)+oApp.ftppswd)

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

	    cCmd = "json/rmi_regis.php?path="+cZipFileName+'&id='+ALLTRIM(TRANSFORM(oJs.cIdCmd))
	    
	    *STRTOFILE(cCmd, '\eddyenviado.txt')
	    cWeb = "--"
	    oApp.runLink(cCmd,@cWeb)
	   	*MESSAGEBOX(cWeb)
	ELSE
		messagebox('Error ftp: '+oIp.cErrorMsg)
		lRetVal = .T.
	ENDIF
	DELETE FILE (cZipFileName)
	DELETE FILE(cTxtFileName)

	RETURN lRetVal 

ENDFUNC



FUNCTION descargarMensajesLocal()

	IF oApp.SQLExec_local('select id, cmd, cast(jparam as char) as jparam, cast(param as char) as param,iduser from ghost.commander WHERE idc='+oApp.json_idc+' and estado=0 and !leido ','curCmd')>0
	
		SELECT curCmd

		SCAN all
			c_IdCmd  = ALLTRIM(TRANSFORM(curCmd.id))
			c_cmd    = ALLTRIM(curCmd.cmd)
			c_Param  = ALLTRIM(curCmd.Param)
			c_jParam = ALLTRIM(curCmd.jParam)
			c_iduser = ALLTRIM(TRANSFORM(curCmd.iduser))
			
			TEXT TO cCmd NOSHOW TEXTMERGE
				INSERT INTO ghost_json (idcmd,cmd,param, jparam, iduser, fechahora) VALUES 
				('<<c_IdCmd>>','<<c_cmd>>','<<c_Param>>','<<c_jParam>>','<<c_iduser>>', '<<ttoc(DATETIME(),1)>>')
			ENDTEXT
			
			oApp.sqlexec(cCmd)
			
			oApp.SQLExec_local('update ghost.commander SET leido=1 WHERE id='+TRANSFORM(curCmd.id))
		ENDSCAN
	ENDIF
	
ENDFUNC


FUNCTION descargarMensajesWeb()

	cCmd = 'json/down_msg.php?todos=1&idc='+oApp.json_idc

	nSecon = SECONDS()
	lSeguir = oApp.runlink(cCmd, @cWebResult)
	nTimeOut = SECONDS()-nSecon	
	
	
	this.json.Caption = 'Rate '+TRANSFORM(nTimeOut)+ ' seg.'

	IF lSeguir AND !EMPTY(cWebResult) AND cWebResult!='@@' 

		DIMENSION acampos[1]
		nLen = oApp.explode(cWebResult,@aCampos,'¯')  && chr  175 
				
		FOR iCount=1 TO nLen
			crow = aCampos[iCount]
			
			IF !EMPTY(crow)
				DIMENSION acamposK[1]
				nLenK = oApp.explode(cRow,@aCamposK,'[')
				
				IF nLenK>3
					
					c_IdCmd  = acamposK[1]
					c_iduser = acamposK[2]
					c_cmd    = acamposK[3]
					c_Param  = acamposK[4]
					c_jParam = ""
					IF nLenK=5
						c_jParam = acamposK[5]
					ENDIF
					
					TEXT TO cCmd NOSHOW TEXTMERGE
						INSERT INTO ghost_json (idcmd,cmd,param, jparam, iduser) VALUES (<<c_IdCmd>>,'<<c_cmd>>','<<c_Param>>','<<c_jParam>>','<<c_iduser>>')
					ENDTEXT
					
					oApp.sqlexec(cCmd)
				ENDIF
				
			ENDIF
		ENDFOR
	ENDIF

ENDFUNC

FUNCTION procesar
PARAMETERS oForm

	oApp.closeDb()
	oApp.openDb()

	oApp.sqlexec('UPDATE ghost_json SET leido="'+this.cIdServ+'"  where leido=0 limit 1')
	
	TEXT TO cCmd NOSHOW textmerge
		select id,
			idcmd,
			cmd,
			cast(param as char) as param,
			cast(jparam as char) as jparam,
			iduser 
		from ghost_json 
		where leido="<<this.cIdServ>>" limit 1
	ENDTEXT
	
				&&cast(jparam as char) as 	
	IF oApp.sqlexec(cCmd,'curProc')>0
		IF !oJs.ltest
			*oApp.sqlexec('DELETE FROM ghost_json WHERE id='+TRANSFORM(curProc.id))
			oApp.sqlexec('update ghost_json set leido=-1 WHERE id='+TRANSFORM(curProc.id))
		ENDIF

		
		cAccion    = ALLTRIM(curProc.cmd)
		oJs.cIdCmd = ALLTRIM(STR(curProc.idcmd))
		oJs.cParam = ALLTRIM(curProc.param)
		oJs.jParam = ALLTRIM(curProc.jparam)
		oJs.cIdUser = ALLTRIM(TRANSFORM(curProc.iduser))
		
		&&MESSAGEBOX(oJs.cParam)
		
		oApp.sqlexec('SELECT * from user where id='+oJs.cIdUser,'curUser')
		
		oJs.filtrar_item 	= (curUser.filtrar_item=1)
		oJs.cxc_vendedor 	= (curUser.cxc_vendedor=1)
		oJs.n_copia_cobro 	= curUser.n_copia_cobro
		oJs.n_copia_venta 	= curUser.n_copia_venta
		oJs.tipo_prn 		= curUser.tipo_prn 
		oJs.frm_fc 			= curUser.frm_fc 
		oJs.frm_nv 			= curUser.frm_nv 
		oJs.frm_ne 			= curUser.frm_ne 
		oJs.frm_rec 		= curUser.frm_rec 

		
		oJs.all_cust = (curUser.all_cust=1)
		oJs.venta_en_linea = (curUser.venta_en_linea=1)
		
		oJs.cIdSalesman = '00000001'
		oJs.cIdBodega = '00000002'
		IF !EMPTY(curUser.idbodega)
			oJs.cIdBodega = ALLTRIM(curUser.idbodega)
			oJs.cIdSalesman = ALLTRIM(curUser.idsalesman)
		ENDIF
		
		TEXT TO cMsgEvent NOSHOW textmerge
			<<ALLTRIM(curUser.login)+'('+oJs.cIdUser+'-'+oJs.cIdBodega+')'>> <<TRANSFORM(oJs.cIdCmd)>> » <<cAccion>>
			(<<ALLTRIM(SUBSTR(oJs.cParam,1,120))>> <<ALLTRIM(SUBSTR(oJs.jParam,1,120))>>)
		ENDTEXT		
		
		IF VARTYPE(oForm)='O'
			oForm.addevent(cMsgEvent)
		ELSE
			? cMsgEvent			
		ENDIF
		
		
		cAccion = ALLTRIM(oApp.fixChars(cAccion))
		
		
		IF SUBSTR(cAccion,1,3)='j__'
			DO j_run.prg WITH ALLTRIM(SUBSTR(cAccion,4))
		ELSE
			&&cAccion = cAccion + '.prg'
			WAIT WINDOW 'Ejecutando... '+cAccion NOWAIT 
			DO &cAccion 
		ENDIF
		
		
	ENDIF


ENDFUNC



ENDDEFINE