<?php
require_once "config.php";

global $DB;

if(date("G")==0 && date("i")<10 && date("i")>=0)
{
	$date = date("d");
	$payday=$DB->selectCell('SELECT id from idate where day=?',$date);
	if($payday) 
	{
		$data = $DB->selectPage($totalCount,"SELECT pack.price,cli.balance,inter.* FROM internet as inter
						     LEFT JOIN clients AS cli ON (inter.client_id=cli.id) 
						     LEFT JOIN packets as pack ON (pack.id=inter.packet_id) 
						     WHERE inter.time_id=?d ORDER by id",$payday);
		foreach($data as $k=>$v)
		{
			$obj["balance"]=$v[balance]-$v[price];
			$DB->query("UPDATE clients set ?a WHERE id=?d",$obj,$v[client_id]);
			if($obj["balance"]<0)
			{
				$DB->query("UPDATE internet set active='0' WHERE client_id=?d",$v[client_id]);
				if($v[active]!=0 && $v[active]!=2)
				echo "/sbin/ipfw -q table ".($v[packet_id]+26)." delete ".$v[ip]." \n";
			}
		}
	}
}
$data_t = $DB->selectPage($totalCount,"SELECT cli.balance,inter.* FROM internet as inter
                                       LEFT JOIN clients AS cli ON (inter.client_id=cli.id)
                                       WHERE inter.active='0' ORDER by id");
	foreach($data_t as $k=>$v)
        {
        	if($v["balance"]>=0)
                {
                	$DB->query("UPDATE internet set active='1' WHERE client_id=?d",$v[client_id]);
                        echo "/sbin/ipfw -q table ".($v[packet_id]+26)." add ".$v[ip]." \n";
                }
      	}

?>
