Files
CKB/app/Models/WorkDealerPrice.php

81 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class WorkDealerPrice extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'work_id',
'dealer_id',
'price',
'currency',
'is_active'
];
protected $casts = [
'price' => 'decimal:2',
'is_active' => 'boolean',
];
/**
* Get the work associated with the price
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function work()
{
return $this->belongsTo(Work::class);
}
/**
* Get the dealer associated with the price
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function dealer()
{
return $this->belongsTo(Dealer::class);
}
/**
* Scope to get only active prices
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActive($query)
{
return $query->where('is_active', true);
}
/**
* Get formatted price with currency
*
* @return string
*/
public function getFormattedPriceAttribute()
{
return number_format($this->price, 0, ',', '.') . ' ' . $this->currency;
}
/**
* Get price for specific work and dealer
*
* @param int $workId
* @param int $dealerId
* @return WorkDealerPrice|null
*/
public static function getPriceForWorkAndDealer($workId, $dealerId)
{
return static::where('work_id', $workId)
->where('dealer_id', $dealerId)
->active()
->first();
}
}