Files
WMS/Modules/Platform/app/Models/Company.php
T

74 lines
1.9 KiB
PHP

<?php
namespace Modules\Platform\app\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Modules\Warehouse\app\Models\Warehouse;
class Company extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'uuid', 'name', 'slug', 'status', 'timezone', 'settings',
'is_system_company', 'stripe_customer_id', 'billing_email',
];
protected $casts = [
'settings' => 'array',
'is_system_company' => 'boolean',
];
protected static function booted(): void
{
static::creating(function (Company $company): void {
$company->uuid ??= (string) Str::uuid();
$company->slug ??= Str::slug($company->name);
$company->status ??= 'active';
$company->timezone ??= 'Europe/London';
});
}
public function users(): HasMany
{
return $this->hasMany(\App\Models\User::class);
}
public function licenseKeys(): HasMany
{
return $this->hasMany(LicenseKey::class);
}
public function payments(): HasMany
{
return $this->hasMany(Payment::class);
}
public function warehouses(): HasMany
{
return $this->hasMany(Warehouse::class);
}
public function hasActiveLicense(): bool
{
if ($this->is_system_company) {
return true;
}
return $this->licenseKeys()
->where('is_active', true)
->where(function ($query): void {
$query->whereNull('starts_at')->orWhere('starts_at', '<=', now());
})
->where(function ($query): void {
$query->whereNull('expires_at')->orWhere('expires_at', '>=', now());
})
->whereNull('revoked_at')
->exists();
}
}