Add all files
This commit is contained in:
79
app/Models/Plan.php
Normal file
79
app/Models/Plan.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Classes\Price as PriceClass;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Plan extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'type',
|
||||
'billing_period',
|
||||
'billing_unit',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the available prices of the plan.
|
||||
*/
|
||||
public function prices()
|
||||
{
|
||||
return $this->hasMany(Price::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the priceable model of the plan.
|
||||
*/
|
||||
public function priceable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price of the plan.
|
||||
*/
|
||||
public function price()
|
||||
{
|
||||
if ($this->type === 'free') {
|
||||
return new PriceClass(['currency' => Currency::find(session('currency', config('settings.default_currency')))], free: true);
|
||||
}
|
||||
$currency = session('currency', config('settings.default_currency'));
|
||||
$price = $this->prices->where('currency_code', $currency)->first();
|
||||
|
||||
return new PriceClass((object) [
|
||||
'price' => $price,
|
||||
'setup_fee' => $price->setup_fee,
|
||||
'currency' => $price->currency,
|
||||
]);
|
||||
}
|
||||
|
||||
// Time between billing periods
|
||||
public function billingDuration(): Attribute
|
||||
{
|
||||
if ($this->type === 'free' || $this->type == 'one-time') {
|
||||
return Attribute::make(get: fn () => 0);
|
||||
}
|
||||
$diffInDays = match ($this->billing_unit) {
|
||||
'day' => 1,
|
||||
'week' => 7,
|
||||
'month' => 30,
|
||||
'year' => 365,
|
||||
};
|
||||
|
||||
return Attribute::make(
|
||||
get: fn () => $diffInDays * $this->billing_period
|
||||
);
|
||||
}
|
||||
|
||||
public function services()
|
||||
{
|
||||
return $this->hasMany(Service::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user