fix bug and optimize

This commit is contained in:
Husnu Setiawan 2026-03-05 01:22:40 +07:00
parent a0d952633f
commit e984b1b672
17 changed files with 80 additions and 32 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class HomeController extends Controller
{
@ -16,6 +17,13 @@ class HomeController extends Controller
public function store(Request $request)
{
return view('home.grocery');
DB::listen(function ($query) {
logger($query->sql);
logger($query->bindings);
logger($query->time);
});
return view('home.grocery');
}
}

View File

@ -89,7 +89,8 @@ class Items extends Model
return $this->hasOne(ItemReference::class, 'item_id')
->where(function ($query) {
$query->whereNull('item_variant_id')->orWhere('item_variant_id', 0);
});
})
->with(['item','itemVariant']);
}
public function variants()
@ -326,7 +327,8 @@ class Items extends Model
public function getDisplayPriceAttribute()
{
try {
return $this->discount_price * $this->conversion;
/* try {
$convertion = $this->conversion_value();
$price = @$this->variants->first()->reference->price->price ?? null;
@ -336,7 +338,7 @@ class Items extends Model
} catch (\Exception $e) {
return 0;
}
}
*/ }
public function getDisplayPriceCurrencyAttribute()
{

View File

@ -13,6 +13,7 @@ class BrandRepository
return Cache::remember('catalog_brand_'.json_encode($params), 60, function () use ($params) {
$category_id = @$params['category_id'];
$limit = @$params['limit'];
if ($category_id) {
$ids = DB::select('select distinct brand from store_category_map a
left join item_dimension b
@ -28,12 +29,15 @@ class BrandRepository
return Brand::whereIn('name', $ids)->orderBy('priority', 'desc')
->where('priority', '>', 0)
->orderBy('name', 'asc')
->take(10)
->withCount('items')
->get();
}
return Brand::orderBy('priority', 'desc')->orderBy('name', 'asc')
->where('priority', '>', 0)
->take(10)
->withCount('items')
->get();
});

View File

@ -7,12 +7,16 @@ use DB;
use Carbon\Carbon;
use Image;
use Storage;
use Cache;
class CategoryRepository
{
public function getList($params){
return Category::get();
return Cache::remember('catalog_category_'.json_encode($params), 60, function () use ($params) {
$limit = $params["limit"] ?? 7;
return Category::take($limit)->withCount('items')->get();
});
}
}

View File

@ -8,11 +8,14 @@ use DB;
use Carbon\Carbon;
use Image;
use Storage;
use Cache;
class GenderRepository
{
public function getList($params){
return Cache::remember('catalog_gender_'.json_encode($params), 60, function () use ($params) {
$category_id = @$params["category_id"];
$brand_id = @$params["brand_id"];
if ($category_id && $brand_id){
@ -28,9 +31,10 @@ class GenderRepository
where store_category_id = ? and brand = ?
and items.is_publish = true ",[$category_id, $brand->name]);
$ids = collect($ids)->pluck("gender");
return Gender::whereIn("name",$ids)->get();
return Gender::whereIn("name",$ids)->withCount('items')->get();
}
return Gender::get();
return Gender::withCount('items')->get();
});
}
}

View File

@ -62,9 +62,19 @@ class ProductRepository
$builder = Items::select('items.*', 'percent')
$builder = Items::with([
'images',
'variants',
'reference'
])->select('items.*', 'percent')
->addSelect(DB::raw("coalesce(d.price, coalesce(pricelist.price, net_price)) as discount_price"))
->addSelect(DB::raw("to_qty / from_qty as conversion"))
->leftJoin("item_convertions", function($join){
$join->on("from_unit","=","items.unit");
$join->on("to_unit","=","items.display_unit");
})
->leftJoin('item_dimension', 'item_dimension.no', 'items.number')
->leftJoin(DB::raw("(select distinct on (item_id) item_id, percent, discounts.created_at,
->leftJoin(DB::raw("(select distinct on (item_id) item_id, percent, price, discounts.created_at,
case when discounts.valid_at is null then 'special-offer' else 'limited-sale' end as event
from discount_items
left join item_reference on item_reference_id = item_reference.id
@ -74,6 +84,17 @@ class ProductRepository
( discounts.valid_at is null or discounts.expired_at >= now())
order by item_id, discounts.created_at desc
) as d"), 'd.item_id', '=', 'items.id')
->leftJoin(DB::raw("(select distinct on (item_id) item_id, price
from discount_items
left join item_reference on item_reference_id = item_reference.id
left join discounts on discounts.id = discount_id
where discounts.type = 'price' and location_id is null and
( discounts.valid_at is null or discounts.valid_at <= now()) and
( discounts.valid_at is null or discounts.expired_at >= now())
order by item_id, discounts.created_at desc
) as pricelist"), 'pricelist.item_id', '=', 'items.id')
->when(true, function ($query) use ($event, $sort, $sorting_ids) {
if ($event) {
$query->orderByRaw("case when brand = 'CHAMELO' then 1 else 2 end ASC ");
@ -96,7 +117,8 @@ class ProductRepository
} elseif ($sort == 'new') {
$query->orderByRaw("case when category1 in ('CLUBS','CLUB','COMPONENT HEAD') and brand = 'PXG' then 1 else 2 end ASC");
} else {
$query->orderByRaw('case when d.created_at is not null then 1 else 2 end ASC, random()');
$query->orderByRaw('case when d.created_at is not null then 1 else 2 end ASC');
$query->inRandomOrder();
}
}
@ -278,7 +300,11 @@ class ProductRepository
public function show($slug)
{
$product = Items::where('slug', $slug)
$product = Items::with([
'images',
'variants',
'reference'
])->where('slug', $slug)
->when(is_int($slug), function ($query) use ($slug) {
return $query->orWhere('id', $slug);
})
@ -305,6 +331,10 @@ class ProductRepository
// Remove except_ids from the list
$ids = array_diff($ids, (array) $except_ids);
return Items::whereIn('id', $ids)->inRandomOrder()->take(10)->get();
return Items::with([
'images',
'variants',
'reference'
])->whereIn('id', $ids)->inRandomOrder()->take(10)->get();
}
}

View File

@ -16,7 +16,7 @@ class FooterBrands extends Component
*/
public function __construct(BrandRepository $brandRepository)
{
$this->brands = $brandRepository->getList([]);
$this->brands = $brandRepository->getList(["limit"=>10]);
}
/**

View File

@ -16,7 +16,7 @@ class FooterCategory extends Component
*/
public function __construct(CategoryRepository $categoryRepository)
{
$this->categories = $categoryRepository->getList([]);
$this->categories = $categoryRepository->getList(['limit' => 7]);
}
/**

View File

@ -16,7 +16,7 @@ class FeaturedCategories extends Component
*/
public function __construct(GenderRepository $genderRepository)
{
$this->genders = $genderRepository->getList([]);
$this->genders = $genderRepository->getList(['limit' => 7]);
}
/**

View File

@ -18,8 +18,8 @@ class HeaderCategoryDropdown extends Component
*/
public function __construct(CategoryRepository $categoryRepository, GenderRepository $genderRepository)
{
$this->categories = $categoryRepository->getList([]);
$this->genders = $genderRepository->getList([]);
$this->categories = $categoryRepository->getList(['limit'=>7]);
$this->genders = $genderRepository->getList(['limit'=>7]);
}
/**

View File

@ -21,7 +21,7 @@ class PopularProducts extends Component
{
//
$this->categories = $categoryRepository->getList([]);
$this->categories = $categoryRepository->getList(['limit'=>7]);
$this->products = $productRepository->getList([
'limit' => 12,
]);

View File

@ -21,8 +21,7 @@ class TopHeader extends Component
*/
public function render(): View|Closure|string
{
$brands = app(\App\Repositories\Catalog\BrandRepository::class)->getList([]);
$brands = app(\App\Repositories\Catalog\BrandRepository::class)->getList(["limit"=>10]);
return view('components.grocery.top-header', compact('brands'));
}
}

View File

@ -28,15 +28,12 @@ class NavbarCategory extends Component
$brandsRepository = new BrandRepository();
$this->genders = $genderRepository->getList([]);
$this->genders = collect($this->genders)->chunk(7);
$this->genders = $genderRepository->getList(['limit' => 7]);
$this->categories = $categoryRepository->getList([]);
$this->categories = $categoryRepository->getList(['limit' => 7]);
// chunk
$this->categories = collect($this->categories)->chunk(7);
$this->brands = $brandsRepository->getList([]);
$this->brands = collect($this->brands)->chunk(7);
$this->brands = $brandsRepository->getList(['limit' => 10]);
}
/**

View File

@ -18,7 +18,7 @@ class Categories extends Component
public function __construct()
{
$categoryRepository = new CategoryRepository();
$this->categories = $categoryRepository->getList([]);
$this->categories = $categoryRepository->getList(['limit'=>7]);
}
/**

View File

@ -29,7 +29,7 @@
<div
class="position-relative d-flex justify-content-between align-items-center h-100 bg-black rounded-5 overflow-hidden ps-2 ps-xl-3">
<div class="d-flex flex-column pt-4 px-3 pb-3">
<p class="fs-xs pb-2 mb-1">{{ $value->productCount() }} products</p>
<p class="fs-xs pb-2 mb-1">{{ $value->items_count }} products</p>
<h2 class="h5 mb-2 mb-xxl-3">{{ $value->name }}</h2>
<div class="nav">
<a class="nav-link animate-underline stretched-link text-body-emphasis text-nowrap px-0"

View File

@ -12,7 +12,7 @@
<ul class="nav flex-column gap-2 mt-n2">
@foreach ($categories as $category)
@if ($category->productCount() > 0)
@if ($category->items_count > 0)
<li class="d-flex w-100 pt-1">
<a class="nav-link animate-underline animate-target d-inline fw-normal text-truncate p-0"
href="{{ route('product.index', ['category_id', $category->id]) }}">{{ $category->name }}</a>

View File

@ -7,7 +7,7 @@
<div class="row nav g-3 g-sm-4">
@foreach ($categories as $key => $value)
@if ($value->productCount() > 0)
@if ($value->items_count > 0)
<div class="col-sm-6 col-md-4 col-lg-12 d-flex">
<div class="position-relative d-flex min-w-0 align-items-center">
<div class="d-flex flex-shrink-0 align-items-center justify-content-center bg-body-tertiary rounded-circle"
@ -19,7 +19,7 @@
href="{{ route('product.index', ['filter[category]' => $value->id]) }}">
<span class="animate-target text-truncate">{{ $value->name ?? '' }}</span>
</a>
<div class="fs-xs fw-normal text-body-secondary">{{ $value->productCount() }}
<div class="fs-xs fw-normal text-body-secondary">{{ $value->items_count }}
products
</div>
</div>