2022-08-17 01:11:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
2022-10-25 06:35:22 +00:00
|
|
|
|
2022-08-17 01:11:22 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Evidencias;
|
|
|
|
use App\Models\plan;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
use Illuminate\Support\Facades\Response as FacadeResponse;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EvidenciasController extends Controller
|
|
|
|
{
|
2022-10-25 06:35:22 +00:00
|
|
|
public function create(Request $request)
|
|
|
|
{
|
2022-08-17 01:11:22 +00:00
|
|
|
$request->validate([
|
2022-10-25 06:35:22 +00:00
|
|
|
"id_plan" => "required|integer",
|
|
|
|
"codigo" => "required",
|
|
|
|
"denominacion" => "required",
|
|
|
|
"adjunto" => "required",
|
2022-08-17 01:11:22 +00:00
|
|
|
]);
|
2022-10-25 06:35:22 +00:00
|
|
|
$id_user = auth()->user();
|
2022-10-26 03:16:24 +00:00
|
|
|
if (plan::where(["id" => $request->id_plan])->exists()) {
|
2022-08-17 01:11:22 +00:00
|
|
|
$plan = plan::find($request->id_plan);
|
2022-10-26 03:16:24 +00:00
|
|
|
if ($id_user->isCreadorPlan($request->id_plan) or $id_user->isAdmin()) {
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia = new Evidencias();
|
|
|
|
$evidencia->id_plan = $request->id_plan;
|
2022-08-25 23:54:30 +00:00
|
|
|
$evidencia->codigo = $plan->codigo;
|
2022-12-15 04:45:35 +00:00
|
|
|
$evidencia->denominacion = $request->denominacion.'.'.$request->adjunto->extension();
|
2022-12-15 04:10:28 +00:00
|
|
|
$path = $request->adjunto->storePubliclyAs(
|
|
|
|
'evidencias',
|
|
|
|
$request->adjunto->getClientOriginalName()
|
|
|
|
);
|
|
|
|
error_log($path);
|
|
|
|
|
2022-10-25 06:35:22 +00:00
|
|
|
$evidencia->adjunto = $path;
|
2022-10-26 03:16:24 +00:00
|
|
|
$evidencia->id_user = $id_user->id;
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia->save();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"message" => "Evidencia creada exitosamente",
|
2022-10-25 06:35:22 +00:00
|
|
|
"evidencia" => $evidencia
|
2022-08-17 01:11:22 +00:00
|
|
|
]);
|
2022-10-25 06:35:22 +00:00
|
|
|
} else {
|
2022-08-17 01:11:22 +00:00
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No tienes permisos para crear esta Evidencia",
|
2022-10-25 06:35:22 +00:00
|
|
|
], 404);
|
2022-08-17 01:11:22 +00:00
|
|
|
}
|
2022-10-25 06:35:22 +00:00
|
|
|
} else {
|
2022-08-17 01:11:22 +00:00
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No se encontro el plan",
|
2022-10-25 06:35:22 +00:00
|
|
|
], 404);
|
2022-08-17 01:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 06:35:22 +00:00
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
if (Evidencias::where("id", $id)->exists()) {
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia = Evidencias::find($id);
|
2022-10-25 06:35:22 +00:00
|
|
|
//Para retornar nombre de user
|
|
|
|
/*$user = User::find($evidencia->id_user);
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia->id_user = $user->name;*/
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"msg" => "!Evidencia",
|
|
|
|
"data" => $evidencia,
|
|
|
|
]);
|
2022-10-25 06:35:22 +00:00
|
|
|
} else {
|
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"msg" => "!No se encontro el evidencia",
|
|
|
|
], 404);
|
2022-08-17 01:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-10-25 06:35:22 +00:00
|
|
|
public function update(Request $request)
|
|
|
|
{
|
2022-08-17 01:11:22 +00:00
|
|
|
$request->validate([
|
2022-10-25 06:35:22 +00:00
|
|
|
"id" => "required|integer",
|
|
|
|
"codigo" => "required",
|
|
|
|
"denominacion" => "required",
|
|
|
|
"adjunto" => "required"
|
2022-08-17 01:11:22 +00:00
|
|
|
]);
|
2022-10-25 06:35:22 +00:00
|
|
|
$id_user = auth()->user();
|
|
|
|
if (Evidencias::where(["id" => $request->id])->exists()) {
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia = Evidencias::find($request->id);
|
|
|
|
$plan = plan::find($evidencia->id_plan);
|
2022-10-26 03:20:29 +00:00
|
|
|
if ($id_user->isCreadorPlan($plan->id) or $id_user->isAdmin()) {
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia->codigo = $request->codigo;
|
2022-12-15 04:40:10 +00:00
|
|
|
$evidencia->denominacion = $request->denominacion.$request->adjunto->extension();
|
2022-12-15 04:10:28 +00:00
|
|
|
$path = $request->adjunto->storePubliclyAs(
|
|
|
|
'evidencias',
|
|
|
|
$request->adjunto->getClientOriginalName()
|
|
|
|
);
|
2022-10-25 06:35:22 +00:00
|
|
|
$evidencia->adjunto = $path;
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia->save();
|
|
|
|
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"message" => "Evidencia actualizada exitosamente",
|
|
|
|
]);
|
2022-10-25 06:35:22 +00:00
|
|
|
} else {
|
2022-08-17 01:11:22 +00:00
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No tienes permisos para actualizar esta evidencia",
|
2022-10-25 06:35:22 +00:00
|
|
|
], 404);
|
2022-08-17 01:11:22 +00:00
|
|
|
}
|
2022-10-25 06:35:22 +00:00
|
|
|
} else {
|
2022-08-17 01:11:22 +00:00
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No se encontro la evidencia",
|
2022-10-25 06:35:22 +00:00
|
|
|
], 404);
|
2022-08-17 01:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($id)
|
|
|
|
{
|
2022-10-25 06:35:22 +00:00
|
|
|
$id_user = auth()->user();
|
|
|
|
if (Evidencias::where(["id" => $id])->exists()) {
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia = Evidencias::find($id);
|
|
|
|
$plan = plan::find($evidencia->id_plan);
|
2022-10-26 03:20:29 +00:00
|
|
|
if ($id_user->isCreadorPlan($plan->id) or $id_user->isAdmin()) {
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia->delete();
|
|
|
|
return response([
|
|
|
|
"status" => 1,
|
|
|
|
"message" => "Evidencia eliminada exitosamente",
|
|
|
|
]);
|
2022-10-25 06:35:22 +00:00
|
|
|
} else {
|
2022-08-17 01:11:22 +00:00
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No tienes permisos para eliminar esta evidencia",
|
2022-10-25 06:35:22 +00:00
|
|
|
], 404);
|
2022-08-17 01:11:22 +00:00
|
|
|
}
|
2022-10-25 06:35:22 +00:00
|
|
|
} else {
|
2022-08-17 01:11:22 +00:00
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"message" => "No se encontro la evidencia",
|
2022-10-25 06:35:22 +00:00
|
|
|
], 404);
|
2022-08-17 01:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 06:35:22 +00:00
|
|
|
public function download($id)
|
|
|
|
{
|
|
|
|
if (Evidencias::where("id", $id)->exists()) {
|
2022-08-17 01:11:22 +00:00
|
|
|
$evidencia = Evidencias::find($id);
|
2022-10-25 06:35:22 +00:00
|
|
|
$path = storage_path('app/' . $evidencia->adjunto);
|
|
|
|
//$evidencia->adjunto = download($path);
|
2022-08-17 01:11:22 +00:00
|
|
|
return response()->download($path);
|
2022-10-25 06:35:22 +00:00
|
|
|
} else {
|
|
|
|
return response([
|
|
|
|
"status" => 0,
|
|
|
|
"msg" => "!No se encontro la evidencia",
|
|
|
|
], 404);
|
2022-08-17 01:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|