feat: streamline lambda function syntax and enhance readability in RestockService
This commit is contained in:
parent
fe0c565196
commit
1fc058f974
@ -30,14 +30,14 @@ public function paginated(int $perPage = 25, string $search = '', string $sort =
|
|||||||
->with([
|
->with([
|
||||||
'createdBy:id',
|
'createdBy:id',
|
||||||
'createdBy.userProfile:id,user_id,full_name',
|
'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'])
|
->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)'),
|
->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:id,product_id,name,stock,reject_stock,retail_stock',
|
||||||
'restockItems.productVariant.product:id,name',
|
'restockItems.productVariant.product:id,name',
|
||||||
])
|
])
|
||||||
->when($search, function ($q) use ($search) {
|
->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}%");
|
->orWhere('notes', 'like', "%{$search}%");
|
||||||
})
|
})
|
||||||
->orderBy($sort, $direction)
|
->orderBy($sort, $direction)
|
||||||
@ -68,6 +68,7 @@ public function getForCreate(): array
|
|||||||
'productVariants:id,product_id,name,stock,reject_stock',
|
'productVariants:id,product_id,name,stock,reject_stock',
|
||||||
'productVariants.productPrices:id,variant_id,type,price',
|
'productVariants.productPrices:id,variant_id,type,price',
|
||||||
])
|
])
|
||||||
|
->active()
|
||||||
->orderBy('name')
|
->orderBy('name')
|
||||||
->get()
|
->get()
|
||||||
->each(function (Product $product) {
|
->each(function (Product $product) {
|
||||||
@ -78,11 +79,11 @@ public function getForCreate(): array
|
|||||||
: null;
|
: null;
|
||||||
|
|
||||||
$capitalPrice = $variant->productPrices
|
$capitalPrice = $variant->productPrices
|
||||||
->first(fn ($price) => $price->type === PriceType::CAPITAL);
|
->first(fn($price) => $price->type === PriceType::CAPITAL);
|
||||||
$variant->capital_price = $capitalPrice?->price ?? 0;
|
$variant->capital_price = $capitalPrice?->price ?? 0;
|
||||||
|
|
||||||
$rejectPrice = $variant->productPrices
|
$rejectPrice = $variant->productPrices
|
||||||
->first(fn ($price) => $price->type === PriceType::REJECT);
|
->first(fn($price) => $price->type === PriceType::REJECT);
|
||||||
$variant->reject_price = $rejectPrice?->price ?? 0;
|
$variant->reject_price = $rejectPrice?->price ?? 0;
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
@ -92,7 +93,7 @@ public function getForCreate(): array
|
|||||||
public function getForEdit(Restock $restock): array
|
public function getForEdit(Restock $restock): array
|
||||||
{
|
{
|
||||||
$restock->load([
|
$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)'),
|
->orderByRaw('(SELECT name FROM product_variants WHERE product_variants.id = restock_items.product_variant_id)'),
|
||||||
'restockItems.productVariant.product',
|
'restockItems.productVariant.product',
|
||||||
]);
|
]);
|
||||||
@ -107,7 +108,7 @@ public function getForEdit(Restock $restock): array
|
|||||||
'photo_url' => $media
|
'photo_url' => $media
|
||||||
? $this->s3Service->getTemporaryUrl($media->file_name)
|
? $this->s3Service->getTemporaryUrl($media->file_name)
|
||||||
: null,
|
: null,
|
||||||
'items' => $restock->restockItems->map(fn (RestockItem $item) => [
|
'items' => $restock->restockItems->map(fn(RestockItem $item) => [
|
||||||
'id' => $item->id,
|
'id' => $item->id,
|
||||||
'product_variant_id' => $item->product_variant_id,
|
'product_variant_id' => $item->product_variant_id,
|
||||||
'quantity' => $item->quantity,
|
'quantity' => $item->quantity,
|
||||||
@ -144,7 +145,7 @@ public function create(array $data): Restock
|
|||||||
NotificationService::notify(
|
NotificationService::notify(
|
||||||
roles: ['Owner', 'Developer', 'Admin Toko'],
|
roles: ['Owner', 'Developer', 'Admin Toko'],
|
||||||
title: 'Restock Baru',
|
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'),
|
url: route('admin.manage.restocks.index'),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -218,7 +219,7 @@ private function buildItemRows(array $items, string $stockType, $now, int &$subt
|
|||||||
->get()
|
->get()
|
||||||
->mapWithKeys(function (ProductVariant $variant) use ($priceType) {
|
->mapWithKeys(function (ProductVariant $variant) use ($priceType) {
|
||||||
$price = $variant->productPrices
|
$price = $variant->productPrices
|
||||||
->first(fn ($p) => $p->type === $priceType);
|
->first(fn($p) => $p->type === $priceType);
|
||||||
|
|
||||||
return [$variant->id => $price?->price ?? 0];
|
return [$variant->id => $price?->price ?? 0];
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user