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

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)
		
	IF cWebResult!='@@'
	
		o = JSON.Parse(cWebResult)

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

ENDFUNC


FUNCTION ListPax
	LOCAL cResp, cCmd, cIdUser,cIdS
	DECLARE aParam [1]
	
	oApp.explode(this.cParam,@aParam,'|')
	
	
	
	cFecha = aParam[1]
	cCode = aParam[2]
	
	cIdS = 'xx'
	IF oApp.sqlexec('SELECT id FROM salesman WHERE code="'+cCode+'"')>0
		cIdS=sqlresult.id
	ENDIF
	

	cResp = ''	
	IF oApp.sqlexec('SELECT a.noteprint as nota FROM invoice_noteprint as a inner join invoice as i on a.id=i.id WHERE i.due= "'+cFecha+'" and i.idsalesman = "'+cIdS+'";','curListPax')>0
		SELECT curListPax
		GO TOP 
		SCAN all
			IF !EMPTY(cResp)
				cResp = cResp + '~'
			ENDIF
			cResp = cResp +  ALLTRIM(curListPax.nota)
		ENDSCAN
		
	ENDIF

	cCmd = 'json/set_respuesta.php?id='+this.cIdCmd+'&resp='+cResp
	
	oApp.runLink(cCmd)

ENDFUNC






FUNCTION SolitarItem
	LOCAL cResp, cCmd, cIdUser,cItemGrupo,cPatron, cFecha,cUnidad
	DECLARE aParam [1]
	
	oApp.explode(this.cParam,@aParam,'|')
	
	cIdUser = aParam[1]
	cPatron = aParam[2]
	cFecha  = aParam[3]

	cResp = '{'

	cItemGrupo = ""	
	IF oApp.sqlexec('SELECT id as iditem, name, idbrand, price, priceb, pricec '+;
		'FROM item WHERE name LIKE "%'+cPatron+'%" AND idgrupo IN (SELECT idgrupo FROM usergrupo WHERE iduser="'+cIduser+'") LIMIT 15','curItems')>0
		cItemGrupo = 'itemsgrupo:['
		SELECT curItems
		SCAN all
			IF LEN(cItemGrupo)>12
				cItemGrupo = cItemGrupo +  ','
			ENDIF
			
			cUnidad = '000'
			IF oApp.sqlexec ('select s.code from itinerario as i inner join salesman as s on i.idsalesman=s.id '+;
							 'where i.idbrand="'+curItems.idbrand+'" and i.fecha="'+cFecha+'"','curUnidad')>0
				cUnidad = curUnidad.code
			ENDIF
			
			
			cItemGrupo = cItemGrupo +  '['
			cItemGrupo = cItemGrupo + this.cll(curItems.iditem)
			cItemGrupo = cItemGrupo + this.cll(curItems.name)
			cItemGrupo = cItemGrupo + this.cll(curItems.idbrand)
			cItemGrupo = cItemGrupo + this.cll(cUnidad)
			cItemGrupo = cItemGrupo + this.cll(curItems.price)
			cItemGrupo = cItemGrupo + this.cll(curItems.priceb)
			cItemGrupo = cItemGrupo + this.cll(curItems.pricec,.t.)
			cItemGrupo = cItemGrupo +  ']'
		ENDSCAN
		cItemGrupo = cItemGrupo + ']'
	ENDIF
	
	cResp = cResp  +cItemGrupo + '}'
	
	
	cCmd = 'json/set_respuesta.php?id='+this.cIdCmd+'&resp='+cResp
	

	oApp.runLink(cCmd)

ENDFUNC


FUNCTION SolitarEsquema
	LOCAL cResp, cIdS, cCode, cFecha, cIdBrand, cIdIt, cIdItem, cCmdSql,e
	*cResp = "{ncol:4,turnos:[1,2,4,3,5,6,8,7,9,10,12,11,13,14,16,15,17,18,20,19,21,22,24,23,25,26,28,27,29,30,32,31,33,34,36,35,37,38,40,39,41,42]}"
	
	DECLARE aParam [1]
	
	oApp.explode(this.cParam,@aParam,'|')
	
	cCode    = aParam[1]
	cIdBrand = aParam[2]
	cFecha   = aParam[3]
	cIdItem  = aParam[4]
	
	cResp = ''
	cIdS=''
	
	IF oApp.sqlexec('SELECT nota, id FROM salesman WHERE code="'+cCode+'"')>0
		cResp = ALLTRIM(sqlresult.nota)
		cIdS=sqlresult.id
		
		
		cCmdSql='select id from itinerario where idbrand="'+cIdBrand+'" and fecha="'+cFecha+'"'
		
		IF oApp.sqlexec (cCmdSql,'curItinerario')>0

			cIdIt = ALLTRIM(STR(curItinerario.id))
			oApp.sqlexec ('update itinerario set idsalesman="'+cIdS+'" where id="'+cIdIt+'"')
		ELSE

			oApp.sqlexec ('insert into itinerario (idbrand,fecha,idsalesman) values ("'+cIdBrand+'","'+cfecha+'","'+cIdS+'")')
		ENDIF

		*oApp.sqlexec('UPDATE invoicedet set idsalesman="'+cIdS+'" where iditem="'+cIdItem+'" and fechaanuncio="'+cFecha+'" ')
		
		LOCAL _o, iCount, cAs, cCmd, cmdO, cOcupados, cOcup	
		
		 
		TEXT TO cCmdO noshow
			SELECT u.name, i.date, i.hora, u.barras
				FROM invoicedet as d 
				INNER JOIN invoice as i on d.id=i.id
				inner join user as u on u.id=i.iduser
		    	WHERE d.fechaanuncio="@fecha" 
		    	      and d.infoadicional1 like "%@asiento%"
		    	      and d.idsalesman = "@idsalesman"
		    LIMIT 1
		ENDTEXT 
		
		_o = JSON.Parse(cResp)
		
		cOcupados = 'ocupados:['
		
		FOR iCount = 1 TO _o.turnos.Count
		
			cAs = padl(INT(_o.turnos[iCount]),2,'0')	
			cCmd = STRTRAN(cCmdO,"@fecha",cFecha)
			cCmd = STRTRAN(cCmd, "@asiento",cAs)
			cCmd = STRTRAN(cCmd, "@idsalesman",cIdS)
			
			cOcup='|||'
			IF oApp.sqlexec(cCmd,'curO')>0
				cOcup = ALLTRIM(left(curO.name,15)) +'|'+ DTOC(curO.date) +'|'+ SUBSTR(curO.hora,1,5) +'|'+ ALLTRIM(curO.barras)
			ENDIF
			cOcupados = cOcupados + this.cll(cOcup,(iCount=_o.turnos.Count))
		ENDFOR

		cResp = SUBSTR(cResp,1,LEN(cResp)-1) + ',' + cOcupados + ']}'
		
	ENDIF
	
	*STRTOFILE(cResp,'eddy.prg')
	
	oApp.runLink('json/set_respuesta.php?id='+this.cIdCmd+'&resp='+cResp)
ENDFUNC


FUNCTION EmitirBoleto
	LOCAL cResp, cIdS, cCmdSql
	LOCAL nPed, cIdUser, cFecha, cIdItem, cCode, cRuc, cNombre, cPrecio, cCant, cBoletos, cTipoPvp
	
	DECLARE aParam [1]
	
	oApp.explode(this.cParam,@aParam,'|')
	
	cIdUser  = aParam[1]
	cFecha   = aParam[2]
	cIdItem  = aParam[3]
	cCode    = aParam[4]
	cRuc     = aParam[5]
	cNombre  = aParam[6]
	cPrecio  = aParam[7]
	cCant    = aParam[8] 
	cBoletos = aParam[9]
	cTipoPvp = aParam[10]
	
	oApp.sqlexec('select iduser,count(iduser)+0000000000 as n from invoice where iduser='+cIdUser,'curUltimo')
	nPed = 1
	IF !ISNULL(curUltimo.n)
		nPed = INT(VAL(curUltimo.n))+1 
	ENDIF
	
	nPed = nPed +  INT(VAL(cIdUser))*1000000
	
	
	cResp = this.facturar(nPed, cIdUser, cFecha, cIdItem, cCode, cRuc, cNombre, cPrecio, cCant, cBoletos, cTipoPvp )
	
	
	oApp.runLink('json/set_respuesta.php?id='+this.cIdCmd+'&resp='+cResp)
ENDFUNC



FUNCTION facturar(nPed, cIdUser, cFecha, cIdItem, cCode, cRuc, cNombre, cPrecio, cCant, cBoletos, cTipoPvp )

	LOCAL cNameUser
	
	oLoader.idcc		= PADL(2,10,'0')
	oLoader.idcp		= PADL(2,10,'0')
	oLoader.idbodega	= PADL(2,8,'0')	

	oLoader.noPed 		= nPed

	_noPed 				= nPed

	oLoader.nofact		= oLoader.noPed
	
	oInvoicer.INFOADICIONAL1 = cBoletos
	oInvoicer.fechaanuncio = CTOD(SUBSTR(cFecha,9,2)+'/'+SUBSTR(cFecha,6,2)+'/'+SUBSTR(cFecha,1,4))
	*oInvoicer.fe_manual = .T.
	
	
	cIdS = 'xx'
	IF oApp.sqlexec('SELECT id FROM salesman WHERE code="'+cCode+'"')>0
		oLoader.idsalesman = sqlresult.id
	ENDIF
	

	IF oApp.sqlexec("select noped from loader_h where noped=?_noPed")=0

		
		oLoader.tiempo		= DTOC(DATE(),1)
		oLoader.iduser 		= cIdUser
		oLoader.ruc 		= cRuc
		oLoader.cliente  	= cNombre
		oLoader.address 	= ""
		oLoader.phone 		= ""
		oLoader.city 		= ""
		
		oLoader.descuento 	= 0
		oLoader.anulado 	= '0'

		oLoader.codvend 	= cCode
		
		oLoader.serie       = '001008'
		
		cNameUser	    	= cIdUser
		IF oApp.sqlexec('select seriepv,name from user where id='+cIdUser)>0
			IF !EMPTY(ALLTRIM(sqlresult.seriepv) )
				oLoader.serie   = ALLTRIM(sqlresult.seriepv) 
			ENDIF
			
			cNameUser	    = ALLTRIM(sqlresult.name) 
		ENDIF
		
		cNameItem	    	= cIdItem
		IF oApp.sqlexec('select name from item where id='+cIdItem)>0
			cNameItem	    = ALLTRIM(sqlresult.name) 
		ENDIF
		
		oLoader.numero      = _noPed
		oLoader.email 		= ''

		oLoader.nota		= cNameUser +'|'+ cFecha +'|'+ cNameItem +'|'+ cCode +'|'+ cRuc +'|'+ cNombre +'|'+ cPrecio +'|'+ cCant +'|'+ cBoletos 
		oLoader.detalle		= ''
  		oLoader.formapago 	= '12'
		oLoader.lcontado	= 1

		oLoader.tipo 		= '01'
		oLoader.idbank		= ''
		
		oLoader.cantidad 	= VAL(cCant)
		oLoader.precio 		= VAL(cPrecio)
		
		oLoader.pvp			= 	cTipoPvp
		oLoader.pvpCab		= 	cTipoPvp


		oLoader.anulado 	= 0
		oLoader.fecha 		= DATE()
		oLoader.venc 		= oInvoicer.fechaanuncio 
		oLoader.hora 		= TIME()
		
		oLoader.iditem		= cIdItem
		oLoader.item 		= cNombre
		oLoader.detalle		= cBoletos 
		
		oLoader.grabar()

		oInvoicer.run(oLoader.noPed, .T.)
	
		RETURN oInvoicer.cSerieSec
	ENDIF
	RETURN 'xxx-xxx-xxxxxxxxx'
ENDFUNC



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

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


FUNCTION enc3 
	PARAMETERS s
	LOCAL sRet, i, l, c
	sRet=''
	l = LEN (ALLTRIM(S))
	FOR i = 1 TO l
	    c = ASC(SUBSTR (s, i, 1))
	    c = IIF(i%2==0, c-7, c+5)
	    sRet = sRet + chr(c)
	NEXT
	RETURN sRet
ENDFUNC



ENDDEFINE