query = "SELECT ma.*, me.fila as fila FROM {$this->atendimento} ma INNER JOIN {$this->evento} me ON ma.uniqueid = me.uniqueid WHERE ma.cliente_id = :cliente_id AND (SELECT m2.evento FROM md_evento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = :evento"; return $this->read($this->query, ['cliente_id' => $cliente_id, 'evento' => $evento])->fetch(); } public function getStatusAtendimento($uniqueid) { $this->query = "SELECT evento FROM md_evento WHERE uniqueid = :uniqueid ORDER BY id DESC LIMIT 1"; return $this->read($this->query, ['uniqueid' => $uniqueid])->fetch(); } public function findAtendId($uniqueid) { $this->query = "SELECT ma.*, me.fila as fila FROM {$this->atendimento} ma INNER JOIN {$this->evento} me ON ma.uniqueid = me.uniqueid WHERE ma.uniqueid = :uniqueid "; return $this->read($this->query, ['uniqueid' => $uniqueid])->fetch(); } public function getAtendimentoByEvento($fila, $evento = 'EMESPERA') { $this->query = "SELECT ma.* FROM {$this->atendimento} ma INNER JOIN {$this->evento} me ON ma.uniqueid = me.uniqueid WHERE (SELECT m2.evento FROM md_evento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = :evento AND me.fila = :fila"; return $this->read($this->query, ['evento' => $evento, 'fila' => $fila])->fetchAll(); } public function findAtendAgent($matricula, $quantidade = 10) { $data = []; $this->query = "SELECT ma.*, ma.nome AS profile_name, ppr.protocolo as protocolo, (SELECT m2.evento FROM md_evento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) AS evento, CASE WHEN (SELECT m2.evento FROM md_evento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'START' THEN 1 ELSE 0 END AS status FROM {$this->atendimento} ma INNER JOIN pbx_protocolo_reg ppr ON ma.uniqueid = ppr.uniqueid "; if ($matricula) { $this->query .= " WHERE ma.matricula = :matricula "; $data['matricula'] = $matricula; } if (empty($quantidade)) { $data['quantidade'] = 10; } else { $data['quantidade'] = $quantidade; } $this->query .= " ORDER BY status DESC, data_reg DESC LIMIT :quantidade "; return $this->read($this->query, $data)->fetchAll(); } public function findAtenEmAberto($cliente_id = null) { $this->query = "SELECT ma.* FROM {$this->atendimento} ma WHERE (SELECT m2.evento FROM md_evento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'START' AND ma.matricula notnull "; $data = []; if ($cliente_id) { $data['cliente_id'] = $cliente_id; $this->query .= 'AND ma.cliente_id = :cliente_id'; return $this->read($this->query, $data)->fetch(); } return $this->read($this->query, $data)->fetchAll(); } public function getAtendimentoAbertoByAgente($matricula) { $this->query = "SELECT ma.* FROM {$this->atendimento} ma WHERE (SELECT m2.evento FROM md_evento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'START' AND ma.matricula = :matricula "; return $this->read($this->query, ['matricula' => $matricula])->fetchAll(); } // public function getAtendFila($fila, $evento = 'LOST_CONNECTION') // { // $this->query = "SELECT ma.*, me.fila as fila FROM {$this->atendimento} ma // INNER JOIN {$this->evento} me // ON ma.uniqueid = me.uniqueid // WHERE me.evento = :evento // AND me.fila = :fila // AND (SELECT m2.evento FROM md_evento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'LOST_CONNECTION' "; // return $this->read($this->query, ['evento' => $evento, 'fila' => $fila])->fetchAll(); // } public function getAtendFila($fila) { $this->query = "SELECT ma.*, me.fila as fila FROM {$this->atendimento} ma INNER JOIN {$this->evento} me ON ma.uniqueid = me.uniqueid AND me.evento IN('EMESPERA', 'LOST_CONNECTION') WHERE me.fila = :fila AND (SELECT m2.evento FROM md_evento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) IN('EMESPERA', 'LOST_CONNECTION') "; return $this->read($this->query, ['fila' => $fila])->fetchAll(); } public function getAtendAbandonado($fila = null) { $data = []; $this->query = "SELECT ma.*, me.fila as fila FROM {$this->atendimento} ma INNER JOIN {$this->evento} me ON ma.uniqueid = me.uniqueid AND me.evento = 'ABANDON' WHERE (SELECT m2.evento FROM md_evento m2 WHERE ma.uniqueid = m2.uniqueid ORDER BY id DESC LIMIT 1) = 'ABANDON' AND ma.data_reg >= CURRENT_DATE "; if ($fila) { $this->query .= ' AND me.fila = :fila'; $data['fila'] = $fila; } return $this->read($this->query, $data)->fetchAll(); } public function createAtendimento($matricula, $cliente_id, $direcao, $context, $nome) { $unique = uniqid('', true); $this->query = "INSERT INTO {$this->atendimento} ( matricula, cliente_id, direcao, uniqueid, context, nome) VALUES( :matricula, :cliente_id, :direcao, :uniqueid, :context, :nome);"; $data['uniqueid'] = $unique; $data['matricula'] = $matricula; $data['cliente_id'] = $cliente_id; $data['direcao'] = $direcao; $data['context'] = $context; $data['nome'] = $nome; $return = $this->create($this->query, $data); if ($return) { return $unique; } return $return; } public function updAtendimento($uniqueid, $matricula) { $this->query = "UPDATE {$this->atendimento} SET matricula = :matricula WHERE uniqueid = :uniqueid;"; $data['matricula'] = $matricula; $data['uniqueid'] = $uniqueid; return $this->update($this->query, $data); } public function getQuantiAtendimentSimultaneos() { $this->query = "SELECT prm_media_simultaneo FROM pbx_parametros LIMIT 1"; return $this->read($this->query)->fetch(); } }