<?
	
	require_once 'config.php';
	
	$end_kon_ref = mysql_query("SELECT * FROM `konkurs_ref` WHERE `status` = '1' AND `end_date` < '".time()."'" );
    while ($end_kon_row = mysql_fetch_assoc($end_kon_ref)) 
    {
		
		if($end_kon_row['id'] > 0) {
			
			if($end_kon_row['type'] == 1) { $type_ref_kon = 'серфинга'; } else
			if($end_kon_row['type'] == 2) { $type_ref_kon = 'платных писем'; } else
			if($end_kon_row['type'] == 3) { $type_ref_kon = 'платных заданий'; }
		
			$end_kon_ref_temp = mysql_query("SELECT * FROM `konkurs_ref_temp` WHERE `idkonkurs` = '".$end_kon_row['id']."' AND `type` = '".$end_kon_row['type']."' AND summ >= '".$end_kon_row['min_type']."' ORDER BY summ DESC limit 3" );
			$agx=1;
			while ($end_kon_row_temp = mysql_fetch_assoc($end_kon_ref_temp)) 
			{	
				
				if($agx == 1) { $prize_ref_kon = $end_kon_row['comrefprice1']; $user_wan = $end_kon_row_temp['iduser'];} else
				if($agx == 2) { $prize_ref_kon = $end_kon_row['comrefprice2']; $user_tu = $end_kon_row_temp['iduser'];} else
				if($agx == 3) { $prize_ref_kon = $end_kon_row['comrefprice3']; $user_fri = $end_kon_row_temp['iduser'];} 
				
				mysql_query("UPDATE konkurs_ref SET balance = balance - '".$prize_ref_kon."', insert_priz = insert_priz + '".$prize_ref_kon."', lider1 = '$user_wan', lider2 = '$user_tu', lider3 = '$user_fri' WHERE id = '".$end_kon_row['id']."'");
				mysql_query("UPDATE tb_users SET money = money + '".$prize_ref_kon."' WHERE id = '".$end_kon_row_temp['iduser']."'");
				mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('".$end_kon_row_temp['iduser']."','".time()."', '7', 'Поздравляем вы победили в конкурсе $type_ref_kon от реферера, зайняли $agx место, и получили приз в размере $prize_ref_kon р. на рабочий счет.')");
				$agx++;
			}
			
			$end_kon_row_ostatok = mysql_fetch_array( mysql_query( "SELECT * FROM konkurs_ref WHERE id = '".$end_kon_row['id']."'" ) );
			if($end_kon_row_ostatok['balance'] > 0) { 
				mysql_query("UPDATE tb_users SET admoney = admoney + '".$end_kon_row_ostatok['balance']."' WHERE id = '".$end_kon_row_ostatok['referername']."'");
				mysql_query("UPDATE konkurs_ref SET balance = '0' WHERE id = '".$end_kon_row_ostatok['id']."'");
				$ostatok_ref_kon = $end_kon_row_ostatok['balance'];
				$coment_ins = ', сдача (остаток за не зайнятые призовые места) в размере '.$ostatok_ref_kon.' р. засчитана на ваш рекламный счет';
			} else {
				$coment_ins = '';
			}
			
			mysql_query("UPDATE konkurs_ref SET status = '0' WHERE id = '".$end_kon_row['id']."'");
			mysql_query("DELETE FROM konkurs_ref_temp WHERE idkonkurs = '".$end_kon_row['id']."'");
			
			if($user_wan > 0 or $user_tu > 0 or $user_fri > 0) {
				if($user_wan > 0) {$winer_ref_1 = 'ID:'.$user_wan.'';} else {$winer_ref_1 = '';} 
				if($user_tu > 0) {$winer_ref_2 = ', ID:'.$user_tu.'';} else {$winer_ref_2 = '';} 
				if($user_fri > 0) {$winer_ref_3 = ', ID:'.$user_fri.'';} else {$winer_ref_3 = '';}
				$winner_text_alt = 'получили приз';
				$winner_text_n = 'победители';
			} else {
				$winer_ref_1 = '';
				$winer_ref_2 = '';
				$winer_ref_3 = '';
				$winner_text_alt = 'нет!';
				$winner_text_n = 'победителей';
			}
			
			mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('".$end_kon_row_ostatok['referername']."','".time()."', '7', 'Ваш конкурс $type_ref_kon для рефералов завершился, $winner_text_n $winer_ref_1 $winer_ref_2 $winer_ref_3 $winner_text_alt $coment_ins ')");
		}
    }
?>