133 lines
3.9 KiB
PHP
133 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Models\Peringkat;
|
|
use App\Models\Bulan;
|
|
use App\Models\Brand;
|
|
use App\Models\Hasil;
|
|
|
|
class PeringkatController extends Controller
|
|
{
|
|
// public function index()
|
|
// {
|
|
// return view('peringkat');
|
|
// }
|
|
|
|
public function index(Request $request)
|
|
{
|
|
// Ambil daftar bulan yang tersedia di tabel peringkat
|
|
$availableMonths = Peringkat::select('bulan')->distinct()->orderBy('bulan')->pluck('bulan');
|
|
|
|
// Konversi ke array bulan teks manual dalam bahasa Indonesia
|
|
$bulanOptions = $availableMonths->mapWithKeys(function ($item) {
|
|
return [$item => $this->getNamaBulan($item)];
|
|
});
|
|
|
|
// Bulan aktif dari request atau pakai bulan terakhir
|
|
$bulanDipilih = $request->get('bulan', $availableMonths->first());
|
|
|
|
// Ambil data peringkat berdasarkan bulan
|
|
$peringkat = Peringkat::where('bulan', $bulanDipilih)->join('brands', 'peringkat.brands', '=', 'brands.id')->select('peringkat.peringkat', 'brands.nama_brand as brand_nama', 'peringkat.total')->orderBy('peringkat.peringkat')->get();
|
|
|
|
// Cek apakah data sudah ada untuk bulan tersebut
|
|
$hasData = $peringkat->isNotEmpty();
|
|
|
|
return view('peringkat', compact('peringkat', 'bulanOptions', 'bulanDipilih', 'hasData'));
|
|
}
|
|
|
|
private function getNamaBulan($bulan)
|
|
{
|
|
$namaBulan = [
|
|
1 => 'Januari',
|
|
2 => 'Februari',
|
|
3 => 'Maret',
|
|
4 => 'April',
|
|
5 => 'Mei',
|
|
6 => 'Juni',
|
|
7 => 'Juli',
|
|
8 => 'Agustus',
|
|
9 => 'September',
|
|
10 => 'Oktober',
|
|
11 => 'November',
|
|
12 => 'Desember',
|
|
];
|
|
|
|
return $namaBulan[$bulan] ?? 'Tidak Diketahui';
|
|
}
|
|
|
|
private function getUrutanBulan()
|
|
{
|
|
$startMonth = session('startMonth') ?? 1;
|
|
$bulan = [];
|
|
|
|
for ($i = 0; $i < 4; $i++) {
|
|
$bulan[] = (($startMonth + $i - 1) % 12) + 1;
|
|
}
|
|
|
|
return $bulan;
|
|
}
|
|
|
|
public function proses(Request $request)
|
|
{
|
|
// Kosongkan tabel peringkat dulu (jika perlu)
|
|
Peringkat::truncate();
|
|
|
|
// Ambil semua bulan unik dari tabel hasil
|
|
$bulans = Hasil::select('bulan')->distinct()->orderBy('bulan')->pluck('bulan');
|
|
|
|
// Ambil semua brand
|
|
$brands = Brand::all();
|
|
$jumlahBrand = $brands->count(); // hitung total brand
|
|
$maxPeringkat = $jumlahBrand;
|
|
|
|
foreach ($bulans as $bulan) {
|
|
$peringkatData = [];
|
|
|
|
foreach ($brands as $brand) {
|
|
// Ambil total, jika tidak ditemukan isi dengan 0
|
|
$total = Hasil::where('brands', $brand->id)->where('bulan', $bulan)->value('total_bulan') ?? 0;
|
|
|
|
$peringkatData[] = [
|
|
'bulan' => $bulan,
|
|
'brands' => $brand->id,
|
|
'total' => $total,
|
|
];
|
|
}
|
|
|
|
// Urutkan descending berdasarkan total
|
|
usort($peringkatData, fn($a, $b) => $b['total'] <=> $a['total']);
|
|
|
|
// Tambahkan peringkat 1 - jumlah brand (dinamis)
|
|
$rank = 1;
|
|
|
|
foreach ($peringkatData as $data) {
|
|
Peringkat::create([
|
|
'bulan' => $data['bulan'],
|
|
'brands' => $data['brands'],
|
|
'total' => $data['total'],
|
|
'peringkat' => $rank,
|
|
]);
|
|
|
|
$rank++;
|
|
if ($rank > $maxPeringkat) {
|
|
$rank = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return redirect()->route('peringkat.index')->with('success', 'Peringkat Brand berhasil dihitung.');
|
|
}
|
|
|
|
public function reset()
|
|
{
|
|
DB::table('peringkat')->truncate();
|
|
|
|
return redirect()->route('peringkat.index')->with('success', 'Peringkat Brand berhasil direset.');
|
|
}
|
|
}
|