store/app/Models/RawMaterialPrice.php
Yoga Pangestu 9089b32d7b
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: streamline variant and price loading in product-related services
- Removed unnecessary ordering by 'created_at' in variant loading across multiple services and models.
- Implemented global scopes for ordering product variants and raw material prices by 'name' and 'variant', respectively.
- Enhanced code readability and maintainability by simplifying query structures in ProductController, OrderService, and others.
2026-07-29 15:06:16 +07:00

109 lines
2.9 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\HasModuleMedia;
use App\Models\Concerns\InteractsWithActivityLog;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
#[Guarded(['id'])]
#[Appends(['price_formatted', 'price_input', 'stock_formatted', 'stock_input'])]
class RawMaterialPrice extends Model implements HasMedia
{
// 1. Use Trait
use HasFactory, HasModuleMedia, InteractsWithActivityLog, SoftDeletes;
protected static function booted(): void
{
static::addGlobalScope('ordered', function (Builder $query): void {
$query->orderBy('variant');
});
}
// 2. Casting
protected function casts(): array
{
return [
'price' => 'integer',
'stock' => 'decimal:4',
];
}
// 3. Attribute
public function priceFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format($this->price, 0, ',', '.'),
);
}
public function priceInput(): Attribute
{
return Attribute::make(
get: fn () => (string) $this->price,
);
}
public function stockFormatted(): Attribute
{
return Attribute::make(
get: function () {
$formatted = rtrim(rtrim(number_format((float) $this->stock, 4, ',', '.'), '0'), ',');
$unitAbbreviation = $this->attributes['unit_abbreviation']
?? $this->rawMaterial?->unit?->abbreviation();
return "{$formatted} {$unitAbbreviation}";
},
);
}
public function stockInput(): Attribute
{
return Attribute::make(
get: fn () => rtrim(rtrim(number_format((float) $this->stock, 4, '.', ''), '0'), '.'),
);
}
// 4. Other Methods
public static function mediaModuleName(): string
{
return 'raw-material';
}
public function registerMediaCollections(): void
{
$this->addMediaCollection('images');
}
// 5. Relation
public function cuttingMaterials(): HasMany
{
return $this->hasMany(CuttingMaterial::class);
}
public function purchaseItems(): HasMany
{
return $this->hasMany(PurchaseItem::class);
}
public function rawMaterial(): BelongsTo
{
return $this->belongsTo(RawMaterial::class)->withTrashed();
}
public function stockMutations(): MorphMany
{
return $this->morphMany(StockMutation::class, 'stockable');
}
}