diff --git a/actions-qrcode.php b/actions-qrcode.php index 4675f256..fa29d48a 100644 --- a/actions-qrcode.php +++ b/actions-qrcode.php @@ -41,11 +41,11 @@ function rent($userId,$bike,$force=FALSE) $stacktopbike=FALSE; $bikeNum = $bike; - $requiredcredit=$credit["min"]+$credit["rent"]+$credit["longrental"]; + $minRequiredCredit = $creditSystem->getMinRequiredCredit(); $creditcheck=checkrequiredcredit($userId); if ($creditcheck === false) { - response(_('You are below required credit') . " " . $requiredcredit . $creditSystem->getCreditCurrency() . ". " . _('Please, recharge your credit.'), ERROR); + response(_('You are below required credit') . " " . $minRequiredCredit . $creditSystem->getCreditCurrency() . ". " . _('Please, recharge your credit.'), ERROR); } checktoomany(0,$userId); diff --git a/actions-sms.php b/actions-sms.php index 32e5e0a4..a19c2659 100644 --- a/actions-sms.php +++ b/actions-sms.php @@ -106,19 +106,19 @@ function credit($number) function rent($number,$bike,$force=FALSE) { - global $db,$forcestack,$watches,$credit, $smsSender, $user, $creditSystem; + global $db, $forcestack, $watches, $credit, $smsSender, $user, $creditSystem; - $stacktopbike=FALSE; + $stacktopbike = FALSE; $userId = $user->findUserIdByNumber($number); - $bikeNum = intval($bike); - $requiredcredit=$credit["min"]+$credit["rent"]+$credit["longrental"]; + $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:') . " " . $requiredcredit . $creditSystem->getCreditCurrency() . "."); + $smsSender->send($number, _('Please, recharge your credit:') . " " . $userRemainingCredit . $creditSystem->getCreditCurrency() . ". " . _('Credit required:') . " " . $minRequiredCredit . $creditSystem->getCreditCurrency() . "."); return; } diff --git a/actions-web.php b/actions-web.php index 752e9878..aa51a76b 100644 --- a/actions-web.php +++ b/actions-web.php @@ -32,12 +32,12 @@ function rent($userId, $bike, $force = false) $stacktopbike = false; $bikeNum = $bike; - $requiredcredit = $credit['min'] + $credit['rent'] + $credit['longrental']; + $minRequiredCredit = $creditSystem->getMinRequiredCredit(); if ($force == false) { $creditcheck = checkrequiredcredit($userId); if ($creditcheck === false) { - response(_('You are below required credit') . ' ' . $requiredcredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR); + response(_('You are below required credit') . ' ' . $minRequiredCredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR); } checktoomany(0, $userId); @@ -605,8 +605,8 @@ function addcredit($userid, $creditmultiplier) { global $db, $credit, $user, $creditSystem; - $requiredcredit = $credit['min'] + $credit['rent'] + $credit['longrental']; - $addcreditamount = $requiredcredit * $creditmultiplier; + $minRequiredCredit = $creditSystem->getMinRequiredCredit(); + $addcreditamount = $minRequiredCredit * $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); @@ -636,8 +636,8 @@ function generatecoupons($multiplier) return; } // if credit system disabled, exit - $requiredcredit = $credit['min'] + $credit['rent'] + $credit['longrental']; - $value = $requiredcredit * $multiplier; + $minRequiredCredit = $creditSystem->getMinRequiredCredit(); + $value = $minRequiredCredit * $multiplier; $codes = $codeGenerator->generate(10, 6); foreach ($codes as $code) { $result = $db->query("INSERT IGNORE INTO coupons SET coupon='" . $code . "',value='" . $value . "',status='0'"); diff --git a/admin.php b/admin.php index 486c1515..d993b41e 100644 --- a/admin.php +++ b/admin.php @@ -53,7 +53,7 @@ if ($creditSystem->isEnabled()) { echo 'var creditenabled=1;' . PHP_EOL; echo 'var creditcurrency="' . $creditSystem->getCreditCurrency() . '"' . PHP_EOL; - $requiredcredit = $credit["min"] + $credit["rent"] + $credit["longrental"]; + $minRequiredCredit = $creditSystem->getMinRequiredCredit(); } else { echo 'var creditenabled=0;', "\n"; } @@ -135,9 +135,9 @@
- - - + + +
@@ -159,9 +159,9 @@
- or - - + or + +
diff --git a/common.php b/common.php index d42751cf..1b97a4e4 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 ab7eaf46..383d00c1 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 41314e73..987005c6 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 c668ad3a..8d03c923 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 0937ecdb..25fb4546 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 37435ad3..aaee3568 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,