@@ -63,41 +63,75 @@ public function getGroupsUsersByCategory($categoryId) {
63
63
64
64
protected function assignTicket (CommonDBTM $ item ) {
65
65
$ itilcategoriesId = $ this ->getTicketCategory ($ item );
66
- if (($ lastAssignmentIndex = $ this ->getLastAssignmentIndex ($ item )) === false ) {
67
- PluginSmartAssignLogger::addWarning (__FUNCTION__ . ' - nothing to to (category is disabled or not configured; getLastAssignmentIndex: ' . $ lastAssignmentIndex );
68
- return ;
69
- }
70
- $ categoryGroupMembers = $ this ->getGroupsUsersByCategory ($ this ->getTicketCategory ($ item ));
71
- if (count ($ categoryGroupMembers ) === 0 ) {
72
- /**
73
- * category w/o group, or group w/o users
74
- */
75
- return ;
76
- }
77
- $ newAssignmentIndex = isset ($ lastAssignmentIndex ) ? $ lastAssignmentIndex + 1 : 0 ;
78
- /**
79
- * round robin
80
- */
81
- if ($ newAssignmentIndex > (count ($ categoryGroupMembers ) - 1 )) {
82
- $ newAssignmentIndex = $ newAssignmentIndex % count ($ categoryGroupMembers );
66
+
67
+ if ($ this ->rrAssignmentsEntity ->getOptionAutoAssignMode () === 0 ) {
68
+ // Modo balanceamento ativo
69
+ $ groupId = $ this ->rrAssignmentsEntity ->getGroupByItilCategory ($ itilcategoriesId );
70
+
71
+ if ($ groupId === false ) {
72
+ PluginSmartAssignLogger::addWarning (__FUNCTION__ . ' - Grupo não encontrado para a categoria: ' . $ itilcategoriesId );
73
+ return ;
74
+ }
75
+
76
+ $ sql = <<<EOT
77
+ SELECT tu.users_id, COUNT(t.id) AS active_tickets
78
+ FROM glpi_tickets_users tu
79
+ JOIN glpi_tickets t ON tu.tickets_id = t.id
80
+ JOIN glpi_groups_users gu ON tu.users_id = gu.users_id
81
+ JOIN glpi_groups_tickets gt ON t.id = gt.tickets_id
82
+ WHERE tu.type = 2
83
+ AND t.status NOT IN (5, 6)
84
+ AND t.is_deleted = 0
85
+ AND gu.groups_id = 15
86
+ AND gt.groups_id = 15
87
+ AND gt.type = 2 -- Adicionando a verificação para `type = 2`
88
+ GROUP BY tu.users_id
89
+ ORDER BY active_tickets ASC, tu.users_id ASC
90
+ LIMIT 10;
91
+ EOT ;
92
+ $ resultCollection = $ this ->DB ->queryOrDie ($ sql , $ this ->DB ->error ());
93
+ $ resultArray = iterator_to_array ($ resultCollection );
94
+
95
+ if (count ($ resultArray ) === 0 ) {
96
+ PluginSmartAssignLogger::addWarning (__FUNCTION__ . ' - Nenhum técnico disponível no grupo: ' . $ groupId );
97
+ return ;
98
+ }
99
+
100
+ $ userId = $ resultArray [0 ]['users_id ' ];
101
+ } else {
102
+ // Modo rodízio
103
+ if (($ lastAssignmentIndex = $ this ->getLastAssignmentIndex ($ item )) === false ) {
104
+ PluginSmartAssignLogger::addWarning (__FUNCTION__ . ' - Nada a fazer (categoria desativada ou não configurada; índice: ' . $ lastAssignmentIndex . ') ' );
105
+ return ;
106
+ }
107
+
108
+ $ categoryGroupMembers = $ this ->getGroupsUsersByCategory ($ this ->getTicketCategory ($ item ));
109
+ if (count ($ categoryGroupMembers ) === 0 ) {
110
+ PluginSmartAssignLogger::addWarning (__FUNCTION__ . ' - Categoria sem grupo ou grupo sem usuários ' );
111
+ return ;
112
+ }
113
+
114
+ $ newAssignmentIndex = isset ($ lastAssignmentIndex ) ? $ lastAssignmentIndex + 1 : 0 ;
115
+
83
116
if ($ newAssignmentIndex > (count ($ categoryGroupMembers ) - 1 )) {
84
- $ newAssignmentIndex = 0 ;
117
+ $ newAssignmentIndex = $ newAssignmentIndex % count ($ categoryGroupMembers );
118
+ }
119
+
120
+ if ($ this ->rrAssignmentsEntity ->getOptionAutoAssignType () === 1 ) {
121
+ $ this ->rrAssignmentsEntity ->updateLastAssignmentIndexCategoria ($ itilcategoriesId , $ newAssignmentIndex );
122
+ } else {
123
+ $ this ->rrAssignmentsEntity ->updateLastAssignmentIndexGrupo ($ itilcategoriesId , $ newAssignmentIndex );
85
124
}
125
+
126
+ $ userId = $ categoryGroupMembers [$ newAssignmentIndex ]['UserId ' ];
86
127
}
87
- if ($ this ->rrAssignmentsEntity ->getOptionAutoAssignType () === 1 ) {
88
- $ this ->rrAssignmentsEntity ->updateLastAssignmentIndexCategoria ($ itilcategoriesId , $ newAssignmentIndex );
89
- } else {
90
- $ this ->rrAssignmentsEntity ->updateLastAssignmentIndexGrupo ($ itilcategoriesId , $ newAssignmentIndex );
91
- }
92
-
93
- /**
94
- * set the assignment
95
- */
128
+
129
+ // Atribuir o ticket ao técnico selecionado
96
130
$ ticketId = $ this ->getTicketId ($ item );
97
- $ userId = $ categoryGroupMembers [$ newAssignmentIndex ]['UserId ' ];
98
131
$ this ->setAssignment ($ ticketId , $ userId , $ itilcategoriesId );
99
132
return $ userId ;
100
133
}
134
+
101
135
102
136
protected function getLastAssignmentIndex (CommonDBTM $ item ) {
103
137
$ itilcategoriesId = $ this ->getTicketCategory ($ item );
0 commit comments