# lb-phone

{% tabs %}
{% tab title="ESX" %}

1. Navigate to `lb-phone/server/custom/frameworks/esx/vehicles.lua`
2. Replace function `GetPlayerVehicles` with this:<br>

   ```lua
   function GetPlayerVehicles(source)
       local toSend = {}
       local vehicles = MySQL.query.await("SELECT * FROM owned_vehicles WHERE owner = ? ", { GetIdentifier(source) })

       for i = 1, #vehicles do
           local vehicle = vehicles[i] or {}

           if vehicle.stored == nil then
               vehicle.stored = vehicle.state
           end

           local impounded = false

           if type(vehicle.stored) ~= "boolean" then
               impounded = vehicle.stored == 2
               vehicle.stored = vehicle.stored == 1
           end

           if GetResourceState("cd_garage") == "started" then
               debugprint("Using cd_garage")

               impounded = vehicle.in_garage == 2
               vehicle.stored = vehicle.in_garage or vehicle.in_garage == 1
               vehicle.garage = vehicle.garage_id
               vehicle.type = vehicle.garage_type
           elseif GetResourceState("loaf_garage") == "started" then
               vehicle.stored = 1
           elseif GetResourceState("jg-advancedgarages") == "started" then
               debugprint("Using jg-advancedgarages")

               vehicle.stored = vehicle.in_garage
               vehicle.garage = vehicle.garage_id

               impounded = vehicle.impound == 1

               if impounded and vehicle.impound_data then
                   vehicle.stored = false
                   vehicle.pound = "Impound"

                   local impoundInfo = json.decode(vehicle.impound_data)

                   vehicle.impoundReason = impoundInfo and {
                       reason = impoundInfo.reason and #impoundInfo.reason > 0 and impoundInfo.reason or nil,
                       retrievable = ConvertJSTimestamp and ConvertJSTimestamp(impoundInfo.retrieval_date) or nil,
                       price = impoundInfo.retrieval_cost,
                       impounder = impoundInfo.charname
                   }
               end
           elseif GetResourceState("op-garages") == "started" then
               debugprint("Using op-garages")
               vehicle.stored = not vehicle.stored

               local garage = exports['op-garages']:getGarageByIndex(tostring(vehicle.vehicleGarage))
               vehicle.garage = garage and garage.Label or "Garage"

               if vehicle.isTowedOut then
                   local impound = exports['op-garages']:getImpoundByIndex(tostring(vehicle.vehicleImpound))
                   vehicle.stored = false
                   vehicle.pound = impound and impound.Label or "Impound"

                   vehicle.impoundReason = {
                       reason = vehicle.impoundReason,
                       retrievable = vehicle.retrievable,
                       price = vehicle.towedPrice,
                       impounder = vehicle.towedOutBy
                   }
               end
           end

           if GetResourceState("qs-advancedgarages") == "started" then
               vehicle.stored = vehicle.garage ~= "OUT"
           end

           local location = vehicle.stored and (vehicle.garage or "Garage") or "out"

           if impounded and vehicle.pound then
               location = vehicle.pound
           end

           ---@type VehicleData
           local newCar = {
               plate = vehicle.plate,
               type = vehicle.type,
               location = location,
               impounded = impounded,
               statistics = {},
               impoundReason = vehicle.impoundReason
           }

           if vehicle.damages then
               local damages = json.decode(vehicle.damages)

               if damages?.engineHealth then
                   newCar.statistics.engine = math.floor(damages.engineHealth / 10 + 0.5)
               end

               if damages?.bodyHealth then
                   newCar.statistics.body = math.floor(damages.bodyHealth / 10 + 0.5)
               end
           end

           local vehicleMods = json.decode(vehicle.vehicle)

           if vehicleMods.fuel then
               newCar.statistics.fuel = math.floor(vehicleMods.fuel + 0.5)
           end

           newCar.model = vehicleMods.model

           toSend[#toSend+1] = newCar
       end

       return toSend
   end
   ```
3. Replace function `GetVehicle` with this:<br>

   ```lua
   function GetVehicle(source, plate)
       local storedCheck = "`%s` = ?"
       local storedColumn = "stored"

       ---@type any
       local storedValue = 1

       ---@type any
       local outValue = 0

       if GetResourceState("cd_garage") == "started" or GetResourceState("jg-advancedgarages") == "started" then
           storedColumn = "in_garage"
       elseif GetResourceState("qs-advancedgarages") == "started" then
           storedCheck = "`%s` != ?"
           storedColumn = "garage"
           storedValue = "OUT"
           outValue = "OUT"
       elseif GetResourceState("op-garages") == "started" then
           storedValue = 0
           outValue = 1
       end

       storedCheck = storedCheck:format(storedColumn)

       local vehicle = MySQL.single.await(
           "SELECT * FROM owned_vehicles WHERE owner = ? AND plate = ? AND " .. storedCheck,
           { GetIdentifier(source), plate, storedValue }
       )

       if not vehicle then
           return
       end

       MySQL.update(
           "UPDATE owned_vehicles SET `" .. storedColumn .. "` = ? WHERE plate = ?",
           { outValue, plate }
       )

       vehicle.model = json.decode(vehicle.vehicle).model

       return vehicle
   end
   ```

{% endtab %}

{% tab title="QB/QBOX" %}

1. Navigate to `lb-phone/server/custom/frameworks/qbcore/vehicles.lua` OR `lb-phone/server/custom/frameworks/qbox/vehicles.lua`
2. Replace function `GetPlayerVehicles` with this:<br>

   ```lua
   function GetPlayerVehicles(source)
       local toSend = {}
       local vehicles = MySQL.query.await(
           "SELECT * FROM player_vehicles WHERE citizenid = ?",
           { GetIdentifier(source) }
       )

       for i = 1, #vehicles do
           local vehicle = vehicles[i] or {}

           if GetResourceState("cd_garage") == "started" then
               vehicle.state = vehicle.in_garage
               vehicle.garage = vehicle.garage_id
               vehicle.type = vehicle.garage_type
           elseif GetResourceState("loaf_garage") == "started" then
               vehicle.state = 1
           elseif GetResourceState("jg-advancedgarages") == "started" then
               vehicle.state = vehicle.in_garage
               vehicle.garage = vehicle.garage_id

               if vehicle.impound == 1 and vehicle.impound_data then
                   vehicle.state = 2
                   vehicle.garage = "Impound"

                   local impoundInfo = json.decode(vehicle.impound_data)

                   vehicle.impoundReason = impoundInfo and {
                       reason = impoundInfo.reason and #impoundInfo.reason > 0 and impoundInfo.reason or nil,
                       retrievable = ConvertJSTimestamp and ConvertJSTimestamp(impoundInfo.retrieval_date) or nil,
                       price = impoundInfo.retrieval_cost,
                       impounder = impoundInfo.charname
                   }
               end
           elseif GetResourceState("qs-advancedgarages") == "started" then
               if vehicle.type == "vehicle" then
                   vehicle.type = "car"
               end
           elseif GetResourceState("op-garages") == "started" then
               debugprint("Using op-garages")
               vehicle.state = not vehicle.state

               local garage = exports['op-garages']:getGarageByIndex(tostring(vehicle.vehicleGarage))
               vehicle.garage = garage and garage.Label or "Garage"

               if vehicle.isTowedOut then
                   local impound = exports['op-garages']:getImpoundByIndex(tostring(vehicle.vehicleImpound))
                   vehicle.state = false
                   vehicle.pound = impound and impound.Label or "Impound"

                   vehicle.impoundReason = {
                       reason = vehicle.impoundReason,
                       retrievable = vehicle.retrievable,
                       price = vehicle.towedPrice,
                       impounder = vehicle.towedOutBy
                   }
               end
           end

           local location = vehicle.garage or "Garage"

           if vehicle.state == 0 then
               location = "out"
           elseif vehicle.state == 1 or vehicle.state == true then
               location = vehicle.garage or "Garage"
           elseif vehicle.state == 2 then
               location = vehicle.garage or "Impound"
           end

           ---@type VehicleData
           local newCar = {
               plate = vehicle.plate,
               type = QB.Shared.Vehicles[vehicle.hash]?.category or vehicle.type or "car",
               location = location,
               impounded = vehicle.state == 2,
               statistics = {
                   engine = math.floor(vehicle.engine / 10 + 0.5),
                   body = math.floor(vehicle.body / 10 + 0.5),
                   fuel = vehicle.fuel
               },
               impoundReason = vehicle.impoundReason
           }

           newCar.model = tonumber(vehicle.hash) --[[@as number]]

           toSend[#toSend+1] = newCar
       end

       return toSend
   end
   ```
3. Replace function `GetVehicle` with this:<br>

   ```lua
   function GetVehicle(source, plate)
       local storedColumn = "state"
       local storedValue = 1
       local outValue = 0

       if GetResourceState("cd_garage") == "started" or GetResourceState("jg-advancedgarages") == "started" then
           storedColumn = "in_garage"
       elseif GetResourceState("op-garages") == "started" then
           storedValue = 0
           outValue = 1
       end

       local vehicle = MySQL.single.await(([[
           SELECT plate, mods, `hash`, fuel
           FROM player_vehicles
           WHERE citizenid = ? AND plate = ? AND `%s` = ?
       ]]):format(storedColumn), { GetIdentifier(source), plate, storedValue })

       if not vehicle then
           return
       end

       MySQL.update(
           "UPDATE player_vehicles SET `" .. storedColumn .. "` = ? WHERE plate = ?",
           { outValue, plate }
       )

       vehicle.model = tonumber(vehicle.hash)

       return vehicle
   end
   ```

{% endtab %}
{% endtabs %}
