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(oJs.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 + oJs.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='+oJs.cIdCmd+'&resp='+cResp)
