Можно было обнулить счёт получателя денег.

This commit is contained in:
lopar 2020-10-28 00:48:16 +02:00
parent 04d1f57a94
commit 5dce7c644f
1 changed files with 30 additions and 15 deletions

View File

@ -56,14 +56,18 @@ class Bank
/**
* Пишем банковское событие в лог в БД
*
* @param $receiverId - user_id получателя
* @param $amount
* @param $operationType
* @param int $receiverId - user_id получателя
* @param int $amount
* @param string $operationType
* @param int $senderId
*
* @throws \Krugozor\Database\Mysql\Exception
*/
private function bankLogs($receiverId, $amount, $operationType)
private function bankLogs(int $receiverId, int $amount, string $operationType, int $senderId = 0)
{
if (!$senderId) {
$senderId = $this->user_id;
}
$text = '';
if ($operationType === "sendMoney") {
$text = self::LOG_SEND . " Комиссия: " . $this->bankComission($amount);
@ -76,7 +80,7 @@ class Bank
}
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);
VALUES (?i, ?i, ?i, "?s", "?s")', $senderId, $receiverId, $amount, $operationType, $text);
}
@ -91,22 +95,25 @@ class Bank
*/
public function sendMoney(int $receiver, int $amount): void
{
$receiverWallet = db::c()->query('SELECT money FROM bank WHERE user_id = ?i', $receiver)->fetch_object();
if ($amount <= 0) {
throw new Exception(self::ERROR_WRONG_AMOUNT);
}
if (!db::c()->query('SELECT 1 FROM bank WHERE user_id = ?i', $receiver)) {
if (!$receiverWallet) {
throw new Exception(self::ERROR_NO_BANK_ACCOUNT);
}
$amountWithComission = $amount + $this->bankComission($amount);
if ($amountWithComission > $this->money) {
throw new Exception(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
}
$this->money -= $amountWithComission;
// Снимаем сумму с комиссией у отправителя
$this->money -= $amountWithComission;
self::setBankMoney($this->money, $this->user_id);
$this->bankLogs($receiver, $this->money, "sendMoney");
// Отдаём сумму на счёт получателю
self::setBankMoney($amount, $receiver);
$this->bankLogs($receiver, $amount, "sendMoney");
$receiverWallet->money += $amount;
self::setBankMoney($receiverWallet->money, $receiver);
$this->bankLogs($receiver, $receiverWallet->money, "receiveMoney");
}
/**
@ -133,7 +140,7 @@ class Bank
// Отдаём сумму на счёт получателю
$this->money += $amount;
self::setBankMoney($this->money, $this->user_id);
$this->bankLogs(0, $amount, "depositMoney");
$this->bankLogs(0, $this->money, "depositMoney");
}
/**
@ -156,24 +163,28 @@ class Bank
// Снимаем сумму с комиссией у отправителя
$this->money -= $amountWithComission;
self::setBankMoney($this->money, $this->user_id);
$this->bankLogs(0, $this->money, "withdrawMoney");
// Отдаём сумму в кошелёк получателя
//todo check it!
$this->user->money += $amount;
self::setWalletMoney($this->user->money, $this->user_id);
$this->bankLogs(0, $amount, "withdrawMoney");
}
/**
* Установить количество денег на банковском счету.
*
* @param int $amount - сумма.
* @param int $user_id - ID пользователя.
* @param int $amount сумма.
* @param int $user_id ID пользователя.
* @param string $operationType Тип операции. По умолчанию пусто. Если ввести, система запишет событие в банковский лог.
*
* @throws \Krugozor\Database\Mysql\Exception
*/
public static function setBankMoney(int $amount, int $user_id): void
public static function setBankMoney(int $amount, int $user_id, string $operationType = ''): void
{
db::c()->query('UPDATE bank SET money = ?i WHERE `id` = ?i', $amount, $user_id);
db::c()->query('UPDATE bank SET money = ?i WHERE user_id = ?i', $amount, $user_id);
if ($operationType) {
(new Bank)->bankLogs(0, $amount, $operationType);
}
}
/**
@ -188,4 +199,8 @@ class Bank
{
db::c()->query('UPDATE users SET money = ?i WHERE `id` = ?i', $amount, $user_id);
}
public function getBankMoney() {
return $this->money;
}
}