Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions app/Models/Plan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Plan extends Model
{
use HasFactory;

protected $fillable = [
'name',
'slug',
'description',
'is_active',
'price_monthly',
'price_yearly',
'stripe_monthly_plan_id',
'stripe_yearly_plan_id',
'paystack_monthly_plan_id',
'paystack_yearly_plan_id',
'trial_days',
'max_schools',
'max_students',
'max_teachers',
'max_parents',
'features',
];

protected $casts = [
'is_active' => 'boolean',
'price_monthly' => 'decimal:2',
'price_yearly' => 'decimal:2',
'features' => 'array',
];

/**
* Get the subscriptions for the plan.
*/
public function subscriptions(): HasMany
{
return $this->hasMany(Subscription::class);
}

/**
* Get the monthly price formatted.
*/
public function getMonthlyPriceFormattedAttribute(): string
{
return '$' . number_format($this->price_monthly, 2);
}

/**
* Get the yearly price formatted.
*/
public function getYearlyPriceFormattedAttribute(): string
{
return '$' . number_format($this->price_yearly, 2);
}

/**
* Get active plans.
*/
public function scopeActive($query)
{
return $query->where('is_active', true);
}
}
17 changes: 16 additions & 1 deletion app/Models/School.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@

namespace App\Models;

use App\Models\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class School extends BaseModel
{
use HasFactory;
use HasFactory, BelongsToTenant;

protected $fillable = [
'tenant_id',
'name',
'address',
'phone',
'email',
'website',
'logo',
];

/**
* Get the users for the school.
*/
public function users(): HasMany
{
return $this->hasMany(User::class);
Expand Down
33 changes: 33 additions & 0 deletions app/Models/Scopes/TenantScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Spatie\Multitenancy\Models\Tenant;

class TenantScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
if (! Tenant::checkCurrent()) {
return;
}

$builder->where($model->getTable() . '.tenant_id', Tenant::current()->id);
}

/**
* Extend the query builder with the needed functions.
*/
public function extend(Builder $builder): void
{
$builder->macro('withoutTenantScope', function (Builder $builder) {
return $builder->withoutGlobalScope($this);
});
}
}
75 changes: 75 additions & 0 deletions app/Models/Subscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Models;

use App\Models\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Subscription extends Model
{
use HasFactory, BelongsToTenant;

protected $fillable = [
'tenant_id',
'plan_id',
'name',
'stripe_id',
'stripe_status',
'stripe_price',
'paystack_id',
'paystack_status',
'paystack_plan',
'quantity',
'trial_ends_at',
'ends_at',
];

protected $casts = [
'trial_ends_at' => 'datetime',
'ends_at' => 'datetime',
];

/**
* Get the tenant that owns the subscription.
*/
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}

/**
* Get the plan that the subscription belongs to.
*/
public function plan(): BelongsTo
{
return $this->belongsTo(Plan::class);
}

/**
* Determine if the subscription is active.
*/
public function active(): bool
{
return $this->stripe_status === 'active' ||
$this->paystack_status === 'active' ||
$this->onTrial();
}

/**
* Determine if the subscription is on trial.
*/
public function onTrial(): bool
{
return $this->trial_ends_at && $this->trial_ends_at->isFuture();
}

/**
* Determine if the subscription has ended.
*/
public function ended(): bool
{
return $this->ends_at && $this->ends_at->isPast();
}
}
88 changes: 88 additions & 0 deletions app/Models/Tenant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Multitenancy\Models\Tenant as BaseTenant;

class Tenant extends BaseTenant
{
use HasFactory, SoftDeletes;

protected $fillable = [
'plan_id',
'name',
'domain',
'database',
'subdomain',
'logo',
'email',
'phone',
'address',
'city',
'state',
'country',
'zip_code',
'timezone',
'locale',
'currency',
'active',
'trial_ends_at',
];

protected $casts = [
'active' => 'boolean',
'trial_ends_at' => 'datetime',
];

/**
* Get the plan that the tenant belongs to.
*/
public function plan(): BelongsTo
{
return $this->belongsTo(Plan::class);
}

/**
* Get the schools for the tenant.
*/
public function schools(): HasMany
{
return $this->hasMany(School::class);
}

/**
* Get the users for the tenant.
*/
public function users(): HasMany
{
return $this->hasMany(User::class);
}

/**
* Get the subscriptions for the tenant.
*/
public function subscriptions(): HasMany
{
return $this->hasMany(Subscription::class);
}

/**
* Check if the tenant is on trial.
*/
public function onTrial(): bool
{
return $this->trial_ends_at && $this->trial_ends_at->isFuture();
}

/**
* Check if the tenant's trial has ended.
*/
public function trialEnded(): bool
{
return $this->trial_ends_at && $this->trial_ends_at->isPast();
}
}
32 changes: 32 additions & 0 deletions app/Models/Traits/BelongsToTenant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Models\Traits;

use App\Models\Scopes\TenantScope;
use App\Models\Tenant;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

trait BelongsToTenant
{
/**
* The "booted" method of the model.
*/
protected static function bootBelongsToTenant(): void
{
static::addGlobalScope(new TenantScope);

static::creating(function ($model) {
if (! $model->tenant_id && \Spatie\Multitenancy\Models\Tenant::checkCurrent()) {
$model->tenant_id = \Spatie\Multitenancy\Models\Tenant::current()->id;
}
});
}

/**
* Get the tenant that owns the model.
*/
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}
}
11 changes: 10 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use App\Events\StudentPromoted;
use App\Models\Traits\BelongsToTenant;

#[ScopedBy([SchoolScope::class])]
class User extends Authenticatable implements FilamentUser, HasName, CanResetPassword
{
use HasApiTokens, HasFactory, Notifiable, HasSuperAdmin, SoftDeletes;
use HasApiTokens, HasFactory, Notifiable, HasSuperAdmin, SoftDeletes, BelongsToTenant;

public static string $TEACHER_ROLE = 'Teacher';
public static string $STUDENT_ROLE = 'Student';
Expand Down Expand Up @@ -609,4 +610,12 @@ public function canBeImpersonated()
User::$ADMIN_ROLE
]);
}

/**
* Get the tenant that owns the user.
*/
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}
}
29 changes: 29 additions & 0 deletions app/Multitenancy/DomainTenantFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Multitenancy;

use Illuminate\Http\Request;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\TenantFinder\TenantFinder;

class DomainTenantFinder extends TenantFinder
{
public function findForRequest(Request $request): ?Tenant
{
$host = $request->getHost();

// Check for a subdomain
$parts = explode('.', $host);
if (count($parts) > 2) {
$subdomain = $parts[0];
return Tenant::query()
->where('subdomain', $subdomain)
->first();
}

// Check for a custom domain
return Tenant::query()
->where('domain', $host)
->first();
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"opcodesio/log-viewer": "^3.8",
"saade/filament-fullcalendar": "^3.0",
"spatie/laravel-db-snapshots": "^2.7",
"spatie/laravel-multitenancy": "^3.2",
"stechstudio/filament-impersonate": "^3.14",
"unicodeveloper/laravel-paystack": "^1.1",
"z3d0x/filament-logger": "^0.7.2"
Expand Down
Loading