2020-07-03 10:26:38 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Author: lopiu
|
|
|
|
|
* Date: 03.07.2020
|
|
|
|
|
* Time: 07:24
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class Bank
|
|
|
|
|
{
|
|
|
|
|
public $user_id;
|
2020-09-25 16:27:11 +00:00
|
|
|
|
public $money;
|
|
|
|
|
private $user;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
|
2020-08-27 09:23:10 +00:00
|
|
|
|
public const BANK_COMISSION = 0.05; //5%
|
2020-09-25 16:27:11 +00:00
|
|
|
|
|
2020-07-03 10:26:38 +00:00
|
|
|
|
const ERROR_NO_MONEY_IN_WALLET = "Ошибка! Нет денег в кошельке!";
|
|
|
|
|
const ERROR_NO_BANK_ACCOUNT = "Ошибка! Счёта не существует!";
|
|
|
|
|
const ERROR_NO_MONEY_IN_BANK_ACCOUNT = "Ошибка! Нет денег на счету!";
|
|
|
|
|
const ERROR_WRONG_AMOUNT = "Ошибка! Сумма должна быть положительной!";
|
|
|
|
|
const LOG_SEND = "Банк: Перевод средств на другой счёт.";
|
|
|
|
|
const LOG_RECEIVE = "Банк: Получение средств.";
|
|
|
|
|
const LOG_DEPOSIT = "Пополнение счёта.";
|
|
|
|
|
const LOG_WITHDRAW = "Снятие денег со счёта.";
|
|
|
|
|
|
|
|
|
|
public function __construct($row)
|
|
|
|
|
{
|
|
|
|
|
$bank_row = db::c()->query('SELECT user_id, money FROM bank WHERE user_id = ?i', $row)->fetch_assoc();
|
2020-09-25 16:27:11 +00:00
|
|
|
|
$this->user = db::c()->query('SELECT money FROM users WHERE id = ?i', $row)->fetch_object();
|
2020-07-03 10:26:38 +00:00
|
|
|
|
foreach ($this as $key => $value) {
|
|
|
|
|
if (isset($bank_row[$key])) {
|
|
|
|
|
$this->$key = $bank_row[$key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Если ВДРУГ у человека нет счёта в банке - создаём.
|
|
|
|
|
if (empty($this->user_id)) {
|
|
|
|
|
db::c()->query('INSERT INTO bank (user_id) VALUES (?i)', $row);
|
|
|
|
|
$this->user_id = $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Комиссия: self::BANK_COMISSION от переводимой суммы, но не менее 1 кр.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @param $amount
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
private function bankComission($amount)
|
|
|
|
|
{
|
|
|
|
|
$bankComission = round($amount * self::BANK_COMISSION);
|
|
|
|
|
if ($bankComission < 1) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return (int)$bankComission;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Пишем банковское событие в лог в БД
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @param $receiverId - user_id получателя
|
|
|
|
|
* @param $amount
|
|
|
|
|
* @param $operationType
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
|
|
|
|
private function bankLogs($receiverId, $amount, $operationType)
|
|
|
|
|
{
|
|
|
|
|
$text = '';
|
|
|
|
|
if ($operationType === "sendMoney") {
|
|
|
|
|
$text = self::LOG_SEND . " Комиссия: " . $this->bankComission($amount);
|
|
|
|
|
} elseif ($operationType === "depositMoney") {
|
|
|
|
|
$receiverId = $this->user_id;
|
|
|
|
|
$text = self::LOG_DEPOSIT;
|
|
|
|
|
} elseif ($operationType === "withdrawMoney") {
|
|
|
|
|
$receiverId = $this->user_id;
|
|
|
|
|
$text = self::LOG_WITHDRAW . " Комиссия: " . $this->bankComission($amount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db::c()->query('INSERT INTO `bank_logs` (sender_id, receiver_id, amount, type, text)
|
|
|
|
|
VALUES (?i, ?i, ?i, "?s", "?s")', $this->user_id, $receiverId, $amount, $operationType, $text);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Перевод денег между бансковскими счетами игроков с банковской комиссией.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
|
|
|
|
* @param int $receiver
|
|
|
|
|
* @param int $amount
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
2020-09-25 16:27:11 +00:00
|
|
|
|
public function sendMoney(int $receiver, int $amount): void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
|
|
|
|
throw new Exception(self::ERROR_WRONG_AMOUNT);
|
|
|
|
|
}
|
|
|
|
|
if (!db::c()->query('SELECT 1 FROM bank WHERE user_id = ?i', $receiver)) {
|
|
|
|
|
throw new Exception(self::ERROR_NO_BANK_ACCOUNT);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-09-25 16:27:11 +00:00
|
|
|
|
$amountWithComission = $amount + $this->bankComission($amount);
|
|
|
|
|
if ($amountWithComission > $this->money) {
|
|
|
|
|
throw new Exception(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
|
|
|
|
}
|
|
|
|
|
$this->money -= $amountWithComission;
|
|
|
|
|
// Снимаем сумму с комиссией у отправителя
|
|
|
|
|
$this->setBankMoney($this->money, $this->user_id);
|
|
|
|
|
// Отдаём сумму на счёт получателю
|
|
|
|
|
$this->setBankMoney($amount, $receiver);
|
|
|
|
|
$this->bankLogs($receiver, $amount, "sendMoney");
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Пополнение банковского счёта игрока
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
|
|
|
|
* @param int $amount - сумма
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
2020-09-25 16:27:11 +00:00
|
|
|
|
public function depositMoney(int $amount): void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
|
|
|
|
throw new Exception(self::ERROR_WRONG_AMOUNT);
|
|
|
|
|
}
|
|
|
|
|
$wallet = db::c()->query('SELECT money FROM users WHERE id = ?i', $this->user_id)->fetch_object();
|
|
|
|
|
if ($wallet->money < $amount) {
|
|
|
|
|
throw new Exception(self::ERROR_NO_MONEY_IN_WALLET);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Забираем деньги из кошелька получателя
|
|
|
|
|
//todo check it!
|
|
|
|
|
$this->user->money -= $amount;
|
|
|
|
|
$this->setWalletMoney($this->user->money, $this->user_id);
|
|
|
|
|
// Отдаём сумму на счёт получателю
|
|
|
|
|
$this->money += $amount;
|
|
|
|
|
$this->setBankMoney($this->money, $this->user_id);
|
|
|
|
|
$this->bankLogs(0, $amount, "depositMoney");
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Снятие денег с банковского счёта игрока с банковской комиссией.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
|
|
|
|
* @param int $amount - сумма
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
2020-09-25 16:27:11 +00:00
|
|
|
|
public function withdrawMoney(int $amount): void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
|
|
|
|
throw new Exception(self::ERROR_WRONG_AMOUNT);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-09-25 16:27:11 +00:00
|
|
|
|
$amountWithComission = $amount + $this->bankComission($amount);
|
|
|
|
|
if ($this->money < $amountWithComission) {
|
|
|
|
|
throw new Exception(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
|
|
|
|
}
|
|
|
|
|
// Снимаем сумму с комиссией у отправителя
|
|
|
|
|
$this->money -= $amountWithComission;
|
|
|
|
|
$this->setBankMoney($this->money, $this->user_id);
|
|
|
|
|
// Отдаём сумму в кошелёк получателя
|
|
|
|
|
//todo check it!
|
|
|
|
|
$this->user->money += $amount;
|
|
|
|
|
$this->setWalletMoney($this->user->money, $this->user_id);
|
|
|
|
|
$this->bankLogs(0, $amount, "withdrawMoney");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function setBankMoney(int $amount, int $user_id): void
|
|
|
|
|
{
|
|
|
|
|
db::c()->query('UPDATE bank SET money = ?i WHERE `id` = ?i', $amount, $user_id);
|
|
|
|
|
}
|
|
|
|
|
private function setWalletMoney(int $amount, int $user_id): void
|
|
|
|
|
{
|
|
|
|
|
db::c()->query('UPDATE users SET money = ?i WHERE `id` = ?i', $amount, $user_id);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|