138 lines
4.0 KiB
PHP
138 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Support\OwnerVerification;
|
|
|
|
class VerificationChangeFormatter
|
|
{
|
|
/**
|
|
* @var list<string>
|
|
*/
|
|
private const HIDDEN_FIELDS = [
|
|
'category_ids',
|
|
'unit',
|
|
];
|
|
|
|
/**
|
|
* @param array{old?: array<string, mixed>|null, new?: array<string, mixed>|null} $payload
|
|
* @return list<array{field: string, old: mixed, new: mixed}>
|
|
*/
|
|
public static function format(array $payload): array
|
|
{
|
|
$old = $payload['old'] ?? null;
|
|
$new = $payload['new'] ?? null;
|
|
|
|
if (! is_array($old) && ! is_array($new)) {
|
|
return [];
|
|
}
|
|
|
|
$fields = array_unique(array_merge(
|
|
array_keys(is_array($old) ? $old : []),
|
|
array_keys(is_array($new) ? $new : []),
|
|
));
|
|
|
|
$changes = [];
|
|
|
|
foreach ($fields as $field) {
|
|
if (in_array($field, self::HIDDEN_FIELDS, true)) {
|
|
continue;
|
|
}
|
|
|
|
$oldValue = is_array($old) ? ($old[$field] ?? null) : null;
|
|
$newValue = is_array($new) ? ($new[$field] ?? null) : null;
|
|
|
|
if ($oldValue === $newValue) {
|
|
continue;
|
|
}
|
|
|
|
$changes[] = [
|
|
'field' => self::label((string) $field),
|
|
'old' => self::presentValue($field, $oldValue),
|
|
'new' => self::presentValue($field, $newValue),
|
|
];
|
|
}
|
|
|
|
return $changes;
|
|
}
|
|
|
|
private static function label(string $field): string
|
|
{
|
|
return match ($field) {
|
|
'name' => 'Nama',
|
|
'description' => 'Deskripsi',
|
|
'category_names' => 'Kategori',
|
|
'supplier_name' => 'Supplier',
|
|
'supplier_id' => 'Supplier',
|
|
'subtotal' => 'Subtotal',
|
|
'discount' => 'Diskon',
|
|
'shipping_cost' => 'Ongkir',
|
|
'total' => 'Total',
|
|
'notes' => 'Keterangan',
|
|
'items' => 'Item Belanja',
|
|
'has_photos' => 'Foto Bukti',
|
|
'unit' => 'Satuan',
|
|
'unit_label' => 'Satuan',
|
|
'is_active' => 'Status Aktif',
|
|
'variants' => 'Varian',
|
|
'prices' => 'Varian Harga',
|
|
default => ucfirst(str_replace('_', ' ', $field)),
|
|
};
|
|
}
|
|
|
|
private static function presentValue(string $field, mixed $value): mixed
|
|
{
|
|
if ($value === null) {
|
|
return null;
|
|
}
|
|
|
|
if ($field === 'is_active') {
|
|
return (bool) $value;
|
|
}
|
|
|
|
if ($field === 'variants' && is_array($value)) {
|
|
return collect($value)
|
|
->map(function (array $variant): string {
|
|
$name = $variant['name'] ?? '-';
|
|
$stock = $variant['stock'] ?? 0;
|
|
|
|
return "{$name} (stok: {$stock})";
|
|
})
|
|
->implode(', ');
|
|
}
|
|
|
|
if ($field === 'prices' && is_array($value)) {
|
|
return collect($value)
|
|
->map(function (array $price): string {
|
|
$variant = $price['variant'] ?? '-';
|
|
$stock = $price['stock'] ?? 0;
|
|
$priceValue = $price['price'] ?? 0;
|
|
|
|
return "{$variant} (stok: {$stock}, harga: {$priceValue})";
|
|
})
|
|
->implode(', ');
|
|
}
|
|
|
|
if ($field === 'items' && is_array($value)) {
|
|
return collect($value)
|
|
->map(function (array $item): string {
|
|
$name = $item['raw_material_name'] ?? '-';
|
|
$variant = $item['variant'] ?? '-';
|
|
$quantity = $item['quantity'] ?? 0;
|
|
$subtotal = $item['subtotal'] ?? 0;
|
|
|
|
return "{$name} / {$variant} (jumlah: {$quantity}, subtotal: {$subtotal})";
|
|
})
|
|
->implode('; ');
|
|
}
|
|
|
|
if ($field === 'has_photos') {
|
|
return (bool) $value ? 'Ada' : 'Tidak ada';
|
|
}
|
|
|
|
if ($field === 'category_names' && is_array($value)) {
|
|
return implode(', ', $value);
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|