From e7637857f4faebb3f98877334c1c772c223d4013 Mon Sep 17 00:00:00 2001 From: elyambay Date: Wed, 12 Sep 2018 23:30:19 -0400 Subject: [PATCH] Feature Informe adjunto --- app/File_attachment.php | 16 +++ app/Http/Controllers/FichasController.php | 39 +++--- .../Controllers/FileAttachmentController.php | 128 ++++++++++++++++++ app/Http/Controllers/ReporteController.php | 33 +++-- app/Traits/PeriodosTrait.php | 28 ++++ ...12_160252_create_file_attachment_table.php | 41 ++++++ resources/views/fichas/create.blade.php | 21 ++- resources/views/fichas/edit.blade.php | 55 +++----- .../views/file_attachment/index.blade.php | 93 +++++++++++++ .../views/file_attachment/notfound.blade.php | 18 +++ resources/views/layout.blade.php | 100 ++++++++------ resources/views/report/ficha.blade.php | 2 +- resources/views/report/layout.blade.php | 3 + routes/web.php | 6 + 14 files changed, 466 insertions(+), 117 deletions(-) create mode 100644 app/File_attachment.php create mode 100644 app/Http/Controllers/FileAttachmentController.php create mode 100644 app/Traits/PeriodosTrait.php create mode 100644 database/migrations/2018_09_12_160252_create_file_attachment_table.php create mode 100644 resources/views/file_attachment/index.blade.php create mode 100644 resources/views/file_attachment/notfound.blade.php diff --git a/app/File_attachment.php b/app/File_attachment.php new file mode 100644 index 0000000..2db43fc --- /dev/null +++ b/app/File_attachment.php @@ -0,0 +1,16 @@ +belongsTo('App\Rubro'); + } +} diff --git a/app/Http/Controllers/FichasController.php b/app/Http/Controllers/FichasController.php index a5da318..018feed 100644 --- a/app/Http/Controllers/FichasController.php +++ b/app/Http/Controllers/FichasController.php @@ -7,25 +7,22 @@ use App\Ficha_dato; use App\Cabecera_encuesta; use App\Rubro; +use App\Traits\PeriodosTrait; use flash; class FichasController extends Controller { + use PeriodosTrait; + public function index(){ - dd("holo"); $dbData = Ficha_dato::get(); return view('fichas.list')->with('dbData', $dbData); } public function create(){ - $periodos = Cabecera_encuesta::get(); - $periodos = $periodos->map(function($item){ - $rubro = $item->rubro->descripcion; - $periodo = $item->periodo; - $item['periodo_combo'] = $periodo.' - '.$rubro; - return $item; - })->unique('periodo')->pluck('periodo_combo', 'periodo'); - $rubros = Rubro::all()->pluck('descripcion', 'id'); + $rubro = Rubro::first()->id; + $periodos = $this->getPeriodos($rubro); + $rubros = $this->getRubros(); return view('fichas.create')->with('periodos', $periodos) ->with('rubros', $rubros); } @@ -43,25 +40,21 @@ public function show($id) } public function edit($id){ - $dbData = Cargo::find($id); - $dbNivel = Nivel::all()->pluck('descripcion', 'id'); - $dbArea = Area::all()->pluck('descripcion', 'id'); - return view('cargos.edit')->with('dbData', $dbData) - ->with('dbNivel', $dbNivel) - ->with('dbArea', $dbArea); + $dbData = Ficha_dato::find($id); + $rubro = Rubro::get()->first()->id; + $periodos = $this->getPeriodos($rubro); + $rubros = $this->getRubros(); + return view('fichas.create')->with('dbData', $dbData) + ->with('periodos', $periodos) + ->with('rubros', $rubros); } public function update(Request $request, $id){ - $dbData = Cargo::find($id); - $dbData->fill($request->all()); - if(!is_null($request->is_temporal)){ - $dbData->is_temporal = 1; - }else{ - $dbData->is_temporal = 0; - } + $dbData = new Ficha_dato($request->all()); + $dbData->save(); - return redirect()->route('cargos.index'); + return redirect()->route('admin_ficha.index'); } public function destroy($id){ diff --git a/app/Http/Controllers/FileAttachmentController.php b/app/Http/Controllers/FileAttachmentController.php new file mode 100644 index 0000000..ceb5805 --- /dev/null +++ b/app/Http/Controllers/FileAttachmentController.php @@ -0,0 +1,128 @@ +getRubros(); + // recuperamos todos los rubros + $rubro = Rubro::all()->first()->id; + $periodos = $this->getPeriodos($rubro); + // Si terminó de subir el archivo mostramos el toast correspondiente + $toast = session('upload_done'); + if($toast){ + session()->forget('upload_done'); + } + $errorUploading = session('upload_nok'); + if($errorUploading){ + session()->forget('upload_nok'); + } + + return view('file_attachment.index')->with('periodos', $periodos) + ->with('rubros', $rubros) + ->with('toast', $toast) + ->with('errorUploading', $errorUploading); + } + + + public function download(){ + $empresa = Auth::user()->empresa; + $id = $empresa->id; + if(Session::has('periodo')){ + $per = Session::get('periodo'); + $dbEncuesta = Cabecera_encuesta::where('empresa_id', $id)->whereRaw("periodo = '". $per."'")->first(); + }else{ + $dbEncuesta = Cabecera_encuesta::where('empresa_id', $id)->whereRaw('id = (select max(id) from cabecera_encuestas where empresa_id = '. $id.')')->first(); + } + $rubro = $empresa->rubro_id; + $periodo = $dbEncuesta->periodo; + $dbFile = File_attachment::where('rubro_id', $rubro)->where('periodo', $periodo)->first(); + if($dbFile){ + $filename = $dbFile->filename; + try{ + return response()->download(storage_path('app/public/uploads/'.$filename), $filename, []); + }catch(\Exception $exception){ + return view('file_attachment.notfound'); + } + + }else{ + return view('file_attachment.notfound'); + } + } + + public function upload(Request $request){ + //dd($request->all()); + // recuperamos el archivo + $file = $request->file('file'); + // validación + $rules = array( 'file' => 'required|mimes:xls,xlsx,pdf'); + $messages = array( 'file.required' => 'No se especificó ningún archivo para subir', + 'file.mimes' => 'El tipo de archivo no es correcto'); + + $validator = Validator::make($request->all(), $rules, $messages); + + if($validator->passes()){ + $filename = $file->getClientOriginalName(); + $rubro = $request->rubro_id; + $periodo = $request->periodo; + try{ + $file->storeAs('public/uploads', $filename); + $dbData = File_attachment::where('rubro_id', $rubro)->where('periodo', $periodo)->first(); + if(!$dbData){ + $dbData = new File_attachment(); + } + $dbData->rubro_id = $rubro; + $dbData->filename = $filename; + $dbData->periodo = $periodo; + $dbData->activo = 1; + $dbData->save(); + }catch(\Exception $exception){ + session(['upload_nok'=>'true']); + redirect()->back()->withInput(); + } + + }else{ + session(['upload_nok'=>'true']); + redirect()->back()->withInput(); + } + session(['upload_done'=>'true']); + return redirect()->route('file_attachment'); + + } + + public function getPeriodosAjax(Request $request){ + + $periodos = $this->getPeriodos($request->rubro_id); + + return $periodos; + } + +} diff --git a/app/Http/Controllers/ReporteController.php b/app/Http/Controllers/ReporteController.php index 022635c..49d6e8f 100644 --- a/app/Http/Controllers/ReporteController.php +++ b/app/Http/Controllers/ReporteController.php @@ -9,6 +9,7 @@ use App\Detalle_encuesta; use App\Cargos_rubro; use App\Empresa; +use App\Ficha_dato; use App\Nivel; use App\Cargo; use App\Rubro; @@ -230,21 +231,28 @@ public function ficha($id){ $dbEncuesta = Cabecera_encuesta::where('empresa_id', $id)->whereRaw('id = (select max(id) from cabecera_encuestas where empresa_id = '. $id.')')->first(); } $cargos = Encuestas_cargo::where('cabecera_encuesta_id', $dbEncuesta->id)->get()->count(); - if ($rubro == 4){ - if($per == "12/2016"){ - $cargos = 160; - }else{ - $cargos = 174; + $periodo = $dbEncuesta->periodo; + $dbFicha = Ficha_dato::where('rubro_id', $rubro)->where('periodo', $periodo)->first(); + if($dbFicha){ + $cargos = $dbFicha->cargos_emergentes; + $tipoCambio = $dbFicha = $dbFicha->tipo_cambio; + }else{ + if ($rubro == 4){ + if($per == "12/2016"){ + $cargos = 160; + }else{ + $cargos = 174; + } + }elseif($rubro == 1){ + $cargos = 400; + }elseif($rubro == 2){ + $cargos = 172; + }elseif($rubro == 3){ + $cargos = 175; } - }elseif($rubro == 1){ - $cargos = 400; - }elseif($rubro == 2){ - $cargos = 172; - }elseif($rubro == 3){ - $cargos = 175; + $tipoCambio = 5600; } - $periodo = $dbEncuesta->periodo; $participantes = Cabecera_encuesta::where('periodo', $periodo)->where('rubro_id', $rubro)->get(); $participantes = $participantes->map(function($item){ @@ -260,6 +268,7 @@ public function ficha($id){ ->with('cargos', $cargos) ->with('periodo', $periodo) ->with('club', $club) + ->with('tipoCambio', $tipoCambio) ->with('participantes', $participantes); } diff --git a/app/Traits/PeriodosTrait.php b/app/Traits/PeriodosTrait.php new file mode 100644 index 0000000..3ee238e --- /dev/null +++ b/app/Traits/PeriodosTrait.php @@ -0,0 +1,28 @@ +pluck('descripcion', 'id'); + return $rubros; + } + + public function getPeriodos($id){ + // recuperamos todas las encuestas del primer rubro recuperado + $periodos = Cabecera_encuesta::where('rubro_id', $id)->get(); + // filtramos los periodos para el primer rubro recuperado + $periodos = $periodos->map(function($item){ + $rubro = $item->rubro->descripcion; + $periodo = $item->periodo; + $item['periodo_combo'] = $periodo.' - '.$rubro; + return $item; + })->unique('periodo')->pluck('periodo_combo', 'periodo'); + + return $periodos; + } +} \ No newline at end of file diff --git a/database/migrations/2018_09_12_160252_create_file_attachment_table.php b/database/migrations/2018_09_12_160252_create_file_attachment_table.php new file mode 100644 index 0000000..7f1c317 --- /dev/null +++ b/database/migrations/2018_09_12_160252_create_file_attachment_table.php @@ -0,0 +1,41 @@ +increments('id'); + $table->integer('rubro_id')->unsigned(); + $table->string('periodo'); + $table->string('filename'); + $table->integer('activo'); + $table->timestamps(); + + $table->foreign('rubro_id') + ->references('id') + ->on('rubros') + ->onDelete('restrict') + ->onUpdate('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/resources/views/fichas/create.blade.php b/resources/views/fichas/create.blade.php index 852e840..01f1bb5 100644 --- a/resources/views/fichas/create.blade.php +++ b/resources/views/fichas/create.blade.php @@ -10,7 +10,6 @@
- @foreach($periodos as $id => $descripcion) @endforeach @@ -67,6 +65,25 @@ });*/ }); + $("#rubro_id").change(function(){ + var selectPeriodo = $("#periodo"); + var rubroId = $(this).val(); + selectPeriodo.empty(); + $.post('{{route('file_attachment.periodos')}}', {"rubro_id": rubroId, "_token": "{{csrf_token()}}"}, + function(json){ + var data = $.map(json, function(text, id){ + return {text:text, id:id}; + }); + for(i = 0; i < data.length; i++){ + selectPeriodo.append( + $("").attr("value", data[i].id) + .text(data[i].text)); + } + + $("select").select2(); + } + ); + }); diff --git a/resources/views/fichas/edit.blade.php b/resources/views/fichas/edit.blade.php index 169e20d..c522c83 100644 --- a/resources/views/fichas/edit.blade.php +++ b/resources/views/fichas/edit.blade.php @@ -7,57 +7,44 @@
-
-
- - -
-
- + @foreach($rubros as $id => $descripcion) + @if ($id == $dbData->rubro_id) @else - - @endif - + + @endif + @endforeach - +
- + @foreach($periodos as $id => $descripcion) + @if ($id == $dbData->periodo) @else - - @endif - + + @endif @endforeach - +
-
- - +
+ + +
+ +
+ +
-
- @if ($dbData->is_temporal == "1") - - - @else - - - @endif -
{{ method_field('PUT') }} +
+ +
+
+
+ + + @if($toast) +
+ @endif + @if ($errorUploading) +
+ @endif + +@stop +@push('scripts') + +@endpush \ No newline at end of file diff --git a/resources/views/file_attachment/notfound.blade.php b/resources/views/file_attachment/notfound.blade.php new file mode 100644 index 0000000..85985f4 --- /dev/null +++ b/resources/views/file_attachment/notfound.blade.php @@ -0,0 +1,18 @@ +@extends('layout') +@section('content') +
+
+
+
+
+ Lo sentimos... +

El archivo solicitado no existe o hubo un error en la descarga.

+
+ +
+
+
+
+@stop \ No newline at end of file diff --git a/resources/views/layout.blade.php b/resources/views/layout.blade.php index e5c1610..83ecc36 100644 --- a/resources/views/layout.blade.php +++ b/resources/views/layout.blade.php @@ -45,57 +45,67 @@ -