PageRenderTime 70ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/ClientesController.php

https://gitlab.com/daniruizcamacho/pfcascensores
PHP | 458 lines | 364 code | 66 blank | 28 comment | 25 complexity | 4a936d554b73220eee4ac0af61143a68 MD5 | raw file
  1. <?php
  2. class ClientesController extends BaseController {
  3. /**
  4. * Index de clientes
  5. *
  6. *
  7. */
  8. public function getIndex(){
  9. $clientespage = 20;
  10. if (Input::has('buscar')) {
  11. $clientespage = 200;
  12. $input = Input::all();
  13. $clientes = DB::table('clientes')->where('activated', '=', 1)
  14. ->where(function($query){
  15. $query->where('direccion', 'like', '%'.Input::get('buscar').'%')
  16. ->orWhere('ciudad', 'like', '%'.Input::get('buscar').'%')
  17. ->orWhere('nombre_contacto', 'like', '%'.Input::get('buscar').'%')
  18. ->orWhere('telefono_contacto', 'like', '%'.Input::get('buscar').'%')
  19. ->orWhere('email_contacto', 'like', '%'.Input::get('buscar').'%');
  20. })->orderBy('nombre_contacto', 'desc')
  21. ->paginate($clientespage);
  22. return View::make('clientes.index')
  23. ->with('input', $input)
  24. ->with('clientes',$clientes);
  25. }
  26. else{
  27. $clientes = DB::table('clientes')
  28. ->where('activated', '=', 1)
  29. ->paginate($clientespage);
  30. return View::make('clientes.index')
  31. ->with('clientes',$clientes);
  32. }
  33. }
  34. /**
  35. * Index de clientes desactivados
  36. *
  37. *
  38. */
  39. public function getDesactivados(){
  40. $clientespage = 20;
  41. if (Input::has('buscar')) {
  42. $clientespage = 200;
  43. $input = Input::all();
  44. $clientes = DB::table('clientes')->where('activated', '=', 0)
  45. ->where(function($query){
  46. $query->where('direccion', 'like', '%'.Input::get('buscar').'%')
  47. ->orWhere('ciudad', 'like', '%'.Input::get('buscar').'%')
  48. ->orWhere('nombre_contacto', 'like', '%'.Input::get('buscar').'%')
  49. ->orWhere('telefono_contacto', 'like', '%'.Input::get('buscar').'%')
  50. ->orWhere('email_contacto', 'like', '%'.Input::get('buscar').'%');
  51. })->orderBy('nombre_contacto', 'desc')
  52. ->paginate($clientespage);
  53. return View::make('clientes.desactivados')
  54. ->with('input', $input)
  55. ->with('clientes',$clientes);
  56. }
  57. else{
  58. $clientes = DB::table('clientes')
  59. ->where('activated', '=', 0)
  60. ->paginate($clientespage);
  61. return View::make('clientes.desactivados')
  62. ->with('clientes',$clientes);
  63. }
  64. }
  65. /**
  66. * Nuevo de clientes
  67. *
  68. *
  69. */
  70. public function getNuevo(){
  71. return View::make('clientes.nuevo');
  72. }
  73. /**
  74. * Post Nuevo de clientes sin añadir ascensor
  75. *
  76. *
  77. */
  78. public function postNuevo(){
  79. $input = Input::all();
  80. $v = Validator::make($input, Cliente::$rules, Cliente::$mensajes);
  81. $pulsado = $input['pulsado'];
  82. if ($v->fails() || $pulsado == 1) {
  83. return Redirect::to('clientes/nuevo')
  84. ->withErrors($v)
  85. ->with('input', $input);
  86. }
  87. else{
  88. //Comprobar que se ha guardado correctamente el cliente
  89. //Caso de exito al guardar se redirige a la ventana de consulta para poder añadir ascensores
  90. $cliente = new Cliente();
  91. $cliente->nombre_contacto = $input['nombrecontacto'];
  92. $cliente->telefono_contacto = $input['telefonocontacto'];
  93. $cliente->email_contacto = $input['emailcontacto'];
  94. $cliente->direccion = $input['direccion'];
  95. $cliente->numero = $input['numero'];
  96. $cliente->ciudad = $input['ciudad'];
  97. $cliente->provincia = $input['provincia'];
  98. $cliente->codigo_postal = $input['codigopostal'];
  99. $cliente->latitud = $input['lat'];
  100. $cliente->longitud = $input['lng'];
  101. $cliente->activated = 1;
  102. $result = $cliente->save();
  103. if($result){
  104. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)));
  105. }
  106. else{
  107. return Redirect::to('clientes/')
  108. ->with('mok', 'AltaError');
  109. }
  110. }
  111. }
  112. public function postConsultar(){
  113. $input = Input::all();
  114. switch ($input['options']) {
  115. case 'Nuevo':
  116. return Redirect::to('ascensores/nuevo')
  117. ->with('idcliente', $input['id']);
  118. break;
  119. case 'Presupuesto':
  120. return Redirect::to('presupuestos/nuevo')
  121. ->with('idcliente', $input['id']);
  122. break;
  123. case 'Mantenimiento':
  124. return Redirect::to('mantenimientoexternos/nuevo')
  125. ->with('idcliente', $input['id']);
  126. break;
  127. default:
  128. return Redirect::to(URL::route('clientesconsultarget', array($input['id'])));
  129. break;
  130. }
  131. }
  132. public function postConsultarGenerarFactura(){
  133. $input = Input::all();
  134. $rutalogo = asset('images/logo_grande.jpg');
  135. $ascensor = Ascensor::where('id', '=', $input['hiddenid'])->first();
  136. $cliente = $ascensor->cliente;
  137. $detalles = DB::table('ascensor_producto')->where('ascensor_id', '=', $input['hiddenid'])->get();
  138. $medida = Medida::where('id', '=', $ascensor->medida_id)->first();
  139. $rutapdfcomprobacion = 'pdf/facturas/'.$ascensor->rae.'.pdf';
  140. if(file_exists($rutapdfcomprobacion)){
  141. unlink(public_path().'/pdf/facturas/'.$ascensor->rae.'.pdf');
  142. }
  143. try{
  144. ob_start();
  145. include(app_path().'/views/pdf/factura.php');
  146. $content = ob_get_clean();
  147. // convert to PDF
  148. require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
  149. $html2pdf = new HTML2PDF('P', 'A4', 'es');
  150. $html2pdf->pdf->SetDisplayMode('fullpage');
  151. $html2pdf->writeHTML($content);
  152. $html2pdf->Output(public_path().'/pdf/facturas/'.$ascensor->rae.'.pdf','F');
  153. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
  154. ->with('mok', 'Factura')
  155. ->with('id', $input['hiddenid']);
  156. }
  157. catch(HTML2PDF_exception $e) {
  158. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
  159. ->with('mok', 'FacturaError');
  160. }
  161. }
  162. public function postConsultarGenerarPresupuesto(){
  163. $input = Input::all();
  164. $rutalogo = asset('images/logo_grande.jpg');
  165. $presupuesto = Presupuesto::where('id', '=', $input['hiddenid'])->first();
  166. $cliente = $presupuesto->cliente;
  167. $detalles = DB::table('presupuesto_producto')->where('presupuesto_id', '=', $input['hiddenid'])->get();
  168. $medida = Medida::where('id', '=', $presupuesto->medida_id)->first();
  169. $rutapdfcomprobacion = 'pdf/presupuestos/'.$presupuesto->id.'.pdf';
  170. if(file_exists($rutapdfcomprobacion)){
  171. unlink(public_path().'/pdf/presupuestos/'.$presupuesto->id.'.pdf');
  172. }
  173. try{
  174. ob_start();
  175. include(app_path().'/views/pdf/presupuesto.php');
  176. $content = ob_get_clean();
  177. // convert to PDF
  178. require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
  179. $html2pdf = new HTML2PDF('P', 'A4', 'es');
  180. $html2pdf->pdf->SetDisplayMode('fullpage');
  181. $html2pdf->writeHTML($content);
  182. $html2pdf->Output(public_path().'/pdf/presupuestos/'.$presupuesto->id.'.pdf','F');
  183. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
  184. ->with('mok', 'Presupuesto')
  185. ->with('id', $input['hiddenid']);
  186. }
  187. catch(HTML2PDF_exception $e) {
  188. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
  189. ->with('mok', 'PresupuestoError');
  190. }
  191. }
  192. public function postConsultarGenerarMantenimiento(){
  193. $input = Input::all();
  194. $rutalogo = asset('images/logo_grande.jpg');
  195. $mantenimiento = Mantenimientoexterno::where('id', '=', $input['hiddenid'])->first();
  196. $cliente = $mantenimiento->cliente;
  197. $rutapdfcomprobacion = 'pdf/mantenimientos/'.$mantenimiento->rae.'.pdf';
  198. if(file_exists($rutapdfcomprobacion)){
  199. unlink(public_path().'/pdf/mantenimientos/'.$mantenimiento->rae.'.pdf');
  200. }
  201. try{
  202. ob_start();
  203. include(app_path().'/views/pdf/mantenimiento.php');
  204. $content = ob_get_clean();
  205. // convert to PDF
  206. require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
  207. $html2pdf = new HTML2PDF('P', 'A4', 'es');
  208. $html2pdf->pdf->SetDisplayMode('fullpage');
  209. $html2pdf->writeHTML($content);
  210. $html2pdf->Output(public_path().'/pdf/mantenimientos/'.$mantenimiento->rae.'.pdf','F');
  211. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
  212. ->with('mok', 'Mantenimiento')
  213. ->with('id', $input['hiddenid']);
  214. }
  215. catch(HTML2PDF_exception $e) {
  216. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
  217. ->with('mok', 'MantenimientoError');
  218. }
  219. }
  220. public function postConsultarValidarMontaje(){
  221. $input = Input::all();
  222. $ultimomantenimiento = date('Y-m-d');
  223. $proximomantenimiento = date('Y-m-d', mktime(0, 0, 0, date("m")+1, date("d"), date("Y")));
  224. $ascensor = Ascensor::where('id', '=', $input['hidden_id'])->first();
  225. $ascensor->montado = 1;
  226. $ascensor->ultimo_mantenimiento = $ultimomantenimiento;
  227. $ascensor->proximo_mantenimiento = $proximomantenimiento;
  228. $result = $ascensor->save();
  229. if ($result) {
  230. $productos = $ascensor->productos;
  231. foreach ($productos as $producto) {
  232. $producto->instaladas = $producto->instaladas + 1;
  233. $producto->save();
  234. }
  235. return Redirect::to(URL::route('clientesconsultarget', array($ascensor->cliente_id)))
  236. ->with('mok', 'ValidarMontaje');
  237. } else {
  238. return Redirect::to(URL::route('clientesconsultarget', array($ascensor->cliente_id)))
  239. ->with('mok', 'ValidarMontajeError');
  240. }
  241. }
  242. public function postActivar(){
  243. $id = Input::get('hidden_id');
  244. $numcliente = Cliente::where('id', '=', $id)->count();
  245. if ($numcliente <= 0) {
  246. return Redirect::to('clientes/')
  247. ->with('mok', 'ActivarError');
  248. } else {
  249. $cliente = Cliente::where('id', '=', $id)->first();
  250. $cliente->activated = 1;
  251. $result = $cliente->save();
  252. if ($result) {
  253. $ascensores = $cliente->ascensores;
  254. foreach ($ascensores as $ascensor) {
  255. $ascensor->activated = 1;
  256. $ascensor->save();
  257. }
  258. $presupuestos = $cliente->presupuestos;
  259. foreach ($presupuestos as $presupuesto) {
  260. $presupuesto->activated = 1;
  261. $presupuesto->save();
  262. }
  263. $mantenimientos = $cliente->mantenimientoexternos;
  264. foreach ($mantenimientos as $mantenimiento) {
  265. $mantenimiento->activated = 1;
  266. $mantenimiento->save();
  267. }
  268. return Redirect::to('clientes/')
  269. ->with('mok', 'Activar');
  270. }
  271. else{
  272. return Redirect::to('clientes/')
  273. ->with('mok', 'ActivarError');
  274. }
  275. }
  276. }
  277. public function postDesactivar(){
  278. $id = Input::get('hidden_id');
  279. $numcliente = Cliente::where('id', '=', $id)->count();
  280. if ($numcliente <= 0) {
  281. return Redirect::to('clientes/')
  282. ->with('mok', 'DesactivarError');
  283. } else {
  284. $cliente = Cliente::where('id', '=', $id)->first();
  285. $cliente->activated = 0;
  286. $result = $cliente->save();
  287. if ($result) {
  288. $ascensores = $cliente->ascensores;
  289. foreach ($ascensores as $ascensor) {
  290. $ascensor->activated = 0;
  291. $ascensor->save();
  292. }
  293. $presupuestos = $cliente->presupuestos;
  294. foreach ($presupuestos as $presupuesto) {
  295. $presupuesto->activated = 0;
  296. $presupuesto->save();
  297. }
  298. $mantenimientos = $cliente->mantenimientoexternos;
  299. foreach ($mantenimientos as $mantenimiento) {
  300. $mantenimiento->activated = 0;
  301. $mantenimiento->save();
  302. }
  303. $descargas = $cliente->descargas;
  304. foreach ($descargas as $descarga) {
  305. if ($descarga->entregado == 0) {
  306. $descarga->delete();
  307. }
  308. }
  309. return Redirect::to('clientes/')
  310. ->with('mok', 'Desactivar');
  311. }
  312. else{
  313. return Redirect::to('clientes/')
  314. ->with('mok', 'DesactivarError');
  315. }
  316. }
  317. }
  318. public function postEditar(){
  319. $input = Input::all();
  320. $v = Validator::make($input, Cliente::$rules_editar, Cliente::$mensajes_editar);
  321. $pulsado = $input['pulsado'];
  322. if ($v->fails() || $pulsado == 1) {
  323. return Redirect::to('clientes/editar/'.$input['id'])
  324. ->withErrors($v)
  325. ->with('input', $input);
  326. }
  327. else{
  328. //Comprobar que se ha guardado correctamente el cliente
  329. //Caso de exito al guardar se redirige a la ventana de consulta para poder añadir ascensores
  330. $cliente = Cliente::where('id', '=', $input['id'])->first();
  331. $cliente->nombre_contacto = $input['nombrecontacto'];
  332. $cliente->telefono_contacto = $input['telefonocontacto'];
  333. $cliente->email_contacto = $input['emailcontacto'];
  334. $cliente->direccion = $input['direccion'];
  335. $cliente->numero = $input['numero'];
  336. $cliente->ciudad = $input['ciudad'];
  337. $cliente->provincia = $input['provincia'];
  338. $cliente->codigo_postal = $input['codigopostal'];
  339. $cliente->latitud = $input['lat'];
  340. $cliente->longitud = $input['lng'];
  341. $cliente->user_id = $input['idempleado'];
  342. $cliente->activated = 1;
  343. $result = $cliente->save();
  344. if($result){
  345. $ascensores = $cliente->ascensores;
  346. $mantenimientos = $cliente->mantenimientoexternos;
  347. foreach ($ascensores as $ascensor) {
  348. $ascensor->user_id = $input['idempleado'];
  349. $ascensor->save();
  350. }
  351. foreach ($mantenimientos as $mantenimiento) {
  352. $mantenimiento->user_id = $input['idempleado'];
  353. $mantenimiento->save();
  354. }
  355. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
  356. ->with('mok', 'Editar');
  357. }
  358. else{
  359. return Redirect::to('clientes/')
  360. ->with('mok', 'EditarError');
  361. }
  362. }
  363. }
  364. public function postConsultarGenerarRae(){
  365. $input = Input::all();
  366. $rutalogo = asset('images/logo_grande.jpg');
  367. $rae = $input['hiddenrae'];
  368. $cliente = Cliente::where('id', '=', $input['hiddenid'])->first();
  369. $rutapdfcomprobacion = 'pdf/rae/'.$rae.'.pdf';
  370. if(file_exists($rutapdfcomprobacion)){
  371. unlink(public_path().'/pdf/rae/'.$rae.'.pdf');
  372. }
  373. try{
  374. ob_start();
  375. include(app_path().'/views/pdf/codigorae.php');
  376. $content = ob_get_clean();
  377. // convert to PDF
  378. require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
  379. $html2pdf = new HTML2PDF('P', 'A4', 'es');
  380. $html2pdf->pdf->SetDisplayMode('fullpage');
  381. $html2pdf->writeHTML($content);
  382. $html2pdf->Output(public_path().'/pdf/rae/'.$rae.'.pdf','F');
  383. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
  384. ->with('mok', 'RAE')
  385. ->with('rae', $rae);
  386. }
  387. catch(HTML2PDF_exception $e) {
  388. return Redirect::to(URL::route('clientesconsultarget', array($cliente->id)))
  389. ->with('mok', 'RAE');
  390. }
  391. }
  392. }