public static function recover($sid, $tid)
 {
     $tokenRecuperable = DB::Table('tokens_' . $sid)->where('tid', $tid)->first();
     $attRecargables = LlamadasController::getAttRecargables($sid);
     $selectSQL = '';
     $updatedValues = array();
     $updatedValues['completed'] = 'N';
     if (sizeof($attRecargables) != 0) {
         $firstElement = true;
         foreach ($attRecargables as $key => $value) {
             if (!$firstElement) {
                 $selectSQL .= ',';
             } else {
                 $firstElement = false;
             }
             $selectSQL .= '`' . $value . '` as ' . $key;
         }
         $sqlUltimosDatosRegistrados = " select " . $selectSQL . " from ( select token,max(id) as maxid from survey_" . $sid . " group by token) as maxTable " . " inner join survey_" . $sid . " as sv on maxTable.token=sv.token and maxTable.maxId = sv.id " . " where maxTable.token='" . $tokenRecuperable->token . "'";
         $rsUpdatedValues = DB::select($sqlUltimosDatosRegistrados)[0];
         var_dump($rsUpdatedValues);
         foreach ($attRecargables as $key => $value) {
             $updatedValues[$key] = $rsUpdatedValues->{$key};
         }
     }
     //Actualizo el token recibido
     DB::table('tokens_' . $sid)->where('tid', $tid)->update($updatedValues);
 }
 public function recover(Request $request, $sid)
 {
     $desde = $request->input('desde');
     $hasta = $request->input('hasta');
     if ($desde < 0) {
         $desde = 1;
     }
     $nLlamadasTot = DB::table('tokens_' . $sid)->count();
     if ($hasta > $nLlamadasTot) {
         $hasta = $nLlamadasTot;
     }
     if ($desde > $hasta) {
         return Redirect::to('survey/' . $sid . '/dispatch')->with('status', 'El limite inferior no puede ser mayor que el limite superior.');
     }
     log::info('[' . $sid . '] Recueperamos las llamadas de el token ' . $desde . ' hasta el ' . $hasta);
     $rellamadaColumn = $sid . 'X' . SettingsController::getAnwswerGroup($sid) . 'X' . SettingsController::getRellamadaAnswer($sid);
     $sqlTokensRecuperables = " select tid,token from tokens_" . $sid . " where token in " . " ( " . " \tselect maxTable.token " . " \tfrom ( select token,max(id) as maxid from survey_" . $sid . " group by token) as maxTable " . " \tinner join survey_" . $sid . " as sv on maxTable.token=sv.token and maxTable.maxId = sv.id " . " \twhere `" . $rellamadaColumn . "`='A1' " . "  ) and tokens_" . $sid . ".completed<>'N'" . " and tid between " . $desde . " and " . $hasta;
     $tokensRecuperables = DB::select($sqlTokensRecuperables);
     foreach ($tokensRecuperables as $tokenRecuperable) {
         //Recuperamos cada llamada
         LlamadasController::recover($sid, $tokenRecuperable->tid);
     }
     return Redirect::to('survey/' . $sid)->with('status', 'Proceso de recuperacion de llamadas, realizado correctamente');
 }