<?
### Версия ПО (ДМ) 1.0.0.0 ( (c)2018 MISOFT Продукт защищен регистрацией патента и авторских прав! www.misoft.studio )
######################################################################################################################

session_start();

sleep( 1 );

header( 'Content-type: text/html; charset=windows-1251' );

if ( $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) exit();

if ( !isset( $_SESSION['iduser'] ) ) exit();

define( 'ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] );

include_once ROOT_DIR . '/engine/config.php';

$get_basket = mysql_query( "SELECT * FROM basket WHERE iduser = '" . $_SESSION['iduser'] . "'" );
$get_basket_row = mysql_fetch_array( mysql_query( "SELECT SUM(price) as sum FROM basket WHERE iduser = '" . $_SESSION['iduser'] . "'" ) );

#Сверяем сумму с балансом
if ( $get_basket_row['sum'] > $config_user['savingscards'] ) {
  echo 'На Вашем кэшбэк счете недостаточно средств';
  exit();
}

if ( mysql_num_rows( $get_basket ) > 0 ) {
  while ( $row_basket = mysql_fetch_array( $get_basket ) ) {
	switch ( $row_basket['type'] ) {
	  #Серфинг
	  case 0:
		mysql_query( "UPDATE dynlink SET balance = balance + '" . $row_basket['price'] . "' WHERE id = '" . $row_basket['ident'] . "' and iduser = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 0, date = '" . time() . "', h_text = 'Пополнение рекламной площадки <b>№" . $row_basket['ident'] . "</b>', payopt = 3" );
		#Проверка, если площадка в серфинге Plus
		$serf_plus = mysql_query( "SELECT id FROM dynlink WHERE id = '" . $row_basket['ident'] . "' and plus = 1 LIMIT 1" );
		if ( mysql_num_rows( $serf_plus ) > 0 ) {
		  mysql_query( "INSERT INTO dynlink_plus SET iduser = '" . $_SESSION['iduser'] . "', date = '" . time() . "' + 172800" );
		}
		break;
	  #Контекстная реклама
	  case 1:
		mysql_query( "UPDATE klink SET balance = balance + '" . $row_basket['price'] . "' WHERE id = '" . $row_basket['ident'] . "' and iduser = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 1, date = '" . time() . "', h_text = 'Пополнение рекламной площадки <b>№" . $row_basket['ident'] . "</b>', payopt = 3" );
		break;
	  #Тесты
	  case 2:
		mysql_query( "UPDATE tests SET balance = balance + '" . $row_basket['price'] . "' WHERE id = '" . $row_basket['ident'] . "' and iduser = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 2, date = '" . time() . "', h_text = 'Пополнение рекламной площадки <b>№" . $row_basket['ident'] . "</b>', payopt = 3" );
		break;
	  #Баннеры 200 х 300
	  case 3:
		mysql_query( "UPDATE banners SET balance = balance + '" . $row_basket['price'] . "' WHERE id = '" . $row_basket['ident'] . "' and iduser = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 3, date = '" . time() . "', h_text = 'Пополнение рекламной площадки <b>№" . $row_basket['ident'] . "</b>', payopt = 3" );
		break;
	  #Баннеры 468 х 60
	  case 4:
		mysql_query( "UPDATE banners SET balance = balance + '" . $row_basket['price'] . "' WHERE id = '" . $row_basket['ident'] . "' and iduser = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 3, date = '" . time() . "', h_text = 'Пополнение рекламной площадки <b>№" . $row_basket['ident'] . "</b>', payopt = 3" );
		break;
	  #Задания
	  case 5:
		mysql_query( "UPDATE tasks SET balance = balance + '" . $row_basket['price'] . "' WHERE id = '" . $row_basket['ident'] . "' and iduser = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 4, date = '" . time() . "', h_text = 'Пополнение рекламной площадки <b>№" . $row_basket['ident'] . "</b>', payopt = 3" );
		break;
	  #V.I.P задания
	  case 6:
		$pr_task = mysql_query( "SELECT id, viptime FROM tasks WHERE id = '" . $row_basket['ident'] . "' LIMIT 1" );
		if ( mysql_num_rows( $pr_task ) > 0 ) {
		  $pr_task_row = mysql_fetch_array( $pr_task );
		  if ( $pr_task_row['viptime'] >= time() ) {
			$time_vip = $row_basket['price'] * 86400;
			mysql_query( "UPDATE tasks SET viptime = viptime + '$time_vip' WHERE id = '" . $pr_task_row['id'] . "'" );
			mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 4, date = '" . time() . "', h_text = 'Размещение задания <b>№" . $row_basket['ident'] . "</b> в V.I.P блоке', payopt = 3" );
		  } else {
			$time_vip = $row_basket['price'] * 86400 + time();
			mysql_query( "UPDATE tasks SET  vip = 1, viptime = '$time_vip' WHERE id = '" . $pr_task_row['id'] . "'" );
			mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 4, date = '" . time() . "', h_text = 'Размещение задания <b>№" . $row_basket['ident'] . "</b> в V.I.P блоке', payopt = 3" );
		  }
		}
		break;
	  #Рекламный счет	
	  case 15:
		mysql_query( "UPDATE user SET adv_money = adv_money + '" . $row_basket['price'] . "'WHERE id = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $pr_task_row['id'] . "', price = '" . $row_basket['price'] . "', type = 100, date = '" . time() . "', h_text = 'Пополнение рекламного счета ', payopt = 3" );
		break;
	  #Ярмарка
	  case 14:
		$referalsINFO = mysql_fetch_assoc(mysql_query("SELECT id, referer, referer2 FROM user WHERE id = '" . $row_basket['ident'] . "'")); 
		$imINFO = mysql_fetch_assoc(mysql_query("SELECT id, referer, autoback FROM user WHERE id = '" . $_SESSION['iduser'] . "'"));
		$priceREFERAL = ($config['coefficient_fair'] * $row_basket['price']);
		$ThePercentageSystem = ($row_basket['price'] - $priceREFERAL);
		#Начисляем вознаграждение за продажу с вычетом проц. сист
		mysql_query("UPDATE user SET money = money + '" . $priceREFERAL . "', earnings_fair = earnings_fair + '" . $priceREFERAL . "' WHERE id = '" . $referalsINFO['referer'] . "'");
		#Переписываем рефералу которого продают нового реферала первого и второго уровней
		mysql_query("UPDATE user SET referer = '" . $_SESSION['iduser'] . "', referer2 = '" . $imINFO['referer'] . "', toref = '0', toref2 = '0', autoback = '" . $imINFO['autoback'] . "', price = '0' WHERE id = '" . $row_basket['ident'] . "'");
		#Пишем в историю операций продавцу о том что его реферал продался
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $referalsINFO['referer'] . "', price = '" . $priceREFERAL . "', type = 14, date = '" . time() . "', h_text = 'Поздравляем ваш реферал <b>ID" . $row_basket['ident'] . "</b> продан на ярмарке (комиссия системы ".$ThePercentageSystem." ".$config['valutaname'].")', payopt = 51" );
		#Пишем событие в ленту пользователю
		$ref_info_fair_dev = mysql_fetch_assoc(mysql_query("SELECT username FROM user WHERE id = '" . $row_basket['ident'] . "'"));
		mysql_query( "INSERT INTO developments_user SET date = '" . time() . "', status = '1', events = 'Ваш реферал (".$ref_info_fair_dev['username'].", id".$row_basket['ident'].") был продан на бирже, вы заработали ".$priceREFERAL." ".$config['valutaname']." Примите наши поздравления, так держать!', iduser = '" . $referalsINFO['referer'] . "' " );	
		#Пишем в историю операций покупателю о том что он купил реферала
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 14, date = '" . time() . "', h_text = 'Вы купили реферала <b>ID" . $row_basket['ident'] . "</b> на ярмарке', payopt = 3" );
		#Пишем в выручку суму прибили проекта от продажи
		mysql_query( "UPDATE viruchka SET money = money + '".$ThePercentageSystem."' WHERE ident = '15'" );
		break;
	  #Рекламная цепочка
	  case 18:
		mysql_query( "UPDATE chain SET balance = balance + '" . $row_basket['price'] . "' WHERE id = '" . $row_basket['ident'] . "' and iduser = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 18, date = '" . time() . "', h_text = 'Пополнение рекламной площадки <b>№" . $row_basket['ident'] . "</b>', payopt = 3" );
		break;
	  #Статическая реклама
	  case 20:
		mysql_query( "UPDATE statlink SET balance = balance + '" . $row_basket['price'] . "' WHERE id = '" . $row_basket['ident'] . "' and iduser = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 20, date = '" . time() . "', h_text = 'Пополнение рекламной площадки <b>№" . $row_basket['ident'] . "</b>', payopt = 3" );
		break;
	  #Авто-серфинг
	  case 16:
		mysql_query( "UPDATE autodynlink SET balance = balance + '" . $row_basket['price'] . "' WHERE id = '" . $row_basket['ident'] . "' and iduser = '" . $_SESSION['iduser'] . "'" );
		mysql_query( "INSERT INTO history_reklama_pay SET iduser = '" . $_SESSION['iduser'] . "', price = '" . $row_basket['price'] . "', type = 16, date = '" . time() . "', h_text = 'Пополнение рекламной площадки <b>№" . $row_basket['ident'] . "</b>', payopt = 3" );
		break;
	}
  }

  mysql_query( "UPDATE user SET golos = golos + 1 WHERE id = '" . $_SESSION['iduser'] . "'" );
  mysql_query( "UPDATE user SET savingscards = savingscards - '" . $get_basket_row['sum'] . "' WHERE id = '" . $_SESSION['iduser'] . "' LIMIT 1" );
  mysql_query( "DELETE FROM basket WHERE iduser = '" . $_SESSION['iduser'] . "'" );

  echo 1;
  exit();
} else {
  exit();
}

?>