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 rent($number, $bike, $force = FALSE)
{
    global $db, $forcestack, $watches, $credit;
    $stacktopbike = FALSE;
    $userId = getUser($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();
            sendSMS($number, _('Please, recharge your credit:') . " " . $row["credit"] . $credit["currency"] . ". " . _('Credit required:') . " " . $requiredcredit . $credit["currency"] . ".");
            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) {
                sendSMS($number, _('You can not rent any bikes. Contact the admins to lift the ban.'));
            } elseif ($limit == 1) {
                sendSMS($number, _('You can only rent') . " " . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . " " . _('at once') . ".");
            } else {
                sendSMS($number, _('You can only rent') . " " . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . " " . _('at once') . " " . _('and you have already rented') . " " . $limit . ".");
            }
            return;
        }
        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 != $bikeNum) {
                response(_('Bike') . " " . $bike . " " . _('is not rentable now, you have to rent bike') . " " . $stacktopbike . " " . _('from this stand') . ".", ERROR);
                return;
            }
        }
    }
    $result = $db->query("SELECT currentUser,currentCode FROM bikes WHERE bikeNum={$bikeNum}");
    if ($result->num_rows != 1) {
        sendSMS($number, "Bike {$bikeNum} does not exist.");
        return;
    }
    $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 LIMIT 1");
    $row = $result->fetch_assoc();
    $note = $row["note"];
    if ($currentUser) {
        $result = $db->query("SELECT number FROM users WHERE userId={$currentUser}");
        $row = $result->fetch_assoc();
        $currentUserNumber = $row["number"];
    }
    $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 ($force == FALSE) {
        if ($currentUser == $userId) {
            sendSMS($number, _('You have already rented the bike') . " " . $bikeNum . ". " . _('Code is') . " " . $currentCode . ". " . _('Return bike with command:') . " RETURN " . _('bikenumber') . " " . _('standname') . ".");
            return;
        }
        if ($currentUser != 0) {
            sendSMS($number, _('Bike') . " " . $bikeNum . " " . _('is already rented') . ".");
            return;
        }
    }
    $message = _('Bike') . " " . $bikeNum . ": " . _('Open with code') . " " . $currentCode . ". " . _('Change code immediately to') . " " . $newCode . " " . _('(open,rotate metal part,set new code,rotate metal part back)') . ".";
    if ($note) {
        $message .= "(" . _('bike note') . ":" . $note . ")";
    }
    sendSMS($number, $message);
    $result = $db->query("UPDATE bikes SET currentUser={$userId},currentCode={$newCode},currentStand=NULL WHERE bikeNum={$bikeNum}");
    if ($force == FALSE) {
        $result = $db->query("INSERT INTO history SET userId={$userId},bikeNum={$bikeNum},action='RENT',parameter={$newCode}");
    } else {
        $result = $db->query("INSERT INTO history SET userId={$userId},bikeNum={$bikeNum},action='FORCERENT',parameter={$newCode}");
        if ($currentUser) {
            sendSMS($currentUserNumber, _('System override') . ": " . _('Your rented bike') . " " . $bikeNum . " " . _('has been rented by admin') . ".");
        }
    }
}