Add sample import CSV and support image download

This commit is contained in:
Arya Dwi Putra
2025-12-03 08:36:50 +07:00
parent 3c7cb12041
commit 403364a6ad
3 changed files with 37 additions and 2 deletions

View File

@@ -256,6 +256,7 @@ class AssetController extends Controller
'quantity' => (int) ($row['quantity'] ?? 1),
'available_quantity' => (int) ($row['available_quantity'] ?? $row['quantity'] ?? 1),
'is_pool' => !empty($row['is_pool']),
'image_url' => $row['image_url'] ?? null,
],
'target_id' => $targetId,
];
@@ -279,16 +280,39 @@ class AssetController extends Controller
foreach ($rows as $row) {
if ($row['status'] === 'invalid') { $invalid++; continue; }
$payload = $row['payload'];
$imageUrl = $payload['image_url'] ?? null;
unset($payload['image_url']);
$assetModel = null;
if ($row['target_id']) {
Asset::where('id', $row['target_id'])->update($payload);
$assetModel = Asset::find($row['target_id']);
$updated++;
} else {
if (!$payload['code']) {
$payload['code'] = 'IMP-'.Str::upper(Str::random(6));
}
Asset::create($payload + ['qr_token' => Str::uuid()]);
$assetModel = Asset::create($payload + ['qr_token' => Str::uuid()]);
$created++;
}
// Download image jika disediakan
if ($assetModel && $imageUrl) {
try {
$resp = \Illuminate\Support\Facades\Http::get($imageUrl);
if ($resp->ok()) {
$path = "assets/{$assetModel->id}/import-".Str::random(6).".jpg";
\Illuminate\Support\Facades\Storage::disk('public')->put($path, $resp->body());
\App\Models\AssetPhoto::create([
'asset_id' => $assetModel->id,
'path' => $path,
'is_primary' => $assetModel->photos()->count() === 0,
]);
}
} catch (\Throwable $e) {
// abaikan error download agar import tetap lanjut
}
}
}
return back()->with('success', "Import selesai. Create: {$created}, Update: {$updated}, Invalid: {$invalid}");

View File

@@ -0,0 +1,6 @@
code,name,serial_number,status,category,location,rfid_tag,nfc_tag,is_consumable,quantity,available_quantity,is_pool,image_url
AST-90001,Laptop Dell XPS 13,SN-XPS-90001,Active,Laptop,Warehouse,RFID-10001,NFC-10001,0,1,1,0,https://images.unsplash.com/photo-1517336714731-489689fd1ca8?auto=format&fit=crop&w=800&q=80
AST-90002,Printer HP LaserJet,SN-HP-90002,Active,Printer,Warehouse,RFID-10002,NFC-10002,0,1,1,0,https://images.unsplash.com/photo-1582719478250-c89cae4dc85b?auto=format&fit=crop&w=800&q=80
AST-90003,Kursi Ergonomis,SN-CHAIR-90003,Active,Furniture,Office A,RFID-10003,NFC-10003,0,10,10,1,https://images.unsplash.com/photo-1433838552652-f9a46b332c40?auto=format&fit=crop&w=800&q=80
AST-90004,Tinta Printer Hitam,,Active,Consumable,Warehouse,,,1,50,50,0,https://images.unsplash.com/photo-1504384308090-c894fdcc538d?auto=format&fit=crop&w=800&q=80
AST-90005,Router Mikrotik CCR,SN-RT-90005,Active,Network,Data Center,RFID-10005,NFC-10005,0,1,1,0,https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&w=800&q=80
1 code name serial_number status category location rfid_tag nfc_tag is_consumable quantity available_quantity is_pool image_url
2 AST-90001 Laptop Dell XPS 13 SN-XPS-90001 Active Laptop Warehouse RFID-10001 NFC-10001 0 1 1 0 https://images.unsplash.com/photo-1517336714731-489689fd1ca8?auto=format&fit=crop&w=800&q=80
3 AST-90002 Printer HP LaserJet SN-HP-90002 Active Printer Warehouse RFID-10002 NFC-10002 0 1 1 0 https://images.unsplash.com/photo-1582719478250-c89cae4dc85b?auto=format&fit=crop&w=800&q=80
4 AST-90003 Kursi Ergonomis SN-CHAIR-90003 Active Furniture Office A RFID-10003 NFC-10003 0 10 10 1 https://images.unsplash.com/photo-1433838552652-f9a46b332c40?auto=format&fit=crop&w=800&q=80
5 AST-90004 Tinta Printer Hitam Active Consumable Warehouse 1 50 50 0 https://images.unsplash.com/photo-1504384308090-c894fdcc538d?auto=format&fit=crop&w=800&q=80
6 AST-90005 Router Mikrotik CCR SN-RT-90005 Active Network Data Center RFID-10005 NFC-10005 0 1 1 0 https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&w=800&q=80

View File

@@ -325,7 +325,12 @@
<form action="{{ route('assets.import') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="modal-body">
<p class="text-muted">Unggah file CSV (Excel bisa ekspor ke CSV). Header yang didukung: <code>code,name,serial_number,status,category,location,rfid_tag,nfc_tag,is_consumable,quantity,available_quantity,is_pool</code>. Baris tanpa nama akan ditandai invalid.</p>
<p class="text-muted">
Unggah file CSV (Excel bisa ekspor ke CSV). Header yang didukung:
<code>code,name,serial_number,status,category,location,rfid_tag,nfc_tag,is_consumable,quantity,available_quantity,is_pool,image_url</code>.
Baris tanpa nama akan ditandai invalid. Contoh file:
<a href="{{ asset('samples/assets_import_sample.csv') }}" target="_blank">assets_import_sample.csv</a>
</p>
<div class="mb-3">
<label class="form-label">File</label>
<input type="file" name="file" class="form-control" accept=".csv,.txt" required>