Ver Mensaje Individual
  #8  
Antiguo 07-02-2017
Avatar de noshy
noshy noshy is offline
Miembro
 
Registrado: jul 2003
Posts: 139
Reputación: 21
noshy Va por buen camino
Código Delphi [-]
procedure TFDespachoAutomatico.server2Execute(AContext: TIdContext);
var
  vDespDatoRec       : string;  //Guarda la Cadena Recibida
  vDespRespLogin     : Integer; //Resuesta del Login
  vDespJSONDato      : TlkJSONobject;
  vDespJSONRespuestaFinal : TlkJSONobject;
  vDespJSONRespuestaLogin : TlkJSONobject;
  vDespUsuario       : string;  //Usuario logueado
  vDespLoginVersion  : string;
  vDespAuxS          : string;  //Variable Auxiliar String
  vDespTipoDato      : Integer; //Tipo de dato solicitado
  vDespMovil         : Integer; //Movil que envia la posicion
  vDespIdViaje       : Integer;   //Id Viaje Datos
  vDespIdViajeEnUso  : Integer; //Id Viaje En Uso
  vDespDomicilio     : string;
  vDespObservaciones : string;
  vDespViajeLat      : string;
  vDespViajeLng      : string;
  vDespRespS         : string;
  vDespMsgError      : string;



        //Funcion IdViajeEnusoInt ----------------------------------------------
        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;
        //Fin Funcion IdViajeEnusoInt ------------------------------------------

        //Funcion GrabarLogint -------------------------------------------------
        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;
        //Fin Funcion GrabarLogint ---------------------------------------------

        //Funcion LogInt -------------------------------------------------------
        procedure LogInt(vDespLog: string; vDespTipo: Integer);
        begin
          case vDespTipo of
           0: begin //Log Errores
                GrabarlogInt(vDespLog,'Errores');  //Log Errores
                //Mostramos el log
                mmoErrores.Lines.Add(DateTimeToStr(Now)+' - '+vDespLog);
                //Booramos el excedente de 100 lineas
                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 //Log Crudo
                GrabarlogInt(vDespLog,'Crudo');  //Log Crudo
                if chkLogCrudo.Checked then
                begin
                  //Mostramos el log
                  mmoLog.Lines.Add(DateTimeToStr(Now)+' - '+vDespLog);
                  //Booramos el excedente de 100 lineas
                  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 //Log Viajes
                GrabarlogInt(vDespLog,'Viajes');  //Log Viajes
                if chkLogViaje.Checked then
                begin
                  //Mostramos el log
                  mmoLog.Lines.Add(DateTimeToStr(Now)+' - '+vDespLog);
                  //Booramos el excedente de 100 lineas
                  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;
        //Fin Funcion LogInt ---------------------------------------------------

        //Funcion DatoViajesInt ------------------------------------------------
        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;
        //Fin Funcion DatoViajesInt ----------------------------------

        //Procedimiento GuardarPosicionInt -------------------------------
        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;
        //FIN Procedimiento GuardarPosicion --------------------------------------

        //Funcion PosicionInt --------------------------------------------------
        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;
        //Fin Funcion PosicionInt ----------------------------------------------

        //Funcion PendienteLatInt ----------------------------------------------
        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;
        //Fin Funcion PendienteLatInt ------------------------------------------

        //funcion PendienteLngInt ----------------------------------------------
        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;
        //Fin funcion PendienteLngInt ------------------------------------------

        //Funcion ArmarDomicilioInt --------------------------------------------
        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;
        //Fin Funcion ArmarDomicilioInt ----------------------------------------

        //Funcion PendienteObservacionesInt ------------------------------------
        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;


        //Funcion PendienteBarrioInt -------------------------------------------
        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;
        //Fin Funcion PendienteBarrioInt ---------------------------------------

        //Funcion PendienteInfoTmpInt ------------------------------------------
        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;
        //Fin Funcion PendienteInfoTmpInt --------------------------------------

        //Funcion ControlInt ---------------------------------------------------
        function ControlInt(vDespControlMovil: Integer):string;
        var
          vDespQryControl  : TADOQuery;
          //vDespCadenaCtr   : string;
          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 ');  //que tipodato = 1 (proposicion) y no este leido
              Parameters.ParamByName('vctrmovil').Value := vDespControlMovil;
              Open;
            end;

            //SI tiene viaje asignado
            while not vDespQryControl.Eof do
            begin
              //vText    := LimpiarTexto(vQryViajes['texto']);
              vDespText    := vDespQryControl['texto'];
              vDespIdViaje := vDespQryControl['id'];

              vDespBarrio  := PendienteBarrioInt(vDespQryControl['idViajeEnUso']);
              if vDespBarrio.Length > 0 then
                vObs := 'Barrio: '+vDespBarrio
              else
                vObs := '';

              //vObs     :=  vObs +' '+  LimpiarTexto(PendienteInfoTmp(vQryViajes['idViajeEnUso']));
              vObs     :=  vObs +' '+  PendienteInfoTmpInt(vDespQryControl['idViajeEnUso']);

              vDespJSONControl.Add('domicilio', vDespText);  //Domicilio
              vDespJSONControl.Add('obs', vObs);             //Observaqciones
              vDespJSONControl.Add('idviaje', vDespIdViaje); //Id VIaje

              //Se marca el viaje como leido y se devuelve la cadena co el viaje
              if DatoViajesInt(True, 0,vDespQryControl['Id']) then //cambiamos el viaje a leido
              begin
                Result := TlkJSON.GenerateText(vDespJSONControl);
              end;

              vDespQryControl.Next;
            end;
          finally
            FreeAndNil(vDespQryControl);
            FreeAndnil(vDespJSONControl);
          end;
        end;
        //Fin Funcion ControlInt -----------------------------------------------

        //Funcion ControlQTAInt ------------------------------------------------
        function ControlQTAInt(vDespQTAMovil: Integer):string;
        var
          vDespQryViajesQTA    : TADOQuery;
          vDespJSONControlQTA : TlkJSONobject;
          //vDespCadena: string;
          vDespIdViaje: Integer;
        begin
          vDespQryViajesQTA := TADOQuery.Create(nil);
          vDespQryViajesQTA.Connection := conDB2;
          vDespJSONControlQTA  := TlkJSONobject.Create;
          try
            //CONTROL DE VIAJES QTA ------------------------------------------------------
            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 ');  //que tipodato = 3 (CANCELADO) y no este leido
              SQL.Add('movil = :vvqtamovil ');
              Parameters.ParamByName('vvqtamovil').Value := vDespQTAMovil;
              Open;
            end;
            //SI tiene viaje asignado
            while not vDespQryViajesQTA.Eof do
            begin
              vDespIdViaje := vDespQryViajesQTA['id'];

              vDespJSONControlQTA.Add('estado', 'qta');  //estado
              vDespJSONControlQTA.Add('movil', IntToStr(vDespQTAMovil));  //movil
              vDespJSONControlQTA.Add('idviajeqta', vDespIdViaje); //Id VIaje

              if DatoViajesInt(True, 0,vDespQryViajesQTA['Id']) then //cambiamos el viaje a leido
              begin
                Result := TlkJSON.GenerateText(vDespJSONControlQTA);
              end;

              vDespQryViajesQTA.Next
            end;
          finally
            FreeAndNil(vDespQryViajesQTA);
            FreeAndnil(vDespJSONControlQTA);
          end;
        end;
        //Fin Funcion ControlQTAInt --------------------------------------------

        //Funcion LoginInt -----------------------------------------------------
        function LoginInt(vDespCadena: string):Integer;
        var
          vDespJSONLogin : TlkJSONobject;
          vDespUser, vDespPass: string;
          vDespQryLogin : TADOQuery;
        begin
          Result := 3; //Por defecto lo ponemos como error interno al resultado
          vDespQryLogin := TADOQuery.Create(nil);
          vDespQryLogin.Connection := conDB;

          vDespUser := '-';
          vDespPass := '-';
          //Creamos el objeto con el json enviado
          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; //Error interno
              //Exit;
            end;

            //Validacion del usuario
            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; //Ingreso permitido
            end
            else
            begin
              Result := 1; //Ingreso denegado
            end;
          finally
            FreeAndNil(vDespQryLogin);
            FreeAndnil(vDespJSONLogin);
          end;
        end;
        //Fin Funcion LoginInt -------------------------------------------------

        //Funcion ValidarMovilQRXInt -------------------------------------------
        function ValidarMovilQRXInt(vMovilQRX: Integer):string;
        var
          vFecha     : TDateTime;
          //vOrigenQRX : string;
          qryQRX     : TADOQuery;
          vJSONQRX   : TlkJSONobject;
          s          : string;
          vResultado :  Boolean;
        begin
          qryQRX            := TADOQuery.Create(nil);
          qryQRX.Connection := conDB;
          vJSONQRX := TlkJSONobject.Create;
          try
            Result := '{}';
            vResultado := False; //QAP

            //Validamos si el movil se encuentra QRX
            with qryQRX do
            begin
              Close;
              SQL.Clear;
              SQL.Add('Select GETDATE() as fecha ');
              Open;
            end;
            vFecha := qryQRX['fecha']; //Obtenermos la fecha del server
            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; //QRX sin fecha de fin
                end
                else
                begin
                  if vFecha < qryQRX['fechafin'] then
                  begin
                    vResultado := True; //QRX dentro de la fecha de fin
                  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;
        //Fin Funcion ValidarMovilQRXInt ---------------------------------------

        //Funcion UsuarioMovilInt ----------------------------------------------
        function UsuarioMovilInt(vUsuario: string): Integer;
        var
          vQryMovil : TADOQuery;
        begin
          vQryMovil := TADOQuery.Create(nil);
          vQryMovil.Connection := conDB;
          try
            //Validacion del usuario
            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;
        //Fin Funcion UsuarioMovilInt ------------------------------------------

        //Funcion ViajesInt ----------------------------------------------------
        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;
            //Log(IntToStr(vTotal)+' Viajes');

            vCount := 1;

            //Log('Reg: '+IntToStr(vQry.RecordCount));

            while not vQryViajes.Eof do
            begin
              //vJSONRes.Add('dv'+IntToStr(vCount), '['+DateToStr(vQry['fecha'])+'] $'+FloatToStrF(vQry['deuda'],ffFixed,14,2)+'-'+trim(vQry['detalle']) );
              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;
            //El en ultimo registro colocamos el total de viajes----
            vJSONViajes.Add('total', IntToStr(vTotal));
            //------------------------------------------------------

            s := TlkJSON.GenerateText(vJSONViajes);

            Result := s;

          finally
            FreeAndNil(vQryViajes);
            FreeAndnil(vJSONViajes);
          end;
        end;
        //Fin Funcion ViajesInt ------------------------------------------------
__________________
\_--> NoShY <--_/
Responder Con Cita