Skip to content

Commit

Permalink
Merge pull request #185 from cyklokoalicia/credits
Browse files Browse the repository at this point in the history
Credits system config usage via CreditSystemInterface
  • Loading branch information
sveneld authored Mar 16, 2024
2 parents 64fcc67 + 2dc2459 commit 5d8838c
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 180 deletions.
22 changes: 11 additions & 11 deletions actions-qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ function response($message,$error=0,$log=1)

function rent($userId,$bike,$force=FALSE)
{
global $db, $forcestack, $watches, $user, $creditSystem;

global $db,$forcestack,$watches,$credit, $user;
$stacktopbike=FALSE;
$bikeNum = $bike;
$requiredcredit=$credit["min"]+$credit["rent"]+$credit["longrental"];
$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;
}

$creditcheck=checkrequiredcredit($userId);
if ($creditcheck===FALSE)
{
response(_('You are below required credit')." ".$requiredcredit.$credit["currency"].". "._('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 +169,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
49 changes: 26 additions & 23 deletions actions-sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,32 @@ 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;
$stacktopbike = FALSE;
$userId = $user->findUserIdByNumber($number);
$bikeNum = intval($bike);
$requiredcredit=$credit["min"]+$credit["rent"]+$credit["longrental"];

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"].".");
return;
}
$bikeNum = intval($bike);

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 Expand Up @@ -361,12 +362,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
40 changes: 22 additions & 18 deletions actions-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ 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);
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 Expand Up @@ -164,7 +167,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 +605,15 @@ function saveuser($userid, $username, $email, $phone, $privileges, $limit)

function addcredit($userid, $creditmultiplier)
{
global $db, $credit, $user;
$requiredcredit = $credit['min'] + $credit['rent'] + $credit['longrental'];
$addcreditamount = $requiredcredit * $creditmultiplier;
global $db, $credit, $user, $creditSystem;

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

response(_('Added') . ' ' . $addcreditamount . $credit['currency'] . ' ' . _('credit for') . ' ' . $userName . '.');
response(_('Added') . ' ' . $addcreditamount . $creditSystem->getCreditCurrency() . ' ' . _('credit for') . ' ' . $userName . '.');
}

function getcouponlist()
Expand All @@ -629,17 +633,18 @@ function getcouponlist()
function generatecoupons($multiplier)
{
global $db, $credit, $codeGenerator, $creditSystem;

if ($creditSystem->isEnabled() == false) {
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'");
}
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 +672,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 +743,7 @@ function mapgetmarkers($userId)

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

if (!$auth->isLoggedIn()) {
response('');
Expand All @@ -754,10 +759,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;
$minRequiredCredit = $creditSystem->getMinRequiredCredit();
} 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 ' ',$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 @@ -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 ' ',$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
Loading

0 comments on commit 5d8838c

Please sign in to comment.