PUBLIC  oJson

oJson = CREATEOBJECT ('ClassJson')


DEFINE CLASS ClassJson AS CUSTOM 

Host 		= 'www.tecsicom.net'
FtpHost     = 'ftp.tecsicom.net'
FtpUser 	= 'data@tecsicom.net'
FtpPswd 	= 'tecsicom2012'

cWebResult = ''
cIdUser = '1'
cRuc    = ''
cIdc    = ''
json_idc = 0

cIdu 	= ''
cIds	= ''
cFileName = ''
cBddName = '' 

cDirBddPd = '\ghost_pd'
cFileNamePd = ''
cBddNamePd = '' 

_cFileName = ''
_cBddName = '' 



t_nfields = 0



FUNCTION init
	DECLARE aV4 [1]
	
	oApp.explode(FILETOSTR('v4.ini'),@aV4)
	
	
	this.cIdc = aV4[1]
	this.cIdu = aV4[2]
	this.cIds = aV4[3]
	
	this.cFileName = this.cIdc+'_'+this.cIdu
	this.cBddName = this.cFileName+'.sqlite'
	
	

	this.cFileNamePd = this.cDirBddPd+'\p'+this.cFileName
	this.cBddNamePd = this.cFileNamePd+'.sqlite'

	
	IF !DIRECTORY(this.cDirBddPd)
		MKDIR (this.cDirBddPd)
	ENDIF
	
ENDFUNC


FUNCTION initMon
LOCAL cCmd


	cCmd = 'json/json_init.php?ruc='+this.cIdc

	*MESSAGEBOX(cCmd)

	this.runLink(cCmd)
	
	this.json_idc = '0'
	IF This.cWebResult='0'
		MESSAGEBOX('No esta registrada esta empresa o hay problemas en la conexión.',63,'Ghost')
	ELSE
		this.json_idc = This.cWebResult
	ENDIF
	
	*MESSAGEBOX(this.json_idc)
ENDFUNC


FUNCTION run (cCmd, cParam, cAccion, oForm, lnowait)
LOCAL cIdCmd, cMsgOut, cResult, cSecond

	cMsgOut = cCmd+'.. ' 
	*WAIT WINDOW cMsgOut nowait
	
	cCmd = '/json/run.php?iduser='+this.cIdUser+'&cmd='+cCmd+'&param='+cParam+'&idc='+this.json_idc

	*MESSAGEBOX(cCmd)
	
	nSecond = SECONDS()
	WAIT WINDOW cMsgOut+ '  '+TRANSFORM(this.json_idc) nowait
	this.runLink(cCmd, @cResult)

	IF cResult!='@@' 			
		nTimeOut = SECONDS()-nSecond
		WAIT WINDOW cMsgOut+TRANSFORM(nTimeOut)+ ' seg. '+TRANSFORM(this.json_idc) nowait
		
		IF vartype(oForm)='O'
			oForm.&cAccion
		ENDIF
	ENDIF
	
ENDFUNC 



FUNCTION traer (cCmd, cParam, cAccion, oForm, lnowait)
LOCAL iCount, lRet, cIdCmd, cMsgOut, cResult, cmdEjec, oForm, cSecond

	cMsgOut = cCmd+'.. ' 
	WAIT WINDOW cMsgOut nowait
	
	cCmd = '/json/solicitar.php?iduser='+this.cIdUser+'&cmd='+cCmd+'&param='+cParam+'&idc='+this.json_idc
	
	
	*MESSAGEBOX(cCmd)
	this.runLink(cCmd, @cResult)

	IF cResult!='@@' 
		cIdCmd = cResult

		lRet = .f.
		
		nSecond = SECONDS()
			
		nLimit = 30
		IF lnowait 
			nLimit = 3000
		ENDIF
		

		FOR iCount = 1 TO nLimit 
		
			IF INKEY() #0
				IF INKEY(0)=27
					IF messagebox('Terminar proceso',32+4,'Ghost')=6
						exit
					ENDIF
				ENDIF			
			ENDIF
			
			nTimeOut = SECONDS()-nSecond
			WAIT WINDOW cMsgOut+TRANSFORM(nTimeOut)+ ' seg. '+TRANSFORM(this.cidc) nowait
			
			IF oApp.runLink('/json/respuesta.php?id='+cIdCmd, @cResult)
				IF (cResult != '@@')
					lRet = .t.
					EXIT
				ENDIF
			ENDIF
		ENDFOR
		
		this.cWebResult = '@@'
		IF lRet
			this.cWebResult = cResult
			IF vartype(oForm)='O'
				oForm.&cAccion
			ENDIF
		ELSE
			nTimeOut = SECONDS()-nSecond
			WAIT WINDOW cMsgOut+TRANSFORM(nTimeOut)+ ' seg.  SIN RESPUESTA' nowait
		ENDIF
		*WAIT WINDOW this.cWebResult  NOWAIT
	ENDIF
	
ENDFUNC 


FUNCTION runLink(cHttp,cResult)
LOCAL lcHTML,lnText,lRet 

	IF VARTYPE(oHTTP)!='O'
		messagebox ('oHTTP no inicializado!','Ghost',63)		
		RETURN .F.
	ENDIF

	lcHTML=""
	lnText=0
	
	lRet = -1
	
	This.cWebResult = ''
	IF this.HTTPConnect ()
		lRet = oHTTP.HTTPGetEx(cHttp,@lcHTML,@lnText)
		*STRTOFILE(cHttp,'\eddy.txt')
		*MESSAGEBOX(cHttp)
		cResult = lcHTML
		This.cWebResult = lcHTML
		oHTTP.HTTPclose()
	ENDIF
	
	IF This.cWebResult=='**'
		MESSAGEBOX('No conectado!',63,'Ghost')
	ENDIF
	
	
	RETURN lRet=0
ENDFUNC

FUNCTION HTTPConnect ()
	IF VARTYPE(oHTTP)!='O'
		RETURN .F.
	ENDIF
	
	nConnHost = oHTTP.HTTPConnect(this.host)
	
	IF nConnHost <>0	
		MESSAGEBOX("Sin conexión a internet",63,"Ghost")
		RETURN .f.		
	ENDIF
	RETURN .t.
ENDFUNC

FUNCTION jset (cLlave, cValor, lFree)
	RETURN cLlave+':"'+cValor+'"'+IIF(lFree,'',',')
ENDFUNC


FUNCTION subir
PARAMETERS db, nToken, cFunc, cTablaDest, l_Update

	LOCAL cCmd, nRecsnSeconds, nSeconds , cTabla, cCampos, cMsg, cWhere0
	


	WAIT WINDOW 'Token '+STR(nToken) nowait

	nSeconds = aDef[nToken,1]
	cTabla   = aDef[nToken,2]
	cCampos  = aDef[nToken,3]
	cMsg 	 = aDef[nToken,4]
	cWhere0	 = aDef[nToken,5]
	
	IF EMPTY(cTablaDest)
		cTablaDest = cTabla   
	ENDIF
	
	
	
	IF aDef[nToken,1] = 0
		aDef[nToken,1] = SECONDS()
	
		IF EMPTY(cFunc)
			cWhere = ''
			IF !EMPTY(cWhere0)
				cWhere = ' WHERE '+cWhere0
			ENDIF

			cCmd = 'SELECT '+cCampos+' FROM '+cTabla+cWhere

			nRecs = oApp.SQLExec  (cCmd,cTabla)
			
			*MESSAGEBOX(cTabla+STR(nRecs))
			oApp.SQLExec ('UPDATE '+cTabla+' SET ya=1 WHERE ya=0')
		ELSE
			nRecs = this.&cFunc (nToken)
		ENDIF
		
		DIMENSION aCampos[1]
		nLen = oApp.stringToArray (cCampos,@aCampos,',')	
			
		cCmd = ''

		iCount = 0
	
		IF !EMPTY(l_Update)
			l_Update = .t.
		ELSE
			l_Update = .F.
		ENDIF
		

		SELECT &cTabla
		GO top

		SCAN all
		
			IF l_update
			
				cCmd = cCmd + 'UPDATE '+cTablaDest+' SET '

				FOR ii = 2 TO nLen
					cCmd = cCmd + aCampos[ii]+'='
					iCampo = aCampos[ii]
					cCmd = cCmd + this.fixstr(&iCampo) + IIF(ii<nLen,',','')
				ENDFOR
				
				iCampo = aCampos[1]
				cCmd = cCmd + 'WHERE '+aCampos[1] + '=' +this.fixstr(&iCampo) + ';'+CHR(13)
				

			ELSE
			
				cCmd = cCmd + 'INSERT INTO '+cTablaDest+' ('

				FOR ii = 1 TO nLen
					cCmd = cCmd + aCampos[ii]+IIF(ii<nLen,',','')
				ENDFOR

				cCmd = cCmd + ') VALUES ('

				FOR ii = 1 TO nLen
					iCampo = aCampos[ii]
					cCmd = cCmd + this.fixstr(&iCampo)+IIF(ii<nLen,',','')
				ENDFOR
				
				cCmd = cCmd + ');' + CHR(13)

			ENDIF
							
			*db.Execute(cCmd)

			SELECT &cTabla
			iCount = iCount + 1
			WAIT WINDOW cTabla+' '+PADL(INT(iCount/nRecs*100),3)+'%' nowait

		ENDSCAN
		
		IF oJs.ltest
			strtofile (cCmd,'\eddy.txt')
		ENDIF
			
		IF !EMPTY(cCmd)	
			db.Execute(cCmd)
		ENDIF
		
		WAIT WINDOW cTabla+' '+ALLTRIM(STR(nRecs))+' registros' NOWAIT 

		IF iCount>0 
			*this.ftpsend('up_'+cTabla,cCmd, ltest)
			*this.msg_add(aDef[nToken,4]+CHR(13)+' No. Reg. ' + allt(STR(iCount)) + '  en  ' + allt(STR(SECONDS()-aDef[nToken,1],10,2)) + ' seg')
		ENDIF
		
		aDef[nToken,1] = 0 
	ENDIF
ENDFUNC



FUNCTION cxc (nToken)
	

	TEXT TO cCmd NOSHOW	
	
		SELECT	A.reference as referencia,
			  	CAST(A.Date as char) as fecha, 
				CAST(A.Due as char) as vence,
				CAST(ROUND(A.Total,2) as char) as total, 
				CAST(ROUND(A.Balance,2) as char) as saldo, 
				CAST(A.Cuota as char) as cuota,
				M.idcustomer
		FROM AccountCus AS M INNER JOIN AccountCusCuota AS A on M.id = a.idaccountcus 
				LEFT JOIN Accountype      AS C on A.IdAccountype = C.Id 
				LEFT JOIN invoice         AS I on M.idinvoice = i.id 
		WHERE A.Balance != 0 AND !M.void AND !M.agrupado 
		ORDER BY M.idcustomer, A.date, A.reference, A.Cuota 
		
	ENDTEXT
	
	&&cCmd = STRTRAN(cCmd,"@idcustomer",cIdCust)

	RETURN oApp.SQLExec (cCmd, aDef[nToken,2])

ENDFUNC






FUNCTION stock_bod (nToken)
		
	&&this.msg_add('Cargando stock general de bodegas...')
	
	*cCondItem = ' WHERE stock.ya=3 '
	
	cCondItem = '  '
	
	*nToken = tk_stock_bod

	cTabla   = aDef[nToken,2]

	TEXT TO cCmd NOSHOW	
		select i.id
			 ,if(isnull(stock.stock), 00000000.00, stock.stock) as stock
			 ,if(isnull(b00.cantidad), 00000000.00, b00.cantidad) as stk00
			 ,if(isnull(b01.cantidad), 00000000.00, b01.cantidad) as stk01
			 ,if(isnull(b02.cantidad), 00000000.00, b02.cantidad) as stk02
			 ,if(isnull(b03.cantidad), 00000000.00, b03.cantidad) as stk03
			 ,if(isnull(b04.cantidad), 00000000.00, b04.cantidad) as stk04
			 ,if(isnull(b05.cantidad), 00000000.00, b05.cantidad) as stk05
			 ,if(isnull(b06.cantidad), 00000000.00, b06.cantidad) as stk06
			 ,if(isnull(b07.cantidad), 00000000.00, b07.cantidad) as stk07
			 ,if(isnull(b08.cantidad), 00000000.00, b08.cantidad) as stk08
			 ,if(isnull(b09.cantidad), 00000000.00, b09.cantidad) as stk09
			 ,if(isnull(b10.cantidad), 00000000.00, b10.cantidad) as stk10
			 ,if(isnull(b11.cantidad), 00000000.00, b11.cantidad) as stk11
			 ,if(isnull(b12.cantidad), 00000000.00, b12.cantidad) as stk12
			 ,if(isnull(b13.cantidad), 00000000.00, b13.cantidad) as stk13
			 ,if(isnull(b14.cantidad), 00000000.00, b14.cantidad) as stk14
			 ,if(isnull(b15.cantidad), 00000000.00, b15.cantidad) as stk15
			 ,if(isnull(b16.cantidad), 00000000.00, b16.cantidad) as stk16
			 ,if(isnull(b17.cantidad), 00000000.00, b17.cantidad) as stk17
			 ,if(isnull(b18.cantidad), 00000000.00, b18.cantidad) as stk18
			 ,if(isnull(b19.cantidad), 00000000.00, b19.cantidad) as stk19
			 ,if(isnull(b20.cantidad), 00000000.00, b20.cantidad) as stk20
			 ,if(isnull(b21.cantidad), 00000000.00, b21.cantidad) as stk21
			 ,if(isnull(b22.cantidad), 00000000.00, b22.cantidad) as stk22
			 ,if(isnull(b23.cantidad), 00000000.00, b23.cantidad) as stk23
			 ,if(isnull(b24.cantidad), 00000000.00, b24.cantidad) as stk24
			 ,if(isnull(b25.cantidad), 00000000.00, b25.cantidad) as stk25
			 ,if(isnull(b26.cantidad), 00000000.00, b26.cantidad) as stk26
			 ,if(isnull(b27.cantidad), 00000000.00, b27.cantidad) as stk27
			 ,if(isnull(b28.cantidad), 00000000.00, b28.cantidad) as stk28
			 ,if(isnull(b29.cantidad), 00000000.00, b29.cantidad) as stk29
			 ,if(isnull(b30.cantidad), 00000000.00, b30.cantidad) as stk30
			 ,if(isnull(b31.cantidad), 00000000.00, b31.cantidad) as stk31
			 ,if(isnull(b32.cantidad), 00000000.00, b32.cantidad) as stk32
			 ,if(isnull(b33.cantidad), 00000000.00, b33.cantidad) as stk33
			 ,if(isnull(b34.cantidad), 00000000.00, b34.cantidad) as stk34
			 ,if(isnull(b35.cantidad), 00000000.00, b35.cantidad) as stk35
			 ,if(isnull(b36.cantidad), 00000000.00, b36.cantidad) as stk36
			 ,if(isnull(b37.cantidad), 00000000.00, b37.cantidad) as stk37
			 ,if(isnull(b38.cantidad), 00000000.00, b38.cantidad) as stk38
			 ,if(isnull(b39.cantidad), 00000000.00, b39.cantidad) as stk39
			 ,if(isnull(b40.cantidad), 00000000.00, b40.cantidad) as stk40
			 ,if(isnull(b41.cantidad), 00000000.00, b41.cantidad) as stk41
			 ,if(isnull(b42.cantidad), 00000000.00, b42.cantidad) as stk42
			 ,if(isnull(b43.cantidad), 00000000.00, b43.cantidad) as stk43
			 ,if(isnull(b44.cantidad), 00000000.00, b44.cantidad) as stk44
			 ,if(isnull(b45.cantidad), 00000000.00, b45.cantidad) as stk45
			 ,if(isnull(b46.cantidad), 00000000.00, b46.cantidad) as stk46
			 ,if(isnull(b47.cantidad), 00000000.00, b47.cantidad) as stk47
			 ,if(isnull(b48.cantidad), 00000000.00, b48.cantidad) as stk48
			 ,if(isnull(b49.cantidad), 00000000.00, b49.cantidad) as stk49
			 ,if(isnull(b50.cantidad), 00000000.00, b50.cantidad) as stk50	 
			 ,if(isnull(b51.cantidad), 00000000.00, b51.cantidad) as stk51
			 ,if(isnull(b52.cantidad), 00000000.00, b52.cantidad) as stk52
			 ,if(isnull(b53.cantidad), 00000000.00, b53.cantidad) as stk53
			 ,if(isnull(b54.cantidad), 00000000.00, b54.cantidad) as stk54
			 ,if(isnull(b55.cantidad), 00000000.00, b55.cantidad) as stk55
			 ,if(isnull(b56.cantidad), 00000000.00, b56.cantidad) as stk56
			 ,if(isnull(b57.cantidad), 00000000.00, b57.cantidad) as stk57
			 ,if(isnull(b58.cantidad), 00000000.00, b58.cantidad) as stk58
		from item as i
			left join stock           on i.id=stock.iditem
			left join stockbod as b00 on b00.idbodega=00 and i.id=b00.iditem
			left join stockbod as b01 on b01.idbodega=01 and i.id=b01.iditem
			left join stockbod as b02 on b02.idbodega=02 and i.id=b02.iditem
			left join stockbod as b03 on b03.idbodega=03 and i.id=b03.iditem
			left join stockbod as b04 on b04.idbodega=04 and i.id=b04.iditem
			left join stockbod as b05 on b05.idbodega=05 and i.id=b05.iditem
			left join stockbod as b06 on b06.idbodega=06 and i.id=b06.iditem
			left join stockbod as b07 on b07.idbodega=07 and i.id=b07.iditem
			left join stockbod as b08 on b08.idbodega=08 and i.id=b08.iditem
			left join stockbod as b09 on b09.idbodega=09 and i.id=b09.iditem
			left join stockbod as b10 on b10.idbodega=10 and i.id=b10.iditem
			left join stockbod as b11 on b11.idbodega=11 and i.id=b11.iditem
			left join stockbod as b12 on b12.idbodega=12 and i.id=b12.iditem
			left join stockbod as b13 on b13.idbodega=13 and i.id=b13.iditem
			left join stockbod as b14 on b14.idbodega=14 and i.id=b14.iditem
			left join stockbod as b15 on b15.idbodega=15 and i.id=b15.iditem
			left join stockbod as b16 on b16.idbodega=16 and i.id=b16.iditem
			left join stockbod as b17 on b17.idbodega=17 and i.id=b17.iditem
			left join stockbod as b18 on b18.idbodega=18 and i.id=b18.iditem
			left join stockbod as b19 on b19.idbodega=19 and i.id=b19.iditem
			left join stockbod as b20 on b20.idbodega=20 and i.id=b20.iditem
			left join stockbod as b21 on b21.idbodega=21 and i.id=b21.iditem
			left join stockbod as b22 on b22.idbodega=22 and i.id=b22.iditem
			left join stockbod as b23 on b23.idbodega=23 and i.id=b23.iditem
			left join stockbod as b24 on b24.idbodega=24 and i.id=b24.iditem
			left join stockbod as b25 on b25.idbodega=25 and i.id=b25.iditem		
			left join stockbod as b26 on b26.idbodega=26 and i.id=b26.iditem
			left join stockbod as b27 on b27.idbodega=27 and i.id=b27.iditem
			left join stockbod as b28 on b28.idbodega=28 and i.id=b28.iditem
			left join stockbod as b29 on b29.idbodega=29 and i.id=b29.iditem
			left join stockbod as b30 on b30.idbodega=30 and i.id=b30.iditem
			left join stockbod as b31 on b31.idbodega=31 and i.id=b31.iditem
			left join stockbod as b32 on b32.idbodega=32 and i.id=b32.iditem
			left join stockbod as b33 on b33.idbodega=33 and i.id=b33.iditem
			left join stockbod as b34 on b34.idbodega=34 and i.id=b34.iditem
			left join stockbod as b35 on b35.idbodega=35 and i.id=b35.iditem
			left join stockbod as b36 on b36.idbodega=36 and i.id=b36.iditem
			left join stockbod as b37 on b37.idbodega=37 and i.id=b37.iditem
			left join stockbod as b38 on b38.idbodega=38 and i.id=b38.iditem
			left join stockbod as b39 on b39.idbodega=39 and i.id=b39.iditem
			left join stockbod as b40 on b40.idbodega=40 and i.id=b40.iditem
			left join stockbod as b41 on b41.idbodega=41 and i.id=b41.iditem
			left join stockbod as b42 on b42.idbodega=42 and i.id=b42.iditem
			left join stockbod as b43 on b43.idbodega=43 and i.id=b43.iditem
			left join stockbod as b44 on b44.idbodega=44 and i.id=b44.iditem
			left join stockbod as b45 on b45.idbodega=45 and i.id=b45.iditem
			left join stockbod as b46 on b46.idbodega=46 and i.id=b46.iditem
			left join stockbod as b47 on b47.idbodega=47 and i.id=b47.iditem
			left join stockbod as b48 on b48.idbodega=48 and i.id=b48.iditem
			left join stockbod as b49 on b49.idbodega=49 and i.id=b49.iditem
			left join stockbod as b50 on b50.idbodega=50 and i.id=b50.iditem
			left join stockbod as b51 on b51.idbodega=51 and i.id=b51.iditem
			left join stockbod as b52 on b52.idbodega=52 and i.id=b52.iditem
			left join stockbod as b53 on b53.idbodega=53 and i.id=b53.iditem
			left join stockbod as b54 on b54.idbodega=54 and i.id=b54.iditem
			left join stockbod as b55 on b55.idbodega=55 and i.id=b55.iditem 
			left join stockbod as b56 on b56.idbodega=56 and i.id=b56.iditem
			left join stockbod as b57 on b57.idbodega=57 and i.id=b57.iditem
			left join stockbod as b58 on b58.idbodega=58 and i.id=b58.iditem
		
	ENDTEXT
	
	RETURN oApp.SQLExec (cCmd+cCondItem, aDef[nToken,2])


ENDFUNC






FUNCTION ultimaventa (nToken)
	
	TEXT TO cCmd NOSHOW	
	
		SELECT  CAST(c.Date as char) as fecha, 
				CAST(ROUND(d.quantity,2) as char) as cantidad, 
				CAST(ROUND(d.price,2) as char) as precio, 
				d.iditem,
				c.idcustomer,
				c.id
		FROM item AS i
			INNER JOIN invoicedet AS d on i.id = d.idItem 
			inner JOIN Invoice AS c on c.id = d.Id 
		WHERE !c.void 
		ORDER BY c.id, c.idcustomer, c.date
	ENDTEXT
	
	&&cCmd = STRTRAN(cCmd,"@idcustomer",cIdCust)

	RETURN oApp.SQLExec (cCmd, aDef[nToken,2])

ENDFUNC

	



FUNCTION ftpsend(cFileDes, cFileOri)
	

	WAIT WINDOW 'Enviando... '+cFileDes NOWAIT
	

	cmdZip =  '7z a -mx=9 '+cFileDes+' '+cFileOri
	RUN &cmdZip
	
	*oShell.Run(cmdZip)
	
	lRetVal = .T.

	IF oIp.FTPSendFile (oApp.FtpHost,;
	            cFileDes,;
	            cFileDes,;
	            oApp.FtpUser,;
	            oApp.FtpPswd)=0
	            
	ELSE
		lRetVal = .f.
		this.msg_add('Error ftp: '+oIp.cErrorMsg)
	ENDIF

	*IF !oJs.ltest
		DELETE FILE (cFileDes)
		*DELETE FILE (cFileOri)
	*ENDIF
	
	
	*MESSAGEBOX('ssssss')

	WAIT WINDOW 'Enviado '+cFileDes TIMEOUT .3

	RETURN lRetVal 

ENDFUNC




FUNCTION msg_add (cMsg)
	cMsg = 'Json Web... ' + CHR(13) + cMsg
	WAIT WINDOW cMsg nowait
	
ENDFUNC


FUNCTION jexec(cmd)
	RETURN this.sqlite(db,cmd)
ENDFUNC

FUNCTION pexec(cmd)
	RETURN this.sqlite(pd,cmd)
ENDFUNC


FUNCTION sqlite (odb,cmd)
	LOCAL o as "adodb.recordset"

	o = odb.execute(cmd)

	IF !empty(odb.LastError) or isnull(o)

	  = MessageBox ( "Ha ocurrido un error al ejecutar: " + ;
	  	chr(13)+ odb.LastError,63,'Ghost' )
	  	
	ENDIF	

	RETURN o

ENDFUNC


FUNCTION t_clear
	this.t_nfields = 0
	DIMENSION a_t_fields[1,2]
ENDFUNC

FUNCTION t_set
	LPARAMETERS cField, eField

	this.t_nfields = this.t_nfields + 1
	DIMENSION a_t_fields[this.t_nfields,2]

	a_t_fields[this.t_nfields,1] = cField
	a_t_fields[this.t_nfields,2] = this.fixstr(eField)
	
	
ENDFUNC


FUNCTION t_insert
LPARAMETERS odb, cNameTable

	LOCAL o as "adodb.recordset"
	LOCAL lastId as "adodb.recordset"
	LOCAL nLastId
	
	nLastId = 0

	IF this.t_nfields>0
		LOCAL ii
		
		cmdInsert = 'insert into '+cNameTable+' ('+a_t_Fields[1,1]
		cmdValues = ') VALUES ('+a_t_fields[1,2]
			
		FOR ii = 2 TO this.t_nfields	
			
			cmdInsert = cmdInsert + ', '+a_t_Fields[ii,1]
			cmdValues = cmdValues + ', '+a_t_fields[ii,2]
			
		ENDFOR
		
		cmdValues = cmdValues + ')'

		
		o = this.sqlite(odb, cmdInsert+cmdValues )
		
		lastId = this.sqlite(odb,'select id from '+cNameTable+' order by 1 desc limit 1')
		
		nLastId = lastId.item(1).item("id");

		this.t_nfields   = 0
		

	ENDIF
	
	RETURN nLastId
	
ENDFUNC

FUNCTION t_update
	PARAMETERS odb, cNameTable, cIdUpdate, cCond, cIdNameField
	
	LOCAL o as "adodb.recordset"
	
	IF VARTYPE(cIdUpdate)='N'
		cIdUpdate = ALLTRIM(STR(cIdUpdate,20,0))
	ENDIF
	

	NOTE      cNametable   =  Nombre de la tabla
	NOTE      cIdUpdate    =  valor con que se compara el Id " WHERE Id = cIdUpdate "
	NOTE      cCond        =  condicion reemplza al anterior " ' WHERE '+cCond  "
	NOTE      cIdNameField =  nombre del campo del Id, por defecto Id

	IF EMPTY(cIdNameField)
		cIdNameField = 'Id'
	ENDIF


	IF this.t_nfields>0
		LOCAL ii, cCad, cWhere0
		
		cWhere0 = ' WHERE '+cIdNameField+'='+cIdUpdate
		IF !EMPTY(cCond)
			cWhere0 = ' WHERE '+cCond
		ENDIF
		
		DIMENSION a_t_fields[this.t_nfields,2]
	
		cmdInsert0 = 'UPDATE '+cNameTable+' SET '+a_t_Fields[1,1]+'='+a_t_Fields[1,2]
		cmdInsert = cmdInsert0 
		
		FOR ii = 2 TO this.t_nfields
			cmdInsert = cmdInsert + ', '+a_t_Fields[ii,1]+'='+a_t_Fields[ii,2]
		ENDFOR

		o = this.sqlite(odb,cmdInsert + cWhere0	)
		
		this.t_nfields = 0
	ENDIF
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

	RETURN '"'+ALLTRIM(sVal)+'"'
ENDFUNC

FUNCTION setbdd_remote

DIMENSION aVv4[1]
LOCAL cRuc, cUser
	oApp.explode(oJs.cParam,@aVv4,'|')
		
	cRuc = aVv4[1]
	cUser = aVv4[2]
		
	this._cFileName = cRuc+'_'+cUser
	this._cBddName = this._cFileName+'.sqlite'

ENDFUNC



ENDDEFINE