125 lines
3.7 KiB
PHP
125 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace App\Exports;
|
||
|
||
use App\Models\PurchaseOrderModel;
|
||
use App\Models\StoreModel;
|
||
use App\Services\PurchaseItemService;
|
||
use Illuminate\Support\Collection;
|
||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||
|
||
/**
|
||
* 门店购买详情导出 · 单门店工作表
|
||
*/
|
||
class PurchaseStoreSheet implements FromCollection, WithStrictNullComparison, WithStyles, WithTitle
|
||
{
|
||
private ?Collection $rows = null;
|
||
|
||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/header/summary) */
|
||
private array $specialRows = [];
|
||
|
||
/** 列头所在行索引 */
|
||
private int $headerRow = 1;
|
||
|
||
public function __construct(
|
||
private readonly PurchaseOrderModel $purchase,
|
||
private readonly StoreModel $store,
|
||
private readonly string $sheetTitle,
|
||
) {
|
||
}
|
||
|
||
/**
|
||
* 导出行:标题/空行/列头/明细/合计
|
||
*/
|
||
public function collection(): Collection
|
||
{
|
||
if ($this->rows !== null) {
|
||
return $this->rows;
|
||
}
|
||
|
||
$items = app(PurchaseItemService::class)->storeSummaryRows($this->purchase->id, $this->store->id);
|
||
|
||
$rows = [];
|
||
$rowIndex = 0;
|
||
|
||
// 标题行
|
||
$rows[] = [$this->store->name . ' · 采购单 ' . $this->purchase->purchase_no];
|
||
$this->specialRows[++$rowIndex] = 'title';
|
||
|
||
// 空行
|
||
$rows[] = [''];
|
||
$rowIndex++;
|
||
|
||
// 列头
|
||
$rows[] = ['序号', '品名', '市场', '包规', '单位', '单价', '数量', '重量(斤)', '预计金额'];
|
||
$this->specialRows[++$rowIndex] = 'header';
|
||
$this->headerRow = $rowIndex;
|
||
|
||
// 明细行
|
||
$totalQuantity = 0;
|
||
$totalWeight = '0';
|
||
$totalAmount = '0';
|
||
foreach (array_values($items) as $sort => $item) {
|
||
$rows[] = [
|
||
$sort + 1,
|
||
$item['product_name'],
|
||
$item['market'],
|
||
$item['product_spec'],
|
||
$item['unit'],
|
||
(float) $item['price'],
|
||
(int) $item['quantity'],
|
||
(float) $item['weight'],
|
||
(float) $item['amount'],
|
||
];
|
||
$rowIndex++;
|
||
$totalQuantity += (int) $item['quantity'];
|
||
$totalWeight = bcadd($totalWeight, (string) $item['weight'], 3);
|
||
$totalAmount = bcadd($totalAmount, (string) $item['amount'], 2);
|
||
}
|
||
|
||
// 合计行
|
||
$rows[] = ['', '合计', '', '', '', '', $totalQuantity, (float) $totalWeight, (float) $totalAmount];
|
||
$this->specialRows[++$rowIndex] = 'summary';
|
||
|
||
return $this->rows = collect($rows);
|
||
}
|
||
|
||
public function title(): string
|
||
{
|
||
return $this->sheetTitle;
|
||
}
|
||
|
||
/**
|
||
* 标题/列头/合计加粗,冻结列头
|
||
*/
|
||
public function styles(Worksheet $sheet): array
|
||
{
|
||
$this->collection();
|
||
|
||
$sheet->freezePane('A' . ($this->headerRow + 1));
|
||
$widths = [6, 24, 12, 14, 8, 10, 10, 12, 12];
|
||
foreach ($widths as $index => $width) {
|
||
$sheet->getColumnDimension(\PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($index + 1))
|
||
->setWidth($width);
|
||
}
|
||
|
||
$styles = [];
|
||
foreach ($this->specialRows as $row => $type) {
|
||
$style = match ($type) {
|
||
'title' => ['font' => ['bold' => true, 'size' => 14]],
|
||
'header', 'summary' => ['font' => ['bold' => true]],
|
||
default => [],
|
||
};
|
||
if ($style !== []) {
|
||
$styles[$row] = $style;
|
||
}
|
||
}
|
||
|
||
return $styles;
|
||
}
|
||
}
|