From ca305324b9ac62b9ee24153d7324158562947d44 Mon Sep 17 00:00:00 2001 From: Sveneld Date: Sat, 16 Mar 2024 22:49:31 +0100 Subject: [PATCH 1/4] Credit System get info about user credits left --- actions-qrcode.php | 13 ++++++------ actions-sms.php | 32 +++++++++++++++-------------- actions-web.php | 24 ++++++++++++---------- admin.php | 29 ++++++++++++--------------- common.php | 50 +++++++++++----------------------------------- index.php | 13 ++---------- 6 files changed, 63 insertions(+), 98 deletions(-) diff --git a/actions-qrcode.php b/actions-qrcode.php index e394cc5..4675f25 100644 --- a/actions-qrcode.php +++ b/actions-qrcode.php @@ -37,17 +37,16 @@ function response($message,$error=0,$log=1) function rent($userId,$bike,$force=FALSE) { + global $db, $forcestack, $watches, $credit, $user, $creditSystem; - global $db,$forcestack,$watches,$credit, $user; $stacktopbike=FALSE; $bikeNum = $bike; $requiredcredit=$credit["min"]+$credit["rent"]+$credit["longrental"]; $creditcheck=checkrequiredcredit($userId); - if ($creditcheck===FALSE) - { - response(_('You are below required credit')." ".$requiredcredit.$credit["currency"].". "._('Please, recharge your credit.'),ERROR); - } + if ($creditcheck === false) { + response(_('You are below required credit') . " " . $requiredcredit . $creditSystem->getCreditCurrency() . ". " . _('Please, recharge your credit.'), ERROR); + } checktoomany(0,$userId); $result=$db->query("SELECT count(*) as countRented FROM bikes where currentUser=$userId"); @@ -169,8 +168,8 @@ function returnbike($userId,$stand) $message.= '
'._('Please').', '._('rotate the lockpad to').' 0000 '._('when leaving').'.'; $creditchange=changecreditendrental($bikeNum,$userId); - if ($creditSystem->isEnabled() AND $creditchange) { - $message.='
'._('Credit change').': -'.$creditchange.getcreditcurrency().'.'; + if ($creditSystem->isEnabled() && $creditchange) { + $message .= '
' . _('Credit change') . ': -' . $creditchange . $creditSystem->getCreditCurrency() . '.'; } $result=$db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='RETURN',parameter=$standId"); diff --git a/actions-sms.php b/actions-sms.php index 76529ef..32e5e0a 100644 --- a/actions-sms.php +++ b/actions-sms.php @@ -96,16 +96,18 @@ function validateReceivedSMS($number,$receivedargumentno,$requiredargumentno,$er function credit($number) { - global $db, $smsSender, $user; - $userid=$user->findUserIdByNumber($number); - $usercredit=getusercredit($userid).getcreditcurrency(); - $smsSender->send($number,_('Your remaining credit:')." ".$usercredit); + global $smsSender, $user, $creditSystem; + + $userId = $user->findUserIdByNumber($number); + $userRemainingCredit = $creditSystem->getUserCredit($userId) . $creditSystem->getCreditCurrency(); + + $smsSender->send($number, _('Your remaining credit:') . " " . $userRemainingCredit); } function rent($number,$bike,$force=FALSE) { + global $db,$forcestack,$watches,$credit, $smsSender, $user, $creditSystem; - global $db,$forcestack,$watches,$credit, $smsSender, $user; $stacktopbike=FALSE; $userId = $user->findUserIdByNumber($number); $bikeNum = intval($bike); @@ -114,13 +116,11 @@ function rent($number,$bike,$force=FALSE) if ($force==FALSE) { $creditcheck=checkrequiredcredit($userId); - if ($creditcheck===FALSE) - { - $result=$db->query("SELECT credit FROM credit WHERE userId=$userId"); - $row=$result->fetch_assoc(); - $smsSender->send($number,_('Please, recharge your credit:')." ".$row["credit"].$credit["currency"].". "._('Credit required:')." ".$requiredcredit.$credit["currency"]."."); + if ($creditcheck === false) { + $userRemainingCredit = $creditSystem->getUserCredit($userId); + $smsSender->send($number, _('Please, recharge your credit:') . " " . $userRemainingCredit . $creditSystem->getCreditCurrency() . ". " . _('Credit required:') . " " . $requiredcredit . $creditSystem->getCreditCurrency() . "."); return; - } + } checktoomany(0,$userId); @@ -361,12 +361,14 @@ function returnBike($number,$bike,$stand,$message="",$force=FALSE) } if ($creditSystem->isEnabled()) { - $message.=_('Credit').": ".getusercredit($userId).getcreditcurrency(); - if ($creditchange) $message.=" (-".$creditchange.")"; - $message.="."; + $userRemainingCredit = $creditSystem->getUserCredit($userId) . $creditSystem->getCreditCurrency(); + $message .= _('Credit') . ": " . $userRemainingCredit; + if ($creditchange) { + $message .= " (-" . $creditchange . ")"; + } + $message .= "."; } $smsSender->send($number,$message); - } diff --git a/actions-web.php b/actions-web.php index fd36610..752e987 100644 --- a/actions-web.php +++ b/actions-web.php @@ -28,7 +28,8 @@ function response($message, $error = 0, $additional = '', $log = 1) function rent($userId, $bike, $force = false) { - global $db, $forcestack, $watches, $credit, $user; + global $db, $forcestack, $watches, $credit, $user, $creditSystem; + $stacktopbike = false; $bikeNum = $bike; $requiredcredit = $credit['min'] + $credit['rent'] + $credit['longrental']; @@ -36,7 +37,7 @@ function rent($userId, $bike, $force = false) if ($force == false) { $creditcheck = checkrequiredcredit($userId); if ($creditcheck === false) { - response(_('You are below required credit') . ' ' . $requiredcredit . $credit['currency'] . '. ' . _('Please, recharge your credit.'), ERROR); + response(_('You are below required credit') . ' ' . $requiredcredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR); } checktoomany(0, $userId); @@ -164,7 +165,7 @@ function returnBike($userId, $bike, $stand, $note = '', $force = false) if ($force == false) { $creditchange = changecreditendrental($bikeNum, $userId); if ($creditSystem->isEnabled() and $creditchange) { - $message .= '
' . _('Credit change') . ': -' . $creditchange . getcreditcurrency() . '.'; + $message .= '
' . _('Credit change') . ': -' . $creditchange . $creditSystem->getCreditCurrency() . '.'; } $result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='RETURN',parameter=$standId"); @@ -602,14 +603,15 @@ function saveuser($userid, $username, $email, $phone, $privileges, $limit) function addcredit($userid, $creditmultiplier) { - global $db, $credit, $user; + global $db, $credit, $user, $creditSystem; + $requiredcredit = $credit['min'] + $credit['rent'] + $credit['longrental']; $addcreditamount = $requiredcredit * $creditmultiplier; $result = $db->query('UPDATE credit SET credit=credit+' . $addcreditamount . ' WHERE userId=' . $userid); $result = $db->query("INSERT INTO history SET userId=$userid,bikeNum=0,action='CREDITCHANGE',parameter='" . $addcreditamount . '|add+' . $addcreditamount . "'"); $userName = $user->findUserName($userid); - response(_('Added') . ' ' . $addcreditamount . $credit['currency'] . ' ' . _('credit for') . ' ' . $userName . '.'); + response(_('Added') . ' ' . $addcreditamount . $creditSystem->getCreditCurrency() . ' ' . _('credit for') . ' ' . $userName . '.'); } function getcouponlist() @@ -629,6 +631,7 @@ function getcouponlist() function generatecoupons($multiplier) { global $db, $credit, $codeGenerator, $creditSystem; + if ($creditSystem->isEnabled() == false) { return; } @@ -639,7 +642,7 @@ function generatecoupons($multiplier) foreach ($codes as $code) { $result = $db->query("INSERT IGNORE INTO coupons SET coupon='" . $code . "',value='" . $value . "',status='0'"); } - response(_('Generated 10 new') . ' ' . $value . ' ' . $credit['currency'] . ' ' . _('coupons') . '.', 0, array('coupons' => $codes)); + response(_('Generated 10 new') . ' ' . $value . ' ' . $creditSystem->getCreditCurrency() . ' ' . _('coupons') . '.', 0, array('coupons' => $codes)); } function sellcoupon($coupon) @@ -667,7 +670,7 @@ function validatecoupon($userid, $coupon) $result = $db->query("UPDATE credit SET credit=credit+'" . $value . "' WHERE userId='" . $userid . "'"); $result = $db->query("INSERT INTO history SET userId=$userid,bikeNum=0,action='CREDITCHANGE',parameter='" . $value . '|add+' . $value . '|' . $coupon . "'"); $result = $db->query("UPDATE coupons SET status='2' WHERE coupon='" . $coupon . "'"); - response('+' . $value . ' ' . $credit['currency'] . '. ' . _('Coupon') . ' ' . $coupon . ' ' . _('has been redeemed') . '.'); + response('+' . $value . ' ' . $creditSystem->getCreditCurrency() . '. ' . _('Coupon') . ' ' . $coupon . ' ' . _('has been redeemed') . '.'); } response(_('Invalid coupon, try again.'), 1); } @@ -738,7 +741,7 @@ function mapgetmarkers($userId) function mapgetlimit($userId) { - global $db, $auth; + global $db, $auth, $creditSystem; if (!$auth->isLoggedIn()) { response(''); @@ -754,10 +757,9 @@ function mapgetlimit($userId) $currentlimit = $limit - $rented; - $usercredit = 0; - $usercredit = getusercredit($userId); + $userCredit = $creditSystem->getUserCredit($userId); - echo json_encode(array('limit' => $currentlimit, 'rented' => $rented, 'usercredit' => $usercredit)); + echo json_encode(array('limit' => $currentlimit, 'rented' => $rented, 'usercredit' => $userCredit)); } function mapgeolocation($userid, $lat, $long) diff --git a/admin.php b/admin.php index 80ee813..486c151 100644 --- a/admin.php +++ b/admin.php @@ -50,16 +50,13 @@ @@ -138,9 +135,9 @@
- - - + + +
@@ -162,9 +159,9 @@
- or - - + or + +
diff --git a/common.php b/common.php index b8ab340..d42751c 100644 --- a/common.php +++ b/common.php @@ -320,19 +320,20 @@ function checktoomany($cron = 1, $userid = 0) // check if user has credit >= minimum credit+rent fee+long rental fee function checkrequiredcredit($userid) { - global $db, $credit, $creditSystem; + global $credit, $creditSystem; if ($creditSystem->isEnabled() == false) { return; } // if credit system disabled, exit - $requiredcredit = $credit['min'] + $credit['rent'] + $credit['longrental']; - $result = $db->query("SELECT credit FROM credit WHERE userId=$userid AND credit>=$requiredcredit"); - if ($result->num_rows == 1) { - $row = $result->fetch_assoc(); + $requiredCredit = $credit['min'] + $credit['rent'] + $credit['longrental']; + + $userRemainingCredit = $creditSystem->getUserCredit($userid); + if ($userRemainingCredit >= $requiredCredit) { return true; } + return false; } @@ -346,7 +347,7 @@ function changecreditendrental($bike, $userid) } // if credit system disabled, exit - $usercredit = getusercredit($userid); + $userCredit = $creditSystem->getUserCredit($userid); $result = $db->query("SELECT time FROM history WHERE bikeNum=$bike AND userId=$userid AND (action='RENT' OR action='FORCERENT') ORDER BY time DESC LIMIT 1"); if ($result->num_rows == 1) { @@ -406,40 +407,13 @@ function changecreditendrental($bike, $userid) $creditchange = $creditchange + $credit['longrental']; $changelog .= 'longrent-' . $credit['longrental'] . ';'; } - $usercredit = $usercredit - $creditchange; - $result = $db->query("UPDATE credit SET credit=$usercredit WHERE userId=$userid"); - $result = $db->query("INSERT INTO history SET userId=$userid,bikeNum=$bike,action='CREDITCHANGE',parameter='" . $creditchange . '|' . $changelog . "'"); - $result = $db->query("INSERT INTO history SET userId=$userid,bikeNum=$bike,action='CREDIT',parameter=$usercredit"); - return $creditchange; - } -} - -function getusercredit($userid) -{ - global $db, $credit, $creditSystem; + $userCredit = $userCredit - $creditchange; + $db->query("UPDATE credit SET credit=$userCredit WHERE userId=$userid"); + $db->query("INSERT INTO history SET userId=$userid,bikeNum=$bike,action='CREDITCHANGE',parameter='" . $creditchange . '|' . $changelog . "'"); + $db->query("INSERT INTO history SET userId=$userid,bikeNum=$bike,action='CREDIT',parameter=$userCredit"); - if ($creditSystem->isEnabled() == false) { - return; - } - // if credit system disabled, exit - - $result = $db->query("SELECT credit FROM credit WHERE userId=$userid"); - $row = $result->fetch_assoc(); - $usercredit = $row['credit']; - - return $usercredit; -} - -function getcreditcurrency() -{ - global $credit, $creditSystem; - - if ($creditSystem->isEnabled() == false) { - return; + return $creditchange; } - // if credit system disabled, exit - - return $credit['currency']; } function issmssystemenabled() diff --git a/index.php b/index.php index 1d1491a..27129ca 100644 --- a/index.php +++ b/index.php @@ -111,7 +111,8 @@ if ($auth->isLoggedIn()) { echo '
  • ', $user->findUserName($userid), ''; if ($creditSystem->isEnabled()) { - echo ' (', getusercredit($userid), ' ', getcreditcurrency(), ' )
  • '; + $userRemainingCredit = $creditSystem->getUserCredit($userid); + echo ' (' . $userRemainingCredit . ' ' . $creditSystem->getCreditCurrency() . ' )
    '; } if ($cities) { echo '
  • ',' - or - - + or + +
    diff --git a/common.php b/common.php index d42751c..1b97a4e 100644 --- a/common.php +++ b/common.php @@ -320,17 +320,17 @@ function checktoomany($cron = 1, $userid = 0) // check if user has credit >= minimum credit+rent fee+long rental fee function checkrequiredcredit($userid) { - global $credit, $creditSystem; + global $creditSystem; if ($creditSystem->isEnabled() == false) { return; } // if credit system disabled, exit - $requiredCredit = $credit['min'] + $credit['rent'] + $credit['longrental']; + $minRequiredCredit = $creditSystem->getMinRequiredCredit(); $userRemainingCredit = $creditSystem->getUserCredit($userid); - if ($userRemainingCredit >= $requiredCredit) { + if ($userRemainingCredit >= $minRequiredCredit) { return true; } diff --git a/install/index.php b/install/index.php index ab7eaf4..383d00c 100644 --- a/install/index.php +++ b/install/index.php @@ -1,6 +1,8 @@ connect(); +/** + * @var CreditSystemInterface $creditSystem + */ +$creditSystem = (new CreditSystemFactory())->getCreditSystem($credit, $db); + $configfile=file($configfilename); foreach ($_POST as $variable=>$value) { @@ -430,13 +437,12 @@ function return_bytes($val) { $newconfig=implode($configfile); file_put_contents($configfilename,$newconfig); $configfile=file($configfilename); -if ($credit["enabled"]==1) - { - $newcredit=($credit["min"]+$credit["rent"]+$credit["longrental"])*10; - $result=$db->query("SELECT userId FROM users WHERE privileges='7'"); - $row=$result->fetch_assoc(); - $result=$db->query("REPLACE INTO credit SET userId='".$row["userId"]."',credit='$newcredit'"); - } +if ($creditSystem->isEnabled()) { + $newcredit = $creditSystem->getMinRequiredCredit() * 10; + $result = $db->query("SELECT userId FROM users WHERE privileges='7'"); + $row = $result->fetch_assoc(); + $result = $db->query("REPLACE INTO credit SET userId='" . $row["userId"] . "',credit='$newcredit'"); +} $db->commit(); ?>

    Installation finished

    diff --git a/src/Credit/CreditSystem.php b/src/Credit/CreditSystem.php index 41314e7..987005c 100644 --- a/src/Credit/CreditSystem.php +++ b/src/Credit/CreditSystem.php @@ -11,7 +11,7 @@ class CreditSystem implements CreditSystemInterface // currency used for credit system private $creditCurrency = "€"; // minimum credit required to allow any bike operations - private $minBikeCredit = 2; + private $minBalanceCredit = 2; // rental fee (after $watches["freetime"]) private $rentalFee = 2; // 0 = disabled, 1 = charge flat price $credit["rent"] every $watches["flatpricecycle"] minutes, @@ -47,6 +47,11 @@ public function getUserCredit($userid) return $result->fetchAssoc()['credit']; } + public function getMinRequiredCredit() + { + return $this->minBalanceCredit + $this->rentalFee + $this->longRentalFee; + } + /** * @return bool */ @@ -62,14 +67,6 @@ public function getCreditCurrency() return $this->creditCurrency; } - /** - * @return int - */ - public function getMinBikeCredit() - { - return $this->minBikeCredit; - } - /** * @return int */ @@ -123,7 +120,7 @@ private function parseConfiguration(array $creditConfiguration) $this->creditCurrency = (string)$creditConfiguration['currency']; } if (isset($creditConfiguration['min'])) { - $this->minBikeCredit = (int)$creditConfiguration['min']; + $this->minBalanceCredit = (int)$creditConfiguration['min']; } if (isset($creditConfiguration['rent'])) { $this->rentalFee = (int)$creditConfiguration['rent']; diff --git a/src/Credit/CreditSystemInterface.php b/src/Credit/CreditSystemInterface.php index c668ad3..8d03c92 100644 --- a/src/Credit/CreditSystemInterface.php +++ b/src/Credit/CreditSystemInterface.php @@ -9,6 +9,11 @@ interface CreditSystemInterface */ public function getUserCredit($userid); + /** + * @return int + */ + public function getMinRequiredCredit(); + /** * @return bool */ @@ -19,11 +24,6 @@ public function isEnabled(); */ public function getCreditCurrency(); - /** - * @return int - */ - public function getMinBikeCredit(); - /** * @return int */ diff --git a/src/Credit/DisabledCreditSystem.php b/src/Credit/DisabledCreditSystem.php index 0937ecd..25fb454 100644 --- a/src/Credit/DisabledCreditSystem.php +++ b/src/Credit/DisabledCreditSystem.php @@ -9,17 +9,17 @@ public function getUserCredit($userid) return 0; } - public function isEnabled() + public function getMinRequiredCredit() { - return false; + return PHP_INT_MAX; } - public function getCreditCurrency() + public function isEnabled() { - return 0; + return false; } - public function getMinBikeCredit() + public function getCreditCurrency() { return 0; } diff --git a/tests/Credit/CreditSystemTest.php b/tests/Credit/CreditSystemTest.php index 37435ad..aaee356 100644 --- a/tests/Credit/CreditSystemTest.php +++ b/tests/Credit/CreditSystemTest.php @@ -16,7 +16,7 @@ public function testConstructor( $configuration, $expectedIsEnabled, $expectedCreditCurrency, - $expectedMinBikeCredit, + $expectedMinRequiredCredit, $expectedRentalFee, $expectedPriceCycle, $expectedLongRentalFee, @@ -26,7 +26,7 @@ public function testConstructor( $creditSystem = new CreditSystem($configuration, $this->createMock(DbInterface::class)); $this->assertEquals($expectedIsEnabled, $creditSystem->isEnabled()); $this->assertEquals($expectedCreditCurrency, $creditSystem->getCreditCurrency()); - $this->assertEquals($expectedMinBikeCredit, $creditSystem->getMinBikeCredit()); + $this->assertEquals($expectedMinRequiredCredit, $creditSystem->getMinRequiredCredit()); $this->assertEquals($expectedRentalFee, $creditSystem->getRentalFee()); $this->assertEquals($expectedPriceCycle, $creditSystem->getPriceCycle()); $this->assertEquals($expectedLongRentalFee, $creditSystem->getLongRentalFee()); @@ -40,7 +40,7 @@ public function constructorDataProvider() 'configuration' => [], 'expectedIsEnabled' => false, 'expectedCreditCurrency' => '€', - 'expectedMinBikeCredit' => 2, + 'expectedMinRequiredCredit' => 9, 'expectedRentalFee' => 2, 'expectedPriceCycle' => 0, 'expectedLongRentalFee' => 5, @@ -60,7 +60,7 @@ public function constructorDataProvider() ], 'expectedIsEnabled' => true, 'expectedCreditCurrency' => '$', - 'expectedMinBikeCredit' => 3, + 'expectedMinRequiredCredit' => 12, 'expectedRentalFee' => 3, 'expectedPriceCycle' => 1, 'expectedLongRentalFee' => 6, From 3a10d067600b257fd807e517bce646da8e21b36f Mon Sep 17 00:00:00 2001 From: Sveneld Date: Sat, 16 Mar 2024 23:20:53 +0100 Subject: [PATCH 3/4] 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; From 2dc2459cba2ee32740caff0938bc528d6675b79c Mon Sep 17 00:00:00 2001 From: Sveneld Date: Sat, 16 Mar 2024 23:31:59 +0100 Subject: [PATCH 4/4] Credit System config fully migrated to work via CreditSystemInterface --- common.php | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/common.php b/common.php index dbe62bf..c78d25a 100644 --- a/common.php +++ b/common.php @@ -340,34 +340,33 @@ function changecreditendrental($bike, $userid) //ak vrati a znova pozica bike do 10 min tak free time nebude mať. $oldRetrun = $db->query("SELECT time FROM history WHERE bikeNum=$bike AND userId=$userid AND (action='RETURN' OR action='FORCERETURN') ORDER BY time DESC LIMIT 1"); - if ($oldRetrun->num_rows==1) - { - $oldRow=$oldRetrun->fetch_assoc(); - $returntime=strtotime($oldRow["time"]); - if(($starttime-$returntime) < 10*60 && $timediff > 5*60) { - $creditchange = $creditchange + $credit['rent']; - $changelog .= 'rerent-' . $credit['rent'] . ';'; - } + if ($oldRetrun->num_rows==1) { + $oldRow = $oldRetrun->fetch_assoc(); + $returntime = strtotime($oldRow["time"]); + if (($starttime - $returntime) < 10 * 60 && $timediff > 5 * 60) { + $creditchange = $creditchange + $creditSystem->getRentalFee(); + $changelog .= 'rerent-' . $creditSystem->getRentalFee() . ';'; + } } //end if ($timediff > $watches['freetime'] * 60) { - $creditchange = $creditchange + $credit['rent']; - $changelog .= 'overfree-' . $credit['rent'] . ';'; + $creditchange = $creditchange + $creditSystem->getRentalFee(); + $changelog .= 'overfree-' . $creditSystem->getRentalFee() . ';'; } if ($watches['freetime'] == 0) { $watches['freetime'] = 1; } // for further calculations - if ($credit['pricecycle'] and $timediff > $watches['freetime'] * 60 * 2) { // after first paid period, i.e. freetime*2; if pricecycle enabled + if ($creditSystem->getPriceCycle() && $timediff > $watches['freetime'] * 60 * 2) { // after first paid period, i.e. freetime*2; if pricecycle enabled $temptimediff = $timediff - ($watches['freetime'] * 60 * 2); - if ($credit['pricecycle'] == 1) { // flat price per cycle + if ($creditSystem->getPriceCycle() == 1) { // flat price per cycle $cycles = ceil($temptimediff / ($watches['flatpricecycle'] * 60)); - $creditchange = $creditchange + ($credit['rent'] * $cycles); - $changelog .= 'flat-' . $credit['rent'] * $cycles . ';'; - } elseif ($credit['pricecycle'] == 2) { // double price per cycle + $creditchange = $creditchange + ($creditSystem->getRentalFee() * $cycles); + $changelog .= 'flat-' . $creditSystem->getRentalFee() * $cycles . ';'; + } elseif ($creditSystem->getPriceCycle() == 2) { // double price per cycle $cycles = ceil($temptimediff / ($watches['doublepricecycle'] * 60)); - $tempcreditrent = $credit['rent']; + $tempcreditrent = $creditSystem->getRentalFee(); for ($i = 1; $i <= $cycles; $i++) { $multiplier = $i; if ($multiplier > $watches['doublepricecyclecap']) { @@ -384,8 +383,8 @@ function changecreditendrental($bike, $userid) } } if ($timediff > $watches['longrental'] * 3600) { - $creditchange = $creditchange + $credit['longrental']; - $changelog .= 'longrent-' . $credit['longrental'] . ';'; + $creditchange = $creditchange + $creditSystem->getLongRentalFee(); + $changelog .= 'longrent-' . $creditSystem->getLongRentalFee() . ';'; } $userCredit = $userCredit - $creditchange; $db->query("UPDATE credit SET credit=$userCredit WHERE userId=$userid");