125 lines
3.9 KiB
PHP
125 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\CooperationCompletion;
|
|
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 CooperationCompletionExport implements FromCollection, WithHeadings, WithStyles, WithMapping
|
|
{
|
|
/**
|
|
* @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 = CooperationCompletion::with(['media.company']);
|
|
}else if(is_role(['3'])){
|
|
$mediaId = Auth::user()->company->media->id;
|
|
$query = CooperationCompletion::with(['media.company', 'order.cooperation'])->where('media_id', '=', $mediaId);
|
|
}
|
|
|
|
if (!empty($this->media[0])) {
|
|
$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->created_at)->locale('id')->translatedFormat('l, d F Y'),
|
|
$report->status === '0' ? 'Menunggu' : ($report->status === '1' ? 'Diterima' : 'Ditolak'),
|
|
];
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
['Penyelesaian Kerja Sama', '', '', '', '', ''],
|
|
['NO', 'Nama Kerja Sama', 'Media', 'Perusahaan', 'Tanggal Upload', 'Status']
|
|
];
|
|
}
|
|
|
|
public function styles(Worksheet $sheet)
|
|
{
|
|
$sheet->mergeCells('A1:F1'); // A1 sampai F1 digabung
|
|
|
|
// Set alignment untuk pusat horizontal dan vertikal
|
|
$sheet->getStyle('A1:F1')->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);
|
|
$sheet->getStyle('F:F')->getAlignment()->setWrapText(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);
|
|
}
|
|
}
|