service = $service; } public static function middleware(): array { return [ //new Middleware('permission:/tool/produsen_calculate'), ]; } public function index(Request $request) { $data['availableYears'] = ActivityForm::select('inventory_year')->whereNotNull('agency_id') ->distinct()->rowActive()->orderBy('inventory_year', 'desc') ->get()->pluck('inventory_year')->toArray(); $data['route'] = $this->route; $data['title'] = $this->title; return view($this->template.'.index', $data); } public function store(Request $request) { // Validate the form data $request->validate([ 'inventory_year' => 'required|integer|min:2000|max:' . date('Y'), ]); $isLocked = ActivityLock::isLocked($request->inventory_year); if ($isLocked) { return back()->withErrors(['error' => 'Hitung Data dari Produsen gagal disimpan. Tahun Inventory tujuan dikunci.']); } try { $data = $request->all(); $this->service->create($data); return redirect()->route($this->route.'.index')->with('success', 'Proses Hitung Data dari Produsen berhasil ditambahkan.'); } catch (\Exception $e) { return back()->withErrors(['error' => 'Proses Hitung Data dari Produsen Aktivitas gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]); } } public function grid(Request $request) { $_data = []; $data = $this->service->getRawAll(); foreach ($data->get() as $key => $row) { $status = SigdStatus::from($row->status); if ($row->finished_time === null || $row->executed_time === null) { $duration = ''; } // Parse the date-time strings into Carbon instances $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; $duration = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); $_data[] = [ 'no' => $key+1, 'inventory_year' => $row->inventory_year, 'status' => $status->badge() ?? '', 'finished_time' => @$row->finished_time ? date('d-m-Y H:i:s', strtotime(@$row->finished_time)) : '', 'executed_time' => @$row->executed_time ? date('d-m-Y H:i:s', strtotime(@$row->executed_time)) : '', 'created_at' => @$row->created_at ? date('d-m-Y H:i:s', strtotime(@$row->created_at)) : '', 'duration' => $duration, ]; } return response()->json($_data); } }