<?
require_once('../connect/connect.php');
include_once('../lib/lib.php');
$bp="../";
mysql_query("DELETE FROM calendar WHERE calendar < NOW()-INTERVAL 1 DAY");
mysql_query("UPDATE rec SET opl='0', str='' WHERE opl='1' AND opl_data < NOW()");
include('../admin/svod_rec.php');

$arr_plugin = view_dir("../plugin/");

foreach($arr_plugin as $arr_t)
	{
	if(in_array($arr_t, $arr_vip_spec))
	mysql_query("UPDATE ".$arr_t." SET vip_data='1970-01-01 00:00:00', vip='0', vip_spec='0' WHERE (vip='1' OR vip_spec='1') AND vip_data < NOW() ");
	}


$r=mysql_query("SELECT * FROM akk WHERE opl='1' AND opl_data < NOW()");
$i=0;
$arr_ids = '(';
while($rr=mysql_fetch_array($r))
	{
	$arr_id[]=$rr['id_akk'];
	if($i)
	$arr_ids .= ',';
	$arr_ids .= $rr['id_akk'];
	$i++;
	}
$arr_ids .= ')';

if($arr_id && count($arr_id)>0)
	{
	mysql_query("UPDATE akk SET opl='0', 
	opl_data='0000-00-00 00:00:00', 
	opl_data2='0000-00-00 00:00:00' 
	WHERE id_akk IN ".$arr_ids);
	
	//////////////////////////////
	include_once("../cashe_n/_svod_account_".$default_dostup.".php");

		if($arr_plugin)
			{
			foreach($arr_plugin as $arr_t)
				{
				include_once("../plugin/".$arr_t."/index.php");

				if(!$arr_typ_catalog[$arr_t]['user_no'])
					{
					if(!$arr_account[$arr_t]['discount'])
						{
						$list_f = mysql_list_fields ($database, $arr_t);
						$n_fields = mysql_num_fields($list_f);
						$disc=0;
						for($i=0; $i<$n_fields; $i++)
							{
							if(mysql_field_name($list_f, $i)=='f_discount_') {$disc=1; break;}
							}
						if($disc)
							{
							mysql_query("UPDATE ".$arr_t." SET f_discount_= '0' WHERE IN ".$arr_ids);
							}
						}
					
					
					foreach ($arr_id as $ari)
						{
						if($arr_account[$arr_t]['limit'])
						$q="SELECT id FROM ".$arr_t." WHERE id_akk='".$ari."' ORDER BY id DESC LIMIT ".$arr_account[$arr_t]['limit']." ";
						else
						$q="";
						if($q)
							{
							$eq=mysql_query($q);
							$i=0;
							$kr='';
							while($eeq=mysql_fetch_array($eq))
								{
								if($i)
								$kr .= ',';
								$kr .= $eeq['id'];
								$i++;
								}
							if($i)
								{
								mysql_query("UPDATE ".$arr_t." SET activ='1' WHERE id_akk='".$ari."' AND id NOT IN (".$kr.") ");
								}
							} 
						else
							{
							mysql_query("UPDATE ".$arr_t." SET activ='1' WHERE id_akk='".$ari."' ");
							}
						}
					}
				}
			}
	///////////////////////////////
	}

?>