<?php
include "../../config/connect.php";

$ambil=$koneksi->query("SELECT * FROM log_voucher");
$ubah= $ambil->fetch_assoc();

$cekid = $ubah['nomor'];

$ambilv = $koneksi->query("SELECT * FROM genvoucher WHERE IdGenVoucher > '$cekid' AND StaVoucher = '1' AND Aktif = ''");
$cekv = $ambilv->fetch_assoc();

$datav = $cekv['Voucher'];

if($datav != "")
{
    //CEK VOUCHER DI MIKROTIK
    $set1 = $koneksi->query("SELECT * FROM setting WHERE IdSetting='1'");
    $pset = $set1->fetch_assoc();

    $m_ip   = $pset['ip_mikrotik'];
  	$m_user = $pset['user_mikrotik'];
  	$m_pass = $pset['pass_mikrotik'];
  	$m_port = $pset['port_mikrotik'];

  	require('../../../4pKhOts0t@27/router_api/routeros_api.class.php');
	$API = new RouterosAPI();
	$API->debug = false;

    if ($API->connect2($m_ip, $m_user, $m_pass, $m_port))
    {
	    
    $ambilv1 = $koneksi->query("SELECT * FROM genvoucher WHERE IdGenVoucher > '$cekid' AND StaVoucher = '1' AND Aktif = '' ORDER BY IdGenVoucher LIMIT 20");
    while ($cekv1 = $ambilv1->fetch_assoc())
    {
    $iddatav1    = $cekv1['IdGenVoucher'];
    $datav1      = $cekv1['Voucher'];
    
        $cek = $API->comm('/ip/hotspot/user/print', 
              array(
                  "?name"     => $datav1,
                  ));
        if ($cek) 
        {
            $koneksi->query("UPDATE log_voucher SET nomor='$iddatav1' WHERE id ='1'");
        }
        else
        {
            $koneksi->query("UPDATE genvoucher SET StaVoucher = '0' WHERE IdGenVoucher = '$iddatav1'");
            $koneksi->query("UPDATE log_voucher SET nomor='$iddatav1' WHERE id ='1'");
         }
    }
   $API->disconnect();
    }
   echo "ADA DATA";
}
else
{
    $koneksi->query("UPDATE log_voucher SET nomor='0' WHERE id ='1'");
}
?>