38 lines
1.0 KiB
PHP
38 lines
1.0 KiB
PHP
<?php
|
||
|
||
namespace App\Exports;
|
||
|
||
use App\Models\BillModel;
|
||
use Illuminate\Support\Collection;
|
||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||
|
||
/**
|
||
* 门店账单批量导出:同一门店的账单合并在一张工作表,不同门店拆分为多个工作表
|
||
*/
|
||
class BillBatchExport implements WithMultipleSheets
|
||
{
|
||
/**
|
||
* @param Collection<int, BillModel> $bills 导出账单(with store)
|
||
*/
|
||
public function __construct(private readonly Collection $bills)
|
||
{
|
||
}
|
||
|
||
/**
|
||
* @return array<int, BillExport>
|
||
*/
|
||
public function sheets(): array
|
||
{
|
||
$usedNames = [];
|
||
$sheets = [];
|
||
foreach ($this->bills->groupBy('store_id') as $storeBills) {
|
||
$store = $storeBills->first()->store;
|
||
$storeId = (int) $storeBills->first()->store_id;
|
||
$sheetTitle = SheetName::make($store->name ?? ('门店#' . $storeId), $storeId, $usedNames);
|
||
$sheets[] = new BillExport($storeBills->values(), 0, $sheetTitle);
|
||
}
|
||
|
||
return $sheets;
|
||
}
|
||
}
|