/app/controllers/ClientesController.php
PHP | 458 lines | 364 code | 66 blank | 28 comment | 25 complexity | 4a936d554b73220eee4ac0af61143a68 MD5 | raw file
- <?php
- class ClientesController extends BaseController {
- /**
- * Index de clientes
- *
- *
- */
- public function getIndex(){
- $clientespage = 20;
- if (Input::has('buscar')) {
- $clientespage = 200;
- $input = Input::all();
- $clientes = DB::table('clientes')->where('activated', '=', 1)
- ->where(function($query){
- $query->where('direccion', 'like', '%'.Input::get('buscar').'%')
- ->orWhere('ciudad', 'like', '%'.Input::get('buscar').'%')
- ->orWhere('nombre_contacto', 'like', '%'.Input::get('buscar').'%')
- ->orWhere('telefono_contacto', 'like', '%'.Input::get('buscar').'%')
- ->orWhere('email_contacto', 'like', '%'.Input::get('buscar').'%');
- })->orderBy('nombre_contacto', 'desc')
- ->paginate($clientespage);
- return View::make('clientes.index')
- ->with('input', $input)
- ->with('clientes',$clientes);
- }
- else{
- $clientes = DB::table('clientes')
- ->where('activated', '=', 1)
- ->paginate($clientespage);
- return View::make('clientes.index')
- ->with('clientes',$clientes);
- }
- }
- /**
- * Index de clientes desactivados
- *
- *
- */
- public function getDesactivados(){
- $clientespage = 20;
- if (Input::has('buscar')) {
- $clientespage = 200;
- $input = Input::all();
- $clientes = DB::table('clientes')->where('activated', '=', 0)
- ->where(function($query){
- $query->where('direccion', 'like', '%'.Input::get('buscar').'%')
- ->orWhere('ciudad', 'like', '%'.Input::get('buscar').'%')
- ->orWhere('nombre_contacto', 'like', '%'.Input::get('buscar').'%')
- ->orWhere('telefono_contacto', 'like', '%'.Input::get('buscar').'%')
- ->orWhere('email_contacto', 'like', '%'.Input::get('buscar').'%');
- })->orderBy('nombre_contacto', 'desc')
- ->paginate($clientespage);
- return View::make('clientes.desactivados')
- ->with('input', $input)
- ->with('clientes',$clientes);
- }
- else{
- $clientes = DB::table('clientes')
- ->where('activated', '=', 0)
- ->paginate($clientespage);
-
- return View::make('clientes.desactivados')
- ->with('clientes',$clientes);
- }
- }
- /**
- * Nuevo de clientes
- *
- *
- */
- public function getNuevo(){
- return View::make('clientes.nuevo');
- }
- /**
- * Post Nuevo de clientes sin añadir ascensor
- *
- *
- */
- public function postNuevo(){
- $input = Input::all();
- $v = Validator::make($input, Cliente::$rules, Cliente::$mensajes);
- $pulsado = $input['pulsado'];
- if ($v->fails() || $pulsado == 1) {
- return Redirect::to('clientes/nuevo')
- ->withErrors($v)
- ->with('input', $input);
- }
- else{
- //Comprobar que se ha guardado correctamente el cliente
- //Caso de exito al guardar se redirige a la ventana de consulta para poder añadir ascensores
- $cliente = new Cliente();
- $cliente->nombre_contacto = $input['nombrecontacto'];
- $cliente->telefono_contacto = $input['telefonocontacto'];
- $cliente->email_contacto = $input['emailcontacto'];
- $cliente->direccion = $input['direccion'];
- $cliente->numero = $input['numero'];
- $cliente->ciudad = $input['ciudad'];
- $cliente->provincia = $input['provincia'];
- $cliente->codigo_postal = $input['codigopostal'];
- $cliente->latitud = $input['lat'];
- $cliente->longitud = $input['lng'];
- $cliente->activated = 1;
- $result = $cliente->save();
- if($result){
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)));
- }
- else{
- return Redirect::to('clientes/')
- ->with('mok', 'AltaError');
- }
- }
- }
- public function postConsultar(){
- $input = Input::all();
- switch ($input['options']) {
- case 'Nuevo':
- return Redirect::to('ascensores/nuevo')
- ->with('idcliente', $input['id']);
- break;
- case 'Presupuesto':
- return Redirect::to('presupuestos/nuevo')
- ->with('idcliente', $input['id']);
- break;
- case 'Mantenimiento':
- return Redirect::to('mantenimientoexternos/nuevo')
- ->with('idcliente', $input['id']);
- break;
-
- default:
- return Redirect::to(URL::route('clientesconsultarget', array($input['id'])));
- break;
- }
- }
- public function postConsultarGenerarFactura(){
- $input = Input::all();
- $rutalogo = asset('images/logo_grande.jpg');
- $ascensor = Ascensor::where('id', '=', $input['hiddenid'])->first();
- $cliente = $ascensor->cliente;
- $detalles = DB::table('ascensor_producto')->where('ascensor_id', '=', $input['hiddenid'])->get();
- $medida = Medida::where('id', '=', $ascensor->medida_id)->first();
- $rutapdfcomprobacion = 'pdf/facturas/'.$ascensor->rae.'.pdf';
- if(file_exists($rutapdfcomprobacion)){
- unlink(public_path().'/pdf/facturas/'.$ascensor->rae.'.pdf');
- }
- try{
- ob_start();
- include(app_path().'/views/pdf/factura.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/facturas/'.$ascensor->rae.'.pdf','F');
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
- ->with('mok', 'Factura')
- ->with('id', $input['hiddenid']);
- }
- catch(HTML2PDF_exception $e) {
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
- ->with('mok', 'FacturaError');
- }
- }
- public function postConsultarGenerarPresupuesto(){
- $input = Input::all();
- $rutalogo = asset('images/logo_grande.jpg');
- $presupuesto = Presupuesto::where('id', '=', $input['hiddenid'])->first();
- $cliente = $presupuesto->cliente;
- $detalles = DB::table('presupuesto_producto')->where('presupuesto_id', '=', $input['hiddenid'])->get();
- $medida = Medida::where('id', '=', $presupuesto->medida_id)->first();
- $rutapdfcomprobacion = 'pdf/presupuestos/'.$presupuesto->id.'.pdf';
- if(file_exists($rutapdfcomprobacion)){
- unlink(public_path().'/pdf/presupuestos/'.$presupuesto->id.'.pdf');
- }
- try{
- ob_start();
- include(app_path().'/views/pdf/presupuesto.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/presupuestos/'.$presupuesto->id.'.pdf','F');
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
- ->with('mok', 'Presupuesto')
- ->with('id', $input['hiddenid']);
- }
- catch(HTML2PDF_exception $e) {
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
- ->with('mok', 'PresupuestoError');
- }
- }
- public function postConsultarGenerarMantenimiento(){
- $input = Input::all();
- $rutalogo = asset('images/logo_grande.jpg');
- $mantenimiento = Mantenimientoexterno::where('id', '=', $input['hiddenid'])->first();
- $cliente = $mantenimiento->cliente;
- $rutapdfcomprobacion = 'pdf/mantenimientos/'.$mantenimiento->rae.'.pdf';
- if(file_exists($rutapdfcomprobacion)){
- unlink(public_path().'/pdf/mantenimientos/'.$mantenimiento->rae.'.pdf');
- }
- try{
- ob_start();
- include(app_path().'/views/pdf/mantenimiento.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/mantenimientos/'.$mantenimiento->rae.'.pdf','F');
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
- ->with('mok', 'Mantenimiento')
- ->with('id', $input['hiddenid']);
- }
- catch(HTML2PDF_exception $e) {
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
- ->with('mok', 'MantenimientoError');
- }
- }
- public function postConsultarValidarMontaje(){
- $input = Input::all();
- $ultimomantenimiento = date('Y-m-d');
- $proximomantenimiento = date('Y-m-d', mktime(0, 0, 0, date("m")+1, date("d"), date("Y")));
- $ascensor = Ascensor::where('id', '=', $input['hidden_id'])->first();
- $ascensor->montado = 1;
- $ascensor->ultimo_mantenimiento = $ultimomantenimiento;
- $ascensor->proximo_mantenimiento = $proximomantenimiento;
- $result = $ascensor->save();
- if ($result) {
- $productos = $ascensor->productos;
- foreach ($productos as $producto) {
- $producto->instaladas = $producto->instaladas + 1;
- $producto->save();
- }
- return Redirect::to(URL::route('clientesconsultarget', array($ascensor->cliente_id)))
- ->with('mok', 'ValidarMontaje');
- } else {
- return Redirect::to(URL::route('clientesconsultarget', array($ascensor->cliente_id)))
- ->with('mok', 'ValidarMontajeError');
- }
-
- }
- public function postActivar(){
- $id = Input::get('hidden_id');
- $numcliente = Cliente::where('id', '=', $id)->count();
- if ($numcliente <= 0) {
- return Redirect::to('clientes/')
- ->with('mok', 'ActivarError');
- } else {
- $cliente = Cliente::where('id', '=', $id)->first();
- $cliente->activated = 1;
- $result = $cliente->save();
- if ($result) {
- $ascensores = $cliente->ascensores;
- foreach ($ascensores as $ascensor) {
- $ascensor->activated = 1;
- $ascensor->save();
- }
- $presupuestos = $cliente->presupuestos;
- foreach ($presupuestos as $presupuesto) {
- $presupuesto->activated = 1;
- $presupuesto->save();
- }
- $mantenimientos = $cliente->mantenimientoexternos;
- foreach ($mantenimientos as $mantenimiento) {
- $mantenimiento->activated = 1;
- $mantenimiento->save();
- }
- return Redirect::to('clientes/')
- ->with('mok', 'Activar');
- }
- else{
- return Redirect::to('clientes/')
- ->with('mok', 'ActivarError');
- }
- }
- }
- public function postDesactivar(){
- $id = Input::get('hidden_id');
- $numcliente = Cliente::where('id', '=', $id)->count();
- if ($numcliente <= 0) {
- return Redirect::to('clientes/')
- ->with('mok', 'DesactivarError');
- } else {
- $cliente = Cliente::where('id', '=', $id)->first();
- $cliente->activated = 0;
- $result = $cliente->save();
- if ($result) {
- $ascensores = $cliente->ascensores;
- foreach ($ascensores as $ascensor) {
- $ascensor->activated = 0;
- $ascensor->save();
- }
- $presupuestos = $cliente->presupuestos;
- foreach ($presupuestos as $presupuesto) {
- $presupuesto->activated = 0;
- $presupuesto->save();
- }
- $mantenimientos = $cliente->mantenimientoexternos;
- foreach ($mantenimientos as $mantenimiento) {
- $mantenimiento->activated = 0;
- $mantenimiento->save();
- }
- $descargas = $cliente->descargas;
- foreach ($descargas as $descarga) {
- if ($descarga->entregado == 0) {
- $descarga->delete();
- }
- }
- return Redirect::to('clientes/')
- ->with('mok', 'Desactivar');
- }
- else{
- return Redirect::to('clientes/')
- ->with('mok', 'DesactivarError');
- }
- }
- }
- public function postEditar(){
- $input = Input::all();
- $v = Validator::make($input, Cliente::$rules_editar, Cliente::$mensajes_editar);
- $pulsado = $input['pulsado'];
- if ($v->fails() || $pulsado == 1) {
- return Redirect::to('clientes/editar/'.$input['id'])
- ->withErrors($v)
- ->with('input', $input);
- }
- else{
- //Comprobar que se ha guardado correctamente el cliente
- //Caso de exito al guardar se redirige a la ventana de consulta para poder añadir ascensores
- $cliente = Cliente::where('id', '=', $input['id'])->first();
- $cliente->nombre_contacto = $input['nombrecontacto'];
- $cliente->telefono_contacto = $input['telefonocontacto'];
- $cliente->email_contacto = $input['emailcontacto'];
- $cliente->direccion = $input['direccion'];
- $cliente->numero = $input['numero'];
- $cliente->ciudad = $input['ciudad'];
- $cliente->provincia = $input['provincia'];
- $cliente->codigo_postal = $input['codigopostal'];
- $cliente->latitud = $input['lat'];
- $cliente->longitud = $input['lng'];
- $cliente->user_id = $input['idempleado'];
- $cliente->activated = 1;
- $result = $cliente->save();
- if($result){
- $ascensores = $cliente->ascensores;
- $mantenimientos = $cliente->mantenimientoexternos;
- foreach ($ascensores as $ascensor) {
- $ascensor->user_id = $input['idempleado'];
- $ascensor->save();
- }
- foreach ($mantenimientos as $mantenimiento) {
- $mantenimiento->user_id = $input['idempleado'];
- $mantenimiento->save();
- }
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
- ->with('mok', 'Editar');
- }
- else{
- return Redirect::to('clientes/')
- ->with('mok', 'EditarError');
- }
- }
- }
- public function postConsultarGenerarRae(){
- $input = Input::all();
- $rutalogo = asset('images/logo_grande.jpg');
- $rae = $input['hiddenrae'];
- $cliente = Cliente::where('id', '=', $input['hiddenid'])->first();
- $rutapdfcomprobacion = 'pdf/rae/'.$rae.'.pdf';
- if(file_exists($rutapdfcomprobacion)){
- unlink(public_path().'/pdf/rae/'.$rae.'.pdf');
- }
- try{
- ob_start();
- include(app_path().'/views/pdf/codigorae.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/rae/'.$rae.'.pdf','F');
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
- ->with('mok', 'RAE')
- ->with('rae', $rae);
- }
- catch(HTML2PDF_exception $e) {
- return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
- ->with('mok', 'RAE');
- }
- }
- }