179 lines
6.6 KiB
PHP
179 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Survey;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Arr;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
class SurveyController extends Controller
|
|
{
|
|
public function index(Request $request){
|
|
if(!session()->has('payment_success')){
|
|
return redirect('/');
|
|
}
|
|
$no_order = $request->query('no_order');
|
|
$type = $request->query('type');
|
|
$data = [
|
|
'title' => 'Survey Gizi',
|
|
'mcu' => false,
|
|
'no_order' => $no_order,
|
|
'type' => $type
|
|
];
|
|
return view('guest.survey.index', $data);
|
|
}
|
|
|
|
public function store(){
|
|
DB::connection('dbOrderGizi')->beginTransaction();
|
|
try {
|
|
$payload =[
|
|
'no_order' => request('no_order'),
|
|
'type' => request('type'),
|
|
'keterangan' => request('kritik_saran'),
|
|
'kepuasan' => request('kepuasan'),
|
|
'created_at' => now()
|
|
];
|
|
Survey::create($payload);
|
|
DB::connection('dbOrderGizi')->commit();
|
|
session()->flash('payment_success', true);
|
|
session()->flash('no_order', $payload['no_order']);
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => 'Survei berhasil disimpan'
|
|
]);
|
|
} catch (\Throwable $th) {
|
|
DB::connection('dbOrderGizi')->rollBack();
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Data gagal disimpan ' . $th->getMessage()
|
|
]);
|
|
}
|
|
|
|
}
|
|
|
|
public function datatable(){
|
|
$data = Survey::query();
|
|
$tanggal = request('tanggal');
|
|
if(!empty($tanggal)){
|
|
$flattened = is_array($tanggal[0]) ? Arr::flatten($tanggal) : $tanggal;
|
|
$data->whereIn(DB::raw('DATE(created_at)'), $flattened);
|
|
}else{
|
|
$now = Carbon::now()->format('Y-m-d');
|
|
$data->whereDate('created_at', $now);
|
|
}
|
|
$data = $data->get();
|
|
return response()->json([
|
|
'rows' => $data->values(),
|
|
'total' => $data->count(),
|
|
]);
|
|
}
|
|
|
|
public function chartDataSurvey(Request $request)
|
|
{
|
|
$tanggal = $request->query('tanggal');
|
|
if(is_string($tanggal)) $tanggal = json_decode($tanggal, true);
|
|
|
|
$query = Survey::query();
|
|
if (!empty($tanggal) && is_array($tanggal)) {
|
|
$query->whereIn(DB::raw('DATE(created_at)'), $tanggal);
|
|
} else {
|
|
$query->whereDate('created_at', Carbon::today());
|
|
}
|
|
|
|
$allData = $query->get();
|
|
|
|
// Pisahkan data untuk perhitungan Card
|
|
$dataBaru = $allData->where('type', 'pengguna_baru');
|
|
$dataLama = $allData->where('type', 'pelanggan_setia');
|
|
|
|
return response()->json([
|
|
'responden' => [
|
|
'baru' => $dataBaru->count(),
|
|
'lama' => $dataLama->count(),
|
|
],
|
|
'kepuasan_baru' => [
|
|
'total' => $dataBaru->count(),
|
|
'puas' => $dataBaru->where('kepuasan', 'Puas')->count(),
|
|
'tidak_puas' => $dataBaru->where('kepuasan', 'Tidak Puas')->count(),
|
|
],
|
|
'kepuasan_lama' => [
|
|
'total' => $dataLama->count(),
|
|
'puas' => $dataLama->where('kepuasan', 'Puas')->count(),
|
|
'tidak_puas' => $dataLama->where('kepuasan', 'Tidak Puas')->count(),
|
|
]
|
|
]);
|
|
}
|
|
|
|
|
|
public function dashboardSurvey(){
|
|
$data = [
|
|
'title' => 'List Survey Order Gizi'
|
|
];
|
|
return view('dashboard.survey.index', $data);
|
|
}
|
|
|
|
public function exportSurveyExcel(){
|
|
$startDate = Carbon::parse(request('start_date'))->startOfDay();
|
|
$endDate = Carbon::parse(request('end_date'))->endOfDay();
|
|
$data = Survey::whereBetween('created_at', [$startDate, $endDate])->get();
|
|
$waktu_cetak = Carbon::now()->locale('id')->translatedFormat('d F Y');
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setCellValue('A1', "Laporan Survei dari {$startDate->format('Y-m-d')} sampai {$endDate->format('Y-m-d')}");
|
|
$sheet->mergeCells('A1:E1');
|
|
$sheet->getStyle('A1')->getFont()->setBold(true)->setSize(14);
|
|
$sheet->getStyle('A1')->getAlignment()->setHorizontal('center');
|
|
|
|
$sheet->setCellValue('A2', "Waktu Cetak: {$waktu_cetak}");
|
|
$sheet->mergeCells('A2:E2');
|
|
$sheet->getStyle('A2')->getAlignment()->setHorizontal('center');
|
|
|
|
// Header tabel
|
|
$headers = ["No", "Nama", "Tipe Survei", "Kepuasan", "Kritik dan Saran"];
|
|
$sheet->fromArray($headers, null, 'A4');
|
|
$sheet->getStyle('A4:E4')->applyFromArray([
|
|
'font' => ['bold' => true],
|
|
'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER],
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN
|
|
]
|
|
]
|
|
]);
|
|
$sheet->getStyle('A4:E4')->getFont()->setBold(true);
|
|
$sheet->getStyle('A4:E4')->getAlignment()->setHorizontal('center');
|
|
$sheet->getStyle('A4:E4')->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
|
|
// Isi data
|
|
$row = 5;
|
|
foreach ($data as $index => $item) {
|
|
$sheet->setCellValue("A{$row}", $index + 1);
|
|
$sheet->setCellValue("B{$row}", $item->order->nama_pemesan);
|
|
$sheet->setCellValue("C{$row}", $item->type === "pelanggan_setia" ? "Pelanggan Setia" : "Pengguna Baru");
|
|
$sheet->setCellValue("D{$row}", $item->kepuasan);
|
|
$sheet->setCellValue("E{$row}", $item->keterangan);
|
|
$row++;
|
|
}
|
|
// Auto size kolom
|
|
foreach(range('A', 'E') as $col){
|
|
$sheet->getColumnDimension($col)->setAutoSize(true);
|
|
}
|
|
$lastRow = $row - 1;
|
|
$sheet->setAutoFilter("A4:E{$lastRow}");
|
|
// Download file
|
|
$fileName = "Laporan Survei {$startDate->format('Y-m-d')} sampai {$endDate->format('Y-m-d')} ". '.xlsx';
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
|
// Output ke browser
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
header("Content-Disposition: attachment; filename=\"{$fileName}\"");
|
|
header('Cache-Control: max-age=0');
|
|
|
|
$writer->save('php://output');
|
|
exit;
|
|
}
|
|
}
|