PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/MantenimientosController.php

https://gitlab.com/daniruizcamacho/pfcascensores
PHP | 382 lines | 313 code | 66 blank | 3 comment | 47 complexity | 1f67e9078b95f221336264577b1f2f90 MD5 | raw file
  1. <?php
  2. class MantenimientosController extends BaseController {
  3. public function getIndex(){
  4. $ascensores = Ascensor::where('activated', '=', 1)
  5. ->where('baja', '=', 1)
  6. ->where('montado', '=', 1)
  7. ->orderBy('proximo_mantenimiento', 'asc')
  8. ->get();
  9. $mantenimientos = Mantenimientoexterno::where('activated', '=', 1)
  10. ->where('baja', '=', 1)
  11. ->orderBy('proximo_mantenimiento', 'asc')
  12. ->get();
  13. return View::make('mantenimientos.index')
  14. ->with('ascensores', $ascensores)
  15. ->with('mantenimientos', $mantenimientos);
  16. }
  17. public function getCompletados(){
  18. $mantenimientospage = 20;
  19. if (Input::has('texto') || Input::has('fechadesde') || Input::has('fechahasta')) {
  20. $mantenimientospage = 200;
  21. $input = Input::all();
  22. $flag = false;
  23. $v = Validator::make($input, Aviso::$rules_filtrar, Aviso::$mensajes_filtrar);
  24. if ($v->fails()) {
  25. return Redirect::to('mantenimientos/completados')
  26. ->withErrors($v);
  27. }
  28. if (Input::has('fechadesde') && Input::has('fechahasta')) {
  29. $datedesde = DateTime::createFromFormat('d/m/Y', Input::get('fechadesde'));
  30. $datehasta = DateTime::createFromFormat('d/m/Y', Input::get('fechahasta'));
  31. if ($datedesde > $datehasta) {
  32. $flag = true;
  33. }
  34. }
  35. if ($flag) {
  36. return Redirect::to('mantenimientos/completados')
  37. ->with('mok', 'FechaIncorrecta');
  38. }
  39. $clienteslist = DB::table('clientes')->where(function($query){
  40. $query->where('direccion', 'like', '%'.Input::get('texto').'%')
  41. ->orWhere('ciudad', 'like', '%'.Input::get('texto').'%')
  42. ->orWhere('nombre_contacto', 'like', '%'.Input::get('texto').'%')
  43. ->orWhere('telefono_contacto', 'like', '%'.Input::get('texto').'%')
  44. ->orWhere('email_contacto', 'like', '%'.Input::get('texto').'%');
  45. })->orderBy('nombre_contacto', 'desc')
  46. ->lists('id');
  47. if (count($clienteslist) > 0) {
  48. $ascensoreslistcli = DB::table('ascensores')->whereIn('cliente_id', $clienteslist)->lists('id');
  49. $mantenimientoexternoslistcli = DB::table('mantenimientoexternos')->whereIn('cliente_id', $clienteslist)->lists('id');
  50. } else {
  51. $ascensoreslistcli = DB::table('ascensores')->where('id', '=', 0)
  52. ->lists('id');
  53. $mantenimientoexternoslistcli = DB::table('mantenimientoexternos')->where('id', '=', 0)
  54. ->lists('id');
  55. }
  56. $ascensoreslist = Ascensor::where('rae', 'like', '%'.Input::get('texto').'%')->lists('id');
  57. $mantenimientoexternoslist = Mantenimientoexterno::where('rae', 'like', '%'.Input::get('texto').'%')->lists('id');
  58. $unionascensores = array_merge($ascensoreslist, $ascensoreslistcli);
  59. $resultascensores = array_unique($unionascensores);
  60. $unionmantenimientos = array_merge($mantenimientoexternoslist, $mantenimientoexternoslistcli);
  61. $resultmantenimientos = array_unique($unionmantenimientos);
  62. if (count($resultascensores) > 0 || count($resultmantenimientos) > 0) {
  63. if (count($resultascensores) > 0) {
  64. $mantenimientosascensor = DB::table('mantenimientos')
  65. ->whereIn('ascensor_id', $resultascensores)
  66. ->lists('id');
  67. }
  68. else{
  69. $mantenimientosascensor = DB::table('mantenimientos')
  70. ->where('id', '=', 0)
  71. ->lists('id');
  72. }
  73. if (count($resultmantenimientos) > 0) {
  74. $mantenimientosexternos= DB::table('mantenimientos')
  75. ->whereIn('mantenimientoexterno_id', $resultmantenimientos)
  76. ->lists('id');
  77. }
  78. else{
  79. $mantenimientosexternos= DB::table('mantenimientos')
  80. ->where('id', '=', 0)
  81. ->lists('id');
  82. }
  83. $union = array_merge($mantenimientosascensor, $mantenimientosexternos);
  84. $result = array_unique($union);
  85. if (count($result)) {
  86. $mantenimientos = DB::table('mantenimientos')
  87. ->whereIn('id', $result)
  88. ->where(function($query){
  89. if (Input::get('fechadesde') != "") {
  90. $date = DateTime::createFromFormat('d/m/Y', Input::get('fechadesde'));
  91. $query->where('fecha', '>=', $date->format('Y-m-d'));
  92. }
  93. if (Input::get('fechahasta') != "") {
  94. $date = DateTime::createFromFormat('d/m/Y', Input::get('fechahasta'));
  95. $query->where('fecha', '<=', $date->modify('+1 day')->format('Y-m-d'));
  96. }
  97. })
  98. ->orderBy('fecha', 'desc')
  99. ->paginate($mantenimientospage);
  100. } else {
  101. $mantenimientos = DB::table('mantenimientos')
  102. ->where('id', '=', 0)
  103. ->paginate($mantenimientospage);
  104. }
  105. } else {
  106. $mantenimientos = DB::table('mantenimientos')
  107. ->where('id', '=', 0)
  108. ->paginate($mantenimientospage);
  109. }
  110. return View::make('mantenimientos.completados')
  111. ->with('input', $input)
  112. ->with('mantenimientos', $mantenimientos);
  113. }
  114. else{
  115. $mantenimientos = DB::table('mantenimientos')
  116. ->orderBy('fecha', 'desc')
  117. ->paginate($mantenimientospage);
  118. }
  119. return View::make('mantenimientos.completados')
  120. ->with('mantenimientos', $mantenimientos);
  121. }
  122. public function postNuevo(){
  123. $input = Input::all();
  124. $v = Validator::make($input, Mantenimiento::$rules, Mantenimiento::$mensajes);
  125. if ($v->fails()) {
  126. return Redirect::to('mantenimientos/nuevo/'.$input['raeascensor'])
  127. ->withErrors($v)
  128. ->with('input', $input);
  129. }
  130. else{
  131. $date = date_create_from_format('d/m/Y H:i', $input['fecha']);
  132. $dateproximo = date_create_from_format('d/m/Y H:i', $input['fecha']);
  133. $intervalo = new DateInterval('P1M');
  134. $dateproximo->add($intervalo);
  135. $mantenimiento = new Mantenimiento();
  136. $mantenimiento->resultado = $input['resultado'];
  137. $mantenimiento->informe = $input['informe'];
  138. $mantenimiento->user_id = $input['empleado'];
  139. $mantenimiento->fecha = $date->format('Y-m-d H:i:s');
  140. if ($input['mantenimiento_id'] != 0) {
  141. $mantenimientoexterno = Mantenimientoexterno::where('id', '=', $input['mantenimiento_id'])->first();
  142. $mantenimientoexterno->ultimo_mantenimiento = $date->format('Y-m-d');
  143. $mantenimientoexterno->proximo_mantenimiento = $dateproximo->format('Y-m-d');
  144. $mantenimientoexterno->save();
  145. $mantenimiento->mantenimientoexterno_id = $mantenimientoexterno->id;
  146. $cliente = $mantenimientoexterno->cliente;
  147. } else {
  148. $ascensor = Ascensor::where('id', '=', $input['ascensor_id'])->first();
  149. $ascensor->ultimo_mantenimiento = $date->format('Y-m-d');
  150. $ascensor->proximo_mantenimiento = $dateproximo->format('Y-m-d');
  151. $ascensor->save();
  152. $mantenimiento->ascensor_id = $ascensor->id;
  153. $cliente = $ascensor->cliente;
  154. $medida = Medida::where('id', '=', $ascensor->medida_id)->first();
  155. }
  156. $mantenimiento->save();
  157. $rutalogo = asset('images/logo_grande.jpg');
  158. try{
  159. ob_start();
  160. include(app_path().'/views/pdf/informemantenimiento.php');
  161. $content = ob_get_clean();
  162. // convert to PDF
  163. require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
  164. $html2pdf = new HTML2PDF('P', 'A4', 'es');
  165. $html2pdf->pdf->SetDisplayMode('fullpage');
  166. $html2pdf->writeHTML($content);
  167. $html2pdf->Output(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf','F');
  168. }
  169. catch(HTML2PDF_exception $e) {
  170. }
  171. $mantenimiento->enviarcorreo();
  172. return Redirect::to('mantenimientos/')
  173. ->with('mok', 'Alta')
  174. ->with('id', $mantenimiento->id);
  175. }
  176. }
  177. public function postEditar(){
  178. $input = Input::all();
  179. $v = Validator::make($input, Mantenimiento::$rules, Mantenimiento::$mensajes);
  180. if ($v->fails()) {
  181. return Redirect::to('mantenimientos/editar/'.$input['idmantenimiento'])
  182. ->withErrors($v)
  183. ->with('input', $input);
  184. }
  185. else{
  186. $date = date_create_from_format('d/m/Y H:i', $input['fecha']);
  187. $dateproximo = date_create_from_format('d/m/Y H:i', $input['fecha']);
  188. $intervalo = new DateInterval('P1M');
  189. $dateproximo->add($intervalo);
  190. $mantenimiento = Mantenimiento::where('id', '=', $input['idmantenimiento'])->first();
  191. $mantenimiento->resultado = $input['resultado'];
  192. $mantenimiento->informe = $input['informe'];
  193. $mantenimiento->user_id = $input['empleado'];
  194. $mantenimiento->fecha = $date->format('Y-m-d H:i:s');
  195. $mantenimiento->save();
  196. if ($input['mantenimiento_id'] != 0) {
  197. $mantenimientoexterno = Mantenimientoexterno::where('id', '=', $input['mantenimiento_id'])->first();
  198. $cliente = $mantenimientoexterno->cliente;
  199. } else {
  200. $ascensor = Ascensor::where('id', '=', $input['ascensor_id'])->first();
  201. $cliente = $ascensor->cliente;
  202. $medida = Medida::where('id', '=', $ascensor->medida_id)->first();
  203. }
  204. $mantenimiento->save();
  205. $rutalogo = asset('images/logo_grande.jpg');
  206. $rutapdfcomprobacion = 'pdf/informemantenimiento/'.$mantenimiento->id.'.pdf';
  207. if(file_exists($rutapdfcomprobacion)){
  208. unlink(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf');
  209. }
  210. try{
  211. ob_start();
  212. include(app_path().'/views/pdf/informemantenimiento.php');
  213. $content = ob_get_clean();
  214. // convert to PDF
  215. require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
  216. $html2pdf = new HTML2PDF('P', 'A4', 'es');
  217. $html2pdf->pdf->SetDisplayMode('fullpage');
  218. $html2pdf->writeHTML($content);
  219. $html2pdf->Output(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf','F');
  220. }
  221. catch(HTML2PDF_exception $e) {
  222. }
  223. return Redirect::to('mantenimientos/completados')
  224. ->with('mok', 'Editar')
  225. ->with('id', $mantenimiento->id);
  226. }
  227. }
  228. public function postGenerarPdf(){
  229. $input = Input::all();
  230. $mantenimiento = Mantenimiento::where('id', '=', $input['hiddenid'])->first();
  231. if ($mantenimiento->mantenimientoexterno_id != 0) {
  232. $mantenimientoexterno = Mantenimientoexterno::where('id', '=', $mantenimiento->mantenimientoexterno_id)->first();
  233. $cliente = $mantenimientoexterno->cliente;
  234. } else {
  235. $ascensor = Ascensor::where('id', '=', $mantenimiento->ascensor_id)->first();
  236. $cliente = $ascensor->cliente;
  237. $medida = Medida::where('id', '=', $ascensor->medida_id)->first();
  238. }
  239. $rutalogo = asset('images/logo_grande.jpg');
  240. $rutapdfcomprobacion = 'pdf/informemantenimiento/'.$mantenimiento->id.'.pdf';
  241. if(file_exists($rutapdfcomprobacion)){
  242. unlink(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf');
  243. }
  244. try{
  245. ob_start();
  246. include(app_path().'/views/pdf/informemantenimiento.php');
  247. $content = ob_get_clean();
  248. // convert to PDF
  249. require_once(public_path().'/packages/html2pdf/html2pdf.class.php');
  250. $html2pdf = new HTML2PDF('P', 'A4', 'es');
  251. $html2pdf->pdf->SetDisplayMode('fullpage');
  252. $html2pdf->writeHTML($content);
  253. $html2pdf->Output(public_path().'/pdf/informemantenimiento/'.$mantenimiento->id.'.pdf','F');
  254. }
  255. catch(HTML2PDF_exception $e) {
  256. }
  257. return Redirect::to('mantenimientos/completados')
  258. ->with('mok', 'GenerarPdf')
  259. ->with('id', $mantenimiento->id);
  260. }
  261. public function getListado(){
  262. $mantenimientospage = 20;
  263. $input = Input::all();
  264. $clienteid = 0;
  265. if (Input::has('raeascensor')){
  266. $ascensor = Ascensor::where('rae', '=', Input::get('raeascensor'))->first();
  267. $mantenimientoexterno = Mantenimientoexterno::where('rae', '=', Input::get('raeascensor'))->first();
  268. if (isset($ascensor)) {
  269. $cliente = $ascensor->cliente;
  270. $clienteid = $cliente->id;
  271. $mantenimientos = DB::table('mantenimientos')
  272. ->where('ascensor_id', '=', $ascensor->id)
  273. ->where(function($query){
  274. if (Input::get('fechadesde') != "") {
  275. $date = DateTime::createFromFormat('d/m/Y', Input::get('fechadesde'));
  276. $query->where('fecha', '>=', $date->format('Y-m-d'));
  277. }
  278. if (Input::get('fechahasta') != "") {
  279. $date = DateTime::createFromFormat('d/m/Y', Input::get('fechahasta'));
  280. $query->where('fecha', '<=', $date->modify('+1 day')->format('Y-m-d'));
  281. }
  282. })
  283. ->orderBy('fecha', 'desc')
  284. ->paginate($mantenimientospage);
  285. }
  286. if (isset($mantenimientoexterno)) {
  287. $cliente = $mantenimientoexterno->cliente;
  288. $clienteid = $cliente->id;
  289. $mantenimientos = DB::table('mantenimientos')
  290. ->where('mantenimientoexterno_id', '=', $mantenimientoexterno->id)
  291. ->where(function($query){
  292. if (Input::get('fechadesde') != "") {
  293. $date = DateTime::createFromFormat('d/m/Y', Input::get('fechadesde'));
  294. $query->where('fecha', '>=', $date->format('Y-m-d'));
  295. }
  296. if (Input::get('fechahasta') != "") {
  297. $date = DateTime::createFromFormat('d/m/Y', Input::get('fechahasta'));
  298. $query->where('fecha', '<=', $date->modify('+1 day')->format('Y-m-d'));
  299. }
  300. })
  301. ->orderBy('fecha', 'desc')
  302. ->paginate($mantenimientospage);
  303. }
  304. if (!isset($ascensor) && !isset($mantenimientoexterno)) {
  305. $mantenimientos = Mantenimiento::where('id', '=', 0)->get();
  306. }
  307. return View::make('mantenimientos.listado')
  308. ->with('clienteid', $clienteid)
  309. ->with('input', $input)
  310. ->with('mantenimientos', $mantenimientos);
  311. }
  312. else{
  313. return Redirect::to('mantenimientos/');
  314. }
  315. }
  316. }