93 lines
2.8 KiB
PHP
93 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ImportLog;
|
|
use App\Models\Prize;
|
|
|
|
class PrizeImportService
|
|
{
|
|
/**
|
|
* Import baris hadiah. Jika kuantiti > 1, generate beberapa item berasingan
|
|
* (cth Hamper #1 .. Hamper #5).
|
|
*
|
|
* @param array<int, array<string, string>> $rows
|
|
*/
|
|
public function import(array $rows, string $filename, ?int $userId = null): ImportLog
|
|
{
|
|
$success = 0;
|
|
$duplicate = 0;
|
|
$failed = 0;
|
|
$errors = [];
|
|
|
|
foreach ($rows as $i => $row) {
|
|
$line = $i + 2;
|
|
|
|
$nama = trim($row['nama_hadiah'] ?? '');
|
|
if ($nama === '') {
|
|
$failed++;
|
|
$errors[] = "Baris {$line}: nama_hadiah wajib diisi.";
|
|
continue;
|
|
}
|
|
|
|
$kuantiti = max(1, (int) ($row['kuantiti'] ?? 1));
|
|
$drawOrder = (int) ($row['susunan_cabutan'] ?? ($row['draw_order'] ?? 0));
|
|
$kodHadiah = trim($row['kod_hadiah'] ?? '');
|
|
$batch = $kodHadiah !== '' ? $kodHadiah : 'B' . str_pad((string) ($i + 1), 4, '0', STR_PAD_LEFT);
|
|
|
|
try {
|
|
foreach ($this->expand($nama, $kuantiti) as $index => $itemName) {
|
|
Prize::create([
|
|
'kod_hadiah' => $kodHadiah ?: null,
|
|
'nama_hadiah' => $itemName,
|
|
'kategori' => trim($row['kategori'] ?? '') ?: null,
|
|
'nilai_anggaran' => $this->parseDecimal($row['nilai_anggaran'] ?? ''),
|
|
'draw_order' => $drawOrder,
|
|
'item_index' => $index,
|
|
'batch_kod' => $batch,
|
|
'status' => Prize::STATUS_BELUM,
|
|
]);
|
|
$success++;
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$failed++;
|
|
$errors[] = "Baris {$line}: {$e->getMessage()}";
|
|
}
|
|
}
|
|
|
|
return ImportLog::create([
|
|
'type' => 'prizes',
|
|
'filename' => $filename,
|
|
'total_rows' => count($rows),
|
|
'success_count' => $success,
|
|
'duplicate_count' => $duplicate,
|
|
'failed_count' => $failed,
|
|
'errors' => $errors ?: null,
|
|
'imported_by' => $userId,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string> diindeks dari 1
|
|
*/
|
|
public function expand(string $nama, int $kuantiti): array
|
|
{
|
|
if ($kuantiti <= 1) {
|
|
return [1 => $nama];
|
|
}
|
|
|
|
$items = [];
|
|
for ($n = 1; $n <= $kuantiti; $n++) {
|
|
$items[$n] = "{$nama} #{$n}";
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
private function parseDecimal(string $value): ?float
|
|
{
|
|
$value = trim(str_replace([',', 'RM', 'rm'], '', $value));
|
|
return $value === '' ? null : (float) $value;
|
|
}
|
|
}
|