function listBikes($number, $stand)
{
    global $db, $forcestack;
    $stacktopbike = FALSE;
    $userId = getUser($number);
    $stand = strtoupper($stand);
    if (!preg_match("/^[A-Z]+[0-9]*\$/", $stand)) {
        sendSMS($number, _('Stand name') . " '{$stand}' " . _('has not been recognized. Stands are marked by CAPITALLETTERS.'));
        return;
    }
    $result = $db->query("SELECT standId FROM stands WHERE standName='{$stand}'");
    if ($result->num_rows != 1) {
        sendSMS($number, _('Stand') . " '{$stand}' " . _('does not exist') . ".");
        return;
    }
    $row = $result->fetch_assoc();
    $standId = $row["standId"];
    if ($forcestack) {
        $stacktopbike = checktopofstack($standId);
    }
    $result = $db->query("SELECT bikeNum FROM bikes where currentStand={$standId} ORDER BY bikeNum");
    $rentedBikes = $result->num_rows;
    if ($rentedBikes == 0) {
        sendSMS($number, _('Stand') . " " . $stand . " " . _('is empty') . ".");
        return;
    }
    $listBikes = "";
    while ($row = $result->fetch_assoc()) {
        $listBikes .= $row["bikeNum"];
        if ($stacktopbike == $row["bikeNum"]) {
            $listBikes .= " " . _('(first)');
        }
        $listBikes .= ",";
    }
    if ($rentedBikes > 1) {
        $listBikes = substr($listBikes, 0, strlen($listBikes) - 1);
    }
    sendSMS($number, sprintf(ngettext('%d bike', '%d bikes', $rentedBikes), $rentedBikes) . " " . _('on stand') . " " . $stand . ": " . $listBikes);
}
function rent($userId, $bike, $force = FALSE)
{
    global $db, $forcestack, $watches, $credit;
    $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);
    }
    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"];
            $user = getusername($userId);
            notifyAdmins(_('Bike') . " " . $bike . " " . _('rented out of stack by') . " " . $user . ". " . $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}' 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>' . _('Change code immediately to') . ' <span class="label label-default">' . $newCode . '</span><br />' . _('(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 listbikes($stand)
{
    global $db, $forcestack;
    $stacktopbike = FALSE;
    $stand = $db->conn->real_escape_string($stand);
    if ($forcestack) {
        $result = $db->query("SELECT standId FROM stands WHERE standName='{$stand}'");
        $row = $result->fetch_assoc();
        $stacktopbike = checktopofstack($row["standId"]);
    }
    $result = $db->query("SELECT bikeNum FROM bikes LEFT JOIN stands ON bikes.currentStand=stands.standId WHERE standName='{$stand}'");
    while ($row = $result->fetch_assoc()) {
        $bikenum = $row["bikeNum"];
        $result2 = $db->query("SELECT note FROM notes WHERE bikeNum='{$bikenum}' AND deleted IS NULL ORDER BY time DESC");
        $note = "";
        while ($row = $result2->fetch_assoc()) {
            $note .= $row["note"] . "; ";
        }
        $note = substr($note, 0, strlen($note) - 2);
        // remove last two chars - comma and space
        if ($note) {
            $bicycles[] = "*" . $bikenum;
            // bike with note / issue
            $notes[] = $note;
        } else {
            $bicycles[] = $bikenum;
            $notes[] = "";
        }
    }
    if (!$result->num_rows) {
        $bicycles = "";
        $notes = "";
    }
    response($bicycles, 0, array("notes" => $notes, "stacktopbike" => $stacktopbike), 0);
}