forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
279 lines
11 KiB
279 lines
11 KiB
#!/usr/bin/php -q |
|
<?php |
|
error_reporting(E_ERROR); |
|
ini_set('display_errors', 0); |
|
require 'util/constantes.php'; |
|
require 'util/util.php'; |
|
require 'util/Pid.php'; |
|
require 'bd.php'; |
|
|
|
define("KAMPO_TEMPO_ARQUIVO", '40'); |
|
|
|
$baseAudio = '/var/spool/asterisk/monitor/'; |
|
$dirOrigemArquivo = KChecaDir('/kampo/kampo/IMPORT/'); |
|
$dirOrigem = KChecaDir('/kampo/kampo/'); |
|
$dirDestino = KChecaDir('/kampo/gm/GM/Recordings/'); |
|
$dirProcessados = KChecaDir('/hdaux/kampo/proc/'); |
|
$dirReport = KChecaDir('/kampo/kampo/'); |
|
|
|
|
|
/* |
|
* Garante apenas uma instancia do script. |
|
*/ |
|
$pid = new Pid(); |
|
|
|
$baseQuery = "select a.id_bilhetes, a.calldate::date, a.calldate::time + '03:00:00' as call_start, a.src, a.dst, a.duration, strtoint(b.param2) as billsec, a.uniqueid, a.userfield, b.evento, b.fila, b.agente |
|
from pbx_bilhetes a, pbx_eventos_dacs b |
|
where b.uid2 = a.uniqueid |
|
and a.calldate::date = '%s' |
|
and date_trunc('minute',a.calldate::time + '03:00:00') = '%s' |
|
and b.fila = 'KAMPO_GM' |
|
and b.evento in('COMPLETAAGENT','COMPLETACALLER', 'TRANSFER') |
|
and a.lastapp <> 'Transferred Call' |
|
and a.dst in(%s);\n"; |
|
|
|
$baseQuery = <<<KMPold |
|
drop table if exists tmp_pesquisa_campo; |
|
create temporary table tmp_pesquisa_campo as |
|
select a.id_bilhetes, a.calldate::date, a.calldate::time as call_start, a.src, a.dst, a.duration, strtoint(b.param2) as billsec, a.uniqueid, a.userfield, b.evento, b.fila, b.agente |
|
from pbx_bilhetes a, pbx_eventos_dacs b |
|
where b.uid2 = a.uniqueid |
|
and a.calldate::date = '%s' |
|
and b.fila = 'KAMPO_GM' |
|
and b.evento in('COMPLETAAGENT','COMPLETACALLER', 'TRANSFER') |
|
and a.lastapp <> 'Transferred Call' |
|
and a.dst in(%s) |
|
and a.id_bilhetes = (select max(id_bilhetes) from pbx_bilhetes where calldate::date = a.calldate::date and dst in(%s)); |
|
|
|
select id_bilhetes, calldate::date, call_start, src, dst, duration, billsec, uniqueid, userfield, evento, fila, agente |
|
from tmp_pesquisa_campo |
|
union all |
|
select b.id_bilhetes, b.calldate::date, b.calldate::time as call_start, b.src, b.dst, b.duration, strtoint(c.param2) as billsec, b.uniqueid, b.userfield, c.evento, c.fila, c.agente |
|
from tmp_pesquisa_campo a, pbx_bilhetes b, pbx_eventos_dacs c |
|
where b.calldate::date = a.calldate |
|
and b.dst = a.dst |
|
and b.src = a.src |
|
and b.id_bilhetes <> a.id_bilhetes |
|
and c.uid2 = b.uniqueid |
|
and c.fila = 'KAMPO_GM' |
|
and c.evento in('COMPLETAAGENT','COMPLETACALLER', 'TRANSFER') |
|
and b.lastapp <> 'Transferred Call' |
|
order by 1; |
|
KMPold; |
|
|
|
$baseQuery = <<<KMP |
|
select a.id_bilhetes, a.calldate::date, a.calldate::time as call_start, a.src, a.dst, a.duration, strtoint(b.param2) as billsec, a.uniqueid, a.userfield, b.evento, b.fila, b.agente |
|
from pbx_bilhetes a, pbx_eventos_dacs b |
|
where b.uid2 = a.uniqueid |
|
and a.calldate::date = '%s' |
|
and strtoint(b.param2) >= '%s' |
|
and b.fila = 'KAMPO_GM' |
|
and b.evento in('COMPLETAAGENT','COMPLETACALLER', 'TRANSFER') |
|
and a.lastapp <> 'Transferred Call' |
|
and a.dst in(%s) |
|
order by calldate; |
|
KMP; |
|
|
|
$baseQuery = <<<KMP |
|
select a.id_bilhetes, a.calldate::date, a.calldate::time as call_start, a.src, a.dst, a.duration, a.billsec, a.uniqueid, a.userfield, '' as evento, 'ramal' as fila, '0000' as agente |
|
from pbx_bilhetes a, ( select nome from pbx_sip_ramais where substring(callerid,1,5) = 'KAMPO') b |
|
where b.nome = a.src |
|
and a.calldate::date = '%s' |
|
and a.billsec >= '%s' |
|
and a.lastapp <> 'Transferred Call' |
|
and a.dst in(%s) |
|
order by calldate; |
|
KMP; |
|
|
|
$baseQuery = <<<KMP |
|
select distinct * from ( |
|
select a.id_bilhetes, a.calldate::date, a.calldate::time as call_start, a.src, a.dst, a.duration, a.billsec, a.uniqueid, a.userfield, '' as evento, 'ramal' as fila, '0000' as agente |
|
from pbx_bilhetes a, ( select nome from pbx_sip_ramais where substring(callerid,1,5) = 'KAMPO') b |
|
where b.nome = a.src |
|
and a.calldate::date = '%s' |
|
and a.billsec >= '%s' |
|
and a.lastapp <> 'Transferred Call' |
|
and a.dst in(%s) |
|
and not exists(select '' from pbx_eventos_dacs where uid2 = a.uniqueid) |
|
union all |
|
select a.id_bilhetes, a.calldate::date, a.calldate::time as call_start, a.src, a.dst, a.duration, strtoint(b.param2) as billsec, a.uniqueid, a.userfield, b.evento, b.fila, b.agente |
|
from pbx_bilhetes a, pbx_eventos_dacs b |
|
where b.uid2 = a.uniqueid |
|
and a.calldate::date = '%s' |
|
and strtoint(b.param2) >= '%s' |
|
and b.fila = 'KAMPO_GM' |
|
and b.evento in('COMPLETAAGENT','COMPLETACALLER', 'TRANSFER') |
|
and a.lastapp <> 'Transferred Call' |
|
and a.dst in(%s) ) as kampo order by calldate, call_start |
|
KMP; |
|
|
|
|
|
foreach (glob($dirOrigemArquivo . "*.csv") as $fileName) { |
|
$start = 0; |
|
$arEncontrado = array(); |
|
$arNaoEncontrado = array(); |
|
$handle = fopen($fileName, 'r'); |
|
|
|
while (!feof($handle)) { |
|
$line = fgets($handle); |
|
if ($start++) { |
|
$noDvr = stripos($fileName, "noDVR") !== false; |
|
|
|
$values = explode(";", $line); |
|
|
|
/* |
|
* Array arquivo pesquisa (Pesquisa Original): |
|
[Case ID] => 1012030196 |
|
[Dial #] => 1 |
|
[Study Code] => gmbrp |
|
[Date] => 20210201 |
|
[Start] => 12:16 |
|
[End] => 12:20 |
|
[Intvr ID] => kb76 |
|
[Phone Number] => 27999431855 |
|
[Phone Number 2] => 2799431855 |
|
[QAS6C/QSS6C Phone Number] => |
|
[Phone Number Dialed] => 27999431855 |
|
[Centre/Dialer] => KB/?? |
|
[Status] => Recieved/Verified |
|
[Filename] => 1012030196.mp3 |
|
[Suspend Flag] => |
|
|
|
Arquivo nao encontrados(noDvr): |
|
[Case ID] => 1046002037 |
|
[Date] => 20210311 |
|
[Phone Number] => 48999597144 |
|
[Phone Number 2] => 4834433326 |
|
[QAS6C/QSS6C Phone Number] => 4834433326 |
|
[Phone Number Dialed] => 48999597144 |
|
*/ |
|
if ($noDvr) { |
|
list($caseId, $date, $start, $end, $fone, $fone1, $fone2, $fone3) = array($values[0], $values[1], date("H:i"), date("H:i"), KremoveCode($values[2]), KremoveCode($values[3]), KremoveCode($values[4]), $values[5]); |
|
} else { |
|
list($caseId, $date, $start, $end, $fone, $fone1, $fone2, $fone3) = array($values[0], $values[3], $values[4], $values[5], KremoveCode($values[7]), KremoveCode($values[8]), KremoveCode($values[9]), $values[10]); |
|
} |
|
$dateDb = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2); |
|
$arErro = array('Case ID' => $caseId, 'Date' => $date, 'Phone Number' => $fone, 'Phone Number 2' => $fone1, 'QAS6C/QSS6C Phone Number' => $fone2, 'Phone Number Dialed' => $fone3); |
|
$fonesConsulta = KgetFoneConsulta(KcompletaCelular(array($fone, $fone1, $fone2))); |
|
$query = sprintf($baseQuery, $dateDb, KAMPO_TEMPO_ARQUIVO, $fonesConsulta, $dateDb, KAMPO_TEMPO_ARQUIVO, $fonesConsulta); |
|
$result = pg_query($query); |
|
if ($result && pg_numrows($result)) { |
|
/* |
|
* Valores trazidos do banco |
|
*/ |
|
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) { |
|
$dst = $row["dst"]; |
|
$data = $row["calldate"]; |
|
$linha = $row["src"]; |
|
$callStart = $row["call_start"]; |
|
$filePesquisa = sprintf("%s%s", $baseAudio, $row["userfield"]); |
|
|
|
if (file_exists($filePesquisa)) { |
|
//$arqDestino = sprintf('%s%s-%s-%s.mp3', KChecaDir($dirDestino . 'DVR-' . date("Ymd") . '/'), soNumero($data), soNumero($callStart), ($caseId)); |
|
$arqDestino = sprintf('%s%s-%s-%s.mp3', KChecaDir($dirDestino), soNumero($data), soNumero($callStart), ($caseId)); |
|
if (KRenFile($caseId, $calldate, $start, $filePesquisa, $arqDestino)) { |
|
$arEncontrado[] = array("Case Id" => $caseId, "Numero Discado" => $dst, "Linha" => $linha, "Data" => $data, "Hora" => $callStart, "Arquivo" => basename($arqDestino)); |
|
} else { |
|
$arNaoEncontrado[] = $arErro; |
|
} |
|
} else { |
|
$arNaoEncontrado[] = $arErro; |
|
} |
|
} |
|
} else if ($caseId) { |
|
$arNaoEncontrado[] = $arErro; |
|
} |
|
} |
|
} |
|
|
|
/* |
|
* Move os arquivos processados para o diretorio configurado na variavel "$dirProcessados". |
|
*/ |
|
rename($fileName, KChecaDir($dirProcessados) . basename($fileName)); |
|
|
|
/* |
|
* Cria o relatorio para os telefones enontrados na base de dados. |
|
*/ |
|
KReport($arEncontrado, $dirReport, 1); |
|
|
|
/* |
|
* Cria o relatorio para os telefones nao enontrados na base de dados. |
|
*/ |
|
KReport($arNaoEncontrado, $dirReport, 0); |
|
fclose($handle); |
|
} |
|
|
|
$pid->PidRemove(); |
|
|
|
function KRenFile($caseId, $date, $hora, $fileOrigem, $fileDestino) { |
|
exec(sprintf('ffmpeg -y -loglevel quiet -i %s -acodec libmp3lame "%s"', $fileOrigem, $fileDestino)); |
|
return file_exists($fileDestino) ? true : false; |
|
} |
|
|
|
function KgetFoneConsulta($arFones) { |
|
$strFones = ''; |
|
foreach ($arFones as $fone) { |
|
$fone = soNumero($fone); |
|
|
|
if ($fone) { |
|
$strFones .= $strFones ? ",'{$fone}'" : "'$fone'"; |
|
} |
|
} |
|
|
|
return $strFones; |
|
} |
|
|
|
function KChecaDir($dir) { |
|
|
|
if (!file_exists($dir)) { |
|
exec('mkdir -p ' . $dir); |
|
} |
|
|
|
return $dir; |
|
} |
|
|
|
function KReport($ar, $dirReport, $arqExiste) { |
|
if (!count($ar)) { |
|
return false; |
|
} |
|
$dirBase = $arqExiste ? KChecaDir($dirReport . "DVR-" . date("Ymd") . "/DVR/") : KChecaDir($dirReport . "DVR-" . date("Ymd") . "/NODVR/"); |
|
$fileReport = $arqExiste ? sprintf('%sDVR-%s-%s.csv', $dirBase, date("Ymd"), date("His")) : sprintf('%snoDVR-%s-%s.csv', $dirBase, date("Ymd"), date("His")); |
|
$handle = fopen($fileReport, 'w'); |
|
$line = implode(";", array_keys($ar[0])) . PHP_EOL; |
|
fwrite($handle, $line); |
|
foreach ($ar as $arline) { |
|
$line = implode(";", $arline) . PHP_EOL; |
|
fwrite($handle, $line); |
|
} |
|
fclose($handle); |
|
|
|
return true; |
|
} |
|
|
|
function KcompletaCelular($numeros) { |
|
|
|
$arReproc = array(); |
|
|
|
foreach ($numeros as $numero) { |
|
$len = strlen($numero); |
|
if ($len >= 11) { |
|
$arReproc[] = $numero; |
|
} else if (($len == 10) && ( ((int) substr($numero, 2, 1) >= 6) && ((int) substr($numero, 2, 1) <= 9) )) { |
|
$arReproc[] = substr($numero, 0, 2) . '9' . substr($numero, 2); |
|
} else { |
|
$arReproc[] = $numero; |
|
} |
|
} |
|
|
|
return $arReproc; |
|
} |
|
|
|
/** |
|
* REMOVE O CODIGO DO PAIS |
|
*/ |
|
function KremoveCode($fone){ |
|
if ($fone && (strlen($fone) == 12 || strlen($fone) == 13) && (substr($fone, 0,2 ) == '55')) { |
|
$fone = substr($fone, 2); |
|
} |
|
return $fone; |
|
} |