locations-bugs #17

Merged
lopar merged 12 commits from locations-bugs into master 2020-10-28 20:25:45 +00:00
Showing only changes of commit 5dce7c644f - Show all commits

View File

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