simedkom/app/Exports/ReportExport.php

112 lines
3.6 KiB
PHP

<?php
namespace App\Exports;
use App\Models\Report;
use Maatwebsite\Excel\Concerns\FromCollection;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
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, WithStyles, WithMapping
{
/**
* @return \Illuminate\Support\Collection
*/
protected $no = 1;
protected $media;
protected $year;
protected $from_month;
protected $to_month;
protected $date;
public function __construct($media, $year, $from_month, $to_month, $date)
{
$this->media = $media;
$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']);
}else if(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->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 Kerjasama', '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);
}
}