update tampilan export excel dan csv
This commit is contained in:
parent
47a988d078
commit
f5d0441cd7
@ -2,80 +2,73 @@
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
class RingkasanExport implements FromCollection, WithHeadings, WithTitle, WithStyles
|
||||
class RingkasanExport implements FromCollection, WithHeadings, WithStyles
|
||||
{
|
||||
private $data;
|
||||
private $page;
|
||||
|
||||
public function __construct(iterable $data, $page = 1)
|
||||
public function __construct(iterable $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->page = $page;
|
||||
}
|
||||
|
||||
|
||||
public function collection()
|
||||
public function collection(): Collection
|
||||
{
|
||||
$collection = collect();
|
||||
$items = method_exists($this->data, 'items') ? $this->data->items() : $this->data;
|
||||
$rows = collect();
|
||||
|
||||
foreach ($items as $item) {
|
||||
$collection->push([
|
||||
'Tanggal' => $item['tanggal'] ?? '-',
|
||||
'Total Item Terjual' => $item['total_item_terjual'] ?? 0,
|
||||
'Total Berat' => $item['total_berat'] ?? 0,
|
||||
'Total Pendapatan' => $item['total_pendapatan'] ?? 0,
|
||||
'Detail Sales' => $this->formatSalesData($item['sales'] ?? []),
|
||||
foreach ($this->data as $item) {
|
||||
$tanggal = $item['tanggal'] ?? '-';
|
||||
$totalItem = $item['total_item'] ?? 0;
|
||||
$totalBerat = $item['total_berat'] ?? '0 g';
|
||||
$totalPendapatan = $item['total_pendapatan'] ?? 'Rp 0';
|
||||
|
||||
// Tambahkan detail sales per baris
|
||||
foreach ($item['sales'] ?? [] as $sale) {
|
||||
$rows->push([
|
||||
'Tanggal' => $tanggal,
|
||||
'Nama Sales' => $sale['nama'] ?? 'Sales Tidak Dikenal',
|
||||
'Item Terjual' => $sale['item_terjual'] ?? 0,
|
||||
'Berat' => $sale['berat'] ?? '-',
|
||||
'Pendapatan' => $sale['pendapatan'] ?? '-',
|
||||
]);
|
||||
}
|
||||
|
||||
// Tambahkan baris total
|
||||
$rows->push([
|
||||
'Tanggal' => $tanggal,
|
||||
'Nama Sales' => 'TOTAL',
|
||||
'Item Terjual' => $totalItem,
|
||||
'Berat' => $totalBerat,
|
||||
'Pendapatan' => $totalPendapatan,
|
||||
]);
|
||||
|
||||
// Tambahkan baris kosong biar rapi
|
||||
$rows->push(['Tanggal' => '', 'Nama Sales' => '', 'Item Terjual' => '', 'Berat' => '', 'Pendapatan' => '']);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'Tanggal',
|
||||
'Total Item Terjual',
|
||||
'Total Berat',
|
||||
'Total Pendapatan',
|
||||
'Detail Sales'
|
||||
'Nama Sales',
|
||||
'Item Terjual',
|
||||
'Berat',
|
||||
'Pendapatan',
|
||||
];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return "Ringkasan Halaman {$this->page}";
|
||||
}
|
||||
|
||||
public function styles(Worksheet $sheet)
|
||||
{
|
||||
return [
|
||||
1 => ['font' => ['bold' => true]],
|
||||
1 => ['font' => ['bold' => true]], // Header bold
|
||||
];
|
||||
}
|
||||
|
||||
private function formatSalesData($sales): string
|
||||
{
|
||||
if (empty($sales)) {
|
||||
return '-';
|
||||
}
|
||||
|
||||
$formatted = [];
|
||||
foreach ($sales as $sale) {
|
||||
$nama = $sale['nama'] ?? 'Sales Tidak Dikenal';
|
||||
$itemTerjual = $sale['item_terjual'] ?? 0;
|
||||
$pendapatan = $sale['pendapatan'] ?? '-';
|
||||
|
||||
$formatted[] = "{$nama}: {$itemTerjual} item, {$pendapatan}";
|
||||
}
|
||||
|
||||
return implode('; ', $formatted);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user