diff --git a/app/Models/Product.php b/app/Models/Product.php index ec9cd4c..e39f671 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -58,21 +58,27 @@ protected function inactive(Builder $query): void public function totalRejectStockFormatted(): Attribute { return Attribute::make( - get: fn () => number_format($this->variants->sum('reject_stock'), 0, ',', '.'), + get: fn () => $this->relationLoaded('variants') + ? number_format($this->variants->sum('reject_stock'), 0, ',', '.') + : '-', ); } public function totalRetailStockFormatted(): Attribute { return Attribute::make( - get: fn () => number_format($this->variants->sum('retail_stock'), 0, ',', '.'), + get: fn () => $this->relationLoaded('variants') + ? number_format($this->variants->sum('retail_stock'), 0, ',', '.') + : '-', ); } public function totalStockFormatted(): Attribute { return Attribute::make( - get: fn () => number_format($this->variants->sum('stock'), 0, ',', '.'), + get: fn () => $this->relationLoaded('variants') + ? number_format($this->variants->sum('stock'), 0, ',', '.') + : '-', ); } diff --git a/app/Models/RawMaterial.php b/app/Models/RawMaterial.php index bab7b12..740a057 100644 --- a/app/Models/RawMaterial.php +++ b/app/Models/RawMaterial.php @@ -66,12 +66,14 @@ public function unitLabel(): Attribute public function totalInventoryValueFormatted(): Attribute { return Attribute::make( - get: fn () => 'Rp '.number_format( - $this->prices->sum(fn (RawMaterialPrice $price) => (float) $price->stock * (int) $price->price), - 0, - ',', - '.', - ), + get: fn () => $this->relationLoaded('prices') + ? 'Rp '.number_format( + $this->prices->sum(fn (RawMaterialPrice $price) => (float) $price->stock * (int) $price->price), + 0, + ',', + '.', + ) + : '-', ); } @@ -79,6 +81,9 @@ public function totalStockFormatted(): Attribute { return Attribute::make( get: function () { + if (! $this->relationLoaded('prices')) { + return '-'; + } $total = $this->prices->sum(fn (RawMaterialPrice $price) => (float) $price->stock); $formatted = rtrim(rtrim(number_format($total, 4, ',', '.'), '0'), ','); diff --git a/app/Services/Master/ProductService.php b/app/Services/Master/ProductService.php index 47ed00e..d0aa14c 100644 --- a/app/Services/Master/ProductService.php +++ b/app/Services/Master/ProductService.php @@ -38,17 +38,14 @@ public function paginateForIndex(array $tableQuery, string $isActive, string $ca 'pendingOwnerVerificationRequest.submittedBy.profile', 'variants' => fn ($query) => $query ->with(['media', 'prices' => fn ($query) => $query->orderBy('type')]) - ->orderBy('created_at'), + ->orderBy('created_at') + ->when($tableQuery['search'] !== '', function ($query) use ($tableQuery): void { + $query->where('name', 'like', "%{$tableQuery['search']}%"); + }), ]) ->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}%")); - }); + $query->whereHas('variants', fn (Builder $query) => $query->where('name', 'like', "%{$search}%")); }) ->when($isActive !== '', fn (Builder $query) => $query->where('is_active', $isActive === '1')) ->when($categoryId !== '', function (Builder $query) use ($categoryId): void {