PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 All 67 releases
fluent-support / app / Models / AgentGroup.php

AgentGroup.php in Fluent Support – Helpdesk & Customer Support Ticket System trunk, at app/Models/AgentGroup.php

92 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Models;
4
5 use FluentSupport\App\Services\Tickets\AgentTicketAccess;
6
7 class AgentGroup extends Tag
8 {
9 protected static $type = 'agent_group';
10
11 public static function boot()
12 {
13 parent::boot();
14
15 static::creating(function ($model) {
16 $model->tag_type = static::$type;
17 if (empty($model->created_by) && $userId = get_current_user_id()) {
18 $model->created_by = $userId;
19 }
20
21 $model->slug = static::slugify($model->title);
22 });
23
24 static::addGlobalScope(function ($builder) {
25 $builder->where('tag_type', static::$type);
26 });
27 }
28
29 public static function slugify($title)
30 {
31 $slug = sanitize_title($title, 'agent-group', 'display');
32 if (static::where('slug', $slug)->first()) {
33 $slug .= '-' . time();
34 }
35 return $slug;
36 }
37
38 public function agents()
39 {
40 return $this->belongsToMany(
41 Agent::class, 'fs_tag_pivot', 'tag_id', 'source_id'
42 )->wherePivot('source_type', 'agent_group');
43 }
44
45 /**
46 * Get the least-loaded agent in this group, respecting mailbox restrictions.
47 *
48 * @param int|null $mailboxId
49 * @param array $currentCounts Optional pre-loaded counts [agent_id => count]
50 * @return Agent|null
51 */
52 public function getLeastLoadedAgent($mailboxId = null, array &$currentCounts = [])
53 {
54 $agents = $this->agents()->get();
55
56 if ($agents->isEmpty()) {
57 return null;
58 }
59
60 if (empty($currentCounts)) {
61 $agentIds = $agents->pluck('id')->toArray();
62 $currentCounts = Ticket::whereIn('agent_id', $agentIds)
63 ->where('status', '!=', 'closed')
64 ->selectRaw('agent_id, COUNT(*) as cnt')
65 ->groupBy('agent_id')
66 ->pluck('cnt', 'agent_id')
67 ->toArray();
68 }
69
70 $selectedAgent = null;
71 $minCount = PHP_INT_MAX;
72 $ticketAccess = new AgentTicketAccess();
73
74 foreach ($agents as $agent) {
75 if ($mailboxId) {
76 $restrictedBoxes = $ticketAccess->getRestrictedMailboxIds($agent);
77 if (in_array((int) $mailboxId, $restrictedBoxes, true)) {
78 continue;
79 }
80 }
81
82 $count = $currentCounts[$agent->id] ?? 0;
83 if ($count < $minCount) {
84 $minCount = $count;
85 $selectedAgent = $agent;
86 }
87 }
88
89 return $selectedAgent;
90 }
91 }
92