store/app/Models/Product.php
Yoga Pangestu cf5b300895
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (8.3) (push) Waiting to run
tests / ci (8.4) (push) Waiting to run
tests / ci (8.5) (push) Waiting to run
Refactor product status management: replace is_active with status enum
- Updated the OwnerVerificationRequest model to change pendingToggleIsActive to pendingToggleStatus, reflecting the new status structure.
- Refactored the Product model to replace is_active with status, utilizing the ProductStatus enum for better clarity and type safety.
- Modified the ProductService to accommodate the new status field, including pagination and creation logic.
- Adjusted the AnalysisService to filter products based on their status.
- Updated the VerificationChangeFormatter to handle the new status field.
- Created a migration to remove is_active from the products table and add status with appropriate default values.
- Refactored the ProductSeeder to use the new status field.
- Updated frontend components and types to reflect the changes from is_active to status.
- Adjusted tests to ensure they validate the new status logic correctly.
2026-07-18 16:34:34 +07:00

129 lines
3.8 KiB
PHP

<?php
namespace App\Models;
use App\Enums\OwnerVerificationStatus;
use App\Enums\ProductStatus;
use App\Models\Concerns\HasPendingOwnerVerification;
use App\Models\Concerns\InteractsWithActivityLog;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Sluggable\Attributes\Sluggable;
/**
* @method static \Illuminate\Database\Eloquent\Builder active()
* @method static \Illuminate\Database\Eloquent\Builder inactive()
*/
#[Guarded(['id'])]
#[Appends(['total_stock_formatted', 'total_reject_stock_formatted', 'total_retail_stock_formatted'])]
#[Sluggable(from: 'name', to: 'slug')]
class Product extends Model
{
// 1. Use Trait
use HasFactory, HasPendingOwnerVerification, InteractsWithActivityLog, SoftDeletes;
// 2. Casting
protected function casts(): array
{
return [
'status' => ProductStatus::class,
];
}
// 3. Scope (grouped by column, then alphabetical)
// Column Group: status
#[Scope]
protected function active(Builder $query): void
{
$query->where('status', ProductStatus::ACTIVE);
}
#[Scope]
protected function inactive(Builder $query): void
{
$query->where('status', ProductStatus::INACTIVE);
}
#[Scope]
protected function draft(Builder $query): void
{
$query->where('status', ProductStatus::DRAFT);
}
// 4. Attribute
public function totalRejectStockFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->relationLoaded('variants')
? number_format($this->variants->sum('reject_stock'), 0, ',', '.')
: '-',
);
}
public function totalRetailStockFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->relationLoaded('variants')
? number_format($this->variants->sum('retail_stock'), 0, ',', '.')
: '-',
);
}
public function totalStockFormatted(): Attribute
{
return Attribute::make(
get: fn () => $this->relationLoaded('variants')
? number_format($this->variants->sum('stock'), 0, ',', '.')
: '-',
);
}
// 5. Other Methods
public static function getActiveWithVariantsAndCategories(): Collection
{
return self::query()
->active()
->with([
'categories',
'variants' => fn ($query) => $query
->with('media')
->orderBy('created_at'),
])
->get();
}
// 6. Relation
public function categories(): BelongsToMany
{
return $this->belongsToMany(Category::class, 'product_categories');
}
public function ownerVerificationRequests(): MorphMany
{
return $this->morphMany(OwnerVerificationRequest::class, 'subject');
}
public function pendingOwnerVerificationRequest(): MorphOne
{
return $this->morphOne(OwnerVerificationRequest::class, 'subject')
->where('status', OwnerVerificationStatus::PENDING)
->latestOfMany();
}
public function variants(): HasMany
{
return $this->hasMany(ProductVariant::class);
}
}