simedkom/app/Exports/ReportExport.php
2025-10-13 08:35:36 +07:00

133 lines
4.1 KiB
PHP

<?php
namespace App\Exports;
use App\Models\Report;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class ReportExport implements FromCollection, WithHeadings, WithMapping, WithStyles
{
/**
* @return \Illuminate\Support\Collection
*/
protected $no = 1;
protected $media;
protected $status;
protected $year;
protected $from_month;
protected $to_month;
protected $date;
public function __construct($media, $status, $year, $from_month, $to_month, $date)
{
$this->media = $media;
$this->status = $status;
$this->year = $year;
$this->from_month = $from_month;
$this->to_month = $to_month;
$this->date = $date;
}
public function collection()
{
if (is_role(['1', '5'])) {
$query = Report::with(['media.company', 'order.cooperation']);
} elseif (is_role(['3'])) {
$mediaId = Auth::user()->company->media->id;
$query = Report::with(['media.company', 'order.cooperation'])->where('media_id', '=', $mediaId);
}
if (! empty($this->media) && is_array($this->media)) {
$query->whereIn('media_id', $this->media);
}
if ($this->status) {
switch ($this->status) {
case 'waiting':
$query->where('status', '0');
break;
case 'accepted':
$query->where('status', '1');
break;
case 'rejected':
$query->where('status', '2');
break;
}
}
if ($this->year) {
$query->whereYear('created_at', $this->year);
}
if ($this->from_month && $this->to_month) {
$start = Carbon::parse($this->from_month)->startOfMonth();
$end = Carbon::parse($this->to_month)->endOfMonth();
$query->whereBetween('created_at', [$start, $end]);
}
if ($this->date) {
$query->whereDate('created_at', $this->date);
}
return $query->get();
}
public function map($report): array
{
return [
$this->no++,
$report->order->cooperation->name,
$report->media->name,
$report->media->company->name,
Carbon::parse($report->publication_date)->locale('id')->translatedFormat('l, d F Y'),
Carbon::parse($report->created_at)->locale('id')->translatedFormat('l, d F Y'),
$report->status === '0' ? 'Menunggu' : ($report->status === '1' ? 'Diterima' : 'Ditolak'),
];
}
public function headings(): array
{
return [
['Laporan Bukti Tayang', '', '', '', '', '', '', ''],
['NO', 'Nama Kerja Sama', 'Media', 'Perusahaan', 'Tanggal Upload', 'Tanggal Melaporkan', 'Status'],
];
}
public function styles(Worksheet $sheet)
{
$sheet->mergeCells('A1:G1'); // A1 sampai G1 digabung
// Set alignment untuk pusat horizontal dan vertikal
$sheet->getStyle('A1:G1')->getAlignment()->setHorizontal('center');
$sheet->getStyle('A1:G1')->getAlignment()->setVertical('center');
$sheet->getColumnDimension('A')->setWidth(7); // Set column A width
$sheet->getColumnDimension('B')->setAutoSize(true);
$sheet->getColumnDimension('C')->setAutoSize(true);
$sheet->getColumnDimension('D')->setAutoSize(true);
$sheet->getColumnDimension('E')->setAutoSize(true);
$sheet->getColumnDimension('F')->setAutoSize(true);
$sheet->getColumnDimension('G')->setAutoSize(true);
$sheet->getStyle('G:G')->getAlignment()->setWrapText(true);
$sheet->getColumnDimension('G')->setAutoSize(true);
// Menambahkan gaya lain jika diperlukan
$sheet->getStyle('A1:G1')->getFont()->setBold(true);
$sheet->getStyle('A1:G1')->getFont()->setSize(14);
}
}