-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat-FileAttachment' into develop
- Loading branch information
Showing
14 changed files
with
466 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
database/migrations/2018_09_12_160252_create_file_attachment_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.