Skip to content

Commit

Permalink
Merge pull request #188 from cyklokoalicia/rentSystem
Browse files Browse the repository at this point in the history
Rent system
  • Loading branch information
sveneld authored May 24, 2024
2 parents 181682c + 6ab0f72 commit ae5f9c4
Show file tree
Hide file tree
Showing 13 changed files with 622 additions and 681 deletions.
151 changes: 2 additions & 149 deletions actions-qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ function response($message,$error=0,$log=1)
$userid = $auth->getUserId();
$number = $user->findPhoneNumber($userid);
logresult($number, $message);
$db->commit();
}
$db->commit();
echo '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>',$systemname,'</title>';
echo '<base href="',$systemURL,'" />';
echo '<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />';
Expand Down Expand Up @@ -68,154 +68,7 @@ function showrentform($userId,$bike)
exit;
}

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

$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");
$row = $result->fetch_assoc();
$countRented = $row["countRented"];

$result=$db->query("SELECT userLimit FROM limits where userId=$userId");
$row = $result->fetch_assoc();
$limit = $row["userLimit"];

if ($countRented>=$limit)
{
if ($limit==0)
{
response(_('You can not rent any bikes. Contact the admins to lift the ban.'),ERROR);
}
elseif ($limit==1)
{
response(_('You can only rent')." ".sprintf(ngettext('%d bike','%d bikes',$limit),$limit)." "._('at once').".",ERROR);
}
else
{
response(_('You can only rent')." ".sprintf(ngettext('%d bike','%d bikes',$limit),$limit)." "._('at once and you have already rented')." ".$limit.".",ERROR);
}
}

if ($forcestack OR $watches["stack"])
{
$result=$db->query("SELECT currentStand FROM bikes WHERE bikeNum='$bike'");
$row=$result->fetch_assoc();
$standid=$row["currentStand"];
$stacktopbike=checktopofstack($standid);
if ($watches["stack"] AND $stacktopbike<>$bike)
{
$result=$db->query("SELECT standName FROM stands WHERE standId='$standid'");
$row=$result->fetch_assoc();
$stand=$row["standName"];
$userName = $user->findUserName($userId);
notifyAdmins(_('Bike')." ".$bike." "._('rented out of stack by')." ".$userName.". ".$stacktopbike." "._('was on the top of the stack at')." ".$stand.".",ERROR);
}
if ($forcestack AND $stacktopbike<>$bike)
{
response(_('Bike')." ".$bike." "._('is not rentable now, you have to rent bike')." ".$stacktopbike." "._('from this stand').".",ERROR);
}
}

$result=$db->query("SELECT currentUser,currentCode FROM bikes WHERE bikeNum=$bikeNum");
$row=$result->fetch_assoc();
$currentCode=sprintf("%04d",$row["currentCode"]);
$currentUser=$row["currentUser"];
$result=$db->query("SELECT note FROM notes WHERE bikeNum='$bikeNum' AND deleted IS NULL ORDER BY time DESC");
$note="";
while ($row=$result->fetch_assoc())
{
$note.=$row["note"]."; ";
}
$note=substr($note,0,strlen($note)-2); // remove last two chars - comma and space

$newCode=sprintf("%04d",rand(100,9900)); //do not create a code with more than one leading zero or more than two leading 9s (kind of unusual/unsafe).

if ($currentUser==$userId)
{
response(_('You have already rented the bike').' '.$bikeNum.'. '._('Code is').' <span class="label label-primary">'.$currentCode.'</span>. '._('Return bike by scanning QR code on a stand').'.',ERROR);
return;
}
if ($currentUser!=0)
{
response(_('Bike')." ".$bikeNum." "._('is already rented').".",ERROR);
return;
}

$message='<h3>'._('Bike').' '.$bikeNum.': <span class="label label-primary">'._('Open with code').' '.$currentCode.'.</span></h3><h3>'._('Change code immediately to').' <span class="label label-default">'.$newCode.'</span></h3>'._('(open, rotate metal part, set new code, rotate metal part back)').'.';
if ($note)
{
$message.="<br />"._('Reported issue:')." <em>".$note."</em>";
}

$result=$db->query("UPDATE bikes SET currentUser=$userId,currentCode=$newCode,currentStand=NULL WHERE bikeNum=$bikeNum");
$result=$db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='RENT',parameter=$newCode");
response($message);

}


function returnbike($userId,$stand)
{

global $db,$connectors, $creditSystem;
$stand=strtoupper($stand);

$result=$db->query("SELECT bikeNum FROM bikes WHERE currentUser=$userId ORDER BY bikeNum");
$bikenumber=$result->num_rows;

if ($bikenumber==0)
{
response(_('You have no rented bikes currently.'),ERROR);
}
elseif ($bikenumber>1)
{
$message=_('You have').' '.$bikenumber.' '._('rented bikes currently. QR code return can be used only when 1 bike is rented. Please, use web');
if ($connectors["sms"]) $message.=_(' or SMS');
$message.=_(' to return the bikes.');
response($message,ERROR);
}
else
{
$result=$db->query("SELECT bikeNum,currentCode FROM bikes WHERE currentUser=$userId");
$row=$result->fetch_assoc();
$currentCode=sprintf("%04d",$row["currentCode"]);
$bikeNum=$row["bikeNum"];

$result=$db->query("SELECT standId FROM stands where standName='$stand'");
$row = $result->fetch_assoc();
$standId = $row["standId"];

$result=$db->query("UPDATE bikes SET currentUser=NULL,currentStand=$standId WHERE bikeNum=$bikeNum and currentUser=$userId");

$message = '<h3>'._('Bike').' '.$bikeNum.': <span class="label label-primary">'._('Lock with code').' '.$currentCode.'.</span></h3>';
$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() && $creditchange) {
$message .= '<br />' . _('Credit change') . ': -' . $creditchange . $creditSystem->getCreditCurrency() . '.';
}
$result=$db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='RETURN',parameter=$standId");

response($message);
}

}

function unrecognizedqrcode($userId)
{
global $db;
response("<h3>"._('Unrecognized QR code action. Try scanning the code again or report this to the system admins.')."</h3>",ERROR);
}

?>
Loading

0 comments on commit ae5f9c4

Please sign in to comment.