134 lines
4.5 KiB
PHP
134 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Company;
|
|
use Carbon\Carbon;
|
|
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 CompanyExport implements FromCollection, WithHeadings, WithStyles, WithMapping
|
|
{
|
|
/**
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
protected $no = 1;
|
|
protected $status;
|
|
protected $edit_status;
|
|
protected $address;
|
|
protected $register_year;
|
|
protected $register_month;
|
|
protected $register_date;
|
|
|
|
public function __construct($status, $edit_status, $address, $register_year, $register_month, $register_date)
|
|
{
|
|
$this->status = $status;
|
|
$this->edit_status = $edit_status;
|
|
$this->address = $address;
|
|
$this->register_year = $register_year;
|
|
$this->register_month = $register_month;
|
|
$this->register_date = $register_date;
|
|
}
|
|
public function collection()
|
|
{
|
|
$query = Company::with(['user']);
|
|
|
|
if($this->status !== 'all'){
|
|
if($this->status === 'waiting'){
|
|
$query->whereHas('user', function ($q){
|
|
$q->where('status', '0');
|
|
});
|
|
}else if($this->status === 'accepted'){
|
|
$query->whereHas('user', function ($q){
|
|
$q->where('status', '1');
|
|
});
|
|
}else if($this->status === 'rejected'){
|
|
$query->whereHas('user', function ($q){
|
|
$q->where('status', '2');
|
|
});
|
|
}
|
|
}
|
|
|
|
if($this->edit_status !== 'all'){
|
|
if($this->edit_status === 'no-edit'){
|
|
$query->where('edit_status', '0');
|
|
}elseif($this->edit_status === 'waiting'){
|
|
$query->where('edit_status', '1');
|
|
}else if($this->edit_status === 'accepted'){
|
|
$query->where('edit_status', '2');
|
|
}else if($this->edit_status === 'rejected'){
|
|
$query->where('edit_status', '3');
|
|
}
|
|
}
|
|
|
|
if ($this->address) {
|
|
$query->where('address', 'LIKE', '%'.$this->address.'%');
|
|
}
|
|
|
|
if ($this->register_year) {
|
|
$query->whereYear('created_at', $this->register_year);
|
|
}
|
|
|
|
if ($this->register_month) {
|
|
$monthYear = Carbon::parse($this->register_month);
|
|
$query->whereYear('created_at', $monthYear->year)
|
|
->whereMonth('created_at', $monthYear->month);
|
|
}
|
|
|
|
if ($this->register_date) {
|
|
$query->whereDate('created_at', $this->register_date);
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
public function map($company): array
|
|
{
|
|
return [
|
|
$this->no++,
|
|
$company->name,
|
|
$company->email,
|
|
$company->director_name,
|
|
$company->address,
|
|
get_user_status($company->user->status),
|
|
get_edit_status($company->edit_status),
|
|
Carbon::parse($company->created_at)->locale('id')->translatedFormat('l, d F Y'),
|
|
Carbon::parse($company->validated_at)->locale('id')->translatedFormat('l, d F Y')
|
|
];
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
['Data Perusahaan', '', '', '', '', '', '', '', '', ''],
|
|
['NO', 'Nama Perusahaan', 'Email', 'Direktur', 'Alamat', 'Status', 'Status Edit', 'Tanggal Daftar', 'Tanggal Validasi']
|
|
];
|
|
}
|
|
|
|
public function styles(Worksheet $sheet)
|
|
{
|
|
$sheet->mergeCells('A1:I1'); // A1 sampai D1 digabung
|
|
|
|
// Set alignment untuk pusat horizontal dan vertikal
|
|
$sheet->getStyle('A1:I1')->getAlignment()->setHorizontal('center');
|
|
$sheet->getStyle('A1:I1')->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->getColumnDimension('H')->setAutoSize(true);
|
|
$sheet->getColumnDimension('I')->setAutoSize(true);
|
|
|
|
// Menambahkan gaya lain jika diperlukan
|
|
$sheet->getStyle('A1:I1')->getFont()->setBold(true);
|
|
$sheet->getStyle('A1:I1')->getFont()->setSize(14);
|
|
}
|
|
}
|