<?php
namespace App\Exports;
use App\Models\KalenderJadwal;
use App\Models\Karyawan;
use DateInterval;
use DatePeriod;
use DateTime;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class KalendersExport implements FromView,WithEvents,WithStyles,ShouldAutoSize
{
protected $karyawans;
protected $row_count = 8;
protected $column_count = 4;
public function styles(Worksheet $sheet)
{
$column = Coordinate::stringFromColumnIndex($this->column_count);
$sheet->getStyle('A8:'.$column.$this->row_count)->applyFromArray([
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['argb' => '000000'],
],
],
]);
$conditional = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT);
$conditional->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
$conditional->setText('WORK');
$conditional->getStyle()->getFont()->applyFromArray(['font' => [
'color' => array('rgb' => '006100'),
]]);
$conditional->getStyle()->getFill()->applyFromArray(['fillType' => 'solid','rotation' => 0, 'color' => ['rgb' => 'C6EFCE'],]);
// $conditional->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
// $conditional->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
$conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT);
$conditional1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
$conditional1->setText('NA');
// $conditional1->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKRED);
$conditional->getStyle()->getFont()->applyFromArray(['font' => [
'color' => array('rgb' => '9C0006'),
]]);
$conditional1->getStyle()->getFill()->applyFromArray(['fillType' => 'solid','rotation' => 0, 'color' => ['rgb' => 'FFC7CE'],]);
// $conditional1->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
// $conditional1->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
$conditional2= new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional2->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT);
$conditional2->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
$conditional2->setText('OFF');
// $conditional2->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKYELLOW);
$conditional2->getStyle()->getFont()->applyFromArray(['font' => [
'color' => array('rgb' => '9C5700'),
]]);
$conditional2->getStyle()->getFill()->applyFromArray(['fillType' => 'solid','rotation' => 0, 'color' => ['rgb' => 'FFEB9C'],]);
// $conditional2->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
// $conditional2->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_YELLOW);
$conditional3= new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional3->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT);
$conditional3->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
$conditional3->setText('CUTI');
// $conditional2->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKYELLOW);
$conditional3->getStyle()->getFont()->applyFromArray(['font' => [
'color' => array('argb' => '9C5700'),
]]);
$conditional3->getStyle()->getFill()->applyFromArray(['fillType' => 'solid','rotation' => 0, 'color' => ['rgb' => '3f8ce7'],]);
$conditional4= new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional4->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT);
$conditional4->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
$conditional4->setText('FB');
// $conditional2->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKYELLOW);
$conditional4->getStyle()->getFont()->applyFromArray(['font' => [
'color' => array('argb' => '9C5700'),
]]);
$conditional4->getStyle()->getFill()->applyFromArray(['fillType' => 'solid','rotation' => 0, 'color' => ['rgb' => '3f8ce7'],]);
$conditionalStyles = $sheet->getStyle("E9")->getConditionalStyles();
$conditionalStyles[] = $conditional;
$conditionalStyles[] = $conditional1;
$conditionalStyles[] = $conditional2;
$conditionalStyles[] = $conditional3;
$conditionalStyles[] = $conditional4;
$sheet->getStyle("E9:".$column.$this->row_count)->setConditionalStyles($conditionalStyles);
$sheet->freezePane("D9");
// $sheet->duplicateStyle(
// $sheet->getStyle('E9'),
// 'E9:'.$column.$this->row_count
// );
// $conditional = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
// $conditional->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
// $conditional->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_EQUAL);
// $conditional->addCondition("WORK");
// $conditional->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN);
// $conditional->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
// $conditional->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
// $conditionalStyles = $sheet->getStyle('E9:'.$column.$this->row_count)->getConditionalStyles();
// $conditionalStyles[] = $conditional;
// $sheet->getStyle('E9:'.$column.$this->row_count)->setConditionalStyles($conditionalStyles);
}
public function view(): View
{
# jika datanya banyak
ini_set('memory_limit','1024M');
# max execution time
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
$periode_awal = request()->periode_awal ?? date("Y-m-d", strtotime('-1 week'));
$periode_akhir = request()->periode_akhir ?? date("Y-m-d");
$begin = new DateTime($periode_awal);
$end = new DateTime($periode_akhir);
$end = $end->modify( '+1 day' );
$periods = new DatePeriod(
$begin,
new DateInterval('P1D'),
$end
);
$karyawans = Karyawan::with('departemen')->filtersInput([
'nama',
'nik',
[
'departemen'=>[
'nama_departemen'
]
]
],'search');
if(!empty(request()->perusahaan_id) && request()->perusahaan_id != 0){
// $karyawans = $karyawans->where('perusahaan_id',request()->perusahaan_id);
$karyawans = $karyawans->whereHas('perusahaans',function($wj){
$wj->where('perusahaan_id',request()->perusahaan_id);
});
}
if(!empty(request()->departemen_id) && request()->departemen_id != 0){
$karyawans = $karyawans->whereHas('departemen',function($wj){
$wj->where('departemen_id',request()->departemen_id);
});
}
if(!empty(request()->lokasi_kerja_id) && request()->lokasi_kerja_id != 0){
$karyawans = $karyawans->whereHas('lokasi_kerja',function($q){
$q->where('lokasi_kerja_id',request()->lokasi_kerja_id);
});
}
$karyawans = $karyawans->get();
$this->row_count += count($karyawans);
$this->column_count += iterator_count($periods);
$this->karyawans = $karyawans;
return view('kalender-jadwal.export', compact('karyawans','periode_awal','periode_akhir','periods'));
}
public function registerEvents(): array
{
return [
// handle by a closure.
AfterSheet::class => function(AfterSheet $event) {
$row_count = $this->row_count;
$column_count = $this->column_count;
$selects= [];
for ($i=5; $i <= $column_count; $i++) {
$selects[] = ['columns_name'=>Coordinate::stringFromColumnIndex($i),'options'=>['NA','OFF','WORK','CUTI','FB']];
}
foreach ($selects as $select){
$drop_column = $select['columns_name'];
$options = $select['options'];
// set dropdown list for first data row
$validation = $event->sheet->getCell("{$drop_column}9")->getDataValidation();
$validation->setType(DataValidation::TYPE_LIST );
$validation->setErrorStyle(DataValidation::STYLE_INFORMATION );
$validation->setAllowBlank(false);
$validation->setShowInputMessage(true);
$validation->setShowErrorMessage(true);
$validation->setShowDropDown(true);
$validation->setErrorTitle('Input error');
$validation->setError('Value is not in list.');
$validation->setPromptTitle('Pick from list');
$validation->setPrompt('Please pick a value from the drop-down list.');
$validation->setFormula1(sprintf('"%s"',implode(',',$options)));
// clone validation to remaining rows
for ($i = 9; $i <= $row_count; $i++) {
$event->sheet->getCell("{$drop_column}{$i}")->setDataValidation(clone $validation);
}
// set columns to autosize
for ($i = 4; $i <= $column_count; $i++) {
$column = Coordinate::stringFromColumnIndex($i);
$event->sheet->getColumnDimension($column)->setAutoSize(true);
}
}
},
];
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]