// Verifica se a campanha est<EFBFBD> ativa ou pendente
$query = "select cmp_status from pbx_campanha where cmp_status in ('1','6') and cmp_descricao = '$campanha'";
$result = pg_query($conexao, $query);
$row = pg_fetch_row($result);
$status = $row[0];
$numlinha = pg_num_rows($result);
if (!$numlinha) {
$query = "update pbx_campanha_agendamento set agd_status = 'CAMPANHA-INDISPONIVEL', agd_agendado = (agd_agendado + 1) where agd_id = '$agd_id'";
pg_query($query);
pg_close();
write("HANGUP");
read();
exit;
}
// Verifica se h<EFBFBD> agentes disponiveis para a campanha
$query = "select count(*) as livres from pbx_supervisor_agentes where upper(status) = upper('livre') and upper(dac) = upper('$campanha')";
$result = pg_query($conexao, $query);
$row = pg_fetch_row($result);
$numlinha = $row[0];
if (!$numlinha) {
$query = "update pbx_campanha_agendamento set agd_status = 'AGENTES-INDISPONIVEIS', agd_agendado = (agd_agendado + 1) where agd_id = '$agd_id'";
pg_query($query);
$query = "select count(*) from pbx_campanha_agendamento where cmp_id = (select cmp_id from pbx_campanha where upper(cmp_descricao) = upper('$campanha')) and agd_agendado < '3' and agd_status <> 'ATENDIDA' ";
$result = pg_query($conexao, $query);
$row = pg_fetch_row($result);
$agendado = $row[0];
if (!$agendado && $status == '6') {
$query = "update pbx_campanha set cmp_status = '5' where upper(cmp_descricao) = upper('$campanha')";