118 lines
3.9 KiB
PHP
118 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Tool;
|
|
|
|
use App\Enums\SigdStatus;
|
|
use App\Models\ActivityForm;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ActivityLock;
|
|
use App\Services\Tool\CopyActivityService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controllers\HasMiddleware;
|
|
use Illuminate\Routing\Controllers\Middleware;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class CopyActivityController implements HasMiddleware
|
|
{
|
|
protected $service;
|
|
|
|
public function __construct(CopyActivityService $service)
|
|
{
|
|
$this->service = $service;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
return [
|
|
//new Middleware('permission:/tool/copy_activity'),
|
|
];
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$availableYears = ActivityForm::select('inventory_year')->whereNull('agency_id')
|
|
->distinct()->rowActive()->orderBy('inventory_year', 'desc')
|
|
->get()->pluck('inventory_year')->toArray();
|
|
|
|
if ($request->ajax()) {
|
|
$query = $this->service->getRawAll();
|
|
|
|
$result = datatables()->of($query)
|
|
->addColumn('duration', function ($row) {
|
|
if ($row->finished_time === null || $row->executed_time === null) {
|
|
return '';
|
|
}
|
|
|
|
$start = Carbon::parse($row->executed_time);
|
|
$end = Carbon::parse($row->finished_time);
|
|
|
|
// Calculate the difference in seconds
|
|
$diffInSeconds = $start->diffInSeconds($end);
|
|
|
|
// Format the duration as HH:MM:SS
|
|
$hours = floor($diffInSeconds / 3600);
|
|
$minutes = floor(($diffInSeconds % 3600) / 60);
|
|
$seconds = $diffInSeconds % 60;
|
|
|
|
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
|
|
})
|
|
->editColumn('created_at', function ($row) {
|
|
return $row->created_at->format('d-m-Y H:i:s')
|
|
?? '';
|
|
})
|
|
->editColumn('executed_time', function ($row) {
|
|
if ($row->executed_time === null) {
|
|
return '';
|
|
}
|
|
|
|
return $row->executed_time->format('d-m-Y H:i:s');
|
|
})
|
|
->editColumn('finished_time', function ($row) {
|
|
if ($row->finished_time === null) {
|
|
return '';
|
|
}
|
|
|
|
return $row->finished_time->format('d-m-Y H:i:s');
|
|
})
|
|
->addColumn('status', function ($row) {
|
|
$status = SigdStatus::from($row->status);
|
|
return $status->badge() ?? '';
|
|
})
|
|
->rawColumns(['status', 'duration'])
|
|
->make(true);
|
|
|
|
return $result;
|
|
}
|
|
|
|
return view('tool.copy.index', [
|
|
'availableYears' => $availableYears
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
// Validate the form data
|
|
$request->validate([
|
|
'from_year' => 'required|integer|min:2000|max:' . date('Y'),
|
|
'to_year' => 'required|integer|min:2000|max:' . date('Y') . '|different:from_year',
|
|
]);
|
|
|
|
$isLocked = ActivityLock::isLocked($request->to_year);
|
|
|
|
if ($isLocked) {
|
|
return back()->withErrors(['error' => 'Salin Aktivitas gagal disimpan. Tahun Inventory tujuan dikunci.']);
|
|
}
|
|
|
|
try {
|
|
$data = $request->all();
|
|
|
|
$this->service->create($data);
|
|
|
|
return redirect()->route('copy.index')->with('success', 'Salin Aktivitas berhasil ditambahkan.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'Salin Aktivitas gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
}
|