<?php
require_once('../wu_init.php');
if (!wu_token()) { exit('wu-error'); }
if (!USER_LOGGED) { exit('wu-error'); }
include('wu_checkadmin.php');

if(isset($_POST['usr']) && isset($_POST['sum']) && isset($_POST['type'])){
if(!empty($_POST['sum'])){
$usr = intval($_POST['usr']);
$sum = mysqli_real_escape_string($connect_db, trim($_POST['sum']));
$type = intval($_POST['type']);
$comment = $_POST['comment'];
if($_POST['tohist'] == 'true') { $tohist = 1; } else { $tohist = 0; }
if($_POST['tonotif'] == 'true') { $tonotif = 1; } else { $tonotif = 0; }

if($_POST['sum'] <= 0) { exit('0'); }


if(empty($comment)) { $msg_add_h = ''; $msg_add_n = ''; } else { $msg_add_h = '. Комментарий: '.$comment; $msg_add_n = ' Комментарий: '.$comment; }

//Зачисление на баланс
if ($type == 1) {
mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_users` SET `bal` = `bal`+'$sum' WHERE uid = '$usr' LIMIT 1");
if($tohist == 1) {
$msg_hist = 'Зачисление администратором'.$msg_add_h;
wu_operation($usr, 2, $sum, $msg_hist);
}
if($tonotif == 1) {
$msg_notif = mysqli_real_escape_string($connect_db, 'На баланс Вашего аккаунта администратором зачислено '.$sum.' руб.'.$msg_add_n);
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_notif` (usr,msg,st,dt) VALUES ('$usr','$msg_notif','1','$dt')");
}
}

//Списание с баланса
if ($type == 2) {
mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_users` SET `bal` = `bal`-'$sum' WHERE uid = '$usr' LIMIT 1");
if($tohist == 1) {
$msg_hist = 'Списание администратором'.$msg_add_h;
wu_operation($usr, 0, $sum, $msg_hist);
}
if($tonotif == 1) {
$msg_notif = mysqli_real_escape_string($connect_db, 'С баланса Вашего аккаунта администратором списано '.$sum.' руб.'.$msg_add_n);
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_notif` (usr,msg,st,dt) VALUES ('$usr','$msg_notif','0','$dt')");
}
}

//Перевод с баланса на заморозку
if ($type == 3) {
mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_users` SET `bal` = `bal`-'$sum', `bal_f` = `bal_f`+'$sum' WHERE uid = '$usr' LIMIT 1");
if($tohist == 1) {
$msg_hist = 'Заморозка средств'.$msg_add_h;
wu_operation($usr, 0, $sum, $msg_hist);
}
if($tonotif == 1) {
$msg_notif = mysqli_real_escape_string($connect_db, 'Администратором заморожено '.$sum.' руб.'.$msg_add_n);
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_notif` (usr,msg,st,dt) VALUES ('$usr','$msg_notif','0','$dt')");
}
}

//Перевод с заморозки на баланс
if ($type == 4) {
$user = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT uid,bal_f FROM `".DB_PREFIX."_users` WHERE uid='$usr' LIMIT 1"));
if ($sum > $user['bal_f']) exit('2');
mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_users` SET `bal` = `bal`+'$sum', `bal_f` = `bal_f`-'$sum' WHERE uid = '$usr' LIMIT 1");
if($tohist == 1) {
$msg_hist = 'Разморозка средств'.$msg_add_h;
wu_operation($usr, 2, $sum, $msg_hist);
}
if($tonotif == 1) {
$msg_notif = mysqli_real_escape_string($connect_db, 'Администратором разморожено '.$sum.' руб.'.$msg_add_n);
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_notif` (usr,msg,st,dt) VALUES ('$usr','$msg_notif','1','$dt')");
}
}

//Зачисление на заморозку
if ($type == 5) {
mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_users` SET `bal_f` = `bal_f`+'$sum' WHERE uid = '$usr' LIMIT 1");
if($tonotif == 1) {
$msg_notif = mysqli_real_escape_string($connect_db, 'Администратором добавлено к замороженным средствам '.$sum.' руб.'.$msg_add_n);
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_notif` (usr,msg,st,dt) VALUES ('$usr','$msg_notif','2','$dt')");
}
}

//Списание с заморозки
if ($type == 6) {
$user = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT uid,bal_f FROM `".DB_PREFIX."_users` WHERE uid='$usr' LIMIT 1"));
if ($sum > $user['bal_f']) exit('2');
mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_users` SET `bal_f` = `bal_f`-'$sum' WHERE uid = '$usr' LIMIT 1");
if($tonotif == 1) {
$msg_notif = mysqli_real_escape_string($connect_db, 'Администратором списано с замороженных средств '.$sum.' руб.'.$msg_add_n);
mysqli_query($connect_db, "INSERT INTO `".DB_PREFIX."_notif` (usr,msg,st,dt) VALUES ('$usr','$msg_notif','2','$dt')");
}
}

exit('1');
} else { exit('0'); }
} else { exit('wu-error'); }
?>