From 72bc8f885826027ae041697ea74e3af2f3d9736b Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Fri, 6 Jul 2018 03:43:33 +0000
Subject: [PATCH] Dept::canAssign  Support Team Check

---
 include/class.dept.php   | 25 ++++++++++++++++---------
 include/class.team.php   |  4 ++++
 include/class.ticket.php |  8 +++++---
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/include/class.dept.php b/include/class.dept.php
index 7c9f92bfc..384a749a2 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -373,18 +373,25 @@ implements TemplateVariable, Searchable {
         return ($this->getSignature() && $this->isPublic());
     }
 
-    //Check if an agent is eligible for assignment
-    function canAssign(Staff $assignee) {
-        //Primary members only
-        if ($this->assignPrimaryOnly() && !$this->isPrimaryMember($assignee))
-            return false;
+    // Check if an agent or team is eligible for assignment
+    function canAssign($assignee) {
+
 
-        //Extended members only
-        if ($this->assignMembersOnly() && !$this->isMember($assignee))
+        if ($assignee instanceof Staff) {
+            // Primary members only
+            if ($this->assignPrimaryOnly() && !$this->isPrimaryMember($assignee))
+                return false;
+
+            // Extended members only
+            if ($this->assignMembersOnly() && !$this->isMember($assignee))
+                return false;
+        } elseif (!$assignee instanceof Team) {
+            // Assignee can only be an Agent or a Team
             return false;
+        }
 
-         //Make sure agent is active & not on vacation
-         if (!$assignee->isActive() || $assignee->onVacation())
+        // Make sure agent / team  is availabe for assignment
+        if (!$assignee->isAvailable())
              return false;
 
         return true;
diff --git a/include/class.team.php b/include/class.team.php
index a4fd4f8c7..2420cebc0 100644
--- a/include/class.team.php
+++ b/include/class.team.php
@@ -126,6 +126,10 @@ implements TemplateVariable {
         return $this->isEnabled();
     }
 
+    function isAvailable() {
+        return ($this->isActive() && $this->members);
+    }
+
     function alertsEnabled() {
         return ($this->flags & self::FLAG_NOALERTS) == 0;
     }
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 0855f1196..de6b6d0f7 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -2397,17 +2397,17 @@ implements RestrictedAccess, Threadable, Searchable {
 
         $evd = array();
         $refer = null;
+        $dept = $this->getDept();
         $assignee = $form->getAssignee();
         if ($assignee instanceof Staff) {
-            $dept = $this->getDept();
             if ($this->getStaffId() == $assignee->getId()) {
                 $errors['assignee'] = sprintf(__('%s already assigned to %s'),
                         __('Ticket'),
                         __('the agent')
                         );
-            } elseif(!$assignee->isAvailable()) {
+            } elseif (!$assignee->isAvailable()) {
                 $errors['assignee'] = __('Agent is unavailable for assignment');
-              } elseif (!$dept->canAssign($assignee)) {
+            } elseif (!$dept->canAssign($assignee)) {
                 $errors['err'] = __('Permission denied');
             } else {
                 $refer = $this->staff ?: null;
@@ -2425,6 +2425,8 @@ implements RestrictedAccess, Threadable, Searchable {
                         __('Ticket'),
                         __('the team')
                         );
+            } elseif (!$dept->canAssign($assignee)) {
+                $errors['err'] = __('Permission denied');
             } else {
                 $refer = $this->team ?: null;
                 $this->team_id = $assignee->getId();
-- 
GitLab