171 lines
5.5 KiB
PHP
171 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Setting;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ReferenceAr;
|
|
use App\Rules\UniqueInSchema;
|
|
use App\Services\Setting\ArService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controllers\HasMiddleware;
|
|
use Illuminate\Routing\Controllers\Middleware;
|
|
|
|
class ArController implements HasMiddleware
|
|
{
|
|
protected $title = 'AR';
|
|
protected $template = 'modules.setting.ar';
|
|
protected $route = 'modules.pengaturan.ar';
|
|
protected $arService;
|
|
|
|
public function __construct(ArService $arService)
|
|
{
|
|
$this->arService = $arService;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
return [
|
|
//new Middleware('permission:/setting/ar'),
|
|
];
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
|
|
return view($this->template.'.index',$data);
|
|
}
|
|
|
|
// public function show(){
|
|
// dd('a');
|
|
// }
|
|
public function create()
|
|
{
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
|
|
return view($this->template.'.create',$data);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'code' => [
|
|
'required',
|
|
'string',
|
|
'max:255',
|
|
new UniqueInSchema('reference', 'ar', 'code'),
|
|
],
|
|
'name' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'row_num' => 'required|numeric',
|
|
]);
|
|
|
|
try {
|
|
$data = array_merge($request->all(), ['active_status' => 0]);
|
|
$this->arService->create($data);
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'AR berhasil ditambahkan.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'AR gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$data['ar'] = $this->arService->find($id);
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
return view($this->template.'.edit', $data);
|
|
}
|
|
|
|
public function update(Request $request, ReferenceAr $ar)
|
|
{
|
|
$request->validate([
|
|
'code' => [
|
|
'required',
|
|
'string',
|
|
'max:255',
|
|
new UniqueInSchema('reference', 'ar', 'code', $ar->id),
|
|
],
|
|
'name' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'row_num' => 'required|numeric',
|
|
]);
|
|
|
|
try {
|
|
$this->arService->update($ar, $request->all());
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'AR berhasil diperbarui.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'AR gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function destroy(ReferenceAr $ar)
|
|
{
|
|
try {
|
|
$this->arService->delete($ar);
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'AR berhasil dihapus.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'AR gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function setAktif($id)
|
|
{
|
|
try {
|
|
$this->arService->setAktif($id);
|
|
return redirect()->route($this->route.'.index')->with('success', 'AR berhasil diaktifkan.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'Gagal mengaktifkan AR. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function grid()
|
|
{
|
|
$data = $this->arService->getAll();
|
|
$_data = [];
|
|
|
|
foreach ($data as $key => $row) {
|
|
|
|
$btn = '<a href="' . route($this->route.'.edit', $row->id) . '" class="btn btn-sm btn-primary">Edit</a>';
|
|
$btn .= ' <form action="' . route($this->route.'.destroy', $row->id) . '" method="POST" style="display: inline;" class="delete-form">';
|
|
$btn .= csrf_field();
|
|
$btn .= method_field('DELETE');
|
|
$btn .= '<button type="button" class="btn btn-sm btn-danger delete-button">Hapus</button>';
|
|
$btn .= '</form>';
|
|
|
|
if ($row->active_status == 0) {
|
|
$btn .= ' <form action="' . route($this->route.'.setAktif', $row->id) . '" method="POST" style="display: inline;" class="set-aktif-form">';
|
|
$btn .= csrf_field();
|
|
$btn .= method_field('PUT');
|
|
$btn .= '<button type="button" class="btn btn-sm btn-info text-white set-aktif-button">Aktifkan</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>';
|
|
}
|
|
|
|
$_data[] = [
|
|
'no' => $key+1,
|
|
'no_baris' => @$row->row_num,
|
|
'code' => $row->code,
|
|
'name' => $row->name,
|
|
'description' => $row->description,
|
|
'no_baris' => $row->row_num,
|
|
'status' => $status,
|
|
'action' => @$btn,
|
|
];
|
|
|
|
}
|
|
|
|
return response()->json($_data);
|
|
}
|
|
}
|