227 lines
7.5 KiB
PHP
227 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Setting;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ReferenceEf;
|
|
use App\Models\ReferenceEfSource;
|
|
use App\Rules\ExistInSchema;
|
|
use App\Rules\UniqueInSchema;
|
|
use App\Services\Setting\EfService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controllers\HasMiddleware;
|
|
use Illuminate\Routing\Controllers\Middleware;
|
|
use Yajra\DataTables\DataTables;
|
|
|
|
class EfController implements HasMiddleware
|
|
{
|
|
protected $title = 'Data EF';
|
|
protected $template = 'modules.setting.ef';
|
|
protected $route = 'modules.pengaturan.ef';
|
|
protected $efService;
|
|
|
|
public function __construct(EfService $efService)
|
|
{
|
|
$this->efService = $efService;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
return [
|
|
//new Middleware('permission:/setting/ef'),
|
|
];
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
|
|
}
|
|
|
|
$data['activities'] = $this->efService->getActivities();
|
|
$data['categories'] = $this->efService->getCategories();
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
|
|
return view($this->template.'.index', $data);
|
|
}
|
|
|
|
|
|
public function getCategoriesByActivity(Request $request)
|
|
{
|
|
$activityCode = $request->input('activity_code');
|
|
$categories = $this->efService->getCategories($activityCode);
|
|
|
|
return response()->json($categories);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$data['activities'] = $this->efService->getActivities();
|
|
$data['efSources'] = $this->efService->getEfSources();
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
return view($this->template.'.create', $data);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'activity_code' => 'required',
|
|
'category' => 'required',
|
|
'ef_source_code' => [
|
|
'required',
|
|
new ExistInSchema('reference', 'ef_source', 'code')
|
|
],
|
|
'description' => 'nullable',
|
|
'tag_1' => 'nullable|string',
|
|
'tag_2' => 'nullable|string',
|
|
'tag_3' => 'nullable|string',
|
|
]);
|
|
|
|
try {
|
|
$request['value'] = getOriginalValue($request['value']);
|
|
$this->efService->create($request->all());
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'EF berhasil ditambahkan.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'EF gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$data['ef'] = $this->efService->find($id);
|
|
$data['activities'] = $this->efService->getActivities();
|
|
$data['efSources'] = $this->efService->getEfSources();
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
return view($this->template.'.edit', $data);
|
|
}
|
|
|
|
public function update(Request $request, ReferenceEf $ef)
|
|
{
|
|
$request->validate([
|
|
'activity_code' => 'required',
|
|
'category' => 'required',
|
|
'ef_source_code' => [
|
|
'required',
|
|
new ExistInSchema('reference', 'ef_source', 'code')
|
|
],
|
|
'value' => 'required',
|
|
'description' => 'nullable',
|
|
'tag_1' => 'nullable|string',
|
|
'tag_2' => 'nullable|string',
|
|
'tag_3' => 'nullable|string',
|
|
]);
|
|
|
|
try {
|
|
$request['value'] = getOriginalValue($request['value']);
|
|
$this->efService->update($ef, $request->all());
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'EF berhasil diperbarui.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'EF gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
|
|
public function destroy(ReferenceEf $ef)
|
|
{
|
|
try {
|
|
$this->efService->delete($ef);
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'Unit berhasil dihapus.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'Unit gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function storeEfSource(Request $request)
|
|
{
|
|
$request->validate([
|
|
'code' => [
|
|
'required',
|
|
'string',
|
|
'max:255',
|
|
new UniqueInSchema('reference', 'ef_source', 'code'),
|
|
],
|
|
'name' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'row_num' => 'required|numeric',
|
|
]);
|
|
|
|
try {
|
|
$efSource = [
|
|
'code' => $request->input('code'),
|
|
'name' => $request->input('name'),
|
|
'description' => $request->input('description'),
|
|
'row_num' => $request->input('row_num'),
|
|
];
|
|
|
|
$this->efService->create($efSource, ReferenceEfSource::class);
|
|
|
|
return response()->json($efSource, 201);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => 'Sumber Data EF gagal dibuat.'], 500);
|
|
}
|
|
}
|
|
|
|
public function grid(Request $request)
|
|
{
|
|
|
|
$_data = [];
|
|
$data = $this->efService->getAll();
|
|
|
|
$activityCodeFilter = $request->activityCodeFilter ?? null;
|
|
$categoryFilter = $request->categoryFilter ?? null;
|
|
|
|
if (!is_null($activityCodeFilter) && $activityCodeFilter != 'SEMUA AKTIVITAS') {
|
|
$data = $data->where('activity_code', $activityCodeFilter);
|
|
}
|
|
|
|
if (!is_null($categoryFilter) && $categoryFilter != 'SEMUA KATEGORI') {
|
|
$data = $data->where('category', $categoryFilter);
|
|
}
|
|
|
|
foreach ($data as $key => $row) {
|
|
|
|
$btn = '<a href="' . route($this->route.'.edit', $row->id) . '" class="btn btn-sm btn-primary">Edit</a>';
|
|
$btn .= ' <form method="POST" action="' . route($this->route.'.destroy', $row->id) . '" style="display: inline">';
|
|
$btn .= csrf_field();
|
|
$btn .= method_field('DELETE');
|
|
$btn .= '<button type="submit" class="btn btn-sm btn-danger" onclick="return confirm(\'Are you sure?\')">Delete</button>';
|
|
$btn .= '</form>';
|
|
|
|
if ($row->active_status == 1) {
|
|
$status = '<span class="badge bg-success">Aktif</span>';
|
|
} else {
|
|
$status = '<span class="badge bg-danger">Tidak Aktif</span>';
|
|
}
|
|
|
|
$tags = array();
|
|
for ($i = 1; $i <= 3; $i++) {
|
|
$col = $row->{'tag_' . $i}; // Correctly access the dynamic property
|
|
if ($col && $col != '') {
|
|
array_push($tags, '<span class="badge badge-info" style="margin:1px">' . $col . '</span>');
|
|
}
|
|
}
|
|
|
|
$_data[] = [
|
|
'no' => $key+1,
|
|
'value' => $row->value ? getFormattedValue($row->value) : 0,
|
|
'activity' => $row->activity ? $row->activity->name : 'N/A',
|
|
'ef_resource' => $row->efSource ? $row->efSource->name : 'N/A',
|
|
'category' => $row->category,
|
|
'description' => $row->description,
|
|
'status' => $status,
|
|
'tags' => $tags,
|
|
'action' => @$btn,
|
|
];
|
|
|
|
}
|
|
|
|
return response()->json($_data);
|
|
}
|
|
}
|