MINI MINI MANI MO
<?php
namespace App\Http\Controllers;
use App\Exports\AsistenciasExport;
use App\Exports\VotosExport;
use App\Models\asistenciaVotos;
use App\Models\EventoVotos;
use App\Models\mensajesVotos;
use App\Models\temasVotos;
use Barryvdh\DomPDF\Facade as PDF;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use phpDocumentor\Reflection\Types\AbstractList;
class RegistrosController extends Controller
{
private $request;
public function __construct(Request $request) {
$this->request=$request;
$this->middleware('auth');
}
public function index()
{
if (auth()->user()->DatosUsers->tipo_usuario == 1) {
$registros = EventoVotos::all()->sortByDesc('id');
return view('registros.index', compact('registros'));
}else{
return redirect('cerrar');
}
}
public function edit($id)
{
$sesion = EventoVotos::find($id);
$activar['estatus'] = 1;
$sesion->update($activar);
$votaciones = temasVotos::where('randomEvento', $sesion->random)->get();
$asistencia = asistenciaVotos::where('randomEvento', $sesion->random)->get();
// dd($votaciones);
return view('registros.show', compact('sesion', 'votaciones', 'asistencia'));
}
public function imprimirVotosFin(Request $request)
{
$idVoto = $request['idVoto'];
$tem = temasVotos::find($idVoto);
$stat['status'] = 1;
$tem->update($stat);
$vot = mensajesVotos::where('id_votacion', $idVoto)->get();
foreach ($vot as $v) {
$voto = mensajesVotos::find($v->id);
$status['status'] = 1;
$voto->update($status);
}
$randomEvento = EventoVotos::where('estatus', 1)->get();
// $comision = Commission::where('random',$randomEvento[0]->tipoEvento)->get();
$temas = temasVotos::where('randomEvento', $randomEvento[0]->random)->where('status', 1)->get();
$sinregistro = mensajesVotos::where('status', 1)->where('mensaje', 'SIN REGISTRO')->get();
$abstencion = mensajesVotos::where('status', 1)->where('mensaje', 'ABSTENCION')->get();
$contra = mensajesVotos::where('status', 1)->where('mensaje', 'CONTRA')->get();
$favor = mensajesVotos::where('status', 1)->where('mensaje', 'FAVOR')->get();
$votacion = DB::select('SELECT v.mensaje, d.nombre, d.ap_pat, d.ap_mat, d.g_parlamentario AS partido
FROM mensajes_votos AS v, datos_users AS d
WHERE d.`id`=v.`id_diputado` AND v.status=1 ORDER BY d.ap_pat, d.ap_mat ASC');
$sinregistro = count($sinregistro);
$abstencion = count($abstencion);
$contra = count($contra);
$favor = count($favor);
$date = Carbon::now();
$date = $date->format('d-m-Y');
$tem = temasVotos::find($idVoto);
$stat['status'] = 0;
$tem->update($stat);
$fechaRegistro = $tem['created_at']->format('d-m-Y');
//dd($fechaRegistro);
$vot = mensajesVotos::where('id_votacion', $idVoto)->get();
foreach ($vot as $v) {
$voto = mensajesVotos::find($v->id);
$status['status'] = 0;
$voto->update($status);
}
$pdf = PDF::loadView('votaciones.imprimirTotalVotos', compact('date', 'votacion', 'sinregistro', 'abstencion', 'contra', 'favor', 'randomEvento', 'temas','fechaRegistro'));
return $pdf->stream('pdf_file.pdf');
}
public function descargaExcelVoto(Request $request)
{
$idVoto = $request['idVoto'];
$temas = temasVotos::find($idVoto);
$votos = mensajesVotos::where('id_votacion', $idVoto)->get();
return Excel::download(new VotosExport($votos, $temas), "votacion.xlsx");
}
public function editarVoto(Request $request)
{
$idVoto = $request['idVoto'];
$temasActivos = temasVotos::where('status', 1)->get();
$votosActivos = mensajesVotos::where('status', 1)->get();
if($temasActivos){
foreach ($temasActivos as $tema){
$tem = temasVotos::find($tema->id);
$desactiva['status'] = 0;
$tem->update($desactiva);
}
}
if($votosActivos) {
foreach ($votosActivos as $votos) {
$vot = mensajesVotos::find($votos->id);
$desactiva['status'] = 0;
$vot->update($desactiva);
}
}
$voto = temasVotos::find($idVoto);
$activar['status'] = 1;
$voto->update($activar);
$mensajeVotos = mensajesVotos::where('id_votacion', $idVoto)->get();
foreach ($mensajeVotos as $sentidoVoto) {
$activarVoto = mensajesVotos::find($sentidoVoto->id);
$acti['status'] = 1;
$activarVoto->update($acti);
}
// return redirect('votos');
}
public function descargaExcelAsiste($id)
{
$asistencias = asistenciaVotos::where('randomEvento', $id)->get();
return Excel::download(new AsistenciasExport($asistencias), "asistencia.xlsx");
}
public function editAsistencia(Request $request)
{
$randomEvento = $request['idVoto'];
$asistenciaActivos = asistenciaVotos::where('status', 1)->get();
if($asistenciaActivos){
foreach ($asistenciaActivos as $asiste){
$tem = asistenciaVotos::find($asiste->id);
$desactiva['status'] = 2;
$tem->update($desactiva);
}
}
$mensajeAsistenia = asistenciaVotos::where('randomEvento', $randomEvento)->get();
foreach ($mensajeAsistenia as $as) {
$activarAsis = asistenciaVotos::find($as->id);
$acti['status'] = 1;
$activarAsis->update($acti);
}
// return redirect('votos');
}
public function editaTemaVotoReg($id){
$temaV = temasVotos::find($id);
// dd($temaV['votacion']);
return view('votaciones.edit', compact('temaV'));
}
public function editSesion($id){
$evento = EventoVotos::find($id);
return view('registros.editEvento', compact('evento'));
}
public function editarSesion(Request $request){
$evento = EventoVotos::find($request['id']);
$up['nombreEvento'] = $request['sesion'];
$evento->update($up);
return redirect('registros');
}
public function eliminarSesion(Request $request){
$id = $request['id'];
$evento = EventoVotos::find($id);
$evento->delete();
return 1;
}
public function publicarVoto(Request $request){
$datos = $request->except('_token', '_method');
$votos = mensajesVotos::where('id_votacion',$datos['idVotacion'])->get();
foreach ($votos as $item){
if ($item->mensaje == 'SIN REGISTRO'){
$sentido = 4;
}else if ($item->mensaje == 'FAVOR'){
$sentido = 1;
}else if ($item->mensaje == 'ABSTENCION'){
$sentido = 3;
}else if ($item->mensaje == 'CONTRA'){
$sentido = 2;
}
$vot[] = ([
'idDiputado' => $item->parlamentario->idLegislativo,
'sentido' => $sentido,
]);
}
$client = new Client();
try{
$datos= ([
'noIniciativaPunto' => $datos['num'],
'idDecreto' => $datos['idDecreto'],
'categoria' => $datos['tipoP'],
'votos' => $vot
]);
$response = $client->post('http://legislacion.local/api/saveVotos', ['json' => $datos]);
$rows = $response->getBody()->getContents();
return $rows;
} catch (\Exception $e) {
return 0;
}
}
public function getDecretos(Request $request){
$datos = $request->except('_method', '_token');
$client = new Client();
try{
// $url = 'https://legislacion.congresoedomex.gob.mx/api/getDecretosSpid/'.$datos['num'];
// $response = $client->get($url);
$response = $client->get('http://legislacion.local/api/getDecretosSpid/'.$datos['num']);
$rows = $response->getBody()->getContents();
return $rows;
} catch (\Exception $e) {
dd($e);
// if($e->getCode() == 404){
// $error = true;
// $msg="Ocurrio un problema al obtener los decretos, comunicate con el departamento de Desarrollo y actualización tecnologica";
//
// }else{
// return redirect()->back()->withErrors(['msg' => 'Error de conexión con el sistema, contacta al desarrollador']);
// }
}
// $rows = json_decode($res->getBody()->getContents(), true);
// dd($rows);
}
public function publicarSesion(Request $request){
$datos = $request->except('_token', '_method');
$sesion = EventoVotos::find($datos['idSesion']);
foreach ($sesion->asistencias as $item) {
if ($item->mensaje == 'ASISTENCIA'){
$sentido = 1;
}else{
$sentido = 2;
}
$asistencia[] = ([
'sentido' => $sentido,
'idDiputado' => $item->parlamentario->idLegislativo
]);
}
$sesion = ([
'numero_sesion' => $datos['numSesion'],
'diputados' => $asistencia
]);
$client = new Client();
try{
$response = $client->post('http://legislacion.local/api/saveAsistencia', ['json' => $sesion]);
$rows = $response->getBody()->getContents();
return $rows;
} catch (\Exception $e) {
return 0;
}
}
public function eliminarVoto(Request $request){
$datos = $request->except('_method', '_token');
$voto = temasVotos::find($datos['id']);
$voto->votos->each->delete();
$voto->delete();
}
}
OHA YOOOO