sigd/app/Http/Controllers/Tool/LockActivityController.php

116 lines
4.0 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 $title = 'Kunci Aktivitas';
protected $template = 'modules.tool.lock';
protected $route = 'modules.kalkulasi.kunci-aktivitas';
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)
{
$data['route'] = $this->route;
$data['title'] = $this->title;
return view($this->template.'.index',$data);
}
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.']);
}
}
public function grid(Request $request)
{
$_data = [];
$data = $this->lockService->getAll();
foreach ($data as $key => $row) {
if ($row->lock_status == 'locked') {
$status = '<span class="badge bg-warning">LOCKED</span>';
} else {
$status = '<span class="badge bg-info">OPEN</span>';
}
$buttonStyle = 'style="min-width: 120px;"'; // Adjust min-width as needed
$action = $row->lock_status == 'locked'
? '<form action="' . route($this->route.'.unlock') . '" method="POST" style="display:inline-block;">'
. csrf_field()
. '<input type="hidden" name="year" value="' . $row->inventory_year . '">'
. '<button type="submit" class="btn btn-sm btn-warning" ' . $buttonStyle . '>Buka Kunci</button>'
. '</form>'
: '<form action="' . route($this->route.'.lock') . '" method="POST" style="display:inline-block;">'
. csrf_field()
. '<input type="hidden" name="year" value="' . $row->inventory_year . '">'
. '<button type="submit" class="btn btn-sm btn-danger" ' . $buttonStyle . '>Kunci</button>'
. '</form>';
$_data[] = [
'no' => $key+1,
'inventory_year' => $row->inventory_year,
'status' => $status,
'executed_time' => @$row->created_at ? date('d-m-Y H:i:s', strtotime(@$row->created_at)) : '',
'action' => $action,
];
}
return response()->json($_data);
}
}