Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
6
Indexable
Never
<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

class PeronServiceExport implements 
FromView,
ShouldAutoSize,
WithStyles,
WithMapping,
WithColumnFormatting
{
    use Exportable;
    protected $data;

    public function __construct(Collection $data)
    {
        $this->data = $data;
    }

    public function styles(Worksheet $sheet)
    {
        $totalColumn = $this->data->count() + 2;
        $styleArray = [
            'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN]],
        ];

        $sheet->getStyle('A1:H' . $totalColumn)->applyFromArray($styleArray);
        return [
            1    => [
                'font' => ['bold' => true, 'size' => 14],
                'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN]],
                'alignment' => ['vertical' => 'center', 'horizontal' => 'center']
            ],
        ];
    }

    public function columnFormats(): array
    {
        return [

        ];
    }

    public function map($item): array
    {
        return [
            'vm_location' => $item->vm->name,
            'vm_id' => $item->vm->id_vm,
            'uppd' => $item->vm->uppd,
            'ref_number' => $item->ref_number,
            'peron_price' => $item->peron_price,
            'tickets_count' => $item->tickets_count,
            'total' => (int)$item->tickets_count * (int)$item->peron_price,
            'created_at' => $item->created_at->format('Ymd'),
        ];
    }

    public function view(): View
    {
        return view('export-peron-service-laporan', [
            'reports' => $this->data->map(fn ($it) => $this->map($it))
        ]);
    }
}