106 lines
3.9 KiB
PHP
106 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Tool;
|
|
|
|
use App\Services\Tool\LockActivityService;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controllers\HasMiddleware;
|
|
use Illuminate\Routing\Controllers\Middleware;
|
|
|
|
class LockActivityController implements HasMiddleware
|
|
{
|
|
protected $lockService;
|
|
|
|
public function __construct(LockActivityService $lockService)
|
|
{
|
|
$this->lockService = $lockService;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
return [
|
|
//new Middleware('permission:/tool/lock_activity'),
|
|
];
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$locks = $this->lockService->getAll();
|
|
|
|
$result = datatables()->of($locks)
|
|
->editColumn('updated_at', function ($row) {
|
|
return $row->created_at->format('d-m-Y H:i:s')
|
|
?? '';
|
|
})
|
|
->addColumn('status', function ($row) {
|
|
if ($row->lock_status == 'locked') {
|
|
return '<span class="badge badge-warning">LOCKED</span>';
|
|
} else {
|
|
return '<span class="badge badge-info">OPEN</span>';
|
|
}
|
|
})
|
|
->addColumn('action', function ($row) {
|
|
$buttonStyle = 'style="min-width: 120px;"'; // Adjust min-width as needed
|
|
|
|
$action = $row->lock_status == 'locked'
|
|
? '<form action="' . route('lock.unlock') . '" method="POST" style="display:inline-block;">'
|
|
. csrf_field()
|
|
. '<input type="hidden" name="year" value="' . $row->inventory_year . '">'
|
|
. '<button type="submit" class="btn btn-warning" ' . $buttonStyle . '>Buka Kunci</button>'
|
|
. '</form>'
|
|
: '<form action="' . route('lock.lock') . '" method="POST" style="display:inline-block;">'
|
|
. csrf_field()
|
|
. '<input type="hidden" name="year" value="' . $row->inventory_year . '">'
|
|
. '<button type="submit" class="btn btn-danger" ' . $buttonStyle . '>Kunci</button>'
|
|
. '</form>';
|
|
|
|
return $action;
|
|
})
|
|
->rawColumns(['status', 'action'])
|
|
->make(true);
|
|
|
|
return $result;
|
|
}
|
|
|
|
return view('tool.lock.index');
|
|
}
|
|
|
|
public function lock(Request $request)
|
|
{
|
|
// Validate the input data
|
|
$request->validate([
|
|
'year' => 'required|integer|min:2000|max:' . date('Y'),
|
|
'copy_activity' => 'nullable|in:yes,no'
|
|
]);
|
|
|
|
try {
|
|
if ($request->copy_activity === 'yes') {
|
|
$this->lockService->copyActivityToNextYear($request->year);
|
|
}
|
|
|
|
$this->lockService->lock($request->year);
|
|
|
|
return redirect()->back()->with('success', 'Tahun Inventory ' . $request->year . ' berhasil dikunci' . ($request->copy_activity === 'ya' ? ' dan data aktivitas berhasil disalin ke tahun berikutnya' : ''));
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->withErrors(['error' => 'Tahun Inventory ' . $request->year . ' gagal dikunci. Mohon dicoba kembali.']);
|
|
}
|
|
}
|
|
|
|
|
|
public function unlock(Request $request)
|
|
{
|
|
$request->validate([
|
|
'year' => 'required|integer|min:2000|max:' . date('Y'),
|
|
]);
|
|
|
|
try {
|
|
$this->lockService->unlock($request->year);
|
|
return redirect()->back()->with('success', 'Tahun Inventory ' . $request->year . ' berhasil dibuka');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->withErrors(['error' => 'Tahun Inventory ' . $request->year . 'gagal dibuka. Mohon dicoba kembali.']);
|
|
}
|
|
}
|
|
}
|