diff --git a/app/Http/Controllers/Admin/WarehouseController.php b/app/Http/Controllers/Admin/WarehouseController.php index 4df4cf4..2ce49e4 100644 --- a/app/Http/Controllers/Admin/WarehouseController.php +++ b/app/Http/Controllers/Admin/WarehouseController.php @@ -722,7 +722,10 @@ class WarehouseController extends Controller ? \App\Models\System\Company::active()->orderBy('name')->get() : collect(); - return view('admin.warehouses.machine-inventory', compact('machines', 'companies')); + // 供批次匯出 modal 勾選用的完整機台清單(全域 scope 已限可存取機台) + $exportMachines = Machine::select('id', 'name', 'serial_no')->orderBy('name')->get(); + + return view('admin.warehouses.machine-inventory', compact('machines', 'companies', 'exportMachines')); } @@ -801,6 +804,122 @@ class WarehouseController extends Controller return response()->streamDownload($callback, $filename, ['Content-Type' => $contentType]); } + /** + * 批次匯出「機台庫存概覽」——每台機台一個分頁(Excel sheet)/一個檔(CSV),內容為該機台每貨道明細。 + * Excel = 多分頁 xlsx(OpenSpout);CSV 無分頁概念,故每台一個 .csv 打包成 ZIP。 + * ids:逗號字串或陣列;空則匯出全部可存取機台。全域 scope 確保只含可存取機台。 + */ + public function machineInventoryExportBatch(Request $request) + { + $isExcel = ($request->input('export', 'csv') === 'excel'); + + $ids = $request->input('ids'); + if (is_string($ids)) { + $ids = array_filter(array_map('intval', explode(',', $ids))); + } elseif (is_array($ids)) { + $ids = array_filter(array_map('intval', $ids)); + } else { + $ids = []; + } + + $query = Machine::with(['slots' => fn ($q) => $q->with('product:id,name,barcode')->orderByRaw('CAST(slot_no AS UNSIGNED) ASC')]) + ->orderBy('name'); + if (!empty($ids)) { + $query->whereIn('id', $ids); + } + $machines = $query->get(); + + $headers = ['貨道號', '商品名稱', '商品條碼', '類型', '目前庫存', '滿庫容量', '庫存率(%)', '效期', '批號', '啟用', '鎖定']; + $ts = now()->format('YmdHis'); + + // 單台的資料列 + $rowsOf = function ($machine) { + $rows = []; + foreach ($machine->slots as $slot) { + $stock = (int) $slot->stock; + $capacity = (int) $slot->max_stock; + $rate = $capacity > 0 ? round($stock / $capacity * 100) : 0; + $rows[] = [ + (string) $slot->slot_no, + $slot->product->name ?? '', + $slot->product->barcode ?? '', + (string) $slot->type, + $stock, + $capacity, + $rate, + $slot->expiry_date ? $slot->expiry_date->format('Y-m-d') : '', + (string) $slot->batch_no, + $slot->is_active ? '是' : '否', + $slot->is_locked ? '是' : '否', + ]; + } + return $rows; + }; + + // 分頁/檔名(機台名稱_序號,去非法字元、限長、去重;Excel sheet 名上限 31 字) + $labelOf = function ($machine, array &$used) { + $b = trim(($machine->name ?? '機台') . '_' . ($machine->serial_no ?? $machine->id)); + $b = preg_replace('/[\\\\\\/\\?\\*\\[\\]:]/u', '_', $b); + $b = mb_substr($b, 0, 28); + $name = $b; + $i = 1; + while (in_array($name, $used, true)) { + $name = mb_substr($b, 0, 24) . '_' . (++$i); + } + $used[] = $name; + return $name; + }; + + if ($isExcel) { + $tmp = tempnam(sys_get_temp_dir(), 'invxlsx'); + $writer = new \OpenSpout\Writer\XLSX\Writer(); + $writer->openToFile($tmp); + $used = []; + $first = true; + foreach ($machines as $machine) { + $sheet = $first ? $writer->getCurrentSheet() : $writer->addNewSheetAndMakeItCurrent(); + $first = false; + $sheet->setName($labelOf($machine, $used)); + $writer->addRow(\OpenSpout\Common\Entity\Row::fromValues($headers)); + foreach ($rowsOf($machine) as $row) { + $writer->addRow(\OpenSpout\Common\Entity\Row::fromValues($row)); + } + } + if ($first) { + $writer->getCurrentSheet()->setName('機台庫存'); + $writer->addRow(\OpenSpout\Common\Entity\Row::fromValues($headers)); + } + $writer->close(); + + return response()->download($tmp, "機台庫存_{$ts}.xlsx", [ + 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + ])->deleteFileAfterSend(true); + } + + // CSV:每台一個 .csv 打包成 ZIP + $tmp = tempnam(sys_get_temp_dir(), 'invzip'); + $zip = new \ZipArchive(); + $zip->open($tmp, \ZipArchive::OVERWRITE); + $used = []; + foreach ($machines as $machine) { + $fh = fopen('php://temp', 'r+'); + fputcsv($fh, $headers); + foreach ($rowsOf($machine) as $row) { + fputcsv($fh, $row); + } + rewind($fh); + $content = "\xEF\xBB\xBF" . stream_get_contents($fh); + fclose($fh); + $zip->addFromString($labelOf($machine, $used) . '.csv', $content); + } + if ($machines->isEmpty()) { + $zip->addFromString('機台庫存.csv', "\xEF\xBB\xBF" . implode(',', $headers) . "\n"); + } + $zip->close(); + + return response()->download($tmp, "機台庫存_{$ts}.zip")->deleteFileAfterSend(true); + } + /** * AJAX:取得單台機台貨道詳情 */ diff --git a/lang/en.json b/lang/en.json index c172046..ebb8007 100644 --- a/lang/en.json +++ b/lang/en.json @@ -779,6 +779,9 @@ "Export Report": "Export Report", "Export": "Export", "Export to CSV": "Export to CSV", + "Export Machine Inventory": "Export Machine Inventory", + "Please select at least one machine": "Please select at least one machine", + "No machines": "No machines", "Export to Excel": "Export to Excel", "External URL": "External URL", "Failed": "Failed", diff --git a/lang/zh_TW.json b/lang/zh_TW.json index 4b307d6..569580e 100644 --- a/lang/zh_TW.json +++ b/lang/zh_TW.json @@ -780,6 +780,9 @@ "Export Report": "匯出報表", "Export": "匯出", "Export to CSV": "匯出成 CSV", + "Export Machine Inventory": "匯出機台庫存", + "Please select at least one machine": "請至少選擇一台機台", + "No machines": "沒有機台", "Export to Excel": "匯出成 Excel", "External URL": "外連 URL", "Failed": "失敗", diff --git a/resources/views/admin/warehouses/machine-inventory.blade.php b/resources/views/admin/warehouses/machine-inventory.blade.php index 6ec0fdf..897b917 100644 --- a/resources/views/admin/warehouses/machine-inventory.blade.php +++ b/resources/views/admin/warehouses/machine-inventory.blade.php @@ -85,6 +85,13 @@ d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /> + @@ -96,6 +103,60 @@ {{-- Detail Mode --}} + {{-- 機台庫存批次匯出 Modal --}} +
{{ __('No machines') }}
+ @endforelse +