Skip to content

Commit

Permalink
Credit System get info about minimal credit balance for rent
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld committed Mar 16, 2024
1 parent ca30532 commit 1eb3a67
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 54 deletions.
4 changes: 2 additions & 2 deletions actions-qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions actions-sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions actions-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'");
Expand Down
14 changes: 7 additions & 7 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down Expand Up @@ -135,9 +135,9 @@
<div class="row">
<div class="col-lg-12">
<button type="button" id="listcoupons" class="btn btn-default" title="<?php echo _('Display existing coupons.'); ?>"><span class="glyphicon glyphicon-list-alt"></span> <?php echo _('List coupons'); ?></button>
<button type="button" id="generatecoupons1" class="btn btn-success" title="<?php echo _('Generate new coupons.'); ?>"><span class="glyphicon glyphicon-plus"></span> <?php echo _('Generate'); echo ' ',$requiredcredit,$creditSystem->getCreditCurrency(),' '; echo _('coupons'); ?></button>
<button type="button" id="generatecoupons2" class="btn btn-success" title="<?php echo _('Generate new coupons.'); ?>"><span class="glyphicon glyphicon-plus"></span> <?php echo _('Generate'); echo ' ',$requiredcredit*5,$creditSystem->getCreditCurrency(),' '; echo _('coupons'); ?></button>
<button type="button" id="generatecoupons3" class="btn btn-success" title="<?php echo _('Generate new coupons.'); ?>"><span class="glyphicon glyphicon-plus"></span> <?php echo _('Generate'); echo ' ',$requiredcredit*10,$creditSystem->getCreditCurrency(),' '; echo _('coupons'); ?></button>
<button type="button" id="generatecoupons1" class="btn btn-success" title="<?php echo _('Generate new coupons.'); ?>"><span class="glyphicon glyphicon-plus"></span> <?php echo _('Generate'); echo ' ',$minRequiredCredit,$creditSystem->getCreditCurrency(),' '; echo _('coupons'); ?></button>
<button type="button" id="generatecoupons2" class="btn btn-success" title="<?php echo _('Generate new coupons.'); ?>"><span class="glyphicon glyphicon-plus"></span> <?php echo _('Generate'); echo ' ',$minRequiredCredit*5,$creditSystem->getCreditCurrency(),' '; echo _('coupons'); ?></button>
<button type="button" id="generatecoupons3" class="btn btn-success" title="<?php echo _('Generate new coupons.'); ?>"><span class="glyphicon glyphicon-plus"></span> <?php echo _('Generate'); echo ' ',$minRequiredCredit*10,$creditSystem->getCreditCurrency(),' '; echo _('coupons'); ?></button>
<div id="creditconsole"></div>
</div>
</div>
Expand All @@ -159,9 +159,9 @@
<div class="form-group"><label for="limit"><?php echo _('Bike limit:'); ?></label> <input type="text" name="limit" id="limit" class="form-control" /></div>
<input type="hidden" name="userid" id="userid" value="" />
<button type="button" id="saveuser" class="btn btn-primary"><?php echo _('Save'); ?></button>
or <button type="button" id="addcredit" class="btn btn-success"><?php echo _('Add'); echo ' ',$requiredcredit,$creditSystem->getCreditCurrency(); ?></button>
<button type="button" id="addcredit2" class="btn btn-success"><?php echo _('Add'); echo ' ',$requiredcredit*5,$creditSystem->getCreditCurrency(); ?></button>
<button type="button" id="addcredit3" class="btn btn-success"><?php echo _('Add'); echo ' ',$requiredcredit*10,$creditSystem->getCreditCurrency(); ?></button>
or <button type="button" id="addcredit" class="btn btn-success"><?php echo _('Add'); echo ' ',$minRequiredCredit,$creditSystem->getCreditCurrency(); ?></button>
<button type="button" id="addcredit2" class="btn btn-success"><?php echo _('Add'); echo ' ',$minRequiredCredit*5,$creditSystem->getCreditCurrency(); ?></button>
<button type="button" id="addcredit3" class="btn btn-success"><?php echo _('Add'); echo ' ',$minRequiredCredit*10,$creditSystem->getCreditCurrency(); ?></button>
</form>
<div id="userconsole"></div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
20 changes: 13 additions & 7 deletions install/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
//@TODO ANaLYTICS!!!

use BikeShare\Credit\CreditSystemFactory;
use BikeShare\Credit\CreditSystemInterface;
use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use Monolog\ErrorHandler;
Expand Down Expand Up @@ -412,6 +414,11 @@ function return_bytes($val) {
*/
$db = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname, $logger);
$db->connect();
/**
* @var CreditSystemInterface $creditSystem
*/
$creditSystem = (new CreditSystemFactory())->getCreditSystem($credit, $db);

$configfile=file($configfilename);
foreach ($_POST as $variable=>$value)
{
Expand All @@ -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();
?>
<h2>Installation finished</h2>
Expand Down
17 changes: 7 additions & 10 deletions src/Credit/CreditSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -47,6 +47,11 @@ public function getUserCredit($userid)
return $result->fetchAssoc()['credit'];
}

public function getMinRequiredCredit()
{
return $this->minBalanceCredit + $this->rentalFee + $this->longRentalFee;
}

/**
* @return bool
*/
Expand All @@ -62,14 +67,6 @@ public function getCreditCurrency()
return $this->creditCurrency;
}

/**
* @return int
*/
public function getMinBikeCredit()
{
return $this->minBikeCredit;
}

/**
* @return int
*/
Expand Down Expand Up @@ -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'];
Expand Down
10 changes: 5 additions & 5 deletions src/Credit/CreditSystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ interface CreditSystemInterface
*/
public function getUserCredit($userid);

/**
* @return int
*/
public function getMinRequiredCredit();

/**
* @return bool
*/
Expand All @@ -19,11 +24,6 @@ public function isEnabled();
*/
public function getCreditCurrency();

/**
* @return int
*/
public function getMinBikeCredit();

/**
* @return int
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Credit/DisabledCreditSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Credit/CreditSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testConstructor(
$configuration,
$expectedIsEnabled,
$expectedCreditCurrency,
$expectedMinBikeCredit,
$expectedMinRequiredCredit,
$expectedRentalFee,
$expectedPriceCycle,
$expectedLongRentalFee,
Expand All @@ -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());
Expand All @@ -40,7 +40,7 @@ public function constructorDataProvider()
'configuration' => [],
'expectedIsEnabled' => false,
'expectedCreditCurrency' => '',
'expectedMinBikeCredit' => 2,
'expectedMinRequiredCredit' => 9,
'expectedRentalFee' => 2,
'expectedPriceCycle' => 0,
'expectedLongRentalFee' => 5,
Expand All @@ -60,7 +60,7 @@ public function constructorDataProvider()
],
'expectedIsEnabled' => true,
'expectedCreditCurrency' => '$',
'expectedMinBikeCredit' => 3,
'expectedMinRequiredCredit' => 12,
'expectedRentalFee' => 3,
'expectedPriceCycle' => 1,
'expectedLongRentalFee' => 6,
Expand Down

0 comments on commit 1eb3a67

Please sign in to comment.