Skip to content

Commit

Permalink
Merge branch 'feat-FileAttachment' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
elyambay committed Sep 13, 2018
2 parents 23d3606 + e763785 commit 3029bd3
Show file tree
Hide file tree
Showing 14 changed files with 466 additions and 116 deletions.
16 changes: 16 additions & 0 deletions app/File_attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class File_attachment extends Model
{
protected $table = "file_attachments";

protected $fillable = ["filename", "rubro_id", "periodo", "activo"];

public function rubro(){
return $this->belongsTo('App\Rubro');
}
}
38 changes: 16 additions & 22 deletions app/Http/Controllers/FichasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +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(){
$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);
}
Expand All @@ -42,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){
Expand Down
128 changes: 128 additions & 0 deletions app/Http/Controllers/FileAttachmentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use App\Cabecera_encuesta;
use App\Encuestas_cargo;
use App\Detalle_encuesta;
use App\Cargos_rubro;
use App\Empresa;
use App\Nivel;
use App\Cargo;
use App\Rubro;
use App\User;
use App\File_attachment;
use App\Traits\PeriodosTrait;
use Hash;
use DB;
use Auth;
use Excel;
use Session;
use Validator;

class FileAttachmentController extends Controller
{
use PeriodosTrait;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$rubros = $this->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;
}

}
33 changes: 21 additions & 12 deletions app/Http/Controllers/ReporteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -231,21 +232,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){
Expand All @@ -261,6 +269,7 @@ public function ficha($id){
->with('cargos', $cargos)
->with('periodo', $periodo)
->with('club', $club)
->with('tipoCambio', $tipoCambio)
->with('participantes', $participantes);
}

Expand Down
28 changes: 28 additions & 0 deletions app/Traits/PeriodosTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Traits;

use App\Rubro;
use App\Cabecera_encuesta;

trait PeriodosTrait{
public function getRubros(){
// recuperamos todos los rubros
$rubros = Rubro::all()->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFileAttachmentTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('file_attachments', function(Blueprint $table){
$table->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()
{
//
}
}
21 changes: 19 additions & 2 deletions resources/views/fichas/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<div class="row">
<div class="input-field col s6">
<select id="rubro_id" name="rubro_id">
<option>Elija una opción</option>
@foreach($rubros as $id => $descripcion)
<option value = {{$id}}>{{$descripcion}}</option>
@endforeach
Expand All @@ -19,7 +18,6 @@
</div>
<div class="input-field col s6">
<select id="periodo" name="periodo">
<option>Elija una opción</option>
@foreach($periodos as $id => $descripcion)
<option value = {{$id}}>{{$descripcion}}</option>
@endforeach
Expand Down Expand Up @@ -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(
$("<option></option>").attr("value", data[i].id)
.text(data[i].text));
}
$("select").select2();
}
);
});
</script>
Expand Down
Loading

0 comments on commit 3029bd3

Please sign in to comment.