*/ public function sheets(): array { // 本采购单内有明细的供应商(按订货明细快照 supplier_id 归集) $query = StoreOrderItemModel::query() ->join('store_order', 'store_order.id', '=', 'store_order_item.order_id') ->where('store_order_item.purchase_id', $this->purchase->id) ->whereNull('store_order.deleted_at') ->where('store_order_item.supplier_id', '>', 0); if ($this->supplierId > 0) { $query->where('store_order_item.supplier_id', $this->supplierId); } $supplierIds = $query->distinct()->pluck('store_order_item.supplier_id'); if ($supplierIds->isEmpty()) { throw new RepositoryException( $this->supplierId > 0 ? '该供应商在此采购单中无采购明细' : '该采购单无供应商采购明细,无法导出' ); } $suppliers = SupplierModel::withTrashed() ->whereIn('id', $supplierIds) ->orderBy('id') ->get(['id', 'name']); $service = app(PurchaseItemService::class); $usedNames = []; $sheets = []; foreach ($suppliers as $supplier) { // 按市场拆分工作表 $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; } }