diff --git a/app/Repositories/SupervisorRepository.php b/app/Repositories/SupervisorRepository.php index 7a566c3..7404f35 100644 --- a/app/Repositories/SupervisorRepository.php +++ b/app/Repositories/SupervisorRepository.php @@ -115,20 +115,59 @@ class SupervisorRepository extends Repository function list(array $params = []): array { $table = $this->table; - $query = "SELECT * FROM $table WHERE 1=1"; + $tableEventosAtendimento = EventosAtendimentosModel::$table; + $query = "SELECT *, + ( + SELECT + count(*) + FROM + atendimento ma + WHERE 'START' = (SELECT m2.evento FROM $tableEventosAtendimento m2 + WHERE ma.uniqueid = m2.uniqueid + ORDER BY m2.id DESC LIMIT 1) + AND ma.matricula = ms.matricula + + ) AS countAtendimentos, + ( + SELECT + count(*) + FROM + atendimento ma + WHERE ma.data_reg >= current_date + AND ma.matricula = ms.matricula + ) AS numero_atendimento_dia FROM $table ms WHERE 1=1"; if ($params['matricula']) { - $query .= " AND matricula = :matricula "; + $query .= " AND ms.matricula = :matricula "; $dados['matricula'] = $params['matricula']; } if ($params['fila']) { - $query .= " AND fila = :fila "; + $query .= " AND ms.fila = :fila "; $dados['fila'] = $params['fila']; } + + if ($params['agente_livre']) { + $query .= " AND ( + SELECT + count(*) + FROM + atendimento ma + WHERE 'START' = (SELECT m2.evento FROM $tableEventosAtendimento m2 + WHERE ma.uniqueid = m2.uniqueid + ORDER BY m2.id DESC LIMIT 1) + AND ma.matricula = ms.matricula + + ) < (SELECT prm_media_simultaneo FROM pbx_parametros pp LIMIT 1 )"; + } + + if ($params['status']) { + $query .= " AND ms.status = :status "; + $dados['status'] = $params['status']; + } if (empty($params['id_empresa'])) { throw new Exception("Parâmetro id_empresa é obrigatório, $table"); } - $query .= " AND id_empresa = :id_empresa "; + $query .= " AND ms.id_empresa = :id_empresa "; $dados['id_empresa'] = $params['id_empresa']; return $this->db->read($query, $dados)->fetchAll(); @@ -141,8 +180,7 @@ class SupervisorRepository extends Repository foreach ($params as $key => $value) { if (array_key_last($params) == $key) { $query .= " $key = :$key"; - } - else { + } else { $query .= " $key = :$key, "; } }