<?
include_once '/home/krista02/vladsoft.tech/misoft/engine/config.php'; /// подключение к бд

	
	$end_kon_ref = mysql_query("SELECT * FROM `contests` WHERE `date_end` < '".time()."'" );
    while ( $end_kon_row = mysql_fetch_array( $end_kon_ref ) ) 
    {
		#считаем количество призовых мест
		if($end_kon_row['priz1'] != 0) { $pr1 = '1'; } else { $pr1 = '0'; }
		if($end_kon_row['priz2'] != 0) { $pr2 = '1'; } else { $pr2 = '0'; }
		if($end_kon_row['priz3'] != 0) { $pr3 = '1'; } else { $pr3 = '0'; }
		if($end_kon_row['priz4'] != 0) { $pr4 = '1'; } else { $pr4 = '0'; }
		if($end_kon_row['priz5'] != 0) { $pr5 = '1'; } else { $pr5 = '0'; }
		if($end_kon_row['priz6'] != 0) { $pr6 = '1'; } else { $pr6 = '0'; }
		if($end_kon_row['priz7'] != 0) { $pr7 = '1'; } else { $pr7 = '0'; }
		if($end_kon_row['priz8'] != 0) { $pr8 = '1'; } else { $pr8 = '0'; }
		if($end_kon_row['priz9'] != 0) { $pr9 = '1'; } else { $pr9 = '0'; }
		if($end_kon_row['priz10'] != 0) { $pr10 = '1'; } else { $pr10 = '0'; }
	
		$randPriz = ($pr1 + $pr2 + $pr3 + $pr4 + $pr5 + $pr6 + $pr7 + $pr8 + $pr9 + $pr10);

		$kon1User = mysql_query( "SELECT * FROM contests_temp WHERE id_konkurs = '" . $end_kon_row['id'] . "' and type = '" . $end_kon_row['type'] . "' and summ >= '" . $end_kon_row['min_type'] . "' ORDER BY summ DESC LIMIT $randPriz" );
        $i = 0;
		
			while ( $kon1UserRow = mysql_fetch_array( $kon1User ) ) {
				$i++;
			
				#проверяем номер места записи и присваиваем данные
				if($i == 1) { $prize_ref_kon = $end_kon_row['priz1']; $user1 = $kon1UserRow['iduser']; } else
				if($i == 2) { $prize_ref_kon = $end_kon_row['priz2']; $user2 = $kon1UserRow['iduser']; } else
				if($i == 3) { $prize_ref_kon = $end_kon_row['priz3']; $user3 = $kon1UserRow['iduser']; } else
				if($i == 4) { $prize_ref_kon = $end_kon_row['priz4']; $user4 = $kon1UserRow['iduser']; } else
				if($i == 5) { $prize_ref_kon = $end_kon_row['priz5']; $user5 = $kon1UserRow['iduser']; } else
				if($i == 6) { $prize_ref_kon = $end_kon_row['priz6']; $user6 = $kon1UserRow['iduser']; } else
				if($i == 7) { $prize_ref_kon = $end_kon_row['priz7']; $user7 = $kon1UserRow['iduser']; } else
				if($i == 8) { $prize_ref_kon = $end_kon_row['priz8']; $user8 = $kon1UserRow['iduser']; } else
				if($i == 9) { $prize_ref_kon = $end_kon_row['priz9']; $user9 = $kon1UserRow['iduser']; } else
				if($i == 10) { $prize_ref_kon = $end_kon_row['priz10']; $user10 = $kon1UserRow['iduser']; } 
			
				#зачисляем призы
				mysql_query("UPDATE user SET money = money + '". $prize_ref_kon ."', earnings_competition = earnings_competition + '". $prize_ref_kon ."' WHERE id = '". $kon1UserRow['iduser'] ."'");
				#отнимаем деньги с бюджета конкурса за выплаченые призы
				mysql_query("UPDATE contests SET allsumm = allsumm - '". $prize_ref_kon ."' WHERE id = '". $end_kon_row['id'] ."'");
			
			}
		
			#Создаем запись в таблице завершеных конкурсов о том что конкурс завершился
			mysql_query("INSERT INTO contests_end SET id_konkurs = '". $end_kon_row['id'] ."', type = '". $end_kon_row['type'] ."', date_start = '". $end_kon_row['date_start'] ."', date_end = '". $end_kon_row['date_end'] ."', iduser1 = '". $user1 ."', iduser2 = '". $user2 ."', iduser3 = '". $user3 ."', iduser4 = '". $user4 ."', iduser5 = '". $user5 ."', iduser6 = '". $user6 ."', iduser7 = '". $user7 ."', iduser8 = '". $user8 ."', iduser9 = '". $user9 ."', iduser10 = '". $user10 ."', priz1 = '". $end_kon_row['priz1'] ."', priz2 = '". $end_kon_row['priz2'] ."', priz3 = '". $end_kon_row['priz3'] ."', priz4 = '". $end_kon_row['priz4'] ."', priz5 = '". $end_kon_row['priz5'] ."', priz6 = '". $end_kon_row['priz6'] ."', priz7 = '". $end_kon_row['priz7'] ."', priz8 = '". $end_kon_row['priz8'] ."', priz9 = '". $end_kon_row['priz9'] ."', priz10 = '". $end_kon_row['priz10'] ."', autor = '".$end_kon_row['autor']."', number_of_prizes = '" . $randPriz . "' ");
		
			#возвращаем здачу рекламодателю
			$end_kon_row_ostatok = mysql_fetch_array( mysql_query( "SELECT allsumm, autor FROM contests WHERE id = '".$end_kon_row['id']."'" ) );
			if($end_kon_row_ostatok['allsumm'] > 0) { 
				mysql_query("UPDATE user SET adv_money = adv_money + '".$end_kon_row_ostatok['allsumm']."' WHERE id = '".$end_kon_row_ostatok['autor']."'");
				///mysql_query("UPDATE contests SET allsumm = '0' WHERE id = '".$end_kon_row_ostatok['id']."'");
			}
		
			#удаляем участников
			mysql_query("DELETE FROM contests_temp WHERE id_konkurs = '" . $end_kon_row['id'] . "'");
		
			#удаляем коркунс
			mysql_query("DELETE FROM contests WHERE id = '" . $end_kon_row['id'] . "'");
		
		
	}


?>