2026-06-13 11:17:32 +03:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use App\Models\Category;
|
|
|
|
|
use App\Models\Product;
|
|
|
|
|
use Illuminate\Support\Collection;
|
2026-06-18 17:01:52 +03:30
|
|
|
use Illuminate\Support\Facades\Schema;
|
2026-06-13 11:17:32 +03:30
|
|
|
|
|
|
|
|
class CategoryService
|
|
|
|
|
{
|
2026-06-13 16:13:06 +03:30
|
|
|
/**
|
|
|
|
|
* دستههای پایه — ترتیب نمایش در ادمین و سلکت محصول.
|
|
|
|
|
* دستههای اضافی فقط از group_type محصولات اضافه میشوند.
|
|
|
|
|
*/
|
2026-06-13 15:55:28 +03:30
|
|
|
public const DEFAULT_CATEGORY_NAMES = [
|
|
|
|
|
'آبشده',
|
|
|
|
|
'مکعب',
|
|
|
|
|
'گوی',
|
|
|
|
|
'سکه',
|
|
|
|
|
'سکه پارسیان',
|
2026-06-13 16:13:06 +03:30
|
|
|
'شمش',
|
|
|
|
|
'شمش ونوس',
|
|
|
|
|
'شمش برنا',
|
2026-06-13 15:55:28 +03:30
|
|
|
'مس',
|
|
|
|
|
'نقره',
|
|
|
|
|
];
|
|
|
|
|
|
2026-06-13 11:17:32 +03:30
|
|
|
/** دستههایی که صریحاً غیرفعال شدهاند — بقیه (بدون ردیف) نمایش داده میشوند */
|
|
|
|
|
public function hiddenCategoryNames(): Collection
|
|
|
|
|
{
|
|
|
|
|
return Category::query()
|
|
|
|
|
->where('active', 0)
|
|
|
|
|
->pluck('name');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function filterProductsByVisibleCategories(Collection $products): Collection
|
|
|
|
|
{
|
|
|
|
|
$hidden = $this->hiddenCategoryNames();
|
|
|
|
|
if ($hidden->isEmpty()) {
|
|
|
|
|
return $products;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $products->filter(function ($product) use ($hidden) {
|
|
|
|
|
$group = trim((string) ($product->group_type ?? ''));
|
|
|
|
|
if ($group === '') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! Category::where('name', $group)->exists()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ! $hidden->contains($group);
|
|
|
|
|
})->values();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sortProductsForStore(Collection $products): Collection
|
|
|
|
|
{
|
|
|
|
|
return $products
|
|
|
|
|
->sort(function ($a, $b) {
|
|
|
|
|
$aInStock = (int) ($a->inventory ?? 0) > 0 ? 0 : 1;
|
|
|
|
|
$bInStock = (int) ($b->inventory ?? 0) > 0 ? 0 : 1;
|
|
|
|
|
if ($aInStock !== $bInStock) {
|
|
|
|
|
return $aInStock <=> $bInStock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ((int) ($b->inventory ?? 0)) <=> ((int) ($a->inventory ?? 0));
|
|
|
|
|
})
|
|
|
|
|
->values();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-13 16:13:06 +03:30
|
|
|
/** @return Collection<int, string> */
|
|
|
|
|
public function collectTargetCategoryNames(): Collection
|
2026-06-13 11:17:32 +03:30
|
|
|
{
|
2026-06-13 15:55:28 +03:30
|
|
|
$fromProducts = Product::query()
|
2026-06-13 11:17:32 +03:30
|
|
|
->whereNotNull('group_type')
|
|
|
|
|
->where('group_type', '!=', '')
|
|
|
|
|
->distinct()
|
|
|
|
|
->pluck('group_type');
|
|
|
|
|
|
2026-06-13 16:13:06 +03:30
|
|
|
$defaults = collect(self::DEFAULT_CATEGORY_NAMES);
|
|
|
|
|
$extras = $fromProducts
|
2026-06-13 15:55:28 +03:30
|
|
|
->map(static fn ($name) => trim((string) $name))
|
|
|
|
|
->filter()
|
2026-06-13 16:13:06 +03:30
|
|
|
->diff($defaults)
|
|
|
|
|
->sort()
|
2026-06-13 15:55:28 +03:30
|
|
|
->values();
|
|
|
|
|
|
2026-06-13 16:13:06 +03:30
|
|
|
return $defaults
|
|
|
|
|
->merge($extras)
|
|
|
|
|
->unique()
|
|
|
|
|
->values();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function syncFromProducts(): int
|
|
|
|
|
{
|
|
|
|
|
$names = $this->collectTargetCategoryNames();
|
2026-06-13 11:17:32 +03:30
|
|
|
$created = 0;
|
2026-06-23 20:35:13 +03:30
|
|
|
$maxOrder = (int) Category::max('sort_order');
|
2026-06-13 11:17:32 +03:30
|
|
|
|
|
|
|
|
foreach ($names as $name) {
|
2026-06-13 16:13:06 +03:30
|
|
|
$existing = Category::where('name', $name)->first();
|
|
|
|
|
|
|
|
|
|
if ($existing) {
|
2026-06-13 11:17:32 +03:30
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 20:35:13 +03:30
|
|
|
$maxOrder++;
|
2026-06-13 11:17:32 +03:30
|
|
|
Category::create([
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'active' => 1,
|
|
|
|
|
'show_in_filter' => 1,
|
|
|
|
|
'show_in_select' => 1,
|
2026-06-23 20:35:13 +03:30
|
|
|
'sort_order' => $maxOrder,
|
2026-06-13 11:17:32 +03:30
|
|
|
]);
|
|
|
|
|
$created++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $created;
|
|
|
|
|
}
|
2026-06-14 12:01:20 +03:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* تبهای فیلتر اپ — فقط دستههای فعال که حداقل یک محصول فعال دارند.
|
|
|
|
|
* show_in_filter دستی لازم نیست؛ از همان منطق نمایش محصولات مشتق میشود.
|
|
|
|
|
*/
|
|
|
|
|
public function clientFilterCategories(): Collection
|
|
|
|
|
{
|
|
|
|
|
$groupsWithProducts = Product::query()
|
|
|
|
|
->where('active', 1)
|
|
|
|
|
->whereNotNull('group_type')
|
|
|
|
|
->where('group_type', '!=', '')
|
|
|
|
|
->distinct()
|
|
|
|
|
->pluck('group_type')
|
|
|
|
|
->map(static fn ($name) => trim((string) $name))
|
|
|
|
|
->filter()
|
|
|
|
|
->unique()
|
|
|
|
|
->values();
|
|
|
|
|
|
|
|
|
|
if ($groupsWithProducts->isEmpty()) {
|
|
|
|
|
return collect();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 17:01:52 +03:30
|
|
|
if (! Schema::hasTable('categories')) {
|
|
|
|
|
return $this->fallbackClientCategories($groupsWithProducts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! Schema::hasColumn('categories', 'sort_order')) {
|
|
|
|
|
return $this->fallbackClientCategories($groupsWithProducts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$categories = Category::query()
|
2026-06-14 12:01:20 +03:30
|
|
|
->where('active', 1)
|
|
|
|
|
->whereIn('name', $groupsWithProducts)
|
|
|
|
|
->orderBy('sort_order')
|
|
|
|
|
->orderBy('id')
|
|
|
|
|
->get(['id', 'name', 'sort_order']);
|
2026-06-18 17:01:52 +03:30
|
|
|
|
|
|
|
|
if ($categories->isEmpty()) {
|
|
|
|
|
$this->syncFromProducts();
|
|
|
|
|
|
|
|
|
|
return Category::query()
|
|
|
|
|
->where('active', 1)
|
|
|
|
|
->whereIn('name', $groupsWithProducts)
|
|
|
|
|
->orderBy('sort_order')
|
|
|
|
|
->orderBy('id')
|
|
|
|
|
->get(['id', 'name', 'sort_order']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $categories;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** وقتی جدول categories هنوز migrate نشده — از group_type محصولات */
|
|
|
|
|
private function fallbackClientCategories(Collection $groupsWithProducts): Collection
|
|
|
|
|
{
|
|
|
|
|
$ordered = collect(self::DEFAULT_CATEGORY_NAMES)
|
|
|
|
|
->merge($groupsWithProducts)
|
|
|
|
|
->unique()
|
|
|
|
|
->values();
|
|
|
|
|
|
|
|
|
|
$order = 0;
|
|
|
|
|
|
|
|
|
|
return $ordered
|
|
|
|
|
->filter(static fn (string $name) => $groupsWithProducts->contains($name))
|
|
|
|
|
->values()
|
|
|
|
|
->map(static function (string $name) use (&$order) {
|
|
|
|
|
$order++;
|
|
|
|
|
|
|
|
|
|
return (object) [
|
|
|
|
|
'id' => $order,
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'sort_order' => $order,
|
|
|
|
|
];
|
|
|
|
|
});
|
2026-06-14 12:01:20 +03:30
|
|
|
}
|
2026-06-13 11:17:32 +03:30
|
|
|
}
|