<?

require_header ('Амбар');

echo "<div class='content'>";
echo "<div class='block'>";
if(isset($_GET['sell_all']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]'"),0)!=0)
{
if(isset($_GET['fish'])){$fish=1;$base="fish";}else{$fish=0;$base="plant";}
$q=mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `fish`='$fish'");
$cm=0;
while($post=mysql_fetch_array($q))
{
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_$base` WHERE `id` = '$post[id_plant]'"));
if($post['tachka']==0)mysql_query("DELETE FROM `kolhoz_warehouse` WHERE `id` = '$post[id]'"); else mysql_query("UPDATE `kolhoz_warehouse` SET `count`='0'  WHERE `id` = '$post[id]'");
$m=floor(($plant['money']/$plant['count'])*$post['count']);
$cm=$cm+$m;
}
mysql_query("UPDATE `kolhoz_user` SET `money` = '".($ku['money']+$cm)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_user` SET `umoney` = '$cm' WHERE `id` = '$ku[id]'");
if(isset($mc) && $mc['nalog']>0)
{
$nmoney=floor(($cm/100)*$mc['nalog']);
mysql_query("UPDATE `kolhoz_user` SET `money` = '".($ku['money']+$cm-$nmoney)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective_user` SET `money` = '".($mcu['money']+$nmoney)."' WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective` SET `money` = '".($mc['money']+$nmoney)."' WHERE `id` = '$mc[id]'");
}
header("Location:/warehouse/".passgen());
exit;
}
if(isset($_GET['sell']) && intval($_GET['sell'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['sell'])."'"), 0)!==0)
{
$pw=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['sell'])."'"));
if(isset($_GET['fish'])){$fish=1;$base="fish";}else{$fish=0;$base="plant";}
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_$base` WHERE `id` = '$pw[id_plant]'"));
if($plant['count']==0){$plant['count']=1;}
$m=floor(($plant['money']/$plant['count'])*$pw['count']);
mysql_query("UPDATE `kolhoz_user` SET `money` = '".($ku['money']+$m)."' WHERE `id` = '$ku[id]'");
if($pw['tachka']==0)mysql_query("DELETE FROM `kolhoz_warehouse` WHERE `id` = '$pw[id]'"); else mysql_query("UPDATE `kolhoz_warehouse` SET `count`='0'  WHERE `id` = '$pw[id]'");
mysql_query("UPDATE `kolhoz_user` SET `umoney` = '$m' WHERE `id` = '$ku[id]'");
if(isset($mc) && $mc['nalog']>0)
{
$nmoney=floor(($m/100)*$mc['nalog']);
mysql_query("UPDATE `kolhoz_user` SET `money` = '".($ku['money']+$m-$nmoney)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective_user` SET `money` = '".($mcu['money']+$nmoney)."' WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective` SET `money` = '".($mc['money']+$nmoney)."' WHERE `id` = '$mc[id]'");
}
header("Location:/warehouse/".passgen());
exit;
}

if(isset($_GET['tachka']) && intval($_GET['tachka'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['tachka'])."' AND `count`>'0'"), 0)!==0)
{
$pw=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['tachka'])."'"));
if($pw['count']>=100)$count=100; else $count=$pw['count'];
mysql_query("UPDATE `kolhoz_warehouse` SET `count` = '".($pw['count']-$count)."',`tachka` = '".($pw['tachka']+$count)."' WHERE `id` = '".intval($_GET['tachka'])."'");
header("Location:/warehouse/".passgen());
exit;
}

if(isset($_GET['tachka_out']) && intval($_GET['tachka_out'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['tachka_out'])."' AND `tachka`>'0'"), 0)!==0)
{
$pw=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['tachka_out'])."'"));
if($pw['tachka']>=100)$count=100; else $count=$pw['tachka'];
mysql_query("UPDATE `kolhoz_warehouse` SET `count` = '".($pw['count']+$count)."',`tachka` = '".($pw['tachka']-$count)."' WHERE `id` = '".intval($_GET['tachka_out'])."'");
header("Location:/warehouse/".passgen());
exit;
}

if(isset($_GET['tachka_out_all']) && intval($_GET['tachka_out_all'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['tachka_out_all'])."' AND `tachka`>'0'"), 0)!==0)
{
$pw=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['tachka_out_all'])."'"));
$count=$pw['tachka'];
mysql_query("UPDATE `kolhoz_warehouse` SET `count` = '".($pw['count']+$count)."',`tachka` = '".($pw['tachka']-$count)."' WHERE `id` = '".intval($_GET['tachka_out_all'])."'");
header("Location:/warehouse/".passgen());
exit;
}

if(isset($_GET['sell_all_market']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_market` WHERE `id_user` = '$ku[id]'"),0)!=0)
{
$q=mysql_query("SELECT * FROM `kolhoz_market` WHERE `id_user` = '$ku[id]'");
$cm=0;
while($post=mysql_fetch_array($q))
{
$plant2=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant2` WHERE `id` = '$post[id_plant2]'"));
$m=floor(($plant2['rubies']/$plant2['count'])*$post['count']);
$cm=$cm+$m;
}
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']+$cm)."' WHERE `id` = '$ku[id]'");
mysql_query("DELETE FROM `kolhoz_market` WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_user` SET `umoney2` = '$cm' WHERE `id` = '$ku[id]'");
/*Налог временно убран
if(isset($mc) && $mc['nalog2']>0)
{
$nrubies=floor(($cm/100)*$mc['nalog2']);
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']+$cm-$nrubies)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective_user` SET `money` = '".($mcu['money']+$nmoney)."' WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective` SET `rubies` = '".($mc['rubies']+$nrubies)."' WHERE `id` = '$mc[id]'");
}*/
header("Location:/warehouse/".passgen());
exit;
}
if(isset($_GET['sell_market']) && intval($_GET['sell_market'])!=NULL && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_market` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['sell_market'])."'"),0)!=0)
{
$pw=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_market` WHERE `id_user` = '$ku[id]' AND `id` = '".intval($_GET['sell_market'])."'"),0);
$plant2=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant2` WHERE `id` = '$pw[id_plant2]'"));
$m=floor(($plant2['rubies']/$plant2['count'])*$pw['count']);
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']+$m)."' WHERE `id` = '$ku[id]'");
mysql_query("DELETE FROM `kolhoz_market` WHERE `id` = '$pw[id]'");
mysql_query("UPDATE `kolhoz_user` SET `umoney2` = '$m' WHERE `id` = '$ku[id]'");
/*if(isset($mc) && $mc['nalog2']>0)
{
$nrubies=floor(($m/100)*$mc['nalog2']);
mysql_query("UPDATE `kolhoz_user` SET `rubies` = '".($ku['rubies']+$m-$nrubies)."' WHERE `id` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective_user` SET `money` = '".($mcu['money']+$nmoney)."' WHERE `id_user` = '$ku[id]'");
mysql_query("UPDATE `kolhoz_collective` SET `rubies` = '".($mc['rubies']+$nrubies)."' WHERE `id` = '$mc[id]'");
}
*/
header("Location:/warehouse/".passgen());
exit;
}

$q=mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `tachka` > '0' AND `fish` = '0'");
if(mysql_num_rows($q)>0){echo '<div><h3>Тачка</h3><ul class="ptm">';
while($post=mysql_fetch_array($q))
{
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$post[id_plant]'"));
echo "<li><div><img width='16' height='16' src='images/icons/tomato.png' alt='o'><span class='title'>".hsc($plant['name'])."</span> (<span>$post[tachka]</span>): <a href='/warehouse/tachka_out-$post[id]'>Выложить из тачки</a>".($post['tachka']>1000?", <a href='/warehouse/tachka_out_all-$post[id]'>Выложить все</a>":null)."
</div></li>";
}
echo "</ul></div>";
}

echo "<div><h3>Овощи</h3>";
echo "<ul class='ptm'>";
$q=mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `count` > '0' AND `fish` = '0'");
if(mysql_num_rows($q)==0)echo "<li>В амбаре нету овощей</li>";
$cm=0;
while($post=mysql_fetch_array($q))
{
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$post[id_plant]'"));
$m=floor(($plant['money']/$plant['count'])*$post['count']);
echo "<li><div><img width='16' height='16' src='images/icons/tomato.png' alt='o'><span class='title'>".hsc($plant['name'])."</span> (<span>$post[count]</span>): <a href='/warehouse/tachka-$post[id]'>В тачку</a>,<a href='/warehouse/sell-$post[id]'>Продать</a> за <img width='16' height='16' src='images/icons/money.png' alt='o'><span>$m</span>
</div></li>";
$cm=$cm+$m;
}
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `fish`='0' AND `count`>'0'"),0)>1)echo "<li><img alt='*' width='16' height='16' src='images/icons/money.png'> <a href='/warehouse/sell_all'>Продать всё</a> за <img width='16' height='16' src='images/icons/money.png' alt='*'><span>$cm</span></li>";
echo "</ul></div>";

echo "<div><h3>Рыба</h3>";
echo "<ul class='ptm'>";
$q=mysql_query("SELECT * FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `arena` = '0' AND `fish` = '1'");
if(mysql_num_rows($q)==0){echo "<li>В амбаре нету рыбы</li>";}else{
$cm=0;
while($post=mysql_fetch_array($q))
{
$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_fish` WHERE `id` = '$post[id_plant]'"));
$m=floor(($plant['money']/$plant['count'])*$post['count']);
echo "<li><div><img width='16' height='16' src='images/icons/tomato.png' alt='o'><span class='title'>".hsc($plant['name'])."</span> (<span>$post[count]</span> кг): <a href='/warehouse/fish/sell-$post[id]'>Продать</a> за <img width='16' height='16' src='images/icons/money.png' alt='o'><span>$m</span>
</div></li>";
$cm=$cm+$m;
}
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_warehouse` WHERE `id_user` = '$ku[id]' AND `fish`='1' AND `count`>'0'"),0)>1)echo "<li><img alt='*' width='16' height='16' src='images/icons/money.png'> <a href='/warehouse/sell_all/fish'>Продать всё</a> за <img width='16' height='16' src='images/icons/money.png' alt='o'><span>$cm</span></li>";
echo "</ul></div>";
}

if($ku['level']>=20){

echo "<div><h3>Домашние заготовки</h3>";
echo "<ul class='ptm'>";
$q=mysql_query("SELECT * FROM `kolhoz_market` WHERE `id_user` = '$ku[id]' AND `arena` = '0'");
if(mysql_num_rows($q)==0)echo "<li>Для продажи ничего нету</li>";
$cm=0;
while($post=mysql_fetch_array($q))
{
$plant2=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant2` WHERE `id` = '$post[id_plant2]'"));
$m=floor(($plant2['rubies']/$plant2['count'])*$post['count']);
echo "<li><div><img width='16' height='16' src='images/icons/recipes.png' alt='o'><span class='title'>".hsc($plant2['name'])."</span> (<span>$post[count]</span>): <a href='/warehouse/sell_market-$post[id]'> продать</a> : <img width='16' height='16' src='images/icons/ruby.png' alt='o'><span>$m</span>
</div></li>";
$cm=$cm+$m;
}
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_market` WHERE `id_user` = '$ku[id]'"),0)>1)echo "<li><img alt='*' width='16' height='16' src='images/icons/ruby.png'> <a href='/warehouse/sell_all_market'>Продать оптом</a> за <img width='16' height='16' src='images/icons/ruby.png' alt='o'><span>$cm</span></li>";
echo "</ul></div>";
}
echo "<div class='pt'><img width='16' height='16' src='images/icons/tomato.png' alt='*'> <a href='/buy_plant'>Купить овощи</a></div></div>";
include_once 'inc/foot.php';
?>