<?php
 namespace App\Models; use App\Traits\HasTranslations; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class TicketDepartment extends Model { use HasFactory, HasTranslations; protected $translatable = [ 'name', 'description', ]; protected $fillable = [ 'name', 'description', 'client_only', 'hidden', 'status', 'sla_plan_id', 'outgoing_email_to', 'outgoing_email_name', 'no_auto_responder', 'have_feedback_request', 'feedback_after_hours', 'mail_hostname', 'mail_port', 'mail_email', 'mail_password', 'slack_enabled', 'slack_channel_prefix', 'telegram_enabled', 'telegram_chat_id', 'discord_enabled', 'discord_webhook_url', 'discord_use_forum_threads', 'teams_enabled', 'teams_webhook_url', 'teams_use_threads', 'pushover_enabled', 'pushover_user_key', 'whatsapp_enabled', 'whatsapp_phone', 'sms_enabled', 'sms_notification_phones', 'mail_import_enabled', 'accept_unregistered_emails', 'mail_protocol', 'mail_ssl', 'mail_footer_strip', 'mail_poll_interval', 'mail_post_action', 'mail_move_folder', 'mail_auth_type', ]; protected $casts = [ 'client_only' => 'boolean', 'hidden' => 'boolean', 'status' => 'boolean', 'no_auto_responder' => 'boolean', 'accept_unregistered_emails' => 'boolean', 'have_feedback_request' => 'boolean', 'feedback_after_hours' => 'integer', 'mail_port' => 'integer', 'slack_enabled' => 'boolean', 'telegram_enabled' => 'boolean', 'discord_enabled' => 'boolean', 'discord_use_forum_threads' => 'boolean', 'teams_enabled' => 'boolean', 'teams_use_threads' => 'boolean', 'pushover_enabled' => 'boolean', 'whatsapp_enabled' => 'boolean', 'sms_enabled' => 'boolean', ]; public function scopeActive($query) { return $query->where('status', true); } public function scopeVisible($query) { return $query->where('hidden', false); } public function scopeClientAccessible($query) { return $query->where('client_only', true); } public function customFields() { return $this->hasMany(TicketDepartmentCustomField::class)->ordered(); } public function admins() { return $this->belongsToMany(Admin::class, 'ticket_department_admins', 'ticket_department_id', 'admin_id') ->withTimestamps(); } public function getAuthorizedAdmins(?string $notificationEvent = null): \Illuminate\Support\Collection { $departmentAdmins = $this->admins()->get(); $superAdmins = Admin::role('super-admin') ->whereNotIn('id', $departmentAdmins->pluck('id')) ->get(); $allAdmins = $departmentAdmins->merge($superAdmins); if ($notificationEvent) { $allAdmins = $allAdmins->filter(function (Admin $admin) use ($notificationEvent) { $channels = $admin->getNotificationChannels($notificationEvent); if ($channels === null) { return true; } return in_array('email', $channels); }); } return $allAdmins; } public function mailboxes() { return $this->hasMany(TicketDepartmentMailbox::class); } public function enabledMailboxes() { return $this->hasMany(TicketDepartmentMailbox::class)->where('enabled', true); } public function shouldSendNewTicketAutoResponse(): bool { return Setting::get('auto_response_on_new_ticket', true) && ! $this->no_auto_responder; } } 