Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/add-auto-finance-payment-ability #60

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Config.UsingTarget = false -- If you are using qb-target (uses entity zones to t
Config.Commission = 0.10 -- Percent that goes to sales person from a full car sale - default 10%
Config.FinanceCommission = 0.05 -- Percent that goes to sales person from a finance sale - default 5%
Config.FinanceZone = vector3(-29.53, -1103.67, 26.42) -- Where the finance menu is located
Config.FinanceAuto = true, -- Automatically take the money out of the user's account if they have it. If the time is up, they'll still need to make payment before repo...
Config.FinanceAutoCashPayAllowed = true, -- If they don't have enough money in bank, do we take from their cash?
Config.PaymentWarning = 10 -- time in minutes that player has to make payment before repo - default 10
Config.PaymentInterval = 24 -- time in hours between payment being due - default 24
Config.MinimumDown = 10 -- minimum percentage allowed down - default 10
Expand Down
2 changes: 2 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Config.UsingTarget = GetConvar('UseTarget', 'false') == 'true'
Config.Commission = 0.10 -- Percent that goes to sales person from a full car sale 10%
Config.FinanceCommission = 0.05 -- Percent that goes to sales person from a finance sale 5%
Config.FinanceZone = vector3(-29.53, -1103.67, 26.42)-- Where the finance menu is located
Config.FinanceAuto = true, -- Automatically take the money out of the user's account if they have it. If the time is up, they'll still need to make payment before repo...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be false by default

Config.FinanceAutoCashPayAllowed = true, -- If they don't have enough money in bank, do we take from their cash?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be false by default

Config.PaymentWarning = 10 -- time in minutes that player has to make payment before repo
Config.PaymentInterval = 24 -- time in hours between payment being due
Config.MinimumDown = 10 -- minimum percentage allowed down
Expand Down
2 changes: 2 additions & 0 deletions locales/en.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ local Translations = {
testdrive_timenoti = "You have %{testdrivetime} minutes remaining",
testdrive_complete = "Vehicle test drive complete",
paymentduein = "Your vehicle payment is due within %{time} minutes",
finance_auto_paid_bank = "A payment of $%{payment} from your bank was made because it was due for your vehicle's %{plate} finance option...",
finance_auto_paid_cash = "A payment of $%{payment} was taken from your pockets because it was due for your vehicle's %{plate} finance option...",
command_transfervehicle = "Gift or sell your vehicle",
command_transfervehicle_help = "ID of buyer",
command_transfervehicle_amount = "Sell amount (optionnal)",
Expand Down
61 changes: 53 additions & 8 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,62 @@ end)
RegisterNetEvent('qb-vehicleshop:server:checkFinance', function()
local src = source
local player = QBCore.Functions.GetPlayer(src)
local cash = player.PlayerData.money['cash']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These vars should be moved down into the auto finance loop code block. Otherwise, they won't update whenever a payment is made.

local bank = player.PlayerData.money['bank']
local query = 'SELECT * FROM player_vehicles WHERE citizenid = ? AND balance > 0 AND financetime < 1'
local result = MySQL.query.await(query, {player.PlayerData.citizenid})
if result[1] then
TriggerClientEvent('QBCore:Notify', src, Lang:t('general.paymentduein', {time = Config.PaymentWarning}))
Wait(Config.PaymentWarning * 60000)
local vehicles = MySQL.query.await(query, {player.PlayerData.citizenid})
for _, v in pairs(vehicles) do
local plate = v.plate
MySQL.query('DELETE FROM player_vehicles WHERE plate = @plate', {['@plate'] = plate})
--MySQL.update('UPDATE player_vehicles SET citizenid = ? WHERE plate = ?', {'REPO-'..v.citizenid, plate}) -- Use this if you don't want them to be deleted
TriggerClientEvent('QBCore:Notify', src, Lang:t('error.repossessed', {plate = plate}), 'error')
if not Config.FinanceAuto then
TriggerClientEvent('QBCore:Notify', src, Lang:t('general.paymentduein', {time = Config.PaymentWarning}))
Wait(Config.PaymentWarning * 60000)
local vehicles = MySQL.query.await(query, {player.PlayerData.citizenid})
for _, v in pairs(vehicles) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code duplication here.

local plate = v.plate
MySQL.query('DELETE FROM player_vehicles WHERE plate = @plate', {['@plate'] = plate})
--MySQL.update('UPDATE player_vehicles SET citizenid = ? WHERE plate = ?', {'REPO-'..v.citizenid, plate}) -- Use this if you don't want them to be deleted
TriggerClientEvent('QBCore:Notify', src, Lang:t('error.repossessed', {plate = plate}), 'error')
end
else
-- Automatically take the money out from their account if they have it... If they do not have it, they have certain amount of time until it is repoed...
local allPaid = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this above if not Config.FinanceAuto then ... else and re-use it in the if not Config.FinanceAuto section. This will help reduce code duplication (this for loop is defined twice).

for ind, row in pairs(result) do
local paid = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary var here. Can just assign allPaid = false when a car is detected as not paid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you don't need semi-colons on line endings in lua

local paymentNeeded = result[ind].paymentamount;
local balance = result[ind].balance;
local payLeft = result[ind].paymentsleft;
local plate = result[ind].plate;
local timer = (Config.PaymentInterval * 60)
if (bank >= paymentNeeded) then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if .. elseif can be combined into just a single if statement.

if (bank >= paymentNeeded) or ((cash >= paymentNeeded) and (Config.FinanceAutoCashPayAllowed)) then
    local paymentMethod = (bank >= paymentNeeded) and 'bank' or 'cash'
    player.Functions.RemoveMoney(paymentMethod, paymentNeeded)
    local newBalance, newPaymentsLeft, newPayment = calculateNewFinance(paymentNeeded, {balance = balance, paymentsLeft = payLeft})
    MySQL.update('UPDATE player_vehicles SET balance = ?, paymentamount = ?, paymentsleft = ?, financetime = ? WHERE plate = ?', {newBalance, newPayment, newPaymentsLeft, timer, plate})
    TriggerClientEvent('QBCore:Notify', src, Lang:t('general.finance_auto_paid_' .. paymentMethod, {payment = paymentNeeded, plate = plate}))
else
    allPaid = false
end

-- Payment via bank
paid = true;
player.Functions.RemoveMoney('bank', paymentNeeded);
TriggerClientEvent('QBCore:Notify', src, Lang:t('general.finance_auto_paid_bank', {payment = paymentNeeded, plate = plate}));
elseif (cash >= paymentNeeded) and (Config.FinanceAutoCashPayAllowed) then
-- Payment via cash
paid = true;
player.Functions.RemoveMoney('cash', paymentNeeded);
TriggerClientEvent('QBCore:Notify', src, Lang:t('general.finance_auto_paid_cash', {payment = paymentNeeded, plate = plate}));
end
if paid then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement & the next one can be removed w/ my changes above.

local newBalance, newPaymentsLeft, newPayment = calculateNewFinance(paymentNeeded, {balance = balance, paymentsLeft = payLeft});
MySQL.update('UPDATE player_vehicles SET balance = ?, paymentamount = ?, paymentsleft = ?, financetime = ? WHERE plate = ?', {newBalance, newPayment, newPaymentsLeft, timer, plate});
end
-- They do not have enough money, let them know the vehicle may be revoked...
if not paid then
allPaid = false;
end
end
if not allPaid then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this out of the if not Config.FinanceAuto then ... else code block & re-use it rather than defining this loop twice.

TriggerClientEvent('QBCore:Notify', src, Lang:t('general.paymentduein', {time = Config.PaymentWarning}))
Wait(Config.PaymentWarning * 60000)
local vehicles = MySQL.query.await(query, {player.PlayerData.citizenid})
for _, v in pairs(vehicles) do
local plate = v.plate
MySQL.query('DELETE FROM player_vehicles WHERE plate = @plate', {['@plate'] = plate})
--MySQL.update('UPDATE player_vehicles SET citizenid = ? WHERE plate = ?', {'REPO-'..v.citizenid, plate}) -- Use this if you don't want them to be deleted
TriggerClientEvent('QBCore:Notify', src, Lang:t('error.repossessed', {plate = plate}), 'error')
end
end
end
end
end)
Expand Down
Loading