procedure TFDespachoAutomatico.server2Execute(AContext: TIdContext);
var
vDespDatoRec : string; vDespRespLogin : Integer; vDespJSONDato : TlkJSONobject;
vDespJSONRespuestaFinal : TlkJSONobject;
vDespJSONRespuestaLogin : TlkJSONobject;
vDespUsuario : string; vDespLoginVersion : string;
vDespAuxS : string; vDespTipoDato : Integer; vDespMovil : Integer; vDespIdViaje : Integer; vDespIdViajeEnUso : Integer; vDespDomicilio : string;
vDespObservaciones : string;
vDespViajeLat : string;
vDespViajeLng : string;
vDespRespS : string;
vDespMsgError : string;
function IdViajeEnusoInt(vIdViaje: Integer):Integer;
var
vQryVEU : TADOQuery;
begin
Result := 0;
vQryVEU := TADOQuery.Create(nil);
vQryVEU.Connection := conDB1;
try
with vQryVEU do
begin
Close;
SQL.Clear;
SQL.Add('Select idViajeEnUso from AppViajes_DatoViajes with (NOLOCK) where ');
SQL.Add('id = :vIdViaje ');
Parameters.ParamByName('vIdViaje').Value := vIdViaje;
Open;
end;
if vQryVEU.RecordCount > 0 then
Result := vQryVEU['idViajeEnUso']
else
Result := 0;
finally
FreeAndNil(vQryVEU);
end;
end;
procedure GrabarlogInt(vDato, vCarpeta : string);
var
F: TextFile;
Nombre :string;
begin
try
if not DirectoryExists(GetCurrentDir + '\Log') then
CreateDir(GetCurrentDir + '\Log');
if not DirectoryExists(GetCurrentDir + '\Log\' + vCarpeta) then
CreateDir(GetCurrentDir + '\Log\' + vCarpeta);
nombre:=GetCurrentDir + '\Log\' + vCarpeta + '\' + FormatDateTime('YYYYMMDD',Now) + '.log';
AssignFile(F, nombre);
if (FileExists(nombre)) then
Append(F)
else
ReWrite(F);
if vCarpeta = 'Despacho' then
Writeln(F, vDato )
else
Writeln(F, '['+DateTimeToStr(Now)+'] '+ vDato );
finally
CloseFile(F);
end;
end;
procedure LogInt(vDespLog: string; vDespTipo: Integer);
begin
case vDespTipo of
0: begin GrabarlogInt(vDespLog,'Errores'); mmoErrores.Lines.Add(DateTimeToStr(Now)+' - '+vDespLog);
if mmoErrores.Lines.Count > 20 then
begin
mmoErrores.Lines.Delete(0);
mmoErrores.Lines.Delete(0);
end;
SendMessage(mmoErrores.Handle, EM_SCROLL, SB_BOTTOM, 0);
end;
1: begin GrabarlogInt(vDespLog,'Crudo'); if chkLogCrudo.Checked then
begin
mmoLog.Lines.Add(DateTimeToStr(Now)+' - '+vDespLog);
if mmoLog.Lines.Count > 20 then
begin
mmoLog.Lines.Delete(0);
mmoLog.Lines.Delete(0);
end;
SendMessage(mmoLog.Handle, EM_SCROLL, SB_BOTTOM, 0);
end;
end;
2: begin GrabarlogInt(vDespLog,'Viajes'); if chkLogViaje.Checked then
begin
mmoLog.Lines.Add(DateTimeToStr(Now)+' - '+vDespLog);
if mmoLog.Lines.Count > 20 then
begin
mmoLog.Lines.Delete(0);
mmoLog.Lines.Delete(0);
end;
SendMessage(mmoLog.Handle, EM_SCROLL, SB_BOTTOM, 0);
end;
end;
end;
end;
function DatoViajesInt(vDespLeido : Boolean; vDespRespuesta, vDespId: Integer):Boolean;
var
vQryDato : TADOQuery;
begin
Result := True;
vQryDato := TADOQuery.Create(nil);
vQryDato.Connection := conDB;
try
with vQryDato do
begin
Close;
SQL.Clear;
SQL.Add('update AppViajes_DatoViajes set leido = :vleido, respuesta = :vrespuesta ');
if vDespRespuesta = 1 then
begin
SQL.Add(',TipoDato = 2 ');
end;
SQL.Add('where id = :vid ');
Parameters.ParamByName('vleido').Value := vDespLeido;
Parameters.ParamByName('vrespuesta').Value := vDespRespuesta;
Parameters.ParamByName('vid').Value := vDespId;
ExecSQL;
end;
finally
FreeAndNil(vQryDato);
end;
end;
procedure GuardarPosicionInt(vDespMovil, vDespEstado, vDespConConsulta: Integer; vDespLat, vDespLng: string; vDespFecha: TDateTime );
var
vCant : Integer;
vQryGP : TADOQuery;
begin
vQryGP := TADOQuery.Create(nil);
vQryGP.Connection := conDB;
try
with vQryGP do
begin
Close;
SQL.Clear;
SQL.Add('Select count(*) as total from AppViajes_UltimaPosicion with (NOLOCK) where movil = :movil');
Parameters.ParamByName('movil').Value := vDespMovil;
Open;
end;
vCant := vQryGP['total'];
if vCant > 0 then
begin
with vQryGP do
begin
Close;
SQL.Clear;
SQL.Add('update AppViajes_UltimaPosicion set lat = :lat, lng = :lng, fecha = :fecha, estado = :estado, conConsulta = :conConsulta');
SQL.Add('where movil = :movil');
Parameters.ParamByName('lat').Value := vDespLat;
Parameters.ParamByName('lng').Value := vDespLng;
Parameters.ParamByName('fecha').Value := vDespFecha;
Parameters.ParamByName('estado').Value := vDespEstado;
Parameters.ParamByName('conConsulta').Value := vDespConConsulta;
Parameters.ParamByName('movil').Value := vDespMovil;
ExecSQL;
end;
end
else
begin
with vQryGP do
begin
Close;
SQL.Clear;
SQL.Add('Insert into AppViajes_UltimaPosicion(movil, lat, lng, fecha, estado, conConsulta)');
SQL.Add('Values(:movil, :lat, :lng, :fecha, :estado, :conConsulta)');
Parameters.ParamByName('movil').Value := vDespMovil;
Parameters.ParamByName('lat').Value := vDespLat;
Parameters.ParamByName('lng').Value := vDespLng;
Parameters.ParamByName('fecha').Value := vDespFecha;
Parameters.ParamByName('estado').Value := vDespEstado;
Parameters.ParamByName('conConsulta').Value := vDespConConsulta;
ExecSQL;
end;
end;
finally
FreeAndNil(vQryGP);
end;
end;
function PosicionInt(vDespCadena: string):Boolean;
var
vJSONPosicion : TlkJSONobject;
vMovil : Integer;
vLat, vLng : string;
vEstado : Integer;
begin
vJSONPosicion := TlkJSON.ParseText(vDespCadena) as TlkJSONobject;
Result := False;
try
vMovil := StrToInt(vartostr(vJSONPosicion.Field['posicion'].Field['movil'].Value));
vLat := vartostr(vJSONPosicion.Field['posicion'].Field['lat'].Value);
vLat := ReplaceStr(Trim(vLat), '.', ',');
vLng := vartostr(vJSONPosicion.Field['posicion'].Field['lng'].Value);
vLng := ReplaceStr(Trim(vLng), '.', ',');
vEstado := StrToInt(vartostr(vJSONPosicion.Field['posicion'].Field['estado'].Value));
if vEstado = 0 then
vEstado := 3;
if vEstado = 1 then
vEstado := 2;
GuardarPosicionInt(vMovil, vEstado, 0, vLat, vLng, now );
Result := True;
finally
FreeAndnil(vJSONPosicion);
end;
end;
function PendienteLatInt(vDespIdViajeEnuso: Integer):string;
var
vQryLat : TADOQuery;
begin
Result := '';
vQryLat := TADOQuery.Create(nil);
vQryLat.Connection := conDB;
try
with vQryLat do
begin
Close;
SQL.Clear;
SQL.Add('Select p.Lat from sat_viajespendientes p, AppViajes_ViajesEnUso u with (NOLOCK) where ');
SQL.Add('p.id = u.idViajePendiente ');
SQL.Add('and u.id = :vidviajeenuso ');
Parameters.ParamByName('vidviajeenuso').Value := vDespIdViajeEnuso;
Open;
end;
if vQryLat.RecordCount > 0 then
Result := vQryLat['Lat']
else
Result := '';
finally
FreeAndNil(vQryLat);
end;
end;
function PendienteLngInt(vIdViajeEnuso: Integer):string;
var
vQryLng : TADOQuery;
begin
Result := '';
vQryLng := TADOQuery.Create(nil);
vQryLng.Connection := conDB;
try
with vQryLng do
begin
Close;
SQL.Clear;
SQL.Add('Select p.Lng from sat_viajespendientes p, AppViajes_ViajesEnUso u with (NOLOCK) where ');
SQL.Add('p.id = u.idViajePendiente ');
SQL.Add('and u.id = :vidviajeenuso ');
Parameters.ParamByName('vidviajeenuso').Value := vIdViajeEnuso;
Open;
end;
if vQryLng.RecordCount > 0 then
Result := vQryLng['Lng']
else
Result := '';
finally
FreeAndNil(vQryLng);
end;
end;
function ArmarDomicilioInt(vDomIdViajeEnuso: Integer):string;
var
vQryCalle : TADOQuery;
vDomicilioArmado : string;
vPiso, vDpto : string;
begin
vDomicilioArmado := '';
Result := '';
try
vQryCalle := TADOQuery.Create(nil);
vQryCalle.Connection := conDB;
with vQryCalle do
begin
Close;
SQL.Clear;
SQL.Add('Select p.calle, p.altura, p.piso, p.dpto from sat_viajespendientes p, AppViajes_ViajesEnUso u with (NOLOCK) where ');
SQL.Add('p.id = u.idViajePendiente ');
SQL.Add('and u.id = :vidviajeenuso ');
Parameters.ParamByName('vidviajeenuso').Value := vDomIdViajeEnuso;
Open;
end;
vDomicilioArmado := Trim(vQryCalle['calle']);
vDomicilioArmado := vDomicilioArmado + ' ' + IntToStr(vQryCalle['altura']);
try
vPiso := Trim(vQryCalle['piso']);
vDpto := Trim(vQryCalle['dpto']);
except
vPiso := '';
vDpto := '';
end;
if vPiso.Length > 0 then
vDomicilioArmado := vDomicilioArmado + ' Piso '+Trim(vQryCalle['piso']);
if vDpto.Length > 0 then
vDomicilioArmado := vDomicilioArmado + ' Dpto '+Trim(vQryCalle['dpto']);
Result := vDomicilioArmado;
finally
FreeAndNil(vQryCalle);
end;
end;
function PendienteObservacionesInt(vObsIdViajeEnuso: Integer):string;
var
vQryObservaciones : TADOQuery;
begin
Result := '';
vQryObservaciones := TADOQuery.Create(nil);
vQryObservaciones.Connection := conDB;
try
with vQryObservaciones do
begin
Close;
SQL.Clear;
SQL.Add('Select p.observaciones from sat_viajespendientes p, AppViajes_ViajesEnUso u with (NOLOCK) where ');
SQL.Add('p.id = u.idViajePendiente ');
SQL.Add('and u.id = :vidviajeenuso ');
Parameters.ParamByName('vidviajeenuso').Value := vObsIdViajeEnuso;
Open;
end;
if vQryObservaciones.RecordCount > 0 then
Result := vQryObservaciones['observaciones']
else
Result := '';
finally
FreeAndNil(vQryObservaciones);
end;
end;
function PendienteBarrioInt(vBarrioIdViajeEnuso: Integer):string;
var
vQryBarrio : TADOQuery;
begin
Result := '';
vQryBarrio := TADOQuery.Create(nil);
vQryBarrio.Connection := conDB;
try
with vQryBarrio do
begin
Close;
SQL.Clear;
SQL.Add('Select p.barrio from sat_viajespendientes p, AppViajes_ViajesEnUso u with (NOLOCK) where ');
SQL.Add('p.id = u.idViajePendiente ');
SQL.Add('and u.id = :vidviajeenuso ');
Parameters.ParamByName('vidviajeenuso').Value := vBarrioIdViajeEnuso;
Open;
end;
while not vQryBarrio.Eof do
begin
Result := Trim(vQryBarrio['barrio']);
vQryBarrio.Next;
end;
finally
FreeAndNil(vQryBarrio);
end;
end;
function PendienteInfoTmpInt(vInfoIdViajeEnuso: Integer):string;
var
vQryInfoTmp : TADOQuery;
begin
Result := '-';
vQryInfoTmp := TADOQuery.Create(nil);
vQryInfoTmp.Connection := conDB;
try
with vQryInfoTmp do
begin
Close;
SQL.Clear;
SQL.Add('Select p.infotmp from sat_viajespendientes p, AppViajes_ViajesEnUso u with (NOLOCK) where ');
SQL.Add('p.id = u.idViajePendiente ');
SQL.Add('and u.id = :vidviajeenuso ');
Parameters.ParamByName('vidviajeenuso').Value := vInfoIdViajeEnuso;
Open;
end;
try
Result := Trim(vQryInfoTmp['infotmp'])
except
Result := '-';
end;
finally
FreeAndNil(vQryInfoTmp);
end;
end;
function ControlInt(vDespControlMovil: Integer):string;
var
vDespQryControl : TADOQuery;
vDespJSONControl : TlkJSONobject;
vDespText, vObs : string;
vDespIdViaje : Integer;
vDespBarrio : string;
begin
Result := '';
vDespQryControl := TADOQuery.Create(nil);
vDespQryControl.Connection := conDB1;
vDespJSONControl := TlkJSONobject.Create;
try
with vDespQryControl do
begin
Close;
SQL.Clear;
SQL.Add('Select id, movil, texto, idViajeEnUso ');
SQL.Add('from AppViajes_DatoViajes with (NOLOCK) where ');
SQL.Add('movil = :vctrmovil and ');
SQL.Add('tipodato = 1 and leido = 0 '); Parameters.ParamByName('vctrmovil').Value := vDespControlMovil;
Open;
end;
while not vDespQryControl.Eof do
begin
vDespText := vDespQryControl['texto'];
vDespIdViaje := vDespQryControl['id'];
vDespBarrio := PendienteBarrioInt(vDespQryControl['idViajeEnUso']);
if vDespBarrio.Length > 0 then
vObs := 'Barrio: '+vDespBarrio
else
vObs := '';
vObs := vObs +' '+ PendienteInfoTmpInt(vDespQryControl['idViajeEnUso']);
vDespJSONControl.Add('domicilio', vDespText); vDespJSONControl.Add('obs', vObs); vDespJSONControl.Add('idviaje', vDespIdViaje);
if DatoViajesInt(True, 0,vDespQryControl['Id']) then begin
Result := TlkJSON.GenerateText(vDespJSONControl);
end;
vDespQryControl.Next;
end;
finally
FreeAndNil(vDespQryControl);
FreeAndnil(vDespJSONControl);
end;
end;
function ControlQTAInt(vDespQTAMovil: Integer):string;
var
vDespQryViajesQTA : TADOQuery;
vDespJSONControlQTA : TlkJSONobject;
vDespIdViaje: Integer;
begin
vDespQryViajesQTA := TADOQuery.Create(nil);
vDespQryViajesQTA.Connection := conDB2;
vDespJSONControlQTA := TlkJSONobject.Create;
try
with vDespQryViajesQTA do
begin
Close;
SQL.Clear;
SQL.Add('Select id, movil, idViajeEnUso ');
SQL.Add('from AppViajes_DatoViajes with (NOLOCK) where ');
SQL.Add('tipodato = 3 and leido = 0 and '); SQL.Add('movil = :vvqtamovil ');
Parameters.ParamByName('vvqtamovil').Value := vDespQTAMovil;
Open;
end;
while not vDespQryViajesQTA.Eof do
begin
vDespIdViaje := vDespQryViajesQTA['id'];
vDespJSONControlQTA.Add('estado', 'qta'); vDespJSONControlQTA.Add('movil', IntToStr(vDespQTAMovil)); vDespJSONControlQTA.Add('idviajeqta', vDespIdViaje);
if DatoViajesInt(True, 0,vDespQryViajesQTA['Id']) then begin
Result := TlkJSON.GenerateText(vDespJSONControlQTA);
end;
vDespQryViajesQTA.Next
end;
finally
FreeAndNil(vDespQryViajesQTA);
FreeAndnil(vDespJSONControlQTA);
end;
end;
function LoginInt(vDespCadena: string):Integer;
var
vDespJSONLogin : TlkJSONobject;
vDespUser, vDespPass: string;
vDespQryLogin : TADOQuery;
begin
Result := 3; vDespQryLogin := TADOQuery.Create(nil);
vDespQryLogin.Connection := conDB;
vDespUser := '-';
vDespPass := '-';
vDespJSONLogin := TlkJSON.ParseText(vDespCadena) as TlkJSONobject;
try
try
vDespUser := vartostr(vDespJSONLogin.Field['login'].Field['user'].Value);
vDespPass := vartostr(vDespJSONLogin.Field['login'].Field['pass'].Value);
except
Result := 3; end;
with vDespQryLogin do
begin
Close;
SQL.Clear;
SQL.Add('Select count(*) as total from AppViajes_Choferes with (NOLOCK) where usuario = :vusu and password = :vpass ');
Parameters.ParamByName('vusu').Value := vDespUser;
Parameters.ParamByName('vpass').Value := vDespPass;
Open;
end;
if vDespQryLogin['total'] > 0 then
begin
Result := 0; end
else
begin
Result := 1; end;
finally
FreeAndNil(vDespQryLogin);
FreeAndnil(vDespJSONLogin);
end;
end;
function ValidarMovilQRXInt(vMovilQRX: Integer):string;
var
vFecha : TDateTime;
qryQRX : TADOQuery;
vJSONQRX : TlkJSONobject;
s : string;
vResultado : Boolean;
begin
qryQRX := TADOQuery.Create(nil);
qryQRX.Connection := conDB;
vJSONQRX := TlkJSONobject.Create;
try
Result := '{}';
vResultado := False;
with qryQRX do
begin
Close;
SQL.Clear;
SQL.Add('Select GETDATE() as fecha ');
Open;
end;
vFecha := qryQRX['fecha']; with qryQRX do
begin
Close;
SQL.Clear;
SQL.Add('Select nromovil,indefinido,fechafin from sat_movilesqrx with (NOLOCK) where nromovil = :vmov ');
SQL.Add('and fechainicio <= GETDATE() ');
Parameters.ParamByName('vmov').Value := vMovilQRX;
Open;
end;
while not qryQRX.Eof do
begin
if qryQRX['nromovil'] = vMovilQRX then
begin
if qryQRX['indefinido']= 's' then
begin
vResultado := True; end
else
begin
if vFecha < qryQRX['fechafin'] then
begin
vResultado := True; end;
end;
end;
qryQRX.Next;
end;
if vResultado then
vJSONQRX.Add('estado', 'QRX')
else
vJSONQRX.Add('estado', 'QAP');
s := TlkJSON.GenerateText(vJSONQRX);
Result := s;
finally
FreeAndNil(qryQRX);
FreeAndnil(vJSONQRX);
end;
end;
function UsuarioMovilInt(vUsuario: string): Integer;
var
vQryMovil : TADOQuery;
begin
vQryMovil := TADOQuery.Create(nil);
vQryMovil.Connection := conDB;
try
with vQryMovil do
begin
Close;
SQL.Clear;
SQL.Add('Select movil from AppViajes_Choferes with (NOLOCK) where usuario = :vusu');
Parameters.ParamByName('vusu').Value := Trim(vUsuario);
Open;
end;
try
Result := vQryMovil['movil'];
except
Result := 0;
end;
finally
FreeAndNil(vQryMovil);
end;
end;
function ViajesInt(vViajeMovil: Integer):String;
var
vJSONViajes : TlkJSONobject;
vQryViajes : TADOQuery;
vCount : integer;
s : string;
vTotal : integer;
vTexto : string;
begin
Result := '';
vQryViajes := TADOQuery.Create(nil);
vQryViajes.Connection := conDB;
vJSONViajes := TlkJSONobject.Create;
try
with vQryViajes do
begin
Close;
SQL.Clear;
SQL.Add('Select top 5 calle, altura, fechaenviado, estado from sat_viajesenviadostmp with (NOLOCK) where movil = :vmovil ');
SQL.Add('order by fechaenviado desc ');
Parameters.ParamByName('vmovil').Value := vViajeMovil;
Open;
end;
try
vTotal := vQryViajes.RecordCount;
except
vTotal := 0;
end;
vCount := 1;
while not vQryViajes.Eof do
begin
vTexto := '['+DateTimeToStr(vQryViajes['fechaenviado'])+'] '+Trim(vQryViajes['calle'])+' '+IntToStr(vQryViajes['altura']);
if vQryViajes['estado'] = 'QTA' then
vTexto := vTexto +' ['+vQryViajes['estado']+']';
vJSONViajes.Add('viaje'+IntToStr(vCount), vTexto);
vCount := vCount + 1;
vQryViajes.Next;
end;
vJSONViajes.Add('total', IntToStr(vTotal));
s := TlkJSON.GenerateText(vJSONViajes);
Result := s;
finally
FreeAndNil(vQryViajes);
FreeAndnil(vJSONViajes);
end;
end;