<?
$id = intval($_POST['id']);
require('config.php');


	$id = ( int ) $_POST['shpa'];
	$InvId = $_POST['InvId'];
	$OutSum = $_POST['OutSum'];
	$pwd2 = 'admin1795';
	$SignatureValue = strtoupper( $_POST['SignatureValue'] );
	$crc = strtoupper( md5( $OutSum . ':' . $InvId . ':' . $pwd2 . ':shpa=' . $id ) );

	if ( $crc != $SignatureValue ) {
		exit();
	} else {
	
		$price = mysql_fetch_array(mysql_query("SELECT sum(price) as sum FROM seo_basket WHERE fromid='$id'"));
	
		if ($_POST['OutSum'] >= $price["sum"]){
			$sql = mysql_query("SELECT * FROM seo_basket WHERE fromid='$id'");
			while($row = mysql_fetch_assoc($sql)) {
				$ident = $row['ident'];
				$price = $row['price'];
				$type = $row['type'];
				switch($type) {
					case '1': $nowtime = time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса динамической ссылки № $ident с помощью ROBOKASSA на сумму $price руб.')");
					mysql_query("UPDATE tb_ads SET balance=balance+'$price' WHERE id='$ident' AND fromus='$id'");
					break;
				
					case '6': $nowtime = time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса письма № $ident с помощью ROBOKASSA на сумму $price руб.')");
					mysql_query("UPDATE seo_paymails SET balance=balance+'$price' WHERE id='$ident' AND fromid='$id'");
					break;
			
					case '2': $nowtime = time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса контекстной ссылки № $ident с помощью ROBOKASSA на сумму $price руб.')"); 
					mysql_query("UPDATE tb_statlinks SET balance=balance+'$price' WHERE id='$ident' AND fromus='$id'");
					break;
			
					case '3':$nowtime = time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса баннера № $ident с помощью ROBOKASSA на сумму $price руб.')"); 
					mysql_query("UPDATE tb_advban SET balance=balance+'$price' WHERE id='$ident' AND fromus='$id'");
					break;
				
					case '9':$nowtime = time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса баннера (100х100) № $ident с помощью ROBOKASSA на сумму $price руб.')"); 
					mysql_query("UPDATE tb_advban_100 SET balance=balance+'$price' WHERE id='$ident' AND fromus='$id'");
					break;			    
				
					case '10':$nowtime = time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса баннера (200х300) № $ident с помощью ROBOKASSA на сумму $price руб.')"); 
					mysql_query("UPDATE tb_advban_300 SET balance=balance+'$price' WHERE id='$ident' AND fromus='$id'");
					break;				
			
					case '4': $res = mysql_fetch_assoc(mysql_query("SELECT * FROM seo_refbirj WHERE rid='$ident'"));
					$rid = $res['rid'];
					$trid = $res['trid'];
					$price = $res['price'];
					$usstat = mysql_fetch_array(mysql_query("SELECT reyting FROM tb_users WHERE id = '$trid'"));
					$price = $price * 0.8;
					$viruchka = $price - ($price * 0.8);
					mysql_query("INSERT INTO tb_viruchka (ident, money, month, year) VALUES (7, '$viruchka', '" . date('m', time()) . "', '" . date('Y', time()) . "')");
					$autoback = mysql_fetch_array(mysql_query("SELECT id, autoback, referer FROM tb_users WHERE id = '$id'"));
					mysql_query("UPDATE tb_users SET referer='$id', referer2='$autoback[referer]', toref=0, toref2=0, autoproc = '$autoback[autoback]' WHERE id = '$rid'");
					mysql_query("UPDATE tb_users SET referer2='$id', toref=0, toref2=0 WHERE referer = '$rid' and id != '" . $autoback['id'] . "'");
					mysql_query("UPDATE tb_users SET money=money+'$price' WHERE id='$trid'");
					mysql_query("DELETE FROM seo_refbirj WHERE rid='$ident'"); $nowtime=time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$trid','$nowtime','7','Ваш реферал # $rid Продан на бирже рефералов!')");
					break;
				
					case '5':$nowtime=time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса задания № $ident с помощью ROBOKASSA на сумму $price руб.')"); 
					mysql_query("UPDATE seo_tasks SET balance=balance+'$price' WHERE id='$ident' AND fromid='$id'");
					break;
            
					case '7':$nowtime=time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','7','Пополнение рекламного счета с помощью ROBOKASSA на сумму $price руб.')"); 
					mysql_query("UPDATE tb_users SET admoney=admoney+'$price' WHERE id='$id'");
					break;
				
					case '8':$nowtime=time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Оплата штрафа за нарушение правил проекта с помощью ROBOKASSA на сумму $price руб.')"); 
					mysql_query("UPDATE tb_users SET finestatus='0' WHERE id='$id'");
					$finesummam = mysql_fetch_array(mysql_query("SELECT fine FROM tb_config WHERE id = '1'"));
					mysql_query("INSERT INTO tb_viruchka (ident, money, month, year) VALUES (10, '$finesummam[fine]', '" . date('m', time()) . "', '" . date('Y', time()) . "')");
					break;

					case '11': $nowtime = time();
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса рекламной цепочки № $ident с помощью ROBOKASSA на сумму $price руб.')"); 
					mysql_query("UPDATE cep_links SET balance=balance+'$price' WHERE id='$ident' AND fromus='$id'");
					break;				
				}; 
			}
			$sum = mysql_fetch_array(mysql_query("SELECT sum(price) as sum FROM seo_basket WHERE fromid = '$id'"));
			if ($sum['sum'] >= 100 and $sum['sum'] < 200) {
				$golos = 1;
			} else
			if ($sum['sum'] >= 200 and $sum['sum'] < 500) {
				$golos = 2;
			} else
			if ($sum['sum'] >= 500) {
				$golos = 5;
			} else {
				$golos = 0;
			}
        
			if ($sum['sum'] >= 10 and $sum['sum'] < 100) {
				$addmoney = $sum['sum'] * 0.01;
			} else
			if ($sum['sum'] >= 100 and $sum['sum'] < 200) {
				$addmoney = $sum['sum'] * 0.02;
			} else
			if ($sum['sum'] >= 200 and $sum['sum'] < 400) {
				$addmoney = $sum['sum'] * 0.03;
			} else
			if ($sum['sum'] >= 400 and $sum['sum'] < 500) {
				$addmoney = $sum['sum'] * 0.04;
			} else
			if ($sum['sum'] >= 500) {
				$addmoney = $sum['sum'] * 0.05;
			}
		
			/// Акции
			$promotion_status = mysql_fetch_array(mysql_query("SELECT status, prom1, prom2, prom3, prom4 FROM promotion_status WHERE id = '1'"));
			if($promotion_status['status'] == 1) {
				$promotionx = mysql_fetch_array(mysql_query("SELECT reyting FROM tb_users WHERE id = '$id'"));
				$promotion_inf = mysql_fetch_array(mysql_query("SELECT statusid FROM promotion_temp WHERE userid = '$id'"));
				$promotion_temp = mysql_fetch_array( mysql_query("SELECT(SELECT count(*) FROM promotion_temp WHERE userid = '$id')temppromo"));
				if ($sum['sum'] >= 10 and $sum['sum'] < 100) {
					if($promotion_status['prom1'] == 1) {$rix=100; $statuspromo = '1';} elseif($promotion_status['prom1'] == 2) {$rix=250; $statuspromo = '2';} else
					if($promotion_status['prom1'] == 3) {$rix=600; $statuspromo = '3';} elseif($promotion_status['prom1'] == 4) {$rix=1000; $statuspromo = '4';}
					if($rix < $promotionx['reyting']) {
						$adrating = ($price*0.1);
					} else {
						$adrating = $rix - $promotionx['reyting'];
					}
				} else
				if ($sum['sum'] >= 100 and $sum['sum'] < 250) {
					if($promotion_status['prom2'] == 1) {$rix=100; $statuspromo = '1';} elseif($promotion_status['prom2'] == 2) {$rix=250; $statuspromo = '2';} else
					if($promotion_status['prom2'] == 3) {$rix=600; $statuspromo = '3';} elseif($promotion_status['prom2'] == 4) {$rix=1000; $statuspromo = '4';}
					if($rix < $promotionx['reyting']) {
						$adrating = ($price*0.1);
					} else {
						$adrating = $rix - $promotionx['reyting'];
					}	
				} else
				if ($sum['sum'] >= 250 and $sum['sum'] < 500) {
					if($promotion_status['prom3'] == 1) {$rix=100; $statuspromo = '1';} elseif($promotion_status['prom3'] == 2) {$rix=250; $statuspromo = '2';} else
					if($promotion_status['prom3'] == 3) {$rix=600; $statuspromo = '3';} elseif($promotion_status['prom3'] == 4) {$rix=1000; $statuspromo = '4';}
					if($rix < $promotionx['reyting']) {
						$adrating = ($price*0.1);
					} else {
						$adrating = $rix - $promotionx['reyting'];
					} 
				} else
				if ($sum['sum'] >= 500) {
					if($promotion_status['prom4'] == 1) {$rix=100; $statuspromo = '1';} elseif($promotion_status['prom4'] == 2) {$rix=250; $statuspromo = '2';} else
					if($promotion_status['prom4'] == 3) {$rix=600; $statuspromo = '3';} elseif($promotion_status['prom4'] == 4) {$rix=1000; $statuspromo = '4';}
					if($rix < $promotionx['reyting']) {
						$adrating = ($price*0.1);
					} else {
						$adrating = $rix - $promotionx['reyting'];
					}
				}
				if($promotion_temp["temppromo"] == 0 and $rix > $promotionx['reyting']) {
					mysql_query("INSERT INTO promotion_temp (statusid, userid, date, turnover, numberpay) VALUES ('$statuspromo', '$id', '".time()."', '".$sum['sum']."', '1')");
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','".time()."','7','Ваш статус обновлен согласно акции рекламодателей!')");
				
				} else
				if($promotion_temp["temppromo"] == 1 and $statuspromo > $promotion_inf['statusid'] and $rix > $promotionx['reyting']) {
					mysql_query("UPDATE promotion_temp SET date='".time()."', statusid='$statuspromo', turnover=turnover+'".$sum['sum']."', numberpay=numberpay+'1' WHERE userid='$id'");
					mysql_query("INSERT INTO tb_allhistory (idus,data,doing,title) values ('$id','".time()."','7','Ваш статус обновлен согласно акции рекламодателей!')");
				}
			} else {
				$adrating = ($price*0.1);
			}
		
			//Конкурсы
			$prov_kon = mysql_query("SELECT id, end_date FROM konkurs WHERE type = 1 ORDER BY id DESC");
			if (mysql_num_rows($prov_kon) > 0) {
				$prov_row = mysql_fetch_array($prov_kon);
				$end_date = explode('.', $prov_row['end_date']);
				$end_date = mktime(0, 0, 0, $end_date[1], $end_date[0], $end_date[2]);
				if (time() < $end_date) {
					$prov_user = mysql_query("SELECT id FROM konkurs_temp WHERE iduser = '$id' and type = 1");
					if (mysql_num_rows($prov_user) > 0) {
						mysql_query("UPDATE konkurs_temp SET summ = summ + '$sum[sum]' WHERE iduser = '$id' and type = 1");
					} else {
						mysql_query("INSERT INTO konkurs_temp (idkonkurs, iduser, type, summ) VALUES ('$prov_row[id]', '$id', 1, '$sum[sum]')");
					}
				}
			}
		
			mysql_query("UPDATE tb_users SET golos = golos + '$golos', points = points + 1, money = money + '$addmoney' WHERE id = '$id'");
			mysql_query("UPDATE tb_users SET reyting=reyting+'$adrating' WHERE id='$id'");
			mysql_query("DELETE FROM seo_basket WHERE fromid='$id'");
		}
	}
?>