修复BUG
This commit is contained in:
@@ -130,7 +130,7 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
'product_spec' => $spec,
|
||||
'unit' => (string) ($snapshot->unit ?? $product->unit),
|
||||
'cost_price' => (float) ($snapshot->cost_price ?? $product->cost_price),
|
||||
// 参考零售价 = 加权平均售价 ÷ 包规数值(无订货行无售价数据,留空)
|
||||
// 单价 = 加权平均售价 ÷ 包规数值(无订货行无售价数据,留空)
|
||||
'retail_price' => $group !== null && (float) $quantity > 0
|
||||
? $this->unitRefPrice((float) bcdiv($amount, $quantity, 4), $spec)
|
||||
: null,
|
||||
@@ -173,7 +173,7 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
|
||||
// 列头
|
||||
$rows[] = array_merge(
|
||||
['序号', '分类', '品名', '供应商', '市场', '包规', '单位', '成本', '参考零售价', '数量', '实际称重', '金额'],
|
||||
['序号', '分类', '品名', '供应商', '市场', '包规', '单位', '成本', '单价', '数量', '实际称重', '金额'],
|
||||
array_values($this->storeNames),
|
||||
);
|
||||
$this->specialRows[++$rowIndex] = 'header';
|
||||
|
||||
@@ -6,11 +6,13 @@ use App\Exceptions\RepositoryException;
|
||||
use App\Models\PurchaseOrderModel;
|
||||
use App\Models\StoreOrderItemModel;
|
||||
use App\Models\SupplierModel;
|
||||
use App\Services\PurchaseItemService;
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
|
||||
/**
|
||||
* 供应商采购明细导出:多供应商合并为一个 XLSX(每供应商一个工作表,工作表名=供应商名称);
|
||||
* 构造传入 supplierId 时仅导出该供应商(单工作表)
|
||||
* 供应商采购明细导出:多供应商合并为一个 XLSX,按「供应商 × 市场」拆分工作表
|
||||
* (工作表名=供应商·市场,市场取商品档案,未设置市场归入「未设置」);
|
||||
* 构造传入 supplierId 时仅导出该供应商
|
||||
*/
|
||||
class PurchaseSupplierExport implements WithMultipleSheets
|
||||
{
|
||||
@@ -51,14 +53,23 @@ class PurchaseSupplierExport implements WithMultipleSheets
|
||||
->orderBy('id')
|
||||
->get(['id', 'name']);
|
||||
|
||||
$service = app(PurchaseItemService::class);
|
||||
$usedNames = [];
|
||||
$sheets = [];
|
||||
foreach ($suppliers as $supplier) {
|
||||
$sheets[] = new PurchaseSupplierSheet(
|
||||
$this->purchase,
|
||||
$supplier,
|
||||
SheetName::make((string) $supplier->name, (int) $supplier->id, $usedNames),
|
||||
);
|
||||
// 按市场拆分工作表
|
||||
$matrix = $service->supplierMarketMatrix($this->purchase->id, (int) $supplier->id);
|
||||
foreach ($matrix['markets'] as $market => $rows) {
|
||||
$marketLabel = $market === '' ? '未设置' : $market;
|
||||
$sheets[] = new PurchaseSupplierSheet(
|
||||
$this->purchase,
|
||||
$supplier,
|
||||
$marketLabel,
|
||||
$rows,
|
||||
$matrix['stores'],
|
||||
SheetName::make($supplier->name . '·' . $marketLabel, (int) $supplier->id, $usedNames),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $sheets;
|
||||
|
||||
@@ -4,20 +4,25 @@ namespace App\Exports;
|
||||
|
||||
use App\Models\PurchaseOrderModel;
|
||||
use App\Models\SupplierModel;
|
||||
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\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
/**
|
||||
* 供应商采购明细导出 · 单供应商工作表(成本口径:金额=Σ数量×成本价)
|
||||
* 供应商采购明细导出 · 单市场工作表
|
||||
* 模板列序:品名、汇总、市场、各门店明细(门店列=该供应商有明细的门店);
|
||||
* 有数据的单元格(品名/汇总/门店数量)填充突出颜色并加粗
|
||||
*/
|
||||
class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison, WithStyles, WithTitle
|
||||
{
|
||||
/** 有数据单元格填充色(浅黄,突出显示) */
|
||||
private const string DATA_FILL = 'FFFFFF99';
|
||||
|
||||
private ?Collection $rows = null;
|
||||
|
||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/header/summary) */
|
||||
@@ -26,15 +31,29 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
/** 列头所在行索引 */
|
||||
private int $headerRow = 1;
|
||||
|
||||
/** @var array<int, array<int, int>> 有数据的单元格:行索引(1 起)=> 列索引(1 起)列表 */
|
||||
private array $dataCells = [];
|
||||
|
||||
/**
|
||||
* @param PurchaseOrderModel $purchase 采购单
|
||||
* @param SupplierModel $supplier 供应商
|
||||
* @param string $marketLabel 市场名(空市场已转为「未设置」)
|
||||
* @param array<int, array<string, mixed>> $productRows 商品行(品名/汇总数量/各门店数量)
|
||||
* @param array<int, string> $stores 门店列(门店ID => 名称)
|
||||
* @param string $sheetTitle 工作表名
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly PurchaseOrderModel $purchase,
|
||||
private readonly SupplierModel $supplier,
|
||||
private readonly string $marketLabel,
|
||||
private readonly array $productRows,
|
||||
private readonly array $stores,
|
||||
private readonly string $sheetTitle,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出行:标题/空行/列头/明细/合计
|
||||
* 导出行:标题/空行/列头(品名、汇总、市场、各门店)/明细/合计
|
||||
*/
|
||||
public function collection(): Collection
|
||||
{
|
||||
@@ -42,48 +61,52 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
return $this->rows;
|
||||
}
|
||||
|
||||
$items = app(PurchaseItemService::class)->supplierRows($this->purchase->id, $this->supplier->id);
|
||||
$storeIds = array_map('intval', array_keys($this->stores));
|
||||
|
||||
$rows = [];
|
||||
$rowIndex = 0;
|
||||
|
||||
// 标题行
|
||||
$rows[] = [$this->supplier->name . ' · 采购单 ' . $this->purchase->purchase_no];
|
||||
$rows[] = [$this->supplier->name . ' · ' . $this->marketLabel . ' · 采购单 ' . $this->purchase->purchase_no];
|
||||
$this->specialRows[++$rowIndex] = 'title';
|
||||
|
||||
// 空行
|
||||
$rows[] = [''];
|
||||
$rowIndex++;
|
||||
|
||||
// 列头
|
||||
$rows[] = ['序号', '品名', '市场', '包规', '单位', '成本价', '数量', '重量(斤)', '金额'];
|
||||
// 列头:品名、汇总、市场、各门店明细
|
||||
$rows[] = array_merge(['品名', '汇总', '市场'], array_values($this->stores));
|
||||
$this->specialRows[++$rowIndex] = 'header';
|
||||
$this->headerRow = $rowIndex;
|
||||
|
||||
// 明细行
|
||||
// 明细行(有数据的单元格记录到 dataCells:品名/汇总/门店数量)
|
||||
$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['cost_price'],
|
||||
(int) $item['quantity'],
|
||||
(float) $item['weight'],
|
||||
(float) $item['amount'],
|
||||
$storeTotals = array_fill_keys($storeIds, 0);
|
||||
foreach ($this->productRows as $productRow) {
|
||||
$line = [
|
||||
$productRow['product_name'],
|
||||
(int) $productRow['quantity'],
|
||||
$this->marketLabel,
|
||||
];
|
||||
$rowIndex++;
|
||||
$totalQuantity += (int) $item['quantity'];
|
||||
$totalWeight = bcadd($totalWeight, (string) $item['weight'], 3);
|
||||
$totalAmount = bcadd($totalAmount, (string) $item['amount'], 2);
|
||||
$filled = [1, 2, 3];
|
||||
foreach ($storeIds as $position => $storeId) {
|
||||
$quantity = (int) ($productRow['store_quantities'][$storeId] ?? 0);
|
||||
$line[] = $quantity > 0 ? $quantity : '';
|
||||
if ($quantity > 0) {
|
||||
$filled[] = 4 + $position;
|
||||
$storeTotals[$storeId] += $quantity;
|
||||
}
|
||||
}
|
||||
$rows[] = $line;
|
||||
$this->dataCells[++$rowIndex] = $filled;
|
||||
$totalQuantity += (int) $productRow['quantity'];
|
||||
}
|
||||
|
||||
// 合计行
|
||||
$rows[] = ['', '合计', '', '', '', '', $totalQuantity, (float) $totalWeight, (float) $totalAmount];
|
||||
$rows[] = array_merge(
|
||||
['合计', $totalQuantity, ''],
|
||||
array_map(static fn (int $storeId): int => $storeTotals[$storeId], $storeIds),
|
||||
);
|
||||
$this->specialRows[++$rowIndex] = 'summary';
|
||||
|
||||
return $this->rows = collect($rows);
|
||||
@@ -95,17 +118,33 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
}
|
||||
|
||||
/**
|
||||
* 标题/列头/合计加粗,冻结列头
|
||||
* 标题/列头/合计加粗,有数据的单元格填充突出颜色,冻结列头
|
||||
*/
|
||||
public function styles(Worksheet $sheet): array
|
||||
{
|
||||
$this->collection();
|
||||
|
||||
$sheet->freezePane('A' . ($this->headerRow + 1));
|
||||
$widths = [6, 24, 12, 14, 8, 10, 10, 12, 12];
|
||||
|
||||
$widths = [24, 10, 12];
|
||||
foreach ($widths as $index => $width) {
|
||||
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex($index + 1))->setWidth($width);
|
||||
}
|
||||
$storeCount = count($this->stores);
|
||||
for ($i = 0; $i < $storeCount; $i++) {
|
||||
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex(4 + $i))->setWidth(12);
|
||||
}
|
||||
|
||||
// 有数据的单元格:填充突出颜色并加粗(含品名/汇总/门店数量)
|
||||
foreach ($this->dataCells as $rowIndex => $columns) {
|
||||
foreach ($columns as $columnIndex) {
|
||||
$style = $sheet->getStyle(Coordinate::stringFromColumnIndex($columnIndex) . $rowIndex);
|
||||
$style->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getStartColor()->setARGB(self::DATA_FILL);
|
||||
$style->getFont()->setBold(true);
|
||||
}
|
||||
}
|
||||
|
||||
$styles = [];
|
||||
foreach ($this->specialRows as $row => $type) {
|
||||
|
||||
Reference in New Issue
Block a user