182 lines
5.7 KiB
PHP
182 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Master;
|
|
|
|
use App\Models\Category;
|
|
use App\Models\Product;
|
|
use App\Models\ProductPrice;
|
|
use App\Models\ProductVariant;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ProductService
|
|
{
|
|
/**
|
|
* @param array{search: string, sort: string, direction: 'asc'|'desc'} $tableQuery
|
|
*/
|
|
public function paginateForIndex(array $tableQuery, string $isActive): LengthAwarePaginator
|
|
{
|
|
$query = Product::query()
|
|
->with([
|
|
'categories',
|
|
'variants' => fn ($query) => $query
|
|
->with(['prices' => fn ($query) => $query->orderBy('type')])
|
|
->orderBy('created_at'),
|
|
])
|
|
->when($tableQuery['search'] !== '', function (Builder $query) use ($tableQuery): void {
|
|
$search = $tableQuery['search'];
|
|
$query->where(function (Builder $query) use ($search): void {
|
|
$query->where('name', 'like', "%{$search}%")
|
|
->orWhere('slug', 'like', "%{$search}%")
|
|
->orWhere('description', 'like', "%{$search}%")
|
|
->orWhereHas('categories', fn (Builder $query) => $query->where('name', 'like', "%{$search}%"))
|
|
->orWhereHas('variants', fn (Builder $query) => $query->where('name', 'like', "%{$search}%"));
|
|
});
|
|
})
|
|
->when(
|
|
$isActive !== '',
|
|
fn (Builder $query) => $query->where('is_active', $isActive === '1')
|
|
);
|
|
|
|
$this->applySorting($query, $tableQuery['sort'], $tableQuery['direction']);
|
|
|
|
return $query
|
|
->paginate(10)
|
|
->withQueryString();
|
|
}
|
|
|
|
/**
|
|
* @return list<array{value: int, label: string}>
|
|
*/
|
|
public function categoryOptions(): array
|
|
{
|
|
return Category::query()
|
|
->orderBy('name')
|
|
->get(['id', 'name'])
|
|
->map(fn (Category $category) => [
|
|
'value' => $category->id,
|
|
'label' => $category->name,
|
|
])
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $validated
|
|
*/
|
|
public function create(array $validated): void
|
|
{
|
|
DB::transaction(function () use ($validated): void {
|
|
$product = Product::create([
|
|
'name' => $validated['name'],
|
|
'description' => $validated['description'] ?? null,
|
|
'is_active' => true,
|
|
]);
|
|
|
|
$product->categories()->sync($validated['category_ids']);
|
|
|
|
foreach ($validated['variants'] as $variantData) {
|
|
$this->createVariant($product, $variantData);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $validated
|
|
*/
|
|
public function update(Product $product, array $validated): void
|
|
{
|
|
DB::transaction(function () use ($validated, $product): void {
|
|
$product->name = $validated['name'];
|
|
$product->description = $validated['description'] ?? null;
|
|
$product->save();
|
|
|
|
$product->categories()->sync($validated['category_ids']);
|
|
|
|
$submittedVariantIds = collect($validated['variants'])
|
|
->pluck('id')
|
|
->filter()
|
|
->map(fn ($id) => (int) $id)
|
|
->all();
|
|
|
|
$product->variants()
|
|
->whereNotIn('id', $submittedVariantIds)
|
|
->get()
|
|
->each(fn (ProductVariant $variant) => $variant->delete());
|
|
|
|
foreach ($validated['variants'] as $variantData) {
|
|
if (! empty($variantData['id'])) {
|
|
$variant = $product->variants()->findOrFail($variantData['id']);
|
|
$variant->name = $variantData['name'];
|
|
$variant->stock = $variantData['stock'];
|
|
$variant->save();
|
|
$this->syncVariantPrices($variant, $variantData['prices']);
|
|
|
|
continue;
|
|
}
|
|
|
|
$this->createVariant($product, $variantData);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function toggleStatus(Product $product, bool $isActive): void
|
|
{
|
|
$product->is_active = $isActive;
|
|
$product->save();
|
|
}
|
|
|
|
public function delete(Product $product): void
|
|
{
|
|
DB::transaction(function () use ($product): void {
|
|
$product->variants()->delete();
|
|
$product->categories()->detach();
|
|
$product->delete();
|
|
});
|
|
}
|
|
|
|
private function applySorting(Builder $query, string $sort, string $direction): void
|
|
{
|
|
if (in_array($sort, ['name', 'slug', 'is_active'], true)) {
|
|
$query->orderBy($sort, $direction);
|
|
|
|
return;
|
|
}
|
|
|
|
$query->latest();
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $variantData
|
|
*/
|
|
private function createVariant(Product $product, array $variantData): ProductVariant
|
|
{
|
|
$variant = $product->variants()->create([
|
|
'name' => $variantData['name'],
|
|
'stock' => $variantData['stock'],
|
|
]);
|
|
|
|
$this->syncVariantPrices($variant, $variantData['prices']);
|
|
|
|
return $variant;
|
|
}
|
|
|
|
/**
|
|
* @param list<array{type: string, price: float|int|string}> $prices
|
|
*/
|
|
private function syncVariantPrices(ProductVariant $variant, array $prices): void
|
|
{
|
|
foreach ($prices as $priceData) {
|
|
ProductPrice::updateOrCreate(
|
|
[
|
|
'variant_id' => $variant->id,
|
|
'type' => $priceData['type'],
|
|
],
|
|
[
|
|
'price' => $priceData['price'],
|
|
],
|
|
);
|
|
}
|
|
}
|
|
}
|