Studios
Business Tablethouse-3.jpg

Examples

Business TabletQBCore · Qbox · ESX
PAGE 04 OF 08  ·  CONFIGURATION → EXAMPLES → TROUBLESHOOTINGdocs.glitchstudios.dev
Browse docs

Every export with an example of it in use. The examples are for a pizzeria with its own resource, my-pizzeria, and a job called pizzeria. Server examples that need a player's citizen ID use this helper. It is written for Qbox, so swap in your framework's call:

local function citizenIdOf(src)
    local player = exports.qbx_core:GetPlayer(src)
    return player and player.PlayerData.citizenid
end

Wrap calls in pcall when your resource can run without the tablet, so a stopped tablet does not throw an error in your script.

Registering#

RegisterBusiness#

Register when your resource starts, and again whenever the tablet restarts.

local function registerTablet()
    if GetResourceState('glitch-businesstablet') ~= 'started' then return end
    -- the tablet can refuse calls for a moment after it starts, so retry briefly
    for _ = 1, 10 do
        local called, ok, reason = pcall(function()
            return exports['glitch-businesstablet']:RegisterBusiness('pizzeria', {
                appName  = 'Pizza This',
                iconPath = 'https://cfx-nui-my-pizzeria/html/logo.png',
                icon     = 'pizza-slice',
                colour   = '#e4572e',
            })
        end)
        if called and ok then return end
        if called and reason then print(('business tablet: %s'):format(reason)) end
        Wait(500)
    end
    print('business tablet registration failed')
end

AddEventHandler('onResourceStart', function(resource)
    if resource == 'glitch-businesstablet' then CreateThread(registerTablet) end
end)

CreateThread(registerTablet)

All the fields are on Registering a business.

UnregisterBusiness#

A business is removed by itself when the resource that registered it stops. Call this to take it off the tablet while your resource keeps running, for example when the owner closes the shop for good. Only the resource that registered a business can remove it.

local ok, reason = exports['glitch-businesstablet']:UnregisterBusiness('pizzeria')
if not ok then print(reason) end -- 'pizzeria is not registered' or 'pizzeria is owned by <resource>'

GetBusinesses#

Returns every registered business, keyed by job name.

for businessId, business in pairs(exports['glitch-businesstablet']:GetBusinesses()) do
    print(businessId, business.appName, business.resource)
end

Each entry holds appName, icon, iconPath, colour, colour2, resource, extension, features and permissions.

IsFeatureEnabled#

Returns true when a feature ('loyalty', 'memberships' or 'ordering') is on for a business.

if exports['glitch-businesstablet']:IsFeatureEnabled('pizzeria', 'loyalty') then
    -- show the "Loyalty card" button on your till
end

Sales#

NextOrderNumber and AddOrderTransaction#

A till that shows the order number before the customer pays takes the number first, then logs the sale with it. The tablet's New sale page and your tills share one sequence, so a number never repeats. A number that is taken but never paid is skipped.

RegisterNetEvent('my-pizzeria:startOrder', function()
    local orderNumber = exports['glitch-businesstablet']:NextOrderNumber('pizzeria') -- nil if pizzeria is not registered
    TriggerClientEvent('my-pizzeria:showOrderNumber', source, orderNumber)
end)

RegisterNetEvent('my-pizzeria:payOrder', function(orderNumber, customerSrc)
    local src = source
    local total = 42 -- work prices out on the server, never trust the client

    local ok, number, commission = exports['glitch-businesstablet']:AddOrderTransaction(src, 'pizzeria', {
        price       = total,
        description = '2x Margherita, 1x Cola',
        cashierId   = citizenIdOf(src),          -- or 'kiosk' for a self-serve kiosk
        recipientId = citizenIdOf(customerSrc),
        source      = 'Till 1',
        orderNumber = orderNumber,
        loyaltyCard = citizenIdOf(customerSrc),  -- the customer earns a stamp
    })
    if not ok then return print('sale was not logged') end
    print(('order #%d logged, cashier earns $%d'):format(number, commission))
end)

The sale shows in Transactions, and the cashier is owed their rank's commission. Leave out orderNumber and the tablet takes the next number itself. Every orderData field is listed on Commands & Exports.

A sale worked by the whole shift, with tax included in the price:

exports['glitch-businesstablet']:AddOrderTransaction(src, 'pizzeria', {
    price       = 640,
    gst         = 58.18,           -- commission is worked out on 640 - 58.18
    description = 'Catering for 20',
    cashierId   = citizenIdOf(src),
    splitOnDuty = true,            -- everyone on duty earns commission on their share
})

GetMenu#

Returns the menu the boss set up on the tablet, in menu order. Use it so your till always matches the tablet.

for _, entry in ipairs(exports['glitch-businesstablet']:GetMenu('pizzeria')) do
    -- entry.item_id, entry.label, entry.description, entry.price, entry.item, entry.category, entry.tag
    if entry.price then
        print(('%s: $%d'):format(entry.label, entry.price))
    else
        print(('%s: price on request'):format(entry.label))
    end
end

GetMemberDiscount#

Returns the best membership discount the customer has (a percent, 0 when they have none) and the tier's name. It is always 0 while memberships are off for the business.

local percent, tier = exports['glitch-businesstablet']:GetMemberDiscount('pizzeria', citizenIdOf(customerSrc))
if percent > 0 then
    total = math.floor(total * (100 - percent) / 100)
    print(('%s member: %d%% off'):format(tier, percent))
end

GetBlacklistEntry#

Looks up a character's full name on the business's blacklist. Expired entries do not count, and the name is not case-sensitive.

local entry = exports['glitch-businesstablet']:GetBlacklistEntry('pizzeria', 'Marco Rossi')
if entry then
    -- entry.name, entry.reason, entry.expiry_date (nil when it never expires)
    return notify(src, ('We cannot serve you: %s'):format(entry.reason))
end

Loyalty cards#

CheckLoyaltyCode#

Looks up a card from the 4-character code the customer reads off their phone. It changes nothing. Pass the staff member's source as the third argument so lookups are rate limited like the tablet's own Loyalty page.

local card, err = exports['glitch-businesstablet']:CheckLoyaltyCode('pizzeria', 'K7QF', src)
if not card then return notify(src, err) end -- for example 'No card with the code K7QF.'

print(('%s has %d of %d stamps'):format(card.name, card.stamps, card.need))
if card.ready > 0 then
    -- card.reward is the reward's name. card.type is 'item' (card.menuItem free, worth up to
    -- card.maxValue) or 'percent' (card.percent off the order)
end

The card also has code, citizenid, redeemed and minSpend.

RedeemLoyaltyCode#

Takes one reward's worth of stamps off the card and tells the customer on their phone. Pass the staff member's citizen ID so staff cannot redeem their own card. The fourth argument is optional: when you pass a customer's citizen ID, the code must belong to them.

local card, err = exports['glitch-businesstablet']:RedeemLoyaltyCode('pizzeria', 'K7QF', citizenIdOf(src), nil)
if not card then return notify(src, err) end -- for example 'This card needs 8 stamps for a reward.'

-- now give the reward on your till, then log the sale as normal
print(('%s redeemed, %d stamps left'):format(card.name, card.stamps))

If two tills redeem the same card at once, only one of them succeeds.

GetLoyaltyCardOf#

A customer's own card at a business, in the same shape as CheckLoyaltyCode, or nil when they have none. Useful for phone orders, where the customer has no code to type.

local card = exports['glitch-businesstablet']:GetLoyaltyCardOf('pizzeria', citizenIdOf(src))
if card and card.ready > 0 then
    local redeemed, err = exports['glitch-businesstablet']:RedeemLoyaltyCode('pizzeria', card.code, nil, card.citizenid)
end

Staff#

HasPermission#

Checks a built-in permission, or one your business added when it registered. It is false unless the business is the player's current job, and the top rank always passes.

lib.callback.register('my-pizzeria:saveSpecials', function(source, specials)
    if not exports['glitch-businesstablet']:HasPermission(source, 'pizzeria', 'manage_menu') then
        return { ok = false, error = 'Your rank cannot change the menu.' }
    end
    -- save the specials
    return { ok = true }
end)

GetCommissionRate#

The commission percent of a citizen's rank, or nil when they do not work there. Use it when your script pays its own commission, then pass the amount to AddOrderTransaction so the tablet does not pay it twice.

local cashier = citizenIdOf(src)
local rate = exports['glitch-businesstablet']:GetCommissionRate('pizzeria', cashier)
if not rate then return notify(src, 'You do not work here.') end

local owed = math.floor(total * rate / 100)
exports['glitch-businesstablet']:AddOrderTransaction(src, 'pizzeria', {
    price = total, description = 'Delivery run', cashierId = cashier,
    commissionOwed = owed, -- the cashier is owed this instead of their rank's rate
})

glitch-businesstablet:updateStaffPay#

A server-only event. After your resource has saved pay it owes an employee itself, fire it so open tablets show the new wages owed without a reload. It changes what the tablet shows, not what is saved.

TriggerEvent('glitch-businesstablet:updateStaffPay', 'pizzeria', employeeCitizenId, 25)

GiveApplicationForm#

Gives a player blank application forms, for example from a stack on the front desk.

exports.ox_target:addBoxZone({
    coords = vec3(-1188.4, -883.2, 14.0), size = vec3(1.0, 1.0, 1.0),
    options = { {
        label = 'Take an application form',
        icon = 'fa-solid fa-file-pen',
        serverEvent = 'my-pizzeria:takeForm',
    } },
})
RegisterNetEvent('my-pizzeria:takeForm', function()
    local ok, err = exports['glitch-businesstablet']:GiveApplicationForm(source, 'pizzeria', 1)
    if not ok then notify(source, err) end
end)

UseApplication#

The job_application item's server export. You do not call it yourself: ox_inventory calls it when a player uses a form. It is set up in the item definition on Installation.

Your own tablet pages#

PushExtension#

Tells every open tablet to reload one of your business's own pages. Call it after something changes outside the tablet, such as stock used up in the kitchen.

local function stockChanged()
    pcall(function() exports['glitch-businesstablet']:PushExtension('pizzeria', 'stock') end)
end

BusinessTabletCall#

Your resource provides this export, and the tablet calls it when your page runs api.ext(page, action, data). Return a table, and set ok = false with an error to show the player a message.

exports('BusinessTabletCall', function(source, businessId, page, action, data)
    if page == 'stock' and action == 'load' then
        return { ok = true, items = GetStock() }
    end
    if page == 'stock' and action == 'restock' then
        if not exports['glitch-businesstablet']:HasPermission(source, businessId, 'manage_menu') then
            return { ok = false, error = 'Your rank cannot restock.' }
        end
        Restock(data.item, tonumber(data.amount) or 0)
        exports['glitch-businesstablet']:PushExtension(businessId, 'stock')
        return { ok = true }
    end
    return { ok = false, error = 'Unknown action.' }
end)

The page side is on Custom pages.

Phone ordering#

With features.ordering = true, the customer phone app calls these exports in your resource. Your resource prices, charges and queues the order. picked is a list of { item, label } from the app, so always price it from your own menu.

local TAKING_ORDERS = true

exports('PhoneOrdersOpen', function()
    return TAKING_ORDERS -- for example, true while staff are on duty
end)

exports('PhoneOrderLimits', function()
    return { pizzeria_margherita = 4, pizzeria_cola = 6 } -- most of one item per order
end)

exports('PhoneOrder', function(src, picked, useCard)
    if not TAKING_ORDERS then return { ok = false, error = 'We are closed right now.' } end
    if #picked == 0 then return { ok = false, error = 'Your order is empty.' } end

    local menu = {}
    for _, entry in ipairs(exports['glitch-businesstablet']:GetMenu('pizzeria')) do menu[entry.item] = entry end

    local total, names = 0, {}
    for _, pick in ipairs(picked) do
        local entry = menu[pick.item]
        if not entry or not entry.price then return { ok = false, error = 'Something on your order is no longer on the menu.' } end
        total += entry.price
        names[#names + 1] = entry.label
    end

    -- useCard: redeem their reward with GetLoyaltyCardOf and RedeemLoyaltyCode (see above)

    if not exports.qbx_core:RemoveMoney(src, 'bank', total, 'pizzeria-phone-order') then
        return { ok = false, error = 'Not enough money in your bank account.' }
    end

    local cid = citizenIdOf(src)
    local ok, orderNumber = exports['glitch-businesstablet']:AddOrderTransaction(src, 'pizzeria', {
        price = total, description = table.concat(names, ', '),
        cashierId = 'kiosk', recipientId = cid, appCustomer = cid, source = 'Phone',
        loyaltyCard = useCard and cid or nil,
    })
    -- add the order to your kitchen queue here
    return { ok = ok, orderNumber = orderNumber, total = total }
end)

exports('PhoneOrders', function(src)
    -- the player's open orders, newest first
    return {
        { orderNumber = 1042, status = 'cooking', label = 'Cooking', total = 42, count = 3, phone = true },
    }
end)

Client#

OpenTablet and CloseTablet#

Open and close the standalone tablet for the player's business, for example from a radial menu or an ox_target option on the office desk.

exports.ox_target:addBoxZone({
    coords = vec3(-1196.1, -891.5, 14.0), size = vec3(1.0, 1.0, 1.0),
    options = { {
        label = 'Use the office tablet',
        icon = 'fa-solid fa-tablet-screen-button',
        groups = 'pizzeria',
        onSelect = function() exports['glitch-businesstablet']:OpenTablet() end,
    } },
})

-- close it if the player is cuffed
AddEventHandler('my-pizzeria:cuffed', function()
    exports['glitch-businesstablet']:CloseTablet()
end)

OpenTablet is also the tablet item's client export (see Installation). Both only work in standalone mode. With sd-tablet or LB Tablet, players open the app on that device.