*/ public function sheets(): array { // 本采购单内有明细的门店(含软删除,保证历史单据可导出) $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'); if ($this->storeId > 0) { $query->where('store_order_item.store_id', $this->storeId); } $storeIds = $query->distinct()->pluck('store_order_item.store_id'); if ($storeIds->isEmpty()) { throw new RepositoryException( $this->storeId > 0 ? '该门店在此采购单中无采购商品' : '该采购单无门店采购明细,无法导出' ); } $stores = StoreModel::withTrashed() ->whereIn('id', $storeIds) ->orderBy('id') ->get(['id', 'name']); $usedNames = []; $sheets = []; foreach ($stores as $store) { $sheets[] = new PurchaseStoreSheet( $this->purchase, $store, SheetName::make((string) $store->name, (int) $store->id, $usedNames), ); } return $sheets; } }