/app/controllers/MantenimientosController.php
PHP | 382 lines | 313 code | 66 blank | 3 comment | 47 complexity | 1f67e9078b95f221336264577b1f2f90 MD5 | raw file
- <?php
- class MantenimientosController extends BaseController {
- public function getIndex(){
- $ascensores = Ascensor::where('activated', '=', 1)
- ->where('baja', '=', 1)
- ->where('montado', '=', 1)
- ->orderBy('proximo_mantenimiento', 'asc')
- ->get();
- $mantenimientos = Mantenimientoexterno::where('activated', '=', 1)
- ->where('baja', '=', 1)
- ->orderBy('proximo_mantenimiento', 'asc')
- ->get();
- return View::make('mantenimientos.index')
- ->with('ascensores', $ascensores)
- ->with('mantenimientos', $mantenimientos);
- }
- public function getCompletados(){
- $mantenimientospage = 20;
- if (Input::has('texto') || Input::has('fechadesde') || Input::has('fechahasta')) {
- $mantenimientospage = 200;
- $input = Input::all();
- $flag = false;
-
- $v = Validator::make($input, Aviso::$rules_filtrar, Aviso::$mensajes_filtrar);
- if ($v->fails()) {
- return Redirect::to('mantenimientos/completados')
- ->withErrors($v);
- }
- if (Input::has('fechadesde') && Input::has('fechahasta')) {
- $datedesde = DateTime::createFromFormat('d/m/Y', Input::get('fechadesde'));
- $datehasta = DateTime::createFromFormat('d/m/Y', Input::get('fechahasta'));
- if ($datedesde > $datehasta) {
- $flag = true;
- }
- }
- if ($flag) {
- return Redirect::to('mantenimientos/completados')
- ->with('mok', 'FechaIncorrecta');
- }
- $clienteslist = DB::table('clientes')->where(function($query){
- $query->where('direccion', 'like', '%'.Input::get('texto').'%')
- ->orWhere('ciudad', 'like', '%'.Input::get('texto').'%')
- ->orWhere('nombre_contacto', 'like', '%'.Input::get('texto').'%')
- ->orWhere('telefono_contacto', 'like', '%'.Input::get('texto').'%')
- ->orWhere('email_contacto', 'like', '%'.Input::get('texto').'%');
- })->orderBy('nombre_contacto', 'desc')
- ->lists('id');
- if (count($clienteslist) > 0) {
- $ascensoreslistcli = DB::table('ascensores')->whereIn('cliente_id', $clienteslist)->lists('id');
- $mantenimientoexternoslistcli = DB::table('mantenimientoexternos')->whereIn('cliente_id', $clienteslist)->lists('id');
- } else {
- $ascensoreslistcli = DB::table('ascensores')->where('id', '=', 0)
- ->lists('id');
- $mantenimientoexternoslistcli = DB::table('mantenimientoexternos')->where('id', '=', 0)
- ->lists('id');
- }
- $ascensoreslist = Ascensor::where('rae', 'like', '%'.Input::get('texto').'%')->lists('id');
- $mantenimientoexternoslist = Mantenimientoexterno::where('rae', 'like', '%'.Input::get('texto').'%')->lists('id');
- $unionascensores = array_merge($ascensoreslist, $ascensoreslistcli);
- $resultascensores = array_unique($unionascensores);
- $unionmantenimientos = array_merge($mantenimientoexternoslist, $mantenimientoexternoslistcli);
- $resultmantenimientos = array_unique($unionmantenimientos);
- if (count($resultascensores) > 0 || count($resultmantenimientos) > 0) {
- if (count($resultascensores) > 0) {
- $mantenimientosascensor = DB::table('mantenimientos')
- ->whereIn('ascensor_id', $resultascensores)
- ->lists('id');
- }
- else{
- $mantenimientosascensor = DB::table('mantenimientos')
- ->where('id', '=', 0)
- ->lists('id');
- }
-
- if (count($resultmantenimientos) > 0) {
- $mantenimientosexternos= DB::table('mantenimientos')
- ->whereIn('mantenimientoexterno_id', $resultmantenimientos)
- ->lists('id');
- }
- else{
- $mantenimientosexternos= DB::table('mantenimientos')
- ->where('id', '=', 0)
- ->lists('id');
- }
- $union = array_merge($mantenimientosascensor, $mantenimientosexternos);
- $result = array_unique($union);
- if (count($result)) {
- $mantenimientos = DB::table('mantenimientos')
- ->whereIn('id', $result)
- ->where(function($query){
- if (Input::get('fechadesde') != "") {
- $date = DateTime::createFromFormat('d/m/Y', Input::get('fechadesde'));
- $query->where('fecha', '>=', $date->format('Y-m-d'));
- }
- if (Input::get('fechahasta') != "") {
- $date = DateTime::createFromFormat('d/m/Y', Input::get('fechahasta'));
- $query->where('fecha', '<=', $date->modify('+1 day')->format('Y-m-d'));
- }
- })
- ->orderBy('fecha', 'desc')
- ->paginate($mantenimientospage);
- } else {
- $mantenimientos = DB::table('mantenimientos')
- ->where('id', '=', 0)
- ->paginate($mantenimientospage);
- }
- } else {
- $mantenimientos = DB::table('mantenimientos')
- ->where('id', '=', 0)
- ->paginate($mantenimientospage);
- }
- return View::make('mantenimientos.completados')
- ->with('input', $input)
- ->with('mantenimientos', $mantenimientos);
- }
- else{
- $mantenimientos = DB::table('mantenimientos')
- ->orderBy('fecha', 'desc')
- ->paginate($mantenimientospage);
- }
- return View::make('mantenimientos.completados')
- ->with('mantenimientos', $mantenimientos);
- }
- public function postNuevo(){
- $input = Input::all();
- $v = Validator::make($input, Mantenimiento::$rules, Mantenimiento::$mensajes);
- if ($v->fails()) {
- return Redirect::to('mantenimientos/nuevo/'.$input['raeascensor'])
- ->withErrors($v)
- ->with('input', $input);
- }
- else{
- $date = date_create_from_format('d/m/Y H:i', $input['fecha']);
- $dateproximo = date_create_from_format('d/m/Y H:i', $input['fecha']);
- $intervalo = new DateInterval('P1M');
- $dateproximo->add($intervalo);
- $mantenimiento = new Mantenimiento();
- $mantenimiento->resultado = $input['resultado'];
- $mantenimiento->informe = $input['informe'];
- $mantenimiento->user_id = $input['empleado'];
- $mantenimiento->fecha = $date->format('Y-m-d H:i:s');
- if ($input['mantenimiento_id'] != 0) {
- $mantenimientoexterno = Mantenimientoexterno::where('id', '=', $input['mantenimiento_id'])->first();
- $mantenimientoexterno->ultimo_mantenimiento = $date->format('Y-m-d');
- $mantenimientoexterno->proximo_mantenimiento = $dateproximo->format('Y-m-d');
- $mantenimientoexterno->save();
- $mantenimiento->mantenimientoexterno_id = $mantenimientoexterno->id;
- $cliente = $mantenimientoexterno->cliente;
- } else {
- $ascensor = Ascensor::where('id', '=', $input['ascensor_id'])->first();
- $ascensor->ultimo_mantenimiento = $date->format('Y-m-d');
- $ascensor->proximo_mantenimiento = $dateproximo->format('Y-m-d');
- $ascensor->save();
- $mantenimiento->ascensor_id = $ascensor->id;
- $cliente = $ascensor->cliente;
- $medida = Medida::where('id', '=', $ascensor->medida_id)->first();
- }
- $mantenimiento->save();
- $rutalogo = asset('images/logo_grande.jpg');
- try{
- ob_start();
- include(app_path().'/views/pdf/informemantenimiento.php');
- $content = ob_get_clean();
- // convert to PDF
- require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
- $html2pdf = new HTML2PDF('P', 'A4', 'es');
- $html2pdf->pdf->SetDisplayMode('fullpage');
- $html2pdf->writeHTML($content);
- $html2pdf->Output(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf','F');
- }
- catch(HTML2PDF_exception $e) {
- }
- $mantenimiento->enviarcorreo();
-
- return Redirect::to('mantenimientos/')
- ->with('mok', 'Alta')
- ->with('id', $mantenimiento->id);
-
- }
- }
- public function postEditar(){
- $input = Input::all();
- $v = Validator::make($input, Mantenimiento::$rules, Mantenimiento::$mensajes);
- if ($v->fails()) {
- return Redirect::to('mantenimientos/editar/'.$input['idmantenimiento'])
- ->withErrors($v)
- ->with('input', $input);
- }
- else{
- $date = date_create_from_format('d/m/Y H:i', $input['fecha']);
- $dateproximo = date_create_from_format('d/m/Y H:i', $input['fecha']);
- $intervalo = new DateInterval('P1M');
- $dateproximo->add($intervalo);
- $mantenimiento = Mantenimiento::where('id', '=', $input['idmantenimiento'])->first();
- $mantenimiento->resultado = $input['resultado'];
- $mantenimiento->informe = $input['informe'];
- $mantenimiento->user_id = $input['empleado'];
- $mantenimiento->fecha = $date->format('Y-m-d H:i:s');
- $mantenimiento->save();
- if ($input['mantenimiento_id'] != 0) {
- $mantenimientoexterno = Mantenimientoexterno::where('id', '=', $input['mantenimiento_id'])->first();
- $cliente = $mantenimientoexterno->cliente;
- } else {
- $ascensor = Ascensor::where('id', '=', $input['ascensor_id'])->first();
- $cliente = $ascensor->cliente;
- $medida = Medida::where('id', '=', $ascensor->medida_id)->first();
- }
- $mantenimiento->save();
- $rutalogo = asset('images/logo_grande.jpg');
- $rutapdfcomprobacion = 'pdf/informemantenimiento/'.$mantenimiento->id.'.pdf';
- if(file_exists($rutapdfcomprobacion)){
- unlink(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf');
- }
- try{
- ob_start();
- include(app_path().'/views/pdf/informemantenimiento.php');
- $content = ob_get_clean();
- // convert to PDF
- require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
- $html2pdf = new HTML2PDF('P', 'A4', 'es');
- $html2pdf->pdf->SetDisplayMode('fullpage');
- $html2pdf->writeHTML($content);
- $html2pdf->Output(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf','F');
- }
- catch(HTML2PDF_exception $e) {
- }
- return Redirect::to('mantenimientos/completados')
- ->with('mok', 'Editar')
- ->with('id', $mantenimiento->id);
-
- }
- }
- public function postGenerarPdf(){
- $input = Input::all();
- $mantenimiento = Mantenimiento::where('id', '=', $input['hiddenid'])->first();
- if ($mantenimiento->mantenimientoexterno_id != 0) {
- $mantenimientoexterno = Mantenimientoexterno::where('id', '=', $mantenimiento->mantenimientoexterno_id)->first();
- $cliente = $mantenimientoexterno->cliente;
- } else {
- $ascensor = Ascensor::where('id', '=', $mantenimiento->ascensor_id)->first();
- $cliente = $ascensor->cliente;
- $medida = Medida::where('id', '=', $ascensor->medida_id)->first();
- }
- $rutalogo = asset('images/logo_grande.jpg');
- $rutapdfcomprobacion = 'pdf/informemantenimiento/'.$mantenimiento->id.'.pdf';
- if(file_exists($rutapdfcomprobacion)){
- unlink(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf');
- }
- try{
- ob_start();
- include(app_path().'/views/pdf/informemantenimiento.php');
- $content = ob_get_clean();
- // convert to PDF
- require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
- $html2pdf = new HTML2PDF('P', 'A4', 'es');
- $html2pdf->pdf->SetDisplayMode('fullpage');
- $html2pdf->writeHTML($content);
- $html2pdf->Output(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf','F');
- }
- catch(HTML2PDF_exception $e) {
- }
- return Redirect::to('mantenimientos/completados')
- ->with('mok', 'GenerarPdf')
- ->with('id', $mantenimiento->id);
- }
- public function getListado(){
- $mantenimientospage = 20;
- $input = Input::all();
- $clienteid = 0;
- if (Input::has('raeascensor')){
- $ascensor = Ascensor::where('rae', '=', Input::get('raeascensor'))->first();
- $mantenimientoexterno = Mantenimientoexterno::where('rae', '=', Input::get('raeascensor'))->first();
- if (isset($ascensor)) {
- $cliente = $ascensor->cliente;
- $clienteid = $cliente->id;
- $mantenimientos = DB::table('mantenimientos')
- ->where('ascensor_id', '=', $ascensor->id)
- ->where(function($query){
- if (Input::get('fechadesde') != "") {
- $date = DateTime::createFromFormat('d/m/Y', Input::get('fechadesde'));
- $query->where('fecha', '>=', $date->format('Y-m-d'));
- }
- if (Input::get('fechahasta') != "") {
- $date = DateTime::createFromFormat('d/m/Y', Input::get('fechahasta'));
- $query->where('fecha', '<=', $date->modify('+1 day')->format('Y-m-d'));
- }
- })
- ->orderBy('fecha', 'desc')
- ->paginate($mantenimientospage);
- }
- if (isset($mantenimientoexterno)) {
- $cliente = $mantenimientoexterno->cliente;
- $clienteid = $cliente->id;
- $mantenimientos = DB::table('mantenimientos')
- ->where('mantenimientoexterno_id', '=', $mantenimientoexterno->id)
- ->where(function($query){
- if (Input::get('fechadesde') != "") {
- $date = DateTime::createFromFormat('d/m/Y', Input::get('fechadesde'));
- $query->where('fecha', '>=', $date->format('Y-m-d'));
- }
- if (Input::get('fechahasta') != "") {
- $date = DateTime::createFromFormat('d/m/Y', Input::get('fechahasta'));
- $query->where('fecha', '<=', $date->modify('+1 day')->format('Y-m-d'));
- }
- })
- ->orderBy('fecha', 'desc')
- ->paginate($mantenimientospage);
- }
- if (!isset($ascensor) && !isset($mantenimientoexterno)) {
- $mantenimientos = Mantenimiento::where('id', '=', 0)->get();
- }
- return View::make('mantenimientos.listado')
- ->with('clienteid', $clienteid)
- ->with('input', $input)
- ->with('mantenimientos', $mantenimientos);
- }
- else{
- return Redirect::to('mantenimientos/');
- }
- }
- }