84 lines
2.1 KiB
PHP
84 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\InteractsWithActivityLog;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends([
|
|
'quantity_formatted',
|
|
'quantity_input',
|
|
'unit_price_formatted',
|
|
'subtotal_formatted',
|
|
'unit_abbreviation',
|
|
])]
|
|
class PurchaseItem extends Model
|
|
{
|
|
use InteractsWithActivityLog;
|
|
use SoftDeletes;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'quantity' => 'decimal:4',
|
|
'unit_price' => 'integer',
|
|
'subtotal' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function quantityFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function () {
|
|
$formatted = rtrim(rtrim(number_format((float) $this->quantity, 4, ',', '.'), '0'), ',');
|
|
|
|
return "{$formatted} {$this->rawMaterialPrice?->rawMaterial?->unit?->abbreviation()}";
|
|
},
|
|
);
|
|
}
|
|
|
|
public function quantityInput(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => rtrim(rtrim(number_format((float) $this->quantity, 4, '.', ''), '0'), '.'),
|
|
);
|
|
}
|
|
|
|
public function unitPriceFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->unit_price, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function subtotalFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->subtotal, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function unitAbbreviation(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->rawMaterialPrice?->rawMaterial?->unit?->abbreviation(),
|
|
);
|
|
}
|
|
|
|
public function purchase(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Purchase::class);
|
|
}
|
|
|
|
public function rawMaterialPrice(): BelongsTo
|
|
{
|
|
return $this->belongsTo(RawMaterialPrice::class);
|
|
}
|
|
}
|