Carga inicial
This commit is contained in:
272
app/Http/Controllers/Reportes/SemanalController.php
Normal file
272
app/Http/Controllers/Reportes/SemanalController.php
Normal file
@@ -0,0 +1,272 @@
|
||||
<?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 SemanalController extends Controller
|
||||
{
|
||||
function getWeek($date){
|
||||
$date = new \DateTime($date);
|
||||
$week = $date->format("W");
|
||||
return $week;
|
||||
}
|
||||
|
||||
public function reportes(){
|
||||
|
||||
$fecha_actual = Carbon::now()->format('Y-m-d');
|
||||
$dias_semana = $this->obtener_semana($fecha_actual);
|
||||
|
||||
$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);
|
||||
$weeks = 0;
|
||||
for($i=$firstWeek;$i<=$lastWeek;$i++) $weeks++;
|
||||
|
||||
$servicios_nombre_precios = $this->serviciosNombresPrecios($dias_semana);
|
||||
$tipos_servicios = $this->tiposServicios($dias_semana);
|
||||
$tipos_estados_servicio = $this->tipoEstadoServicio($dias_semana);
|
||||
$tipos_origen = $this->tiposOrigen($dias_semana);
|
||||
$atencion_cliente = $this->atencionCliente($dias_semana);
|
||||
$operadores = $this->operadores($dias_semana);
|
||||
$servicios_vehiculos_precios = $this->serviciosVehiculosPrecios($dias_semana);
|
||||
$capacidad_aprovechada = $this->capacidadAprovechada($currentWeek, $weeks);
|
||||
|
||||
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($dias_semana){
|
||||
|
||||
$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')
|
||||
->whereIn(DB::raw("DATE(servicios_det.fecha_solicitud)"),$dias_semana)
|
||||
->groupBy('cat_servicios.nombre')
|
||||
->get();
|
||||
|
||||
$total_costo = (string)number_format(collect($servicios)->sum('costo'),2);
|
||||
|
||||
return compact('servicios', 'total_costo');
|
||||
}
|
||||
|
||||
public function tiposServicios($dias_semana){
|
||||
|
||||
$servicios = 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')
|
||||
->whereIn(DB::raw("DATE(servicios_det.fecha_solicitud)"),$dias_semana)
|
||||
->groupBy('cat_tipos_servicios.nombre')
|
||||
->get();
|
||||
|
||||
return $servicios;
|
||||
}
|
||||
|
||||
public function tipoEstadoServicio($dias_semana){
|
||||
|
||||
$servicios = 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')
|
||||
->whereIn(DB::raw("DATE(servicios_det.fecha_solicitud)"),$dias_semana)
|
||||
->groupBy('cat_estatus_servicios.nombre')
|
||||
->get();
|
||||
|
||||
return $servicios;
|
||||
}
|
||||
|
||||
public function tiposOrigen($dias_semana){
|
||||
|
||||
$servicios = 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')
|
||||
->whereIn(DB::raw("DATE(servicios_det.fecha_solicitud)"),$dias_semana)
|
||||
->groupBy('cat_origenes.nombre')
|
||||
->get();
|
||||
|
||||
return $servicios;
|
||||
}
|
||||
|
||||
public function atencionCliente($dias_semana){
|
||||
|
||||
$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)
|
||||
->whereIn(DB::raw("DATE(servicios_det.fecha_solicitud)"),$dias_semana);
|
||||
|
||||
|
||||
$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($dias_semana){
|
||||
|
||||
$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)
|
||||
->whereIn(DB::raw("DATE(servicios_det.fecha_solicitud)"),$dias_semana);
|
||||
|
||||
$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($dias_semana){
|
||||
|
||||
$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')
|
||||
->whereIn(DB::raw("DATE(servicios_det.fecha_solicitud)"),$dias_semana)
|
||||
->groupBy('cat_vehiculos.num_economico')
|
||||
->get();
|
||||
|
||||
return $servicios;
|
||||
}
|
||||
|
||||
public function capacidadAprovechada($semana_actual, $weeks=4){
|
||||
|
||||
$respuesta = [];
|
||||
|
||||
$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'")
|
||||
];
|
||||
|
||||
$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)
|
||||
->whereRaw("WEEKOFYEAR(DATE(servicios_det.fecha_solicitud))=?", $semana_actual)
|
||||
->groupBy('cat_vehiculos.tipo_vehiculo_id', 'cat_tipos_vehiculos.nombre', 'cat_tipos_vehiculos.objetivo_mensual')
|
||||
->first();
|
||||
|
||||
if($datos){
|
||||
array_push($respuesta, [
|
||||
'tipo_vehiculo_id'=>$datos->tipo_vehiculo_id,
|
||||
'tipo_vehiculo'=>$datos->tipo_vehiculo,
|
||||
'objetivo_mensual'=>$datos->objetivo_mensual,
|
||||
'objetivo_semanal'=>($weeks == 0) ? 0 : $datos->objetivo_mensual/($weeks),
|
||||
'semanas'=>$weeks,
|
||||
'total_duracion'=>$datos->total_duracion,
|
||||
'total_duracion_fracc'=>$datos->total_duracion_fracc,
|
||||
]);
|
||||
}else{
|
||||
array_push($respuesta, [
|
||||
'tipo_vehiculo_id'=>$tmpId,
|
||||
'tipo_vehiculo'=>$tmpNombre,
|
||||
'objetivo_mensual'=>0,
|
||||
'objetivo_semanal'=>0,
|
||||
'semanas'=>$weeks,
|
||||
'total_duracion'=>null,
|
||||
'total_duracion_fracc'=>null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user