286 lines
11 KiB
PHP
286 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Reportes;
|
|
|
|
use const App\Http\Controllers\ASESOR_OPERACIONES;
|
|
use const App\Http\Controllers\ATENCION_CLIENTES;
|
|
use App\Models\TipoVehiculo;
|
|
use App\Models\CatEstatuServicio;
|
|
use App\Models\ServicioDet;
|
|
use App\Models\ServicioEnc;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use App\Http\Controllers\Controller;
|
|
use DB;
|
|
use Log;
|
|
|
|
class MensualController extends Controller{
|
|
|
|
function getWeek($date){
|
|
$date = new \DateTime($date);
|
|
$week = $date->format("W");
|
|
return $week;
|
|
}
|
|
|
|
public function reportes(){
|
|
$mes_actual = Carbon::now()->format('m');
|
|
|
|
$currentWeek = $this->getWeek(date("Y-m-d"));
|
|
$firstDateOfMonth = date("Y").'-'.date("m").'-01';
|
|
$firstWeek = $this->getWeek($firstDateOfMonth);
|
|
$lastDateOfMonth = date("Y-m-t", strtotime($firstDateOfMonth));
|
|
$lastWeek = $this->getWeek($lastDateOfMonth);
|
|
|
|
$servicios_nombre_precios = $this->serviciosNombresPrecios($mes_actual);
|
|
$tipos_servicios = $this->tiposServicios($mes_actual);
|
|
$tipos_estados_servicio = $this->tipoEstadoServicio($mes_actual);
|
|
$tipos_origen = $this->tiposOrigen($mes_actual);
|
|
$atencion_cliente = $this->atencionCliente($mes_actual);
|
|
$operadores = $this->operadores($mes_actual);
|
|
$servicios_vehiculos_precios = $this->serviciosVehiculosPrecios($mes_actual);
|
|
$capacidad_aprovechada = $this->capacidadAprovechada($mes_actual, $firstWeek, $lastWeek);
|
|
|
|
return response()->success(compact('servicios_nombre_precios','tipos_servicios', 'tipos_estados_servicio', 'tipos_origen', 'atencion_cliente', 'operadores', 'servicios_vehiculos_precios', 'capacidad_aprovechada'));
|
|
}
|
|
|
|
public function serviciosNombresPrecios($mes_actual){
|
|
|
|
$servicios = ServicioDet::select('cat_servicios.nombre as name', DB::raw("SUM(servicios_det.costo_servicio) AS costo, count(servicios_det.servicio_id) as y"))
|
|
->join('cat_servicios', 'servicios_det.servicio_id', '=', 'cat_servicios.id')
|
|
->whereMonth('servicios_det.fecha_solicitud', $mes_actual)
|
|
->groupBy('cat_servicios.nombre')
|
|
->get();
|
|
|
|
$total_costo = (string)number_format(collect($servicios)->sum('costo'),2);
|
|
|
|
return compact('servicios', 'total_costo');
|
|
}
|
|
|
|
public function tiposServicios($mes_actual){
|
|
|
|
$sevicios = ServicioDet::select('cat_tipos_servicios.nombre as name', DB::raw("count(servicios_det.tipo_servicio_id) as y"))
|
|
->join('cat_tipos_servicios', 'servicios_det.tipo_servicio_id', '=', 'cat_tipos_servicios.id')
|
|
->whereMonth('servicios_det.fecha_solicitud', $mes_actual)
|
|
->groupBy('cat_tipos_servicios.nombre')
|
|
->get();
|
|
|
|
return $sevicios;
|
|
}
|
|
|
|
public function tipoEstadoServicio($mes_actual){
|
|
|
|
$sevicios = ServicioDet::select('cat_estatus_servicios.nombre as name', DB::raw("count(servicios_det.estatus_servicio_id) as y"))
|
|
->join('cat_estatus_servicios', 'servicios_det.estatus_servicio_id', '=', 'cat_estatus_servicios.id')
|
|
->whereMonth('servicios_det.fecha_solicitud', $mes_actual)
|
|
->groupBy('cat_estatus_servicios.nombre')
|
|
->get();
|
|
|
|
return $sevicios;
|
|
}
|
|
|
|
public function tiposOrigen($mes_actual){
|
|
|
|
$sevicios = ServicioEnc::select('cat_origenes.nombre as name', DB::raw("count(servicios_enc.origen_id) as y"))
|
|
->join('servicios_det', 'servicios_enc.id', '=', 'servicios_det.servicio_enc_id')
|
|
->join('cat_origenes', 'servicios_enc.origen_id', '=', 'cat_origenes.id')
|
|
->whereMonth('servicios_det.fecha_solicitud', $mes_actual)
|
|
->groupBy('cat_origenes.nombre')
|
|
->get();
|
|
|
|
return $sevicios;
|
|
}
|
|
|
|
public function atencionCliente($mes_actual){
|
|
|
|
$usuarios_series = [];
|
|
$estatus_series = [];
|
|
|
|
$estatus_servicio = CatEstatuServicio::get();
|
|
|
|
foreach ($estatus_servicio as $e){
|
|
|
|
$usuarios_series = [];
|
|
$valores_estatus = [];
|
|
|
|
$atencion_cliente = User::select('id', DB::raw("CONCAT(nombre, ' ',apellido_paterno) as nombre_atencion"))
|
|
->where('tipo_empleado_id', ATENCION_CLIENTES)
|
|
->get();
|
|
|
|
foreach ($atencion_cliente as $aC){
|
|
|
|
$total = ServicioEnc::join('servicios_det', 'servicios_enc.id', '=', 'servicios_det.servicio_enc_id')
|
|
->join('cat_estatus_servicios', 'servicios_det.estatus_servicio_id', '=', 'cat_estatus_servicios.id')
|
|
->where('servicios_enc.usuario_agenda_id',$aC->id)
|
|
->whereMonth('servicios_det.fecha_solicitud', $mes_actual);
|
|
|
|
|
|
$total_general = ($total->count() == 1)? " (".$total->count()." servicio)" : " (".$total->count()." servicios)";
|
|
$nombre = $aC->nombre_atencion.$total_general;
|
|
|
|
array_push($usuarios_series, $nombre);
|
|
|
|
$total_estatus = $total->where('servicios_det.estatus_servicio_id', $e->id)->count();
|
|
array_push($valores_estatus, $total_estatus);
|
|
}
|
|
|
|
$estatus_series[$e->nombre] = $valores_estatus;
|
|
}
|
|
|
|
|
|
$datos = [];
|
|
foreach ($estatus_series as $key => $e){
|
|
array_push($datos, ["name"=>$key, "data"=>$e]);
|
|
}
|
|
|
|
$result = ["usuarios"=>$usuarios_series, "datos"=>$datos];
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function operadores($mes_actual){
|
|
|
|
$usuarios_series = [];
|
|
$estatus_series = [];
|
|
|
|
$estatus_servicio = CatEstatuServicio::get();
|
|
|
|
foreach ($estatus_servicio as $e){
|
|
|
|
$usuarios_series = [];
|
|
$valores_estatus = [];
|
|
|
|
$operadores = User::select('id', DB::raw("CONCAT(nombre, ' ',apellido_paterno) as nombre_atencion"))
|
|
->where('tipo_empleado_id', ASESOR_OPERACIONES)
|
|
->get();
|
|
|
|
foreach ($operadores as $o){
|
|
|
|
|
|
$total = ServicioDet::join('cat_estatus_servicios', 'servicios_det.estatus_servicio_id', '=', 'cat_estatus_servicios.id')
|
|
->where('servicios_det.operador_id',$o->id)
|
|
->whereMonth('servicios_det.fecha_solicitud', $mes_actual);
|
|
|
|
$total_general = ($total->count() == 1)? " (".$total->count()." servicio)" : " (".$total->count()." servicios)";
|
|
$nombre = $o->nombre_atencion.$total_general;
|
|
|
|
array_push($usuarios_series, $nombre);
|
|
|
|
$total_estatus = $total->where('servicios_det.estatus_servicio_id', $e->id)->count();
|
|
array_push($valores_estatus, $total_estatus);
|
|
}
|
|
|
|
$estatus_series[$e->nombre] = $valores_estatus;
|
|
}
|
|
|
|
|
|
$datos = [];
|
|
foreach ($estatus_series as $key => $e){
|
|
array_push($datos, ["name"=>$key, "data"=>$e]);
|
|
}
|
|
$result = ["usuarios"=>$usuarios_series, "datos"=>$datos];
|
|
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function serviciosVehiculosPrecios($mes_actual){
|
|
|
|
$servicios = ServicioDet::select('cat_vehiculos.num_economico as name', DB::raw("SUM(servicios_det.costo_servicio) AS costo, count(servicios_det.servicio_id) as y"))
|
|
->join('cat_vehiculos', 'servicios_det.vehiculo_id', '=', 'cat_vehiculos.id')
|
|
->whereMonth('servicios_det.fecha_solicitud', $mes_actual)
|
|
->groupBy('cat_vehiculos.num_economico')
|
|
->get();
|
|
|
|
return $servicios;
|
|
}
|
|
|
|
public function capacidadAprovechada($mes_actual, $firstWeek, $lastWeek){
|
|
|
|
$respuesta = [];
|
|
$index = 0;
|
|
|
|
$tipos_vehiculo = TipoVehiculo::select(['id', 'nombre'])->orderBy('nombre')->get();
|
|
foreach ($tipos_vehiculo as $t){
|
|
$tmpId = $t->id;
|
|
$tmpNombre = $t->nombre;
|
|
|
|
$campos = [
|
|
'cat_vehiculos.tipo_vehiculo_id',
|
|
'cat_tipos_vehiculos.nombre AS tipo_vehiculo',
|
|
'cat_tipos_vehiculos.objetivo_mensual',
|
|
DB::raw("SEC_TO_TIME(SUM(TIME_TO_SEC(servicios_progreso.duracion))) AS 'total_duracion'"),
|
|
DB::raw("TRUNCATE(SUM((TIME_TO_SEC(TIME(servicios_progreso.duracion))/60)/60), 3) AS 'total_duracion_fracc'")
|
|
];
|
|
|
|
$objetivoSemanal = 0;
|
|
$objetivoMensual = 0;
|
|
$totalWeeks = 0;
|
|
$sumatoriaPorcentajes = 0.0;
|
|
|
|
$informacion = [];
|
|
|
|
for($i=$firstWeek;$i<=$lastWeek;$i++){
|
|
// execute this week
|
|
$datos = ServicioDet::select($campos)
|
|
->join('servicios_enc', 'servicios_det.servicio_enc_id', '=', 'servicios_enc.id')
|
|
->join('cat_servicios', 'servicios_det.servicio_id', '=', 'cat_servicios.id')
|
|
->join('cat_tipos_servicios', 'servicios_det.tipo_servicio_id', '=', 'cat_tipos_servicios.id')
|
|
->join('cat_estatus_servicios','servicios_det.estatus_servicio_id','=','cat_estatus_servicios.id')
|
|
->join('cat_vehiculos', 'servicios_det.vehiculo_id', '=', 'cat_vehiculos.id')
|
|
->join('cat_tipos_vehiculos', 'cat_tipos_vehiculos.id', '=', 'cat_vehiculos.tipo_vehiculo_id')
|
|
->leftJoin('servicios_progreso', 'servicios_det.id', '=', 'servicios_progreso.servicio_det_id')
|
|
->join('cat_origenes', 'servicios_enc.origen_id', '=', 'cat_origenes.id')
|
|
->where('cat_vehiculos.tipo_vehiculo_id', $tmpId)
|
|
//->whereMonth('servicios_det.fecha_solicitud', $mes_actual)
|
|
->whereRaw("WEEKOFYEAR(DATE(servicios_det.fecha_solicitud))=?", $i)
|
|
->groupBy('cat_vehiculos.tipo_vehiculo_id', 'cat_tipos_vehiculos.nombre', 'cat_tipos_vehiculos.objetivo_mensual')
|
|
->first();
|
|
|
|
if($datos){
|
|
$sumatoriaPorcentajes = floatval($sumatoriaPorcentajes) + floatval($datos->total_duracion_fracc);
|
|
$objetivoMensual = $datos->objetivo_mensual;
|
|
array_push($informacion, [
|
|
'week_num'=>$i,
|
|
'total_duracion'=>$datos->total_duracion,
|
|
'total_duracion_fracc'=>$datos->total_duracion_fracc,
|
|
]);
|
|
}else{
|
|
array_push($informacion, [
|
|
'week_num'=>$i,
|
|
'total_duracion'=>null,
|
|
'total_duracion_fracc'=>null
|
|
]);
|
|
}
|
|
|
|
$totalWeeks++;
|
|
}
|
|
|
|
$respuesta[$index]['tipo_vehiculo_id'] = $tmpId;
|
|
$respuesta[$index]['tipo_vehiculo'] = $tmpNombre;
|
|
$respuesta[$index]['objetivo_mensual'] = $objetivoMensual;
|
|
$respuesta[$index]['objetivo_semanal'] = ($totalWeeks == 0) ? 0 : $objetivoMensual/($totalWeeks);
|
|
$respuesta[$index]['semanas'] = $totalWeeks;
|
|
$respuesta[$index]['total_cumplimiento'] = $sumatoriaPorcentajes;
|
|
$respuesta[$index]['datos'] = $informacion;
|
|
$index++;
|
|
}
|
|
|
|
return $respuesta;
|
|
}
|
|
|
|
function obtener_semana($date) {
|
|
|
|
$date2 = strtotime($date);
|
|
$inicio0 = strtotime('sunday this week -1 week', $date2);
|
|
$inicio=date('Y-m-d', $inicio0);
|
|
|
|
$dias = array();
|
|
for($i=1;$i<=7;$i++){
|
|
$dia = date("Y-m-d", strtotime("$inicio +$i day"));
|
|
array_push($dias,$dia);
|
|
}
|
|
|
|
return $dias;
|
|
}
|
|
}
|