public function getResults()
 {
     $input = Request::createFromGlobals();
     // Get customers
     $clientes = Reclamo::orderBy('cliente_nombre')->lists('cliente_nombre');
     $clientes = $clientes->unique();
     // Get unfiltered requests
     $reclamos = Reclamo::with('ef_request');
     // Apply filters
     $reclamos = $this->applyFilters($reclamos);
     $reclamos = $reclamos->get();
     $records = [];
     foreach ($clientes as $cliente) {
         $cliente_reclamos = $reclamos->filter(function ($reclamo) use($cliente) {
             return $reclamo->cliente_nombre == $cliente;
         });
         $numAbiertos = 0;
         $montoAbiertos = 0;
         $totalTimeArrayAbiertos = ['work' => 0, 'non-work' => 0, 'total' => 0];
         $numCerrados = 0;
         $montoCerrados = 0;
         $totalTimeArrayCerrados = ['work' => 0, 'non-work' => 0, 'total' => 0];
         foreach ($cliente_reclamos as $reclamo) {
             if ($reclamo->reclamo_fecha_fin != null && $reclamo->reclamo_fecha_fin != '' && $reclamo->reclamo_fecha_fin != '0000-00-00 00:00:00') {
                 // Set end date
                 $date_end = $reclamo->reclamo_fecha_fin;
                 // Get times
                 $timeArray = TimeController::getHours($reclamo->reclamo_fecha_inicio, $date_end);
                 $totalTimeArrayCerrados['work'] += $timeArray['work'];
                 $totalTimeArrayCerrados['non-work'] += $timeArray['non-work'];
                 $totalTimeArrayCerrados['total'] += $timeArray['total'];
                 $numCerrados++;
                 $montoCerrados += $reclamo->nc_monto;
             } else {
                 // Set end date
                 $date_end = date('Y-m-d H:i:s');
                 // Get times
                 $timeArray = TimeController::getHours($reclamo->reclamo_fecha_inicio, $date_end);
                 $totalTimeArrayAbiertos['work'] += $timeArray['work'];
                 $totalTimeArrayAbiertos['non-work'] += $timeArray['non-work'];
                 $totalTimeArrayAbiertos['total'] += $timeArray['total'];
                 $numAbiertos++;
                 $montoAbiertos += $reclamo->nc_monto;
             }
             // Set end date
             $date_end = $reclamo->reclamo_fecha_fin ? $reclamo->reclamo_fecha_fin : date('Y-m-d H:i:s');
             // Get times
             $timeArray = TimeController::getHours($reclamo->reclamo_fecha_inicio, $date_end);
         }
         $duracionAbiertos = 0;
         $duracionCerrados = 0;
         if ($input->duration == 'calendar') {
             if ($numAbiertos > 0) {
                 $duracionAbiertos = $totalTimeArrayAbiertos['total'] / $numAbiertos / 24;
             }
             if ($numCerrados > 0) {
                 $duracionCerrados = $totalTimeArrayCerrados['total'] / $numCerrados / 24;
             }
         } else {
             if ($numAbiertos > 0) {
                 $duracionAbiertos = $totalTimeArrayAbiertos['work'] / $numAbiertos / 8;
             }
             if ($numCerrados > 0) {
                 $duracionCerrados = $totalTimeArrayCerrados['work'] / $numCerrados / 8;
             }
         }
         if ($numAbiertos > 0 || $numCerrados > 0) {
             $records[] = ['nombre' => $cliente, 'num_abiertos' => $numAbiertos, 'monto_abiertos' => $montoAbiertos, 'duracion_abiertos' => $duracionAbiertos, 'num_cerrados' => $numCerrados, 'monto_cerrados' => $montoCerrados, 'duracion_cerrados' => $duracionCerrados, 'total' => $numAbiertos + $numCerrados];
         }
     }
     // Get the records
     return $records;
 }
 public function getResults()
 {
     $input = Request::createFromGlobals();
     // Tomar clases
     $clases = Reclamo::orderBy('reclamo_clase')->lists('reclamo_clase');
     $clases = $clases->unique();
     // Get unfiltered requests
     $reclamos = Reclamo::where('id', '>', 0);
     // Apply filters
     $reclamos = $this->applyFilters($reclamos);
     $reclamos = $reclamos->with('reclamo_causas')->get();
     $records = [];
     foreach ($clases as $clase) {
         $finalData = [];
         $obj_clase = Clase::find($clase);
         $problemas = Problema::where('clase_id', $obj_clase->id)->get();
         foreach ($problemas as $problema) {
             $causas = Causa::where('problema_id', $problema->id)->get();
             foreach ($causas as $causa) {
                 $cliente_reclamos = $reclamos->filter(function ($reclamo) use($clase) {
                     return $reclamo->reclamo_clase == $clase;
                 })->filter(function ($reclamo) use($causa) {
                     $retval = false;
                     foreach ($reclamo->reclamo_causas as $reclamo_causa) {
                         if ($reclamo_causa->causa_id == $causa->id && !$retval) {
                             $retval = true;
                         }
                     }
                     return $retval;
                 });
                 $numAbiertos = 0;
                 $montoAbiertos = 0;
                 $totalTimeArrayAbiertos = ['work' => 0, 'non-work' => 0, 'total' => 0];
                 $numCerrados = 0;
                 $montoCerrados = 0;
                 $totalTimeArrayCerrados = ['work' => 0, 'non-work' => 0, 'total' => 0];
                 foreach ($cliente_reclamos as $reclamo) {
                     if ($reclamo->reclamo_fecha_fin != null && $reclamo->reclamo_fecha_fin != '' && $reclamo->reclamo_fecha_fin != '0000-00-00 00:00:00') {
                         // Set end date
                         $date_end = $reclamo->reclamo_fecha_fin;
                         // Get times
                         $timeArray = TimeController::getHours($reclamo->reclamo_fecha_inicio, $date_end);
                         $totalTimeArrayCerrados['work'] += $timeArray['work'];
                         $totalTimeArrayCerrados['non-work'] += $timeArray['non-work'];
                         $totalTimeArrayCerrados['total'] += $timeArray['total'];
                         $numCerrados++;
                         $montoCerrados += $reclamo->nc_monto;
                     } else {
                         // Set end date
                         $date_end = date('Y-m-d H:i:s');
                         // Get times
                         $timeArray = TimeController::getHours($reclamo->reclamo_fecha_inicio, $date_end);
                         $totalTimeArrayAbiertos['work'] += $timeArray['work'];
                         $totalTimeArrayAbiertos['non-work'] += $timeArray['non-work'];
                         $totalTimeArrayAbiertos['total'] += $timeArray['total'];
                         $numAbiertos++;
                         $montoAbiertos += $reclamo->nc_monto;
                     }
                     // Set end date
                     $date_end = $reclamo->reclamo_fecha_fin ? $reclamo->reclamo_fecha_fin : date('Y-m-d H:i:s');
                     // Get times
                     $timeArray = TimeController::getHours($reclamo->reclamo_fecha_inicio, $date_end);
                 }
                 $duracionAbiertos = 0;
                 $duracionCerrados = 0;
                 if ($input->duration == 'calendar') {
                     if ($numAbiertos > 0) {
                         $duracionAbiertos = $totalTimeArrayAbiertos['total'] / $numAbiertos / 24;
                     }
                     if ($numCerrados > 0) {
                         $duracionCerrados = $totalTimeArrayCerrados['total'] / $numCerrados / 24;
                     }
                 } else {
                     if ($numAbiertos > 0) {
                         $duracionAbiertos = $totalTimeArrayAbiertos['work'] / $numAbiertos / 8;
                     }
                     if ($numCerrados > 0) {
                         $duracionCerrados = $totalTimeArrayCerrados['work'] / $numCerrados / 8;
                     }
                 }
                 if ($numAbiertos + $numCerrados > 0) {
                     $finalData[] = ['nombre' => $problema->nombre . ' - ' . $causa->nombre, 'num_abiertos' => $numAbiertos, 'monto_abiertos' => $montoAbiertos, 'duracion_abiertos' => $duracionAbiertos, 'num_cerrados' => $numCerrados, 'monto_cerrados' => $montoCerrados, 'duracion_cerrados' => $duracionCerrados, 'total' => $numAbiertos + $numCerrados];
                 }
             }
         }
         if (count($finalData) > 0) {
             $records[] = ['name' => $obj_clase->nombre, 'data' => $finalData];
         }
     }
     // Get the records
     return $records;
 }