session_start();
$menu=3;
include("lib/conexao.php");
include("lib/acesso.php");
include("lib/controle.php");
include("lib/verificaSessao.php");
include("lib/funcoes.php");
$c = Acesso::SELECT_ID_CAMINHO('', 'carteira', 1);
$mensagemrotina = "";
//rotina para dar baixa automática nas faturas do inter
require 'lib/interv2/vendor/autoload.php';
use Divulgueregional\ApiInterV2\InterBanking;
$config = [
'certificate' => INTER_CLIENT_CERTIFICADO,
'certificateKey' => INTER_CLIENT_CHAVE,
'verify' => false
];
$bankingInter = new InterBanking($config);
$token = $bankingInter->getToken(INTER_CLIENT_ID, INTER_CLIENT_SECRET);
if(isset($token['error'])) {
echo $token['error'];
} else {
$bankingInter->setToken($token['access_token']);
$abertas = Acesso::SELECT('f.id, f.boletonossonumero', 'fatura f join cliente_cobrancas c on f.cobranca=c.id', 'f.pago=0 and f.status <> 4 and c.carteira=1', '', '');
foreach ($abertas as $key => $a) {
try {
$boletoDetalhado = $bankingInter->boletoDetalhado($a['boletonossonumero']);
if(isset($boletoDetalhado['status'])) {
if($boletoDetalhado['status']==200) {
if($boletoDetalhado['response']->situacao=='PAGO') {
$databaixa = substr($boletoDetalhado['response']->dataHoraSituacao, 0, 10);
$cols = array('pago'=>1, 'baixaem' => $databaixa, 'baixaautomatica' => 1, 'valorpago' => $boletoDetalhado['response']->valorTotalRecebimento);
Acesso::UPDATE('fatura', $cols, $a['id']);
Controle::INSERT('fatura', $a['id'], 'Baixa', 'Baixa automática na fatura', $idU);
$mensagemrotina .= "Boleto ".$a['id']." baixado
";
}
} else {
$mensagemrotina .= "Erro ao buscar boleto id ".$a['id'] . "
";
}
} else {
$mensagemrotina .= "Erro ao buscar boleto id ".$a['id'] . "
";
}
} catch (\Exception $e) {
$mensagemrotina .= "Erro ao buscar boleto id ".$a['id'] . ": ".$e->getMessage()."
";
}
}
}
//rotina para desbloquear clientes
$bloqueados = Acesso::SELECT('id, nome', 'cliente', 'bloqueado=1', '', '');
foreach ($bloqueados as $key => $b) {
$abertas = Acesso::SELECT('id', 'fatura', 'now() > vencimento and pago=0 and status <> 4 and cliente='.$b['id'], '', '');
//se não tem mais faturas em aberto, desbloqueia
if(sizeof($abertas)==0) {
$cols = array('bloqueado'=>0);
Acesso::UPDATE('cliente', $cols, $b['id']);
Controle::INSERT('cliente', $b['id'], 'Desbloqueio automático', $b['nome'], $idU);
$mensagemrotina .= "Cliente ".$b['nome']." desbloqueado
";
}
}
//rotina para bloquear
$bloquear = Acesso::SELECT('f.cliente, c.nome', 'fatura f join cliente c on f.cliente=c.id join cliente_cobrancas cc on cc.id=f.cobranca', 'c.bloqueado=0 and f.status<>4 and f.pago=0 and DATEDIFF(now(), f.vencimento) > cc.diasbloqueio', '', 'group by f.cliente, c.nome');
foreach ($bloquear as $key => $b) {
$cols = array('bloqueado'=>1);
Acesso::UPDATE('cliente', $cols, $b['cliente']);
Controle::INSERT('cliente', $b['cliente'], 'Bloqueio automático', $b['nome'], $idU);
$mensagemrotina .= "Cliente ".$b['nome']." bloqueado
";
}
//clientes bloqueados
$clientes = Acesso::SELECT('id, nome', 'cliente', 'bloqueado=1', '', 'order by nome');
//faturas vencidas
$vencidas = Acesso::SELECT('c.nome, f.valortotal, f.vencimento, f.id, f.boleto, f.boletonossonumero', 'cliente c join fatura f on c.id=f.cliente', 'f.vencimento<(NOW() - INTERVAL 2 DAY) and f.pago=0 and status <> 4', '', 'order by f.vencimento');
//faturas pagas
//$pagas = Acesso::SELECT('c.nome, f.valortotal, f.vencimento, f.baixaem, f.id, f.boleto, f.boletonossonumero', 'cliente c join fatura f on c.id=f.cliente', 'f.pago=1', '', 'order by baixaem desc limit 20');
?>