feat: streamline lambda function syntax and enhance readability in RestockService

This commit is contained in:
Yoga Pangestu 2026-08-06 14:01:37 +07:00
parent fe0c565196
commit 1fc058f974

View File

@ -30,14 +30,14 @@ public function paginated(int $perPage = 25, string $search = '', string $sort =
->with([
'createdBy:id',
'createdBy.userProfile:id,user_id,full_name',
'restockItems' => fn ($q) => $q
'restockItems' => fn($q) => $q
->select(['id', 'restock_id', 'product_variant_id', 'quantity', 'unit_price', 'subtotal'])
->orderByRaw('(SELECT name FROM product_variants WHERE product_variants.id = restock_items.product_variant_id)'),
'restockItems.productVariant:id,product_id,name,stock,reject_stock,retail_stock',
'restockItems.productVariant.product:id,name',
])
->when($search, function ($q) use ($search) {
$q->whereHas('restockItems.productVariant.product', fn ($sq) => $sq->where('name', 'like', "%{$search}%"))
$q->whereHas('restockItems.productVariant.product', fn($sq) => $sq->where('name', 'like', "%{$search}%"))
->orWhere('notes', 'like', "%{$search}%");
})
->orderBy($sort, $direction)
@ -68,6 +68,7 @@ public function getForCreate(): array
'productVariants:id,product_id,name,stock,reject_stock',
'productVariants.productPrices:id,variant_id,type,price',
])
->active()
->orderBy('name')
->get()
->each(function (Product $product) {
@ -78,11 +79,11 @@ public function getForCreate(): array
: null;
$capitalPrice = $variant->productPrices
->first(fn ($price) => $price->type === PriceType::CAPITAL);
->first(fn($price) => $price->type === PriceType::CAPITAL);
$variant->capital_price = $capitalPrice?->price ?? 0;
$rejectPrice = $variant->productPrices
->first(fn ($price) => $price->type === PriceType::REJECT);
->first(fn($price) => $price->type === PriceType::REJECT);
$variant->reject_price = $rejectPrice?->price ?? 0;
});
}),
@ -92,7 +93,7 @@ public function getForCreate(): array
public function getForEdit(Restock $restock): array
{
$restock->load([
'restockItems' => fn ($q) => $q
'restockItems' => fn($q) => $q
->orderByRaw('(SELECT name FROM product_variants WHERE product_variants.id = restock_items.product_variant_id)'),
'restockItems.productVariant.product',
]);
@ -107,7 +108,7 @@ public function getForEdit(Restock $restock): array
'photo_url' => $media
? $this->s3Service->getTemporaryUrl($media->file_name)
: null,
'items' => $restock->restockItems->map(fn (RestockItem $item) => [
'items' => $restock->restockItems->map(fn(RestockItem $item) => [
'id' => $item->id,
'product_variant_id' => $item->product_variant_id,
'quantity' => $item->quantity,
@ -144,7 +145,7 @@ public function create(array $data): Restock
NotificationService::notify(
roles: ['Owner', 'Developer', 'Admin Toko'],
title: 'Restock Baru',
body: 'Restock '.($stockType === ProductStockQuality::GOOD->value ? 'produk' : 'reject').' sebesar Rp '.number_format($subtotal, 0, ',', '.').' berhasil dicatat oleh '.auth()->user()->full_name.'.',
body: 'Restock ' . ($stockType === ProductStockQuality::GOOD->value ? 'produk' : 'reject') . ' sebesar Rp ' . number_format($subtotal, 0, ',', '.') . ' berhasil dicatat oleh ' . auth()->user()->full_name . '.',
url: route('admin.manage.restocks.index'),
);
@ -218,7 +219,7 @@ private function buildItemRows(array $items, string $stockType, $now, int &$subt
->get()
->mapWithKeys(function (ProductVariant $variant) use ($priceType) {
$price = $variant->productPrices
->first(fn ($p) => $p->type === $priceType);
->first(fn($p) => $p->type === $priceType);
return [$variant->id => $price?->price ?? 0];
});