'array', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime' ]; /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = [ 'created_at', 'updated_at', 'deleted_at' ]; /** * Get users for the company. */ public function users(): HasMany { return $this->hasMany(\Modules\Auth\Models\User::class, 'users_bus_id'); } /** * Get modules for the company. */ public function modules(): BelongsToMany { return $this->belongsToMany(\App\Models\Module::class, 'bus_modules', 'bus_modules_bus_id', 'bus_modules_module_id') ->withPivot(['bus_modules_is_enabled', 'bus_modules_settings', 'bus_modules_enabled_by', 'bus_modules_enabled_at', 'bus_modules_show_public_nav', 'bus_modules_show_dashboard_nav']) ->withTimestamps(); } /** * Get activity logs for the company. */ public function activityLogs(): HasMany { return $this->hasMany(\App\Models\ActivityLog::class, 'activity_logs_bus_id'); } /** * Get enabled modules only. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeEnabledModules($query) { return $query->where('bus_modules.bus_modules_is_enabled', true); } /** * Get active companies only. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeActive($query) { return $query->where('bus_status', 'active'); } /** * Get companies by type. * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $type * @return \Illuminate\Database\Eloquent\Builder */ public function scopeType($query, string $type) { return $query->where('bus_type', $type); } /** * Check if company has specific module enabled. * * @param string $moduleName * @return bool */ public function hasModuleEnabled(string $moduleName): bool { return $this->modules() ->where('modules.modules_name', $moduleName) ->where('bus_modules.bus_modules_is_enabled', true) ->exists(); } /** * Enable module for company. * * @param int $moduleId * @param int $enabledBy * @param array $settings * @return bool */ public function enableModule(int $moduleId, int $enabledBy, array $settings = []): bool { $this->modules()->attach($moduleId, [ 'bus_modules_is_enabled' => true, 'bus_modules_settings' => json_encode($settings), 'bus_modules_enabled_by' => $enabledBy, 'bus_modules_enabled_at' => \Carbon\Carbon::now() ]); return true; } /** * Disable module for company. * * @param int $moduleId * @return bool */ public function disableModule(int $moduleId): bool { return $this->modules()->updateExistingPivot($moduleId, [ 'bus_modules_is_enabled' => false ]); } /** * Get logo URL. * * @return string */ public function getLogoUrlAttribute(): string { if ($this->bus_logo) { return url('storage/uploads/company/logos/' . $this->bus_logo); } return asset_url('img/default-logo.png'); } /** * Get business card URL. * * @return string */ public function getBusinessCardUrlAttribute(): string { if ($this->bus_business_card) { return url('storage/uploads/company/business-cards/' . $this->bus_business_card); } return asset_url('img/default-business-card.png'); } /** * Get document URL. * * @return string */ public function getDocumentUrlAttribute(): string { if ($this->bus_document) { return url('storage/uploads/company/documents/' . $this->bus_document); } return '#'; } /** * Get display name. * * @return string */ public function getDisplayNameAttribute(): string { return $this->bus_name ?: $this->bus_slug; } /** * Get full address. * * @return string */ public function getFullAddressAttribute(): string { $address = []; if ($this->bus_address) { $address[] = $this->bus_address; } if ($this->bus_city) { $address[] = $this->bus_city; } if ($this->bus_state) { $address[] = $this->bus_state; } if ($this->bus_country) { $address[] = $this->bus_country; } if ($this->bus_postal_code) { $address[] = $this->bus_postal_code; } return implode(', ', $address); } /** * Get setting value. * * @param string $key * @param mixed $default * @return mixed */ public function getSetting(string $key, $default = null) { $settings = $this->bus_settings ?? []; return $settings[$key] ?? $default; } /** * Set setting value. * * @param string $key * @param mixed $value * @return void */ public function setSetting(string $key, $value): void { $settings = $this->bus_settings ?? []; $settings[$key] = $value; $this->bus_settings = $settings; } /** * Get user count. * * @return int */ public function getUserCountAttribute(): int { return $this->users()->count(); } /** * Get active user count. * * @return int */ public function getActiveUserCountAttribute(): int { return $this->users()->active()->count(); } }