simedkom/app/Exports/CooperationArchiveExport.php
2025-05-28 11:09:44 +07:00

110 lines
3.5 KiB
PHP

<?php
namespace App\Exports;
use App\Models\Cooperation;
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 CooperationArchiveExport implements FromCollection, WithHeadings, WithStyles, WithMapping
{
/**
* @return \Illuminate\Support\Collection
*/
protected $no = 1;
protected $year;
protected $month;
protected $date;
public function __construct($year, $month, $date)
{
$this->year = $year;
$this->month = $month;
$this->date = $date;
}
public function collection()
{
$query = Cooperation::with(['media', 'mediaOrder']);
if (is_role(['1'])) {
// Hanya untuk role 1: filter kerja sama lebih dari 1 tahun
if (!$this->year && !$this->month && !$this->date) {
$query->where('created_at', '<=', Carbon::now()->subYear());
}
} else if (is_role(['3'])) {
$mediaId = Auth::user()->company->media->id;
$query->whereHas('proposals', function ($query) use ($mediaId) {
$query->where('status', 1)->where('media_id', $mediaId);
});
if (!$this->year && !$this->month && !$this->date) {
$query->where('created_at', '<=', Carbon::now()->subYear());
}
}
if ($this->year) {
$query->whereYear('created_at', $this->year);
}
if ($this->month) {
$monthYear = Carbon::parse($this->month);
$query->whereYear('created_at', $monthYear->year)
->whereMonth('created_at', $monthYear->month);
}
if ($this->date) {
$query->whereDate('created_at', $this->date);
}
return $query->get();
}
public function map($cooperationArchive): array
{
return [
$this->no++,
$cooperationArchive->name,
Carbon::parse($cooperationArchive->start_date)->locale('id')->translatedFormat('l, d F Y'),
Carbon::parse($cooperationArchive->end_date)->locale('id')->translatedFormat('l, d F Y'),
strip_tags($cooperationArchive->description),
Carbon::parse($cooperationArchive->created_at)->locale('id')->translatedFormat('l, d F Y'),
];
}
public function headings(): array
{
return [
['Data Rekap Konten', '', '', '', '', ''],
['NO', 'Nama Kerjasama', 'Taanggal Mulai', 'Tanggal Selesai', 'Deskripsi', 'Dibuat Pada']
];
}
public function styles(Worksheet $sheet)
{
$sheet->mergeCells('A1:I1'); // A1 sampai D1 digabung
// Set alignment untuk pusat horizontal dan vertikal
$sheet->getStyle('A1:J1')->getAlignment()->setHorizontal('center');
$sheet->getStyle('A1:F1')->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);
// Menambahkan gaya lain jika diperlukan
$sheet->getStyle('A1:F1')->getFont()->setBold(true);
$sheet->getStyle('A1:F1')->getFont()->setSize(14);
}
}