<?php
require_once('../wu_init.php');
require('../wu-functions/wu_functions.php');
if (!wu_token()) { exit('wu-error'); }
if (!USER_LOGGED) { exit('3'); }

if(isset($_POST['id'])){
if(!empty($_POST['id'])){
$id = intval($_POST['id']);
$item = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT id,usr,inf,ty,co,ti,st,log,ref,rat FROM ".DB_PREFIX."_es INNER JOIN ".DB_PREFIX."_users ON ".DB_PREFIX."_es.usr = ".DB_PREFIX."_users.uid WHERE id='$id' LIMIT 1"));
if ($item['usr'] == $u_id) { exit('5'); }
if ($item['st'] != 1) { exit('3'); }
$nus = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT uid,bal FROM ".DB_PREFIX."_users WHERE uid='$u_id' LIMIT 1"));
if ($nus['bal'] < $item['co']) { exit('0'); }
$rat = $item['rat'];
if ($rat < 10) { $com = '0.20'; } if ($rat >= 10 && $rat < 50) { $com = '0.18'; } if ($rat >= 50 && $rat < 100) { $com = '0.16'; } if ($rat >= 100 && $rat < 250) { $com = '0.14'; } if ($rat >= 250) { $com = '0.12'; }
$ecom = $item['co']*$com;
$ecost = $item['co']-$ecom;
if ($item['inf'] == 1) {
$unitq = mysqli_query($connect_db, "SELECT id FROM ".DB_PREFIX."_es_u WHERE m='$id' AND st = '0' LIMIT 1");
$unith = mysqli_num_rows($unitq);
if ($unith == 0) { exit('3'); }
$unita = mysqli_fetch_assoc($unitq);
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_es_b` (usr,m,un,dt) VALUES ('$u_id','$id','$unita[id]','$dt')");
mysqli_query($connect_db, "UPDATE ".DB_PREFIX."_es_u SET st='1' WHERE id='$unita[id]' LIMIT 1");
}
if ($item['inf'] == 0) {
$isb = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT count(`id`) AS `c` FROM ".DB_PREFIX."_es_b WHERE usr = '$u_id' AND m = '$id'"));
if ($isb['c'] > 0) { exit('4'); }
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_es_b` (usr,m,dt) VALUES ('$u_id','$id','$dt')");
}
wu_log_act(0, "Купил товар $item[ti] за $item[co] руб. у $item[log]");
mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_config` SET adm_profit=adm_profit+'$ecom' WHERE id='1' LIMIT 1");
mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_es` SET buy=buy+1 WHERE id='$id' LIMIT 1");
// Покупатель
mysqli_query($connect_db, "UPDATE ".DB_PREFIX."_users SET bal=bal-'$item[co]', rat=rat+0.2 WHERE uid='$u_id' LIMIT 1");
wu_operation($u_id, 0, $item['co'], "Вы купили товар $item[ti]");

//Запись в лог админа
wu_log_fin($u_id, 1, "Купил товар $item[ti] за $item[co] руб.");

// Продавец
mysqli_query($connect_db, "UPDATE ".DB_PREFIX."_users SET bal=bal+'$ecost', rat=rat+0.2 WHERE uid='$item[usr]' LIMIT 1");
wu_operation($item['usr'], 1, $ecost, "У Вас купили товар $item[ti]");
if ($item['ref'] > 0) {
$ratra = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT uid,rat FROM ".DB_PREFIX."_users WHERE uid='$item[ref]'"));
$ratr = $ratra['rat'];
if ($ratr < 10) { $rplus = '0.01'; } if ($ratr >= 10 && $ratr < 50) { $rplus = '0.02'; } if ($ratr >= 50 && $ratr < 100) { $rplus = '0.03'; } if ($ratr >= 100 && $ratr < 250) { $rplus = '0.04'; } if ($ratr >= 250) { $rplus = '0.05'; }
$toref = $ecost*$rplus;
mysqli_query($connect_db, "UPDATE ".DB_PREFIX."_users SET toref=toref+'$toref' WHERE uid='$item[usr]' LIMIT 1");
mysqli_query($connect_db, "UPDATE ".DB_PREFIX."_users SET bal=bal+'$toref' WHERE uid='$item[ref]' LIMIT 1");
wu_operation($item['ref'], 1, $toref, "Реферальные отчисления");
}

//Отправка уведомления в личный кабинет и уведомления на E-mail
$msg = "У Вас купили $item[ti]. Вам начислено $ecost руб.";
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_notif` (usr,msg,st,dt) VALUES ('$item[usr]','$msg','1','$dt')");
wu_email_send_usr($item['usr'], 'Новая покупка товара', $msg);

exit('1');
} else { exit('3'); }
} else { exit('3'); }
?>