input('brand', $brands->first()->id ?? 1); // Ambil data testing sesuai brand $testings = Testing::where('brands', $brand)->orderBy('no')->get(); // Cek apakah ada data testing sama sekali $hasData = $testings->isNotEmpty(); return view('testing', compact('testings', 'brand', 'brands', 'hasData')); } // public function generate(Request $request) // { // // Mengambil data dari tabel samples // DB::insert(" // INSERT INTO testing (no, data_1, data_2, data_3, target, brands) // SELECT // ROW_NUMBER() OVER (PARTITION BY s4.brand ORDER BY s4.id) AS no, // s1.data AS data_1, -- minggu -3 sebelum target // s2.data AS data_2, -- minggu -2 sebelum target // s3.data AS data_3, -- minggu -1 sebelum target // s4.data AS target, -- target minggu sekarang // s4.brand AS brands // FROM samples s4 // JOIN samples s3 ON s3.id = s4.id - 1 // JOIN samples s2 ON s2.id = s4.id - 2 // JOIN samples s1 ON s1.id = s4.id - 3 // WHERE s4.brand BETWEEN 1 AND 11 // AND s3.brand = s4.brand // AND s2.brand = s4.brand // AND s1.brand = s4.brand // AND s4.bulan BETWEEN 9 AND 12 // ORDER BY s4.brand, s4.id; // "); // return redirect()->route('testing.index')->with('success', 'Data Testing berhasil diambil.'); // } public function reset() { DB::table('testing')->truncate(); return redirect()->route('testing.index')->with('success', 'Data Testing berhasil direset.'); } public function generate(Request $request) { $startMonth = session('startMonth'); // Ambil bulan mulai dari session if (!$startMonth) { return redirect()->route('testing.index')->with('error', 'Start month tidak ditemukan di session.'); } // Hitung bulan untuk testing: 4 bulan setelah training (bulan ke 9–12 dari startMonth) $validMonths = []; for ($i = 8; $i < 12; $i++) { $month = ($startMonth + $i - 1) % 12 + 1; $validMonths[] = $month; } // Ambil brand_id aktif $brands = DB::table('products') ->where('status', 'aktif') ->distinct() ->pluck('brand_id') ->toArray(); if (empty($brands)) { return redirect()->route('testing.index')->with('error', 'Tidak ada brand aktif ditemukan.'); } // Insert data testing DB::insert(" INSERT INTO testing (no, data_1, data_2, data_3, target, brands) SELECT ROW_NUMBER() OVER (PARTITION BY s4.brand ORDER BY s4.id) AS no, s1.data AS data_1, -- minggu -3 sebelum target s2.data AS data_2, -- minggu -2 sebelum target s3.data AS data_3, -- minggu -1 sebelum target s4.data AS target, -- target minggu sekarang s4.brand AS brands FROM samples s4 JOIN samples s3 ON s3.id = s4.id - 1 JOIN samples s2 ON s2.id = s4.id - 2 JOIN samples s1 ON s1.id = s4.id - 3 WHERE s4.brand IN (" . implode(',', $brands) . ") AND s3.brand = s4.brand AND s2.brand = s4.brand AND s1.brand = s4.brand AND s4.bulan IN (" . implode(',', $validMonths) . ") ORDER BY s4.brand, s4.id "); return redirect()->route('testing.index')->with('success', 'Data Testing berhasil diambil.'); } }