/**
  * @param Atendimento $atendimento
  * @param mixed       $statusAtual (array[int] | int)
  * @param int         $novoStatus
  * @param string      $campoData
  *
  * @return bool
  */
 private function mudaStatusAtendimento(Atendimento $atendimento, $statusAtual, $novoStatus, $campoData)
 {
     $cond = '';
     if ($campoData !== null) {
         $cond = ", e.{$campoData} = :data";
     }
     if (!is_array($statusAtual)) {
         $statusAtual = [$statusAtual];
     }
     // atualizando atendimento
     $query = $this->em()->createQuery("\n            UPDATE\n                Novosga\\Model\\Atendimento e\n            SET\n                e.status = :novoStatus {$cond}\n            WHERE\n                e.id = :id AND\n                e.status IN (:statusAtual)\n        ");
     if ($campoData !== null) {
         $query->setParameter('data', DateUtil::nowSQL());
     }
     $query->setParameter('novoStatus', $novoStatus);
     $query->setParameter('id', $atendimento->getId());
     $query->setParameter('statusAtual', $statusAtual);
     return $query->execute() > 0;
 }
示例#2
0
 /**
  * Move os registros da tabela atendimento para a tabela de historico de atendimentos.
  * Se a unidade não for informada, será acumulado serviços de todas as unidades.
  *
  * @param Unidade|int $unidade
  *
  * @throws Exception
  */
 public function acumularAtendimentos($unidade = 0)
 {
     if ($unidade instanceof Unidade) {
         $unidadeId = $unidade->getId();
     } else {
         $unidadeId = max($unidade, 0);
         $unidade = $unidadeId > 0 ? $this->em->find('Novosga\\Model\\Unidade', $unidadeId) : null;
     }
     AppConfig::getInstance()->hook('attending.pre-reset', $unidade);
     $data = DateUtil::nowSQL();
     $conn = $this->em->getConnection();
     // tables name
     $historicoTable = $this->em->getClassMetadata('Novosga\\Model\\AtendimentoHistorico')->getTableName();
     $historicoCodifTable = $this->em->getClassMetadata('Novosga\\Model\\AtendimentoCodificadoHistorico')->getTableName();
     $atendimentoTable = $this->em->getClassMetadata('Novosga\\Model\\Atendimento')->getTableName();
     $atendimentoCodifTable = $this->em->getClassMetadata('Novosga\\Model\\AtendimentoCodificado')->getTableName();
     $atendimentoMetaTable = $this->em->getClassMetadata('Novosga\\Model\\AtendimentoMeta')->getTableName();
     $historicoMetaTable = $this->em->getClassMetadata('Novosga\\Model\\AtendimentoHistoricoMeta')->getTableName();
     try {
         $conn->beginTransaction();
         // copia os atendimentos para o historico
         $sql = "\n                INSERT INTO {$historicoTable}\n                (\n                    id, unidade_id, usuario_id, servico_id, prioridade_id, status, sigla_senha, num_senha, num_senha_serv,\n                    nm_cli, num_local, dt_cheg, dt_cha, dt_ini, dt_fim, ident_cli, usuario_tri_id, atendimento_id\n                )\n                SELECT\n                    a.id, a.unidade_id, a.usuario_id, a.servico_id, a.prioridade_id, a.status, a.sigla_senha, a.num_senha, a.num_senha_serv,\n                    a.nm_cli, a.num_local, a.dt_cheg, a.dt_cha, a.dt_ini, a.dt_fim, a.ident_cli, a.usuario_tri_id, a.atendimento_id\n                FROM\n                    {$atendimentoTable} a\n                WHERE\n                    a.dt_cheg <= :data AND (a.unidade_id = :unidade OR :unidade = 0)\n            ";
         // atendimentos pais (nao oriundos de redirecionamento)
         $query = $conn->prepare("{$sql} AND a.atendimento_id IS NULL");
         $query->bindValue('data', $data, PDO::PARAM_STR);
         $query->bindValue('unidade', $unidadeId, PDO::PARAM_INT);
         $query->execute();
         // atendimentos filhos (oriundos de redirecionamento)
         $query = $conn->prepare("{$sql} AND a.atendimento_id IS NOT NULL");
         $query->bindValue('data', $data, PDO::PARAM_STR);
         $query->bindValue('unidade', $unidadeId, PDO::PARAM_INT);
         $query->execute();
         // copia os metadados
         $sql = "\n                INSERT INTO {$historicoMetaTable}\n                (\n                    atendimento_id, name, value\n                )\n                SELECT\n                    a.atendimento_id, a.name, a.value\n                FROM\n                    {$atendimentoMetaTable}  a\n                WHERE\n                    a.atendimento_id IN (SELECT b.id FROM {$atendimentoTable} b WHERE b.dt_cheg <= :data AND (b.unidade_id = :unidade OR :unidade = 0))\n            ";
         $query = $conn->prepare($sql);
         $query->bindValue('data', $data, PDO::PARAM_STR);
         $query->bindValue('unidade', $unidadeId, PDO::PARAM_INT);
         $query->execute();
         // copia os atendimentos codificados para o historico
         $query = $conn->prepare("\n                INSERT INTO {$historicoCodifTable}\n                SELECT\n                    ac.atendimento_id, ac.servico_id, ac.valor_peso\n                FROM\n                    {$atendimentoCodifTable} ac\n                WHERE\n                    ac.atendimento_id IN (\n                        SELECT a.id FROM {$atendimentoTable} a WHERE dt_cheg <= :data AND (a.unidade_id = :unidade OR :unidade = 0)\n                    )\n            ");
         $query->bindValue('data', $data, PDO::PARAM_STR);
         $query->bindValue('unidade', $unidadeId, PDO::PARAM_INT);
         $query->execute();
         // limpa atendimentos codificados
         $this->em->createQuery('
                     DELETE Novosga\\Model\\AtendimentoCodificado e WHERE e.atendimento IN (
                         SELECT a.id FROM Novosga\\Model\\Atendimento a WHERE a.dataChegada <= :data AND (a.unidade = :unidade OR :unidade = 0)
                     )
                 ')->setParameter('data', $data)->setParameter('unidade', $unidadeId)->execute();
         // limpa metadata
         $this->em->createQuery('
                     DELETE Novosga\\Model\\AtendimentoMeta e WHERE e.atendimento IN (
                         SELECT a.id FROM Novosga\\Model\\Atendimento a WHERE a.dataChegada <= :data AND (a.unidade = :unidade OR :unidade = 0)
                     )
                 ')->setParameter('data', $data)->setParameter('unidade', $unidadeId)->execute();
         // limpa o auto-relacionamento para poder excluir os atendimento sem dar erro de constraint (#136)
         $this->em->createQuery('UPDATE Novosga\\Model\\Atendimento e SET e.pai = NULL WHERE e.dataChegada <= :data AND (e.unidade = :unidade OR :unidade = 0)')->setParameter('unidade', $unidadeId)->setParameter('data', $data)->execute();
         // limpa atendimentos da unidade
         $this->em->createQuery('DELETE Novosga\\Model\\Atendimento e WHERE e.dataChegada <= :data AND (e.unidade = :unidade OR :unidade = 0)')->setParameter('data', $data)->setParameter('unidade', $unidadeId)->execute();
         // limpa a tabela de senhas a serem exibidas no painel
         $this->em->createQuery('DELETE Novosga\\Model\\PainelSenha e WHERE (e.unidade = :unidade OR :unidade = 0)')->setParameter('unidade', $unidadeId)->execute();
         // zera o contador das senhas
         $this->em->createQuery('UPDATE Novosga\\Model\\Contador e SET e.total = 0 WHERE (e.unidade = :unidade OR :unidade = 0)')->setParameter('unidade', $unidadeId)->execute();
         $conn->commit();
     } catch (Exception $e) {
         try {
             $conn->rollBack();
         } catch (Exception $e2) {
         }
         throw $e;
     }
     AppConfig::getInstance()->hook('attending.reset', $unidade);
 }
 private function tempo_medio_atendentes($dataInicial, $dataFinal)
 {
     $dados = array();
     $query = $this->em()->createQuery("\n            SELECT\n                CONCAT(u.nome, CONCAT(' ', u.sobrenome)) as atendente,\n                COUNT(a) as total,\n                AVG(a.dataChamada - a.dataChegada) as espera,\n                AVG(a.dataInicio - a.dataChamada) as deslocamento,\n                AVG(a.dataFim - a.dataInicio) as atendimento,\n                AVG(a.dataFim - a.dataChegada) as tempoTotal\n            FROM\n                Novosga\\Model\\ViewAtendimento a\n                JOIN a.usuario u\n            WHERE\n                a.dataChegada >= :dataInicial AND\n                a.dataChegada <= :dataFinal AND\n                a.dataFim IS NOT NULL\n            GROUP BY\n                u\n            ORDER BY\n                u.nome\n        ");
     $query->setParameter('dataInicial', $dataInicial);
     $query->setParameter('dataFinal', $dataFinal);
     $query->setMaxResults(self::MAX_RESULTS);
     $rs = $query->getResult();
     foreach ($rs as $r) {
         $d = array('atendente' => $r['atendente'], 'total' => $r['total']);
         try {
             // se der erro tentando converter a data do banco para segundos, assume que ja esta em segundos
             // Isso é necessário para manter a compatibilidade entre os bancos
             $d['espera'] = DateUtil::timeToSec($r['espera']);
             $d['deslocamento'] = DateUtil::timeToSec($r['deslocamento']);
             $d['atendimento'] = DateUtil::timeToSec($r['atendimento']);
             $d['tempoTotal'] = DateUtil::timeToSec($r['tempoTotal']);
         } catch (\Exception $e) {
             $d['espera'] = $r['espera'];
             $d['deslocamento'] = $r['deslocamento'];
             $d['atendimento'] = $r['atendimento'];
             $d['tempoTotal'] = $r['tempoTotal'];
         }
         $dados[] = $d;
     }
     return $dados;
 }