From 3a10d067600b257fd807e517bce646da8e21b36f Mon Sep 17 00:00:00 2001 From: Sveneld Date: Sat, 16 Mar 2024 23:20:53 +0100 Subject: [PATCH] Credit System check does user have enought credits for rent --- actions-qrcode.php | 15 ++++++++------- actions-sms.php | 19 ++++++++++--------- actions-web.php | 8 +++++--- common.php | 20 -------------------- src/Credit/CreditSystem.php | 5 +++++ src/Credit/CreditSystemInterface.php | 5 +++++ src/Credit/DisabledCreditSystem.php | 5 +++++ 7 files changed, 38 insertions(+), 39 deletions(-) diff --git a/actions-qrcode.php b/actions-qrcode.php index fa29d48..198b421 100644 --- a/actions-qrcode.php +++ b/actions-qrcode.php @@ -37,16 +37,17 @@ function response($message,$error=0,$log=1) function rent($userId,$bike,$force=FALSE) { - global $db, $forcestack, $watches, $credit, $user, $creditSystem; + global $db, $forcestack, $watches, $user, $creditSystem; - $stacktopbike=FALSE; - $bikeNum = $bike; - $minRequiredCredit = $creditSystem->getMinRequiredCredit(); - - $creditcheck=checkrequiredcredit($userId); - if ($creditcheck === false) { + $stacktopbike=FALSE; + $bikeNum = $bike; + if (!$creditSystem->isEnoughCreditForRent($userId)) { + $minRequiredCredit = $creditSystem->getMinRequiredCredit(); response(_('You are below required credit') . " " . $minRequiredCredit . $creditSystem->getCreditCurrency() . ". " . _('Please, recharge your credit.'), ERROR); + + return; } + checktoomany(0,$userId); $result=$db->query("SELECT count(*) as countRented FROM bikes where currentUser=$userId"); diff --git a/actions-sms.php b/actions-sms.php index a19c265..03d9e38 100644 --- a/actions-sms.php +++ b/actions-sms.php @@ -111,16 +111,17 @@ function rent($number,$bike,$force=FALSE) $stacktopbike = FALSE; $userId = $user->findUserIdByNumber($number); $bikeNum = intval($bike); - $minRequiredCredit = $creditSystem->getMinRequiredCredit(); - if ($force==FALSE) - { - $creditcheck=checkrequiredcredit($userId); - if ($creditcheck === false) { - $userRemainingCredit = $creditSystem->getUserCredit($userId); - $smsSender->send($number, _('Please, recharge your credit:') . " " . $userRemainingCredit . $creditSystem->getCreditCurrency() . ". " . _('Credit required:') . " " . $minRequiredCredit . $creditSystem->getCreditCurrency() . "."); - return; - } + if ($force == FALSE) { + if (!$creditSystem->isEnoughCreditForRent($userId)) { + $minRequiredCredit = $creditSystem->getMinRequiredCredit(); + $userRemainingCredit = $creditSystem->getUserCredit($userId); + $smsSender->send( + $number, + _('Please, recharge your credit:') . " " . $userRemainingCredit . $creditSystem->getCreditCurrency() . ". " . _('Credit required:') . " " . $minRequiredCredit . $creditSystem->getCreditCurrency() . "." + ); + return; + } checktoomany(0,$userId); diff --git a/actions-web.php b/actions-web.php index aa51a76..c4edd97 100644 --- a/actions-web.php +++ b/actions-web.php @@ -32,13 +32,15 @@ function rent($userId, $bike, $force = false) $stacktopbike = false; $bikeNum = $bike; - $minRequiredCredit = $creditSystem->getMinRequiredCredit(); if ($force == false) { - $creditcheck = checkrequiredcredit($userId); - if ($creditcheck === false) { + if (!$creditSystem->isEnoughCreditForRent($userId)) { + $minRequiredCredit = $creditSystem->getMinRequiredCredit(); response(_('You are below required credit') . ' ' . $minRequiredCredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR); + + return; } + checktoomany(0, $userId); $result = $db->query("SELECT count(*) as countRented FROM bikes where currentUser=$userId"); diff --git a/common.php b/common.php index 1b97a4e..dbe62bf 100644 --- a/common.php +++ b/common.php @@ -317,26 +317,6 @@ function checktoomany($cron = 1, $userid = 0) } } -// check if user has credit >= minimum credit+rent fee+long rental fee -function checkrequiredcredit($userid) -{ - global $creditSystem; - - if ($creditSystem->isEnabled() == false) { - return; - } - // if credit system disabled, exit - - $minRequiredCredit = $creditSystem->getMinRequiredCredit(); - - $userRemainingCredit = $creditSystem->getUserCredit($userid); - if ($userRemainingCredit >= $minRequiredCredit) { - return true; - } - - return false; -} - // subtract credit for rental function changecreditendrental($bike, $userid) { diff --git a/src/Credit/CreditSystem.php b/src/Credit/CreditSystem.php index 987005c..68404d3 100644 --- a/src/Credit/CreditSystem.php +++ b/src/Credit/CreditSystem.php @@ -52,6 +52,11 @@ public function getMinRequiredCredit() return $this->minBalanceCredit + $this->rentalFee + $this->longRentalFee; } + public function isEnoughCreditForRent($userid) + { + return $this->getUserCredit($userid) >= $this->getMinRequiredCredit(); + } + /** * @return bool */ diff --git a/src/Credit/CreditSystemInterface.php b/src/Credit/CreditSystemInterface.php index 8d03c92..194578f 100644 --- a/src/Credit/CreditSystemInterface.php +++ b/src/Credit/CreditSystemInterface.php @@ -14,6 +14,11 @@ public function getUserCredit($userid); */ public function getMinRequiredCredit(); + /** + * @return bool + */ + public function isEnoughCreditForRent($userid); + /** * @return bool */ diff --git a/src/Credit/DisabledCreditSystem.php b/src/Credit/DisabledCreditSystem.php index 25fb454..2978b7a 100644 --- a/src/Credit/DisabledCreditSystem.php +++ b/src/Credit/DisabledCreditSystem.php @@ -14,6 +14,11 @@ public function getMinRequiredCredit() return PHP_INT_MAX; } + public function isEnoughCreditForRent($userid) + { + return true; + } + public function isEnabled() { return false;