ultimatepos/Modules/ShopFloor/Http/Controllers/OeeController.php

68 lines
2.3 KiB
PHP

<?php
namespace Modules\ShopFloor\Http\Controllers;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Modules\ShopFloor\Models\OeeSnapshot;
use Modules\ShopFloor\Models\WorkCenter;
use Modules\ShopFloor\Services\OeeCalculationService;
class OeeController extends Controller
{
public function index(Request $request, OeeCalculationService $oeeService)
{
if (! auth()->user()->can('shopfloor.oee.view')) {
abort(403);
}
$business_id = $request->session()->get('user.business_id');
$date = $request->input('date', now()->toDateString());
$snapshots = OeeSnapshot::forBusiness($business_id)
->with('workCenter')
->where('snapshot_date', $date)
->orderBy('work_center_id')
->get();
if ($snapshots->isEmpty()) {
$oeeService->calculateAllForDate($business_id, Carbon::parse($date));
$snapshots = OeeSnapshot::forBusiness($business_id)
->with('workCenter')
->where('snapshot_date', $date)
->orderBy('work_center_id')
->get();
}
$workCenters = WorkCenter::forBusiness($business_id)->orderBy('name')->pluck('name', 'id');
return view('shopfloor::oee.index', compact('snapshots', 'date', 'workCenters'));
}
public function calculate(Request $request, OeeCalculationService $oeeService)
{
if (! auth()->user()->can('shopfloor.oee.view')) {
abort(403);
}
$business_id = $request->session()->get('user.business_id');
$validated = $request->validate([
'date' => 'nullable|date',
'work_center_id' => 'nullable|integer|exists:sf_work_centers,id',
]);
$date = Carbon::parse($validated['date'] ?? now()->toDateString());
if (! empty($validated['work_center_id'])) {
$oeeService->calculateForWorkCenter($business_id, (int) $validated['work_center_id'], $date);
} else {
$oeeService->calculateAllForDate($business_id, $date);
}
return redirect()
->action([self::class, 'index'], ['date' => $date->toDateString()])
->with('status', ['success' => 1, 'msg' => __('shopfloor::lang.oee_calculated')]);
}
}