<?php
//запуск раз в сутки
DEFINE("ROOT_DIR", realpath(dirname(__FILE__).'/../') );
require(ROOT_DIR."/config.php");

$on_off = mysql_result(mysql_query("SELECT `price` FROM `tb_config` WHERE `item`='comp_day' AND `howmany`='1'"),0,0);
if($on_off == 1){
$type = mysql_result(mysql_query("SELECT `price` FROM `tb_config` WHERE `item`='c_day_type' AND `howmany`='1'"),0,0);
$price = mysql_result(mysql_query("SELECT `price` FROM `tb_config` WHERE `item`='c_day_price' AND `howmany`='1'"),0,0);
$min_clik = mysql_result(mysql_query("SELECT `price` FROM `tb_config` WHERE `item`='c_day_min_clik' AND `howmany`='1'"),0,0);

$sql = mysql_query("SELECT `user`, COUNT(new_user) AS num FROM `b_comp_day` WHERE `type`='1' AND `new_user` IN (SELECT `username` FROM `b_users` WHERE `visits` >= '$min_clik' AND `cod`!=0) GROUP BY `user` ORDER BY `num` DESC LIMIT 5") OR DIE(mysql_error());
if(mysql_num_rows($sql) > 0){
while($res = mysql_fetch_assoc($sql)){
$ss = mysql_query("SELECT * FROM `b_users` WHERE `id`='".$res["user"]."'");
$ress = mysql_fetch_assoc($ss);
if($type == 1){
$procent = round($res["num"]*$price/100, 2);
$bonus = $res["num"]*$price+$procent;
mysql_query("UPDATE `b_users` SET `money`=`money`+'$bonus' WHERE `id`='".$res["user"]."'") OR DIE(mysql_error());

mysql_query("INSERT INTO `b_mails` (`text`,`fromid`,`fromus`,`toid`,`tous`,`timemessage`,`read`) VALUES ('Ежедневный конкурс','2','Sustem','".$res["user"]."','".$ress["username"]."','".time()."','5')") or die(mysql_error());
$ident = mysql_result(mysql_query("SELECT `id` FROM `b_mails` ORDER BY `id` DESC LIMIT 1"),0);
mysql_query("INSERT INTO `b_mails` (`text`,`fromid`,`fromus`,`toid`,`tous`,`timemessage`,`ident`) VALUES ('Приз за ежедневный конкурс реферелов! Вы набрали ".$res["num"]." шт. Ваш приз составляет <font color=red>$bonus</font> руб.','2','Sustem','".$res["user"]."','".$ress["username"]."','".time()."','$ident')") or die(mysql_error());

}else{
$price = round($res["num"]*$price/100, 0);
$sqli = mysql_query("SELECT `username` FROM `b_users` WHERE `referer`='' LIMIT 3") OR DIE(mysql_error());
while($resi = mysql_fetch_assoc($sqli)){
mysql_query("UPDATE `b_users` SET `referer`='".$res["user"]."' WHERE `referer`=''") OR DIE(mysql_error());
mysql_query("INSERT INTO `tb_mail` (`sender`,`recipient`,`subject`,`message`,`data`) VALUES ('Система','".$ress["username"]."','Ежедневный конкурс','Приз за ежедневный конкурс реферелов! Вы набрали ".$res["num"]." шт. Ваш приз составляет <font color=red>$price</font> рефералов','".time()."')");
}
}
}
}

}

$min_sum = mysql_result(mysql_query("SELECT `price` FROM `tb_config` WHERE `item`='c_day_min_sum' AND `howmany`='2'"),0,0);
$sqli = mysql_query("SELECT `user`, SUM( count ) AS `summ` FROM `b_comp_day` WHERE `user`!='1' AND `type`='2' AND `count` >= '$min_sum' GROUP BY `user` ORDER BY SUM( count ) DESC LIMIT 3") OR DIE(mysql_error());
if(mysql_num_rows($sqli) > 0){
$number = 0;
while($resi = mysql_fetch_assoc($sqli)){
$ssd = mysql_query("SELECT * FROM `b_users` WHERE `id`='".$resi["user"]."'");
$ressd = mysql_fetch_assoc($ssd);
$number++;
switch($number){
case 1:$kol = '10';break;
case 2:$kol = '5';break;
case 3:$kol = '3';break;
}
$count_sum = mysql_result(mysql_query("SELECT SUM( count ) FROM `b_comp_day` WHERE `user`!='1' AND `type`='2' AND `user`='".$resi["user"]."'"),0,0);
$bonus2 = $count_sum*$kol/100;
mysql_query("UPDATE `b_users` SET `r_money`=`r_money`+'$bonus2' WHERE `id`='".$resi["user"]."'") OR DIE(mysql_error());

mysql_query("INSERT INTO `b_mails` (`text`,`fromid`,`fromus`,`toid`,`tous`,`timemessage`,`read`) VALUES ('Ежедневный конкурс','2','Sustem','".$resi["user"]."','".$ressd["username"]."','".time()."','5')") or die(mysql_error());
$idente = mysql_result(mysql_query("SELECT `id` FROM `b_mails` ORDER BY `id` DESC LIMIT 1"),0);
mysql_query("INSERT INTO `b_mails` (`text`,`fromid`,`fromus`,`toid`,`tous`,`timemessage`,`ident`) VALUES ('Приз за ".$number."-е место в ежедневный конкурсе <b>Рекламодателей</b>! Ваш заказ рекламы на сумму $count_sum руб. Ваш приз составляет <font color=red>$bonus2</font> руб.','2','Sustem','".$resi["user"]."','".$ressd["username"]."','".time()."','$idente')") or die(mysql_error());
}
}

$min_sum1 = mysql_result(mysql_query("SELECT `price` FROM `tb_config` WHERE `item`='c_day_min_sum' AND `howmany`='3'"),0,0);
$sqli1 = mysql_query("SELECT `user`, SUM( count ) AS `summ` FROM `b_comp_day` WHERE `type`='3' AND `count` >= '1' GROUP BY `user` ORDER BY SUM( count ) DESC LIMIT 3") OR DIE(mysql_error());
if(mysql_num_rows($sqli1) > 0){
$number1 = 0;
while($resi1 = mysql_fetch_assoc($sqli1)){
$ssd1 = mysql_query("SELECT * FROM `b_users` WHERE `id`='".$resi1["user"]."'");
$ressd1 = mysql_fetch_assoc($ssd1);
$number1++;
switch($number1){
case 1:$kol1 = '20';break;
case 2:$kol1 = '15';break;
case 3:$kol1 = '10';break;
}
$count_sum1 = mysql_result(mysql_query("SELECT SUM( count ) FROM `b_comp_day` WHERE `user`!='1' AND `type`='3' AND `user`='".$resi1["user"]."'"),0,0);
$bonus3 = $count_sum1*$kol1/100;
mysql_query("UPDATE `b_users` SET `reyting`=`reyting`+'$bonus3' WHERE `id`='".$resi1["user"]."'") OR DIE(mysql_error());

mysql_query("INSERT INTO `b_mails` (`text`,`fromid`,`fromus`,`toid`,`tous`,`timemessage`,`read`) VALUES ('Ежедневный конкурс','2','Sustem','".$resi1["user"]."','".$ressd1["username"]."','".time()."','5')") or die(mysql_error());
$idente = mysql_result(mysql_query("SELECT `id` FROM `b_mails` ORDER BY `id` DESC LIMIT 1"),0);
mysql_query("INSERT INTO `b_mails` (`text`,`fromid`,`fromus`,`toid`,`tous`,`timemessage`,`ident`) VALUES ('Приз за ".$number1."-е место в ежедневный конкурсе <b>Рейтинга</b>! Ваше количество кликов $count_sum1 шт. Ваш приз составляет <font color=red>$bonus3</font> Бал.','2','Sustem','".$resi1["user"]."','".$ressd1["username"]."','".time()."','$idente')") or die(mysql_error());
}
}

mysql_query("truncate table `b_comp_day`") OR DIE(mysql_error());
?>