107 lines
3.3 KiB
PHP
107 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Cooperation;
|
|
use App\Models\Media;
|
|
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 CooperationExport 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(['3'])) {
|
|
$mediaId = Auth::user()->company->media->id;
|
|
|
|
$query->whereHas('proposals', function ($q) use ($mediaId) {
|
|
$q->where('status', 1)
|
|
->where('media_id', $mediaId);
|
|
});
|
|
}
|
|
|
|
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($cooperation): array
|
|
{
|
|
return [
|
|
$this->no++,
|
|
$cooperation->name,
|
|
Carbon::parse($cooperation->start_date)->locale('id')->translatedFormat('l, d F Y'),
|
|
Carbon::parse($cooperation->end_date)->locale('id')->translatedFormat('l, d F Y'),
|
|
strip_tags($cooperation->description),
|
|
$cooperation->media->pluck('name')->implode(', '),
|
|
Carbon::parse($cooperation->created_at)->locale('id')->translatedFormat('l, d F Y'),
|
|
];
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
['Data Rekap Konten', '', '', '', '', '', ''],
|
|
['NO', 'Nama Kerja Sama', 'Tanggal Mulai', 'Tanggal Selesai', 'Deskripsi', 'Media', 'Dibuat Pada']
|
|
];
|
|
}
|
|
|
|
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->getStyle('F:F')->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);
|
|
}
|
|
}
|