<?
$sell_sum=0;
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_garden` WHERE `id_user` = '$ku[id]' AND `id_pet` != '0'"),0)>0)
{
$q=mysql_query("SELECT `id_pet` FROM `kolhoz_garden` WHERE `id_user` = '$ku[id]' AND `id_pet` != '0'");
while($post=mysql_fetch_array($q)){
$sell_s=mysql_result(mysql_query("SELECT `cena` FROM `kolhoz_tree` WHERE `id` = '$post[id_pet]'"),0);
$sell_sum=$sell_sum+floor($sell_s/3);
}
}
$buy_sum=0;
if(isset($_SESSION['buy_tree']))$clean_gr=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_garden` WHERE `id_user` = '$ku[id]' AND `id_pet` = '0'"),0);
if(isset($clean_gr) && $clean_gr>0  )
{
$byu_s=mysql_result(mysql_query("SELECT `cena` FROM `kolhoz_tree` WHERE `id` = '".(intval($_SESSION['buy_tree']))."'"),0);
$buy_sum=$byu_s*$clean_gr;
}

if(isset($_GET['sell_all']) && $sell_sum>0)
{
if(isset($_GET['ok']))
{
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']+$sell_sum)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_garden` SET `id_pet` = '0' WHERE `id_user` = '$ku[id]'");
header("Location:/mygarden/moderate/".passgen());
exit;
}else
podtv("mygarden/moderate/sell_all/ok","mygarden/moderate");
include_once 'inc/foot.php';
}


if(isset($_GET['buy_all']) && $buy_sum>0)
{
$byu_l=mysql_result(mysql_query("SELECT `level` FROM `kolhoz_tree` WHERE `id` = '".(intval($_SESSION['buy_tree']))."'"),0);
if($byu_l>$ku['level']){
$_SESSION['msg_err']='Произошла ошибка';
header("Location:/mygarden/moderate/".passgen());
exit;
}
if($ku['rubies']>=$buy_sum)
{
if(isset($_GET['ok']))
{
mysql_query("UPDATE `kolhoz_garden` SET `id_pet` = '".(intval($_SESSION['buy_tree']))."', `time_money` = '$time', `time_money2` = '0', `last_rf` = '$time' WHERE `id_pet` = '0' AND `id_user`='$ku[id]'");
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']-$buy_sum)."' WHERE `id` = '$ku[id]'");
header("Location:/mygarden/moderate/".passgen());
exit;
}
podtv("mygarden/moderate/buy_all/ok","mygarden/moderate");
include_once 'inc/foot.php';
}
else
{
no_rubies($buy_sum-$ku['rubies']);
}
}
?>