<?
$sell_sum=0;
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_petground` WHERE `id_user` = '$ku[id]' AND `id_pet` != '0'"),0)>0)
{
$q=mysql_query("SELECT `id_pet` FROM `kolhoz_petground` 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_pet` WHERE `id` = '$post[id_pet]'"),0);
$sell_sum=$sell_sum+$sell_s/2;
}
}
$buy_sum=0;
if(isset($_SESSION['buy_pet']))$clean_gr=mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_petground` 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_pet` WHERE `id` = '".(intval($_SESSION['buy_pet']))."'"),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 `money` = '".($ku['money']+$sell_sum)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_petground` SET `id_pet` = '0' WHERE `id_user` = '$ku[id]'");
header("Location:/mypetfarm/moderate/".passgen());
exit;
}else
podtv("mypetfarm/moderate/sell_all/ok","mypetfarm/moderate");
include_once 'inc/foot.php';
}


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