Skip to content

Commit

Permalink
Credit System check does user have enought credits for rent
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld committed Mar 16, 2024
1 parent 1eb3a67 commit 3a10d06
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 39 deletions.
15 changes: 8 additions & 7 deletions actions-qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
19 changes: 10 additions & 9 deletions actions-sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 5 additions & 3 deletions actions-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
20 changes: 0 additions & 20 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Credit/CreditSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Credit/CreditSystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public function getUserCredit($userid);
*/
public function getMinRequiredCredit();

/**
* @return bool
*/
public function isEnoughCreditForRent($userid);

/**
* @return bool
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Credit/DisabledCreditSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public function getMinRequiredCredit()
return PHP_INT_MAX;
}

public function isEnoughCreditForRent($userid)
{
return true;
}

public function isEnabled()
{
return false;
Expand Down

0 comments on commit 3a10d06

Please sign in to comment.