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.
206 lines
6.6 KiB
206 lines
6.6 KiB
#!/usr/bin/php -q |
|
<?php |
|
|
|
include "util/util.php"; |
|
|
|
error_reporting(E_ERROR); |
|
ini_set('display_errors', 0); |
|
|
|
$conexao = pg_connect(__GetConectString()); |
|
|
|
define('CONF_DATA_HORA_INI', '2020-06-01 00:00:00'); |
|
define('CONF_DATA_HORA_FIM', '2020-08-07 23:59:59'); |
|
|
|
/* |
|
* Calculo do tempo de execucao. |
|
*/ |
|
$time = time(); |
|
|
|
/* |
|
* Informe o arquivo de dados na linha de comando se necessario. |
|
*/ |
|
$defFile = '/var/log/asterisk/cdr-custom/Master.csv'; |
|
$workFile = isset($argv[1]) && file_exists($argv[1]) ? $argv[1] : $defFile; |
|
$pathLog = "/var/log/asterisk/resumo-cdr.log"; |
|
|
|
try { |
|
|
|
/* |
|
* Representa a as colunas da tabela "cdr" de acordo com a sequencia do Master.csv |
|
*/ |
|
$fields = array("clid", "src", "dst", "dcontext", "channel", "dstchannel", "lastapp", "lastdata", "calldate", "answerdate", "hangupdate", "duration", "billsec", "disposition", "amaflags", "uniqueid", "userfield"); |
|
|
|
/* |
|
* Forma a linha de insert, basta alterar o array conforme versao do asterisk. |
|
*/ |
|
$fildSel = ''; |
|
foreach ($fields as $value) { |
|
$fildSel .= !$fildSel ? $value : ("," . $value); |
|
} |
|
$queryModel .= "insert into cdr({$fildSel})values({values});\n"; |
|
|
|
/* |
|
* Abre arquivo de dados. |
|
*/ |
|
if (!($handle = fopen($workFile, 'r'))) { |
|
GeraExcept("Não foi possível abrir o arquivo! $workFile \n"); |
|
} |
|
|
|
pg_query("truncate table cdr"); |
|
$inclusao = 'OK'; |
|
$erroImport = 0; |
|
$registros = 0; |
|
while (!feof($handle)) { |
|
$buffer = trim(fgets($handle)); |
|
$values = PreparaLinhaCdr($buffer); |
|
$registros++; |
|
try { |
|
$data = ''; |
|
|
|
if(strtotime($values[9]) < strtotime(CONF_DATA_HORA_INI) || strtotime($values[9]) > strtotime(CONF_DATA_HORA_FIM)){ |
|
continue; |
|
} |
|
|
|
unset($values[15],$values[18],$values[19],$values[20],$values[21],$values[22],$values[23]); |
|
|
|
foreach ($values as $value) { |
|
$data .= !$data ? __NullDb($value) : ("," . __NullDb($value)); |
|
} |
|
|
|
$query = str_replace("{values}", $data, $queryModel); |
|
|
|
if (!pg_query($query)) { |
|
GeraExcept("Erro: {$query}", true); |
|
} |
|
} catch (Exception $ex) { |
|
$erroImport++; |
|
WriteLog($ex->getMessage(), $pathLog); |
|
echo $ex->getMessage() . "\n"; |
|
} |
|
} |
|
|
|
$query = "insert into ast_bilhetes(accountcode, src, dst, dcontext, clid, channel, dstchannel, lastapp, lastdata, calldate, duration, billsec, disposition, amaflags, uniqueid, userfield) |
|
select coalesce(accountcode, ''), coalesce(src, ''), coalesce(dst, ''), coalesce(dcontext, ''), clid, coalesce(channel, ''), coalesce(dstchannel, ''), coalesce(lastapp, ''), coalesce(lastdata, ''), calldate, duration, billsec, coalesce(disposition, ''), strtoint(amaflags) , uniqueid, coalesce(userfield, '') |
|
from cdr a where not exists(select '' from pbx_bilhetes where uniqueid = a.uniqueid);"; |
|
if (!pg_query($query)) { |
|
$inclusao = 'Erro'; |
|
GeraExcept("Erro ao carregar cdr", true); |
|
} |
|
} catch (Exception $ex) { |
|
echo $ex->getMessage() . "\n"; |
|
WriteLog($ex->getMessage(), $pathLog); |
|
} |
|
|
|
WriteLog(sprintf("Execucao encerrada: %ss Registros: %s Erros Processamento: %s Inclusao: %s ", round(time() - $time), $erroImport, $inclusao), $pathLog); |
|
|
|
function PreparaLinhaCdr($buffer) { |
|
$tamBuffer = strlen($buffer); |
|
$contaAspa = 0; |
|
$start = 0; |
|
$value = ''; |
|
$fields = array(); |
|
$ch = ''; |
|
$chOld = ''; |
|
$VerificaQuebra = function($numOcorencia) { |
|
return ($numOcorencia % 2) == 0; |
|
}; |
|
|
|
for ($i = 0; $i < $tamBuffer; $i++) { |
|
|
|
$ch = substr($buffer, $i, 1); |
|
|
|
if (($ch == '"') && ($chOld != '\\')) { |
|
$contaAspa++; |
|
} |
|
|
|
if (((($ch == ',')) && $VerificaQuebra($contaAspa)) || (($i + 1) == $tamBuffer)) { |
|
$contaAspa = 0; |
|
$fields[] = __RemoveAcentos(trim($value)); |
|
$value = ''; |
|
} |
|
|
|
if (((($ch != '"') && ($ch != ',')) || (($ch == ',') && !$VerificaQuebra($contaAspa))) || ($chOld == '\\')) { |
|
$value .= $ch; |
|
} |
|
|
|
$chOld = $ch; |
|
} |
|
|
|
return $fields; |
|
} |
|
|
|
function PreparaLinhaCdrOld($buffer) { |
|
$tamBuffer = strlen($buffer); |
|
$IniciaCampo = array(); |
|
$start = 0; |
|
$value = ''; |
|
$fields = array(); |
|
$VerificaQuebra = function($ar) { |
|
return (count($ar) % 2) == 0; |
|
}; |
|
|
|
for ($i = 0; $i < $tamBuffer; $i++) { |
|
|
|
$ch = substr($buffer, $i, 1); |
|
|
|
if ($ch == '"') { |
|
$IniciaCampo[] = $ch; |
|
} |
|
|
|
if (((($ch == ',')) && $VerificaQuebra($IniciaCampo)) || (($i + 1) == $tamBuffer)) { |
|
$IniciaCampo = array(); |
|
$fields[] = __RemoveAcentos($value); |
|
$value = ''; |
|
} |
|
|
|
if (($ch != '"') && ($ch != ',')) { |
|
$value .= $ch; |
|
} |
|
} |
|
|
|
return $fields; |
|
} |
|
|
|
function __GetConectString() { |
|
$dbPort = 5432; |
|
$dbHost = '127.0.0.1'; |
|
$dbName = 'pbx'; |
|
$dbUser = 'contacte'; |
|
$dbPassword = 'ctepgSQL'; |
|
return "host='$dbHost' port='$dbPort' dbname='$dbName' user='$dbUser' password='$dbPassword'"; |
|
} |
|
|
|
function __NullDb($value) { |
|
/* |
|
* Var nao esta setado eh empty |
|
* Zero eh empty |
|
* String vazia '' eh empty |
|
* String vazia "" eh empty |
|
* Espaco " " nao eh empty |
|
* False eh empty |
|
* Array vazio eh empty |
|
*/ |
|
|
|
/* Valores empty que nao serao considerados nulos para o banco de dados */ |
|
if (is_numeric($value) && (((int) $value) === 0)) |
|
return __QuotedStr($value); |
|
return empty($value) ? 'null' : __QuotedStr($value); |
|
} |
|
|
|
function __QuotedStr($str) { |
|
return sprintf("'%s'", pg_escape_string(trim($str))); |
|
} |
|
|
|
function __RemoveAcentos($str, $upper = False) { |
|
$text = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý'); |
|
$subs = array('A', 'A', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'U', 'P', 'B', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y'); |
|
for ($i = 0; $i < strlen($str); $i++) { |
|
$j = array_search($str[$i], $text); |
|
if ($j) |
|
$str[$i] = $subs[$j]; |
|
} |
|
|
|
if ($upper) |
|
$str = strtoupper($str); |
|
return($str); |
|
}
|
|
|