Skip to content

Commit

Permalink
Credit System get info about user credits left
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld committed Mar 16, 2024
1 parent 64fcc67 commit ca30532
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 98 deletions.
13 changes: 6 additions & 7 deletions actions-qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -169,8 +168,8 @@ function returnbike($userId,$stand)
$message.= '<br />'._('Please').', <strong>'._('rotate the lockpad to').' <span class="label label-default">0000</span></strong> '._('when leaving').'.';

$creditchange=changecreditendrental($bikeNum,$userId);
if ($creditSystem->isEnabled() AND $creditchange) {
$message.='<br />'._('Credit change').': -'.$creditchange.getcreditcurrency().'.';
if ($creditSystem->isEnabled() && $creditchange) {
$message .= '<br />' . _('Credit change') . ': -' . $creditchange . $creditSystem->getCreditCurrency() . '.';
}
$result=$db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='RETURN',parameter=$standId");

Expand Down
32 changes: 17 additions & 15 deletions actions-sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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);

}


Expand Down
24 changes: 13 additions & 11 deletions actions-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ 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'];

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);

Expand Down Expand Up @@ -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 .= '<br />' . _('Credit change') . ': -' . $creditchange . getcreditcurrency() . '.';
$message .= '<br />' . _('Credit change') . ': -' . $creditchange . $creditSystem->getCreditCurrency() . '.';
}

$result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='RETURN',parameter=$standId");
Expand Down Expand Up @@ -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()
Expand All @@ -629,6 +631,7 @@ function getcouponlist()
function generatecoupons($multiplier)
{
global $db, $credit, $codeGenerator, $creditSystem;

if ($creditSystem->isEnabled() == false) {
return;
}
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -738,7 +741,7 @@ function mapgetmarkers($userId)

function mapgetlimit($userId)
{
global $db, $auth;
global $db, $auth, $creditSystem;

if (!$auth->isLoggedIn()) {
response('');
Expand All @@ -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)
Expand Down
29 changes: 13 additions & 16 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@
<?php if (file_exists("analytics.php")) require("analytics.php"); ?>
<script>
<?php
if ($creditSystem->isEnabled())
{
echo 'var creditenabled=1;',"\n";
echo 'var creditcurrency="',$credit["currency"],'"',";\n";
$requiredcredit=$credit["min"]+$credit["rent"]+$credit["longrental"];
}
else
{
echo 'var creditenabled=0;',"\n";
}
if ($creditSystem->isEnabled()) {
echo 'var creditenabled=1;' . PHP_EOL;
echo 'var creditcurrency="' . $creditSystem->getCreditCurrency() . '"' . PHP_EOL;
$requiredcredit = $credit["min"] + $credit["rent"] + $credit["longrental"];
} else {
echo 'var creditenabled=0;', "\n";
}
?>
</script>
</head>
Expand Down Expand Up @@ -138,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,$credit["currency"],' '; 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,$credit["currency"],' '; 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,$credit["currency"],' '; 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 ' ',$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>
<div id="creditconsole"></div>
</div>
</div>
Expand All @@ -162,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,$credit["currency"]; ?></button>
<button type="button" id="addcredit2" class="btn btn-success"><?php echo _('Add'); echo ' ',$requiredcredit*5,$credit["currency"]; ?></button>
<button type="button" id="addcredit3" class="btn btn-success"><?php echo _('Add'); echo ' ',$requiredcredit*10,$credit["currency"]; ?></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>
</form>
<div id="userconsole"></div>
</div>
Expand Down
50 changes: 12 additions & 38 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 2 additions & 11 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
if ($auth->isLoggedIn()) {
echo '<li><span class="glyphicon glyphicon-user"></span> <small>', $user->findUserName($userid), '</small>';
if ($creditSystem->isEnabled()) {
echo ' (<span id="usercredit" title="', _('Remaining credit'), '">', getusercredit($userid), '</span> ', getcreditcurrency(), ' <button type="button" class="btn btn-success btn-xs" id="opencredit" title="', _('Add credit'), '"><span class="glyphicon glyphicon-plus"></span></button>)<span id="couponblock"><br /><span class="form-inline"><input type="text" class="form-control input-sm" id="coupon" placeholder="XXXXXX" /><button type="button" class="btn btn-primary btn-sm" id="validatecoupon" title="', _('Confirm coupon'), '"><span class="glyphicon glyphicon-plus"></span></button></span></span></li>';
$userRemainingCredit = $creditSystem->getUserCredit($userid);
echo ' (<span id="usercredit" title="', _('Remaining credit'), '">' . $userRemainingCredit . '</span> ' . $creditSystem->getCreditCurrency() . ' <button type="button" class="btn btn-success btn-xs" id="opencredit" title="', _('Add credit'), '"><span class="glyphicon glyphicon-plus"></span></button>)<span id="couponblock"><br /><span class="form-inline"><input type="text" class="form-control input-sm" id="coupon" placeholder="XXXXXX" /><button type="button" class="btn btn-primary btn-sm" id="validatecoupon" title="', _('Confirm coupon'), '"><span class="glyphicon glyphicon-plus"></span></button></span></span></li>';
}
if ($cities) {
echo '<li>','<select class="form-control input-sm" id="citychange" title="', _('My City'), '">';
Expand All @@ -131,16 +132,6 @@
<div class="col-xs-1 col-sm-1 col-md-1 col-lg-1">
</div>
</div>
<?php if (time() < 1561420000 ) { ?>
<div class="row bg-info">
<div class="col-xs-11 col-sm-11 col-md-11 col-lg-11 text-center" style="padding: 0.5em;">
<a href="https://www.rozhodni-bsk.sk/bratislava-2-2019/" target="_blank">
Zahlasuj za podporu Bielych bicyklov z participatívneho rozpočtu BSK!</a>
</div>
<div class="col-xs-1 col-sm-1 col-md-1 col-lg-1">
</div>
</div>
<?php } ?>
<?php if ($auth->isLoggedIn()): ?>
<div class="row">
<div class="col-xs-11 col-sm-11 col-md-11 col-lg-11">
Expand Down

0 comments on commit ca30532

Please sign in to comment.