66use App \Models \Line ;
77use App \Models \OeeRecord ;
88use App \Services \Production \DowntimeService ;
9+ use App \Services \Production \OeeCalculationService ;
910use Carbon \Carbon ;
1011use Illuminate \Http \Request ;
12+ use Illuminate \Support \Facades \Cache ;
1113
1214class OeeController extends Controller
1315{
1416 public function __construct (
1517 protected DowntimeService $ downtimeService ,
18+ protected OeeCalculationService $ oeeService ,
1619 ) {}
1720
1821 public function index (Request $ request )
@@ -22,6 +25,9 @@ public function index(Request $request)
2225 $ dateFrom = $ request ->query ('date_from ' , today ()->subDays (7 )->toDateString ());
2326 $ dateTo = $ request ->query ('date_to ' , today ()->toDateString ());
2427
28+ // Auto-calculate OEE for today if not cached
29+ $ this ->ensureOeeCalculated ();
30+
2531 $ query = OeeRecord::with (['line ' , 'shift ' ])
2632 ->whereBetween ('record_date ' , [$ dateFrom , $ dateTo ])
2733 ->orderByDesc ('record_date ' );
@@ -68,6 +74,8 @@ public function show(Line $line, Request $request)
6874 $ dateFrom = $ request ->query ('date_from ' , today ()->subDays (7 )->toDateString ());
6975 $ dateTo = $ request ->query ('date_to ' , today ()->toDateString ());
7076
77+ $ this ->ensureOeeCalculated ();
78+
7179 $ records = OeeRecord::where ('line_id ' , $ line ->id )
7280 ->whereBetween ('record_date ' , [$ dateFrom , $ dateTo ])
7381 ->with ('shift ' )
@@ -82,4 +90,18 @@ public function show(Line $line, Request $request)
8290
8391 return view ('admin.oee.show ' , compact ('line ' , 'records ' , 'downtimeByReason ' , 'dateFrom ' , 'dateTo ' ));
8492 }
93+
94+ /**
95+ * Auto-calculate OEE for today and yesterday if not already done.
96+ * Cached for 15 minutes to avoid recalculating on every page load.
97+ */
98+ private function ensureOeeCalculated (): void
99+ {
100+ Cache::remember ('oee_calculated_ ' .today ()->toDateString (), 900 , function () {
101+ $ this ->oeeService ->calculateAll (today ());
102+ $ this ->oeeService ->calculateAll (Carbon::yesterday ());
103+
104+ return true ;
105+ });
106+ }
85107}
0 commit comments